[clang] [HLSL][Matrix] Add support for Matrix element and trunc Casts (PR #168915)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 25 09:50:53 PST 2025


================
@@ -2422,9 +2422,27 @@ static Value *EmitHLSLElementwiseCast(CodeGenFunction &CGF, LValue SrcVal,
     }
     return V;
   }
+  if (auto *MatTy = DestTy->getAs<ConstantMatrixType>()) {
+    assert(LoadList.size() >= MatTy->getNumElementsFlattened() &&
+           "Flattened type on RHS must have the same number or more elements "
+           "than vector on LHS.");
+    llvm::Value *V =
+        CGF.Builder.CreateLoad(CGF.CreateIRTemp(DestTy, "flatcast.tmp"));
+    // write to V.
+    for (unsigned I = 0, E = MatTy->getNumElementsFlattened(); I < E; I++) {
+      RValue RVal = CGF.EmitLoadOfLValue(LoadList[I], Loc);
----------------
farzonl wrote:

This code will just impact the elementwise casts not the tests you have,  but yeah This should just be a matter of chaging `CK_HLSLMatrixTruncation`  in `VisitCastExpr `to not do `getNumElementsFlattened()` and instead generate the index I.

https://github.com/llvm/llvm-project/pull/168915


More information about the cfe-commits mailing list