Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir][arith] Crash when using --canonicalize #92057

Closed
wangyongj1a opened this issue May 14, 2024 · 6 comments · Fixed by #93102
Closed

[mlir][arith] Crash when using --canonicalize #92057

wangyongj1a opened this issue May 14, 2024 · 6 comments · Fixed by #93102
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] mlir:arith

Comments

@wangyongj1a
Copy link

wangyongj1a commented May 14, 2024

I have the following MLIR program:
test.mlir:

func.func @func1() {
  %1 = arith.constant 5 : i16
  %i0 = arith.constant 0 : index
  %3 = tensor.from_elements %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1  : tensor<12xi16>
  %10 = arith.extsi %3 : tensor<12xi16> to tensor<?xi64>
  %7 = tensor.dim %10, %i0 : tensor<?xi64>
  return
}

The above MLIR program will cause a crash when using the following command:

mlir-opt --canonicalize test.mlir

And the crash backtrace is:

mlir-opt: /data/tmp/v0514/llvm-project/mlir/lib/IR/BuiltinAttributes.cpp:1342: static mlir::DenseElementsAttr mlir::DenseIntOrFPElementsAttr::getRaw(mlir::ShapedType, llvm::ArrayRef<char>): Assertion `type.hasStaticShape() && "type must have static shape"' 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/v0514/llvm-project/build/bin/mlir-opt --canonicalize test.mlir
 #0 0x0000559cf13586ef llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x15946ef)
 #1 0x0000559cf1355744 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f5be1cbb420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x00007f5be178800b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #4 0x00007f5be1767859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #5 0x00007f5be1767729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
 #6 0x00007f5be1778fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #7 0x0000559cf4427f58 mlir::DenseIntOrFPElementsAttr::getRaw(mlir::ShapedType, llvm::ArrayRef<char>) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4663f58)
 #8 0x0000559cf442924a mlir::DenseIntOrFPElementsAttr::getRaw(mlir::ShapedType, unsigned long, llvm::ArrayRef<llvm::APInt>) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x466524a)
 #9 0x0000559cf4429559 mlir::DenseElementsAttr::get(mlir::ShapedType, llvm::ArrayRef<llvm::APInt>) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4665559)
#10 0x0000559cf15d5bd1 mlir::arith::ExtSIOp::fold(mlir::arith::ExtSIOpGenericAdaptor<llvm::ArrayRef<mlir::Attribute>>) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x1811bd1)
#11 0x0000559cf1631e07 mlir::LogicalResult llvm::detail::UniqueFunctionBase<mlir::LogicalResult, mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&>::CallImpl<mlir::Op<mlir::arith::ExtSIOp, mlir::OpTrait::ZeroRegions, mlir::OpTrait::OneResult, mlir::OpTrait::OneTypedResult<mlir::Type>::Impl, mlir::OpTrait::ZeroSuccessors, mlir::OpTrait::OneOperand, mlir::OpTrait::OpInvariants, mlir::InferIntRangeInterface::Trait, mlir::ConditionallySpeculatable::Trait, mlir::OpTrait::AlwaysSpeculatableImplTrait, mlir::MemoryEffectOpInterface::Trait, mlir::OpTrait::SameOperandsAndResultShape, mlir::CastOpInterface::Trait, mlir::VectorUnrollOpInterface::Trait, mlir::OpTrait::Elementwise, mlir::OpTrait::Scalarizable, mlir::OpTrait::Vectorizable, mlir::OpTrait::Tensorizable>::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/v0514/llvm-project/build/bin/mlir-opt+0x186de07)
#12 0x0000559cf1629376 mlir::RegisteredOperationName::Model<mlir::arith::ExtSIOp>::foldHook(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x1865376)
#13 0x0000559cf44dee34 mlir::Operation::fold(llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x471ae34)
#14 0x0000559cf44df1c8 mlir::Operation::fold(llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x471b1c8)
#15 0x0000559cf437a72e (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#16 0x0000559cf437d81b mlir::applyPatternsAndFoldGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x45b981b)
#17 0x0000559cf42e900a (anonymous namespace)::Canonicalizer::runOnOperation() Canonicalizer.cpp:0:0
#18 0x0000559cf42c9881 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4505881)
#19 0x0000559cf42c9d6a mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4505d6a)
#20 0x0000559cf42ca524 mlir::PassManager::run(mlir::Operation*) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4506524)
#21 0x0000559cf42bb9ab performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#22 0x0000559cf42bc364 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
#23 0x0000559cf42bc514 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
#24 0x0000559cf43c1d6e 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&, llvm::StringRef, llvm::StringRef) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x45fdd6e)
#25 0x0000559cf42b4114 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x44f0114)
#26 0x0000559cf42bc66c mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x44f866c)
#27 0x0000559cf42bcb36 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x44f8b36)
#28 0x0000559cf12783fb main (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x14b43fb)
#29 0x00007f5be1769083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#30 0x0000559cf132773e _start (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x156373e)
Aborted (core dumped)

My git version is 4c79d38.

@wangyongj1a wangyongj1a changed the title Crash Crash when using --canonicalize May 14, 2024
@EugeneZelenko EugeneZelenko added mlir crash Prefer [crash-on-valid] or [crash-on-invalid] and removed new issue labels May 14, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented May 14, 2024

@llvm/issue-subscribers-mlir

Author: None (wangyongj1a)

I have the following MLIR program: test.mlir: ``` func.func @func1() { %1 = arith.constant 5 : i16 %i0 = arith.constant 0 : index %3 = tensor.from_elements %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1, %1 : tensor<12xi16> %10 = arith.extsi %3 : tensor<12xi16> to tensor<?xi64> %7 = tensor.dim %10, %i0 : tensor<?xi64> return } ``` The above MLIR program will cause a crash when using the following command: ``` mlir-opt --canonicalize test.mlir ``` And the crash backtrace is: ``` mlir-opt: /data/tmp/v0514/llvm-project/mlir/lib/IR/BuiltinAttributes.cpp:1342: static mlir::DenseElementsAttr mlir::DenseIntOrFPElementsAttr::getRaw(mlir::ShapedType, llvm::ArrayRef<char>): Assertion `type.hasStaticShape() && "type must have static shape"' 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/v0514/llvm-project/build/bin/mlir-opt --canonicalize test.mlir #0 0x0000559cf13586ef llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x15946ef) #1 0x0000559cf1355744 SignalHandler(int) Signals.cpp:0:0 #2 0x00007f5be1cbb420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420) #3 0x00007f5be178800b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b) #4 0x00007f5be1767859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859) #5 0x00007f5be1767729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729) #6 0x00007f5be1778fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6) #7 0x0000559cf4427f58 mlir::DenseIntOrFPElementsAttr::getRaw(mlir::ShapedType, llvm::ArrayRef<char>) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4663f58) #8 0x0000559cf442924a mlir::DenseIntOrFPElementsAttr::getRaw(mlir::ShapedType, unsigned long, llvm::ArrayRef<llvm::APInt>) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x466524a) #9 0x0000559cf4429559 mlir::DenseElementsAttr::get(mlir::ShapedType, llvm::ArrayRef<llvm::APInt>) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4665559) #10 0x0000559cf15d5bd1 mlir::arith::ExtSIOp::fold(mlir::arith::ExtSIOpGenericAdaptor<llvm::ArrayRef<mlir::Attribute>>) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x1811bd1) #11 0x0000559cf1631e07 mlir::LogicalResult llvm::detail::UniqueFunctionBase<mlir::LogicalResult, mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&>::CallImpl<mlir::Op<mlir::arith::ExtSIOp, mlir::OpTrait::ZeroRegions, mlir::OpTrait::OneResult, mlir::OpTrait::OneTypedResult<mlir::Type>::Impl, mlir::OpTrait::ZeroSuccessors, mlir::OpTrait::OneOperand, mlir::OpTrait::OpInvariants, mlir::InferIntRangeInterface::Trait, mlir::ConditionallySpeculatable::Trait, mlir::OpTrait::AlwaysSpeculatableImplTrait, mlir::MemoryEffectOpInterface::Trait, mlir::OpTrait::SameOperandsAndResultShape, mlir::CastOpInterface::Trait, mlir::VectorUnrollOpInterface::Trait, mlir::OpTrait::Elementwise, mlir::OpTrait::Scalarizable, mlir::OpTrait::Vectorizable, mlir::OpTrait::Tensorizable>::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/v0514/llvm-project/build/bin/mlir-opt+0x186de07) #12 0x0000559cf1629376 mlir::RegisteredOperationName::Model<mlir::arith::ExtSIOp>::foldHook(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x1865376) #13 0x0000559cf44dee34 mlir::Operation::fold(llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x471ae34) #14 0x0000559cf44df1c8 mlir::Operation::fold(llvm::SmallVectorImpl<mlir::OpFoldResult>&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x471b1c8) #15 0x0000559cf437a72e (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0 #16 0x0000559cf437d81b mlir::applyPatternsAndFoldGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x45b981b) #17 0x0000559cf42e900a (anonymous namespace)::Canonicalizer::runOnOperation() Canonicalizer.cpp:0:0 #18 0x0000559cf42c9881 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4505881) #19 0x0000559cf42c9d6a mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4505d6a) #20 0x0000559cf42ca524 mlir::PassManager::run(mlir::Operation*) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x4506524) #21 0x0000559cf42bb9ab performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0 #22 0x0000559cf42bc364 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 #23 0x0000559cf42bc514 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 #24 0x0000559cf43c1d6e 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&, llvm::StringRef, llvm::StringRef) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x45fdd6e) #25 0x0000559cf42b4114 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x44f0114) #26 0x0000559cf42bc66c mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x44f866c) #27 0x0000559cf42bcb36 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x44f8b36) #28 0x0000559cf12783fb main (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x14b43fb) #29 0x00007f5be1769083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083) #30 0x0000559cf132773e _start (/data/tmp/v0514/llvm-project/build/bin/mlir-opt+0x156373e) Aborted (core dumped) ``` My git version is 4c79d38.

@KB9
Copy link
Contributor

KB9 commented May 21, 2024

I'll have a look into this for you.

@kuhar
Copy link
Member

kuhar commented May 23, 2024

Do we actually want to support this form of static -> dynamic type casting on arith cast ops?
cc: @MaheshRavishankar

@joker-eph
Copy link
Collaborator

This is the problem of arith on tensor not being strictly tested, the IR is likely "valid" and we shouldn't crash.

@kuhar
Copy link
Member

kuhar commented May 23, 2024

This is the problem of arith on tensor not being strictly tested, the IR is likely "valid" and we shouldn't crash.

I'd be leaning towards making this invalid and updating the verifier (or rather the td defs), unless we have compelling reasons to support this. WDYT @joker-eph?

@kuhar kuhar changed the title Crash when using --canonicalize [mlir][arith] Crash when using --canonicalize May 23, 2024
@joker-eph
Copy link
Collaborator

Can you survey the state of what arith accepts in general to see if this is an outlier?

kuhar pushed a commit that referenced this issue May 23, 2024
This prevents an assertion when constructing the DenseElementsAttr
result, where the passed-in type is expected to have a static shape.

Fixes #92057
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] mlir:arith
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants