Skip to content

Commit d3d49bc

Browse files
authored
[InstrProfiling] Don't attempt to create duplicate data variables. (#71998)
Fixes a bug introduced by commit f95b2f1 ("Reland [InstrProf][compiler-rt] Enable MC/DC Support in LLVM Source-based Code Coverage (1/3)") createDataVariable() needs to check that a data variable wasn't already created before creating it. Previously, this was done inadvertantly in getOrCreateRegionCounters(), which checked that the RegionCounters was not created multiple times before creating the counter section and the data variable. When the creation of the data variable was abstracted into its own function (createDataVariable()), there was no corresponding check. This was failing on a case in which an instrumented function was being inlined into multiple functions and a duplicate data variable was created, which led to a segfault in emitNameData(). Test case added based on the repro that also ensures a single data variable was created in this case.
1 parent fdc904e commit d3d49bc

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,10 @@ void InstrProfiling::createDataVariable(InstrProfCntrInstBase *Inc,
12511251
GlobalVariable *NamePtr = Inc->getName();
12521252
auto &PD = ProfileDataMap[NamePtr];
12531253

1254+
// Return if data variable was already created.
1255+
if (PD.DataVar)
1256+
return;
1257+
12541258
LLVMContext &Ctx = M->getContext();
12551259

12561260
Function *Fn = Inc->getParent()->getParent();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
;; Check that only one data variable is created when an instrprof.increment is
2+
;; inlined into more than one function.
3+
; RUN: opt %s -passes='cgscc(inline),instrprof' -S | FileCheck %s
4+
5+
target triple = "x86_64-unknown-linux-gnu"
6+
7+
; CHECK: @__profd_foobar = private global
8+
; CHECK-NOT @__profd_foobar
9+
10+
declare void @llvm.instrprof.increment(ptr %0, i64 %1, i32 %2, i32 %3)
11+
@__profn_foobar = private constant [6 x i8] c"foobar"
12+
13+
define internal void @foobar() {
14+
call void @llvm.instrprof.increment(ptr @__profn_foobar, i64 123456, i32 32, i32 0)
15+
ret void
16+
}
17+
18+
define void @foo() {
19+
call void @foobar()
20+
ret void
21+
}
22+
23+
define void @bar() {
24+
call void @foobar()
25+
ret void
26+
}

0 commit comments

Comments
 (0)