llvm.org GIT mirror llvm / 4ed7d96
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326350 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 2 years ago
5 changed file(s) with 28 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
108108 void emitRegistration();
109109
110110 /// Emit the necessary plumbing to pull in the runtime initialization.
111 void emitRuntimeHook();
111 /// Returns true if a change was made.
112 bool emitRuntimeHook();
112113
113114 /// Add uses of our data variables and runtime hook.
114115 void emitUses();
447447 }
448448
449449 bool InstrProfiling::run(Module &M, const TargetLibraryInfo &TLI) {
450 // Improve compile time by avoiding linear scans when there is no work.
451 GlobalVariable *CoverageNamesVar =
452 M.getNamedGlobal(getCoverageUnusedNamesVarName());
453 if (!containsProfilingIntrinsics(M) && !CoverageNamesVar)
454 return false;
455
456 bool MadeChange = false;
457
458450 this->M = &M;
459451 this->TLI = &TLI;
460452 NamesVar = nullptr;
464456 getMemOPSizeRangeFromOption(MemOPSizeRange, MemOPSizeRangeStart,
465457 MemOPSizeRangeLast);
466458 TT = Triple(M.getTargetTriple());
459
460 // Emit the runtime hook even if no counters are present.
461 bool MadeChange = emitRuntimeHook();
462
463 // Improve compile time by avoiding linear scans when there is no work.
464 GlobalVariable *CoverageNamesVar =
465 M.getNamedGlobal(getCoverageUnusedNamesVarName());
466 if (!containsProfilingIntrinsics(M) && !CoverageNamesVar)
467 return MadeChange;
467468
468469 // We did not know how many value sites there would be inside
469470 // the instrumented function. This is counting the number of instrumented
497498 emitVNodes();
498499 emitNameData();
499500 emitRegistration();
500 emitRuntimeHook();
501501 emitUses();
502502 emitInitialization();
503503 return true;
913913 IRB.CreateRetVoid();
914914 }
915915
916 void InstrProfiling::emitRuntimeHook() {
916 bool InstrProfiling::emitRuntimeHook() {
917917 // We expect the linker to be invoked with -u flag for linux,
918918 // for which case there is no need to emit the user function.
919919 if (Triple(M->getTargetTriple()).isOSLinux())
920 return;
920 return false;
921921
922922 // If the module's provided its own runtime, we don't need to do anything.
923923 if (M->getGlobalVariable(getInstrProfRuntimeHookVarName()))
924 return;
924 return false;
925925
926926 // Declare an external variable that will pull in the runtime initialization.
927927 auto *Int32Ty = Type::getInt32Ty(M->getContext());
946946
947947 // Mark the user variable as used so that it isn't stripped out.
948948 UsedVars.push_back(User);
949 return true;
949950 }
950951
951952 void InstrProfiling::emitUses() {
55 ; RUN: opt < %s -mtriple=x86_64-unknown-linux -passes=instrprof -S | FileCheck %s --check-prefix=LINUX --check-prefix=COMMON
66 ; RUN: opt < %s -mtriple=x86_64-pc-win32-coff -instrprof -S | FileCheck %s --check-prefix=COFF
77 ; RUN: opt < %s -mtriple=x86_64-pc-win32-coff -passes=instrprof -S | FileCheck %s --check-prefix=COFF
8
9 ; OTHER: @__llvm_profile_runtime = external global i32
10 ; LINUX-NOT: @__llvm_profile_runtime = external global i32
811
912 @__profn_foo = hidden constant [3 x i8] c"foo"
1013 @__profn_foo_weak = weak hidden constant [8 x i8] c"foo_weak"
5154
5255 declare void @llvm.instrprof.increment(i8*, i64, i32, i32)
5356
54 ; OTHER: @__llvm_profile_runtime = external global i32
55 ; LINUX-NOT: @__llvm_profile_runtime = external global i32
56
5757 ; OTHER: define linkonce_odr hidden i32 @__llvm_profile_runtime_user() {{.*}} {
5858 ; OTHER: %[[REG:.*]] = load i32, i32* @__llvm_profile_runtime
5959 ; OTHER: ret i32 %[[REG]]
None ;; No instrumentation should be emitted if there are no counter increments.
0 ;; Emit the runtime hook even if there are no counter increments.
11
2 ; RUN: opt < %s -instrprof -S | FileCheck %s
3 ; RUN: opt < %s -passes=instrprof -S | FileCheck %s
4 ; CHECK-NOT: @__profc
5 ; CHECK-NOT: @__profd
6 ; CHECK-NOT: @__llvm_profile_runtime
2 ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -instrprof -S | FileCheck %s -check-prefixes=ALL,DARWIN
3 ; RUN: opt < %s -mtriple=x86_64-apple-macosx10.10.0 -passes=instrprof -S | FileCheck %s -check-prefixes=ALL,DARWIN
4 ; RUN: opt < %s -mtriple=x86_64-linux-unknown -passes=instrprof -S | FileCheck %s -check-prefixes=ALL,LINUX
5 ; ALL-NOT: @__profc
6 ; ALL-NOT: @__profd
7 ; DARWIN: @__llvm_profile_runtime
8 ; LINUX-NOT: @__llvm_profile_runtime
79
810 define void @foo() {
911 ret void
11 ; RUN: opt < %s -passes=instrprof -S | FileCheck %s
22
33 target triple = "x86_64-apple-macosx10.10.0"
4
5 ; CHECK: @__llvm_profile_runtime = external global i32
46
57 @__profn_foo = hidden constant [3 x i8] c"foo"
68 ; CHECK-NOT: __profn_foo
3436
3537 declare void @llvm.instrprof.increment(i8*, i64, i32, i32)
3638
37 ; CHECK: @__llvm_profile_runtime = external global i32
3839 ; CHECK: @llvm.used = appending global {{.*}} @__profd_foo {{.*}} @__profd_bar {{.*}} @__profd_baz {{.*}} section "llvm.metadata"