[llvm] [LV][VPlan] Implement VPlan-based cost for exit condition. (PR #125640)

Elvis Wang via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 7 22:50:53 PDT 2025


================
@@ -116,11 +119,12 @@ define i1 @test_extra_cmp_user(ptr nocapture noundef %dst, ptr nocapture noundef
 ; CHECK-LABEL: LV: Checking a loop in 'test_extra_cmp_user'
 ; CHECK: Cost of 4 for VF 8: induction instruction   %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
 ; CHECK-NEXT: Cost of 0 for VF 8: induction instruction   %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
-; CHECK-NEXT: Cost of 4 for VF 8: exit condition instruction   %exitcond.not = icmp eq i64 %indvars.iv.next, 16
 ; CHECK-NEXT: Cost of 0 for VF 8: EMIT vp<{{.+}}> = CANONICAL-INDUCTION ir<0>, vp<%index.next>
-; CHECK: Cost for VF 8: 12
+; CHECK: Cost of 1 for VF 8: EMIT branch-on-count vp<%{{.+}}>, vp<%{{.+}}>
+; CHECK: Cost for VF 8: 13
----------------
ElvisWang123 wrote:

Add the cost of icmp to make test more clearly.

In this case,  the icmp will be widen and  has a external user in the middle block, the cost is 4. Also the cost of the branch-on-count will generate a scalar icmp which cost 1. So the entire cost increase.

Actually, if forcing VF=8, the plan will select UF=2. The cost of ICmp and branch-on-count should be free.

VPlan:
```
VPlan 'Initial VPlan for VF={2,4,8,16},UF>=1' {
Live-in vp<%0> = VF
Live-in vp<%1> = VF * UF
Live-in vp<%2> = vector-trip-count
Live-in ir<16> = original trip-count

ir-bb<entry>:
Successor(s): scalar.ph, vector.ph

vector.ph:
Successor(s): vector loop

<x1> vector loop: {
  vector.body:
    EMIT vp<%3> = CANONICAL-INDUCTION ir<0>, vp<%index.next>
    ir<%indvars.iv> = WIDEN-INDUCTION  ir<0>, ir<1>, vp<%0>
    vp<%4> = SCALAR-STEPS vp<%3>, ir<1>, vp<%0>
    CLONE ir<%arrayidx> = getelementptr inbounds nuw ir<%src>, vp<%4>
    vp<%5> = vector-pointer ir<%arrayidx>
    WIDEN ir<%0> = load vp<%5>
    CLONE ir<%arrayidx2> = getelementptr inbounds nuw ir<%dst>, vp<%4>
    vp<%6> = vector-pointer ir<%arrayidx2>
    WIDEN ir<%1> = load vp<%6>
    WIDEN ir<%add> = add nsw ir<%1>, ir<%0>
    vp<%7> = vector-pointer ir<%arrayidx2>
    WIDEN store vp<%7>, ir<%add>
    WIDEN ir<%indvars.iv.next> = add nuw nsw ir<%indvars.iv>, ir<1>
    WIDEN ir<%exitcond.not> = icmp eq ir<%indvars.iv.next>, ir<16>
    EMIT vp<%index.next> = add nuw vp<%3>, vp<%1>
    EMIT branch-on-count vp<%index.next>, vp<%2>
  No successors
}
Successor(s): middle.block

middle.block:
  EMIT vp<%9> = extract-last-element ir<%exitcond.not>
  EMIT vp<%cmp.n> = icmp eq ir<16>, vp<%2>
  EMIT branch-on-cond vp<%cmp.n>
Successor(s): ir-bb<exit>, scalar.ph
```

https://github.com/llvm/llvm-project/pull/125640


More information about the llvm-commits mailing list