Issue 53501
Summary Failure to optimize very easy tail-call when calling malloc
Labels new issue
Assignees
Reporter GabrielRavier
    (This is on current trunk)

```cpp
#include <stdlib.h>

void *f(size_t sz)
{
    return malloc(sz);
}
```
seems like it should be easily optimizable to a tail-call, but with -O3, LLVM outputs this:
```asm
f(unsigned long): # @f(unsigned long)
  push rax
  call malloc
  pop rcx
  ret
```
whereas GCC outputs this:
```asm
f(unsigned long):
  jmp malloc
```
For some reason LLVM seems to not like a tail-call specifically when it is calling malloc. Even trying to add `[[clang::musttail]]` does not work, with Clang crashing in the x86 backend with this message:
```
fatal error: error in backend: failed to perform tail call elimination on a call site marked musttail
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++ -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -O3 <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 '@_Z1fm'
 #0 0x00005593db160f1f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x00005593db15edf0 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x355edf0)
 #2 0x00005593db09a4d2 llvm::CrashRecoveryContext::HandleExit(int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x349a4d2)
 #3 0x00005593db1579be llvm::sys::Process::Exit(int, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x35579be)
 #4 0x00005593d8d8cfe6 (/opt/compiler-explorer/clang-trunk/bin/clang+++0x118cfe6)
 #5 0x00005593db0a19ff llvm::report_fatal_error(llvm::Twine const&, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x34a19ff)
 #6 0x00005593db0a1b88 (/opt/compiler-explorer/clang-trunk/bin/clang+++0x34a1b88)
 #7 0x00005593d9ee24c5 llvm::X86TargetLowering::LowerCall(llvm::TargetLowering::CallLoweringInfo&, llvm::SmallVectorImpl<llvm::SDValue>&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x22e24c5)
 #8 0x00005593dbf39e48 llvm::TargetLowering::LowerCallTo(llvm::TargetLowering::CallLoweringInfo&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4339e48)
 #9 0x00005593dbf43b43 llvm::SelectionDAGBuilder::lowerInvokable(llvm::TargetLowering::CallLoweringInfo&, llvm::BasicBlock const*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4343b43)
#10 0x00005593dbf616ee llvm::SelectionDAGBuilder::LowerCallTo(llvm::CallBase const&, llvm::SDValue, bool, bool, llvm::BasicBlock const*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x43616ee)
#11 0x00005593dbf4ef1e llvm::SelectionDAGBuilder::visitCall(llvm::CallInst const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x434ef1e)
#12 0x00005593dbf787dc llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x43787dc)
#13 0x00005593dbfd0ad7 llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void>, false, true>, bool&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x43d0ad7)
#14 0x00005593dbfd2aac llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x43d2aac)
#15 0x00005593dbfd4c72 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (.part.890) SelectionDAGISel.cpp:0:0
#16 0x00005593d9dcf060 (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) X86ISelDAGToDAG.cpp:0:0
#17 0x00005593da474b78 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2874b78)
#18 0x00005593da8d6f59 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2cd6f59)
#19 0x00005593da8d71f1 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2cd71f1)
#20 0x00005593da8d8467 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x2cd8467)
#21 0x00005593db48c134 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x388c134)
#22 0x00005593dc0e08ef clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x44e08ef)
#23 0x00005593dcf75fe9 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5375fe9)
#24 0x00005593dc0e0ac2 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x44e0ac2)
#25 0x00005593dbadc651 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3edc651)
#26 0x00005593dba77aa2 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3e77aa2)
#27 0x00005593dbba6523 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3fa6523)
#28 0x00005593d8d8eb24 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x118eb24)
#29 0x00005593d8d8aadd ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#30 0x00005593db915695 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#31 0x00005593db09a363 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x349a363)
#32 0x00005593db917768 clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, bool*) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3d17768)
#33 0x00005593db8eb9da clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ceb9da)
#34 0x00005593db8ec51f clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3cec51f)
#35 0x00005593db8f4ec5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3cf4ec5)
#36 0x00005593d8c9c1d2 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0x109c1d2)
#37 0x00007f40eb1290b3 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b3)
#38 0x00005593d8d8a65a _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0x118a65a)
clang-14: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Compiler returned: 70
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to