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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR]Crashed on `-sparse-compiler` pass with Assertion Failure `dim < numLoops'
        </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 on llvm release 16:

Steps to reproduce:

```
mlir-opt test.mlir "-sparse-compiler=enable-buffer-initialization=true "
```
Test case:
```
#map = affine_map<(d0) -> (0, d0)>
#map1 = affine_map<(d0) -> (d0)>
module {
  func.func @main(%arg0: tensor<1x5xf32>, %arg1: tensor<5xf32>) -> tensor<5xf32> {
    %0 = linalg.generic {indexing_maps = [#map, #map1], iterator_types = ["parallel"]} ins(%arg0 : tensor<1x5xf32>) outs(%arg1 : tensor<5xf32>) {
    ^bb0(%in: f32, %out: f32):
      linalg.yield %in : f32
    } -> tensor<5xf32>
    return %0 : tensor<5xf32>
  }
}
```
crash trace:
```
mlir-opt: /home/ty/fuzzer/llvm-project-16/mlir/include/mlir/Dialect/SparseTensor/Utils/Merger.h:322: void mlir::sparse_tensor::Merger::setDimAndDimLevelType(unsigned int, unsigned int, unsigned int, mlir::sparse_tensor::DimLevelType): Assertion `dim < numLoops' 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/fuzzer/llvm-project-16/mlir/build/bin/mlir-opt test.mlir "-sparse-compiler=enable-buffer-initialization=true "
 #0 0x0000000000481cad llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/ty/fuzzer/llvm-project-16/llvm/lib/Support/Unix/Signals.inc:567:11
 #1 0x000000000048213b PrintStackTraceSignalHandler(void*) /home/ty/fuzzer/llvm-project-16/llvm/lib/Support/Unix/Signals.inc:641:1
 #2 0x00000000004804d6 llvm::sys::RunSignalHandlers() /home/ty/fuzzer/llvm-project-16/llvm/lib/Support/Signals.cpp:104:5
 #3 0x0000000000482865 SignalHandler(int) /home/ty/fuzzer/llvm-project-16/llvm/lib/Support/Unix/Signals.inc:412:1
 #4 0x00007fd01e598980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #5 0x00007fd01d488e87 raise /build/glibc-CVJwZb/glibc-2.27/signal/../sysdeps/unix/sysv/linux/raise.c:51:0
 #6 0x00007fd01d48a7f1 abort /build/glibc-CVJwZb/glibc-2.27/stdlib/abort.c:81:0
 #7 0x00007fd01d47a3fa __assert_fail_base /build/glibc-CVJwZb/glibc-2.27/assert/assert.c:89:0
 #8 0x00007fd01d47a472 (/lib/x86_64-linux-gnu/libc.so.6+0x30472)
 #9 0x00000000020e56a3 mlir::sparse_tensor::Merger::setDimAndDimLevelType(unsigned int, unsigned int, unsigned int, mlir::sparse_tensor::DimLevelType) /home/ty/fuzzer/llvm-project-16/mlir/include/mlir/Dialect/SparseTensor/Utils/Merger.h:323:28
#10 0x00000000020de1c0 findAffine(mlir::sparse_tensor::Merger&, unsigned int, unsigned int, mlir::AffineExpr, mlir::sparse_tensor::DimLevelType, unsigned int&, bool) /home/ty/fuzzer/llvm-project-16/mlir/lib/Dialect/SparseTensor/Transforms/Sparsification.cpp:188:5
#11 0x00000000020dc645 findSparseAnnotations(mlir::sparse_tensor::CodegenEnv&) /home/ty/fuzzer/llvm-project-16/mlir/lib/Dialect/SparseTensor/Transforms/Sparsification.cpp:270:11
#12 0x00000000020dc098 (anonymous namespace)::GenericOpSparsifier::matchAndRewrite(mlir::linalg::GenericOp, mlir::PatternRewriter&) const /home/ty/fuzzer/llvm-project-16/mlir/lib/Dialect/SparseTensor/Transforms/Sparsification.cpp:1528:9
#13 0x000000000108fa4b mlir::detail::OpOrInterfaceRewritePatternBase<mlir::linalg::GenericOp>::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&) const /home/ty/fuzzer/llvm-project-16/mlir/include/mlir/IR/PatternMatch.h:330:12
#14 0x0000000004a54e90 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/fuzzer/llvm-project-16/mlir/lib/Rewrite/PatternApplicator.cpp:200:25
#15 0x0000000003b4fe33 (anonymous namespace)::GreedyPatternRewriteDriver::simplify(llvm::MutableArrayRef<mlir::Region>) /home/ty/fuzzer/llvm-project-16/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp:292:19
#16 0x0000000003b4f2ce mlir::applyPatternsAndFoldGreedily(llvm::MutableArrayRef<mlir::Region>, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig) /home/ty/fuzzer/llvm-project-16/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp:452:8
#17 0x000000000063d865 mlir::applyPatternsAndFoldGreedily(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig) /home/ty/fuzzer/llvm-project-16/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h:86:10
#18 0x000000000209d25a (anonymous namespace)::SparsificationPass::runOnOperation() /home/ty/fuzzer/llvm-project-16/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorPasses.cpp:76:11
#19 0x0000000003a69757 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) /home/ty/fuzzer/llvm-project-16/mlir/lib/Pass/Pass.cpp:471:21
#20 0x0000000003a69d6d mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) /home/ty/fuzzer/llvm-project-16/mlir/lib/Pass/Pass.cpp:534:16
#21 0x0000000003a6df31 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int)::$_6::operator()(mlir::OpPassManager&, mlir::Operation*) const /home/ty/fuzzer/llvm-project-16/mlir/lib/Pass/Pass.cpp:456:12
#22 0x0000000003a6dcb2 mlir::LogicalResult llvm::function_ref<mlir::LogicalResult (mlir::OpPassManager&, mlir::Operation*)>::callback_fn<mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int)::$_6>(long, mlir::OpPassManager&, mlir::Operation*) /home/ty/fuzzer/llvm-project-16/llvm/include/llvm/ADT/STLFunctionalExtras.h:45:12
#23 0x0000000000ae8571 llvm::function_ref<mlir::LogicalResult (mlir::OpPassManager&, mlir::Operation*)>::operator()(mlir::OpPassManager&, mlir::Operation*) const /home/ty/fuzzer/llvm-project-16/llvm/include/llvm/ADT/STLFunctionalExtras.h:68:12
#24 0x0000000000ae5e75 mlir::Pass::runPipeline(mlir::OpPassManager&, mlir::Operation*) /home/ty/fuzzer/llvm-project-16/mlir/include/mlir/Pass/Pass.h:196:12
#25 0x0000000003d992c1 mlir::sparse_tensor::SparsificationAndBufferizationPass::runOnOperation() /home/ty/fuzzer/llvm-project-16/mlir/lib/Dialect/SparseTensor/Transforms/SparsificationAndBufferizationPass.cpp:154:18
#26 0x0000000003a69757 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) /home/ty/fuzzer/llvm-project-16/mlir/lib/Pass/Pass.cpp:471:21
#27 0x0000000003a69d6d mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) /home/ty/fuzzer/llvm-project-16/mlir/lib/Pass/Pass.cpp:534:16
#28 0x0000000003a6b6ac mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) /home/ty/fuzzer/llvm-project-16/mlir/lib/Pass/Pass.cpp:839:10
#29 0x0000000003a6b5d1 mlir::PassManager::run(mlir::Operation*) /home/ty/fuzzer/llvm-project-16/mlir/lib/Pass/Pass.cpp:819:60
#30 0x0000000003a61f32 performActions(llvm::raw_ostream&, bool, bool, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, llvm::function_ref<mlir::LogicalResult (mlir::PassManager&)>, bool, bool) /home/ty/fuzzer/llvm-project-16/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:91:17
#31 0x0000000003a61b8f processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, bool, bool, bool, bool, bool, bool, llvm::function_ref<mlir::LogicalResult (mlir::PassManager&)>, mlir::DialectRegistry&, llvm::ThreadPool*) /home/ty/fuzzer/llvm-project-16/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:139:12
#32 0x0000000003a61948 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<mlir::LogicalResult (mlir::PassManager&)>, mlir::DialectRegistry&, bool, bool, bool, bool, bool, bool, bool)::$_0::operator()(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) const /home/ty/fuzzer/llvm-project-16/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:181:12
#33 0x0000000003a6183d 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>>, llvm::function_ref<mlir::LogicalResult (mlir::PassManager&)>, mlir::DialectRegistry&, bool, bool, bool, bool, bool, bool, bool)::$_0>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) /home/ty/fuzzer/llvm-project-16/llvm/include/llvm/ADT/STLFunctionalExtras.h:45:12
#34 0x0000000003b93a99 llvm::function_ref<mlir::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>::operator()(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) const /home/ty/fuzzer/llvm-project-16/llvm/include/llvm/ADT/STLFunctionalExtras.h:68:12
#35 0x0000000003b93075 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/fuzzer/llvm-project-16/mlir/lib/Support/ToolUtilities.cpp:28:12
#36 0x0000000003a60873 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<mlir::LogicalResult (mlir::PassManager&)>, mlir::DialectRegistry&, bool, bool, bool, bool, bool, bool, bool) /home/ty/fuzzer/llvm-project-16/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:186:10
#37 0x0000000003a60a0f mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::PassPipelineCLParser const&, mlir::DialectRegistry&, bool, bool, bool, bool, bool, bool, bool, bool) /home/ty/fuzzer/llvm-project-16/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:209:10
#38 0x0000000003a61651 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&, bool) /home/ty/fuzzer/llvm-project-16/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:306:14
#39 0x0000000000411609 main /home/ty/fuzzer/llvm-project-16/mlir/tools/mlir-opt/mlir-opt.cpp:271:7
#40 0x00007fd01d46bc87 __libc_start_main /build/glibc-CVJwZb/glibc-2.27/csu/../csu/libc-start.c:344:0
#41 0x00000000004112ea _start (/home/ty/fuzzer/llvm-project-16/mlir/build/bin/mlir-opt+0x4112ea)
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsW0tv27ry_zTMhoghkXousnDt-P_vRYIEac5dnI1BiZTNW4nSJak27qe_ICVZj9SNkzhNcNCiiC2LnMdvhsOZEUWU4hvB2AXwPwF_eUZqvS3lxSdG8r9Ztd3Js6Sku4t7pjQsBczzbwWULGdEMegGAM-BswRO-_eLZpWCuoSSVbKkdcomA0DgtP_tZZFzeV5WGmqm9MxcQYDQuaqIVOw8LYuK50wCvGSCJDk7T-osY_KcC645yfkPonkpAF5qWTMz86dMrOgpUQNZxgMAwgWpIMBLSLKMC7YuSAXwAqCIOgDF8BzgSwhQ5AC0gPYngC-Hc90jJo_nFSWtcwZB-Km5hjCrRTozfyDwnIJwAVAEkE_kxgF4DjUTqpQAL9wH_yHDyFBCC9iMcEcj-vst90d3hnyhoeFYBXIuSL6ZbZhgkqdmEBeUPXCxMUopO8a4idW54W61B_7SXHHNJNGlXOtdxQajUUUkyXOWGwv5SxAuIReqVw8e1C-GZa37kS48qOdYIf8ySZxmGhdmjhnYoFXWev9DvPcIaP-1-u84yym0c2E3tqcdLg-A2o-RTNdSdLj-TOJ2KAiXrRvtv4xdM5VEbaGWJD3ovd0aMowAWm3LggG00juAVln94weTAK3Mqj2vZPkflupzNwBoZWYBtOIizWvK-h-WnOQs1QCtvthFeN-IjlZ_aZ4rgFbXTG6YnG0BnmOEDNNvJafQTsdzgOfN4l13OpufmjntbaaXvJgLuuTFFfvG8vtdxQCKamHjEIVcaGOqp65_yXBM25gZzpVi0oQLCAKH8gICvICiLq7KslIAhTAjPGd01oB6e3U5_3IJVZ0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