[llvm] [Mips] Derive module ABI flags from a function definition (PR #220672)

Ying Huang via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 02:45:40 PDT 2026


================
@@ -730,14 +730,33 @@ void MipsAsmPrinter::emitStartOfAsmFile(Module &M) {
     // around it by re-initializing the PIC state here.
     TS.setPic(OutContext.getObjectFileInfo()->isPositionIndependent());
 
-    // Try to get target-features from the first function.
+    // Try to get target-features from the first function definition. A module
+    // can start with declarations, which carry no target attributes, so
+    // reading them off M.begin() unconditionally selects the default subtarget
+    // for such a module. That is most visible under LTO, where the target
+    // machine has no feature string of its own and the layout of the merged
+    // module decides which ABI flags are emitted. Fall back to the first
+    // function if the module has no definitions at all.
+    const Function *F = M.empty() ? nullptr : &*M.begin();
+    for (const Function &Fn : M) {
+      if (!Fn.isDeclaration()) {
+        F = &Fn;
+        break;
+      }
+    }
+
+    // A module with no functions at all tells us nothing about the ABI it was
+    // built for. Emitting the default subtarget's FP ABI would be a guess, and
+    // a linker has to reject it when the rest of the link disagrees, so leave
+    // the FP ABI unset for those. LTO produces such modules routinely.
+    const bool HaveFpABI = !M.empty() || !TM.getTargetFeatureString().empty();
+
     StringRef FS = TM.getTargetFeatureString();
-    Module::iterator F = M.begin();
-    if (FS.empty() && M.size() && F->hasFnAttribute("target-features"))
+    if (FS.empty() && F && F->hasFnAttribute("target-features"))
       FS = F->getFnAttribute("target-features").getValueAsString();
 
     std::string strFS = FS.str();
-    if (M.size() && F->getFnAttribute("use-soft-float").getValueAsBool())
+    if (F && F->getFnAttribute("use-soft-float").getValueAsBool())
----------------
yingopq wrote:

M.size() and F have the same effect?

https://github.com/llvm/llvm-project/pull/220672


More information about the llvm-commits mailing list