Skip to content

[clang][LoongArch] Emit "target-abi" module flag - #223647

Merged
arsenm merged 1 commit into
llvm:mainfrom
Mintsuki:loongarch-emit-target-abi
Sep 16, 2026
Merged

arsenm merged 1 commit into
llvm:mainfrom
Mintsuki:loongarch-emit-target-abi

Conversation

@Mintsuki

Copy link
Copy Markdown
Contributor

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)

The LoongArch backend takes the ABI from the "target-abi" module flag,
but clang only emitted that flag for ARM, PowerPC and RISC-V. LTO code
generation, which does not see the -target-abi option clang was invoked
with, thus fell back to the default ABI of the target, which is lp64d
for loongarch64-unknown-elf. Objects built with -mabi=lp64s and LTO then
ended up with the double-float ABI, which LLD refuses to link together
with the soft-float objects built without LTO.

Emit the flag for LoongArch as well.

Assisted-by: Claude Code (Claude Opus 5)
Signed-off-by: Mintsuki <[email protected]>
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Sep 15, 2026
@Mintsuki

Copy link
Copy Markdown
Contributor Author

Reproducer for the silent miscompile, with LLVM 22.1.8:

$ cat lib.c
double scale(double x, double y) { return x * y + 1.0; }
$ clang --target=loongarch64-unknown-elf -mabi=lp64s -O2 -fPIC -flto -c lib.c
$ ld.lld -shared lib.o -o lib.so
$ llvm-readelf -h lib.so | grep Flags
  Flags:                             0x43, DOUBLE-FLOAT, OBJ-v1
$ llvm-objdump -d --no-show-raw-insn lib.so | grep -A6 "<scale>:"
0000000000010240 <scale>:
   10240:       vldi    $vr2, -912
   10244:       fmadd.d $fa0, $fa0, $fa1, $fa2
   10248:       ret

With this patch, scale takes its arguments in $a0/$a1 and returns in
$a0, as lp64s requires, and the output is marked soft-float:

  Flags:                             0x41, SOFT-FLOAT, OBJ-v1
0000000000010240 <scale>:
   10240:       movgr2fr.d      $fa0, $a1
   10244:       movgr2fr.d      $fa1, $a0
   10248:       vldi    $vr2, -912
   1024c:       fmadd.d $fa0, $fa1, $fa0, $fa2
   10250:       movfr2gr.d      $a0, $fa0
   10254:       ret

Reproducer for the link error:

$ cat a.c
int b(void);
int _start(void) { return b(); }
$ cat b.c
int b(void) { return 0; }
$ clang --target=loongarch64-unknown-elf -mabi=lp64s -flto -c a.c
$ clang --target=loongarch64-unknown-elf -mabi=lp64s -c b.c
$ ld.lld a.o b.o
ld.lld: error: a.out.lto.o: cannot link object files with different ABI from b.o

With this patch, the link succeeds and a.out is marked soft-float.

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

Author: Mintsuki (Mintsuki)

Changes

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)


Full diff: https://github.com/llvm/llvm-project/pull/223647.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+4-5)
  • (added) clang/test/CodeGen/LoongArch/target-abi-module-flag.c (+21)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 4839e2f45f72a..06b9ac9178f6f 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1495,12 +1495,11 @@ 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.
+  // names. 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() || T.isPPC())) {
+      !ABIStr.empty() && (T.isARM() || T.isThumb() || T.isRISCV() ||
+                          T.isPPC() || T.isLoongArch())) {
     getModule().addModuleFlag(llvm::Module::Error, "target-abi",
                               llvm::MDString::get(VMContext, ABIStr));
   }
