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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR]`-ownership-based-buffer-deallocation` triggers Assertion Failure `other && other->block == block && "Expected other operation to have the same parent block."'
        </td>
    </tr>

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

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

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

<pre>
    Test on commit: https://github.com/llvm/llvm-project/commit/6548b6354d1d990e1c98736f5e7c3de876bedc8e
steps to reproduce:
```
mlir-opt test.mlir --ownership-based-buffer-deallocation
```
test case:
```
module {
  func.func @main(%arg0: tensor<?x?x?xi1>, %arg1: tensor<?x?x?xi1>) -> () {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %c2 = arith.constant 2 : index
    %8 = bufferization.to_memref %arg0 : memref<?x?x?xi1>
    %dim = tensor.dim %arg0, %c0 : tensor<?x?x?xi1>
    %dim_0 = tensor.dim %arg1, %c0 : tensor<?x?x?xi1>
    %9 = arith.maxui %dim, %dim_0 : index
    %dim_5 = tensor.dim %arg0, %c0 : tensor<?x?x?xi1>
    %12 = arith.cmpi eq, %dim_5, %c1 : index
    cf.br ^bb1
  ^bb1:  // pred: ^bb0
    %dim_6 = tensor.dim %arg0, %c1 : tensor<?x?x?xi1>
    %dim_7 = tensor.dim %arg0, %c2 : tensor<?x?x?xi1>
    %alloc = memref.alloc(%9, %dim_6, %dim_7) {alignment = 64 : i64} : memref<?x?x?xi1>
    %dim_8 = memref.dim %8, %c1 : memref<?x?x?xi1>
    %dim_9 = memref.dim %8, %c2 : memref<?x?x?xi1>
 affine.for %arg7 = 0 to %9 {
      affine.for %arg8 = 0 to %dim_8 {
 affine.for %arg9 = 0 to %dim_9 {
          %61 = affine.load %8[%c0, %arg8, %arg9] : memref<?x?x?xi1>
          affine.store %61, %alloc[%arg7, %arg8, %arg9] : memref<?x?x?xi1>
        }
      }
    }
    %13 = bufferization.to_tensor %alloc : memref<?x?x?xi1>
    cf.br ^bb3(%13 : tensor<?x?x?xi1>)
  ^bb3(%14: tensor<?x?x?xi1>):
    return
 }
}
```
crash trace:
```
mlir-opt: /home/workdir/llvm-project-19/mlir/lib/IR/Operation.cpp:389: bool mlir::Operation::isBeforeInBlock(Operation *): Assertion `other && other->block == block && "Expected other operation to have the same parent block."' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ./mlir-opt /home/workdir/test.mlir --ownership-based-buffer-deallocation
 #0 0x00005656b1e033b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (./mlir-opt+0x10723b8)
 #1 0x00005656b1e00ede llvm::sys::RunSignalHandlers() (./mlir-opt+0x106fede)
 #2 0x00005656b1e03dc8 SignalHandler(int) Signals.cpp:0:0
 #3 0x0000710884489520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007108844dd9fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x0000710884489476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x000071088446f7f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x000071088446f71b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x0000710884480e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00005656b521ecb3 mlir::Operation::isBeforeInBlock(mlir::Operation*) (./mlir-opt+0x448dcb3)
#10 0x00005656b22f5196 void std::__final_insertion_sort<mlir::Value*, __gnu_cxx::__ops::_Iter_comp_iter<mlir::bufferization::ValueComparator>>(mlir::Value*, mlir::Value*, __gnu_cxx::__ops::_Iter_comp_iter<mlir::bufferization::ValueComparator>) (./mlir-opt+0x1564196)
#11 0x00005656b22f1f30 mlir::bufferization::DeallocationState::getLiveMemrefsIn(mlir::Block*, llvm::SmallVectorImpl<mlir::Value>&) (./mlir-opt+0x1560f30)
#12 0x00005656b237bb0d mlir::WalkResult llvm::function_ref<mlir::WalkResult (mlir::Block*)>::callback_fn<(anonymous namespace)::BufferDeallocation::deallocate(mlir::FunctionOpInterface)::$_0>(long, mlir::Block*) OwnershipBasedBufferDeallocation.cpp:0:0
#13 0x00005656b237a4ed mlir::WalkResult mlir::detail::walk<mlir::ForwardDominanceIterator<false>>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Block*)>, mlir::WalkOrder) (./mlir-opt+0x15e94ed)
#14 0x00005656b237a4d2 mlir::WalkResult mlir::detail::walk<mlir::ForwardDominanceIterator<false>>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Block*)>, mlir::WalkOrder) (./mlir-opt+0x15e94d2)
#15 0x00005656b237a4d2 mlir::WalkResult mlir::detail::walk<mlir::ForwardDominanceIterator<false>>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Block*)>, mlir::WalkOrder) (./mlir-opt+0x15e94d2)
#16 0x00005656b237a4d2 mlir::WalkResult mlir::detail::walk<mlir::ForwardDominanceIterator<false>>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Block*)>, mlir::WalkOrder) (./mlir-opt+0x15e94d2)
#17 0x00005656b2379521 mlir::bufferization::deallocateBuffersOwnershipBased(mlir::FunctionOpInterface, mlir::bufferization::DeallocationOptions) (./mlir-opt+0x15e8521)
#18 0x00005656b2381437 mlir::WalkResult llvm::function_ref<mlir::WalkResult (mlir::Operation*)>::callback_fn<std::enable_if<!llvm::is_one_of<mlir::func::FuncOp, mlir::Operation*, mlir::Region*, mlir::Block*>::value && std::is_same<mlir::WalkResult, mlir::WalkResult>::value, mlir::WalkResult>::type mlir::detail::walk<(mlir::WalkOrder)1, mlir::ForwardIterator, (anonymous namespace)::OwnershipBasedBufferDeallocationPass::runOnOperation()::'lambda'(mlir::func::FuncOp), mlir::func::FuncOp, mlir::WalkResult>(mlir::Operation*, (anonymous namespace)::OwnershipBasedBufferDeallocationPass::runOnOperation()::'lambda'(mlir::func::FuncOp)&&)::'lambda'(mlir::Operation*)>(long, mlir::Operation*) OwnershipBasedBufferDeallocation.cpp:0:0
#19 0x00005656b1f22b37 mlir::WalkResult mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Operation*)>, mlir::WalkOrder) (./mlir-opt+0x1191b37)
#20 0x00005656b23811fe (anonymous namespace)::OwnershipBasedBufferDeallocationPass::runOnOperation() OwnershipBasedBufferDeallocation.cpp:0:0
#21 0x00005656b507f6df mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (./mlir-opt+0x42ee6df)
#22 0x00005656b507fec2 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (./mlir-opt+0x42eeec2)
#23 0x00005656b508266e mlir::PassManager::run(mlir::Operation*) (./mlir-opt+0x42f166e)
#24 0x00005656b507b192 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#25 0x00005656b507adfb llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::$_0>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#26 0x00005656b5124ae5 mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) (./mlir-opt+0x4393ae5)
#27 0x00005656b5075ff2 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (./mlir-opt+0x42e4ff2)
#28 0x00005656b50762a3 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (./mlir-opt+0x42e52a3)
#29 0x00005656b50764b2 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (./mlir-opt+0x42e54b2)
#30 0x00005656b1de25d7 main (./mlir-opt+0x10515d7)
#31 0x0000710884470d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#32 0x0000710884470e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#33 0x00005656b1de2145 _start (./mlir-opt+0x1051145)
Aborted (core dumped)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWltv47iS_jXKCxFDou4PeXAnbWyADhJ0D2YfBUos2dxIpIakupP99QtSsi3Kt7jRszPnnAFykahiXb4qVpVIEaXYmgPcefEnL364Ib3eCHn3xhTha_-mFPT97jdQGgmOKtG2THvhEm207pQXLj288vBqzfSmLxeVaD28aprv23-3nRT_A5X28GqcildJHGVlEsYRDWie-xBUeZaGSR1DWoUUsjQpgVYZeP6D5y-Vhk4hLZCETgraV2CE2kde4o8_9rZtmLwVnUYalF6YO3R7K35wkGrDutuSKKC3ZV_XIG8pkKYRFdFM8KPMDA9UEXVamqB9A8hLPw33CNU9rxbmD_IivyWMezjzcEzk2jeAaeBKSC-898LV2_aXBV742cP3aCAMLhHm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