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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR]`-lift-cf-to-scf` triggers Assertion Failure `use_empty() && "Cannot destroy a value that still has uses!"'
        </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 -lift-cf-to-scf
```
test case:
```
#map2 = affine_map<(d0, d1, d2) -> (0, d1, d2)>
#map3 = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
module {
  func.func @main(%arg0: tensor<12x6xf32>, %arg1: tensor<12x6xf32>, %arg2: tensor<?x?xf32>, %arg3: tensor<4xi1>, %arg4: tensor<4xf32>, %arg5: tensor<4xf32>, %arg6: tensor<?x?x?xi1>, %arg7: tensor<?x?x?xi1>) -> () {
 %0 = llvm.mlir.constant(1 : index) : i64
    %1 = llvm.mlir.constant(0 : index) : i64
    %3 = llvm.mlir.constant(2 : index) : i64
    %4 = builtin.unrealized_conversion_cast %3 : i64 to index
    %5 = builtin.unrealized_conversion_cast %1 : i64 to index
    %6 = builtin.unrealized_conversion_cast %0 : i64 to index
    %dim = tensor.dim %arg6, %5 : tensor<?x?x?xi1>
    %11 = builtin.unrealized_conversion_cast %dim : index to i64
    %dim_0 = tensor.dim %arg7, %5 : tensor<?x?x?xi1>
    %12 = builtin.unrealized_conversion_cast %dim_0 : index to i64
    %13 = llvm.intr.umax(%11, %12)  : (i64, i64) -> i64
 %14 = builtin.unrealized_conversion_cast %13 : i64 to index
    %dim_1 = tensor.dim %arg6, %6 : tensor<?x?x?xi1>
    %dim_2 = tensor.dim %arg7, %6 : tensor<?x?x?xi1>
    %dim_3 = tensor.dim %arg6, %4 : tensor<?x?x?xi1>
    %23 = llvm.icmp "eq" %11, %0 : i64
 llvm.cond_br %23, ^bb1, ^bb2
  ^bb1:
    %24 = tensor.empty(%14, %dim_1, %dim_3) : tensor<?x?x?xi1>
    %25 = linalg.generic {indexing_maps = [#map2, #map3], iterator_types = ["parallel", "parallel", "parallel"]} ins(%arg6 : tensor<?x?x?xi1>) outs(%24 : tensor<?x?x?xi1>) {
    ^bb0(%in: i1, %out: i1):
      linalg.yield %in : i1
    } -> tensor<?x?x?xi1>
    cf.br ^bb3(%25 : tensor<?x?x?xi1>)
  ^bb2: 
 cf.br ^bb3(%arg6 : tensor<?x?x?xi1>)
  ^bb3(%26: tensor<?x?x?xi1>):
 return
  }
}
```
crash trace:
```
mlir-opt: /home/workdir/llvm-project-19/mlir/include/mlir/IR/UseDefLists.h:198: mlir::IRObjectWithUseList<mlir::BlockOperand>::~IRObjectWithUseList() [OperandType = mlir::BlockOperand]: Assertion `use_empty() && "Cannot destroy a value that still has uses!"' 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 -lift-cf-to-scf
 #0 0x000064e92129a3b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (./mlir-opt+0x10723b8)
 #1 0x000064e921297ede llvm::sys::RunSignalHandlers() (./mlir-opt+0x106fede)
 #2 0x000064e92129adc8 SignalHandler(int) Signals.cpp:0:0
 #3 0x000072cf702fc520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x000072cf703509fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x000072cf702fc476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x000072cf702e27f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x000072cf702e271b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x000072cf702f3e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x000064e9245f5f86 (./mlir-opt+0x43cdf86)
#10 0x000064e9246c9c66 llvm::iplist_impl<llvm::simple_ilist<mlir::Block>, llvm::ilist_traits<mlir::Block>>::erase(llvm::ilist_iterator<llvm::ilist_detail::node_options<mlir::Block, true, false, void, false, void>, false, false>) (./mlir-opt+0x44a1c66)
#11 0x000064e9246c875b mlir::Region::~Region() (./mlir-opt+0x44a075b)
#12 0x000064e924562d78 mlir::transformCFGToSCF(mlir::Region&, mlir::CFGToSCFInterface&, mlir::DominanceInfo&) (./mlir-opt+0x433ad78)
#13 0x000064e923c18a5f mlir::WalkResult llvm::function_ref<mlir::WalkResult (mlir::Operation*)>::callback_fn<(anonymous namespace)::LiftControlFlowToSCF::runOnOperation()::'lambda'(mlir::func::FuncOp)::operator()(mlir::func::FuncOp) const::'lambda'(mlir::Operation*)>(long, mlir::Operation*) ControlFlowToSCF.cpp:0:0
#14 0x000064e923c186be 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)::LiftControlFlowToSCF::runOnOperation()::'lambda'(mlir::func::FuncOp), mlir::func::FuncOp, mlir::WalkResult>(mlir::Operation*, (anonymous namespace)::LiftControlFlowToSCF::runOnOperation()::'lambda'(mlir::func::FuncOp)&&)::'lambda'(mlir::Operation*)>(long, mlir::Operation*) ControlFlowToSCF.cpp:0:0
#15 0x000064e9213b9b37 mlir::WalkResult mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<mlir::WalkResult (mlir::Operation*)>, mlir::WalkOrder) (./mlir-opt+0x1191b37)
#16 0x000064e923c1838a (anonymous namespace)::LiftControlFlowToSCF::runOnOperation() ControlFlowToSCF.cpp:0:0
#17 0x000064e9245166df mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (./mlir-opt+0x42ee6df)
#18 0x000064e924516ec2 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (./mlir-opt+0x42eeec2)
#19 0x000064e92451966e mlir::PassManager::run(mlir::Operation*) (./mlir-opt+0x42f166e)
#20 0x000064e924512192 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#21 0x000064e924511dfb 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
#22 0x000064e9245bbae5 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)
#23 0x000064e92450cff2 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (./mlir-opt+0x42e4ff2)
#24 0x000064e92450d2a3 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (./mlir-opt+0x42e52a3)
#25 0x000064e92450d4b2 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (./mlir-opt+0x42e54b2)
#26 0x000064e9212795d7 main (./mlir-opt+0x10515d7)
#27 0x000072cf702e3d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#28 0x000072cf702e3e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#29 0x000064e921279145 _start (./mlir-opt+0x1051145)
Aborted (core dumped)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWk1v2zrW_jXMhoghkfpcZOE69fsGaJEg7cVdGhRJ2ZxIpC5Jtcks5rcPSMmOqNhxXXTQOwM0iUQePuf78FAsMUZsJec3IP0A0tsr0tud0jfPwhC5ja4qxV5uvnJjoZKQqrYVFuAl3FnbGYCXAK0BWm-F3fXVgqoWoHXTfNv_ue60-genFqD1uBStszQpqgynCYtZWUY8pmWR46xOeU4x40WeVZzRgoPoFkRLY3lnoFVQ804r1lPumPopkEXjP__aNkJfq85Cy41duDd43YjaXtP62qprQ-ujyxw1pMScxAUIt6RDEOBbSOpaSL5pSQfwCqCCRQCtIIv9bwRQCa8B_ggBKuYTAH-cwuEL4d7MHPBaxfqGQ5B_GN4hrHtJF-4XBEnUEiEBKgBKid5GznOWS6M0wKsYPWfPNUYOCq3gQBKfJ0EBCcDrZ_czp8IBVfIs4mA6mU3Pl6dn5rOjQrifGaP8HOGrkd3zqx0BSiPvJRfIPp4WVEljibQAFTF0uEIy_uyXuZcs2bsAutXx6dXR-dX49Gp0fnXiV1e9aKyQi15qThrxT842VMlvXBuh5IYSY_ecPIJLswF1CpVeABW_D5VdABW9D8VE68EGzy786xgYg-tTeMbxgaviCyQbWI8O8NLNjM9Eu4mOS5f_lHToMuk20fvyxZPYEtLq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