Skip to content

Commit 48b01a0

Browse files
committed
Add new MutatatingUseContexts for deinit and SetDiscriminant
1 parent f7ca97a commit 48b01a0

File tree

9 files changed

+57
-41
lines changed

9 files changed

+57
-41
lines changed

Diff for: compiler/rustc_borrowck/src/def_use.rs

+4
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,9 @@ pub fn categorize(context: PlaceContext) -> Option<DefUse> {
7272

7373
// Debug info is neither def nor use.
7474
PlaceContext::NonUse(NonUseContext::VarDebugInfo) => None,
75+
76+
PlaceContext::MutatingUse(MutatingUseContext::Deinit | MutatingUseContext::SetDiscriminant) => {
77+
bug!("These statements are not allowed in this MIR phase")
78+
}
7579
}
7680
}

Diff for: compiler/rustc_codegen_ssa/src/mir/analyze.rs

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
211211

212212
PlaceContext::MutatingUse(
213213
MutatingUseContext::Store
214+
| MutatingUseContext::Deinit
215+
| MutatingUseContext::SetDiscriminant
214216
| MutatingUseContext::AsmOutput
215217
| MutatingUseContext::Borrow
216218
| MutatingUseContext::AddressOf

Diff for: compiler/rustc_middle/src/mir/visit.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,14 @@ macro_rules! make_mir_visitor {
395395
StatementKind::SetDiscriminant { place, .. } => {
396396
self.visit_place(
397397
place,
398-
PlaceContext::MutatingUse(MutatingUseContext::Store),
398+
PlaceContext::MutatingUse(MutatingUseContext::SetDiscriminant),
399399
location
400400
);
401401
}
402402
StatementKind::Deinit(place) => {
403403
self.visit_place(
404404
place,
405-
PlaceContext::MutatingUse(MutatingUseContext::Store),
405+
PlaceContext::MutatingUse(MutatingUseContext::Deinit),
406406
location
407407
)
408408
}
@@ -1181,6 +1181,10 @@ pub enum NonMutatingUseContext {
11811181
pub enum MutatingUseContext {
11821182
/// Appears as LHS of an assignment.
11831183
Store,
1184+
/// Appears on `SetDiscriminant`
1185+
SetDiscriminant,
1186+
/// Appears on `Deinit`
1187+
Deinit,
11841188
/// Output operand of an inline assembly block.
11851189
AsmOutput,
11861190
/// Destination of a call.

Diff for: compiler/rustc_mir_dataflow/src/impls/init_locals.rs

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ impl<T> Visitor<'_> for TransferFunction<'_, T>
7777
where
7878
T: GenKill<Local>,
7979
{
80+
// FIXME: Using `visit_local` here is a bug. For example, on `move _5.field` we mark `_5` as
81+
// deinitialized, although clearly it is only partially deinitialized. This analysis is not
82+
// actually used anywhere at the moment, so this is not critical, but this does need to be fixed
83+
// before it starts being used again.
8084
fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) {
8185
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, NonUseContext};
8286
match context {
@@ -87,6 +91,9 @@ where
8791
| MutatingUseContext::Yield,
8892
) => {}
8993

94+
// If it's deinitialized, it's no longer init
95+
PlaceContext::MutatingUse(MutatingUseContext::Deinit) => self.trans.kill(local),
96+
9097
// Otherwise, when a place is mutated, we must consider it possibly initialized.
9198
PlaceContext::MutatingUse(_) => self.trans.gen(local),
9299

Diff for: compiler/rustc_mir_dataflow/src/impls/liveness.rs

+7-25
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,6 @@ use crate::{AnalysisDomain, Backward, CallReturnPlaces, GenKill, GenKillAnalysis
1818
/// such an assignment is currently marked as a "use" of `x` in an attempt to be maximally
1919
/// conservative.
2020
///
21-
/// ## Enums and `SetDiscriminant`
22-
///
23-
/// Assigning a literal value to an `enum` (e.g. `Option<i32>`), does not result in a simple
24-
/// assignment of the form `_1 = /*...*/` in the MIR. For example, the following assignment to `x`:
25-
///
26-
/// ```
27-
/// x = Some(4);
28-
/// ```
29-
///
30-
/// compiles to this MIR
31-
///
32-
/// ```
33-
/// ((_1 as Some).0: i32) = const 4_i32;
34-
/// discriminant(_1) = 1;
35-
/// ```
36-
///
37-
/// However, `MaybeLiveLocals` **does** mark `x` (`_1`) as "killed" after a statement like this.
38-
/// That's because it treats the `SetDiscriminant` operation as a definition of `x`, even though
39-
/// the writes that actually initialized the locals happened earlier.
40-
///
41-
/// This makes `MaybeLiveLocals` unsuitable for certain classes of optimization normally associated
42-
/// with a live variables analysis, notably dead-store elimination. It's a dirty hack, but it works
43-
/// okay for the generator state transform (currently the main consumer of this analysis).
44-
///
4521
/// [`MaybeBorrowedLocals`]: super::MaybeBorrowedLocals
4622
/// [flow-test]: https://github.com/rust-lang/rust/blob/a08c47310c7d49cbdc5d7afb38408ba519967ecd/src/test/ui/mir-dataflow/liveness-ptr.rs
4723
/// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis
@@ -161,7 +137,13 @@ impl DefUse {
161137
match context {
162138
PlaceContext::NonUse(_) => None,
163139

164-
PlaceContext::MutatingUse(MutatingUseContext::Store) => Some(DefUse::Def),
140+
PlaceContext::MutatingUse(MutatingUseContext::Store | MutatingUseContext::Deinit) => {
141+
Some(DefUse::Def)
142+
}
143+
144+
// Setting the discriminant is not a use because it does no reading, but it is also not
145+
// a def because it does not overwrite the whole place
146+
PlaceContext::MutatingUse(MutatingUseContext::SetDiscriminant) => None,
165147

166148
// `MutatingUseContext::Call` and `MutatingUseContext::Yield` indicate that this is the
167149
// destination place for a `Call` return or `Yield` resume respectively. Since this is

Diff for: compiler/rustc_mir_transform/src/const_prop.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,10 @@ impl Visitor<'_> for CanConstProp {
897897
// mutations of the same local via `Store`
898898
| MutatingUse(MutatingUseContext::Call)
899899
| MutatingUse(MutatingUseContext::AsmOutput)
900+
| MutatingUse(MutatingUseContext::Deinit)
900901
// Actual store that can possibly even propagate a value
901-
| MutatingUse(MutatingUseContext::Store) => {
902+
| MutatingUse(MutatingUseContext::Store)
903+
| MutatingUse(MutatingUseContext::SetDiscriminant) => {
902904
if !self.found_assignment.insert(local) {
903905
match &mut self.can_const_prop[local] {
904906
// If the local can only get propagated in its own block, then we don't have

Diff for: compiler/rustc_mir_transform/src/const_prop_lint.rs

+2
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,9 @@ impl Visitor<'_> for CanConstProp {
778778
// mutations of the same local via `Store`
779779
| MutatingUse(MutatingUseContext::Call)
780780
| MutatingUse(MutatingUseContext::AsmOutput)
781+
| MutatingUse(MutatingUseContext::Deinit)
781782
// Actual store that can possibly even propagate a value
783+
| MutatingUse(MutatingUseContext::SetDiscriminant)
782784
| MutatingUse(MutatingUseContext::Store) => {
783785
if !self.found_assignment.insert(local) {
784786
match &mut self.can_const_prop[local] {

Diff for: src/test/mir-opt/dest-prop/union.main.DestinationPropagation.diff

+12-6
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,30 @@
1717
}
1818

1919
bb0: {
20-
StorageLive(_1); // scope 0 at $DIR/union.rs:13:9: 13:11
21-
StorageLive(_2); // scope 0 at $DIR/union.rs:13:23: 13:28
22-
_2 = val() -> bb1; // scope 0 at $DIR/union.rs:13:23: 13:28
20+
- StorageLive(_1); // scope 0 at $DIR/union.rs:13:9: 13:11
21+
- StorageLive(_2); // scope 0 at $DIR/union.rs:13:23: 13:28
22+
- _2 = val() -> bb1; // scope 0 at $DIR/union.rs:13:23: 13:28
23+
+ nop; // scope 0 at $DIR/union.rs:13:9: 13:11
24+
+ nop; // scope 0 at $DIR/union.rs:13:23: 13:28
25+
+ (_1.0: u32) = val() -> bb1; // scope 0 at $DIR/union.rs:13:23: 13:28
2326
// mir::Constant
2427
// + span: $DIR/union.rs:13:23: 13:26
2528
// + literal: Const { ty: fn() -> u32 {val}, val: Value(Scalar(<ZST>)) }
2629
}
2730

2831
bb1: {
2932
Deinit(_1); // scope 0 at $DIR/union.rs:13:14: 13:30
30-
(_1.0: u32) = move _2; // scope 0 at $DIR/union.rs:13:14: 13:30
31-
StorageDead(_2); // scope 0 at $DIR/union.rs:13:29: 13:30
33+
- (_1.0: u32) = move _2; // scope 0 at $DIR/union.rs:13:14: 13:30
34+
- StorageDead(_2); // scope 0 at $DIR/union.rs:13:29: 13:30
35+
+ nop; // scope 0 at $DIR/union.rs:13:14: 13:30
36+
+ nop; // scope 0 at $DIR/union.rs:13:29: 13:30
3237
StorageLive(_3); // scope 1 at $DIR/union.rs:15:5: 15:27
3338
StorageLive(_4); // scope 1 at $DIR/union.rs:15:10: 15:26
3439
_4 = (_1.0: u32); // scope 2 at $DIR/union.rs:15:19: 15:24
3540
StorageDead(_4); // scope 1 at $DIR/union.rs:15:26: 15:27
3641
StorageDead(_3); // scope 1 at $DIR/union.rs:15:27: 15:28
37-
StorageDead(_1); // scope 0 at $DIR/union.rs:16:1: 16:2
42+
- StorageDead(_1); // scope 0 at $DIR/union.rs:16:1: 16:2
43+
+ nop; // scope 0 at $DIR/union.rs:16:1: 16:2
3844
return; // scope 0 at $DIR/union.rs:16:2: 16:2
3945
}
4046
}

Diff for: src/test/mir-opt/early_otherwise_branch_68867.try_sum.EarlyOtherwiseBranch.before-SimplifyConstCondition-final.after.diff

+14-7
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,22 @@
6565

6666
bb0: {
6767
- StorageLive(_3); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:8: 27:6
68+
- StorageLive(_4); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:14: 21:24
69+
- StorageLive(_5); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:15: 21:16
70+
- _5 = _1; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:15: 21:16
6871
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:8: 27:6
69-
StorageLive(_4); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:14: 21:24
70-
StorageLive(_5); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:15: 21:16
71-
_5 = _1; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:15: 21:16
72+
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:14: 21:24
73+
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:15: 21:16
74+
+ (_4.0: &ViewportPercentageLength) = _1; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:15: 21:16
7275
StorageLive(_6); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:18: 21:23
7376
_6 = _2; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:18: 21:23
7477
Deinit(_4); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:14: 21:24
75-
(_4.0: &ViewportPercentageLength) = move _5; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:14: 21:24
78+
- (_4.0: &ViewportPercentageLength) = move _5; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:14: 21:24
79+
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:14: 21:24
7680
(_4.1: &ViewportPercentageLength) = move _6; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:14: 21:24
7781
StorageDead(_6); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:23: 21:24
78-
StorageDead(_5); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:23: 21:24
82+
- StorageDead(_5); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:23: 21:24
83+
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:23: 21:24
7984
_11 = discriminant((*(_4.0: &ViewportPercentageLength))); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:14: 21:24
8085
- switchInt(move _11) -> [0_isize: bb1, 1_isize: bb3, 2_isize: bb4, 3_isize: bb5, otherwise: bb11]; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:8: 21:24
8186
+ StorageLive(_34); // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:8: 21:24
@@ -100,8 +105,9 @@
100105
discriminant(_0) = 1; // scope 0 at $DIR/early_otherwise_branch_68867.rs:26:21: 26:28
101106
StorageDead(_33); // scope 0 at $DIR/early_otherwise_branch_68867.rs:26:27: 26:28
102107
- StorageDead(_3); // scope 0 at $DIR/early_otherwise_branch_68867.rs:27:6: 27:7
108+
- StorageDead(_4); // scope 0 at $DIR/early_otherwise_branch_68867.rs:28:1: 28:2
103109
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:27:6: 27:7
104-
StorageDead(_4); // scope 0 at $DIR/early_otherwise_branch_68867.rs:28:1: 28:2
110+
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:28:1: 28:2
105111
return; // scope 0 at $DIR/early_otherwise_branch_68867.rs:28:2: 28:2
106112
}
107113

@@ -293,8 +299,9 @@
293299
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:5: 27:7
294300
discriminant(_0) = 0; // scope 0 at $DIR/early_otherwise_branch_68867.rs:21:5: 27:7
295301
- StorageDead(_3); // scope 0 at $DIR/early_otherwise_branch_68867.rs:27:6: 27:7
302+
- StorageDead(_4); // scope 0 at $DIR/early_otherwise_branch_68867.rs:28:1: 28:2
296303
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:27:6: 27:7
297-
StorageDead(_4); // scope 0 at $DIR/early_otherwise_branch_68867.rs:28:1: 28:2
304+
+ nop; // scope 0 at $DIR/early_otherwise_branch_68867.rs:28:1: 28:2
298305
return; // scope 0 at $DIR/early_otherwise_branch_68867.rs:28:2: 28:2
299306
}
300307

0 commit comments

Comments
 (0)