about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <nnethercote@mozilla.com>2020-04-19 20:48:43 +1000
committerNicholas Nethercote <nnethercote@mozilla.com>2020-04-22 15:22:18 +1000
commitae322ff651cb43d8242d8375563e7a252885921e (patch)
treeec97e43141a167ef3735c576f7d638dc2ab9436b
parentb9f6dfef0bebd2c2c4d871c4d02f4c5a70844fe9 (diff)
downloadrust-ae322ff651cb43d8242d8375563e7a252885921e.tar.gz
rust-ae322ff651cb43d8242d8375563e7a252885921e.zip
Add a new option `-Cbitcode-in-rlib`.
It defaults to true, but Cargo will set this to false whenever it can to
reduce compile times.
-rw-r--r--src/doc/rustc/src/codegen-options/index.md20
-rw-r--r--src/librustc_codegen_ssa/back/write.rs3
-rw-r--r--src/librustc_interface/tests.rs4
-rw-r--r--src/librustc_session/config.rs10
-rw-r--r--src/librustc_session/options.rs2
5 files changed, 38 insertions, 1 deletions
diff --git a/src/doc/rustc/src/codegen-options/index.md b/src/doc/rustc/src/codegen-options/index.md
index 5dda5ec2cb8..eb7e34ad9ed 100644
--- a/src/doc/rustc/src/codegen-options/index.md
+++ b/src/doc/rustc/src/codegen-options/index.md
@@ -387,6 +387,26 @@ It takes one of the following values:
 For example, for gcc flavor linkers, this issues the `-nodefaultlibs` flag to
 the linker.
 
+## bitcode-in-rlib
+
+This flag controls whether or not the compiler puts compressed LLVM bitcode
+into generated rlibs. It takes one of the following values:
+
+* `y`, `yes`, `on`, or no value: put bitcode in rlibs (the default).
+* `n`, `no`, or `off`: omit bitcode from rlibs.
+
+LLVM bitcode is only needed when link-time optimization (LTO) is being
+performed, but it is enabled by default for backwards compatibility reasons.
+
+The use of `-C bitcode-in-rlib=no` can significantly improve compile times and
+reduce generated file sizes. For these reasons, Cargo uses `-C
+bitcode-in-rlib=no` whenever possible. Likewise, if you are building directly
+with `rustc` we recommend using `-C bitcode-in-rlib=no` whenever you are not
+using LTO.
+
+If combined with `-C lto`, `-C bitcode-in-rlib=no` will cause `rustc` to abort
+at start-up, because the combination is invalid.
+
 [option-emit]: ../command-line-arguments.md#option-emit
 [option-o-optimize]: ../command-line-arguments.md#option-o-optimize
 [profile-guided optimization]: ../profile-guided-optimization.md
diff --git a/src/librustc_codegen_ssa/back/write.rs b/src/librustc_codegen_ssa/back/write.rs
index d81a767abd4..db60760e459 100644
--- a/src/librustc_codegen_ssa/back/write.rs
+++ b/src/librustc_codegen_ssa/back/write.rs
@@ -378,7 +378,8 @@ pub struct CompiledModules {
 }
 
 fn need_crate_bitcode_for_rlib(sess: &Session) -> bool {
-    sess.crate_types.borrow().contains(&config::CrateType::Rlib)
+    sess.opts.cg.bitcode_in_rlib
+        && sess.crate_types.borrow().contains(&config::CrateType::Rlib)
         && sess.opts.output_types.contains_key(&OutputType::Exe)
 }
 
diff --git a/src/librustc_interface/tests.rs b/src/librustc_interface/tests.rs
index 13c0c8f46b9..02fad11d9b8 100644
--- a/src/librustc_interface/tests.rs
+++ b/src/librustc_interface/tests.rs
@@ -505,6 +505,10 @@ fn test_codegen_options_tracking_hash() {
     opts = reference.clone();
     opts.cg.linker_plugin_lto = LinkerPluginLto::LinkerPluginAuto;
     assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
+
+    opts = reference.clone();
+    opts.cg.bitcode_in_rlib = false;
+    assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
 }
 
 #[test]
diff --git a/src/librustc_session/config.rs b/src/librustc_session/config.rs
index 2513cfa73e5..f6d7e091e00 100644
--- a/src/librustc_session/config.rs
+++ b/src/librustc_session/config.rs
@@ -1685,6 +1685,16 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
         );
     }
 
+    if !cg.bitcode_in_rlib {
+        match cg.lto {
+            LtoCli::No | LtoCli::Unspecified => {}
+            LtoCli::Yes | LtoCli::NoParam | LtoCli::Thin | LtoCli::Fat => early_error(
+                error_format,
+                "options `-C bitcode-in-rlib=no` and `-C lto` are incompatible",
+            ),
+        }
+    }
+
     let prints = collect_print_requests(&mut cg, &mut debugging_opts, matches, error_format);
 
     let cg = cg;
diff --git a/src/librustc_session/options.rs b/src/librustc_session/options.rs
index 5e17fc98985..62eb3fca595 100644
--- a/src/librustc_session/options.rs
+++ b/src/librustc_session/options.rs
@@ -703,6 +703,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
         "compile the program with profiling instrumentation"),
     profile_use: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
         "use the given `.profdata` file for profile-guided optimization"),
+    bitcode_in_rlib: bool = (true, parse_bool, [TRACKED],
+        "emit bitcode in rlibs (default: yes)"),
 }
 
 options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,