Skip to content

Commit 47a161d

Browse files
author
git apple-llvm automerger
committed
Merge commit '251edd122808' from llvm.org/main into next
2 parents 974efe1 + 251edd1 commit 47a161d

File tree

8 files changed

+807
-26
lines changed

8 files changed

+807
-26
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,9 +2443,9 @@ def err_init_conversion_failed : Error<
24432443
"cannot initialize %select{a variable|a parameter|template parameter|"
24442444
"return object|statement expression result|an "
24452445
"exception object|a member subobject|an array element|a new value|a value|a "
2446-
"base class|a constructor delegation|a vector element|a block element|a "
2447-
"block element|a complex element|a lambda capture|a compound literal "
2448-
"initializer|a related result|a parameter of CF audited function|a "
2446+
"base class|a constructor delegation|a vector element|a matrix element|a "
2447+
"block element|a block element|a complex element|a lambda capture|a compound"
2448+
" literal initializer|a related result|a parameter of CF audited function|a "
24492449
"structured binding|a member subobject}0 "
24502450
"%diff{of type $ with an %select{rvalue|lvalue}2 of type $|"
24512451
"with an %select{rvalue|lvalue}2 of incompatible type}1,3"
@@ -6626,9 +6626,9 @@ def warn_extern_init : Warning<"'extern' variable has an initializer">,
66266626
def err_variable_object_no_init : Error<
66276627
"variable-sized object may not be initialized">;
66286628
def err_excess_initializers : Error<
6629-
"excess elements in %select{array|vector|scalar|union|struct}0 initializer">;
6629+
"excess elements in %select{array|vector|matrix|scalar|union|struct}0 initializer">;
66306630
def ext_excess_initializers : ExtWarn<
6631-
"excess elements in %select{array|vector|scalar|union|struct}0 initializer">,
6631+
"excess elements in %select{array|vector|matrix|scalar|union|struct}0 initializer">,
66326632
InGroup<ExcessInitializers>;
66336633
def err_excess_initializers_for_sizeless_type : Error<
66346634
"excess elements in initializer for indivisible sizeless type %0">;

