Skip to content

Segfault in mlir-translate --mlir-to-llvmir with LLVM 16 #63832

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

Closed
azteca1998 opened this issue Jul 12, 2023 · 6 comments · Fixed by #68136
Closed

Segfault in mlir-translate --mlir-to-llvmir with LLVM 16 #63832

azteca1998 opened this issue Jul 12, 2023 · 6 comments · Fixed by #68136
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] mlir

Comments

@azteca1998
Copy link

LLVM version

LLVM 16.0.6 in both Linux and Mac OS.

MLIR input

module attributes {llvm.data_layout = ""} {
  llvm.func @"hello_starknet::hello_starknet::main"(%arg0: i64, %arg1: !llvm.ptr) -> !llvm.struct<(i64, ptr, struct<()>)> attributes {llvm.emit_c_interface, sym_visibility = "public"} {
    %0 = llvm.mlir.constant(true) : i1
    %1 = llvm.mlir.constant(false) : i1
    %2 = llvm.mlir.constant(0 : i64) : i64
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x !llvm.struct<packed (i1, array<39 x i8>)> {alignment = 8 : i64} : (i64) -> !llvm.ptr<struct<packed (i1, array<39 x i8>)>>
    %5 = llvm.alloca %3 x i64 {alignment = 8 : i64} : (i64) -> !llvm.ptr<i64>
    %6 = llvm.alloca %3 x !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)> {alignment = 8 : i64} : (i64) -> !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)>>
    %7 = llvm.alloca %3 x !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)> {alignment = 8 : i64} : (i64) -> !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)>>
    %8 = llvm.getelementptr %arg1[0] : (!llvm.ptr) -> !llvm.ptr<ptr>
    %9 = llvm.load %8 : !llvm.ptr<ptr>
    llvm.store %arg0, %5 : !llvm.ptr<i64>
    %10 = llvm.getelementptr %arg1[1] : (!llvm.ptr) -> !llvm.ptr<func<void (ptr<struct<packed (i1, array<39 x i8>)>>, ptr, ptr<i64>, i64)>>
    llvm.call %10(%4, %9, %5, %2) : (!llvm.ptr<struct<packed (i1, array<39 x i8>)>>, !llvm.ptr, !llvm.ptr<i64>, i64) -> ()
    %11 = llvm.load %4 : !llvm.ptr<struct<packed (i1, array<39 x i8>)>>
    %12 = llvm.extractvalue %11[0] : !llvm.struct<packed (i1, array<39 x i8>)> 
    %13 = llvm.getelementptr %4[8] : (!llvm.ptr<struct<packed (i1, array<39 x i8>)>>) -> !llvm.ptr<i252>
    %14 = llvm.load %13 : !llvm.ptr<i252>
    %15 = llvm.getelementptr %4[8] : (!llvm.ptr<struct<packed (i1, array<39 x i8>)>>) -> !llvm.ptr<struct<(ptr<i252>, i32, i32)>>
    %16 = llvm.load %15 : !llvm.ptr<struct<(ptr<i252>, i32, i32)>>
    %17 = llvm.load %5 : !llvm.ptr<i64>
    llvm.cond_br %12, ^bb1(%14 : i252), ^bb2(%16 : !llvm.struct<(ptr<i252>, i32, i32)>)
  ^bb1(%18: i252):  // pred: ^bb0
    %19 = llvm.mlir.undef : !llvm.struct<packed (i1, array<7 x i8>, i252)>
    %20 = llvm.insertvalue %1, %19[0] : !llvm.struct<packed (i1, array<7 x i8>, i252)> 
    %21 = llvm.insertvalue %18, %20[2] : !llvm.struct<packed (i1, array<7 x i8>, i252)> 
    %22 = llvm.bitcast %7 : !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)>> to !llvm.ptr<struct<packed (i1, array<7 x i8>, i252)>>
    llvm.store %21, %22 {alignment = 8 : i64} : !llvm.ptr<struct<packed (i1, array<7 x i8>, i252)>>
    llvm.br ^bb3(%17, %arg1 : i64, !llvm.ptr)
  ^bb2(%23: !llvm.struct<(ptr<i252>, i32, i32)>):  // pred: ^bb0
    %24 = llvm.mlir.undef : !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)>
    %25 = llvm.insertvalue %0, %24[0] : !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)> 
    %26 = llvm.insertvalue %23, %25[2] : !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)> 
    %27 = llvm.bitcast %6 : !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)>> to !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)>>
    llvm.store %26, %27 {alignment = 8 : i64} : !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)>>
    llvm.br ^bb3(%17, %arg1 : i64, !llvm.ptr)
  ^bb3(%28: i64, %29: !llvm.ptr):  // 2 preds: ^bb1, ^bb2
    %30 = llvm.mlir.undef : !llvm.struct<()>
    %31 = llvm.mlir.undef : !llvm.struct<(i64, ptr, struct<()>)>
    %32 = llvm.insertvalue %28, %31[0] : !llvm.struct<(i64, ptr, struct<()>)> 
    %33 = llvm.insertvalue %29, %32[1] : !llvm.struct<(i64, ptr, struct<()>)> 
    %34 = llvm.insertvalue %30, %33[2] : !llvm.struct<(i64, ptr, struct<()>)> 
    llvm.return %34 : !llvm.struct<(i64, ptr, struct<()>)>
  }
  llvm.func @"_mlir_ciface_hello_starknet::hello_starknet::main"(%arg0: !llvm.ptr<struct<(i64, ptr, struct<()>)>>, %arg1: i64, %arg2: !llvm.ptr) attributes {llvm.emit_c_interface, sym_visibility = "public"} {
    %0 = llvm.call @"hello_starknet::hello_starknet::main"(%arg1, %arg2) : (i64, !llvm.ptr) -> !llvm.struct<(i64, ptr, struct<()>)>
    llvm.store %0, %arg0 : !llvm.ptr<struct<(i64, ptr, struct<()>)>>
    llvm.return
  }
}

