llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.84k stars 11.47k forks source link

[X86] LLVM ERROR: cannot select with `-fglobal-isel -finstrument-functions -flto` #88058

Open patrick-rivos opened 5 months ago

patrick-rivos commented 5 months ago

Testcase:

int main() {}

Backtrace (from godbolt):

clang: warning: -fglobal-isel support for the 'x86_64' architecture is incomplete [-Wglobal-isel]
LLVM ERROR: cannot select: %2:gpr(p0) = G_INTRINSIC intrinsic(@llvm.returnaddress), 0 (in function: main)
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.  Program arguments: /opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -pie -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /app/output.s /lib/x86_64-linux-gnu/Scrt1.o /lib/x86_64-linux-gnu/crti.o /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/crtbeginS.o -L./lib -L/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/../lib/x86_64-unknown-linux-gnu -L/opt/compiler-explorer/clang-assertions-trunk-20240408/lib/clang/19/lib/x86_64-unknown-linux-gnu -L/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1 -L/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../x86_64-linux-gnu/lib -L/lib -L/usr/lib -plugin-opt=mcpu=x86-64 -plugin-opt=-global-isel=1 /tmp/example-a842c4.o -rpath ./lib -rpath /opt/compiler-explorer/gcc-snapshot/lib64 -rpath /opt/compiler-explorer/gcc-snapshot/lib32 -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/crtendS.o /lib/x86_64-linux-gnu/crtn.o
1.  Running pass 'Function Pass Manager' on module 'ld-temp.o'.
2.  Running pass 'InstructionSelect' on function '@main'
 #0 0x00000000009c2928 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x9c2928)
 #1 0x00000000009c007c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007fef58842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007fef588969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007fef58842476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007fef588287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x0000000000823a2d (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x823a2d)
 #7 0x0000000002a792eb reportGISelDiagnostic(llvm::DiagnosticSeverity, llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, llvm::MachineOptimizationRemarkMissed&) Utils.cpp:0:0
 #8 0x0000000002a83313 llvm::reportGISelFailure(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, char const*, llvm::StringRef, llvm::MachineInstr const&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x2a83313)
 #9 0x00000000029f67f7 llvm::InstructionSelect::runOnMachineFunction(llvm::MachineFunction&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x29f67f7)
#10 0x00000000035f4611 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#11 0x00000000050ced73 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x50ced73)
#12 0x00000000050cefb1 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x50cefb1)
#13 0x00000000050cf815 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x50cf815)
#14 0x0000000002f7b8fd codegen(llvm::lto::Config const&, llvm::TargetMachine*, std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>, unsigned int, llvm::Module&, llvm::ModuleSummaryIndex const&) LTOBackend.cpp:0:0
#15 0x0000000002f7d145 llvm::lto::backend(llvm::lto::Config const&, std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>, unsigned int, llvm::Module&, llvm::ModuleSummaryIndex&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x2f7d145)
#16 0x0000000002f6f658 llvm::lto::LTO::runRegularLTO(std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x2f6f658)
#17 0x0000000002f6fbbc llvm::lto::LTO::run(std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>, std::function<llvm::Expected<std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>> (unsigned int, llvm::StringRef, llvm::Twine const&)>) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x2f6fbbc)
#18 0x0000000000b89259 lld::elf::BitcodeCompiler::compile() (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xb89259)
#19 0x0000000000aeaf5f void lld::elf::LinkerDriver::compileBitcodeFiles<llvm::object::ELFType<(llvm::endianness)1, true>>(bool) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xaeaf5f)
#20 0x0000000000b00f3b void lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::endianness)1, true>>(llvm::opt::InputArgList&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xb00f3b)
#21 0x0000000000b0d264 lld::elf::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xb0d264)
#22 0x0000000000b0ea52 lld::elf::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xb0ea52)
#23 0x00000000009f8e1b lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x9f8e1b)
#24 0x0000000000934c81 lld_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x934c81)
#25 0x000000000082d5d4 main (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x82d5d4)
#26 0x00007fef58829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#27 0x00007fef58829e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#28 0x000000000093460e _start (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x93460e)
clang: error: unable to execute command: Aborted (core dumped)
clang: error: linker command failed due to signal (use -v to see invocation)
Compiler returned: 254

Godbolt: https://godbolt.org/z/6K4j7W759 Works in LLVM 17.

Found via fuzzer

Possibly related to RISC-V failure: #88057

arsenm commented 5 months ago

X86 isn't expected to generally work with GlobalISel today. The generic intrinsic should have been translated as a generic instruction though

llvmbot commented 4 months ago

@llvm/issue-subscribers-backend-x86

Author: Patrick O'Neill (patrick-rivos)

