Skip to content

Link with ld.gold by default #29974

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

Merged
merged 1 commit into from
Dec 30, 2015
Merged
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
2 changes: 2 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
"explicitly enable the cfg(debug_assertions) directive"),
inline_threshold: Option<usize> = (None, parse_opt_uint,
"set the inlining threshold for"),
disable_gold: bool = (false, parse_bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is… a pretty confusing option (both its description and behaviour).

We already have -C linker which possibly swaps out gcc for something else and now we pass a special flag into linker assuming it is gcc-compatible (in absence of this option) without checking if specified linker is indeed compatible. That might make -C linker harder to use.

Maybe it would be better to have -C linker=gcc which overrides any default/dubious flags we might add?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have -C linker which possibly swaps out gcc for something else and now we pass a special flag into linker assuming it is gcc-compatible (in absence of this option) without checking if specified linker is indeed compatible. That might make -C linker harder to use

As far as I can tell that is an existing problem. Even if you override the linker, rustc assumes it takes either gcc or link.exe style arguments, depending on platform. IOW I tihnk -C linker is only good for specifying a different path to the expected linker, not for using a completely different 'style' of linker.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super worried about random codegen options being deprecated at some point, an option like this is pretty flavorful and likely to only be a one-time use case (if ever), so if we in the future just hide this and make it ignored by default then it's probably not too bad.

It may be the case that gcc -fuse-ld=gold -fuse-ld=not-gold (or some similar incantation) also works, so we could just be sure to allow that pattern, but I don't know if it exists. Either way this seems fine to me for now.

"disable use of the ld.gold linker"),
}


Expand Down
4 changes: 4 additions & 0 deletions src/librustc_trans/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,10 @@ fn link_args(cmd: &mut Linker,
cmd.args(&rpath::get_rpath_flags(&mut rpath_config));
}

// Use the gold linker if possible instead of ld. It is much
// faster.
cmd.try_gold_linker();

// Finally add all the linker arguments provided on the command line along
// with any #[link_args] attributes found inside the crate
if let Some(ref args) = sess.opts.cg.link_args {
Expand Down
51 changes: 51 additions & 0 deletions src/librustc_trans/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::env;
use std::ffi::OsString;
use std::fs::{self, File};
use std::io::{self, BufWriter};
Expand Down Expand Up @@ -56,6 +57,7 @@ pub trait Linker {
fn no_whole_archives(&mut self);
fn export_symbols(&mut self, sess: &Session, trans: &CrateTranslation,
tmpdir: &Path);
fn try_gold_linker(&mut self);
}

pub struct GnuLinker<'a> {
Expand Down Expand Up @@ -199,6 +201,53 @@ impl<'a> Linker for GnuLinker<'a> {
fn export_symbols(&mut self, _: &Session, _: &CrateTranslation, _: &Path) {
// noop, visibility in object files takes care of this
}

fn try_gold_linker(&mut self) {
// Only use gold under specific conditions that we know work

let gold_exists = match env::var_os("PATH") {
Some(ref env_path) => {
env::split_paths(env_path).any(|mut p| {
p.push("ld.gold");
p.exists()
})
}
None => false
};
let host_is_linux = cfg!(target_os = "linux");
// Defensively prevent trying to use gold for bogus cross-targets.
let target_is_host_compatible = {
let host_os_is_target_os = self.sess.target.target.target_os == env::consts::OS;
let host_arch_is_target_arch = self.sess.target.target.arch == env::consts::ARCH;
// Support x86_64->i686 and reverse
let host_and_target_are_x86ish =
(self.sess.target.target.arch == "x86" ||
self.sess.target.target.arch == "x86_64") &&
(env::consts::ARCH == "x86" ||
env::consts::ARCH == "x86_64");
host_os_is_target_os && (host_arch_is_target_arch || host_and_target_are_x86ish)
};
// We have strong confidence that x86 works, but not much
// visibility into other architectures.
let target_works_with_gold =
self.sess.target.target.arch == "x86" ||
self.sess.target.target.arch == "x86_64";
let opt_out = self.sess.opts.cg.disable_gold;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d rather to just have a regular if here: if self.sess.opts.cg.disable_gold { return; }


let can_use_gold =
gold_exists &&
host_is_linux &&
target_is_host_compatible &&
target_works_with_gold &&
!opt_out;

if can_use_gold {
info!("linking with ld.gold");
self.cmd.arg("-fuse-ld=gold");
} else {
info!("linking with ld");
}
}
}

pub struct MsvcLinker<'a> {
Expand Down Expand Up @@ -358,4 +407,6 @@ impl<'a> Linker for MsvcLinker<'a> {
arg.push(path);
self.cmd.arg(&arg);
}

fn try_gold_linker(&mut self) {}
}