Skip to content

Commit b01e5b2

Browse files
authored
[ctxprof][nfc] Refactor __llvm_ctx_profile_start_context (#133744)
Most of the functionality will be reused with the auto-root detection mechanism (which is introduced subsequently in PR #133147).
1 parent 89c25c5 commit b01e5b2

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,25 @@ ContextRoot *FunctionData::getOrAllocateContextRoot() {
277277
return Root;
278278
}
279279

280+
ContextNode *tryStartContextGivenRoot(ContextRoot *Root, GUID Guid,
281+
uint32_t Counters, uint32_t Callsites)
282+
SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
283+
IsUnderContext = true;
284+
__sanitizer::atomic_fetch_add(&Root->TotalEntries, 1,
285+
__sanitizer::memory_order_relaxed);
286+
if (!Root->FirstMemBlock) {
287+
setupContext(Root, Guid, Counters, Callsites);
288+
}
289+
if (Root->Taken.TryLock()) {
290+
__llvm_ctx_profile_current_context_root = Root;
291+
onContextEnter(*Root->FirstNode);
292+
return Root->FirstNode;
293+
}
294+
// If this thread couldn't take the lock, return scratch context.
295+
__llvm_ctx_profile_current_context_root = nullptr;
296+
return TheScratchContext;
297+
}
298+
280299
ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
281300
uint32_t NumCounters) {
282301

@@ -369,24 +388,8 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData *Data, void *Callee,
369388
ContextNode *__llvm_ctx_profile_start_context(
370389
FunctionData *FData, GUID Guid, uint32_t Counters,
371390
uint32_t Callsites) SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
372-
IsUnderContext = true;
373-
374-
auto *Root = FData->getOrAllocateContextRoot();
375-
376-
__sanitizer::atomic_fetch_add(&Root->TotalEntries, 1,
377-
__sanitizer::memory_order_relaxed);
378-
379-
if (!Root->FirstMemBlock) {
380-
setupContext(Root, Guid, Counters, Callsites);
381-
}
382-
if (Root->Taken.TryLock()) {
383-
__llvm_ctx_profile_current_context_root = Root;
384-
onContextEnter(*Root->FirstNode);
385-
return Root->FirstNode;
386-
}
387-
// If this thread couldn't take the lock, return scratch context.
388-
__llvm_ctx_profile_current_context_root = nullptr;
389-
return TheScratchContext;
391+
return tryStartContextGivenRoot(FData->getOrAllocateContextRoot(), Guid,
392+
Counters, Callsites);
390393
}
391394

392395
void __llvm_ctx_profile_release_context(FunctionData *FData)

0 commit comments

Comments
 (0)