llvm / llvm-project

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

[RISC-V] LLVM ERROR: unable to legalize instruction with `-fglobal-isel -finstrument-functions -flto -fuse-ld=lld` #88057

Open patrick-rivos opened 3 months ago

patrick-rivos commented 3 months ago

Testcase:

int main() {}

Backtrace:

> /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/clang -fglobal-isel -finstrument-functions -flto -fuse-ld=lld red.c -o rv64gcv.out
clang: warning: -fglobal-isel support for the 'riscv64' architecture is incomplete [-Wglobal-isel]
LLVM ERROR: unable to legalize instruction: %2:_(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: /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld --sysroot=/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot -z relro --hash-style=gnu --eh-frame-hdr -m elf64lriscv -X -pie -dynamic-linker /lib/ld-linux-riscv64-lp64d.so.1 -o rv64gcv.out /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/usr/lib64/lp64d/Scrt1.o /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d/crti.o /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d/crtbeginS.o -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../lib/riscv64-unknown-linux-gnu -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/lib/clang/19/lib/riscv64-unknown-linux-gnu -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/../lib64 -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib64/lp64d -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/usr/lib64/lp64d -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib -plugin-opt=mcpu=generic-rv64 -plugin-opt=-global-isel=1 /scratch/tmp/red-42c349.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d/crtendS.o /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d/crtn.o
1.      Running pass 'Function Pass Manager' on module 'ld-temp.o'.
2.      Running pass 'Legalizer' on function '@main'
 #0 0x00005a56a3adf630 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x6a7630)
 #1 0x00005a56a3adca3f llvm::sys::RunSignalHandlers() (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x6a4a3f)
 #2 0x00005a56a3adcb95 SignalHandler(int) Signals.cpp:0:0
 #3 0x00007ef751842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007ef7518969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007ef7518969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x00007ef7518969fc pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x00007ef751842476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x00007ef7518287f3 abort ./stdlib/abort.c:81:7
 #9 0x00005a56a399e63c llvm::UniqueStringSaver::save(llvm::StringRef) (.cold) StringSaver.cpp:0:0
#10 0x00005a56a4343f22 reportGISelDiagnostic(llvm::DiagnosticSeverity, llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, llvm::MachineOptimizationRemarkMissed&) Utils.cpp:0:0
#11 0x00005a56a4345ee0 llvm::reportGISelFailure(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, char const*, llvm::StringRef, llvm::MachineInstr const&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0xf0dee0)
#12 0x00005a56a42cb7fa llvm::Legalizer::runOnMachineFunction(llvm::MachineFunction&) (.part.0) Legalizer.cpp:0:0
#13 0x00005a56a4a02007 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0
#14 0x00005a56a6613fc6 llvm::FPPassManager::runOnFunction(llvm::Function&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x31dbfc6)
#15 0x00005a56a6614219 llvm::FPPassManager::runOnModule(llvm::Module&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x31dc219)
#16 0x00005a56a6614b85 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x31dcb85)
#17 0x00005a56a473ec2a 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
#18 0x00005a56a47409d9 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&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x13089d9)
#19 0x00005a56a4731b0e llvm::lto::LTO::runRegularLTO(std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x12f9b0e)
#20 0x00005a56a4732063 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&)>) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x12fa063)
#21 0x00005a56a3cbff60 lld::elf::BitcodeCompiler::compile() (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x887f60)
#22 0x00005a56a3c16c27 void lld::elf::LinkerDriver::compileBitcodeFiles<llvm::object::ELFType<(llvm::endianness)1, true>>(bool) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x7dec27)
#23 0x00005a56a3c32161 void lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::endianness)1, true>>(llvm::opt::InputArgList&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x7fa161)
#24 0x00005a56a3c381c5 lld::elf::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x8001c5)
#25 0x00005a56a3c3953a lld::elf::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x80153a)
#26 0x00005a56a3b17a6e lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x6dfa6e)
#27 0x00005a56a3a53aa6 lld_main(int, char**, llvm::ToolContext const&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x61baa6)
#28 0x00005a56a39a6ebb main (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x56eebb)
#29 0x00007ef751829d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#30 0x00007ef751829e40 call_init ./csu/../csu/libc-start.c:128:20
#31 0x00007ef751829e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#32 0x00005a56a3a53305 _start (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x61b305)
clang: error: unable to execute command: Aborted (core dumped)
clang: error: linker command failed due to signal (use -v to see invocation)

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

Found via fuzzer

X86 also has an issue with these flags, filing that separately since it triggers a different assert/backtrace.

patrick-rivos commented 3 months ago

-fuse-ld=lld is not relevant to the issue: https://godbolt.org/z/n7rn1beEd

llvmbot commented 3 months ago

@llvm/issue-subscribers-backend-risc-v

Author: Patrick O'Neill (patrick-rivos)

Testcase: ```c int main() {} ``` Backtrace: ``` > /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/clang -fglobal-isel -finstrument-functions -flto -fuse-ld=lld red.c -o rv64gcv.out clang: warning: -fglobal-isel support for the 'riscv64' architecture is incomplete [-Wglobal-isel] LLVM ERROR: unable to legalize instruction: %2:_(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: /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld --sysroot=/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot -z relro --hash-style=gnu --eh-frame-hdr -m elf64lriscv -X -pie -dynamic-linker /lib/ld-linux-riscv64-lp64d.so.1 -o rv64gcv.out /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/usr/lib64/lp64d/Scrt1.o /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d/crti.o /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d/crtbeginS.o -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../lib/riscv64-unknown-linux-gnu -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/lib/clang/19/lib/riscv64-unknown-linux-gnu -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/../lib64 -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib64/lp64d -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/usr/lib64/lp64d -L/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib -plugin-opt=mcpu=generic-rv64 -plugin-opt=-global-isel=1 /scratch/tmp/red-42c349.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d/crtendS.o /scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/../sysroot/lib/gcc/riscv64-unknown-linux-gnu/14.0.1/lib64/lp64d/crtn.o 1. Running pass 'Function Pass Manager' on module 'ld-temp.o'. 2. Running pass 'Legalizer' on function '@main' #0 0x00005a56a3adf630 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x6a7630) #1 0x00005a56a3adca3f llvm::sys::RunSignalHandlers() (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x6a4a3f) #2 0x00005a56a3adcb95 SignalHandler(int) Signals.cpp:0:0 #3 0x00007ef751842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520) #4 0x00007ef7518969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76 #5 0x00007ef7518969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10 #6 0x00007ef7518969fc pthread_kill ./nptl/pthread_kill.c:89:10 #7 0x00007ef751842476 gsignal ./signal/../sysdeps/posix/raise.c:27:6 #8 0x00007ef7518287f3 abort ./stdlib/abort.c:81:7 #9 0x00005a56a399e63c llvm::UniqueStringSaver::save(llvm::StringRef) (.cold) StringSaver.cpp:0:0 #10 0x00005a56a4343f22 reportGISelDiagnostic(llvm::DiagnosticSeverity, llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, llvm::MachineOptimizationRemarkMissed&) Utils.cpp:0:0 #11 0x00005a56a4345ee0 llvm::reportGISelFailure(llvm::MachineFunction&, llvm::TargetPassConfig const&, llvm::MachineOptimizationRemarkEmitter&, char const*, llvm::StringRef, llvm::MachineInstr const&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0xf0dee0) #12 0x00005a56a42cb7fa llvm::Legalizer::runOnMachineFunction(llvm::MachineFunction&) (.part.0) Legalizer.cpp:0:0 #13 0x00005a56a4a02007 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (.part.0) MachineFunctionPass.cpp:0:0 #14 0x00005a56a6613fc6 llvm::FPPassManager::runOnFunction(llvm::Function&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x31dbfc6) #15 0x00005a56a6614219 llvm::FPPassManager::runOnModule(llvm::Module&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x31dc219) #16 0x00005a56a6614b85 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x31dcb85) #17 0x00005a56a473ec2a 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 #18 0x00005a56a47409d9 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&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x13089d9) #19 0x00005a56a4731b0e llvm::lto::LTO::runRegularLTO(std::function<llvm::Expected<std::unique_ptr<llvm::CachedFileStream, std::default_delete<llvm::CachedFileStream>>> (unsigned int, llvm::Twine const&)>) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x12f9b0e) #20 0x00005a56a4732063 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&)>) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x12fa063) #21 0x00005a56a3cbff60 lld::elf::BitcodeCompiler::compile() (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x887f60) #22 0x00005a56a3c16c27 void lld::elf::LinkerDriver::compileBitcodeFiles<llvm::object::ELFType<(llvm::endianness)1, true>>(bool) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x7dec27) #23 0x00005a56a3c32161 void lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::endianness)1, true>>(llvm::opt::InputArgList&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x7fa161) #24 0x00005a56a3c381c5 lld::elf::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x8001c5) #25 0x00005a56a3c3953a lld::elf::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x80153a) #26 0x00005a56a3b17a6e lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x6dfa6e) #27 0x00005a56a3a53aa6 lld_main(int, char**, llvm::ToolContext const&) (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x61baa6) #28 0x00005a56a39a6ebb main (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x56eebb) #29 0x00007ef751829d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16 #30 0x00007ef751829e40 call_init ./csu/../csu/libc-start.c:128:20 #31 0x00007ef751829e40 __libc_start_main ./csu/../csu/libc-start.c:379:5 #32 0x00005a56a3a53305 _start (/scratch/tc-testing/tc-apr-4/build-rv64gcv/build-llvm-linux/bin/ld.lld+0x61b305) clang: error: unable to execute command: Aborted (core dumped) clang: error: linker command failed due to signal (use -v to see invocation) ``` Godbolt: https://godbolt.org/z/s83eaxrW4 Works in LLVM 17. Found via fuzzer X86 also has an issue with these flags, filing that separately since it triggers a different assert/backtrace.