[MLIR] CSE misses an optimization opportunity to eliminate redundant `memref.load`
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
@llvm/issue-subscribers-mlir
Author: MonSec-2077
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
I can take a look.
@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
Thanks @madhur13490. I think I had typo in patch description which prevented the link between the issue and PR.