From d6aed77f0d19664be48d531552692520ae2a6f1a Mon Sep 17 00:00:00 2001 From: Gulfem Savrun Yeniceri Date: Fri, 26 Aug 2022 16:38:44 +0000 Subject: [PATCH] [InstrProfiling] No runtime hook for unused funcs This is a reland of https://reviews.llvm.org/D122336. Original patch caused a problem in collecting coverage in Fuchsia because it was returning early without putting unused function names into __llvm_prf_names section. This patch fixes that issue. The original commit message is as the following: 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 emits runtime hook 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 https://reviews.llvm.org/D98061, 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. Reviewed By: phosek Differential Revision: https://reviews.llvm.org/D122336 --- .../CoverageMapping/unused_function_no_runtime_hook.cpp | 6 ++++++ llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp | 14 ++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 clang/test/CoverageMapping/unused_function_no_runtime_hook.cpp 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 0000000..5a835ae --- /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 6404315..ce99728 100644 --- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp +++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -525,9 +525,8 @@ 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. @@ -567,7 +566,14 @@ bool InstrProfiling::run( emitVNodes(); emitNameData(); - emitRuntimeHook(); + + // Emit runtime hook except for the cases where coverage is enabled on + // code that is eliminated by the front-end, e.g. unused functions with + // internal linkage, and the target does not require pulling in profile + // runtime. + if (containsProfilingIntrinsics(M) || !CoverageNamesVar || NeedsRuntimeHook) + emitRuntimeHook(); + emitRegistration(); emitUses(); emitInitialization(); -- 2.7.4