Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4408092

Browse files
committed
Disable some MIR opts with #[optimize(none)]
1 parent 64434e3 commit 4408092

28 files changed

+172
-89
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub enum InstructionSetAttr {
5555

5656
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic, PartialEq, Eq)]
5757
pub enum OptimizeAttr {
58-
/// `#[optimize(none)]`
58+
/// `#[optimize(none)]`. This implies `#[inline(never)]`, required by LLVM
5959
None,
6060
/// `#[optimize(speed)]`
6161
Speed,
@@ -64,7 +64,7 @@ pub enum OptimizeAttr {
6464
}
6565

6666
impl OptimizeAttr {
67-
pub fn is_none(&self) -> bool {
67+
pub fn do_not_optimize(&self) -> bool {
6868
matches!(*self, OptimizeAttr::None)
6969
}
7070
}

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ pub trait MirPass<'tcx> {
153153
to_profiler_name(self.name())
154154
}
155155

156+
fn min_mir_opt_level(&self) -> usize {
157+
0
158+
}
159+
156160
/// Returns `true` if this pass is enabled with the current combination of compiler flags.
157161
fn is_enabled(&self, _sess: &Session) -> bool {
158162
true
@@ -443,9 +447,8 @@ pub struct Body<'tcx> {
443447
/// If `-Cinstrument-coverage` is not active, or if an individual function
444448
/// is not eligible for coverage, then this should always be `None`.
445449
pub function_coverage_info: Option<Box<coverage::FunctionCoverageInfo>>,
446-
447-
/// Whether optimization is disabled by `#[optimize(none)]`
448-
pub optimization_disabled: bool,
450+
// /// Whether optimization is disabled by `#[optimize(none)]`
451+
// pub optimization_disabled: bool,
449452
}
450453

451454
impl<'tcx> Body<'tcx> {
@@ -460,7 +463,6 @@ impl<'tcx> Body<'tcx> {
460463
span: Span,
461464
coroutine: Option<Box<CoroutineInfo<'tcx>>>,
462465
tainted_by_errors: Option<ErrorGuaranteed>,
463-
optimization_disabled: bool,
464466
) -> Self {
465467
// We need `arg_count` locals, and one for the return place.
466468
assert!(
@@ -490,7 +492,6 @@ impl<'tcx> Body<'tcx> {
490492
tainted_by_errors,
491493
coverage_info_hi: None,
492494
function_coverage_info: None,
493-
optimization_disabled,
494495
};
495496
body.is_polymorphic = body.has_non_region_param();
496497
body
@@ -522,7 +523,6 @@ impl<'tcx> Body<'tcx> {
522523
tainted_by_errors: None,
523524
coverage_info_hi: None,
524525
function_coverage_info: None,
525-
optimization_disabled: false,
526526
};
527527
body.is_polymorphic = body.has_non_region_param();
528528
body

compiler/rustc_mir_build/src/build/custom/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ pub(super) fn build_custom_mir<'tcx>(
6262
pass_count: 0,
6363
coverage_info_hi: None,
6464
function_coverage_info: None,
65-
optimization_disabled: false,
6665
};
6766

6867
body.local_decls.push(LocalDecl::new(return_ty, return_ty_span));

compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,6 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -
718718
span,
719719
coroutine,
720720
Some(guar),
721-
false,
722721
)
723722
}
724723

@@ -795,13 +794,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
795794
}
796795
}
797796

798-
let def_id = self.def_id.to_def_id();
799-
let optimization_disabled = if self.tcx.def_kind(def_id).has_codegen_attrs() {
800-
self.tcx.codegen_fn_attrs(def_id).optimize.as_ref().is_some_and(|o| o.is_none())
801-
} else {
802-
false
803-
};
804-
805797
let mut body = Body::new(
806798
MirSource::item(self.def_id.to_def_id()),
807799
self.cfg.basic_blocks,
@@ -813,7 +805,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
813805
self.fn_span,
814806
self.coroutine,
815807
None,
816-
optimization_disabled,
817808
);
818809
body.coverage_info_hi = self.coverage_info.map(|b| b.into_done());
819810
body

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use crate::ssa::SsaLocals;
1919
pub struct CopyProp;
2020

2121
impl<'tcx> MirPass<'tcx> for CopyProp {
22+
fn min_mir_opt_level(&self) -> usize {
23+
1
24+
}
25+
2226
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
2327
sess.mir_opt_level() >= 1
2428
}

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const PLACE_LIMIT: usize = 100;
2828
pub struct DataflowConstProp;
2929

3030
impl<'tcx> MirPass<'tcx> for DataflowConstProp {
31+
fn min_mir_opt_level(&self) -> usize {
32+
3
33+
}
34+
3135
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
3236
sess.mir_opt_level() >= 3
3337
}

compiler/rustc_mir_transform/src/dead_store_elimination.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ impl<'tcx> MirPass<'tcx> for DeadStoreElimination {
140140
}
141141
}
142142

143+
fn min_mir_opt_level(&self) -> usize {
144+
2
145+
}
146+
143147
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
144148
sess.mir_opt_level() >= 2
145149
}

compiler/rustc_mir_transform/src/deduplicate_blocks.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ use super::simplify::simplify_cfg;
1515
pub struct DeduplicateBlocks;
1616

1717
impl<'tcx> MirPass<'tcx> for DeduplicateBlocks {
18+
fn min_mir_opt_level(&self) -> usize {
19+
4
20+
}
21+
1822
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
1923
sess.mir_opt_level() >= 4
2024
}

compiler/rustc_mir_transform/src/dest_prop.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ use crate::MirPass;
150150
pub struct DestinationPropagation;
151151

152152
impl<'tcx> MirPass<'tcx> for DestinationPropagation {
153+
fn min_mir_opt_level(&self) -> usize {
154+
3
155+
}
156+
153157
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
154158
// For now, only run at MIR opt level 3. Two things need to be changed before this can be
155159
// turned on by default:

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ use super::simplify::simplify_cfg;
9292
pub struct EarlyOtherwiseBranch;
9393

9494
impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
95+
fn min_mir_opt_level(&self) -> usize {
96+
2
97+
}
98+
9599
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
96100
sess.mir_opt_level() >= 2
97101
}

0 commit comments

Comments
 (0)