[flang-commits] [flang] [flang] Add pass to version innermost loops on a likely constant trip count (1/2) (PR #231205)

via flang-commits flang-commits at lists.llvm.org
Fri Oct 16 23:08:40 PDT 2026


================
@@ -481,6 +481,40 @@ def LoopVersioning : Pass<"loop-versioning", "mlir::func::FuncOp"> {
   ];
 }
 
+def LoopTripCountVersioning
+    : Pass<"loop-tripcount-versioning", "mlir::func::FuncOp"> {
+  let summary = "Version innermost loops on a likely constant trip count";
+  let description = [{
+    Adds a runtime check and two variants of an innermost loop whose trip count
+    is likely to equal a compile-time constant, so that one variant has a
+    constant trip count that LLVM can fully unroll, or at least vectorize
+    without a minimum-iteration check and remainder loop. Both variants are the
+    original loop with only the bound changed, so the transformation is always
+    legal; the heuristics decide only where it is worthwhile.
+
+    The first heuristic takes the constant from the declared extent of an
+    explicit-shape array whose leading dimension the loop indexes: for
+    `a(1:n, :)` on `a(4, :)` the innermost loop runs `1..n`, and versioning on
+    `n == 4` gives the fast variant a trip count of 4. Further heuristics may
+    be added in the future.
+
+    Candidates are limited by the declared extent (`max-section-extent`) and
+    by the size of the fully unrolled fast variant, extent times loop body
+    operations (`max-unrolled-ops`, matching LLVM's -O3 full-unroll budget),
----------------
MattPD wrote:

Thanks, that resolves the sweep question. Keeping the limit at 300 is fine with me. I would want execution-time comparisons before raising it to 1200.

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


More information about the flang-commits mailing list