Skip to content

[r2r] Add helpers for class init#124649

Merged
BrzVlad merged 3 commits intodotnet:mainfrom
BrzVlad:fix-r2r-class-init
Feb 24, 2026
Merged

[r2r] Add helpers for class init#124649
BrzVlad merged 3 commits intodotnet:mainfrom
BrzVlad:fix-r2r-class-init

Conversation

@BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented Feb 20, 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();

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();
```
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copilot AI review requested due to automatic review settings February 24, 2026 14:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

@BrzVlad BrzVlad merged commit 27689b6 into dotnet:main Feb 24, 2026
106 of 109 checks passed
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();
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants