Repository navigation
Description
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
[-]Optimised [/-][+]Method changes behaviour after optimisation[/+]EgorBo commentedon Nov 29, 2023
Seems to be PGO related? the virtual call looks to be the culprit
cc @dotnet/jit-contrib
AndyAyersMS commentedon Nov 29, 2023
@EgorBo are you looking deeper?
EgorBo commentedon Nov 29, 2023
@AndyAyersMS Haven't looked deeply yet, but created a bit more reduced repro: https://gist.github.com/EgorBo/ce16d475e6f77c9c8bbde1d71c9b0552 the bug is in the
Testmethod (different behaviour between Tier1-PGO and other tiers).AndyAyersMS commentedon Nov 29, 2023
I can't repro this with the reduced version, but I can with the original.
EgorBo commentedon Nov 29, 2023
@AndyAyersMS ah oops, here is the updated version: https://gist.githubusercontent.com/EgorBo/64d758d2322cbfdf81abb75493833444/raw/50fbd94241638c9ca138b570631036e7efd9044b/repro2.cs
AndyAyersMS commentedon Nov 29, 2023
Thanks.
Fails with OSR disabled, and since the patchpoint in
Testis at offset 0 the OSR and TIer1 codegen are very similar, save for prolog/epilog.2 remaining items
EgorBo commentedon Nov 29, 2023
Looks like it's not, I checked locally
AndyAyersMS commentedon Nov 30, 2023
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.
AndyAyersMS commentedon Nov 30, 2023
Looks like this is an old bug that has just now surfaced:
AndyAyersMS commentedon Nov 30, 2023
Seems like GDV also plays a key role here. Without GDV, we tentatively form an out-of-order looking sequence like:
But if we then inline, this becomes something like
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.
With GDV though we "hoist" the return value assignments up and we don't get this magic re-ordering.
AndyAyersMS commentedon Dec 1, 2023
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.
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_EXPRto a spill temp we form a store to the temp and then invokeimpStoreStructwhich 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.
JIT: fix issue with out of order importer spilling around some calls
JIT: fix issue with out of order importer spilling around some calls (#…
JIT: fix issue with out of order importer spilling around some calls
AndyAyersMS commentedon Dec 4, 2023
@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.
ojb500 commentedon Dec 4, 2023
Thank you for the speedy resolution and all the great work on .NET generally in recent years 👍
JIT: fix issue with out of order importer spilling around some calls (#…