Skip to content

Commit 7c6345d

Browse files
committed
Allow use of -Clto=thin with -Ccodegen-units=1 in general
The current logic to ignore ThinLTO when `-Ccodegen-units=1` makes sense for local ThinLTO but even in this scenario, a user may still want (non-local) ThinLTO for the purpose of optimizing dependencies into the final crate which is being compiled with 1 CGU. The previous behavior was even more confusing because if you were generating a binary (`--emit=link`), then you would get ThinLTO but if you asked for LLVM IR or bytecode, then it would silently change to using regular LTO. With this change, we only override the defaults for local ThinLTO if you ask for a single output such as LLVM IR or bytecode and in all other cases honor the requested LTO setting.
1 parent 9b0a099 commit 7c6345d

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

compiler/rustc_session/src/config.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ impl Default for Options {
738738
actually_rustdoc: false,
739739
trimmed_def_paths: TrimmedDefPaths::default(),
740740
cli_forced_codegen_units: None,
741-
cli_forced_thinlto_off: false,
741+
cli_forced_local_thinlto_off: false,
742742
remap_path_prefix: Vec::new(),
743743
real_rust_source_base_dir: None,
744744
edition: DEFAULT_EDITION,
@@ -1720,7 +1720,7 @@ fn should_override_cgus_and_disable_thinlto(
17201720
error_format: ErrorOutputType,
17211721
mut codegen_units: Option<usize>,
17221722
) -> (bool, Option<usize>) {
1723-
let mut disable_thinlto = false;
1723+
let mut disable_local_thinlto = false;
17241724
// Issue #30063: if user requests LLVM-related output to one
17251725
// particular path, disable codegen-units.
17261726
let incompatible: Vec<_> = output_types
@@ -1745,12 +1745,12 @@ fn should_override_cgus_and_disable_thinlto(
17451745
}
17461746
early_warn(error_format, "resetting to default -C codegen-units=1");
17471747
codegen_units = Some(1);
1748-
disable_thinlto = true;
1748+
disable_local_thinlto = true;
17491749
}
17501750
}
17511751
_ => {
17521752
codegen_units = Some(1);
1753-
disable_thinlto = true;
1753+
disable_local_thinlto = true;
17541754
}
17551755
}
17561756
}
@@ -1759,7 +1759,7 @@ fn should_override_cgus_and_disable_thinlto(
17591759
early_error(error_format, "value for codegen units must be a positive non-zero integer");
17601760
}
17611761

1762-
(disable_thinlto, codegen_units)
1762+
(disable_local_thinlto, codegen_units)
17631763
}
17641764

17651765
fn check_thread_count(unstable_opts: &UnstableOptions, error_format: ErrorOutputType) {
@@ -2249,7 +2249,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
22492249
let output_types = parse_output_types(&unstable_opts, matches, error_format);
22502250

22512251
let mut cg = CodegenOptions::build(matches, error_format);
2252-
let (disable_thinlto, mut codegen_units) = should_override_cgus_and_disable_thinlto(
2252+
let (disable_local_thinlto, mut codegen_units) = should_override_cgus_and_disable_thinlto(
22532253
&output_types,
22542254
matches,
22552255
error_format,
@@ -2492,7 +2492,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
24922492
actually_rustdoc: false,
24932493
trimmed_def_paths: TrimmedDefPaths::default(),
24942494
cli_forced_codegen_units: codegen_units,
2495-
cli_forced_thinlto_off: disable_thinlto,
2495+
cli_forced_local_thinlto_off: disable_local_thinlto,
24962496
remap_path_prefix,
24972497
real_rust_source_base_dir,
24982498
edition,

compiler/rustc_session/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ top_level_options!(
181181
#[rustc_lint_opt_deny_field_access("use `Session::codegen_units` instead of this field")]
182182
cli_forced_codegen_units: Option<usize> [UNTRACKED],
183183
#[rustc_lint_opt_deny_field_access("use `Session::lto` instead of this field")]
184-
cli_forced_thinlto_off: bool [UNTRACKED],
184+
cli_forced_local_thinlto_off: bool [UNTRACKED],
185185

186186
/// Remap source path prefixes in all output (messages, object files, debug, etc.).
187187
remap_path_prefix: Vec<(PathBuf, PathBuf)> [TRACKED_NO_CRATE_HASH],

compiler/rustc_session/src/session.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,8 @@ impl Session {
991991
return config::Lto::Fat;
992992
}
993993
config::LtoCli::Thin => {
994-
return if self.opts.cli_forced_thinlto_off {
995-
config::Lto::Fat
996-
} else {
997-
config::Lto::Thin
998-
};
994+
// The user explicitly asked for ThinLTO
995+
return config::Lto::Thin;
999996
}
1000997
}
1001998

@@ -1007,7 +1004,7 @@ impl Session {
10071004

10081005
// If processing command line options determined that we're incompatible
10091006
// with ThinLTO (e.g., `-C lto --emit llvm-ir`) then return that option.
1010-
if self.opts.cli_forced_thinlto_off {
1007+
if self.opts.cli_forced_local_thinlto_off {
10111008
return config::Lto::No;
10121009
}
10131010

0 commit comments

Comments
 (0)