Skip to content

Do not skip locals init in state machines #14984

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

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Compiler/CodeGen/IlxGen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,12 @@ let AddStorageForLocalWitness eenv (w, s) =
let AddStorageForLocalWitnesses witnesses eenv =
(eenv, witnesses) ||> List.fold AddStorageForLocalWitness

let ForceInitLocals eenv =
if eenv.initLocals then
eenv
else
{ eenv with initLocals = true }

//--------------------------------------------------------------------------
// Lookup eenv
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -5934,6 +5940,9 @@ and GenStructStateMachine cenv cgbuf eenvouter (res: LoweredStateMachine) sequel
let eenvinner =
AddTemplateReplacement eenvinner (templateTyconRef, ilCloTypeRef, cloinfo.cloFreeTyvars, templateTypeInst)

// In MoveNext we're relying on default initialization of locals, so force it in spite of the presence of any SkipLocalsInit
let eenvinner = ForceInitLocals eenvinner

let infoReader = InfoReader.InfoReader(g, cenv.amap)

// We codegen the IResumableStateMachine implementation for each generated struct type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,37 @@ IL_0019: ldarg.0
IL_001a: ldloc.0
IL_001b: callvirt instance !1 class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<valuetype [runtime]System.Nullable`1<int64>,!!a>::Invoke(!0)
IL_0020: stloc.2
IL_0021: ret"""]
IL_0021: ret"""]

[<FSharp.Test.FactForNETCOREAPP>]
let ``Zero init performed in state machine MoveNext despite the attribute``() =
FSharp """
module SkipLocalsInit
open System

[<System.Runtime.CompilerServices.SkipLocalsInit>]
let compute () =
task {
try
do! System.Threading.Tasks.Task.Delay 10
with e ->
printfn "%s" (e.ToString())
}
"""
|> compile
|> shouldSucceed
|> verifyIL ["""
.override [runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext

.maxstack 5
.locals init (int32 V_0,
"""

"""
.method public static class [runtime]System.Threading.Tasks.Task`1<class [FSharp.Core]Microsoft.FSharp.Core.Unit>
compute() cil managed
{
.custom instance void [runtime]System.Runtime.CompilerServices.SkipLocalsInitAttribute::.ctor() = ( 01 00 00 00 )

.maxstack 4
.locals (valuetype SkipLocalsInit/compute@7 V_0,"""]
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,26 @@ foo()
|> fun f -> f.Wait()
"""
|> verify3511AndRun
|> shouldSucceed

[<FSharp.Test.FactForNETCOREAPP>] // https://github.com/dotnet/fsharp/issues/13386
let ``SkipLocalsInit does not cause an exception``() =
FSharp """
module TestProject1

[<System.Runtime.CompilerServices.SkipLocalsInit>]
let compute () =
task {
try
do! System.Threading.Tasks.Task.Delay 10
with e ->
printfn "%s" (e.ToString())
}

// multiple invocations to trigger tiered compilation
for i in 1 .. 100 do
compute().Wait ()
"""
|> withOptimize
|> compileExeAndRun
|> shouldSucceed