[clang] [HLSL][Matrix] Make HLSLElementwiseCast respect matrix memory layout (PR #184429)
Farzon Lotfi via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 3 12:49:09 PST 2026
================
@@ -2564,20 +2564,24 @@ static Value *EmitHLSLElementwiseCast(CodeGenFunction &CGF, LValue SrcVal,
"Flattened type on RHS must have the same number or more elements "
"than vector on LHS.");
+ bool IsRowMajor = CGF.getLangOpts().getDefaultMatrixMemoryLayout() ==
+ LangOptions::MatrixMemoryLayout::MatrixRowMajor;
+
llvm::Value *V = CGF.Builder.CreateLoad(
CGF.CreateIRTempWithoutCast(DestTy, "flatcast.tmp"));
- // V is an allocated temporary to build the truncated matrix into.
- for (unsigned I = 0, E = MatTy->getNumElementsFlattened(); I < E; I++) {
- unsigned ColMajorIndex =
- (I % MatTy->getNumRows()) * MatTy->getNumColumns() +
- (I / MatTy->getNumRows());
- RValue RVal = CGF.EmitLoadOfLValue(LoadList[ColMajorIndex], Loc);
- assert(RVal.isScalar() &&
- "All flattened source values should be scalars.");
- llvm::Value *Cast = CGF.EmitScalarConversion(
- RVal.getScalarVal(), LoadList[ColMajorIndex].getType(),
- MatTy->getElementType(), Loc);
- V = CGF.Builder.CreateInsertElement(V, Cast, I);
+ // V is an allocated temporary for constructing the matrix.
+ for (unsigned Row = 0, RE = MatTy->getNumRows(); Row < RE; Row++) {
+ for (unsigned Col = 0, CE = MatTy->getNumColumns(); Col < CE; Col++) {
+ unsigned LoadIdx = MatTy->getRowMajorFlattenedIndex(Row, Col);
----------------
farzonl wrote:
You need a comment for why `LoadIdx` and `MatrixIdx` are different now.
https://github.com/llvm/llvm-project/pull/184429
More information about the cfe-commits
mailing list