Skip to content

Commit

Permalink
[IR][NFC] Change UndefMaskElem to PoisonMaskElem
Browse files Browse the repository at this point in the history
Following the change in shufflevector semantics,
poison will be used to represent undefined elements in shufflevector masks.

Differential Revision: https://reviews.llvm.org/D149256
  • Loading branch information
ManuelJBrito committed Apr 27, 2023
1 parent 1e74373 commit d22edb9
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 196 deletions.
4 changes: 2 additions & 2 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -4093,8 +4093,8 @@ int LLVMGetUndefMaskElem(void);
* Get the mask value at position Elt in the mask of a ShuffleVector
* instruction.
*
* \Returns the result of \c LLVMGetUndefMaskElem() if the mask value is undef
* at that position.
* \Returns the result of \c LLVMGetUndefMaskElem() if the mask value is
* poison at that position.
*/
int LLVMGetMaskValue(LLVMValueRef ShuffleVectorInst, unsigned Elt);

Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
APInt DemandedDstElts =
APInt::getZero(Shuffle->getShuffleMask().size());
for (auto I : enumerate(Shuffle->getShuffleMask())) {
if (I.value() != UndefMaskElem)
if (I.value() != PoisonMaskElem)
DemandedDstElts.setBit(I.index());
}
return TargetTTI->getReplicationShuffleCost(
Expand Down
10 changes: 5 additions & 5 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2002,15 +2002,15 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
// ShuffleVectorInst Class
//===----------------------------------------------------------------------===//

constexpr int UndefMaskElem = -1;
constexpr int PoisonMaskElem = -1;

/// This instruction constructs a fixed permutation of two
/// input vectors.
///
/// For each element of the result vector, the shuffle mask selects an element
/// from one of the input vectors to copy to the result. Non-negative elements
/// in the mask represent an index into the concatenated pair of input vectors.
/// UndefMaskElem (-1) specifies that the result element is undefined.
/// PoisonMaskElem (-1) specifies that the result element is poison.
///
/// For scalable vectors, all the elements of the mask must be 0 or -1. This
/// requirement may be relaxed in the future.
Expand Down Expand Up @@ -2068,16 +2068,16 @@ class ShuffleVectorInst : public Instruction {
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);

/// Return the shuffle mask value of this instruction for the given element
/// index. Return UndefMaskElem if the element is undef.
/// index. Return PoisonMaskElem if the element is undef.
int getMaskValue(unsigned Elt) const { return ShuffleMask[Elt]; }

/// Convert the input shuffle mask operand to a vector of integers. Undefined
/// elements of the mask are returned as UndefMaskElem.
/// elements of the mask are returned as PoisonMaskElem.
static void getShuffleMask(const Constant *Mask,
SmallVectorImpl<int> &Result);

/// Return the mask for this instruction as a vector of integers. Undefined
/// elements of the mask are returned as UndefMaskElem.
/// elements of the mask are returned as PoisonMaskElem.
void getShuffleMask(SmallVectorImpl<int> &Result) const {
Result.assign(ShuffleMask.begin(), ShuffleMask.end());
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5235,7 +5235,7 @@ static Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1,
ArrayRef<int> Mask, Type *RetTy,
const SimplifyQuery &Q,
unsigned MaxRecurse) {
if (all_of(Mask, [](int Elem) { return Elem == UndefMaskElem; }))
if (all_of(Mask, [](int Elem) { return Elem == PoisonMaskElem; }))
return UndefValue::get(RetTy);

auto *InVecTy = cast<VectorType>(Op0->getType());
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6438,7 +6438,7 @@ static bool canCreateUndefOrPoison(const Operator *Op, bool PoisonOnly,
ArrayRef<int> Mask = isa<ConstantExpr>(Op)
? cast<ConstantExpr>(Op)->getShuffleMask()
: cast<ShuffleVectorInst>(Op)->getShuffleMask();
return is_contained(Mask, UndefMaskElem);
return is_contained(Mask, PoisonMaskElem);
}
case Instruction::FNeg:
case Instruction::PHI:
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Analysis/VectorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,13 @@ void llvm::processShuffleMasks(
int Idx = I * SzDest + K;
if (Idx == Sz)
break;
if (Mask[Idx] >= Sz || Mask[Idx] == UndefMaskElem)
if (Mask[Idx] >= Sz || Mask[Idx] == PoisonMaskElem)
continue;
int SrcRegIdx = Mask[Idx] / SzSrc;
// Add a cost of PermuteTwoSrc for each new source register permute,
// if we have more than one source registers.
if (RegMasks[SrcRegIdx].empty())
RegMasks[SrcRegIdx].assign(SzDest, UndefMaskElem);
RegMasks[SrcRegIdx].assign(SzDest, PoisonMaskElem);
RegMasks[SrcRegIdx][K] = Mask[Idx] % SzSrc;
}
}
Expand Down Expand Up @@ -482,16 +482,16 @@ void llvm::processShuffleMasks(
auto &&CombineMasks = [](MutableArrayRef<int> FirstMask,
ArrayRef<int> SecondMask) {
for (int Idx = 0, VF = FirstMask.size(); Idx < VF; ++Idx) {
if (SecondMask[Idx] != UndefMaskElem) {
assert(FirstMask[Idx] == UndefMaskElem &&
if (SecondMask[Idx] != PoisonMaskElem) {
assert(FirstMask[Idx] == PoisonMaskElem &&
"Expected undefined mask element.");
FirstMask[Idx] = SecondMask[Idx] + VF;
}
}
};
auto &&NormalizeMask = [](MutableArrayRef<int> Mask) {
for (int Idx = 0, VF = Mask.size(); Idx < VF; ++Idx) {
if (Mask[Idx] != UndefMaskElem)
if (Mask[Idx] != PoisonMaskElem)
Mask[Idx] = Idx;
}
};
Expand Down
36 changes: 18 additions & 18 deletions llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2383,7 +2383,7 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
EVT EltVT = NewVT.getVectorElementType();
SmallVector<SDValue> Ops(NewElts, DAG.getUNDEF(EltVT));
for (unsigned I = 0; I < NewElts; ++I) {
if (Mask[I] == UndefMaskElem)
if (Mask[I] == PoisonMaskElem)
continue;
unsigned Idx = Mask[I];
if (Idx >= NewElts)
Expand Down Expand Up @@ -2423,20 +2423,20 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
// Use shuffles operands instead of shuffles themselves.
// 1. Adjust mask.
for (int &Idx : Mask) {
if (Idx == UndefMaskElem)
if (Idx == PoisonMaskElem)
continue;
unsigned SrcRegIdx = Idx / NewElts;
if (Inputs[SrcRegIdx].isUndef()) {
Idx = UndefMaskElem;
Idx = PoisonMaskElem;
continue;
}
auto *Shuffle =
dyn_cast<ShuffleVectorSDNode>(Inputs[SrcRegIdx].getNode());
if (!Shuffle || !is_contained(P.second, SrcRegIdx))
continue;
int MaskElt = Shuffle->getMaskElt(Idx % NewElts);
if (MaskElt == UndefMaskElem) {
Idx = UndefMaskElem;
if (MaskElt == PoisonMaskElem) {
Idx = PoisonMaskElem;
continue;
}
Idx = MaskElt % NewElts +
Expand All @@ -2455,11 +2455,11 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
// Check if any concat_vectors can be simplified.
SmallBitVector UsedSubVector(2 * std::size(Inputs));
for (int &Idx : Mask) {
if (Idx == UndefMaskElem)
if (Idx == PoisonMaskElem)
continue;
unsigned SrcRegIdx = Idx / NewElts;
if (Inputs[SrcRegIdx].isUndef()) {
Idx = UndefMaskElem;
Idx = PoisonMaskElem;
continue;
}
TargetLowering::LegalizeTypeAction TypeAction =
Expand Down Expand Up @@ -2489,7 +2489,7 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
if (!Pairs.empty() && Pairs.front().size() > 1) {
// Adjust mask.
for (int &Idx : Mask) {
if (Idx == UndefMaskElem)
if (Idx == PoisonMaskElem)
continue;
unsigned SrcRegIdx = Idx / NewElts;
auto *It = find_if(
Expand Down Expand Up @@ -2531,14 +2531,14 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
!Shuffle->getOperand(1).isUndef()) {
// Find the only used operand, if possible.
for (int &Idx : Mask) {
if (Idx == UndefMaskElem)
if (Idx == PoisonMaskElem)
continue;
unsigned SrcRegIdx = Idx / NewElts;
if (SrcRegIdx != I)
continue;
int MaskElt = Shuffle->getMaskElt(Idx % NewElts);
if (MaskElt == UndefMaskElem) {
Idx = UndefMaskElem;
if (MaskElt == PoisonMaskElem) {
Idx = PoisonMaskElem;
continue;
}
int OpIdx = MaskElt / NewElts;
Expand All @@ -2564,14 +2564,14 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
// Found that operand is used already.
// 1. Fix the mask for the reused operand.
for (int &Idx : Mask) {
if (Idx == UndefMaskElem)
if (Idx == PoisonMaskElem)
continue;
unsigned SrcRegIdx = Idx / NewElts;
if (SrcRegIdx != I)
continue;
int MaskElt = Shuffle->getMaskElt(Idx % NewElts);
if (MaskElt == UndefMaskElem) {
Idx = UndefMaskElem;
if (MaskElt == PoisonMaskElem) {
Idx = PoisonMaskElem;
continue;
}
int MaskIdx = MaskElt / NewElts;
Expand All @@ -2588,7 +2588,7 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
Inputs[I] = Shuffle->getOperand(Op);
// Adjust mask.
for (int &Idx : Mask) {
if (Idx == UndefMaskElem)
if (Idx == PoisonMaskElem)
continue;
unsigned SrcRegIdx = Idx / NewElts;
if (SrcRegIdx != I)
Expand Down Expand Up @@ -2622,11 +2622,11 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
auto &&UniqueConstantVec = UniqueConstantInputs.takeVector();
unsigned ConstNum = UniqueConstantVec.size();
for (int &Idx : Mask) {
if (Idx == UndefMaskElem)
if (Idx == PoisonMaskElem)
continue;
unsigned SrcRegIdx = Idx / NewElts;
if (Inputs[SrcRegIdx].isUndef()) {
Idx = UndefMaskElem;
Idx = PoisonMaskElem;
continue;
}
const auto It = find(UniqueConstantVec, Inputs[SrcRegIdx]);
Expand Down Expand Up @@ -2655,7 +2655,7 @@ void DAGTypeLegalizer::SplitVecRes_VECTOR_SHUFFLE(ShuffleVectorSDNode *N,
// Build a shuffle mask for the output, discovering on the fly which
// input vectors to use as shuffle operands.
unsigned FirstMaskIdx = High * NewElts;
SmallVector<int> Mask(NewElts * std::size(Inputs), UndefMaskElem);
SmallVector<int> Mask(NewElts * std::size(Inputs), PoisonMaskElem);
copy(ArrayRef(OrigMask).slice(FirstMaskIdx, NewElts), Mask.begin());
assert(!Output && "Expected default initialized initial value.");
TryPeekThroughShufflesInputs(Mask);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ static void PrintShuffleMask(raw_ostream &Out, Type *Ty, ArrayRef<int> Mask) {
bool FirstElt = true;
if (all_of(Mask, [](int Elt) { return Elt == 0; })) {
Out << "zeroinitializer";
} else if (all_of(Mask, [](int Elt) { return Elt == UndefMaskElem; })) {
} else if (all_of(Mask, [](int Elt) { return Elt == PoisonMaskElem; })) {
Out << "poison";
} else {
Out << "<";
Expand All @@ -435,7 +435,7 @@ static void PrintShuffleMask(raw_ostream &Out, Type *Ty, ArrayRef<int> Mask) {
else
Out << ", ";
Out << "i32 ";
if (Elt == UndefMaskElem)
if (Elt == PoisonMaskElem)
Out << "poison";
else
Out << Elt;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
Type *EltTy = V1VTy->getElementType();

// Undefined shuffle mask -> undefined value.
if (all_of(Mask, [](int Elt) { return Elt == UndefMaskElem; })) {
if (all_of(Mask, [](int Elt) { return Elt == PoisonMaskElem; })) {
return UndefValue::get(VectorType::get(EltTy, MaskEltCount));
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3949,7 +3949,7 @@ int LLVMGetMaskValue(LLVMValueRef SVInst, unsigned Elt) {
return I->getMaskValue(Elt);
}

int LLVMGetUndefMaskElem(void) { return UndefMaskElem; }
int LLVMGetUndefMaskElem(void) { return PoisonMaskElem; }

LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) {
Value *P = unwrap(AtomicInst);
Expand Down
20 changes: 10 additions & 10 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2164,8 +2164,8 @@ void ShuffleVectorInst::commute() {
SmallVector<int, 16> NewMask(NumMaskElts);
for (int i = 0; i != NumMaskElts; ++i) {
int MaskElt = getMaskValue(i);
if (MaskElt == UndefMaskElem) {
NewMask[i] = UndefMaskElem;
if (MaskElt == PoisonMaskElem) {
NewMask[i] = PoisonMaskElem;
continue;
}
assert(MaskElt >= 0 && MaskElt < 2 * NumOpElts && "Out-of-range mask");
Expand All @@ -2186,11 +2186,11 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
int V1Size =
cast<VectorType>(V1->getType())->getElementCount().getKnownMinValue();
for (int Elem : Mask)
if (Elem != UndefMaskElem && Elem >= V1Size * 2)
if (Elem != PoisonMaskElem && Elem >= V1Size * 2)
return false;

if (isa<ScalableVectorType>(V1->getType()))
if ((Mask[0] != 0 && Mask[0] != UndefMaskElem) || !all_equal(Mask))
if ((Mask[0] != 0 && Mask[0] != PoisonMaskElem) || !all_equal(Mask))
return false;

return true;
Expand Down Expand Up @@ -2289,7 +2289,7 @@ Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask,
}
SmallVector<Constant *, 16> MaskConst;
for (int Elem : Mask) {
if (Elem == UndefMaskElem)
if (Elem == PoisonMaskElem)
MaskConst.push_back(UndefValue::get(Int32Ty));
else
MaskConst.push_back(ConstantInt::get(Int32Ty, Elem));
Expand Down Expand Up @@ -2627,7 +2627,7 @@ static bool isReplicationMaskWithParams(ArrayRef<int> Mask,
"Run out of mask?");
Mask = Mask.drop_front(ReplicationFactor);
if (!all_of(CurrSubMask, [CurrElt](int MaskElt) {
return MaskElt == UndefMaskElem || MaskElt == CurrElt;
return MaskElt == PoisonMaskElem || MaskElt == CurrElt;
}))
return false;
}
Expand All @@ -2639,7 +2639,7 @@ static bool isReplicationMaskWithParams(ArrayRef<int> Mask,
bool ShuffleVectorInst::isReplicationMask(ArrayRef<int> Mask,
int &ReplicationFactor, int &VF) {
// undef-less case is trivial.
if (!llvm::is_contained(Mask, UndefMaskElem)) {
if (!llvm::is_contained(Mask, PoisonMaskElem)) {
ReplicationFactor =
Mask.take_while([](int MaskElt) { return MaskElt == 0; }).size();
if (ReplicationFactor == 0 || Mask.size() % ReplicationFactor != 0)
Expand All @@ -2657,7 +2657,7 @@ bool ShuffleVectorInst::isReplicationMask(ArrayRef<int> Mask,
// Before doing that, let's perform basic correctness checking first.
int Largest = -1;
for (int MaskElt : Mask) {
if (MaskElt == UndefMaskElem)
if (MaskElt == PoisonMaskElem)
continue;
// Elements must be in non-decreasing order.
if (MaskElt < Largest)
Expand Down Expand Up @@ -2703,11 +2703,11 @@ bool ShuffleVectorInst::isOneUseSingleSourceMask(ArrayRef<int> Mask, int VF) {
return false;
for (unsigned K = 0, Sz = Mask.size(); K < Sz; K += VF) {
ArrayRef<int> SubMask = Mask.slice(K, VF);
if (all_of(SubMask, [](int Idx) { return Idx == UndefMaskElem; }))
if (all_of(SubMask, [](int Idx) { return Idx == PoisonMaskElem; }))
continue;
SmallBitVector Used(VF, false);
for_each(SubMask, [&Used, VF](int Idx) {
if (Idx != UndefMaskElem && Idx < VF)
if (Idx != PoisonMaskElem && Idx < VF)
Used.set(Idx);
});
if (!Used.all())
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14744,9 +14744,9 @@ bool AArch64TargetLowering::lowerInterleavedStore(StoreInst *SI,
auto Mask = SVI->getShuffleMask();

// Sanity check if all the indices are NOT in range.
// If mask is `undef` or `poison`, `Mask` may be a vector of -1s.
// If all of them are `undef`, OOB read will happen later.
if (llvm::all_of(Mask, [](int Idx) { return Idx == UndefMaskElem; })) {
// If mask is `poison`, `Mask` may be a vector of -1s.
// If all of them are `poison`, OOB read will happen later.
if (llvm::all_of(Mask, [](int Idx) { return Idx == PoisonMaskElem; })) {
return false;
}
// A 64bit st2 which does not start at element 0 will involved adding extra
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3265,9 +3265,9 @@ InstructionCost AArch64TTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
unsigned NumSources = 0;
for (unsigned E = 0; E < LTNumElts; E++) {
int MaskElt = (N * LTNumElts + E < TpNumElts) ? Mask[N * LTNumElts + E]
: UndefMaskElem;
: PoisonMaskElem;
if (MaskElt < 0) {
NMask.push_back(UndefMaskElem);
NMask.push_back(PoisonMaskElem);
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/X86/X86TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ InstructionCost X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
LegalVT.getVectorNumElements() * std::max(NumOfSrcs, E);
unsigned NumOfSrcRegs = NormalizedVF / LegalVT.getVectorNumElements();
unsigned NumOfDestRegs = NormalizedVF / LegalVT.getVectorNumElements();
SmallVector<int> NormalizedMask(NormalizedVF, UndefMaskElem);
SmallVector<int> NormalizedMask(NormalizedVF, PoisonMaskElem);
copy(Mask, NormalizedMask.begin());
unsigned PrevSrcReg = 0;
ArrayRef<int> PrevRegMask;
Expand All @@ -1634,7 +1634,7 @@ InstructionCost X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
return;
}
if (SrcReg != DestReg &&
any_of(RegMask, [](int I) { return I != UndefMaskElem; })) {
any_of(RegMask, [](int I) { return I != PoisonMaskElem; })) {
// Just a copy of the source register.
Cost += TTI::TCC_Basic;
}
Expand Down

0 comments on commit d22edb9

Please sign in to comment.