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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Convert-openacc-to-scf triggers crashes in OperationLegalizer
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    Hi, I was trying `mlir-opt --convert-openacc-to-scf` on the following `temp.mlir`.

It crashed, the log is as follows:
```
Assertion failed: ((replacedRoot() || updatedRootInPlace()) && "expected pattern to replace the root operation"), function legalizePatternResult, file DialectConversion.cpp, line 2036.
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 --convert-openacc-to-scf temp.mlir
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  mlir-opt 0x00000001003b05bc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  mlir-opt                 0x00000001003af624 llvm::sys::RunSignalHandlers() + 112
2  mlir-opt 0x00000001003b0c54 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x00000001a56894c4 _sigtramp + 56
4  libsystem_pthread.dylib 0x00000001a5671ee0 pthread_kill + 288
5  libsystem_c.dylib 0x00000001a55ac340 abort + 168
6  libsystem_c.dylib 0x00000001a55ab754 err + 0
7  mlir-opt                 0x0000000101ce7198 mlir::LogicalResult llvm::function_ref<mlir::LogicalResult (mlir::Pattern const&)>::callback_fn<(anonymous namespace)::OperationLegalizer::legalizeWithPattern(mlir::Operation*, mlir::ConversionPatternRewriter&)::$_20>(long, mlir::Pattern const&) (.cold.2) + 0
8  mlir-opt                 0x00000001015fed88 mlir::LogicalResult llvm::function_ref<mlir::LogicalResult (mlir::Pattern const&)>::callback_fn<(anonymous namespace)::OperationLegalizer::legalizeWithPattern(mlir::Operation*, mlir::ConversionPatternRewriter&)::$_20>(long, mlir::Pattern const&) + 184
9  mlir-opt                 0x000000010184fa78 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&)>) + 1096
10 mlir-opt                 0x00000001015fd4b0 (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) + 1948
11 mlir-opt 0x00000001015f6b1c (anonymous namespace)::OperationConverter::convertOperations(llvm::ArrayRef<mlir::Operation*>, llvm::function_ref<void (mlir::Diagnostic&)>) + 928
12 mlir-opt 0x00000001015f8d18 mlir::applyPartialConversion(mlir::Operation*, mlir::ConversionTarget&, mlir::FrozenRewritePatternSet const&, llvm::DenseSet<mlir::Operation*, llvm::DenseMapInfo<mlir::Operation*, void>>*) + 80
13 mlir-opt                 0x000000010127f674 (anonymous namespace)::ConvertOpenACCToSCFPass::runOnOperation() + 820
14 mlir-opt 0x00000001015bb4dc mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) + 420
15 mlir-opt 0x00000001015bba0c mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) + 320
16 mlir-opt                 0x00000001015bd388 mlir::PassManager::run(mlir::Operation*) + 1148
17 mlir-opt 0x00000001015b6840 performActions(llvm::raw_ostream&, bool, bool, std::__1::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, llvm::function_ref<mlir::LogicalResult (mlir::PassManager&)>, bool, bool) + 504
18 mlir-opt                 0x00000001015b6410 mlir::LogicalResult llvm::function_ref<mlir::LogicalResult (std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<mlir::LogicalResult (mlir::PassManager&)>, mlir::DialectRegistry&, bool, bool, bool, bool, bool, bool, bool)::$_0>(long, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) + 704
19 mlir-opt                 0x000000010162102c mlir::splitAndProcessBuffer(std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<mlir::LogicalResult (std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>, llvm::raw_ostream&, bool, bool) + 656
20 mlir-opt 0x00000001015b4838 mlir::MlirOptMain(llvm::raw_ostream&, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<mlir::LogicalResult (mlir::PassManager&)>, mlir::DialectRegistry&, bool, bool, bool, bool, bool, bool, bool) + 216
21 mlir-opt                 0x00000001015b4d2c mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&, bool) + 1208
22 mlir-opt 0x00000001002530a0 main + 108
23 dyld 0x0000000104c9d088 start + 516

```

Building commit: db6961db
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWU1v2zwS_jXKhYhBUR-WDz44ToIGSBAjDnaxezEociRzS5Fakkrq_vqFJMofsd26b3MoFq9RWI05HM7M88xwSFFrRakApkFyEyS3V7Rxa22mc5BgnVCwrIRbX-Wab6ZfREDm6AG9U4uc2QhVoiDFlRTmWtcOXV8zrd7AuGtdg6KMXTt9bVkRpBhphdwaUKGl1O9-ooOqHrWzgxSPAnwb4Fn__eAQM9SugbfrtfOkLpGw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