Skip to content

Commit

Permalink
Iterate ReferencePropagation to fixpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed May 13, 2023
1 parent 25ef277 commit 1965237
Show file tree
Hide file tree
Showing 14 changed files with 801 additions and 400 deletions.
6 changes: 4 additions & 2 deletions compiler/rustc_mir_transform/src/ref_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ impl<'tcx> MirPass<'tcx> for ReferencePropagation {
#[instrument(level = "trace", skip(self, tcx, body))]
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!(def_id = ?body.source.def_id());
propagate_ssa(tcx, body);
while propagate_ssa(tcx, body) {}
}
}

fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
let ssa = SsaLocals::new(body);

let mut replacer = compute_replacement(tcx, body, &ssa);
Expand All @@ -94,6 +94,8 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if replacer.any_replacement {
crate::simplify::remove_unused_definitions(body);
}

replacer.any_replacement
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
16 changes: 8 additions & 8 deletions tests/mir-opt/reference_prop.debuginfo.ReferencePropagation.diff
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
let _5: &u8; // in scope 2 at $DIR/reference_prop.rs:+7:9: +7:17
scope 3 {
- debug reborrow => _5; // in scope 3 at $DIR/reference_prop.rs:+7:9: +7:17
+ debug reborrow => _1; // in scope 3 at $DIR/reference_prop.rs:+7:9: +7:17
+ debug reborrow => &_2; // in scope 3 at $DIR/reference_prop.rs:+7:9: +7:17
let _9: &i32; // in scope 3 at $DIR/reference_prop.rs:+11:14: +11:31
let mut _22: &std::option::Option<i32>; // in scope 3 at $DIR/reference_prop.rs:+11:14: +11:31
scope 4 {
Expand All @@ -50,14 +50,14 @@
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/reference_prop.rs:+3:9: +3:19
- StorageLive(_1); // scope 0 at $DIR/reference_prop.rs:+3:9: +3:19
StorageLive(_2); // scope 0 at $DIR/reference_prop.rs:+3:27: +3:31
_2 = const 5_u8; // scope 0 at $DIR/reference_prop.rs:+3:27: +3:31
_1 = &mut _2; // scope 0 at $DIR/reference_prop.rs:+3:22: +3:31
- _1 = &mut _2; // scope 0 at $DIR/reference_prop.rs:+3:22: +3:31
- StorageLive(_3); // scope 1 at $DIR/reference_prop.rs:+4:9: +4:14
_23 = const _; // scope 1 at $DIR/reference_prop.rs:+4:17: +4:24
// mir::Constant
// + span: $DIR/reference_prop.rs:463:17: 463:24
// + span: $DIR/reference_prop.rs:535:17: 535:24
// + literal: Const { ty: &T, val: Unevaluated(debuginfo, [], Some(promoted[2])) }
- _3 = &((*_23).0: u8); // scope 1 at $DIR/reference_prop.rs:+4:17: +4:24
- StorageLive(_5); // scope 2 at $DIR/reference_prop.rs:+7:9: +7:17
Expand All @@ -73,7 +73,7 @@
- StorageLive(_9); // scope 3 at $DIR/reference_prop.rs:+11:14: +11:31
_22 = const _; // scope 3 at $DIR/reference_prop.rs:+11:14: +11:31
// mir::Constant
// + span: $DIR/reference_prop.rs:470:14: 470:31
// + span: $DIR/reference_prop.rs:542:14: 542:31
// + literal: Const { ty: &Option<i32>, val: Unevaluated(debuginfo, [], Some(promoted[1])) }
- _9 = &(((*_22) as Some).0: i32); // scope 3 at $DIR/reference_prop.rs:+11:14: +11:31
- _6 = const (); // scope 4 at $DIR/reference_prop.rs:+11:36: +11:38
Expand All @@ -98,14 +98,14 @@
StorageLive(_12); // scope 5 at $DIR/reference_prop.rs:+16:83: +16:90
_21 = const _; // scope 5 at $DIR/reference_prop.rs:+16:83: +16:90
// mir::Constant
// + span: $DIR/reference_prop.rs:475:83: 475:90
// + span: $DIR/reference_prop.rs:547:83: 547:90
// + literal: Const { ty: &[i32; 10], val: Unevaluated(debuginfo, [], Some(promoted[0])) }
_12 = &(*_21); // scope 5 at $DIR/reference_prop.rs:+16:83: +16:90
StorageLive(_14); // scope 5 at $DIR/reference_prop.rs:+16:91: +16:93
_14 = RangeFull; // scope 5 at $DIR/reference_prop.rs:+16:91: +16:93
_11 = <[i32; 10] as Index<RangeFull>>::index(move _12, move _14) -> bb5; // scope 5 at $DIR/reference_prop.rs:+16:83: +16:94
// mir::Constant
// + span: $DIR/reference_prop.rs:475:83: 475:94
// + span: $DIR/reference_prop.rs:547:83: 547:94
// + literal: Const { ty: for<'a> fn(&'a [i32; 10], RangeFull) -> &'a <[i32; 10] as Index<RangeFull>>::Output {<[i32; 10] as Index<RangeFull>>::index}, val: Value(<ZST>) }
}

Expand Down Expand Up @@ -142,7 +142,7 @@
- StorageDead(_5); // scope 2 at $DIR/reference_prop.rs:+18:1: +18:2
- StorageDead(_3); // scope 1 at $DIR/reference_prop.rs:+18:1: +18:2
StorageDead(_2); // scope 0 at $DIR/reference_prop.rs:+18:1: +18:2
StorageDead(_1); // scope 0 at $DIR/reference_prop.rs:+18:1: +18:2
- StorageDead(_1); // scope 0 at $DIR/reference_prop.rs:+18:1: +18:2
StorageDead(_11); // scope 0 at $DIR/reference_prop.rs:+18:1: +18:2
StorageDead(_10); // scope 0 at $DIR/reference_prop.rs:+18:1: +18:2
return; // scope 0 at $DIR/reference_prop.rs:+18:2: +18:2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
_5 = (*_2); // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
_0 = opaque::<i32>(_5) -> bb3; // scope 0 at $DIR/reference_prop.rs:+16:13: +16:38
// mir::Constant
// + span: $DIR/reference_prop.rs:383:28: 383:34
// + span: $DIR/reference_prop.rs:455:28: 455:34
// + literal: Const { ty: fn(i32) {opaque::<i32>}, val: Value(<ZST>) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@
StorageDead(_3); // scope 0 at $DIR/reference_prop.rs:+21:13: +21:27
_0 = opaque::<i32>(_6) -> bb2; // scope 0 at $DIR/reference_prop.rs:+22:13: +22:38
// mir::Constant
// + span: $DIR/reference_prop.rs:417:28: 417:34
// + span: $DIR/reference_prop.rs:489:28: 489:34
// + literal: Const { ty: fn(i32) {opaque::<i32>}, val: Value(<ZST>) }
}

