@@ -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 {
0 commit comments