[clang] [HLSL][Matrix] Add support for Matrix element and trunc Casts (PR #168915)
Chris B via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 20 10:30:49 PST 2025
================
@@ -2088,6 +2091,59 @@ static bool IsVectorElementConversion(Sema &S, QualType FromType,
return false;
}
+/// Determine whether the conversion from FromType to ToType is a valid
+/// matrix conversion.
+///
+/// \param ICK Will be set to the matrix conversion kind, if this is a matrix
+/// conversion.
+static bool IsMatrixConversion(Sema &S, QualType FromType, QualType ToType,
+ ImplicitConversionKind &ICK,
+ ImplicitConversionKind &ElConv, Expr *From,
+ bool InOverloadResolution, bool CStyle) {
+ // The non HLSL Matrix conversion rules are not clear.
+ if (!S.getLangOpts().HLSL)
+ return false;
+
+ auto *ToMatrixType = ToType->getAs<ConstantMatrixType>();
+ auto *FromMatrixType = FromType->getAs<ConstantMatrixType>();
+
+ // If both arguments are vectors, handle possible vector truncation and
+ // element conversion.
+ if (ToMatrixType && FromMatrixType) {
+ unsigned FromCols = FromMatrixType->getNumColumns();
+ unsigned ToCols = ToMatrixType->getNumColumns();
+ if (FromCols < ToCols)
+ return false;
+
+ unsigned FromRows = FromMatrixType->getNumRows();
+ unsigned ToRows = ToMatrixType->getNumRows();
+ if (FromRows < ToRows)
+ return false;
+
+ unsigned FromElts = FromMatrixType->getNumElementsFlattened();
+ unsigned ToElts = ToMatrixType->getNumElementsFlattened();
+ if (FromElts == ToElts)
----------------
llvm-beanz wrote:
This is correct, but I think it would be clearer if you wrote it as:
```suggestion
if (FromRows == ToRows && FromCols == ToCols)
```
Writing the condition like this stands alone (if the dimensions are the same), whereas the way you wrote it you need to realize that the code has already ruled out dimension extensions, so the only way the total number of elements can match is if the dimensions are the same.
https://github.com/llvm/llvm-project/pull/168915
More information about the cfe-commits
mailing list