Skip to content

Commit 32c267d

Browse files
committed
[AsmPrinter] Remove timers
Timers are an out-of-line function call and a global variable access, here twice per emitted instruction. At this granularity, not only the time results become skewed, but the timers also add a performance overhead when profiling is disabled. Therefore, remove the timers.
1 parent 0fb26fa commit 32c267d

File tree

4 files changed

+56
-142
lines changed

4 files changed

+56
-142
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,6 @@ class AsmPrinter : public MachineFunctionPass {
144144
using GOTEquivUsePair = std::pair<const GlobalVariable *, unsigned>;
145145
MapVector<const MCSymbol *, GOTEquivUsePair> GlobalGOTEquivs;
146146

147-
/// struct HandlerInfo and Handlers permit users or target extended
148-
/// AsmPrinter to add their own handlers.
149-
template <class H> struct HandlerInfo {
150-
std::unique_ptr<H> Handler;
151-
StringRef TimerName;
152-
StringRef TimerDescription;
153-
StringRef TimerGroupName;
154-
StringRef TimerGroupDescription;
155-
156-
HandlerInfo(std::unique_ptr<H> Handler, StringRef TimerName,
157-
StringRef TimerDescription, StringRef TimerGroupName,
158-
StringRef TimerGroupDescription)
159-
: Handler(std::move(Handler)), TimerName(TimerName),
160-
TimerDescription(TimerDescription), TimerGroupName(TimerGroupName),
161-
TimerGroupDescription(TimerGroupDescription) {}
162-
};
163-
164147
// Flags representing which CFI section is required for a function/module.
165148
enum class CFISection : unsigned {
166149
None = 0, ///< Do not emit either .eh_frame or .debug_frame
@@ -206,11 +189,11 @@ class AsmPrinter : public MachineFunctionPass {
206189

207190
/// A vector of all debug/EH info emitters we should use. This vector
208191
/// maintains ownership of the emitters.
209-
SmallVector<HandlerInfo<AsmPrinterHandler>, 2> Handlers;
192+
SmallVector<std::unique_ptr<AsmPrinterHandler>, 2> Handlers;
210193
size_t NumUserHandlers = 0;
211194

212195
/// Debuginfo handler. Protected so that targets can add their own.
213-
SmallVector<HandlerInfo<DebugHandlerBase>, 1> DebugHandlers;
196+
SmallVector<std::unique_ptr<DebugHandlerBase>, 1> DebugHandlers;
214197
size_t NumUserDebugHandlers = 0;
215198

216199
StackMaps SM;
@@ -536,12 +519,12 @@ class AsmPrinter : public MachineFunctionPass {
536519
// Overridable Hooks
537520
//===------------------------------------------------------------------===//
538521

539-
void addAsmPrinterHandler(HandlerInfo<AsmPrinterHandler> Handler) {
522+
void addAsmPrinterHandler(std::unique_ptr<AsmPrinterHandler> Handler) {
540523
Handlers.insert(Handlers.begin(), std::move(Handler));
541524
NumUserHandlers++;
542525
}
543526

544-
void addDebugHandler(HandlerInfo<DebugHandlerBase> Handler) {
527+
void addDebugHandler(std::unique_ptr<DebugHandlerBase> Handler) {
545528
DebugHandlers.insert(DebugHandlers.begin(), std::move(Handler));
546529
NumUserDebugHandlers++;
547530
}

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 47 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
#include "llvm/Support/Format.h"
114114
#include "llvm/Support/MathExtras.h"
115115
#include "llvm/Support/Path.h"
116-
#include "llvm/Support/Timer.h"
117116
#include "llvm/Support/VCSRevision.h"
118117
#include "llvm/Support/raw_ostream.h"
119118
#include "llvm/Target/TargetLoweringObjectFile.h"
@@ -156,17 +155,6 @@ static cl::bits<PGOMapFeaturesEnum> PgoAnalysisMapFeatures(
156155
"Enable extended information within the SHT_LLVM_BB_ADDR_MAP that is "
157156
"extracted from PGO related analysis."));
158157

159-
const char DWARFGroupName[] = "dwarf";
160-
const char DWARFGroupDescription[] = "DWARF Emission";
161-
const char DbgTimerName[] = "emit";
162-
const char DbgTimerDescription[] = "Debug Info Emission";
163-
const char EHTimerName[] = "write_exception";
164-
const char EHTimerDescription[] = "DWARF Exception Writer";
165-
const char CFGuardName[] = "Control Flow Guard";
166-
const char CFGuardDescription[] = "Control Flow Guard";
167-
const char CodeViewLineTablesGroupName[] = "linetables";
168-
const char CodeViewLineTablesGroupDescription[] = "CodeView Line Tables";
169-
170158
STATISTIC(EmittedInsts, "Number of machine instrs printed");
171159

172160
char AsmPrinter::ID = 0;
@@ -550,19 +538,13 @@ bool AsmPrinter::doInitialization(Module &M) {
550538

551539
if (MAI->doesSupportDebugInformation()) {
552540
bool EmitCodeView = M.getCodeViewFlag();
553-
if (EmitCodeView && TM.getTargetTriple().isOSWindows()) {
554-
DebugHandlers.emplace_back(std::make_unique<CodeViewDebug>(this),
555-
DbgTimerName, DbgTimerDescription,
556-
CodeViewLineTablesGroupName,
557-
CodeViewLineTablesGroupDescription);
558-
}
541+
if (EmitCodeView && TM.getTargetTriple().isOSWindows())
542+
DebugHandlers.push_back(std::make_unique<CodeViewDebug>(this));
559543
if (!EmitCodeView || M.getDwarfVersion()) {
560544
assert(MMI && "MMI could not be nullptr here!");
561545
if (MMI->hasDebugInfo()) {
562546
DD = new DwarfDebug(this);
563-
DebugHandlers.emplace_back(std::unique_ptr<DwarfDebug>(DD),
564-
DbgTimerName, DbgTimerDescription,
565-
DWARFGroupName, DWARFGroupDescription);
547+
DebugHandlers.push_back(std::unique_ptr<DwarfDebug>(DD));
566548
}
567549
}
568550
}
@@ -625,26 +607,16 @@ bool AsmPrinter::doInitialization(Module &M) {
625607
break;
626608
}
627609
if (ES)
628-
Handlers.emplace_back(std::unique_ptr<EHStreamer>(ES), EHTimerName,
629-
EHTimerDescription, DWARFGroupName,
630-
DWARFGroupDescription);
610+
Handlers.push_back(std::unique_ptr<EHStreamer>(ES));
631611

632612
// Emit tables for any value of cfguard flag (i.e. cfguard=1 or cfguard=2).
633613
if (mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("cfguard")))
634-
Handlers.emplace_back(std::make_unique<WinCFGuard>(this), CFGuardName,
635-
CFGuardDescription, DWARFGroupName,
636-
DWARFGroupDescription);
614+
Handlers.push_back(std::make_unique<WinCFGuard>(this));
637615

638-
for (const auto &HI : DebugHandlers) {
639-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
640-
HI.TimerGroupDescription, TimePassesIsEnabled);
641-
HI.Handler->beginModule(&M);
642-
}
643-
for (const auto &HI : Handlers) {
644-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
645-
HI.TimerGroupDescription, TimePassesIsEnabled);
646-
HI.Handler->beginModule(&M);
647-
}
616+
for (auto &Handler : DebugHandlers)
617+
Handler->beginModule(&M);
618+
for (auto &Handler : Handlers)
619+
Handler->beginModule(&M);
648620

649621
return false;
650622
}
@@ -791,11 +763,8 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
791763
// sections and expected to be contiguous (e.g. ObjC metadata).
792764
const Align Alignment = getGVAlignment(GV, DL);
793765

794-
for (auto &HI : DebugHandlers) {
795-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
796-
HI.TimerGroupDescription, TimePassesIsEnabled);
797-
HI.Handler->setSymbolSize(GVSym, Size);
798-
}
766+
for (auto &Handler : DebugHandlers)
767+
Handler->setSymbolSize(GVSym, Size);
799768

800769
// Handle common symbols
801770
if (GVKind.isCommon()) {
@@ -1066,22 +1035,14 @@ void AsmPrinter::emitFunctionHeader() {
10661035
}
10671036

10681037
// Emit pre-function debug and/or EH information.
1069-
for (const auto &HI : DebugHandlers) {
1070-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1071-
HI.TimerGroupDescription, TimePassesIsEnabled);
1072-
HI.Handler->beginFunction(MF);
1073-
HI.Handler->beginBasicBlockSection(MF->front());
1074-
}
1075-
for (const auto &HI : Handlers) {
1076-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1077-
HI.TimerGroupDescription, TimePassesIsEnabled);
1078-
HI.Handler->beginFunction(MF);
1079-
}
1080-
for (const auto &HI : Handlers) {
1081-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1082-
HI.TimerGroupDescription, TimePassesIsEnabled);
1083-
HI.Handler->beginBasicBlockSection(MF->front());
1038+
for (auto &Handler : DebugHandlers) {
1039+
Handler->beginFunction(MF);
1040+
Handler->beginBasicBlockSection(MF->front());
10841041
}
1042+
for (auto &Handler : Handlers)
1043+
Handler->beginFunction(MF);
1044+
for (auto &Handler : Handlers)
1045+
Handler->beginBasicBlockSection(MF->front());
10851046

