[clang] a0365ab - [Driver] Support -gz=zstd

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 8 01:39:15 PDT 2022


Author: Fangrui Song
Date: 2022-09-08T01:39:06-07:00
New Revision: a0365abad8113d7e60294c5e581c24ec06ba037e

URL: https://github.com/llvm/llvm-project/commit/a0365abad8113d7e60294c5e581c24ec06ba037e
DIFF: https://github.com/llvm/llvm-project/commit/a0365abad8113d7e60294c5e581c24ec06ba037e.diff

LOG: [Driver] Support -gz=zstd

The driver option translates to --compress-debug-sections=zstd cc1/cc1as/GNU
assembler/linker options.

`clang -g -gz=zstd -c a.c` generates ELFCOMPRESS_ZSTD compressed debug info
sections if compression decreases size.

Added: 
    clang/test/Driver/compress-unavailable.s
    clang/test/Driver/compress-zstd.c

Modified: 
    clang/include/clang/Basic/DiagnosticDriverKinds.td
    clang/include/clang/Driver/Options.td
    clang/lib/Driver/ToolChains/Clang.cpp
    clang/lib/Driver/ToolChains/CommonArgs.cpp
    clang/lib/Driver/ToolChains/Gnu.cpp
    clang/test/CMakeLists.txt
    clang/test/Misc/cc1as-compress.s
    clang/test/lit.site.cfg.py.in

Removed: 
    clang/test/Driver/nozlibcompress.c


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index df85139c3ca22..1717a2ef71f1e 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -406,7 +406,7 @@ def warn_missing_sysroot : Warning<"no such sysroot directory: '%0'">,
   InGroup<DiagGroup<"missing-sysroot">>;
 def warn_incompatible_sysroot : Warning<"using sysroot for '%0' but targeting '%1'">,
   InGroup<DiagGroup<"incompatible-sysroot">>;
-def warn_debug_compression_unavailable : Warning<"cannot compress debug sections (zlib not enabled)">,
+def warn_debug_compression_unavailable : Warning<"cannot compress debug sections (%0 not enabled)">,
   InGroup<DiagGroup<"debug-compression-unavailable">>;
 def warn_drv_disabling_vptr_no_rtti_default : Warning<
   "implicitly disabling vptr sanitizer because rtti wasn't enabled">,

diff  --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index cfc0994d69d4e..4c95e3c4cae38 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5273,8 +5273,8 @@ def record_command_line : Separate<["-"], "record-command-line">,
   HelpText<"The string to embed in the .LLVM.command.line section.">,
   MarshallingInfoString<CodeGenOpts<"RecordCommandLine">>;
 def compress_debug_sections_EQ : Joined<["-", "--"], "compress-debug-sections=">,
-    HelpText<"DWARF debug sections compression type">, Values<"none,zlib">,
-    NormalizedValuesScope<"llvm::DebugCompressionType">, NormalizedValues<["None", "Z"]>,
+    HelpText<"DWARF debug sections compression type">, Values<"none,zlib,zstd">,
+    NormalizedValuesScope<"llvm::DebugCompressionType">, NormalizedValues<["None", "Z", "Zstd"]>,
     MarshallingInfoEnum<CodeGenOpts<"CompressDebugSections">, "None">;
 def compress_debug_sections : Flag<["-", "--"], "compress-debug-sections">,
   Alias<compress_debug_sections_EQ>, AliasArgs<["zlib"]>;

diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 837486971d112..45a71be97acd0 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1146,7 +1146,14 @@ static void RenderDebugInfoCompressionArgs(const ArgList &Args,
         CmdArgs.push_back(
             Args.MakeArgString("--compress-debug-sections=" + Twine(Value)));
       } else {
-        D.Diag(diag::warn_debug_compression_unavailable);
+        D.Diag(diag::warn_debug_compression_unavailable) << "zlib";
+      }
+    } else if (Value == "zstd") {
+      if (llvm::compression::zstd::isAvailable()) {
+        CmdArgs.push_back(
+            Args.MakeArgString("--compress-debug-sections=" + Twine(Value)));
+      } else {
+        D.Diag(diag::warn_debug_compression_unavailable) << "zstd";
       }
     } else {
       D.Diag(diag::err_drv_unsupported_option_argument)

diff  --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 4760a6f528627..22025d95e7c8f 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -285,7 +285,7 @@ void tools::addLinkerCompressDebugSectionsOption(
   // argument.
   if (const Arg *A = Args.getLastArg(options::OPT_gz_EQ)) {
     StringRef V = A->getValue();
-    if (V == "none" || V == "zlib")
+    if (V == "none" || V == "zlib" || V == "zstd")
       CmdArgs.push_back(Args.MakeArgString("--compress-debug-sections=" + V));
     else
       TC.getDriver().Diag(diag::err_drv_unsupported_option_argument)

diff  --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 047ec67f95e12..e153f86334aae 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -730,7 +730,7 @@ void tools::gnutools::Assembler::ConstructJob(Compilation &C,
       CmdArgs.push_back("--compress-debug-sections");
     } else {
       StringRef Value = A->getValue();
-      if (Value == "none" || Value == "zlib") {
+      if (Value == "none" || Value == "zlib" || Value == "zstd") {
         CmdArgs.push_back(
             Args.MakeArgString("--compress-debug-sections=" + Twine(Value)));
       } else {

diff  --git a/clang/test/CMakeLists.txt b/clang/test/CMakeLists.txt
index 08166a1d5cfd7..c3dcc53b78ab5 100644
--- a/clang/test/CMakeLists.txt
+++ b/clang/test/CMakeLists.txt
@@ -12,6 +12,7 @@ llvm_canonicalize_cmake_booleans(
   CLANG_SPAWN_CC1
   ENABLE_BACKTRACES
   LLVM_ENABLE_ZLIB
+  LLVM_ENABLE_ZSTD
   LLVM_ENABLE_PER_TARGET_RUNTIME_DIR
   LLVM_ENABLE_THREADS
   LLVM_WITH_Z3

diff  --git a/clang/test/Driver/compress-unavailable.s b/clang/test/Driver/compress-unavailable.s
new file mode 100644
index 0000000000000..e44fcb4ce9d5e
--- /dev/null
+++ b/clang/test/Driver/compress-unavailable.s
@@ -0,0 +1,8 @@
+// RUN: %clang -### -fintegrated-as -gz=none -c %s 2>&1 | FileCheck %s --check-prefix=NOWARN
+// NOWARN-NOT: warning: cannot compress debug sections (zlib not enabled)
+
+// RUN: %if !zlib %{ %clang -### -fintegrated-as -gz -c %s 2>&1 | FileCheck %s --check-prefix=WARN-ZLIB %}
+// WARN-ZLIB: warning: cannot compress debug sections (zlib not enabled)
+
+// RUN: %if !zstd %{ %clang -### -fintegrated-as -gz=zstd -c %s 2>&1 | FileCheck %s --check-prefix=WARN-ZSTD %}
+// WARN-ZSTD: warning: cannot compress debug sections (zstd not enabled)

diff  --git a/clang/test/Driver/compress-zstd.c b/clang/test/Driver/compress-zstd.c
new file mode 100644
index 0000000000000..84b26354589e9
--- /dev/null
+++ b/clang/test/Driver/compress-zstd.c
@@ -0,0 +1,7 @@
+// REQUIRES: zstd
+
+// RUN: %clang -### --target=aarch64-unknown-linux-gnu -gz=zstd -x assembler %s 2>&1 | FileCheck %s
+// RUN: %clang -### --target=x86_64-pc-freebsd -gz=zstd %s 2>&1 | FileCheck %s
+
+// CHECK: {{"-cc1(as)?".* "--compress-debug-sections=zstd"}}
+// CHECK: "--compress-debug-sections=zstd"

diff  --git a/clang/test/Driver/nozlibcompress.c b/clang/test/Driver/nozlibcompress.c
deleted file mode 100644
index 45935f595285f..0000000000000
--- a/clang/test/Driver/nozlibcompress.c
+++ /dev/null
@@ -1,7 +0,0 @@
-// REQUIRES: !zlib
-
-// RUN: %clang -### -fintegrated-as -gz -c %s 2>&1 | FileCheck %s -check-prefix CHECK-WARN
-// RUN: %clang -### -fintegrated-as -gz=none -c %s 2>&1 | FileCheck -allow-empty -check-prefix CHECK-NOWARN %s
-
-// CHECK-WARN: warning: cannot compress debug sections (zlib not enabled)
-// CHECK-NOWARN-NOT: warning: cannot compress debug sections (zlib not enabled)

diff  --git a/clang/test/Misc/cc1as-compress.s b/clang/test/Misc/cc1as-compress.s
index 17c09cc653b4d..8937e5d1b275c 100644
--- a/clang/test/Misc/cc1as-compress.s
+++ b/clang/test/Misc/cc1as-compress.s
@@ -3,3 +3,4 @@
 
 // RUN: %clang -cc1as -triple i686 --compress-debug-sections %s -o /dev/null
 // RUN: %clang -cc1as -triple i686 -compress-debug-sections=zlib %s -o /dev/null
+// RUN: %if zstd %{ %clang -cc1as -triple x86_64 -compress-debug-sections=zstd %s -o /dev/null %}

diff  --git a/clang/test/lit.site.cfg.py.in b/clang/test/lit.site.cfg.py.in
index 8c62d61c57b40..b5c9b1a463ba6 100644
--- a/clang/test/lit.site.cfg.py.in
+++ b/clang/test/lit.site.cfg.py.in
@@ -21,6 +21,7 @@ config.host_cc = "@CMAKE_C_COMPILER@"
 config.host_cxx = "@CMAKE_CXX_COMPILER@"
 config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
 config.have_zlib = @LLVM_ENABLE_ZLIB@
+config.have_zstd = @LLVM_ENABLE_ZSTD@
 config.clang_arcmt = @CLANG_ENABLE_ARCMT@
 config.clang_default_pie_on_linux = @CLANG_DEFAULT_PIE_ON_LINUX@
 config.clang_enable_opaque_pointers = @CLANG_ENABLE_OPAQUE_POINTERS_INTERNAL@


        


More information about the cfe-commits mailing list