Skip to content

Commit 2ece31b

Browse files
committed
Do partial SsaLocals analysis in unoptimized builds
1 parent 81d8c74 commit 2ece31b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+354
-314
lines changed

compiler/rustc_mir_transform/src/add_retag.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ fn may_contain_reference<'tcx>(ty: Ty<'tcx>, depth: u32, tcx: TyCtxt<'tcx>) -> b
4949
}
5050

5151
impl<'tcx> crate::MirPass<'tcx> for AddRetag {
52-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
53-
sess.opts.unstable_opts.mir_emit_retag
52+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
53+
tcx.sess.opts.unstable_opts.mir_emit_retag
5454
}
5555

5656
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/check_alignment.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ use rustc_middle::mir::interpret::Scalar;
33
use rustc_middle::mir::visit::PlaceContext;
44
use rustc_middle::mir::*;
55
use rustc_middle::ty::{Ty, TyCtxt};
6-
use rustc_session::Session;
76

87
use crate::check_pointers::{BorrowCheckMode, PointerCheck, check_pointers};
98

109
pub(super) struct CheckAlignment;
1110

1211
impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
13-
fn is_enabled(&self, sess: &Session) -> bool {
12+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
1413
// FIXME(#112480) MSVC and rustc disagree on minimum stack alignment on x86 Windows
15-
if sess.target.llvm_target == "i686-pc-windows-msvc" {
14+
if tcx.sess.target.llvm_target == "i686-pc-windows-msvc" {
1615
return false;
1716
}
18-
sess.ub_checks()
17+
tcx.sess.ub_checks()
1918
}
2019

2120
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/check_null.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ use rustc_index::IndexVec;
22
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext};
33
use rustc_middle::mir::*;
44
use rustc_middle::ty::{Ty, TyCtxt};
5-
use rustc_session::Session;
65

76
use crate::check_pointers::{BorrowCheckMode, PointerCheck, check_pointers};
87

98
pub(super) struct CheckNull;
109

1110
impl<'tcx> crate::MirPass<'tcx> for CheckNull {
12-
fn is_enabled(&self, sess: &Session) -> bool {
13-
sess.ub_checks()
11+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
12+
tcx.sess.ub_checks()
1413
}
1514

1615
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use rustc_middle::mir::*;
55
use rustc_middle::ty::TyCtxt;
66
use tracing::{debug, instrument};
77

8-
use crate::ssa::SsaLocals;
8+
use crate::pass_manager as pm;
9+
use crate::ssa::{SsaAnalysis, SsaLocals};
910

1011
/// Unify locals that copy each other.
1112
///
@@ -17,19 +18,39 @@ use crate::ssa::SsaLocals;
1718
/// where each of the locals is only assigned once.
1819
///
1920
/// We want to replace all those locals by `_a`, either copied or moved.
20-
pub(super) struct CopyProp;
21+
pub(super) enum CopyProp {
22+
Partial,
23+
Full,
24+
}
2125

2226
impl<'tcx> crate::MirPass<'tcx> for CopyProp {
23-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
24-
sess.mir_opt_level() >= 1
27+
fn name(&self) -> &'static str {
28+
match self {
29+
CopyProp::Partial => "CopyProp-partial",
30+
CopyProp::Full => "CopyProp",
31+
}
32+
}
33+
34+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
35+
match self {
36+
CopyProp::Partial => {
37+
tcx.sess.mir_opt_level() == 1
38+
&& !pm::should_run_pass(tcx, &CopyProp::Full, pm::Optimizations::Allowed)
39+
}
40+
CopyProp::Full => tcx.sess.mir_opt_level() >= 2,
41+
}
2542
}
2643

2744
#[instrument(level = "trace", skip(self, tcx, body))]
2845
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2946
debug!(def_id = ?body.source.def_id());
3047

