llvm / llvm/llvm-project

[MLIR] LICM cannot recognize the invariant in nested `affine.for` loop

Open
#215,492 4 comments 0 reactions 0 assignees View on GitHub
mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

In the following input program, the operation at line 9 is loop-invariant. However, MLIR LICM fails to recognize it and hoist it out of the loop, resulting in a missed optimization bug.

### Input Program
**input.mlir**
```llvm
#set = affine_set<(d0, d1) : (d1 - d0 >= 0)>
module {
func.func @invariant_affine_if() {
%alloc = memref.alloc() : memref<10xf32>
%cst = arith.constant 8.000000e+00 : f32
affine.for %arg0 = 0 to 10 {
affine.for %arg1 = 0 to 10 {
affine.if #set(%arg0, %arg0) {
%0 = arith.addf %cst, %cst : f32
%c0 = arith.constant 0 : index
%alloca = memref.alloca() : memref<1xf32>
memref.store %0, %alloca[%c0] : memref<1xf32>
%1 = arith.addf %cst, %cst : f32
affine.store %1, %alloc[%arg0] : memref<10xf32>
}
}
}
return
}
}
```

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

Version: 9d3a3fa57c47fd06efea20f100ec747f5e5fbfdd

Contributor guide

Open the contributing guide

Research direction

Reproduce the nested-loop case from input.mlir with mlir-opt input.mlir -loop-invariant-code-motion, then inspect the MLIR loop-invariant code-motion pass entry point and its handling of nested affine.for and affine.if operations. Done means the invariant operation is hoisted out of the inner loop without changing the program's behavior.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.