Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/coreclr/jit/objectalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2456,25 +2456,28 @@ void ObjectAllocator::RewriteUses()
CallArg* const thisArg = call->gtArgs.GetThisArg();
GenTree* const delegateThis = thisArg->GetNode();

if (delegateThis->OperIs(GT_LCL_VAR))
if (delegateThis->OperIs(GT_LCL_VAR, GT_LCL_ADDR))
{
GenTreeLclVarCommon* const lcl = delegateThis->AsLclVarCommon();
bool const isStackAllocatedDelegate =
delegateThis->OperIs(GT_LCL_ADDR) || m_allocator->DoesLclVarPointToStack(lcl->GetLclNum());

if (m_allocator->DoesLclVarPointToStack(lcl->GetLclNum()))
if (isStackAllocatedDelegate)
{
JITDUMP("Expanding delegate invoke [%06u]\n", m_compiler->dspTreeID(call));

// Expand the delgate invoke early, so that physical promotion has
// a chance to promote the delegate fields.
//
// Note the instance field may also be stack allocatable (someday)
//
Copy link

Copilot AI May 29, 2025

Choose a reason for hiding this comment

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

Passing the 'complexOk' flag as true ensures proper cloning of address nodes; consider adding a brief inline comment to detail the necessity of this flag in handling GT_LCL_ADDR nodes.

Suggested change
//
//
// The 'complexOk' flag is set to true to ensure proper cloning of address nodes,
// particularly for GT_LCL_ADDR nodes, which are crucial for delegate invocation expansion.

Copilot uses AI. Check for mistakes.
GenTree* const cloneThis = m_compiler->gtClone(lcl);
GenTree* const cloneThis = m_compiler->gtClone(lcl, /* complexOk */ true);
unsigned const instanceOffset = m_compiler->eeGetEEInfo()->offsetOfDelegateInstance;
GenTree* const newThisAddr =
m_compiler->gtNewOperNode(GT_ADD, TYP_I_IMPL, cloneThis,
m_compiler->gtNewIconNode(instanceOffset, TYP_I_IMPL));

// For now assume the instance is heap...
// For now assume the instance field is on the heap...
//
GenTree* const newThis = m_compiler->gtNewIndir(TYP_REF, newThisAddr);
thisArg->SetEarlyNode(newThis);
Expand Down
28 changes: 28 additions & 0 deletions src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ static AllocationKind StackAllocation()
return expectedAllocationKind;
}

static AllocationKind HeapAllocation()
{
AllocationKind expectedAllocationKind = AllocationKind.Heap;
if (GCStressEnabled())
{
Console.WriteLine("GCStress is enabled");
expectedAllocationKind = AllocationKind.Undefined;
}
return expectedAllocationKind;
}

static int CallTestAndVerifyAllocation(Test test, int expectedResult, AllocationKind expectedAllocationsKind, bool throws = false)
{
string methodName = test.Method.Name;
Expand Down Expand Up @@ -122,4 +133,21 @@ public static int Test1()
RunTest1();
return CallTestAndVerifyAllocation(RunTest1, 100, StackAllocation());
}

// Here the delegate gets stack allocated, but not the closure.
// With PGO the delegate is also inlined.
//
[MethodImpl(MethodImplOptions.NoInlining)]
static int RunTest2Inner(int a) => InvokeFunc(x => x + a);

static int InvokeFunc(Func<int, int> func) => func(101);

[MethodImpl(MethodImplOptions.NoInlining)]
static int RunTest2() => RunTest2Inner(-1);

[Fact]
public static int Test2()
{
return CallTestAndVerifyAllocation(RunTest2, 100, HeapAllocation());
}
}
Loading