[llvm] CodeGen: Initialize TargetLoweringObjectFile from MachineModuleInfo (PR #226836)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 27 14:07:35 PDT 2026
https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/226836
MachineModuleInfo passes TLOF to the MCContext the TargetLoweringObjectFile
but nothing initialized it until the AsmPrinter pass ran, so every codegen pass
in between saw it uninitialized. Initialize it from MachineModuleInfo, and drop
the calls llc and SPIRVTranslate used to work around this.
SPIRVTranslate's MachineModuleInfoWrapperPass was never passed on to
addPassesToEmitFile, so it was initializing a throwaway context.
Co-Authored-By: Claude Opus 5 <noreply at anthropic.com>
>From cdcccdc22f75957e8d788a00111df37cddfd20cf Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sun, 27 Sep 2026 10:14:34 +0200
Subject: [PATCH] CodeGen: Initialize TargetLoweringObjectFile from
MachineModuleInfo
MachineModuleInfo passes TLOF to the MCContext the TargetLoweringObjectFile
but nothing initialized it until the AsmPrinter pass ran, so every codegen pass
in between saw it uninitialized. Initialize it from MachineModuleInfo, and drop
the calls llc and SPIRVTranslate used to work around this.
SPIRVTranslate's MachineModuleInfoWrapperPass was never passed on to
addPassesToEmitFile, so it was initializing a throwaway context.
Co-Authored-By: Claude Opus 5 <noreply at anthropic.com>
---
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 3 ---
llvm/lib/CodeGen/MachineModuleInfo.cpp | 4 ++++
llvm/lib/Target/SPIRV/SPIRVAPI.cpp | 6 ------
llvm/tools/llc/lib/NewPMDriver.cpp | 3 ---
llvm/tools/llc/lib/llcdriver.cpp | 3 ---
5 files changed, 4 insertions(+), 15 deletions(-)
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 8c30f91ac0855..bd929901612f9 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -531,9 +531,6 @@ bool AsmPrinter::doInitialization(Module &M) {
AddrLabelSymbols = nullptr;
- // Initialize TargetLoweringObjectFile.
- TM.getObjFileLowering()->Initialize(OutContext, TM);
-
TM.getObjFileLowering()->getModuleMetadata(M);
// On AIX, we delay emitting any section information until
diff --git a/llvm/lib/CodeGen/MachineModuleInfo.cpp b/llvm/lib/CodeGen/MachineModuleInfo.cpp
index dd9defdec276b..cc7958e59f680 100644
--- a/llvm/lib/CodeGen/MachineModuleInfo.cpp
+++ b/llvm/lib/CodeGen/MachineModuleInfo.cpp
@@ -219,6 +219,8 @@ bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
});
MMI.getTarget().verifyOptionsConsistency(M);
+ MMI.getTarget().getObjFileLowering()->Initialize(MMI.getContext(),
+ MMI.getTarget());
return false;
}
@@ -244,5 +246,7 @@ MachineModuleAnalysis::run(Module &M, ModuleAnalysisManager &) {
DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
});
MMI.getTarget().verifyOptionsConsistency(M);
+ MMI.getTarget().getObjFileLowering()->Initialize(MMI.getContext(),
+ MMI.getTarget());
return Result(MMI);
}
diff --git a/llvm/lib/Target/SPIRV/SPIRVAPI.cpp b/llvm/lib/Target/SPIRV/SPIRVAPI.cpp
index 17def333a065f..33516582500a9 100644
--- a/llvm/lib/Target/SPIRV/SPIRVAPI.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVAPI.cpp
@@ -11,7 +11,6 @@
#include "SPIRVTargetMachine.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/CodeGen/CommandFlags.h"
-#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/DataLayout.h"
@@ -22,7 +21,6 @@
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Pass.h"
#include "llvm/Support/TargetSelect.h"
-#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/SubtargetFeature.h"
#include "llvm/TargetParser/Triple.h"
@@ -113,10 +111,6 @@ SPIRVTranslate(Module *M, std::string &SpirvObj, std::string &ErrMsg,
TargetLibraryInfoImpl TLII(M->getTargetTriple());
legacy::PassManager PM;
PM.add(new TargetLibraryInfoWrapperPass(TLII));
- std::unique_ptr<MachineModuleInfoWrapperPass> MMIWP(
- new MachineModuleInfoWrapperPass(Target.get()));
- Target->getObjFileLowering()->Initialize(MMIWP->getMMI().getContext(),
- *Target);
SmallString<4096> OutBuffer;
raw_svector_ostream OutStream(OutBuffer);
diff --git a/llvm/tools/llc/lib/NewPMDriver.cpp b/llvm/tools/llc/lib/NewPMDriver.cpp
index 3eac9bd15355e..50d08a76f0078 100644
--- a/llvm/tools/llc/lib/NewPMDriver.cpp
+++ b/llvm/tools/llc/lib/NewPMDriver.cpp
@@ -44,7 +44,6 @@
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Target/CGPassBuilderOption.h"
-#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/Scalar/LoopPassManager.h"
@@ -120,8 +119,6 @@ int llvm::compileModuleWithNewPM(
MachineModuleInfo MMI(Target.get());
- Target->getObjFileLowering()->Initialize(MMI.getContext(), *Target);
-
PassInstrumentationCallbacks PIC;
StandardInstrumentations SI(Context, Opt.DebugPM,
VK == VerifierKind::EachPass);
diff --git a/llvm/tools/llc/lib/llcdriver.cpp b/llvm/tools/llc/lib/llcdriver.cpp
index 7f0ee013cd947..cbb96f0b6b065 100644
--- a/llvm/tools/llc/lib/llcdriver.cpp
+++ b/llvm/tools/llc/lib/llcdriver.cpp
@@ -53,7 +53,6 @@
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/WithColor.h"
-#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/TargetParser/SubtargetFeature.h"
@@ -847,8 +846,6 @@ static int compileModule(char **argv, SmallVectorImpl<PassPlugin> &PluginList,
}
}
- Target->getObjFileLowering()->Initialize(MMIWP->getMMI().getContext(),
- *Target);
if (MIR) {
assert(MMIWP && "Forgot to create MMIWP?");
if (MIR->parseMachineFunctions(*M, MMIWP->getMMI()))
More information about the llvm-commits
mailing list