Skip to content

Commit 44a4170

Browse files
committed
[MIPS][MSA] Handle UNDEFs in shuffle indices for VSHF
Currently VSHF does not handle UNDEF indices. However isSPLATI() is able to handle undefs, which may pass indices with undefs to this function. Adding a check to handle undefs in shuffle indices. Also, shuffle mask widened from v2 vector types are guranteed to contain UNDEFs. These shuffle lower logics can handle UNDEFs, so we just leave it as is, except for VSHF, which we must use whatever necessary to fill the UNDEFs. TODO: Remove CI skip after updating test cases. skip-checks: true
1 parent 0bcb1d6 commit 44a4170

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

llvm/lib/Target/Mips/MipsSEISelLowering.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "llvm/TargetParser/Triple.h"
4343
#include <algorithm>
4444
#include <cassert>
45+
#include <cstddef>
4546
#include <cstdint>
4647
#include <iterator>
4748
#include <utility>
@@ -2968,8 +2969,14 @@ static SDValue lowerVECTOR_SHUFFLE_PCKOD(SDValue Op, EVT ResTy,
29682969
// if the type is v8i16 and all the indices are less than 8 then the second
29692970
// operand is unused and can be replaced with anything. We choose to replace it
29702971
// with the used operand since this reduces the number of instructions overall.
2972+
//
2973+
// NOTE: SPLATI shuffle masks may contain UNDEFs, since isSPLATI() treats
2974+
// UNDEFs as same as SPLATI index.
2975+
// For other instances we use the last valid index if UNDEF is
2976+
// encountered.
29712977
static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
29722978
const SmallVector<int, 16> &Indices,
2979+
const bool isSPLATI,
29732980
SelectionDAG &DAG) {
29742981
SmallVector<SDValue, 16> Ops;
29752982
SDValue Op0;
@@ -2981,6 +2988,9 @@ static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
29812988
SDLoc DL(Op);
29822989
int ResTyNumElts = ResTy.getVectorNumElements();
29832990

2991+
assert(Indices[0] >= 0 &&
2992+
"shuffle mask starts with an UNDEF, which is not expected");
2993+
29842994
for (int i = 0; i < ResTyNumElts; ++i) {
29852995
// Idx == -1 means UNDEF
29862996
int Idx = Indices[i];
@@ -2990,9 +3000,17 @@ static SDValue lowerVECTOR_SHUFFLE_VSHF(SDValue Op, EVT ResTy,
29903000
if (ResTyNumElts <= Idx && Idx < ResTyNumElts * 2)
29913001
Using2ndVec = true;
29923002
}
2993-
2994-
for (int Idx : Indices)
3003+
int LastValidIndex = 0;
3004+
for (size_t i = 0; i < Indices.size(); i++) {
3005+
int Idx = Indices[i];
3006+
if (Idx < 0) {
3007+
// Continue using splati index or use the last valid index.
3008+
Idx = isSPLATI ? Indices[0] : LastValidIndex;
3009+
} else {
3010+
LastValidIndex = Idx;
3011+
}
29953012
Ops.push_back(DAG.getTargetConstant(Idx, DL, MaskEltTy));
3013+
}
29963014

29973015
SDValue MaskVec = DAG.getBuildVector(MaskVecTy, DL, Ops);
29983016

@@ -3035,7 +3053,7 @@ SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
30353053
// splati.[bhwd] is preferable to the others but is matched from
30363054
// MipsISD::VSHF.
30373055
if (isVECTOR_SHUFFLE_SPLATI(Op, ResTy, Indices, DAG))
3038-
return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
3056+
return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, true, DAG);
30393057
SDValue Result;
30403058
if ((Result = lowerVECTOR_SHUFFLE_ILVEV(Op, ResTy, Indices, DAG)))
30413059
return Result;
@@ -3051,7 +3069,7 @@ SDValue MipsSETargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
30513069
return Result;
30523070
if ((Result = lowerVECTOR_SHUFFLE_SHF(Op, ResTy, Indices, DAG)))
30533071
return Result;
3054-
return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, DAG);
3072+
return lowerVECTOR_SHUFFLE_VSHF(Op, ResTy, Indices, false, DAG);
30553073
}
30563074

30573075
MachineBasicBlock *

0 commit comments

Comments
 (0)