Skip to content

Commit 039a158

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 01abb8b commit 039a158

File tree

4 files changed

+55
-137
lines changed

4 files changed

+55
-137
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: 48 additions & 108 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;
@@ -548,19 +536,13 @@ bool AsmPrinter::doInitialization(Module &M) {
548536

549537
if (MAI->doesSupportDebugInformation()) {
550538
bool EmitCodeView = M.getCodeViewFlag();
551-
if (EmitCodeView && TM.getTargetTriple().isOSWindows()) {
552-
DebugHandlers.emplace_back(std::make_unique<CodeViewDebug>(this),
553-
DbgTimerName, DbgTimerDescription,
554-
CodeViewLineTablesGroupName,
555-
CodeViewLineTablesGroupDescription);
556-
}
539+
if (EmitCodeView && TM.getTargetTriple().isOSWindows())
540+
DebugHandlers.push_back(std::make_unique<CodeViewDebug>(this));
557541
if (!EmitCodeView || M.getDwarfVersion()) {
558542
assert(MMI && "MMI could not be nullptr here!");
559543
if (MMI->hasDebugInfo()) {
560544
DD = new DwarfDebug(this);
561-
DebugHandlers.emplace_back(std::unique_ptr<DwarfDebug>(DD),
562-
DbgTimerName, DbgTimerDescription,
563-
DWARFGroupName, DWARFGroupDescription);
545+
DebugHandlers.push_back(std::unique_ptr<DwarfDebug>(DD));
564546
}
565547
}
566548
}
@@ -623,26 +605,16 @@ bool AsmPrinter::doInitialization(Module &M) {
623605
break;
624606
}
625607
if (ES)
626-
Handlers.emplace_back(std::unique_ptr<EHStreamer>(ES), EHTimerName,
627-
EHTimerDescription, DWARFGroupName,
628-
DWARFGroupDescription);
608+
Handlers.push_back(std::unique_ptr<EHStreamer>(ES));
629609

630610
// Emit tables for any value of cfguard flag (i.e. cfguard=1 or cfguard=2).
631611
if (mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("cfguard")))
632-
Handlers.emplace_back(std::make_unique<WinCFGuard>(this), CFGuardName,
633-
CFGuardDescription, DWARFGroupName,
634-
DWARFGroupDescription);
635-
636-
for (const auto &HI : DebugHandlers) {
637-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
638-
HI.TimerGroupDescription, TimePassesIsEnabled);
639-
HI.Handler->beginModule(&M);
640-
}
641-
for (const auto &HI : Handlers) {
642-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
643-
HI.TimerGroupDescription, TimePassesIsEnabled);
644-
HI.Handler->beginModule(&M);
645-
}
612+
Handlers.push_back(std::make_unique<WinCFGuard>(this));
613+
614+
for (auto &Handler : DebugHandlers)
615+
Handler->beginModule(&M);
616+
for (auto &Handler : Handlers)
617+
Handler->beginModule(&M);
646618

647619
return false;
648620
}
@@ -789,11 +761,8 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
789761
// sections and expected to be contiguous (e.g. ObjC metadata).
790762
const Align Alignment = getGVAlignment(GV, DL);
791763

792-
for (auto &HI : DebugHandlers) {
793-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
794-
HI.TimerGroupDescription, TimePassesIsEnabled);
795-
HI.Handler->setSymbolSize(GVSym, Size);
796-
}
764+
for (auto &Handler : DebugHandlers)
765+
Handler->setSymbolSize(GVSym, Size);
797766

798767
// Handle common symbols
799768
if (GVKind.isCommon()) {
@@ -1064,22 +1033,14 @@ void AsmPrinter::emitFunctionHeader() {
10641033
}
10651034

10661035
// Emit pre-function debug and/or EH information.
1067-
for (const auto &HI : DebugHandlers) {
1068-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1069-
HI.TimerGroupDescription, TimePassesIsEnabled);
1070-
HI.Handler->beginFunction(MF);
1071-
HI.Handler->beginBasicBlockSection(MF->front());
1072-
}
1073-
for (const auto &HI : Handlers) {
1074-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1075-
HI.TimerGroupDescription, TimePassesIsEnabled);
1076-
HI.Handler->beginFunction(MF);
1077-
}
1078-
for (const auto &HI : Handlers) {
1079-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1080-
HI.TimerGroupDescription, TimePassesIsEnabled);
1081-
HI.Handler->beginBasicBlockSection(MF->front());
1036+
for (auto &Handler : DebugHandlers) {
1037+
Handler->beginFunction(MF);
1038+
Handler->beginBasicBlockSection(MF->front());
10821039
}
1040+
for (auto &Handler : Handlers)
1041+
Handler->beginFunction(MF);
1042+
for (auto &Handler : Handlers)
1043+
Handler->beginBasicBlockSection(MF->front());
10831044