Stack trace after crashing

Stack trace in Linux (LLVM debug info available)

PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: mlir-translate --mlir-to-llvmir
 #0 0x000055e5468be14b llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /opt/llvm/llvm-project-16.0.6.src/llvm/lib/Support/Unix/Signals.inc:567:13
 #1 0x000055e5468bc450 llvm::sys::RunSignalHandlers() /opt/llvm/llvm-project-16.0.6.src/llvm/lib/Support/Signals.cpp:105:18
 #2 0x000055e5468beb0d SignalHandler(int) /opt/llvm/llvm-project-16.0.6.src/llvm/lib/Support/Unix/Signals.inc:0:3
 #3 0x00007f173484fab0 (/usr/lib/libc.so.6+0x39ab0)
 #4 0x000055e546d43d85 mlir::TypeStorage::getAbstractType() /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/IR/TypeSupport.h:130:13
 #5 0x000055e546d43d85 mlir::Type::getTypeID() /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/IR/Types.h:115:37
 #6 0x000055e546d43d85 bool mlir::detail::StorageUserBase<mlir::LLVM::LLVMVoidType, mlir::Type, mlir::TypeStorage, mlir::detail::TypeUniquer>::classof<mlir::Type>(mlir::Type) /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/IR/StorageUniquerSupport.h:112:16
 #7 0x000055e546deeccb llvm::TypeSwitch<mlir::Type, llvm::Type*>& llvm::TypeSwitch<mlir::Type, llvm::Type*>::Case<mlir::LLVM::LLVMVoidType, mlir::LLVM::detail::TypeToLLVMIRTranslatorImpl::translateType(mlir::Type)::'lambda'(mlir::LLVM::LLVMVoidType)>(mlir::LLVM::detail::TypeToLLVMIRTranslatorImpl::translateType(mlir::Type)::'lambda'(mlir::LLVM::LLVMVoidType)&&) /opt/llvm/llvm-project-16.0.6.src/llvm/include/llvm/ADT/TypeSwitch.h:121:14
 #8 0x000055e546deeccb llvm::TypeSwitch<mlir::Type, llvm::Type*>& llvm::detail::TypeSwitchBase<llvm::TypeSwitch<mlir::Type, llvm::Type*>, mlir::Type>::Case<mlir::LLVM::detail::TypeToLLVMIRTranslatorImpl::translateType(mlir::Type)::'lambda'(mlir::LLVM::LLVMVoidType)>(mlir::LLVM::detail::TypeToLLVMIRTranslatorImpl::translateType(mlir::Type)::'lambda'(mlir::LLVM::LLVMVoidType)&&) /opt/llvm/llvm-project-16.0.6.src/llvm/include/llvm/ADT/TypeSwitch.h:60:29
 #9 0x000055e546deeccb mlir::LLVM::detail::TypeToLLVMIRTranslatorImpl::translateType(mlir::Type) /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Target/LLVMIR/TypeToLLVM.cpp:39:14
