Skip to content

Commit dd1b4ab

Browse files
authored
[HLSL][Matrix] Add support for Matrix element and trunc Casts (#168915)
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 45918f5 commit dd1b4ab

27 files changed

+1084
-23
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/Basic/DiagnosticGroups.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@ def SuperSubClassMismatch : DiagGroup<"super-class-method-mismatch">;
10611061
def OverridingMethodMismatch : DiagGroup<"overriding-method-mismatch">;
10621062
def VariadicMacros : DiagGroup<"variadic-macros">;
10631063
def VectorConversion : DiagGroup<"vector-conversion">; // clang specific
1064+
def MatrixConversion : DiagGroup<"matrix-conversion">; // clang specific
10641065
def VexingParse : DiagGroup<"vexing-parse">;
10651066
def VLAUseStaticAssert : DiagGroup<"vla-extension-static-assert">;
10661067
def VLACxxExtension : DiagGroup<"vla-cxx-extension", [VLAUseStaticAssert]>;
@@ -1335,6 +1336,8 @@ def : DiagGroup<"int-conversions",
13351336
[IntConversion]>; // -Wint-conversions = -Wint-conversion
13361337
def : DiagGroup<"vector-conversions",
13371338
[VectorConversion]>; // -Wvector-conversions = -Wvector-conversion
1339+
def : DiagGroup<"matrix-conversions",
1340+
[MatrixConversion]>; // -Wmatrix-conversions = -Wmatrix-conversion
13381341
def : DiagGroup<"unused-local-typedefs", [UnusedLocalTypedef]>;
13391342
// -Wunused-local-typedefs = -Wunused-local-typedef
13401343

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4356,6 +4356,9 @@ def warn_param_typestate_mismatch : Warning<
43564356
def warn_unknown_sanitizer_ignored : Warning<
43574357
"unknown sanitizer '%0' ignored">, InGroup<UnknownSanitizers>;
43584358

4359+
def warn_impcast_matrix_scalar : Warning<
4360+
"implicit conversion turns matrix to scalar: %0 to %1">,
4361+
InGroup<MatrixConversion>;
43594362
def warn_impcast_vector_scalar : Warning<
43604363
"implicit conversion turns vector to scalar: %0 to %1">,
43614364
InGroup<Conversion>, DefaultIgnore;
@@ -13282,7 +13285,10 @@ def err_hlsl_builtin_scalar_vector_mismatch
1328213285
"vector type with matching scalar element type%diff{: $ vs $|}2,3">;
1328313286

1328413287
def warn_hlsl_impcast_vector_truncation : Warning<
13285-
"implicit conversion truncates vector: %0 to %1">, InGroup<Conversion>;
13288+
"implicit conversion truncates vector: %0 to %1">, InGroup<VectorConversion>;
13289+
13290+
def warn_hlsl_impcast_matrix_truncation : Warning<
13291+
"implicit conversion truncates matrix: %0 to %1">, InGroup<MatrixConversion>;
1328613292

1328713293
def warn_hlsl_availability : Warning<
1328813294
"%0 is only available %select{|in %4 environment }3on %1 %2 or newer">,

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 Matrix 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
@@ -11770,6 +11770,10 @@ bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {
1177011770
Elements.push_back(Val.getVectorElt(I));
1177111771
return Success(Elements, E);
1177211772
}
11773+
case CK_HLSLMatrixTruncation: {
11774+
// TODO: See #168935. Add matrix truncation support to expr constant.
11775+
return Error(E);
11776+
}
1177311777
case CK_HLSLAggregateSplatCast: {
1177411778
APValue Val;
1177511779
QualType ValTy;
@@ -18440,6 +18444,10 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1844018444
return Error(E);
1844118445
return Success(Val.getVectorElt(0), E);
1844218446
}
18447+
case CK_HLSLMatrixTruncation: {
18448+
// TODO: See #168935. Add matrix truncation support to expr constant.
18449+
return Error(E);
18450+
}
1844318451
case CK_HLSLElementwiseCast: {
1844418452
SmallVector<APValue> SrcVals;
1844518453
SmallVector<QualType> SrcTypes;
@@ -19033,6 +19041,10 @@ bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
1903319041
return Error(E);
1903419042
return Success(Val.getVectorElt(0), E);
1903519043
}
19044+
case CK_HLSLMatrixTruncation: {
19045+
// TODO: See #168935. Add matrix truncation support to expr constant.
19046+
return Error(E);
19047+
}
1903619048
case CK_HLSLElementwiseCast: {
1903719049
SmallVector<APValue> SrcVals;
1903819050
SmallVector<QualType> SrcTypes;
@@ -19190,6 +19202,7 @@ bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
1919019202
case CK_IntegralToFixedPoint:
1919119203
case CK_MatrixCast:
1919219204
case CK_HLSLVectorTruncation:
19205+
case CK_HLSLMatrixTruncation:
1919319206
case CK_HLSLElementwiseCast:
1919419207
case CK_HLSLAggregateSplatCast:
1919519208
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:
@@ -1323,6 +1324,7 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *e) {
13231324
case CK_IntegralToFixedPoint:
13241325
case CK_MatrixCast:
13251326
case CK_HLSLVectorTruncation:
1327+
case CK_HLSLMatrixTruncation:
13261328
case CK_HLSLArrayRValue:
13271329
case CK_HLSLElementwiseCast:
13281330
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
@@ -5798,6 +5798,7 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
57985798
case CK_IntegralToFixedPoint:
57995799
case CK_MatrixCast:
58005800
case CK_HLSLVectorTruncation:
5801+
case CK_HLSLMatrixTruncation:
58015802
case CK_HLSLArrayRValue:
58025803
case CK_HLSLElementwiseCast:
58035804
case CK_HLSLAggregateSplatCast:

0 commit comments

Comments
 (0)