[clang] [llvm] [HIP] change compress level (PR #83605)

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 1 10:32:36 PST 2024


https://github.com/yxsamliu updated https://github.com/llvm/llvm-project/pull/83605

>From 6b5687e16c826053d690b08b6fe714e055905479 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Fri, 1 Mar 2024 13:16:45 -0500
Subject: [PATCH] [HIP] change compress level

Change compression level to 20 for zstd better
compression rate.
---
 clang/lib/Driver/OffloadBundler.cpp            | 17 +++++++++++++----
 clang/test/Driver/clang-offload-bundler-zlib.c |  5 +++--
 clang/test/Driver/clang-offload-bundler-zstd.c |  3 ++-
 llvm/include/llvm/Support/Compression.h        |  1 +
 4 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Driver/OffloadBundler.cpp b/clang/lib/Driver/OffloadBundler.cpp
index 99a34d25cfcd56..cd6eb5fa3a31c8 100644
--- a/clang/lib/Driver/OffloadBundler.cpp
+++ b/clang/lib/Driver/OffloadBundler.cpp
@@ -942,12 +942,19 @@ CompressedOffloadBundle::compress(const llvm::MemoryBuffer &Input,
       Input.getBuffer().size());
 
   llvm::compression::Format CompressionFormat;
+  int Level;
 
-  if (llvm::compression::zstd::isAvailable())
+  if (llvm::compression::zstd::isAvailable()) {
     CompressionFormat = llvm::compression::Format::Zstd;
-  else if (llvm::compression::zlib::isAvailable())
+    // Use a high zstd compress level by default for better size reduction.
+    const int DefaultZstdLevel = 20;
+    Level = DefaultZstdLevel;
+  } else if (llvm::compression::zlib::isAvailable()) {
     CompressionFormat = llvm::compression::Format::Zlib;
-  else
+    // Use default level for zlib since higher level does not have significant
+    // improvement.
+    Level = llvm::compression::zlib::DefaultCompression;
+  } else
     return createStringError(llvm::inconvertibleErrorCode(),
                              "Compression not supported");
 
@@ -955,7 +962,8 @@ CompressedOffloadBundle::compress(const llvm::MemoryBuffer &Input,
                             ClangOffloadBundlerTimerGroup);
   if (Verbose)
     CompressTimer.startTimer();
-  llvm::compression::compress(CompressionFormat, BufferUint8, CompressedBuffer);
+  llvm::compression::compress({CompressionFormat, Level}, BufferUint8,
+                              CompressedBuffer);
   if (Verbose)
     CompressTimer.stopTimer();
 
@@ -980,6 +988,7 @@ CompressedOffloadBundle::compress(const llvm::MemoryBuffer &Input,
         CompressionFormat == llvm::compression::Format::Zstd ? "zstd" : "zlib";
     llvm::errs() << "Compressed bundle format version: " << Version << "\n"
                  << "Compression method used: " << MethodUsed << "\n"
+                 << "Compression level: " << Level << "\n"
                  << "Binary size before compression: " << UncompressedSize
                  << " bytes\n"
                  << "Binary size after compression: " << CompressedBuffer.size()
diff --git a/clang/test/Driver/clang-offload-bundler-zlib.c b/clang/test/Driver/clang-offload-bundler-zlib.c
index a57ee6da9a86a6..f8ffb5db3368fe 100644
--- a/clang/test/Driver/clang-offload-bundler-zlib.c
+++ b/clang/test/Driver/clang-offload-bundler-zlib.c
@@ -1,4 +1,4 @@
-// REQUIRES: zlib
+// REQUIRES: zlib && !zstd
 // REQUIRES: x86-registered-target
 // UNSUPPORTED: target={{.*}}-darwin{{.*}}, target={{.*}}-aix{{.*}}
 
@@ -34,7 +34,8 @@
 // RUN: diff %t.tgt2 %t.res.tgt2
 
 //
-// COMPRESS: Compression method used:
+// COMPRESS: Compression method used: zlib
+// COMPRESS: Compression level: 6
 // DECOMPRESS: Decompression method:
 // NOHOST-NOT: host-
 // NOHOST-DAG: hip-amdgcn-amd-amdhsa--gfx900
diff --git a/clang/test/Driver/clang-offload-bundler-zstd.c b/clang/test/Driver/clang-offload-bundler-zstd.c
index 3b577d4d166a3f..200634e05fa145 100644
--- a/clang/test/Driver/clang-offload-bundler-zstd.c
+++ b/clang/test/Driver/clang-offload-bundler-zstd.c
@@ -31,7 +31,8 @@
 // RUN: diff %t.tgt1 %t.res.tgt1
 // RUN: diff %t.tgt2 %t.res.tgt2
 //
-// COMPRESS: Compression method used
+// COMPRESS: Compression method used: zstd
+// COMPRESS: Compression level: 20
 // DECOMPRESS: Decompression method
 // NOHOST-NOT: host-
 // NOHOST-DAG: hip-amdgcn-amd-amdhsa--gfx900
diff --git a/llvm/include/llvm/Support/Compression.h b/llvm/include/llvm/Support/Compression.h
index c3ba3274d6ed87..f30da1ba3874e6 100644
--- a/llvm/include/llvm/Support/Compression.h
+++ b/llvm/include/llvm/Support/Compression.h
@@ -94,6 +94,7 @@ struct Params {
   constexpr Params(Format F)
       : format(F), level(F == Format::Zlib ? zlib::DefaultCompression
                                            : zstd::DefaultCompression) {}
+  constexpr Params(Format F, int L) : format(F), level(L) {}
   Params(DebugCompressionType Type) : Params(formatFor(Type)) {}
 
   Format format;



More information about the cfe-commits mailing list