Repository navigation
clang: Emit "target-abi" module flag for ARM - #217601
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-backend-arm Author: Matt Arsenault (arsenm) ChangesPreviously only RISCV emitted the "target-abi" module flag. Co-authored-by: Claude (Claude-Opus-4.8) <[email protected]> 2 Files Affected:
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index d348e80780ee9..50901fe388140 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1472,6 +1472,17 @@ void CodeGenModule::Release() {
}
llvm::Triple T = Context.getTargetInfo().getTriple();
+
+ // TODO: This should probably be just generally emitted for non-empty ABI
+ // names. LoongArch actively consumes the flag, but it is excluded here.
+ // Other targets have no apparent need for the ABI name, but set a non-empty
+ // value.
+ if (StringRef ABIStr = Target.getABI();
+ !ABIStr.empty() && (T.isARM() || T.isThumb() || T.isRISCV())) {
+ getModule().addModuleFlag(llvm::Module::Error, "target-abi",
+ llvm::MDString::get(VMContext, ABIStr));
+ }
+
if (T.isARM() || T.isThumb()) {
// The minimum width of an enum in bytes
uint32_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
@@ -1479,10 +1490,7 @@ void CodeGenModule::Release() {
}
if (T.isRISCV()) {
- StringRef ABIStr = Target.getABI();
llvm::LLVMContext &Ctx = TheModule.getContext();
- getModule().addModuleFlag(llvm::Module::Error, "target-abi",
- llvm::MDString::get(Ctx, ABIStr));
// Add the canonical ISA string as metadata so the backend can set the ELF
// attributes correctly. We use AppendUnique so LTO will keep all of the
diff --git a/clang/test/CodeGen/arm-target-abi-module-flag.c b/clang/test/CodeGen/arm-target-abi-module-flag.c
new file mode 100644
index 0000000000000..9707372b2a3fd
--- /dev/null
+++ b/clang/test/CodeGen/arm-target-abi-module-flag.c
@@ -0,0 +1,19 @@
+// Check that clang emits the "target-abi" module flag for ARM/Thumb using the
+// target ABI string.
+
+// Default ABIs (no -target-abi override).
+// RUN: %clang_cc1 -triple armv7-linux-gnueabihf -emit-llvm -o - %s | FileCheck --check-prefix=AAPCS-LINUX %s
+// RUN: %clang_cc1 -triple armv7-none-eabi -emit-llvm -o - %s | FileCheck --check-prefix=AAPCS %s
+// RUN: %clang_cc1 -triple armv7-apple-darwin -emit-llvm -o - %s | FileCheck --check-prefix=APCS-GNU %s
+// RUN: %clang_cc1 -triple armv7k-apple-watchos -emit-llvm -o - %s | FileCheck --check-prefix=AAPCS16 %s
+// RUN: %clang_cc1 -triple thumbv7-linux-gnueabihf -emit-llvm -o - %s | FileCheck --check-prefix=AAPCS-LINUX %s
+
+// Explicit -target-abi override differing from the triple default.
+// RUN: %clang_cc1 -triple armv7-linux-gnueabihf -target-abi apcs-gnu -emit-llvm -o - %s | FileCheck --check-prefix=APCS-GNU %s
+
+// AAPCS-LINUX: !{i32 1, !"target-abi", !"aapcs-linux"}
+// AAPCS: !{i32 1, !"target-abi", !"aapcs"}
+// APCS-GNU: !{i32 1, !"target-abi", !"apcs-gnu"}
+// AAPCS16: !{i32 1, !"target-abi", !"aapcs16"}
+
+int x;
|
🐧 Linux x64 Test Results
Failed Tests(click on a test name to see its output) ClangClang.CodeGen/ubsan-function-sugared.cppClang.CodeGen/ubsan-function.cppIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
🪟 Windows x64 Test Results
Failed Tests(click on a test name to see its output) ClangClang.CodeGen/ubsan-function-sugared.cppClang.CodeGen/ubsan-function.cppIf these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the |
86fca79 to
fbbef72
Compare
011eb21 to
646ea6d
Compare
rnk
left a comment
There was a problem hiding this comment.
What is the public interface to reach -target-abi? Testing shows it's a -cc1 flag, and I couldn't find anything in the command line reference.
Anyway, this is a setting you can set in the frontend, so we should encode it in the IR for the backend.
-mabi |
Previously only RISCV emitted the "target-abi" module flag. We probably should just generally emit this for non-empty ABI names but that's a broader behavior change. I'm also confused because the clang side defines a non-empty value for many targets with no apparent use in llvm. Co-authored-by: Claude (Claude-Opus-4.8) <[email protected]>
fbbef72 to
43f21cb
Compare
The LoongArch backend reads the ABI from the "target-abi" module flag, but clang only emits that flag for ARM, PowerPC and RISC-V. Since LTO code generation doesn't see the -target-abi option passed to clang, it falls back to the default ABI of the target, which is lp64d for loongarch64-unknown-elf, regardless of the ABI the code was compiled for. When an FPU is enabled, this silently miscompiles code built for another ABI. For example, with -mabi=lp64s, functions get the lp64d calling convention after LTO, passing and returning floating-point values in FP registers instead of general-purpose ones, and the output is marked as double-float. Nothing reports this if every object goes through LTO. For instance, a shared library built this way links without error, and so does lp64s code built without LTO that uses it. Without an FPU, the generated code keeps the soft-float calling convention, but the output is still marked as double-float. The mismatch only shows up as an error when LTO output is linked together with relocatable objects built without LTO, which LLD rejects with "cannot link object files with different ABI". This patch adds LoongArch to the targets for which clang emits the flag, so LTO uses the ABI the code was compiled for. It follows the per-target approach taken for ARM (#217601) and PowerPC (#221669), and leaves the existing TODO about emitting the flag for every target with a non-empty ABI name as is. With the flag present, LTO-linking bitcode built for different LoongArch ABIs now fails with "linking module flags 'target-abi': IDs have conflicting values" instead of silently picking a single ABI. A -target-abi given to the LTO backend that disagrees with the flag is now also an error. Both match the existing RISC-V behavior. Assisted-by: Claude Code (Claude Opus 5) Signed-off-by: Mintsuki <[email protected]>

Previously only RISCV emitted the "target-abi" module flag.
We probably should just generally emit this for non-empty ABI names
but that's a broader behavior change. I'm also confused because the
clang side defines a non-empty value for many targets with no apparent
use in llvm.
Co-authored-by: Claude (Claude-Opus-4.8) [email protected]