Skip to content

Commit

Permalink
Rollup merge of #127164 - Nadrieril:clean-lowering-loop, r=matthewjasper
Browse files Browse the repository at this point in the history
match lowering: Clarify the main loop of the algorithm

Now that we expand or-patterns in a single place in the algorithm, we can move it (back) to the main part of the loop. This makes the call-graph of the main loop rather simple: `match_candidates` has three branches that each call back to `match_candidates`. The remaining tricky part is `finalize_or_candidate`.

I also factored out the whole "process a prefix of the candidates then process the rest" thing which I think helps legibility.

The first two commits are a fix for an indexing mistake I introduced in #126553, already sumitted in #127028 but feel free to merge this first.

r? `@matthewjasper`
  • Loading branch information
matthiaskrgr committed Jul 12, 2024
2 parents 5e311f9 + 42772e9 commit 58fe37f
Show file tree
Hide file tree
Showing 21 changed files with 425 additions and 540 deletions.
345 changes: 155 additions & 190 deletions compiler/rustc_mir_build/src/build/matches/mod.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn full_tested_match() -> () {
_6 = &((_2 as Some).0: i32);
_3 = &fake shallow _2;
StorageLive(_7);
_7 = guard() -> [return: bb8, unwind: bb16];
_7 = guard() -> [return: bb8, unwind: bb15];
}

