[MLIR][Affine] buffer-loop-hoisting moves allocations out of affine.parallel
While investigating #223944, I noticed that buffer-loop-hoisting moves an allocation out of affine.parallel.
With this input:
func.func @affine_parallel_alloc(%out: memref<2xindex>) {
%c0 = arith.constant 0 : index
affine.parallel (%i) = (0) to (2) {
%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>
}
return
}
Running:
mlir-opt input.mlir -buffer-loop-hoisting
on 16ff0ec65655a16f9a01c20d059d1e6ddb27f6b9 produces:
module {
func.func @affine_parallel_alloc(%arg0: memref<2xindex>) {
%c0 = arith.constant 0 : index
%alloc = memref.alloc() : memref<1xindex>
affine.parallel (%arg1) = (0) to (2) {
memref.store %arg1, %alloc[%c0] : memref<1xindex>
%0 = memref.load %alloc[%c0] : memref<1xindex>
memref.store %0, %arg0[%arg1] : memref<2xindex>
}
return
}
}
The allocation is now shared between the parallel iterations, rather than being local to each iteration. Since each iteration writes to and reads from the same cell, I’d expect the allocation to remain inside affine.parallel. I’ve checked the transformed IR, but haven’t tested the resulting behaviour under parallel execution.
Based on what I’ve looked at, this seems to come from AffineParallelOp missing HasParallelRegion. It implements LoopLikeOpInterface, so isSequentialLoop treats it as sequential when that trait is absent.
#90735 introduced this safeguard for scf.parallel and scf.forall. I was thinking of adding the same trait to affine.parallel, with a regression test for the case above.
I’m planning to work on this separately from #223944, since it reproduces independently of the changes being considered there.
@llvm/issue-subscribers-mlir
Author: Matteo Di Iorio (splicer3)
With this input:
func.func @<!-- -->affine_parallel_alloc(%out: memref<2xindex>) {
%c0 = arith.constant 0 : index
affine.parallel (%i) = (0) to (2) {
%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>
}
return
}
Running:
mlir-opt input.mlir -buffer-loop-hoisting
on 16ff0ec65655a16f9a01c20d059d1e6ddb27f6b9 produces:
module {
func.func @<!-- -->affine_parallel_alloc(%arg0: memref<2xindex>) {
%c0 = arith.constant 0 : index
%alloc = memref.alloc() : memref<1xindex>
affine.parallel (%arg1) = (0) to (2) {
memref.store %arg1, %alloc[%c0] : memref<1xindex>
%0 = memref.load %alloc[%c0] : memref<1xindex>
memref.store %0, %arg0[%arg1] : memref<2xindex>
}
return
}
}
The allocation is now shared between the parallel iterations, rather than being local to each iteration. Since each iteration writes to and reads from the same cell, I’d expect the allocation to remain inside affine.parallel. I’ve checked the transformed IR, but haven’t tested the resulting behaviour under parallel execution.
Based on what I’ve looked at, this seems to come from AffineParallelOp missing HasParallelRegion. It implements LoopLikeOpInterface, so isSequentialLoop treats it as sequential when that trait is absent.
#90735 introduced this safeguard for scf.parallel and scf.forall. I was thinking of adding the same trait to affine.parallel, with a regression test for the case above.
I’m planning to work on this separately from #223944, since it reproduces independently of the changes being considered there.
@llvm/issue-subscribers-mlir-affine
Author: Matteo Di Iorio (splicer3)
With this input:
func.func @<!-- -->affine_parallel_alloc(%out: memref<2xindex>) {
%c0 = arith.constant 0 : index
affine.parallel (%i) = (0) to (2) {
%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>
}
return
}
Running:
mlir-opt input.mlir -buffer-loop-hoisting
on 16ff0ec65655a16f9a01c20d059d1e6ddb27f6b9 produces:
module {
func.func @<!-- -->affine_parallel_alloc(%arg0: memref<2xindex>) {
%c0 = arith.constant 0 : index
%alloc = memref.alloc() : memref<1xindex>
affine.parallel (%arg1) = (0) to (2) {
memref.store %arg1, %alloc[%c0] : memref<1xindex>
%0 = memref.load %alloc[%c0] : memref<1xindex>
memref.store %0, %arg0[%arg1] : memref<2xindex>
}
return
}
}
The allocation is now shared between the parallel iterations, rather than being local to each iteration. Since each iteration writes to and reads from the same cell, I’d expect the allocation to remain inside affine.parallel. I’ve checked the transformed IR, but haven’t tested the resulting behaviour under parallel execution.
Based on what I’ve looked at, this seems to come from AffineParallelOp missing HasParallelRegion. It implements LoopLikeOpInterface, so isSequentialLoop treats it as sequential when that trait is absent.
#90735 introduced this safeguard for scf.parallel and scf.forall. I was thinking of adding the same trait to affine.parallel, with a regression test for the case above.
I’m planning to work on this separately from #223944, since it reproduces independently of the changes being considered there.