https://github.com/shiltian created 
https://github.com/llvm/llvm-project/pull/244213

Since 630b6b92235e, clang keeps the data layout of an input IR module and checks
it against the layout that the target computes. The check compares the two
strings, but the bitcode reader upgrades the data layout string of older bitcode
by adding the missing specifications at the end, so the upgraded string may list
them in a different order. Clang then rejects older AMDGPU bitcode , although
the layouts are essentially the same. This PR compares the layouts instead.

This PR was assisted by AI but I reviewed all the changes.


>From 340a16f2a6c7a7521e6a392ffd7ebb990d425df0 Mon Sep 17 00:00:00 2001
From: Shilei Tian <[email protected]>
Date: Wed, 18 Nov 2026 23:37:55 -0500
Subject: [PATCH] [Clang] Compare data layouts, not strings, in the backend
 check

Since 630b6b92235e, clang keeps the data layout of an input IR module and checks
it against the layout that the target computes. The check compares the two
strings, but the bitcode reader upgrades the data layout string of older bitcode
by adding the missing specifications at the end, so the upgraded string may list
them in a different order. Clang then rejects older AMDGPU bitcode , although
the layouts are essentially the same. This PR compares the layouts instead.

This PR was assisted by AI but I reviewed all the changes.
---
 clang/lib/CodeGen/BackendUtil.cpp             | 10 ++++++----
 .../CodeGen/AMDGPU/upgraded-datalayout.ll     | 20 +++++++++++++++++++
 2 files changed, 26 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/CodeGen/AMDGPU/upgraded-datalayout.ll

diff --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index 61864100756044..57d80160255c7a 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1625,13 +1625,15 @@ void clang::emitBackendOutput(CompilerInstance &CI, 
CodeGenOptions &CGOpts,
 
   // Verify the module's DataLayout against the one the target computes for the
   // module's ABI. This respects the target-abi module flag rather than 
assuming
-  // the DataLayout is a fixed property of the target options.
+  // the DataLayout is a fixed property of the target options. Compare the
+  // layouts, not their strings: a string that the bitcode reader upgraded can
+  // list the same specifications in a different order.
   if (AsmHelper.TM) {
-    std::string DLDesc = M->getDataLayout().getStringRepresentation();
     std::string TDesc = M->getTargetTriple().computeDataLayout(
         AsmHelper.TM->getTargetABIName(*M));
-    if (DLDesc != TDesc)
-      Diags.Report(diag::err_data_layout_mismatch) << DLDesc << TDesc;
+    if (M->getDataLayout() != DataLayout(TDesc))
+      Diags.Report(diag::err_data_layout_mismatch)
+          << M->getDataLayout().getStringRepresentation() << TDesc;
   }
 }
 
diff --git a/clang/test/CodeGen/AMDGPU/upgraded-datalayout.ll 
b/clang/test/CodeGen/AMDGPU/upgraded-datalayout.ll
new file mode 100644
index 00000000000000..2eb52d54a81640
--- /dev/null
+++ b/clang/test/CodeGen/AMDGPU/upgraded-datalayout.ll
@@ -0,0 +1,20 @@
+; REQUIRES: amdgpu-registered-target
+
+; The data layout string below comes from an older LLVM. When clang reads the
+; bitcode, it upgrades the string. The upgraded string describes the target
+; layout, but it lists the specifications in a different order. Clang must
+; accept it.
+; RUN: llvm-as %s -o %t.bc
+; RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -target-cpu gfx900 -x ir %t.bc 
-emit-obj -o /dev/null
+
+; A layout that differs from the target layout is still an error.
+; RUN: sed 's/p3:32:32/p3:64:64/' %s | llvm-as -o %t.bad.bc
+; RUN: not %clang_cc1 -triple amdgcn-amd-amdhsa -target-cpu gfx900 -x ir 
%t.bad.bc -emit-llvm-bc -o /dev/null 2>&1 | FileCheck %s
+; CHECK: error: backend data layout '{{.*}}-p3:64:64-{{.*}}' does not match 
expected target description '{{.*}}-p3:32:32-{{.*}}'
+
+target datalayout = 
"e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9"
+target triple = "amdgcn-amd-amdhsa"
+
+define void @func() {
+  ret void
+}

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

Reply via email to