bb8: {
Expand Down Expand Up @@ -118,11 +118,7 @@ fn full_tested_match() -> () {
unreachable;
}

bb15: {
goto -> bb14;
}

bb16 (cleanup): {
bb15 (cleanup): {
resume;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn full_tested_match2() -> () {
_6 = &((_2 as Some).0: i32);
_3 = &fake shallow _2;
StorageLive(_7);
_7 = guard() -> [return: bb8, unwind: bb16];
_7 = guard() -> [return: bb8, unwind: bb15];
}

bb8: {
Expand Down Expand Up @@ -118,11 +118,7 @@ fn full_tested_match2() -> () {
unreachable;
}

bb15: {
goto -> bb14;
}

bb16 (cleanup): {
bb15 (cleanup): {
resume;
}
}
86 changes: 39 additions & 47 deletions tests/mir-opt/building/match/match_false_edges.main.built.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -38,65 +38,61 @@ fn main() -> () {
StorageLive(_2);
_2 = Option::<i32>::Some(const 1_i32);
PlaceMention(_2);
_5 = discriminant(_2);
switchInt(move _5) -> [1: bb8, otherwise: bb2];
_4 = discriminant(_2);
switchInt(move _4) -> [1: bb2, otherwise: bb1];
}

bb1: {
FakeRead(ForMatchedPlace(None), _2);
unreachable;
falseEdge -> [real: bb14, imaginary: bb4];
}

bb2: {
falseEdge -> [real: bb15, imaginary: bb3];
falseEdge -> [real: bb9, imaginary: bb1];
}

bb3: {
_4 = discriminant(_2);
switchInt(move _4) -> [1: bb6, otherwise: bb4];
goto -> bb1;
}

bb4: {
_5 = discriminant(_2);
switchInt(move _5) -> [1: bb6, otherwise: bb5];
}

bb5: {
StorageLive(_14);
_14 = _2;
_1 = const 4_i32;
StorageDead(_14);
goto -> bb21;
}

bb5: {
goto -> bb1;
goto -> bb20;
}

bb6: {
falseEdge -> [real: bb16, imaginary: bb4];
falseEdge -> [real: bb15, imaginary: bb5];
}

bb7: {
goto -> bb4;
goto -> bb5;
}

bb8: {
falseEdge -> [real: bb10, imaginary: bb2];
FakeRead(ForMatchedPlace(None), _2);
unreachable;
}

bb9: {
goto -> bb2;
}

bb10: {
StorageLive(_7);
_7 = &((_2 as Some).0: i32);
_3 = &fake shallow _2;
StorageLive(_8);
_8 = guard() -> [return: bb11, unwind: bb24];
_8 = guard() -> [return: bb10, unwind: bb22];
}

bb11: {
switchInt(move _8) -> [0: bb13, otherwise: bb12];
bb10: {
switchInt(move _8) -> [0: bb12, otherwise: bb11];
}

bb12: {
bb11: {
StorageDead(_8);
FakeRead(ForMatchGuard, _3);
FakeRead(ForGuardBinding, _7);
Expand All @@ -105,42 +101,42 @@ fn main() -> () {
_1 = const 1_i32;
StorageDead(_6);
StorageDead(_7);
goto -> bb21;
goto -> bb20;
}

bb13: {
goto -> bb14;
bb12: {
goto -> bb13;
}

bb14: {
bb13: {
StorageDead(_8);
StorageDead(_7);
falseEdge -> [real: bb9, imaginary: bb2];
falseEdge -> [real: bb3, imaginary: bb1];
}

bb15: {
bb14: {
StorageLive(_9);
_9 = _2;
_1 = const 2_i32;
StorageDead(_9);
goto -> bb21;
goto -> bb20;
}

bb16: {
bb15: {
StorageLive(_11);
_11 = &((_2 as Some).0: i32);
_3 = &fake shallow _2;
StorageLive(_12);
StorageLive(_13);
_13 = (*_11);
_12 = guard2(move _13) -> [return: bb17, unwind: bb24];
_12 = guard2(move _13) -> [return: bb16, unwind: bb22];
}

bb17: {
switchInt(move _12) -> [0: bb19, otherwise: bb18];
bb16: {
switchInt(move _12) -> [0: bb18, otherwise: bb17];
}

bb18: {
bb17: {
StorageDead(_13);
StorageDead(_12);
FakeRead(ForMatchGuard, _3);
Expand All @@ -150,38 +146,34 @@ fn main() -> () {
_1 = const 3_i32;
StorageDead(_10);
StorageDead(_11);
goto -> bb21;
goto -> bb20;
}

bb19: {
goto -> bb20;
bb18: {
goto -> bb19;
}

bb20: {
bb19: {
StorageDead(_13);
StorageDead(_12);
StorageDead(_11);
falseEdge -> [real: bb7, imaginary: bb4];
falseEdge -> [real: bb7, imaginary: bb5];
}

bb21: {
bb20: {
PlaceMention(_1);
StorageDead(_2);
StorageDead(_1);
_0 = const ();
return;
}

bb22: {
bb21: {
FakeRead(ForMatchedPlace(None), _1);
unreachable;
}

bb23: {
goto -> bb22;
}

bb24 (cleanup): {
bb22 (cleanup): {
resume;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,33 @@ fn match_bool(_1: bool) -> usize {

bb0: {
PlaceMention(_1);
switchInt(_1) -> [0: bb2, otherwise: bb4];
switchInt(_1) -> [0: bb1, otherwise: bb2];
}

bb1: {
FakeRead(ForMatchedPlace(None), _1);
unreachable;
_0 = const 20_usize;
goto -> bb6;
}

bb2: {
_0 = const 20_usize;
goto -> bb7;
falseEdge -> [real: bb5, imaginary: bb1];
}

bb3: {
goto -> bb1;
}

bb4: {
falseEdge -> [real: bb6, imaginary: bb2];
FakeRead(ForMatchedPlace(None), _1);
unreachable;
}

bb5: {
goto -> bb2;
}

bb6: {
_0 = const 10_usize;
goto -> bb7;
goto -> bb6;
}

bb7: {
bb6: {
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn match_enum(_1: E1) -> bool {
bb0: {
PlaceMention(_1);
_2 = discriminant(_1);
switchInt(move _2) -> [0: bb3, 1: bb5, 2: bb7, otherwise: bb2];
switchInt(move _2) -> [0: bb2, 1: bb4, 2: bb6, otherwise: bb1];
}

bb1: {
Expand All @@ -17,44 +17,40 @@ fn match_enum(_1: E1) -> bool {
}

bb2: {
goto -> bb1;
goto -> bb8;
}

bb3: {
goto -> bb9;
goto -> bb1;
}

bb4: {
goto -> bb2;
goto -> bb8;
}

bb5: {
goto -> bb9;
goto -> bb1;
}

bb6: {
goto -> bb2;
_0 = const false;
goto -> bb10;
}

bb7: {
_0 = const false;
goto -> bb11;
goto -> bb1;
}

bb8: {
goto -> bb2;
falseEdge -> [real: bb9, imaginary: bb6];
}

bb9: {
falseEdge -> [real: bb10, imaginary: bb7];
}

bb10: {
_0 = const true;
goto -> bb11;
goto -> bb10;
}

bb11: {
bb10: {
return;
}
}
Loading

0 comments on commit 58fe37f

Please sign in to comment.