Skip to content

Commit f6b0555

Browse files
authored
[AsmPrinter] Reintroduce full AsmPrinterHandler API (llvm#122297)
This restores the functionality of AsmPrinterHandlers to what it was prior to llvm#96785. The attempted hack there of adding a duplicate DebugHandlerBase handling added a lot of hidden state and assumptions, which just segfaulted when we tried to continuing using this API. Instead, this just goes back to the old design, but adds a separate array for the basic EH handles. The duplicate array is identical to the other array of handler, but which doesn't get their begin/endInstruction callbacks called. This still saves the negligible but measurable amount of virtual function calls as was the goal of llvm#96785, while restoring the API to the pre-LLVM-19 status quo.
1 parent eac23a5 commit f6b0555

File tree

7 files changed

+65
-108
lines changed

7 files changed

+65
-108
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class DebugHandlerBase;
4444
class DIE;
4545
class DIEAbbrev;
4646
class DwarfDebug;
47+
class EHStreamer;
4748
class GCMetadataPrinter;
4849
class GCStrategy;
4950
class GlobalAlias;
@@ -187,15 +188,17 @@ class AsmPrinter : public MachineFunctionPass {
187188
/// For dso_local functions, the current $local alias for the function.
188189
MCSymbol *CurrentFnBeginLocal = nullptr;
189190

190-
/// A vector of all debug/EH info emitters we should use. This vector
191-
/// maintains ownership of the emitters.
191+
/// A handle to the EH info emitter (if present).
192+
// Only for EHStreamer subtypes, but some C++ compilers will incorrectly warn
193+
// us if we declare that directly.
194+
SmallVector<std::unique_ptr<AsmPrinterHandler>, 1> EHHandlers;
195+
196+
// A vector of all Debuginfo emitters we should use. Protected so that
197+
// targets can add their own. This vector maintains ownership of the
198+
// emitters.
192199
SmallVector<std::unique_ptr<AsmPrinterHandler>, 2> Handlers;
193200
size_t NumUserHandlers = 0;
194201

195-
/// Debuginfo handler. Protected so that targets can add their own.
196-
SmallVector<std::unique_ptr<DebugHandlerBase>, 1> DebugHandlers;
197-
size_t NumUserDebugHandlers = 0;
198-
199202
StackMaps SM;
200203

201204
private:
@@ -527,8 +530,6 @@ class AsmPrinter : public MachineFunctionPass {
527530

528531
void addAsmPrinterHandler(std::unique_ptr<AsmPrinterHandler> Handler);
529532

530-
void addDebugHandler(std::unique_ptr<DebugHandlerBase> Handler);
531-
532533
// Targets can, or in the case of EmitInstruction, must implement these to
533534
// customize output.
534535

llvm/include/llvm/CodeGen/AsmPrinterHandler.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ class AsmPrinterHandler {
6464
/// immediately prior to markFunctionEnd.
6565
virtual void endBasicBlockSection(const MachineBasicBlock &MBB) {}
6666

67+
/// For symbols that have a size designated (e.g. common symbols),
68+
/// this tracks that size.
69+
virtual void setSymbolSize(const MCSymbol *Sym, uint64_t Size) {}
70+
71+
/// Process beginning of an instruction.
72+
virtual void beginInstruction(const MachineInstr *MI) {}
73+
74+
/// Process end of an instruction.
75+
virtual void endInstruction() {}
76+
77+
virtual void beginCodeAlignment(const MachineBasicBlock &MBB) {}
78+
6779
/// Emit target-specific EH funclet machinery.
6880
virtual void beginFunclet(const MachineBasicBlock &MBB,
6981
MCSymbol *Sym = nullptr) {}

llvm/include/llvm/CodeGen/DebugHandlerBase.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,10 @@ struct DbgVariableLocation {
5050

5151
/// Base class for debug information backends. Common functionality related to
5252
/// tracking which variables and scopes are alive at a given PC live here.
53-
class DebugHandlerBase {
53+
class DebugHandlerBase : public AsmPrinterHandler {
5454
protected:
5555
DebugHandlerBase(AsmPrinter *A);
5656

57-
public:
58-
virtual ~DebugHandlerBase();
59-
60-
protected:
6157
/// Target of debug info emission.
6258
AsmPrinter *Asm = nullptr;
6359

@@ -120,24 +116,20 @@ class DebugHandlerBase {
120116
private:
121117
InstructionOrdering InstOrdering;
122118

119+
// AsmPrinterHandler overrides.
123120
public:
124-
/// For symbols that have a size designated (e.g. common symbols),
125-
/// this tracks that size. Only used by DWARF.
126-
virtual void setSymbolSize(const MCSymbol *Sym, uint64_t Size) {}
127-
128-
virtual void beginModule(Module *M);
129-
virtual void endModule() = 0;
121+
virtual ~DebugHandlerBase() override;
130122

131-
virtual void beginInstruction(const MachineInstr *MI);
132-
virtual void endInstruction();
123+
void beginModule(Module *M) override;
133124

134-
void beginFunction(const MachineFunction *MF);
135-
void endFunction(const MachineFunction *MF);
125+
void beginInstruction(const MachineInstr *MI) override;
126+
void endInstruction() override;
136127

137-
void beginBasicBlockSection(const MachineBasicBlock &MBB);
138-
void endBasicBlockSection(const MachineBasicBlock &MBB);
128+
void beginFunction(const MachineFunction *MF) override;
129+
void endFunction(const MachineFunction *MF) override;
139130

140-
virtual void beginCodeAlignment(const MachineBasicBlock &MBB) {}
131+
void beginBasicBlockSection(const MachineBasicBlock &MBB) override;
132+
void endBasicBlockSection(const MachineBasicBlock &MBB) override;
141133

142134
/// Return Label preceding the instruction.
143135
MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,11 @@ bool AsmPrinter::doInitialization(Module &M) {
561561
if (MAI->doesSupportDebugInformation()) {
562562
bool EmitCodeView = M.getCodeViewFlag();
563563
if (EmitCodeView && TM.getTargetTriple().isOSWindows())
564-
DebugHandlers.push_back(std::make_unique<CodeViewDebug>(this));
564+
Handlers.push_back(std::make_unique<CodeViewDebug>(this));
565565
if (!EmitCodeView || M.getDwarfVersion()) {
566566
if (hasDebugInfo()) {
567567
DD = new DwarfDebug(this);
568-
DebugHandlers.push_back(std::unique_ptr<DwarfDebug>(DD));
568+
Handlers.push_back(std::unique_ptr<DwarfDebug>(DD));
569569
}
570570
}
571571
}
@@ -632,12 +632,12 @@ bool AsmPrinter::doInitialization(Module &M) {
632632

633633
// Emit tables for any value of cfguard flag (i.e. cfguard=1 or cfguard=2).
634634
if (mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("cfguard")))
635-
Handlers.push_back(std::make_unique<WinCFGuard>(this));
635+
EHHandlers.push_back(std::make_unique<WinCFGuard>(this));
636636

637-
for (auto &Handler : DebugHandlers)
638-
Handler->beginModule(&M);
639637
for (auto &Handler : Handlers)
640638
Handler->beginModule(&M);
639+
for (auto &Handler : EHHandlers)
640+
Handler->beginModule(&M);
641641

642642
return false;
643643
}
@@ -784,7 +784,7 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
784784
// sections and expected to be contiguous (e.g. ObjC metadata).
785785
const Align Alignment = getGVAlignment(GV, DL);
786786

787-
for (auto &Handler : DebugHandlers)
787+
for (auto &Handler : Handlers)
788788
Handler->setSymbolSize(GVSym, Size);
789789

790790
// Handle common symbols
@@ -1054,14 +1054,14 @@ void AsmPrinter::emitFunctionHeader() {
10541054
}
10551055

10561056
// Emit pre-function debug and/or EH information.
1057-
for (auto &Handler : DebugHandlers) {
1057+
for (auto &Handler : Handlers) {
10581058
Handler->beginFunction(MF);
10591059
Handler->beginBasicBlockSection(MF->front());
10601060
}
1061-
for (auto &Handler : Handlers)
1061+
for (auto &Handler : EHHandlers) {
10621062
Handler->beginFunction(MF);
1063-
for (auto &Handler : Handlers)
10641063
Handler->beginBasicBlockSection(MF->front());
1064+
}
10651065

10661066
// Emit the prologue data.
10671067
if (F.hasPrologueData())
@@ -1836,7 +1836,7 @@ void AsmPrinter::emitFunctionBody() {
18361836
if (MDNode *MD = MI.getPCSections())
18371837
emitPCSectionsLabel(*MF, *MD);
18381838

1839-
for (auto &Handler : DebugHandlers)
1839+
for (auto &Handler : Handlers)
18401840
Handler->beginInstruction(&MI);
18411841

18421842
if (isVerbose())
@@ -1952,7 +1952,7 @@ void AsmPrinter::emitFunctionBody() {
19521952
if (MCSymbol *S = MI.getPostInstrSymbol())
19531953
OutStreamer->emitLabel(S);
19541954

1955-
for (auto &Handler : DebugHandlers)
1955+
for (auto &Handler : Handlers)
19561956
Handler->endInstruction();
19571957
}
19581958

@@ -2089,24 +2089,26 @@ void AsmPrinter::emitFunctionBody() {
20892089
// Call endBasicBlockSection on the last block now, if it wasn't already
20902090
// called.
20912091
if (!MF->back().isEndSection()) {
2092-
for (auto &Handler : DebugHandlers)
2093-
Handler->endBasicBlockSection(MF->back());
20942092
for (auto &Handler : Handlers)
20952093
Handler->endBasicBlockSection(MF->back());
2094+
for (auto &Handler : EHHandlers)
2095+
Handler->endBasicBlockSection(MF->back());
20962096
}
20972097
for (auto &Handler : Handlers)
20982098
Handler->markFunctionEnd();
2099+
for (auto &Handler : EHHandlers)
2100+
Handler->markFunctionEnd();
20992101
// Update the end label of the entry block's section.
21002102
MBBSectionRanges[MF->front().getSectionID()].EndLabel = CurrentFnEnd;
21012103

21022104
// Print out jump tables referenced by the function.
21032105
emitJumpTableInfo();
21042106

21052107
// Emit post-function debug and/or EH information.
2106-
for (auto &Handler : DebugHandlers)
2107-
Handler->endFunction(MF);
21082108
for (auto &Handler : Handlers)
21092109
Handler->endFunction(MF);
2110+
for (auto &Handler : EHHandlers)
2111+
Handler->endFunction(MF);
21102112

21112113
// Emit section containing BB address offsets and their metadata, when
21122114
// BB labels are requested for this function. Skip empty functions.
@@ -2583,17 +2585,16 @@ bool AsmPrinter::doFinalization(Module &M) {
25832585
emitGlobalIFunc(M, IFunc);
25842586

25852587
// Finalize debug and EH information.
2586-
for (auto &Handler : DebugHandlers)
2587-
Handler->endModule();
25882588
for (auto &Handler : Handlers)
25892589
Handler->endModule();
2590+
for (auto &Handler : EHHandlers)
2591+
Handler->endModule();
25902592

25912593
// This deletes all the ephemeral handlers that AsmPrinter added, while
25922594
// keeping all the user-added handlers alive until the AsmPrinter is
25932595
// destroyed.
2596+
EHHandlers.clear();
25942597
Handlers.erase(Handlers.begin() + NumUserHandlers, Handlers.end());
2595-
DebugHandlers.erase(DebugHandlers.begin() + NumUserDebugHandlers,
2596-
DebugHandlers.end());
25972598
DD = nullptr;
25982599

25992600
// If the target wants to know about weak references, print them all.
@@ -4196,6 +4197,10 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
41964197
Handler->endFunclet();
41974198
Handler->beginFunclet(MBB);
41984199
}
4200+
for (auto &Handler : EHHandlers) {
4201+
Handler->endFunclet();
4202+
Handler->beginFunclet(MBB);
4203+
}
41994204
}
42004205

42014206
// Switch to a new section if this basic block must begin a section. The
@@ -4208,7 +4213,7 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
42084213
CurrentSectionBeginSym = MBB.getSymbol();
42094214
}
42104215

4211-
for (auto &Handler : DebugHandlers)
4216+
for (auto &Handler : Handlers)
42124217
Handler->beginCodeAlignment(MBB);
42134218

42144219
// Emit an alignment directive for this block, if needed.
@@ -4268,21 +4273,21 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
42684273
// if it begins a section (Entry block call is handled separately, next to
42694274
// beginFunction).
42704275
if (MBB.isBeginSection() && !MBB.isEntryBlock()) {
4271-
for (auto &Handler : DebugHandlers)
4272-
Handler->beginBasicBlockSection(MBB);
42734276
for (auto &Handler : Handlers)
42744277
Handler->beginBasicBlockSection(MBB);
4278+
for (auto &Handler : EHHandlers)
4279+
Handler->beginBasicBlockSection(MBB);
42754280
}
42764281
}
42774282

42784283
void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {
42794284
// Check if CFI information needs to be updated for this MBB with basic block
42804285
// sections.
42814286
if (MBB.isEndSection()) {
4282-
for (auto &Handler : DebugHandlers)
4283-
Handler->endBasicBlockSection(MBB);
42844287
for (auto &Handler : Handlers)
42854288
Handler->endBasicBlockSection(MBB);
4289+
for (auto &Handler : EHHandlers)
4290+
Handler->endBasicBlockSection(MBB);
42864291
}
42874292
}
42884293

@@ -4415,12 +4420,7 @@ void AsmPrinter::addAsmPrinterHandler(
44154420
NumUserHandlers++;
44164421
}
44174422

4418-
void AsmPrinter::addDebugHandler(std::unique_ptr<DebugHandlerBase> Handler) {
4419-
DebugHandlers.insert(DebugHandlers.begin(), std::move(Handler));
4420-
NumUserDebugHandlers++;
4421-
}
4422-
4423-
/// Pin vtable to this file.
4423+
/// Pin vtables to this file.
44244424
AsmPrinterHandler::~AsmPrinterHandler() = default;
44254425

44264426
void AsmPrinterHandler::markFunctionEnd() {}

llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#define LLVM_LIB_CODEGEN_ASMPRINTER_PSEUDOPROBEPRINTER_H
1515

1616
#include "llvm/ADT/DenseMap.h"
17-
#include "llvm/CodeGen/AsmPrinterHandler.h"
1817

1918
namespace llvm {
2019

llvm/lib/Target/BPF/BPFAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool BPFAsmPrinter::doInitialization(Module &M) {
6060
// Only emit BTF when debuginfo available.
6161
if (MAI->doesSupportDebugInformation() && !M.debug_compile_units().empty()) {
6262
BTF = new BTFDebug(this);
63-
DebugHandlers.push_back(std::unique_ptr<BTFDebug>(BTF));
63+
Handlers.push_back(std::unique_ptr<BTFDebug>(BTF));
6464
}
6565

6666
return false;

llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,13 @@ class AsmPrinterHandlerTest : public AsmPrinterFixtureBase {
384384
public:
385385
TestHandler(AsmPrinterHandlerTest &Test) : Test(Test) {}
386386
virtual ~TestHandler() {}
387+
virtual void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {}
387388
virtual void beginModule(Module *M) override { Test.BeginCount++; }
388389
virtual void endModule() override { Test.EndCount++; }
389390
virtual void beginFunction(const MachineFunction *MF) override {}
390391
virtual void endFunction(const MachineFunction *MF) override {}
392+
virtual void beginInstruction(const MachineInstr *MI) override {}
393+
virtual void endInstruction() override {}
391394
};
392395

393396
protected:
@@ -424,54 +427,4 @@ TEST_F(AsmPrinterHandlerTest, Basic) {
424427
ASSERT_EQ(EndCount, 3);
425428
}
426429

427-
class AsmPrinterDebugHandlerTest : public AsmPrinterFixtureBase {
428-
class TestDebugHandler : public DebugHandlerBase {
429-
AsmPrinterDebugHandlerTest &Test;
430-
431-
public:
432-
TestDebugHandler(AsmPrinterDebugHandlerTest &Test, AsmPrinter *AP)
433-
: DebugHandlerBase(AP), Test(Test) {}
434-
virtual ~TestDebugHandler() {}
435-
virtual void beginModule(Module *M) override { Test.BeginCount++; }
436-
virtual void endModule() override { Test.EndCount++; }
437-
virtual void beginFunctionImpl(const MachineFunction *MF) override {}
438-
virtual void endFunctionImpl(const MachineFunction *MF) override {}
439-
virtual void beginInstruction(const MachineInstr *MI) override {}
440-
virtual void endInstruction() override {}
441-
};
442-
443-
protected:
444-
bool init(const std::string &TripleStr, unsigned DwarfVersion,
445-
dwarf::DwarfFormat DwarfFormat) {
446-
if (!AsmPrinterFixtureBase::init(TripleStr, DwarfVersion, DwarfFormat))
447-
return false;
448-
449-
auto *AP = TestPrinter->getAP();
450-
AP->addDebugHandler(std::make_unique<TestDebugHandler>(*this, AP));
451-
TargetMachine *TM = &AP->TM;
452-
legacy::PassManager PM;
453-
PM.add(new MachineModuleInfoWrapperPass(TM));
454-
PM.add(TestPrinter->releaseAP()); // Takes ownership of destroying AP
455-
LLVMContext Context;
456-
std::unique_ptr<Module> M(new Module("TestModule", Context));
457-
M->setDataLayout(TM->createDataLayout());
458-
PM.run(*M);
459-
// Now check that we can run it twice.
460-
AP->addDebugHandler(std::make_unique<TestDebugHandler>(*this, AP));
461-
PM.run(*M);
462-
return true;
463-
}
464-
465-
int BeginCount = 0;
466-
int EndCount = 0;
467-
};
468-
469-
TEST_F(AsmPrinterDebugHandlerTest, Basic) {
470-
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
471-
GTEST_SKIP();
472-
473-
ASSERT_EQ(BeginCount, 3);
474-
ASSERT_EQ(EndCount, 3);
475-
}
476-
477430
} // end namespace

0 commit comments

Comments
 (0)