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

LTO plugin uses wrong ABI for LTO objects on riscv #50591

Open
llvmbot opened this issue Jul 28, 2021 · 8 comments
Open

LTO plugin uses wrong ABI for LTO objects on riscv #50591

llvmbot opened this issue Jul 28, 2021 · 8 comments
Labels
ABI Application Binary Interface backend:RISC-V bugzilla Issues migrated from bugzilla confirmed Verified by a second party llvm-tools All llvm tools that do not have corresponding tag LTO Link time optimization (regular/full LTO or ThinLTO)

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 28, 2021

Bugzilla Link 51247
Version 12.0
OS Linux
Reporter LLVM Bugzilla Contributor

Extended Description

It looks like the LTO plugin fails to properly set the target-abi for the LTO object.

$ clang -flto hello.c -v
clang version 12.0.1
Target: riscv64-suse-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib64/gcc/riscv64-suse-linux/10
Found candidate GCC installation: /usr/bin/../lib64/gcc/riscv64-suse-linux/11
Found candidate GCC installation: /usr/bin/../lib64/gcc/riscv64-suse-linux/9
Found candidate GCC installation: /usr/lib64/gcc/riscv64-suse-linux/10
Found candidate GCC installation: /usr/lib64/gcc/riscv64-suse-linux/11
Found candidate GCC installation: /usr/lib64/gcc/riscv64-suse-linux/9
Selected GCC installation: /usr/bin/../lib64/gcc/riscv64-suse-linux/11
"/usr/bin/clang-12.0.1" -cc1 -triple riscv64-suse-linux-gnu -emit-llvm-bc -flto -flto-unit -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello.c -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -target-feature +m -target-feature +a -target-feature +f -target-feature +d -target-feature +c -target-feature +relax -target-feature -save-restore -target-abi lp64d -msmall-data-limit 8 -fno-split-dwarf-inlining -debugger-tuning=gdb -v -resource-dir /usr/lib64/clang/12.0.1 -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/12.0.1/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /suse/schwab/src/test -ferror-limit 19 -fno-signed-char -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -o /tmp/hello-229842.o -x c hello.c
clang -cc1 version 12.0.1 based upon LLVM 12.0.1 default target riscv64-suse-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib64/clang/12.0.1/include
/usr/include
End of search list.
"/usr/bin/ld" -z relro --hash-style=gnu --hash-style=both --build-id --enable-new-dtags --eh-frame-hdr -m elf64lriscv -dynamic-linker /lib/ld-linux-riscv64-lp64d.so.1 -o a.out /usr/bin/../lib64/gcc/riscv64-suse-linux/11/../../../../lib64/crt1.o /usr/bin/../lib64/gcc/riscv64-suse-linux/11/crti.o /usr/bin/../lib64/gcc/riscv64-suse-linux/11/crtbegin.o -L/usr/bin/../lib64/gcc/riscv64-suse-linux/11 -L/usr/bin/../lib64/gcc/riscv64-suse-linux/11/../../../../lib64 -L/usr/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib64/lp64d -L/usr/lib64/lp64d -L/usr/bin/../lib64/gcc/riscv64-suse-linux/11/../../../../riscv64-suse-linux/lib -L/usr/bin/../lib64/gcc/riscv64-suse-linux/11/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib -plugin /usr/bin/../lib64/LLVMgold.so /tmp/hello-229842.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib64/gcc/riscv64-suse-linux/11/crtend.o /usr/bin/../lib64/gcc/riscv64-suse-linux/11/crtn.o
/usr/bin/ld: /tmp/lto-llvm-5c94e7.o: can't link soft-float modules with double-float modules
/usr/bin/ld: failed to merge target specific data of file /tmp/lto-llvm-5c94e7.o
clang-12.0: error: linker command failed with exit code 1 (use -v to see invocation)

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@r-value
Copy link

r-value commented Feb 7, 2022

Encountered in the same issue.

Minimal recurrence in Arch Linux RISC-V nspawn container:

