[llvm] CodeGen: Synthesize "float-abi" module flag from -float-abi (PR #215795)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 10:50:07 PDT 2026


https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/215795

>From b85730b4c00a22b6d9535bc91f658d81cd905465 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Mon, 20 Jul 2026 19:28:58 +0200
Subject: [PATCH 1/3] CodeGen: Synthesize "float-abi" module flag from
 -float-abi

Avoid annoying test updates when the corresponding TargetOptions
field is removed. Make the -float-abi llc/opt option a lit test
convenience that records the floating-point ABI in the IR,
mirroring how -mcpu/-mattr are recorded as function attributes.

Co-authored-by: Claude (Claude-Opus-4.8) <noreply at anthropic.com>
---
 llvm/lib/CodeGen/CommandFlags.cpp             | 12 ++++++-
 .../CodeGen/ARM/float-abi-synthesize-flag.ll  | 32 +++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/ARM/float-abi-synthesize-flag.ll

diff --git a/llvm/lib/CodeGen/CommandFlags.cpp b/llvm/lib/CodeGen/CommandFlags.cpp
index 306c0ff7c704b..88987d0bc5558 100644
--- a/llvm/lib/CodeGen/CommandFlags.cpp
+++ b/llvm/lib/CodeGen/CommandFlags.cpp
@@ -270,7 +270,9 @@ codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
   CGBINDOPT(EnableHonorSignDependentRoundingFPMath);
 
   static cl::opt<FloatABI::ABIType> FloatABIForCalls(
-      "float-abi", cl::desc("Choose float ABI type"),
+      "float-abi",
+      cl::desc(
+          "Choose float ABI type (writes the \"float-abi\" IR module flag)"),
       cl::init(FloatABI::Default),
       cl::values(clEnumValN(FloatABI::Default, "default",
                             "Target default float ABI type"),
@@ -774,6 +776,14 @@ void codegen::setFunctionAttributes(Function &F, StringRef CPU,
 
 void codegen::setFunctionAttributes(Module &M, StringRef CPU,
                                     StringRef Features, StringRef TuneCPU) {
+  // Synthesize the "float-abi" module flag from the -float-abi option,
+  FloatABI::ABIType ABI = getFloatABIForCalls();
+  if (ABI != FloatABI::Default && !M.getModuleFlag("float-abi")) {
+    M.addModuleFlag(
+        Module::Error, "float-abi",
+        MDString::get(M.getContext(), FloatABI::getABITypeName(ABI)));
+  }
+
   for (Function &F : M)
     setFunctionAttributes(F, CPU, Features, TuneCPU);
 }
diff --git a/llvm/test/CodeGen/ARM/float-abi-synthesize-flag.ll b/llvm/test/CodeGen/ARM/float-abi-synthesize-flag.ll
new file mode 100644
index 0000000000000..ce4d47d18010f
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/float-abi-synthesize-flag.ll
@@ -0,0 +1,32 @@
+; Check behavior of the -float-abi command-line option; it should
+; synthesize the "float-abi" module flag, unless one is already
+; present
+
+; RUN: split-file %s %t
+
+; -float-abi=hard writes the module flag.
+; RUN: llc -mtriple=armv7-none-eabi -float-abi=hard -stop-after=finalize-isel %t/none.ll -o - | FileCheck %s --check-prefix=HARD
+
+; -float-abi=soft writes the module flag.
+; RUN: llc -mtriple=armv7-none-eabi -float-abi=soft -stop-after=finalize-isel %t/none.ll -o - | FileCheck %s --check-prefix=SOFT
+
+; Without -float-abi, no flag is synthesized.
+; RUN: llc -mtriple=armv7-none-eabi -stop-after=finalize-isel %t/none.ll -o - | FileCheck %s --check-prefix=NONE
+
+; An explicit in-IR flag is not overridden by -float-abi.
+; RUN: llc -mtriple=armv7-none-eabi -float-abi=soft -stop-after=finalize-isel %t/hard.ll -o - | FileCheck %s --check-prefix=HARD
+
+;--- none.ll
+define void @f() {
+  ret void
+}
+; HARD: !{i32 1, !"float-abi", !"hard"}
+; SOFT: !{i32 1, !"float-abi", !"soft"}
+; NONE-NOT: !"float-abi"
+
+;--- hard.ll
+define void @f() {
+  ret void
+}
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"float-abi", !"hard"}

>From 3e176625a2beeeead285a0d27d2a25a4870b86d2 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 13 Aug 2026 01:56:11 +0200
Subject: [PATCH 2/3] Error on -float-abi conflicting with the "float-abi"
 module flag

---
 llvm/lib/CodeGen/CommandFlags.cpp             | 22 ++++++++++++++-----
 .../test/CodeGen/ARM/float-abi-module-flag.ll |  8 ++++---
 .../CodeGen/ARM/float-abi-synthesize-flag.ll  |  8 +++++--
 .../CodeGen/CSKY/float-abi-module-flag.ll     |  5 +++--
 4 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/CodeGen/CommandFlags.cpp b/llvm/lib/CodeGen/CommandFlags.cpp
index 88987d0bc5558..e99f1d8a859b9 100644
--- a/llvm/lib/CodeGen/CommandFlags.cpp
+++ b/llvm/lib/CodeGen/CommandFlags.cpp
@@ -23,6 +23,7 @@
 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
@@ -776,12 +777,23 @@ void codegen::setFunctionAttributes(Function &F, StringRef CPU,
 
 void codegen::setFunctionAttributes(Module &M, StringRef CPU,
                                     StringRef Features, StringRef TuneCPU) {
-  // Synthesize the "float-abi" module flag from the -float-abi option,
+  // Synthesize the "float-abi" module flag from the -float-abi option.
   FloatABI::ABIType ABI = getFloatABIForCalls();
-  if (ABI != FloatABI::Default && !M.getModuleFlag("float-abi")) {
-    M.addModuleFlag(
-        Module::Error, "float-abi",
-        MDString::get(M.getContext(), FloatABI::getABITypeName(ABI)));
+  if (ABI != FloatABI::Default) {
+    if (auto *Existing =
+            dyn_cast_or_null<MDString>(M.getModuleFlag("float-abi"))) {
+      // The module already records a float ABI; -float-abi must not contradict
+      // it.
+      if (Existing->getString() != FloatABI::getABITypeName(ABI))
+        reportFatalUsageError(
+            "-float-abi=" + FloatABI::getABITypeName(ABI) +
+            " conflicts with the \"float-abi\" module flag \"" +
+            Existing->getString() + "\"");
+    } else {
+      M.addModuleFlag(
+          Module::Error, "float-abi",
+          MDString::get(M.getContext(), FloatABI::getABITypeName(ABI)));
+    }
   }
 
   for (Function &F : M)
diff --git a/llvm/test/CodeGen/ARM/float-abi-module-flag.ll b/llvm/test/CodeGen/ARM/float-abi-module-flag.ll
index ea57078c619bd..79e4d73cf7b1a 100644
--- a/llvm/test/CodeGen/ARM/float-abi-module-flag.ll
+++ b/llvm/test/CodeGen/ARM/float-abi-module-flag.ll
@@ -14,9 +14,9 @@
 ; The triple default applies with no module flag.
 ; RUN: llc -mtriple=armv7-none-eabi -mattr=+vfp3 < %t/none.ll | FileCheck %s --check-prefix=SOFT
 
-; An explicit module flag takes precedence over a conflicting -float-abi option.
-; RUN: llc -mtriple=armv7-none-eabi -mattr=+vfp3 -float-abi=soft < %t/hard.ll | FileCheck %s --check-prefix=HARD
-; RUN: llc -mtriple=armv7-none-eabi -mattr=+vfp3 -float-abi=hard < %t/soft.ll | FileCheck %s --check-prefix=SOFT
+; A -float-abi option conflicting with the module flag is an error.
+; RUN: not llc -mtriple=armv7-none-eabi -mattr=+vfp3 -float-abi=soft < %t/hard.ll -filetype=null 2>&1 | FileCheck %s --check-prefix=CONFLICT-SOFT
+; RUN: not llc -mtriple=armv7-none-eabi -mattr=+vfp3 -float-abi=hard < %t/soft.ll -filetype=null 2>&1 | FileCheck %s --check-prefix=CONFLICT-HARD
 
 ;--- hard.ll
 define float @f(float %x) {
@@ -26,6 +26,7 @@ define float @f(float %x) {
 !llvm.module.flags = !{!0}
 !0 = !{i32 1, !"float-abi", !"hard"}
 ; HARD: vadd.f32 s0,
+; CONFLICT-SOFT: -float-abi=soft conflicts with the "float-abi" module flag "hard"
 
 ;--- soft.ll
 define float @f(float %x) {
@@ -35,6 +36,7 @@ define float @f(float %x) {
 !llvm.module.flags = !{!0}
 !0 = !{i32 1, !"float-abi", !"soft"}
 ; SOFT: vmov {{s[0-9]+}}, r0
+; CONFLICT-HARD: -float-abi=hard conflicts with the "float-abi" module flag "soft"
 
 ;--- none.ll
 define float @f(float %x) {
diff --git a/llvm/test/CodeGen/ARM/float-abi-synthesize-flag.ll b/llvm/test/CodeGen/ARM/float-abi-synthesize-flag.ll
index ce4d47d18010f..147a13b3211c5 100644
--- a/llvm/test/CodeGen/ARM/float-abi-synthesize-flag.ll
+++ b/llvm/test/CodeGen/ARM/float-abi-synthesize-flag.ll
@@ -13,8 +13,11 @@
 ; Without -float-abi, no flag is synthesized.
 ; RUN: llc -mtriple=armv7-none-eabi -stop-after=finalize-isel %t/none.ll -o - | FileCheck %s --check-prefix=NONE
 
-; An explicit in-IR flag is not overridden by -float-abi.
-; RUN: llc -mtriple=armv7-none-eabi -float-abi=soft -stop-after=finalize-isel %t/hard.ll -o - | FileCheck %s --check-prefix=HARD
+; -float-abi matching an existing in-IR flag is accepted.
+; RUN: llc -mtriple=armv7-none-eabi -float-abi=hard -stop-after=finalize-isel %t/hard.ll -o - | FileCheck %s --check-prefix=HARD
+
+; -float-abi conflicting with an existing in-IR flag is an error.
+; RUN: not llc -mtriple=armv7-none-eabi -float-abi=soft -stop-after=finalize-isel %t/hard.ll -filetype=null 2>&1 | FileCheck %s --check-prefix=CONFLICT
 
 ;--- none.ll
 define void @f() {
@@ -30,3 +33,4 @@ define void @f() {
 }
 !llvm.module.flags = !{!0}
 !0 = !{i32 1, !"float-abi", !"hard"}
+; CONFLICT: -float-abi=soft conflicts with the "float-abi" module flag "hard"
diff --git a/llvm/test/CodeGen/CSKY/float-abi-module-flag.ll b/llvm/test/CodeGen/CSKY/float-abi-module-flag.ll
index 78cffe05ade2f..84461789dcfa8 100644
--- a/llvm/test/CodeGen/CSKY/float-abi-module-flag.ll
+++ b/llvm/test/CodeGen/CSKY/float-abi-module-flag.ll
@@ -16,8 +16,8 @@
 ; no "float-abi" flag.
 ; RUN: llc -csky-no-aliases -mtriple=csky -mattr=+2e3,+fpuv2_sf,+fpuv2_df,+hard-float -float-abi=hard < %t/none.ll | FileCheck %s --check-prefix=HARD
 
-; An explicit module flag takes precedence over the legacy -float-abi option.
-; RUN: llc -csky-no-aliases -mtriple=csky -mattr=+2e3,+fpuv2_sf,+fpuv2_df,+hard-float -float-abi=hard < %t/soft.ll | FileCheck %s --check-prefix=SOFT
+; A -float-abi option conflicting with the module flag is an error.
+; RUN: not llc -csky-no-aliases -mtriple=csky -mattr=+2e3,+fpuv2_sf,+fpuv2_df,+hard-float -float-abi=hard < %t/soft.ll -filetype=null 2>&1 | FileCheck %s --check-prefix=CONFLICT
 
 ;--- none.ll
 define float @f(float %x, float %y) {
@@ -43,3 +43,4 @@ define float @f(float %x, float %y) {
 
 ; SOFT: fmtvrl
 ; HARD-NOT: fmtvrl
+; CONFLICT: -float-abi=hard conflicts with the "float-abi" module flag "soft"

>From 00fb3f716110eca17e5501a814a9f2e2875ebca5 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 13 Aug 2026 11:51:10 +0200
Subject: [PATCH 3/3] Remove opt description change

---
 llvm/lib/CodeGen/CommandFlags.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/CommandFlags.cpp b/llvm/lib/CodeGen/CommandFlags.cpp
index e99f1d8a859b9..f0aa3f49ffaa4 100644
--- a/llvm/lib/CodeGen/CommandFlags.cpp
+++ b/llvm/lib/CodeGen/CommandFlags.cpp
@@ -271,9 +271,7 @@ codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
   CGBINDOPT(EnableHonorSignDependentRoundingFPMath);
 
   static cl::opt<FloatABI::ABIType> FloatABIForCalls(
-      "float-abi",
-      cl::desc(
-          "Choose float ABI type (writes the \"float-abi\" IR module flag)"),
+      "float-abi", cl::desc("Choose float ABI type"),
       cl::init(FloatABI::Default),
       cl::values(clEnumValN(FloatABI::Default, "default",
                             "Target default float ABI type"),



More information about the llvm-commits mailing list