Skip to content

Commit 068d85c

Browse files
committed
[HLSL][Matrix] Add support for Matrix element and trunc Casts
fixes #168737 fixes #168755 This change fixes adds support for Matrix truncations via the ICK_HLSL_Matrix_Truncation enum. That ends up being most of the files changed. It also allows Matrix as an HLSL Elementwise cast as long as the cast does not perform a shape transformation ie 3x2 to 2x3. Tests for the new elementwise and truncation behavior were added. As well as sema tests to make sure we error n the shape transformation cast. I am punting right now on the ConstExpr Matrix support. That will need to be addressed later. Will file a seperate issue for that if reviewers agree it can wait.
1 parent 1262acf commit 068d85c

File tree

19 files changed

+516
-15
lines changed

19 files changed

+516
-15
lines changed

clang/include/clang/AST/OperationKinds.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@ CAST_OPERATION(IntToOCLSampler)
364364
// Truncate a vector type by dropping elements from the end (HLSL only).
365365
CAST_OPERATION(HLSLVectorTruncation)
366366

367+
// Truncate a matrix type by dropping elements from the end (HLSL only).
368+
CAST_OPERATION(HLSLMatrixTruncation)
369+
367370
// Non-decaying array RValue cast (HLSL only).
368371
CAST_OPERATION(HLSLArrayRValue)
369372

clang/include/clang/Sema/Overload.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ class Sema;
198198
/// HLSL vector truncation.
199199
ICK_HLSL_Vector_Truncation,
200200

201+
/// HLSL Matrid truncation.
202+
ICK_HLSL_Matrix_Truncation,
203+
201204
/// HLSL non-decaying array rvalue cast.
202205
ICK_HLSL_Array_RValue,
203206

clang/lib/AST/Expr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,7 @@ bool CastExpr::CastConsistency() const {
19371937
case CK_FixedPointToBoolean:
19381938
case CK_HLSLArrayRValue:
19391939
case CK_HLSLVectorTruncation:
1940+
case CK_HLSLMatrixTruncation:
19401941
case CK_HLSLElementwiseCast:
19411942
case CK_HLSLAggregateSplatCast:
19421943
CheckNoBasePath:

clang/lib/AST/ExprConstant.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11773,6 +11773,10 @@ bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {
1177311773
Elements.push_back(Val.getVectorElt(I));
1177411774
return Success(Elements, E);
1177511775
}
11776+
case CK_HLSLMatrixTruncation: {
11777+
// TODO: support Expr Constant for Matrix Truncation
11778+
return Error(E);
11779+
}
1177611780
case CK_HLSLAggregateSplatCast: {
1177711781
APValue Val;
1177811782
QualType ValTy;
@@ -18011,6 +18015,10 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1801118015
return Error(E);
1801218016
return Success(Val.getVectorElt(0), E);
1801318017
}
18018+
case CK_HLSLMatrixTruncation: {
18019+
// TODO: support Expr Constant for Matrix Truncation
18020+
return Error(E);
18021+
}
1801418022
case CK_HLSLElementwiseCast: {
1801518023
SmallVector<APValue> SrcVals;
1801618024
SmallVector<QualType> SrcTypes;
@@ -18604,6 +18612,10 @@ bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
1860418612
return Error(E);
1860518613
return Success(Val.getVectorElt(0), E);
1860618614
}
18615+
case CK_HLSLMatrixTruncation: {
18616+
// TODO: support Expr Constant for Matrix Truncation
18617+
return Error(E);
18618+
}
1860718619
case CK_HLSLElementwiseCast: {
1860818620
SmallVector<APValue> SrcVals;
1860918621
SmallVector<QualType> SrcTypes;
@@ -18761,6 +18773,7 @@ bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
1876118773
case CK_IntegralToFixedPoint:
1876218774
case CK_MatrixCast:
1876318775
case CK_HLSLVectorTruncation:
18776+
case CK_HLSLMatrixTruncation:
1876418777
case CK_HLSLElementwiseCast:
1876518778
case CK_HLSLAggregateSplatCast:
1876618779
llvm_unreachable("invalid cast kind for complex value");

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ Address CIRGenFunction::emitPointerWithAlignment(const Expr *expr,
188188
case CK_HLSLArrayRValue:
189189
case CK_HLSLElementwiseCast:
190190
case CK_HLSLVectorTruncation:
191+
case CK_HLSLMatrixTruncation:
191192
case CK_IntToOCLSampler:
192193
case CK_IntegralCast:
193194
case CK_IntegralComplexCast:
@@ -1279,6 +1280,7 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *e) {
12791280
case CK_IntegralToFixedPoint:
12801281
case CK_MatrixCast:
12811282
case CK_HLSLVectorTruncation:
1283+
case CK_HLSLMatrixTruncation:
12821284
case CK_HLSLArrayRValue:
12831285
case CK_HLSLElementwiseCast:
12841286
case CK_HLSLAggregateSplatCast:

clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ mlir::Value ComplexExprEmitter::emitCast(CastKind ck, Expr *op,
534534
case CK_IntegralToFixedPoint:
535535
case CK_MatrixCast:
536536
case CK_HLSLVectorTruncation:
537+
case CK_HLSLMatrixTruncation:
537538
case CK_HLSLArrayRValue:
538539
case CK_HLSLElementwiseCast:
539540
case CK_HLSLAggregateSplatCast:

clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,7 @@ class ConstExprEmitter
10121012
case CK_MatrixCast:
10131013
case CK_HLSLArrayRValue:
10141014
case CK_HLSLVectorTruncation:
1015+
case CK_HLSLMatrixTruncation:
10151016
case CK_HLSLElementwiseCast:
10161017
case CK_HLSLAggregateSplatCast:
10171018
return {};

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5744,6 +5744,7 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
57445744
case CK_IntegralToFixedPoint:
57455745
case CK_MatrixCast:
57465746
case CK_HLSLVectorTruncation:
5747+
case CK_HLSLMatrixTruncation:
57475748
case CK_HLSLArrayRValue:
57485749
case CK_HLSLElementwiseCast:
57495750
case CK_HLSLAggregateSplatCast:

clang/lib/CodeGen/CGExprAgg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) {
10361036
case CK_ZeroToOCLOpaqueType:
10371037
case CK_MatrixCast:
10381038
case CK_HLSLVectorTruncation:
1039-
1039+
case CK_HLSLMatrixTruncation:
10401040
case CK_IntToOCLSampler:
10411041
case CK_FloatingToFixedPoint:
10421042
case CK_FixedPointToFloating:
@@ -1550,6 +1550,7 @@ static bool castPreservesZero(const CastExpr *CE) {
15501550
case CK_NonAtomicToAtomic:
15511551
case CK_AtomicToNonAtomic:
15521552
case CK_HLSLVectorTruncation:
1553+
case CK_HLSLMatrixTruncation:
15531554
case CK_HLSLElementwiseCast:
15541555
case CK_HLSLAggregateSplatCast:
15551556
return true;

clang/lib/CodeGen/CGExprComplex.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
621621
case CK_IntegralToFixedPoint:
622622
case CK_MatrixCast:
623623
case CK_HLSLVectorTruncation:
624+
case CK_HLSLMatrixTruncation:
624625
case CK_HLSLArrayRValue:
625626
case CK_HLSLElementwiseCast:
626627
case CK_HLSLAggregateSplatCast:

0 commit comments

Comments
 (0)