[clang] [llvm] demangle function names in trace files (PR #87626)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 19 01:26:03 PDT 2024
https://github.com/Trass3r updated https://github.com/llvm/llvm-project/pull/87626
>From 153bb2fd38b8bb18281bd52c2a21b6e4a75f3fc8 Mon Sep 17 00:00:00 2001
From: Andreas Hollandt <trass3r at gmail.com>
Date: Tue, 4 Oct 2022 12:05:39 +0200
Subject: [PATCH 1/2] demangle OptFunction trace names
This improves consistency in the trace files as other entries are demangled too.
Fixes #45901.
---
clang/test/Driver/ftime-trace-sections.py | 11 +++++++++++
llvm/lib/IR/LegacyPassManager.cpp | 4 +++-
llvm/lib/Passes/CMakeLists.txt | 1 +
llvm/lib/Passes/StandardInstrumentations.cpp | 3 ++-
4 files changed, 17 insertions(+), 2 deletions(-)
mode change 100644 => 100755 clang/test/Driver/ftime-trace-sections.py
diff --git a/clang/test/Driver/ftime-trace-sections.py b/clang/test/Driver/ftime-trace-sections.py
old mode 100644
new mode 100755
index 02afa4ac54eb7b..b332931d29a622
--- a/clang/test/Driver/ftime-trace-sections.py
+++ b/clang/test/Driver/ftime-trace-sections.py
@@ -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"]
@@ -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!")
diff --git a/llvm/lib/IR/LegacyPassManager.cpp b/llvm/lib/IR/LegacyPassManager.cpp
index d361bd9a983912..2d0d696acddfcd 100644
--- a/llvm/lib/IR/LegacyPassManager.cpp
+++ b/llvm/lib/IR/LegacyPassManager.cpp
@@ -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"
@@ -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);
diff --git a/llvm/lib/Passes/CMakeLists.txt b/llvm/lib/Passes/CMakeLists.txt
index 6425f4934b2103..b5224327d79216 100644
--- a/llvm/lib/Passes/CMakeLists.txt
+++ b/llvm/lib/Passes/CMakeLists.txt
@@ -21,6 +21,7 @@ add_llvm_component_library(LLVMPasses
CodeGen
Core
Coroutines
+ Demangle
HipStdPar
IPO
InstCombine
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index c18b462258623d..a54a15603e699a 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -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"
@@ -1576,7 +1577,7 @@ void TimeProfilingPassesHandler::registerCallbacks(
}
void TimeProfilingPassesHandler::runBeforePass(StringRef PassID, Any IR) {
- timeTraceProfilerBegin(PassID, getIRName(IR));
+ timeTraceProfilerBegin(PassID, demangle(getIRName(IR)));
}
void TimeProfilingPassesHandler::runAfterPass() { timeTraceProfilerEnd(); }
>From 5ae3effb583da45200dff2e17c0528baa5e4bcea Mon Sep 17 00:00:00 2001
From: Andreas Hollandt <trass3r at gmail.com>
Date: Mon, 8 Apr 2024 21:32:24 +0200
Subject: [PATCH 2/2] add demangled parameter to getIRName
---
llvm/lib/Passes/StandardInstrumentations.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index a54a15603e699a..13c4f38dcf448d 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -235,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();
@@ -249,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");
}
@@ -1577,7 +1577,7 @@ void TimeProfilingPassesHandler::registerCallbacks(
}
void TimeProfilingPassesHandler::runBeforePass(StringRef PassID, Any IR) {
- timeTraceProfilerBegin(PassID, demangle(getIRName(IR)));
+ timeTraceProfilerBegin(PassID, getIRName(IR, true));
}
void TimeProfilingPassesHandler::runAfterPass() { timeTraceProfilerEnd(); }
More information about the cfe-commits
mailing list