Crash when converting tensor.insert_slice op with leading dimensions of size 1 to rank-reduced version
The compiler crashes triggering an assert for the following: input
// reproducer.mlir
func.func @foo(%arg0: tensor<1x1x32xi32>, %arg1: tensor<1x32xi32>) -> tensor<1x1x32xi32> {
%inserted_slice = tensor.insert_slice %arg1 into %arg0[0, 0, 0] [1, 1, 32] [1, 1, 1] : tensor<1x32xi32> into tensor<1x1x32xi32>
return %inserted_slice : tensor<1x1x32xi32>
}
compile
./bin/mlir-opt reproducer.mlir -linalg-fold-unit-extent-dims
backtrace
mlir-opt: /home/culrho01/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:194: ArrayRef<T> llvm::ArrayRef<long>::slice(size_t, size_t) const [T = long]: Assertion `N+M <= size() && "Invalid specifier"' failed.
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1 0x0000ffffe86e3a14 in __GI_abort () at abort.c:79
#2 0x0000ffffe86f038c in __assert_fail_base (fmt=0xffffe87e98a8 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=assertion@entry=0xffffebde80f0 "N+M <= size() && \"Invalid specifier\"", file=file@entry=0xffffebdd4ecc "/home/culrho01/llvm-project/llvm/include/llvm/ADT/ArrayRef.h", line=line@entry=194,
function=function@entry=0xffffebde8d7e "ArrayRef<T> llvm::ArrayRef<long>::slice(size_t, size_t) const [T = long]") at assert.c:92
#3 0x0000ffffe86f03f4 in __GI___assert_fail (assertion=0xffffebde80f0 "N+M <= size() && \"Invalid specifier\"", file=0xffffebdd4ecc "/home/culrho01/llvm-project/llvm/include/llvm/ADT/ArrayRef.h", line=194, function=0xffffebde8d7e "ArrayRef<T> llvm::ArrayRef<long>::slice(size_t, size_t) const [T = long]") at assert.c:101
#4 0x0000ffffebf644d0 in llvm::ArrayRef<long>::slice (this=0xffffffffbd90, N=0, M=3) at /home/culrho01/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:194
#5 0x0000ffffebeeab34 in computeTensorReshapeCollapsedType (type=..., reassociation=...) at /home/culrho01/llvm-project/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp:1290
#6 0x0000ffffebeea944 in mlir::tensor::CollapseShapeOp::build (b=..., result=..., src=..., reassociation=..., attrs=...) at /home/culrho01/llvm-project/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp:1307
#7 0x0000fffff1262428 in mlir::OpBuilder::create<mlir::tensor::CollapseShapeOp, mlir::detail::TypedValue<mlir::TensorType>, llvm::SmallVector<llvm::SmallVector<long, 2u>, 1u>&> (this=0xffffffffce00, location=..., args=..., args=...) at /home/culrho01/llvm-project/mlir/include/mlir/IR/Builders.h:462
#8 0x0000fffff1252bd4 in (anonymous namespace)::RankReducedInsertSliceOp<mlir::tensor::InsertSliceOp>::matchAndRewrite (this=0x3d29a0, insertSliceOp=..., rewriter=...) at /home/culrho01/llvm-project/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp:650
#9 0x0000fffff1261da8 in mlir::detail::OpOrInterfaceRewritePatternBase<mlir::tensor::InsertSliceOp>::matchAndRewrite (this=0x3d29a0, op=0x3e30a0, rewriter=...) at /home/culrho01/llvm-project/mlir/include/mlir/IR/PatternMatch.h:330
#10 0x0000ffffea33f4c4 in 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&)>) (this=0xffffffffce20, op=0x3e30a0, rewriter=..., canApply=..., onFailure=..., onSuccess=...)
at /home/culrho01/llvm-project/mlir/lib/Rewrite/PatternApplicator.cpp:200
#11 0x0000ffffea4dc9fc in (anonymous namespace)::GreedyPatternRewriteDriver::simplify (this=0xffffffffcdf8, regions=...) at /home/culrho01/llvm-project/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp:292
#12 0x0000ffffea4dc1a8 in mlir::applyPatternsAndFoldGreedily (regions=..., patterns=..., config=...) at /home/culrho01/llvm-project/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp:452
#13 0x0000fffff12348c8 in mlir::applyPatternsAndFoldGreedily (op=0x382120, patterns=..., config=...) at /home/culrho01/llvm-project/mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h:86
#14 0x0000fffff1255ea4 in (anonymous namespace)::LinalgFoldUnitExtentDimsPass::runOnOperation (this=0x3d5400) at /home/culrho01/llvm-project/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp:719
#15 0x0000ffffe9b098b0 in mlir::detail::OpToOpPassAdaptor::run (pass=0x3d5400, op=0x382120, am=..., verifyPasses=true, parentInitGeneration=1) at /home/culrho01/llvm-project/mlir/lib/Pass/Pass.cpp:470
#16 0x0000ffffe9b09e3c in mlir::detail::OpToOpPassAdaptor::runPipeline (pm=..., op=0x382120, am=..., verifyPasses=true, parentInitGeneration=1, instrumentor=0x0, parentInfo=0x0) at /home/culrho01/llvm-project/mlir/lib/Pass/Pass.cpp:534
#17 0x0000ffffe9b0b420 in mlir::PassManager::runPasses (this=0xffffffffd9a0, op=0x382120, am=...) at /home/culrho01/llvm-project/mlir/lib/Pass/Pass.cpp:839
#18 0x0000ffffe9b0b354 in mlir::PassManager::run (this=0xffffffffd9a0, op=0x382120) at /home/culrho01/llvm-project/mlir/lib/Pass/Pass.cpp:819
#19 0x0000fffff524864c in performActions(llvm::raw_ostream&, bool, bool, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, llvm::function_ref<mlir::LogicalResult (mlir::PassManager&)>, bool, bool) (os=..., verifyDiagnostics=false, verifyPasses=true, sourceMgr=warning: RTTI symbol not found for class 'std::_Sp_counted_ptr_inplace<llvm::SourceMgr, std::allocator<llvm::SourceMgr>, (__gnu_cxx::_Lock_policy)2>'
warning: RTTI symbol not found for class 'std::_Sp_counted_ptr_inplace<llvm::SourceMgr, std::allocator<llvm::SourceMgr>, (__gnu_cxx::_Lock_policy)2>'
std::shared_ptr<class llvm::SourceMgr> (use count 1, weak count 0) = {...},
context=0xffffffffdc90, passManagerSetupFn=..., emitBytecode=false, implicitModule=true) at /home/culrho01/llvm-project/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:91
#20 0x0000fffff5248338 in 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*) (os=..., ownedBuffer=std::unique_ptr<class llvm::MemoryBuffer> = {...},
verifyDiagnostics=false, verifyPasses=true, allowUnregisteredDialects=false, preloadDialectsInContext=false, emitBytecode=false, implicitModule=true, passManagerSetupFn=..., registry=..., threadPool=0x30c2e0) at /home/culrho01/llvm-project/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:139
#21 0x0000fffff5248124 in 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 (this=0xffffffffe2a8, chunkBuffer=..., os=...) at /home/culrho01/llvm-project/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:181
#22 0x0000fffff5248034 in 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&) (callable=281474976703144, params=..., params=...) at /home/culrho01/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:45
#23 0x0000ffffe90175ac in 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 (this=0xffffffffe228, params=..., params=...)
at /home/culrho01/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:68
#24 0x0000ffffe9016d9c in 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) (
originalBuffer=std::unique_ptr<class llvm::MemoryBuffer> = {...}, processChunkBuffer=..., os=..., enableSplitting=false, insertMarkerInOutput=true) at /home/culrho01/llvm-project/mlir/lib/Support/ToolUtilities.cpp:28
#25 0x0000fffff5247194 in 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) (outputStream=..., buffer=std::unique_ptr<class llvm::MemoryBuffer> = {...},
passManagerSetupFn=..., registry=..., splitInputFile=false, verifyDiagnostics=false, verifyPasses=true, allowUnregisteredDialects=false, preloadDialectsInContext=false, emitBytecode=false, implicitModule=true) at /home/culrho01/llvm-project/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:186
#26 0x0000fffff524731c in mlir::MlirOptMain (outputStream=..., buffer=..., passPipeline=..., registry=..., splitInputFile=false, verifyDiagnostics=false, verifyPasses=true, allowUnregisteredDialects=false, preloadDialectsInContext=false, emitBytecode=false, implicitModule=true, dumpPassPipeline=false)
at /home/culrho01/llvm-project/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:209
#27 0x0000fffff5247e4c in mlir::MlirOptMain (argc=3, argv=0xffffffffedb8, toolName=..., registry=..., preloadDialectsInContext=false) at /home/culrho01/llvm-project/mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:306
#28 0x000000000025c4ac in main (argc=3, argv=0xffffffffedb8) at /home/culrho01/llvm-project/mlir/tools/mlir-opt/mlir-opt.cpp:250
analysis
The assert is firing because of an out-of-bounds call to slice in computeTensorReshapeCollapsedType.
auto band = shape.slice(currentDim, dim);
shape is of size 2 reflecting the input tensor tensor<1x32xi32> and slice is called with currentDim=0 and dim=3, where dim comes from the reassociation. I dumped some relevant values before the crash:
reassociation->size(): 1
reassoc_idx.size(): 3
reassoc_idx: <0,1,2,>
shape.size(): 2
(d0, d1, d2) -> (d0, d1, d2)
currentDim: 0
dim: 3
<CRASH>
I think the issue is the reassociation computed in RankReducedInsertSliceOp by getReassociationMapForFoldingUnitDims and the IR we want is?
func.func @foo(%arg0: tensor<1x1x32xi32>, %arg1: tensor<1x32xi32>) -> tensor<1x1x32xi32> {
%collapsed = tensor.collapse_shape %arg1 [[0,1]] : tensor<1x32xi32> into tensor<32xi32>
%inserted_slice = tensor.insert_slice %collapsed into %arg0[0, 0, 0] [1, 1, 32] [1, 1, 1] : tensor<32xi32> into tensor<1x1x32xi32>
return %inserted_slice : tensor<1x1x32xi32>
}
I've posted a patch on Phab that seems to fix the issue for me, but would like input from others @MaheshRavishankar
@llvm/issue-subscribers-mlir-linalg
Sorry I forgot to push send on my comment.
I confirmed that this crash is fixed by PR #74723.