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

[MLIR] Missed Optimization: Disjoint stores prevent hoisting an invariant `affine.load`

Open dev-816 opened this issue 2 months ago • 3 comments

The load from A[0] can move before the loop because all loop stores access A[1..16]. However, the interference check treats any write to the same memref as a conflict without distinguishing the accessed addresses.

Inpu Program

input.mlir

func.func @addresses(%A: memref<17xi32>, %v: i32) {
  affine.store %v, %A[0] : memref<17xi32>
  affine.for %i = 0 to 16 {
    %x = affine.load %A[0] : memref<17xi32>
    affine.store %x, %A[%i + 1] : memref<17xi32>
  }
  return
}

Command

mlir-opt input.mlir -affine-loop-invariant-code-motion

Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25

dev-816 avatar Sep 29 '26 16:09 dev-816

@llvm/issue-subscribers-mlir

Author: CC_Fans (dev-816)

The load from `A[0]` can move before the loop because all loop stores access `A[1..16]`. However, the interference check treats any write to the same memref as a conflict without distinguishing the accessed addresses.

Inpu Program

input.mlir

func.func @<!-- -->addresses(%A: memref&lt;17xi32&gt;, %v: i32) {
  affine.store %v, %A[0] : memref&lt;17xi32&gt;
  affine.for %i = 0 to 16 {
    %x = affine.load %A[0] : memref&lt;17xi32&gt;
    affine.store %x, %A[%i + 1] : memref&lt;17xi32&gt;
  }
  return
}

Command

mlir-opt input.mlir -affine-loop-invariant-code-motion

Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25

I'm cleaning up my implementation and will open a PR soon.

heturing avatar Oct 04 '26 07:10 heturing

I've opened a PR. Reviews and feedback are welcome.

heturing avatar Oct 05 '26 04:10 heturing