Improve static compilation of state machines#19297
Open
majocha wants to merge 16 commits intodotnet:mainfrom
Open
Improve static compilation of state machines#19297majocha wants to merge 16 commits intodotnet:mainfrom
majocha wants to merge 16 commits intodotnet:mainfrom
Conversation
Contributor
❗ Release notes required
|
Improve reduction of resumable code in state machines Enhance application reduction in state machine lowering for F# computation expressions by tracking let-bound resumable code in the environment and resolving references during reduction. This enables correct handling of optimizer-generated continuations and deeper reduction of nested applications. Also, update test comments to reflect resolved state machine compilation issues.
Contributor
Author
|
Looks like this now to some point reinvents #14930. I'll try to include some of the relevant repros in tests. Too bad they are scattered across issues and comments. |
Contributor
Author
|
Another observation: Lack of FS3511 warning does not mean the state machine actually compiled statically. Sometimes the fallback to dynamic implementation gives no warning. |
majocha
commented
Feb 17, 2026
| // The tasks below used to fail state machine compilation. This failure was causing subsequent problems in code generation. | ||
| // See https://github.com/dotnet/fsharp/issues/13404 | ||
|
|
||
| #nowarn "3511" // state machine not statically compilable - this is a separate issue, see https://github.com/dotnet/fsharp/issues/13404 |
Contributor
Author
There was a problem hiding this comment.
This issue is fixed now.
majocha
commented
Feb 17, 2026
| } | ||
| """ | ||
| |> compile | ||
| |> verifyIL [ ".override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext" ] |
Contributor
Author
There was a problem hiding this comment.
State machines sometimes fallback to dynamic implmentation without FS3511 warning. Checking for generated MoveNext is more reliable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #19296, also includes test cases from #14930, brazenly vibecoded.
However I think the risk of changes here is under control, given that we have quite a few relevant projects and their tests in the regression matrix.
OK here goes AI summary:
This pull request improves the F# compiler's handling of state machine lowering, especially for cases involving resumable code and control flow constructs like
if __useResumableCode. It fixes issues where inlined helpers or nested resumable code constructs could incorrectly fall back to dynamic branches at runtime, and adds comprehensive test coverage for these scenarios. The main focus is on ensuring that statically-compilable state machines are correctly recognized and optimized, and that code generation remains robust for complex patterns.Key changes include:
Compiler logic improvements:
LowerStateMachines.fsto correctly handle resumable code bindings found inside debug points, ensuring that nested resumable code constructs are properly expanded.if __useResumableCode.Test suite enhancements:
FailingInlinedHelper) and test case to verify that inlined helpers containingif __useResumableCodeare expanded correctly, addressing a real-world bug. [1] [2]Documentation/test comments:
NestedTaskFailures.fstest to reflect that certain state machine failures are now fixed, clarifying the status of known issues.Fixes State machines: low-level resumable code not always expanded correctly, without warning #19296, also includes test cases from Try fix static compilation of state machines #14930