llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[mlir] memref.subview/dim folding triggers Assertion "(subview.isDynamicSize(sourceIndex) && "expected dynamic subview size")"

Open CelestineSmith opened this issue 3 years ago • 8 comments

MLIR built at commit https://github.com/llvm/llvm-project/commit/159cac50 Reproduced with:

mlir-opt --one-shot-bufferize temp.mlir

temp.mlir:

func.func @func(%arg0: tensor<?x?x?xf32>, %arg1: index) -> index {
  %c1 = arith.constant 1 : index
  %extracted_slice = tensor.extract_slice %arg0[0, 0, 0] [1, %arg1, 1] [1, 1, 1] : tensor<?x?x?xf32> to tensor<1x?xf32>
  %dim = tensor.dim %extracted_slice, %c1 : tensor<1x?xf32>
  return %dim : index
} 

trace:

Assertion failed: (subview.isDynamicSize(sourceIndex) && "expected dynamic subview size"), function fold, file MemRefOps.cpp, line 1109.
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 --one-shot-bufferize 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                 0x0000000100be8818 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  mlir-opt                 0x0000000100be7880 llvm::sys::RunSignalHandlers() + 112
2  mlir-opt                 0x0000000100be8eb0 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x00000001aad414c4 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001aad29ee0 pthread_kill + 288
5  libsystem_c.dylib        0x00000001aac64340 abort + 168
6  libsystem_c.dylib        0x00000001aac63754 err + 0
7  mlir-opt                 0x0000000102700680 mlir::memref::DimOp::fold(mlir::memref::DimOpGenericAdaptor<llvm::ArrayRef<mlir::Attribute>>) (.cold.14) + 0
8  mlir-opt                 0x0000000101525914 mlir::memref::DimOp::fold(mlir::memref::DimOpGenericAdaptor<llvm::ArrayRef<mlir::Attribute>>) + 1388
9  mlir-opt                 0x000000010150ac00 mlir::LogicalResult mlir::Op<mlir::memref::DimOp, mlir::OpTrait::ZeroRegions, mlir::OpTrait::OneResult, mlir::OpTrait::OneTypedResult<mlir::IndexType>::Impl, mlir::OpTrait::ZeroSuccessors, mlir::OpTrait::NOperands<2u>::Impl, mlir::OpTrait::OpInvariants, mlir::OpAsmOpInterface::Trait, mlir::OpTrait::MemRefsNormalizable, mlir::ConditionallySpeculatable::Trait, mlir::MemoryEffectOpInterface::Trait, mlir::ShapedDimOpInterface::Trait, mlir::InferTypeOpInterface::Trait>::foldSingleResultHook<mlir::memref::DimOp>(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) + 176
10 mlir-opt                 0x0000000101509d94 mlir::RegisteredOperationName::Model<mlir::memref::DimOp>::foldHook(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) + 48
11 mlir-opt                 0x0000000102308174 mlir::Operation::fold(llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) + 72
12 mlir-opt                 0x0000000102248bc4 mlir::OperationFolder::tryToFold(mlir::OpBuilder&, mlir::Operation*, llvm::SmallVectorImpl<mlir::Value>&, llvm::function_ref<void (mlir::Operation*)>) + 556
13 mlir-opt                 0x0000000102248850 mlir::OperationFolder::tryToFold(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, llvm::function_ref<void (mlir::Operation*)>, bool*) + 508
14 mlir-opt                 0x000000010224d3c4 mlir::applyPatternsAndFoldGreedily(llvm::MutableArrayRef<mlir::Region>, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig) + 3592
15 mlir-opt                 0x00000001022139a8 (anonymous namespace)::Canonicalizer::runOnOperation() + 124
16 mlir-opt                 0x00000001021fe6c4 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) + 428
17 mlir-opt                 0x00000001021febf4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) + 320
18 mlir-opt                 0x0000000100e172fc (anonymous namespace)::OneShotBufferizePass::runOnOperation() + 832
19 mlir-opt                 0x00000001021fe6c4 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) + 428
20 mlir-opt                 0x00000001021febf4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) + 320
21 mlir-opt                 0x0000000102200570 mlir::PassManager::run(mlir::Operation*) + 1148
22 mlir-opt                 0x00000001021f9934 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
23 mlir-opt                 0x00000001021f9504 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
24 mlir-opt                 0x00000001022649a0 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
25 mlir-opt                 0x00000001021f792c 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
26 mlir-opt                 0x00000001021f7e20 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&, bool) + 1208
27 mlir-opt                 0x0000000100a8b41c main + 108
28 dyld                     0x00000001058d1088 start + 516

Isolated test case to reproduce:

// RUN: mlir-opt %s -canonicalize

func.func @func(%arg0: tensor<?x?x?xf32>, %arg1: index) -> index {
  %0 = bufferization.to_memref %arg0 : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>>
  %c1 = arith.constant 1 : index
  %subview = memref.subview %0[0, 0, 0] [1, %arg1, 1] [1, 1, 1] : memref<?x?x?xf32, strided<[?, ?, ?], offset: ?>> to memref<1x?xf32, strided<[?, ?], offset: ?>>
  %dim = memref.dim %subview, %c1 : memref<1x?xf32, strided<[?, ?], offset: ?>>
  return %dim : index
}

CelestineSmith avatar Jan 17 '23 01:01 CelestineSmith

still reproducible at 9c4f0d83

Colloportus0 avatar Feb 14 '23 10:02 Colloportus0

This is not a bug in the bufferization. The issue is caused by a bug in the computation of dropped dimensions of a memref.subview. The issue was introduced here: https://reviews.llvm.org/D109418

matthias-springer avatar Aug 24 '23 10:08 matthias-springer

oh that patch is two years old! Thanks for reporting and the repro. Ill take a look next week.

MaheshRavishankar avatar Aug 24 '23 18:08 MaheshRavishankar

I believe @matthias-springer put a patch out already if you can help review: https://reviews.llvm.org/D158721

joker-eph avatar Aug 25 '23 04:08 joker-eph

https://reviews.llvm.org/D158721 does not fix this issue, it's for another (but related) bug.

The main issue with this crash here is that SubViewOp::getDroppedDims returns an incorrect result in some cases. I'm not sure what's the best way to fix that. I left some comments on https://reviews.llvm.org/D109418.

matthias-springer avatar Aug 25 '23 08:08 matthias-springer

https://reviews.llvm.org/D159008 fixes this issue.

MaheshRavishankar avatar Aug 28 '23 18:08 MaheshRavishankar

Testing this in IREE as well for potential fallout (https://github.com/openxla/iree/pull/14851)

MaheshRavishankar avatar Aug 28 '23 18:08 MaheshRavishankar

Seems like fixed

joker-eph avatar Mar 12 '26 23:03 joker-eph