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

ML inliner and coroutine code crash #62616

Closed
pkasting opened this issue May 9, 2023 · 15 comments
Closed

ML inliner and coroutine code crash #62616

pkasting opened this issue May 9, 2023 · 15 comments
Assignees
Labels
backend:X86 crash Prefer [crash-on-valid] or [crash-on-invalid]

Comments

@pkasting
Copy link
Member

pkasting commented May 9, 2023

The following code causes a fatal error when run with -std=c++20 -O -mllvm -enable-ml-inliner=release. See repro at https://godbolt.org/z/zze1o6d4a.

#include <coroutine>

void func();

struct S {
  struct promise_type {
    S get_return_object();
    std::suspend_never initial_suspend();
    std::suspend_always yield_value(int);
    std::suspend_never final_suspend() noexcept;
    void unhandled_exception();

    ~promise_type() { func(); }
  };
};

S get_s() {
  co_yield 0;
}

The output is:

error: Could not setup Inlining Advisor for the requested mode and/or options
fatal error: error in backend: Cannot select: intrinsic %llvm.coro.size
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++20 -O -mllvm -enable-ml-inliner=release <source>
1.	<eof> parser at end of file
2.	Code generation
3.	Running pass 'Function Pass Manager' on module '<source>'.
4.	Running pass 'X86 DAG->DAG Instruction Selection' on function '@_Z5get_sv'
 #0 0x000055a645ba338f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x35ea38f)
 #1 0x000055a645ba13bc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x35e83bc)
 #2 0x000055a645af3617 llvm::CrashRecoveryContext::HandleExit(int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x353a617)
 #3 0x000055a645b99022 llvm::sys::Process::Exit(int, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x35e0022)
 #4 0x000055a64343a51f (/opt/compiler-explorer/clang-trunk/bin/clang+++0xe8151f)
 #5 0x000055a645afc4ca llvm::report_fatal_error(llvm::Twine const&, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x35434ca)
 #6 0x000055a646bb8af0 llvm::SelectionDAGISel::CannotYetSelect(llvm::SDNode*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x45ffaf0)
 #7 0x000055a646bbdd4d llvm::SelectionDAGISel::SelectCodeCommon(llvm::SDNode*, unsigned char const*, unsigned int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4604d4d)
 #8 0x000055a6448517ea (anonymous namespace)::X86DAGToDAGISel::Select(llvm::SDNode*) X86ISelDAGToDAG.cpp:0:0
 #9 0x000055a646bb60e7 llvm::SelectionDAGISel::DoInstructionSelection() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x45fd0e7)
#10 0x000055a646bc2459 llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4609459)
#11 0x000055a646bc523b llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x460c23b)
#12 0x000055a646bc7826 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (.part.0) SelectionDAGISel.cpp:0:0
#13 0x000055a64485b405 (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) X86ISelDAGToDAG.cpp:0:0
#14 0x000055a645023040 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#15 0x000055a64553bcda llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2f82cda)
#16 0x000055a64553be69 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2f82e69)
#17 0x000055a64553ca50 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2f83a50)
#18 0x000055a645e051e2 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x384c1e2)
#19 0x000055a646d07f32 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x474ef32)
#20 0x000055a6480c6119 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5b0d119)
#21 0x000055a646d06e35 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x474de35)
#22 0x000055a6465f1001 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4038001)
#23 0x000055a6465732b3 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3fba2b3)
#24 0x000055a6466d1623 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4118623)
#25 0x000055a64343bb4c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xe82b4c)
#26 0x000055a6434370bf ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#27 0x000055a6463cdcfd void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#28 0x000055a645af3507 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x353a507)
#29 0x000055a6463cdf3c clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#30 0x000055a64639541c clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ddc41c)
#31 0x000055a646395e0d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ddce0d)
#32 0x000055a6463a043c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3de743c)
#33 0x000055a64343967f clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xe8067f)
#34 0x000055a6433383c5 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xd7f3c5)
#35 0x00007f02b7df4083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#36 0x000055a64343329e _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xe7a29e)
clang++: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Compiler returned: 70
@EugeneZelenko EugeneZelenko added backend:X86 crash Prefer [crash-on-valid] or [crash-on-invalid] and removed new issue labels May 9, 2023
@llvmbot
Copy link
Member

llvmbot commented May 9, 2023

@llvm/issue-subscribers-backend-x86

@aeubanks
Copy link
Contributor

"parser at end of file" doesn't mean anything, the lines above it are what matter

error: Could not setup Inlining Advisor for the requested mode and/or options

is because godbolt's clang isn't compiled with ML inlining support. although I'm surprised it kept going after that.

fatal error: error in backend: Cannot select: intrinsic %llvm.coro.size

looks like the real issue. can you get a repro without the inliner flag?

@pkasting
Copy link
Member Author

I can't repro on Win locally without that flag, and with that flag I always get the "error in backend" error. If you tell me how to build with ML inliner support on Windows (if such a thing exists), I can try to rebuild my LLVM checkout that way.

The original error on the Android bot is here, but uninformative: https://logs.chromium.org/logs/chromium/buildbucket/cr-buildbucket/8774566019004443201/+/u/compile__with_patch_/stdout

@aeubanks
Copy link
Contributor