10861047
// Emit the prologue data.
10871048
if (F.hasPrologueData())
@@ -1776,8 +1737,8 @@ void AsmPrinter::emitFunctionBody() {
17761737
if (MDNode *MD = MI.getPCSections())
17771738
emitPCSectionsLabel(*MF, *MD);
17781739

1779-
for (const auto &HI : DebugHandlers)
1780-
HI.Handler->beginInstruction(&MI);
1740+
for (auto &Handler : DebugHandlers)
1741+
Handler->beginInstruction(&MI);
17811742

17821743
if (isVerbose())
17831744
emitComments(MI, OutStreamer->getCommentOS());
@@ -1871,8 +1832,8 @@ void AsmPrinter::emitFunctionBody() {
18711832
if (MCSymbol *S = MI.getPostInstrSymbol())
18721833
OutStreamer->emitLabel(S);
18731834

1874-
for (const auto &HI : DebugHandlers)
1875-
HI.Handler->endInstruction();
1835+
for (auto &Handler : DebugHandlers)
1836+
Handler->endInstruction();
18761837
}
18771838

18781839
// We must emit temporary symbol for the end of this basic block, if either
@@ -2003,22 +1964,13 @@ void AsmPrinter::emitFunctionBody() {
20031964
// Call endBasicBlockSection on the last block now, if it wasn't already
20041965
// called.
20051966
if (!MF->back().isEndSection()) {
2006-
for (const auto &HI : DebugHandlers) {
2007-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2008-
HI.TimerGroupDescription, TimePassesIsEnabled);
2009-
HI.Handler->endBasicBlockSection(MF->back());
2010-
}
2011-
for (const auto &HI : Handlers) {
2012-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2013-
HI.TimerGroupDescription, TimePassesIsEnabled);
2014-
HI.Handler->endBasicBlockSection(MF->back());
2015-
}
2016-
}
2017-
for (const auto &HI : Handlers) {
2018-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2019-
HI.TimerGroupDescription, TimePassesIsEnabled);
2020-
HI.Handler->markFunctionEnd();
1967+
for (auto &Handler : DebugHandlers)
1968+
Handler->endBasicBlockSection(MF->back());
1969+
for (auto &Handler : Handlers)
1970+
Handler->endBasicBlockSection(MF->back());
20211971
}
1972+
for (auto &Handler : Handlers)
1973+
Handler->markFunctionEnd();
20221974

