@@ -53,10 +53,9 @@ class CtxInstrumentationLowerer final {
53
53
Module &M;
54
54
ModuleAnalysisManager &MAM;
55
55
Type *ContextNodeTy = nullptr ;
56
- Type *ContextRootTy = nullptr ;
57
56
Type *FunctionDataTy = nullptr ;
58
57
59
- DenseMap <const Function *, Constant *> ContextRootMap ;
58
+ DenseSet <const Function *> ContextRootSet ;
60
59
Function *StartCtx = nullptr ;
61
60
Function *GetCtx = nullptr ;
62
61
Function *ReleaseCtx = nullptr ;
@@ -114,15 +113,6 @@ CtxInstrumentationLowerer::CtxInstrumentationLowerer(Module &M,
114
113
auto *I32Ty = Type::getInt32Ty (M.getContext ());
115
114
auto *I64Ty = Type::getInt64Ty (M.getContext ());
116
115
117
- // The ContextRoot type
118
- ContextRootTy =
119
- StructType::get (M.getContext (), {
120
- PointerTy, /* FirstNode*/
121
- PointerTy, /* FirstMemBlock*/
122
- PointerTy, /* CurrentMem*/
123
- I64Ty, /* TotalEntries*/
124
- SanitizerMutexType, /* Taken*/
125
- });
126
116
FunctionDataTy =
127
117
StructType::get (M.getContext (), {
128
118
PointerTy, /* Next*/
@@ -144,10 +134,7 @@ CtxInstrumentationLowerer::CtxInstrumentationLowerer(Module &M,
144
134
if (const auto *F = M.getFunction (Fname)) {
145
135
if (F->isDeclaration ())
146
136
continue ;
147
- auto *G = M.getOrInsertGlobal (Fname + " _ctx_root" , ContextRootTy);
148
- cast<GlobalVariable>(G)->setInitializer (
149
- Constant::getNullValue (ContextRootTy));
150
- ContextRootMap.insert (std::make_pair (F, G));
137
+ ContextRootSet.insert (F);
151
138
for (const auto &BB : *F)
152
139
for (const auto &I : BB)
153
140
if (const auto *CB = dyn_cast<CallBase>(&I))
@@ -165,7 +152,7 @@ CtxInstrumentationLowerer::CtxInstrumentationLowerer(Module &M,
165
152
M.getOrInsertFunction (
166
153
CompilerRtAPINames::StartCtx,
167
154
FunctionType::get (PointerTy,
168
- {PointerTy, /* ContextRoot */
155
+ {PointerTy, /* FunctionData */
169
156
I64Ty, /* Guid*/ I32Ty,
170
157
/* NumCounters*/ I32Ty /* NumCallsites*/ },
171
158
false ))
@@ -184,7 +171,7 @@ CtxInstrumentationLowerer::CtxInstrumentationLowerer(Module &M,
184
171
M.getOrInsertFunction (CompilerRtAPINames::ReleaseCtx,
185
172
FunctionType::get (Type::getVoidTy (M.getContext ()),
186
173
{
187
- PointerTy, /* ContextRoot */
174
+ PointerTy, /* FunctionData */
188
175
},
189
176
false ))
190
177
.getCallee ());
@@ -224,7 +211,7 @@ bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
224
211
Value *RealContext = nullptr ;
225
212
226
213
StructType *ThisContextType = nullptr ;
227
- Value *TheRootContext = nullptr ;
214
+ Value *TheRootFuctionData = nullptr ;
228
215
Value *ExpectedCalleeTLSAddr = nullptr ;
229
216
Value *CallsiteInfoTLSAddr = nullptr ;
230
217
@@ -246,23 +233,23 @@ bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
246
233
ArrayType::get (Builder.getPtrTy (), NumCallsites)});
247
234
// Figure out which way we obtain the context object for this function -
248
235
// if it's an entrypoint, then we call StartCtx, otherwise GetCtx. In the
249
- // former case, we also set TheRootContext since we need to release it
236
+ // former case, we also set TheRootFuctionData since we need to release it
250
237
// at the end (plus it can be used to know if we have an entrypoint or a
251
238
// regular function)
252
- auto Iter = ContextRootMap.find (&F);
253
- if (Iter != ContextRootMap.end ()) {
254
- TheRootContext = Iter->second ;
239
+ // Make up a compact name, these names end up taking up a lot of space
240
+ // in the binary.
241
+ auto *FData = new GlobalVariable (M, FunctionDataTy, false ,
242
+ GlobalVariable::InternalLinkage,
243
+ Constant::getNullValue (FunctionDataTy));
244
+
245
+ if (ContextRootSet.contains (&F)) {
255
246
Context = Builder.CreateCall (
256
- StartCtx, {TheRootContext , Guid, Builder.getInt32 (NumCounters),
247
+ StartCtx, {FData , Guid, Builder.getInt32 (NumCounters),
257
248
Builder.getInt32 (NumCallsites)});
249
+ TheRootFuctionData = FData;
258
250
ORE.emit (
259
251
[&] { return OptimizationRemark (DEBUG_TYPE, " Entrypoint" , &F); });
260
252
} else {
261
- // Make up a compact name, these names end up taking up a lot of space
262
- // in the binary.
263
- auto *FData = new GlobalVariable (
264
- M, FunctionDataTy, false , GlobalVariable::InternalLinkage,
265
- Constant::getNullValue (FunctionDataTy));
266
253
Context = Builder.CreateCall (GetCtx, {FData, &F, Guid,
267
254
Builder.getInt32 (NumCounters),
268
255
Builder.getInt32 (NumCallsites)});
@@ -347,10 +334,10 @@ bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
347
334
break ;
348
335
}
349
336
I.eraseFromParent ();
350
- } else if (TheRootContext && isa<ReturnInst>(I)) {
337
+ } else if (TheRootFuctionData && isa<ReturnInst>(I)) {
351
338
// Remember to release the context if we are an entrypoint.
352
339
IRBuilder<> Builder (&I);
353
- Builder.CreateCall (ReleaseCtx, {TheRootContext });
340
+ Builder.CreateCall (ReleaseCtx, {TheRootFuctionData });
354
341
ContextWasReleased = true ;
355
342
}
356
343
}
@@ -359,7 +346,7 @@ bool CtxInstrumentationLowerer::lowerFunction(Function &F) {
359
346
// to disallow this, (so this then stays as an error), another is to detect
360
347
// that and then do a wrapper or disallow the tail call. This only affects
361
348
// instrumentation, when we want to detect the call graph.
362
- if (TheRootContext && !ContextWasReleased)
349
+ if (TheRootFuctionData && !ContextWasReleased)
363
350
F.getContext ().emitError (
364
351
" [ctx_prof] An entrypoint was instrumented but it has no `ret` "
365
352
" instructions above which to release the context: " +
0 commit comments