-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix type initialization ordering under R2R (#84275)
Expand the fix in #80485 to cover helpers including common ones for R2R as well as some generic helpers for JIT. This would cause regressions (100ks code size in diffs) if done alone, so it also includes checks on whether a helper may trigger a cctor and whether it is beforefieldinit. The precise ordering of the fix is only required for a non-beforefieldinit ctor. This recovers the (small) losses from #80485 and mitigates this fix. As before, the primary diff is moving type initialization into (for correctness) or out of (the mitigation optimization) a JIT tree. These leads to secondary effects as values may need to be preserved around the type initialization code. The end result of the fix+mitigations is very little code size change, but viewing the diffs suggests possible future code improvements. However, the impact of any of these should be established first. - A type initializer will never invoke itself - When type initialization is removed/CSEed late, it is too late for forward subst/etc., so the loss of the intact tree can still impact code generation - A custom calling convention for the static helpers (tuned to the common case of no type initialization needing to occur) could help with the impact of calling it in the middle of a calculation - Code can use beforefieldinit in more place Fixes #84007
- Loading branch information
Showing
5 changed files
with
148 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/tests/Loader/classloader/TypeInitialization/CctorsWithSideEffects/CctorForWrite2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
// Showed a JIT importer bug where a tree would be constructed for | ||
// call / stsfld and then the type initialization call inserted before | ||
// the entire tree. The call needs to happen before type initialization. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
public class CorrectException : Exception | ||
{ | ||
} | ||
|
||
public class CCC | ||
{ | ||
public static int Main() => ClassCallingCctor<object>.Test(); | ||
} | ||
|
||
public class ClassCallingCctor<T> | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static int Call() => throw new CorrectException(); | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
internal static int Test() | ||
{ | ||
try | ||
{ | ||
ClassWithCctor<T>.StaticField = Call(); | ||
} | ||
catch (CorrectException) | ||
{ | ||
return 100; | ||
} | ||
catch | ||
{ | ||
} | ||
return 1; | ||
} | ||
} | ||
|
||
public class ClassWithCctor<T> | ||
{ | ||
public static int StaticField; | ||
static ClassWithCctor() => throw new Exception(); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/tests/Loader/classloader/TypeInitialization/CctorsWithSideEffects/CctorForWrite2.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="$(MSBuildProjectName).cs" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters