llvm / llvm/llvm-project

[MLIR] LICM fails to hoist loop-invariant `arith.addi` in a reachable loop

Open
#215,720 3 comments 0 reactions 0 assignees View on GitHub
missed-optimization mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Hi, I found a missed optimization bug in MLIR LICM. In the following example, `%2 = arith.addi %arg0, %arg0 : index` is loop-invariant in a reachable loop and should be hoisted outside the loop. However, MLIR fails to do so, resulting in a missed opt bug.

### Input Program

**input.mlir**

```llvm
module {
func.func @valid_symbol_affine_scope(%arg0: index, %arg1: memref) {
test.affine_scope {
%c1 = arith.constant 1 : index
%0 = arith.subi %arg0, %c1 : index
affine.for %arg2 = %0 to %arg0 {
%1 = arith.subi %0, %arg2 : index
test.affine_scope {
affine.for %arg3 = %1 to %arg0 {
%3 = affine.load %arg1[%arg0 - 1] : memref
affine.store %3, %arg1[%arg0 - 1] : memref
}
%2 = arith.addi %arg0, %arg0 : index
%c0 = arith.constant 0 : index
%alloca = memref.alloca() : memref<1xindex>
memref.store %2, %alloca[%c0] : memref<1xindex>
"terminate"() : () -> ()
}
}
"terminate"() : () -> ()
}
return
}
}
```

### Command
```bash
mlir-opt --allow-unregistered-dialect --loop-invariant-code-motion input.mlir
```

Version: 9d3a3fa57c47fd06efea20f100ec747f5e5fbfdd

Contributor guide

Open the contributing guide

Research direction

Start by running the provided input.mlir with mlir-opt --allow-unregistered-dialect --loop-invariant-code-motion and confirm that the arith.addi remains inside the reachable loop. Then trace the MLIR LICM pass and its handling of nested affine and test.affine_scope regions. Done means the invariant arith.addi is hoisted outside the loop and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.