Skip to content

Commit

Permalink
[Support] Change compression::zlib::{compress,uncompress} to use uint…
Browse files Browse the repository at this point in the history
…8_t *

It's more natural to use uint8_t * (std::byte needs C++17 and llvm has
too much uint8_t *) and most callers use uint8_t * instead of char *.
The functions are recently moved into `llvm::compression::zlib::`, so
downstream projects need to make adaption anyway.
  • Loading branch information
MaskRay committed Jul 13, 2022
1 parent c916840 commit e690137
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 87 deletions.
14 changes: 8 additions & 6 deletions clang-tools-extra/clangd/index/Serialization.cpp
Expand Up @@ -191,10 +191,11 @@ class StringTableOut {
RawTable.push_back(0);
}
if (llvm::compression::zlib::isAvailable()) {
llvm::SmallString<1> Compressed;
llvm::compression::zlib::compress(RawTable, Compressed);
llvm::SmallVector<uint8_t, 0> Compressed;
llvm::compression::zlib::compress(llvm::arrayRefFromStringRef(RawTable),
Compressed);
write32(RawTable.size(), OS);
OS << Compressed;
OS << llvm::toStringRef(Compressed);
} else {
write32(0, OS); // No compression.
OS << RawTable;
Expand All @@ -220,7 +221,7 @@ llvm::Expected<StringTableIn> readStringTable(llvm::StringRef Data) {
return error("Truncated string table");

llvm::StringRef Uncompressed;
llvm::SmallString<1> UncompressedStorage;
llvm::SmallVector<uint8_t, 0> UncompressedStorage;
if (UncompressedSize == 0) // No compression
Uncompressed = R.rest();
else if (llvm::compression::zlib::isAvailable()) {
Expand All @@ -234,9 +235,10 @@ llvm::Expected<StringTableIn> readStringTable(llvm::StringRef Data) {
R.rest().size(), UncompressedSize);

if (llvm::Error E = llvm::compression::zlib::uncompress(
R.rest(), UncompressedStorage, UncompressedSize))
llvm::arrayRefFromStringRef(R.rest()), UncompressedStorage,
UncompressedSize))
return std::move(E);
Uncompressed = UncompressedStorage;
Uncompressed = toStringRef(UncompressedStorage);
} else
return error("Compressed string table, but zlib is unavailable");

Expand Down
7 changes: 4 additions & 3 deletions clang/lib/Serialization/ASTReader.cpp
Expand Up @@ -1466,14 +1466,15 @@ bool ASTReader::ReadSLocEntry(int ID) {
Error("zlib is not available");
return nullptr;
}
SmallString<0> Uncompressed;
SmallVector<uint8_t, 0> Uncompressed;
if (llvm::Error E = llvm::compression::zlib::uncompress(
Blob, Uncompressed, Record[0])) {
llvm::arrayRefFromStringRef(Blob), Uncompressed, Record[0])) {
Error("could not decompress embedded file contents: " +
llvm::toString(std::move(E)));
return nullptr;
}
return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
return llvm::MemoryBuffer::getMemBufferCopy(
llvm::toStringRef(Uncompressed), Name);
} else if (RecCode == SM_SLOC_BUFFER_BLOB) {
return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
} else {
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/Serialization/ASTWriter.cpp
Expand Up @@ -2000,12 +2000,13 @@ static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,

// Compress the buffer if possible. We expect that almost all PCM
// consumers will not want its contents.
SmallString<0> CompressedBuffer;
SmallVector<uint8_t, 0> CompressedBuffer;
if (llvm::compression::zlib::isAvailable()) {
llvm::compression::zlib::compress(Blob.drop_back(1), CompressedBuffer);
llvm::compression::zlib::compress(
llvm::arrayRefFromStringRef(Blob.drop_back(1)), CompressedBuffer);
RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1};
Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
CompressedBuffer);
llvm::toStringRef(CompressedBuffer));
return;
}

