Closed
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone
Description
Description
When using Span to map a local variable to an array, after a certain number of iterations, changing the local variable does not change the bytes in the Span
int value = 0;
Span<byte> span = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref value, 1));
for (int i = 0; i < numEntries; i++)
{
//value = 0; bug fix for .net 7.x Release
value = random.Next();
file.Write(span);
}
Reproduction Steps
using System.Runtime.InteropServices;
#if DEBUG
string config = "Deb";
#else
string config = "Rel";
#endif
string template = "{0}.{1}.dat.{2}";
test(5000, 0);
test(5000, 1);
test(5000, 2);
cmp(0, 1);
cmp(0, 2);
cmp(1, 2);
void cmp(int fIdx, int sIdx)
{
byte[] f = File.ReadAllBytes(string.Format(template, config, Environment.Version, fIdx));
byte[] s = File.ReadAllBytes(string.Format(template, config, Environment.Version, sIdx));
for (int i = 0; i < f.Length; i++)
{
if (f[i] != s[i])
{
Console.WriteLine("first diff {0} ~ {1}, offset={2}, {3}/{4}", f[i], s[i], i, string.Format(template, config, Environment.Version, fIdx), string.Format(template, config, Environment.Version, sIdx));
return;
}
}
Console.WriteLine("{0}/{1} is equal", string.Format(template, config, Environment.Version, fIdx), string.Format(template, config, Environment.Version, sIdx));
}
void test(long numEntries, int idx)
{
Random random = new Random(1);
using var file = File.OpenWrite(string.Format(template, config, Environment.Version, idx));
file.SetLength(0);
int value = 0;
Span<byte> span = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref value, 1));
for (int i = 0; i < numEntries; i++)
{
//value = 0; bug fix for .net 7.x Release
value = random.Next();
file.Write(span);
}
}
Expected behavior
all output files must be identical
Actual behavior
Results
first diff 15 ~ 222, offset=19996, Rel.7.0.9.dat.0/Rel.7.0.9.dat.1
first diff 96 ~ 241, offset=3996, Rel.7.0.9.dat.0/Rel.7.0.9.dat.2
first diff 96 ~ 241, offset=3996, Rel.7.0.9.dat.1/Rel.7.0.9.dat.2
Deb.7.0.9.dat.0/Deb.7.0.9.dat.1 is equal
Deb.7.0.9.dat.0/Deb.7.0.9.dat.2 is equal
Deb.7.0.9.dat.1/Deb.7.0.9.dat.2 is equal
Rel.6.0.20.dat.0/Rel.6.0.20.dat.1 is equal
Rel.6.0.20.dat.0/Rel.6.0.20.dat.2 is equal
Rel.6.0.20.dat.1/Rel.6.0.20.dat.2 is equal
Regression?
No response
Known Workarounds
No response
Configuration
.net 6.0.20
.net 7.0.9
Windows Server 2022
Other information
No response
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Activity
MichalPetryka commentedon Jul 29, 2023
Does this reproduce with
DOTNET_TieredCompilation=0? Could you check on .Net 8 too?This looks like an OSR bug, cc @AndyAyersMS
AndyAyersMS commentedon Jul 29, 2023
Yes, it does.
@check4game thank you for reporting this.
AndyAyersMS commentedon Jul 29, 2023
Also repros in 8.0.
AndyAyersMS commentedon Jul 30, 2023
@dotnet/jit-contrib feel free to poach this one if you want to gain some experience with OSR.
AndyAyersMS commentedon Jul 31, 2023
The bug is in this bit of code. For OSR we don't have accurate ref counts like we do for regular methods, since some of the uses may be in the Tier0 part of the method.
runtime/src/coreclr/jit/liveness.cpp
Lines 1576 to 1599 in e42429e
Fix is to simply exclude OSR locals here.
JIT: don't dead store untracked OSR locals based on ref count
JIT: don't dead store OSR exposed locals (#89743)
JIT: don't dead store OSR exposed locals
JIT: don't dead store OSR exposed locals
JIT: don't dead store OSR exposed locals (#89867)