[Mlir-commits] [mlir] [mlir][memref] Move dealloc for unused memref to after alloc (PR #227975)

Matthias Springer llvmlistbot at llvm.org
Thu Oct 1 19:59:29 PDT 2026


https://github.com/matthias-springer commented:

I didn't have time to look into this any further, but here are two more test cases that were flagged by a coding agent:

```mlir
//--- crash.mlir
// memref.realloc has a Free effect on %a, so it is selected as the freeing op.
// Its first operand sets lastDeallocOperandDef to the local memref.alloc.
// The defining op of %size is outside the scf.if body; findAncestorOpInBlock
// returns nullptr, which is passed to happensBefore and dereferenced.
// Expected: successful transformation that preserves operand dominance.
// Actual: null dereference at OptimizeAllocationLiveness.cpp:166.
func.func @realloc_outer_size(%cond: i1) {
  %size = arith.constant 16 : index
  scf.if %cond {
    %a = memref.alloc() : memref<8xf32>
    %b = memref.realloc %a(%size) : memref<8xf32> to memref<?xf32>
    memref.dealloc %b : memref<?xf32>
  }
  return
}

//--- async-ordering.mlir
// The allocated memref has no user other than gpu.dealloc. The completion
// token's gpu.wait is not included in the memref's use/alias analysis.
// Expected: retain the wait before deallocation, or otherwise preserve the
// allocation's completion dependency.
// Actual order: gpu.alloc async -> gpu.dealloc -> gpu.wait.
// The GPU async interface allows allocation to remain in flight until the
// token is waited on; moving deallocation ahead of that wait loses ordering.
func.func @async_alloc_with_dependency(%dep: !gpu.async.token) {
  %a, %token = gpu.alloc async [%dep] () : memref<8xf32>
  gpu.wait [%token]
  gpu.dealloc %a : memref<8xf32>
  return
}
```


https://github.com/llvm/llvm-project/pull/227975


More information about the Mlir-commits mailing list