[clang] [Clang][HLSL] Emit constant matrix values in column-major order (PR #227356)
Deric C. via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 29 10:51:18 PDT 2026
================
@@ -2087,6 +2087,17 @@ llvm::Constant *ConstantEmitter::emitForMemory(CodeGenModule &CGM,
}
}
+ if (destType->isConstantMatrixType() &&
+ isMatrixRowMajor(CGM.getLangOpts(), destType)) {
+ const auto *MT = destType->castAs<ConstantMatrixType>();
+ SmallVector<llvm::Constant *, 16> Inits(MT->getNumElementsFlattened());
+ for (unsigned Row = 0; Row != MT->getNumRows(); ++Row)
+ for (unsigned Col = 0; Col != MT->getNumColumns(); ++Col)
+ Inits[MT->getRowMajorFlattenedIndex(Row, Col)] =
+ C->getAggregateElement(MT->getColumnMajorFlattenedIndex(Row, Col));
+ return llvm::ConstantVector::get(Inits);
----------------
Icohedron wrote:
You need to convert bools for memory.
```suggestion
llvm::Constant *MemoryValue = llvm::ConstantVector::get(Inits);
if (destType->isConstantMatrixBoolType())
return llvm::ConstantExpr::getZExt(
MemoryValue, CGM.getTypes().convertTypeForLoadStore(destType));
return MemoryValue;
```
Example lit test to exercise it:
```hlsl
// RUN: %clang_cc1 -std=hlsl202x -triple dxil-pc-shadermodel6.3-library \
// RUN: -emit-llvm -disable-llvm-passes -finclude-default-header -o - %s \
// RUN: | FileCheck %s
// @_ZL1B is the LLVM global for B. Its initializer must contain six i32
// elements: row 0 is (1, 0, 1), followed by row 1 (0, 1, 0).
// CHECK: @_ZL1B = internal global <6 x i32> <i32 1, i32 0, i32 1, i32 0, i32 1, i32 0>
static row_major bool2x3 B = bool2x3(true, false, true, false, true, false);
// Reading B loads its row-major memory representation, then transposes it
// into column-major register order. B[1][1] is register element 1 + 1*2 = 3.
export bool read_bool_matrix() {
// CHECK-LABEL: define {{.*}} i1 @_Z16read_bool_matrixv
// CHECK: [[FROM_MEMORY:%.*]] = load <6 x i32>, ptr @_ZL1B
// CHECK: [[IN_REGISTER:%.*]] = call <6 x i32> @llvm.matrix.transpose.v6i32(<6 x i32> [[FROM_MEMORY]], i32 3, i32 2)
// CHECK: extractelement <6 x i32> [[IN_REGISTER]], i32 3
return B[1][1];
}
```
https://github.com/llvm/llvm-project/pull/227356
More information about the cfe-commits
mailing list