-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RyuJIT and static fields initialization in beforefieldinited types #4346
Comments
It is intentional change to avoid differences in timing of static constructor invocations across different JIT versions, and between JIT and AOT configurations. These timing differences has been historically source of number of compatibility problems. The behavior is controlled by |
But doesn't this change itself introduce another difference in timing? |
Yes, it does. Our compatibility requirements between different runtimes (.NET Framework vs. CoreCLR) are not as high as compatibility requirements between versions of the same runtime (v1, v2, ...). BTW: The new behavior is enabled under a set of quirks even for .NET 4.5.2. |
@jkotas Does it all mean that in .NET 4.6 RTM static readonly fields of scalar types won't be treated as JIT time constants? |
They should be still treated as JIT time constants in methods that are JITed after the static constructor has run. |
Ahaaa.. Now I see..:eyes: class Program
{
private static bool s_flag = true;
static void Main(string[] args)
{
FirstAccess();
Test();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int FirstAccess()
{
return s_flag ? 33 : 66;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int Test()
{
return s_flag ? 33 : 66;
}
} Test method: G_M65206_IG02:
0FB6057354EAFF movzx rax, byte ptr [classVar[0x755f5cc0]]
85C0 test eax, eax
7506 jne SHORT G_M65206_IG04
B842000000 mov eax, 66
G_M65206_IG03:
C3 ret
G_M65206_IG04:
B821000000 mov eax, 33
G_M65206_IG05:
C3 ret The same method if s_flag is a readonly field: G_M65206_IG02:
B821000000 mov eax, 33
G_M65206_IG03:
C3 ret |
|
I get used to that JIT normally calls static fields initializer on compiling a method where the static fields are used, so that when the method executes it can access the fields directly without checking if they've been initialized.
RyuJIT seems has changed how it deals with initialization of static fields in beforefieldinited types.
Test code:
Generated code:
Is it an intentional and permanent change?
The text was updated successfully, but these errors were encountered: