Skip to content

[InstCombine] Do not fold shufflevector(select) if the select condition is a vector #113993

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

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2902,8 +2902,12 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {

if (match(RHS, m_Constant())) {
if (auto *SI = dyn_cast<SelectInst>(LHS)) {
if (Instruction *I = FoldOpIntoSelect(SVI, SI))
return I;
// We cannot do this fold for elementwise select since ShuffleVector is
// not elementwise.
if (SI->getCondition()->getType()->isIntegerTy()) {
if (Instruction *I = FoldOpIntoSelect(SVI, SI))
return I;
}
Comment on lines +2907 to +2910
Copy link
Contributor

@MatzeB MatzeB Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could still perform this transformation when the shuffle operand types are the same as the shuffle output type? Regardless it is probably best to just land this change as quick as possible to unbreak things. I can look into re-enabling it for same-type myself then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a counterexample: https://alive2.llvm.org/ce/z/s8saCx.

}
if (auto *PN = dyn_cast<PHINode>(LHS)) {
if (Instruction *I = foldOpIntoPhi(SVI, PN))
Expand Down
24 changes: 24 additions & 0 deletions llvm/test/Transforms/InstCombine/vec_shuffle.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,30 @@ define <2 x i32> @foldselect0(i1 %c) {
ret <2 x i32> %shuf
}

; Make sure we do not crash in this case.
define <4 x float> @shuf_larger_length_vec_select(<2 x i1> %cond) {
; CHECK-LABEL: @shuf_larger_length_vec_select(
; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[COND:%.*]], <2 x float> zeroinitializer, <2 x float> <float 1.000000e+00, float 1.000000e+00>
; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <2 x float> [[SEL]], <2 x float> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
; CHECK-NEXT: ret <4 x float> [[SHUF]]
;
%sel = select <2 x i1> %cond, <2 x float> zeroinitializer, <2 x float> splat(float 1.000000e+00)
%shuf = shufflevector <2 x float> %sel, <2 x float> zeroinitializer, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
ret <4 x float> %shuf
}

; Make sure we do not fold in this case.
define <4 x i32> @shuf_same_length_vec_select(<4 x i1> %cond) {
; CHECK-LABEL: @shuf_same_length_vec_select(
; 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>
; 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>
; CHECK-NEXT: ret <4 x i32> [[SHUF]]
;
%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>
%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>
ret <4 x i32> %shuf
}

declare i1 @cond()
declare <4 x i32> @value()

Expand Down
Loading