Skip to content

Commit a0353d5

Browse files
committed
Do partial SsaLocals analysis in unoptimized builds
1 parent 124cc92 commit a0353d5

File tree

49 files changed

+1444
-1887
lines changed

Some content is hidden

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

49 files changed

+1444
-1887
lines changed

compiler/rustc_mir_transform/src/copy_prop.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_middle::mir::*;
55
use rustc_middle::ty::TyCtxt;
66
use tracing::{debug, instrument};
77

8-
use crate::ssa::SsaLocals;
8+
use crate::ssa::{SsaAnalysis, SsaLocals};
99

1010
/// Unify locals that copy each other.
1111
///
@@ -29,7 +29,11 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
2929
debug!(def_id = ?body.source.def_id());
3030

3131
let typing_env = body.typing_env(tcx);
32-
let ssa = SsaLocals::new(tcx, body, typing_env);
32+
let ssa_analysis = match tcx.sess.mir_opt_level() {
33+
0 | 1 => SsaAnalysis::Partial,
34+
_ => SsaAnalysis::Full,
35+
};
36+
let ssa = SsaLocals::new(tcx, body, typing_env, ssa_analysis);
3337

3438
let fully_moved = fully_moved_locals(&ssa, body);
3539
debug!(?fully_moved);

compiler/rustc_mir_transform/src/gvn.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,25 @@ use rustc_span::def_id::DefId;
107107
use smallvec::SmallVec;
108108
use tracing::{debug, instrument, trace};
109109

110-
use crate::ssa::{AssignedValue, SsaLocals};
110+
use crate::ssa::{AssignedValue, SsaAnalysis, SsaLocals};
111111

112112
pub(super) struct GVN;
113113

114114
impl<'tcx> crate::MirPass<'tcx> for GVN {
115115
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
116-
sess.mir_opt_level() >= 2
116+
sess.mir_opt_level() >= 1
117117
}
118118

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

123123
let typing_env = body.typing_env(tcx);
124-
let ssa = SsaLocals::new(tcx, body, typing_env);
124+
let ssa_analysis = match tcx.sess.mir_opt_level() {
125+
0 | 1 => SsaAnalysis::Partial,
126+
_ => SsaAnalysis::Full,
127+
};
128+
let ssa = SsaLocals::new(tcx, body, typing_env, ssa_analysis);
125129
// Clone dominators because we need them while mutating the body.
126130
let dominators = body.basic_blocks.dominators().clone();
127131

compiler/rustc_mir_transform/src/ref_prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_mir_dataflow::Analysis;
1111
use rustc_mir_dataflow::impls::{MaybeStorageDead, always_storage_live_locals};
1212
use tracing::{debug, instrument};
1313

14-
use crate::ssa::{SsaLocals, StorageLiveLocals};
14+
use crate::ssa::{SsaAnalysis, SsaLocals, StorageLiveLocals};
1515

1616
/// Propagate references using SSA analysis.
1717
///
@@ -89,7 +89,7 @@ impl<'tcx> crate::MirPass<'tcx> for ReferencePropagation {
8989

9090
fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
9191
let typing_env = body.typing_env(tcx);
92-
let ssa = SsaLocals::new(tcx, body, typing_env);
92+
let ssa = SsaLocals::new(tcx, body, typing_env, SsaAnalysis::Full);
9393

9494
let mut replacer = compute_replacement(tcx, body, &ssa);
9595
debug!(?replacer.targets);

compiler/rustc_mir_transform/src/ssa.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ pub(super) enum AssignedValue<'a, 'tcx> {
3838
Terminator,
3939
}
4040

41+
pub(super) enum SsaAnalysis {
42+
Full,
43+
Partial,
44+
}
45+
4146
impl SsaLocals {
4247
pub(super) fn new<'tcx>(
4348
tcx: TyCtxt<'tcx>,
4449
body: &Body<'tcx>,
4550
typing_env: ty::TypingEnv<'tcx>,
51+
analysis: SsaAnalysis,
4652
) -> SsaLocals {
4753
let assignment_order = Vec::with_capacity(body.local_decls.len());
4854

@@ -79,9 +85,15 @@ impl SsaLocals {
7985
// borrows, we need to check the types. For raw pointers and mutable borrows, the locals
8086
// have already been marked as non-SSA.
8187
debug!(?visitor.borrowed_locals);
88+
8289
for local in visitor.borrowed_locals.iter() {
83-
if !body.local_decls[local].ty.is_freeze(tcx, typing_env) {
84-
visitor.assignments[local] = Set1::Many;
90+
match analysis {
91+
SsaAnalysis::Full => {
92+
if !body.local_decls[local].ty.is_freeze(tcx, typing_env) {
93+
visitor.assignments[local] = Set1::Many;
94+
}
95+
}
96+
SsaAnalysis::Partial => visitor.assignments[local] = Set1::Many,
8597
}
8698
}
8799

tests/mir-opt/copy-prop/borrowed_local.borrowed.CopyProp.panic-unwind.diff

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
let mut _3: &T;
88

99
bb0: {
10-
- _2 = copy _1;
10+
_2 = copy _1;
1111
_3 = &_1;
1212
_0 = opaque::<&T>(copy _3) -> [return: bb1, unwind continue];
1313
}
1414

1515
bb1: {
16-
- _0 = opaque::<T>(copy _2) -> [return: bb2, unwind continue];
17-
+ _0 = opaque::<T>(copy _1) -> [return: bb2, unwind continue];
16+
_0 = opaque::<T>(copy _2) -> [return: bb2, unwind continue];
1817
}
1918

2019
bb2: {

0 commit comments

Comments
 (0)