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

Pranav Alavandi llvmlistbot at llvm.org
Thu Oct 1 01:03:09 PDT 2026


pranavalavandi wrote:

> A coding agent found that this pass now produces invalid IR for such a test case (dominance violation):
> 
> ```mlir
> func.func @foo(%d: !gpu.async.token) {
>   %a = gpu.alloc
>   %w = gpu.wait async [%d]
>   %done = gpu.dealloc async [%w] %a
>   gpu.wait %done
>   return
> }
> ```

Ah yeah that makes sense. I have a fix in my branch- the solution would be to only move the `dealloc` to be after the "lowest" operand to preserve dominance. However I realised that this code is an existing bug already:

```
func.func @foo(%d: !gpu.async.token) {
  %c0 = arith.constant 0 : index
  %a = gpu.alloc () : memref<8xf32>
  %x = memref.load %a[%c0] : memref<8xf32>
  %w = gpu.wait async [%d]
  %done = gpu.dealloc async [%w] %a : memref<8xf32>
  gpu.wait [%done]
  return
}
```

And so since we currently move the code to be after the `lastUser` we do not check any operands when hoisting it. 

I think the fix here is to unconditionally check the positions of any operands for the `dealloc` op, and to only move it to be after that. If that is lower in the program than `lastUser` then that is the case.  

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


More information about the Mlir-commits mailing list