Skip to content

Commit 180a9ba

Browse files
committed
[HLSL][Matrix] Add Matrix Swizzle AST Node
fixes #159438
1 parent 2ab198f commit 180a9ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2600
-40
lines changed

clang/include/clang/AST/ComputeDependence.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ArrayInitLoopExpr;
4444
class ImplicitValueInitExpr;
4545
class InitListExpr;
4646
class ExtVectorElementExpr;
47+
class MatrixElementExpr;
4748
class BlockExpr;
4849
class AsTypeExpr;
4950
class DeclRefExpr;
@@ -133,6 +134,7 @@ ExprDependence computeDependence(ArrayInitLoopExpr *E);
133134
ExprDependence computeDependence(ImplicitValueInitExpr *E);
134135
ExprDependence computeDependence(InitListExpr *E);
135136
ExprDependence computeDependence(ExtVectorElementExpr *E);
137+
ExprDependence computeDependence(MatrixElementExpr *E);
136138
ExprDependence computeDependence(BlockExpr *E,
137139
bool ContainsUnexpandedParameterPack);
138140
ExprDependence computeDependence(AsTypeExpr *E);

clang/include/clang/AST/Expr.h

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ class Expr : public ValueStmt {
291291
LV_NotObjectType,
292292
LV_IncompleteVoidType,
293293
LV_DuplicateVectorComponents,
294+
LV_DuplicateMatrixComponents,
294295
LV_InvalidExpression,
295296
LV_InvalidMessageExpression,
296297
LV_MemberFunction,
@@ -306,6 +307,7 @@ class Expr : public ValueStmt {
306307
MLV_NotObjectType,
307308
MLV_IncompleteVoidType,
308309
MLV_DuplicateVectorComponents,
310+
MLV_DuplicateMatrixComponents,
309311
MLV_InvalidExpression,
310312
MLV_LValueCast, // Specialized form of MLV_InvalidExpression.
311313
MLV_IncompleteType,
@@ -344,6 +346,7 @@ class Expr : public ValueStmt {
344346
CL_Void, // Void cannot be an lvalue in C.
345347
CL_AddressableVoid, // Void expression whose address can be taken in C.
346348
CL_DuplicateVectorComponents, // A vector shuffle with dupes.
349+
CL_DuplicateMatrixComponents, // A matrix shuffle with dupes.
347350
CL_MemberFunction, // An expression referring to a member function
348351
CL_SubObjCPropertySetting,
349352
CL_ClassTemporary, // A temporary of class type, or subobject thereof.
@@ -6488,30 +6491,24 @@ class GenericSelectionExpr final
64886491
// Clang Extensions
64896492
//===----------------------------------------------------------------------===//
64906493

6491-
/// ExtVectorElementExpr - This represents access to specific elements of a
6492-
/// vector, and may occur on the left hand side or right hand side. For example
6493-
/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector.
6494-
///
6495-
/// Note that the base may have either vector or pointer to vector type, just
6496-
/// like a struct field reference.
6497-
///
6498-
class ExtVectorElementExpr : public Expr {
6494+
template <class Derived> class ElementAccessExprBase : public Expr {
6495+
protected:
64996496
Stmt *Base;
65006497
IdentifierInfo *Accessor;
65016498
SourceLocation AccessorLoc;
6502-
public:
6503-
ExtVectorElementExpr(QualType ty, ExprValueKind VK, Expr *base,
6504-
IdentifierInfo &accessor, SourceLocation loc)
6505-
: Expr(ExtVectorElementExprClass, ty, VK,
6506-
(VK == VK_PRValue ? OK_Ordinary : OK_VectorComponent)),
6507-
Base(base), Accessor(&accessor), AccessorLoc(loc) {
6508-
setDependence(computeDependence(this));
6499+
6500+
ElementAccessExprBase(StmtClass SC, QualType Ty, ExprValueKind VK, Expr *Base,
6501+
IdentifierInfo &Accessor, SourceLocation Loc,
6502+
ExprObjectKind OK)
6503+
: Expr(SC, Ty, VK, OK), Base(Base), Accessor(&Accessor),
6504+
AccessorLoc(Loc) {
6505+
setDependence(computeDependence(static_cast<Derived *>(this)));
65096506
}
65106507

6511-
/// Build an empty vector element expression.
6512-
explicit ExtVectorElementExpr(EmptyShell Empty)
6513-
: Expr(ExtVectorElementExprClass, Empty) { }
6508+
explicit ElementAccessExprBase(StmtClass SC, EmptyShell Empty)
6509+
: Expr(SC, Empty) {}
65146510

6511+
public:
65156512
const Expr *getBase() const { return cast<Expr>(Base); }
65166513
Expr *getBase() { return cast<Expr>(Base); }
65176514
void setBase(Expr *E) { Base = E; }
@@ -6522,34 +6519,71 @@ class ExtVectorElementExpr : public Expr {
65226519
SourceLocation getAccessorLoc() const { return AccessorLoc; }
65236520
void setAccessorLoc(SourceLocation L) { AccessorLoc = L; }
65246521

6525-
/// getNumElements - Get the number of components being selected.
6526-
unsigned getNumElements() const;
6527-
6528-
/// containsDuplicateElements - Return true if any element access is
6529-
/// repeated.
6530-
bool containsDuplicateElements() const;
6531-
6532-
/// getEncodedElementAccess - Encode the elements accessed into an llvm
6533-
/// aggregate Constant of ConstantInt(s).
6534-
void getEncodedElementAccess(SmallVectorImpl<uint32_t> &Elts) const;
6535-
65366522
SourceLocation getBeginLoc() const LLVM_READONLY {
65376523
return getBase()->getBeginLoc();
65386524
}
65396525
SourceLocation getEndLoc() const LLVM_READONLY { return AccessorLoc; }
65406526

6527+
/*static bool classof(const Stmt *T) {
6528+
return T->getStmtClass() == ExtVectorElementExprClass ||
6529+
T->getStmtClass() == MatrixElementExprClass;
6530+
}*/
6531+
6532+
child_range children() { return child_range(&Base, &Base + 1); }
6533+
const_child_range children() const {
6534+
return const_child_range(&Base, &Base + 1);
6535+
}
6536+
};
6537+
6538+
/// ExtVectorElementExpr - This represents access to specific elements of a
6539+
/// vector, and may occur on the left hand side or right hand side. For example
6540+
/// the following is legal: "V.xy = V.zw" if V is a 4 element extended vector.
6541+
///
6542+
/// Note that the base may have either vector or pointer to vector type, just
6543+
/// like a struct field reference.
6544+
///
6545+
class ExtVectorElementExpr
6546+
: public ElementAccessExprBase<ExtVectorElementExpr> {
6547+
public:
6548+
ExtVectorElementExpr(QualType Ty, ExprValueKind VK, Expr *Base,
6549+
IdentifierInfo &Accessor, SourceLocation Loc)
6550+
: ElementAccessExprBase(
6551+
ExtVectorElementExprClass, Ty, VK, Base, Accessor, Loc,
6552+
(VK == VK_PRValue ? OK_Ordinary : OK_VectorComponent)) {}
6553+
6554+
explicit ExtVectorElementExpr(EmptyShell Empty)
6555+
: ElementAccessExprBase(ExtVectorElementExprClass, Empty) {}
6556+
6557+
unsigned getNumElements() const;
6558+
bool containsDuplicateElements() const;
6559+
void getEncodedElementAccess(SmallVectorImpl<uint32_t> &Elts) const;
6560+
65416561
/// isArrow - Return true if the base expression is a pointer to vector,
65426562
/// return false if the base expression is a vector.
65436563
bool isArrow() const;
65446564

65456565
static bool classof(const Stmt *T) {
65466566
return T->getStmtClass() == ExtVectorElementExprClass;
65476567
}
6568+
};
65486569

6549-
// Iterators
6550-
child_range children() { return child_range(&Base, &Base+1); }
6551-
const_child_range children() const {
6552-
return const_child_range(&Base, &Base + 1);
6570+
class MatrixElementExpr : public ElementAccessExprBase<MatrixElementExpr> {
6571+
public:
6572+
MatrixElementExpr(QualType Ty, ExprValueKind VK, Expr *Base,
6573+
IdentifierInfo &Accessor, SourceLocation Loc)
6574+
: ElementAccessExprBase(
6575+
MatrixElementExprClass, Ty, VK, Base, Accessor, Loc,
6576+
OK_Ordinary /*TODO: Should we add a new OK_MatrixComponent?*/) {}
6577+
6578+
explicit MatrixElementExpr(EmptyShell Empty)
6579+
: ElementAccessExprBase(MatrixElementExprClass, Empty) {}
6580+
6581+
unsigned getNumElements() const;
6582+
bool containsDuplicateElements() const;
6583+
void getEncodedElementAccess(SmallVectorImpl<uint32_t> &Elts) const;
6584+
6585+
static bool classof(const Stmt *T) {
6586+
return T->getStmtClass() == MatrixElementExprClass;
65536587
}
65546588
};
65556589

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2940,6 +2940,7 @@ DEF_TRAVERSE_STMT(UserDefinedLiteral, {})
29402940
DEF_TRAVERSE_STMT(DesignatedInitExpr, {})
29412941
DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {})
29422942
DEF_TRAVERSE_STMT(ExtVectorElementExpr, {})
2943+
DEF_TRAVERSE_STMT(MatrixElementExpr, {})
29432944
DEF_TRAVERSE_STMT(GNUNullExpr, {})
29442945
DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {})
29452946
DEF_TRAVERSE_STMT(NoInitExpr, {})

clang/include/clang/AST/TextNodeDumper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ class TextNodeDumper
286286
void VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Node);
287287
void VisitMemberExpr(const MemberExpr *Node);
288288
void VisitExtVectorElementExpr(const ExtVectorElementExpr *Node);
289+
void VisitMatrixElementExpr(const MatrixElementExpr *Node);
289290
void VisitBinaryOperator(const BinaryOperator *Node);
290291
void VisitCompoundAssignOperator(const CompoundAssignOperator *Node);
291292
void VisitAddrLabelExpr(const AddrLabelExpr *Node);

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9336,6 +9336,8 @@ def err_typecheck_lvalue_casts_not_supported : Error<
93369336

93379337
def err_typecheck_duplicate_vector_components_not_mlvalue : Error<
93389338
"vector is not assignable (contains duplicate components)">;
9339+
def err_typecheck_duplicate_matrix_components_not_mlvalue : Error<
9340+
"matrix is not assignable (contains duplicate components)">;
93399341
def err_block_decl_ref_not_modifiable_lvalue : Error<
93409342
"variable is not assignable (missing __block type specifier)">;
93419343
def err_lambda_decl_ref_not_modifiable_lvalue : Error<
@@ -13055,6 +13057,7 @@ def err_builtin_matrix_stride_too_small: Error<
1305513057
"stride must be greater or equal to the number of rows">;
1305613058
def err_builtin_matrix_invalid_dimension: Error<
1305713059
"%0 dimension is outside the allowed range [1, %1]">;
13060+
def err_builtin_matrix_invalid_member: Error<"invalid matrix member '%0' expected %1">;
1305813061

1305913062
def warn_mismatched_import : Warning<
1306013063
"import %select{module|name}0 (%1) does not match the import %select{module|name}0 (%2) of the "
@@ -13312,6 +13315,15 @@ def err_hlsl_builtin_scalar_vector_mismatch
1331213315
"%select{all|second and third}0 arguments to %1 must be of scalar or "
1331313316
"vector type with matching scalar element type%diff{: $ vs $|}2,3">;
1331413317

13318+
def err_hlsl_matrix_element_not_in_bounds : Error<
13319+
"matrix %select{row|column}0 element accessor is out of bounds of %select{zero|one}1 based indexing">;
13320+
13321+
def err_hlsl_matrix_index_out_of_bounds : Error<
13322+
"matrix %select{row|column}0 index %1 is out of bounds of %select{rows|columns}0 size %2">;
13323+
13324+
def err_hlsl_matrix_swizzle_invalid_length : Error<
13325+
"matrix swizzle length must be between 1 and 4 but is %0">;
13326+
1331513327
def warn_hlsl_impcast_vector_truncation : Warning<
1331613328
"implicit conversion truncates vector: %0 to %1">, InGroup<VectorConversion>;
1331713329

clang/include/clang/Basic/StmtNodes.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def CStyleCastExpr : StmtNode<ExplicitCastExpr>;
9191
def OMPArrayShapingExpr : StmtNode<Expr>;
9292
def CompoundLiteralExpr : StmtNode<Expr>;
9393
def ExtVectorElementExpr : StmtNode<Expr>;
94+
def MatrixElementExpr : StmtNode<Expr>;
9495
def InitListExpr : StmtNode<Expr>;
9596
def DesignatedInitExpr : StmtNode<Expr>;
9697
def DesignatedInitUpdateExpr : StmtNode<Expr>;

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ class SemaHLSL : public SemaBase {
214214
bool transformInitList(const InitializedEntity &Entity, InitListExpr *Init);
215215
bool handleInitialization(VarDecl *VDecl, Expr *&Init);
216216
void deduceAddressSpace(VarDecl *Decl);
217+
QualType CheckMatrixComponent(Sema &S, QualType baseType, ExprValueKind &VK,
218+
SourceLocation OpLoc,
219+
const IdentifierInfo *CompName,
220+
SourceLocation CompLoc);
217221

218222
private:
219223
// HLSL resource type attributes need to be processed all at once.

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,9 @@ enum StmtCode {
16931693
/// An ExtVectorElementExpr record.
16941694
EXPR_EXT_VECTOR_ELEMENT,
16951695

1696+
/// A MatrixElementExpr record.
1697+
EXPR_MATRIX_ELEMENT,
1698+
16961699
/// An InitListExpr record.
16971700
EXPR_INIT_LIST,
16981701

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,6 +4090,12 @@ bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
40904090
return true;
40914091
}
40924092

4093+
template <class Emitter>
4094+
bool Compiler<Emitter>::VisitMatrixElementExpr(const MatrixElementExpr *Node) {
4095+
// TODO
4096+
return false;
4097+
}
4098+
40934099
template <class Emitter>
40944100
bool Compiler<Emitter>::VisitExtVectorElementExpr(
40954101
const ExtVectorElementExpr *E) {

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
223223
bool VisitConvertVectorExpr(const ConvertVectorExpr *E);
224224
bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);
225225
bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E);
226+
bool VisitMatrixElementExpr(const MatrixElementExpr *E);
226227
bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E);
227228
bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E);
228229
bool VisitStmtExpr(const StmtExpr *E);

0 commit comments

Comments
 (0)