Skip to content

Commit 80ffec7

Browse files
authored
[AsmPrinter] Remove timers (#97046)
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. Also outside of the innermost loop, timers add a measurable overhead. As this is quite expensive for a mostly unused profiling facility, remove the timers. Fixes #39650.
1 parent f209469 commit 80ffec7

File tree

4 files changed

+69
-156
lines changed

4 files changed

+69
-156
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "llvm/ADT/MapVector.h"
2020
#include "llvm/ADT/SmallVector.h"
2121
#include "llvm/BinaryFormat/Dwarf.h"
22-
#include "llvm/CodeGen/AsmPrinterHandler.h"
23-
#include "llvm/CodeGen/DebugHandlerBase.h"
2422
#include "llvm/CodeGen/DwarfStringPoolEntry.h"
2523
#include "llvm/CodeGen/MachineFunctionPass.h"
2624
#include "llvm/CodeGen/StackMaps.h"
@@ -35,12 +33,14 @@
3533
namespace llvm {
3634

3735
class AddrLabelMap;
36+
class AsmPrinterHandler;
3837
class BasicBlock;
3938
class BlockAddress;
4039
class Constant;
4140
class ConstantArray;
4241
class ConstantPtrAuth;
4342
class DataLayout;
43+
class DebugHandlerBase;
4444
class DIE;
4545
class DIEAbbrev;
4646
class DwarfDebug;
@@ -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,15 +519,9 @@ class AsmPrinter : public MachineFunctionPass {
536519
// Overridable Hooks
537520
//===------------------------------------------------------------------===//
538521

539-
void addAsmPrinterHandler(HandlerInfo<AsmPrinterHandler> Handler) {
540-
Handlers.insert(Handlers.begin(), std::move(Handler));
541-
NumUserHandlers++;
542-
}
522+
void addAsmPrinterHandler(std::unique_ptr<AsmPrinterHandler> Handler);
543523

544-
void addDebugHandler(HandlerInfo<DebugHandlerBase> Handler) {
545-
DebugHandlers.insert(DebugHandlers.begin(), std::move(Handler));
546-
NumUserDebugHandlers++;
547-
}
524+
void addDebugHandler(std::unique_ptr<DebugHandlerBase> Handler);
548525

549526
// Targets can, or in the case of EmitInstruction, must implement these to
550527
// customize output.

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 58 additions & 113 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,11 +1737,8 @@ void AsmPrinter::emitFunctionBody() {
17761737
if (MDNode *MD = MI.getPCSections())
17771738
emitPCSectionsLabel(*MF, *MD);
17781739

1779-
for (const auto &HI : DebugHandlers) {
1780-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1781-
HI.TimerGroupDescription, TimePassesIsEnabled);
1782-
HI.Handler->beginInstruction(&MI);
1783-
}
1740+
for (auto &Handler : DebugHandlers)
1741+
Handler->beginInstruction(&MI);
17841742

17851743
if (isVerbose())
17861744
emitComments(MI, OutStreamer->getCommentOS());
@@ -1874,11 +1832,8 @@ void AsmPrinter::emitFunctionBody() {
18741832
if (MCSymbol *S = MI.getPostInstrSymbol())
18751833
OutStreamer->emitLabel(S);
18761834

1877-
for (const auto &HI : DebugHandlers) {
1878-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
1879-
HI.TimerGroupDescription, TimePassesIsEnabled);
1880-
HI.Handler->endInstruction();
1881-
}
1835+
for (auto &Handler : DebugHandlers)
1836+
Handler->endInstruction();
18821837
}
18831838

18841839
// We must emit temporary symbol for the end of this basic block, if either
@@ -2009,22 +1964,13 @@ void AsmPrinter::emitFunctionBody() {
20091964
// Call endBasicBlockSection on the last block now, if it wasn't already
20101965
// called.
20111966
if (!MF->back().isEndSection()) {
2012-
for (const auto &HI : DebugHandlers) {
2013-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2014-
HI.TimerGroupDescription, TimePassesIsEnabled);
2015-
HI.Handler->endBasicBlockSection(MF->back());
2016-
}
2017-
for (const auto &HI : Handlers) {
2018-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2019-
HI.TimerGroupDescription, TimePassesIsEnabled);
2020-
HI.Handler->endBasicBlockSection(MF->back());
2021-
}
2022-
}
2023-
for (const auto &HI : Handlers) {
2024-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2025-
HI.TimerGroupDescription, TimePassesIsEnabled);
2026-
HI.Handler->markFunctionEnd();
1967+
for (auto &Handler : DebugHandlers)
1968+
Handler->endBasicBlockSection(MF->back());
1969+
for (auto &Handler : Handlers)
1970+
Handler->endBasicBlockSection(MF->back());
20271971
}
1972+
for (auto &Handler : Handlers)
1973+
Handler->markFunctionEnd();
20281974

