Skip to content

Commit b970b2f

Browse files
lewingCopilot
andcommitted
Lower Wasm R2R delegate constructors to R2R helpers
Replace the Crossgen GetDelegateCtor override (which returned CoreLib MethodDescs across module boundaries) with a new JIT-EE callback, getDelegateCtorHelper, that selects a fixed managed helper: - CORINFO_HELP_DELEGATE_CTOR_CLOSED (Delegate.CtorClosed) for closed non-virtual, non-generic reference-type instance targets. - CORINFO_HELP_DELEGATE_CONSTRUCT (Delegate.DelegateConstruct) for all other shapes; this is the managed implementation of every delegate ctor. The helpers are exposed as READYTORUN_HELPER_DelegateConstruct/ DelegateCtorClosed (R2R 30.1), so images stay valid across split composite and non-cross-module-inlining builds. Removes the dedicated root provider and manifest token pre-creation. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 4dd9dfb0-13d5-4a8c-9419-3f808cbd9b8b
1 parent 197dc72 commit b970b2f

36 files changed

Lines changed: 293 additions & 154 deletions

‎src/coreclr/inc/corinfo.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ enum CorInfoHelpFunc
593593
CORINFO_HELP_ALLOC_CONTINUATION_METHOD,
594594
CORINFO_HELP_ALLOC_CONTINUATION_CLASS,
595595

596+
// Managed delegate constructors called directly where dynamically composed delegate constructor helpers are unavailable
597+
CORINFO_HELP_DELEGATE_CONSTRUCT, // General delegate constructor (Delegate.DelegateConstruct)
598+
CORINFO_HELP_DELEGATE_CTOR_CLOSED, // Closed instance delegate constructor (Delegate.CtorClosed)
599+
596600
CORINFO_HELP_COUNT,
597601
};
598602

@@ -2774,6 +2778,15 @@ class ICorStaticInfo
27742778
CORINFO_LOOKUP * pLookup
27752779
) = 0;
27762780

2781+
// Returns the managed helper to call in place of a delegate constructor on targets that
2782+
// cannot use dynamically composed delegate constructor helpers. The helper takes the same
2783+
// (delegate, target, methodPtr) arguments as the constructor. targetMethod may be NULL when
2784+
// the target is not known. Returns CORINFO_HELP_UNDEF to keep the original constructor call.
2785+
virtual CorInfoHelpFunc getDelegateCtorHelper(
2786+
CORINFO_CLASS_HANDLE delegateType,
2787+
CORINFO_METHOD_HANDLE targetMethod
2788+
) = 0;
2789+
27772790
// This function tries to initialize the class (run the class constructor).
27782791
// this function returns whether the JIT must insert helper calls before
27792792
// accessing static field or method.

‎src/coreclr/inc/icorjitinfoimpl_generated.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ void getReadyToRunDelegateCtorHelper(
312312
CORINFO_METHOD_HANDLE callerHandle,
313313
CORINFO_LOOKUP* pLookup) override;
314314