3148
let typing_env = body.typing_env(tcx);
32-
let ssa = SsaLocals::new(tcx, body, typing_env);
49+
let ssa_analysis = match self {
50+
CopyProp::Partial => SsaAnalysis::Partial,
51+
CopyProp::Full => SsaAnalysis::Full,
52+
};
53+
let ssa = SsaLocals::new(tcx, body, typing_env, ssa_analysis);
3354

3455
let fully_moved = fully_moved_locals(&ssa, body);
3556
debug!(?fully_moved);

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use crate::coverage::mappings::ExtractedMappings;
3030
pub(super) struct InstrumentCoverage;
3131

3232
impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {
33-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
34-
sess.instrument_coverage()
33+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
34+
tcx.sess.instrument_coverage()
3535
}
3636

3737
fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const PLACE_LIMIT: usize = 100;
3535
pub(super) struct DataflowConstProp;
3636

3737
impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
38-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
39-
sess.mir_opt_level() >= 3
38+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
39+
tcx.sess.mir_opt_level() >= 3
4040
}
4141

4242
#[instrument(skip_all level = "debug")]

compiler/rustc_mir_transform/src/dead_store_elimination.rs

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

143-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
144-
sess.mir_opt_level() >= 2
143+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
144+
tcx.sess.mir_opt_level() >= 2
145145
}
146146

147147
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/dest_prop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ use tracing::{debug, trace};
149149
pub(super) struct DestinationPropagation;
150150

151151
impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
152-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
152+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
153153
// For now, only run at MIR opt level 3. Two things need to be changed before this can be
154154
// turned on by default:
155155
// 1. Because of the overeager removal of storage statements, this can cause stack space
@@ -158,7 +158,7 @@ impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
158158
// 2. Despite being an overall perf improvement, this still causes a 30% regression in
159159
// keccak. We can temporarily fix this by bounding function size, but in the long term
160160
// we should fix this by being smarter about invalidating analysis results.
161-
sess.mir_opt_level() >= 3
161+
tcx.sess.mir_opt_level() >= 3
162162
}
163163

164164
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ use crate::patch::MirPatch;
9393
pub(super) struct EarlyOtherwiseBranch;
9494

9595
impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
96-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
97-
sess.mir_opt_level() >= 2
96+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
97+
tcx.sess.mir_opt_level() >= 2
9898
}
9999

100100
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,42 @@ use rustc_span::def_id::DefId;
109109
use smallvec::SmallVec;
110110
use tracing::{debug, instrument, trace};
111111

112-
use crate::ssa::SsaLocals;
112+
use crate::pass_manager as pm;
113+
use crate::ssa::{SsaAnalysis, SsaLocals};
113114

114-
pub(super) struct GVN;
115+
pub(super) enum GVN {
116+
Partial,
117+
Full,
118+
}
115119

116120
impl<'tcx> crate::MirPass<'tcx> for GVN {
117-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
118-
sess.mir_opt_level() >= 2
121+
fn name(&self) -> &'static str {
122+
match self {
123+
GVN::Partial => "GVN-partial",
124+
GVN::Full => "GVN",
125+
}
126+
}
127+
128+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
129+
match self {
130+
GVN::Partial => {
131+
tcx.sess.mir_opt_level() == 1
132+
&& !pm::should_run_pass(tcx, &GVN::Full, pm::Optimizations::Allowed)
133+
}
134+
GVN::Full => tcx.sess.mir_opt_level() >= 2,
135+
}
119136
}
120137

121138
#[instrument(level = "trace", skip(self, tcx, body))]
122139
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
123140
debug!(def_id = ?body.source.def_id());
124141

125142
let typing_env = body.typing_env(tcx);
126-
let ssa = SsaLocals::new(tcx, body, typing_env);
143+
let ssa_analysis = match self {
144+
GVN::Partial => SsaAnalysis::Partial,
145+
GVN::Full => SsaAnalysis::Full,
146+
};
147+
let ssa = SsaLocals::new(tcx, body, typing_env, ssa_analysis);
127148
// Clone dominators because we need them while mutating the body.
128149
let dominators = body.basic_blocks.dominators().clone();
129150

0 commit comments

Comments
 (0)