llvm / llvm/llvm-project

[MLIR] LICM fails to hoist loop-invariant ops from nested regions in `scf.parallel`

Open
#215,458 1 comment 0 reactions 1 assignee Claimed by @ukalappa-mips View on GitHub
mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

MLIR LICM misses hoisting of speculatable loop-invariant ops nested inside scf.if within `scf.parallel`. In the example below, `arith.addi %arg6, %arg7` is loop invariant with respect to `scf.parallel`. It has no side effects and is speculatable, but `-loop-invariant-code-motion` does not hoist it out of the loop, leading to a missed opt bug.

### Input Program

**input.mlir**

```llvm
module {
func.func @routine_call_in_nested_if(
%arg0: memref<4xf32>,
%arg1: memref<4xf32>,
%x: index,
%y: index) {
acc.compute_region
ins(%arg10 = %arg0, %arg11 = %arg1, %sx = %x, %sy = %y)
: (memref<4xf32>, memref<4xf32>, index, index) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c2 = arith.constant 2 : index

scf.parallel (%iv) = (%c0) to (%c2) step (%c1) {
%cond = arith.cmpi eq, %iv, %c0 : index

scf.if %cond {
%1 = arith.addi %sx, %sy : index // should be optimized
%alloca = memref.alloca() : memref<1xindex>
memref.store %1, %alloca[%c0] : memref<1xindex>
func.call @vector_routine(%arg10) : (memref<4xf32>) -> ()
} else {
func.call @vector_routine(%arg11) : (memref<4xf32>) -> ()
}

scf.reduce
} {acc.par_dims = #acc}

acc.yield
} {origin = "acc.parallel"}

return
}

func.func private @vector_routine(memref<4xf32>)
attributes {acc.routine_info = #acc.routine_info<[@acc_routine_vector]>}

acc.routine @acc_routine_vector func(@vector_routine) vector
}
```

### Used Command
```bash
mlir-opt input.mlir -loop-invariant-code-motion
```

Version: 9d3a3fa57c47fd06efea20f100ec747f5e5fbfdd

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.