Skip to content

JIT: generalize escape analysis delegate invoke expansion slightly #116099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2401,25 +2401,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
Preview

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
26 changes: 26 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 @@
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,19 @@
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 Test2(int a) => InvokeFunc(x => x + a);

Check failure on line 141 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build coreclr Common Pri0 Test Build AnyOS AnyCPU checked)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L141

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(141,16): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 141 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Release AllSubsets_Mono_LLVMAot_RuntimeTests llvmaot)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L141

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(141,16): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 141 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L141

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(141,16): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 141 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L141

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(141,16): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/Users/runner/work/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 141 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Interpreter_RuntimeTests monointerpreter)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L141

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(141,16): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/Users/runner/work/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

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

[Fact]
public static int Test2()

Check failure on line 146 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build coreclr Common Pri0 Test Build AnyOS AnyCPU checked)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L146

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(146,23): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 146 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Release AllSubsets_Mono_LLVMAot_RuntimeTests llvmaot)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L146

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(146,23): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 146 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L146

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(146,23): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 146 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L146

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(146,23): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/Users/runner/work/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 146 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Interpreter_RuntimeTests monointerpreter)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L146

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(146,23): error xUnit1024: Test method 'Test2' on test class 'Delegates' has the same name as another method declared on class 'Delegates'. Rename method(s) so that there are no overloaded names. (https://xunit.net/xunit.analyzers/rules/xUnit1024) [/Users/runner/work/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]
{
RunTest2();

Check failure on line 148 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build coreclr Common Pri0 Test Build AnyOS AnyCPU checked)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L148

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(148,9): error CS0103: The name 'RunTest2' does not exist in the current context [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 148 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Release AllSubsets_Mono_LLVMAot_RuntimeTests llvmaot)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L148

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(148,9): error CS0103: The name 'RunTest2' does not exist in the current context [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 148 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L148

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(148,9): error CS0103: The name 'RunTest2' does not exist in the current context [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 148 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L148

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(148,9): error CS0103: The name 'RunTest2' does not exist in the current context [/Users/runner/work/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 148 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Interpreter_RuntimeTests monointerpreter)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L148

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(148,9): error CS0103: The name 'RunTest2' does not exist in the current context [/Users/runner/work/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]
return CallTestAndVerifyAllocation(RunTest2, 100, HeapAllocation());

Check failure on line 149 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build coreclr Common Pri0 Test Build AnyOS AnyCPU checked)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L149

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(149,44): error CS0103: The name 'RunTest2' does not exist in the current context [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 149 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Release AllSubsets_Mono_LLVMAot_RuntimeTests llvmaot)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L149

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(149,44): error CS0103: The name 'RunTest2' does not exist in the current context [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 149 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L149

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(149,44): error CS0103: The name 'RunTest2' does not exist in the current context [/__w/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 149 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Minijit_RuntimeTests minijit)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L149

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(149,44): error CS0103: The name 'RunTest2' does not exist in the current context [/Users/runner/work/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]

Check failure on line 149 in src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Release AllSubsets_Mono_Interpreter_RuntimeTests monointerpreter)

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs#L149

src/tests/JIT/opt/ObjectStackAllocation/Delegates.cs(149,44): error CS0103: The name 'RunTest2' does not exist in the current context [/Users/runner/work/1/s/src/tests/JIT/opt/ObjectStackAllocation/Delegates.csproj]
}
}
Loading