Skip to content

Commit 46ae8eb

Browse files
committed
Auto merge of rust-lang#134051 - saethlin:partial-ssa-analysis, r=<try>
Do partial SsaLocals analysis in unoptimized builds I've been musing about the perf implications of all the queries that `is_freeze` results in. Let's perf it. CI will fail because I'm too lazy to figure out what's going on with the mir-opt tests for now. If perf looks bad, it's not worth fixing the tests. (it's not like this makes GVN lightweight or light on queries overall, because evaluating consts is still quite a lot of queries) r? ghost
2 parents df5b8e3 + 696799b commit 46ae8eb

File tree

44 files changed

+1254
-1664
lines changed

Some content is hidden

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

44 files changed

+1254
-1664
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
///
@@ -85,7 +85,7 @@ impl<'tcx> crate::MirPass<'tcx> for ReferencePropagation {
8585

8686
fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
8787
let typing_env = body.typing_env(tcx);
88-
let ssa = SsaLocals::new(tcx, body, typing_env);
88+
let ssa = SsaLocals::new(tcx, body, typing_env, SsaAnalysis::Full);
8989

9090
let mut replacer = compute_replacement(tcx, body, &ssa);
9191
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/gvn.arithmetic.GVN.panic-unwind.diff

-43
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
let mut _74: u64;
8080

8181
bb0: {
82-
StorageLive(_2);
8382
StorageLive(_3);
8483
StorageLive(_4);
8584
_4 = copy _1;
@@ -92,8 +91,6 @@
9291

9392
bb1: {
9493
StorageDead(_3);
95-
StorageDead(_2);
96-
StorageLive(_5);
9794
StorageLive(_6);
9895
StorageLive(_7);
9996
_7 = copy _1;
@@ -106,8 +103,6 @@
106103

107104
bb2: {
108105
StorageDead(_6);
109-
StorageDead(_5);
110-
StorageLive(_8);
111106
- StorageLive(_9);
112107
+ nop;
113108
StorageLive(_10);
@@ -125,8 +120,6 @@
125120
bb3: {
126121
- StorageDead(_9);
127122
+ nop;
128-
StorageDead(_8);
129-
StorageLive(_12);
130123
StorageLive(_13);
131124
StorageLive(_14);
132125
_14 = copy _1;
@@ -139,8 +132,6 @@
139132

140133
bb4: {
141134
StorageDead(_13);
142-
StorageDead(_12);
143-
StorageLive(_15);
144135
StorageLive(_16);
145136
StorageLive(_17);
146137
_17 = copy _1;
@@ -153,8 +144,6 @@
153144

154145
bb5: {
155146
StorageDead(_16);
156-
StorageDead(_15);
157-
StorageLive(_18);
158147
StorageLive(_19);
159148
StorageLive(_20);
160149
_20 = copy _1;
@@ -173,8 +162,6 @@
173162

174163
bb7: {
175164
StorageDead(_19);
176-
StorageDead(_18);
177-
StorageLive(_22);
178165
StorageLive(_23);
179166
StorageLive(_24);
180167
_24 = copy _1;
@@ -194,8 +181,6 @@
194181

195182
bb9: {
196183
StorageDead(_23);
197-
StorageDead(_22);
198-
StorageLive(_26);
199184
StorageLive(_27);
200185
StorageLive(_28);
201186
_28 = copy _1;
@@ -215,8 +200,6 @@
215200

216201
bb11: {
217202
StorageDead(_27);
218-
StorageDead(_26);
219-
StorageLive(_30);
220203
StorageLive(_31);
221204
StorageLive(_32);
222205
_32 = copy _1;
@@ -235,8 +218,6 @@
235218

236219
bb13: {
237220
StorageDead(_31);
238-
StorageDead(_30);
239-
StorageLive(_34);
240221
StorageLive(_35);
241222
StorageLive(_36);
242223
_36 = copy _1;
@@ -255,8 +236,6 @@
255236

256237
bb15: {
257238
StorageDead(_35);
258-
StorageDead(_34);
259-
StorageLive(_38);
260239
StorageLive(_39);
261240
StorageLive(_40);
262241
_40 = copy _1;
@@ -276,8 +255,6 @@
276255

277256
bb17: {
278257
StorageDead(_39);
279-
StorageDead(_38);
280-
StorageLive(_42);
281258
StorageLive(_43);
282259
StorageLive(_44);
283260
_44 = copy _1;
@@ -297,8 +274,6 @@
297274

298275
bb19: {
299276
StorageDead(_43);
300-
StorageDead(_42);
301-
StorageLive(_46);
302277
StorageLive(_47);
303278
StorageLive(_48);
304279
_48 = copy _1;
@@ -317,8 +292,6 @@
317292

318293
bb21: {
319294
StorageDead(_47);
320-
StorageDead(_46);
321-
StorageLive(_50);
322295
StorageLive(_51);
323296
StorageLive(_52);
324297
_52 = copy _1;
@@ -331,8 +304,6 @@
331304

332305
bb22: {
333306
StorageDead(_51);
334-
StorageDead(_50);
335-
StorageLive(_53);
336307
StorageLive(_54);
337308
StorageLive(_55);
338309
_55 = copy _1;
@@ -345,8 +316,6 @@
345316

346317
bb23: {
347318
StorageDead(_54);
348-
StorageDead(_53);
349-
StorageLive(_56);
350319
StorageLive(_57);
351320
StorageLive(_58);
352321
_58 = copy _1;
@@ -359,8 +328,6 @@
359328

360329
bb24: {
361330
StorageDead(_57);
362-
StorageDead(_56);
363-
StorageLive(_59);
364331
StorageLive(_60);
365332
StorageLive(_61);
366333
_61 = copy _1;
@@ -373,8 +340,6 @@
373340

374341
bb25: {
375342
StorageDead(_60);
376-
StorageDead(_59);
377-
StorageLive(_62);
378343
StorageLive(_63);
379344
StorageLive(_64);
380345
_64 = copy _1;
@@ -387,8 +352,6 @@
387352

388353
bb26: {
389354
StorageDead(_63);
390-
StorageDead(_62);
391-
StorageLive(_65);
392355
StorageLive(_66);
393356
StorageLive(_67);
394357
_67 = copy _1;
@@ -404,8 +367,6 @@
404367

405368
bb27: {
406369
StorageDead(_66);
407-
StorageDead(_65);
408-
StorageLive(_69);
409370
StorageLive(_70);
410371
StorageLive(_71);
411372
_71 = copy _1;
@@ -418,8 +379,6 @@
418379

419380
bb28: {
420381
StorageDead(_70);
421-
StorageDead(_69);
422-
StorageLive(_72);
423382
StorageLive(_73);
424383
StorageLive(_74);
425384
_74 = copy _1;
@@ -432,8 +391,6 @@
432391

433392
bb29: {
434393
StorageDead(_73);
435-
StorageDead(_72);
436-
_0 = const ();
437394
return;
438395
}
439396
}

tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff

-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
let mut _22: (u64, bool);
2828

2929
bb0: {
30-
StorageLive(_2);
3130
StorageLive(_3);
3231
StorageLive(_4);
3332
_4 = copy _1;
@@ -47,8 +46,6 @@
4746

4847
bb2: {
4948
StorageDead(_3);
50-
StorageDead(_2);
51-
StorageLive(_6);
5249
StorageLive(_7);
5350
StorageLive(_8);
5451
_8 = copy _1;
@@ -68,8 +65,6 @@
6865

6966
bb4: {
7067
StorageDead(_7);
71-
StorageDead(_6);
72-
StorageLive(_10);
7368
- StorageLive(_11);
7469
+ nop;
7570
StorageLive(_12);
@@ -94,8 +89,6 @@
9489
bb6: {
9590
- StorageDead(_11);
9691
+ nop;
97-
StorageDead(_10);
98-
StorageLive(_15);
9992
StorageLive(_16);
10093
StorageLive(_17);
10194
_17 = copy _1;
@@ -115,8 +108,6 @@
115108

116109
bb8: {
117110
StorageDead(_16);
118-
StorageDead(_15);
119-
StorageLive(_19);
120111
StorageLive(_20);
121112
StorageLive(_21);
122113
_21 = copy _1;
@@ -136,8 +127,6 @@
136127

137128
bb10: {
138129
StorageDead(_20);
139-
StorageDead(_19);
140-
_0 = const ();
141130
return;
142131
}
143132
}

0 commit comments

Comments
 (0)