-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add #[loop_match]
for improved DFA codegen
#138780
base: master
Are you sure you want to change the base?
Add #[loop_match]
for improved DFA codegen
#138780
Conversation
Co-authored-by: Folkert de Vries <folkert@folkertdev.nl>
Some changes occurred in match checking cc @Nadrieril Some changes occurred in compiler/rustc_passes/src/check_attr.rs Some changes occurred in cc @BoxyUwU |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @folkertdev for putting up this PR. The big picture looks right, in terms of the behavior of the tests and how to approach the experiment in terms of starting with the attributes for thiis.
This is a first partial pass on the details.
@rustbot author
@@ -244,6 +248,188 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { | |||
None | |||
}) | |||
} | |||
ExprKind::LoopMatch { state, region_scope, ref arms, .. } => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably the body of this match arm should be broken out and located elsewhere, in the same way that match_expr
is, and should have a substantial doc comment above it, like match_expr
does.
let rustc_middle::thir::ExprKind::Scope { value, .. } = | ||
self.thir[value.unwrap()].kind | ||
else { | ||
panic!(); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is unreachable, it's worth marking it that way. Either way, it's worth a comment about this.
//let is_coroutine = this.coroutine.is_some(); | ||
|
||
/*// Link the exit drop tree to unwind drop tree. | ||
if drops.drops.iter().any(|drop_node| drop_node.data.kind == DropKind::Value) { | ||
let unwind_target = this.diverge_cleanup_target(region_scope, span); | ||
let mut unwind_indices = IndexVec::from_elem_n(unwind_target, 1); | ||
for (drop_idx, drop_node) in drops.drops.iter_enumerated().skip(1) { | ||
match drop_node.data.kind { | ||
DropKind::Storage | DropKind::ForLint => { | ||
if is_coroutine { | ||
let unwind_drop = this.scopes.unwind_drops.add_drop( | ||
drop_node.data, | ||
unwind_indices[drop_node.next], | ||
); | ||
unwind_indices.push(unwind_drop); | ||
} else { | ||
unwind_indices.push(unwind_indices[drop_node.next]); | ||
} | ||
} | ||
DropKind::Value => { | ||
let unwind_drop = this | ||
.scopes | ||
.unwind_drops | ||
.add_drop(drop_node.data, unwind_indices[drop_node.next]); | ||
this.scopes.unwind_drops.add_entry_point( | ||
blocks[drop_idx].unwrap(), | ||
unwind_indices[drop_node.next], | ||
); | ||
unwind_indices.push(unwind_drop); | ||
} | ||
} | ||
} | ||
}*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the story here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropping the rest of the local variables when unwinding out of drops on #[const_continue]
isn't implemented yet.
/// A `#[loop_match] loop { state = 'blk: { match state { ... } } }` expression. | ||
LoopMatch { | ||
state: ExprId, | ||
|
||
region_scope: region::Scope, | ||
arms: Box<[ArmId]>, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the relevant pretty
test for this and the other to ensure these get pretty printed correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what sort of a test should I add for this? would //@ pretty-mode:hir,typed
exercise the relevant logic (we only add this node for thir
, not for hir
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have pretty printing support for THIR. Only -Zunpretty=thir-tree
and -Zunpretty=thir-flat
exist, which both use Debug
impls.
Co-authored-by: Travis Cross <tc@traviscross.com>
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed review!
I've fixed a bunch of the low-hanging fruit (e.g. in the tests). For the actual pattern matching logic, I have a branch with what I believe is a better solution that re-uses more existing pattern matching infra. We'll come back to that here once björn has had a chance to look at it.
// FIXME do we need the breakable scope? | ||
this.in_breakable_scope(Some(loop_block), destination, expr_span, |this| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that breakable scope is definitely needed (to break out of the loop
)
|
||
// FIXME do we need the breakable scope? | ||
this.in_breakable_scope(Some(loop_block), destination, expr_span, |this| { | ||
// conduct the test, if necessary |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted. In this case I think the comment is not really in the best place.
/// A `#[loop_match] loop { state = 'blk: { match state { ... } } }` expression. | ||
LoopMatch { | ||
state: ExprId, | ||
|
||
region_scope: region::Scope, | ||
arms: Box<[ArmId]>, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what sort of a test should I add for this? would //@ pretty-mode:hir,typed
exercise the relevant logic (we only add this node for thir
, not for hir
).
Some changes occurred in exhaustiveness checking cc @Nadrieril Some changes occurred in match lowering cc @Nadrieril |
tracking issue: #138777
project goal: rust-lang/rust-project-goals#258
This PR adds the
#[loop_match]
attribute, which aims to improve code generation for state machines. For some (very exciting) benchmarks, see rust-lang/rust-project-goals#258 (comment)Currently, a very restricted syntax pattern is accepted. We'd like to get feedback and merge this now before we go too far in a direction that others have concerns with.
current state
We accept code that looks like this
#[loop_match]
: normalcontinue
andbreak
continue to work#[const_continue] is only allowed in loops annotated with
#[loop_match]`future work
break
valuemaybe future work
continue 'label value
syntax, which#[const_continue]
could then use.State::Initial
)break
/continue
expressions that are not marked with#[const_continue]
r? @traviscross