Skip to content

Call Enter/LeaveRegion APIs for binary expressions - #76412

Merged
RikkiGibson merged 8 commits into
dotnet:mainfrom
RikkiGibson:binary-region
Dec 17, 2024
Merged

RikkiGibson merged 8 commits into
dotnet:mainfrom
RikkiGibson:binary-region

Conversation

@RikkiGibson

Copy link
Copy Markdown
Member

Closes #38087

@ghost ghost added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 13, 2024
protected virtual void VisitBinaryOperatorChildren(ArrayBuilder<BoundBinaryOperator> stack)
{
var binary = stack.Pop();
EnterRegionIfNeeded(binary);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't call 'VisitAlways' on the binary nodes themselves to avoid recursion. Therefore we need to take care to call Enter/LeaveRegion APIs appropriately when we start/finish with those nodes.

See also #74317 for a non-recursive visitor which makes similar calls.

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

Thanks. I can also hellp with adding an ExtractMethod test if you need it.

do
{
stack.Push(binary);
EnterRegionIfNeeded(binary);

@RikkiGibson RikkiGibson Dec 13, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We need to take care that Enter is called for all the binary ops, from outermost to innermost, and then Leave is called from innermost to outermost. Most convenient way to do this is to call Enter right after Push, then Leave right before "Pop or exit".

@RikkiGibson
RikkiGibson marked this pull request as ready for review December 13, 2024 20:44
@RikkiGibson
RikkiGibson requested a review from a team as a code owner December 13, 2024 20:44
@RikkiGibson

Copy link
Copy Markdown
Member Author

@CyrusNajmabadi If you have some time feel free to push an ExtractMethod test to this PR.

@CyrusNajmabadi
CyrusNajmabadi requested a review from a team as a code owner December 13, 2024 21:02
@RikkiGibson

Copy link
Copy Markdown
Member Author

@dotnet/roslyn-compiler for review of a small fix

@AlekseyTs

Copy link
Copy Markdown
Contributor

Does VB have the same issue?

@RikkiGibson

Copy link
Copy Markdown
Member Author

VB implementation appears to be accounting for this when visiting binary expressions.

' As we emulate visiting, enter the region if needed.
' We shouldn't do this for the top most node,
' VisitBinaryOperator caller such as VisitRvalue(...) does this.
If Me._regionPlace = RegionPlace.Before Then
If stack.Count > 0 Then
' Skipping the first node
Debug.Assert(stack(0) Is node)
For i As Integer = 1 To stack.Count - 1
If stack(i) Is Me._firstInRegion Then
Me.EnterRegion()
GoTo EnteredRegion
End If
Next
' Note, the last binary operator is not pushed to the stack, it is stored in [binary].
Debug.Assert(binary IsNot node)
If binary Is Me._firstInRegion Then
Me.EnterRegion()
End If
EnteredRegion:
Else
Debug.Assert(binary Is node)
End If
End If

But, at a glance, I didn't find a test in RegionAnalysisTests.vb which is doing a dataflow analysis on a nested binary operator.

@AlekseyTs

Copy link
Copy Markdown
Contributor

Consider adding a test

@AlekseyTs

Copy link
Copy Markdown
Contributor

Done with review pass (commit 7)

@AlekseyTs AlekseyTs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM (commit 8)

@RikkiGibson
RikkiGibson requested a review from a team December 16, 2024 20:01
@jaredpar jaredpar removed the untriaged Issues and PRs which have not yet been triaged by a lead label Dec 16, 2024
@RikkiGibson
RikkiGibson merged commit b2b9e32 into dotnet:main Dec 17, 2024
@RikkiGibson
RikkiGibson deleted the binary-region branch December 17, 2024 22:40
@dotnet-policy-service dotnet-policy-service Bot added this to the Next milestone Dec 17, 2024
@dibarbet dibarbet modified the milestones: Next, 17.13 P3 Jan 7, 2025
AlekseyTs pushed a commit that referenced this pull request Oct 20, 2026
…86100)

Fixes #86099

Since #67913, `AbstractFlowPass.VisitCall` walks a chain of receiver
calls with an explicit stack, so the receiver calls never go through
`VisitAlways`. When the region passed to `AnalyzeDataFlow` is one of
those receivers, the region is never entered and the analysis returns
`Succeeded == false` with empty results.

This calls `EnterRegionIfNeeded` for each receiver call on the way down
the chain and `LeaveRegionIfNeeded` after each one is visited, like
#76412 did for binary operators. The outermost call still goes through
`VisitAlways`, so its region is left only after the state is restored
for omitted calls.

Added `Repro_86099`, `CallChainReceivers` and
`CallChainReceiverWithOutArgument` to `RegionAnalysisTests`. All three
fail without the change and pass with it. The rest of the Emit3 tests
pass on macOS arm64 (net10.0), except 263 tests that fail to start
`ilasm` because only the osx-x64 binary is available here. I haven't run
the tests on Windows.
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.

Extract Method on part of an expression generates invalid code

6 participants