[llvm] [MC] Move MCTargetOptions ownership from MCContext to MCAsmInfo (PR #180449)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 8 17:38:31 PST 2026
https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/180449
>From f133ff9490ea91989051fc189cacce0181ba243f Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 8 Feb 2026 17:04:30 -0800
Subject: [PATCH 1/2] [MC] Move MCTargetOptions ownership from MCContext to
MCAsmInfo
Except MC-internal `MCAsmInfo()` uses, MCAsmInfo is always constructed
with `const MCTargetOptions &` via `TargetRegistry::createMCAsmInfo`
(https://reviews.llvm.org/D41349). Store the pointer in MCAsmInfo and
change `MCContext::getTargetOptions()` to retrieve it from there,
removing the `MCTargetOptions const *TargetOptions` member from
MCContext.
MCContext's constructor still accepts an MCTargetOptions parameter
for now. An assert verifies it matches MCAsmInfo's copy when provided.
A subsequent change will remove this parameter and update all callers.
In addition, assert that MCTargetOptions is non-null in the MCAssembler
constructor and remove the now-unnecessary null checks in
ELFObjectWriter.
---
llvm/include/llvm/MC/MCAsmInfo.h | 5 +++++
llvm/include/llvm/MC/MCContext.h | 4 +---
llvm/include/llvm/MC/TargetRegistry.h | 6 +++++-
llvm/lib/MC/ELFObjectWriter.cpp | 7 +++----
llvm/lib/MC/MCContext.cpp | 26 +++++++++++++++++---------
5 files changed, 31 insertions(+), 17 deletions(-)
diff --git a/llvm/include/llvm/MC/MCAsmInfo.h b/llvm/include/llvm/MC/MCAsmInfo.h
index c61a6cb5bb0ab..0897f00f23c8c 100644
--- a/llvm/include/llvm/MC/MCAsmInfo.h
+++ b/llvm/include/llvm/MC/MCAsmInfo.h
@@ -434,6 +434,8 @@ class LLVM_ABI MCAsmInfo {
llvm::StringMap<uint32_t> NameToAtSpecifier;
void initializeAtSpecifiers(ArrayRef<AtSpecifier>);
+ const MCTargetOptions *TargetOptions = nullptr;
+
public:
explicit MCAsmInfo();
virtual ~MCAsmInfo();
@@ -442,6 +444,9 @@ class LLVM_ABI MCAsmInfo {
MCAsmInfo(MCAsmInfo const &) = delete;
MCAsmInfo &operator=(MCAsmInfo const &) = delete;
+ const MCTargetOptions *getTargetOptions() const { return TargetOptions; }
+ void setTargetOptions(const MCTargetOptions &TO) { TargetOptions = &TO; }
+
/// Get the code pointer size in bytes.
unsigned getCodePointerSize() const { return CodePointerSize; }
diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h
index c5de76bd30b3f..b4ae879ffdc07 100644
--- a/llvm/include/llvm/MC/MCContext.h
+++ b/llvm/include/llvm/MC/MCContext.h
@@ -326,8 +326,6 @@ class MCContext {
/// Do automatic reset in destructor
bool AutoReset;
- MCTargetOptions const *TargetOptions;
-
bool HadError = false;
void reportCommon(SMLoc Loc,
@@ -417,7 +415,7 @@ class MCContext {
const MCSubtargetInfo *getSubtargetInfo() const { return MSTI; }
- const MCTargetOptions *getTargetOptions() const { return TargetOptions; }
+ LLVM_ABI const MCTargetOptions *getTargetOptions() const;
LLVM_ABI CodeViewContext &getCVContext();
diff --git a/llvm/include/llvm/MC/TargetRegistry.h b/llvm/include/llvm/MC/TargetRegistry.h
index 234c587c8e951..422104eb6c1f9 100644
--- a/llvm/include/llvm/MC/TargetRegistry.h
+++ b/llvm/include/llvm/MC/TargetRegistry.h
@@ -21,6 +21,7 @@
#include "llvm-c/DisassemblerTypes.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
+#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Compiler.h"
@@ -409,7 +410,10 @@ class Target {
const MCTargetOptions &Options) const {
if (!MCAsmInfoCtorFn)
return nullptr;
- return MCAsmInfoCtorFn(MRI, TheTriple, Options);
+ auto *MAI = MCAsmInfoCtorFn(MRI, TheTriple, Options);
+ if (MAI)
+ MAI->setTargetOptions(Options);
+ return MAI;
}
/// Create a MCObjectFileInfo implementation for the specified target
diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index 3d7f503bf08b0..8ae3111a44516 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -673,7 +673,7 @@ MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
const StringRef SectionName = Sec.getName();
const MCTargetOptions *TO = Ctx.getTargetOptions();
- if (TO && TO->Crel) {
+ if (TO->Crel) {
MCSectionELF *RelaSection =
Ctx.createELFRelSection(".crel" + SectionName, ELF::SHT_CREL, Flags,
/*EntrySize=*/1, Sec.getGroup(), &Sec);
@@ -724,8 +724,7 @@ void ELFWriter::writeSectionData(MCSection &Sec) {
StringRef SectionName = Section.getName();
auto &Ctx = Asm.getContext();
const DebugCompressionType CompressionType =
- Ctx.getTargetOptions() ? Ctx.getTargetOptions()->CompressDebugSections
- : DebugCompressionType::None;
+ Ctx.getTargetOptions()->CompressDebugSections;
if (CompressionType == DebugCompressionType::None ||
!SectionName.starts_with(".debug_")) {
Asm.writeSectionData(W.OS, &Section);
@@ -1378,7 +1377,7 @@ bool ELFObjectWriter::usesRela(const MCTargetOptions *TO,
const MCSectionELF &Sec) const {
return (hasRelocationAddend() &&
Sec.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE) ||
- (TO && TO->Crel);
+ TO->Crel;
}
bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 5df8692d2e7ba..7334229ba1403 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -71,11 +71,15 @@ MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai,
MAI(mai), MRI(mri), MSTI(msti), Symbols(Allocator),
InlineAsmUsedLabelNames(Allocator),
CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
- AutoReset(DoAutoReset), TargetOptions(TargetOpts) {
- SaveTempLabels = TargetOptions && TargetOptions->MCSaveTempLabels;
+ AutoReset(DoAutoReset) {
+ assert(MAI && MAI->getTargetOptions() &&
+ "MCAsmInfo and MCTargetOptions must be available");
+ assert((!TargetOpts || TargetOpts == MAI->getTargetOptions()) &&
+ "MCTargetOptions, if specified, must match MCAsmInfo");
+ SaveTempLabels = getTargetOptions() && getTargetOptions()->MCSaveTempLabels;
if (SaveTempLabels)
setUseNamesOnTempLabels(true);
- SecureLogFile = TargetOptions ? TargetOptions->AsSecureLogFile : "";
+ SecureLogFile = getTargetOptions() ? getTargetOptions()->AsSecureLogFile : "";
if (SrcMgr && SrcMgr->getNumBuffers())
MainFileName = std::string(SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())
@@ -117,6 +121,10 @@ MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai,
}
}
+const MCTargetOptions *MCContext::getTargetOptions() const {
+ return MAI->getTargetOptions();
+}
+
MCContext::~MCContext() {
if (AutoReset)
reset();
@@ -984,14 +992,14 @@ void MCContext::RemapDebugPaths() {
//===----------------------------------------------------------------------===//
EmitDwarfUnwindType MCContext::emitDwarfUnwindInfo() const {
- if (!TargetOptions)
+ if (!getTargetOptions())
return EmitDwarfUnwindType::Default;
- return TargetOptions->EmitDwarfUnwind;
+ return getTargetOptions()->EmitDwarfUnwind;
}
bool MCContext::emitCompactUnwindNonCanonical() const {
- if (TargetOptions)
- return TargetOptions->EmitCompactUnwindNonCanonical;
+ if (getTargetOptions())
+ return getTargetOptions()->EmitCompactUnwindNonCanonical;
return false;
}
@@ -1127,9 +1135,9 @@ void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
}
void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) {
- if (TargetOptions && TargetOptions->MCNoWarn)
+ if (getTargetOptions() && getTargetOptions()->MCNoWarn)
return;
- if (TargetOptions && TargetOptions->MCFatalWarnings) {
+ if (getTargetOptions() && getTargetOptions()->MCFatalWarnings) {
reportError(Loc, Msg);
} else {
reportCommon(Loc, [&](SMDiagnostic &D, const SourceMgr *SMP) {
>From 6f153bb65422d94188d5e40a46756d7b69eb4073 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 8 Feb 2026 17:38:21 -0800
Subject: [PATCH 2/2] unneeded nullness check
---
llvm/lib/MC/MCAsmStreamer.cpp | 2 --
llvm/lib/MC/MCContext.cpp | 14 +++++---------
llvm/lib/MC/MCObjectStreamer.cpp | 8 +++-----
3 files changed, 8 insertions(+), 16 deletions(-)
diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index d9a22bc2716fd..e7100914af39e 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -100,8 +100,6 @@ class MCAsmStreamer final : public MCStreamer {
Context.setUseNamesOnTempLabels(true);
auto *TO = Context.getTargetOptions();
- if (!TO)
- return;
IsVerboseAsm = TO->AsmVerbose;
if (IsVerboseAsm)
InstPrinter->setCommentStream(CommentStream);
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 7334229ba1403..5849bb3c3e1b8 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -76,10 +76,10 @@ MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai,
"MCAsmInfo and MCTargetOptions must be available");
assert((!TargetOpts || TargetOpts == MAI->getTargetOptions()) &&
"MCTargetOptions, if specified, must match MCAsmInfo");
- SaveTempLabels = getTargetOptions() && getTargetOptions()->MCSaveTempLabels;
+ SaveTempLabels = getTargetOptions()->MCSaveTempLabels;
if (SaveTempLabels)
setUseNamesOnTempLabels(true);
- SecureLogFile = getTargetOptions() ? getTargetOptions()->AsSecureLogFile : "";
+ SecureLogFile = getTargetOptions()->AsSecureLogFile;
if (SrcMgr && SrcMgr->getNumBuffers())
MainFileName = std::string(SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())
@@ -992,15 +992,11 @@ void MCContext::RemapDebugPaths() {
//===----------------------------------------------------------------------===//
EmitDwarfUnwindType MCContext::emitDwarfUnwindInfo() const {
- if (!getTargetOptions())
- return EmitDwarfUnwindType::Default;
return getTargetOptions()->EmitDwarfUnwind;
}
bool MCContext::emitCompactUnwindNonCanonical() const {
- if (getTargetOptions())
- return getTargetOptions()->EmitCompactUnwindNonCanonical;
- return false;
+ return getTargetOptions()->EmitCompactUnwindNonCanonical;
}
void MCContext::setGenDwarfRootFile(StringRef InputFileName, StringRef Buffer) {
@@ -1135,9 +1131,9 @@ void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
}
void MCContext::reportWarning(SMLoc Loc, const Twine &Msg) {
- if (getTargetOptions() && getTargetOptions()->MCNoWarn)
+ if (getTargetOptions()->MCNoWarn)
return;
- if (getTargetOptions() && getTargetOptions()->MCFatalWarnings) {
+ if (getTargetOptions()->MCFatalWarnings) {
reportError(Loc, Msg);
} else {
reportCommon(Loc, [&](SMDiagnostic &D, const SourceMgr *SMP) {
diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 453d4d20d9727..6d46655d8c7fd 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -35,7 +35,7 @@ MCObjectStreamer::MCObjectStreamer(MCContext &Context,
assert(Assembler->getBackendPtr() && Assembler->getEmitterPtr());
IsObj = true;
setAllowAutoPadding(Assembler->getBackend().allowAutoPadding());
- if (Context.getTargetOptions() && Context.getTargetOptions()->MCRelaxAll)
+ if (Context.getTargetOptions()->MCRelaxAll)
Assembler->setRelaxAll(true);
}
@@ -170,8 +170,7 @@ void MCObjectStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
void MCObjectStreamer::reset() {
if (Assembler) {
Assembler->reset();
- if (getContext().getTargetOptions())
- Assembler->setRelaxAll(getContext().getTargetOptions()->MCRelaxAll);
+ Assembler->setRelaxAll(getContext().getTargetOptions()->MCRelaxAll);
}
EmitEHFrame = true;
EmitDebugFrame = false;
@@ -197,8 +196,7 @@ void MCObjectStreamer::emitFrames() {
if (EmitDebugFrame)
MCDwarfFrameEmitter::emit(*this, false);
- if (EmitSFrame || (getContext().getTargetOptions() &&
- getContext().getTargetOptions()->EmitSFrameUnwind))
+ if (EmitSFrame || getContext().getTargetOptions()->EmitSFrameUnwind)
MCSFrameEmitter::emit(*this);
}
More information about the llvm-commits
mailing list