#10 0x000055e546e21c20 convertOperationImpl(mlir::Operation&, llvm::IRBuilderBase&, mlir::LLVM::ModuleTranslation&) /opt/llvm/llvm-project-16.0.6.src/build/tools/mlir/include/mlir/Dialect/LLVMIR/LLVMConversions.inc:144:49
#11 0x000055e546cb7287 mlir::LogicalResult::failed() const /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:44:33
#12 0x000055e546cb7287 mlir::failed(mlir::LogicalResult) /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:72:58
#13 0x000055e546cb7287 mlir::LLVM::ModuleTranslation::convertOperation(mlir::Operation&, llvm::IRBuilderBase&) /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:567:7
#14 0x000055e546cb7ac2 mlir::LogicalResult::failed() const /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:44:33
#15 0x000055e546cb7ac2 mlir::failed(mlir::LogicalResult) /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:72:58
#16 0x000055e546cb7ac2 mlir::LLVM::ModuleTranslation::convertBlock(mlir::Block&, bool, llvm::IRBuilderBase&) /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:613:9
#17 0x000055e546cb900c mlir::LLVM::ModuleTranslation::convertOneFunction(mlir::LLVM::LLVMFuncOp) /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:858:16
#18 0x000055e546cba606 mlir::LogicalResult::failed() const /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:44:33
#19 0x000055e546cba606 mlir::failed(mlir::LogicalResult) /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:72:58
#20 0x000055e546cba606 mlir::LLVM::ModuleTranslation::convertFunctions() /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:1082:9
#21 0x000055e546cbc522 mlir::LogicalResult::failed() const /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:44:33
#22 0x000055e546cbc522 mlir::failed(mlir::LogicalResult) /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:72:58
#23 0x000055e546cbc522 mlir::translateModuleToLLVMIR(mlir::Operation*, llvm::LLVMContext&, llvm::StringRef) /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp:1417:7
#24 0x000055e546cb48c6 std::__uniq_ptr_impl<llvm::Module, std::default_delete<llvm::Module>>::_M_ptr() const /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/unique_ptr.h:199:51
#25 0x000055e546cb48c6 std::unique_ptr<llvm::Module, std::default_delete<llvm::Module>>::get() const /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/unique_ptr.h:470:21
#26 0x000055e546cb48c6 std::unique_ptr<llvm::Module, std::default_delete<llvm::Module>>::operator bool() const /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/unique_ptr.h:487:16
#27 0x000055e546cb48c6 mlir::registerToLLVMIRTranslation()::$_0::operator()(mlir::Operation*, llvm::raw_ostream&) const /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp:31:14
#28 0x000055e546cb48c6 mlir::LogicalResult std::__invoke_impl<mlir::LogicalResult, mlir::registerToLLVMIRTranslation()::$_0&, mlir::Operation*, llvm::raw_ostream&>(std::__invoke_other, mlir::registerToLLVMIRTranslation()::$_0&, mlir::Operation*&&, llvm::raw_ostream&) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/invoke.h:61:14
#29 0x000055e546cb48c6 std::enable_if<is_invocable_r_v<mlir::LogicalResult, mlir::registerToLLVMIRTranslation()::$_0&, mlir::Operation*, llvm::raw_ostream&>, mlir::LogicalResult>::type std::__invoke_r<mlir::LogicalResult, mlir::registerToLLVMIRTranslation()::$_0&, mlir::Operation*, llvm::raw_ostream&>(mlir::registerToLLVMIRTranslation()::$_0&, mlir::Operation*&&, llvm::raw_ostream&) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/invoke.h:114:9
#30 0x000055e546cb48c6 std::_Function_handler<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&), mlir::registerToLLVMIRTranslation()::$_0>::_M_invoke(std::_Any_data const&, mlir::Operation*&&, llvm::raw_ostream&) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/std_function.h:290:9
#31 0x000055e546fec6e8 std::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)>::operator()(mlir::Operation*, llvm::raw_ostream&) const /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/std_function.h:591:9
#32 0x000055e546fec6e8 mlir::TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(llvm::StringRef, llvm::StringRef, std::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)> const&, std::function<void (mlir::DialectRegistry&)> const&)::$_3::operator()(std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*) const /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Tools/mlir-translate/Translation.cpp:153:16
#33 0x000055e546fec6e8 mlir::LogicalResult std::__invoke_impl<mlir::LogicalResult, mlir::TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(llvm::StringRef, llvm::StringRef, std::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)> const&, std::function<void (mlir::DialectRegistry&)> const&)::$_3&, std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*>(std::__invoke_other, mlir::TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(llvm::StringRef, llvm::StringRef, std::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)> const&, std::function<void (mlir::DialectRegistry&)> const&)::$_3&, std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*&&) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/invoke.h:61:14
#34 0x000055e546fec6e8 std::enable_if<is_invocable_r_v<mlir::LogicalResult, mlir::TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(llvm::StringRef, llvm::StringRef, std::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)> const&, std::function<void (mlir::DialectRegistry&)> const&)::$_3&, std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*>, mlir::LogicalResult>::type std::__invoke_r<mlir::LogicalResult, mlir::TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(llvm::StringRef, llvm::StringRef, std::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)> const&, std::function<void (mlir::DialectRegistry&)> const&)::$_3&, std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*>(mlir::TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(llvm::StringRef, llvm::StringRef, std::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)> const&, std::function<void (mlir::DialectRegistry&)> const&)::$_3&, std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*&&) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/invoke.h:114:9
#35 0x000055e546fec6e8 std::_Function_handler<mlir::LogicalResult (std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*), mlir::TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(llvm::StringRef, llvm::StringRef, std::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)> const&, std::function<void (mlir::DialectRegistry&)> const&)::$_3>::_M_invoke(std::_Any_data const&, std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*&&) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/std_function.h:290:9
#36 0x000055e546feb21b std::function<mlir::LogicalResult (std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*)>::operator()(std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*) const /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/std_function.h:591:9
#37 0x000055e546feb21b mlir::Translation::operator()(std::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*) const /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Tools/mlir-translate/Translation.h:81:12
#38 0x000055e546feb21b mlir::mlirTranslateMain(int, char**, llvm::StringRef)::$_0::operator()(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) const /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp:95:14
#39 0x000055e546feb21b mlir::LogicalResult llvm::function_ref<mlir::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&)>::callback_fn<mlir::mlirTranslateMain(int, char**, llvm::StringRef)::$_0>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) /opt/llvm/llvm-project-16.0.6.src/llvm/include/llvm/ADT/STLFunctionalExtras.h:45:12
#40 0x000055e546ff0ca5 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 /opt/llvm/llvm-project-16.0.6.src/llvm/include/llvm/ADT/STLFunctionalExtras.h:68:12
#41 0x000055e546ff0ca5 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) /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Support/ToolUtilities.cpp:28:12
#42 0x000055e546fe9bc2 mlir::mlirTranslateMain(int, char**, llvm::StringRef) /opt/llvm/llvm-project-16.0.6.src/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp:106:14
#43 0x000055e54689dad6 mlir::LogicalResult::failed() const /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:44:33
#44 0x000055e54689dad6 mlir::failed(mlir::LogicalResult) /opt/llvm/llvm-project-16.0.6.src/mlir/include/mlir/Support/LogicalResult.h:72:58
#45 0x000055e54689dad6 main /opt/llvm/llvm-project-16.0.6.src/mlir/tools/mlir-translate/mlir-translate.cpp:34:10
#46 0x00007f1734839850 (/usr/lib/libc.so.6+0x23850)
#47 0x00007f173483990a __libc_start_main (/usr/lib/libc.so.6+0x2390a)
#48 0x000055e54689d9c5 _start (/opt/llvm/dist/bin/mlir-translate+0x3819c5)
Segmentation fault (core dumped)