[root@nspawn ~]# cat > test.c
int main() { return 0; }
[root@nspawn ~]# clang -march=rv64gc -mabi=lp64d -flto test.c -o test -v
clang version 13.0.0
Target: riscv64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/sbin
Found candidate GCC installation: /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0
Selected GCC installation: /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0
 "/usr/bin/clang-13" -cc1 -triple riscv64-unknown-linux-gnu -emit-llvm-bc -flto -flto-unit -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -target-feature +m -target-feature +a -target-feature +f -target-feature +d -target-feature +c -target-feature +relax -target-feature -save-restore -target-abi lp64d -msmall-data-limit 8 -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/root -resource-dir /usr/lib/clang/13.0.0 -internal-isystem /usr/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/../../../../riscv64-unknown-linux-gnu/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir=/root -ferror-limit 19 -stack-protector 2 -fno-signed-char -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -o /tmp/test-50f8f6.o -x c test.c
clang -cc1 version 13.0.0 based upon LLVM 13.0.0 default target riscv64-unknown-linux-gnu
ignoring nonexistent directory "/usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/../../../../riscv64-unknown-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/clang/13.0.0/include
 /usr/local/include
 /usr/include
End of search list.
 "/usr/sbin/ld" -pie --eh-frame-hdr -m elf64lriscv -dynamic-linker /lib/ld-linux-riscv64-lp64d.so.1 -o test /usr/bin/../lib/Scrt1.o /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crti.o /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crtbeginS.o -L/usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0 -L/usr/bin/../lib -L/lib -L/usr/lib -plugin /usr/bin/../lib/LLVMgold.so /tmp/test-50f8f6.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crtendS.o /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crtn.o
/usr/sbin/ld: /tmp/lto-llvm-99c7f9.o: can't link soft-float modules with double-float modules
/usr/sbin/ld: failed to merge target specific data of file /tmp/lto-llvm-99c7f9.o
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)

It seems that clang can't compile even the simplest C program with an empty main function.

It does work with LTO disabled:

[root@nspawn ~]# clang -march=rv64gc -mabi=lp64d -fno-lto test.c -o test -v
clang version 13.0.0
Target: riscv64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/sbin
Found candidate GCC installation: /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0
Selected GCC installation: /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0
 "/usr/bin/clang-13" -cc1 -triple riscv64-unknown-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -target-feature +m -target-feature +a -target-feature +f -target-feature +d -target-feature +c -target-feature +relax -target-feature -save-restore -target-abi lp64d -msmall-data-limit 8 -debugger-tuning=gdb -v -fcoverage-compilation-dir=/root -resource-dir /usr/lib/clang/13.0.0 -internal-isystem /usr/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/../../../../riscv64-unknown-linux-gnu/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir=/root -ferror-limit 19 -stack-protector 2 -fno-signed-char -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -o /tmp/test-f03fed.o -x c test.c
clang -cc1 version 13.0.0 based upon LLVM 13.0.0 default target riscv64-unknown-linux-gnu
ignoring nonexistent directory "/usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/../../../../riscv64-unknown-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/clang/13.0.0/include
 /usr/local/include
 /usr/include
End of search list.
 "/usr/sbin/ld" -pie --eh-frame-hdr -m elf64lriscv -dynamic-linker /lib/ld-linux-riscv64-lp64d.so.1 -o test /usr/bin/../lib/Scrt1.o /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crti.o /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crtbeginS.o -L/usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0 -L/usr/bin/../lib -L/lib -L/usr/lib /tmp/test-f03fed.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crtendS.o /usr/sbin/../lib/gcc/riscv64-unknown-linux-gnu/11.1.0/crtn.o

@llvmbot
Copy link
Collaborator Author

llvmbot commented Feb 7, 2022

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

@kito-cheng
Copy link
Member

@zakk0610 did you mind take a look for this?

@jrtc27
Copy link
Collaborator

jrtc27 commented Feb 7, 2022

This is a long-standing issue, the patches got abandoned before they were finished...

@zakk0610
Copy link
Member

zakk0610 commented Feb 8, 2022

You can try to give -plugin-opt=target-abi=lp64d to work around this issue.

I'm not sure why LTO had enabled for RISC-V in LLVM 12.0.

@XieJiSS
Copy link

XieJiSS commented Feb 19, 2022

