<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59118>59118</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Buffer deallocation pass non-deterministic output
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir,
            mlir:bufferization
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          bondhugula
      </td>
    </tr>
</table>

<pre>
    Each run of the `buffer-deallocation` produces different IR (a different ordering of `bufferization.clone` operations). A simple test case is below.  A previous commit from Feb 2022 mentions this issue, but this never got fixed.

```
commit d955ca49379e73485304eb7f500db53e33109b0f
Author: Benjamin Kramer <benny.kra@googlemail.com>
Date:   Thu Feb 17 13:51:27 2022 +0100

    [BufferDeallocation] Don't assume successor operands are unique
    
    This would create a double free when a memref is passed twice to the
    same op. This wasn't a problem at the time the pass was written but is
    common since the introduction of scf.while.
    
    There's a latent non-determinism that's triggered by the test, but this
    change is messy enough as-is so I'll leave that for later.
    
    Differential Revision: https://reviews.llvm.org/D120044
```

```
module {
  memref.global "private" constant @__constant_xi32 : memref<i32> = dense<0>
  func.func @buffer_dealloc_issue(%arg0: memref<32x64xf32>, %arg1: memref<32x64x6xf32>, %arg2: memref<i32>, %arg3: memref<32x64xf32>) {
    %c0_i32 = arith.constant 0 : i32
    %0 = memref.get_global @__constant_xi32 : memref<i32>
    %1 = affine.load %arg2[] : memref<i32>
    %2 = arith.maxsi %1, %c0_i32 : i32
    %3:4 = scf.while (%arg4 = %0, %arg5 = %0, %arg6 = %arg0, %arg7 = %arg1) : (memref<i32>, memref<i32>, memref<32x64xf32>, memref<32x64x6xf32>) -> (memref<i32>, memref<i32>, memref<32x64xf32>, memref<32x64x6xf32>) {
      %4 = affine.load %arg5[] : memref<i32>
      %5 = arith.cmpi slt, %4, %2 : i32
      scf.condition(%5) %arg4, %arg5, %arg6, %arg7 : memref<i32>, memref<i32>, memref<32x64xf32>, memref<32x64x6xf32>
    } do {
    ^bb0(%arg4: memref<i32>, %arg5: memref<i32>, %arg6: memref<32x64xf32>, %arg7: memref<32x64x6xf32>):  // no predecessors
      %4 = memref.alloc() : memref<i32>
      %5 = memref.alloc() : memref<32x64xf32>
      %6 = memref.alloc() : memref<32x64x6xf32>
      %7 = memref.alloc() : memref<i32>
      scf.yield %7, %4, %5, %6 : memref<i32>, memref<i32>, memref<32x64xf32>, memref<32x64x6xf32>
    }
    return
  }
}
```

`$ mlir-opt -buffer-deallocation test_case.mlir`
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9Vk1v4zYQ_TXyZRBBor6sgw9JnACL3hZ7NyhpJHNLkS5JxUl_fYeUP7PebFq0BQxZGnIeZ-Y9ktPo7m31xNstmEmB7sFtEaIyaaa-R3PXIZdSt9wJrcgKO6O7qUULnfDjqBx8-QoRW_ILizYdGqEGD3eCEn8GkLiVWqGH0js0wWQjVsdwD1aMO4ng0DpouUUQFhqUeh8Dje4Mvgg9WWj1OAoHvdEjPGMDLGEMRlrXQ1H45CWsnTBij9BMbrYofEEDgyY_8YpdHCXrKLk_PMvk8AufB_yuLoqW53VW1Vhl-bLIkhybqi-SpGuKDLMsTeom6Wen-8lttYmye3hA9Z2PQsFvho-0ZpQ9NqjUW_y74VGeDFoPEkcuZEwLRdnT7L_mDr03wLftFNJKK0gzMhUpPVg1pxmxhyRNksvoyQWi4uEhFHl9SVexhjX9scoBp4KMCHZqiTurzVx81VngBmFS4g-q1xnu9PbN126vJ9lBa5BiBOJZTw3R1BtE2G9RkWnE0WDv-drRStiB24uWmNReTWc0SwWhleMDLLeH4LyqCHIE7oL8nKB5_sWj-XmwN8I5WsrzKewZ0XOlFSlHtbOHUC4o1BfAy8-2fbzfConx7fRIsBQDlQEkZUfaVVqR6B0aolDYkUC5CzOcEcNA0zto3uYoSaeXGruIasvVEOQ7UrXfAJWehi2RcEcmq-ELAUoJEvkLhgWgJ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