[lld] [llvm] [LTO] Override TargetABI from module flags if present when creating T… (PR #126497)

Kito Cheng via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 12 00:50:22 PST 2025


https://github.com/kito-cheng updated https://github.com/llvm/llvm-project/pull/126497

>From cca7238486736793248ca898e171633efc5bbaad Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Mon, 10 Feb 2025 00:57:03 -0800
Subject: [PATCH 1/2] [LTO] Override TargetABI from module flags if present
 when creating TargetMachine

RISC-V's data layout is also determined by the ABI, not only the target triple,
however the TargetMachine is created with the target triple's data layout,
that is not always correct. This patch changes the TargetMachine's data
layout to the one specified in the module flags if present.

The same problem will happen with other targets like MIPS, but unfortunately,
MIPS didn't emit the target-abi into the module flags, so this patch only
fixes the issue for RISC-V.

NOTE: MIPS with -mabi=n32 can trigger the same issue.

Another possible solution is add new parameter to the TargetMachine
constructor, but that would require changes in all the targets.
---
 lld/test/ELF/lto/riscv-ilp32e.ll | 25 +++++++++++++++++++++++++
 llvm/lib/LTO/LTOBackend.cpp      | 12 +++++++++++-
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 lld/test/ELF/lto/riscv-ilp32e.ll

diff --git a/lld/test/ELF/lto/riscv-ilp32e.ll b/lld/test/ELF/lto/riscv-ilp32e.ll
new file mode 100644
index 0000000000000..ea2b57d483358
--- /dev/null
+++ b/lld/test/ELF/lto/riscv-ilp32e.ll
@@ -0,0 +1,25 @@
+; REQUIRES: riscv
+;
+; Check that we don't crash on DataLayout incompatibility issue.
+;
+; RUN: llvm-as %s -o %t.o
+; RUN: ld.lld %t.o -o %t.elf
+; RUN: llvm-readobj -h %t.elf | FileCheck %s --check-prefixes=CHECK
+;
+; CHECK:  Machine: EM_RISCV (0xF3)
+; CHECK:  EF_RISCV_RVE (0x8)
+
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32-S32"
+target triple = "riscv32-unknown-unknown-elf"
+
+define dso_local i32 @_start() #0 {
+entry:
+  ret i32 0
+}
+
+attributes #0 = { "target-cpu"="generic-rv32" "target-features"="+32bit,+e" }
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 1, !"target-abi", !"ilp32e"}
diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index 8a2dddce4892c..b9d2242d4a2cf 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -221,8 +221,18 @@ createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) {
   else
     CodeModel = M.getCodeModel();
 
+  TargetOptions TargetOpts = Conf.Options;
+
+  if (TargetOpts.MCOptions.ABIName.empty()) {
+    Metadata *ABI = M.getModuleFlag("target-abi");
+    if (ABI) {
+      const StringRef &ModABI = cast<MDString>(ABI)->getString();
+      TargetOpts.MCOptions.ABIName = ModABI;
+    }
+  }
+
   std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
-      TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
+      TheTriple, Conf.CPU, Features.getString(), TargetOpts, RelocModel,
       CodeModel, Conf.CGOptLevel));
 
   assert(TM && "Failed to create target machine");

>From d9972a8ae24c7909215f7c93f8c41ddec4837ccc Mon Sep 17 00:00:00 2001
From: Kito Cheng <kito.cheng at sifive.com>
Date: Wed, 12 Feb 2025 00:47:55 -0800
Subject: [PATCH 2/2] address comment

---
 llvm/test/LTO/RISCV/lit.local.cfg                         | 2 ++
 {lld/test/ELF/lto => llvm/test/LTO/RISCV}/riscv-ilp32e.ll | 6 ++----
 2 files changed, 4 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/LTO/RISCV/lit.local.cfg
 rename {lld/test/ELF/lto => llvm/test/LTO/RISCV}/riscv-ilp32e.ll (79%)

diff --git a/llvm/test/LTO/RISCV/lit.local.cfg b/llvm/test/LTO/RISCV/lit.local.cfg
new file mode 100644
index 0000000000000..17351748513d9
--- /dev/null
+++ b/llvm/test/LTO/RISCV/lit.local.cfg
@@ -0,0 +1,2 @@
+if not "RISCV" in config.root.targets:
+    config.unsupported = True
diff --git a/lld/test/ELF/lto/riscv-ilp32e.ll b/llvm/test/LTO/RISCV/riscv-ilp32e.ll
similarity index 79%
rename from lld/test/ELF/lto/riscv-ilp32e.ll
rename to llvm/test/LTO/RISCV/riscv-ilp32e.ll
index ea2b57d483358..45cc1b78f649a 100644
--- a/lld/test/ELF/lto/riscv-ilp32e.ll
+++ b/llvm/test/LTO/RISCV/riscv-ilp32e.ll
@@ -1,10 +1,8 @@
-; REQUIRES: riscv
-;
 ; Check that we don't crash on DataLayout incompatibility issue.
 ;
 ; RUN: llvm-as %s -o %t.o
-; RUN: ld.lld %t.o -o %t.elf
-; RUN: llvm-readobj -h %t.elf | FileCheck %s --check-prefixes=CHECK
+; RUN: llvm-lto2 run -r %t.o,_start %t.o -o %t.elf
+; RUN: llvm-readobj -h %t.elf.0 | FileCheck %s --check-prefixes=CHECK
 ;
 ; CHECK:  Machine: EM_RISCV (0xF3)
 ; CHECK:  EF_RISCV_RVE (0x8)



More information about the llvm-commits mailing list