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