Skip to content
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

Zero init values on demand despite SkipLocalsInit being set #11688

Merged
merged 4 commits into from
Sep 6, 2021
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
3 changes: 2 additions & 1 deletion src/fsharp/IlxGen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4580,8 +4580,9 @@ and GenDefaultValue cenv cgbuf eenv (ty, m) =
// "initobj" (Generated by EmitInitLocal) doesn't work on byref types
// But ilzero(&ty) only gets generated in the built-in get-address function so
// we can just rely on zeroinit of all IL locals.
if realloc && not (IsILTypeByref ilTy) then
if (realloc || not eenv.initLocals) && not (IsILTypeByref ilTy) then
EmitInitLocal cgbuf ilTy locIdx

EmitGetLocal cgbuf ilTy locIdx
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,56 @@ type X () =

.maxstack 6
.locals (int32 V_0)
"""]
""" ]

[<Fact>]
let ``Zero init performed to get defaults despite the attribute``() =
FSharp """
module SkipLocalsInit
open System

[<System.Runtime.CompilerServices.SkipLocalsInit>]
let z () =
let mutable a = Unchecked.defaultof<System.DateTime>
a

[<System.Runtime.CompilerServices.SkipLocalsInitAttribute>]
let x f =
let a = if 1 / 1 = 1 then Nullable () else Nullable 5L
f a |> ignore
"""
|> compile
|> shouldSucceed
|> verifyIL ["""
.locals (valuetype [runtime]System.DateTime V_0)
IL_0000: ldloca.s V_0
IL_0002: initobj [runtime]System.DateTime
IL_0008: ldloc.0
IL_0009: ret
"""

"""
.locals (valuetype [runtime]System.Nullable`1<int64> V_0,
valuetype [runtime]System.Nullable`1<int64> V_1,
!!a V_2)
IL_0000: ldc.i4.1
IL_0001: ldc.i4.1
IL_0002: div
IL_0003: ldc.i4.1
IL_0004: bne.un.s IL_0011

IL_0006: ldloca.s V_1
IL_0008: initobj valuetype [runtime]System.Nullable`1<int64>
IL_000e: ldloc.1
IL_000f: br.s IL_0018

IL_0011: ldc.i4.5
IL_0012: conv.i8
IL_0013: newobj instance void valuetype [runtime]System.Nullable`1<int64>::.ctor(!0)
IL_0018: stloc.0
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"""]
#endif