Skip to content

[LoopVectorize][NFC] Simplify ScaledReductionExitInstrs map #123368

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
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8711,7 +8711,7 @@ void VPRecipeBuilder::collectScaledReductions(VFRange &Range) {
PartialReductionChain Chain = Pair.first;
if (ExtendIsOnlyUsedByPartialReductions(Chain.ExtendA) &&
ExtendIsOnlyUsedByPartialReductions(Chain.ExtendB))
ScaledReductionExitInstrs.insert(std::make_pair(Chain.Reduction, Pair));
ScaledReductionMap.insert(std::make_pair(Chain.Reduction, Pair.second));
}
}

Expand Down Expand Up @@ -8803,9 +8803,8 @@ VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
Phi->getIncomingValueForBlock(OrigLoop->getLoopPreheader()));

// If the PHI is used by a partial reduction, set the scale factor.
std::optional<std::pair<PartialReductionChain, unsigned>> Pair =
getScaledReductionForInstr(RdxDesc.getLoopExitInstr());
unsigned ScaleFactor = Pair ? Pair->second : 1;
unsigned ScaleFactor =
getScalingForReduction(RdxDesc.getLoopExitInstr()).value_or(1);
PhiRecipe = new VPReductionPHIRecipe(
Phi, RdxDesc, *StartV, CM.isInLoopReduction(Phi),
CM.useOrderedReductions(RdxDesc), ScaleFactor);
Expand Down Expand Up @@ -8840,7 +8839,7 @@ VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
if (isa<LoadInst>(Instr) || isa<StoreInst>(Instr))
return tryToWidenMemory(Instr, Operands, Range);

if (getScaledReductionForInstr(Instr))
if (getScalingForReduction(Instr))
return tryToCreatePartialReduction(Instr, Operands);

if (!shouldWiden(Instr, Range))
Expand Down
16 changes: 6 additions & 10 deletions llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ class VPRecipeBuilder {
/// created.
SmallVector<VPHeaderPHIRecipe *, 4> PhisToFix;

/// The set of reduction exit instructions that will be scaled to
/// a smaller VF via partial reductions, paired with the scaling factor.
DenseMap<const Instruction *, std::pair<PartialReductionChain, unsigned>>
ScaledReductionExitInstrs;
/// A mapping of partial reduction exit instructions to their scaling factor.
DenseMap<const Instruction *, unsigned> ScaledReductionMap;

/// Check if \p I can be widened at the start of \p Range and possibly
/// decrease the range such that the returned value holds for the entire \p
Expand Down Expand Up @@ -157,12 +155,10 @@ class VPRecipeBuilder {
: Plan(Plan), OrigLoop(OrigLoop), TLI(TLI), TTI(TTI), Legal(Legal),
CM(CM), PSE(PSE), Builder(Builder) {}

std::optional<std::pair<PartialReductionChain, unsigned>>
getScaledReductionForInstr(const Instruction *ExitInst) {
auto It = ScaledReductionExitInstrs.find(ExitInst);
return It == ScaledReductionExitInstrs.end()
? std::nullopt
: std::make_optional(It->second);
std::optional<unsigned> getScalingForReduction(const Instruction *ExitInst) {
auto It = ScaledReductionMap.find(ExitInst);
return It == ScaledReductionMap.end() ? std::nullopt
: std::make_optional(It->second);
}

/// Find all possible partial reductions in the loop and track all of those
Expand Down
Loading