azurelinux icon indicating copy to clipboard operation
azurelinux copied to clipboard

LTO does not work with Clang

Open reynoldsbd opened this issue 1 year ago • 3 comments

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 avatar Dec 13 '23 18:12 reynoldsbd

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.

anphel31 avatar Mar 27 '24 22:03 anphel31

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

reynoldsbd avatar Mar 30 '24 13:03 reynoldsbd

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?

JoD avatar May 02 '24 05:05 JoD