Stack trace in Mac OS (no LLVM debug info available)

PLEASE submit a bug report to https://github.com/Homebrew/homebrew-core/issues and include the crash backtrace.
Stack dump:
0.      Program arguments: mlir-translate --mlir-to-llvmir
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  libLLVM.dylib            0x000000010df943cc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  libLLVM.dylib            0x0000000111dc8850 SignalHandler(int) + 304
2  libsystem_platform.dylib 0x00000001826d6a24 _sigtramp + 56
3  mlir-translate           0x0000000104e748dc convertOperationImpl(mlir::Operation&, llvm::IRBuilderBase&, mlir::LLVM::ModuleTranslation&) + 18804
4  mlir-translate           0x0000000104e748dc convertOperationImpl(mlir::Operation&, llvm::IRBuilderBase&, mlir::LLVM::ModuleTranslation&) + 18804
5  mlir-translate           0x0000000104eaa4a4 mlir::LLVM::ModuleTranslation::convertOperation(mlir::Operation&, llvm::IRBuilderBase&) + 84
6  mlir-translate           0x0000000104eaa98c mlir::LLVM::ModuleTranslation::convertBlock(mlir::Block&, bool, llvm::IRBuilderBase&) + 448
7  mlir-translate           0x0000000104eb34b8 mlir::translateModuleToLLVMIR(mlir::Operation*, llvm::LLVMContext&, llvm::StringRef) + 31388
8  mlir-translate           0x0000000104eb824c std::__1::__function::__func<mlir::registerToLLVMIRTranslation()::$_0, std::__1::allocator<mlir::registerToLLVMIRTranslation()::$_0>, mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)>::operator()(mlir::Operation*&&, llvm::raw_ostream&) + 60
9  mlir-translate           0x0000000104f7416c std::__1::__function::__func<mlir::TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(llvm::StringRef, llvm::StringRef, std::__1::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)> const&, std::__1::function<void (mlir::DialectRegistry&)> const&)::$_0, std::__1::allocator<mlir::TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(llvm::StringRef, llvm::StringRef, std::__1::function<mlir::LogicalResult (mlir::Operation*, llvm::raw_ostream&)> const&, std::__1::function<void (mlir::DialectRegistry&)> const&)::$_0>, mlir::LogicalResult (std::__1::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*)>::operator()(std::__1::shared_ptr<llvm::SourceMgr> const&, llvm::raw_ostream&, mlir::MLIRContext*&&) + 2000
10 mlir-translate           0x0000000104f716a8 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::mlirTranslateMain(int, char**, llvm::StringRef)::$_0>(long, std::__1::unique_ptr<llvm::MemoryBuffer, std::__1::default_delete<llvm::MemoryBuffer>>, llvm::raw_ostream&) + 452
11 mlir-translate           0x0000000104f774fc 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) + 648
12 mlir-translate           0x0000000104f70164 mlir::mlirTranslateMain(int, char**, llvm::StringRef) + 720
13 mlir-translate           0x00000001047a0eb4 main + 68
14 dyld                     0x000000018234ff28 start + 2236
Segmentation fault: 11
@EugeneZelenko EugeneZelenko added mlir crash Prefer [crash-on-valid] or [crash-on-invalid] and removed new issue labels Jul 12, 2023
@llvmbot
Copy link
Member

llvmbot commented Jul 12, 2023

@llvm/issue-subscribers-mlir

@joker-eph
Copy link
Collaborator

The IR unfortunately does not parse at HEAD anymore so I can't try to see if it is already fixed easily:

$ ./bin/mlir-translate --mlir-to-llvmir /tmp/t.mlir 
/tmp/t.mlir:15:35: error: custom op 'llvm.call' expected indirect call to have 2 trailing types
    llvm.call %10(%4, %9, %5, %2) : (!llvm.ptr<struct<packed (i1, array<39 x i8>)>>, !llvm.ptr, !llvm.ptr<i64>, i64) -> ()
                                  ^

@joker-eph joker-eph changed the title Segfault in mlir-translate. Segfault in mlir-translate --mlir-to-llvmir with LLVM 16 Jul 12, 2023
@azteca1998
Copy link
Author

According to the docs, an indirect llvm.call is expected to 2 trailing types, but attempting to add the first one causes a custom op 'llvm.call' expected function type in both the LSP and mlir-translate.

@azteca1998
Copy link
Author

azteca1998 commented Jul 13, 2023

Translating a test program from LLVM IR to MLIR generates an indirect call without the first type either:

define void @main(ptr %0) {
    call void %0()
    ret void
}
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<i1, dense<8> : vector<2xi32>>, #dlti.dl_entry<i8, dense<8> : vector<2xi32>>, #dlti.dl_entry<i16, dense<16> : vector<2xi32>>, #dlti.dl_entry<i32, dense<32> : vector<2xi32>>, #dlti.dl_entry<i64, dense<[32, 64]> : vector<2xi32>>, #dlti.dl_entry<f16, dense<16> : vector<2xi32>>, #dlti.dl_entry<f64, dense<64> : vector<2xi32>>, #dlti.dl_entry<f128, dense<128> : vector<2xi32>>>} {
  llvm.func @main(%arg0: !llvm.ptr) {
    llvm.call %arg0() : () -> ()
    llvm.return
  }
}

