[clang] [Matrix] Copy Row data from padded cbuffer offsets before swizzle (PR #185346)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 8 21:50:27 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Farzon Lotfi (farzonl)
<details>
<summary>Changes</summary>
fixes https://github.com/llvm/llvm-project/issues/184849
The fix is just to copy the data before a swizzle can happen
---
Full diff: https://github.com/llvm/llvm-project/pull/185346.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/CGExpr.cpp (+10-3)
- (added) clang/test/CodeGenHLSL/BasicFeatures/CBufferMatrixSingleSubscriptSwizzle.hlsl (+19)
``````````diff
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index eebb36276e0eb..bb6708569cb38 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -5135,9 +5135,16 @@ LValue CodeGenFunction::EmitMatrixSingleSubscriptExpr(
const MatrixSingleSubscriptExpr *E) {
LValue Base = EmitLValue(E->getBase());
llvm::Value *RowIdx = EmitMatrixIndexExpr(E->getRowIdx());
- return LValue::MakeMatrixRow(
- MaybeConvertMatrixAddress(Base.getAddress(), *this), RowIdx,
- E->getBase()->getType(), Base.getBaseInfo(), TBAAAccessInfo());
+
+ RawAddress MatAddr = Base.getAddress();
+ if (getLangOpts().HLSL &&
+ E->getBase()->getType().getAddressSpace() == LangAS::hlsl_constant)
+ MatAddr = CGM.getHLSLRuntime().createBufferMatrixTempAddress(
+ Base, E->getExprLoc(), *this);
+
+ return LValue::MakeMatrixRow(MaybeConvertMatrixAddress(MatAddr, *this),
+ RowIdx, E->getBase()->getType(),
+ Base.getBaseInfo(), TBAAAccessInfo());
}
LValue CodeGenFunction::EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E) {
diff --git a/clang/test/CodeGenHLSL/BasicFeatures/CBufferMatrixSingleSubscriptSwizzle.hlsl b/clang/test/CodeGenHLSL/BasicFeatures/CBufferMatrixSingleSubscriptSwizzle.hlsl
new file mode 100644
index 0000000000000..46d51725827a5
--- /dev/null
+++ b/clang/test/CodeGenHLSL/BasicFeatures/CBufferMatrixSingleSubscriptSwizzle.hlsl
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.7-library -disable-llvm-passes -emit-llvm -finclude-default-header -fmatrix-memory-layout=row-major -o - %s | FileCheck %s
+
+cbuffer CB {
+ float3x2 Mat;
+}
+
+// CHECK: @Mat = external hidden addrspace(2) global <{ [2 x <{ <2 x float>, target("dx.Padding", 8) }>], <2 x float> }>, align 4
+
+// CHECK-LABEL: define hidden noundef nofpclass(nan inf) <2 x float> @_Z15get_row_swizzlev()
+// CHECK: %matrix.buf.copy = alloca [3 x <2 x float>], align 8
+// CHECK: %cbuf.load = load <2 x float>, ptr addrspace(2) @Mat, align 8
+// CHECK: %cbuf.load2 = load <2 x float>, ptr addrspace(2) getelementptr inbounds nuw (i8, ptr addrspace(2) @Mat, i32 16), align 8
+// CHECK: %cbuf.load4 = load <2 x float>, ptr addrspace(2) getelementptr inbounds nuw (i8, ptr addrspace(2) @Mat, i32 32), align 8
+// CHECK: %0 = load <6 x float>, ptr %matrix.buf.copy, align 8
+// CHECK: %1 = shufflevector <6 x float> %0, <6 x float> poison, <2 x i32> <i32 4, i32 1>
+
+float2 get_row_swizzle() {
+ return Mat[1].gr;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/185346
More information about the cfe-commits
mailing list