llvm / llvm/llvm-project

MLIR inliner doesn't respect alloca scope

Open
#197,562 3 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

https://godbolt.org/z/6T7vE9KhT

inlining this:
```

// RUN: mlir-opt --inline %s | FileCheck %s

module {

func.func private @use_i32(memref)

func.func @callee(%arg0: memref) {
%0 = memref.alloca() : memref
%c1_i32 = arith.constant 1 : i32
memref.store %c1_i32, %0[] : memref
func.call @use_i32(%0) : (memref) -> ()
return
}

func.func @test_alloca_in_loop(%arg0: memref) {
%c0 = arith.constant 0 : index
%c10 = arith.constant 10 : index
%c1 = arith.constant 1 : index
cf.br ^bb1(%c0 : index)

^bb1(%i: index):
%cond = arith.cmpi ult, %i, %c10 : index
cf.cond_br %cond, ^bb2, ^bb3

^bb2:
func.call @callee(%arg0) : (memref) -> ()
%next = arith.addi %i, %c1 : index
cf.br ^bb1(%next : index)

^bb3:
return
}
}

```

results in the alloca being within the scf for [potentially causing overflow]

same issue for llvm alloca: https://godbolt.org/z/xWezh5rod

```
// RUN: mlir-opt --inline %s | FileCheck %s

module {
func.func private @use_i32(!llvm.ptr)

func.func @callee(%arg0: memref) {
%c1_i32 = arith.constant 1 : i32
%0 = llvm.alloca %c1_i32 x i32 : (i32) -> !llvm.ptr
func.call @use_i32(%0) : (!llvm.ptr) -> ()
return
}

func.func @test_alloca_in_loop(%arg0: memref) {
%c0 = arith.constant 0 : index
%c10 = arith.constant 10 : index
%c1 = arith.constant 1 : index
cf.br ^bb1(%c0 : index)

^bb1(%i: index):
%cond = arith.cmpi ult, %i, %c10 : index
cf.cond_br %cond, ^bb2, ^bb3

^bb2:
func.call @callee(%arg0) : (memref) -> ()
%next = arith.addi %i, %c1 : index
cf.br ^bb1(%next : index)

^bb3:
return
}
}
```

cc @pengmai @ftynse @ivanradanov

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the two provided MLIR snippets by running `mlir-opt --inline` and inspecting the output, using the Godbolt examples for comparison. Investigate how the inline pass handles `memref.alloca` and `llvm.alloca` across the shown loop, and consider the issue done when both allocas retain the required scope and the FileCheck-based reproductions pass.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.