Skip to content

Commit ddb67e6

Browse files
authored
[llvm][ctx_profile] Add the llvm.instrprof.callsite intrinsic (#89939)
Add the callsite intrinsic. Structurally, it is very similar to the counter intrinsics, hence the inheritance relationship. We can probably rename `InstrProfCntrInstBase` to `InstrProfIndexedBase` later - because the "counting" aspect is really left to derived types of `InstrProfCntrInstBase`, and it only concerns itself with the index aspect (which is what we care about for `callsite`, too) (Tracking Issue: #89287, RFC referenced there)
1 parent 4e34035 commit ddb67e6

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

llvm/docs/LangRef.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14139,6 +14139,41 @@ Semantics:
1413914139
""""""""""
1414014140
See description of '``llvm.instrprof.increment``' intrinsic.
1414114141

14142+
'``llvm.instrprof.callsite``' Intrinsic
14143+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14144+
14145+
Syntax:
14146+
"""""""
14147+
14148+
::
14149+
14150+
declare void @llvm.instrprof.callsite(ptr <name>, i64 <hash>,
14151+
i32 <num-counters>,
14152+
i32 <index>, ptr <callsite>)
14153+
14154+
Overview:
14155+
"""""""""
14156+
14157+
.. FIXME: detail when it's emitted once the support is added
14158+
14159+
The '``llvm.instrprof.callsite``' intrinsic should be emitted before a callsite
14160+
that's not to a "fake" callee (like another intrinsic or asm).
14161+
14162+
Arguments:
14163+
""""""""""
14164+
The first 4 arguments are similar to ``llvm.instrprof.increment``. The indexing
14165+
is specific to callsites, meaning callsites are indexed from 0, independent from
14166+
the indexes used by the other intrinsics (such as
14167+
``llvm.instrprof.increment[.step]``).
14168+
14169+
The last argument is the called value of the callsite this intrinsic precedes.
14170+
14171+
Semantics:
14172+
""""""""""
14173+
.. FIXME: detail how when the lowering pass is added.
14174+
14175+
This is lowered by contextual profiling.
14176+
1414214177
'``llvm.instrprof.timestamp``' Intrinsic
1414314178
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414414179

llvm/include/llvm/IR/IntrinsicInst.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,7 @@ class InstrProfInstBase : public IntrinsicInst {
14351435
case Intrinsic::instrprof_cover:
14361436
case Intrinsic::instrprof_increment:
14371437
case Intrinsic::instrprof_increment_step:
1438+
case Intrinsic::instrprof_callsite:
14381439
case Intrinsic::instrprof_timestamp:
14391440
case Intrinsic::instrprof_value_profile:
14401441
return true;
@@ -1519,6 +1520,21 @@ class InstrProfIncrementInstStep : public InstrProfIncrementInst {
15191520
}
15201521
};
15211522

1523+
/// This represents the llvm.instrprof.callsite intrinsic.
1524+
/// It is structurally like the increment or step counters, hence the
1525+
/// inheritance relationship, albeit somewhat tenuous (it's not 'counting' per
1526+
/// se)
1527+
class InstrProfCallsite : public InstrProfCntrInstBase {
1528+
public:
1529+
static bool classof(const IntrinsicInst *I) {
1530+
return I->getIntrinsicID() == Intrinsic::instrprof_callsite;
1531+
}
1532+
static bool classof(const Value *V) {
1533+
return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1534+
}
1535+
Value *getCallee() const;
1536+
};
1537+
15221538
/// This represents the llvm.instrprof.timestamp intrinsic.
15231539
class InstrProfTimestampInst : public InstrProfCntrInstBase {
15241540
public:

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,11 @@ def int_instrprof_increment_step : Intrinsic<[],
914914
[llvm_ptr_ty, llvm_i64_ty,
915915
llvm_i32_ty, llvm_i32_ty, llvm_i64_ty]>;
916916

917+
// Callsite instrumentation for contextual profiling
918+
def int_instrprof_callsite : Intrinsic<[],
919+
[llvm_ptr_ty, llvm_i64_ty,
920+
llvm_i32_ty, llvm_i32_ty, llvm_ptr_ty]>;
921+
917922
// A timestamp for instrumentation based profiling.
918923
def int_instrprof_timestamp : Intrinsic<[], [llvm_ptr_ty, llvm_i64_ty,
919924
llvm_i32_ty, llvm_i32_ty]>;

llvm/lib/IR/IntrinsicInst.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ Value *InstrProfIncrementInst::getStep() const {
291291
return ConstantInt::get(Type::getInt64Ty(Context), 1);
292292
}
293293

294+
Value *InstrProfCallsite::getCallee() const {
295+
if (isa<InstrProfCallsite>(this))
296+
return getArgOperand(4);
297+
return nullptr;
298+
}
299+
294300
std::optional<RoundingMode> ConstrainedFPIntrinsic::getRoundingMode() const {
295301
unsigned NumOperands = arg_size();
296302
Metadata *MD = nullptr;

llvm/unittests/IR/IntrinsicsTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ TEST_F(IntrinsicsTest, InstrProfInheritance) {
8181
__ISA(InstrProfCoverInst, InstrProfCntrInstBase);
8282
__ISA(InstrProfIncrementInst, InstrProfCntrInstBase);
8383
__ISA(InstrProfIncrementInstStep, InstrProfIncrementInst);
84+
__ISA(InstrProfCallsite, InstrProfCntrInstBase);
8485
__ISA(InstrProfTimestampInst, InstrProfCntrInstBase);
8586
__ISA(InstrProfValueProfileInst, InstrProfCntrInstBase);
8687
__ISA(InstrProfMCDCBitmapInstBase, InstrProfInstBase);
@@ -94,6 +95,7 @@ TEST_F(IntrinsicsTest, InstrProfInheritance) {
9495
{Intrinsic::instrprof_cover, isInstrProfCoverInst},
9596
{Intrinsic::instrprof_increment, isInstrProfIncrementInst},
9697
{Intrinsic::instrprof_increment_step, isInstrProfIncrementInstStep},
98+
{Intrinsic::instrprof_callsite, isInstrProfCallsite},
9799
{Intrinsic::instrprof_mcdc_condbitmap_update,
98100
isInstrProfMCDCCondBitmapUpdate},
99101
{Intrinsic::instrprof_mcdc_parameters,

0 commit comments

Comments
 (0)