[MLIR] `buffer-loop-hoisting` fails to hoist buffer in `scf.while`
buffer-loop-hoisting fails to hoist memref.alloc out of the scf.while loop. Although the allocated buffer %buffer escapes as the result of the scf.while loop (%last), it is not passed back across iterations via scf.yield (which only yields an index).
Since every iteration overwrites the buffer before reading it and no cross-iteration memory dependence exists, hoisting %buffer to the loop entry block is completely safe. The current alias analysis conservatively treats the loop exit return value as a cross-iteration back-edge alias, causing a missed optimization.
Reproduction Example
input.mlir
module {
func.func @exit_alias(%arg0: index) -> index {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%0 = scf.while (%arg1 = %c0) : (index) -> memref<1xindex> {
%alloc = memref.alloc() : memref<1xindex>
memref.store %arg1, %alloc[%c0] : memref<1xindex>
%2 = arith.cmpi slt, %arg1, %arg0 : index
scf.condition(%2) %alloc : memref<1xindex>
} do {
^bb0(%arg1: memref<1xindex>):
%2 = memref.load %arg1[%c0] : memref<1xindex>
%3 = arith.addi %2, %c1 : index
scf.yield %3 : index
}
%1 = memref.load %0[%c0] : memref<1xindex>
return %1 : index
}
}
Command
mlir-opt input.mlir -buffer-loop-hoisting
Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25
@llvm/issue-subscribers-mlir
Author: MonSec-2077
Since every iteration overwrites the buffer before reading it and no cross-iteration memory dependence exists, hoisting %buffer to the loop entry block is completely safe. The current alias analysis conservatively treats the loop exit return value as a cross-iteration back-edge alias, causing a missed optimization.
Reproduction Example
input.mlir
module {
func.func @<!-- -->exit_alias(%arg0: index) -> index {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%0 = scf.while (%arg1 = %c0) : (index) -> memref<1xindex> {
%alloc = memref.alloc() : memref<1xindex>
memref.store %arg1, %alloc[%c0] : memref<1xindex>
%2 = arith.cmpi slt, %arg1, %arg0 : index
scf.condition(%2) %alloc : memref<1xindex>
} do {
^bb0(%arg1: memref<1xindex>):
%2 = memref.load %arg1[%c0] : memref<1xindex>
%3 = arith.addi %2, %c1 : index
scf.yield %3 : index
}
%1 = memref.load %0[%c0] : memref<1xindex>
return %1 : index
}
}
Command
mlir-opt input.mlir -buffer-loop-hoisting
Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25
I'm interested in looking into this if no one has already started working on it.
@MonSec-2077 can I ask you if this case was brought up in a specific frontend or compilation pipeline? Based on what I've looked at, I think there are two ways to handle this: A. Make a change in the hoisting pass that is SCF-specific, or B. A broader, generic change using interfaces, which would need additional semantic guarantees and could affect existing hoisting behaviour in other dialects.
Option A is simpler, but I'm worried about introducing dialect specific logic in a pass that is mostly interface based. For that reason I'd want to know where this pattern occurs in practice to better understand whether the benefits would justify the compromises of both options.
BufferAllocationLoopHoistingState uses the common dominator of all aliases as a proxy for passed via back-edge:
https://github.com/llvm/llvm-project/blob/d738d1b2be16f513816f32e5638a9bb2ce667d23/mlir/lib/Dialect/Bufferization/Transforms/BufferOptimizations.cpp#L369-L377
Here, the alias set includes the scf.while result, which is defined outside the loop, so hoisting is rejected. But the buffer only leaves via the exit edge (scf.condition, never via scf.yield), right?
No need for SCF-specific logic or a new interface, from my POV @splicer3.
It should be enough do to something like:
- Tell exit edges apart from loop-internal ones
- Compute the alias set for this check without exit edges, including everything downstream of the exit result
That's a cool issue, I would love to analyse this !!
My fault. I hadn't noticed that @splicer3 wanted to work on it. That said, @purnima-nlp, let's wait a few days to get feedback from @splicer3 🙏
Thanks @FedericoBruzzone, I’m still looking into this, and thanks @purnima-nlp for checking the reproducer against #224921.
I tried removing the scf.while result from a copy of the alias set used by this check, since in the repro there are also no aliases downstream of that result:
auto filteredAliases = resultAliases;
filteredAliases.erase(whileOp->getResult(0));
Block *domFull =
findCommonDominator(allocValue, resultAliases, dominance);
Block *domFiltered =
findCommonDominator(allocValue, filteredAliases, dominance);
Both return whileOp->getBlock(), and the filtered alias set boils down to the allocation in before and the memref argument in after.
I also passed domFiltered through to aliasDominatorBlock and inside isLegalPlacement, it still equals op->getBlock(), so the unchanged condition returns false and the hoist is rejected.
I was thinking about following casts/subviews and the value forwarding described by RegionBranchOpInterface, stopping at the loop's exit edges and checking whether the buffer reaches an argument of the region containing the allocation. The full alias information would be kept for the other checks. I'd like to hear your thoughts about it as well.
Also sorry for the late response, I should have more time this week.
No worries at all @splicer3. Thanks for checking, and good catch. My suggestion was incomplete. Dropping the exit result isn't enough. The after block argument sits in a different region than the alloc, so the common dominator still collapses to the block containing the scf.while.
At first glance, IIUC it looks like it might work, but I can't tell you any more than that :(
I'll keep working on that approach then and follow up if I find something else or when I get close to submitting a PR.
I also found that buffer-loop-hoisting can move allocations out of affine.parallel. I’ve opened another issue for that because it reproduces independently of the changes discussed here.
Without addressing that, the hoisting changes I'm planning would allow an allocation to move out of the while and then out of an enclosing affine.parallel as well. I’m planning to fix that first, rather than mixing it here.
I do have a prototype local change that fixes this with the approach we discussed above, but I'm still polishing it out.
I also found yet another case of hoisting out of a parallel loop, with OpenACC this time, which I've reported in #226090. For affine.parallel it was a trivial fix since adding the HasParallelRegion trait was enough, but acc.loop can be both sequential and parallel, so to preserve hoisting in the sequential case a different approach is needed.
Great! Don't hesitate to submit a PR when you're ready.
Thank you for your work. Really appreciated :D