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 does not work with Clang #6999

Open
reynoldsbd opened this issue Dec 13, 2023 · 3 comments
Open

LTO does not work with Clang #6999

reynoldsbd opened this issue Dec 13, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@reynoldsbd
Copy link
Member

Describe the bug

When compiling a project using Clang, I am unable to use link-time optimization (LTO) due to the absence of /usr/lib/LLVMgold.so.

LLVMgold.so is a a linker plugin which is part of the LLVM/Clang project itself. It acts as "glue" between the Clang compiler driver and GNU gold, which is used to perform the actual linking when LTO is enabled. More information here:

https://llvm.org/docs/GoldPlugin.html

LLVMgold.so appears to be missing from Mariner's clang package. Based on my reading of the spec files and LLVM's CMake files, I believe this is because LLVM_BINUTILS_INCDIR is not being specified at the time LLVM/Clang is built.

Proposed fix:

  1. If it isn't already, list the binutils package (which provides GNU gold) as a build-time dependency of the clang package (or llvm package, not sure which?)
  2. Update the build spec for clang and/or llvm to specify LLVM_BINUTILS_INCDIR on the CMake command line

To Reproduce

Steps to reproduce the behavior:

$ cat <<EOF > test_lto.c
> #include <stdio.h>
> int main(void) {
>   printf("hello, with lto\n");
>   return 0;
> }
> EOF
$ clang -flto test_lto.c -o test_lto
/usr/bin/ld: /usr/bin/../lib/LLVMgold.so: error loading plugin: /usr/bin/../lib/LLVMgold.so: cannot open shared object file: No such file or directory
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)

Expected behavior

Clang should compile and link the program without error, using LTO.

Next steps

Please advise whether the proposed fix above makes sense, or whether there is some additional context as to why this does not work.

If this seems okay, I am happy to submit a PR with these changes.

@reynoldsbd reynoldsbd added the bug Something isn't working label Dec 13, 2023
@anphel31
Copy link
Member

I don't see any reason we cannot support this. I've opened #8582 to fix this on our 3.0 branch. I found that setting LLVM_BINUTILS_INCDIR in llvm.spec is enough to get /usr/lib/LLVMgold.so. Building clang with the same has no effect, and warns with Manually-specified variables were not used by the project: LLVM_BINUTILS_INCDIR. The repro steps worked well after installing the updated llvm on my test machine (I didn't need to rebuild/update clang).
Let me know if you need this on Mariner 2.0 - we can probably backport it as well.

@reynoldsbd
Copy link
Member Author

Thanks for taking a look! Our team is still using 2.0 so a backport would be much appreciated.

@JoD
Copy link

JoD commented May 2, 2024

Got the same issue on a fresh Ubuntu 24.04 install. No idea how to fix it (can't find llvm.spec or LLVMgold.so. Any idea for a workaround here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants