https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/224280

The offload wrapper module was created with only a target triple, and the
datalayout wasn't set until later, where it was copied from the TargetMachine,
after the wrapping code already ran.

The wrapping code did query the datalayout for the pointer size, so this was
broken for host and devices with different pointer sizes.
queries the DataLayout (e.g. the size_t / intptr type via getSizeTTy), so on a
host whose pointer size differs from the default layout (e.g. a 32-bit host) it
used the wrong integer width for image offsets.

This removes a use of TargetMachine::createDataLayout, which I am trying to
remove.

Co-authored-by: Claude (Claude-Opus-4.8) <[email protected]>

>From 4fc3aca12aa21e58b98845dd39a463af3c324be2 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <[email protected]>
Date: Thu, 17 Sep 2026 13:53:35 +0200
Subject: [PATCH] clang-linker-wrapper: Set the host DataLayout on the offload
 wrapper module

The offload wrapper module was created with only a target triple, and the
datalayout wasn't set until later, where it was copied from the TargetMachine,
after the wrapping code already ran.

The wrapping code did query the datalayout for the pointer size, so this was
broken for host and devices with different pointer sizes.
queries the DataLayout (e.g. the size_t / intptr type via getSizeTTy), so on a
host whose pointer size differs from the default layout (e.g. a 32-bit host) it
used the wrong integer width for image offsets.

This removes a use of TargetMachine::createDataLayout, which I am trying to
remove.

Co-authored-by: Claude (Claude-Opus-4.8) <[email protected]>
---
 .../linker-wrapper-host-datalayout.c          | 27 +++++++++++++++++++
 .../ClangLinkerWrapper.cpp                    |  9 +++----
 2 files changed, 31 insertions(+), 5 deletions(-)
 create mode 100644 
clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-host-datalayout.c

diff --git 
a/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-host-datalayout.c 
b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-host-datalayout.c
new file mode 100644
index 0000000000000..0c1e0ed04dd9d
--- /dev/null
+++ 
b/clang/test/OffloadTools/clang-linker-wrapper/linker-wrapper-host-datalayout.c
@@ -0,0 +1,27 @@
+// Check that the offload wrapper module is created with the correct host
+// DataLayout, so the wrapping code emits pointer-size-dependent values using
+// the correct pointer width rather than the default layout's.
+
+// REQUIRES: x86-registered-target, amdgpu-registered-target
+
+// RUN: %clang -cc1 -triple i386-unknown-linux-gnu -emit-obj %s -o %t.elf.o
+// RUN: %clang -cc1 -triple amdgpu9.00-amd-amdhsa -emit-llvm-bc %s -o 
%t.amdgpu.bc
+
+// RUN: llvm-offload-binary -o %t.out \
+// RUN:   --image=file=%t.amdgpu.bc,kind=openmp,triple=amdgpu9.00-amd-amdhsa
+// RUN: %clang -cc1 %s -triple i386-unknown-linux-gnu -emit-obj -o %t.o 
-fembed-offload-object=%t.out
+
+// RUN: clang-linker-wrapper --host-triple=i386-unknown-linux-gnu --dry-run \
+// RUN:   --print-wrapped-module --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 \
+// RUN:   | FileCheck %s
+
+// CHECK: target datalayout = "e-m:e-p:32:32-{{.*}}"
+// CHECK: target triple = "i386-unknown-linux-gnu"
+
+// The image offsets should use the host's 32-bit size_t type, instead of the
+// incorrect i64 according to the default DataLayout.
+
+// CHECK: @.omp_offloading.device_images =
+// CHECK-SAME: getelementptr (i8, ptr @.omp_offloading.device_image, i32 
{{[0-9]+}})
+
+__attribute__((visibility("protected"), used)) int x;
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index ec1b824589de9..59faf38dda140 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -706,9 +706,6 @@ Expected<StringRef> compileModule(Module &M, OffloadKind 
Kind) {
       T->createTargetMachine(M.getTargetTriple(), CPU, Features, Options,
                              Reloc::PIC_, M.getCodeModel()));
 
-  if (M.getDataLayout().isDefault())
-    M.setDataLayout(TM->createDataLayout());
-
   int FD = -1;
   auto TempFileOrErr = createOutputFile(
       ExecutableName + "." + getOffloadKindName(Kind) + ".image.wrapper", "o");
@@ -809,8 +806,10 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> 
Buffers,
 
   LLVMContext Context;
   Module M("offload.wrapper.module", Context);
-  M.setTargetTriple(Triple(
-      Args.getLastArgValue(OPT_host_triple_EQ, 
sys::getDefaultTargetTriple())));
+  Triple TheTriple(
+      Args.getLastArgValue(OPT_host_triple_EQ, sys::getDefaultTargetTriple()));
+  M.setTargetTriple(TheTriple);
+  M.setDataLayout(TheTriple.computeDataLayout());
 
   switch (Kind) {
   case OFK_OpenMP:

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to