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

[MLIR] `--control-flow-sink` misses sinking into multi-block `scf.execute_region`

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

MLIR optimization --control-flow-sink misses a legal sinking opportunity when the only use of a side-effect-free op is inside a multi-block scf.execute_region. %0 should be sunk into the scf.execute_region, since arith.muli is side-effect-free/speculatable and all uses are inside the region. However, the input program is unchanged, resulting in a missed optimization bug.

Input Program

input.mlir

module {
  func.func private @sink_i32(i32)
  func.func @multiblock_execute_region(%arg0: i32, %arg1: i32) {
    %0 = arith.muli %arg0, %arg1 : i32
    scf.execute_region {
      cf.br ^bb1
    ^bb1:  // pred: ^bb0
      func.call @sink_i32(%0) : (i32) -> ()
      scf.yield
    }
    return
  }
}

Command

mlir-opt input.mlir --control-flow-sink

Version: ef88536bbf62469eb2396d719d237b3191cf4843

dev-816 avatar Aug 17 '26 13:08 dev-816

@llvm/issue-subscribers-mlir

Author: CC_Fans (dev-816)

MLIR optimization `--control-flow-sink` misses a legal sinking opportunity when the only use of a side-effect-free op is inside a multi-block `scf.execute_region`. `%0` should be sunk into the `scf.execute_region`, since `arith.muli` is side-effect-free/speculatable and all uses are inside the region. However, the input program is unchanged, resulting in a missed optimization bug.

Input Program

input.mlir

module {
  func.func private @<!-- -->sink_i32(i32)
  func.func @<!-- -->multiblock_execute_region(%arg0: i32, %arg1: i32) {
    %0 = arith.muli %arg0, %arg1 : i32
    scf.execute_region {
      cf.br ^bb1
    ^bb1:  // pred: ^bb0
      func.call @<!-- -->sink_i32(%0) : (i32) -&gt; ()
      scf.yield
    }
    return
  }
}

Command

mlir-opt input.mlir --control-flow-sink

Version: ef88536bbf62469eb2396d719d237b3191cf4843

I'd like to work on this issue, I think I might have a fix for this.

xakep8 avatar Aug 18 '26 06:08 xakep8