I don't believe it's supported on windows. This is something you'll probably need to repro on Linux. If there's a Chrome change I can try to repro the crash with I can try taking a look.

@pkasting
Copy link
Member Author

When I tried just now on my Linux checkout with the Chromium clang and the testcase at the top of this bug, I got:

PLEASE submit a bug report to https://crbug.com in the Tools>LLVM component, run tools/clang/scripts/process_crashreports.py (only if inside Google) to upload crash related files, and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /usr/local/google/home/pkasting/src/trunk/src/third_party/llvm-build/Release+Asserts/bin/clang -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name test.cc -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/local/google/home/pkasting/src/trunk/src -resource-dir /usr/local/google/home/pkasting/src/trunk/src/third_party/llvm-build/Release+Asserts/lib/clang/17 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/x86_64-linux-gnu/c++/12 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/backward -internal-isystem /usr/local/google/home/pkasting/src/trunk/src/third_party/llvm-build/Release+Asserts/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O1 -std=c++20 -fdeprecated-macro -fdebug-compilation-dir=/usr/local/google/home/pkasting/src/trunk/src -ferror-limit 19 -fgnuc-version=4.2.1 -fno-implicit-modules -fcxx-exceptions -fexceptions -fcolor-diagnostics -mllvm -enable-ml-inliner=release -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/test-30e027.o -x c++ test.cc
1.	<eof> parser at end of file
2.	Optimizer
#0 0x0000558790cbebbf (/usr/local/google/home/pkasting/src/trunk/src/third_party/llvm-build/Release+Asserts/bin/clang+0x3ebebbf)
clang: error: unable to execute command: Segmentation fault
clang: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 17.0.0 (https://chromium.googlesource.com/a/external/github.com/llvm/llvm-project 0c545a441285a73e00b859dd52f1a85cb9eeeefc)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/google/home/pkasting/src/trunk/src/third_party/llvm-build/Release+Asserts/bin

Is that sufficient?

@aeubanks
Copy link
Contributor

ah yes thanks

@mtrofin this looks like an ML inliner advisor issue

$ cat /tmp/b.ll
define void @_Z5get_sv() presplitcoroutine {
  %1 = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
  %2 = call ptr @llvm.coro.begin(token %1, ptr null)
  %3 = call token @llvm.coro.save(ptr null)
  %4 = call i8 @llvm.coro.suspend(token none, i1 false)
  call void @_ZN1S12promise_typeD2Ev()
  ret void
}

declare token @llvm.coro.id(i32, ptr readnone, ptr nocapture readonly, ptr)
declare ptr @llvm.coro.begin(token, ptr writeonly)
declare token @llvm.coro.save(ptr)
declare i8 @llvm.coro.suspend(token, i1)

declare void @__clang_call_terminate()

define void @_ZN1S12promise_typeD2Ev() personality ptr null {
  invoke void @_Z4funcv()
          to label %1 unwind label %2

1:                                                ; preds = %0
  ret void

2:                                                ; preds = %0
  %3 = landingpad { ptr, i32 }
          catch ptr null
  call void @__clang_call_terminate()
  unreachable
}

declare void @_Z4funcv()
$ opt -disable-output /tmp/b.ll -O2 -enable-ml-inliner=release
terminate called after throwing an instance of 'std::out_of_range'
  what():  map::at

probably something about coroutine lowering passes introducing functions that the ML inline advisor isn't expecting

@aeubanks aeubanks changed the title "parser at end of file" with ML inliner and coroutine code ML inliner and coroutine code crash Jul 26, 2023
@mtrofin
Copy link
Member

mtrofin commented Jul 26, 2023

Ack, I'll take a look. Thanks for the repro!

@zmodem
Copy link
Collaborator

zmodem commented Aug 28, 2023

@mtrofin, did you had a chance to look at this?

@mtrofin
Copy link
Member

mtrofin commented Aug 28, 2023

Not yet. Talked to @kazutakahirata to take a look, but didn't update the assignees (sorry).

@kazutakahirata
Copy link
Contributor

Ack.

@mtrofin
Copy link
Member

mtrofin commented Aug 28, 2023

@zmodem to double-check, this is using the model that's part of the chromium's build of llvm, correct?

@zmodem
Copy link
Collaborator

zmodem commented Aug 28, 2023

@zmodem to double-check, this is using the model that's part of the chromium's build of llvm, correct?

Yes. (I didn't check, but I assume so; this came from https://crbug.com/1467960).

@aeubanks
Copy link
Contributor

might be wrong, but I doubt the exact model will matter since these functions are so small and will probably get inlined given a reasonable model

@mtrofin mtrofin self-assigned this Oct 4, 2023
mtrofin added a commit to mtrofin/llvm-project that referenced this issue Oct 4, 2023
The coroutine handle methods needed to be added to the `FunctionLevels`. We already had logic for their appearing between calls into the `Inliner` pass.
@mtrofin
Copy link
Member

mtrofin commented Oct 4, 2023

#68263

@pkasting - I can't for some reason add you as a reviewer to the PR, so notifying this way.

@mtrofin
Copy link
Member

mtrofin commented Oct 5, 2023

PR #68263 should resolve this.

@mtrofin mtrofin closed this as completed Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 crash Prefer [crash-on-valid] or [crash-on-invalid]
Projects
None yet
Development

No branches or pull requests

7 participants