-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Enable switch dispatch for is-expressions #65874
Conversation
} | ||
else if (canProduceLinearSequence(decisionDag.RootNode, whenTrueLabel: node.WhenFalseLabel, whenFalseLabel: node.WhenTrueLabel)) |
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.
The inverted canProduceLinearSequence
check - used to lower negated is-expressions - was preventing us to use the general rewriter which handles switch dispatch, but that is already accounted for: when IsNegated is true, the decision dag is created off of the inner pattern.
roslyn/src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Lines 58 to 60 in 85712db
bool negated = pattern.IsNegated(out var innerPattern); | |
BoundDecisionDag decisionDag = DecisionDagBuilder.CreateDecisionDagForIsPattern( | |
this.Compilation, pattern.Syntax, expression, innerPattern, whenTrueLabel: whenTrueLabel, whenFalseLabel: whenFalseLabel, diagnostics); |
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.
Also filed #65876
{ | ||
// Code size 161 (0xa1) | ||
// Code size 167 (0xa7) |
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.
The reason for this change is that we no longer choose the (inverted) linear rewriter for or
patterns so the result is not a direct boolean value.
{ | ||
public static bool Test(int a) | ||
{ | ||
return (a is 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8); |
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.
nit: Consider adding a similar test with non-contiguous values (should emit a switch IL instruction if they are packed enough, or a binary tree of conditions if they are further apart).
} | ||
} | ||
"""; | ||
var compilation = CompileAndVerify(source); |
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 also verify execution
Comment also applies to string test below #Closed
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.
LGTM Thanks (iteration 3) with minor test comments
src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_IsPatternOperator.cs
Outdated
Show resolved
Hide resolved
var isPatternRewriter = new IsPatternExpressionLinearLocalRewriter(node, this); | ||
result = isPatternRewriter.LowerIsPatternAsLinearTestSequence(node, decisionDag, whenTrueLabel: node.WhenTrueLabel, whenFalseLabel: node.WhenFalseLabel); | ||
isPatternRewriter.Free(); | ||
result = _factory.Literal(false); |
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.
var isPatternRewriter = new IsPatternExpressionLinearLocalRewriter(node, this); | ||
result = isPatternRewriter.LowerIsPatternAsLinearTestSequence(node, decisionDag, whenTrueLabel: node.WhenTrueLabel, whenFalseLabel: node.WhenFalseLabel); | ||
isPatternRewriter.Free(); | ||
result = _factory.Literal(false); |
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.
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.
There's two ways an is-expression can result in a constant:
- if the pattern is logically irrefutable (warns - caught by linear rewriter) or impossible (error - won't reach codegen)
- same as above but due to a constant input (warns for both cases)
In the latter, the linear check would catch both and produce a constant, but since we're removing the inverted one, constant false needs to be handled separately.
There's a few tests for either scenario none of which verifies codegen except IsNot_12
. I'll add more in the next iteration.
@@ -17,20 +17,22 @@ public override BoundNode VisitIsPatternExpression(BoundIsPatternExpression node | |||
BoundDecisionDag decisionDag = node.GetDecisionDagForLowering(_factory.Compilation); | |||
bool negated = node.IsNegated; | |||
BoundExpression result; | |||
if (canProduceLinearSequence(decisionDag.RootNode, whenTrueLabel: node.WhenTrueLabel, whenFalseLabel: node.WhenFalseLabel)) | |||
if (IsFailureNode(decisionDag.RootNode, node.WhenFalseLabel)) |
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.
Keeping the previous logic here but limiting the inverted scenario to immediate failures to not capture an or
sequence.
/azp run |
Azure Pipelines successfully started running 2 pipeline(s). |
// If the given pattern always fails due to a constant input (see comments on BoundDecisionDag.SimplifyDecisionDagIfConstantInput), | ||
// we build a linear test sequence with the whenTrue and whenFalse labels swapped and then negate the result, to keep the result a constant. | ||
// Note that the positive case will be handled by canProduceLinearSequence above, however, we avoid to produce a full inverted linear sequence here | ||
// because we may be able to generate better code for a sequence of `or` patterns, using a switch dispatch, for example, which is done in the general rewriter. | ||
negated = !negated; |
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.
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 would be IsNot_18
. Let me know if there's any other scenario you want to verify. Thanks.
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.
Took me a while to understand what you were concerning about.
- In the first
if
we check if there's a single path to the true label with all the other paths leading to false label. - In the second
if
we check if there's a direct path to the false label (in which case there would be no true label).
We still need to negate the result here because whenTrue and whenFalse are swapped for the linear rewriter as it expects whenTrue to be the "final" leaf node. Of course nothing about that has changed in this PR beyond limiting the scenario that leads to this code path.
Done with review pass (commit 12) |
@@ -4186,7 +4186,99 @@ .maxstack 2 | |||
[Fact()] | |||
public void IsWarningSwitchEmit() |
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.
This doesn't have much utility beyond comparing the result. Let me know if I should revert since large codegen tests are generally avoided.
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.
LGTM Thanks (iteration 12)
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.
LGTM (commit 12)
Fixes #45679