-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[SLP][REVEC] Make ShuffleCostEstimator and ShuffleInstructionBuilder support vector instructions. #99499
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
Merged
HanKuanChen
merged 11 commits into
llvm:main
from
HanKuanChen:slp-revec-processBuildVector
Aug 7, 2024
Merged
[SLP][REVEC] Make ShuffleCostEstimator and ShuffleInstructionBuilder support vector instructions. #99499
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
968f97d
[SLP][REVEC] Pre-commit test.
HanKuanChen c43c8e4
[SLP][REVEC] Make ShuffleCostEstimator::gather support vector
HanKuanChen fdaef59
[SLP][REVEC] Make BoUpSLP::gather support vector instructions.
HanKuanChen 1bfa95d
[SLP][REVEC] NFC. Add transformScalarShuffleIndiciesToVector.
HanKuanChen 534f25c
[SLP][REVEC] Make ShuffleInstructionBuilder::finalize support vector
HanKuanChen 3e632e1
[SLP][REVEC] NFC. Add a helper function to get VF for
HanKuanChen a58871f
[SLP][REVEC] Make ShuffleCostEstimator::add and
HanKuanChen 201cf72
[SLP][REVEC] Make ShuffleCostEstimator::createShuffle support vector
HanKuanChen ed8f565
[SLP][REVEC] Make ExtractAndExtendIfNeeded support vector instructions.
HanKuanChen ccd6f2b
[SLP][REVEC] Apply comments.
HanKuanChen ae35ae0
[SLP][REVEC] Apply comments.
HanKuanChen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,6 +253,21 @@ static FixedVectorType *getWidenedType(Type *ScalarTy, unsigned VF) { | |
VF * getNumElements(ScalarTy)); | ||
} | ||
|
||
static void transformScalarShuffleIndiciesToVector(unsigned VecTyNumElements, | ||
SmallVectorImpl<int> &Mask) { | ||
// The ShuffleBuilder implementation use shufflevector to splat an "element". | ||
// But the element have different meaning for SLP (scalar) and REVEC | ||
// (vector). We need to expand Mask into masks which shufflevector can use | ||
// directly. | ||
SmallVector<int> NewMask(Mask.size() * VecTyNumElements); | ||
for (unsigned I : seq<unsigned>(Mask.size())) | ||
for (auto [J, MaskV] : enumerate(MutableArrayRef(NewMask).slice( | ||
I * VecTyNumElements, VecTyNumElements))) | ||
MaskV = Mask[I] == PoisonMaskElem ? PoisonMaskElem | ||
: Mask[I] * VecTyNumElements + J; | ||
Mask.swap(NewMask); | ||
} | ||
|
||
/// \returns True if the value is a constant (but not globals/constant | ||
/// expressions). | ||
static bool isConstant(Value *V) { | ||
|
@@ -7762,6 +7777,31 @@ namespace { | |
/// The base class for shuffle instruction emission and shuffle cost estimation. | ||
class BaseShuffleAnalysis { | ||
protected: | ||
Type *ScalarTy = nullptr; | ||
|
||
BaseShuffleAnalysis(Type *ScalarTy) : ScalarTy(ScalarTy) {} | ||
|
||
/// V is expected to be a vectorized value. | ||
/// When REVEC is disabled, there is no difference between VF and | ||
/// VNumElements. | ||
/// When REVEC is enabled, VF is VNumElements / ScalarTyNumElements. | ||
/// e.g., if ScalarTy is <4 x Ty> and V1 is <8 x Ty>, 2 is returned instead | ||
/// of 8. | ||
unsigned getVF(Value *V) const { | ||
assert(V && "V cannot be nullptr"); | ||
assert(isa<FixedVectorType>(V->getType()) && | ||
"V does not have FixedVectorType"); | ||
assert(ScalarTy && "ScalarTy cannot be nullptr"); | ||
unsigned ScalarTyNumElements = getNumElements(ScalarTy); | ||
unsigned VNumElements = | ||
cast<FixedVectorType>(V->getType())->getNumElements(); | ||
assert(VNumElements > ScalarTyNumElements && | ||
"the number of elements of V is not large enough"); | ||
assert(VNumElements % ScalarTyNumElements == 0 && | ||
"the number of elements of V is not a vectorized value"); | ||
return VNumElements / ScalarTyNumElements; | ||
} | ||
|
||
/// Checks if the mask is an identity mask. | ||
/// \param IsStrict if is true the function returns false if mask size does | ||
/// not match vector size. | ||
|
@@ -8258,7 +8298,6 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis { | |
bool IsFinalized = false; | ||
SmallVector<int> CommonMask; | ||
SmallVector<PointerUnion<Value *, const TreeEntry *>, 2> InVectors; | ||
Type *ScalarTy = nullptr; | ||
const TargetTransformInfo &TTI; | ||
InstructionCost Cost = 0; | ||
SmallDenseSet<Value *> VectorizedVals; | ||
|
@@ -8840,14 +8879,14 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis { | |
} else if (V1 && P2.isNull()) { | ||
// Shuffle single vector. | ||
ExtraCost += GetValueMinBWAffectedCost(V1); | ||
CommonVF = cast<FixedVectorType>(V1->getType())->getNumElements(); | ||
CommonVF = getVF(V1); | ||
assert( | ||
all_of(Mask, | ||
[=](int Idx) { return Idx < static_cast<int>(CommonVF); }) && | ||
"All elements in mask must be less than CommonVF."); | ||
} else if (V1 && !V2) { | ||
// Shuffle vector and tree node. | ||
unsigned VF = cast<FixedVectorType>(V1->getType())->getNumElements(); | ||
unsigned VF = getVF(V1); | ||
const TreeEntry *E2 = P2.get<const TreeEntry *>(); | ||
CommonVF = std::max(VF, E2->getVectorFactor()); | ||
assert(all_of(Mask, | ||
|
@@ -8873,7 +8912,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis { | |
V2 = getAllOnesValue(*R.DL, getWidenedType(ScalarTy, CommonVF)); | ||
} else if (!V1 && V2) { | ||
// Shuffle vector and tree node. | ||
unsigned VF = cast<FixedVectorType>(V2->getType())->getNumElements(); | ||
unsigned VF = getVF(V2); | ||
const TreeEntry *E1 = P1.get<const TreeEntry *>(); | ||
CommonVF = std::max(VF, E1->getVectorFactor()); | ||
assert(all_of(Mask, | ||
|
@@ -8901,9 +8940,8 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis { | |
V2 = getAllOnesValue(*R.DL, getWidenedType(ScalarTy, CommonVF)); | ||
} else { | ||
assert(V1 && V2 && "Expected both vectors."); | ||
unsigned VF = cast<FixedVectorType>(V1->getType())->getNumElements(); | ||
CommonVF = | ||
std::max(VF, cast<FixedVectorType>(V2->getType())->getNumElements()); | ||
unsigned VF = getVF(V1); | ||
CommonVF = std::max(VF, getVF(V2)); | ||
assert(all_of(Mask, | ||
[=](int Idx) { | ||
return Idx < 2 * static_cast<int>(CommonVF); | ||
|
@@ -8921,6 +8959,11 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis { | |
V2 = getAllOnesValue(*R.DL, getWidenedType(ScalarTy, CommonVF)); | ||
} | ||
} | ||
if (auto *VecTy = dyn_cast<FixedVectorType>(ScalarTy)) { | ||
assert(SLPReVec && "FixedVectorType is not expected."); | ||
transformScalarShuffleIndiciesToVector(VecTy->getNumElements(), | ||
CommonMask); | ||
} | ||
InVectors.front() = | ||
Constant::getNullValue(getWidenedType(ScalarTy, CommonMask.size())); | ||
if (InVectors.size() == 2) | ||
|
@@ -8933,7 +8976,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis { | |
ShuffleCostEstimator(Type *ScalarTy, TargetTransformInfo &TTI, | ||
ArrayRef<Value *> VectorizedVals, BoUpSLP &R, | ||
SmallPtrSetImpl<Value *> &CheckedExtracts) | ||
: ScalarTy(ScalarTy), TTI(TTI), | ||
: BaseShuffleAnalysis(ScalarTy), TTI(TTI), | ||
VectorizedVals(VectorizedVals.begin(), VectorizedVals.end()), R(R), | ||
CheckedExtracts(CheckedExtracts) {} | ||
Value *adjustExtracts(const TreeEntry *E, MutableArrayRef<int> Mask, | ||
|
@@ -9138,7 +9181,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis { | |
} | ||
assert(!InVectors.empty() && !CommonMask.empty() && | ||
"Expected only tree entries from extracts/reused buildvectors."); | ||
unsigned VF = cast<FixedVectorType>(V1->getType())->getNumElements(); | ||
unsigned VF = getVF(V1); | ||
if (InVectors.size() == 2) { | ||
Cost += createShuffle(InVectors.front(), InVectors.back(), CommonMask); | ||
transformMaskAfterShuffle(CommonMask, CommonMask); | ||
|
@@ -9172,12 +9215,32 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis { | |
} | ||
Vals.push_back(Constant::getNullValue(V->getType())); | ||
} | ||
if (auto *VecTy = dyn_cast<FixedVectorType>(Vals.front()->getType())) { | ||
assert(SLPReVec && "FixedVectorType is not expected."); | ||
// When REVEC is enabled, we need to expand vector types into scalar | ||
// types. | ||
unsigned VecTyNumElements = VecTy->getNumElements(); | ||
SmallVector<Constant *> NewVals(VF * VecTyNumElements, nullptr); | ||
for (auto [I, V] : enumerate(Vals)) { | ||
Type *ScalarTy = V->getType()->getScalarType(); | ||
Constant *NewVal; | ||
if (isa<PoisonValue>(V)) | ||
NewVal = PoisonValue::get(ScalarTy); | ||
else if (isa<UndefValue>(V)) | ||
NewVal = UndefValue::get(ScalarTy); | ||
else | ||
NewVal = Constant::getNullValue(ScalarTy); | ||
std::fill_n(NewVals.begin() + I * VecTyNumElements, VecTyNumElements, | ||
NewVal); | ||
} | ||
Vals.swap(NewVals); | ||
} | ||
return ConstantVector::get(Vals); | ||
} | ||
return ConstantVector::getSplat( | ||
ElementCount::getFixed( | ||
cast<FixedVectorType>(Root->getType())->getNumElements()), | ||
getAllOnesValue(*R.DL, ScalarTy)); | ||
getAllOnesValue(*R.DL, ScalarTy->getScalarType())); | ||
} | ||
InstructionCost createFreeze(InstructionCost Cost) { return Cost; } | ||
/// Finalize emission of the shuffles. | ||
|
@@ -11677,8 +11740,8 @@ Value *BoUpSLP::gather(ArrayRef<Value *> VL, Value *Root, Type *ScalarTy) { | |
Type *Ty) { | ||
Value *Scalar = V; | ||
if (Scalar->getType() != Ty) { | ||
assert(Scalar->getType()->isIntegerTy() && Ty->isIntegerTy() && | ||
"Expected integer types only."); | ||
assert(Scalar->getType()->isIntOrIntVectorTy() && | ||
Ty->isIntOrIntVectorTy() && "Expected integer types only."); | ||
Value *V = Scalar; | ||
if (auto *CI = dyn_cast<CastInst>(Scalar); | ||
isa_and_nonnull<SExtInst, ZExtInst>(CI)) { | ||
|
@@ -11691,10 +11754,21 @@ Value *BoUpSLP::gather(ArrayRef<Value *> VL, Value *Root, Type *ScalarTy) { | |
V, Ty, !isKnownNonNegative(Scalar, SimplifyQuery(*DL))); | ||
} | ||
|
||
Vec = Builder.CreateInsertElement(Vec, Scalar, Builder.getInt32(Pos)); | ||
auto *InsElt = dyn_cast<InsertElementInst>(Vec); | ||
if (!InsElt) | ||
return Vec; | ||
Instruction *InsElt; | ||
if (auto *VecTy = dyn_cast<FixedVectorType>(Scalar->getType())) { | ||
assert(SLPReVec && "FixedVectorType is not expected."); | ||
Vec = InsElt = Builder.CreateInsertVector( | ||
Vec->getType(), Vec, V, | ||
Builder.getInt64(Pos * VecTy->getNumElements())); | ||
auto *II = dyn_cast<IntrinsicInst>(InsElt); | ||
if (!II || II->getIntrinsicID() != Intrinsic::vector_insert) | ||
return Vec; | ||
} else { | ||
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. No need for else after return 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. The else is for |
||
Vec = Builder.CreateInsertElement(Vec, Scalar, Builder.getInt32(Pos)); | ||
InsElt = dyn_cast<InsertElementInst>(Vec); | ||
if (!InsElt) | ||
return Vec; | ||
} | ||
GatherShuffleExtractSeq.insert(InsElt); | ||
CSEBlocks.insert(InsElt->getParent()); | ||
// Add to our 'need-to-extract' list. | ||
|
@@ -11795,7 +11869,6 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis { | |
/// resulting shuffle and the second operand sets to be the newly added | ||
/// operand. The \p CommonMask is transformed in the proper way after that. | ||
SmallVector<Value *, 2> InVectors; | ||
Type *ScalarTy = nullptr; | ||
IRBuilderBase &Builder; | ||
BoUpSLP &R; | ||
|
||
|
@@ -11921,7 +11994,7 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis { | |
|
||
public: | ||
ShuffleInstructionBuilder(Type *ScalarTy, IRBuilderBase &Builder, BoUpSLP &R) | ||
: ScalarTy(ScalarTy), Builder(Builder), R(R) {} | ||
: BaseShuffleAnalysis(ScalarTy), Builder(Builder), R(R) {} | ||
|
||
/// Adjusts extractelements after reusing them. | ||
Value *adjustExtracts(const TreeEntry *E, MutableArrayRef<int> Mask, | ||
|
@@ -12178,7 +12251,7 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis { | |
break; | ||
} | ||
} | ||
int VF = cast<FixedVectorType>(V1->getType())->getNumElements(); | ||
int VF = getVF(V1); | ||
for (unsigned Idx = 0, Sz = CommonMask.size(); Idx < Sz; ++Idx) | ||
if (Mask[Idx] != PoisonMaskElem && CommonMask[Idx] == PoisonMaskElem) | ||
CommonMask[Idx] = Mask[Idx] + (It == InVectors.begin() ? 0 : VF); | ||
|
@@ -12201,6 +12274,15 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis { | |
finalize(ArrayRef<int> ExtMask, unsigned VF = 0, | ||
function_ref<void(Value *&, SmallVectorImpl<int> &)> Action = {}) { | ||
IsFinalized = true; | ||
SmallVector<int> NewExtMask(ExtMask); | ||
if (auto *VecTy = dyn_cast<FixedVectorType>(ScalarTy)) { | ||
assert(SLPReVec && "FixedVectorType is not expected."); | ||
transformScalarShuffleIndiciesToVector(VecTy->getNumElements(), | ||
CommonMask); | ||
transformScalarShuffleIndiciesToVector(VecTy->getNumElements(), | ||
NewExtMask); | ||
ExtMask = NewExtMask; | ||
} | ||
if (Action) { | ||
Value *Vec = InVectors.front(); | ||
if (InVectors.size() == 2) { | ||
|
@@ -13984,6 +14066,17 @@ Value *BoUpSLP::vectorizeTree( | |
if (GEP->hasName()) | ||
CloneGEP->takeName(GEP); | ||
Ex = CloneGEP; | ||
} else if (auto *VecTy = | ||
dyn_cast<FixedVectorType>(Scalar->getType())) { | ||
assert(SLPReVec && "FixedVectorType is not expected."); | ||
unsigned VecTyNumElements = VecTy->getNumElements(); | ||
HanKuanChen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// When REVEC is enabled, we need to extract a vector. | ||
// Note: The element size of Scalar may be different from the | ||
// element size of Vec. | ||
Ex = Builder.CreateExtractVector( | ||
FixedVectorType::get(Vec->getType()->getScalarType(), | ||
VecTyNumElements), | ||
Vec, Builder.getInt64(ExternalUse.Lane * VecTyNumElements)); | ||
} else { | ||
Ex = Builder.CreateExtractElement(Vec, Lane); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.