Expand Down
12 changes: 5 additions & 7 deletions lld/ELF/InputSection.cpp
Expand Up @@ -111,18 +111,17 @@ size_t InputSectionBase::getSize() const {

void InputSectionBase::uncompress() const {
size_t size = uncompressedSize;
char *uncompressedBuf;
uint8_t *uncompressedBuf;
{
static std::mutex mu;
std::lock_guard<std::mutex> lock(mu);
uncompressedBuf = bAlloc().Allocate<char>(size);
uncompressedBuf = bAlloc().Allocate<uint8_t>(size);
}

if (Error e = compression::zlib::uncompress(toStringRef(rawData),
uncompressedBuf, size))
if (Error e = compression::zlib::uncompress(rawData, uncompressedBuf, size))
fatal(toString(this) +
": uncompress failed: " + llvm::toString(std::move(e)));
rawData = makeArrayRef((uint8_t *)uncompressedBuf, size);
rawData = makeArrayRef(uncompressedBuf, size);
uncompressedSize = -1;
}

Expand Down Expand Up @@ -1219,8 +1218,7 @@ template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
// to the buffer.
if (uncompressedSize >= 0) {
size_t size = uncompressedSize;
if (Error e = compression::zlib::uncompress(toStringRef(rawData),
(char *)buf, size))
if (Error e = compression::zlib::uncompress(rawData, buf, size))
fatal(toString(this) +
": uncompress failed: " + llvm::toString(std::move(e)));
uint8_t *bufEnd = buf + size;
Expand Down
3 changes: 1 addition & 2 deletions lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
Expand Up @@ -3386,8 +3386,7 @@ size_t ObjectFileELF::ReadSectionData(Section *section,
auto buffer_sp =
std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0);
if (auto error = Decompressor->decompress(
{reinterpret_cast<char *>(buffer_sp->GetBytes()),
size_t(buffer_sp->GetByteSize())})) {
{buffer_sp->GetBytes(), size_t(buffer_sp->GetByteSize())})) {
GetModule()->ReportWarning(
"Decompression of section '%s' failed: %s",
section->GetName().GetCString(),
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/Object/Decompressor.h
Expand Up @@ -33,12 +33,12 @@ class Decompressor {
/// @param Out Destination buffer.
template <class T> Error resizeAndDecompress(T &Out) {
Out.resize(DecompressedSize);
return decompress({Out.data(), (size_t)DecompressedSize});
return decompress({(uint8_t *)Out.data(), (size_t)DecompressedSize});
}

/// Uncompress section data to raw buffer provided.
/// @param Buffer Destination buffer.
Error decompress(MutableArrayRef<char> Buffer);
Error decompress(MutableArrayRef<uint8_t> Buffer);

/// Return memory buffer size required for decompression.
uint64_t getDecompressedSize() { return DecompressedSize; }
Expand Down
10 changes: 6 additions & 4 deletions llvm/include/llvm/Support/Compression.h
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_SUPPORT_COMPRESSION_H
#define LLVM_SUPPORT_COMPRESSION_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/DataTypes.h"

namespace llvm {
Expand All @@ -30,14 +31,15 @@ constexpr int BestSizeCompression = 9;

bool isAvailable();

void compress(StringRef InputBuffer, SmallVectorImpl<char> &CompressedBuffer,
void compress(ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &CompressedBuffer,
int Level = DefaultCompression);

Error uncompress(StringRef InputBuffer, char *UncompressedBuffer,
Error uncompress(ArrayRef<uint8_t> Input, uint8_t *UncompressedBuffer,
size_t &UncompressedSize);

Error uncompress(StringRef InputBuffer,
SmallVectorImpl<char> &UncompressedBuffer,
Error uncompress(ArrayRef<uint8_t> Input,
SmallVectorImpl<uint8_t> &UncompressedBuffer,
size_t UncompressedSize);

} // End of namespace zlib
Expand Down
11 changes: 6 additions & 5 deletions llvm/lib/MC/ELFObjectWriter.cpp
Expand Up @@ -145,7 +145,7 @@ struct ELFWriter {
uint64_t align(unsigned Alignment);

bool maybeWriteCompression(uint64_t Size,
SmallVectorImpl<char> &CompressedContents,
SmallVectorImpl<uint8_t> &CompressedContents,
bool ZLibStyle, unsigned Alignment);

public:
Expand Down Expand Up @@ -819,7 +819,7 @@ MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,

// Include the debug info compression header.
bool ELFWriter::maybeWriteCompression(
uint64_t Size, SmallVectorImpl<char> &CompressedContents, bool ZLibStyle,
uint64_t Size, SmallVectorImpl<uint8_t> &CompressedContents, bool ZLibStyle,
unsigned Alignment) {
if (ZLibStyle) {
uint64_t HdrSize =
Expand Down Expand Up @@ -875,9 +875,10 @@ void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
raw_svector_ostream VecOS(UncompressedData);
Asm.writeSectionData(VecOS, &Section, Layout);

SmallVector<char, 128> CompressedContents;
SmallVector<uint8_t, 128> CompressedContents;
compression::zlib::compress(
StringRef(UncompressedData.data(), UncompressedData.size()),
makeArrayRef(reinterpret_cast<uint8_t *>(UncompressedData.data()),
UncompressedData.size()),
CompressedContents);

bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z;
Expand All @@ -897,7 +898,7 @@ void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
// Add "z" prefix to section name. This is zlib-gnu style.
MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str());
}
W.OS << CompressedContents;
W.OS << toStringRef(CompressedContents);
}

void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Expand Down
13 changes: 4 additions & 9 deletions llvm/lib/ObjCopy/ELF/ELFObject.cpp
Expand Up @@ -463,11 +463,9 @@ Error ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
? (ZlibGnuMagic.size() + sizeof(Sec.Size))
: sizeof(Elf_Chdr_Impl<ELFT>);

StringRef CompressedContent(
reinterpret_cast<const char *>(Sec.OriginalData.data()) + DataOffset,
Sec.OriginalData.size() - DataOffset);

SmallVector<char, 128> DecompressedContent;
ArrayRef<uint8_t> CompressedContent(Sec.OriginalData.data() + DataOffset,
Sec.OriginalData.size() - DataOffset);
SmallVector<uint8_t, 128> DecompressedContent;
if (Error Err =
compression::zlib::uncompress(CompressedContent, DecompressedContent,
static_cast<size_t>(Sec.Size)))
Expand Down Expand Up @@ -545,10 +543,7 @@ CompressedSection::CompressedSection(const SectionBase &Sec,
DebugCompressionType CompressionType)
: SectionBase(Sec), CompressionType(CompressionType),
DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
compression::zlib::compress(
StringRef(reinterpret_cast<const char *>(OriginalData.data()),
OriginalData.size()),
CompressedData);
compression::zlib::compress(OriginalData, CompressedData);

assert(CompressionType != DebugCompressionType::None);
Flags |= ELF::SHF_COMPRESSED;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/ObjCopy/ELF/ELFObject.h
Expand Up @@ -539,7 +539,7 @@ class CompressedSection : public SectionBase {
DebugCompressionType CompressionType;
uint64_t DecompressedSize;
uint64_t DecompressedAlign;
SmallVector<char, 128> CompressedData;
SmallVector<uint8_t, 128> CompressedData;

public:
CompressedSection(const SectionBase &Sec,
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Object/Decompressor.cpp
Expand Up @@ -92,7 +92,8 @@ bool Decompressor::isCompressedELFSection(uint64_t Flags, StringRef Name) {
return (Flags & ELF::SHF_COMPRESSED) || isGnuStyle(Name);
}

Error Decompressor::decompress(MutableArrayRef<char> Buffer) {
Error Decompressor::decompress(MutableArrayRef<uint8_t> Buffer) {
size_t Size = Buffer.size();
return compression::zlib::uncompress(SectionData, Buffer.data(), Size);
return compression::zlib::uncompress(arrayRefFromStringRef(SectionData),
Buffer.data(), Size);
}
10 changes: 5 additions & 5 deletions llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp
Expand Up @@ -124,21 +124,21 @@ Error RawCoverageFilenamesReader::read(CovMapVersion Version) {
coveragemap_error::decompression_failed);

// Allocate memory for the decompressed filenames.
SmallVector<char, 0> StorageBuf;
SmallVector<uint8_t, 0> StorageBuf;

// Read compressed filenames.
StringRef CompressedFilenames = Data.substr(0, CompressedLen);
Data = Data.substr(CompressedLen);
auto Err = compression::zlib::uncompress(CompressedFilenames, StorageBuf,
UncompressedLen);
auto Err = compression::zlib::uncompress(
arrayRefFromStringRef(CompressedFilenames), StorageBuf,
UncompressedLen);
if (Err) {
consumeError(std::move(Err));
return make_error<CoverageMapError>(
coveragemap_error::decompression_failed);
}

StringRef UncompressedFilenames(StorageBuf.data(), StorageBuf.size());
RawCoverageFilenamesReader Delegate(UncompressedFilenames, Filenames,
RawCoverageFilenamesReader Delegate(toStringRef(StorageBuf), Filenames,
CompilationDir);
return Delegate.readUncompressed(Version, NumFilenames);
}
Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp
Expand Up @@ -46,11 +46,12 @@ void CoverageFilenamesSectionWriter::write(raw_ostream &OS, bool Compress) {
}
}

SmallString<128> CompressedStr;
SmallVector<uint8_t, 128> CompressedStr;
bool doCompression = Compress && compression::zlib::isAvailable() &&
DoInstrProfNameCompression;
if (doCompression)
compression::zlib::compress(FilenamesStr, CompressedStr,
compression::zlib::compress(arrayRefFromStringRef(FilenamesStr),
CompressedStr,
compression::zlib::BestSizeCompression);

// ::= <num-filenames>
Expand All @@ -60,7 +61,7 @@ void CoverageFilenamesSectionWriter::write(raw_ostream &OS, bool Compress) {
encodeULEB128(Filenames.size(), OS);
encodeULEB128(FilenamesStr.size(), OS);
encodeULEB128(doCompression ? CompressedStr.size() : 0U, OS);
OS << (doCompression ? CompressedStr.str() : StringRef(FilenamesStr));
OS << (doCompression ? toStringRef(CompressedStr) : StringRef(FilenamesStr));
}

namespace {
Expand Down
19 changes: 8 additions & 11 deletions llvm/lib/ProfileData/InstrProf.cpp
Expand Up @@ -464,13 +464,13 @@ Error collectPGOFuncNameStrings(ArrayRef<std::string> NameStrs,
return WriteStringToResult(0, UncompressedNameStrings);
}

SmallString<128> CompressedNameStrings;
compression::zlib::compress(StringRef(UncompressedNameStrings),
SmallVector<uint8_t, 128> CompressedNameStrings;
compression::zlib::compress(arrayRefFromStringRef(UncompressedNameStrings),
CompressedNameStrings,
compression::zlib::BestSizeCompression);

return WriteStringToResult(CompressedNameStrings.size(),
CompressedNameStrings);
toStringRef(CompressedNameStrings));
}

StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
Expand Down Expand Up @@ -500,23 +500,20 @@ Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
uint64_t CompressedSize = decodeULEB128(P, &N);
P += N;
bool isCompressed = (CompressedSize != 0);
SmallString<128> UncompressedNameStrings;
SmallVector<uint8_t, 128> UncompressedNameStrings;
StringRef NameStrings;
if (isCompressed) {
if (!llvm::compression::zlib::isAvailable())
return make_error<InstrProfError>(instrprof_error::zlib_unavailable);

StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
CompressedSize);
if (Error E = compression::zlib::uncompress(CompressedNameStrings,
UncompressedNameStrings,
UncompressedSize)) {
if (Error E = compression::zlib::uncompress(
makeArrayRef(P, CompressedSize), UncompressedNameStrings,
UncompressedSize)) {
consumeError(std::move(E));
return make_error<InstrProfError>(instrprof_error::uncompress_failed);
}
P += CompressedSize;
NameStrings = StringRef(UncompressedNameStrings.data(),
UncompressedNameStrings.size());
NameStrings = toStringRef(UncompressedNameStrings);
} else {
NameStrings =
StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
Expand Down
8 changes: 3 additions & 5 deletions llvm/lib/ProfileData/SampleProfReader.cpp
Expand Up @@ -880,12 +880,10 @@ std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
if (!llvm::compression::zlib::isAvailable())
return sampleprof_error::zlib_unavailable;

StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
*CompressSize);
char *Buffer = Allocator.Allocate<char>(DecompressBufSize);
uint8_t *Buffer = Allocator.Allocate<uint8_t>(DecompressBufSize);
size_t UCSize = DecompressBufSize;
llvm::Error E =
compression::zlib::uncompress(CompressedStrings, Buffer, UCSize);
llvm::Error E = compression::zlib::uncompress(
makeArrayRef(Data, *CompressSize), Buffer, UCSize);
if (E)
return sampleprof_error::uncompress_failed;
DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/ProfileData/SampleProfWriter.cpp
Expand Up @@ -85,12 +85,13 @@ std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
if (UncompressedStrings.size() == 0)
return sampleprof_error::success;
auto &OS = *OutputStream;
SmallString<128> CompressedStrings;
compression::zlib::compress(UncompressedStrings, CompressedStrings,
SmallVector<uint8_t, 128> CompressedStrings;
compression::zlib::compress(arrayRefFromStringRef(UncompressedStrings),
CompressedStrings,
compression::zlib::BestSizeCompression);
encodeULEB128(UncompressedStrings.size(), OS);
encodeULEB128(CompressedStrings.size(), OS);
OS << CompressedStrings.str();
OS << toStringRef(CompressedStrings);
UncompressedStrings.clear();
return sampleprof_error::success;
}
Expand Down

0 comments on commit e690137

Please sign in to comment.