Skip to content
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

Merged
merged 12 commits into from
Jan 12, 2023
Merged

Enable switch dispatch for is-expressions #65874

merged 12 commits into from
Jan 12, 2023

Conversation

alrz
Copy link
Member

@alrz alrz commented Dec 8, 2022

Fixes #45679

@alrz alrz requested a review from a team as a code owner December 8, 2022 17:08
@ghost ghost added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Dec 8, 2022
}
else if (canProduceLinearSequence(decisionDag.RootNode, whenTrueLabel: node.WhenFalseLabel, whenFalseLabel: node.WhenTrueLabel))
Copy link
Member Author

@alrz alrz Dec 8, 2022

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.

bool negated = pattern.IsNegated(out var innerPattern);
BoundDecisionDag decisionDag = DecisionDagBuilder.CreateDecisionDagForIsPattern(
this.Compilation, pattern.Syntax, expression, innerPattern, whenTrueLabel: whenTrueLabel, whenFalseLabel: whenFalseLabel, diagnostics);

Copy link
Member Author

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)
Copy link
Member Author

@alrz alrz Dec 8, 2022

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.

@jcouv jcouv self-assigned this Dec 8, 2022
{
public static bool Test(int a)
{
return (a is 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8);
Copy link
Member

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);
Copy link
Member

@jcouv jcouv Dec 8, 2022

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

Copy link
Member

@jcouv jcouv left a 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

@alrz alrz requested a review from a team December 20, 2022 05:56
@jcouv jcouv requested review from RikkiGibson and 333fred December 20, 2022 07:58
var isPatternRewriter = new IsPatternExpressionLinearLocalRewriter(node, this);
result = isPatternRewriter.LowerIsPatternAsLinearTestSequence(node, decisionDag, whenTrueLabel: node.WhenTrueLabel, whenFalseLabel: node.WhenFalseLabel);
isPatternRewriter.Free();
result = _factory.Literal(false);
Copy link
Contributor

@AlekseyTs AlekseyTs Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result = _factory.Literal(false);

Consider adding a comment explaining what assumption we are making here and why #Closed

var isPatternRewriter = new IsPatternExpressionLinearLocalRewriter(node, this);
result = isPatternRewriter.LowerIsPatternAsLinearTestSequence(node, decisionDag, whenTrueLabel: node.WhenTrueLabel, whenFalseLabel: node.WhenFalseLabel);
isPatternRewriter.Free();
result = _factory.Literal(false);
Copy link
Contributor

@AlekseyTs AlekseyTs Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result = _factory.Literal(false);

Is there a targeted test covering this code path? #Closed

Copy link
Member Author

@alrz alrz Dec 21, 2022

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))
Copy link
Member Author

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.

@alrz alrz requested a review from AlekseyTs January 4, 2023 22:09
@AlekseyTs
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@AlekseyTs AlekseyTs requested a review from jcouv January 8, 2023 00:47
// 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;
Copy link
Contributor

@AlekseyTs AlekseyTs Jan 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

negated = !negated;

Is there a targeted test covering this code path now? #Closed

Copy link
Member Author

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.

Copy link
Member Author

@alrz alrz Jan 12, 2023

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.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 12)

@@ -4186,7 +4186,99 @@ .maxstack 2
[Fact()]
public void IsWarningSwitchEmit()
Copy link
Member Author

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.

@jcouv jcouv added this to the 17.6 milestone Jan 12, 2023
Copy link
Member

@jcouv jcouv left a 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)

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (commit 12)

@jcouv jcouv merged commit e5ef66e into dotnet:main Jan 12, 2023
@ghost ghost modified the milestones: 17.6, Next Jan 12, 2023
@Cosifne Cosifne modified the milestones: Next, 17.6 P1 Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emit switch for suitable 'or' patterns
6 participants