Skip to content

Commit 18b8b75

Browse files
Mogballwhitneywhtsang
authored andcommitted
[Layouts] Fix invertAndCompose when there are identity dimensions (#5468)
I found a counterexample where `invertAndCompose` was giving an incorrect result. Thanks for @lezcano for pointing out a possible solution: if input dim 0 and 4 are identity dimensions, then they are added back to the reduced inverse as dimensions 3 and 4. Them reshaping the result will assign the wrong bases to the wrong dimensions. Use a transpose instead to order the input and output dimensions correctly instead.
1 parent df900e4 commit 18b8b75

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

lib/Tools/LinearLayout.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -917,17 +917,10 @@ LinearLayout LinearLayout::invertAndCompose(const LinearLayout &outer) const {
917917
ret *= LinearLayout::identity1D(A.getInDimSize(dim), dim, dim);
918918
}
919919

920-
// Reshape the result
921-
SmallVector<std::pair<StringAttr, int32_t>> inDimsA;
922-
SmallVector<std::pair<StringAttr, int32_t>> inDimsB;
923-
for (auto dim : A.getInDimNames()) {
924-
inDimsA.push_back({dim, A.getInDimSize(dim)});
925-
}
926-
for (auto dim : B.getInDimNames()) {
927-
inDimsB.push_back({dim, B.getInDimSize(dim)});
928-
}
929-
ret = ret.reshapeIns(inDimsB).reshapeOuts(inDimsA);
930-
return ret;
920+
// Reorder the dimensions in the result to match the order expected by the
921+
// current and outer layouts.
922+
return ret.transposeIns(llvm::to_vector(B.getInDimNames()))
923+
.transposeOuts(llvm::to_vector(A.getInDimNames()));
931924
}
932925

933926
LinearLayout LinearLayout::invert() const {

0 commit comments

Comments
 (0)