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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR]`-remove-dead-values` triggers Assertion Failure `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"'
        </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/47928ab16b675c17826ada16f23aa0569e93a474
steps to reproduce:
```
mlir-opt test.mlir -remove-dead-values
```
test case:
```
module{
  func.func @fuse_repeated(%fill: tensor<2xf32>, %output: tensor<2xf32>) -> tensor<2xf32> {
    %c0 = arith.constant 0.0 : f32
    %0 = linalg.fill ins(%c0 : f32) outs(%fill : tensor<2xf32>) -> tensor<2xf32>
    %1 = scf.forall (%i) in (2) shared_outs(%arg1 = %output) -> (tensor<2xf32>) {
      %2 = tensor.extract_slice %0[%i][1][1] : tensor<2xf32> to tensor<1xf32>
      %3 = tensor.extract_slice %arg1[%i][1][1] : tensor<2xf32> to tensor<1xf32>
      %4 = linalg.elemwise_unary ins(%2 : tensor<1xf32>) outs(%3 : tensor<1xf32>) -> tensor<1xf32>
      scf.forall.in_parallel {
 tensor.parallel_insert_slice %4 into %arg1[%i][1][1] : tensor<1xf32> into tensor<2xf32>
      }
    }
    return %1 : tensor<2xf32>
 }
}
```
crash trace:
```
mlir-opt: /home/workdir/llvm-project-19/llvm/include/llvm/Support/Casting.h:578: decltype(auto) llvm::cast(From *) [To = mlir::RegionBranchTerminatorOpInterface, From = mlir::Operation]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' 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 -remove-dead-values
 #0 0x00005db04a3b89b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (./mlir-opt+0x107f9b8)
 #1 0x00005db04a3b64de llvm::sys::RunSignalHandlers() (./mlir-opt+0x107d4de)
 #2 0x00005db04a3b93c8 SignalHandler(int) Signals.cpp:0:0
 #3 0x00007cfbcc34f520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007cfbcc3a39fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007cfbcc34f476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007cfbcc3357f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007cfbcc33571b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007cfbcc346e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00005db04d6ab51d (anonymous namespace)::cleanRegionBranchOp(mlir::RegionBranchOpInterface, mlir::dataflow::RunLivenessAnalysis&)::$_3::operator()(mlir::RegionSuccessor const&, mlir::Operation*) const RemoveDeadValues.cpp:0:0
#10 0x00005db04d6a7c9a (anonymous namespace)::cleanRegionBranchOp(mlir::RegionBranchOpInterface, mlir::dataflow::RunLivenessAnalysis&) RemoveDeadValues.cpp:0:0
#11 0x00005db04a4caa2e void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (./mlir-opt+0x1191a2e)
#12 0x00005db04a4caa2e void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (./mlir-opt+0x1191a2e)
#13 0x00005db04d6a5d35 (anonymous namespace)::RemoveDeadValues::runOnOperation() RemoveDeadValues.cpp:0:0
#14 0x00005db04d67208f mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (./mlir-opt+0x433908f)
#15 0x00005db04d672872 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (./mlir-opt+0x4339872)
#16 0x00005db04d67501e mlir::PassManager::run(mlir::Operation*) (./mlir-opt+0x433c01e)
#17 0x00005db04d66db3b performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#18 0x00005db04d66d79b 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
#19 0x00005db04d717f25 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+0x43def25)
#20 0x00005db04d668682 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (./mlir-opt+0x432f682)
#21 0x00005db04d668933 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (./mlir-opt+0x432f933)
#22 0x00005db04d668b42 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (./mlir-opt+0x432fb42)
#23 0x00005db04a397bd7 main (./mlir-opt+0x105ebd7)
#24 0x00007cfbcc336d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#25 0x00007cfbcc336e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#26 0x00005db04a397745 _start (./mlir-opt+0x105e745)
Aborted (core dumped)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkWVtv6yoW_jX0BTWywdeHPqTpjmZLu2rVVmceIwzYYUrAA7iXfz8CO7WdJt07W3PmKp2zYxvWWt_61s2mxFrRKM6vQHoN0psL0rmtNldvwhLVRBeVZu9XT9w6qBWkercTDuAl3DrXWoCXAK0BWjfCbbtqQfUOoLWUL_ufy9bov3HqAFoPomid5CUqSBVnVZanNM4LlBFG4qxGmJAozUpeYpLkCYhuQLS0jrcWOg0Nb41mHeXeaFgCWTT8F253UphL3TrouHULfwcvDd_pF37JOGGXL0R23B4V9RKQEntat2ad5CC_7m8hrDtFF_4fCJKo7izfGN5y4jgDqAAorYWUnibHldUG4BV6qzEC-BtAKwhQqjvXdu7EjhJeAvzt8wqcAIBeC40gwDeQGOG2C6qVdUQ5GC384yX0QtPd_WYpFJHNwgOEQtkeLh0lUAl15-zoBjwT5cxmHGxaWi9qbYhXFvQKLy6UvwsW7ZYYzjajYWKaXnTkam8QoOIomhk5wTgKGvrNC_7mDKFuY6WgPNDh891DSW9Aeh2PP8cd9jn48TD-5GswiL806H3659tMplHlku9eheWbThHzPsYXzfXHE9pGzvHpTfNIH0Myhngh1KYl_orLSVAGVvYrG6EsNxN2EiiU0-fxtAfSi36RiRCC_GaSmNMbw11n1D5Zj6V6v_VDaLyYNwlqiN1CH_Sf9ihvB6D1Vu84QOtXbZ6ZMAc98zIux2YqFJUd4-ODx65ttfH9dEWsE6pZbAFepnnhVTNOpXtvOUAF6Zz2EQxieAnwkhLrACrWRu8gQMtQOun1kw555AH22x54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