Skip to content

demangle function names in trace files #87626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clang/test/Driver/ftime-trace-sections.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def is_before(range1, range2):

log_contents = json.loads(sys.stdin.read())
events = log_contents["traceEvents"]

instants = [event for event in events if event["name"] == "InstantiateFunction"]
codegens = [event for event in events if event["name"] == "CodeGen Function"]
opts = [event for event in events if event["name"] == "OptFunction"]
frontends = [event for event in events if event["name"] == "Frontend"]
backends = [event for event in events if event["name"] == "Backend"]

Expand Down Expand Up @@ -48,3 +51,11 @@ def is_before(range1, range2):
]
):
sys.exit("Not all Frontend section are before all Backend sections!")

# Check that entries for foo exist and are in a demangled form.
if not any(e for e in instants if "foo<int>" in e["args"]["detail"]):
sys.exit("Missing Instantiate entry for foo!")
if not any(e for e in codegens if "foo<int>" in e["args"]["detail"]):
sys.exit("Missing CodeGen entry for foo!")
if not any(e for e in opts if "foo<int>" in e["args"]["detail"]):
sys.exit("Missing Optimize entry for foo!")
4 changes: 3 additions & 1 deletion llvm/lib/IR/LegacyPassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "llvm/IR/LegacyPassManager.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/Demangle/Demangle.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/LLVMContext.h"
Expand Down Expand Up @@ -1416,7 +1417,8 @@ bool FPPassManager::runOnFunction(Function &F) {

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

for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
FunctionPass *FP = getContainedPass(Index);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_llvm_component_library(LLVMPasses
CodeGen
Core
Coroutines
Demangle
HipStdPar
IPO
InstCombine
Expand Down
9 changes: 5 additions & 4 deletions llvm/lib/Passes/StandardInstrumentations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "llvm/CodeGen/MIRPrinter.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/Demangle/Demangle.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
Expand Down Expand Up @@ -234,12 +235,12 @@ void printIR(raw_ostream &OS, const MachineFunction *MF) {
MF->print(OS);
}

std::string getIRName(Any IR) {
std::string getIRName(Any IR, bool demangled = false) {
if (unwrapIR<Module>(IR))
return "[module]";

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

if (const auto *C = unwrapIR<LazyCallGraph::SCC>(IR))
return C->getName();
Expand All @@ -248,7 +249,7 @@ std::string getIRName(Any IR) {
return L->getName().str();

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

llvm_unreachable("Unknown wrapped IR type");
}
Expand Down Expand Up @@ -1576,7 +1577,7 @@ void TimeProfilingPassesHandler::registerCallbacks(
}

void TimeProfilingPassesHandler::runBeforePass(StringRef PassID, Any IR) {
timeTraceProfilerBegin(PassID, getIRName(IR));
timeTraceProfilerBegin(PassID, getIRName(IR, true));
}

void TimeProfilingPassesHandler::runAfterPass() { timeTraceProfilerEnd(); }
Expand Down