[llvm] 1dfe26e - LTO: Stop overwriting the merged module's DataLayout (#223987)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 07:09:11 PDT 2026


Author: Matt Arsenault
Date: 2026-09-21T16:09:04+02:00
New Revision: 1dfe26e2259d80c442f0b4be66198316e2b8229d

URL: https://github.com/llvm/llvm-project/commit/1dfe26e2259d80c442f0b4be66198316e2b8229d
DIFF: https://github.com/llvm/llvm-project/commit/1dfe26e2259d80c442f0b4be66198316e2b8229d.diff

LOG: LTO: Stop overwriting the merged module's DataLayout (#223987)

LTOCodeGenerator::optimize() unconditionally reset the merged module's
DataLayout from TargetMachine::createDataLayout(). The merged module
already carries the correct DataLayout since IRMover propagates the source
module's layout into the empty destination. The module should be the source of
truth for its DataLayout.

Recomputing it from the TargetMachine is also latently wrong, since the
DataLayout depends on the target-abi module flag which the
TargetMachine's cached DataLayout cannot account for.

Co-authored-by: Claude (Claude-Opus-4.8) <noreply at anthropic.com>

Added: 
    llvm/test/LTO/RISCV/merged-module-datalayout.ll

Modified: 
    llvm/lib/LTO/LTOCodeGenerator.cpp
    llvm/test/LTO/ARM/runtime-library-subtarget.ll
    llvm/test/LTO/X86/cfi_endproc.ll
    llvm/test/LTO/X86/linkonce_odr_func.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 8b43c64e53507..6cb30123c9572 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -603,8 +603,14 @@ bool LTOCodeGenerator::optimize() {
   // Mark which symbols can not be internalized
   this->applyScopeRestrictions();
 
-  // Add an appropriate DataLayout instance for this module...
-  MergedModule->setDataLayout(TargetMach->createDataLayout());
+  // Seed a DataLayout only if the merged module does not already carry one, so
+  // an input module's own DataLayout is preserved. Compute it from the module's
+  // ABI so the target-abi module flag is respected.
+  if (MergedModule->getDataLayout().isDefault()) {
+    MergedModule->setDataLayout(
+        MergedModule->getTargetTriple().computeDataLayout(
+            TargetMach->getTargetABIName(*MergedModule)));
+  }
 
   if (!SaveIRBeforeOptPath.empty()) {
     std::error_code EC;

diff  --git a/llvm/test/LTO/ARM/runtime-library-subtarget.ll b/llvm/test/LTO/ARM/runtime-library-subtarget.ll
index aab1d90ea3b9b..90e095e85e3f8 100644
--- a/llvm/test/LTO/ARM/runtime-library-subtarget.ll
+++ b/llvm/test/LTO/ARM/runtime-library-subtarget.ll
@@ -4,7 +4,7 @@
 ; RUN: llvm-lto -o %t2 %t1 -mcpu arm1176jz-s
 ; RUN: llvm-nm %t2 | FileCheck %s
 
-target datalayout = "e-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
+target datalayout = "e-m:o-p:32:32-Fi8-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
 target triple = "thumbv7-apple-ios"
 
 ; CHECK: ___addsf3vfp

diff  --git a/llvm/test/LTO/RISCV/merged-module-datalayout.ll b/llvm/test/LTO/RISCV/merged-module-datalayout.ll
new file mode 100644
index 0000000000000..b66a2042a1147
--- /dev/null
+++ b/llvm/test/LTO/RISCV/merged-module-datalayout.ll
@@ -0,0 +1,26 @@
+; Check that legacy LTO preserves the input module's DataLayout
+; instead of overwriting it with one recomputed from the
+; TargetMachine. The ilp32e ABI is carried by the target-abi module
+; flag, so the TargetMachine's option-derived DataLayout would use the
+; default -S128 stack alignment rather than the -S32 the module
+; actually requires.
+
+; RUN: llvm-as %s -o %t.o
+; RUN: llvm-lto -save-merged-module -o %t.elf %t.o
+; RUN: llvm-dis %t.elf.merged.bc -o - | FileCheck %s
+
+; CHECK: target datalayout = "e-m:e-p:32:32-i64:64-n32-S32"
+
+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/test/LTO/X86/cfi_endproc.ll b/llvm/test/LTO/X86/cfi_endproc.ll
index 1251c0ade80a5..5aff5a4493848 100644
--- a/llvm/test/LTO/X86/cfi_endproc.ll
+++ b/llvm/test/LTO/X86/cfi_endproc.ll
@@ -4,7 +4,7 @@
 ; RUN: llvm-lto -o %t3 -exported-symbol=main %t1
 ; RUN: llvm-nm %t3 | FileCheck %s -check-prefix=EXPORT
 
-target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"
 
 module asm ".text"

diff  --git a/llvm/test/LTO/X86/linkonce_odr_func.ll b/llvm/test/LTO/X86/linkonce_odr_func.ll
index 3451a1196ff7f..be3da4acd9cf1 100644
--- a/llvm/test/LTO/X86/linkonce_odr_func.ll
+++ b/llvm/test/LTO/X86/linkonce_odr_func.ll
@@ -4,7 +4,7 @@
 ; RUN:     -dso-symbol=v4 -dso-symbol=v5 -dso-symbol=v6 %t1 -O0
 ; RUN: llvm-nm %t2 | FileCheck %s
 
-target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"
 
 ; CHECK: W foo1


        


More information about the llvm-commits mailing list