<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/70183>70183</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir][tensor] --canonicalize crashed with assertion failure "invalid index for shaped type"
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
anonymousMCS
</td>
</tr>
</table>
<pre>
git version: b745ce952560716545e82770bf835e48a567eaf1
system: Ubuntu 20.04.6 LTS (Focal Fossa)
reproduced with: mlir-opt --canonicalize a.mlir
a.mlir:
```
module {
func.func private @func2() {
%c1 = arith.constant 1 : index
%idx28 = index.constant 28
%c29 = arith.constant 29 : index
%3 = bufferization.alloc_tensor(%c29) : tensor<?xi16>
// The crash would disappear, if %idx28 is 0. So the crash may due to UB in tensor.dim.
%dim = tensor.dim %3, %idx28 : tensor<?xi16>
%alloc_21 = memref.alloc(%c29) : memref<?x26x2xi32>
%16 = affine.vector_load %alloc_21[%c1, %c1, %dim] : memref<?x26x2xi32>, vector<7xi32>
return
}
}
```
trace:
```
mlir-opt: /data/bin/llvm-project/build/tools/mlir/include/mlir/IR/BuiltinTypeInterfaces.h.inc:240: bool mlir::detail::ShapedTypeTrait<ConcreteType>::isDynamicDim(unsigned int) const [with ConcreteType = mlir::RankedTensorType]: Assertion `idx < getRank() && "invalid index for shaped 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: /data/bin/llvm-project/build/bin/mlir-opt --canonicalize a.mlir
#0 0x0000556306ca73af llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/bin/llvm-project/build/bin/mlir-opt+0x15ca3af)
#1 0x0000556306ca4444 SignalHandler(int) Signals.cpp:0:0
#2 0x00007f5329672420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
#3 0x00007f532915d00b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
#4 0x00007f532913c859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
#5 0x00007f532913c729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
#6 0x00007f532914dfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
#7 0x000055630711bb9d (/data/bin/llvm-project/build/bin/mlir-opt+0x1a3eb9d)
#8 0x000055630711c4e2 (anonymous namespace)::FoldDimOfAllocTensorOp::matchAndRewrite(mlir::tensor::DimOp, mlir::PatternRewriter&) const BufferizationOps.cpp:0:0
#9 0x00005563071100aa mlir::detail::OpOrInterfaceRewritePatternBase<mlir::tensor::DimOp>::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&) const (/data/bin/llvm-project/build/bin/mlir-opt+0x1a330aa)
#10 0x000055630c4c20b8 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&)>) (/data/bin/llvm-project/build/bin/mlir-opt+0x6de50b8)
#11 0x00005563098a216c (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#12 0x00005563098a65db mlir::applyPatternsAndFoldGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (/data/bin/llvm-project/build/bin/mlir-opt+0x41c95db)
#13 0x000055630980c39a (anonymous namespace)::Canonicalizer::runOnOperation() Canonicalizer.cpp:0:0
#14 0x00005563097ed39e mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/bin/llvm-project/build/bin/mlir-opt+0x411039e)
#15 0x00005563097ed87a mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/bin/llvm-project/build/bin/mlir-opt+0x411087a)
#16 0x00005563097ee0a4 mlir::PassManager::run(mlir::Operation*) (/data/bin/llvm-project/build/bin/mlir-opt+0x41110a4)
#17 0x00005563097de96b performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#18 0x00005563097dfe24 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 0x00005563097dffd4 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 0x00005563098dc8e4 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) (/data/bin/llvm-project/build/bin/mlir-opt+0x41ff8e4)
#21 0x00005563097d92a7 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/data/bin/llvm-project/build/bin/mlir-opt+0x40fc2a7)
#22 0x00005563097e02fb mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/data/bin/llvm-project/build/bin/mlir-opt+0x41032fb)
#23 0x0000556306bdfa1b main (/data/bin/llvm-project/build/bin/mlir-opt+0x1502a1b)
#24 0x00007f532913e083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#25 0x0000556306c77cde _start (/data/bin/llvm-project/build/bin/mlir-opt+0x159acde)
Aborted (core dumped)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWV1v2zjW_jXMDRGDor4vcuHY9fsWaJAgyWAvDUo8srmVSC1JpfH8-gUp2ZacuMmMi9nZLVrXFsnnPOeThxQzRmwkwA2Kb1G8vGKd3Sp9w6SSu0Z15m7xdFUovrvZCItfQBuhJArnuEijuIQ8pnFC0iCJoxgymqakqLIwhihjcZICqwJElojM-0-zMxYat_q3opO2w5TMSDRL8LfnJ4xotlIlq_FKGcMQzccrNbRa8a4Ejn8Iu3UQTS30tWotvr4uHVtRslr8DpjN3Mh48fAknI8fooQMf_3PRvGuBozS2_43xlUny5n7wK0WL8wCRhFxvymiGaL5eC7GiMZlgFG4xEwLu52VShrLpMXu4RwLyeF1MlvwV5r5BX7suIBmwzwH6cS8xfRP3wMN_eyiqyrQ4ndmhZIzVteqXFuQRmnP3KGiAWJ4HC5QuHoVQYLCL2PAFaIr_LwFXGpmtviH6mqOuTCsbYFpRBdYVEdlhMFkhp8UtocVDdth3gG2Cv92i4UcBM64aGZHNbloPPHjoFfG4Y8s9SHbuFeV9n5ooNFQ9eq_0bsfHJBo8kpfRUhP4YKkN35VCQmzFyit0utaMT6W5dLG-X4ge_jCRYPi5c-F0QXuUVG4SKcMNNhOyz0dlC6HqD18mYZv_2k1K-GDOB-yxvFCdMWZZYiuCiERXdX1S3PdavVPKK172ImaI7qyStUG0ZVPIroSsqw7DscHXx8RXd12orZCPu9a-Cot6IqV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