Skip to content

Improve elimination of redundant evaluations during pattern matching operation#82142

Merged
AlekseyTs merged 9 commits into
dotnet:mainfrom
AlekseyTs:Issue82063
Feb 4, 2026
Merged

Improve elimination of redundant evaluations during pattern matching operation#82142
AlekseyTs merged 9 commits into
dotnet:mainfrom
AlekseyTs:Issue82063

Conversation

@AlekseyTs

Copy link
Copy Markdown
Contributor

Fixes #82063

@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@RikkiGibson, @333fred, @jcouv, @dotnet/roslyn-compiler Please review

1 similar comment
@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@RikkiGibson, @333fred, @jcouv, @dotnet/roslyn-compiler Please review

@AlekseyTs AlekseyTs requested a review from jcouv January 27, 2026 16:19
Comment thread src/Compilers/CSharp/Test/Emit3/Semantics/PatternMatchingTests_ListPatterns.cs Outdated
@AlekseyTs AlekseyTs requested review from a team and RikkiGibson January 28, 2026 15:15
@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@RikkiGibson, @333fred, @jcouv, @dotnet/roslyn-compiler Please review


if (!dagBuilder._forLowering && lengthValues.Any(BinaryOperatorKind.Equal, lengthValue))
{
dagBuilder._suitableForLowering = false;

@jcouv jcouv Jan 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Consider leaving a comment on why this is not suitable for lowering #Resolved

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This existing existing requirement. I did not add it

<Field Name="IsExplicitTest" Type="bool"/>
</Node>
<Node Name="BoundDagExplicitNullTest" Base="BoundDagTest">
<Node Name="BoundDagExplicitNullTest" Base="BoundDagTest" UpdateMethodModifiers="new">

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: instead of introducing new machinery, consider using a dedicated name like UpdateInput to avoid overloading Update

@AlekseyTs AlekseyTs requested a review from a team January 28, 2026 23:58

@jcouv jcouv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM Thanks (commit 4)

@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@RikkiGibson, @333fred, @dotnet/roslyn-compiler For a second review

@AlekseyTs AlekseyTs requested a review from a team January 29, 2026 00:59
// Then the process of removal made another update to that indexer evaluation.
// This means that the evaluation would somehow take an input that is calculated
// from itself, only then temps update would update the evaluation.
// But a circuarity like this is not possible with indexer evaluations.

@RikkiGibson RikkiGibson Jan 29, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// But a circuarity like this is not possible with indexer evaluations.
// But a circularity like this is not possible with indexer evaluations.
``` #Resolved

@AlekseyTs AlekseyTs enabled auto-merge (squash) January 29, 2026 03:47
@AlekseyTs AlekseyTs disabled auto-merge January 29, 2026 13:46

@333fred 333fred left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I did not review the tests, I'm leaving that to the other approvals.

Comment thread src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs
Comment on lines +2216 to +2217
public readonly Tests? ConditionToUseFinalResult;
public readonly Tests? TempsUpdatedResult;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be useful to write out some of what was mentioned in the overview meeting on what these fields are useful for, why some of the branches need temps removed and some not.

Comment thread src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs Outdated
Comment thread src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs Outdated
@333fred

333fred commented Jan 31, 2026

Copy link
Copy Markdown
Member

Addressed comments look good. Not going to sign off since I didn't review all the tests and you already have 2 approvals, but there's nothing blocking from me.

Unified equivalence implementation with equality implementation for BoundDagTemp and BoundDagEvaluation. This reduces scenarios when temp remapping is necessary. This also improves ability to merge equivalent Dag states. This is what `uniquifyState` function is doing. As a result we create a Decision Dag with less amount of states and emit smaller IL.
@AlekseyTs

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@AlekseyTs

Copy link
Copy Markdown
Contributor Author

@jcouv, @RikkiGibson, @333fred I made some changes since you last look at the PR. I am still investigating Correctness_Rebuild failure. However, it would be good if you could review the changes while I am doing the investigation.

@AlekseyTs AlekseyTs marked this pull request as draft February 3, 2026 01:43
Relaxed equality implementation for BoundDagTemp and BoundDagEvaluation. This reduces scenarios when temp remapping is necessary. This also improves ability to merge equivalent Dag states. This is what `uniquifyState` function is doing. As a result we create a Decision Dag with less states and emit smaller IL.
@AlekseyTs AlekseyTs marked this pull request as ready for review February 3, 2026 15:23
@jcouv

jcouv commented Feb 3, 2026

Copy link
Copy Markdown
Member
                    else if (!typeEval.Input.Equals(e1.Input))

I didn't follow why this case was removed.


Refers to: src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs:2673 in 090043b. [](commit_id = 090043b, deletion_comment = True)

@jcouv jcouv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM Thanks (commit 9)

@jcouv jcouv self-assigned this Feb 3, 2026
@AlekseyTs

Copy link
Copy Markdown
Contributor Author
                    else if (!typeEval.Input.Equals(e1.Input))

I didn't follow why this case was removed.

I changed my mind about doing this specific rewrite. Especially that given the new equality implementation the new type evaluation might be considered equal to the one we are replacing, which makes their result temps equal and the temp remapping would be circular, causing an exception. Rather than complicating the logic, I decided to not do the rewrite because it does not result in complete elimination anyway.


In reply to: 3843329071


Refers to: src/Compilers/CSharp/Portable/Binder/DecisionDagBuilder.cs:2673 in 090043b. [](commit_id = 090043b, deletion_comment = True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code generated by pattern matching includes unnecessary (duplicate) evaluations

5 participants