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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 04:53:50 PDT 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/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>

>From 98700ef91f29c803cde45ee2450ab30e5cf10f4c Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Wed, 16 Sep 2026 13:41:35 +0200
Subject: [PATCH] LTO: Stop overwriting the merged module's DataLayout

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>
---
 llvm/lib/LTO/LTOCodeGenerator.cpp             |  3 ---
 .../LTO/RISCV/merged-module-datalayout.ll     | 26 +++++++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/LTO/RISCV/merged-module-datalayout.ll

diff --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 8b43c64e53507..3013d912f006a 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -603,9 +603,6 @@ bool LTOCodeGenerator::optimize() {
   // Mark which symbols can not be internalized
   this->applyScopeRestrictions();
 
-  // Add an appropriate DataLayout instance for this module...
-  MergedModule->setDataLayout(TargetMach->createDataLayout());
-
   if (!SaveIRBeforeOptPath.empty()) {
     std::error_code EC;
     raw_fd_ostream OS(SaveIRBeforeOptPath, EC, sys::fs::OF_None);
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"}



More information about the llvm-commits mailing list