Skip to content

Commit bee6b1d

Browse files
omern1Davide Italiano
authored andcommitted
[InstCombine] Fix assert condition in foldSelectShuffleOfSelectShuffle
Bug introduced in e239198. The assert() is making an assumption that the resulting shuffle mask will always select elements from both vectors, this is untrue in the case of two shuffles being folded if the former shuffle has a mask with undef elements in it. In such a case folding the shuffles might result in a mask which only selects from one of the vectors because the other elements (in the mask) are undef. Differential Revision: https://reviews.llvm.org/D136256
1 parent fcaaa82 commit bee6b1d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,10 @@ static Instruction *foldSelectShuffleOfSelectShuffle(ShuffleVectorInst &Shuf) {
20092009
for (unsigned i = 0; i != NumElts; ++i)
20102010
NewMask[i] = Mask[i] < (signed)NumElts ? Mask[i] : Mask1[i];
20112011

2012-
assert(ShuffleVectorInst::isSelectMask(NewMask) && "Unexpected shuffle mask");
2012+
// A select mask with undef elements might look like an identity mask.
2013+
assert((ShuffleVectorInst::isSelectMask(NewMask) ||
2014+
ShuffleVectorInst::isIdentityMask(NewMask)) &&
2015+
"Unexpected shuffle mask");
20132016
return new ShuffleVectorInst(X, Y, NewMask);
20142017
}
20152018

llvm/test/Transforms/InstCombine/shuffle_select.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,3 +1621,13 @@ define <4 x i32> @sel_common_op_extra_use(<4 x i32> %x, <4 x i32> %y) {
16211621
%s2 = shufflevector <4 x i32> %s1, <4 x i32> %x, <4 x i32> <i32 0, i32 1, i32 6, i32 7>
16221622
ret <4 x i32> %s2
16231623
}
1624+
1625+
define <4 x float> @identity_mask(<4 x float>%x, <4 x float> %y) {
1626+
; CHECK-LABEL: @identity_mask(
1627+
; CHECK-NEXT: [[S2:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> poison, <4 x i32> <i32 0, i32 undef, i32 2, i32 3>
1628+
; CHECK-NEXT: ret <4 x float> [[S2]]
1629+
;
1630+
%s1 = shufflevector <4 x float> %x, <4 x float> %y, <4 x i32> <i32 0, i32 5, i32 undef, i32 undef>
1631+
%s2 = shufflevector <4 x float> %s1, <4 x float> %x, <4 x i32> <i32 0, i32 undef, i32 6, i32 7>
1632+
ret <4 x float> %s2
1633+
}

0 commit comments

Comments
 (0)