bb2: {
_7 = (*_4); // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
_0 = opaque::<i32>(_7) -> bb3; // scope 0 at $DIR/reference_prop.rs:+27:13: +27:38
// mir::Constant
// + span: $DIR/reference_prop.rs:422:28: 422:34
// + span: $DIR/reference_prop.rs:494:28: 494:34
// + literal: Const { ty: fn(i32) {opaque::<i32>}, val: Value(<ZST>) }
}

bb3: {
_8 = (*_5); // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
_0 = opaque::<i32>(_8) -> bb4; // scope 0 at $DIR/reference_prop.rs:+33:13: +33:43
// mir::Constant
// + span: $DIR/reference_prop.rs:428:33: 428:39
// + span: $DIR/reference_prop.rs:500:33: 500:39
// + literal: Const { ty: fn(i32) {opaque::<i32>}, val: Value(<ZST>) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
_3 = (*_2); // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL
_0 = opaque::<i32>(_3) -> bb1; // scope 0 at $DIR/reference_prop.rs:+14:13: +14:43
// mir::Constant
// + span: $DIR/reference_prop.rs:357:33: 357:39
// + span: $DIR/reference_prop.rs:429:33: 429:39
// + literal: Const { ty: fn(i32) {opaque::<i32>}, val: Value(<ZST>) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
let _3: *mut i32; // in scope 2 at $DIR/reference_prop.rs:+3:9: +3:13
scope 3 {
- debug xraw => _3; // in scope 3 at $DIR/reference_prop.rs:+3:9: +3:13
+ debug xraw => _4; // in scope 3 at $DIR/reference_prop.rs:+3:9: +3:13
+ debug xraw => &_1; // in scope 3 at $DIR/reference_prop.rs:+3:9: +3:13
let _6: &i32; // in scope 3 at $DIR/reference_prop.rs:+4:9: +4:13
scope 4 {
- debug xshr => _6; // in scope 4 at $DIR/reference_prop.rs:+4:9: +4:13
+ debug xshr => _2; // in scope 4 at $DIR/reference_prop.rs:+4:9: +4:13
+ debug xshr => &_1; // in scope 4 at $DIR/reference_prop.rs:+4:9: +4:13
let _7: i32; // in scope 4 at $DIR/reference_prop.rs:+6:9: +6:10
scope 5 {
debug a => _7; // in scope 5 at $DIR/reference_prop.rs:+6:9: +6:10
Expand All @@ -38,7 +38,7 @@
StorageLive(_1); // scope 0 at $DIR/reference_prop.rs:+1:9: +1:14
_1 = const 2_i32; // scope 0 at $DIR/reference_prop.rs:+1:17: +1:18
- StorageLive(_2); // scope 1 at $DIR/reference_prop.rs:+2:9: +2:13
_2 = &mut _1; // scope 1 at $DIR/reference_prop.rs:+2:16: +2:22
- _2 = &mut _1; // scope 1 at $DIR/reference_prop.rs:+2:16: +2:22
- StorageLive(_3); // scope 2 at $DIR/reference_prop.rs:+3:9: +3:13
- StorageLive(_4); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:36
- StorageLive(_5); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
Expand All @@ -49,7 +49,6 @@
- StorageDead(_4); // scope 2 at $DIR/reference_prop.rs:+3:36: +3:37
- StorageLive(_6); // scope 3 at $DIR/reference_prop.rs:+4:9: +4:13
- _6 = &(*_2); // scope 3 at $DIR/reference_prop.rs:+4:16: +4:22
+ _4 = &raw mut _1; // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
StorageLive(_7); // scope 4 at $DIR/reference_prop.rs:+6:9: +6:10
- _7 = (*_6); // scope 4 at $DIR/reference_prop.rs:+6:13: +6:18
- StorageLive(_8); // scope 5 at $DIR/reference_prop.rs:+7:5: +7:26
Expand Down
Loading

0 comments on commit 1965237

Please sign in to comment.