Skip to content

Commit 1831109

Browse files
authored
[InstCombine] Do not fold shufflevector(select) if the select condition is a vector (#113993)
Since `shufflevector` is not element-wise, we cannot do fold it into select when the select condition is a vector. For shufflevector that doesn't change the length, it doesn't crash, but it is still a miscompilation: https://alive2.llvm.org/ce/z/s8saCx Fixes #113986.
1 parent 0c1c37b commit 1831109

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,8 +2902,12 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
29022902

29032903
if (match(RHS, m_Constant())) {
29042904
if (auto *SI = dyn_cast<SelectInst>(LHS)) {
2905-
if (Instruction *I = FoldOpIntoSelect(SVI, SI))
2906-
return I;
2905+
// We cannot do this fold for elementwise select since ShuffleVector is
2906+
// not elementwise.
2907+
if (SI->getCondition()->getType()->isIntegerTy()) {
2908+
if (Instruction *I = FoldOpIntoSelect(SVI, SI))
2909+
return I;
2910+
}
29072911
}
29082912
if (auto *PN = dyn_cast<PHINode>(LHS)) {
29092913
if (Instruction *I = foldOpIntoPhi(SVI, PN))

llvm/test/Transforms/InstCombine/vec_shuffle.ll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,6 +2387,30 @@ define <2 x i32> @foldselect0(i1 %c) {
23872387
ret <2 x i32> %shuf
23882388
}
23892389

2390+
; Make sure we do not crash in this case.
2391+
define <4 x float> @shuf_larger_length_vec_select(<2 x i1> %cond) {
2392+
; CHECK-LABEL: @shuf_larger_length_vec_select(
2393+
; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[COND:%.*]], <2 x float> zeroinitializer, <2 x float> <float 1.000000e+00, float 1.000000e+00>
2394+
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <2 x float> [[SEL]], <2 x float> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
2395+
; CHECK-NEXT: ret <4 x float> [[SHUF]]
2396+
;
2397+
%sel = select <2 x i1> %cond, <2 x float> zeroinitializer, <2 x float> splat(float 1.000000e+00)
2398+
%shuf = shufflevector <2 x float> %sel, <2 x float> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
2399+
ret <4 x float> %shuf
2400+
}
2401+
2402+
; Make sure we do not fold in this case.
2403+
define <4 x i32> @shuf_same_length_vec_select(<4 x i1> %cond) {
2404+
; CHECK-LABEL: @shuf_same_length_vec_select(
2405+
; CHECK-NEXT: [[SEL:%.*]] = select <4 x i1> [[COND:%.*]], <4 x i32> <i32 poison, i32 1, i32 2, i32 3>, <4 x i32> <i32 poison, i32 5, i32 6, i32 7>
2406+
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[SEL]], <4 x i32> <i32 poison, i32 9, i32 poison, i32 poison>, <4 x i32> <i32 2, i32 1, i32 3, i32 5>
2407+
; CHECK-NEXT: ret <4 x i32> [[SHUF]]
2408+
;
2409+
%sel = select <4 x i1> %cond, <4 x i32> <i32 0, i32 1, i32 2, i32 3>, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
2410+
%shuf = shufflevector <4 x i32> %sel, <4 x i32> <i32 8, i32 9, i32 10, i32 11>, <4 x i32> <i32 2, i32 1, i32 3, i32 5>
2411+
ret <4 x i32> %shuf
2412+
}
2413+
23902414
declare i1 @cond()
23912415
declare <4 x i32> @value()
23922416

0 commit comments

Comments
 (0)