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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR][Bufferization] ownership-based-buffer-deallocation breaks with masked xferOps
        </td>
    </tr>

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

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

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

<pre>
    Hi, I came across a bug in buffer-deallocation-pipeline on masked transferOps. It fails with `error: 'vector.mask' op expects only one operation to mask`. It seems `BufferDeallocation::deallocate(Block *block)` creates a constant in the masked block which is illegal. Here.
https://github.com/llvm/llvm-project/blob/2f8c786846972f85a1967502050675c724a2904d/mlir/lib/Dialect/Bufferization/Transforms/OwnershipBasedBufferDeallocation.cpp#L632

I did not figure out a way to make it nice and reusable other than adding InsertionGuards or forcing to create the constant in the parent region specifically for this case. Any ideas ?

Reproduction.
```
// RUN: mlir-opt %s -ownership-based-buffer-deallocation
module {
 func.func @matmul(%i: index, %mem: memref<?x?xf32, strided<[?, ?], offset: ?>>) -> vector<[4]x[4]xf32> {
    %cst = arith.constant 0.000000e+00 : f32
    %c0 = arith.constant 0 : index
    %6 = vector.create_mask %i, %i : vector<[4]x[4]xi1>
    %7 = vector.mask %6 { vector.transfer_read %mem[%c0, %c0], %cst {in_bounds = [true, true]} : memref<?x?xf32, strided<[?, ?], offset: ?>>, vector<[4]x[4]xf32> } : vector<[4]x[4]xi1> -> vector<[4]x[4]xf32>
    return %7 : vector<[4]x[4]xf32>
 }
}
```

Outputs:
```
error: 'vector.mask' op expects only one operation to mask
    %7 = vector.mask %6 { vector.transfer_read %mem[%c0, %c0], %cst {in_bounds = [true, true]} : memref<?x?xf32, strided<[?, ?], offset: ?>>, vector<[4]x[4]xf32> } : vector<[4]x[4]xi1> -> vector<[4]x[4]xf32>
         ^
test.mlir:6:10: note: see current operation:
%4 = "vector.mask"(%3) ({
  %6 = "arith.constant"() <{value = true}> : () -> i1
  %7 = "vector.transfer_read"(%arg1, %2, %2, %1) <{in_bounds = [true, true], operandSegmentSizes = array<i32: 1, 2, 1, 0>, permutation_map = affine_map<(d0, d1) -> (d0, d1)>}> : (memref<?x?xf32, strided<[?, ?], offset: ?>>, index, index, f32) -> vector<[4]x[4]xf32>
 "vector.yield"(%7) : (vector<[4]x[4]xf32>) -> ()
}) : (vector<[4]x[4]xi1>) -> vector<[4]x[4]xf32>
```

Kiss,
Hugo.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV1uT26gS_jX4pcsqhG72gx_scXwydXJOqpLd5ymEWjYZBCpAc8mv3wJJHnt2djOpzeO6bIEk-vJ93TRt7pw8asQNKXak2C_44E_GbvTwjfNFbZrnzUdJ2A3cguAdAhfWOAcc6uEIUkM9tC3aZYNcKSO4l0Yve9mjkhrBaOi4u8cGvOXatWg_9y6BWw8tl8rBo_QnICVFa40l2RYIqx5QeGOTIEdYBaYHfOpReAdGq2cwQW2PNloCb6IBUtKo1SF2LijcRa_2F06RbEuy7dlNJGy1U0bcA2HbOkwIW5OSgrDIPQaAwmjnufYBpT_hjCQuhseTFCeQDqRSeOQqgY9oMSF0T-j25H3vgkF2IOxwlP401IkwHWEHpR7mYdlb8w2FJ-xQK1MTdmDtSlSrcpWX64q1q4Kn67IqKKMFLatCVCznbE3zhrBDp6QNemSQ20uuRkUjbvl9hMwOv0Xaje0cYYfPjxqtO8l-xx02f6YoEX1PWPapzNiIY7zeQiMb0MZDK4-DRTCDBw6P_Hmk_x5BetBSIHDdgMXB8VohGH9CC_7ENfCmkfoIt9qhDZb-M3DbODAWWmNFeOXNxHyk-jX1PbeoPVg8hqC7HoVspeBKPQcF4E_SgeAOE9jqZ5ANcgckO1yi-IK9Nc0gItDpRUmn73gbowVffv9_yMRA8NL0HggrHCzNzN2yDuQt30j7UUtnmkEhkGo33kM7a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