10841045
// Emit the prologue data.
10851046
if (F.hasPrologueData())
@@ -1773,8 +1734,8 @@ void AsmPrinter::emitFunctionBody() {
17731734
if (MDNode *MD = MI.getPCSections())
17741735
emitPCSectionsLabel(*MF, *MD);
17751736

1776-
for (const auto &HI : DebugHandlers)
1777-
HI.Handler->beginInstruction(&MI);
1737+
for (auto &Handler : DebugHandlers)
1738+
Handler->beginInstruction(&MI);
17781739

17791740
if (isVerbose())
17801741
emitComments(MI, OutStreamer->getCommentOS());
@@ -1868,8 +1829,8 @@ void AsmPrinter::emitFunctionBody() {
18681829
if (MCSymbol *S = MI.getPostInstrSymbol())
18691830
OutStreamer->emitLabel(S);
18701831

1871-
for (const auto &HI : DebugHandlers)
1872-
HI.Handler->endInstruction();
1832+
for (auto &Handler : DebugHandlers)
1833+
Handler->endInstruction();
18731834
}
18741835

18751836
// We must emit temporary symbol for the end of this basic block, if either
@@ -2000,22 +1961,13 @@ void AsmPrinter::emitFunctionBody() {
20001961
// Call endBasicBlockSection on the last block now, if it wasn't already
20011962
// called.
20021963
if (!MF->back().isEndSection()) {
2003-
for (const auto &HI : DebugHandlers) {
2004-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2005-
HI.TimerGroupDescription, TimePassesIsEnabled);
2006-
HI.Handler->endBasicBlockSection(MF->back());
2007-
}
2008-
for (const auto &HI : Handlers) {
2009-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2010-
HI.TimerGroupDescription, TimePassesIsEnabled);
2011-
HI.Handler->endBasicBlockSection(MF->back());
2012-
}
2013-
}
2014-
for (const auto &HI : Handlers) {
2015-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2016-
HI.TimerGroupDescription, TimePassesIsEnabled);
2017-
HI.Handler->markFunctionEnd();
1964+
for (auto &Handler : DebugHandlers)
1965+
Handler->endBasicBlockSection(MF->back());
1966+
for (auto &Handler : Handlers)
1967+
Handler->endBasicBlockSection(MF->back());
20181968
}
1969+
for (auto &Handler : Handlers)
1970+
Handler->markFunctionEnd();
20191971

20201972
MBBSectionRanges[MF->front().getSectionIDNum()] =
20211973
MBBSectionRange{CurrentFnBegin, CurrentFnEnd};
@@ -2024,16 +1976,10 @@ void AsmPrinter::emitFunctionBody() {
20241976
emitJumpTableInfo();
20251977

20261978
// Emit post-function debug and/or EH information.
2027-
for (const auto &HI : DebugHandlers) {
2028-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2029-
HI.TimerGroupDescription, TimePassesIsEnabled);
2030-
HI.Handler->endFunction(MF);
2031-
}
2032-
for (const auto &HI : Handlers) {
2033-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2034-
HI.TimerGroupDescription, TimePassesIsEnabled);
2035-
HI.Handler->endFunction(MF);
2036-
}
1979+
for (auto &Handler : DebugHandlers)
1980+
Handler->endFunction(MF);
1981+
for (auto &Handler : Handlers)
1982+
Handler->endFunction(MF);
20371983

20381984
// Emit section containing BB address offsets and their metadata, when
20391985
// BB labels are requested for this function. Skip empty functions.
@@ -2470,16 +2416,10 @@ bool AsmPrinter::doFinalization(Module &M) {
24702416
emitGlobalIFunc(M, IFunc);
24712417

24722418
// Finalize debug and EH information.
2473-
for (const auto &HI : DebugHandlers) {
2474-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2475-
HI.TimerGroupDescription, TimePassesIsEnabled);
2476-
HI.Handler->endModule();
2477-
}
2478-
for (const auto &HI : Handlers) {
2479-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2480-
HI.TimerGroupDescription, TimePassesIsEnabled);
2481-
HI.Handler->endModule();
2482-
}
2419+
for (auto &Handler : DebugHandlers)
2420+
Handler->endModule();
2421+
for (auto &Handler : Handlers)
2422+
Handler->endModule();
24832423

24842424
// This deletes all the ephemeral handlers that AsmPrinter added, while
24852425
// keeping all the user-added handlers alive until the AsmPrinter is
@@ -4001,9 +3941,9 @@ static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
40013941
void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
40023942
// End the previous funclet and start a new one.
40033943
if (MBB.isEHFuncletEntry()) {
4004-
for (const auto &HI : Handlers) {
4005-
HI.Handler->endFunclet();
4006-
HI.Handler->beginFunclet(MBB);
3944+
for (auto &Handler : Handlers) {
3945+
Handler->endFunclet();
3946+
Handler->beginFunclet(MBB);
40073947
}
40083948
}
40093949

@@ -4074,21 +4014,21 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
40744014
// if it begins a section (Entry block call is handled separately, next to
40754015
// beginFunction).
40764016
if (MBB.isBeginSection() && !MBB.isEntryBlock()) {
4077-
for (const auto &HI : DebugHandlers)
4078-
HI.Handler->beginBasicBlockSection(MBB);
4079-
for (const auto &HI : Handlers)
4080-
HI.Handler->beginBasicBlockSection(MBB);
4017+
for (auto &Handler : DebugHandlers)
4018+
Handler->beginBasicBlockSection(MBB);
4019+
for (auto &Handler : Handlers)
4020+
Handler->beginBasicBlockSection(MBB);
40814021
}
40824022
}
40834023

40844024
void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {
40854025
// Check if CFI information needs to be updated for this MBB with basic block
40864026
// sections.
40874027
if (MBB.isEndSection()) {
4088-
for (const auto &HI : DebugHandlers)
4089-
HI.Handler->endBasicBlockSection(MBB);
4090-
for (const auto &HI : Handlers)
4091-
HI.Handler->endBasicBlockSection(MBB);
4028+
for (auto &Handler : DebugHandlers)
4029+
Handler->endBasicBlockSection(MBB);
4030+
for (auto &Handler : Handlers)
4031+
Handler->endBasicBlockSection(MBB);
40924032
}
40934033
}
40944034

llvm/lib/Target/BPF/BPFAsmPrinter.cpp

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

6867
return false;

llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,7 @@ class AsmPrinterHandlerTest : public AsmPrinterFixtureBase {
395395
return false;
396396

397397
auto *AP = TestPrinter->getAP();
398-
AP->addAsmPrinterHandler(AsmPrinter::HandlerInfo(
399-
std::unique_ptr<AsmPrinterHandler>(new TestHandler(*this)),
400-
"TestTimerName", "TestTimerDesc", "TestGroupName", "TestGroupDesc"));
398+
AP->addAsmPrinterHandler(std::make_unique<TestHandler>(*this));
401399
LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(&AP->TM);
402400
legacy::PassManager PM;
403401
PM.add(new MachineModuleInfoWrapperPass(LLVMTM));
@@ -407,9 +405,7 @@ class AsmPrinterHandlerTest : public AsmPrinterFixtureBase {
407405
M->setDataLayout(LLVMTM->createDataLayout());
408406
PM.run(*M);
409407
// Now check that we can run it twice.
410-
AP->addAsmPrinterHandler(AsmPrinter::HandlerInfo(
411-
std::unique_ptr<AsmPrinterHandler>(new TestHandler(*this)),
412-
"TestTimerName", "TestTimerDesc", "TestGroupName", "TestGroupDesc"));
408+
AP->addAsmPrinterHandler(std::make_unique<TestHandler>(*this));
413409
PM.run(*M);
414410
return true;
415411
}

0 commit comments

Comments
 (0)