Skip to content
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

JIT: enhance escape analysis to understand local struct fields #113977

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

AndyAyersMS
Copy link
Member

Continuation of #113141

Implement a very simplistic "field sensitive" analysis for gc structs where we model each struct as simply its gc field(s).

That is, given a gc struct G with GC field f, we model

G.f = ...

as an assignment to G and

   = G.f

as a read from G. Since we know G itself cannot escape, "escaping" of G means G.f can escape.

Note this conflates the fields of G: either they all escape or none of them do. We could make this more granular but it's not clear that doing so is worthwhile, and it requires more up-front work to determine how to track each field's status.

If the struct or struct fields are only consumed locally, we may be able to prove the gc referents don't escape.

This is a subset/elaboration of #112543 that does not try and reason interprocedurally.

Contributes to #104936 / #108913
Fixes #113236 (once the right inlines happen)

Continuation of dotnet#113141

Implement a very simplistic "field sensitive" analysis for gc structs where we model each struct as simply its gc field(s).

That is, given a gc struct `G` with GC field `f`, we model
```
G.f = ...
```
as an assignment to `G` and
```
   = G.f
```
as a read from `G`. Since we know `G` itself cannot escape, "escaping" of `G` means `G.f` can escape.

Note this conflates the fields of `G`: either they all escape or none of them do. We could make this more granular
but it's not clear that doing so is worthwhile, and it requires more up-front work to determine how to track each field's status.

If the struct or struct fields are only consumed locally, we may be able to prove the gc referents don't escape.

This is a subset/elaboration of dotnet#112543 that does not try and reason interprocedurally.

Contributes to dotnet#104936 / dotnet#108913
Fixes dotnet#113236 (once the right inlines happen)
@Copilot Copilot bot review requested due to automatic review settings March 27, 2025 17:08
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Mar 27, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request enhances the JIT escape analysis by introducing field‐sensitive handling of GC structs and expanding test coverage for various scenarios involving spans and struct escapes. Key changes include the addition of new GC struct definitions and related tests, multiple span capture/escape test methods, and adjustments to the allocation verification logic in the tests.

Files not reviewed (6)
  • src/coreclr/jit/compiler.h: Language not supported
  • src/coreclr/jit/jitconfigvalues.h: Language not supported
  • src/coreclr/jit/layout.cpp: Language not supported
  • src/coreclr/jit/layout.h: Language not supported
  • src/coreclr/jit/objectalloc.cpp: Language not supported
  • src/coreclr/jit/objectalloc.h: Language not supported
Comments suppressed due to low confidence (2)

src/tests/JIT/opt/ObjectStackAllocation/ObjectStackAllocationTests.cs:476

  • The method 'SpanCaptureArray3' is defined but never invoked by any test. Consider adding a test call (via CallTestAndVerifyAllocation) or removing the method if it is not needed.
static int SpanCaptureArray3()

src/tests/JIT/opt/ObjectStackAllocation/ObjectStackAllocationTests.cs:512

  • The 'SpanEscapeArrayReturn' method is defined but not used in any test invocation. Consider adding a corresponding test or removing the method if it is unnecessary.
static int SpanEscapeArrayReturn() => SpanEscapeArrayReturnHelper()[10];

g.a[0] = "Goodbye";
g.b[0] = "Hello";

ref string[] rs = ref g.b;
Copy link
Preview

Copilot AI Mar 27, 2025

Choose a reason for hiding this comment

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

The use of a ref assignment between g.b and later g.a in a conditional block could benefit from a clarifying comment to explain the intended behavior.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

@AndyAyersMS
Copy link
Member Author

@dotnet/jit-contrib PTAL

This refreshes #113141 based on recent BV refactoring and other changes. I have not incorporated all the feedback from that PR yet, so feel free to re-raise.

SPMI should do a decent job here demonstrating impact. I also haven't yet looked through all the regressions in detail.

{
gcType = TYPE_GC_BYREF;
}
SetGCPtr(startSlot + slot, gcType);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as previously.. This feels awfully specific to escape analysis to add in the generic builder type.

@AndyAyersMS
Copy link
Member Author

Current diffs.

Looks like there is a bug to sort out first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Escape Analysis: Elide an array allocation in BitConverter.GetBytes pattern
2 participants