[llvm] [LV]: Improve accuracy of calculating remaining iterations of MainLoopVF (PR #156723)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 30 05:29:47 PDT 2025


================
@@ -4371,11 +4371,30 @@ VectorizationFactor LoopVectorizationPlanner::selectEpilogueVectorizationFactor(
   const SCEV *TC =
       vputils::getSCEVExprForVPValue(getPlanFor(MainLoopVF).getTripCount(), SE);
   assert(!isa<SCEVCouldNotCompute>(TC) && "Trip count SCEV must be computable");
-  RemainingIterations =
-      SE.getURemExpr(TC, SE.getElementCount(TCType, MainLoopVF * IC));
+
+  // Calculate runtime estimated value for TC/MainLoopVF only when they are in
+  // different numerical space.
+  // TODO: This is a workaround until SCEV can evaluate vscale-based
+  // expressions.
+  if (match(TC, m_scev_Mul(m_SCEV(), m_SCEVVScale())) &&
+      MainLoopVF.isScalable()) {
+    RemainingIterations =
+        SE.getURemExpr(TC, SE.getElementCount(TCType, MainLoopVF * IC));
+  } else {
+    if (match(TC, m_scev_Mul(m_SCEV(), m_SCEVVScale()))) {
----------------
david-arm wrote:

I'm not sure if this is right, because what about when the trip count is `tc = mul i64 %n, %vscale`, i.e. unknown?

Don't you just want to create a SCEV mul expression based on `CM.getVScaleForTuning`, i.e. if you did something like:

```
const SCEV *KnownMinTC;
bool ScalableTC = match(TC, m_scev_Mul(m_SCEV(KnownMinTC), m_SCEVVScale()));
...

if (ScalableTC) {
  TC = SE.getMulExpr(KnownMinTC, SE.getConstant(CM.getVScaleForTuning()));
}
```

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


More information about the llvm-commits mailing list