LLVM 24.0.0git
PGOCtxProfLowering.cpp
Go to the documentation of this file.
1//===- PGOCtxProfLowering.cpp - Contextual PGO Instr. Lowering ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/Analysis/CFG.h"
15#include "llvm/IR/Analysis.h"
16#include "llvm/IR/Constants.h"
18#include "llvm/IR/GlobalValue.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/InstrTypes.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/PassManager.h"
27#include <utility>
28
29using namespace llvm;
30
31#define DEBUG_TYPE "ctx-instr-lower"
32
34 const cl::LibraryOptionsContext *LibraryOptions) {
36 .profile_context_root.empty();
37}
38
39// the names of symbols we expect in compiler-rt. Using a namespace for
40// readability.
42static auto StartCtx = "__llvm_ctx_profile_start_context";
43static auto ReleaseCtx = "__llvm_ctx_profile_release_context";
44static auto GetCtx = "__llvm_ctx_profile_get_context";
45static auto ExpectedCalleeTLS = "__llvm_ctx_profile_expected_callee";
46static auto CallsiteTLS = "__llvm_ctx_profile_callsite";
47} // namespace CompilerRtAPINames
48
49namespace {
50// The lowering logic and state.
51class CtxInstrumentationLowerer final {
52 Module &M;
54 Type *ContextNodeTy = nullptr;
55 StructType *FunctionDataTy = nullptr;
56
57 DenseSet<const Function *> ContextRootSet;
58 Function *StartCtx = nullptr;
59 Function *GetCtx = nullptr;
60 Function *ReleaseCtx = nullptr;
61 GlobalVariable *ExpectedCalleeTLS = nullptr;
62 GlobalVariable *CallsiteInfoTLS = nullptr;
63 Constant *CannotBeRootInitializer = nullptr;
64
65public:
66 CtxInstrumentationLowerer(Module &M, ModuleAnalysisManager &MAM);
67 // return true if lowering happened (i.e. a change was made)
68 bool lowerFunction(Function &F);
69};
70
71// llvm.instrprof.increment[.step] captures the total number of counters as one
72// of its parameters, and llvm.instrprof.callsite captures the total number of
73// callsites. Those values are the same for instances of those intrinsics in
74// this function. Find the first instance of each and return them.
75std::pair<uint32_t, uint32_t> getNumCountersAndCallsites(const Function &F) {
76 uint32_t NumCounters = 0;
77 uint32_t NumCallsites = 0;
78 for (const auto &BB : F) {
79 for (const auto &I : BB) {
80 if (const auto *Incr = dyn_cast<InstrProfIncrementInst>(&I)) {
81 uint32_t V =
82 static_cast<uint32_t>(Incr->getNumCounters()->getZExtValue());
83 assert((!NumCounters || V == NumCounters) &&
84 "expected all llvm.instrprof.increment[.step] intrinsics to "
85 "have the same total nr of counters parameter");
86 NumCounters = V;
87 } else if (const auto *CSIntr = dyn_cast<InstrProfCallsite>(&I)) {
88 uint32_t V =
89 static_cast<uint32_t>(CSIntr->getNumCounters()->getZExtValue());
90 assert((!NumCallsites || V == NumCallsites) &&
91 "expected all llvm.instrprof.callsite intrinsics to have the "
92 "same total nr of callsites parameter");
93 NumCallsites = V;
94 }
95#ifdef NDEBUG
96 if (NumCounters && NumCallsites)
97 return std::make_pair(NumCounters, NumCallsites);
98#endif
99 }
100 }
101 return {NumCounters, NumCallsites};
102}
103
104void emitUnsupportedRootError(const Function &F, StringRef Reason) {
105 F.getContext().emitError("[ctxprof] The function " + F.getName() +
106 " was indicated as context root but " + Reason +
107 ", which is not supported.");
108}
109} // namespace
110
111// set up tie-in with compiler-rt.
112// NOTE!!!
113// These have to match compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
114CtxInstrumentationLowerer::CtxInstrumentationLowerer(Module &M,
116 : M(M), MAM(MAM) {
117 auto *PointerTy = PointerType::get(M.getContext(), 0);
118 auto *SanitizerMutexType = Type::getInt8Ty(M.getContext());
119 auto *I32Ty = Type::getInt32Ty(M.getContext());
120 auto *I64Ty = Type::getInt64Ty(M.getContext());
121
122#define _PTRDECL(_, __) PointerTy,
123#define _VOLATILE_PTRDECL(_, __) PointerTy,
124#define _CONTEXT_ROOT PointerTy,
125#define _MUTEXDECL(_) SanitizerMutexType,
126
127 FunctionDataTy = StructType::get(
128 M.getContext(), {CTXPROF_FUNCTION_DATA(_PTRDECL, _CONTEXT_ROOT,
129 _VOLATILE_PTRDECL, _MUTEXDECL)});
130#undef _PTRDECL
131#undef _CONTEXT_ROOT
132#undef _VOLATILE_PTRDECL
133#undef _MUTEXDECL
134
135#define _PTRDECL(_, __) Constant::getNullValue(PointerTy),
136#define _VOLATILE_PTRDECL(_, __) _PTRDECL(_, __)
137#define _MUTEXDECL(_) Constant::getNullValue(SanitizerMutexType),
138#define _CONTEXT_ROOT \
139 Constant::getIntegerValue( \
140 PointerTy, \
141 APInt(M.getDataLayout().getPointerTypeSizeInBits(PointerTy), 1U)),
142 CannotBeRootInitializer = ConstantStruct::get(
145#undef _PTRDECL
146#undef _CONTEXT_ROOT
147#undef _VOLATILE_PTRDECL
148#undef _MUTEXDECL
149
150 // The Context header.
151 ContextNodeTy = StructType::get(M.getContext(), {
152 I64Ty, /*Guid*/
153 PointerTy, /*Next*/
154 I32Ty, /*NumCounters*/
155 I32Ty, /*NumCallsites*/
156 });
157
158 const InstrumentationOptions &Opts =
159 M.getContext().getLibraryOptions().get<InstrumentationOptions>();
160 // Define a global for each entrypoint. We'll reuse the entrypoint's name
161 // as prefix. We assume the entrypoint names to be unique.
162 for (StringRef Fname : Opts.profile_context_root) {
163 if (const auto *F = M.getFunction(Fname)) {
164 if (F->isDeclaration())
165 continue;
166 ContextRootSet.insert(F);
167 for (const auto &BB : *F)
168 for (const auto &I : BB)
169 if (const auto *CB = dyn_cast<CallBase>(&I))
170 if (CB->isMustTailCall())
171 emitUnsupportedRootError(*F, "it features musttail calls");
172 }
173 }
174
175 // Declare the functions we will call.
177 M.getOrInsertFunction(
179 FunctionType::get(PointerTy,
180 {PointerTy, /*FunctionData*/
181 I64Ty, /*Guid*/ I32Ty,
182 /*NumCounters*/ I32Ty /*NumCallsites*/},
183 false))
184 .getCallee());
186 M.getOrInsertFunction(CompilerRtAPINames::GetCtx,
187 FunctionType::get(PointerTy,
188 {PointerTy, /*FunctionData*/
189 PointerTy, /*Callee*/
190 I64Ty, /*Guid*/
191 I32Ty, /*NumCounters*/
192 I32Ty}, /*NumCallsites*/
193 false))
194 .getCallee());
196 M.getOrInsertFunction(CompilerRtAPINames::ReleaseCtx,
197 FunctionType::get(Type::getVoidTy(M.getContext()),
198 {
199 PointerTy, /*FunctionData*/
200 },
201 false))
202 .getCallee());
203
204 // Declare the TLSes we will need to use.
205 CallsiteInfoTLS =
206 new GlobalVariable(M, PointerTy, false, GlobalValue::ExternalLinkage,
208 CallsiteInfoTLS->setThreadLocal(true);
209 CallsiteInfoTLS->setVisibility(llvm::GlobalValue::HiddenVisibility);
211 new GlobalVariable(M, PointerTy, false, GlobalValue::ExternalLinkage,
213 ExpectedCalleeTLS->setThreadLocal(true);
215}
216
219 CtxInstrumentationLowerer Lowerer(M, MAM);
220 bool Changed = false;
221 for (auto &F : M)
222 Changed |= Lowerer.lowerFunction(F);
224}
225
226bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
227 if (F.isDeclaration())
228 return false;
229
230 // Probably pointless to try to do anything here, unlikely to be
231 // performance-affecting.
232 if (!llvm::canReturn(F)) {
233 for (auto &BB : F)
234 for (auto &I : make_early_inc_range(BB))
236 I.eraseFromParent();
237 if (ContextRootSet.contains(&F))
238 emitUnsupportedRootError(F, "it does not return");
239 return true;
240 }
241
242 auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
243 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
244
245 Value *Guid = nullptr;
246 auto [NumCounters, NumCallsites] = getNumCountersAndCallsites(F);
247
248 Value *Context = nullptr;
249 Value *RealContext = nullptr;
250
251 StructType *ThisContextType = nullptr;
252 Value *TheRootFunctionData = nullptr;
253 Value *ExpectedCalleeTLSAddr = nullptr;
254 Value *CallsiteInfoTLSAddr = nullptr;
255 const bool HasMusttail = [&F]() {
256 for (auto &BB : F)
257 for (auto &I : BB)
258 if (auto *CB = dyn_cast<CallBase>(&I))
259 if (CB->isMustTailCall())
260 return true;
261 return false;
262 }();
263
264 if (HasMusttail && ContextRootSet.contains(&F)) {
265 F.getContext().emitError(
266 "[ctx_prof] A function with musttail calls was explicitly requested as "
267 "root. That is not supported because we cannot instrument a return "
268 "instruction to release the context: " +
269 F.getName());
270 return false;
271 }
272 auto &Head = F.getEntryBlock();
273 for (auto &I : Head) {
274 // Find the increment intrinsic in the entry basic block.
275 if (auto *Mark = dyn_cast<InstrProfIncrementInst>(&I)) {
276 assert(Mark->getIndex()->isZero());
277
278 IRBuilder<> Builder(Mark);
279 Guid = Builder.getInt64(cast<Function>(*Mark->getNameValue()).getGUID());
280 // The type of the context of this function is now knowable since we have
281 // NumCallsites and NumCounters. We declare it here because it's more
282 // convenient - we have the Builder.
283 ThisContextType = StructType::get(
284 F.getContext(),
285 {ContextNodeTy, ArrayType::get(Builder.getInt64Ty(), NumCounters),
286 ArrayType::get(Builder.getPtrTy(), NumCallsites)});
287 // Figure out which way we obtain the context object for this function -
288 // if it's an entrypoint, then we call StartCtx, otherwise GetCtx. In the
289 // former case, we also set TheRootFunctionData since we need to release
290 // it at the end (plus it can be used to know if we have an entrypoint or
291 // a regular function). Don't set a name, they end up taking a lot of
292 // space and we don't need them.
293
294 // Zero-initialize the FunctionData, except for functions that have
295 // musttail calls. There, we set the CtxRoot field to 1, which will be
296 // treated as a "can't be set as root".
297 TheRootFunctionData = new GlobalVariable(
298 M, FunctionDataTy, false, GlobalVariable::InternalLinkage,
299 HasMusttail ? CannotBeRootInitializer
300 : Constant::getNullValue(FunctionDataTy));
301
302 if (ContextRootSet.contains(&F)) {
303 Context = Builder.CreateCall(
304 StartCtx, {TheRootFunctionData, Guid, Builder.getInt32(NumCounters),
305 Builder.getInt32(NumCallsites)});
306 ORE.emit(
307 [&] { return OptimizationRemark(DEBUG_TYPE, "Entrypoint", &F); });
308 } else {
309 Context = Builder.CreateCall(GetCtx, {TheRootFunctionData, &F, Guid,
310 Builder.getInt32(NumCounters),
311 Builder.getInt32(NumCallsites)});
312 ORE.emit([&] {
313 return OptimizationRemark(DEBUG_TYPE, "RegularFunction", &F);
314 });
315 }
316 // The context could be scratch.
317 auto *CtxAsInt = Builder.CreatePtrToInt(Context, Builder.getInt64Ty());
318 if (NumCallsites > 0) {
319 // Figure out which index of the TLS 2-element buffers to use.
320 // Scratch context => we use index == 1. Real contexts => index == 0.
321 auto *Index = Builder.CreateAnd(CtxAsInt, Builder.getInt64(1));
322 // The GEPs corresponding to that index, in the respective TLS.
323 ExpectedCalleeTLSAddr = Builder.CreateGEP(
324 PointerType::getUnqual(F.getContext()),
325 Builder.CreateThreadLocalAddress(ExpectedCalleeTLS), {Index});
326 CallsiteInfoTLSAddr = Builder.CreateGEP(
327 Builder.getInt32Ty(),
328 Builder.CreateThreadLocalAddress(CallsiteInfoTLS), {Index});
329 }
330 // Because the context pointer may have LSB set (to indicate scratch),
331 // clear it for the value we use as base address for the counter vector.
332 // This way, if later we want to have "real" (not clobbered) buffers
333 // acting as scratch, the lowering (at least this part of it that deals
334 // with counters) stays the same.
335 RealContext = Builder.CreateIntToPtr(
336 Builder.CreateAnd(CtxAsInt, Builder.getInt64(-2)),
337 PointerType::getUnqual(F.getContext()));
338 I.eraseFromParent();
339 break;
340 }
341 }
342 if (!Context) {
343 ORE.emit([&] {
344 return OptimizationRemarkMissed(DEBUG_TYPE, "Skip", &F)
345 << "Function doesn't have instrumentation, skipping";
346 });
347 return false;
348 }
349
350 bool ContextWasReleased = false;
351 for (auto &BB : F) {
352 for (auto &I : llvm::make_early_inc_range(BB)) {
353 if (auto *Instr = dyn_cast<InstrProfCntrInstBase>(&I)) {
354 IRBuilder<> Builder(Instr);
355 switch (Instr->getIntrinsicID()) {
356 case llvm::Intrinsic::instrprof_increment:
357 case llvm::Intrinsic::instrprof_increment_step: {
358 // Increments (or increment-steps) are just a typical load - increment
359 // - store in the RealContext.
360 auto *AsStep = cast<InstrProfIncrementInst>(Instr);
361 auto *GEP = Builder.CreateGEP(
362 ThisContextType, RealContext,
363 {Builder.getInt32(0), Builder.getInt32(1), AsStep->getIndex()});
364 Builder.CreateStore(
365 Builder.CreateAdd(Builder.CreateLoad(Builder.getInt64Ty(), GEP),
366 AsStep->getStep()),
367 GEP);
368 } break;
369 case llvm::Intrinsic::instrprof_callsite:
370 // callsite lowering: write the called value in the expected callee
371 // TLS we treat the TLS as volatile because of signal handlers and to
372 // avoid these being moved away from the callsite they decorate.
373 auto *CSIntrinsic = dyn_cast<InstrProfCallsite>(Instr);
374 Builder.CreateStore(CSIntrinsic->getCallee(), ExpectedCalleeTLSAddr,
375 true);
376 // write the GEP of the slot in the sub-contexts portion of the
377 // context in TLS. Now, here, we use the actual Context value - as
378 // returned from compiler-rt - which may have the LSB set if the
379 // Context was scratch. Since the header of the context object and
380 // then the values are all 8-aligned (or, really, insofar as we care,
381 // they are even) - if the context is scratch (meaning, an odd value),
382 // so will the GEP. This is important because this is then visible to
383 // compiler-rt which will produce scratch contexts for callers that
384 // have a scratch context.
385 Builder.CreateStore(
386 Builder.CreateGEP(ThisContextType, Context,
387 {Builder.getInt32(0), Builder.getInt32(2),
388 CSIntrinsic->getIndex()}),
389 CallsiteInfoTLSAddr, true);
390 break;
391 }
392 I.eraseFromParent();
393 } else if (!HasMusttail && isa<ReturnInst>(I)) {
394 // Remember to release the context if we are an entrypoint.
395 IRBuilder<> Builder(&I);
396 Builder.CreateCall(ReleaseCtx, {TheRootFunctionData});
397 ContextWasReleased = true;
398 }
399 }
400 }
401 if (!HasMusttail && !ContextWasReleased)
403 "[ctx_prof] A function that doesn't have musttail calls was "
404 "instrumented but it has no `ret` "
405 "instructions above which to release the context: " +
406 F.getName());
407 return true;
408}
409
412 bool Changed = false;
413 for (auto &F : M) {
414 if (F.isDeclaration())
415 continue;
416 if (F.hasFnAttribute(Attribute::NoInline))
417 continue;
418 if (!F.isWeakForLinker())
419 continue;
420
421 if (F.hasFnAttribute(Attribute::AlwaysInline))
422 F.removeFnAttr(Attribute::AlwaysInline);
423
424 F.addFnAttr(Attribute::NoInline);
425 Changed = true;
426 }
427 if (Changed)
429 return PreservedAnalyses::all();
430}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define CTXPROF_FUNCTION_DATA(PTRDECL, CONTEXT_PTR, VOLATILE_PTRDECL, MUTEXDECL)
The internal structure of FunctionData.
#define DEBUG_TYPE
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define _MUTEXDECL(_)
#define _VOLATILE_PTRDECL(_, __)
#define _PTRDECL(_, __)
#define _CONTEXT_ROOT
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This file contains some templates that are useful if you are working with the STL at all.
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2918
LLVM_ABI void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
Diagnostic information for missed-optimization remarks.
Diagnostic information for applied optimization remarks.
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
static LLVM_ABI bool isCtxIRPGOInstrEnabled(const cl::LibraryOptionsContext *LibraryOptions)
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Class to represent struct types.
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:467
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
Changed
Pass manager infrastructure for declaring and invalidating analyses.
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
void * PointerTy
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI bool canReturn(const Function &F)
Return true if there is at least a path through which F can return, false if there is no such path.
Definition CFG.cpp:405