Skip to content

Commit 4bfd62a

Browse files
committed
Auto merge of #71323 - nnethercote:bitcode-in-rlib, r=alexcrichton
Add `-Cbitcode-in-rlib`. This is a cut-down version of #70458 that gets the compile-time wins. r? @alexcrichton
2 parents 2dc5b60 + a105c5c commit 4bfd62a

File tree

12 files changed

+60
-7
lines changed

12 files changed

+60
-7
lines changed

Diff for: src/bootstrap/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Step for Std {
4545
let compiler = builder.compiler(0, builder.config.build);
4646

4747
let mut cargo = builder.cargo(compiler, Mode::Std, target, cargo_subcommand(builder.kind));
48-
std_cargo(builder, target, &mut cargo);
48+
std_cargo(builder, target, compiler.stage, &mut cargo);
4949

5050
builder.info(&format!("Checking std artifacts ({} -> {})", &compiler.host, target));
5151
run_cargo(

Diff for: src/bootstrap/compile.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Step for Std {
8686
target_deps.extend(copy_third_party_objects(builder, &compiler, target).into_iter());
8787

8888
let mut cargo = builder.cargo(compiler, Mode::Std, target, "build");
89-
std_cargo(builder, target, &mut cargo);
89+
std_cargo(builder, target, compiler.stage, &mut cargo);
9090

9191
builder.info(&format!(
9292
"Building stage{} std artifacts ({} -> {})",
@@ -164,7 +164,7 @@ fn copy_third_party_objects(
164164

165165
/// Configure cargo to compile the standard library, adding appropriate env vars
166166
/// and such.
167-
pub fn std_cargo(builder: &Builder<'_>, target: Interned<String>, cargo: &mut Cargo) {
167+
pub fn std_cargo(builder: &Builder<'_>, target: Interned<String>, stage: u32, cargo: &mut Cargo) {
168168
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
169169
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
170170
}
@@ -231,6 +231,18 @@ pub fn std_cargo(builder: &Builder<'_>, target: Interned<String>, cargo: &mut Ca
231231
}
232232
}
233233
}
234+
235+
// By default, rustc uses `-Cbitcode-in-rlib=yes`, and Cargo overrides that
236+
// with `-Cbitcode-in-rlib=no` for non-LTO builds. However, libstd must be
237+
// built with bitcode so that the produced rlibs can be used for both LTO
238+
// builds (which use bitcode) and non-LTO builds (which use object code).
239+
// So we override the override here!
240+
//
241+
// But we don't bother for the stage 0 compiler because it's never used
242+
// with LTO.
243+
if stage >= 1 {
244+
cargo.rustflag("-Cbitcode-in-rlib=yes");
245+
}
234246
}
235247

236248
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

Diff for: src/bootstrap/doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl Step for Std {
394394

395395
let run_cargo_rustdoc_for = |package: &str| {
396396
let mut cargo = builder.cargo(compiler, Mode::Std, target, "rustdoc");
397-
compile::std_cargo(builder, target, &mut cargo);
397+
compile::std_cargo(builder, target, compiler.stage, &mut cargo);
398398

399399
// Keep a whitelist so we do not build internal stdlib crates, these will be
400400
// build by the rustc step later if enabled.

Diff for: src/bootstrap/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ impl Step for Crate {
17251725
let mut cargo = builder.cargo(compiler, mode, target, test_kind.subcommand());
17261726
match mode {
17271727
Mode::Std => {
1728-
compile::std_cargo(builder, target, &mut cargo);
1728+
compile::std_cargo(builder, target, compiler.stage, &mut cargo);
17291729
}
17301730
Mode::Rustc => {
17311731
builder.ensure(compile::Rustc { compiler, target });

Diff for: src/doc/rustc/src/codegen-options/index.md

+20
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,26 @@ It takes one of the following values:
387387
For example, for gcc flavor linkers, this issues the `-nodefaultlibs` flag to
388388
the linker.
389389

390+
## bitcode-in-rlib
391+
392+
This flag controls whether or not the compiler puts compressed LLVM bitcode
393+
into generated rlibs. It takes one of the following values:
394+
395+
* `y`, `yes`, `on`, or no value: put bitcode in rlibs (the default).
396+
* `n`, `no`, or `off`: omit bitcode from rlibs.
397+
398+
LLVM bitcode is only needed when link-time optimization (LTO) is being
399+
performed, but it is enabled by default for backwards compatibility reasons.
400+
401+
The use of `-C bitcode-in-rlib=no` can significantly improve compile times and
402+
reduce generated file sizes. For these reasons, Cargo uses `-C
403+
bitcode-in-rlib=no` whenever possible. Likewise, if you are building directly
404+
with `rustc` we recommend using `-C bitcode-in-rlib=no` whenever you are not
405+
using LTO.
406+
407+
If combined with `-C lto`, `-C bitcode-in-rlib=no` will cause `rustc` to abort
408+
at start-up, because the combination is invalid.
409+
390410
[option-emit]: ../command-line-arguments.md#option-emit
391411
[option-o-optimize]: ../command-line-arguments.md#option-o-optimize
392412
[profile-guided optimization]: ../profile-guided-optimization.md

Diff for: src/librustc_codegen_ssa/back/write.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ pub struct CompiledModules {
378378
}
379379

380380
fn need_crate_bitcode_for_rlib(sess: &Session) -> bool {
381-
sess.crate_types.borrow().contains(&config::CrateType::Rlib)
381+
sess.opts.cg.bitcode_in_rlib
382+
&& sess.crate_types.borrow().contains(&config::CrateType::Rlib)
382383
&& sess.opts.output_types.contains_key(&OutputType::Exe)
383384
}
384385

Diff for: src/librustc_interface/tests.rs

+4
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ fn test_codegen_options_tracking_hash() {
505505
opts = reference.clone();
506506
opts.cg.linker_plugin_lto = LinkerPluginLto::LinkerPluginAuto;
507507
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
508+
509+
opts = reference.clone();
510+
opts.cg.bitcode_in_rlib = false;
511+
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
508512
}
509513

510514
#[test]

Diff for: src/librustc_middle/mir/cache.rs

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ impl Cache {
6060
}
6161

6262
/// This will recompute the predecessors cache if it is not available
63-
// njn: typedef?
6463
fn predecessors(
6564
&mut self,
6665
body: &Body<'_>,

Diff for: src/librustc_session/config.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,16 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
16851685
);
16861686
}
16871687

1688+
if !cg.bitcode_in_rlib {
1689+
match cg.lto {
1690+
LtoCli::No | LtoCli::Unspecified => {}
1691+
LtoCli::Yes | LtoCli::NoParam | LtoCli::Thin | LtoCli::Fat => early_error(
1692+
error_format,
1693+
"options `-C bitcode-in-rlib=no` and `-C lto` are incompatible",
1694+
),
1695+
}
1696+
}
1697+
16881698
let prints = collect_print_requests(&mut cg, &mut debugging_opts, matches, error_format);
16891699

16901700
let cg = cg;

Diff for: src/librustc_session/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
703703
"compile the program with profiling instrumentation"),
704704
profile_use: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
705705
"use the given `.profdata` file for profile-guided optimization"),
706+
bitcode_in_rlib: bool = (true, parse_bool, [TRACKED],
707+
"emit bitcode in rlibs (default: yes)"),
706708
}
707709

708710
options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,

Diff for: src/test/ui/lto-and-no-bitcode-in-rlib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: -C lto -C bitcode-in-rlib=no
2+
3+
fn main() {}

Diff for: src/test/ui/lto-and-no-bitcode-in-rlib.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: options `-C bitcode-in-rlib=no` and `-C lto` are incompatible
2+

0 commit comments

Comments
 (0)