20231975
MBBSectionRanges[MF->front().getSectionIDNum()] =
20241976
MBBSectionRange{CurrentFnBegin, CurrentFnEnd};
@@ -2027,16 +1979,10 @@ void AsmPrinter::emitFunctionBody() {
20271979
emitJumpTableInfo();
20281980

20291981
// Emit post-function debug and/or EH information.
2030-
for (const auto &HI : DebugHandlers) {
2031-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2032-
HI.TimerGroupDescription, TimePassesIsEnabled);
2033-
HI.Handler->endFunction(MF);
2034-
}
2035-
for (const auto &HI : Handlers) {
2036-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2037-
HI.TimerGroupDescription, TimePassesIsEnabled);
2038-
HI.Handler->endFunction(MF);
2039-
}
1982+
for (auto &Handler : DebugHandlers)
1983+
Handler->endFunction(MF);
1984+
for (auto &Handler : Handlers)
1985+
Handler->endFunction(MF);
20401986

20411987
// Emit section containing BB address offsets and their metadata, when
20421988
// BB labels are requested for this function. Skip empty functions.
@@ -2473,16 +2419,10 @@ bool AsmPrinter::doFinalization(Module &M) {
24732419
emitGlobalIFunc(M, IFunc);
24742420

24752421
// Finalize debug and EH information.
2476-
for (const auto &HI : DebugHandlers) {
2477-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2478-
HI.TimerGroupDescription, TimePassesIsEnabled);
2479-
HI.Handler->endModule();
2480-
}
2481-
for (const auto &HI : Handlers) {
2482-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2483-
HI.TimerGroupDescription, TimePassesIsEnabled);
2484-
HI.Handler->endModule();
2485-
}
2422+
for (auto &Handler : DebugHandlers)
2423+
Handler->endModule();
2424+
for (auto &Handler : Handlers)
2425+
Handler->endModule();
24862426

