Skip to content

Commit 93183bc

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 7b5163d commit 93183bc

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
@@ -1934,6 +1934,7 @@ bool CastExpr::CastConsistency() const {
19341934
case CK_FixedPointToBoolean:
19351935
case CK_HLSLArrayRValue:
19361936
case CK_HLSLVectorTruncation:
1937+
case CK_HLSLMatrixTruncation:
19371938
case CK_HLSLElementwiseCast:
19381939
case CK_HLSLAggregateSplatCast:
19391940
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;
@@ -18163,6 +18167,10 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1816318167
return Error(E);
1816418168
return Success(Val.getVectorElt(0), E);
1816518169
}
18170+
case CK_HLSLMatrixTruncation: {
18171+
// TODO: support Expr Constant for Matrix Truncation
18172+
return Error(E);
18173+
}
1816618174
case CK_HLSLElementwiseCast: {
1816718175
SmallVector<APValue> SrcVals;
1816818176
SmallVector<QualType> SrcTypes;
@@ -18756,6 +18764,10 @@ bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
1875618764
return Error(E);
1875718765
return Success(Val.getVectorElt(0), E);
1875818766
}
18767+
case CK_HLSLMatrixTruncation: {
18768+
// TODO: support Expr Constant for Matrix Truncation
18769+
return Error(E);
18770+
}
1875918771
case CK_HLSLElementwiseCast: {
1876018772
SmallVector<APValue> SrcVals;
1876118773
SmallVector<QualType> SrcTypes;
@@ -18913,6 +18925,7 @@ bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
1891318925
case CK_IntegralToFixedPoint:
1891418926
case CK_MatrixCast:
1891518927
case CK_HLSLVectorTruncation:
18928+
case CK_HLSLMatrixTruncation:
1891618929
case CK_HLSLElementwiseCast:
1891718930
case CK_HLSLAggregateSplatCast:
1891818931
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:
@@ -1290,6 +1291,7 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *e) {
12901291
case CK_IntegralToFixedPoint:
12911292
case CK_MatrixCast:
12921293
case CK_HLSLVectorTruncation:
1294+
case CK_HLSLMatrixTruncation:
12931295
case CK_HLSLArrayRValue:
12941296
case CK_HLSLElementwiseCast:
12951297
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
@@ -5734,6 +5734,7 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
57345734
case CK_IntegralToFixedPoint:
57355735
case CK_MatrixCast:
57365736
case CK_HLSLVectorTruncation:
5737+
case CK_HLSLMatrixTruncation:
57375738
case CK_HLSLArrayRValue:
57385739
case CK_HLSLElementwiseCast:
57395740
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)