https://github.com/inbelic created 
https://github.com/llvm/llvm-project/pull/211978

Resolves https://github.com/llvm/llvm-project/issues/211977

>From 8b69382e5cf0ccfa0b8df2559a4e0f6448f0fc87 Mon Sep 17 00:00:00 2001
From: Finn Plummer <[email protected]>
Date: Sat, 25 Jul 2026 02:21:52 +0000
Subject: [PATCH] [CodeGenTypes] Preserve element type attributes for
 constantarray types

---
 clang/lib/CodeGen/CodeGenTypes.cpp            | 13 ++++++++++
 .../test/CodeGenHLSL/matrix-array-layout.hlsl | 26 +++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100644 clang/test/CodeGenHLSL/matrix-array-layout.hlsl

diff --git a/clang/lib/CodeGen/CodeGenTypes.cpp 
b/clang/lib/CodeGen/CodeGenTypes.cpp
index 55fe216580314..d50e8aef7dcf7 100644
--- a/clang/lib/CodeGen/CodeGenTypes.cpp
+++ b/clang/lib/CodeGen/CodeGenTypes.cpp
@@ -121,6 +121,19 @@ llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
     return llvm::ArrayType::get(IRElemTy, MT->getNumElementsFlattened());
   }
 
+  // Convert constant arrays element-wise so that sugar on the element type 
that
+  // affects the in-memory layout is preserved.
+  if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
+    llvm::Type *EltTy = ConvertTypeForMem(CAT->getElementType());
+    // Lower arrays of undefined struct type to arrays of i8, matching
+    // ConvertType.
+    if (!EltTy->isSized()) {
+      SkippedLayout = true;
+      EltTy = llvm::Type::getInt8Ty(getLLVMContext());
+    }
+    return llvm::ArrayType::get(EltTy, CAT->getZExtSize());
+  }
+
   llvm::Type *R = ConvertType(T);
 
   // Check for the boolean vector case.
diff --git a/clang/test/CodeGenHLSL/matrix-array-layout.hlsl 
b/clang/test/CodeGenHLSL/matrix-array-layout.hlsl
new file mode 100644
index 0000000000000..6e15c6b9e0953
--- /dev/null
+++ b/clang/test/CodeGenHLSL/matrix-array-layout.hlsl
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.4-library -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s
+
+// Regression test for the in-memory layout of arrays of matrices with explicit
+// row_major / column_major keywords.
+//
+// ConvertTypeForMem lays a matrix out as [ArrayLen x <VecLen>]:
+//   row_major    float2x3 -> [2 x <3 x float>]  (2 rows of 3 columns)
+//   column_major float2x3 -> [3 x <2 x float>]  (3 rows of 2 columns)
+//
+// The element of a matrix array must keep the same layout as the equivalent
+// bare matrix.
+
+export void f() {
+  row_major    float2x3 rm_arr[2];
+  column_major float2x3 cm_arr[2];
+  row_major    float2x3 rm_bare;
+  column_major float2x3 cm_bare;
+  rm_arr[0] = rm_bare;
+  cm_arr[0] = cm_bare;
+}
+
+// The array element layout matches the bare matrix layout for each 
orientation.
+// CHECK: %rm_arr = alloca [2 x [2 x <3 x float>]], align 4
+// CHECK: %cm_arr = alloca [2 x [3 x <2 x float>]], align 4
+// CHECK: %rm_bare = alloca [2 x <3 x float>], align 4
+// CHECK: %cm_bare = alloca [3 x <2 x float>], align 4

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

Reply via email to