Testcase: ```c int main() {} ``` Backtrace (from godbolt): ``` clang: warning: -fglobal-isel support for the 'x86_64' architecture is incomplete [-Wglobal-isel] LLVM ERROR: cannot select: %2:gpr(p0) = G_INTRINSIC intrinsic(@llvm.returnaddress), 0 (in function: main) PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace. Stack dump: 0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -pie -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /app/output.s /lib/x86_64-linux-gnu/Scrt1.o /lib/x86_64-linux-gnu/crti.o /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/crtbeginS.o -L./lib -L/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/../lib/x86_64-unknown-linux-gnu -L/opt/compiler-explorer/clang-assertions-trunk-20240408/lib/clang/19/lib/x86_64-unknown-linux-gnu -L/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1 -L/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/../../../../x86_64-linux-gnu/lib -L/lib -L/usr/lib -plugin-opt=mcpu=x86-64 -plugin-opt=-global-isel=1 /tmp/example-a842c4.o -rpath ./lib -rpath /opt/compiler-explorer/gcc-snapshot/lib64 -rpath /opt/compiler-explorer/gcc-snapshot/lib32 -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.1/crtendS.o /lib/x86_64-linux-gnu/crtn.o 1. Running pass 'Function Pass Manager' on module 'ld-temp.o'. 2. Running pass 'InstructionSelect' on function '@main' #0 0x00000000009c2928 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x9c2928) #1 0x00000000009c007c SignalHandler(int) Signals.cpp:0:0 #2 0x00007fef58842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520) #3 0x00007fef588969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc) #4 0x00007fef58842476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476) #5 0x00007fef588287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3) #6 0x0000000000823a2d (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x823a2d) #7 0x0000000002a792eb reportGISelDiagnostic(llvm::DiagnosticSeverity, llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, llvm::MachineOptimizationRemarkMissed&) Utils.cpp:0:0 #8 0x0000000002a83313 llvm::reportGISelFailure(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, char const*, llvm::StringRef, llvm::MachineInstr const&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x2a83313) #9 0x00000000029f67f7 llvm::InstructionSelect::runOnMachineFunction(llvm::MachineFunction&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x29f67f7) #10 0x00000000035f4611 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0 #11 0x00000000050ced73 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x50ced73) #12 0x00000000050cefb1 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x50cefb1) #13 0x00000000050cf815 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x50cf815) #14 0x0000000002f7b8fd codegen(llvm::lto::Config const&, llvm::TargetMachine*, std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>, unsigned int, llvm::Module&, llvm::ModuleSummaryIndex const&) LTOBackend.cpp:0:0 #15 0x0000000002f7d145 llvm::lto::backend(llvm::lto::Config const&, std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>, unsigned int, llvm::Module&, llvm::ModuleSummaryIndex&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x2f7d145) #16 0x0000000002f6f658 llvm::lto::LTO::runRegularLTO(std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x2f6f658) #17 0x0000000002f6fbbc llvm::lto::LTO::run(std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>, std::function<llvm::Expected<std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>> (unsigned int, llvm::StringRef, llvm::Twine const&)>) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x2f6fbbc) #18 0x0000000000b89259 lld::elf::BitcodeCompiler::compile() (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xb89259) #19 0x0000000000aeaf5f void lld::elf::LinkerDriver::compileBitcodeFiles<llvm::object::ELFType<(llvm::endianness)1, true>>(bool) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xaeaf5f) #20 0x0000000000b00f3b void lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::endianness)1, true>>(llvm::opt::InputArgList&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xb00f3b) #21 0x0000000000b0d264 lld::elf::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xb0d264) #22 0x0000000000b0ea52 lld::elf::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0xb0ea52) #23 0x00000000009f8e1b lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x9f8e1b) #24 0x0000000000934c81 lld_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x934c81) #25 0x000000000082d5d4 main (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x82d5d4) #26 0x00007fef58829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90) #27 0x00007fef58829e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40) #28 0x000000000093460e _start (/opt/compiler-explorer/clang-assertions-trunk-20240408/bin/ld.lld+0x93460e) clang: error: unable to execute command: Aborted (core dumped) clang: error: linker command failed due to signal (use -v to see invocation) Compiler returned: 254 ``` Godbolt: https://godbolt.org/z/6K4j7W759 Works in LLVM 17. Found via fuzzer Possibly related to RISC-V failure: #88057
e-kud commented 4 months ago

No need in -flto. It's enough to enforce global-isel, e.g. -finstrument-functions -mllvm -global-isel

@llvm.returnaddress is not supported expectedly when we try to select

define dso_local i32 @main()  {
entry:
  %0 = call ptr @llvm.returnaddress(i32 0)
  call void @__cyg_profile_func_enter(ptr @main, ptr %0)
  %1 = call ptr @llvm.returnaddress(i32 0)
  call void @__cyg_profile_func_exit(ptr @main, ptr %1)
  ret i32 0
}

AArch64 is able to compile but it keeps @llvm.returnaddress through all the selection process. It seems that we need to introduce a new opcode. However we don't need to map it into ISD::RETURNADDR as most targets have a custom lowering for it.