Skip to content

Commit 3cb75ba

Browse files
vtjnashZentrik
authored andcommitted
[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. (cherry picked from commit f6b0555) Co-authored-by: Zentrik <[email protected]>
1 parent 5b3f2e8 commit 3cb75ba

File tree

7 files changed

+62
-105
lines changed

7 files changed

+62
-105
lines changed

Diff for: llvm/include/llvm/CodeGen/AsmPrinter.h

+9-8
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:
@@ -521,8 +524,6 @@ class AsmPrinter : public MachineFunctionPass {
521524

522525
void addAsmPrinterHandler(std::unique_ptr<AsmPrinterHandler> Handler);
523526

524-
void addDebugHandler(std::unique_ptr<DebugHandlerBase> Handler);
525-
526527
// Targets can, or in the case of EmitInstruction, must implement these to
527528
// customize output.
528529

Diff for: llvm/include/llvm/CodeGen/AsmPrinterHandler.h

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ 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+
6777
/// Emit target-specific EH funclet machinery.
6878
virtual void beginFunclet(const MachineBasicBlock &MBB,
6979
MCSymbol *Sym = nullptr) {}

Diff for: llvm/include/llvm/CodeGen/DebugHandlerBase.h

+10-16
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,22 +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) {}
121+
virtual ~DebugHandlerBase() override;
127122

128-
virtual void beginModule(Module *M);
129-
virtual void endModule() = 0;
123+
void beginModule(Module *M) override;
130124

131-
virtual void beginInstruction(const MachineInstr *MI);
132-
virtual void endInstruction();
125+
void beginInstruction(const MachineInstr *MI) override;
126+
void endInstruction() override;
133127

134-
void beginFunction(const MachineFunction *MF);
135-
void endFunction(const MachineFunction *MF);
128+
void beginFunction(const MachineFunction *MF) override;
129+
void endFunction(const MachineFunction *MF) override;
136130

137-
void beginBasicBlockSection(const MachineBasicBlock &MBB);
138-
void endBasicBlockSection(const MachineBasicBlock &MBB);
131+
void beginBasicBlockSection(const MachineBasicBlock &MBB) override;
132+
void endBasicBlockSection(const MachineBasicBlock &MBB) override;
139133

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

Diff for: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

+29-29
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,12 @@ bool AsmPrinter::doInitialization(Module &M) {
539539
if (MAI->doesSupportDebugInformation()) {
540540
bool EmitCodeView = M.getCodeViewFlag();
541541
if (EmitCodeView && TM.getTargetTriple().isOSWindows())
542-
DebugHandlers.push_back(std::make_unique<CodeViewDebug>(this));
542+
Handlers.push_back(std::make_unique<CodeViewDebug>(this));
543543
if (!EmitCodeView || M.getDwarfVersion()) {
544544
assert(MMI && "MMI could not be nullptr here!");
545545
if (MMI->hasDebugInfo()) {
546546
DD = new DwarfDebug(this);
547-
DebugHandlers.push_back(std::unique_ptr<DwarfDebug>(DD));
547+
Handlers.push_back(std::unique_ptr<DwarfDebug>(DD));
548548
}
549549
}
550550
}
@@ -611,12 +611,12 @@ bool AsmPrinter::doInitialization(Module &M) {
611611

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

616-
for (auto &Handler : DebugHandlers)
617-
Handler->beginModule(&M);
618616
for (auto &Handler : Handlers)
619617
Handler->beginModule(&M);
618+
for (auto &Handler : EHHandlers)
619+
Handler->beginModule(&M);
620620

621621
return false;
622622
}
@@ -763,7 +763,7 @@ void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
763763
// sections and expected to be contiguous (e.g. ObjC metadata).
764764
const Align Alignment = getGVAlignment(GV, DL);
765765

766-
for (auto &Handler : DebugHandlers)
766+
for (auto &Handler : Handlers)
767767
Handler->setSymbolSize(GVSym, Size);
768768

769769
// Handle common symbols
@@ -1035,14 +1035,14 @@ void AsmPrinter::emitFunctionHeader() {
10351035
}
10361036

10371037
// Emit pre-function debug and/or EH information.
1038-
for (auto &Handler : DebugHandlers) {
1038+
for (auto &Handler : Handlers) {
10391039
Handler->beginFunction(MF);
10401040
Handler->beginBasicBlockSection(MF->front());
10411041
}
1042-
for (auto &Handler : Handlers)
1042+
for (auto &Handler : EHHandlers) {
10431043
Handler->beginFunction(MF);
1044-
for (auto &Handler : Handlers)
10451044
Handler->beginBasicBlockSection(MF->front());
1045+
}
10461046

10471047
// Emit the prologue data.
10481048
if (F.hasPrologueData())
@@ -1737,7 +1737,7 @@ void AsmPrinter::emitFunctionBody() {
17371737
if (MDNode *MD = MI.getPCSections())
17381738
emitPCSectionsLabel(*MF, *MD);
17391739

1740-
for (auto &Handler : DebugHandlers)
1740+
for (auto &Handler : Handlers)
17411741
Handler->beginInstruction(&MI);
17421742

17431743
if (isVerbose())
@@ -1832,7 +1832,7 @@ void AsmPrinter::emitFunctionBody() {
18321832
if (MCSymbol *S = MI.getPostInstrSymbol())
18331833
OutStreamer->emitLabel(S);
18341834

1835-
for (auto &Handler : DebugHandlers)
1835+
for (auto &Handler : Handlers)
18361836
Handler->endInstruction();
18371837
}
18381838

@@ -1966,13 +1966,15 @@ void AsmPrinter::emitFunctionBody() {
19661966
// Call endBasicBlockSection on the last block now, if it wasn't already
19671967
// called.
19681968
if (!MF->back().isEndSection()) {
1969-
for (auto &Handler : DebugHandlers)
1970-
Handler->endBasicBlockSection(MF->back());
19711969
for (auto &Handler : Handlers)
19721970
Handler->endBasicBlockSection(MF->back());
1971+
for (auto &Handler : EHHandlers)
1972+
Handler->endBasicBlockSection(MF->back());
19731973
}
19741974
for (auto &Handler : Handlers)
19751975
Handler->markFunctionEnd();
1976+
for (auto &Handler : EHHandlers)
1977+
Handler->markFunctionEnd();
19761978

19771979
assert(!MBBSectionRanges.contains(MF->front().getSectionID()) &&
19781980
"Overwrite section range");
@@ -1983,10 +1985,10 @@ void AsmPrinter::emitFunctionBody() {
19831985
emitJumpTableInfo();
19841986

19851987
// Emit post-function debug and/or EH information.
1986-
for (auto &Handler : DebugHandlers)
1987-
Handler->endFunction(MF);
19881988
for (auto &Handler : Handlers)
19891989
Handler->endFunction(MF);
1990+
for (auto &Handler : EHHandlers)
1991+
Handler->endFunction(MF);
19901992

19911993
// Emit section containing BB address offsets and their metadata, when
19921994
// BB labels are requested for this function. Skip empty functions.
@@ -2425,17 +2427,16 @@ bool AsmPrinter::doFinalization(Module &M) {
24252427
emitGlobalIFunc(M, IFunc);
24262428

24272429
// Finalize debug and EH information.
2428-
for (auto &Handler : DebugHandlers)
2429-
Handler->endModule();
24302430
for (auto &Handler : Handlers)
24312431
Handler->endModule();
2432+
for (auto &Handler : EHHandlers)
2433+
Handler->endModule();
24322434

24332435
// This deletes all the ephemeral handlers that AsmPrinter added, while
24342436
// keeping all the user-added handlers alive until the AsmPrinter is
24352437
// destroyed.
2438+
EHHandlers.clear();
24362439
Handlers.erase(Handlers.begin() + NumUserHandlers, Handlers.end());
2437-
DebugHandlers.erase(DebugHandlers.begin() + NumUserDebugHandlers,
2438-
DebugHandlers.end());
24392440
DD = nullptr;
24402441

24412442
// If the target wants to know about weak references, print them all.
@@ -3957,6 +3958,10 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
39573958
Handler->endFunclet();
39583959
Handler->beginFunclet(MBB);
39593960
}
3961+
for (auto &Handler : EHHandlers) {
3962+
Handler->endFunclet();
3963+
Handler->beginFunclet(MBB);
3964+
}
39603965
}
39613966

39623967
// Switch to a new section if this basic block must begin a section. The
@@ -4026,21 +4031,21 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
40264031
// if it begins a section (Entry block call is handled separately, next to
40274032
// beginFunction).
40284033
if (MBB.isBeginSection() && !MBB.isEntryBlock()) {
4029-
for (auto &Handler : DebugHandlers)
4030-
Handler->beginBasicBlockSection(MBB);
40314034
for (auto &Handler : Handlers)
40324035
Handler->beginBasicBlockSection(MBB);
4036+
for (auto &Handler : EHHandlers)
4037+
Handler->beginBasicBlockSection(MBB);
40334038
}
40344039
}
40354040

40364041
void AsmPrinter::emitBasicBlockEnd(const MachineBasicBlock &MBB) {
40374042
// Check if CFI information needs to be updated for this MBB with basic block
40384043
// sections.
40394044
if (MBB.isEndSection()) {
4040-
for (auto &Handler : DebugHandlers)
4041-
Handler->endBasicBlockSection(MBB);
40424045
for (auto &Handler : Handlers)
40434046
Handler->endBasicBlockSection(MBB);
4047+
for (auto &Handler : EHHandlers)
4048+
Handler->endBasicBlockSection(MBB);
40444049
}
40454050
}
40464051

@@ -4174,12 +4179,7 @@ void AsmPrinter::addAsmPrinterHandler(
41744179
NumUserHandlers++;
41754180
}
41764181

4177-
void AsmPrinter::addDebugHandler(std::unique_ptr<DebugHandlerBase> Handler) {
4178-
DebugHandlers.insert(DebugHandlers.begin(), std::move(Handler));
4179-
NumUserDebugHandlers++;
4180-
}
4181-
4182-
/// Pin vtable to this file.
4182+
/// Pin vtables to this file.
41834183
AsmPrinterHandler::~AsmPrinterHandler() = default;
41844184

41854185
void AsmPrinterHandler::markFunctionEnd() {}

Diff for: llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.h

-1
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

Diff for: llvm/lib/Target/BPF/BPFAsmPrinter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +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.push_back(std::unique_ptr<BTFDebug>(BTF));
65+
Handlers.push_back(std::unique_ptr<BTFDebug>(BTF));
6666
}
6767

6868
return false;

Diff for: llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp

+3-50
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-
LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(&AP->TM);
452-
legacy::PassManager PM;
453-
PM.add(new MachineModuleInfoWrapperPass(LLVMTM));
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(LLVMTM->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)