未验证 提交 448da58c 编写于 作者: K kerams 提交者: GitHub

Do not skip locals init in state machines (#14984)

上级 3d4eb5c0
...@@ -1317,6 +1317,12 @@ let AddStorageForLocalWitness eenv (w, s) = ...@@ -1317,6 +1317,12 @@ let AddStorageForLocalWitness eenv (w, s) =
let AddStorageForLocalWitnesses witnesses eenv = let AddStorageForLocalWitnesses witnesses eenv =
(eenv, witnesses) ||> List.fold AddStorageForLocalWitness (eenv, witnesses) ||> List.fold AddStorageForLocalWitness
let ForceInitLocals eenv =
if eenv.initLocals then
eenv
else
{ eenv with initLocals = true }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// Lookup eenv // Lookup eenv
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
...@@ -5934,6 +5940,9 @@ and GenStructStateMachine cenv cgbuf eenvouter (res: LoweredStateMachine) sequel ...@@ -5934,6 +5940,9 @@ and GenStructStateMachine cenv cgbuf eenvouter (res: LoweredStateMachine) sequel
let eenvinner = let eenvinner =
AddTemplateReplacement eenvinner (templateTyconRef, ilCloTypeRef, cloinfo.cloFreeTyvars, templateTypeInst) 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) let infoReader = InfoReader.InfoReader(g, cenv.amap)
// We codegen the IResumableStateMachine implementation for each generated struct type // We codegen the IResumableStateMachine implementation for each generated struct type
......
...@@ -180,3 +180,36 @@ IL_001a: ldloc.0 ...@@ -180,3 +180,36 @@ 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_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_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,"""]
\ No newline at end of file
...@@ -84,3 +84,25 @@ foo() ...@@ -84,3 +84,25 @@ foo()
""" """
|> verify3511AndRun |> verify3511AndRun
|> shouldSucceed |> 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
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册