Skip to content

Commit a1aeb60

Browse files
committed
Auto merge of #51207 - michaelwoerister:build-lld-on-linux, r=<try>
[experimental]: Build LLVM with ThinLTO enabled This is a work in progress. Let's see how it goes.
2 parents 4f99f37 + 55aaf63 commit a1aeb60

File tree

21 files changed

+197
-24
lines changed

21 files changed

+197
-24
lines changed

src/bootstrap/builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use std::fs;
1717
use std::hash::Hash;
1818
use std::ops::Deref;
1919
use std::path::{Path, PathBuf};
20-
use std::process::Command;
2120
use std::time::{Instant, Duration};
2221
use std::collections::HashMap;
2322

23+
use build_helper::command_ext::Command;
2424
use compile;
2525
use install;
2626
use dist;
@@ -658,7 +658,7 @@ impl<'a> Builder<'a> {
658658
if let Some(ref error_format) = self.config.rustc_error_format {
659659
cargo.env("RUSTC_ERROR_FORMAT", error_format);
660660
}
661-
if cmd != "build" && cmd != "check" && want_rustdoc {
661+
if cmd != "build" && cmd != "check" && cmd != "rustc" && want_rustdoc {
662662
cargo.env("RUSTDOC_LIBDIR", self.rustc_libdir(self.compiler(2, self.config.build)));
663663
}
664664

@@ -804,7 +804,7 @@ impl<'a> Builder<'a> {
804804
}
805805
}
806806

807-
if cmd == "build" && mode == Mode::Libstd
807+
if (cmd == "build" || cmd == "rustc") && mode == Mode::Libstd
808808
&& self.config.extended && compiler.is_final_stage(self)
809809
{
810810
cargo.env("RUSTC_SAVE_ANALYSIS", "api".to_string());

src/bootstrap/cc_detect.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
use std::collections::HashSet;
3535
use std::{env, iter};
3636
use std::path::{Path, PathBuf};
37-
use std::process::Command;
3837

3938
use build_helper::output;
39+
use build_helper::command_ext::Command;
4040
use cc;
4141

4242
use Build;
@@ -158,7 +158,9 @@ fn set_compiler(cfg: &mut cc::Build,
158158
return
159159
}
160160

161-
let output = output(c.to_command().arg("--version"));
161+
162+
let output = output(Command::from_std_command(c.to_command())
163+
.arg("--version"));
162164
let i = match output.find(" 4.") {
163165
Some(i) => i,
164166
None => return,

src/bootstrap/channel.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
//! print out as part of its version information.
1717
1818
use std::path::Path;
19-
use std::process::Command;
2019

2120
use build_helper::output;
21+
use build_helper::command_ext::Command;
2222

2323
use Build;
2424
use config::Config;

src/bootstrap/compile.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ use std::fs::{self, File};
2222
use std::io::BufReader;
2323
use std::io::prelude::*;
2424
use std::path::{Path, PathBuf};
25-
use std::process::{Command, Stdio};
25+
use std::process::Stdio;
2626
use std::str;
2727
use std::cmp::min;
2828

2929
use build_helper::{output, mtime, up_to_date};
30+
use build_helper::command_ext::Command;
3031
use filetime::FileTime;
3132
use serde_json;
3233

3334
use util::{exe, libdir, is_dylib, CiEnv};
3435
use {Compiler, Mode};
3536
use native;
3637
use tool;
37-
3838
use cache::{INTERNER, Interned};
3939
use builder::{Step, RunConfig, ShouldRun, Builder};
4040

@@ -634,20 +634,28 @@ impl Step for CodegenBackend {
634634
return;
635635
}
636636

637-
let mut cargo = builder.cargo(compiler, Mode::Librustc, target, "build");
637+
let mut cargo = builder.cargo(compiler, Mode::Librustc, target, "rustc");
638638
let mut features = builder.rustc_features().to_string();
639639
cargo.arg("--manifest-path")
640640
.arg(builder.src.join("src/librustc_codegen_llvm/Cargo.toml"));
641641
rustc_cargo_env(builder, &mut cargo);
642642

643643
features += &build_codegen_backend(&builder, &mut cargo, &compiler, target, backend);
644+
cargo.arg("--features").arg(features);
644645

645646
let tmp_stamp = builder.cargo_out(compiler, Mode::Librustc, target)
646647
.join(".tmp.stamp");
647648

649+
if builder.config.llvm_thin_lto {
650+
cargo.deferred_arg("--")
651+
.deferred_arg("-Clink-arg=-fuse-ld=lld")
652+
.deferred_arg("-Clink-arg=-flto=thin")
653+
.deferred_arg("-Clink-arg=-O2");
654+
}
655+
648656
let _folder = builder.fold_output(|| format!("stage{}-rustc_codegen_llvm", compiler.stage));
649657
let files = run_cargo(builder,
650-
cargo.arg("--features").arg(features),
658+
&mut cargo,
651659
&tmp_stamp,
652660
false);
653661
if builder.config.dry_run {

src/bootstrap/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub struct Config {
7979
pub llvm_assertions: bool,
8080
pub llvm_optimize: bool,
8181
pub llvm_release_debuginfo: bool,
82+
pub llvm_thin_lto: bool,
8283
pub llvm_version_check: bool,
8384
pub llvm_static_stdcpp: bool,
8485
pub llvm_link_shared: bool,
@@ -161,6 +162,7 @@ pub struct Target {
161162
pub cc: Option<PathBuf>,
162163
pub cxx: Option<PathBuf>,
163164
pub ar: Option<PathBuf>,
165+
pub ranlib: Option<PathBuf>,
164166
pub linker: Option<PathBuf>,
165167
pub ndk: Option<PathBuf>,
166168
pub crt_static: Option<bool>,
@@ -245,6 +247,7 @@ struct Llvm {
245247
assertions: Option<bool>,
246248
optimize: Option<bool>,
247249
release_debuginfo: Option<bool>,
250+
thin_lto: Option<bool>,
248251
version_check: Option<bool>,
249252
static_libstdcpp: Option<bool>,
250253
targets: Option<String>,
@@ -321,6 +324,7 @@ struct TomlTarget {
321324
cc: Option<String>,
322325
cxx: Option<String>,
323326
ar: Option<String>,
327+
ranlib: Option<String>,
324328
linker: Option<String>,
325329
android_ndk: Option<String>,
326330
crt_static: Option<bool>,
@@ -499,6 +503,7 @@ impl Config {
499503
llvm_assertions = llvm.assertions;
500504
set(&mut config.llvm_optimize, llvm.optimize);
501505
set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
506+
set(&mut config.llvm_thin_lto, llvm.thin_lto);
502507
set(&mut config.llvm_version_check, llvm.version_check);
503508
set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
504509
set(&mut config.llvm_link_shared, llvm.link_shared);
@@ -569,6 +574,7 @@ impl Config {
569574
target.cc = cfg.cc.clone().map(PathBuf::from);
570575
target.cxx = cfg.cxx.clone().map(PathBuf::from);
571576
target.ar = cfg.ar.clone().map(PathBuf::from);
577+
target.ranlib = cfg.ranlib.clone().map(PathBuf::from);
572578
target.linker = cfg.linker.clone().map(PathBuf::from);
573579
target.crt_static = cfg.crt_static.clone();
574580
target.musl_root = cfg.musl_root.clone().map(PathBuf::from);

src/bootstrap/dist.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ use std::env;
2222
use std::fs::{self, File};
2323
use std::io::{Read, Write};
2424
use std::path::{PathBuf, Path};
25-
use std::process::{Command, Stdio};
25+
use std::process::Stdio;
2626

2727
use build_helper::output;
28+
use build_helper::command_ext::Command;
2829

2930
use {Compiler, Mode};
3031
use channel;

src/bootstrap/install.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
use std::env;
1717
use std::fs;
1818
use std::path::{Path, PathBuf, Component};
19-
use std::process::Command;
2019

2120
use dist::{self, pkgname, sanitize_sh, tmpdir};
2221

2322
use builder::{Builder, RunConfig, ShouldRun, Step};
23+
use build_helper::command_ext::Command;
2424
use cache::Interned;
2525
use config::Config;
2626

src/bootstrap/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ use std::env;
145145
use std::fs::{self, OpenOptions, File};
146146
use std::io::{self, Seek, SeekFrom, Write, Read};
147147
use std::path::{PathBuf, Path};
148-
use std::process::{self, Command};
148+
use std::process;
149149
use std::slice;
150150
use std::str;
151151

152152
use build_helper::{run_silent, run_suppressed, try_run_silent, try_run_suppressed, output, mtime};
153+
use build_helper::command_ext::Command;
153154
use filetime::FileTime;
154155

155156
use util::{exe, libdir, OutputFolder, CiEnv};
@@ -264,6 +265,7 @@ pub struct Build {
264265
cc: HashMap<Interned<String>, cc::Tool>,
265266
cxx: HashMap<Interned<String>, cc::Tool>,
266267
ar: HashMap<Interned<String>, PathBuf>,
268+
ranlib: HashMap<Interned<String>, PathBuf>,
267269
// Misc
268270
crates: HashMap<Interned<String>, Crate>,
269271
is_sudo: bool,
@@ -365,6 +367,7 @@ impl Build {
365367
cc: HashMap::new(),
366368
cxx: HashMap::new(),
367369
ar: HashMap::new(),
370+
ranlib: HashMap::new(),
368371
crates: HashMap::new(),
369372
lldb_version: None,
370373
lldb_python_dir: None,
@@ -724,6 +727,11 @@ impl Build {
724727
self.ar.get(&target).map(|p| &**p)
725728
}
726729

730+
/// Returns the path to the `ranlib` utility for the target specified.
731+
fn ranlib(&self, target: Interned<String>) -> Option<&Path> {
732+
self.ranlib.get(&target).map(|p| &**p)
733+
}
734+
727735
/// Returns the path to the C++ compiler for the target specified.
728736
fn cxx(&self, target: Interned<String>) -> Result<&Path, String> {
729737
match self.cxx.get(&target) {

src/bootstrap/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
// except according to those terms.
1010

1111
use std::collections::HashMap;
12-
use std::process::Command;
1312
use std::path::PathBuf;
1413

1514
use build_helper::output;
15+
use build_helper::command_ext::Command;
1616
use serde_json;
1717

1818
use {Build, Crate};

src/bootstrap/native.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use std::ffi::OsString;
2323
use std::fs::{self, File};
2424
use std::io::{Read, Write};
2525
use std::path::{Path, PathBuf};
26-
use std::process::Command;
2726

2827
use build_helper::output;
28+
use build_helper::command_ext::Command;
2929
use cmake;
3030
use cc;
3131

@@ -154,6 +154,11 @@ impl Step for Llvm {
154154
.define("LLVM_TARGET_ARCH", target.split('-').next().unwrap())
155155
.define("LLVM_DEFAULT_TARGET_TRIPLE", target);
156156

157+
if builder.config.llvm_thin_lto {
158+
cfg.define("LLVM_ENABLE_LTO", "Thin")
159+
.define("LLVM_ENABLE_LLD", "ON");
160+
}
161+
157162
// By default, LLVM will automatically find OCaml and, if it finds it,
158163
// install the LLVM bindings in LLVM_OCAML_INSTALL_PATH, which defaults
159164
// to /usr/bin/ocaml.
@@ -352,6 +357,14 @@ fn configure_cmake(builder: &Builder,
352357
}
353358
}
354359

360+
if let Some(ranlib) = builder.ranlib(target) {
361+
if ranlib.is_absolute() {
362+
// LLVM build breaks if `CMAKE_RANLIB` is a relative path, for some reason it
363+
// tries to resolve this path in the LLVM build directory.
364+
cfg.define("CMAKE_RANLIB", sanitize_cc(ranlib));
365+
}
366+
}
367+
355368
if env::var_os("SCCACHE_ERROR_LOG").is_some() {
356369
cfg.env("RUST_LOG", "sccache=warn");
357370
}

src/bootstrap/sanity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use std::ffi::{OsString, OsStr};
2424
use std::fs::{self, File};
2525
use std::io::Read;
2626
use std::path::PathBuf;
27-
use std::process::Command;
2827

2928
use build_helper::output;
29+
use build_helper::command_ext::Command;
3030

3131
use Build;
3232

src/bootstrap/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use std::iter;
1919
use std::fmt;
2020
use std::fs::{self, File};
2121
use std::path::{PathBuf, Path};
22-
use std::process::Command;
2322
use std::io::Read;
2423

2524
use build_helper::{self, output};
25+
use build_helper::command_ext::Command;
2626

2727
use builder::{Kind, RunConfig, ShouldRun, Builder, Compiler, Step};
2828
use Crate as CargoCrate;

src/bootstrap/tool.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ use std::fs;
1212
use std::env;
1313
use std::iter;
1414
use std::path::PathBuf;
15-
use std::process::{Command, exit};
15+
use std::process::exit;
1616

1717
use Mode;
1818
use Compiler;
1919
use builder::{Step, RunConfig, ShouldRun, Builder};
20+
use build_helper::command_ext::Command;
2021
use util::{exe, add_lib_path};
2122
use compile::{self, libtest_stamp, libstd_stamp, librustc_stamp};
2223
use native;

src/bootstrap/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use std::str;
1818
use std::fs;
1919
use std::io::{self, Write};
2020
use std::path::{Path, PathBuf};
21-
use std::process::Command;
2221
use std::time::{SystemTime, Instant};
2322

2423
use config::Config;
2524
use builder::Builder;
25+
use build_helper::command_ext::Command;
2626

2727
/// Returns the `name` as the filename of a static library for `target`.
2828
pub fn staticlib(name: &str, target: &str) -> String {

0 commit comments

Comments
 (0)