Skip to content

Commit eea150c

Browse files
committed
[VPlan] Include IV phi and backedge cost in VPlan cost computation.
In WebAssembly, costs != 0 are assigned to be backedge and induction phis, so make sure we include those costs in the VPlan-based cost model. This fixes a downstream crash with WebAssembly after 242cc20 (#92555)
1 parent dfe55a1 commit eea150c

File tree

4 files changed

+99
-22
lines changed

4 files changed

+99
-22
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7325,38 +7325,31 @@ InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
73257325
// cost model. Note that we do this as pre-processing; the VPlan may not have
73267326
// any recipes associated with the original induction increment instruction
73277327
// and may replace truncates with VPWidenIntOrFpInductionRecipe. We precompute
7328-
// the cost of both induction increment instructions that are represented by
7329-
// recipes and those that are not, to avoid distinguishing between them here,
7330-
// and skip all recipes that represent induction increments (the former case)
7331-
// later on, if they exist, to avoid counting them twice. Similarly we
7332-
// pre-compute the cost of any optimized truncates.
7328+
// the cost of induction phis and increments (both that are represented by
7329+
// recipes and those that are not), to avoid distinguishing between them here,
7330+
// and skip all recipes that represent induction phis and increments (the
7331+
// former case) later on, if they exist, to avoid counting them twice.
7332+
// Similarly we pre-compute the cost of any optimized truncates.
73337333
// TODO: Switch to more accurate costing based on VPlan.
73347334
for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
73357335
Instruction *IVInc = cast<Instruction>(
73367336
IV->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
7337-
if (CostCtx.SkipCostComputation.insert(IVInc).second) {
7338-
InstructionCost InductionCost = CostCtx.getLegacyCost(IVInc, VF);
7339-
LLVM_DEBUG({
7340-
dbgs() << "Cost of " << InductionCost << " for VF " << VF
7341-
<< ":\n induction increment " << *IVInc << "\n";
7342-
IVInc->dump();
7343-
});
7344-
Cost += InductionCost;
7345-
}
7337+
SmallVector<Instruction *> IVInsts = {IV, IVInc};
73467338
for (User *U : IV->users()) {
73477339
auto *CI = cast<Instruction>(U);
73487340
if (!CostCtx.CM.isOptimizableIVTruncate(CI, VF))
73497341
continue;
7350-
assert(!CostCtx.SkipCostComputation.contains(CI) &&
7351-
"Same cast for multiple inductions?");
7352-
CostCtx.SkipCostComputation.insert(CI);
7353-
InstructionCost CastCost = CostCtx.getLegacyCost(CI, VF);
7342+
IVInsts.push_back(CI);
7343+
}
7344+
for (Instruction *IVInst : IVInsts) {
7345+
if (!CostCtx.SkipCostComputation.insert(IVInst).second)
7346+
continue;
7347+
InstructionCost InductionCost = CostCtx.getLegacyCost(IVInst, VF);
73547348
LLVM_DEBUG({
7355-
dbgs() << "Cost of " << CastCost << " for VF " << VF
7356-
<< ":\n induction cast " << *CI << "\n";
7357-
CI->dump();
7349+
dbgs() << "Cost of " << InductionCost << " for VF " << VF
7350+
<< ": induction instruction " << *IVInst << "\n";
73587351
});
7359-
Cost += CastCost;
7352+
Cost += InductionCost;
73607353
}
73617354
}
73627355

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,11 @@ InstructionCost VPRegionBlock::cost(ElementCount VF, VPCostContext &Ctx) {
764764
InstructionCost Cost = 0;
765765
for (VPBlockBase *Block : vp_depth_first_shallow(getEntry()))
766766
Cost += Block->cost(VF, Ctx);
767+
InstructionCost BackedgeCost =
768+
Ctx.TTI.getCFInstrCost(Instruction::Br, TTI::TCK_RecipThroughput);
769+
LLVM_DEBUG(dbgs() << "Cost of " << BackedgeCost << " for VF " << VF
770+
<< ": vector loop backedge\n");
771+
Cost += BackedgeCost;
767772
return Cost;
768773
}
769774

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -p loop-vectorize -S %s | FileCheck %s
3+
4+
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-f128:64-n32:64-S128-ni:1:10:20"
5+
target triple = "wasm32-unknown-emscripten"
6+
7+
define void @induction_phi_and_branch_cost(ptr %end, ptr %start.1, ptr %start.2) #0 {
8+
; CHECK-LABEL: define void @induction_phi_and_branch_cost(
9+
; CHECK-SAME: ptr [[END:%.*]], ptr [[START_1:%.*]], ptr [[START_2:%.*]]) #[[ATTR0:[0-9]+]] {
10+
; CHECK-NEXT: [[ENTRY:.*]]:
11+
; CHECK-NEXT: [[END2:%.*]] = ptrtoint ptr [[END]] to i32
12+
; CHECK-NEXT: [[START_11:%.*]] = ptrtoint ptr [[START_1]] to i32
13+
; CHECK-NEXT: [[TMP0:%.*]] = sub i32 [[START_11]], [[END2]]
14+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[TMP0]], 2
15+
; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i32 [[TMP1]], 1
16+
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[TMP2]], 4
17+
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
18+
; CHECK: [[VECTOR_PH]]:
19+
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[TMP2]], 4
20+
; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[TMP2]], [[N_MOD_VF]]
21+
; CHECK-NEXT: [[TMP3:%.*]] = mul i32 [[N_VEC]], -4
22+
; CHECK-NEXT: [[IND_END:%.*]] = getelementptr i8, ptr [[START_1]], i32 [[TMP3]]
23+
; CHECK-NEXT: [[TMP4:%.*]] = mul i32 [[N_VEC]], -4
24+
; CHECK-NEXT: [[IND_END3:%.*]] = getelementptr i8, ptr [[START_2]], i32 [[TMP4]]
25+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
26+
; CHECK: [[VECTOR_BODY]]:
27+
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
28+
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = mul i32 [[INDEX]], -4
29+
; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[OFFSET_IDX]], 0
30+
; CHECK-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr [[START_2]], i32 [[TMP5]]
31+
; CHECK-NEXT: [[TMP6:%.*]] = getelementptr i32, ptr [[NEXT_GEP]], i32 0
32+
; CHECK-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[TMP6]], i32 -3
33+
; CHECK-NEXT: store <4 x i32> zeroinitializer, ptr [[TMP7]], align 4
34+
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
35+
; CHECK-NEXT: [[TMP8:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
36+
; CHECK-NEXT: br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
37+
; CHECK: [[MIDDLE_BLOCK]]:
38+
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[TMP2]], [[N_VEC]]
39+
; CHECK-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
40+
; CHECK: [[SCALAR_PH]]:
41+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi ptr [ [[IND_END]], %[[MIDDLE_BLOCK]] ], [ [[START_1]], %[[ENTRY]] ]
42+
; CHECK-NEXT: [[BC_RESUME_VAL4:%.*]] = phi ptr [ [[IND_END3]], %[[MIDDLE_BLOCK]] ], [ [[START_2]], %[[ENTRY]] ]
43+
; CHECK-NEXT: br label %[[LOOP:.*]]
44+
; CHECK: [[LOOP]]:
45+
; CHECK-NEXT: [[PTR_IV:%.*]] = phi ptr [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[PTR_IV_NEXT:%.*]], %[[LOOP]] ]
46+
; CHECK-NEXT: [[PTR_IV_2:%.*]] = phi ptr [ [[BC_RESUME_VAL4]], %[[SCALAR_PH]] ], [ [[PTR_IV_2_NEXT:%.*]], %[[LOOP]] ]
47+
; CHECK-NEXT: [[PTR_IV_NEXT]] = getelementptr nusw i8, ptr [[PTR_IV]], i32 -4
48+
; CHECK-NEXT: [[PTR_IV_2_NEXT]] = getelementptr i8, ptr [[PTR_IV_2]], i32 -4
49+
; CHECK-NEXT: store i32 0, ptr [[PTR_IV_2]], align 4
50+
; CHECK-NEXT: [[EC:%.*]] = icmp eq ptr [[PTR_IV]], [[END]]
51+
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP3:![0-9]+]]
52+
; CHECK: [[EXIT]]:
53+
; CHECK-NEXT: ret void
54+
;
55+
entry:
56+
br label %loop
57+
58+
loop:
59+
%ptr.iv = phi ptr [ %start.1, %entry ], [ %ptr.iv.next, %loop ]
60+
%ptr.iv.2 = phi ptr [ %start.2, %entry ], [ %ptr.iv.2.next, %loop ]
61+
%ptr.iv.next = getelementptr nusw i8, ptr %ptr.iv, i32 -4
62+
%ptr.iv.2.next = getelementptr i8, ptr %ptr.iv.2, i32 -4
63+
store i32 0, ptr %ptr.iv.2, align 4
64+
%ec = icmp eq ptr %ptr.iv, %end
65+
br i1 %ec, label %exit, label %loop
66+
67+
exit:
68+
ret void
69+
}
70+
71+
attributes #0 = { "target-features"="+simd128" }
72+
;.
73+
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
74+
; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
75+
; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
76+
; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
77+
;.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if not "WebAssembly" in config.root.targets:
2+
config.unsupported = True

0 commit comments

Comments
 (0)