24872427
// This deletes all the ephemeral handlers that AsmPrinter added, while
24882428
// keeping all the user-added handlers alive until the AsmPrinter is
@@ -4004,9 +3944,9 @@ static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
40043944
void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
40053945
// End the previous funclet and start a new one.
40063946
if (MBB.isEHFuncletEntry()) {
4007-
for (const auto &HI : Handlers) {
4008-
HI.Handler->endFunclet();
4009-
HI.Handler->beginFunclet(MBB);
3947+
for (auto &Handler : Handlers) {
3948+
Handler->endFunclet();
3949+
Handler->beginFunclet(MBB);
40103950
}
40113951
}
40123952

@@ -4077,21 +4017,21 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
40774017
// if it begins a section (Entry block call is handled separately, next to
40784018
// beginFunction).
40794019
if (MBB.isBeginSection() && !MBB.isEntryBlock()) {
4080-
for (const auto &HI : DebugHandlers)
4081-
HI.Handler->beginBasicBlockSection(MBB);
4082-
for (const auto &HI : Handlers)
4083-
HI.Handler->beginBasicBlockSection(MBB);
4020+
for (auto &Handler : DebugHandlers)
4021+
Handler->beginBasicBlockSection(MBB);
4022+
for (auto &Handler : Handlers)
4023+
Handler->beginBasicBlockSection(MBB);
40844024
}
40854025
}
40864026

40874027
void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {
40884028
// Check if CFI information needs to be updated for this MBB with basic block
40894029
// sections.
40904030
if (MBB.isEndSection()) {
4091-
for (const auto &HI : DebugHandlers)
4092-
HI.Handler->endBasicBlockSection(MBB);
4093-
for (const auto &HI : Handlers)
4094-
HI.Handler->endBasicBlockSection(MBB);
4031+
for (auto &Handler : DebugHandlers)
4032+
Handler->endBasicBlockSection(MBB);
4033+
for (auto &Handler : Handlers)
4034+
Handler->endBasicBlockSection(MBB);
40954035
}
40964036
}
40974037

