Description
The following piece of code prints 11 11 under Debug mode, but it prints 11 10 under Release mode. Upon observation, we can see that 11 11 is the correct value, and Release mode's strong optimizations incorrectly inlined the stored values.
P p = new P(10);
System.Console.WriteLine(p.i1.data);
System.Console.WriteLine(p[1].data);
struct P
{
public P(int value)
{
i0 = new I(value);
i1 = new I(value + 1);
}
public I i0;
public I i1;
public I this[int index] => System.Runtime.CompilerServices.Unsafe.Add(ref i0, index);
}
readonly struct I
{
public I(int data) => this.data = data;
public readonly int data;
}
The output of the JIT compiler shows that under Release mode, it directly feeds the constants 0xA and 0xB into the WriteLine method (the 0xA value is incorrect):
L000e: mov dword ptr [ebp-8], 0xa
L0015: mov dword ptr [ebp-4], 0xb
L001c: mov ecx, [ebp-4]
L001f: call System.Console.WriteLine(Int32)
L0024: mov ecx, [ebp-8]
L0027: call System.Console.WriteLine(Int32)
SharpLab
Reproduction Steps
- Create a
dotnet project using the most recent commit on the main branch as of this writing (5961604).
- Copy and paste the code snippet.
- Build and run the project in
Debug mode.
- Build and run the project in
Release mode.
- Observe the difference.
Expected behavior
The build under Release mode prints 11 10.
Actual behavior
It should print 11 11.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
Description
The following piece of code prints
11 11underDebugmode, but it prints11 10underReleasemode. Upon observation, we can see that11 11is the correct value, andReleasemode's strong optimizations incorrectly inlined the stored values.The output of the JIT compiler shows that under
Releasemode, it directly feeds the constants0xAand0xBinto theWriteLinemethod (the0xAvalue is incorrect):SharpLab
Reproduction Steps
dotnetproject using the most recent commit on themainbranch as of this writing (5961604).Debugmode.Releasemode.Expected behavior
The build under
Releasemode prints11 10.Actual behavior
It should print
11 11.Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response