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
| 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.
There was a problem hiding this comment.
oh, then I'd move the function definition inside of here too. attaching it to MatchPairTree doesn't make sense to me
There was a problem hiding this comment.
In that case I would prefer to make it a top-level free function, as I think it’s too large to comfortable be an inline helper function.
But pulling it out of impl MatchPairTree is certainly fine.
There was a problem hiding this comment.
Fair. Then impl InterPat maybe?
There was a problem hiding this comment.
It was mainly in MatchPairTree because it replaced some code that was already in that impl block (which in turn was only there because I didn't move it out in #137875).
Now that it's a free function, I don't see any particular benefit from putting it in impl InterPat instead.
This comment has been minimized.
This comment has been minimized.
|
(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.
There was a problem hiding this comment.
Sounds good. How do you feel about making most of InterPat into an enum to distinguish the or-pattern vs non-or-pattern cases? Cause they share very little at this point
There was a problem hiding this comment.
I was planning on doing something like that eventually.
I didn’t do it in my initial draft because it clashed a bit with how I had written squash_inter_pat, and I wanted to keep this initial switchover simple. But with the new structure maybe it’s worth doing now. I’ll have a poke around and try it out.
There was a problem hiding this comment.
Hmm, after trying it out, I think my preference is to stick with the struct for now, with the intention to maybe make it more enum-based later on.
|
Updated some comments (diff); still considering what to do about the style feedback. |
bce0c38 to
2165ec3
Compare
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. |
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