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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Crash when using --tosa-to-tensor
        </td>
    </tr>

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

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

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

<pre>
    I have the following MLIR program:
test.mlir:
```
module {
 func.func @func1() {

    %c-138_i16 = arith.constant -138 : i16
 %from_elements_8 = tensor.splat %c-138_i16 : tensor<2x2x5xi16>
    %cast_9 = tensor.cast %from_elements_8 : tensor<2x2x5xi16> to tensor<?x?x?xi16>
 %c-2_i64 = arith.constant -2 : i64
    %from_elements_10 = tensor.from_elements %c-2_i64 : tensor<1xi64>

    %463 = tosa.pad %cast_9, %from_elements_10 : (tensor<?x?x?xi16>, tensor<1xi64>) -> tensor<?x?x?xi16>

    return
  }
}
```
The above MLIR program will cause a crash when using the following command:
```
mlir-opt --tosa-to-tensor test.mlir
```
And the crash backtrace is:
```
mlir-opt: /data/tmp/v0827/llvm-project/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp:1150: mlir::OpFoldResult mlir::tensor::ExtractOp::fold(mlir::tensor::ExtractOp::FoldAdaptor): Assertion `static_cast<int64_t>(indices.size()) == tensorType.getRank() && "rank mismatch"' 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/tmp/v0827/llvm-project/build/bin/mlir-opt --tosa-to-tensor test.mlir
 #0 0x000055e82b2546ff llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x15266ff)
 #1 0x000055e82b251774 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007ff0cc37a420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007ff0cbe4700b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007ff0cbe26859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x00007ff0cbe26729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #6 0x00007ff0cbe37fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #7 0x000055e82d3586f8 mlir::tensor::ExtractOp::fold(mlir::tensor::ExtractOpGenericAdaptor<llvm::ArrayRef<mlir::Attribute>>) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x362a6f8)
 #8 0x000055e82d2ec291 llvm::LogicalResult llvm::detail::UniqueFunctionBase<llvm::LogicalResult, mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&>::CallImpl<mlir::Op<mlir::tensor::ExtractOp, mlir::OpTrait::ZeroRegions, mlir::OpTrait::OneResult, mlir::OpTrait::OneTypedResult<mlir::Type>::Impl, mlir::OpTrait::ZeroSuccessors, mlir::OpTrait::AtLeastNOperands<1u>::Impl, mlir::OpTrait::OpInvariants, mlir::OpAsmOpInterface::Trait, mlir::ConditionallySpeculatable::Trait, mlir::OpTrait::AlwaysSpeculatableImplTrait, mlir::MemoryEffectOpInterface::Trait, mlir::InferTypeOpInterface::Trait>::getFoldHookFn()::'lambda'(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) const>(void*, mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x35be291)
 #9 0x000055e82d2fd1d6 mlir::RegisteredOperationName::Model<mlir::tensor::ExtractOp>::foldHook(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x35cf1d6)
#10 0x000055e82e5ff274 mlir::Operation::fold(llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x48d1274)
#11 0x000055e82e5ff608 mlir::Operation::fold(llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x48d1608)
#12 0x000055e82e52a7c7 mlir::OpBuilder::tryFold(mlir::Operation*, llvm::SmallVectorImpl<mlir::Value>&) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x47fc7c7)
#13 0x000055e82dcc8621 (anonymous namespace)::PadConverter::matchAndRewrite(mlir::tosa::PadOp, mlir::tosa::PadOpAdaptor, mlir::ConversionPatternRewriter&) const TosaToTensor.cpp:0:0
#14 0x000055e82dcc40c3 mlir::OpConversionPattern<mlir::tosa::PadOp>::matchAndRewrite(mlir::Operation*, llvm::ArrayRef<mlir::Value>, mlir::ConversionPatternRewriter&) const (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x3f960c3)
#15 0x000055e82e47972a mlir::ConversionPattern::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&) const (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x474b72a)
#16 0x000055e83155c1f0 mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<llvm::LogicalResult (mlir::Pattern const&)>) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x782e1f0)
#17 0x000055e82e482b5b (anonymous namespace)::OperationLegalizer::legalize(mlir::Operation*, mlir::ConversionPatternRewriter&) DialectConversion.cpp:0:0
#18 0x000055e82e48423e mlir::OperationConverter::convert(mlir::ConversionPatternRewriter&, mlir::Operation*) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x475623e)
#19 0x000055e82e4884cb mlir::OperationConverter::convertOperations(llvm::ArrayRef<mlir::Operation*>) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x475a4cb)
#20 0x000055e82e4899e9 mlir::applyPartialConversion(mlir::Operation*, mlir::ConversionTarget const&, mlir::FrozenRewritePatternSet const&, mlir::ConversionConfig) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x475b9e9)
#21 0x000055e82dcc2fab (anonymous namespace)::TosaToTensor::runOnOperation() TosaToTensorPass.cpp:0:0
#22 0x000055e82e3e0511 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x46b2511)
#23 0x000055e82e3e08e9 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x46b28e9)
#24 0x000055e82e3e1a14 mlir::PassManager::run(mlir::Operation*) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x46b3a14)
#25 0x000055e82e3d26bb performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#26 0x000055e82e3d3084 processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, llvm::ThreadPoolInterface*) MlirOptMain.cpp:0:0
#27 0x000055e82e3d3234 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&)::'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
#28 0x000055e82e4da1fe 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) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x47ac1fe)
#29 0x000055e82e3caad9 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x469cad9)
#30 0x000055e82e3d33a1 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x46a53a1)
#31 0x000055e82e3d3866 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x46a5866)
#32 0x000055e82b16e6eb main (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x14406eb)
#33 0x00007ff0cbe28083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#34 0x000055e82b227b0e _start (/data/tmp/v0827/llvm-project/build/bin/mlir-opt+0x14f9b0e)
Aborted (core dumped)
```
My git version is 9007864f72d07ee26d710fc10b1f918c628affa0.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWl9T4zgS_zTmRUVKlv_mgYcMTG6pGhYKuH24l1RbloN2F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