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

[MLIR] CSE misses an optimization opportunity to eliminate redundant `memref.load`

Open MonSec-2077 opened this issue 3 months ago • 4 comments

MLIR --cse misses an optimization opportunity to eliminate a redundant memref.load within the same region, even when the first load (at line 3) strictly dominates the second (at line 6) and no intervening writes or side effects exist.

Input Program

input.mlir

module {
  func.func @domination(%arg0: memref<?xi32>, %arg1: index) -> (i32, i32) {
    %0 = memref.load %arg0[%arg1] : memref<?xi32>
    cf.br ^bb1
  ^bb1:
    %1 = memref.load %arg0[%arg1] : memref<?xi32>
    return %0, %1 : i32, i32
  }
}

Command

mlir-opt --cse input.mlir

Version: 157af9cb0e9cad7df0e8e1d44c78f08ffa7c48e7

MonSec-2077 avatar Aug 22 '26 10:08 MonSec-2077

@llvm/issue-subscribers-mlir

Author: MonSec-2077

MLIR `--cse` misses an optimization opportunity to eliminate a redundant `memref.load` within the same region, even when the first load (at line 3) strictly dominates the second (at line 6) and no intervening writes or side effects exist.

Input Program

input.mlir

module {
  func.func @<!-- -->domination(%arg0: memref&lt;?xi32&gt;, %arg1: index) -&gt; (i32, i32) {
    %0 = memref.load %arg0[%arg1] : memref&lt;?xi32&gt;
    cf.br ^bb1
  ^bb1:
    %1 = memref.load %arg0[%arg1] : memref&lt;?xi32&gt;
    return %0, %1 : i32, i32
  }
}

Command

mlir-opt --cse input.mlir

Version: 157af9cb0e9cad7df0e8e1d44c78f08ffa7c48e7

I can take a look.

prometheusfma-llvm avatar Aug 22 '26 15:08 prometheusfma-llvm

@prometheusfma-llvm have you had a chance to look at this?

As far as I could tell here, the code that causes this missed optimization is the:

existing->getBlock() == op->getBlock()

But I imagine this may require a lot more refactoring as it will affect the CSE pass itself entirely. Maybe others have an opinion as well.

cc:@Men-cotton

pranavalavandi avatar Aug 26 '26 06:08 pranavalavandi

Thanks @madhur13490. I think I had typo in patch description which prevented the link between the issue and PR.

prometheusfma-llvm avatar Aug 26 '26 08:08 prometheusfma-llvm