[llvm] [LV] Vectorize early exit loops with stores using masking (PR #178454)

Graham Hunter via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 29 08:47:46 PST 2026


================
@@ -506,11 +506,16 @@ vputils::getRecipesForUncountableExit(VPlan &Plan,
   // }
 
   // Find the uncountable loop exit condition.
+  // TODO: Determine what we should be matching here after reviewers have
+  //       had a chance to look; BranchOnTwoConds or BranchOnCond with Or.
----------------
huntergr-arm wrote:

I guess @annamthomas or other RVV folks might want to weigh in as well, since there was interest in a predicated approach from their side.

I do have a fully predicated patch that drops in on top of Florian's approach as well, but when we (Arm) discussed it internally we had concerns about the overhead of full masking for multiple uncountable exits; the example we had was:

(Scalar code, _SN_ is a statement with a side-effect)
```
for (int i=0; i<n; ++i) {
  S1;
  if (cond1)
    break;
  S2;
  if (cond2)
    break;
  S3
}
```

(ACLE-style pseudo code)
```
// determine vector iterations, whether we enter vector body, etc...
for (; i < vec_n; i+=inc) {
  svuint32_t cond1_load = svld1_u32(ptrue, pred1 + i);
  svuint32_t cond2_load = svld1_u32(ptrue, pred2 + i);

  svbool_t pcond1 = svcmpgt(ptrue, cond1_load, 500);  // cond1
  svbool_t pcond2 = svcmple(ptrue, cond2_load, 30); // cond2
  svbool_t pcond = svor(pcond1, pcond2);

  // Mask for all active lanes, including the one that exited early
  svbool_t mask_before_exit = svbrka_z(ptrue, pcond);
  vecS1(mask_before_exit)
  // Mask after pcond1, which may have been the first exit reached
  // Bic would just clear the exiting lane if it did
  // Would need extra bics for more exits
  svbool_t mask_between = svbic(mask_before_exit, pcond1);
  vecS2(mask_between)
  // Mask all lanes before the exiting lane
  svbool_t mask_after = svbrkb_z(ptrue, pcond);
  vecS3(mask_after)

  if (svptest_any(ptrue, pcond))
    return;
}
// Not tail-folded, so scalar loop would be at the end here...
```

Here, since there are side-effects before, after, and between the two uncountable exits, we need to generate multiple masks to ensure we don't execute extra lanes. Increasing the number of exits also increases the number of masks needed, though we may be able to remove some.

The hybrid approach in this current PR avoids the need for generating the extra masks.

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


More information about the llvm-commits mailing list