-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[InstCombine] Fold shuffled intrinsic operands with constant operands #141300
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
Changes from 3 commits
cde5645
c0da580
575d406
171e648
89d4bca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1399,9 +1399,8 @@ static Instruction *factorizeMinMaxTree(IntrinsicInst *II) { | |
|
||
/// If all arguments of the intrinsic are unary shuffles with the same mask, | ||
/// try to shuffle after the intrinsic. | ||
static Instruction * | ||
foldShuffledIntrinsicOperands(IntrinsicInst *II, | ||
InstCombiner::BuilderTy &Builder) { | ||
Instruction * | ||
InstCombinerImpl::foldShuffledIntrinsicOperands(IntrinsicInst *II) { | ||
// TODO: This should be extended to handle other intrinsics like fshl, ctpop, | ||
// etc. Use llvm::isTriviallyVectorizable() and related to determine | ||
// which intrinsics are safe to shuffle? | ||
|
@@ -1419,9 +1418,11 @@ foldShuffledIntrinsicOperands(IntrinsicInst *II, | |
} | ||
|
||
Value *X; | ||
Constant *C; | ||
ArrayRef<int> Mask; | ||
if (!match(II->getArgOperand(0), | ||
m_Shuffle(m_Value(X), m_Undef(), m_Mask(Mask)))) | ||
auto *NonConstArg = find_if_not(II->args(), IsaPred<Constant>); | ||
if (!NonConstArg || | ||
!match(NonConstArg, m_Shuffle(m_Value(X), m_Undef(), m_Mask(Mask)))) | ||
return nullptr; | ||
|
||
// At least 1 operand must have 1 use because we are creating 2 instructions. | ||
|
@@ -1430,14 +1431,21 @@ foldShuffledIntrinsicOperands(IntrinsicInst *II, | |
|
||
// See if all arguments are shuffled with the same mask. | ||
SmallVector<Value *, 4> NewArgs(II->arg_size()); | ||
NewArgs[0] = X; | ||
Type *SrcTy = X->getType(); | ||
for (unsigned i = 1, e = II->arg_size(); i != e; ++i) { | ||
if (!match(II->getArgOperand(i), | ||
m_Shuffle(m_Value(X), m_Undef(), m_SpecificMask(Mask))) || | ||
X->getType() != SrcTy) | ||
for (unsigned i = 0, e = II->arg_size(); i != e; ++i) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use args() here (and push_back). |
||
if (match(II->getArgOperand(i), | ||
m_Shuffle(m_Value(X), m_Undef(), m_SpecificMask(Mask))) && | ||
X->getType() == SrcTy) | ||
NewArgs[i] = X; | ||
else if (match(II->getArgOperand(i), m_ImmConstant(C))) { | ||
// If it's a constant, try find the constant that would be shuffled to C. | ||
if (Constant *ShuffledC = | ||
unshuffleConstant(Mask, C, cast<VectorType>(SrcTy))) | ||
NewArgs[i] = ShuffledC; | ||
else | ||
return nullptr; | ||
} else | ||
return nullptr; | ||
NewArgs[i] = X; | ||
} | ||
|
||
// intrinsic (shuf X, M), (shuf Y, M), ... --> shuf (intrinsic X, Y, ...), M | ||
|
@@ -3849,7 +3857,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { | |
if (Instruction *R = FoldOpIntoSelect(*II, Sel)) | ||
return R; | ||
|
||
if (Instruction *Shuf = foldShuffledIntrinsicOperands(II, Builder)) | ||
if (Instruction *Shuf = foldShuffledIntrinsicOperands(II)) | ||
return Shuf; | ||
|
||
// Some intrinsics (like experimental_gc_statepoint) can be used in invoke | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -802,6 +802,67 @@ define <2 x float> @fma_unary_shuffle_ops_narrowing(<3 x float> %x, <3 x float> | |
ret <2 x float> %r | ||
} | ||
|
||
define <2 x float> @fma_unary_shuffle_ops_1_const(<2 x float> %x, <2 x float> %y) { | ||
; CHECK-LABEL: @fma_unary_shuffle_ops_1_const( | ||
; CHECK-NEXT: [[Y:%.*]] = call <2 x float> @llvm.fma.v2f32(<2 x float> [[X:%.*]], <2 x float> <float 2.000000e+00, float 1.000000e+00>, <2 x float> [[Y1:%.*]]) | ||
; CHECK-NEXT: [[B:%.*]] = shufflevector <2 x float> [[Y]], <2 x float> poison, <2 x i32> <i32 1, i32 0> | ||
; CHECK-NEXT: ret <2 x float> [[B]] | ||
; | ||
%a = shufflevector <2 x float> %x, <2 x float> poison, <2 x i32> <i32 1, i32 0> | ||
%b = shufflevector <2 x float> %y, <2 x float> poison, <2 x i32> <i32 1, i32 0> | ||
%r = call <2 x float> @llvm.fma(<2 x float> <float 1.0, float 2.0>, <2 x float> %a, <2 x float> %b) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, I found these tests a bit confusing because the order of arguments changed. It would be more obvious to put the constants in the second arg in the first place, rather than have InstCombine canonicalize them first. |
||
ret <2 x float> %r | ||
} | ||
|
||
define <2 x float> @fma_unary_shuffle_ops_2_const(<2 x float> %x) { | ||
; CHECK-LABEL: @fma_unary_shuffle_ops_2_const( | ||
; CHECK-NEXT: [[X:%.*]] = call <2 x float> @llvm.fma.v2f32(<2 x float> <float 2.000000e+00, float 1.000000e+00>, <2 x float> <float 2.000000e+00, float 1.000000e+00>, <2 x float> [[X1:%.*]]) | ||
; CHECK-NEXT: [[A:%.*]] = shufflevector <2 x float> [[X]], <2 x float> poison, <2 x i32> <i32 1, i32 0> | ||
; CHECK-NEXT: ret <2 x float> [[A]] | ||
; | ||
%a = shufflevector <2 x float> %x, <2 x float> poison, <2 x i32> <i32 1, i32 0> | ||
%r = call <2 x float> @llvm.fma(<2 x float> <float 1.0, float 2.0>, <2 x float> <float 1.0, float 2.0>, <2 x float> %a) | ||
ret <2 x float> %r | ||
} | ||
|
||
define <vscale x 2 x float> @fma_unary_shuffle_ops_1_const_scalable(<vscale x 2 x float> %x, <vscale x 2 x float> %y) { | ||
; CHECK-LABEL: @fma_unary_shuffle_ops_1_const_scalable( | ||
; CHECK-NEXT: [[R:%.*]] = call <vscale x 2 x float> @llvm.fma.nxv2f32(<vscale x 2 x float> [[A:%.*]], <vscale x 2 x float> splat (float 4.200000e+01), <vscale x 2 x float> [[B:%.*]]) | ||
; CHECK-NEXT: [[R1:%.*]] = shufflevector <vscale x 2 x float> [[R]], <vscale x 2 x float> poison, <vscale x 2 x i32> zeroinitializer | ||
; CHECK-NEXT: ret <vscale x 2 x float> [[R1]] | ||
; | ||
%a = shufflevector <vscale x 2 x float> %x, <vscale x 2 x float> poison, <vscale x 2 x i32> zeroinitializer | ||
%b = shufflevector <vscale x 2 x float> %y, <vscale x 2 x float> poison, <vscale x 2 x i32> zeroinitializer | ||
%r = call <vscale x 2 x float> @llvm.fma(<vscale x 2 x float> splat (float 42.0), <vscale x 2 x float> %a, <vscale x 2 x float> %b) | ||
ret <vscale x 2 x float> %r | ||
} | ||
|
||
define <vscale x 2 x float> @fma_unary_shuffle_ops_2_const_scalable(<vscale x 2 x float> %x) { | ||
; CHECK-LABEL: @fma_unary_shuffle_ops_2_const_scalable( | ||
; CHECK-NEXT: [[X:%.*]] = call <vscale x 2 x float> @llvm.fma.nxv2f32(<vscale x 2 x float> splat (float 4.200000e+01), <vscale x 2 x float> splat (float 4.200000e+01), <vscale x 2 x float> [[X1:%.*]]) | ||
; CHECK-NEXT: [[A:%.*]] = shufflevector <vscale x 2 x float> [[X]], <vscale x 2 x float> poison, <vscale x 2 x i32> zeroinitializer | ||
; CHECK-NEXT: ret <vscale x 2 x float> [[A]] | ||
; | ||
%a = shufflevector <vscale x 2 x float> %x, <vscale x 2 x float> poison, <vscale x 2 x i32> zeroinitializer | ||
%r = call <vscale x 2 x float> @llvm.fma(<vscale x 2 x float> splat (float 42.0), <vscale x 2 x float> splat (float 42.0), <vscale x 2 x float> %a) | ||
ret <vscale x 2 x float> %r | ||
} | ||
|
||
define <3 x float> @fma_unary_shuffle_ops_widening_1_const(<2 x float> %x, <2 x float> %y) { | ||
; CHECK-LABEL: @fma_unary_shuffle_ops_widening_1_const( | ||
; CHECK-NEXT: [[A:%.*]] = shufflevector <2 x float> [[X:%.*]], <2 x float> poison, <3 x i32> <i32 1, i32 0, i32 poison> | ||
; CHECK-NEXT: call void @use_vec3(<3 x float> [[A]]) | ||
; CHECK-NEXT: [[Y:%.*]] = call fast <2 x float> @llvm.fma.v2f32(<2 x float> [[X]], <2 x float> splat (float 4.200000e+01), <2 x float> [[Y1:%.*]]) | ||
; CHECK-NEXT: [[B:%.*]] = shufflevector <2 x float> [[Y]], <2 x float> poison, <3 x i32> <i32 1, i32 0, i32 poison> | ||
; CHECK-NEXT: ret <3 x float> [[B]] | ||
; | ||
%a = shufflevector <2 x float> %x, <2 x float> poison, <3 x i32> <i32 1, i32 0, i32 poison> | ||
call void @use_vec3(<3 x float> %a) | ||
%b = shufflevector <2 x float> %y, <2 x float> poison, <3 x i32> <i32 1, i32 0, i32 poison> | ||
%r = call fast <3 x float> @llvm.fma(<3 x float> splat (float 42.0), <3 x float> %a, <3 x float> %b) | ||
ret <3 x float> %r | ||
} | ||
|
||
; negative test - must have 3 shuffles | ||
|
||
define <2 x float> @fma_unary_shuffle_ops_unshuffled(<2 x float> %x, <2 x float> %y, <2 x float> %z) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to replace m_Undef -> m_Poison in a followup, for clarity.