Skip to content

Commit

Permalink
[llvm][ctx_profile] Add instrumentation lowering (#90821)
Browse files Browse the repository at this point in the history
This adds the instrumentation lowering pass.

(Tracking Issue: #89287, RFC referenced there)
  • Loading branch information
mtrofin committed May 8, 2024
1 parent 1710c8c commit 96568f3
Show file tree
Hide file tree
Showing 8 changed files with 626 additions and 6 deletions.
48 changes: 43 additions & 5 deletions llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14111,6 +14111,25 @@ structures and the code to increment the appropriate value, in a
format that can be written out by a compiler runtime and consumed via
the ``llvm-profdata`` tool.

.. FIXME: write complete doc on contextual instrumentation and link from here
.. and from llvm.instrprof.callsite.

The intrinsic is lowered differently for contextual profiling by the
``-ctx-instr-lower`` pass. Here:

* the entry basic block increment counter is lowered as a call to compiler-rt,
to either ``__llvm_ctx_profile_start_context`` or
``__llvm_ctx_profile_get_context``. Either returns a pointer to a context object
which contains a buffer into which counter increments can happen. Note that the
pointer value returned by compiler-rt may have its LSB set - counter increments
happen offset from the address with the LSB cleared.

* all the other lowerings of ``llvm.instrprof.increment[.step]`` happen within
that context.

* the context is assumed to be a local value to the function, and no concurrency
concerns need to be handled by LLVM.

'``llvm.instrprof.increment.step``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -14156,10 +14175,10 @@ Syntax:
Overview:
"""""""""

.. FIXME: detail when it's emitted once the support is added

The '``llvm.instrprof.callsite``' intrinsic should be emitted before a callsite
that's not to a "fake" callee (like another intrinsic or asm).
that's not to a "fake" callee (like another intrinsic or asm). It is used by
contextual profiling and has side-effects. Its lowering happens in IR, and
target-specific backends should never encounter it.

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

Semantics:
""""""""""
.. FIXME: detail how when the lowering pass is added.

This is lowered by contextual profiling.
This is lowered by contextual profiling. In contextual profiling, functions get,
from compiler-rt, a pointer to a context object. The context object consists of
a buffer LLVM can use to perform counter increments (i.e. the lowering of
``llvm.instrprof.increment[.step]``. The address range following the counter
buffer, ``<num-counters>`` x ``sizeof(ptr)`` - sized, is expected to contain
pointers to contexts of functions called from this function ("subcontexts").
LLVM does not dereference into that memory region, just calculates GEPs.

The lowering of ``llvm.instrprof.callsite`` consists of:

* write to ``__llvm_ctx_profile_expected_callee`` the ``<callsite>`` value;

* write to ``__llvm_ctx_profile_callsite`` the address into this function's
context of the ``<index>`` position into the subcontexts region.


``__llvm_ctx_profile_{expected_callee|callsite}`` are initialized by compiler-rt
and are TLS. They are both vectors of pointers of size 2. The index into each is
determined when the current function obtains the pointer to its context from
compiler-rt. The pointer's LSB gives the index.


'``llvm.instrprof.timestamp``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_PGOCTXPROFLOWERING_H
#define LLVM_TRANSFORMS_INSTRUMENTATION_PGOCTXPROFLOWERING_H

#include "llvm/IR/PassManager.h"
namespace llvm {
class Type;

class PGOCtxProfLoweringPass {
class PGOCtxProfLoweringPass : public PassInfoMixin<PGOCtxProfLoweringPass> {
public:
explicit PGOCtxProfLoweringPass() = default;
static bool isContextualIRPGOEnabled();

PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
};
} // namespace llvm
#endif
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
#include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h"
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
#include "llvm/Transforms/Instrumentation/PoisonChecking.h"
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include "llvm/Transforms/Instrumentation/InstrOrderFile.h"
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
#include "llvm/Transforms/Scalar/ADCE.h"
Expand Down Expand Up @@ -834,6 +835,10 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
PTO.EagerlyInvalidateAnalyses));
}

if (PGOCtxProfLoweringPass::isContextualIRPGOEnabled()) {
MPM.addPass(PGOCtxProfLoweringPass());
return;
}
// Add the profile lowering pass.
InstrProfOptions Options;
if (!ProfileFile.empty())
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ MODULE_PASS("inliner-wrapper-no-mandatory-first",
MODULE_PASS("insert-gcov-profiling", GCOVProfilerPass())
MODULE_PASS("instrorderfile", InstrOrderFilePass())
MODULE_PASS("instrprof", InstrProfilingLoweringPass())
MODULE_PASS("ctx-instr-lower", PGOCtxProfLoweringPass())
MODULE_PASS("internalize", InternalizePass())
MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())
MODULE_PASS("iroutliner", IROutlinerPass())
Expand Down
Loading

0 comments on commit 96568f3

Please sign in to comment.