[mlir][openacc] buffer-loop-hoisting moves allocations out of parallel acc.loop
While working on #223944 I found that buffer-loop-hoisting can move an allocation out of a parallel acc.loop, making the buffer shared between iterations.
This is similar to the affine.parallel problem that has been fixed in #225324, but acc.loop can represent both sequential and parallel execution, making this less trivial to fix.
Reproducer:
func.func @parallel_acc_alloc(%out: memref<2xindex>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c2 = arith.constant 2 : index
acc.parallel {
acc.loop gang vector control(%i : index) = (%c0 : index)
to (%c2 : index) step (%c1 : index) {
%buffer = memref.alloc() : memref<1xindex>
memref.store %i, %buffer[%c0] : memref<1xindex>
%value = memref.load %buffer[%c0] : memref<1xindex>
memref.store %value, %out[%i] : memref<2xindex>
acc.yield
} independent
acc.yield
}
return
}
mlir-opt input.mlir --buffer-loop-hoisting
The allocation moves before acc.loop, when it should remain inside the loop so each iteration has its own buffer.
Running with --acc-compute-lowering afterwards makes it so the allocation ends up outside scf.parallel, and parallel iterations store to and load from the same location.
isSequentialLoop currently checks for LoopLikeOpInterface or a region control-flow cycle, excluding operations with HasParallelRegion. acc.loop doesn't have that trait so parallel instances pass the check.
Adding the trait unconditionally would also block hoisting from sequential acc.loop instances since execution mode can depend on the clauses, device-type overrides and enclosing context.
Maybe a per-operation query on LoopLikeOpInterface could fix this so OpenACC can describe its execution semantics while keeping buffer hoisting agnostic from specific dialects? If anyone has some feedback, it's more than welcome.
Tested with a local build based on 4f7dd9b7eb6285ac32467860b817119ba4c0fa11.
@llvm/issue-subscribers-mlir
Author: Matteo Di Iorio (splicer3)
This is similar to the affine.parallel problem that has been fixed in #225324, but acc.loop can represent both sequential and parallel execution, making this less trivial to fix.
Reproducer:
func.func @<!-- -->parallel_acc_alloc(%out: memref<2xindex>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c2 = arith.constant 2 : index
acc.parallel {
acc.loop gang vector control(%i : index) = (%c0 : index)
to (%c2 : index) step (%c1 : index) {
%buffer = memref.alloc() : memref<1xindex>
memref.store %i, %buffer[%c0] : memref<1xindex>
%value = memref.load %buffer[%c0] : memref<1xindex>
memref.store %value, %out[%i] : memref<2xindex>
acc.yield
} independent
acc.yield
}
return
}
mlir-opt input.mlir --buffer-loop-hoisting
The allocation moves before acc.loop, when it should remain inside the loop so each iteration has its own buffer.
Running with --acc-compute-lowering afterwards makes it so the allocation ends up outside scf.parallel, and parallel iterations store to and load from the same location.
isSequentialLoop currently checks for LoopLikeOpInterface or a region control-flow cycle, excluding operations with HasParallelRegion. acc.loop doesn't have that trait so parallel instances pass the check.
Adding the trait unconditionally would also block hoisting from sequential acc.loop instances since execution mode can depend on the clauses, device-type overrides and enclosing context.
Maybe a per-operation query on LoopLikeOpInterface could fix this so OpenACC can describe its execution semantics while keeping buffer hoisting agnostic from specific dialects? If anyone has some feedback, it's more than welcome.
Tested with a local build based on 4f7dd9b7eb6285ac32467860b817119ba4c0fa11.
Closed by #231372.