diff --git a/clang-tools-extra/clangd/index/Serialization.cpp b/clang-tools-extra/clangd/index/Serialization.cpp --- a/clang-tools-extra/clangd/index/Serialization.cpp +++ b/clang-tools-extra/clangd/index/Serialization.cpp @@ -190,9 +190,9 @@ RawTable.append(std::string(S)); RawTable.push_back(0); } - if (llvm::zlib::isAvailable()) { + if (llvm::compression::serialize::isAvailable()) { llvm::SmallString<1> Compressed; - llvm::zlib::compress(RawTable, Compressed); + llvm::compression::serialize::compress(RawTable, Compressed); write32(RawTable.size(), OS); OS << Compressed; } else { @@ -223,7 +223,7 @@ llvm::SmallString<1> UncompressedStorage; if (UncompressedSize == 0) // No compression Uncompressed = R.rest(); - else if (llvm::zlib::isAvailable()) { + else if (llvm::compression::serialize::isAvailable()) { // Don't allocate a massive buffer if UncompressedSize was corrupted // This is effective for sharded index, but not big monolithic ones, as // once compressed size reaches 4MB nothing can be ruled out. @@ -233,12 +233,13 @@ return error("Bad stri table: uncompress {0} -> {1} bytes is implausible", R.rest().size(), UncompressedSize); - if (llvm::Error E = llvm::zlib::uncompress(R.rest(), UncompressedStorage, - UncompressedSize)) + if (llvm::Error E = llvm::compression::serialize::uncompress( + R.rest(), UncompressedStorage, UncompressedSize)) return std::move(E); Uncompressed = UncompressedStorage; } else - return error("Compressed string table, but zlib is unavailable"); + return error("Compressed string table, but " + + compression::serialize::AlgorithmName + " is unavailable"); StringTableIn Table; llvm::StringSaver Saver(Table.Arena); diff --git a/clang-tools-extra/clangd/unittests/SerializationTests.cpp b/clang-tools-extra/clangd/unittests/SerializationTests.cpp --- a/clang-tools-extra/clangd/unittests/SerializationTests.cpp +++ b/clang-tools-extra/clangd/unittests/SerializationTests.cpp @@ -391,8 +391,8 @@ // Check we detect invalid string table size size without allocating it first. // If this detection fails, the test should allocate a huge array and crash. TEST(SerializationTest, NoCrashOnBadStringTableSize) { - if (!llvm::zlib::isAvailable()) { - log("skipping test, no zlib"); + if (!llvm::compression::serialize::isAvailable()) { + log("skipping test, no " + compression::serialize::AlgorithmName); return; } diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -1144,7 +1144,7 @@ if (Value == "none") { CmdArgs.push_back("--compress-debug-sections=none"); } else if (Value == "zlib") { - if (llvm::zlib::isAvailable()) { + if (llvm::compression::zlib::isAvailable()) { CmdArgs.push_back( Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); } else { diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -1462,13 +1462,14 @@ unsigned RecCode = MaybeRecCode.get(); if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { - if (!llvm::zlib::isAvailable()) { - Error("zlib is not available"); + if (!llvm::compression::serialize::isAvailable()) { + Error(llvm::compression::serialize::AlgorithmName + + " is not available"); return nullptr; } SmallString<0> Uncompressed; - if (llvm::Error E = - llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { + if (llvm::Error E = llvm::compression::serialize::uncompress( + Blob, Uncompressed, Record[0])) { Error("could not decompress embedded file contents: " + llvm::toString(std::move(E))); return nullptr; diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -2001,8 +2001,8 @@ // Compress the buffer if possible. We expect that almost all PCM // consumers will not want its contents. SmallString<0> CompressedBuffer; - if (llvm::zlib::isAvailable()) { - llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer); + if (llvm::compression::serialize::isAvailable()) { + llvm::compression::serialize::compress(Blob.drop_back(1), CompressedBuffer); RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED, Blob.size() - 1}; Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record, CompressedBuffer); diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -953,7 +953,7 @@ return false; if (s != "zlib") error("unknown --compress-debug-sections value: " + s); - if (!zlib::isAvailable()) + if (!compression::zlib::isAvailable()) error("--compress-debug-sections: zlib is not available"); return true; } diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -73,9 +73,9 @@ // If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no // longer supported. if (flags & SHF_COMPRESSED) { - if (!zlib::isAvailable()) - error(toString(file) + ": contains a compressed section, " + - "but zlib is not available"); + if (!compression::zlib::isAvailable()) + error(toString(file) + ": contains a compressed section, " + "but " + + compression::zlib::AlgorithmName + " is not available"); invokeELFT(parseCompressedHeader); } } @@ -122,7 +122,8 @@ uncompressedBuf = bAlloc().Allocate(size); } - if (Error e = zlib::uncompress(toStringRef(rawData), uncompressedBuf, size)) + if (Error e = compression::zlib::uncompress(toStringRef(rawData), + uncompressedBuf, size)) fatal(toString(this) + ": uncompress failed: " + llvm::toString(std::move(e))); rawData = makeArrayRef((uint8_t *)uncompressedBuf, size); @@ -1213,7 +1214,8 @@ // to the buffer. if (uncompressedSize >= 0) { size_t size = uncompressedSize; - if (Error e = zlib::uncompress(toStringRef(rawData), (char *)buf, size)) + if (Error e = compression::zlib::uncompress(toStringRef(rawData), + (char *)buf, size)) fatal(toString(this) + ": uncompress failed: " + llvm::toString(std::move(e))); uint8_t *bufEnd = buf + size; diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst --- a/llvm/docs/ReleaseNotes.rst +++ b/llvm/docs/ReleaseNotes.rst @@ -180,6 +180,14 @@ constant fold the operands if possible and create an instruction otherwise: * ``LLVMConstExtractValue`` +* Refactor compression namespaces across the project, making way for a possible + introduction of alternatives to zlib compression in the llvm toolchain. + Changes are as follows: + * Relocate the ``llvm::zlib`` namespace to ``llvm::compression::zlib``. + * Code that explictly needs ``zlib`` compression (IE zlib elf debug sections) should use ``llvm::compression::zlib``. + * Code interfacing with compressed profile data should use ``llvm::compression::profile``. + * Code interfacing with compressed serialized data should use ``llvm::compression::serialize``. + Changes to the Go bindings -------------------------- diff --git a/llvm/include/llvm/Support/Compression.h b/llvm/include/llvm/Support/Compression.h --- a/llvm/include/llvm/Support/Compression.h +++ b/llvm/include/llvm/Support/Compression.h @@ -20,8 +20,11 @@ class Error; class StringRef; +namespace compression { + namespace zlib { +static constexpr std::string AlgorithmName = "zlib"; static constexpr int NoCompression = 0; static constexpr int BestSpeedCompression = 1; static constexpr int DefaultCompression = 6; @@ -41,7 +44,13 @@ uint32_t crc32(StringRef Buffer); -} // End of namespace zlib +} // End of namespace zlib + +namespace profile = llvm::compression::zlib; + +namespace serialize = llvm::compression::zlib; + +} // End of namespace compression } // End of namespace llvm diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -876,8 +876,9 @@ Asm.writeSectionData(VecOS, &Section, Layout); SmallVector CompressedContents; - zlib::compress(StringRef(UncompressedData.data(), UncompressedData.size()), - CompressedContents); + compression::zlib::compress( + StringRef(UncompressedData.data(), UncompressedData.size()), + CompressedContents); bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z; if (!maybeWriteCompression(UncompressedData.size(), CompressedContents, diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp --- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp +++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp @@ -468,8 +468,9 @@ Sec.OriginalData.size() - DataOffset); SmallVector DecompressedContent; - if (Error Err = zlib::uncompress(CompressedContent, DecompressedContent, - static_cast(Sec.Size))) + if (Error Err = + compression::zlib::uncompress(CompressedContent, DecompressedContent, + static_cast(Sec.Size))) return createStringError(errc::invalid_argument, "'" + Sec.Name + "': " + toString(std::move(Err))); @@ -544,9 +545,10 @@ DebugCompressionType CompressionType) : SectionBase(Sec), CompressionType(CompressionType), DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) { - zlib::compress(StringRef(reinterpret_cast(OriginalData.data()), - OriginalData.size()), - CompressedData); + compression::zlib::compress( + StringRef(reinterpret_cast(OriginalData.data()), + OriginalData.size()), + CompressedData); assert(CompressionType != DebugCompressionType::None); Flags |= ELF::SHF_COMPRESSED; diff --git a/llvm/lib/Object/Decompressor.cpp b/llvm/lib/Object/Decompressor.cpp --- a/llvm/lib/Object/Decompressor.cpp +++ b/llvm/lib/Object/Decompressor.cpp @@ -19,7 +19,7 @@ Expected Decompressor::create(StringRef Name, StringRef Data, bool IsLE, bool Is64Bit) { - if (!zlib::isAvailable()) + if (!compression::zlib::isAvailable()) return createError("zlib is not available"); Decompressor D(Data); @@ -94,5 +94,5 @@ Error Decompressor::decompress(MutableArrayRef Buffer) { size_t Size = Buffer.size(); - return zlib::uncompress(SectionData, Buffer.data(), Size); + return compression::zlib::uncompress(SectionData, Buffer.data(), Size); } diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp --- a/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -119,7 +119,7 @@ return Err; if (CompressedLen > 0) { - if (!zlib::isAvailable()) + if (!compression::profile::isAvailable()) return make_error( coveragemap_error::decompression_failed); @@ -129,8 +129,8 @@ // Read compressed filenames. StringRef CompressedFilenames = Data.substr(0, CompressedLen); Data = Data.substr(CompressedLen); - auto Err = - zlib::uncompress(CompressedFilenames, StorageBuf, UncompressedLen); + auto Err = compression::profile::uncompress(CompressedFilenames, StorageBuf, + UncompressedLen); if (Err) { consumeError(std::move(Err)); return make_error( diff --git a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp --- a/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp +++ b/llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp @@ -47,10 +47,11 @@ } SmallString<128> CompressedStr; - bool doCompression = - Compress && zlib::isAvailable() && DoInstrProfNameCompression; + bool doCompression = Compress && compression::profile::isAvailable() && + DoInstrProfNameCompression; if (doCompression) - zlib::compress(FilenamesStr, CompressedStr, zlib::BestSizeCompression); + compression::profile::compress(FilenamesStr, CompressedStr, + compression::profile::BestSizeCompression); // ::= // diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp --- a/llvm/lib/ProfileData/InstrProf.cpp +++ b/llvm/lib/ProfileData/InstrProf.cpp @@ -150,8 +150,9 @@ OS << "empty raw profile file"; break; case instrprof_error::zlib_unavailable: - OS << "profile uses zlib compression but the profile reader was built " - "without zlib support"; + OS << ("profile uses " + compression::profile::AlgorithmName + + " compression but the profile reader was built " + "without " + + compression::profile::AlgorithmName + " support"); break; } @@ -467,8 +468,9 @@ } SmallString<128> CompressedNameStrings; - zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings, - zlib::BestSizeCompression); + compression::profile::compress(StringRef(UncompressedNameStrings), + CompressedNameStrings, + compression::profile::BestSizeCompression); return WriteStringToResult(CompressedNameStrings.size(), CompressedNameStrings); @@ -488,7 +490,7 @@ NameStrs.push_back(std::string(getPGOFuncNameVarInitializer(NameVar))); } return collectPGOFuncNameStrings( - NameStrs, zlib::isAvailable() && doCompression, Result); + NameStrs, compression::profile::isAvailable() && doCompression, Result); } Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { @@ -504,14 +506,14 @@ SmallString<128> UncompressedNameStrings; StringRef NameStrings; if (isCompressed) { - if (!llvm::zlib::isAvailable()) + if (!llvm::compression::profile::isAvailable()) return make_error(instrprof_error::zlib_unavailable); StringRef CompressedNameStrings(reinterpret_cast(P), CompressedSize); - if (Error E = - zlib::uncompress(CompressedNameStrings, UncompressedNameStrings, - UncompressedSize)) { + if (Error E = compression::profile::uncompress(CompressedNameStrings, + UncompressedNameStrings, + UncompressedSize)) { consumeError(std::move(E)); return make_error(instrprof_error::uncompress_failed); } diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp --- a/llvm/lib/ProfileData/SampleProfReader.cpp +++ b/llvm/lib/ProfileData/SampleProfReader.cpp @@ -877,7 +877,7 @@ if (std::error_code EC = CompressSize.getError()) return EC; - if (!llvm::zlib::isAvailable()) + if (!llvm::compression::profile::isAvailable()) return sampleprof_error::zlib_unavailable; StringRef CompressedStrings(reinterpret_cast(Data), @@ -885,7 +885,7 @@ char *Buffer = Allocator.Allocate(DecompressBufSize); size_t UCSize = DecompressBufSize; llvm::Error E = - zlib::uncompress(CompressedStrings, Buffer, UCSize); + compression::profile::uncompress(CompressedStrings, Buffer, UCSize); if (E) return sampleprof_error::uncompress_failed; DecompressBuf = reinterpret_cast(Buffer); diff --git a/llvm/lib/ProfileData/SampleProfWriter.cpp b/llvm/lib/ProfileData/SampleProfWriter.cpp --- a/llvm/lib/ProfileData/SampleProfWriter.cpp +++ b/llvm/lib/ProfileData/SampleProfWriter.cpp @@ -78,7 +78,7 @@ } std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() { - if (!llvm::zlib::isAvailable()) + if (!llvm::compression::profile::isAvailable()) return sampleprof_error::zlib_unavailable; std::string &UncompressedStrings = static_cast(LocalBufStream.get())->str(); @@ -86,8 +86,8 @@ return sampleprof_error::success; auto &OS = *OutputStream; SmallString<128> CompressedStrings; - zlib::compress(UncompressedStrings, CompressedStrings, - zlib::BestSizeCompression); + compression::profile::compress(UncompressedStrings, CompressedStrings, + compression::profile::BestSizeCompression); encodeULEB128(UncompressedStrings.size(), OS); encodeULEB128(CompressedStrings.size(), OS); OS << CompressedStrings.str(); diff --git a/llvm/lib/Support/Compression.cpp b/llvm/lib/Support/Compression.cpp --- a/llvm/lib/Support/Compression.cpp +++ b/llvm/lib/Support/Compression.cpp @@ -23,10 +23,9 @@ using namespace llvm; +using namespace llvm::compression; + #if LLVM_ENABLE_ZLIB -static Error createError(StringRef Err) { - return make_error(Err, inconvertibleErrorCode()); -} static StringRef convertZlibCodeToString(int Code) { switch (Code) { @@ -70,15 +69,17 @@ // Tell MemorySanitizer that zlib output buffer is fully initialized. // This avoids a false report when running LLVM with uninstrumented ZLib. __msan_unpoison(UncompressedBuffer, UncompressedSize); - return Res ? createError(convertZlibCodeToString(Res)) : Error::success(); + return Res ? make_error(convertZlibCodeToString(Res), + inconvertibleErrorCode()) + : Error::success(); } Error zlib::uncompress(StringRef InputBuffer, SmallVectorImpl &UncompressedBuffer, size_t UncompressedSize) { UncompressedBuffer.resize_for_overwrite(UncompressedSize); - Error E = - uncompress(InputBuffer, UncompressedBuffer.data(), UncompressedSize); + Error E = zlib::uncompress(InputBuffer, UncompressedBuffer.data(), + UncompressedSize); UncompressedBuffer.truncate(UncompressedSize); return E; } diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp --- a/llvm/tools/llvm-mc/llvm-mc.cpp +++ b/llvm/tools/llvm-mc/llvm-mc.cpp @@ -403,7 +403,7 @@ MAI->setRelaxELFRelocations(RelaxELFRel); if (CompressDebugSections != DebugCompressionType::None) { - if (!zlib::isAvailable()) { + if (!compression::zlib::isAvailable()) { WithColor::error(errs(), ProgName) << "build tools with zlib to enable -compress-debug-sections"; return 1; diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp --- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp +++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp @@ -739,7 +739,7 @@ .str() .c_str()); } - if (!zlib::isAvailable()) + if (!compression::zlib::isAvailable()) return createStringError( errc::invalid_argument, "LLVM was not compiled with LLVM_ENABLE_ZLIB: can not compress"); @@ -998,7 +998,7 @@ "--decompress-debug-sections"); } - if (Config.DecompressDebugSections && !zlib::isAvailable()) + if (Config.DecompressDebugSections && !compression::zlib::isAvailable()) return createStringError( errc::invalid_argument, "LLVM was not compiled with LLVM_ENABLE_ZLIB: cannot decompress"); diff --git a/llvm/unittests/ProfileData/InstrProfTest.cpp b/llvm/unittests/ProfileData/InstrProfTest.cpp --- a/llvm/unittests/ProfileData/InstrProfTest.cpp +++ b/llvm/unittests/ProfileData/InstrProfTest.cpp @@ -1146,17 +1146,19 @@ for (bool DoCompression : {false, true}) { // Compressing: std::string FuncNameStrings1; - EXPECT_THAT_ERROR(collectPGOFuncNameStrings( - FuncNames1, (DoCompression && zlib::isAvailable()), - FuncNameStrings1), - Succeeded()); + EXPECT_THAT_ERROR( + collectPGOFuncNameStrings( + FuncNames1, (DoCompression && compression::profile::isAvailable()), + FuncNameStrings1), + Succeeded()); // Compressing: std::string FuncNameStrings2; - EXPECT_THAT_ERROR(collectPGOFuncNameStrings( - FuncNames2, (DoCompression && zlib::isAvailable()), - FuncNameStrings2), - Succeeded()); + EXPECT_THAT_ERROR( + collectPGOFuncNameStrings( + FuncNames2, (DoCompression && compression::profile::isAvailable()), + FuncNameStrings2), + Succeeded()); for (int Padding = 0; Padding < 2; Padding++) { // Join with paddings : diff --git a/llvm/unittests/Support/CompressionTest.cpp b/llvm/unittests/Support/CompressionTest.cpp --- a/llvm/unittests/Support/CompressionTest.cpp +++ b/llvm/unittests/Support/CompressionTest.cpp @@ -19,6 +19,8 @@ using namespace llvm; +using namespace llvm::compression; + namespace { #if LLVM_ENABLE_ZLIB