clang/include/clang/Sema/Initialization.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ class alignas(8) InitializedEntity {
9191
/// or vector.
9292
EK_VectorElement,
9393

94+
/// The entity being initialized is an element of a matrix.
95+
/// or matrix.
96+
EK_MatrixElement,
97+
9498
/// The entity being initialized is a field of block descriptor for
9599
/// the copied-in c++ object.
96100
EK_BlockElement,
@@ -205,8 +209,8 @@ class alignas(8) InitializedEntity {
205209
/// virtual base.
206210
llvm::PointerIntPair<const CXXBaseSpecifier *, 1> Base;
207211

208-
/// When Kind == EK_ArrayElement, EK_VectorElement, or
209-
/// EK_ComplexElement, the index of the array or vector element being
212+
/// When Kind == EK_ArrayElement, EK_VectorElement, EK_MatrixElement,
213+
/// or EK_ComplexElement, the index of the array or vector element being
210214
/// initialized.
211215
unsigned Index;
212216

@@ -536,15 +540,15 @@ class alignas(8) InitializedEntity {
536540
/// element's index.
537541
unsigned getElementIndex() const {
538542
assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement ||
539-
getKind() == EK_ComplexElement);
543+
getKind() == EK_MatrixElement || getKind() == EK_ComplexElement);
540544
return Index;
541545
}
542546

543547
/// If this is already the initializer for an array or vector
544548
/// element, sets the element index.
545549
void setElementIndex(unsigned Index) {
546550
assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement ||
547-
getKind() == EK_ComplexElement);
551+
getKind() == EK_MatrixElement || getKind() == EK_ComplexElement);
548552
this->Index = Index;
549553
}
550554

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ getEntityLifetime(const InitializedEntity *Entity,
155155
case InitializedEntity::EK_LambdaToBlockConversionBlockElement:
156156
case InitializedEntity::EK_LambdaCapture:
157157
case InitializedEntity::EK_VectorElement:
158+
case InitializedEntity::EK_MatrixElement:
158159
case InitializedEntity::EK_ComplexElement:
159160
return {nullptr, LK_FullExpression};
160161

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "clang/AST/Expr.h"
2222
#include "clang/AST/HLSLResource.h"
2323
#include "clang/AST/Type.h"
24+
#include "clang/AST/TypeBase.h"
2425
#include "clang/AST/TypeLoc.h"
2526
#include "clang/Basic/Builtins.h"
2627
#include "clang/Basic/DiagnosticSema.h"
@@ -3432,6 +3433,11 @@ static void BuildFlattenedTypeList(QualType BaseTy,
34323433
List.insert(List.end(), VT->getNumElements(), VT->getElementType());
34333434
continue;
34343435
}
3436+
if (const auto *MT = dyn_cast<ConstantMatrixType>(T)) {
3437+
List.insert(List.end(), MT->getNumElementsFlattened(),
3438+
MT->getElementType());
3439+
continue;
3440+
}
34353441
if (const auto *RD = T->getAsCXXRecordDecl()) {
34363442
if (RD->isStandardLayout())
34373443
RD = RD->getStandardLayoutBaseWithFields();
@@ -4230,6 +4236,32 @@ class InitListTransformer {
42304236
}
42314237
return true;
42324238
}
4239+
if (auto *MTy = Ty->getAs<ConstantMatrixType>()) {
4240+
unsigned Rows = MTy->getNumRows();
4241+
unsigned Cols = MTy->getNumColumns();
4242+
QualType ElemTy = MTy->getElementType();
4243+
4244+
for (unsigned C = 0; C < Cols; ++C) {
4245+
for (unsigned R = 0; R < Rows; ++R) {
4246+
// row index literal
4247+
Expr *RowIdx = IntegerLiteral::Create(
4248+
Ctx, llvm::APInt(Ctx.getIntWidth(Ctx.IntTy), R), Ctx.IntTy,
4249+
E->getBeginLoc());
4250+
// column index literal
4251+
Expr *ColIdx = IntegerLiteral::Create(
4252+
Ctx, llvm::APInt(Ctx.getIntWidth(Ctx.IntTy), C), Ctx.IntTy,
4253+
E->getBeginLoc());
4254+
ExprResult ElExpr = S.CreateBuiltinMatrixSubscriptExpr(
4255+
E, RowIdx, ColIdx, E->getEndLoc());
4256+
if (ElExpr.isInvalid())
4257+
return false;
4258+
if (!castInitializer(ElExpr.get()))
4259+
return false;
4260+
ElExpr.get()->setType(ElemTy);
4261+
}
4262+
}
4263+
return true;
4264+
}
42334265

42344266
if (auto *ArrTy = dyn_cast<ConstantArrayType>(Ty.getTypePtr())) {
42354267
uint64_t Size = ArrTy->getZExtSize();
@@ -4283,14 +4315,17 @@ class InitListTransformer {
42834315
return *(ArgIt++);
42844316

42854317
llvm::SmallVector<Expr *> Inits;
4286-
assert(!isa<MatrixType>(Ty) && "Matrix types not yet supported in HLSL");
42874318
Ty = Ty.getDesugaredType(Ctx);
4288-
if (Ty->isVectorType() || Ty->isConstantArrayType()) {
4319+
if (Ty->isVectorType() || Ty->isConstantArrayType() ||
4320+
Ty->isConstantMatrixType()) {
42894321
QualType ElTy;
42904322
uint64_t Size = 0;
42914323
if (auto *ATy = Ty->getAs<VectorType>()) {
42924324
ElTy = ATy->getElementType();
42934325
Size = ATy->getNumElements();
4326+
} else if (auto *CMTy = Ty->getAs<ConstantMatrixType>()) {
4327+
ElTy = CMTy->getElementType();
4328+
Size = CMTy->getNumElementsFlattened();
42944329
} else {
42954330
auto *VTy = cast<ConstantArrayType>(Ty.getTypePtr());
42964331
ElTy = VTy->getElementType();

clang/lib/Sema/SemaInit.cpp

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/ExprObjC.h"
2020
#include "clang/AST/RecursiveASTVisitor.h"
2121
#include "clang/AST/IgnoreExpr.h"
22+
#include "clang/AST/TypeBase.h"
2223
#include "clang/AST/TypeLoc.h"
2324
#include "clang/Basic/SourceManager.h"
2425
#include "clang/Basic/Specifiers.h"
@@ -636,6 +637,9 @@ class InitListChecker {
636637
unsigned &Index,
637638
InitListExpr *StructuredList,
638639
unsigned &StructuredIndex);
640+
void CheckMatrixType(const InitializedEntity &Entity, InitListExpr *IList,
641+
QualType DeclType, unsigned &Index,
642+
InitListExpr *StructuredList, unsigned &StructuredIndex);
639643
void CheckVectorType(const InitializedEntity &Entity,
640644
InitListExpr *IList, QualType DeclType, unsigned &Index,
641645
InitListExpr *StructuredList,
@@ -1239,7 +1243,8 @@ InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity,
12391243
return;
12401244

12411245
if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement ||
1242-
ElementEntity.getKind() == InitializedEntity::EK_VectorElement)
1246+
ElementEntity.getKind() == InitializedEntity::EK_VectorElement ||
1247+
ElementEntity.getKind() == InitializedEntity::EK_MatrixElement)
12431248
ElementEntity.setElementIndex(Init);
12441249

12451250
if (Init >= NumInits && (ILE->hasArrayFiller() || SkipEmptyInitChecks))
@@ -1511,6 +1516,7 @@ static void warnBracedScalarInit(Sema &S, const InitializedEntity &Entity,
15111516

15121517
switch (Entity.getKind()) {
15131518
case InitializedEntity::EK_VectorElement:
1519+
case InitializedEntity::EK_MatrixElement:
15141520
case InitializedEntity::EK_ComplexElement:
15151521
case InitializedEntity::EK_ArrayElement:
15161522
case InitializedEntity::EK_Parameter:
@@ -1610,11 +1616,12 @@ void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity,
16101616
SemaRef.Diag(IList->getInit(Index)->getBeginLoc(), DK)
16111617
<< T << IList->getInit(Index)->getSourceRange();
16121618
} else {
1613-
int initKind = T->isArrayType() ? 0 :
1614-
T->isVectorType() ? 1 :
1615-
T->isScalarType() ? 2 :
1616-
T->isUnionType() ? 3 :
1617-
4;
1619+
int initKind = T->isArrayType() ? 0
1620+
: T->isVectorType() ? 1
1621+
: T->isMatrixType() ? 2
1622+
: T->isScalarType() ? 3
1623+
: T->isUnionType() ? 4
1624+
: 5;
16181625

16191626
unsigned DK = ExtraInitsIsError ? diag::err_excess_initializers
16201627
: diag::ext_excess_initializers;
@@ -1668,6 +1675,9 @@ void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity,
16681675
} else if (DeclType->isVectorType()) {
16691676
CheckVectorType(Entity, IList, DeclType, Index,
16701677
StructuredList, StructuredIndex);
1678+
} else if (DeclType->isMatrixType()) {
1679+
CheckMatrixType(Entity, IList, DeclType, Index, StructuredList,
1680+
StructuredIndex);
16711681
} else if (const RecordDecl *RD = DeclType->getAsRecordDecl()) {
16721682
auto Bases =
16731683
CXXRecordDecl::base_class_const_range(CXXRecordDecl::base_class_const_iterator(),
@@ -2115,6 +2125,37 @@ void InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
21152125
AggrDeductionCandidateParamTypes->push_back(DeclType);
21162126
}
21172127

2128+
void InitListChecker::CheckMatrixType(const InitializedEntity &Entity,
2129+
InitListExpr *IList, QualType DeclType,
2130+
unsigned &Index,
2131+
InitListExpr *StructuredList,
2132+
unsigned &StructuredIndex) {
2133+
if (!SemaRef.getLangOpts().HLSL)
2134+
return;
2135+
2136+
const ConstantMatrixType *MT = DeclType->castAs<ConstantMatrixType>();
2137+
QualType ElemTy = MT->getElementType();
2138+
const unsigned MaxElts = MT->getNumElementsFlattened();
2139+
2140+
unsigned NumEltsInit = 0;
2141+
InitializedEntity ElemEnt =
2142+
InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
2143+
2144+
while (NumEltsInit < MaxElts && Index < IList->getNumInits()) {
2145+
// Not a sublist: just consume directly.
2146+
ElemEnt.setElementIndex(Index);
2147+
CheckSubElementType(ElemEnt, IList, ElemTy, Index, StructuredList,
2148+
StructuredIndex);
2149+
++NumEltsInit;
2150+
}
2151+
2152+
// For HLSL The error for this case is handled in SemaHLSL's initializer
2153+
// list diagnostics, That means the execution should require NumEltsInit
2154+
// to equal Max initializers. In other words execution should never
2155+
// reach this point if this condition is not true".
2156+
assert(NumEltsInit == MaxElts && "NumEltsInit must equal MaxElts");
2157+
}
2158+
21182159
void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
21192160
InitListExpr *IList, QualType DeclType,
21202161
unsigned &Index,
@@ -3967,6 +4008,9 @@ InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index,
39674008
} else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) {
39684009
Kind = EK_VectorElement;
39694010
Type = VT->getElementType();
4011+
} else if (const MatrixType *MT = Parent.getType()->getAs<MatrixType>()) {
4012+
Kind = EK_MatrixElement;
4013+
Type = MT->getElementType();
39704014
} else {
39714015
const ComplexType *CT = Parent.getType()->getAs<ComplexType>();
39724016
assert(CT && "Unexpected type");
@@ -4015,6 +4059,7 @@ DeclarationName InitializedEntity::getName() const {
40154059
case EK_Delegating:
40164060
case EK_ArrayElement:
40174061
case EK_VectorElement:
4062+
case EK_MatrixElement:
40184063
case EK_ComplexElement:
40194064
case EK_BlockElement:
40204065
case EK_LambdaToBlockConversionBlockElement:
@@ -4048,6 +4093,7 @@ ValueDecl *InitializedEntity::getDecl() const {
40484093
case EK_Delegating:
40494094
case EK_ArrayElement:
40504095
case EK_VectorElement:
4096+
case EK_MatrixElement:
40514097
case EK_ComplexElement:
40524098
case EK_BlockElement:
40534099
case EK_LambdaToBlockConversionBlockElement:
@@ -4081,6 +4127,7 @@ bool InitializedEntity::allowsNRVO() const {
40814127
case EK_Delegating:
40824128
case EK_ArrayElement:
40834129
case EK_VectorElement:
4130+
case EK_MatrixElement:
40844131
case EK_ComplexElement:
40854132
case EK_BlockElement:
40864133
case EK_LambdaToBlockConversionBlockElement:
@@ -4120,6 +4167,9 @@ unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const {
41204167
case EK_Delegating: OS << "Delegating"; break;
41214168
case EK_ArrayElement: OS << "ArrayElement " << Index; break;
41224169
case EK_VectorElement: OS << "VectorElement " << Index; break;
4170+
case EK_MatrixElement:
4171+
OS << "MatrixElement " << Index;
4172+
break;
41234173
case EK_ComplexElement: OS << "ComplexElement " << Index; break;
41244174
case EK_BlockElement: OS << "Block"; break;
41254175
case EK_LambdaToBlockConversionBlockElement:
@@ -6357,7 +6407,7 @@ static void TryOrBuildParenListInitialization(
63576407
Sequence.SetFailed(InitializationSequence::FK_ParenthesizedListInitFailed);
63586408
if (!VerifyOnly) {
63596409
QualType T = Entity.getType();
6360-
int InitKind = T->isArrayType() ? 0 : T->isUnionType() ? 3 : 4;
6410+
int InitKind = T->isArrayType() ? 0 : T->isUnionType() ? 4 : 5;
63616411
SourceRange ExcessInitSR(Args[EntityIndexToProcess]->getBeginLoc(),
63626412
Args.back()->getEndLoc());
63636413
S.Diag(Kind.getLocation(), diag::err_excess_initializers)
@@ -7150,7 +7200,8 @@ void InitializationSequence::InitializeFrom(Sema &S,
71507200
// For HLSL ext vector types we allow list initialization behavior for C++
71517201
// functional cast expressions which look like constructor syntax. This is
71527202
// accomplished by converting initialization arguments to InitListExpr.
7153-
if (S.getLangOpts().HLSL && Args.size() > 1 && DestType->isExtVectorType() &&
7203+
if (S.getLangOpts().HLSL && Args.size() > 1 &&
7204+
(DestType->isExtVectorType() || DestType->isConstantMatrixType()) &&
71547205
(SourceType.isNull() ||
71557206
!Context.hasSameUnqualifiedType(SourceType, DestType))) {
71567207
InitListExpr *ILE = new (Context)
@@ -7315,6 +7366,7 @@ static AssignmentAction getAssignmentAction(const InitializedEntity &Entity,
73157366
case InitializedEntity::EK_Binding:
73167367
case InitializedEntity::EK_ArrayElement:
73177368
case InitializedEntity::EK_VectorElement:
7369+
case InitializedEntity::EK_MatrixElement:
73187370
case InitializedEntity::EK_ComplexElement:
73197371
case InitializedEntity::EK_BlockElement:
73207372
case InitializedEntity::EK_LambdaToBlockConversionBlockElement:
@@ -7340,6 +7392,7 @@ static bool shouldBindAsTemporary(const InitializedEntity &Entity) {
73407392
case InitializedEntity::EK_Base:
73417393
case InitializedEntity::EK_Delegating:
73427394
case InitializedEntity::EK_VectorElement:
7395+
case InitializedEntity::EK_MatrixElement:
73437396
case InitializedEntity::EK_ComplexElement:
73447397
case InitializedEntity::EK_Exception:
73457398
case InitializedEntity::EK_BlockElement:
@@ -7370,6 +7423,7 @@ static bool shouldDestroyEntity(const InitializedEntity &Entity) {
73707423
case InitializedEntity::EK_Base:
73717424
case InitializedEntity::EK_Delegating:
73727425
case InitializedEntity::EK_VectorElement:
7426+
case InitializedEntity::EK_MatrixElement:
73737427
case InitializedEntity::EK_ComplexElement:
73747428
case InitializedEntity::EK_BlockElement:
73757429
case InitializedEntity::EK_LambdaToBlockConversionBlockElement:
@@ -7423,6 +7477,7 @@ static SourceLocation getInitializationLoc(const InitializedEntity &Entity,
74237477
case InitializedEntity::EK_Base:
74247478
case InitializedEntity::EK_Delegating:
74257479
case InitializedEntity::EK_VectorElement:
7480+
case InitializedEntity::EK_MatrixElement:
74267481
case InitializedEntity::EK_ComplexElement:
74277482
case InitializedEntity::EK_BlockElement:
74287483
case InitializedEntity::EK_LambdaToBlockConversionBlockElement:
@@ -8172,11 +8227,13 @@ ExprResult InitializationSequence::Perform(Sema &S,
81728227
ExprResult CurInit((Expr *)nullptr);
81738228
SmallVector<Expr*, 4> ArrayLoopCommonExprs;
81748229

8175-
// HLSL allows vector initialization to function like list initialization, but
8176-
// use the syntax of a C++-like constructor.
8177-
bool IsHLSLVectorInit = S.getLangOpts().HLSL && DestType->isExtVectorType() &&
8178-
isa<InitListExpr>(Args[0]);
8179-
(void)IsHLSLVectorInit;
8230+
// HLSL allows vector/matrix initialization to function like list
8231+
// initialization, but use the syntax of a C++-like constructor.
8232+
bool IsHLSLVectorOrMatrixInit =
8233+
S.getLangOpts().HLSL &&
8234+
(DestType->isExtVectorType() || DestType->isConstantMatrixType()) &&
8235+
isa<InitListExpr>(Args[0]);
8236+
(void)IsHLSLVectorOrMatrixInit;
81808237

81818238
// For initialization steps that start with a single initializer,
81828239
// grab the only argument out the Args and place it into the "current"
@@ -8215,7 +8272,7 @@ ExprResult InitializationSequence::Perform(Sema &S,
82158272
case SK_StdInitializerList:
82168273
case SK_OCLSamplerInit:
82178274
case SK_OCLZeroOpaqueType: {
8218-
assert(Args.size() == 1 || IsHLSLVectorInit);
8275+
assert(Args.size() == 1 || IsHLSLVectorOrMatrixInit);
82198276
CurInit = Args[0];
82208277
if (!CurInit.get()) return ExprError();
82218278
break;
@@ -9533,7 +9590,7 @@ bool InitializationSequence::Diagnose(Sema &S,
95339590
<< R;
95349591
else
95359592
S.Diag(Kind.getLocation(), diag::err_excess_initializers)
9536-
<< /*scalar=*/2 << R;
9593+
<< /*scalar=*/3 << R;
95379594
break;
95389595
}
95399596

0 commit comments

Comments
 (0)