Skip to content

Commit 0397226

Browse files
committed
[SLP]Fix PR90892: do a correct sign analysis of the entries elements in gather shuffles.
Need to do extra analysis of the scalar elements of the tree entry to be shuffled instead of the vectorized value to correctly deduce signedness info.
1 parent 48039b1 commit 0397226

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11640,13 +11640,14 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis {
1164011640

1164111641
/// Cast value \p V to the vector type with the same number of elements, but
1164211642
/// the base type \p ScalarTy.
11643-
Value *castToScalarTyElem(Value *V) {
11643+
Value *castToScalarTyElem(Value *V,
11644+
std::optional<bool> IsSigned = std::nullopt) {
1164411645
auto *VecTy = cast<VectorType>(V->getType());
1164511646
if (VecTy->getElementType() == ScalarTy)
1164611647
return V;
1164711648
return Builder.CreateIntCast(
1164811649
V, VectorType::get(ScalarTy, VecTy->getElementCount()),
11649-
!isKnownNonNegative(V, SimplifyQuery(*R.DL)));
11650+
IsSigned ? *IsSigned : !isKnownNonNegative(V, SimplifyQuery(*R.DL)));
1165011651
}
1165111652

1165211653
public:
@@ -11795,12 +11796,30 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis {
1179511796
/// Adds 2 input vectors (in form of tree entries) and the mask for their
1179611797
/// shuffling.
1179711798
void add(const TreeEntry &E1, const TreeEntry &E2, ArrayRef<int> Mask) {
11798-
add(E1.VectorizedValue, E2.VectorizedValue, Mask);
11799+
Value *V1 = E1.VectorizedValue;
11800+
if (V1->getType()->isIntOrIntVectorTy())
11801+
V1 = castToScalarTyElem(V1, all_of(E1.Scalars, [&](Value *V) {
11802+
return !isKnownNonNegative(
11803+
V, SimplifyQuery(*R.DL));
11804+
}));
11805+
Value *V2 = E2.VectorizedValue;
11806+
if (V2->getType()->isIntOrIntVectorTy())
11807+
V2 = castToScalarTyElem(V2, all_of(E2.Scalars, [&](Value *V) {
11808+
return !isKnownNonNegative(
11809+
V, SimplifyQuery(*R.DL));
11810+
}));
11811+
add(V1, V2, Mask);
1179911812
}
1180011813
/// Adds single input vector (in form of tree entry) and the mask for its
1180111814
/// shuffling.
1180211815
void add(const TreeEntry &E1, ArrayRef<int> Mask) {
11803-
add(E1.VectorizedValue, Mask);
11816+
Value *V1 = E1.VectorizedValue;
11817+
if (V1->getType()->isIntOrIntVectorTy())
11818+
V1 = castToScalarTyElem(V1, all_of(E1.Scalars, [&](Value *V) {
11819+
return !isKnownNonNegative(
11820+
V, SimplifyQuery(*R.DL));
11821+
}));
11822+
add(V1, Mask);
1180411823
}
1180511824
/// Adds 2 input vectors and the mask for their shuffling.
1180611825
void add(Value *V1, Value *V2, ArrayRef<int> Mask) {

llvm/test/Transforms/SLPVectorizer/RISCV/shuffled-gather-casted.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ define i32 @test(ptr %p) {
99
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <4 x i16> <i16 0, i16 poison, i16 0, i16 0>, i16 [[D_0]], i32 1
1010
; CHECK-NEXT: [[TMP1:%.*]] = or <4 x i16> [[TMP0]], zeroinitializer
1111
; CHECK-NEXT: [[TMP2:%.*]] = and <4 x i16> [[TMP1]], zeroinitializer
12-
; CHECK-NEXT: [[TMP3:%.*]] = sext <4 x i16> [[TMP0]] to <4 x i32>
12+
; CHECK-NEXT: [[TMP3:%.*]] = zext <4 x i16> [[TMP0]] to <4 x i32>
1313
; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <4 x i32> [[TMP3]], <4 x i32> <i32 0, i32 poison, i32 0, i32 0>, <4 x i32> <i32 4, i32 1, i32 6, i32 7>
1414
; CHECK-NEXT: [[TMP5:%.*]] = icmp sgt <4 x i32> [[TMP4]], zeroinitializer
1515
; CHECK-NEXT: [[TMP6:%.*]] = select <4 x i1> [[TMP5]], <4 x i16> [[TMP2]], <4 x i16> <i16 0, i16 2, i16 0, i16 0>

0 commit comments

Comments
 (0)