@@ -290,7 +290,7 @@ static cl::opt<unsigned> ForceTargetMaxVectorInterleaveFactor(
290
290
cl::desc(" A flag that overrides the target's max interleave factor for "
291
291
" vectorized loops." ));
292
292
293
- cl::opt<unsigned > ForceTargetInstructionCost (
293
+ static cl::opt<unsigned > ForceTargetInstructionCost (
294
294
" force-target-instruction-cost" , cl::init(0 ), cl::Hidden,
295
295
cl::desc(" A flag that overrides the target's expected cost for "
296
296
" an instruction to a single constant value. Mostly "
@@ -412,6 +412,14 @@ static bool hasIrregularType(Type *Ty, const DataLayout &DL) {
412
412
return DL.getTypeAllocSizeInBits (Ty) != DL.getTypeSizeInBits (Ty);
413
413
}
414
414
415
+ // / A helper function that returns the reciprocal of the block probability of
416
+ // / predicated blocks. If we return X, we are assuming the predicated block
417
+ // / will execute once for every X iterations of the loop header.
418
+ // /
419
+ // / TODO: We should use actual block probability here, if available. Currently,
420
+ // / we always assume predicated blocks have a 50% chance of executing.
421
+ static unsigned getReciprocalPredBlockProb () { return 2 ; }
422
+
415
423
// / Returns "best known" trip count for the specified loop \p L as defined by
416
424
// / the following procedure:
417
425
// / 1) Returns exact trip count if it is known.
@@ -1613,16 +1621,6 @@ class LoopVectorizationCostModel {
1613
1621
// / \p VF is the vectorization factor chosen for the original loop.
1614
1622
bool isEpilogueVectorizationProfitable (const ElementCount VF) const ;
1615
1623
1616
- // / Return the cost of instructions in an inloop reduction pattern, if I is
1617
- // / part of that pattern.
1618
- std::optional<InstructionCost>
1619
- getReductionPatternCost (Instruction *I, ElementCount VF, Type *VectorTy,
1620
- TTI::TargetCostKind CostKind) const ;
1621
-
1622
- // / Returns the execution time cost of an instruction for a given vector
1623
- // / width. Vector width of one means scalar.
1624
- VectorizationCostTy getInstructionCost (Instruction *I, ElementCount VF);
1625
-
1626
1624
private:
1627
1625
unsigned NumPredStores = 0 ;
1628
1626
@@ -1648,11 +1646,21 @@ class LoopVectorizationCostModel {
1648
1646
// / of elements.
1649
1647
ElementCount getMaxLegalScalableVF (unsigned MaxSafeElements);
1650
1648
1649
+ // / Returns the execution time cost of an instruction for a given vector
1650
+ // / width. Vector width of one means scalar.
1651
+ VectorizationCostTy getInstructionCost (Instruction *I, ElementCount VF);
1652
+
1651
1653
// / The cost-computation logic from getInstructionCost which provides
1652
1654
// / the vector type as an output parameter.
1653
1655
InstructionCost getInstructionCost (Instruction *I, ElementCount VF,
1654
1656
Type *&VectorTy);
1655
1657
1658
+ // / Return the cost of instructions in an inloop reduction pattern, if I is
1659
+ // / part of that pattern.
1660
+ std::optional<InstructionCost>
1661
+ getReductionPatternCost (Instruction *I, ElementCount VF, Type *VectorTy,
1662
+ TTI::TargetCostKind CostKind) const ;
1663
+
1656
1664
// / Calculate vectorization cost of memory instruction \p I.
1657
1665
InstructionCost getMemoryInstructionCost (Instruction *I, ElementCount VF);
1658
1666
@@ -7280,10 +7288,7 @@ LoopVectorizationPlanner::plan(ElementCount UserVF, unsigned UserIC) {
7280
7288
if (!MaxFactors.hasVector ())
7281
7289
return VectorizationFactor::Disabled ();
7282
7290
7283
- // Select the optimal vectorization factor according to the legacy cost-model.
7284
- // This is now only used to verify the decisions by the new VPlan-based
7285
- // cost-model and will be retired once the VPlan-based cost-model is
7286
- // stabilized.
7291
+ // Select the optimal vectorization factor.
7287
7292
VectorizationFactor VF = selectVectorizationFactor (VFCandidates);
7288
7293
assert ((VF.Width .isScalar () || VF.ScalarCost > 0 ) && " when vectorizing, the scalar cost must be non-zero." );
7289
7294
if (!hasPlanWithVF (VF.Width )) {
@@ -7294,166 +7299,6 @@ LoopVectorizationPlanner::plan(ElementCount UserVF, unsigned UserIC) {
7294
7299
return VF;
7295
7300
}
7296
7301
7297
- InstructionCost VPCostContext::getLegacyCost (Instruction *UI,
7298
- ElementCount VF) const {
7299
- return CM.getInstructionCost (UI, VF).first ;
7300
- }
7301
-
7302
- bool VPCostContext::skipCostComputation (Instruction *UI, bool IsVector) const {
7303
- return (IsVector && CM.VecValuesToIgnore .contains (UI)) ||
7304
- SkipCostComputation.contains (UI);
7305
- }
7306
-
7307
- InstructionCost LoopVectorizationPlanner::cost (VPlan &Plan,
7308
- ElementCount VF) const {
7309
- InstructionCost Cost = 0 ;
7310
- LLVMContext &LLVMCtx = OrigLoop->getHeader ()->getContext ();
7311
- VPCostContext CostCtx (CM.TTI , Legal->getWidestInductionType (), LLVMCtx, CM);
7312
-
7313
- // Cost modeling for inductions is inaccurate in the legacy cost model
7314
- // compared to the recipes that are generated. To match here initially during
7315
- // VPlan cost model bring up directly use the induction costs from the legacy
7316
- // cost model. Note that we do this as pre-processing; the VPlan may not have
7317
- // any recipes associated with the original induction increment instruction.
7318
- // We precompute the cost of both induction increment instructions that are
7319
- // represented by recipes and those that are not, to avoid distinguishing
7320
- // between them here, and skip all recipes that represent induction increments
7321
- // (the former case) later on, if they exist, to avoid counting them twice.
7322
- // TODO: Switch to more accurate costing based on VPlan.
7323
- for (const auto &[IV, _] : Legal->getInductionVars ()) {
7324
- Instruction *IVInc = cast<Instruction>(
7325
- IV->getIncomingValueForBlock (OrigLoop->getLoopLatch ()));
7326
- assert (!CostCtx.SkipCostComputation .contains (IVInc) &&
7327
- " Same IV increment for multiple inductions?" );
7328
- CostCtx.SkipCostComputation .insert (IVInc);
7329
- InstructionCost InductionCost = CostCtx.getLegacyCost (IVInc, VF);
7330
- LLVM_DEBUG ({
7331
- dbgs () << " Cost of " << InductionCost << " for VF " << VF
7332
- << " :\n induction increment " << *IVInc << " \n " ;
7333
- IVInc->dump ();
7334
- });
7335
- Cost += InductionCost;
7336
- }
7337
-
7338
- // / Compute the cost of all exiting conditions of the loop using the legacy
7339
- // / cost model. This is to match the legacy behavior, which adds the cost of
7340
- // / all exit conditions. Note that this over-estimates the cost, as there will
7341
- // / be a single condition to control the vector loop.
7342
- SmallVector<BasicBlock *> Exiting;
7343
- CM.TheLoop ->getExitingBlocks (Exiting);
7344
- // Add the cost of all exit conditions.
7345
- for (BasicBlock *EB : Exiting) {
7346
- auto *Term = dyn_cast<BranchInst>(EB->getTerminator ());
7347
- if (!Term)
7348
- continue ;
7349
- if (auto *CondI = dyn_cast<Instruction>(Term->getOperand (0 ))) {
7350
- assert (!CostCtx.SkipCostComputation .contains (CondI) &&
7351
- " Condition already skipped?" );
7352
- CostCtx.SkipCostComputation .insert (CondI);
7353
- Cost += CostCtx.getLegacyCost (CondI, VF);
7354
- }
7355
- }
7356
-
7357
- // The legacy cost model has special logic to compute the cost of in-loop
7358
- // reductions, which may be smaller than the sum of all instructions involved
7359
- // in the reduction. For AnyOf reductions, VPlan codegen may remove the select
7360
- // which the legacy cost model uses to assign cost. Pre-compute their costs
7361
- // for now.
7362
- // TODO: Switch to costing based on VPlan once the logic has been ported.
7363
- for (const auto &[RedPhi, RdxDesc] : Legal->getReductionVars ()) {
7364
- if (!CM.isInLoopReduction (RedPhi) &&
7365
- !RecurrenceDescriptor::isAnyOfRecurrenceKind (
7366
- RdxDesc.getRecurrenceKind ()))
7367
- continue ;
7368
-
7369
- // AnyOf reduction codegen may remove the select. To match the legacy cost
7370
- // model, pre-compute the cost for AnyOf reductions here.
7371
- if (RecurrenceDescriptor::isAnyOfRecurrenceKind (
7372
- RdxDesc.getRecurrenceKind ())) {
7373
- auto *Select = cast<SelectInst>(*find_if (
7374
- RedPhi->users (), [](User *U) { return isa<SelectInst>(U); }));
7375
- assert (!CostCtx.SkipCostComputation .contains (Select) &&
7376
- " reduction op visited multiple times" );
7377
- CostCtx.SkipCostComputation .insert (Select);
7378
- auto ReductionCost = CostCtx.getLegacyCost (Select, VF);
7379
- LLVM_DEBUG (dbgs () << " Cost of " << ReductionCost << " for VF " << VF
7380
- << " :\n any-of reduction " << *Select << " \n " );
7381
- Cost += ReductionCost;
7382
- continue ;
7383
- }
7384
-
7385
- const auto &ChainOps = RdxDesc.getReductionOpChain (RedPhi, OrigLoop);
7386
- SetVector<Instruction *> ChainOpsAndOperands (ChainOps.begin (),
7387
- ChainOps.end ());
7388
- // Also include the operands of instructions in the chain, as the cost-model
7389
- // may mark extends as free.
7390
- for (auto *ChainOp : ChainOps) {
7391
- for (Value *Op : ChainOp->operands ()) {
7392
- if (auto *I = dyn_cast<Instruction>(Op))
7393
- ChainOpsAndOperands.insert (I);
7394
- }
7395
- }
7396
-
7397
- // Pre-compute the cost for I, if it has a reduction pattern cost.
7398
- for (Instruction *I : ChainOpsAndOperands) {
7399
- auto ReductionCost = CM.getReductionPatternCost (
7400
- I, VF, ToVectorTy (I->getType (), VF), TTI::TCK_RecipThroughput);
7401
- if (!ReductionCost)
7402
- continue ;
7403
-
7404
- assert (!CostCtx.SkipCostComputation .contains (I) &&
7405
- " reduction op visited multiple times" );
7406
- CostCtx.SkipCostComputation .insert (I);
7407
- LLVM_DEBUG (dbgs () << " Cost of " << ReductionCost << " for VF " << VF
7408
- << " :\n in-loop reduction " << *I << " \n " );
7409
- Cost += *ReductionCost;
7410
- }
7411
- }
7412
-
7413
- // Now compute and add the VPlan-based cost.
7414
- Cost += Plan.cost (VF, CostCtx);
7415
- LLVM_DEBUG (dbgs () << " Cost for VF " << VF << " : " << Cost << " \n " );
7416
- return Cost;
7417
- }
7418
-
7419
- VPlan &LoopVectorizationPlanner::getBestPlan () const {
7420
- // If there is a single VPlan with a single VF, return it directly.
7421
- VPlan &FirstPlan = *VPlans[0 ];
7422
- if (VPlans.size () == 1 && size (FirstPlan.vectorFactors ()) == 1 )
7423
- return FirstPlan;
7424
-
7425
- VPlan *BestPlan = &FirstPlan;
7426
- ElementCount ScalarVF = ElementCount::getFixed (1 );
7427
- assert (hasPlanWithVF (ScalarVF) &&
7428
- " More than a single plan/VF w/o any plan having scalar VF" );
7429
-
7430
- InstructionCost ScalarCost = cost (getBestPlanFor (ScalarVF), ScalarVF);
7431
- VectorizationFactor BestFactor (ScalarVF, ScalarCost, ScalarCost);
7432
-
7433
- bool ForceVectorization = Hints.getForce () == LoopVectorizeHints::FK_Enabled;
7434
- if (ForceVectorization) {
7435
- // Ignore scalar width, because the user explicitly wants vectorization.
7436
- // Initialize cost to max so that VF = 2 is, at least, chosen during cost
7437
- // evaluation.
7438
- BestFactor.Cost = InstructionCost::getMax ();
7439
- }
7440
-
7441
- for (auto &P : VPlans) {
7442
- for (ElementCount VF : P->vectorFactors ()) {
7443
- if (VF.isScalar ())
7444
- continue ;
7445
- InstructionCost Cost = cost (*P, VF);
7446
- VectorizationFactor CurrentFactor (VF, Cost, ScalarCost);
7447
- if (isMoreProfitable (CurrentFactor, BestFactor)) {
7448
- BestFactor = CurrentFactor;
7449
- BestPlan = &*P;
7450
- }
7451
- }
7452
- }
7453
- BestPlan->setVF (BestFactor.Width );
7454
- return *BestPlan;
7455
- }
7456
-
7457
7302
VPlan &LoopVectorizationPlanner::getBestPlanFor (ElementCount VF) const {
7458
7303
assert (count_if (VPlans,
7459
7304
[VF](const VPlanPtr &Plan) { return Plan->hasVF (VF); }) ==
@@ -10312,15 +10157,8 @@ bool LoopVectorizePass::processLoop(Loop *L) {
10312
10157
VF.MinProfitableTripCount , IC, &LVL, &CM, BFI,
10313
10158
PSI, Checks);
10314
10159
10315
- VPlan &BestPlan = LVP.getBestPlan ();
10316
- assert (size (BestPlan.vectorFactors ()) == 1 &&
10317
- " Plan should have a single VF" );
10318
- ElementCount Width = *BestPlan.vectorFactors ().begin ();
10319
- LLVM_DEBUG (dbgs () << " VF picked by VPlan cost model: " << Width
10320
- << " \n " );
10321
- assert (VF.Width == Width &&
10322
- " VPlan cost model and legacy cost model disagreed" );
10323
- LVP.executePlan (Width, IC, BestPlan, LB, DT, false );
10160
+ VPlan &BestPlan = LVP.getBestPlanFor (VF.Width );
10161
+ LVP.executePlan (VF.Width , IC, BestPlan, LB, DT, false );
10324
10162
++LoopsVectorized;
10325
10163
10326
10164
// Add metadata to disable runtime unrolling a scalar loop when there
0 commit comments