diff --git a/clang/test/CodeGen/LoongArch/target-abi-module-flag.c b/clang/test/CodeGen/LoongArch/target-abi-module-flag.c
new file mode 100644
index 0000000000000..5a4826aff180b
--- /dev/null
+++ b/clang/test/CodeGen/LoongArch/target-abi-module-flag.c
@@ -0,0 +1,21 @@
+// Check that clang emits the "target-abi" module flag for LoongArch using the
+// target ABI string.
+
+// Default ABIs (no -target-abi override).
+// RUN: %clang_cc1 -triple loongarch32 -emit-llvm -o - %s | FileCheck --check-prefix=ILP32D %s
+// RUN: %clang_cc1 -triple loongarch64 -emit-llvm -o - %s | FileCheck --check-prefix=LP64D %s
+
+// Explicit -target-abi overrides differing from the triple default.
+// RUN: %clang_cc1 -triple loongarch32 -target-abi ilp32f -emit-llvm -o - %s | FileCheck --check-prefix=ILP32F %s
+// RUN: %clang_cc1 -triple loongarch32 -target-abi ilp32s -emit-llvm -o - %s | FileCheck --check-prefix=ILP32S %s
+// RUN: %clang_cc1 -triple loongarch64 -target-abi lp64f -emit-llvm -o - %s | FileCheck --check-prefix=LP64F %s
+// RUN: %clang_cc1 -triple loongarch64 -target-abi lp64s -emit-llvm -o - %s | FileCheck --check-prefix=LP64S %s
+
+// ILP32D: !{i32 1, !"target-abi", !"ilp32d"}
+// ILP32F: !{i32 1, !"target-abi", !"ilp32f"}
+// ILP32S: !{i32 1, !"target-abi", !"ilp32s"}
+// LP64D: !{i32 1, !"target-abi", !"lp64d"}
+// LP64F: !{i32 1, !"target-abi", !"lp64f"}
+// LP64S: !{i32 1, !"target-abi", !"lp64s"}
+
+int x;

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-clang-codegen

Author: Mintsuki (Mintsuki)

Changes

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)


Full diff: https://github.com/llvm/llvm-project/pull/223647.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+4-5)
  • (added) clang/test/CodeGen/LoongArch/target-abi-module-flag.c (+21)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 4839e2f45f72a..06b9ac9178f6f 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1495,12 +1495,11 @@ 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.
+  // names. 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() || T.isPPC())) {
+      !ABIStr.empty() && (T.isARM() || T.isThumb() || T.isRISCV() ||
+                          T.isPPC() || T.isLoongArch())) {
     getModule().addModuleFlag(llvm::Module::Error, "target-abi",
                               llvm::MDString::get(VMContext, ABIStr));
   }
diff --git a/clang/test/CodeGen/LoongArch/target-abi-module-flag.c b/clang/test/CodeGen/LoongArch/target-abi-module-flag.c
new file mode 100644
index 0000000000000..5a4826aff180b
--- /dev/null
+++ b/clang/test/CodeGen/LoongArch/target-abi-module-flag.c
@@ -0,0 +1,21 @@
+// Check that clang emits the "target-abi" module flag for LoongArch using the
+// target ABI string.
+
+// Default ABIs (no -target-abi override).
+// RUN: %clang_cc1 -triple loongarch32 -emit-llvm -o - %s | FileCheck --check-prefix=ILP32D %s
+// RUN: %clang_cc1 -triple loongarch64 -emit-llvm -o - %s | FileCheck --check-prefix=LP64D %s
+
+// Explicit -target-abi overrides differing from the triple default.
+// RUN: %clang_cc1 -triple loongarch32 -target-abi ilp32f -emit-llvm -o - %s | FileCheck --check-prefix=ILP32F %s
+// RUN: %clang_cc1 -triple loongarch32 -target-abi ilp32s -emit-llvm -o - %s | FileCheck --check-prefix=ILP32S %s
+// RUN: %clang_cc1 -triple loongarch64 -target-abi lp64f -emit-llvm -o - %s | FileCheck --check-prefix=LP64F %s
+// RUN: %clang_cc1 -triple loongarch64 -target-abi lp64s -emit-llvm -o - %s | FileCheck --check-prefix=LP64S %s
+
+// ILP32D: !{i32 1, !"target-abi", !"ilp32d"}
+// ILP32F: !{i32 1, !"target-abi", !"ilp32f"}
+// ILP32S: !{i32 1, !"target-abi", !"ilp32s"}
+// LP64D: !{i32 1, !"target-abi", !"lp64d"}
+// LP64F: !{i32 1, !"target-abi", !"lp64f"}
+// LP64S: !{i32 1, !"target-abi", !"lp64s"}
+
+int x;

