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

Krzysztof Drewniak llvmlistbot at llvm.org
Mon Apr 14 09:21:26 PDT 2025


krzysz00 wrote:

Re the [2, 3] and [4, 6] example, what I think @ftynse is saying is that, *by definition of `affine.for`*, the upper bound will always be the [2, 3] value, and the [4, 6] value can be ignored.

---

Now, to give the proposed optimization without GPU context

Let `0 <= x < y` - we don't know what `x` is, but it's in `[0, y)`

Then, the loop
```
affine.for %arg0 = %x to %y step %y {
  [body]
}
```
is guaranteed to run exactly once and so can be unrolled to just `body`.

Usually, `%y` here is a constant - in the context of this GPU stuff, it'll be something like "number of threads in a block"

In the unrolling case, this generalizes to
```
// 0 <= %r < %y
%b = %y * N + %r
affine.for %arg0 = %x to %r step %y {
  ...
}
```
being unrollable `N` times.

Note similarly, that if we have `0 <= %z < %y`, we can optimize
```
affine.for %arg0 = %x to %z step %y {
  ...
}
```
to
```
affine.if %x < %z {
  ...
}
```

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


More information about the Mlir-commits mailing list