cxx-cmake-example icon indicating copy to clipboard operation
cxx-cmake-example copied to clipboard

--emit=llvm-ir may be required for LTO

Open w0xel opened this issue 3 years ago • 3 comments

I only got LTO to inline the rust functions by adding --emit=llvm-ir to the RUSTFLAGS.

Not sure if this is a bug in llvm/rust, but I had a rather long journey of: trying in various containers, compiling rust myself with patches and external llvm, installing a archlinux-vm to try their custom-built rustc without success of getting the cross language LTO to work. I now wanted to debug this by looking at the IR, but just activating it with --emit=llvm-ir finally made it inline the function. I have no idea why the -Clinker-plugin-lto flag is not enough here, but this info might help other people trying to get this to work.

My setup is currently on Ubuntu 21.04 with rust stable from rustup and llvm-12 from the llvm repo:

% rustc --version --verbose
rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-unknown-linux-gnu
release: 1.53.0
LLVM version: 12.0.1
% clang --version
Ubuntu clang version 12.0.1-++20210630032550+fed41342a82f-1~exp1~20210630133300.126
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

w0xel avatar Jul 17 '21 21:07 w0xel

I'm running into this issue too. Adding what you suggested got me past 80% (Building main.cpp.o) but it still fails to link

[ 40%] Building CXX object rust_part/CMakeFiles/rust_part.dir/rust_part.cpp.o
[ 60%] Linking CXX static library librust_part.a
[ 60%] Built target rust_part
[ 80%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
ld.lld: error: undefined symbol: main
>>> referenced by /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o:(_start)
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:99: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

TheBestJohn avatar Aug 13 '21 22:08 TheBestJohn

Hi sorry for the late reply, I added a docker file to the repo, can you try that and see if you can reproduce the results?

XiangpengHao avatar Jan 16 '22 19:01 XiangpengHao

the other half of my issue wasn't to do with cxx, it was a compound issue while cross compiling. I will be checking out the docker file in near future though, thank you. I'll attempt to report back when I do.

TheBestJohn avatar Feb 03 '22 16:02 TheBestJohn