Skip to content

[Matrix][HLSL] Add support for Matrix as an HLSL Elementwise cast #168737

@farzonl

Description

@farzonl

This case needs to be a part of the tests

export int3x2 fn_helper(float2x3 f23) {
    int3x2 i32 = (int3x2)f23;
    return i32;
}
<source>:3:18: error: cannot convert from 'float2x3' to 'int3x2'
    int3x2 i32 = (int3x2)f23;

https://godbolt.org/z/anTMGGGc9

Originally posted by @farzonl in #162711

CanPerformElementwiseCast in clang/lib/Sema/SemaHLSL.cpp defaults to true. Meaning right now we are erroneously makring all matrix casts as elementwise cast. Now that we have a new type in ConstantMantrix we need to check for this inside of CanPerformElementwiseCast.

The function itself notes it needs matrix support

// TODO: update this code when matrices are added; see issue #88060
bool SemaHLSL::CanPerformElementwiseCast(Expr *Src, QualType DestTy) {

Will want to confirm with @spall that the below fix is thr right one.

diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 5555916c2536..d790bd83659b 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -3728,7 +3728,6 @@ bool SemaHLSL::CanPerformAggregateSplatCast(Expr *Src, QualType DestTy) {
 }
 
 // Can we perform an HLSL Elementwise cast?
-// TODO: update this code when matrices are added; see issue #88060
 bool SemaHLSL::CanPerformElementwiseCast(Expr *Src, QualType DestTy) {
 
   // Don't handle casts where LHS and RHS are any combination of scalar/vector
@@ -3740,6 +3739,10 @@ bool SemaHLSL::CanPerformElementwiseCast(Expr *Src, QualType DestTy) {
   if (SrcTy->isVectorType() &&
       (DestTy->isScalarType() || DestTy->isVectorType()))
     return false;
+  
+  if (SrcTy->isConstantMatrixType() &&
+      (DestTy->isScalarType() || DestTy->isConstantMatrixType()))
+    return false;
 
   llvm::SmallVector<QualType> DestTypes;
   BuildFlattenedTypeList(DestTy, DestTypes);

Metadata

Metadata

Assignees

Labels

HLSLHLSL Language Supportclang:frontendLanguage frontend issues, e.g. anything involving "Sema"

Type

Projects

Status

Active

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions