[Mlir-commits] [mlir] [mlir][affine] Use value bound inference to determine minimum/maximum trip counts in loop analysis (PR #128113)

Oleksandr Alex Zinenko llvmlistbot at llvm.org
Mon Apr 14 02:22:39 PDT 2025


ftynse wrote:

> tripCount = (upper - (blockSize - 1)) div stride
maxTripCount = (uppper - 0) div stride
(blockSize - 1 ) = maxThreadId = blockSIze - 1
0 = minThreadId = 0

Affine loop transformations shouldn't care about block sizes or thread IDs. They should only reason about ranges of the induction variables, regardless of where those ranges come from.

>  max bound is used to determine whether to keep the last loop (which is equivalent to an if statement).

It appears to me that the loop must be preserved whenever the upper bound is not equal to the lower bound, which is equivalent to saying we cannot find the single constant bound (min=max=constant). This is exactly what we already do, just using less powerful reasoning.

> Suppose there is a loop here, but affineMap has two results. They are in the ranges [4, 6] and [2, 3], and before they should have resulted in [2, 3], which is clearly not true, and now the result is [2, 6]. It shows that the unroll trip should be 2, and 6 shows the need to keep a loop that controls the boundary.

The upper bound of the affine loop is systematically a minimum of all the expressions. So if you two expressions with ranges [4, 6] and [2, 3] (non-overlapping), the expressions with [4, 6] will _never_ affect the bound. We can even remove it as an optimization.

This is why it's important to understand the mathematics behind these computations...


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


More information about the Mlir-commits mailing list