Note that the work around should be -Wl,-plugin-opt=-target-abi=lp64d (there's an additional - before target).

Ref: https://lists.llvm.org/pipermail/llvm-dev/2020-January/137951.html

@luxufan
Copy link
Contributor

luxufan commented Mar 8, 2022

It was caused by that when use codegen module to generate object code, the -target-abi xxx attribute in llvm IR file was ignored.

Related patch: https://reviews.llvm.org/D121183

luxufan pushed a commit that referenced this issue Mar 23, 2022
…ibute

In the past, when construct RISCVAsmBackend, MCTargetOptions.ABIName would be passed and stored in RISCVAsmBackend.
But MCTargetOptions.ABIName can only be specified by -target-abi xxx in command line, if the .ll file has target-abi attribute, the codegen module will ignore it. And the generated object file would have incorrect EFlags value.

#50591 also caused by this problem.

This patch override the AsmPrinter::emitFunctionEntryLabel function and use it to set the target abi value that get from .ll file's target-abi attribute. And storing the target-abi in RISCVTargetStreamer instead of RISCVAsmBackend.

Differential Revision: https://reviews.llvm.org/D121183
unicornx added a commit to aosp-riscv/platform_build_soong that referenced this issue May 20, 2022
build report error for following BP config
```
 sanitize: {
	 cfi: true,
}
```
build report error for upon BP config

> Hard-float 'd' ABI can't be used for a target that doesn't support
> the D instruction set extension (ignoring target-abi)
> ld.lld: error: lto.tmp: cannot link object files with different
> floating-point ABI
> clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

refer to llvm/llvm-project#50591, it is
an existing llvm issue.

Before we upgrade the clang version, we provide this workaround to
pass build.
melonedo pushed a commit to melonedo/llvm-project that referenced this issue Aug 8, 2022
…ibute

In the past, when construct RISCVAsmBackend, MCTargetOptions.ABIName would be passed and stored in RISCVAsmBackend.
But MCTargetOptions.ABIName can only be specified by -target-abi xxx in command line, if the .ll file has target-abi attribute, the codegen module will ignore it. And the generated object file would have incorrect EFlags value.

llvm/llvm-project#50591 also caused by this problem.

This patch override the AsmPrinter::emitFunctionEntryLabel function and use it to set the target abi value that get from .ll file's target-abi attribute. And storing the target-abi in RISCVTargetStreamer instead of RISCVAsmBackend.

Differential Revision: https://reviews.llvm.org/D121183
mem-frob pushed a commit to draperlaboratory/hope-llvm-project that referenced this issue Oct 7, 2022
…ibute

In the past, when construct RISCVAsmBackend, MCTargetOptions.ABIName would be passed and stored in RISCVAsmBackend.
But MCTargetOptions.ABIName can only be specified by -target-abi xxx in command line, if the .ll file has target-abi attribute, the codegen module will ignore it. And the generated object file would have incorrect EFlags value.

llvm/llvm-project#50591 also caused by this problem.

This patch override the AsmPrinter::emitFunctionEntryLabel function and use it to set the target abi value that get from .ll file's target-abi attribute. And storing the target-abi in RISCVTargetStreamer instead of RISCVAsmBackend.

Differential Revision: https://reviews.llvm.org/D121183
@thesamesam thesamesam added the LTO Link time optimization (regular/full LTO or ThinLTO) label Nov 5, 2022
@EugeneZelenko EugeneZelenko added the ABI Application Binary Interface label Jun 14, 2023
@ilovepi
Copy link
Contributor

ilovepi commented Nov 28, 2023

I've been investigating some related issues on RISC-V for a while (65090), and would like to get this solved.

Looking at the issue tracker, there seems to be a number of related issues:

I also see a few related posts on discourse https://discourse.llvm.org/t/myterious-soft-float-output-in-lto-cache/70753 and especially https://discourse.llvm.org/t/encode-target-abi-into-llvm-bitcode-for-lto/54116

There are also some earlier attempts to fix the problem, like https://reviews.llvm.org/D132843 and https://reviews.llvm.org/D71387, though it isn't clear to me what the status of those are given that the authors have moved on from RISC-V related work.

Within the issues and discussions, I found several comments from maintainers that note that this is a known problem and provide a work around.

However, a work around is normally a temporary solution, so I'd like to understand what the problem is in more detail, so we can decide on how to solve it. Or in the case that we do know, what steps are left to have a working solution before the next major release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ABI Application Binary Interface backend:RISC-V bugzilla Issues migrated from bugzilla confirmed Verified by a second party llvm-tools All llvm tools that do not have corresponding tag LTO Link time optimization (regular/full LTO or ThinLTO)
Projects
None yet
Development

No branches or pull requests