diff options
author | Fangrui Song <i@maskray.me> | 2022-09-07 23:53:14 -0700 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2022-09-07 23:53:14 -0700 |
commit | 19dc3cff0f771bb8933136ef68e782553e920d04 (patch) | |
tree | 8a5e2daae26709c2930498928787eb831b691f1b /llvm/lib/Support/Compression.cpp | |
parent | b53bccb18d2a437b04f4259cdcced479c909cbfd (diff) | |
download | llvm-19dc3cff0f771bb8933136ef68e782553e920d04.zip llvm-19dc3cff0f771bb8933136ef68e782553e920d04.tar.gz llvm-19dc3cff0f771bb8933136ef68e782553e920d04.tar.bz2 |
[Support] Add llvm::compression::{getReasonIfUnsupported,compress,decompress}
as high-level API on top of `llvm::compression::{zlib,zstd}::*`:
* getReasonIfUnsupported: return nullptr if the specified format is
supported, or (if unsupported) a string like `LLVM was not built with LLVM_ENABLE_ZLIB ...`
* compress: dispatch to zlib::uncompress or zstd::uncompress
* decompress: dispatch to zlib::uncompress or zstd::uncompress
Move `llvm::DebugCompressionType` from MC to Support to avoid Support->MC cyclic
dependency. There are 40+ uses in llvm-project.
Add another enum class `llvm::compression::Format` to represent supported
compression formats, which may be a superset of ELF compression formats.
See D130458 (llvm-objcopy --{,de}compress-debug-sections for zstd) for a use
case.
Link: https://discourse.llvm.org/t/rfc-zstandard-as-a-second-compression-method-to-llvm/63399
("[RFC] Zstandard as a second compression method to LLVM")
Differential Revision: https://reviews.llvm.org/D130506
Diffstat (limited to 'llvm/lib/Support/Compression.cpp')
-rw-r--r-- | llvm/lib/Support/Compression.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/llvm/lib/Support/Compression.cpp b/llvm/lib/Support/Compression.cpp index e8fb715..2e0a2c5 100644 --- a/llvm/lib/Support/Compression.cpp +++ b/llvm/lib/Support/Compression.cpp @@ -27,6 +27,50 @@ using namespace llvm; using namespace llvm::compression; +const char *compression::getReasonIfUnsupported(compression::Format F) { + switch (F) { + case compression::Format::Zlib: + if (zlib::isAvailable()) + return nullptr; + return "LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at " + "build time"; + case compression::Format::Zstd: + if (zstd::isAvailable()) + return nullptr; + return "LLVM was not built with LLVM_ENABLE_ZSTD or did not find zstd at " + "build time"; + } +} + +void compression::compress(Params P, ArrayRef<uint8_t> Input, + SmallVectorImpl<uint8_t> &Output) { + switch (P.Format) { + case compression::Format::Zlib: + zlib::compress(Input, Output, P.Level); + break; + case compression::Format::Zstd: + zstd::compress(Input, Output, P.Level); + break; + } +} + +Error compression::decompress(compression::Format F, ArrayRef<uint8_t> Input, + SmallVectorImpl<uint8_t> &Output, + size_t UncompressedSize) { + switch (F) { + case compression::Format::Zlib: + return zlib::uncompress(Input, Output, UncompressedSize); + case compression::Format::Zstd: + return zstd::uncompress(Input, Output, UncompressedSize); + } +} + +Error compression::decompress(DebugCompressionType T, ArrayRef<uint8_t> Input, + SmallVectorImpl<uint8_t> &Output, + size_t UncompressedSize) { + return decompress(formatFor(T), Input, Output, UncompressedSize); +} + #if LLVM_ENABLE_ZLIB static StringRef convertZlibCodeToString(int Code) { |