@Mintsuki Mintsuki changed the title [clang][LoongArch] Emit target-abi module flag [clang][LoongArch] Emit "target-abi" module flag Sep 15, 2026
@Mintsuki

Copy link
Copy Markdown
Contributor Author

@arsenm @SixWeining

@SixWeining SixWeining left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@SixWeining

Copy link
Copy Markdown
Contributor

cc @wangleiat @zhaoqi5

@arsenm
arsenm merged commit 4beeff3 into llvm:main Sep 16, 2026
15 checks passed
@Mintsuki

Copy link
Copy Markdown
Contributor Author

/cherry-pick 4beeff3

@llvmbot

llvmbot commented Sep 16, 2026

Copy link
Copy Markdown
Member

/cherry-pick 4beeff3

Error: Command failed due to missing milestone.

@Mintsuki

Copy link
Copy Markdown
Contributor Author

@heiher @SixWeining apparently the milestone needs to be set on this PR. Given the severity of the bug, could someone set the LLVM 23.x Release milestone on this? Thanks.

@efriedma-quic

Copy link
Copy Markdown
Contributor

Done

@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in LLVM Release Status Sep 16, 2026
@arsenm

arsenm commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Why would this need a release branch? The recent target-abi changes were not in the release branch?

@Mintsuki

Copy link
Copy Markdown
Contributor Author

/cherry-pick 4beeff3

@llvmbot

llvmbot commented Sep 16, 2026

Copy link
Copy Markdown
Member

Failed to cherry-pick: 4beeff3

https://github.com/llvm/llvm-project/actions/runs/35131517924

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 21, 2026
Set target-abi module flag for LoongArch targets

Like the earlier RISC-V fix (rust-lang#123612), emit the "target-abi" module flag for LoongArch so that LTO correctly picks up the ABI.

This follows the recent LLVM change (llvm/llvm-project#223647) that relies on the module flag for proper TargetMachine / data-layout handling during LTO on LoongArch.
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Sep 21, 2026
Rollup merge of #162948 - heiher:loong-emit-target-abi, r=nikic

Set target-abi module flag for LoongArch targets

Like the earlier RISC-V fix (#123612), emit the "target-abi" module flag for LoongArch so that LTO correctly picks up the ABI.

This follows the recent LLVM change (llvm/llvm-project#223647) that relies on the module flag for proper TargetMachine / data-layout handling during LTO on LoongArch.
pull Bot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Sep 22, 2026
Set target-abi module flag for LoongArch targets

Like the earlier RISC-V fix (rust-lang/rust#123612), emit the "target-abi" module flag for LoongArch so that LTO correctly picks up the ABI.

This follows the recent LLVM change (llvm/llvm-project#223647) that relies on the module flag for proper TargetMachine / data-layout handling during LTO on LoongArch.
pull Bot pushed a commit to xtqqczze/rust-lang-rustc-dev-guide that referenced this pull request Sep 30, 2026
Set target-abi module flag for LoongArch targets

Like the earlier RISC-V fix (rust-lang/rust#123612), emit the "target-abi" module flag for LoongArch so that LTO correctly picks up the ABI.

This follows the recent LLVM change (llvm/llvm-project#223647) that relies on the module flag for proper TargetMachine / data-layout handling during LTO on LoongArch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category release:cherry-pick-failed

Projects

Development

Successfully merging this pull request may close these issues.

5 participants