Skip to content

Commit b807a2b

Browse files
committed
[VPlan] Move VPReplicateRecipe::execute to VPlanRecipes.cpp (NFC).
Consolidate ::execute implementation in VPlanRecipes.cpp, in line with other ::execute implementations.
1 parent a1803ea commit b807a2b

File tree

2 files changed

+102
-101
lines changed

2 files changed

+102
-101
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,64 +2312,6 @@ static bool useMaskedInterleavedAccesses(const TargetTransformInfo &TTI) {
23122312
return TTI.enableMaskedInterleavedAccessVectorization();
23132313
}
23142314

2315-
/// A helper function to scalarize a single Instruction in the innermost loop.
2316-
/// Generates a sequence of scalar instances for lane \p Lane. Uses the VPValue
2317-
/// operands from \p RepRecipe instead of \p Instr's operands.
2318-
static void scalarizeInstruction(const Instruction *Instr,
2319-
VPReplicateRecipe *RepRecipe,
2320-
const VPLane &Lane, VPTransformState &State) {
2321-
assert((!Instr->getType()->isAggregateType() ||
2322-
canVectorizeTy(Instr->getType())) &&
2323-
"Expected vectorizable or non-aggregate type.");
2324-
2325-
// Does this instruction return a value ?
2326-
bool IsVoidRetTy = Instr->getType()->isVoidTy();
2327-
2328-
Instruction *Cloned = Instr->clone();
2329-
if (!IsVoidRetTy) {
2330-
Cloned->setName(Instr->getName() + ".cloned");
2331-
#if !defined(NDEBUG)
2332-
// Verify that VPlan type inference results agree with the type of the
2333-
// generated values.
2334-
assert(State.TypeAnalysis.inferScalarType(RepRecipe) == Cloned->getType() &&
2335-
"inferred type and type from generated instructions do not match");
2336-
#endif
2337-
}
2338-
2339-
RepRecipe->applyFlags(*Cloned);
2340-
2341-
if (auto DL = RepRecipe->getDebugLoc())
2342-
State.setDebugLocFrom(DL);
2343-
2344-
// Replace the operands of the cloned instructions with their scalar
2345-
// equivalents in the new loop.
2346-
for (const auto &I : enumerate(RepRecipe->operands())) {
2347-
auto InputLane = Lane;
2348-
VPValue *Operand = I.value();
2349-
if (vputils::isUniformAfterVectorization(Operand))
2350-
InputLane = VPLane::getFirstLane();
2351-
Cloned->setOperand(I.index(), State.get(Operand, InputLane));
2352-
}
2353-
State.addNewMetadata(Cloned, Instr);
2354-
2355-
// Place the cloned scalar in the new loop.
2356-
State.Builder.Insert(Cloned);
2357-
2358-
State.set(RepRecipe, Cloned, Lane);
2359-
2360-
// If we just cloned a new assumption, add it the assumption cache.
2361-
if (auto *II = dyn_cast<AssumeInst>(Cloned))
2362-
State.AC->registerAssumption(II);
2363-
2364-
assert(
2365-
(RepRecipe->getParent()->getParent() ||
2366-
!RepRecipe->getParent()->getPlan()->getVectorLoopRegion() ||
2367-
all_of(RepRecipe->operands(),
2368-
[](VPValue *Op) { return Op->isDefinedOutsideLoopRegions(); })) &&
2369-
"Expected a recipe is either within a region or all of its operands "
2370-
"are defined outside the vectorized region.");
2371-
}
2372-
23732315
Value *
23742316
InnerLoopVectorizer::getOrCreateVectorTripCount(BasicBlock *InsertBlock) {
23752317
if (VectorTripCount)
@@ -10081,49 +10023,6 @@ void VPDerivedIVRecipe::execute(VPTransformState &State) {
1008110023
State.set(this, DerivedIV, VPLane(0));
1008210024
}
1008310025

10084-
void VPReplicateRecipe::execute(VPTransformState &State) {
10085-
Instruction *UI = getUnderlyingInstr();
10086-
if (State.Lane) { // Generate a single instance.
10087-
assert((State.VF.isScalar() || !isUniform()) &&
10088-
"uniform recipe shouldn't be predicated");
10089-
assert(!State.VF.isScalable() && "Can't scalarize a scalable vector");
10090-
scalarizeInstruction(UI, this, *State.Lane, State);
10091-
// Insert scalar instance packing it into a vector.
10092-
if (State.VF.isVector() && shouldPack()) {
10093-
// If we're constructing lane 0, initialize to start from poison.
10094-
if (State.Lane->isFirstLane()) {
10095-
assert(!State.VF.isScalable() && "VF is assumed to be non scalable.");
10096-
Value *Poison = PoisonValue::get(
10097-
VectorType::get(UI->getType(), State.VF));
10098-
State.set(this, Poison);
10099-
}
10100-
State.packScalarIntoVectorizedValue(this, *State.Lane);
10101-
}
10102-
return;
10103-
}
10104-
10105-
if (IsUniform) {
10106-
// Uniform within VL means we need to generate lane 0.
10107-
scalarizeInstruction(UI, this, VPLane(0), State);
10108-
return;
10109-
}
10110-
10111-
// A store of a loop varying value to a uniform address only needs the last
10112-
// copy of the store.
10113-
if (isa<StoreInst>(UI) &&
10114-
vputils::isUniformAfterVectorization(getOperand(1))) {
10115-
auto Lane = VPLane::getLastLaneForVF(State.VF);
10116-
scalarizeInstruction(UI, this, VPLane(Lane), State);
10117-
return;
10118-
}
10119-
10120-
// Generate scalar instances for all VF lanes.
10121-
assert(!State.VF.isScalable() && "Can't scalarize a scalable vector");
10122-
const unsigned EndLane = State.VF.getKnownMinValue();
10123-
for (unsigned Lane = 0; Lane < EndLane; ++Lane)
10124-
scalarizeInstruction(UI, this, VPLane(Lane), State);
10125-
}
10126-
1012710026
// Determine how to lower the scalar epilogue, which depends on 1) optimising
1012810027
// for minimum code-size, 2) predicate compiler options, 3) loop hints forcing
1012910028
// predication, and 4) a TTI hook that analyses whether the loop is suitable

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/STLExtras.h"
2121
#include "llvm/ADT/SmallVector.h"
2222
#include "llvm/ADT/Twine.h"
23+
#include "llvm/Analysis/AssumptionCache.h"
2324
#include "llvm/Analysis/IVDescriptors.h"
2425
#include "llvm/Analysis/LoopInfo.h"
2526
#include "llvm/IR/BasicBlock.h"
@@ -2547,6 +2548,107 @@ void VPReductionEVLRecipe::print(raw_ostream &O, const Twine &Indent,
25472548
}
25482549
#endif
25492550

2551+
/// A helper function to scalarize a single Instruction in the innermost loop.
2552+
/// Generates a sequence of scalar instances for lane \p Lane. Uses the VPValue
2553+
/// operands from \p RepRecipe instead of \p Instr's operands.
2554+
static void scalarizeInstruction(const Instruction *Instr,
2555+
VPReplicateRecipe *RepRecipe,
2556+
const VPLane &Lane, VPTransformState &State) {
2557+
assert((!Instr->getType()->isAggregateType() ||
2558+
canVectorizeTy(Instr->getType())) &&
2559+
"Expected vectorizable or non-aggregate type.");
2560+
2561+
// Does this instruction return a value ?
2562+
bool IsVoidRetTy = Instr->getType()->isVoidTy();
2563+
2564+
Instruction *Cloned = Instr->clone();
2565+
if (!IsVoidRetTy) {
2566+
Cloned->setName(Instr->getName() + ".cloned");
2567+
#if !defined(NDEBUG)
2568+
// Verify that VPlan type inference results agree with the type of the
2569+
// generated values.
2570+
assert(State.TypeAnalysis.inferScalarType(RepRecipe) == Cloned->getType() &&
2571+
"inferred type and type from generated instructions do not match");
2572+
#endif
2573+
}
2574+
2575+
RepRecipe->applyFlags(*Cloned);
2576+
2577+
if (auto DL = RepRecipe->getDebugLoc())
2578+
State.setDebugLocFrom(DL);
2579+
2580+
// Replace the operands of the cloned instructions with their scalar
2581+
// equivalents in the new loop.
2582+
for (const auto &I : enumerate(RepRecipe->operands())) {
2583+
auto InputLane = Lane;
2584+
VPValue *Operand = I.value();
2585+
if (vputils::isUniformAfterVectorization(Operand))
2586+
InputLane = VPLane::getFirstLane();
2587+
Cloned->setOperand(I.index(), State.get(Operand, InputLane));
2588+
}
2589+
State.addNewMetadata(Cloned, Instr);
2590+
2591+
// Place the cloned scalar in the new loop.
2592+
State.Builder.Insert(Cloned);
2593+
2594+
State.set(RepRecipe, Cloned, Lane);
2595+
2596+
// If we just cloned a new assumption, add it the assumption cache.
2597+
if (auto *II = dyn_cast<AssumeInst>(Cloned))
2598+
State.AC->registerAssumption(II);
2599+
2600+
assert(
2601+
(RepRecipe->getParent()->getParent() ||
2602+
!RepRecipe->getParent()->getPlan()->getVectorLoopRegion() ||
2603+
all_of(RepRecipe->operands(),
2604+
[](VPValue *Op) { return Op->isDefinedOutsideLoopRegions(); })) &&
2605+
"Expected a recipe is either within a region or all of its operands "
2606+
"are defined outside the vectorized region.");
2607+
}
2608+
2609+
void VPReplicateRecipe::execute(VPTransformState &State) {
2610+
Instruction *UI = getUnderlyingInstr();
2611+
if (State.Lane) { // Generate a single instance.
2612+
assert((State.VF.isScalar() || !isUniform()) &&
2613+
"uniform recipe shouldn't be predicated");
2614+
assert(!State.VF.isScalable() && "Can't scalarize a scalable vector");
2615+
scalarizeInstruction(UI, this, *State.Lane, State);
2616+
// Insert scalar instance packing it into a vector.
2617+
if (State.VF.isVector() && shouldPack()) {
2618+
// If we're constructing lane 0, initialize to start from poison.
2619+
if (State.Lane->isFirstLane()) {
2620+
assert(!State.VF.isScalable() && "VF is assumed to be non scalable.");
2621+
Value *Poison =
2622+
PoisonValue::get(VectorType::get(UI->getType(), State.VF));
2623+
State.set(this, Poison);
2624+
}
2625+
State.packScalarIntoVectorizedValue(this, *State.Lane);
2626+
}
2627+
return;
2628+
}
2629+
2630+
if (IsUniform) {
2631+
// Uniform within VL means we need to generate lane 0.
2632+
scalarizeInstruction(UI, this, VPLane(0), State);
2633+
return;
2634+
}
2635+
2636+
// A store of a loop varying value to a uniform address only needs the last
2637+
// copy of the store.
2638+
if (isa<StoreInst>(UI) &&
2639+
vputils::isUniformAfterVectorization(getOperand(1))) {
2640+
auto Lane = VPLane::getLastLaneForVF(State.VF);
2641+
scalarizeInstruction(UI, this, VPLane(Lane), State);
2642+
return;
2643+
}
2644+
2645+
// Generate scalar instances for all VF lanes.
2646+
assert(!State.VF.isScalable() && "Can't scalarize a scalable vector");
2647+
const unsigned EndLane = State.VF.getKnownMinValue();
2648+
for (unsigned Lane = 0; Lane < EndLane; ++Lane)
2649+
scalarizeInstruction(UI, this, VPLane(Lane), State);
2650+
}
2651+
25502652
bool VPReplicateRecipe::shouldPack() const {
25512653
// Find if the recipe is used by a widened recipe via an intervening
25522654
// VPPredInstPHIRecipe. In this case, also pack the scalar values in a vector.

0 commit comments

Comments
 (0)