Skip to content

SkipLocalsInit condition applies to NetCoreAppCurrent only #54964

Closed

Description

@ViktorHofer

See

<SkipLocalsInit Condition="'$(SkipLocalsInit)' == '' and '$(MSBuildProjectExtension)' == '.csproj' and '$(IsNETCoreAppSrc)' == 'true' and ($([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', '$(NetCoreAppCurrent)')))">true</SkipLocalsInit>

The SkipLocalsInit property is conditioned on NetCoreAppCurrent inbox assets only. Some packages contain both inbox and OOB assets. These optimization will be lost for net6.0 when NetCoreAppCurrent means net7.0 and new packages will be released.

One option would be to support SkipLocalsInit on all .NETCoreApp tfms. Another option would be to not apply SkipLocalsInit to assets that ship in packages as well but which would cause a perf regression.

cc @stephentoub @ericstj @jkotas @eerhardt

Activity

  1. ghost added
    untriaged
    on Jun 30, 2021
  2. added this to the 6.0.0 milestone on Jun 30, 2021
  3. stephentoub commented on Jun 30, 2021

    @stephentoub
    Member

    The SkipLocalsInit property is conditioned on NetCoreAppCurrent inbox assets only

    This correctly reflects the original intent. We audited the libs in the shared framework; we didn't do so for OOBs.

    Some packages contain both inbox and OOB assets.

    Can you elaborate on that? What does it mean for a package to contain an inbox asset?

    One option would be to support SkipLocalsInit on all .NETCoreApp tfms.

    We would need to go through all of our assets that this would apply to, in particular ones with stackallocs, P/Invokes, and in general unsafe code, and make sure everything is sufficiently protected.

    Another option would be to not apply SkipLocalsInit to assets that ship in packages as well but which would cause a perf regression.

    This is where we should start, and then opt things in as we find opportunities / meaningful improvements.

  4. removed
    untriaged
    on Jul 7, 2021
  5. joperezr commented on Aug 5, 2021

    @joperezr
    Member

    Can you elaborate on that? What does it mean for a package to contain an inbox asset?

    I believe that what @ViktorHofer meant here, is that there are some inbox assets that also get shipped via its library's OOB package, for example System.Text.Json. It's net6.0 asset ships inbox, but also gets shipped in the System.Text.Json package since we want to be able to replace it if we want to via the package, and I can see how @ViktorHofer concern is valid in case we were shipping a slightly different asset inbox than the OOB one. That said, I believe that is not the case here, as if I understand correctly then the OOB asset should also use SkipLocalsInit as the "IsNETCoreAppSrc" condition will still evaluate to true.

    @ViktorHofer given the answers from @stephentoub above, are there still concerns with the way this feature works today? If not, let's move this issue out of 6.0.0 in order to track in the future the work to try to enable the feature for assets targeting older frameworks.

  6. ViktorHofer commented on Aug 20, 2021

    @ViktorHofer
    MemberAuthor

    Can you elaborate on that? What does it mean for a package to contain an inbox asset?

    6.0 packages produced in dotnet/runtime contain net6.0 assets even if those ship as part of a shared framework. One example of that is System.Text.Json. During a build, conflict resolution which is part of the sdk trims out assets which are satisfied by the shared framework. Conflict resolution takes the PlatformManifest which lists all assemblies including their assembly and file versions as an input and compares them against references which are added i.e. via a package.

    In the event of servicing, a change in System.Text.Json is delivered via the shared framework and/or via the updated System.Text.Json package. Conflict resolution will make sure that the best compatible asset is chosen.

    This correctly reflects the original intent. We audited the libs in the shared framework; we didn't do so for OOBs.

    Consider the 7.0 package of System.Text.Json which will include both the net6.0 asset as well as the net7.0 asset (according to our package support policy). With the current condition:

    <SkipLocalsInit Condition="'$(SkipLocalsInit)' == '' and '$(MSBuildProjectExtension)' == '.csproj' and '$(IsNETCoreAppSrc)' == 'true' and ($([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', '$(NetCoreAppCurrent)')))">true</SkipLocalsInit>

    SkipLocalsInit is only applied to $(NetCoreAppCurrent) which maps to net7.0 when producing a 7.0 package. As the net6.0 asset is live built as well, it will loose the SkipLocalsInit optimization which it received when $(NetCoreAppCurrent) mapped to net6.0.
    My assumption is that we don't want to regress the previous shipped asset by changing the SkipLocalsInit condition to:

    <SkipLocalsInit Condition="'$(SkipLocalsInit)' == '' and '$(MSBuildProjectExtension)' == '.csproj' and '$(IsNETCoreAppSrc)' == 'true'">true</SkipLocalsInit>

    That way the optimization will still only apply to assemblies which are part of the shared framework but for both the current release and previous releases.

  7. stephentoub commented on Aug 20, 2021

    @stephentoub
    Member

    My assumption is that we don't want to regress the previous shipped asset

    Yes, if that's what's going to happen, we should fix that.

  8. stephentoub commented on Aug 20, 2021

    @stephentoub
    Member
        [Benchmark]
        public int GetValue() => GetValue(42);
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        private int GetValue(int? n)
        {
            if (n.HasValue) return n.Value;
            return -1;
        }

    .NET 5:

    ; Program.GetValue(System.Nullable`1<Int32>)
           sub       rsp,28
           mov       [rsp+38],rdx
           movzx     eax,byte ptr [rsp+38]
           test      eax,eax
           je        short M01_L00
           test      eax,eax
           je        short M01_L01
           mov       eax,[rsp+3C]
           add       rsp,28
           ret
    M01_L00:
           mov       eax,0FFFFFFFF
           add       rsp,28
           ret
    M01_L01:
           call      System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue()
           int       3
    ; Total bytes of code 47

    .NET 6:

    ; Program.GetValue(System.Nullable`1<Int32>)
           mov       [rsp+10],rdx
           cmp       byte ptr [rsp+10],0
           je        short M01_L00
           mov       eax,[rsp+14]
           ret
    M01_L00:
           mov       eax,0FFFFFFFF
           ret
    ; Total bytes of code 23
  9. 8 remaining items

  10. added this to the 6.0.0 milestone on Aug 20, 2021
  11. ViktorHofer commented on Aug 20, 2021

    @ViktorHofer
    MemberAuthor

    Adding the issue back to 6.0 as I believe this needs to be fixed in both main and release/6.0-rc1.

  12. modified the milestones: 6.0.0, 7.0.0 on Aug 23, 2021
  13. ViktorHofer commented on Aug 23, 2021

    @ViktorHofer
    MemberAuthor

    Retargeting to 7.0 as the remaining work is main specific. After #57844 is in, this will be a matter of updating the condition and removing the last and ... part.

  14. ghost added
    in-pr
    on Sep 30, 2021
  15. ghost removed
    in-pr
    on Oct 4, 2021
  16. ghost locked as resolved and limited conversation to collaborators on Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions