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

    <tr>
        <th>Summary</th>
        <td>
            [mlir][affine] --affine-loop-coalescing crashed with assertion failure "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>
          anonymousMCS
      </td>
    </tr>
</table>

<pre>
    git version: 0359a78f77fbee15a3c3ca9e6a44301336cc0f6e

system: ```Ubuntu 20.04.6 LTS (Focal Fossa)```

reproduced with: ```mlir-opt --affine-loop-coalescing a.mlir```

a.mlir:
```
#map3 = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
#map4 = affine_map<(d0, d1, d2) -> (d0, d1)>
func.func private @func1(%arg0: index, %arg1: i1) {
  %c0 = arith.constant 0 : index
  %c5 = arith.constant 5 : index
  %c7 = arith.constant 7 : index
  %c11 = arith.constant 11 : index
  %c23 = arith.constant 23 : index
  %c24 = arith.constant 24 : index
  %c26 = arith.constant 26 : index
  %c31 = arith.constant 31 : index
  %c9_41 = arith.constant 9 : index
  %41 = index.shl %c9_41, %c23
  %alloc_45 = memref.alloc(%c24, %c5) : memref<31x?x?xi64>
  %alloc_47 = memref.alloc(%41, %c7) : memref<31x?x?xi64>
  %alloc_48 = memref.alloc(%c11) {alignment = 64 : i64} : memref<31x?xi64>
  %49 = bufferization.to_tensor %alloc_48 : memref<31x?xi64>
  %alloc_49 = memref.alloc(%41, %c0) {alignment = 64 : i64} : memref<31x?x?xi64>
  %50 = bufferization.to_tensor %alloc_49 : memref<31x?x?xi64>
  %51 = linalg.generic {indexing_maps = [#map3, #map3, #map3, #map4, #map3], iterator_types = ["parallel", "parallel", "parallel"]} ins(%alloc_45, %alloc_47, %alloc_47, %49 : memref<31x?x?xi64>, memref<31x?x?xi64>, memref<31x?x?xi64>, tensor<31x?xi64>) outs(%50 : tensor<31x?x?xi64>) {
 ^bb0(%in: i64, %in_79: i64, %in_80: i64, %in_81: i64, %out: i64):
 linalg.yield %in : i64
  } -> tensor<31x?x?xi64>
  %54 = bufferization.to_memref %51 : memref<31x?x?xi64>
  %alloc_59 = memref.alloc(%c31, %c26) {alignment = 64 : i64} : memref<31x?x?xi64>
 %102 = bufferization.to_tensor %alloc_59 : memref<31x?x?xi64>
  %c31_61 = arith.constant 31 : index
  %c1_62 = arith.constant 1 : index
  %dim = memref.dim %54, %c1_62 : memref<31x?x?xi64>
  %c0_65 = arith.constant 0 : index
  %c1_66 = arith.constant 1 : index
  %103 = scf.for %arg2 = %c0_65 to %c31_61 step %c1_66 iter_args(%arg3 = %51) -> (tensor<31x?x?xi64>) {
    %149 = scf.for %arg4 = %c0_65 to %dim step %c1_66 iter_args(%arg5 = %51) -> (tensor<31x?x?xi64>) {
      %150 = bufferization.to_memref %arg5 : memref<31x?x?xi64>
      scf.yield %102 : tensor<31x?x?xi64>
 }
    scf.yield %102 : tensor<31x?x?xi64>
  }
  return 
}
```

