[llvm-branch-commits] [llvm] LoongArch: Respect the target-abi module flag in the ELF header (PR #226498)

Matt Arsenault via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Sep 25 06:47:21 PDT 2026


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

The target streamer read the ABI from MCTargetOptions when constructed,
so the e_flags recorded the command line -target-abi and ignored the
module flag.

Co-Authored-By: Claude Opus 5 <noreply at anthropic.com>

>From 1ae3b7170edd53d844fc83ddb545dc3ff6a0c30d Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Fri, 25 Sep 2026 15:37:51 +0200
Subject: [PATCH] LoongArch: Respect the target-abi module flag in the ELF
 header

The target streamer read the ABI from MCTargetOptions when constructed,
so the e_flags recorded the command line -target-abi and ignored the
module flag.

Co-Authored-By: Claude Opus 5 <noreply at anthropic.com>
---
 llvm/lib/Target/LoongArch/LoongArchAsmPrinter.cpp | 10 ++++++++++
 llvm/lib/Target/LoongArch/LoongArchAsmPrinter.h   |  2 ++
 llvm/test/CodeGen/LoongArch/module-target-abi.ll  | 13 +++++++++++++
 3 files changed, 25 insertions(+)
 create mode 100644 llvm/test/CodeGen/LoongArch/module-target-abi.ll

diff --git a/llvm/lib/Target/LoongArch/LoongArchAsmPrinter.cpp b/llvm/lib/Target/LoongArch/LoongArchAsmPrinter.cpp
index 52afb2a8af1997..38ee358a4de6c5 100644
--- a/llvm/lib/Target/LoongArch/LoongArchAsmPrinter.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchAsmPrinter.cpp
@@ -21,6 +21,7 @@
 #include "llvm/CodeGen/AsmPrinter.h"
 #include "llvm/CodeGen/MachineJumpTableInfo.h"
 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
+#include "llvm/IR/Module.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCInstBuilder.h"
@@ -47,6 +48,15 @@ LoongArchTargetStreamer &LoongArchAsmPrinter::getTargetStreamer() const {
       *OutStreamer->getTargetStreamer());
 }
 
+void LoongArchAsmPrinter::emitStartOfAsmFile(Module &M) {
+  StringRef ABIName = M.getTargetABIFromMD();
+  if (!ABIName.empty()) {
+    getTargetStreamer().setTargetABI(LoongArchABI::computeTargetABI(
+        TM.getTargetTriple(), TM.getMCSubtargetInfo().getFeatureBits(),
+        ABIName));
+  }
+}
+
 void LoongArchAsmPrinter::emitInstruction(const MachineInstr *MI) {
   LoongArch_MC::verifyInstructionPredicates(
       MI->getOpcode(), getSubtargetInfo().getFeatureBits());
diff --git a/llvm/lib/Target/LoongArch/LoongArchAsmPrinter.h b/llvm/lib/Target/LoongArch/LoongArchAsmPrinter.h
index bc8145386474d0..09d8a36b86d9cf 100644
--- a/llvm/lib/Target/LoongArch/LoongArchAsmPrinter.h
+++ b/llvm/lib/Target/LoongArch/LoongArchAsmPrinter.h
@@ -43,6 +43,8 @@ class LLVM_LIBRARY_VISIBILITY LoongArchAsmPrinter : public AsmPrinter {
 
   bool runOnMachineFunction(MachineFunction &MF) override;
 
+  void emitStartOfAsmFile(Module &M) override;
+
   void emitInstruction(const MachineInstr *MI) override;
 
   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
diff --git a/llvm/test/CodeGen/LoongArch/module-target-abi.ll b/llvm/test/CodeGen/LoongArch/module-target-abi.ll
new file mode 100644
index 00000000000000..306ca8b650a231
--- /dev/null
+++ b/llvm/test/CodeGen/LoongArch/module-target-abi.ll
@@ -0,0 +1,13 @@
+; The "target-abi" module flag selects the ABI used for codegen, and must also
+; drive the ABI recorded in the ELF header e_flags.
+; RUN: llc --mtriple=loongarch64 --mattr=+d --filetype=obj < %s -o %t.o
+; RUN: llvm-readobj -h %t.o | FileCheck %s
+
+; CHECK: EF_LOONGARCH_ABI_SOFT_FLOAT
+
+define void @nothing() nounwind {
+  ret void
+}
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"target-abi", !"lp64s"}



More information about the llvm-branch-commits mailing list