Closed ryanheath closed 3 years ago
@ryanheath Could you please create a self-contained repro (a console app with Main) because it's not clear what ViewPosition
and PrivateDebtDetail
are - are they structs? what layout, etc.
@EgorBo We have tried to localize the problem in a console app but the problem is not reproduced, not in debug mode, nor release mode, nor with added optimization attributes,
The ViewPosition class is large with lots of properties, see attached source. Program.txt
@ryalanms Thank you for the repro! Slightly more compact:
using System;
using System.Runtime.CompilerServices;
class Program
{
static void Main()
{
Console.WriteLine(Test(null, DateTime.UtcNow, 42));
}
[MethodImpl(MethodImplOptions.NoInlining |
MethodImplOptions.AggressiveOptimization)]
static DateTime Test(DateTime? date1, DateTime date2, int n)
{
if (date1 != null)
return n == 1 ? date2 : date1.Value;
if (n == 42)
return date2;
return date1 ?? date2;
}
}
Under Debug:
2/2/2021 1:17:19 PM
Under Release:
1/1/0001 12:00:00 AM
Doesn't reproduce on netcoreapp3.1
so must be a 5.0
regression.
@EgorBo Great to see you succeed to reproduce it!
@EgorBo can you investigate where things go wrong?
@AndyAyersMS sure!
@AndyAyersMS ah, actually it doesn't reproduce on master (and even on .NET6-preview1 binaries) - but does reproduce on .NET 5 (even 5.0.200) Do you want me to find the exact commit which fixed it (if we want to backport to .NET 5.0) ?
Do you want me to find the exact commit which fixed ...
Yes. I think we'll want to fix this in 5.0.
Since you know which method has bad codegen, you can try diffing the 5.x and 6.x jit dumps to see where they diverge. That will likely pinpoint where the fix happened.
Do you want me to find the exact commit which fixed ...
Yes. I think we'll want to fix this in 5.0.
Since you know which method has bad codegen, you can try diffing the 5.x and 6.x jit dumps to see where they diverge. That will likely pinpoint where the fix happened.
I've bisected the problem and it turns out https://github.com/dotnet/runtime/pull/45134 fixed the issue - the ^ repro used to assert in Checked mode:
[000054] -----+------ * LCL_VAR long V07 tmp3
Assert failure(PID 23208 [0x00005aa8], Thread: 27944 [0x6d28]): Assertion failed '!"Incompatible types for gtNewTempAssign"' in 'Program:Test(System.Nullable`1[DateTime],System.DateTime,int):System.DateTime' during 'Morph - Global' (IL size 51)
File: runtime-2\src\coreclr\jit\gentree.cpp Line: 15490
Image: ConsoleApp11\ConsoleApp11\bin\Release\net5.0\win10-x64\ConsoleApp11.exe
Looks like the fix is already backported and should be available in .NET 5.0.3 https://github.com/dotnet/runtime/pull/45989
Thanks Egor. Looks like 5.0.3 should be available in the next week or so.
@ryanheath when 5.0.3 is out, it would be great if you could verify that it fixes your full app.
Thanks @Egor! @AndyAyersMS I'll test it as soon as it comes out.
@EgorBo @AndyAyersMS The bug is fixed also at our end, thanks!
Configuration
Description
We encountered a bug in optimized code generation, the code produces wrong results, however, when the NoOptimization attribute is applied, the code produces the correct results. Unfortunately, we cannot reproduce the bug on its own. We'll provide the piece of source code, the optimized and no-optimized disassembly of the source.
The piece of code
For example, when this function is called with:
the returned value is 0001-01-04 00:52:06 while it should have been 2021-01-18 00:00
See below the disassembly of the same code piece. We don't really see differences apart from addresses and optimized away method calls.
Disassembly of optimized code generation
Disassembly of noOptimized code generation