[llvm] r326350 - [InstrProfiling] Emit the runtime hook when no counters are lowered

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 28 11:00:09 PST 2018


Author: vedantk
Date: Wed Feb 28 11:00:08 2018
New Revision: 326350

URL: http://llvm.org/viewvc/llvm-project?rev=326350&view=rev
Log:
[InstrProfiling] Emit the runtime hook when no counters are lowered

The API verification tool tapi has difficulty processing frameworks
which enable code coverage, but which have no code. The profile lowering
pass does not emit the runtime hook in this case because no counters are
lowered.

While the hook is not needed for program correctness (the profile
runtime doesn't have to be linked in), it's needed to allow tapi to
validate the exported symbol set of instrumented binaries.

It was not possible to add a workaround in tapi for empty binaries due
to an architectural issue: tapi generates its expected symbol set before
it inspects a binary. Changing that model has a higher cost than simply
forcing llvm to always emit the runtime hook.

rdar://36076904

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

Modified:
    llvm/trunk/include/llvm/Transforms/InstrProfiling.h
    llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp
    llvm/trunk/test/Instrumentation/InstrProfiling/linkage.ll
    llvm/trunk/test/Instrumentation/InstrProfiling/no-counters.ll
    llvm/trunk/test/Instrumentation/InstrProfiling/profiling.ll

Modified: llvm/trunk/include/llvm/Transforms/InstrProfiling.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/InstrProfiling.h?rev=326350&r1=326349&r2=326350&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/InstrProfiling.h (original)
+++ llvm/trunk/include/llvm/Transforms/InstrProfiling.h Wed Feb 28 11:00:08 2018
@@ -109,7 +109,8 @@ private:
   void emitRegistration();
 
   /// Emit the necessary plumbing to pull in the runtime initialization.
-  void emitRuntimeHook();
+  /// Returns true if a change was made.
+  bool emitRuntimeHook();
 
   /// Add uses of our data variables and runtime hook.
   void emitUses();

Modified: llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp?rev=326350&r1=326349&r2=326350&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp Wed Feb 28 11:00:08 2018
@@ -448,14 +448,6 @@ static bool containsProfilingIntrinsics(
 }
 
 bool InstrProfiling::run(Module &M, const TargetLibraryInfo &TLI) {
-  // Improve compile time by avoiding linear scans when there is no work.
-  GlobalVariable *CoverageNamesVar =
-      M.getNamedGlobal(getCoverageUnusedNamesVarName());
-  if (!containsProfilingIntrinsics(M) && !CoverageNamesVar)
-    return false;
-
-  bool MadeChange = false;
-
   this->M = &M;
   this->TLI = &TLI;
   NamesVar = nullptr;
@@ -466,6 +458,15 @@ bool InstrProfiling::run(Module &M, cons
                               MemOPSizeRangeLast);
   TT = Triple(M.getTargetTriple());
 
+  // Emit the runtime hook even if no counters are present.
+  bool 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;
+
   // We did not know how many value sites there would be inside
   // the instrumented function. This is counting the number of instrumented
   // target value sites to enter it as field in the profile data variable.
@@ -498,7 +499,6 @@ bool InstrProfiling::run(Module &M, cons
   emitVNodes();
   emitNameData();
   emitRegistration();
-  emitRuntimeHook();
   emitUses();
   emitInitialization();
   return true;
@@ -914,15 +914,15 @@ void InstrProfiling::emitRegistration()
   IRB.CreateRetVoid();
 }
 
-void InstrProfiling::emitRuntimeHook() {
+bool InstrProfiling::emitRuntimeHook() {
   // We expect the linker to be invoked with -u<hook_var> flag for linux,
   // for which case there is no need to emit the user function.
   if (Triple(M->getTargetTriple()).isOSLinux())
-    return;
+    return false;
 
   // If the module's provided its own runtime, we don't need to do anything.
   if (M->getGlobalVariable(getInstrProfRuntimeHookVarName()))
-    return;
+    return false;
 
   // Declare an external variable that will pull in the runtime initialization.
   auto *Int32Ty = Type::getInt32Ty(M->getContext());
@@ -947,6 +947,7 @@ void InstrProfiling::emitRuntimeHook() {
 
   // Mark the user variable as used so that it isn't stripped out.
   UsedVars.push_back(User);
+  return true;
 }
 
 void InstrProfiling::emitUses() {

Modified: llvm/trunk/test/Instrumentation/InstrProfiling/linkage.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/InstrProfiling/linkage.ll?rev=326350&r1=326349&r2=326350&view=diff
==============================================================================
--- llvm/trunk/test/Instrumentation/InstrProfiling/linkage.ll (original)
+++ llvm/trunk/test/Instrumentation/InstrProfiling/linkage.ll Wed Feb 28 11:00:08 2018
@@ -7,6 +7,9 @@
 ; RUN: opt < %s  -mtriple=x86_64-pc-win32-coff -instrprof -S | FileCheck %s --check-prefix=COFF
 ; RUN: opt < %s  -mtriple=x86_64-pc-win32-coff -passes=instrprof -S | FileCheck %s --check-prefix=COFF
 
+; OTHER: @__llvm_profile_runtime = external global i32
+; LINUX-NOT: @__llvm_profile_runtime = external global i32
+
 @__profn_foo = hidden constant [3 x i8] c"foo"
 @__profn_foo_weak = weak hidden constant [8 x i8] c"foo_weak"
 @"__profn_linkage.ll:foo_internal" = internal constant [23 x i8] c"linkage.ll:foo_internal"
@@ -52,9 +55,6 @@ define available_externally void @foo_ex
 
 declare void @llvm.instrprof.increment(i8*, i64, i32, i32)
 
-; OTHER: @__llvm_profile_runtime = external global i32
-; LINUX-NOT: @__llvm_profile_runtime = external global i32
-
 ; OTHER: define linkonce_odr hidden i32 @__llvm_profile_runtime_user() {{.*}} {
 ; OTHER:   %[[REG:.*]] = load i32, i32* @__llvm_profile_runtime
 ; OTHER:   ret i32 %[[REG]]

Modified: llvm/trunk/test/Instrumentation/InstrProfiling/no-counters.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/InstrProfiling/no-counters.ll?rev=326350&r1=326349&r2=326350&view=diff
==============================================================================
--- llvm/trunk/test/Instrumentation/InstrProfiling/no-counters.ll (original)
+++ llvm/trunk/test/Instrumentation/InstrProfiling/no-counters.ll Wed Feb 28 11:00:08 2018
@@ -1,10 +1,12 @@
-;; No instrumentation should be emitted if there are no counter increments.
+;; Emit the runtime hook even if there are no counter increments.
 
-; RUN: opt < %s -instrprof -S | FileCheck %s
-; RUN: opt < %s -passes=instrprof -S | FileCheck %s
-; CHECK-NOT: @__profc
-; CHECK-NOT: @__profd
-; CHECK-NOT: @__llvm_profile_runtime
+; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -instrprof -S | FileCheck %s -check-prefixes=ALL,DARWIN
+; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -passes=instrprof -S | FileCheck %s -check-prefixes=ALL,DARWIN
+; RUN: opt < %s -mtriple=x86_64-linux-unknown -passes=instrprof -S | FileCheck %s -check-prefixes=ALL,LINUX
+; ALL-NOT: @__profc
+; ALL-NOT: @__profd
+; DARWIN: @__llvm_profile_runtime
+; LINUX-NOT: @__llvm_profile_runtime
 
 define void @foo() {
   ret void

Modified: llvm/trunk/test/Instrumentation/InstrProfiling/profiling.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/InstrProfiling/profiling.ll?rev=326350&r1=326349&r2=326350&view=diff
==============================================================================
--- llvm/trunk/test/Instrumentation/InstrProfiling/profiling.ll (original)
+++ llvm/trunk/test/Instrumentation/InstrProfiling/profiling.ll Wed Feb 28 11:00:08 2018
@@ -3,6 +3,8 @@
 
 target triple = "x86_64-apple-macosx10.10.0"
 
+; CHECK: @__llvm_profile_runtime = external global i32
+
 @__profn_foo = hidden constant [3 x i8] c"foo"
 ; CHECK-NOT: __profn_foo
 @__profn_bar = hidden constant [4 x i8] c"bar\00"
@@ -35,5 +37,4 @@ define void @baz() {
 
 declare void @llvm.instrprof.increment(i8*, i64, i32, i32)
 
-; CHECK: @__llvm_profile_runtime = external global i32
 ; CHECK: @llvm.used = appending global {{.*}} @__profd_foo {{.*}} @__profd_bar {{.*}} @__profd_baz {{.*}} section "llvm.metadata"




More information about the llvm-commits mailing list