[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 Dec 2 08:53:20 PST 2025
================
@@ -2954,15 +2976,47 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
llvm::Value *Zero = llvm::Constant::getNullValue(CGF.SizeTy);
return Builder.CreateExtractElement(Vec, Zero, "cast.vtrunc");
}
+ case CK_HLSLMatrixTruncation: {
+ assert((DestTy->isMatrixType() || DestTy->isBuiltinType()) &&
+ "Destination type must be a matrix or builtin type.");
+ Value *Mat = Visit(E);
+ if (auto *MatTy = DestTy->getAs<ConstantMatrixType>()) {
+ SmallVector<int> Mask;
+ unsigned NumCols = MatTy->getNumColumns();
+ unsigned NumRows = MatTy->getNumRows();
+ unsigned ColOffset = NumCols;
+ if (auto *SrcMatTy = E->getType()->getAs<ConstantMatrixType>())
+ ColOffset = SrcMatTy->getNumColumns();
+ for (unsigned R = 0; R < NumRows; R++) {
+ for (unsigned C = 0; C < NumCols; C++) {
+ unsigned I = R * ColOffset + C;
+ Mask.push_back(I);
+ }
+ }
+
+ return Builder.CreateShuffleVector(Mat, Mask, "trunc");
+ }
+ llvm::Value *Zero = llvm::Constant::getNullValue(CGF.SizeTy);
+ return Builder.CreateExtractElement(Mat, Zero, "cast.mtrunc");
----------------
farzonl wrote:
Yeah we only do ConstantMatrix for HLSL so I'll update the assert to `assert((DestTy->isConstantMatrixType()....);` and then drop the if statement for ` auto *MatTy = DestTy->getAs<ConstantMatrixType>();` That will let me have one return for the case block.
https://github.com/llvm/llvm-project/pull/168915
More information about the cfe-commits
mailing list