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

Support static linking LLVM with ThinLTO #69081

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/bootstrap/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,33 @@ impl<'a> Builder<'a> {
}
}

// If we're statically linking in an LLVM compiled for ThinLTO,
// configure the linker accordingly.
if Mode::Rustc == mode && self.config.llvm_thin_lto && !self.config.llvm_link_shared {
if target.contains("msvc") {
// Here we assume the linker is lld-link.exe.
rustflags.arg(&format!("-Clink-arg=/opt:lldtojobs={}", self.jobs()));
} else {
// Here we assume the linker is clang and that lld is available.
// If not, there'll be linker errors.
rustflags.arg("-Clink-arg=-fuse-ld=lld");
rustflags.arg("-Clink-arg=-flto=thin");

// Copy the optimization flags LLVM uses for its Release and
// RelWithDebugInfo builds.
if self.config.llvm_optimize {
if self.config.llvm_release_debuginfo {
rustflags.arg("-Clink-arg=-O2");
} else {
rustflags.arg("-Clink-arg=-O3");
}
}

// Make LLD respect the `-j` option.
rustflags.arg(&format!("-Clink-arg=-Wl,--thinlto-jobs={}", self.jobs()));
}
}

// This tells Cargo (and in turn, rustc) to output more complete
// dependency information. Most importantly for rustbuild, this
// includes sysroot artifacts, like libstd, which means that we don't
Expand Down
5 changes: 4 additions & 1 deletion src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,12 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: Interne
let file = compiler_file(builder, builder.cxx(target).unwrap(), target, "libstdc++.a");
cargo.env("LLVM_STATIC_STDCPP", file);
}
if builder.config.llvm_link_shared || builder.config.llvm_thin_lto {
if builder.config.llvm_link_shared {
cargo.env("LLVM_LINK_SHARED", "1");
}
if builder.config.llvm_thin_lto {
cargo.env("LLVM_LTO", "thin");
}
if builder.config.llvm_use_libcxx {
cargo.env("LLVM_USE_LIBCXX", "1");
}
Expand Down
1 change: 1 addition & 0 deletions src/ci/docker/dist-x86_64-linux/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ ENV RUST_CONFIGURE_ARGS \
--set target.x86_64-unknown-linux-gnu.linker=clang \
--set target.x86_64-unknown-linux-gnu.ar=/rustroot/bin/llvm-ar \
--set target.x86_64-unknown-linux-gnu.ranlib=/rustroot/bin/llvm-ranlib \
--set llvm.link-shared=true \
--set llvm.thin-lto=true \
--set rust.jemalloc
ENV SCRIPT python2.7 ../x.py dist --host $HOSTS --target $HOSTS
Expand Down
4 changes: 2 additions & 2 deletions src/ci/docker/x86_64-gnu-llvm-7/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
COPY scripts/sccache.sh /scripts/
RUN sh /scripts/sccache.sh

# using llvm-link-shared due to libffi issues -- see #34486
# using llvm.link-shared due to libffi issues -- see #34486
ENV RUST_CONFIGURE_ARGS \
--build=x86_64-unknown-linux-gnu \
--llvm-root=/usr/lib/llvm-7 \
--enable-llvm-link-shared \
--set llvm.link-shared=true \
--set rust.thin-lto-import-instr-limit=10

ENV SCRIPT python2.7 ../x.py test src/tools/tidy && python2.7 ../x.py test
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_llvm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ fn main() {
}

if flag.starts_with("-flto") {
// Handled below.
continue;
}

Expand All @@ -153,6 +154,10 @@ fn main() {
cfg.define("NDEBUG", None);
}

if let Ok(kind) = env::var("LLVM_LTO") {
cfg.flag(&format!("-flto={}", kind));
}

build_helper::rerun_if_changed_anything_in_dir(Path::new("../rustllvm"));
cfg.file("../rustllvm/PassWrapper.cpp")
.file("../rustllvm/RustWrapper.cpp")
Expand Down