-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[HLSL] Add matrix constructors using initalizer lists #162743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -91,6 +91,10 @@ class alignas(8) InitializedEntity { | |||||
/// or vector. | ||||||
EK_VectorElement, | ||||||
|
||||||
/// The entity being initialized is an element of a matrix. | ||||||
/// or matrix. | ||||||
EK_MatrixElement, | ||||||
|
||||||
/// The entity being initialized is a field of block descriptor for | ||||||
/// the copied-in c++ object. | ||||||
EK_BlockElement, | ||||||
|
@@ -205,8 +209,8 @@ class alignas(8) InitializedEntity { | |||||
/// virtual base. | ||||||
llvm::PointerIntPair<const CXXBaseSpecifier *, 1> Base; | ||||||
|
||||||
/// When Kind == EK_ArrayElement, EK_VectorElement, or | ||||||
/// EK_ComplexElement, the index of the array or vector element being | ||||||
/// When Kind == EK_ArrayElement, EK_VectorElement, or EK_MatrixElement, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the word "or" appears twice
Suggested change
|
||||||
/// or EK_ComplexElement, the index of the array or vector element being | ||||||
/// initialized. | ||||||
unsigned Index; | ||||||
|
||||||
|
@@ -536,15 +540,15 @@ class alignas(8) InitializedEntity { | |||||
/// element's index. | ||||||
unsigned getElementIndex() const { | ||||||
assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement || | ||||||
getKind() == EK_ComplexElement); | ||||||
getKind() == EK_MatrixElement || getKind() == EK_ComplexElement); | ||||||
return Index; | ||||||
} | ||||||
|
||||||
/// If this is already the initializer for an array or vector | ||||||
/// element, sets the element index. | ||||||
void setElementIndex(unsigned Index) { | ||||||
assert(getKind() == EK_ArrayElement || getKind() == EK_VectorElement || | ||||||
getKind() == EK_ComplexElement); | ||||||
getKind() == EK_MatrixElement || getKind() == EK_ComplexElement); | ||||||
this->Index = Index; | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,6 +21,7 @@ | |||||||||
#include "clang/AST/Expr.h" | ||||||||||
#include "clang/AST/HLSLResource.h" | ||||||||||
#include "clang/AST/Type.h" | ||||||||||
#include "clang/AST/TypeBase.h" | ||||||||||
#include "clang/AST/TypeLoc.h" | ||||||||||
#include "clang/Basic/Builtins.h" | ||||||||||
#include "clang/Basic/DiagnosticSema.h" | ||||||||||
|
@@ -3351,6 +3352,11 @@ static void BuildFlattenedTypeList(QualType BaseTy, | |||||||||
List.insert(List.end(), VT->getNumElements(), VT->getElementType()); | ||||||||||
continue; | ||||||||||
} | ||||||||||
if (const auto *MT = dyn_cast<ConstantMatrixType>(T)) { | ||||||||||
List.insert(List.end(), MT->getNumElementsFlattened(), | ||||||||||
MT->getElementType()); | ||||||||||
continue; | ||||||||||
} | ||||||||||
if (const auto *RD = T->getAsCXXRecordDecl()) { | ||||||||||
if (RD->isStandardLayout()) | ||||||||||
RD = RD->getStandardLayoutBaseWithFields(); | ||||||||||
|
@@ -4149,6 +4155,32 @@ class InitListTransformer { | |||||||||
} | ||||||||||
return true; | ||||||||||
} | ||||||||||
if (auto *MTy = Ty->getAs<ConstantMatrixType>()) { | ||||||||||
unsigned Rows = MTy->getNumRows(); | ||||||||||
unsigned Cols = MTy->getNumColumns(); | ||||||||||
QualType ElemTy = MTy->getElementType(); | ||||||||||
|
||||||||||
for (unsigned C = 0; C < Cols; ++C) { | ||||||||||
for (unsigned R = 0; R < Rows; ++R) { | ||||||||||
// row index literal | ||||||||||
Expr *RowIdx = IntegerLiteral::Create( | ||||||||||
Ctx, llvm::APInt(Ctx.getIntWidth(Ctx.IntTy), R), Ctx.IntTy, | ||||||||||
E->getBeginLoc()); | ||||||||||
// column index literal | ||||||||||
Expr *ColIdx = IntegerLiteral::Create( | ||||||||||
Ctx, llvm::APInt(Ctx.getIntWidth(Ctx.IntTy), C), Ctx.IntTy, | ||||||||||
E->getBeginLoc()); | ||||||||||
ExprResult ElExpr = S.CreateBuiltinMatrixSubscriptExpr( | ||||||||||
E, RowIdx, ColIdx, E->getEndLoc()); | ||||||||||
if (ElExpr.isInvalid()) | ||||||||||
return false; | ||||||||||
if (!buildInitializerListImpl(ElExpr.get())) | ||||||||||
return false; | ||||||||||
Comment on lines
+4177
to
+4178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line I copied from the
Suggested change
I couldn't find a material difference in behavior in my testing. suggestions on test cases to know which one is the right error handling would be appreicated. |
||||||||||
ElExpr.get()->setType(ElemTy); | ||||||||||
} | ||||||||||
} | ||||||||||
return true; | ||||||||||
} | ||||||||||
|
||||||||||
if (auto *ArrTy = dyn_cast<ConstantArrayType>(Ty.getTypePtr())) { | ||||||||||
uint64_t Size = ArrTy->getZExtSize(); | ||||||||||
|
@@ -4202,14 +4234,17 @@ class InitListTransformer { | |||||||||
return *(ArgIt++); | ||||||||||
|
||||||||||
llvm::SmallVector<Expr *> Inits; | ||||||||||
assert(!isa<MatrixType>(Ty) && "Matrix types not yet supported in HLSL"); | ||||||||||
Ty = Ty.getDesugaredType(Ctx); | ||||||||||
if (Ty->isVectorType() || Ty->isConstantArrayType()) { | ||||||||||
if (Ty->isVectorType() || Ty->isConstantArrayType() || | ||||||||||
Ty->isConstantMatrixType()) { | ||||||||||
QualType ElTy; | ||||||||||
uint64_t Size = 0; | ||||||||||
if (auto *ATy = Ty->getAs<VectorType>()) { | ||||||||||
ElTy = ATy->getElementType(); | ||||||||||
Size = ATy->getNumElements(); | ||||||||||
} else if (auto *CMTy = Ty->getAs<ConstantMatrixType>()) { | ||||||||||
ElTy = CMTy->getElementType(); | ||||||||||
Size = CMTy->getNumElementsFlattened(); | ||||||||||
} else { | ||||||||||
auto *VTy = cast<ConstantArrayType>(Ty.getTypePtr()); | ||||||||||
ElTy = VTy->getElementType(); | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a block element
appears twice in this list