https://github.com/farzonl created https://github.com/llvm/llvm-project/pull/185346
fixes https://github.com/llvm/llvm-project/issues/184849 The fix is just to copy the data before a swizzle can happen >From a4af2ae560b273b925b839d74db4ef8ed196514e Mon Sep 17 00:00:00 2001 From: Farzon Lotfi <[email protected]> Date: Mon, 9 Mar 2026 00:44:58 -0400 Subject: [PATCH 1/2] [Matrix] Copy Row data from padded cbuffer offsets before swizzle fixes https://github.com/llvm/llvm-project/issues/184849 The fix is just to copy the data before a swizzle can happen --- clang/lib/CodeGen/CGExpr.cpp | 10 +++++++++- .../CBufferMatrixSingleSubscriptSwizzle.hlsl | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 clang/test/CodeGenHLSL/BasicFeatures/CBufferMatrixSingleSubscriptSwizzle.hlsl diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index eebb36276e0eb..6afb39f194de1 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -5135,8 +5135,16 @@ LValue CodeGenFunction::EmitMatrixSingleSubscriptExpr( const MatrixSingleSubscriptExpr *E) { LValue Base = EmitLValue(E->getBase()); llvm::Value *RowIdx = EmitMatrixIndexExpr(E->getRowIdx()); + + 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(Base.getAddress(), *this), RowIdx, + MaybeConvertMatrixAddress(MatAddr, *this), RowIdx, E->getBase()->getType(), Base.getBaseInfo(), TBAAAccessInfo()); } 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; +} >From e7facbfaf924efb7183da3327c9d33b67bb8a175 Mon Sep 17 00:00:00 2001 From: Farzon Lotfi <[email protected]> Date: Mon, 9 Mar 2026 00:47:54 -0400 Subject: [PATCH 2/2] run clang-format --- clang/lib/CodeGen/CGExpr.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 6afb39f194de1..bb6708569cb38 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -5139,13 +5139,12 @@ LValue CodeGenFunction::EmitMatrixSingleSubscriptExpr( RawAddress MatAddr = Base.getAddress(); if (getLangOpts().HLSL && E->getBase()->getType().getAddressSpace() == LangAS::hlsl_constant) - MatAddr = - CGM.getHLSLRuntime().createBufferMatrixTempAddress(Base, E->getExprLoc(), - *this); + MatAddr = CGM.getHLSLRuntime().createBufferMatrixTempAddress( + Base, E->getExprLoc(), *this); - return LValue::MakeMatrixRow( - MaybeConvertMatrixAddress(MatAddr, *this), RowIdx, - E->getBase()->getType(), Base.getBaseInfo(), TBAAAccessInfo()); + return LValue::MakeMatrixRow(MaybeConvertMatrixAddress(MatAddr, *this), + RowIdx, E->getBase()->getType(), + Base.getBaseInfo(), TBAAAccessInfo()); } LValue CodeGenFunction::EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E) { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