This one doesn't crash, but it still can't be translated back to LLVM IR: use of value '%arg0' expects different type than prior uses: '!llvm.ptr<func<void ()>>' vs '!llvm.ptr'.

@azteca1998
Copy link
Author

We modified our compiler so that it doesn't generate that indirect call and it still crashes so we know it's not that (or at least not only that). The new program that crashes mlir-translate is:

module attributes {llvm.data_layout = ""} {
  llvm.func @cairo_native__libfunc__starknet__get_block_hash(!llvm.ptr<struct<packed (i1, array<39 x i8>)>>, !llvm.ptr, !llvm.ptr<i64>, i64) attributes {sym_visibility = "private"}
  llvm.func @"hello_starknet::hello_starknet::main"(%arg0: i64, %arg1: !llvm.ptr) -> !llvm.struct<(i64, ptr, struct<()>)> attributes {llvm.emit_c_interface, sym_visibility = "public"} {
    %0 = llvm.mlir.constant(true) : i1
    %1 = llvm.mlir.constant(false) : i1
    %2 = llvm.mlir.constant(0 : i64) : i64
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x !llvm.struct<packed (i1, array<39 x i8>)> {alignment = 8 : i64} : (i64) -> !llvm.ptr<struct<packed (i1, array<39 x i8>)>>
    %5 = llvm.alloca %3 x i64 {alignment = 8 : i64} : (i64) -> !llvm.ptr<i64>
    %6 = llvm.alloca %3 x !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)> {alignment = 8 : i64} : (i64) -> !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)>>
    %7 = llvm.alloca %3 x !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)> {alignment = 8 : i64} : (i64) -> !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)>>
    %8 = llvm.getelementptr %arg1[0] : (!llvm.ptr) -> !llvm.ptr<ptr>
    %9 = llvm.load %8 : !llvm.ptr<ptr>
    llvm.store %arg0, %5 : !llvm.ptr<i64>
    llvm.call @cairo_native__libfunc__starknet__get_block_hash(%4, %9, %5, %2) : (!llvm.ptr<struct<packed (i1, array<39 x i8>)>>, !llvm.ptr, !llvm.ptr<i64>, i64) -> ()
    %10 = llvm.load %4 : !llvm.ptr<struct<packed (i1, array<39 x i8>)>>
    %11 = llvm.extractvalue %10[0] : !llvm.struct<packed (i1, array<39 x i8>)>
    %12 = llvm.getelementptr %4[8] : (!llvm.ptr<struct<packed (i1, array<39 x i8>)>>) -> !llvm.ptr<i252>
    %13 = llvm.load %12 : !llvm.ptr<i252>
    %14 = llvm.getelementptr %4[8] : (!llvm.ptr<struct<packed (i1, array<39 x i8>)>>) -> !llvm.ptr<struct<(ptr<i252>, i32, i32)>>
    %15 = llvm.load %14 : !llvm.ptr<struct<(ptr<i252>, i32, i32)>>
    %16 = llvm.load %5 : !llvm.ptr<i64>
    llvm.cond_br %11, ^bb1(%13 : i252), ^bb2(%15 : !llvm.struct<(ptr<i252>, i32, i32)>)
  ^bb1(%17: i252):  // pred: ^bb0
    %18 = llvm.mlir.undef : !llvm.struct<packed (i1, array<7 x i8>, i252)>
    %19 = llvm.insertvalue %1, %18[0] : !llvm.struct<packed (i1, array<7 x i8>, i252)>
    %20 = llvm.insertvalue %17, %19[2] : !llvm.struct<packed (i1, array<7 x i8>, i252)>
    %21 = llvm.bitcast %7 : !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)>> to !llvm.ptr<struct<packed (i1, array<7 x i8>, i252)>>
    llvm.store %20, %21 {alignment = 8 : i64} : !llvm.ptr<struct<packed (i1, array<7 x i8>, i252)>>
    llvm.br ^bb3(%16, %arg1 : i64, !llvm.ptr)
  ^bb2(%22: !llvm.struct<(ptr<i252>, i32, i32)>):  // pred: ^bb0
    %23 = llvm.mlir.undef : !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)>
    %24 = llvm.insertvalue %0, %23[0] : !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)>
    %25 = llvm.insertvalue %22, %24[2] : !llvm.struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)>
    %26 = llvm.bitcast %6 : !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>, array<16 x i8>)>> to !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)>>
    llvm.store %25, %26 {alignment = 8 : i64} : !llvm.ptr<struct<packed (i1, array<7 x i8>, struct<(ptr<i252>, i32, i32)>)>>
    llvm.br ^bb3(%16, %arg1 : i64, !llvm.ptr)
  ^bb3(%27: i64, %28: !llvm.ptr):  // 2 preds: ^bb1, ^bb2
    %29 = llvm.mlir.undef : !llvm.struct<()>
    %30 = llvm.mlir.undef : !llvm.struct<(i64, ptr, struct<()>)>
    %31 = llvm.insertvalue %27, %30[0] : !llvm.struct<(i64, ptr, struct<()>)>
    %32 = llvm.insertvalue %28, %31[1] : !llvm.struct<(i64, ptr, struct<()>)>
    %33 = llvm.insertvalue %29, %32[2] : !llvm.struct<(i64, ptr, struct<()>)>
    llvm.return %33 : !llvm.struct<(i64, ptr, struct<()>)>
  }
  llvm.func @"_mlir_ciface_hello_starknet::hello_starknet::main"(%arg0: !llvm.ptr<struct<(i64, ptr, struct<()>)>>, %arg1: i64, %arg2: !llvm.ptr) attributes {llvm.emit_c_interface, sym_visibility = "public"} {
    %0 = llvm.call @"hello_starknet::hello_starknet::main"(%arg1, %arg2) : (i64, !llvm.ptr) -> !llvm.struct<(i64, ptr, struct<()>)>
    llvm.store %0, %arg0 : !llvm.ptr<struct<(i64, ptr, struct<()>)>>
    llvm.return
  }
}