20291975
MBBSectionRanges[MF->front().getSectionIDNum()] =
20301976
MBBSectionRange{CurrentFnBegin, CurrentFnEnd};
@@ -2033,16 +1979,10 @@ void AsmPrinter::emitFunctionBody() {
20331979
emitJumpTableInfo();
20341980

20351981
// Emit post-function debug and/or EH information.
2036-
for (const auto &HI : DebugHandlers) {
2037-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2038-
HI.TimerGroupDescription, TimePassesIsEnabled);
2039-
HI.Handler->endFunction(MF);
2040-
}
2041-
for (const auto &HI : Handlers) {
2042-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2043-
HI.TimerGroupDescription, TimePassesIsEnabled);
2044-
HI.Handler->endFunction(MF);
2045-
}
1982+
for (auto &Handler : DebugHandlers)
1983+
Handler->endFunction(MF);
1984+
for (auto &Handler : Handlers)
1985+
Handler->endFunction(MF);
20461986

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

24812421
// Finalize debug and EH information.
2482-
for (const auto &HI : DebugHandlers) {
2483-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2484-
HI.TimerGroupDescription, TimePassesIsEnabled);
2485-
HI.Handler->endModule();
2486-
}
2487-
for (const auto &HI : Handlers) {
2488-
NamedRegionTimer T(HI.TimerName, HI.TimerDescription, HI.TimerGroupName,
2489-
HI.TimerGroupDescription, TimePassesIsEnabled);
2490-
HI.Handler->endModule();
2491-
}
2422+
for (auto &Handler : DebugHandlers)
2423+
Handler->endModule();
2424+
for (auto &Handler : Handlers)
2425+
Handler->endModule();
24922426

24932427
// This deletes all the ephemeral handlers that AsmPrinter added, while
24942428
// keeping all the user-added handlers alive until the AsmPrinter is
@@ -4010,9 +3944,9 @@ static void emitBasicBlockLoopComments(const MachineBasicBlock &MBB,
40103944
void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
40113945
// End the previous funclet and start a new one.
40123946
if (MBB.isEHFuncletEntry()) {
4013-
for (const auto &HI : Handlers) {
4014-
HI.Handler->endFunclet();
4015-
HI.Handler->beginFunclet(MBB);
3947+
for (auto &Handler : Handlers) {
3948+
Handler->endFunclet();
3949+
Handler->beginFunclet(MBB);
40163950
}
40173951
}
40183952

@@ -4083,21 +4017,21 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
40834017
// if it begins a section (Entry block call is handled separately, next to
40844018
// beginFunction).
40854019
if (MBB.isBeginSection() && !MBB.isEntryBlock()) {
4086-
for (const auto &HI : DebugHandlers)
4087-
HI.Handler->beginBasicBlockSection(MBB);
4088-
for (const auto &HI : Handlers)
4089-
HI.Handler->beginBasicBlockSection(MBB);
4020+
for (auto &Handler : DebugHandlers)
4021+
Handler->beginBasicBlockSection(MBB);
4022+
for (auto &Handler : Handlers)
4023+
Handler->beginBasicBlockSection(MBB);
40904024
}
40914025
}
40924026

40934027
void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {
40944028
// Check if CFI information needs to be updated for this MBB with basic block
40954029
// sections.
40964030
if (MBB.isEndSection()) {
4097-
for (const auto &HI : DebugHandlers)
4098-
HI.Handler->endBasicBlockSection(MBB);
4099-
for (const auto &HI : Handlers)
4100-
HI.Handler->endBasicBlockSection(MBB);
4031+
for (auto &Handler : DebugHandlers)
4032+
Handler->endBasicBlockSection(MBB);
4033+
for (auto &Handler : Handlers)
4034+
Handler->endBasicBlockSection(MBB);
41014035
}
41024036
}
41034037

@@ -4225,6 +4159,17 @@ void AsmPrinter::emitStackMaps() {
42254159
SM.serializeToStackMapSection();
42264160
}
42274161

4162+
void AsmPrinter::addAsmPrinterHandler(
4163+
std::unique_ptr<AsmPrinterHandler> Handler) {
4164+
Handlers.insert(Handlers.begin(), std::move(Handler));
4165+
NumUserHandlers++;
4166+
}
4167+
4168+
void AsmPrinter::addDebugHandler(std::unique_ptr<DebugHandlerBase> Handler) {
4169+
DebugHandlers.insert(DebugHandlers.begin(), std::move(Handler));
4170+
NumUserDebugHandlers++;
4171+
}
4172+
42284173
/// Pin vtable to this file.
42294174
AsmPrinterHandler::~AsmPrinterHandler() = default;
42304175

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;

0 commit comments

Comments
 (0)