Repository navigation
Optimizer: lower a state machine behind bindings left by an inlined Run - #20846
auduchinok wants to merge 6 commits into
Conversation
✅ Release notes checked
|
T-Gro
left a comment
There was a problem hiding this comment.
🤖 🕵️ AI review — verify independently.
| let eenv = RemoveTemplateReplacement eenv | ||
| GenStructStateMachine cenv cgbuf eenv res sequel | ||
| let (LoweredStateMachine(prefixBindings = prefixBindings)) = res | ||
| GenStructStateMachineAfterPrefix cenv cgbuf eenv prefixBindings res sequel |
There was a problem hiding this comment.
🤖 🕵️ [P1] Nested state-machine arguments read the wrong Data field — this prints 1 instead of 42 with --optimize+ and --optimize-; ILVerify fails with StackUnexpected. The prefix still needs the enclosing template replacement, which has already been removed here.
module IlPrefixProbe
#nowarn "3513"
open Microsoft.FSharp.Core.CompilerServices
open Microsoft.FSharp.Core.CompilerServices.StateMachineHelpers
type Inner(offset: int) =
member _.Offset = offset
member inline this.Run(value: int) =
if __useResumableCode then
__stateMachine<unit, int>
(MoveNextMethodImpl<_>(fun _ -> ()))
(SetStateMachineMethodImpl<_>(fun _ _ -> ()))
(AfterCode<_, _>(fun _ -> value + this.Offset))
else
value + this.Offset
let inline run () =
if __useResumableCode then
__stateMachine<int, int>
(MoveNextMethodImpl<_>(fun _ -> ()))
(SetStateMachineMethodImpl<_>(fun _ _ -> ()))
(AfterCode<_, _>(fun sm ->
sm.Data <- 41
Inner(1).Run(sm.Data)))
else
42
[<EntryPoint>]
let main _ =
let result = run ()
printfn "%d" result
if result = 42 then 0 else 1|
|
||
| and GenStructStateMachineAfterPrefix cenv cgbuf eenv prefixBindings res sequel = | ||
| match prefixBindings with | ||
| | [] -> GenStructStateMachine cenv cgbuf (RemoveTemplateReplacement eenv) res sequel |
There was a problem hiding this comment.
🤖🕵️ [P1] Corrupted Data reads from an independent machine — this prints 1 instead of 42 with --optimize+ and --optimize-; the previous head prints 42.
#nowarn "3513"
open Microsoft.FSharp.Core.CompilerServices
open Microsoft.FSharp.Core.CompilerServices.StateMachineHelpers
[<NoCompilerInlining>]
let sourceMachine () =
let mutable sm = ResumableStateMachine<int>()
sm.Data <- 41
sm
type Inner(offset: int) =
member _.Offset = offset
member inline this.Run(value: int) =
if __useResumableCode then
__stateMachine<unit, int>
(MoveNextMethodImpl<_>(fun _ -> ()))
(SetStateMachineMethodImpl<_>(fun _ _ -> ()))
(AfterCode<_, _>(fun _ -> value + this.Offset))
else
value + this.Offset
let inline run () =
if __useResumableCode then
__stateMachine<string, int>
(MoveNextMethodImpl<_>(fun _ -> ()))
(SetStateMachineMethodImpl<_>(fun _ _ -> ()))
(AfterCode<_, _>(fun _ ->
Inner(1).Run((sourceMachine ()).Data)))
else
42
[<EntryPoint>]
let main _ =
let result = run ()
printfn "%d" result
if result = 42 then 0 else 18002cf6 to
c1ae690
Compare
Build Failure AnalysisSummary - The Windows Visual Studio build fails while compiling Root cause: state-machine lowering exposes ambiguous constrained receiver bindingsThe failing compiler is the PR bootstrap compiler ( Affected errors
Fix attempt - blocked Delivery - none No code was published. A source workaround could explicitly upcast the two receivers, but that would mask an optimizer/codegen regression outside the PR diff and was not published without targeted validation. The workspace has no usable .NET SDK, and this project is part of the Windows-only Visual Studio integration build. Checks - Build evidence
Structured data: {
"artifact_kind": "analysis",
"fix_delivery": "none",
"fix_status": "blocked",
"workflow_artifact": "build-failure-analysis"
}
|
pcshrosbree
left a comment
There was a problem hiding this comment.
I have one evaluation-order question inline, and the second bot repro predates this PR. I built this PR's head (9389f24) and its merge base (fe180de) locally and ran both repros on them.
On the second bot finding (Data read from an independent machine): that repro prints 1 on both builds, with --optimize+ and with --optimize-, so it predates this PR. (SDK 10.0.402 prints 1 with --optimize+ and 42 with --optimize-.) While a template replacement is active, GenNamedTyAppAux swaps any ResumableStateMachine<_> for the template struct, whatever its type argument.
| ||> List.foldBack (fun bind (kept, needed) -> | ||
| let isKept = | ||
| Zset.contains bind.Var needed.FreeLocals | ||
| || not (isExpandVar g bind.Var) && (not (isPureTemp g bind) || Zset.contains bind.Var used) |
There was a problem hiding this comment.
This change makes an effect in the code argument run after the arguments that follow it. Should an effect in an expand-var definition stay in the prefix, or should such a machine take the dynamic path?
For example:
let inline mkCode () =
note "make code" ()
b.Delay(fun () -> b.Return 3)
b.Run(mkCode (), note "line arg" 7)No kept binding needs code, so this drops it and keeps line. takeDefinition then expands code inside MoveNext, and TryReduceApp keeps note "make code" ahead of the body. With this PR's build, the repro below logs line arg, make code, body, with --optimize+ and with --optimize-. The source evaluation order is make code, line arg, body, and SDK 10.0.402's dynamic path (--optimize-) logs that order. The merge-base build logs make code, line arg, make code, body in both modes, so the PR replaces a duplicated effect with a reordered one.
Full repro
module Order
#nowarn "3513"
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
open Microsoft.FSharp.Core.CompilerServices
open Microsoft.FSharp.Core.CompilerServices.StateMachineHelpers
let log = ResizeArray<string>()
[<MethodImpl(MethodImplOptions.NoInlining)>]
let note (s: string) (v: 'a) = log.Add s; v
[<MethodImpl(MethodImplOptions.NoInlining)>]
let start<'M when 'M :> IAsyncStateMachine> (sm: byref<'M>) = sm.MoveNext()
[<Struct; NoEquality; NoComparison>]
type D<'T> = { mutable Result: 'T }
type B() =
member inline _.Delay([<InlineIfLambda>] f: unit -> ResumableCode<'D, 'T>) =
ResumableCode<'D, 'T>(fun sm -> (f ()).Invoke(&sm))
member inline _.Return(v: 'T) =
ResumableCode<D<'T>, 'T>(fun sm -> note "body" (); sm.Data.Result <- v; true)
member inline _.Run(code: ResumableCode<D<'T>, 'T>, [<CallerLineNumber; Optional; DefaultParameterValue(0)>] line: int) : 'T =
if __useResumableCode then
__stateMachine<D<'T>, 'T>
(MoveNextMethodImpl<D<'T>>(fun sm -> __resumeAt sm.ResumptionPoint; code.Invoke(&sm) |> ignore))
(SetStateMachineMethodImpl<D<'T>>(fun _ _ -> ()))
(AfterCode<D<'T>, 'T>(fun sm -> start &sm; note (sprintf "after line=%d" line) sm.Data.Result))
else
note "dynamic" ()
let mutable sm = ResumableStateMachine<D<'T>>()
code.Invoke(&sm) |> ignore
note (sprintf "after line=%d" line) sm.Data.Result
let b = B()
let inline mkCode () =
note "make code" ()
b.Delay(fun () -> b.Return 3)
[<EntryPoint>]
let main _ =
let r = b.Run(mkCode (), note "line arg" 7)
printfn "%d: %s" r (String.concat ", " log)
0| match e with | ||
| | Expr.Let (bind, bodyExpr, _, _) when isExpandVar g bind.Var || isUnusedPureTemp g bind bodyExpr -> | ||
| let env = if isExpandVar g bind.Var then recordDefinition env bind else env | ||
| Some (RewriteExpr (makeRewriteEnv env) bodyExpr) |
There was a problem hiding this comment.
🤖 🕵️ [P2] Removing an address-taken expand-var local breaks optimized compilation: FS0971, Undefined value 'mutable copy'
#nowarn "3513"
open Microsoft.FSharp.Core.CompilerServices
open Microsoft.FSharp.Core.CompilerServices.StateMachineHelpers
type Observer =
[<NoCompilerInlining>]
static member Touch<'T>(value: byref<'T>) : int = 42
let inline touchCopy (value: 'T) =
let mutable copy = value
Observer.Touch(©)
let inline run () =
if __useResumableCode then
__stateMachine<unit, int>
(MoveNextMethodImpl<unit>(fun _ -> ()))
(SetStateMachineMethodImpl<unit>(fun _ _ -> ()))
(AfterCode<unit, int>(fun _ ->
touchCopy (Unchecked.defaultof<ResumableCode<unit, unit>>)))
else
42
printfn "RESULT=%d" (run ())Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
1c15587 to
22c0e02
Compare
Fixes #20836.