trace:
```
mlir-opt: /data/bin/llvm-project/mlir/include/mlir/IR/UseDefLists.h:188: mlir::IRObjectWithUseList<OperandType>::~IRObjectWithUseList() [with OperandType = mlir::OpOperand]: 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: /data/bin/llvm-project/build/bin/mlir-opt --affine-loop-coalescing reduced.mlir
 #0 0x00005619a3454a9f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/bin/llvm-project/build/bin/mlir-opt+0x15d2a9f)
 #1 0x00005619a3451b34 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f04231d7420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f0422cc200b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007f0422ca1859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x00007f0422ca1729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #6 0x00007f0422cb2fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #7 0x00005619a60e48e7 mlir::Block::~Block() (/data/bin/llvm-project/build/bin/mlir-opt+0x42628e7)
 #8 0x00005619a61cf3ba mlir::Region::~Region() (/data/bin/llvm-project/build/bin/mlir-opt+0x434d3ba)
 #9 0x00005619a61bb91c mlir::Operation::~Operation() (/data/bin/llvm-project/build/bin/mlir-opt+0x433991c)
#10 0x00005619a61bbadd mlir::Operation::erase() (/data/bin/llvm-project/build/bin/mlir-opt+0x4339add)
#11 0x00005619a4b136e1 mlir::coalesceLoops(llvm::MutableArrayRef<mlir::scf::ForOp>) (/data/bin/llvm-project/build/bin/mlir-opt+0x2c916e1)
#12 0x00005619a3579606 mlir::LogicalResult mlir::affine::coalescePerfectlyNestedLoops<mlir::scf::ForOp>(mlir::scf::ForOp) (/data/bin/llvm-project/build/bin/mlir-opt+0x16f7606)
#13 0x00005619a3579c22 void llvm::function_ref<void (mlir::Operation*)>::callback_fn<(anonymous namespace)::LoopCoalescingPass::runOnOperation()::'lambda'(mlir::Operation*)>(long, mlir::Operation*) LoopCoalescing.cpp:0:0
#14 0x00005619a3578de0 void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (.constprop.1) LoopCoalescing.cpp:0:0
#15 0x00005619a3579f44 (anonymous namespace)::LoopCoalescingPass::runOnOperation() LoopCoalescing.cpp:0:0
#16 0x00005619a5fb286e mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/bin/llvm-project/build/bin/mlir-opt+0x413086e)
#17 0x00005619a5fb2d4a mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/bin/llvm-project/build/bin/mlir-opt+0x4130d4a)
#18 0x00005619a5fb374c mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::'lambda'(mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::OpPMInfo&)::operator()(mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::OpPMInfo&) const Pass.cpp:0:0
#19 0x00005619a5fb1ea5 mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool) (/data/bin/llvm-project/build/bin/mlir-opt+0x412fea5)
#20 0x00005619a5fb260a mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/bin/llvm-project/build/bin/mlir-opt+0x413060a)
#21 0x00005619a5fb2d4a mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/bin/llvm-project/build/bin/mlir-opt+0x4130d4a)
#22 0x00005619a5fb3574 mlir::PassManager::run(mlir::Operation*) (/data/bin/llvm-project/build/bin/mlir-opt+0x4131574)
#23 0x00005619a5fa3e3b performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#24 0x00005619a5fa52f4 processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, llvm::ThreadPool*) MlirOptMain.cpp:0:0
#25 0x00005619a5fa54a4 mlir::LogicalResult llvm::function_ref<mlir::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&)::'lambda'(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#26 0x00005619a60a1eb4 mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<mlir::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, bool, bool) (/data/bin/llvm-project/build/bin/mlir-opt+0x421feb4)
#27 0x00005619a5f9e777 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/data/bin/llvm-project/build/bin/mlir-opt+0x411c777)
#28 0x00005619a5fa57cb mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/data/bin/llvm-project/build/bin/mlir-opt+0x41237cb)
#29 0x00005619a338cb8b main (/data/bin/llvm-project/build/bin/mlir-opt+0x150ab8b)
#30 0x00007f0422ca3083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#31 0x00005619a342533e _start (/data/bin/llvm-project/build/bin/mlir-opt+0x15a333e)
Aborted (core dumped)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWl9z4yi2_zTKCxUXAvTvIQ9up103VZ2bVNJT8-hCErK5jUEXUHe8D_vZtxCyLSnyxIlTszNbOzVJ23A4_M7_A4Qaw9eSsZsg-hJEt1e0sRulb6hUcrdVjblfPF_lqtzdrLkFP5k2XMkAzwHEUUaT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