Skip to content

Commit 63e1647

Browse files
authored
CodeGen: Remove MachineModuleInfo reference from MachineFunction (#100357)
This avoids another unserializable field. Move the DbgInfoAvailable field into the AsmPrinter, which is only really a cache/convenience bit for checking a direct IR module metadata check.
1 parent 4ce3993 commit 63e1647

24 files changed

+57
-62
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ class AsmPrinter : public MachineFunctionPass {
225225
/// split stack prologue.
226226
bool HasNoSplitStack = false;
227227

228+
/// True if debugging information is available in this module.
229+
bool DbgInfoAvailable = false;
230+
228231
protected:
229232
explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
230233

@@ -430,6 +433,9 @@ class AsmPrinter : public MachineFunctionPass {
430433
/// Get the CFISection type for the module.
431434
CFISection getModuleCFISectionType() const { return ModuleCFISection; }
432435

436+
/// Returns true if valid debug info is present.
437+
bool hasDebugInfo() const { return DbgInfoAvailable; }
438+
433439
bool needsSEHMoves();
434440

435441
/// Since emitting CFI unwind information is entangled with supporting the

llvm/include/llvm/CodeGen/MIRPrinter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace llvm {
2121

2222
class MachineBasicBlock;
2323
class MachineFunction;
24+
class MachineModuleInfo;
2425
class Module;
2526
template <typename T> class SmallVectorImpl;
2627

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class MachineConstantPool;
5959
class MachineFrameInfo;
6060
class MachineFunction;
6161
class MachineJumpTableInfo;
62-
class MachineModuleInfo;
6362
class MachineRegisterInfo;
6463
class MCContext;
6564
class MCInstrDesc;
@@ -260,7 +259,6 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
260259
const LLVMTargetMachine &Target;
261260
const TargetSubtargetInfo *STI;
262261
MCContext &Ctx;
263-
MachineModuleInfo &MMI;
264262

265263
// RegInfo - Information about each register in use in the function.
266264
MachineRegisterInfo *RegInfo;
@@ -395,15 +393,15 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
395393

396394
/// \}
397395

398-
/// Clear all the members of this MachineFunction, but the ones used
399-
/// to initialize again the MachineFunction.
400-
/// More specifically, this deallocates all the dynamically allocated
401-
/// objects and get rid of all the XXXInfo data structure, but keep
402-
/// unchanged the references to Fn, Target, MMI, and FunctionNumber.
396+
/// Clear all the members of this MachineFunction, but the ones used to
397+
/// initialize again the MachineFunction. More specifically, this deallocates
398+
/// all the dynamically allocated objects and get rids of all the XXXInfo data
399+
/// structure, but keeps unchanged the references to Fn, Target, and
400+
/// FunctionNumber.
403401
void clear();
404402
/// Allocate and initialize the different members.
405403
/// In particular, the XXXInfo data structure.
406-
/// \pre Fn, Target, MMI, and FunctionNumber are properly set.
404+
/// \pre Fn, Target, and FunctionNumber are properly set.
407405
void init();
408406

409407
public:
@@ -632,8 +630,8 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
632630
const static unsigned int DebugOperandMemNumber;
633631

634632
MachineFunction(Function &F, const LLVMTargetMachine &Target,
635-
const TargetSubtargetInfo &STI, unsigned FunctionNum,
636-
MachineModuleInfo &MMI);
633+
const TargetSubtargetInfo &STI, MCContext &Ctx,
634+
unsigned FunctionNum);
637635
MachineFunction(const MachineFunction &) = delete;
638636
MachineFunction &operator=(const MachineFunction &) = delete;
639637
~MachineFunction();
@@ -665,7 +663,6 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
665663

666664
GISelChangeObserver *getObserver() const { return Observer; }
667665

668-
MachineModuleInfo &getMMI() const { return MMI; }
669666
MCContext &getContext() const { return Ctx; }
670667

671668
/// Returns the Section this function belongs to.

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ class MachineModuleInfo {
9999
/// want.
100100
MachineModuleInfoImpl *ObjFileMMI;
101101

102-
/// True if debugging information is available in this module.
103-
bool DbgInfoAvailable = false;
104-
105102
/// Maps IR Functions to their corresponding MachineFunctions.
106103
DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
107104
/// Next unique number available for a MachineFunction.
@@ -168,9 +165,6 @@ class MachineModuleInfo {
168165
return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
169166
}
170167

171-
/// Returns true if valid debug info is present.
172-
bool hasDebugInfo() const { return DbgInfoAvailable; }
173-
174168
/// \}
175169
}; // End class MachineModuleInfo
176170

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct KnownBits;
7676
class LLVMContext;
7777
class MachineBasicBlock;
7878
class MachineConstantPoolValue;
79+
class MachineModuleInfo;
7980
class MCSymbol;
8081
class OptimizationRemarkEmitter;
8182
class ProfileSummaryInfo;

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class LiveIntervals;
4646
class LiveVariables;
4747
class MachineLoop;
4848
class MachineMemOperand;
49+
class MachineModuleInfo;
4950
class MachineRegisterInfo;
5051
class MCAsmInfo;
5152
class MCInst;

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ bool AsmPrinter::doInitialization(Module &M) {
436436
MMI = MMIWP ? &MMIWP->getMMI() : nullptr;
437437
HasSplitStack = false;
438438
HasNoSplitStack = false;
439+
DbgInfoAvailable = !M.debug_compile_units().empty();
439440

440441
AddrLabelSymbols = nullptr;
441442

@@ -541,8 +542,7 @@ bool AsmPrinter::doInitialization(Module &M) {
541542
if (EmitCodeView && TM.getTargetTriple().isOSWindows())
542543
DebugHandlers.push_back(std::make_unique<CodeViewDebug>(this));
543544
if (!EmitCodeView || M.getDwarfVersion()) {
544-
assert(MMI && "MMI could not be nullptr here!");
545-
if (MMI->hasDebugInfo()) {
545+
if (hasDebugInfo()) {
546546
DD = new DwarfDebug(this);
547547
DebugHandlers.push_back(std::unique_ptr<DwarfDebug>(DD));
548548
}
@@ -1277,8 +1277,7 @@ AsmPrinter::getFunctionCFISectionType(const Function &F) const {
12771277
if (MAI->usesCFIWithoutEH() && F.hasUWTable())
12781278
return CFISection::EH;
12791279

1280-
assert(MMI != nullptr && "Invalid machine module info");
1281-
if (MMI->hasDebugInfo() || TM.Options.ForceDwarfFrameSection)
1280+
if (hasDebugInfo() || TM.Options.ForceDwarfFrameSection)
12821281
return CFISection::Debug;
12831282

12841283
return CFISection::None;
@@ -1669,10 +1668,9 @@ void AsmPrinter::emitPCSections(const MachineFunction &MF) {
16691668
}
16701669

16711670
/// Returns true if function begin and end labels should be emitted.
1672-
static bool needFuncLabels(const MachineFunction &MF,
1673-
const MachineModuleInfo &MMI) {
1674-
if (!MF.getLandingPads().empty() || MF.hasEHFunclets() ||
1675-
MMI.hasDebugInfo() ||
1671+
static bool needFuncLabels(const MachineFunction &MF, const AsmPrinter &Asm) {
1672+
if (Asm.hasDebugInfo() || !MF.getLandingPads().empty() ||
1673+
MF.hasEHFunclets() ||
16761674
MF.getFunction().hasMetadata(LLVMContext::MD_pcsections))
16771675
return true;
16781676

@@ -1944,7 +1942,7 @@ void AsmPrinter::emitFunctionBody() {
19441942
// are automatically sized.
19451943
bool EmitFunctionSize = MAI->hasDotTypeDotSizeDirective() && !TT.isWasm();
19461944

1947-
if (needFuncLabels(*MF, *MMI) || EmitFunctionSize) {
1945+
if (EmitFunctionSize || needFuncLabels(*MF, *this)) {
19481946
// Create a symbol for the end of function.
19491947
CurrentFnEnd = createTempSymbol("func_end");
19501948
OutStreamer->emitLabel(CurrentFnEnd);
@@ -2588,7 +2586,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
25882586
if (F.hasFnAttribute("patchable-function-entry") ||
25892587
F.hasFnAttribute("function-instrument") ||
25902588
F.hasFnAttribute("xray-instruction-threshold") ||
2591-
needFuncLabels(MF, *MMI) || NeedsLocalForSize ||
2589+
needFuncLabels(MF, *this) || NeedsLocalForSize ||
25922590
MF.getTarget().Options.EmitStackSizeSection ||
25932591
MF.getTarget().Options.BBAddrMap || MF.hasBBLabels()) {
25942592
CurrentFnBegin = createTempSymbol("func_begin");

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
613613
void CodeViewDebug::beginModule(Module *M) {
614614
// If module doesn't have named metadata anchors or COFF debug section
615615
// is not available, skip any debug info related stuff.
616-
if (!MMI->hasDebugInfo() ||
616+
if (!Asm->hasDebugInfo() ||
617617
!Asm->getObjFileLowering().getCOFFDebugSymbolsSection()) {
618618
Asm = nullptr;
619619
return;
@@ -636,7 +636,7 @@ void CodeViewDebug::beginModule(Module *M) {
636636
}
637637

638638
void CodeViewDebug::endModule() {
639-
if (!Asm || !MMI->hasDebugInfo())
639+
if (!Asm || !Asm->hasDebugInfo())
640640
return;
641641

642642
// The COFF .debug$S section consists of several subsections, each starting

llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
351351
}
352352

353353
void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
354-
if (!Asm || !MMI->hasDebugInfo())
354+
if (!Asm || !Asm->hasDebugInfo())
355355
return;
356356

357357
assert(CurMI == nullptr);
@@ -377,7 +377,7 @@ void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
377377
}
378378

379379
void DebugHandlerBase::endInstruction() {
380-
if (!Asm || !MMI->hasDebugInfo())
380+
if (!Asm || !Asm->hasDebugInfo())
381381
return;
382382

383383
assert(CurMI != nullptr);

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ void DwarfDebug::endModule() {
14341434

14351435
// If we aren't actually generating debug info (check beginModule -
14361436
// conditionalized on the presence of the llvm.dbg.cu metadata node)
1437-
if (!Asm || !MMI->hasDebugInfo())
1437+
if (!Asm || !Asm->hasDebugInfo())
14381438
return;
14391439

14401440
// Finalize the debug info for the module.

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ static inline Align getFnStackAlignment(const TargetSubtargetInfo *STI,
162162
}
163163

164164
MachineFunction::MachineFunction(Function &F, const LLVMTargetMachine &Target,
165-
const TargetSubtargetInfo &STI,
166-
unsigned FunctionNum, MachineModuleInfo &mmi)
167-
: F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) {
165+
const TargetSubtargetInfo &STI, MCContext &Ctx,
166+
unsigned FunctionNum)
167+
: F(F), Target(Target), STI(&STI), Ctx(Ctx) {
168168
FunctionNumber = FunctionNum;
169169
init();
170170
}
@@ -659,9 +659,9 @@ bool MachineFunction::needsFrameMoves() const {
659659
// under this switch, we'd like .debug_frame to be precise when using -g. At
660660
// this moment, there's no way to specify that some CFI directives go into
661661
// .eh_frame only, while others go into .debug_frame only.
662-
return getMMI().hasDebugInfo() ||
663-
getTarget().Options.ForceDwarfFrameSection ||
664-
F.needsUnwindTableEntry();
662+
return getTarget().Options.ForceDwarfFrameSection ||
663+
F.needsUnwindTableEntry() ||
664+
!F.getParent()->debug_compile_units().empty();
665665
}
666666

667667
namespace llvm {

llvm/lib/CodeGen/MachineFunctionAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ MachineFunctionAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
3737
.getCachedResult<MachineModuleAnalysis>(*F.getParent())
3838
->getMMI();
3939
auto MF = std::make_unique<MachineFunction>(
40-
F, *TM, STI, Context.generateMachineFunctionNum(F), MMI);
40+
F, *TM, STI, MMI.getContext(), Context.generateMachineFunctionNum(F));
4141
MF->initTargetMachineFunctionInfo(STI);
4242

4343
// MRI callback for target specific initializations.

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ MachineModuleInfoImpl::~MachineModuleInfoImpl() = default;
2727
void MachineModuleInfo::initialize() {
2828
ObjFileMMI = nullptr;
2929
NextFnNum = 0;
30-
DbgInfoAvailable = false;
3130
}
3231

3332
void MachineModuleInfo::finalize() {
@@ -87,7 +86,7 @@ MachineFunction &MachineModuleInfo::getOrCreateMachineFunction(Function &F) {
8786
if (I.second) {
8887
// No pre-existing machine function, create a new one.
8988
const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
90-
MF = new MachineFunction(F, TM, STI, NextFnNum++, *this);
89+
MF = new MachineFunction(F, TM, STI, getContext(), NextFnNum++);
9190
MF->initTargetMachineFunctionInfo(STI);
9291

9392
// MRI callback for target specific initializations.
@@ -207,7 +206,6 @@ bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
207206
Ctx.diagnose(
208207
DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
209208
});
210-
MMI.DbgInfoAvailable = !M.debug_compile_units().empty();
211209
return false;
212210
}
213211

@@ -232,6 +230,5 @@ MachineModuleAnalysis::run(Module &M, ModuleAnalysisManager &) {
232230
Ctx.diagnose(
233231
DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
234232
});
235-
MMI.DbgInfoAvailable = !M.debug_compile_units().empty();
236233
return Result(MMI);
237234
}

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,6 @@ void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O,
928928
}
929929

930930
bool NVPTXAsmPrinter::doFinalization(Module &M) {
931-
bool HasDebugInfo = MMI && MMI->hasDebugInfo();
932-
933931
// If we did not emit any functions, then the global declarations have not
934932
// yet been emitted.
935933
if (!GlobalsEmitted) {
@@ -945,7 +943,7 @@ bool NVPTXAsmPrinter::doFinalization(Module &M) {
945943
auto *TS =
946944
static_cast<NVPTXTargetStreamer *>(OutStreamer->getTargetStreamer());
947945
// Close the last emitted section
948-
if (HasDebugInfo) {
946+
if (hasDebugInfo()) {
949947
TS->closeLastSection();
950948
// Emit empty .debug_loc section for better support of the empty files.
951949
OutStreamer->emitRawText("\t.section\t.debug_loc\t{\t}");

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,11 +3137,11 @@ void PPCAIXAsmPrinter::emitInstruction(const MachineInstr *MI) {
31373137
break;
31383138
MCSymbol *TempSym = OutContext.createNamedTempSymbol();
31393139
OutStreamer->emitLabel(TempSym);
3140-
OutStreamer->emitXCOFFExceptDirective(CurrentFnSym, TempSym,
3141-
LangMO.getImm(), ReasonMO.getImm(),
3142-
Subtarget->isPPC64() ? MI->getMF()->getInstructionCount() * 8 :
3143-
MI->getMF()->getInstructionCount() * 4,
3144-
MMI->hasDebugInfo());
3140+
OutStreamer->emitXCOFFExceptDirective(
3141+
CurrentFnSym, TempSym, LangMO.getImm(), ReasonMO.getImm(),
3142+
Subtarget->isPPC64() ? MI->getMF()->getInstructionCount() * 8
3143+
: MI->getMF()->getInstructionCount() * 4,
3144+
hasDebugInfo());
31453145
break;
31463146
}
31473147
case PPC::GETtlsMOD32AIX:
@@ -3199,7 +3199,7 @@ void PPCAIXAsmPrinter::emitInstruction(const MachineInstr *MI) {
31993199

32003200
bool PPCAIXAsmPrinter::doFinalization(Module &M) {
32013201
// Do streamer related finalization for DWARF.
3202-
if (!MAI->usesDwarfFileAndLocDirectives() && MMI->hasDebugInfo())
3202+
if (!MAI->usesDwarfFileAndLocDirectives() && hasDebugInfo())
32033203
OutStreamer->doFinalizationAtSectionEnd(
32043204
OutStreamer->getContext().getObjectFileInfo()->getTextSection());
32053205

llvm/tools/llvm-reduce/ReducerWorkItem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ static std::unique_ptr<MachineFunction> cloneMF(MachineFunction *SrcMF,
232232
MachineModuleInfo &DestMMI) {
233233
auto DstMF = std::make_unique<MachineFunction>(
234234
SrcMF->getFunction(), SrcMF->getTarget(), SrcMF->getSubtarget(),
235-
SrcMF->getFunctionNumber(), DestMMI);
235+
SrcMF->getContext(), SrcMF->getFunctionNumber());
236236
DenseMap<MachineBasicBlock *, MachineBasicBlock *> Src2DstMBB;
237237

238238
auto *SrcMRI = &SrcMF->getRegInfo();

llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class AArch64SelectionDAGTest : public testing::Test {
6161

6262
MachineModuleInfo MMI(TM.get());
6363

64-
MF = std::make_unique<MachineFunction>(*F, *TM, *TM->getSubtargetImpl(*F), 0,
65-
MMI);
64+
MF = std::make_unique<MachineFunction>(*F, *TM, *TM->getSubtargetImpl(*F),
65+
MMI.getContext(), 0);
6666

6767
DAG = std::make_unique<SelectionDAG>(*TM, CodeGenOptLevel::None);
6868
if (!DAG)

llvm/unittests/CodeGen/InstrRefLDVTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class InstrRefLDVTest : public testing::Test {
9494
const TargetSubtargetInfo &STI = *Machine->getSubtargetImpl(*F);
9595

9696
MF = std::make_unique<MachineFunction>(*F, (LLVMTargetMachine &)*Machine,
97-
STI, FunctionNum, *MMI);
97+
STI, MMI->getContext(), FunctionNum);
9898

9999
// Create metadata: CU, subprogram, some blocks and an inline function
100100
// scope.

llvm/unittests/CodeGen/MFCommon.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ std::unique_ptr<MachineFunction> createMachineFunction(LLVMContext &Ctx,
135135
MachineModuleInfo MMI(TM);
136136
const TargetSubtargetInfo &STI = *TM->getSubtargetImpl(*F);
137137

138-
return std::make_unique<MachineFunction>(*F, *TM, STI, FunctionNum, MMI);
138+
return std::make_unique<MachineFunction>(*F, *TM, STI, MMI.getContext(),
139+
FunctionNum);
139140
}
140141

llvm/unittests/CodeGen/SelectionDAGAddressAnalysisTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class SelectionDAGAddressAnalysisTest : public testing::Test {
7272
MachineModuleInfo MMI(TM.get());
7373

7474
MF = std::make_unique<MachineFunction>(*F, *TM, *TM->getSubtargetImpl(*F),
75-
0, MMI);
75+
MMI.getContext(), 0);
7676

7777
DAG = std::make_unique<SelectionDAG>(*TM, CodeGenOptLevel::None);
7878
if (!DAG)

llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class SelectionDAGPatternMatchTest : public testing::Test {
7171
MachineModuleInfo MMI(TM.get());
7272

7373
MF = std::make_unique<MachineFunction>(*F, *TM, *TM->getSubtargetImpl(*F),
74-
0, MMI);
74+
MMI.getContext(), 0);
7575

7676
DAG = std::make_unique<SelectionDAG>(*TM, CodeGenOptLevel::None);
7777
if (!DAG)

0 commit comments

Comments
 (0)