[clang] Add option to avoid generating coverage mappings for unused functions (PR #92582)

Andrew Wock via cfe-commits cfe-commits at lists.llvm.org
Fri May 17 10:25:26 PDT 2024


https://github.com/ajwock created https://github.com/llvm/llvm-project/pull/92582

This change adds an option which prevents generating coverage mapping data for functions which aren't used.  This reduces binary size in cases where many unused static inline functions are automatically generated.

>From da030dd074ad253c7f0b0376384c441ceaf3c42c Mon Sep 17 00:00:00 2001
From: Andrew Wock <ajwock at gmail.com>
Date: Fri, 17 May 2024 11:52:16 -0400
Subject: [PATCH] Add option to avoid generating coverage mappings for unused
 functions

This change adds an option which prevents generating coverage mapping
data for functions which aren't used.  This reduces binary size in cases
where many unused static inline functions are automatically generated.

Signed-off-by: Andrew Wock <ajwock at gmail.com>
---
 clang/include/clang/Basic/CodeGenOptions.def       |  3 +++
 clang/include/clang/Driver/Options.td              |  3 +++
 clang/lib/CodeGen/CodeGenModule.cpp                |  4 +++-
 clang/test/CoverageMapping/deferred_unused_names.c | 11 +++++++++++
 4 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CoverageMapping/deferred_unused_names.c

diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index 07b0ca1691a67..8e63d28ffdb83 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -222,6 +222,9 @@ CODEGENOPT(CoverageMapping , 1, 0) ///< Generate coverage mapping regions to
                                    ///< enable code coverage analysis.
 CODEGENOPT(DumpCoverageMapping , 1, 0) ///< Dump the generated coverage mapping
                                        ///< regions.
+CODEGENOPT(NoUnusedCoverage , 1, 0) ///< Turn off coverage mapping for code that
+                                    ///< is not emitted.
+
 CODEGENOPT(MCDCCoverage , 1, 0) ///< Enable MC/DC code coverage criteria.
 
   /// If -fpcc-struct-return or -freg-struct-return is specified.
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 7bb781667e926..5ca8425e4dd71 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -7065,6 +7065,9 @@ def coverage_version_EQ : Joined<["-"], "coverage-version=">,
 def dump_coverage_mapping : Flag<["-"], "dump-coverage-mapping">,
   HelpText<"Dump the coverage mapping records, for testing">,
   MarshallingInfoFlag<CodeGenOpts<"DumpCoverageMapping">>;
+def no_unused_coverage : Flag<["-"], "no-unused-coverage">,
+  HelpText<"Turn off coverage mapping for code that is not emitted">,
+  MarshallingInfoFlag<CodeGenOpts<"NoUnusedCoverage">>;
 def fuse_register_sized_bitfield_access: Flag<["-"], "fuse-register-sized-bitfield-access">,
   HelpText<"Use register sized accesses to bit-fields, when possible.">,
   MarshallingInfoFlag<CodeGenOpts<"UseRegisterSizedBitfieldAccess">>;
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 489c08a4d4819..a037c258bfa27 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -891,7 +891,9 @@ void CodeGenModule::Release() {
   EmitGlobalAnnotations();
   EmitStaticExternCAliases();
   checkAliases();
-  EmitDeferredUnusedCoverageMappings();
+  if (!CodeGenOpts.NoUnusedCoverage) {
+    EmitDeferredUnusedCoverageMappings();
+  }
   CodeGenPGO(*this).setValueProfilingFlag(getModule());
   CodeGenPGO(*this).setProfileVersion(getModule());
   if (CoverageMapping)
diff --git a/clang/test/CoverageMapping/deferred_unused_names.c b/clang/test/CoverageMapping/deferred_unused_names.c
new file mode 100644
index 0000000000000..488bba527631b
--- /dev/null
+++ b/clang/test/CoverageMapping/deferred_unused_names.c
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -mllvm -enable-name-compression=false -no-unused-coverage -fprofile-instrument=clang -fcoverage-mapping -emit-llvm -main-file-name unused_names.c -o - %s > %t
+// RUN: FileCheck -input-file %t %s
+
+// There should only be a prf_names entry for bar, as the other two functions are
+// unused.
+//
+// CHECK-DAG: @__llvm_prf_nm = private constant [5 x i8] c"\03\00bar", section "{{.*__llvm_prf_names|\.lprfn\$M}}"
+
+int bar(void) { return 0; }
+inline int baz(void) { return 0; }
+static int qux(void) { return 42; }



More information about the cfe-commits mailing list