[Mlir-commits] [mlir] [MLIR][SCF] Sink scf.if from scf.while before region into after region. (PR #165216)

Kunwar Grover llvmlistbot at llvm.org
Fri Nov 7 07:33:45 PST 2025


Groverkss wrote:

<blockquote>
As far as I understand, this transformation is doing:

if (%cond) {
  foo()
}
cond_br %cond ^continue, ^end

^continue:
    ...
^end:
   ...
->

cond_br %cond ^continue, ^end

^continue:
    foo()
    ...
^end:
   ...
</blockquite>

I think this may be over simplifying the transformation a bit. Maybe I'm wrong (so feel free to correct my example if i'm wrong), but let me try to give an example. Let's take the case:

```
while {

%something = some_loop_invariant_compute_but_i_never_hoisted_it_out()
if (%cond) {
  foo(%something)
}
cond_br %cond ^continue, ^end

} while {

^continue:
    ...
^end:
   ...
}
```

If you decide to do the same transformation, you need to somehow also forward `%something`. You need to either:

- Check you don't need to forward anything from the conditional region, which would mean check all uses inside the if block
OR
- Clone the entire backward chain

Both of these seem heavy weight. Note that you cannot really safely do this transformation without doing atleast one of these. Analyze a whole region or cloning an entire backward chain (or finding that backward chain) seems a bit too much for a canonicalization maybe.

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


More information about the Mlir-commits mailing list