diff --git a/src/coreclr/jit/objectalloc.cpp b/src/coreclr/jit/objectalloc.cpp index 485392d930fabb..87672d0e98f1f9 100644 --- a/src/coreclr/jit/objectalloc.cpp +++ b/src/coreclr/jit/objectalloc.cpp @@ -999,12 +999,23 @@ bool ObjectAllocator::CanLclVarEscapeViaParentStack(ArrayStack* parent case GT_CALL: { - GenTreeCall* asCall = parent->AsCall(); + GenTreeCall* const call = parent->AsCall(); - if (asCall->IsHelperCall()) + if (call->IsHelperCall()) { canLclVarEscapeViaParentStack = - !Compiler::s_helperCallProperties.IsNoEscape(comp->eeGetHelperNum(asCall->gtCallMethHnd)); + !Compiler::s_helperCallProperties.IsNoEscape(comp->eeGetHelperNum(call->gtCallMethHnd)); + } + else if (call->gtCallType == CT_USER_FUNC) + { + // Delegate invoke won't escape the delegate which is passed as "this" + // And gets expanded inline later. + // + if ((call->gtCallMoreFlags & GTF_CALL_M_DELEGATE_INV) != 0) + { + GenTree* const thisArg = call->gtArgs.GetThisArg()->GetNode(); + canLclVarEscapeViaParentStack = thisArg != tree; + } } break; }