llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[MLIR][Affine] buffer-loop-hoisting moves allocations out of affine.parallel

Open splicer3 opened this issue 2 months ago • 2 comments

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.

splicer3 avatar Sep 21 '26 17:09 splicer3

@llvm/issue-subscribers-mlir

Author: Matteo Di Iorio (splicer3)

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&lt;2xindex&gt;) {
  %c0 = arith.constant 0 : index
  affine.parallel (%i) = (0) to (2) {
    %buffer = memref.alloc() : memref&lt;1xindex&gt;
    memref.store %i, %buffer[%c0] : memref&lt;1xindex&gt;
    %value = memref.load %buffer[%c0] : memref&lt;1xindex&gt;
    memref.store %value, %out[%i] : memref&lt;2xindex&gt;
  }
  return
}

Running:

mlir-opt input.mlir -buffer-loop-hoisting

on 16ff0ec65655a16f9a01c20d059d1e6ddb27f6b9 produces:

module {
  func.func @<!-- -->affine_parallel_alloc(%arg0: memref&lt;2xindex&gt;) {
    %c0 = arith.constant 0 : index
    %alloc = memref.alloc() : memref&lt;1xindex&gt;
    affine.parallel (%arg1) = (0) to (2) {
      memref.store %arg1, %alloc[%c0] : memref&lt;1xindex&gt;
      %0 = memref.load %alloc[%c0] : memref&lt;1xindex&gt;
      memref.store %0, %arg0[%arg1] : memref&lt;2xindex&gt;
    }
    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)

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&lt;2xindex&gt;) {
  %c0 = arith.constant 0 : index
  affine.parallel (%i) = (0) to (2) {
    %buffer = memref.alloc() : memref&lt;1xindex&gt;
    memref.store %i, %buffer[%c0] : memref&lt;1xindex&gt;
    %value = memref.load %buffer[%c0] : memref&lt;1xindex&gt;
    memref.store %value, %out[%i] : memref&lt;2xindex&gt;
  }
  return
}

Running:

mlir-opt input.mlir -buffer-loop-hoisting

on 16ff0ec65655a16f9a01c20d059d1e6ddb27f6b9 produces:

module {
  func.func @<!-- -->affine_parallel_alloc(%arg0: memref&lt;2xindex&gt;) {
    %c0 = arith.constant 0 : index
    %alloc = memref.alloc() : memref&lt;1xindex&gt;
    affine.parallel (%arg1) = (0) to (2) {
      memref.store %arg1, %alloc[%c0] : memref&lt;1xindex&gt;
      %0 = memref.load %alloc[%c0] : memref&lt;1xindex&gt;
      memref.store %0, %arg0[%arg1] : memref&lt;2xindex&gt;
    }
    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.