Skip to content

Commit

Permalink
demangle function names in trace files (llvm#87626)
Browse files Browse the repository at this point in the history
This improves consistency in the trace files as other entries are
demangled too.
Submitted by jamieschmeiser on behalf of trass3r
@jamieschmeiser @An-DJ
  • Loading branch information
Trass3r authored Jul 10, 2024
1 parent 6002e2f commit 0fa20c5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
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 @@ -1415,7 +1416,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 @@ -249,7 +250,7 @@ std::string getIRName(Any IR) {
L->getHeader()->getParent()->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 @@ -1579,7 +1580,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

0 comments on commit 0fa20c5

Please sign in to comment.