mir_build: Add an extra intermediate step in MIR building for patterns #155144
mir_build: Add an extra intermediate step in MIR building for patterns #155144Zalathar wants to merge 2 commits intorust-lang:mainfrom
Conversation
|
|
This comment has been minimized.
This comment has been minimized.
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
mir_build: Add an extra intermediate step in MIR building for patterns
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (76367e4): comparison URL. Overall result: ❌✅ regressions and improvements - no action neededBenchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf. @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (secondary 2.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -9.8%, secondary -4.3%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 491.148s -> 509.241s (3.68%) |
This comment was marked as resolved.
This comment was marked as resolved.
0623749 to
c9949ba
Compare
This comment has been minimized.
This comment has been minimized.
9631275 to
7abdb0f
Compare
|
Marking this as ready for consideration. I'm not entirely confident about the name “InterPat”, and I'm aware that this change doesn't yet demonstrate any clear benefit on its own, but I think it's a worthwhile step towards hopefully being able to disentangle match-lowering a bit more. |
|
Some changes occurred in match lowering cc @Nadrieril |
| // Recursively squash any subpatterns into refutable `MatchPairTree` forests. | ||
| let mut subpairs = vec![]; | ||
| for subpat in inter_pat.subpats { | ||
| MatchPairTree::squash_inter_pat(subpat, &mut subpairs, extra_data); | ||
| } | ||
|
|
||
| if let Some(testable_case) = inter_pat.testable_case { |
There was a problem hiding this comment.
For clarity I'd prefer the rest of the function to be in the else branch of the "or pattern" case
| // In order to please the borrow checker, when lowering a pattern | ||
| // like `x @ subpat` we must establish any bindings in `subpat` | ||
| // before establishing the binding for `x`. |
There was a problem hiding this comment.
I think that comment get lost, I don't see it anymore
There was a problem hiding this comment.
I found the original comment to be too long and verbose, so I rewrote it as this shorter comment in squash_inter_pat:
// If present, the binding must be pushed _after_ traversing subpatterns.
// This is so that when lowering something like `x @ NonCopy { copy_field }`,
// the binding to `copy_field` will occur before the binding for `x`.
// See <https://github.com/rust-lang/rust/issues/69971> for more background.Do you think that's fine, or is there more detail you'd like me to retain?
(Note that the ordering restriction is not yet relevant when building InterPat; it only becomes important when squashing InterPat into MatchPairTree.)
| match_pairs: &mut Vec<Self>, // Newly-created nodes are added to this vector | ||
| extra_data: &mut PatternExtraData<'tcx>, // Bindings/ascriptions are added here | ||
| ) { | ||
| extra_data.ascriptions.extend(inter_pat.ascriptions); |
There was a problem hiding this comment.
Would you mind starting the function with a let InterPat { ascriptions, ....} = inter_pat with an exhaustive field list so we're sure not to forget a field?
| ascriptions: vec![], | ||
| is_never: inter_pat.is_never, | ||
| }; | ||
| MatchPairTree::squash_inter_pat(inter_pat, &mut match_pairs, &mut extra_data); |
There was a problem hiding this comment.
nit: I have a preference for inlining squash_inter_pat into here, the current split took me a couple seconds to understand
There was a problem hiding this comment.
squash_inter_pat is recursive (since it recursively squashes subpatterns), so I don't think it can be inlined.
This makes it easier to perform coordinated modifications to `FlatPat::new` and `MatchPairTree::from_pattern`, because the two functions are heavily coupled.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
(Rebased onto main to make things easier; no actual changes yet.) |
| // Or-patterns never need a place during MIR building. | ||
| place: None, |
There was a problem hiding this comment.
Note that currently we retain places for or-pattern nodes and then (I believe) never use them, so this is a deliberate change from current behaviour.
|
Updated some comments (diff); still considering what to do about the style feedback. |
This splits out a separate `InterPat` data structure from `MatchPairTree`/`FlatPat`/`Candidate`, which should hopefully make it easier to modify these earlier parts of match lowering without having to simultaneously adjust many use-sites in later steps.
View all comments
This is an attempt to partly decouple the data structures created by
match_pair.rsfrom the data structures that are ultimately consumed by the main part of match lowering.In some ways this is a reversal from #137875. That PR succeeded in removing the
TestCase::Irrefutablevariant, by taking a pre-existing “simplification” step and fusing it directly intoMatchPairTree::for_pattern. Unfortunately, in doing so it also reinforced a very high degree of coupling between the transformations performed inmatch_pair, and the data structures used by later steps.My hope is that these changes will make it easier for follow-up work to further separate decision-making from MIR building when lowering patterns.
r? Nadrieril