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();
```
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 |
| break; | ||
|
|
||
| case CorInfoHelpFunc.CORINFO_HELP_INITCLASS: | ||
| if (!((ReadyToRunCompilerContext)_compilation.TypeSystemContext).TargetAllowsRuntimeCodeGeneration) |
There was a problem hiding this comment.
These is no point in enabling this for TargetAllowsRuntimeCodeGeneration only.
This case was NYI for R2R since it is not aligned with how static constructors get triggered for R2R. It is ok to do this as a simple fix everywhere.
Separately, we may want to look at whether the current scheme for triggering static constructors in R2R is the most efficient one. The current scheme was fine tuned for x86/x64 without W^X. It is probably not the best scheme for other arches, in particular the ones that prohibit code generation.
| READYTORUN_HELPER_AllocContinuationClass = 0x114, | ||
| READYTORUN_HELPER_AllocContinuationMethod = 0x115, | ||
|
|
||
| READYTORUN_HELPER_InitClass = 0x116, |
There was a problem hiding this comment.
We usually bump minor R2R version when adding new helpers.
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.