Skip to content

Commit 960a210

Browse files
authored
[ctx_prof] Remove the dependency on the "name" GlobalVariable (llvm#105731)
We don't need that name variable for contextual instrumentation, we just use the function to get its GUID which we pass to the runtime, and rely on metadata to capture it through the various optimization passes. This change removes the need for the name global variable.
1 parent 1821cb3 commit 960a210

File tree

5 files changed

+39
-45
lines changed

5 files changed

+39
-45
lines changed

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,11 +1503,19 @@ class InstrProfInstBase : public IntrinsicInst {
15031503
return isCounterBase(*Instr) || isMCDCBitmapBase(*Instr);
15041504
return false;
15051505
}
1506-
// The name of the instrumented function.
1506+
1507+
// The name of the instrumented function, assuming it is a global variable.
15071508
GlobalVariable *getName() const {
1508-
return cast<GlobalVariable>(
1509-
const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
1509+
return cast<GlobalVariable>(getNameValue());
1510+
}
1511+
1512+
// The "name" operand of the profile instrumentation instruction - this is the
1513+
// operand that can be used to relate the instruction to the function it
1514+
// belonged to at instrumentation time.
1515+
Value *getNameValue() const {
1516+
return const_cast<Value *>(getArgOperand(0))->stripPointerCasts();
15101517
}
1518+
15111519
// The hash of the CFG for the instrumented function.
15121520
ConstantInt *getHash() const {
15131521
return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));

llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
226226

227227
IRBuilder<> Builder(Mark);
228228

229-
Guid = Builder.getInt64(AssignGUIDPass::getGUID(F));
229+
Guid = Builder.getInt64(
230+
AssignGUIDPass::getGUID(cast<Function>(*Mark->getNameValue())));
230231
// The type of the context of this function is now knowable since we have
231232
// NrCallsites and NrCounters. We delcare it here because it's more
232233
// convenient - we have the Builder.

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ struct SelectInstVisitor : public InstVisitor<SelectInstVisitor> {
464464
VisitMode Mode = VM_counting; // Visiting mode.
465465
unsigned *CurCtrIdx = nullptr; // Pointer to current counter index.
466466
unsigned TotalNumCtrs = 0; // Total number of counters
467-
GlobalVariable *FuncNameVar = nullptr;
467+
GlobalValue *FuncNameVar = nullptr;
468468
uint64_t FuncHash = 0;
469469
PGOUseFunc *UseFunc = nullptr;
470470
bool HasSingleByteCoverage;
@@ -482,7 +482,7 @@ struct SelectInstVisitor : public InstVisitor<SelectInstVisitor> {
482482
// Ind is a pointer to the counter index variable; \p TotalNC
483483
// is the total number of counters; \p FNV is the pointer to the
484484
// PGO function name var; \p FHash is the function hash.
485-
void instrumentSelects(unsigned *Ind, unsigned TotalNC, GlobalVariable *FNV,
485+
void instrumentSelects(unsigned *Ind, unsigned TotalNC, GlobalValue *FNV,
486486
uint64_t FHash) {
487487
Mode = VM_instrument;
488488
CurCtrIdx = Ind;
@@ -901,13 +901,14 @@ void FunctionInstrumenter::instrument() {
901901
SplitIndirectBrCriticalEdges(F, /*IgnoreBlocksWithoutPHI=*/false, BPI, BFI);
902902
}
903903

904+
const bool IsCtxProf = InstrumentationType == PGOInstrumentationType::CTXPROF;
904905
FuncPGOInstrumentation<PGOEdge, PGOBBInfo> FuncInfo(
905-
F, TLI, ComdatMembers, true, BPI, BFI,
906+
F, TLI, ComdatMembers, /*CreateGlobalVar=*/!IsCtxProf, BPI, BFI,
906907
InstrumentationType == PGOInstrumentationType::CSFDO,
907908
shouldInstrumentEntryBB(), PGOBlockCoverage);
908909

909-
auto Name = FuncInfo.FuncNameVar;
910-
auto CFGHash =
910+
auto *const Name = IsCtxProf ? cast<GlobalValue>(&F) : FuncInfo.FuncNameVar;
911+
auto *const CFGHash =
911912
ConstantInt::get(Type::getInt64Ty(M.getContext()), FuncInfo.FunctionHash);
912913
// Make sure that pointer to global is passed in with zero addrspace
913914
// This is relevant during GPU profiling
@@ -929,7 +930,7 @@ void FunctionInstrumenter::instrument() {
929930
unsigned NumCounters =
930931
InstrumentBBs.size() + FuncInfo.SIVisitor.getNumOfSelectInsts();
931932

932-
if (InstrumentationType == PGOInstrumentationType::CTXPROF) {
933+
if (IsCtxProf) {
933934
auto *CSIntrinsic =
934935
Intrinsic::getDeclaration(&M, Intrinsic::instrprof_callsite);
935936
// We want to count the instrumentable callsites, then instrument them. This
@@ -995,7 +996,7 @@ void FunctionInstrumenter::instrument() {
995996
}
996997

997998
// Now instrument select instructions:
998-
FuncInfo.SIVisitor.instrumentSelects(&I, NumCounters, FuncInfo.FuncNameVar,
999+
FuncInfo.SIVisitor.instrumentSelects(&I, NumCounters, Name,
9991000
FuncInfo.FunctionHash);
10001001
assert(I == NumCounters);
10011002

llvm/test/Transforms/PGOProfile/ctx-instrumentation.ll

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,6 @@
99
declare void @bar()
1010

1111
;.
12-
; INSTRUMENT: @__profn_foo = private constant [3 x i8] c"foo"
13-
; INSTRUMENT: @__profn_an_entrypoint = private constant [13 x i8] c"an_entrypoint"
14-
; INSTRUMENT: @__profn_another_entrypoint_no_callees = private constant [29 x i8] c"another_entrypoint_no_callees"
15-
; INSTRUMENT: @__profn_simple = private constant [6 x i8] c"simple"
16-
; INSTRUMENT: @__profn_no_callsites = private constant [12 x i8] c"no_callsites"
17-
; INSTRUMENT: @__profn_no_counters = private constant [11 x i8] c"no_counters"
18-
;.
19-
; LOWERING: @__profn_foo = private constant [3 x i8] c"foo"
20-
; LOWERING: @__profn_an_entrypoint = private constant [13 x i8] c"an_entrypoint"
21-
; LOWERING: @__profn_another_entrypoint_no_callees = private constant [29 x i8] c"another_entrypoint_no_callees"
22-
; LOWERING: @__profn_simple = private constant [6 x i8] c"simple"
23-
; LOWERING: @__profn_no_callsites = private constant [12 x i8] c"no_callsites"
24-
; LOWERING: @__profn_no_counters = private constant [11 x i8] c"no_counters"
2512
; LOWERING: @an_entrypoint_ctx_root = global { ptr, ptr, ptr, i8 } zeroinitializer
2613
; LOWERING: @another_entrypoint_no_callees_ctx_root = global { ptr, ptr, ptr, i8 } zeroinitializer
2714
; LOWERING: @__llvm_ctx_profile_callsite = external hidden thread_local global ptr
@@ -30,16 +17,16 @@ declare void @bar()
3017
define void @foo(i32 %a, ptr %fct) {
3118
; INSTRUMENT-LABEL: define void @foo(
3219
; INSTRUMENT-SAME: i32 [[A:%.*]], ptr [[FCT:%.*]]) {
33-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_foo, i64 728453322856651412, i32 2, i32 0)
20+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @foo, i64 728453322856651412, i32 2, i32 0)
3421
; INSTRUMENT-NEXT: [[T:%.*]] = icmp eq i32 [[A]], 0
3522
; INSTRUMENT-NEXT: br i1 [[T]], label [[YES:%.*]], label [[NO:%.*]]
3623
; INSTRUMENT: yes:
37-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_foo, i64 728453322856651412, i32 2, i32 1)
38-
; INSTRUMENT-NEXT: call void @llvm.instrprof.callsite(ptr @__profn_foo, i64 728453322856651412, i32 2, i32 0, ptr [[FCT]])
24+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @foo, i64 728453322856651412, i32 2, i32 1)
25+
; INSTRUMENT-NEXT: call void @llvm.instrprof.callsite(ptr @foo, i64 728453322856651412, i32 2, i32 0, ptr [[FCT]])
3926
; INSTRUMENT-NEXT: call void [[FCT]](i32 [[A]])
4027
; INSTRUMENT-NEXT: br label [[EXIT:%.*]]
4128
; INSTRUMENT: no:
42-
; INSTRUMENT-NEXT: call void @llvm.instrprof.callsite(ptr @__profn_foo, i64 728453322856651412, i32 2, i32 1, ptr @bar)
29+
; INSTRUMENT-NEXT: call void @llvm.instrprof.callsite(ptr @foo, i64 728453322856651412, i32 2, i32 1, ptr @bar)
4330
; INSTRUMENT-NEXT: call void @bar()
4431
; INSTRUMENT-NEXT: br label [[EXIT]]
4532
; INSTRUMENT: exit:
@@ -92,12 +79,12 @@ exit:
9279
define void @an_entrypoint(i32 %a) {
9380
; INSTRUMENT-LABEL: define void @an_entrypoint(
9481
; INSTRUMENT-SAME: i32 [[A:%.*]]) {
95-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_an_entrypoint, i64 784007058953177093, i32 2, i32 0)
82+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @an_entrypoint, i64 784007058953177093, i32 2, i32 0)
9683
; INSTRUMENT-NEXT: [[T:%.*]] = icmp eq i32 [[A]], 0
9784
; INSTRUMENT-NEXT: br i1 [[T]], label [[YES:%.*]], label [[NO:%.*]]
9885
; INSTRUMENT: yes:
99-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_an_entrypoint, i64 784007058953177093, i32 2, i32 1)
100-
; INSTRUMENT-NEXT: call void @llvm.instrprof.callsite(ptr @__profn_an_entrypoint, i64 784007058953177093, i32 1, i32 0, ptr @foo)
86+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @an_entrypoint, i64 784007058953177093, i32 2, i32 1)
87+
; INSTRUMENT-NEXT: call void @llvm.instrprof.callsite(ptr @an_entrypoint, i64 784007058953177093, i32 1, i32 0, ptr @foo)
10188
; INSTRUMENT-NEXT: call void @foo(i32 1, ptr null)
10289
; INSTRUMENT-NEXT: ret void
10390
; INSTRUMENT: no:
@@ -144,11 +131,11 @@ no:
144131
define void @another_entrypoint_no_callees(i32 %a) {
145132
; INSTRUMENT-LABEL: define void @another_entrypoint_no_callees(
146133
; INSTRUMENT-SAME: i32 [[A:%.*]]) {
147-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_another_entrypoint_no_callees, i64 784007058953177093, i32 2, i32 0)
134+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @another_entrypoint_no_callees, i64 784007058953177093, i32 2, i32 0)
148135
; INSTRUMENT-NEXT: [[T:%.*]] = icmp eq i32 [[A]], 0
149136
; INSTRUMENT-NEXT: br i1 [[T]], label [[YES:%.*]], label [[NO:%.*]]
150137
; INSTRUMENT: yes:
151-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_another_entrypoint_no_callees, i64 784007058953177093, i32 2, i32 1)
138+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @another_entrypoint_no_callees, i64 784007058953177093, i32 2, i32 1)
152139
; INSTRUMENT-NEXT: ret void
153140
; INSTRUMENT: no:
154141
; INSTRUMENT-NEXT: ret void
@@ -184,7 +171,7 @@ no:
184171
define void @simple(i32 %a) {
185172
; INSTRUMENT-LABEL: define void @simple(
186173
; INSTRUMENT-SAME: i32 [[A:%.*]]) {
187-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_simple, i64 742261418966908927, i32 1, i32 0)
174+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @simple, i64 742261418966908927, i32 1, i32 0)
188175
; INSTRUMENT-NEXT: ret void
189176
;
190177
; LOWERING-LABEL: define void @simple(
@@ -202,11 +189,11 @@ define void @simple(i32 %a) {
202189
define i32 @no_callsites(i32 %a) {
203190
; INSTRUMENT-LABEL: define i32 @no_callsites(
204191
; INSTRUMENT-SAME: i32 [[A:%.*]]) {
205-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_no_callsites, i64 784007058953177093, i32 2, i32 0)
192+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @no_callsites, i64 784007058953177093, i32 2, i32 0)
206193
; INSTRUMENT-NEXT: [[C:%.*]] = icmp eq i32 [[A]], 0
207194
; INSTRUMENT-NEXT: br i1 [[C]], label [[YES:%.*]], label [[NO:%.*]]
208195
; INSTRUMENT: yes:
209-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_no_callsites, i64 784007058953177093, i32 2, i32 1)
196+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @no_callsites, i64 784007058953177093, i32 2, i32 1)
210197
; INSTRUMENT-NEXT: ret i32 1
211198
; INSTRUMENT: no:
212199
; INSTRUMENT-NEXT: ret i32 0
@@ -238,8 +225,8 @@ no:
238225

239226
define void @no_counters() {
240227
; INSTRUMENT-LABEL: define void @no_counters() {
241-
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @__profn_no_counters, i64 742261418966908927, i32 1, i32 0)
242-
; INSTRUMENT-NEXT: call void @llvm.instrprof.callsite(ptr @__profn_no_counters, i64 742261418966908927, i32 1, i32 0, ptr @bar)
228+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @no_counters, i64 742261418966908927, i32 1, i32 0)
229+
; INSTRUMENT-NEXT: call void @llvm.instrprof.callsite(ptr @no_counters, i64 742261418966908927, i32 1, i32 0, ptr @bar)
243230
; INSTRUMENT-NEXT: call void @bar()
244231
; INSTRUMENT-NEXT: ret void
245232
;

llvm/test/Transforms/PGOProfile/ctx-prof-use-prelink.ll

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77

88
declare void @bar()
99

10-
;.
11-
; CHECK: @__profn_foo = private constant [3 x i8] c"foo"
12-
;.
1310
define void @foo(i32 %a, ptr %fct) {
1411
; CHECK-LABEL: define void @foo(
1512
; CHECK-SAME: i32 [[A:%.*]], ptr [[FCT:%.*]]) local_unnamed_addr !guid [[META0:![0-9]+]] {
16-
; CHECK-NEXT: call void @llvm.instrprof.increment(ptr @__profn_foo, i64 728453322856651412, i32 2, i32 0)
13+
; CHECK-NEXT: call void @llvm.instrprof.increment(ptr @foo, i64 728453322856651412, i32 2, i32 0)
1714
; CHECK-NEXT: [[T:%.*]] = icmp eq i32 [[A]], 0
1815
; CHECK-NEXT: br i1 [[T]], label %[[YES:.*]], label %[[NO:.*]]
1916
; CHECK: [[YES]]:
20-
; CHECK-NEXT: call void @llvm.instrprof.increment(ptr @__profn_foo, i64 728453322856651412, i32 2, i32 1)
21-
; CHECK-NEXT: call void @llvm.instrprof.callsite(ptr @__profn_foo, i64 728453322856651412, i32 2, i32 0, ptr [[FCT]])
17+
; CHECK-NEXT: call void @llvm.instrprof.increment(ptr @foo, i64 728453322856651412, i32 2, i32 1)
18+
; CHECK-NEXT: call void @llvm.instrprof.callsite(ptr @foo, i64 728453322856651412, i32 2, i32 0, ptr [[FCT]])
2219
; CHECK-NEXT: call void [[FCT]](i32 0)
2320
; CHECK-NEXT: br label %[[EXIT:.*]]
2421
; CHECK: [[NO]]:
25-
; CHECK-NEXT: call void @llvm.instrprof.callsite(ptr @__profn_foo, i64 728453322856651412, i32 2, i32 1, ptr @bar)
22+
; CHECK-NEXT: call void @llvm.instrprof.callsite(ptr @foo, i64 728453322856651412, i32 2, i32 1, ptr @bar)
2623
; CHECK-NEXT: call void @bar()
2724
; CHECK-NEXT: br label %[[EXIT]]
2825
; CHECK: [[EXIT]]:

0 commit comments

Comments
 (0)