[clang] c7f91e2 - [InstrProfiling] No runtime hook for unused funcs

Gulfem Savrun Yeniceri via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 25 10:03:44 PDT 2022


Author: Gulfem Savrun Yeniceri
Date: 2022-03-25T17:03:03Z
New Revision: c7f91e227a799dfee05962bb108274dbfe809fee

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

LOG: [InstrProfiling] No runtime hook for unused funcs

CoverageMappingModuleGen generates a coverage mapping record
even for unused functions with internal linkage, e.g.
static int foo() { return 100; }
Clang frontend eliminates such functions, but InstrProfiling pass
still pulls in profile runtime since there is a coverage record.
Fuchsia uses runtime counter relocation, and pulling in profile
runtime for unused functions causes a linker error:
undefined hidden symbol: __llvm_profile_counter_bias.
Since 389dc94d4be7, we do not hook profile runtime for the binaries
that none of its translation units have been instrumented in Fuchsia.
This patch extends that for the instrumented binaries that
consist of only unused functions.

Differential Revision: https://reviews.llvm.org/D122336

Added: 
    clang/test/CoverageMapping/unused_function_no_runtime_hook.cpp

Modified: 
    llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/CoverageMapping/unused_function_no_runtime_hook.cpp b/clang/test/CoverageMapping/unused_function_no_runtime_hook.cpp
new file mode 100644
index 0000000000000..5a835ae311697
--- /dev/null
+++ b/clang/test/CoverageMapping/unused_function_no_runtime_hook.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang -target x86_64-unknown-fuchsia -fprofile-instr-generate -fcoverage-mapping -emit-llvm -S %s -o - | FileCheck %s
+
+// CHECK-NOT: @__llvm_profile_runtime
+static int f0() {
+  return 100;
+}

diff  --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
index 379c41ce66936..a8b013b6f2e6a 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -558,16 +558,18 @@ bool InstrProfiling::run(
   TT = Triple(M.getTargetTriple());
 
   bool MadeChange = false;
-
-  // Emit the runtime hook even if no counters are present.
-  if (needsRuntimeHookUnconditionally(TT))
+  bool NeedsRuntimeHook = needsRuntimeHookUnconditionally(TT);
+  if (NeedsRuntimeHook)
     MadeChange = emitRuntimeHook();
 
   // Improve compile time by avoiding linear scans when there is no work.
   GlobalVariable *CoverageNamesVar =
       M.getNamedGlobal(getCoverageUnusedNamesVarName());
-  if (!containsProfilingIntrinsics(M) && !CoverageNamesVar)
-    return MadeChange;
+  if (!containsProfilingIntrinsics(M)) {
+    if (!CoverageNamesVar || !NeedsRuntimeHook) {
+      return MadeChange;
+    }
+  }
 
   // We did not know how many value sites there would be inside
   // the instrumented function. This is counting the number of instrumented


        


More information about the cfe-commits mailing list