Skip to content

Commit 8a2d049

Browse files
committed
[ctxprof] Instrument direct calls to other things than Functions
1 parent 96336b2 commit 8a2d049

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,8 @@ class CallBase : public Instruction {
13431343
Use &getCalledOperandUse() { return Op<CalledOperandOpEndIdx>(); }
13441344

13451345
/// Returns the function called, or null if this is an indirect function
1346-
/// invocation or the function signature does not match the call signature.
1346+
/// invocation or the function signature does not match the call signature, or
1347+
/// the call target is an alias.
13471348
Function *getCalledFunction() const {
13481349
if (auto *F = dyn_cast_or_null<Function>(getCalledOperand()))
13491350
if (F->getValueType() == getFunctionType())

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ class InstrProfCallsite : public InstrProfCntrInstBase {
15361536
static bool canInstrumentCallsite(const CallBase &CB) {
15371537
return !CB.isInlineAsm() &&
15381538
(CB.isIndirectCall() ||
1539-
(CB.getCalledFunction() && !CB.getCalledFunction()->isIntrinsic()));
1539+
(CB.getIntrinsicID() == Intrinsic::not_intrinsic));
15401540
}
15411541
LLVM_ABI Value *getCallee() const;
15421542
LLVM_ABI void setCallee(Value *Callee);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; REQUIRES: x86-registered-target
2+
; Test that calls to aliases are instrumented, and the assembly references the
3+
; aliased function.
4+
;
5+
; RUN: opt -passes=ctx-instr-gen,assign-guid,ctx-instr-lower -profile-context-root=an_entrypoint \
6+
; RUN: -profile-context-root=another_entrypoint_no_callees \
7+
; RUN: -S %s -o %t.ll
8+
; RUN: llc < %t.ll | FileCheck %s
9+
target triple = "x86_64-unknown-linux-gnu"
10+
11+
@foo_alias = weak_odr unnamed_addr alias void (), ptr @foo
12+
13+
define void @foo(i32) {
14+
ret void
15+
}
16+
17+
define void @call_alias(ptr %a) {
18+
entry:
19+
call void @foo(i32 0, ptr %a)
20+
ret void
21+
}
22+
23+
; CHECK-LABEL: call_alias:
24+
; CHECK: movq foo@GOTPCREL(%rip), [[REG:%r[a-z0-9]+]]
25+
; CHECK-NEXT: movq [[REG]], %fs:__llvm_ctx_profile_expected_callee@TPOFF{{.*}}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
declare void @bar()
1010

11+
;.
12+
; INSTRUMENT: @foo_alias = weak_odr unnamed_addr alias void (i32, ptr), ptr @foo
1113
;.
1214
; LOWERING: @__llvm_ctx_profile_callsite = external hidden thread_local global ptr
1315
; LOWERING: @__llvm_ctx_profile_expected_callee = external hidden thread_local global ptr
@@ -19,6 +21,8 @@ declare void @bar()
1921
; LOWERING: @[[GLOB5:[0-9]+]] = internal global { ptr, ptr, ptr, ptr, i8 } zeroinitializer
2022
; LOWERING: @[[GLOB6:[0-9]+]] = internal global { ptr, ptr, ptr, ptr, i8 } zeroinitializer
2123
; LOWERING: @[[GLOB7:[0-9]+]] = internal global { ptr, ptr, ptr, ptr, i8 } { ptr null, ptr null, ptr inttoptr (i64 1 to ptr), ptr null, i8 0 }
24+
; LOWERING: @[[GLOB8:[0-9]+]] = internal global { ptr, ptr, ptr, ptr, i8 } zeroinitializer
25+
; LOWERING: @foo_alias = weak_odr unnamed_addr alias void (i32, ptr), ptr @foo
2226
;.
2327
define void @foo(i32 %a, ptr %fct) {
2428
; INSTRUMENT-LABEL: define void @foo(
@@ -335,6 +339,41 @@ define void @unreachable() {
335339
;
336340
unreachable
337341
}
342+
343+
@foo_alias = weak_odr unnamed_addr alias void (i32, ptr), ptr @foo
344+
345+
define void @call_alias(ptr %a) {
346+
; INSTRUMENT-LABEL: define void @call_alias(
347+
; INSTRUMENT-SAME: ptr [[A:%.*]]) {
348+
; INSTRUMENT-NEXT: entry:
349+
; INSTRUMENT-NEXT: call void @llvm.instrprof.increment(ptr @call_alias, i64 742261418966908927, i32 1, i32 0)
350+
; INSTRUMENT-NEXT: call void @llvm.instrprof.callsite(ptr @call_alias, i64 742261418966908927, i32 1, i32 0, ptr @foo_alias)
351+
; INSTRUMENT-NEXT: call void @foo_alias(i32 0, ptr [[A]])
352+
; INSTRUMENT-NEXT: ret void
353+
;
354+
; LOWERING-LABEL: define void @call_alias(
355+
; LOWERING-SAME: ptr [[A:%.*]]) !guid [[META10:![0-9]+]] {
356+
; LOWERING-NEXT: entry:
357+
; LOWERING-NEXT: [[TMP0:%.*]] = call ptr @__llvm_ctx_profile_get_context(ptr @[[GLOB8]], ptr @call_alias, i64 2172368043968427688, i32 1, i32 1)
358+
; LOWERING-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[TMP0]] to i64
359+
; LOWERING-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], 1
360+
; LOWERING-NEXT: [[TMP3:%.*]] = call ptr @llvm.threadlocal.address.p0(ptr @__llvm_ctx_profile_expected_callee)
361+
; LOWERING-NEXT: [[TMP4:%.*]] = getelementptr ptr, ptr [[TMP3]], i64 [[TMP2]]
362+
; LOWERING-NEXT: [[TMP5:%.*]] = call ptr @llvm.threadlocal.address.p0(ptr @__llvm_ctx_profile_callsite)
363+
; LOWERING-NEXT: [[TMP6:%.*]] = getelementptr i32, ptr [[TMP5]], i64 [[TMP2]]
364+
; LOWERING-NEXT: [[TMP7:%.*]] = and i64 [[TMP1]], -2
365+
; LOWERING-NEXT: [[TMP8:%.*]] = inttoptr i64 [[TMP7]] to ptr
366+
; LOWERING-NEXT: store volatile ptr @foo_alias, ptr [[TMP4]], align 8
367+
; LOWERING-NEXT: [[TMP9:%.*]] = getelementptr { { i64, ptr, i32, i32 }, [1 x i64], [1 x ptr] }, ptr [[TMP0]], i32 0, i32 2, i32 0
368+
; LOWERING-NEXT: store volatile ptr [[TMP9]], ptr [[TMP6]], align 8
369+
; LOWERING-NEXT: call void @foo_alias(i32 0, ptr [[A]])
370+
; LOWERING-NEXT: call void @__llvm_ctx_profile_release_context(ptr @[[GLOB8]])
371+
; LOWERING-NEXT: ret void
372+
;
373+
entry:
374+
call void @foo_alias(i32 0, ptr %a)
375+
ret void
376+
}
338377
;.
339378
; LOWERING: attributes #[[ATTR0]] = { noreturn }
340379
; LOWERING: attributes #[[ATTR1:[0-9]+]] = { nounwind }
@@ -353,4 +392,5 @@ define void @unreachable() {
353392
; LOWERING: [[META7]] = !{i64 -4680624981836544329}
354393
; LOWERING: [[META8]] = !{i64 5519225910966780583}
355394
; LOWERING: [[META9]] = !{i64 -565652589829076809}
395+
; LOWERING: [[META10]] = !{i64 2172368043968427688}
356396
;.

0 commit comments

Comments
 (0)