Repository navigation
Closed
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIbug
Milestone
Description
Description
The function F, below, returns 1 when run in Release mode on .NET 5 on x64 Win 10.
It is expected to return 0 - which it does indeed return in both Debug configuration, and also under .NET Core 3.1.
using System;
namespace Miscompile
{
class Program
{
static void Main()
{
int result = F();
if (result == 0)
{
Console.WriteLine("OK");
}
else
{
Console.WriteLine($"FAILED! result = {result}");
}
}
// When compiled in Release mode, this function returns 1 under .NET 5
// Expect it to return 0, which is the case under .NET Core 3.1
private static int F()
{
bool x = false;
int c = 0;
try
{
while (true)
{
// It appears this assignment is erroneously removed or hoisted out of loop?
c = 0;
if (!x)
{
c++;
x = true;
}
else
{
return c;
}
// Uncommenting the below fixes issue, even if the assignment
// to c at loop start is commented out
// c = 0;
}
}
finally
{
// finally block appears necessary to
// reproduce the error
// Commenting out the finally block, the if expression,
// or even just the WriteLine statement fixes the problem
if (c != 0)
{
Console.WriteLine("c not zero");
}
}
}
}
}Configuration
.NET Version: .NET 5
OS: Windows 10
Arch: x64
Does not occur on .NET Core 3.1 or below.
Does not occur in Debug builds.
Not tested on other architectures or operating systems.
Regression?
Yes - worked in .NET 3.1 and before.
Other information
Workarounds - see the comments in code above. There are two minor alterations that stop it miscompiling.
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIbug
Type
Projects
- StatusShow more project fieldsDone
Activity
EgorBo commentedon Feb 17, 2021
A bit simplified repro:
Here is the asm diff https://www.diffchecker.com/yYXyTBgm
Left: SSA-phase is disabled, Right: SSA is enabled (by default)
Left one produces the expected output.
(I also disabled finally-cloning for both to make asm less verbose)
As @james-hunt-ra pointed out,
c=0is removed from loop (but it's notPHASE_OPTIMIZE_BRANCHESorPHASE_HOIST_LOOP_CODErelated)/cc @dotnet/jit-contrib
AndyAyersMS commentedon Feb 17, 2021
Seems like it could be the new in 5.0 optimization that removes redundant zero inits (#36918, and subsequent PRs like #37786, #38314).
AndyAyersMS commentedon Feb 17, 2021
The redundant zero init opt is checking
lvLiveInOutOfHndlrbut only after possibly optimizing away a store.Wonder if the last bool in this clause was meant to be
&&? cc @erozenfeldruntime/src/coreclr/jit/optimizer.cpp
Lines 9475 to 9478 in 9195df6
erozenfeld commentedon Feb 17, 2021
We should never remove zero inits that are in a loop so these lines should be:
AndyAyersMS commentedon Feb 18, 2021
Thanks Eugene... let me see what this does.
AndyAyersMS commentedon Feb 18, 2021
Seeing a fair number of diffs.
Will take a while to sort which of these are fixes and which of these are us now being too cautious, and to assess how much of the original opt is now undone.
AndyAyersMS commentedon Feb 18, 2021
@EgorBo is going to follow up here.
erozenfeld commentedon Feb 18, 2021
Slightly less conservative check is:
similar to what we do in
fgVarNeedsExplicitZeroIniterozenfeld commentedon Feb 18, 2021
@EgorBo ping me if you have questions about the algorithm
EgorBo commentedon Mar 15, 2021
Closed by #48810 (5.0) and #48571 (main)