[r2r] Add helpers for class init#124649
Merged
BrzVlad merged 3 commits intodotnet:mainfrom Feb 24, 2026
Merged
Conversation
Code needing to check for class init would be skipped for R2R compilation, probably because at runtime we would be likely to detect the check as redundant. On iOS this means that the entire method would be interpreted which is way more expensive than a small check for class initialization. This commit adds a r2r helper for this, which will end up as a call to the standard jit helper: System.Runtime.CompilerServices.Helpers.InitClass/InitInstantiatedClass. In the example below, G<>.Touch was skipped in R2R.
```
public class G<T>
{
static Type theType;
static G()
{
theType = typeof(T);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static Type Touch()
{
return theType;
}
}
G<string>.Touch();
```
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds ReadyToRun (R2R) helper support for class initialization to prevent expensive interpretation on platforms without runtime JIT support (iOS, tvOS, WASM). Previously, methods requiring class initialization checks would be entirely skipped during R2R compilation and interpreted at runtime. With these changes, the class initialization helpers are compiled ahead-of-time on non-JIT platforms while still allowing JIT optimization on platforms that support runtime code generation.
Changes:
- Added two new R2R helper enum values (0x116 and 0x117) for InitClass and InitInstClass across C# and C++ codebases
- Implemented conditional logic to use R2R helpers on iOS/WASM while allowing runtime JIT on other platforms
- Added signature parsing support for the new helper types
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/coreclr/tools/Common/Internal/Runtime/ReadyToRunConstants.cs | Added InitClass (0x116) and InitInstClass (0x117) enum values to ReadyToRunHelper |
| src/coreclr/inc/readytorun.h | Added corresponding C++ enum values READYTORUN_HELPER_InitClass and READYTORUN_HELPER_InitInstClass |
| src/coreclr/inc/readytorunhelpers.h | Mapped new R2R helpers to existing CORINFO_HELP_INITCLASS and CORINFO_HELP_INITINSTCLASS JIT helpers |
| src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunSignature.cs | Added signature parsing cases for INIT_CLASS and INIT_INST_CLASS |
| src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs | Implemented conditional helper resolution based on TargetAllowsRuntimeCodeGeneration flag |
jkotas
reviewed
Feb 20, 2026
src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Feb 24, 2026
jkotas
reviewed
Feb 24, 2026
jkotas
approved these changes
Feb 24, 2026
8 tasks
This was referenced Feb 24, 2026
Open
iremyux
pushed a commit
to iremyux/dotnet-runtime
that referenced
this pull request
Mar 2, 2026
Code needing to check for class init would be skipped for R2R
compilation, probably because at runtime we would be likely to detect
the check as redundant. On iOS this means that the entire method would
be interpreted which is way more expensive than a small check for class
initialization. This commit adds a r2r helper for this, which will end
up as a call to the standard jit helper:
System.Runtime.CompilerServices.Helpers.InitClass/InitInstantiatedClass.
In the example below, G<>.Touch was skipped in R2R.
```
public class G<T>
{
static Type theType;
static G()
{
theType = typeof(T);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static Type Touch()
{
return theType;
}
}
G<string>.Touch();
```
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Code needing to check for class init would be skipped for R2R compilation, probably because at runtime we would be likely to detect the check as redundant. On iOS this means that the entire method would be interpreted which is way more expensive than a small check for class initialization. This commit adds a r2r helper for this, which will end up as a call to the standard jit helper: System.Runtime.CompilerServices.Helpers.InitClass/InitInstantiatedClass. In the example below, G<>.Touch was skipped in R2R.