Skip to content

Commit e2e9e49

Browse files
committed
Extract mir_opt_level to a method and use Option to be able to know if the value is provided or not
1 parent 476acbf commit e2e9e49

File tree

14 files changed

+34
-29
lines changed

14 files changed

+34
-29
lines changed

compiler/rustc_mir/src/transform/const_goto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct ConstGoto;
2828

2929
impl<'tcx> MirPass<'tcx> for ConstGoto {
3030
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
31-
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
31+
if tcx.sess.mir_opt_level() < 3 {
3232
return;
3333
}
3434
trace!("Running ConstGoto on {:?}", body.source);

compiler/rustc_mir/src/transform/const_prop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
725725
return None;
726726
}
727727

728-
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 3 {
728+
if self.tcx.sess.mir_opt_level() >= 3 {
729729
self.eval_rvalue_with_identities(rvalue, place)
730730
} else {
731731
self.use_ecx(|this| this.ecx.eval_rvalue_into_place(rvalue, place))
@@ -903,7 +903,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
903903

904904
/// Returns `true` if and only if this `op` should be const-propagated into.
905905
fn should_const_prop(&mut self, op: &OpTy<'tcx>) -> bool {
906-
let mir_opt_level = self.tcx.sess.opts.debugging_opts.mir_opt_level;
906+
let mir_opt_level = self.tcx.sess.mir_opt_level();
907907

908908
if mir_opt_level == 0 {
909909
return false;
@@ -1073,7 +1073,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
10731073

10741074
// Only const prop copies and moves on `mir_opt_level=2` as doing so
10751075
// currently slightly increases compile time in some cases.
1076-
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
1076+
if self.tcx.sess.mir_opt_level() >= 2 {
10771077
self.propagate_operand(operand)
10781078
}
10791079
}

compiler/rustc_mir/src/transform/deduplicate_blocks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct DeduplicateBlocks;
1616

1717
impl<'tcx> MirPass<'tcx> for DeduplicateBlocks {
1818
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19-
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
19+
if tcx.sess.mir_opt_level() < 3 {
2020
return;
2121
}
2222
debug!("Running DeduplicateBlocks on `{:?}`", body.source);

compiler/rustc_mir/src/transform/dest_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'tcx> MirPass<'tcx> for DestinationPropagation {
129129
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
130130
// Only run at mir-opt-level=2 or higher for now (we don't fix up debuginfo and remove
131131
// storage statements at the moment).
132-
if tcx.sess.opts.debugging_opts.mir_opt_level <= 1 {
132+
if tcx.sess.mir_opt_level() <= 1 {
133133
return;
134134
}
135135

compiler/rustc_mir/src/transform/early_otherwise_branch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct EarlyOtherwiseBranch;
2626

2727
impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
2828
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
29-
if tcx.sess.opts.debugging_opts.mir_opt_level < 2 {
29+
if tcx.sess.mir_opt_level() < 2 {
3030
return;
3131
}
3232
trace!("running EarlyOtherwiseBranch on {:?}", body.source);

compiler/rustc_mir/src/transform/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ crate fn is_enabled(tcx: TyCtxt<'_>) -> bool {
5252
return enabled;
5353
}
5454

55-
tcx.sess.opts.debugging_opts.mir_opt_level >= 2
55+
tcx.sess.mir_opt_level() >= 2
5656
}
5757

5858
impl<'tcx> MirPass<'tcx> for Inline {

compiler/rustc_mir/src/transform/match_branches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct MatchBranchSimplification;
4040
4141
impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
4242
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
43-
if tcx.sess.opts.debugging_opts.mir_opt_level <= 1 {
43+
if tcx.sess.mir_opt_level() <= 1 {
4444
return;
4545
}
4646

compiler/rustc_mir/src/transform/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
475475
}
476476

477477
fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
478-
let mir_opt_level = tcx.sess.opts.debugging_opts.mir_opt_level;
478+
let mir_opt_level = tcx.sess.mir_opt_level();
479479

480480
// Lowering generator control-flow and variables has to happen before we do anything else
481481
// to them. We run some optimizations before that, because they may be harder to do on the state

compiler/rustc_mir/src/transform/multiple_return_terminators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct MultipleReturnTerminators;
1010

1111
impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
1212
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
13-
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
13+
if tcx.sess.mir_opt_level() < 3 {
1414
return;
1515
}
1616

compiler/rustc_mir/src/transform/nrvo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct RenameReturnPlace;
3434

3535
impl<'tcx> MirPass<'tcx> for RenameReturnPlace {
3636
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
37-
if tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
37+
if tcx.sess.mir_opt_level() == 0 {
3838
return;
3939
}
4040

compiler/rustc_mir/src/transform/unreachable_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct UnreachablePropagation;
1212

1313
impl MirPass<'_> for UnreachablePropagation {
1414
fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15-
if tcx.sess.opts.debugging_opts.mir_opt_level < 3 {
15+
if tcx.sess.mir_opt_level() < 3 {
1616
// Enable only under -Zmir-opt-level=3 as in some cases (check the deeply-nested-opt
1717
// perf benchmark) LLVM may spend quite a lot of time optimizing the generated code.
1818
return;

compiler/rustc_session/src/config.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,21 +1938,23 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
19381938
Some(SymbolManglingVersion::V0) => {}
19391939
}
19401940

1941-
if debugging_opts.mir_opt_level > 1 {
1942-
// Functions inlined during MIR transform can, at best, make it impossible to
1943-
// effectively cover inlined functions, and, at worst, break coverage map generation
1944-
// during LLVM codegen. For example, function counter IDs are only unique within a
1945-
// function. Inlining after these counters are injected can produce duplicate counters,
1946-
// resulting in an invalid coverage map (and ICE); so this option combination is not
1947-
// allowed.
1948-
early_warn(
1949-
error_format,
1950-
&format!(
1951-
"`-Z mir-opt-level={}` (or any level > 1) enables function inlining, which \
1941+
if let Some(mir_opt_level) = debugging_opts.mir_opt_level {
1942+
if mir_opt_level > 1 {
1943+
// Functions inlined during MIR transform can, at best, make it impossible to
1944+
// effectively cover inlined functions, and, at worst, break coverage map generation
1945+
// during LLVM codegen. For example, function counter IDs are only unique within a
1946+
// function. Inlining after these counters are injected can produce duplicate counters,
1947+
// resulting in an invalid coverage map (and ICE); so this option combination is not
1948+
// allowed.
1949+
early_warn(
1950+
error_format,
1951+
&format!(
1952+
"`-Z mir-opt-level={}` (or any level > 1) enables function inlining, which \
19521953
is incompatible with `-Z instrument-coverage`. Inlining will be disabled.",
1953-
debugging_opts.mir_opt_level,
1954-
),
1955-
);
1954+
mir_opt_level,
1955+
),
1956+
);
1957+
}
19561958
}
19571959
}
19581960

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,8 +999,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
999999
mir_emit_retag: bool = (false, parse_bool, [TRACKED],
10001000
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
10011001
(default: no)"),
1002-
mir_opt_level: usize = (1, parse_uint, [TRACKED],
1003-
"MIR optimization level (0-3; default: 1)"),
1002+
mir_opt_level: Option<usize> = (None, parse_opt_uint, [TRACKED],
1003+
"MIR optimization level (0-3; default: Some(1) in non optimized builds and Some(2) in optimized builds)"),
10041004
mutable_noalias: bool = (false, parse_bool, [TRACKED],
10051005
"emit noalias metadata for mutable references (default: no)"),
10061006
new_llvm_pass_manager: bool = (false, parse_bool, [TRACKED],

compiler/rustc_session/src/session.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,9 @@ impl Session {
640640
pub fn binary_dep_depinfo(&self) -> bool {
641641
self.opts.debugging_opts.binary_dep_depinfo
642642
}
643+
pub fn mir_opt_level(&self) -> usize {
644+
self.opts.debugging_opts.mir_opt_level.unwrap_or(1)
645+
}
643646

644647
/// Gets the features enabled for the current compilation session.
645648
/// DO NOT USE THIS METHOD if there is a TyCtxt available, as it circumvents

0 commit comments

Comments
 (0)