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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR]`convertFuncOpTypes` leads to assertion failure `isa<To>(Val) && "cast<Ty>() argument of incompatible type!" 
        </td>
    </tr>

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

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

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

<pre>
    Test commit: https://github.com/llvm/llvm-project/commit/00701d32b7a495a69a420e0d9958ab15091da028

Steps to reproduce:

```
mlir-opt test.mlir -test-legalize-type-conversion
```
Test case:

```
 llvm.func @main() {
    llvm.return 
  }
```
Crash trace:
```
mlir-opt: /home/ty/llvm-project/llvm/include/llvm/Support/Casting.h:567: decltype(auto) llvm::cast(const From&) [with To = mlir::FunctionType; From = mlir::Type]: 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: /home/ty/llvm-project/build/bin/mlir-opt test.mlir -test-legalize-type-conversion
 #0 0x000055efdf3145da llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/ty/llvm-project/build/bin/mlir-opt+0x22b5da)
 #1 0x000055efdf31186c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007fdda409b980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #3 0x00007fdda2d4ce87 raise /build/glibc-CVJwZb/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51:0
 #4 0x00007fdda2d4e7f1 abort /build/glibc-CVJwZb/glibc-2.27/stdlib/abort.c:81:0
 #5 0x00007fdda2d3e3fa __assert_fail_base /build/glibc-CVJwZb/glibc-2.27/assert/assert.c:89:0
 #6 0x00007fdda2d3e472 (/lib/x86_64-linux-gnu/libc.so.6+0x30472)
 #7 0x000055efe0cb60c3 convertFuncOpTypes(mlir::FunctionOpInterface, mlir::TypeConverter&, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
 #8 0x000055efe0ca4ff8 mlir::ConversionPattern::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&) const (/home/ty/llvm-project/build/bin/mlir-opt+0x1bbbff8)
 #9 0x000055efe0ffe2d9 mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<mlir::LogicalResult (mlir::Pattern const&)>) (/home/ty/llvm-project/build/bin/mlir-opt+0x1f152d9)
#10 0x000055efe0caf9c7 (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#11 0x000055efe0cb0628 (anonymous namespace)::OperationConverter::convertOperations(llvm::ArrayRef<mlir::Operation*>, llvm::function_ref<void (mlir::Diagnostic&)>) DialectConversion.cpp:0:0
#12 0x000055efe0cb40e0 mlir::applyPartialConversion(mlir::Operation*, mlir::ConversionTarget&, mlir::FrozenRewritePatternSet const&, llvm::DenseSet<mlir::Operation*, llvm::DenseMapInfo<mlir::Operation*, void>>*) (/home/ty/llvm-project/build/bin/mlir-opt+0x1bcb0e0)
#13 0x000055efe0a8e9b2 (anonymous namespace)::TestTypeConversionDriver::runOnOperation() TestPatterns.cpp:0:0
#14 0x000055efe0c32cde mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/home/ty/llvm-project/build/bin/mlir-opt+0x1b49cde)
#15 0x000055efe0c33b33 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (.constprop.510) Pass.cpp:0:0
#16 0x000055efe0c341bd mlir::PassManager::run(mlir::Operation*) (/home/ty/llvm-project/build/bin/mlir-opt+0x1b4b1bd)
#17 0x000055efe0c28133 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#18 0x000055efe0c28801 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
#19 0x000055efe0c289f1 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
#20 0x000055efe0cfa900 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) (/home/ty/llvm-project/build/bin/mlir-opt+0x1c11900)
#21 0x000055efe0c23b13 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/home/ty/llvm-project/build/bin/mlir-opt+0x1b3ab13)
#22 0x000055efe0c2633e mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&, bool) (/home/ty/llvm-project/build/bin/mlir-opt+0x1b3d33e)
#23 0x000055efdf2e0540 main (/home/ty/llvm-project/build/bin/mlir-opt+0x1f7540)
#24 0x00007fdda2d2fc87 __libc_start_main /build/glibc-CVJwZb/glibc-2.27/csu/../csu/libc-start.c:344:0
#25 0x000055efdf2f33aa _start (/home/ty/llvm-project/build/bin/mlir-opt+0x20a3aa)

```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWV9v2zgS_zTMCxGDIiVZesiD49S4HhokaIJ92BdjJFI2rzSpI6k23k9_oCT_key0bpPb21ugjSWRnJnf_GaG_8A5udJC3KDkFiV3V9D4tbE3twLU76Jeb-1VYfj25lk4j0uz2UiP2Ayvva8dYjNEF4guVtKvm2JSmg2iC6W-7n6ua2v-JUqP6KIfSheETEnEGS2mEOcJpDnElAjC8zzJoIgSkkccCM0QuUNk1v198qJ22BtsRW0Nb0oRVB91QCnp_7WvGyXttak99sL5SXjD1-HxWokVKPmHuPbbWlyXRn8V1kmjz0rpIIP7gTIckE6qRpcYxWQDUiOaIZpjNL3te2DcdbLCN1bj3Vc0vTsrcW7BrbG3cIzzPMLABaKLtdkIRBd-e-r4ng6pS9Vwcfjw1NS1saHHHJyXejVZIzZL0mkQyUWpgosQzaDxJqBph7EZYrMSnEc0K412Hi9sID1t8Sa336Rf42eDEbvDwcRuwKLRpZdGPweJ7LYdM-rSNiV3QffMOWFDd4xSIh0gNn82iH1ANPsNVKso6EsxorQ1hc2ft117aAS7ajZCe2wqLHVpNjV4WSiBOzwRohTRKa5AKsEnnTcfP32YPX3Arik20mPARbMKoWasD0H307EunWuEQ3SBQXPcex77tcBly2wB5ZeW3ckuvKH8gnmzqfd0kwki-aM1KwubPST3Y7aLRioefkMULn4xDzCijGDyQgghSSIqXrEoTjgcxYDbuu7h0UrtWwDPbbzS7NDJwrelcd4K6EJkjqX2HYHZr8BA9Ja8UFokHBDND7ZG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