[llvm] CodeGen: Prefer getting the Triple from the Module when convenient (PR #228429)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 06:00:51 PDT 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/228429

Take the triple from the contextual module rather than TargetMachine
when it's already readily available.

>From d5d0559639ad5a2798d69842bc86de1e724d5000 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Fri, 2 Oct 2026 14:55:02 +0200
Subject: [PATCH] CodeGen: Prefer getting the Triple from the Module when
 convenient

Take the triple from the contextual module rather than TargetMachine
when it's already readily available.
---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 33 +++++++++++-----------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index d5b8cefe918faf..e7aedcf31abc93 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -523,7 +523,7 @@ bool AsmPrinter::doInitialization(Module &M) {
   HasSplitStack = false;
   HasNoSplitStack = false;
   DbgInfoAvailable = !M.debug_compile_units().empty();
-  const Triple &Target = TM.getTargetTriple();
+  const Triple &Target = M.getTargetTriple();
 
   AddrLabelSymbols = nullptr;
 
@@ -773,8 +773,8 @@ MCSymbol *AsmPrinter::getSymbolPreferLocal(const GlobalValue &GV) const {
   // assembler would otherwise be conservative and assume a global default
   // visibility symbol can be interposable, even if the code generator already
   // assumed it.
-  if (TM.getTargetTriple().isOSBinFormatELF() && GV.canBenefitFromLocalAlias()) {
-    const Module &M = *GV.getParent();
+  const Module &M = *GV.getParent();
+  if (M.getTargetTriple().isOSBinFormatELF() && GV.canBenefitFromLocalAlias()) {
     if (TM.getRelocationModel() != Reloc::Static &&
         M.getPIELevel() == PIELevel::Default && GV.isDSOLocal())
       return getSymbolWithGlobalValueBase(&GV, "$local");
@@ -2098,8 +2098,9 @@ void AsmPrinter::emitFunctionBody() {
   bool HasAnyRealCode = false;
   int NumInstsInFunction = 0;
   // Only x86 needs this padding; the Arm unwinders back the PC up themselves.
-  bool NeedsEHaNops = MMI->getModule()->getModuleFlag("eh-asynch") &&
-                      TM.getTargetTriple().isX86();
+  const Module *M = MMI->getModule();
+  bool NeedsEHaNops =
+      M->getTargetTriple().isX86() && M->getModuleFlag("eh-asynch");
 
   const MCSubtargetInfo *STI = nullptr;
   if (this->MF)
@@ -2347,7 +2348,7 @@ void AsmPrinter::emitFunctionBody() {
       // If there is a post-instruction symbol, emit a label for it here.
       if (MCSymbol *S = MI.getPostInstrSymbol()) {
         // Emit the weak symbol attribute used for the prefetch target fallback.
-        if (TM.getTargetTriple().isOSBinFormatELF()) {
+        if (M->getTargetTriple().isOSBinFormatELF()) {
           MCSymbolELF *ESym = static_cast<MCSymbolELF *>(S);
           if (ESym->getBinding() == ELF::STB_WEAK)
             OutStreamer->emitSymbolAttribute(S, MCSA_Weak);
@@ -2456,7 +2457,7 @@ void AsmPrinter::emitFunctionBody() {
   // after linking, causing the kernel not to load the binary:
   // https://developercommunity.visualstudio.com/content/problem/45366/vc-linker-creates-invalid-dll-with-clang-cl.html
   // FIXME: Hide this behind some API in e.g. MCAsmInfo or MCTargetStreamer.
-  const Triple &TT = TM.getTargetTriple();
+  const Triple &TT = M->getTargetTriple();
   if (!HasAnyRealCode && (MAI.hasSubsectionsViaSymbols() ||
                           (TT.isOSWindows() && TT.isOSBinFormatCOFF()))) {
     MCInst Noop = MF->getSubtarget().getInstrInfo()->getNop();
@@ -2694,7 +2695,7 @@ void AsmPrinter::emitGlobalAlias(const Module &M, const GlobalAlias &GA) {
   // so AIX has to use the extra-label-at-definition strategy. At this
   // point, all the extra label is emitted, we just have to emit linkage for
   // those labels.
-  if (TM.getTargetTriple().isOSBinFormatXCOFF()) {
+  if (M.getTargetTriple().isOSBinFormatXCOFF()) {
     // Linkage for alias of global variable has been emitted.
     if (isa_and_nonnull<GlobalVariable>(BaseObject))
       return;
@@ -2719,7 +2720,7 @@ void AsmPrinter::emitGlobalAlias(const Module &M, const GlobalAlias &GA) {
   // This affects codegen when the aliasee is not a function.
   if (IsFunction) {
     OutStreamer->emitSymbolAttribute(Name, MCSA_ELF_TypeFunction);
-    if (TM.getTargetTriple().isOSBinFormatCOFF()) {
+    if (M.getTargetTriple().isOSBinFormatCOFF()) {
       OutStreamer->beginCOFFSymbolDef(Name);
       OutStreamer->emitCOFFSymbolStorageClass(
           GA.hasLocalLinkage() ? COFF::IMAGE_SYM_CLASS_STATIC
@@ -2766,7 +2767,7 @@ void AsmPrinter::emitGlobalIFunc(Module &M, const GlobalIFunc &GI) {
       assert(GI.hasLocalLinkage() && "Invalid ifunc linkage");
   };
 
-  if (TM.getTargetTriple().isOSBinFormatELF()) {
+  if (M.getTargetTriple().isOSBinFormatELF()) {
     MCSymbol *Name = getSymbol(&GI);
     EmitLinkage(Name);
     OutStreamer->emitSymbolAttribute(Name, MCSA_ELF_TypeIndFunction);
@@ -2782,7 +2783,7 @@ void AsmPrinter::emitGlobalIFunc(Module &M, const GlobalIFunc &GI) {
     return;
   }
 
-  if (!TM.getTargetTriple().isOSBinFormatMachO() || !getIFuncMCSubtargetInfo())
+  if (!M.getTargetTriple().isOSBinFormatMachO() || !getIFuncMCSubtargetInfo())
     reportFatalUsageError("IFuncs are not supported on this platform");
 
   // On Darwin platforms, emit a manually-constructed .symbol_resolver that
@@ -2948,7 +2949,7 @@ bool AsmPrinter::doFinalization(Module &M) {
   // accesses to MF specific features at the module level and so that
   // we can conditionalize accesses based on whether or not it is nullptr.
   MF = nullptr;
-  const Triple &Target = TM.getTargetTriple();
+  const Triple &Target = M.getTargetTriple();
 
   std::vector<GlobalVariable *> GlobalsToTag;
   for (GlobalVariable &G : M.globals()) {
@@ -3101,7 +3102,7 @@ bool AsmPrinter::doFinalization(Module &M) {
   // sections after DWARF.
   for (const auto &IFunc : M.ifuncs())
     emitGlobalIFunc(M, IFunc);
-  if (TM.getTargetTriple().isOSBinFormatXCOFF() && hasDebugInfo()) {
+  if (M.getTargetTriple().isOSBinFormatXCOFF() && hasDebugInfo()) {
     // Emit section end. This is used to tell the debug line section where the
     // end is for a text section if we don't use .loc to represent the debug
     // line.
@@ -3162,7 +3163,7 @@ bool AsmPrinter::doFinalization(Module &M) {
 
   // Emit .note.GNU-split-stack and .note.GNU-no-split-stack sections if
   // split-stack is used.
-  if (TM.getTargetTriple().isOSBinFormatELF() && HasSplitStack) {
+  if (M.getTargetTriple().isOSBinFormatELF() && HasSplitStack) {
     OutStreamer->switchSection(OutContext.getELFSection(".note.GNU-split-stack",
                                                         ELF::SHT_PROGBITS, 0));
     if (HasNoSplitStack)
@@ -5254,7 +5255,7 @@ AsmPrinter::getCodeViewJumpTableInfo(int JTI, const MachineInstr *BranchInstr,
 }
 
 void AsmPrinter::emitCOFFReplaceableFunctionData(Module &M) {
-  const Triple &TT = TM.getTargetTriple();
+  const Triple &TT = M.getTargetTriple();
   assert(TT.isOSBinFormatCOFF());
 
   bool IsTargetArm64EC = TT.isWindowsArm64EC();
@@ -5319,7 +5320,7 @@ void AsmPrinter::emitCOFFReplaceableFunctionData(Module &M) {
 }
 
 void AsmPrinter::emitCOFFFeatureSymbol(Module &M) {
-  const Triple &TT = TM.getTargetTriple();
+  const Triple &TT = M.getTargetTriple();
   assert(TT.isOSBinFormatCOFF());
 
   // Emit an absolute @feat.00 symbol.



More information about the llvm-commits mailing list