Skip to content

Commit 82266d3

Browse files
authored
[nfc][ctx_prof] Factor the callsite instrumentation exclusion criteria (#108471)
Reusing this in the logic fetching the instrumentation in `CtxProfAnalysis`.
1 parent c0b7f1b commit 82266d3

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,12 @@ class InstrProfCallsite : public InstrProfCntrInstBase {
15871587
static bool classof(const Value *V) {
15881588
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
15891589
}
1590+
// We instrument direct calls (but not to intrinsics), or indirect calls.
1591+
static bool canInstrumentCallsite(const CallBase &CB) {
1592+
return !CB.isInlineAsm() &&
1593+
(CB.isIndirectCall() ||
1594+
(CB.getCalledFunction() && !CB.getCalledFunction()->isIntrinsic()));
1595+
}
15901596
Value *getCallee() const;
15911597
void setCallee(Value *Callee);
15921598
};

llvm/lib/Analysis/CtxProfAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ PreservedAnalyses CtxProfAnalysisPrinterPass::run(Module &M,
234234
}
235235

236236
InstrProfCallsite *CtxProfAnalysis::getCallsiteInstrumentation(CallBase &CB) {
237-
while (auto *Prev = CB.getPrevNode())
237+
for (auto *Prev = CB.getPrevNode(); Prev; Prev = Prev->getPrevNode())
238238
if (auto *IPC = dyn_cast<InstrProfCallsite>(Prev))
239239
return IPC;
240240
return nullptr;

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -945,9 +945,7 @@ void FunctionInstrumenter::instrument() {
945945
for (auto &BB : F)
946946
for (auto &Instr : BB)
947947
if (auto *CS = dyn_cast<CallBase>(&Instr)) {
948-
if ((CS->getCalledFunction() &&
949-
CS->getCalledFunction()->isIntrinsic()) ||
950-
dyn_cast<InlineAsm>(CS->getCalledOperand()))
948+
if (!InstrProfCallsite::canInstrumentCallsite(*CS))
951949
continue;
952950
Visitor(CS);
953951
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,26 @@ define void @no_counters() {
250250
call void @bar()
251251
ret void
252252
}
253+
254+
; Ensure "calls" to inline asm don't get callsite-instrumented.
255+
define void @inlineasm() {
256+
; INSTRUMENT-LABEL: define void @inlineasm() {
257+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @inlineasm, i64 742261418966908927, i32 1, i32 0)
258+
; INSTRUMENT-NEXT: call void asm "nop", ""()
259+
; INSTRUMENT-NEXT: ret void
260+
;
261+
; LOWERING-LABEL: define void @inlineasm(
262+
; LOWERING-SAME: ) !guid [[META6:![0-9]+]] {
263+
; LOWERING-NEXT: [[TMP1:%.*]] = call ptr @__llvm_ctx_profile_get_context(ptr @inlineasm, i64 -3771893999295659109, i32 1, i32 0)
264+
; LOWERING-NEXT: [[TMP2:%.*]] = ptrtoint ptr [[TMP1]] to i64
265+
; LOWERING-NEXT: [[TMP3:%.*]] = and i64 [[TMP2]], -2
266+
; LOWERING-NEXT: [[TMP4:%.*]] = inttoptr i64 [[TMP3]] to ptr
267+
; LOWERING-NEXT: call void asm "nop", ""()
268+
; LOWERING-NEXT: ret void
269+
;
270+
call void asm "nop", ""()
271+
ret void
272+
}
253273
;.
254274
; LOWERING: attributes #[[ATTR0:[0-9]+]] = { nounwind }
255275
; LOWERING: attributes #[[ATTR1:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
@@ -262,4 +282,5 @@ define void @no_counters() {
262282
; LOWERING: [[META3]] = !{i64 -3006003237940970099}
263283
; LOWERING: [[META4]] = !{i64 5679753335911435902}
264284
; LOWERING: [[META5]] = !{i64 5458232184388660970}
285+
; LOWERING: [[META6]] = !{i64 -3771893999295659109}
265286
;.

llvm/unittests/Analysis/CtxProfAnalysisTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ define void @another_entrypoint_no_callees(i32 %a) {
6464
ret void
6565
}
6666
67+
define void @inlineasm() {
68+
call void asm "nop", ""()
69+
ret void
70+
}
71+
6772
attributes #0 = { noinline }
6873
!0 = !{ i64 11872291593386833696 }
6974
)IR";
@@ -115,6 +120,27 @@ TEST_F(CtxProfAnalysisTest, GetCallsiteIDTest) {
115120
EXPECT_THAT(InsValues, testing::ElementsAre(0, 1));
116121
}
117122

123+
TEST_F(CtxProfAnalysisTest, GetCallsiteIDInlineAsmTest) {
124+
ModulePassManager MPM;
125+
MPM.addPass(PGOInstrumentationGen(PGOInstrumentationType::CTXPROF));
126+
EXPECT_FALSE(MPM.run(*M, MAM).areAllPreserved());
127+
auto *F = M->getFunction("inlineasm");
128+
ASSERT_NE(F, nullptr);
129+
std::vector<const Instruction *> InsValues;
130+
131+
for (auto &BB : *F)
132+
for (auto &I : BB)
133+
if (auto *CB = dyn_cast<CallBase>(&I)) {
134+
// Skip instrumentation inserted intrinsics.
135+
if (CB->getCalledFunction() && CB->getCalledFunction()->isIntrinsic())
136+
continue;
137+
auto *Ins = CtxProfAnalysis::getCallsiteInstrumentation(*CB);
138+
InsValues.push_back(Ins);
139+
}
140+
141+
EXPECT_THAT(InsValues, testing::ElementsAre(nullptr));
142+
}
143+
118144
TEST_F(CtxProfAnalysisTest, GetCallsiteIDNegativeTest) {
119145
auto *F = M->getFunction("foo");
120146
ASSERT_NE(F, nullptr);

0 commit comments

Comments
 (0)