315+
CorInfoHelpFunc getDelegateCtorHelper(
316+
CORINFO_CLASS_HANDLE delegateType,
317+
CORINFO_METHOD_HANDLE targetMethod) override;
318+
315319
CorInfoInitClassResult initClass(
316320
CORINFO_FIELD_HANDLE field,
317321
CORINFO_METHOD_HANDLE method,

‎src/coreclr/inc/jiteeversionguid.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737

3838
#include <minipal/guid.h>
3939

40-
constexpr GUID JITEEVersionIdentifier = { /* fa0c6a6f-b219-4b60-b928-042c72667eb3 */
41-
0xfa0c6a6f,
42-
0xb219,
43-
0x4b60,
44-
{0xb9, 0x28, 0x04, 0x2c, 0x72, 0x66, 0x7e, 0xb3}
40+
constexpr GUID JITEEVersionIdentifier = { /* a072db52-dff2-4c6e-a281-d61ba1cdb315 */
41+
0xa072db52,
42+
0xdff2,
43+
0x4c6e,
44+
{0xa2, 0x81, 0xd6, 0x1b, 0xa1, 0xcd, 0xb3, 0x15}
4545
};
4646

4747
#endif // JIT_EE_VERSIONING_GUID_H

‎src/coreclr/inc/jithelpers.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@
368368
DYNAMICJITHELPER(CORINFO_HELP_ALLOC_CONTINUATION_METHOD, NULL, METHOD__ASYNC_HELPERS__ALLOC_CONTINUATION_METHOD)
369369
DYNAMICJITHELPER(CORINFO_HELP_ALLOC_CONTINUATION_CLASS, NULL, METHOD__ASYNC_HELPERS__ALLOC_CONTINUATION_CLASS)
370370

371+
DYNAMICJITHELPER(CORINFO_HELP_DELEGATE_CONSTRUCT, NULL, METHOD__DELEGATE__CONSTRUCT_DELEGATE)
372+
DYNAMICJITHELPER(CORINFO_HELP_DELEGATE_CTOR_CLOSED, NULL, METHOD__DELEGATE__CTOR_CLOSED)
373+
371374
#undef JITHELPER
372375
#undef DYNAMICJITHELPER
373376
#undef JITHELPER

‎src/coreclr/inc/readytorun.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// If you update this, ensure you run `git grep MINIMUM_READYTORUN_MAJOR_VERSION`
2121
// and handle pending work.
2222
#define READYTORUN_MAJOR_VERSION 30
23-
#define READYTORUN_MINOR_VERSION 0x0000
23+
#define READYTORUN_MINOR_VERSION 0x0001
2424

2525
#define MINIMUM_READYTORUN_MAJOR_VERSION 26
2626

@@ -78,6 +78,7 @@
7878
// consumes the scaffolding, so the flag is only ever set on WebAssembly images.
7979
// R2R Version 29.3 adds READYTORUN_HELPER_BulkWriteBarrierSmall.
8080
// R2R Version 30 requires implicit byref arguments to always be outside of the GC heap
81+
// R2R Version 30.1 adds READYTORUN_HELPER_DelegateConstruct and READYTORUN_HELPER_DelegateCtorClosed
8182

8283
struct READYTORUN_CORE_HEADER
8384
{
@@ -521,6 +522,10 @@ enum ReadyToRunHelper
521522
READYTORUN_HELPER_InitClass = 0x116,
522523
READYTORUN_HELPER_InitInstClass = 0x117,
523524
READYTORUN_HELPER_R2RToInterpreter = 0x118,
525+
526+
// Managed delegate constructors for targets without dynamically composed delegate constructor helpers
527+
READYTORUN_HELPER_DelegateConstruct = 0x119,
528+
READYTORUN_HELPER_DelegateCtorClosed = 0x11A,
524529
};
525530

526531
#include "readytoruninstructionset.h"

‎src/coreclr/inc/readytorunhelpers.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ HELPER(READYTORUN_HELPER_GetCurrentManagedThreadId, CORINFO_HELP_GETCURRENTMANAG
134134
HELPER(READYTORUN_HELPER_AllocContinuation, CORINFO_HELP_ALLOC_CONTINUATION, )
135135
HELPER(READYTORUN_HELPER_AllocContinuationClass, CORINFO_HELP_ALLOC_CONTINUATION_CLASS, )
136136
HELPER(READYTORUN_HELPER_AllocContinuationMethod, CORINFO_HELP_ALLOC_CONTINUATION_METHOD, )
137+
138+
HELPER(READYTORUN_HELPER_DelegateConstruct, CORINFO_HELP_DELEGATE_CONSTRUCT, )
139+
HELPER(READYTORUN_HELPER_DelegateCtorClosed, CORINFO_HELP_DELEGATE_CTOR_CLOSED, )
137140
HELPER(READYTORUN_HELPER_InitClass, CORINFO_HELP_INITCLASS, )
138141
HELPER(READYTORUN_HELPER_InitInstClass, CORINFO_HELP_INITINSTCLASS, )
139142

‎src/coreclr/jit/ICorJitInfo_names_generated.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ DEF_CLR_API(getStringChar)
7777
DEF_CLR_API(getObjectType)
7878
DEF_CLR_API(getReadyToRunHelper)
7979
DEF_CLR_API(getReadyToRunDelegateCtorHelper)
80+
DEF_CLR_API(getDelegateCtorHelper)
8081
DEF_CLR_API(initClass)
8182
DEF_CLR_API(classMustBeLoadedBeforeCodeIsRun)
8283
DEF_CLR_API(getBuiltinClass)

‎src/coreclr/jit/ICorJitInfo_wrapper_generated.hpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,16 @@ void WrapICorJitInfo::getReadyToRunDelegateCtorHelper(
725725
API_LEAVE(getReadyToRunDelegateCtorHelper);
726726
}
727727

728+
CorInfoHelpFunc WrapICorJitInfo::getDelegateCtorHelper(
729+
CORINFO_CLASS_HANDLE delegateType,
730+
CORINFO_METHOD_HANDLE targetMethod)
731+
{
732+
API_ENTER(getDelegateCtorHelper);
733+
CorInfoHelpFunc temp = wrapHnd->getDelegateCtorHelper(delegateType, targetMethod);
734+
API_LEAVE(getDelegateCtorHelper);
735+
return temp;
736+
}
737+
728738
CorInfoInitClassResult WrapICorJitInfo::initClass(
729739
CORINFO_FIELD_HANDLE field,
730740
CORINFO_METHOD_HANDLE method,

‎src/coreclr/jit/flowgraph.cpp‎

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,59 +1131,75 @@ GenTree* Compiler::fgOptimizeDelegateConstructor(GenTreeCall* call,
11311131
}
11321132

11331133
#ifdef FEATURE_READYTORUN
1134-
if (IsAot() && IsTargetAbi(CORINFO_NATIVEAOT_ABI))
1134+
if (IsAot())
11351135
{
1136-
if (ldftnToken != nullptr)
1136+
if (IsTargetAbi(CORINFO_NATIVEAOT_ABI))
11371137
{
1138-
JITDUMP("optimized\n");
1139-
1140-
GenTree* thisPointer = call->gtArgs.GetThisArg()->GetNode();
1141-
GenTree* targetObjPointers = call->gtArgs.GetArgByIndex(1)->GetNode();
1142-
CORINFO_LOOKUP pLookup;
1143-
info.compCompHnd->getReadyToRunDelegateCtorHelper(&ldftnToken->m_token, ldftnToken->m_tokenConstraint,
1144-
clsHnd, info.compMethodHnd, &pLookup);
1145-
if (!pLookup.lookupKind.needsRuntimeLookup)
1146-
{
1147-
call = gtNewHelperCallNode(CORINFO_HELP_READYTORUN_DELEGATE_CTOR, TYP_VOID, thisPointer,
1148-
targetObjPointers);
1149-
call->setEntryPoint(pLookup.constLookup);
1150-
}
1151-
else
1138+
if (ldftnToken != nullptr)
11521139
{
1153-
assert(oper != GT_FTN_ADDR);
1154-
1155-
if (pLookup.lookupKind.runtimeLookupKind != CORINFO_LOOKUP_NOT_SUPPORTED)
1140+
JITDUMP("optimized\n");
1141+
1142+
GenTree* thisPointer = call->gtArgs.GetThisArg()->GetNode();
1143+
GenTree* targetObjPointers = call->gtArgs.GetArgByIndex(1)->GetNode();
1144+
CORINFO_LOOKUP pLookup;
1145+
info.compCompHnd->getReadyToRunDelegateCtorHelper(&ldftnToken->m_token, ldftnToken->m_tokenConstraint,
1146+
clsHnd, info.compMethodHnd, &pLookup);
1147+
if (!pLookup.lookupKind.needsRuntimeLookup)
11561148
{
1157-
assert((pLookup.runtimeLookup.indirections == CORINFO_USEHELPER) &&
1158-
(pLookup.runtimeLookup.helper == CORINFO_HELP_READYTORUN_DELEGATE_CTOR));
1159-
GenTree* ctxTree = getRuntimeContextTree(pLookup.lookupKind.runtimeLookupKind);
1160-
call = gtNewHelperCallNode(CORINFO_HELP_READYTORUN_DELEGATE_CTOR, TYP_VOID, thisPointer,
1161-
targetObjPointers, ctxTree);
1162-
call->setEntryPoint(pLookup.runtimeLookup.helperEntryPoint);
1149+
call = gtNewHelperCallNode(CORINFO_HELP_READYTORUN_DELEGATE_CTOR, TYP_VOID, thisPointer,
1150+
targetObjPointers);
1151+
call->setEntryPoint(pLookup.constLookup);
11631152
}
11641153
else
11651154
{
1166-
// Runtime does not support inlining of all shapes of runtime lookups
1167-
// Inlining has to be aborted in such a case
1168-
assert(compIsForInlining());
1169-
compInlineResult->NoteFatal(InlineObservation::CALLSITE_GENERIC_DICTIONARY_LOOKUP);
1170-
JITDUMP("not optimized, generic inlining restriction\n");
1155+
assert(oper != GT_FTN_ADDR);
1156+
1157+
if (pLookup.lookupKind.runtimeLookupKind != CORINFO_LOOKUP_NOT_SUPPORTED)
1158+
{
1159+
assert((pLookup.runtimeLookup.indirections == CORINFO_USEHELPER) &&
1160+
(pLookup.runtimeLookup.helper == CORINFO_HELP_READYTORUN_DELEGATE_CTOR));
1161+
GenTree* ctxTree = getRuntimeContextTree(pLookup.lookupKind.runtimeLookupKind);
1162+
call = gtNewHelperCallNode(CORINFO_HELP_READYTORUN_DELEGATE_CTOR, TYP_VOID, thisPointer,
1163+
targetObjPointers, ctxTree);
1164+
call->setEntryPoint(pLookup.runtimeLookup.helperEntryPoint);
1165+
}
1166+
else
1167+
{
1168+
// Runtime does not support inlining of all shapes of runtime lookups
1169+
// Inlining has to be aborted in such a case
1170+
assert(compIsForInlining());
1171+
compInlineResult->NoteFatal(InlineObservation::CALLSITE_GENERIC_DICTIONARY_LOOKUP);
1172+
JITDUMP("not optimized, generic inlining restriction\n");
1173+
}
11711174
}
11721175
}
1176+
else
1177+
{
1178+
JITDUMP("not optimized, NATIVEAOT no ldftnToken\n");
1179+
}
11731180
}
1181+
#ifdef TARGET_WASM
1182+
// Wasm does not use the dynamically composed delegate constructor helpers. Call a managed
1183+
// helper that takes the same arguments as the constructor instead.
11741184
else
11751185
{
1176-
JITDUMP("not optimized, NATIVEAOT no ldftnToken\n");
1177-
}
1178-
1179-
return call;
1180-
}
1186+
CorInfoHelpFunc helper = info.compCompHnd->getDelegateCtorHelper(clsHnd, targetMethodHnd);
1187+
if (helper != CORINFO_HELP_UNDEF)
1188+
{
1189+
JITDUMP("optimized, Wasm delegate constructor helper %d\n", helper);
11811190

1182-
#ifndef TARGET_WASM
1183-
if (IsAot())
1184-
{
1185-
// ReadyToRun has this optimization for non-virtual function pointers only for now.
1186-
if ((oper == GT_FTN_ADDR) && (ldftnToken != nullptr))
1191+
GenTree* thisPointer = call->gtArgs.GetArgByIndex(0)->GetNode();
1192+
GenTree* targetObjPointers = call->gtArgs.GetArgByIndex(1)->GetNode();
1193+
call = gtNewHelperCallNode(helper, TYP_VOID, thisPointer, targetObjPointers, targetMethod);
1194+
}
1195+
else
1196+
{
1197+
JITDUMP("not optimized, no Wasm delegate constructor helper\n");
1198+
}
1199+
}
1200+
#else
1201+
// ReadyToRun has this optimization for a non-virtual function pointers only for now.
1202+
else if ((oper == GT_FTN_ADDR) && (ldftnToken != nullptr))
11871203
{
11881204
JITDUMP("optimized\n");
11891205

@@ -1201,13 +1217,11 @@ GenTree* Compiler::fgOptimizeDelegateConstructor(GenTreeCall* call,
12011217
{
12021218
JITDUMP("not optimized, R2R virtual case\n");
12031219
}
1204-
1205-
return call;
1220+
#endif
12061221
}
1207-
#endif // !TARGET_WASM
1222+
else
12081223
#endif
1209-
1210-
if (targetMethodHnd != nullptr)
1224+
if (targetMethodHnd != nullptr)
12111225
{
12121226
CORINFO_METHOD_HANDLE alternateCtor = nullptr;
12131227
DelegateCtorArgs ctorData;
@@ -1226,16 +1240,6 @@ GenTree* Compiler::fgOptimizeDelegateConstructor(GenTreeCall* call,
12261240

12271241
call->gtCallMethHnd = alternateCtor;
12281242

1229-
#ifdef FEATURE_READYTORUN
1230-
if (IsAot())
1231-
{
1232-
// The entry point was computed for the original constructor.
1233-
CORINFO_CONST_LOOKUP entryPoint;
1234-
info.compCompHnd->getFunctionEntryPoint(alternateCtor, &entryPoint);
1235-
call->setEntryPoint(entryPoint);
1236-
}
1237-
#endif
1238-
12391243
CallArg* lastArg = nullptr;
12401244
if (ctorData.pArg3 != nullptr)
12411245
{

‎src/coreclr/nativeaot/Runtime/inc/ModuleHeaders.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct ReadyToRunHeaderConstants
1212
static const uint32_t Signature = 0x00525452; // 'RTR'
1313

1414
static const uint32_t CurrentMajorVersion = 30;
15-
static const uint32_t CurrentMinorVersion = 0;
15+
static const uint32_t CurrentMinorVersion = 1;
1616
};
1717

1818
struct ReadyToRunHeader

0 commit comments

Comments
 (0)