Skip to content

Commit 9cd1e40

Browse files
farzonlIcohedronhekota
authored
[HLSL] Layout Initalizer list in Column order via index conversion (#166277)
fixes #165663 The bug was that we were using the initalizer lists index to populate the matrix. This meant that [0..n] would coorelate to [0..n] indicies of the flattened matrix. Hence why we were seeing the Row-major order: [ 0 1 2 3 4 5 ]. To fix this we can simply converted these indicies to the Column-major order: [ 0 3 1 4 2 5 ]. The net effect of this is the layout of the matrix is now correct and we don't need to change the MatrixSubscriptExpr indexing scheme. --------- Co-authored-by: Deric C. <[email protected]> Co-authored-by: Helena Kotas <[email protected]>
1 parent a2977de commit 9cd1e40

File tree

4 files changed

+198
-101
lines changed

4 files changed

+198
-101
lines changed

clang/lib/Sema/SemaInit.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,26 +1897,29 @@ void InitListChecker::CheckMatrixType(const InitializedEntity &Entity,
18971897
return;
18981898

18991899
const ConstantMatrixType *MT = DeclType->castAs<ConstantMatrixType>();
1900+
1901+
// For HLSL, the error reporting for this case is handled in SemaHLSL's
1902+
// initializer list diagnostics. That means the execution should require
1903+
// getNumElementsFlattened to equal getNumInits. In other words the execution
1904+
// should never reach this point if this condition is not true".
1905+
assert(IList->getNumInits() == MT->getNumElementsFlattened() &&
1906+
"Inits must equal Matrix element count");
1907+
19001908
QualType ElemTy = MT->getElementType();
1901-
const unsigned MaxElts = MT->getNumElementsFlattened();
19021909

1903-
unsigned NumEltsInit = 0;
1910+
Index = 0;
19041911
InitializedEntity ElemEnt =
19051912
InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
19061913

1907-
while (NumEltsInit < MaxElts && Index < IList->getNumInits()) {
1914+
while (Index < IList->getNumInits()) {
19081915
// Not a sublist: just consume directly.
1909-
ElemEnt.setElementIndex(Index);
1910-
CheckSubElementType(ElemEnt, IList, ElemTy, Index, StructuredList,
1916+
unsigned ColMajorIndex = (Index % MT->getNumRows()) * MT->getNumColumns() +
1917+
(Index / MT->getNumRows());
1918+
ElemEnt.setElementIndex(ColMajorIndex);
1919+
CheckSubElementType(ElemEnt, IList, ElemTy, ColMajorIndex, StructuredList,
19111920
StructuredIndex);
1912-
++NumEltsInit;
1921+
++Index;
19131922
}
1914-
1915-
// For HLSL The error for this case is handled in SemaHLSL's initializer
1916-
// list diagnostics, That means the execution should require NumEltsInit
1917-
// to equal Max initializers. In other words execution should never
1918-
// reach this point if this condition is not true".
1919-
assert(NumEltsInit == MaxElts && "NumEltsInit must equal MaxElts");
19201923
}
19211924

19221925
void InitListChecker::CheckVectorType(const InitializedEntity &Entity,

0 commit comments

Comments
 (0)