Skip to content

Method changes behaviour after optimisation #95394

Closed

Description

@ojb500

Description

Apologies if this is not in the right repo.

On migrating from .NET 6 to .NET 8 we noticed some failing tests.

It would appear that the behaviour of one of our methods changes after tiered compilation.

The issue only appears in release and goes away if MethodImpl.NoOptimization is applied to the method or <TieredCompilation>false</TieredCompilation> is set in the project file.

Reproduction Steps

A minimal console app that demonstrates the problem is at: https://github.com/ojb500/optimization-problem

Expected behavior

The results of the method calls should be consistent.

Actual behavior

The method starts to return an incorrect result after a few calls.

Regression?

Last known to be working with SDK 6.0.417 and runtime 6.0.25.

Known Workarounds

No response

Configuration

OS: Windows 10 Enterprise 22H2 build 19045.3693
win-x64

(We also see this on our build servers which have a different configuration)

❯ dotnet --list-sdks
6.0.417 [C:\Program Files\dotnet\sdk]
8.0.100 [C:\Program Files\dotnet\sdk]
❯ dotnet --list-runtimes
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other information

No response

Activity

  1. ghost added
    untriaged
    on Nov 29, 2023
  2. ghost added
    needs-area-label
    on Nov 29, 2023
  3. changed the title [-]Optimised [/-] [+]Method changes behaviour after optimisation[/+] on Nov 29, 2023
  4. added
    area-CodeGen-coreclr
    and removed
    needs-area-label
    on Nov 29, 2023
  5. EgorBo commented on Nov 29, 2023

    @EgorBo
    Member

    Seems to be PGO related? the virtual call looks to be the culprit

    cc @dotnet/jit-contrib

  6. added this to the 9.0.0 milestone on Nov 29, 2023
  7. removed
    untriaged
    on Nov 29, 2023
  8. AndyAyersMS commented on Nov 29, 2023

    @AndyAyersMS
    Member

    @EgorBo are you looking deeper?

  9. EgorBo commented on Nov 29, 2023

    @EgorBo
    Member

    @EgorBo are you looking deeper?

    @AndyAyersMS Haven't looked deeply yet, but created a bit more reduced repro: https://gist.github.com/EgorBo/ce16d475e6f77c9c8bbde1d71c9b0552 the bug is in the Test method (different behaviour between Tier1-PGO and other tiers).

  10. AndyAyersMS commented on Nov 29, 2023

    @AndyAyersMS
    Member

    I can't repro this with the reduced version, but I can with the original.

  11. EgorBo commented on Nov 29, 2023

    @EgorBo
    Member
  12. AndyAyersMS commented on Nov 29, 2023

    @AndyAyersMS
    Member

    Thanks.

    Fails with OSR disabled, and since the patchpoint in Test is at offset 0 the OSR and TIer1 codegen are very similar, save for prolog/epilog.

  13. 2 remaining items

  14. EgorBo commented on Nov 29, 2023

    @EgorBo
    Member

    I recall we changed some of the spill analysis in .NET 8 so that might also be relevant.

    Perhaps, #73233 ?

    Looks like it's not, I checked locally

  15. AndyAyersMS commented on Nov 30, 2023

    @AndyAyersMS
    Member

    I think the issue is that when we're importing the call we haven't yet added the return buffer, so we don't realize that the call interferes with the struct on the stack; we only add the return buffer later once we see how the call result is used, in impStoreStruct, and we spill then since we now see the interference, but we've already appended the call, since it was an inline candidate. So the spill ends up after the call instead of before.

    Not sure of the right fix just yet, we could always put the struct return into a new temp for instance and then rely on forward sub to get rid of the temp in most cases, or maybe try and surgically spill before the call (seems tricky), or else defer splitting inline candidates out of their constituent trees.

    Also not sure when this bug was introduced, it could be from relatively recent changes or it may be something pre-existing that is just exposed now because we're able to do more inlining.

  16. AndyAyersMS commented on Nov 30, 2023

    @AndyAyersMS
    Member

    Looks like this is an old bug that has just now surfaced:

    • This repros in 7.0 with TieredPGO=1.
    • This repros in 6.0 with TieredPGO=1, TC_QuickJitForLoops=1
    • This also repros in main
  17. AndyAyersMS commented on Nov 30, 2023

    @AndyAyersMS
    Member

    Seems like GDV also plays a key role here. Without GDV, we tentatively form an out-of-order looking sequence like:

    t = call(...);
    temp = t;
    ret-expr
    

    But if we then inline, this becomes something like

    q = ...  (inline stuff, into return value temp)
    temp = t;
    t = q;  
    

    where the assignment happens at the ret-expr location, and if we fail to inline, we move the call itself to the ret-expr position and do a similar assignment.

    temp = t;
    t = call(...);
    

    With GDV though we "hoist" the return value assignments up and we don't get this magic re-ordering.

  18. AndyAyersMS commented on Dec 1, 2023

    @AndyAyersMS
    Member

    we could always put the struct return into a new temp for instance

    Tried this but ran into various complications, and the copies do not go away easily... perhaps fwd sub or something should look for cases where it can de-overlap lifetimes but that doesn't happen now.

    try and surgically spill before the call (seems tricky),

    Have been looking at this and the mechanical part seems doable, and fixes the particular repro -- basically if we generate any spills of the local we're about to sink into the call, we move all those statements to just before the call (which will be the root of some prior statement).

    However I am hitting issues with recursive spilling, if we are at the end of a block and need to spill a struct GT_RET_EXPR to a spill temp we form a store to the temp and then invoke impStoreStruct which sees the store is to a temp and tries to spill it; the second spill should not do anything as that temp won't be on the eval stack, but the spill code is blocked on entry by the recursion guard.

    So I may need a prepass to avoid trying to spill in cases like this where there won't be anything to spill.

  19. added a commit that references this issue on Dec 2, 2023
    60638b8
  20. ghost added
    in-pr
    on Dec 2, 2023
  21. added a commit that references this issue on Dec 4, 2023
    dcf2241
  22. ghost removed
    in-pr
    on Dec 4, 2023
  23. added a commit that references this issue on Dec 4, 2023
    fd254c8
  24. AndyAyersMS commented on Dec 4, 2023

    @AndyAyersMS
    Member

    @ojb500 thank you for reporting this.

    It is fixed in our mainline and I have requested the fix to be ported back to 8.0. Best guess is that this would show up in a February servicing release. I will keep you posted on its progress.

  25. ojb500 commented on Dec 4, 2023

    @ojb500
    Author

    Thank you for the speedy resolution and all the great work on .NET generally in recent years 👍

  26. added a commit that references this issue on Jan 2, 2024
    14a32f8
  27. locked and limited conversation to collaborators on Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrbug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions