Skip to content

Commit 69ade7c

Browse files
authored
[LV] Check if the VF is scalar by VFRange in handleUncountableEarlyExit. (#135294)
This patch check if the plan contains scalar VF by VFRange instead of Plan. This patch also clamp the range to contains either only scalar or only vector VFs to prevent mis-compile. Split from #113903.
1 parent f39242c commit 69ade7c

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9779,7 +9779,7 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
97799779
Legal->getUncountableEarlyExitingBlock()) {
97809780
VPlanTransforms::runPass(VPlanTransforms::handleUncountableEarlyExit, *Plan,
97819781
*PSE.getSE(), OrigLoop, UncountableExitingBlock,
9782-
RecipeBuilder);
9782+
RecipeBuilder, Range);
97839783
}
97849784
DenseMap<VPValue *, VPValue *> IVEndValues;
97859785
addScalarResumePhis(RecipeBuilder, *Plan, IVEndValues);

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "VPlanAnalysis.h"
1818
#include "VPlanCFG.h"
1919
#include "VPlanDominatorTree.h"
20+
#include "VPlanHelpers.h"
2021
#include "VPlanPatternMatch.h"
2122
#include "VPlanUtils.h"
2223
#include "VPlanVerifier.h"
@@ -2457,7 +2458,8 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan,
24572458

24582459
void VPlanTransforms::handleUncountableEarlyExit(
24592460
VPlan &Plan, ScalarEvolution &SE, Loop *OrigLoop,
2460-
BasicBlock *UncountableExitingBlock, VPRecipeBuilder &RecipeBuilder) {
2461+
BasicBlock *UncountableExitingBlock, VPRecipeBuilder &RecipeBuilder,
2462+
VFRange &Range) {
24612463
VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
24622464
auto *LatchVPBB = cast<VPBasicBlock>(LoopRegion->getExiting());
24632465
VPBuilder Builder(LatchVPBB->getTerminator());
@@ -2512,8 +2514,14 @@ void VPlanTransforms::handleUncountableEarlyExit(
25122514
ExitIRI->addOperand(IncomingFromLatch);
25132515
ExitIRI->extractLastLaneOfOperand(MiddleBuilder);
25142516
}
2515-
// Add the incoming value from the early exit.
2516-
if (!IncomingFromEarlyExit->isLiveIn() && !Plan.hasScalarVFOnly()) {
2517+
2518+
auto IsVector = [](ElementCount VF) { return VF.isVector(); };
2519+
// When the VFs are vectors, need to add `extract` to get the incoming value
2520+
// from early exit. When the range contains scalar VF, limit the range to
2521+
// scalar VF to prevent mis-compilation for the range containing both scalar
2522+
// and vector VFs.
2523+
if (!IncomingFromEarlyExit->isLiveIn() &&
2524+
LoopVectorizationPlanner::getDecisionAndClampRange(IsVector, Range)) {
25172525
VPValue *FirstActiveLane = EarlyExitB.createNaryOp(
25182526
VPInstruction::FirstActiveLane, {EarlyExitTakenCond}, nullptr,
25192527
"first.active.lane");

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class PredicatedScalarEvolution;
2828
class TargetLibraryInfo;
2929
class VPBuilder;
3030
class VPRecipeBuilder;
31+
class VFRange;
3132

3233
extern cl::opt<bool> VerifyEachVPlan;
3334

@@ -173,7 +174,8 @@ struct VPlanTransforms {
173174
static void handleUncountableEarlyExit(VPlan &Plan, ScalarEvolution &SE,
174175
Loop *OrigLoop,
175176
BasicBlock *UncountableExitingBlock,
176-
VPRecipeBuilder &RecipeBuilder);
177+
VPRecipeBuilder &RecipeBuilder,
178+
VFRange &Range);
177179

178180
/// Lower abstract recipes to concrete ones, that can be codegen'd. Use \p
179181
/// CanonicalIVTy as type for all un-typed live-ins in VPTypeAnalysis.

0 commit comments

Comments
 (0)