llvm / llvm/llvm-project

[MLIR] Unused `scf.if` results are not removed by `--remove-dead-values`

Open
#216,646 1 comment 0 reactions 0 assignees View on GitHub
mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The compiler optimization `--remove-dead-values` does not remove an unused result of `scf.if` (`%r` at line 3) when the branches contain side-effecting operations, even though only the result is dead and the `scf.if` itself must be preserved, leading to a missed optimization bug.

### Input Program

`input.mlir`

```llvm
module {
func.func @func_t13(%cond: i1, %m: memref) {
%r = scf.if %cond -> (i32) {
%c1 = arith.constant 1 : i32
memref.store %c1, %m[] : memref
scf.yield %c1 : i32
} else {
%c2 = arith.constant 2 : i32
memref.store %c2, %m[] : memref
scf.yield %c2 : i32
}
return
}
}
```

Ideally, it should optimized into the following form:

```llvm
module {
func.func @func_t13(%cond: i1, %m: memref) {
scf.if %cond {
%c1 = arith.constant 1 : i32
memref.store %c1, %m[] : memref
} else {
%c2 = arith.constant 2 : i32
memref.store %c2, %m[] : memref
}
return
}
}
```

### Command
```bash
mlir-opt --remove-dead-values input.mlir
```

Version: ef88536bbf62469eb2396d719d237b3191cf4843

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with input.mlir using mlir-opt --remove-dead-values and compare the output with the expected form. Trace the --remove-dead-values entry point and the handling of scf.if results; done means the unused result is removed while both side-effecting branches and the scf.if remain.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.