@azteca1998
Copy link
Author

I've been playing with the code above and I've found that the operation which causes the crash is llvm.getelementptr.

A minimal example that crashes:

llvm.func @main(%0 : !llvm.ptr) -> !llvm.ptr<ptr> {
    %1 = llvm.getelementptr %0[0] : (!llvm.ptr) -> !llvm.ptr<ptr>
    llvm.return %1 : !llvm.ptr<ptr>
}

A minimal example that doesn't crash:

llvm.func @main(%0 : !llvm.ptr) -> !llvm.ptr {
    %1 = llvm.getelementptr %0[0] { elem_type = !llvm.ptr } : (!llvm.ptr) -> !llvm.ptr
    llvm.return %1 : !llvm.ptr
}

I'm aware that LLVM wants to remove non-opaque pointers but afaik that is in LLVM, and not MLIR. Since mlir-translate automatically makes all !llvm.ptr types opaque I assume that it's fine to use typed pointers in MLIR. Is this assumption wrong? Regardless of whether the assumption is right or wrong this is most likely a bug that should be fixed.

rikhuijzer added a commit that referenced this issue Oct 5, 2023
As was correctly pointed out by @azteca1998, the element type for a
`llvm.getelementptr` was only read when using an attribute and not when
using a type. As pointed out in
#63832 (comment),
the translation to LLVM would work for
```mlir
llvm.func @main(%0 : !llvm.ptr) -> !llvm.ptr {
    %1 = llvm.getelementptr %0[0] { elem_type = !llvm.ptr } : (!llvm.ptr) -> !llvm.ptr
    llvm.return %1 : !llvm.ptr
}
```
but not for
```mlir
llvm.func @main(%0 : !llvm.ptr) -> !llvm.ptr<ptr> {
    %1 = llvm.getelementptr %0[0] : (!llvm.ptr) -> !llvm.ptr<ptr>
    llvm.return %1 : !llvm.ptr<ptr>
}
```
This was caused by the `LLVM_GEPOp` builder only reading the type from
the attribute (`{ elem_type = !llvm.ptr }`), but not from the pointer
type (`!llvm.ptr<ptr>`).

Fixes #63832.

EDIT: During review Markus Böck pointed out that this bugfix adds new functionality for typed pointers, but this functionality shouldn't be there in the first place. In response, Oleksandr "Alex" Zinenko pointed out that this is okay for now since the typed pointers will be removed in an upcoming release anyway, so it's best to merge this PR and spend time on the removal instead.
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants