Skip to content

Commit 0fa20c5

Browse files
authored
demangle function names in trace files (#87626)
This improves consistency in the trace files as other entries are demangled too. Submitted by jamieschmeiser on behalf of trass3r @jamieschmeiser @An-DJ
1 parent 6002e2f commit 0fa20c5

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

clang/test/Driver/ftime-trace-sections.py

100644100755
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ def is_before(range1, range2):
1919

2020
log_contents = json.loads(sys.stdin.read())
2121
events = log_contents["traceEvents"]
22+
23+
instants = [event for event in events if event["name"] == "InstantiateFunction"]
2224
codegens = [event for event in events if event["name"] == "CodeGen Function"]
25+
opts = [event for event in events if event["name"] == "OptFunction"]
2326
frontends = [event for event in events if event["name"] == "Frontend"]
2427
backends = [event for event in events if event["name"] == "Backend"]
2528

@@ -48,3 +51,11 @@ def is_before(range1, range2):
4851
]
4952
):
5053
sys.exit("Not all Frontend section are before all Backend sections!")
54+
55+
# Check that entries for foo exist and are in a demangled form.
56+
if not any(e for e in instants if "foo<int>" in e["args"]["detail"]):
57+
sys.exit("Missing Instantiate entry for foo!")
58+
if not any(e for e in codegens if "foo<int>" in e["args"]["detail"]):
59+
sys.exit("Missing CodeGen entry for foo!")
60+
if not any(e for e in opts if "foo<int>" in e["args"]["detail"]):
61+
sys.exit("Missing Optimize entry for foo!")

llvm/lib/IR/LegacyPassManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "llvm/IR/LegacyPassManager.h"
1414
#include "llvm/ADT/MapVector.h"
15+
#include "llvm/Demangle/Demangle.h"
1516
#include "llvm/IR/DiagnosticInfo.h"
1617
#include "llvm/IR/IRPrintingPasses.h"
1718
#include "llvm/IR/LLVMContext.h"
@@ -1415,7 +1416,8 @@ bool FPPassManager::runOnFunction(Function &F) {
14151416

14161417
// Store name outside of loop to avoid redundant calls.
14171418
const StringRef Name = F.getName();
1418-
llvm::TimeTraceScope FunctionScope("OptFunction", Name);
1419+
llvm::TimeTraceScope FunctionScope(
1420+
"OptFunction", [&F]() { return demangle(F.getName().str()); });
14191421

14201422
for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
14211423
FunctionPass *FP = getContainedPass(Index);

llvm/lib/Passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_llvm_component_library(LLVMPasses
2121
CodeGen
2222
Core
2323
Coroutines
24+
Demangle
2425
HipStdPar
2526
IPO
2627
InstCombine

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/CodeGen/MIRPrinter.h"
2323
#include "llvm/CodeGen/MachineFunction.h"
2424
#include "llvm/CodeGen/MachineModuleInfo.h"
25+
#include "llvm/Demangle/Demangle.h"
2526
#include "llvm/IR/Constants.h"
2627
#include "llvm/IR/Function.h"
2728
#include "llvm/IR/Module.h"
@@ -234,12 +235,12 @@ void printIR(raw_ostream &OS, const MachineFunction *MF) {
234235
MF->print(OS);
235236
}
236237

237-
std::string getIRName(Any IR) {
238+
std::string getIRName(Any IR, bool demangled = false) {
238239
if (unwrapIR<Module>(IR))
239240
return "[module]";
240241

241242
if (const auto *F = unwrapIR<Function>(IR))
242-
return F->getName().str();
243+
return demangled ? demangle(F->getName()) : F->getName().str();
243244

244245
if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR))
245246
return C->getName();
@@ -249,7 +250,7 @@ std::string getIRName(Any IR) {
249250
L->getHeader()->getParent()->getName().str();
250251

251252
if (const auto *MF = unwrapIR<MachineFunction>(IR))
252-
return MF->getName().str();
253+
return demangled ? demangle(MF->getName()) : MF->getName().str();
253254

254255
llvm_unreachable("Unknown wrapped IR type");
255256
}
@@ -1579,7 +1580,7 @@ void TimeProfilingPassesHandler::registerCallbacks(
15791580
}
15801581

15811582
void TimeProfilingPassesHandler::runBeforePass(StringRef PassID, Any IR) {
1582-
timeTraceProfilerBegin(PassID, getIRName(IR));
1583+
timeTraceProfilerBegin(PassID, getIRName(IR, true));
15831584
}
15841585

15851586
void TimeProfilingPassesHandler::runAfterPass() { timeTraceProfilerEnd(); }

0 commit comments

Comments
 (0)