Skip to content

Commit 96568f3

Browse files
authored
[llvm][ctx_profile] Add instrumentation lowering (llvm#90821)
This adds the instrumentation lowering pass. (Tracking Issue: llvm#89287, RFC referenced there)
1 parent 1710c8c commit 96568f3

File tree

8 files changed

+626
-6
lines changed

8 files changed

+626
-6
lines changed

llvm/docs/LangRef.rst

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14111,6 +14111,25 @@ structures and the code to increment the appropriate value, in a
1411114111
format that can be written out by a compiler runtime and consumed via
1411214112
the ``llvm-profdata`` tool.
1411314113

14114+
.. FIXME: write complete doc on contextual instrumentation and link from here
14115+
.. and from llvm.instrprof.callsite.
14116+
14117+
The intrinsic is lowered differently for contextual profiling by the
14118+
``-ctx-instr-lower`` pass. Here:
14119+
14120+
* the entry basic block increment counter is lowered as a call to compiler-rt,
14121+
to either ``__llvm_ctx_profile_start_context`` or
14122+
``__llvm_ctx_profile_get_context``. Either returns a pointer to a context object
14123+
which contains a buffer into which counter increments can happen. Note that the
14124+
pointer value returned by compiler-rt may have its LSB set - counter increments
14125+
happen offset from the address with the LSB cleared.
14126+
14127+
* all the other lowerings of ``llvm.instrprof.increment[.step]`` happen within
14128+
that context.
14129+
14130+
* the context is assumed to be a local value to the function, and no concurrency
14131+
concerns need to be handled by LLVM.
14132+
1411414133
'``llvm.instrprof.increment.step``' Intrinsic
1411514134
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1411614135

@@ -14156,10 +14175,10 @@ Syntax:
1415614175
Overview:
1415714176
"""""""""
1415814177

14159-
.. FIXME: detail when it's emitted once the support is added
14160-
1416114178
The '``llvm.instrprof.callsite``' intrinsic should be emitted before a callsite
14162-
that's not to a "fake" callee (like another intrinsic or asm).
14179+
that's not to a "fake" callee (like another intrinsic or asm). It is used by
14180+
contextual profiling and has side-effects. Its lowering happens in IR, and
14181+
target-specific backends should never encounter it.
1416314182

1416414183
Arguments:
1416514184
""""""""""
@@ -14172,9 +14191,28 @@ The last argument is the called value of the callsite this intrinsic precedes.
1417214191

1417314192
Semantics:
1417414193
""""""""""
14175-
.. FIXME: detail how when the lowering pass is added.
1417614194

14177-
This is lowered by contextual profiling.
14195+
This is lowered by contextual profiling. In contextual profiling, functions get,
14196+
from compiler-rt, a pointer to a context object. The context object consists of
14197+
a buffer LLVM can use to perform counter increments (i.e. the lowering of
14198+
``llvm.instrprof.increment[.step]``. The address range following the counter
14199+
buffer, ``<num-counters>`` x ``sizeof(ptr)`` - sized, is expected to contain
14200+
pointers to contexts of functions called from this function ("subcontexts").
14201+
LLVM does not dereference into that memory region, just calculates GEPs.
14202+
14203+
The lowering of ``llvm.instrprof.callsite`` consists of:
14204+
14205+
* write to ``__llvm_ctx_profile_expected_callee`` the ``<callsite>`` value;
14206+
14207+
* write to ``__llvm_ctx_profile_callsite`` the address into this function's
14208+
context of the ``<index>`` position into the subcontexts region.
14209+
14210+
14211+
``__llvm_ctx_profile_{expected_callee|callsite}`` are initialized by compiler-rt
14212+
and are TLS. They are both vectors of pointers of size 2. The index into each is
14213+
determined when the current function obtains the pointer to its context from
14214+
compiler-rt. The pointer's LSB gives the index.
14215+
1417814216

1417914217
'``llvm.instrprof.timestamp``' Intrinsic
1418014218
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

llvm/include/llvm/Transforms/Instrumentation/PGOCtxProfLowering.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_PGOCTXPROFLOWERING_H
1313
#define LLVM_TRANSFORMS_INSTRUMENTATION_PGOCTXPROFLOWERING_H
1414

15+
#include "llvm/IR/PassManager.h"
1516
namespace llvm {
1617
class Type;
1718

18-
class PGOCtxProfLoweringPass {
19+
class PGOCtxProfLoweringPass : public PassInfoMixin<PGOCtxProfLoweringPass> {
1920
public:
2021
explicit PGOCtxProfLoweringPass() = default;
2122
static bool isContextualIRPGOEnabled();
23+
24+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
2225
};
2326
} // namespace llvm
2427
#endif

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
#include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h"
178178
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
179179
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
180+
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
180181
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
181182
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
182183
#include "llvm/Transforms/Instrumentation/PoisonChecking.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include "llvm/Transforms/Instrumentation/InstrOrderFile.h"
7575
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
7676
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
77+
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
7778
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
7879
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
7980
#include "llvm/Transforms/Scalar/ADCE.h"
@@ -834,6 +835,10 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
834835
PTO.EagerlyInvalidateAnalyses));
835836
}
836837

838+
if (PGOCtxProfLoweringPass::isContextualIRPGOEnabled()) {
839+
MPM.addPass(PGOCtxProfLoweringPass());
840+
return;
841+
}
837842
// Add the profile lowering pass.
838843
InstrProfOptions Options;
839844
if (!ProfileFile.empty())

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ MODULE_PASS("inliner-wrapper-no-mandatory-first",
7777
MODULE_PASS("insert-gcov-profiling", GCOVProfilerPass())
7878
MODULE_PASS("instrorderfile", InstrOrderFilePass())
7979
MODULE_PASS("instrprof", InstrProfilingLoweringPass())
80+
MODULE_PASS("ctx-instr-lower", PGOCtxProfLoweringPass())
8081
MODULE_PASS("internalize", InternalizePass())
8182
MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())
8283
MODULE_PASS("iroutliner", IROutlinerPass())

0 commit comments

Comments
 (0)