llvm/lib/Target/BPF/BPFAsmPrinter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ bool BPFAsmPrinter::doInitialization(Module &M) {
6262
// Only emit BTF when debuginfo available.
6363
if (MAI->doesSupportDebugInformation() && !M.debug_compile_units().empty()) {
6464
BTF = new BTFDebug(this);
65-
DebugHandlers.emplace_back(std::unique_ptr<BTFDebug>(BTF), "emit",
66-
"Debug Info Emission", "BTF", "BTF Emission");
65+
DebugHandlers.push_back(std::unique_ptr<BTFDebug>(BTF));
6766
}
6867

6968
return false;

llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,7 @@ class AsmPrinterHandlerTest : public AsmPrinterFixtureBase {
397397
return false;
398398

399399
auto *AP = TestPrinter->getAP();
400-
AP->addAsmPrinterHandler(AsmPrinter::HandlerInfo<AsmPrinterHandler>(
401-
std::unique_ptr<AsmPrinterHandler>(new TestHandler(*this)),
402-
"TestTimerName", "TestTimerDesc", "TestGroupName", "TestGroupDesc"));
400+
AP->addAsmPrinterHandler(std::make_unique<TestHandler>(*this));
403401
LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(&AP->TM);
404402
legacy::PassManager PM;
405403
PM.add(new MachineModuleInfoWrapperPass(LLVMTM));
@@ -409,9 +407,7 @@ class AsmPrinterHandlerTest : public AsmPrinterFixtureBase {
409407
M->setDataLayout(LLVMTM->createDataLayout());
410408
PM.run(*M);
411409
// Now check that we can run it twice.
412-
AP->addAsmPrinterHandler(AsmPrinter::HandlerInfo<AsmPrinterHandler>(
413-
std::unique_ptr<AsmPrinterHandler>(new TestHandler(*this)),
414-
"TestTimerName", "TestTimerDesc", "TestGroupName", "TestGroupDesc"));
410+
AP->addAsmPrinterHandler(std::make_unique<TestHandler>(*this));
415411
PM.run(*M);
416412
return true;
417413
}
@@ -451,9 +447,7 @@ class AsmPrinterDebugHandlerTest : public AsmPrinterFixtureBase {
451447
return false;
452448

453449
auto *AP = TestPrinter->getAP();
454-
AP->addDebugHandler(AsmPrinter::HandlerInfo<DebugHandlerBase>(
455-
std::make_unique<TestDebugHandler>(*this, AP), "TestTimerName",
456-
"TestTimerDesc", "TestGroupName", "TestGroupDesc"));
450+
AP->addDebugHandler(std::make_unique<TestDebugHandler>(*this, AP));
457451
LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(&AP->TM);
458452
legacy::PassManager PM;
459453
PM.add(new MachineModuleInfoWrapperPass(LLVMTM));
@@ -463,9 +457,7 @@ class AsmPrinterDebugHandlerTest : public AsmPrinterFixtureBase {
463457
M->setDataLayout(LLVMTM->createDataLayout());
464458
PM.run(*M);
465459
// Now check that we can run it twice.
466-
AP->addDebugHandler(AsmPrinter::HandlerInfo<DebugHandlerBase>(
467-
std::make_unique<TestDebugHandler>(*this, AP), "TestTimerName",
468-
"TestTimerDesc", "TestGroupName", "TestGroupDesc"));
460+
AP->addDebugHandler(std::make_unique<TestDebugHandler>(*this, AP));
469461
PM.run(*M);
470462
return true;
471463
}

0 commit comments

Comments
 (0)