Skip to content

Commit 0738953

Browse files
committed
Revert "[IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst."
This reverts commit b186f1f. Causes crashes, see https://reviews.llvm.org/D158449.
1 parent 28245b4 commit 0738953

File tree

15 files changed

+296
-490
lines changed

15 files changed

+296
-490
lines changed

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -935,28 +935,31 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
935935
ArrayRef<int> Mask,
936936
VectorType *Ty, int &Index,
937937
VectorType *&SubTy) const {
938-
if (Mask.empty())
938+
int Limit = Mask.size() * 2;
939+
if (Mask.empty() ||
940+
// Extra check required by isSingleSourceMaskImpl function (called by
941+
// ShuffleVectorInst::isSingleSourceMask).
942+
any_of(Mask, [Limit](int I) { return I >= Limit; }))
939943
return Kind;
940-
int NumSrcElts = Ty->getElementCount().getKnownMinValue();
941944
switch (Kind) {
942945
case TTI::SK_PermuteSingleSrc:
943-
if (ShuffleVectorInst::isReverseMask(Mask, NumSrcElts))
946+
if (ShuffleVectorInst::isReverseMask(Mask))
944947
return TTI::SK_Reverse;
945-
if (ShuffleVectorInst::isZeroEltSplatMask(Mask, NumSrcElts))
948+
if (ShuffleVectorInst::isZeroEltSplatMask(Mask))
946949
return TTI::SK_Broadcast;
947950
break;
948951
case TTI::SK_PermuteTwoSrc: {
949952
int NumSubElts;
950953
if (Mask.size() > 2 && ShuffleVectorInst::isInsertSubvectorMask(
951-
Mask, NumSrcElts, NumSubElts, Index)) {
954+
Mask, Mask.size(), NumSubElts, Index)) {
952955
SubTy = FixedVectorType::get(Ty->getElementType(), NumSubElts);
953956
return TTI::SK_InsertSubvector;
954957
}
955-
if (ShuffleVectorInst::isSelectMask(Mask, NumSrcElts))
958+
if (ShuffleVectorInst::isSelectMask(Mask))
956959
return TTI::SK_Select;
957-
if (ShuffleVectorInst::isTransposeMask(Mask, NumSrcElts))
960+
if (ShuffleVectorInst::isTransposeMask(Mask))
958961
return TTI::SK_Transpose;
959-
if (ShuffleVectorInst::isSpliceMask(Mask, NumSrcElts, Index))
962+
if (ShuffleVectorInst::isSpliceMask(Mask, Index))
960963
return TTI::SK_Splice;
961964
break;
962965
}

llvm/include/llvm/IR/Instructions.h

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,32 +2068,30 @@ class ShuffleVectorInst : public Instruction {
20682068
/// Return true if this shuffle mask chooses elements from exactly one source
20692069
/// vector.
20702070
/// Example: <7,5,undef,7>
2071-
/// This assumes that vector operands (of length \p NumSrcElts) are the same
2072-
/// length as the mask.
2073-
static bool isSingleSourceMask(ArrayRef<int> Mask, int NumSrcElts);
2074-
static bool isSingleSourceMask(const Constant *Mask, int NumSrcElts) {
2071+
/// This assumes that vector operands are the same length as the mask.
2072+
static bool isSingleSourceMask(ArrayRef<int> Mask);
2073+
static bool isSingleSourceMask(const Constant *Mask) {
20752074
assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
20762075
SmallVector<int, 16> MaskAsInts;
20772076
getShuffleMask(Mask, MaskAsInts);
2078-
return isSingleSourceMask(MaskAsInts, NumSrcElts);
2077+
return isSingleSourceMask(MaskAsInts);
20792078
}
20802079

20812080
/// Return true if this shuffle chooses elements from exactly one source
20822081
/// vector without changing the length of that vector.
20832082
/// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
20842083
/// TODO: Optionally allow length-changing shuffles.
20852084
bool isSingleSource() const {
2086-
return !changesLength() &&
2087-
isSingleSourceMask(ShuffleMask, ShuffleMask.size());
2085+
return !changesLength() && isSingleSourceMask(ShuffleMask);
20882086
}
20892087

20902088
/// Return true if this shuffle mask chooses elements from exactly one source
20912089
/// vector without lane crossings. A shuffle using this mask is not
20922090
/// necessarily a no-op because it may change the number of elements from its
20932091
/// input vectors or it may provide demanded bits knowledge via undef lanes.
20942092
/// Example: <undef,undef,2,3>
2095-
static bool isIdentityMask(ArrayRef<int> Mask, int NumSrcElts);
2096-
static bool isIdentityMask(const Constant *Mask, int NumSrcElts) {
2093+
static bool isIdentityMask(ArrayRef<int> Mask);
2094+
static bool isIdentityMask(const Constant *Mask) {
20972095
assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
20982096

20992097
// Not possible to express a shuffle mask for a scalable vector for this
@@ -2103,7 +2101,7 @@ class ShuffleVectorInst : public Instruction {
21032101

21042102
SmallVector<int, 16> MaskAsInts;
21052103
getShuffleMask(Mask, MaskAsInts);
2106-
return isIdentityMask(MaskAsInts, NumSrcElts);
2104+
return isIdentityMask(MaskAsInts);
21072105
}
21082106

21092107
/// Return true if this shuffle chooses elements from exactly one source
@@ -2116,7 +2114,7 @@ class ShuffleVectorInst : public Instruction {
21162114
if (isa<ScalableVectorType>(getType()))
21172115
return false;
21182116

2119-
return !changesLength() && isIdentityMask(ShuffleMask, ShuffleMask.size());
2117+
return !changesLength() && isIdentityMask(ShuffleMask);
21202118
}
21212119

21222120
/// Return true if this shuffle lengthens exactly one source vector with
@@ -2140,12 +2138,12 @@ class ShuffleVectorInst : public Instruction {
21402138
/// In that case, the shuffle is better classified as an identity shuffle.
21412139
/// This assumes that vector operands are the same length as the mask
21422140
/// (a length-changing shuffle can never be equivalent to a vector select).
2143-
static bool isSelectMask(ArrayRef<int> Mask, int NumSrcElts);
2144-
static bool isSelectMask(const Constant *Mask, int NumSrcElts) {
2141+
static bool isSelectMask(ArrayRef<int> Mask);
2142+
static bool isSelectMask(const Constant *Mask) {
21452143
assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
21462144
SmallVector<int, 16> MaskAsInts;
21472145
getShuffleMask(Mask, MaskAsInts);
2148-
return isSelectMask(MaskAsInts, NumSrcElts);
2146+
return isSelectMask(MaskAsInts);
21492147
}
21502148

21512149
/// Return true if this shuffle chooses elements from its source vectors
@@ -2157,41 +2155,39 @@ class ShuffleVectorInst : public Instruction {
21572155
/// In that case, the shuffle is better classified as an identity shuffle.
21582156
/// TODO: Optionally allow length-changing shuffles.
21592157
bool isSelect() const {
2160-
return !changesLength() && isSelectMask(ShuffleMask, ShuffleMask.size());
2158+
return !changesLength() && isSelectMask(ShuffleMask);
21612159
}
21622160

21632161
/// Return true if this shuffle mask swaps the order of elements from exactly
21642162
/// one source vector.
21652163
/// Example: <7,6,undef,4>
2166-
/// This assumes that vector operands (of length \p NumSrcElts) are the same
2167-
/// length as the mask.
2168-
static bool isReverseMask(ArrayRef<int> Mask, int NumSrcElts);
2169-
static bool isReverseMask(const Constant *Mask, int NumSrcElts) {
2164+
/// This assumes that vector operands are the same length as the mask.
2165+
static bool isReverseMask(ArrayRef<int> Mask);
2166+
static bool isReverseMask(const Constant *Mask) {
21702167
assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
21712168
SmallVector<int, 16> MaskAsInts;
21722169
getShuffleMask(Mask, MaskAsInts);
2173-
return isReverseMask(MaskAsInts, NumSrcElts);
2170+
return isReverseMask(MaskAsInts);
21742171
}
21752172

21762173
/// Return true if this shuffle swaps the order of elements from exactly
21772174
/// one source vector.
21782175
/// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
21792176
/// TODO: Optionally allow length-changing shuffles.
21802177
bool isReverse() const {
2181-
return !changesLength() && isReverseMask(ShuffleMask, ShuffleMask.size());
2178+
return !changesLength() && isReverseMask(ShuffleMask);
21822179
}
21832180

21842181
/// Return true if this shuffle mask chooses all elements with the same value
21852182
/// as the first element of exactly one source vector.
21862183
/// Example: <4,undef,undef,4>
2187-
/// This assumes that vector operands (of length \p NumSrcElts) are the same
2188-
/// length as the mask.
2189-
static bool isZeroEltSplatMask(ArrayRef<int> Mask, int NumSrcElts);
2190-
static bool isZeroEltSplatMask(const Constant *Mask, int NumSrcElts) {
2184+
/// This assumes that vector operands are the same length as the mask.
2185+
static bool isZeroEltSplatMask(ArrayRef<int> Mask);
2186+
static bool isZeroEltSplatMask(const Constant *Mask) {
21912187
assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
21922188
SmallVector<int, 16> MaskAsInts;
21932189
getShuffleMask(Mask, MaskAsInts);
2194-
return isZeroEltSplatMask(MaskAsInts, NumSrcElts);
2190+
return isZeroEltSplatMask(MaskAsInts);
21952191
}
21962192

21972193
/// Return true if all elements of this shuffle are the same value as the
@@ -2201,8 +2197,7 @@ class ShuffleVectorInst : public Instruction {
22012197
/// TODO: Optionally allow length-changing shuffles.
22022198
/// TODO: Optionally allow splats from other elements.
22032199
bool isZeroEltSplat() const {
2204-
return !changesLength() &&
2205-
isZeroEltSplatMask(ShuffleMask, ShuffleMask.size());
2200+
return !changesLength() && isZeroEltSplatMask(ShuffleMask);
22062201
}
22072202

22082203
/// Return true if this shuffle mask is a transpose mask.
@@ -2237,12 +2232,12 @@ class ShuffleVectorInst : public Instruction {
22372232
/// ; Transposed matrix
22382233
/// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 >
22392234
/// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 >
2240-
static bool isTransposeMask(ArrayRef<int> Mask, int NumSrcElts);
2241-
static bool isTransposeMask(const Constant *Mask, int NumSrcElts) {
2235+
static bool isTransposeMask(ArrayRef<int> Mask);
2236+
static bool isTransposeMask(const Constant *Mask) {
22422237
assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
22432238
SmallVector<int, 16> MaskAsInts;
22442239
getShuffleMask(Mask, MaskAsInts);
2245-
return isTransposeMask(MaskAsInts, NumSrcElts);
2240+
return isTransposeMask(MaskAsInts);
22462241
}
22472242

22482243
/// Return true if this shuffle transposes the elements of its inputs without
@@ -2251,30 +2246,27 @@ class ShuffleVectorInst : public Instruction {
22512246
/// exact specification.
22522247
/// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
22532248
bool isTranspose() const {
2254-
return !changesLength() && isTransposeMask(ShuffleMask, ShuffleMask.size());
2249+
return !changesLength() && isTransposeMask(ShuffleMask);
22552250
}
22562251

22572252
/// Return true if this shuffle mask is a splice mask, concatenating the two
22582253
/// inputs together and then extracts an original width vector starting from
22592254
/// the splice index.
22602255
/// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
2261-
/// This assumes that vector operands (of length \p NumSrcElts) are the same
2262-
/// length as the mask.
2263-
static bool isSpliceMask(ArrayRef<int> Mask, int NumSrcElts, int &Index);
2264-
static bool isSpliceMask(const Constant *Mask, int NumSrcElts, int &Index) {
2256+
static bool isSpliceMask(ArrayRef<int> Mask, int &Index);
2257+
static bool isSpliceMask(const Constant *Mask, int &Index) {
22652258
assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
22662259
SmallVector<int, 16> MaskAsInts;
22672260
getShuffleMask(Mask, MaskAsInts);
2268-
return isSpliceMask(MaskAsInts, NumSrcElts, Index);
2261+
return isSpliceMask(MaskAsInts, Index);
22692262
}
22702263

22712264
/// Return true if this shuffle splices two inputs without changing the length
22722265
/// of the vectors. This operation concatenates the two inputs together and
22732266
/// then extracts an original width vector starting from the splice index.
22742267
/// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
22752268
bool isSplice(int &Index) const {
2276-
return !changesLength() &&
2277-
isSpliceMask(ShuffleMask, ShuffleMask.size(), Index);
2269+
return !changesLength() && isSpliceMask(ShuffleMask, Index);
22782270
}
22792271

22802272
/// Return true if this shuffle mask is an extract subvector mask.

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24151,7 +24151,7 @@ static SDValue foldExtractSubvectorFromShuffleVector(SDNode *N,
2415124151

2415224152
// Profitability check: only deal with extractions from the first subvector
2415324153
// unless the mask becomes an identity mask.
24154-
if (!ShuffleVectorInst::isIdentityMask(NewMask, NewMask.size()) ||
24154+
if (!ShuffleVectorInst::isIdentityMask(NewMask) ||
2415524155
any_of(NewMask, [](int M) { return M < 0; }))
2415624156
for (auto &DemandedSubvector : DemandedSubvectors)
2415724157
if (DemandedSubvector.second != 0)

llvm/lib/IR/Instructions.cpp

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,10 +2136,10 @@ static bool isSingleSourceMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
21362136
return UsesLHS || UsesRHS;
21372137
}
21382138

2139-
bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask, int NumSrcElts) {
2139+
bool ShuffleVectorInst::isSingleSourceMask(ArrayRef<int> Mask) {
21402140
// We don't have vector operand size information, so assume operands are the
21412141
// same size as the mask.
2142-
return isSingleSourceMaskImpl(Mask, NumSrcElts);
2142+
return isSingleSourceMaskImpl(Mask, Mask.size());
21432143
}
21442144

21452145
static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
@@ -2154,75 +2154,65 @@ static bool isIdentityMaskImpl(ArrayRef<int> Mask, int NumOpElts) {
21542154
return true;
21552155
}
21562156

2157-
bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask, int NumSrcElts) {
2158-
if (Mask.size() != static_cast<unsigned>(NumSrcElts))
2159-
return false;
2157+
bool ShuffleVectorInst::isIdentityMask(ArrayRef<int> Mask) {
21602158
// We don't have vector operand size information, so assume operands are the
21612159
// same size as the mask.
2162-
return isIdentityMaskImpl(Mask, NumSrcElts);
2160+
return isIdentityMaskImpl(Mask, Mask.size());
21632161
}
21642162

2165-
bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask, int NumSrcElts) {
2166-
if (Mask.size() != static_cast<unsigned>(NumSrcElts))
2167-
return false;
2168-
if (!isSingleSourceMask(Mask, NumSrcElts))
2163+
bool ShuffleVectorInst::isReverseMask(ArrayRef<int> Mask) {
2164+
if (!isSingleSourceMask(Mask))
21692165
return false;
21702166

21712167
// The number of elements in the mask must be at least 2.
2172-
if (NumSrcElts < 2)
2168+
int NumElts = Mask.size();
2169+
if (NumElts < 2)
21732170
return false;
21742171

2175-
for (int I = 0, E = Mask.size(); I < E; ++I) {
2176-
if (Mask[I] == -1)
2172+
for (int i = 0; i < NumElts; ++i) {
2173+
if (Mask[i] == -1)
21772174
continue;
2178-
if (Mask[I] != (NumSrcElts - 1 - I) &&
2179-
Mask[I] != (NumSrcElts + NumSrcElts - 1 - I))
2175+
if (Mask[i] != (NumElts - 1 - i) && Mask[i] != (NumElts + NumElts - 1 - i))
21802176
return false;
21812177
}
21822178
return true;
21832179
}
21842180

2185-
bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask, int NumSrcElts) {
2186-
if (Mask.size() != static_cast<unsigned>(NumSrcElts))
2187-
return false;
2188-
if (!isSingleSourceMask(Mask, NumSrcElts))
2181+
bool ShuffleVectorInst::isZeroEltSplatMask(ArrayRef<int> Mask) {
2182+
if (!isSingleSourceMask(Mask))
21892183
return false;
2190-
for (int I = 0, E = Mask.size(); I < E; ++I) {
2191-
if (Mask[I] == -1)
2184+
for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
2185+
if (Mask[i] == -1)
21922186
continue;
2193-
if (Mask[I] != 0 && Mask[I] != NumSrcElts)
2187+
if (Mask[i] != 0 && Mask[i] != NumElts)
21942188
return false;
21952189
}
21962190
return true;
21972191
}
21982192

2199-
bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask, int NumSrcElts) {
2200-
if (Mask.size() != static_cast<unsigned>(NumSrcElts))
2201-
return false;
2193+
bool ShuffleVectorInst::isSelectMask(ArrayRef<int> Mask) {
22022194
// Select is differentiated from identity. It requires using both sources.
2203-
if (isSingleSourceMask(Mask, NumSrcElts))
2195+
if (isSingleSourceMask(Mask))
22042196
return false;
2205-
for (int I = 0, E = Mask.size(); I < E; ++I) {
2206-
if (Mask[I] == -1)
2197+
for (int i = 0, NumElts = Mask.size(); i < NumElts; ++i) {
2198+
if (Mask[i] == -1)
22072199
continue;
2208-
if (Mask[I] != I && Mask[I] != (NumSrcElts + I))
2200+
if (Mask[i] != i && Mask[i] != (NumElts + i))
22092201
return false;
22102202
}
22112203
return true;
22122204
}
22132205

2214-
bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask, int NumSrcElts) {
2206+
bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask) {
22152207
// Example masks that will return true:
22162208
// v1 = <a, b, c, d>
22172209
// v2 = <e, f, g, h>
22182210
// trn1 = shufflevector v1, v2 <0, 4, 2, 6> = <a, e, c, g>
22192211
// trn2 = shufflevector v1, v2 <1, 5, 3, 7> = <b, f, d, h>
22202212

2221-
if (Mask.size() != static_cast<unsigned>(NumSrcElts))
2222-
return false;
22232213
// 1. The number of elements in the mask must be a power-of-2 and at least 2.
2224-
int Sz = Mask.size();
2225-
if (Sz < 2 || !isPowerOf2_32(Sz))
2214+
int NumElts = Mask.size();
2215+
if (NumElts < 2 || !isPowerOf2_32(NumElts))
22262216
return false;
22272217

22282218
// 2. The first element of the mask must be either a 0 or a 1.
@@ -2231,26 +2221,23 @@ bool ShuffleVectorInst::isTransposeMask(ArrayRef<int> Mask, int NumSrcElts) {
22312221

22322222
// 3. The difference between the first 2 elements must be equal to the
22332223
// number of elements in the mask.
2234-
if ((Mask[1] - Mask[0]) != NumSrcElts)
2224+
if ((Mask[1] - Mask[0]) != NumElts)
22352225
return false;
22362226

22372227
// 4. The difference between consecutive even-numbered and odd-numbered
22382228
// elements must be equal to 2.
2239-
for (int I = 2; I < Sz; ++I) {
2240-
int MaskEltVal = Mask[I];
2229+
for (int i = 2; i < NumElts; ++i) {
2230+
int MaskEltVal = Mask[i];
22412231
if (MaskEltVal == -1)
22422232
return false;
2243-
int MaskEltPrevVal = Mask[I - 2];
2233+
int MaskEltPrevVal = Mask[i - 2];
22442234
if (MaskEltVal - MaskEltPrevVal != 2)
22452235
return false;
22462236
}
22472237
return true;
22482238
}
22492239

2250-
bool ShuffleVectorInst::isSpliceMask(ArrayRef<int> Mask, int NumSrcElts,
2251-
int &Index) {
2252-
if (Mask.size() != static_cast<unsigned>(NumSrcElts))
2253-
return false;
2240+
bool ShuffleVectorInst::isSpliceMask(ArrayRef<int> Mask, int &Index) {
22542241
// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
22552242
int StartIndex = -1;
22562243
for (int I = 0, E = Mask.size(); I != E; ++I) {
@@ -2261,7 +2248,7 @@ bool ShuffleVectorInst::isSpliceMask(ArrayRef<int> Mask, int NumSrcElts,
22612248
if (StartIndex == -1) {
22622249
// Don't support a StartIndex that begins in the second input, or if the
22632250
// first non-undef index would access below the StartIndex.
2264-
if (MaskEltVal < I || NumSrcElts <= (MaskEltVal - I))
2251+
if (MaskEltVal < I || E <= (MaskEltVal - I))
22652252
return false;
22662253

22672254
StartIndex = MaskEltVal - I;
@@ -2556,7 +2543,7 @@ bool ShuffleVectorInst::isOneUseSingleSourceMask(int VF) const {
25562543
// case.
25572544
if (isa<ScalableVectorType>(getType()))
25582545
return false;
2559-
if (!isSingleSourceMask(ShuffleMask, VF))
2546+
if (!isSingleSourceMask(ShuffleMask))
25602547
return false;
25612548

25622549
return isOneUseSingleSourceMask(ShuffleMask, VF);

0 commit comments

Comments
 (0)