Skip to content

JIT: boost inlining for methods that may return small arrays #114806

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

Conversation

AndyAyersMS
Copy link
Member

Look for inlinees that may be allocating and returning small fixed sized arrays. When inlined, these array allocations may end up non-escaping and be stack allocated.

This analysis is approximate; we can't tell for sure in the IL scan what the array size is, and we can't easily tell if the allocated array is actually returned.

Contributes to #113236

Look for inlinees that may be allocating and returning small fixed sized arrays.
When inlined, these array allocations may end up non-escaping and be stack allocated.

This analysis is approximate; we can't tell for sure in the IL scan what the array
size is, and we can't easily tell if the allocated array is actually returned.

Contributes to dotnet#113236
@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 17, 2025
@AndyAyersMS
Copy link
Member Author

@EgorBo FYI

Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@AndyAyersMS
Copy link
Member Author

@MihuBot

@AndyAyersMS AndyAyersMS marked this pull request as ready for review April 24, 2025 17:44
@Copilot Copilot AI review requested due to automatic review settings April 24, 2025 17:44
@AndyAyersMS
Copy link
Member Author

@EgorBo PTAL
cc @dotnet/jit-contrib

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 PR enhances the JIT inlining heuristics by adding a new observation for methods that may return small fixed-sized arrays, potentially enabling stack allocation of these arrays when inlined.

  • Introduces a new flag (m_MayReturnSmallArray) in the inline policy.
  • Marks inline observations based on array allocation in CEE_NEWARR cases to adjust the inlining multiplier.
  • Updates XML dumping and multiplier determination logic to include the new observation.

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/coreclr/jit/inlinepolicy.h Added a new boolean flag for tracking small array returns.
src/coreclr/jit/inlinepolicy.cpp Implemented observation handling and updated multiplier.
src/coreclr/jit/fgbasic.cpp Integrated logic to detect array allocations in inline code.
Files not reviewed (1)
  • src/coreclr/jit/inline.def: Language not supported
Comments suppressed due to low confidence (1)

src/coreclr/jit/fgbasic.cpp:924

  • [nitpick] Consider renaming 'isReturnsArrayKnown' to 'arrayTypeDetermined' or a similarly descriptive name to more clearly convey its purpose.
bool        isReturnsArrayKnown = false;

@@ -1776,6 +1780,12 @@ double ExtendedDefaultPolicy::DetermineMultiplier()
}
}

if (m_MayReturnSmallArray)
Copy link
Preview

Copilot AI Apr 24, 2025

Choose a reason for hiding this comment

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

It might help maintainability to add a comment explaining why the multiplier is increased by 4.0 when a small array return is observed.

Copilot uses AI. Check for mistakes.

@AndyAyersMS
Copy link
Member Author

Seeing quite a few failures with

Assert failure(PID 4332 [0x000010ec], Thread: 196 [0x00c4]): !"Did findClass() return a Byref?"

CORECLR! CEEInfo::getClassAttribsInternal + 0x34A (0x00007ff9`494486aa)
CORECLR! CEEInfo::getClassAttribs + 0xFA (0x00007ff9`494482da)
CLRJIT! Compiler::fgFindJumpTargets<1> + 0x1DF7 (0x00007ff9`4d34d7f7

@EgorBo
Copy link
Member

EgorBo commented Apr 24, 2025

Seeing quite a few failures with

Assert failure(PID 4332 [0x000010ec], Thread: 196 [0x00c4]): !"Did findClass() return a Byref?"

CORECLR! CEEInfo::getClassAttribsInternal + 0x34A (0x00007ff9`494486aa)
CORECLR! CEEInfo::getClassAttribs + 0xFA (0x00007ff9`494482da)
CLRJIT! Compiler::fgFindJumpTargets<1> + 0x1DF7 (0x00007ff9`4d34d7f7

@AndyAyersMS I presume it fails on ref int[] Callee()

@AndyAyersMS
Copy link
Member Author

Seeing quite a few failures with

Assert failure(PID 4332 [0x000010ec], Thread: 196 [0x00c4]): !"Did findClass() return a Byref?"

CORECLR! CEEInfo::getClassAttribsInternal + 0x34A (0x00007ff9`494486aa)
CORECLR! CEEInfo::getClassAttribs + 0xFA (0x00007ff9`494482da)
CLRJIT! Compiler::fgFindJumpTargets<1> + 0x1DF7 (0x00007ff9`4d34d7f7

@AndyAyersMS I presume it fails on ref int[] Callee()

Yeah likely....

@EgorBo
Copy link
Member

EgorBo commented Apr 24, 2025

Yeah likely....

I wonder if you just can check info.compMethodInfo->args.retType and if it's byref - give up (or if you want to get underlying type - you can then call info.compCompHnd->getChildType on it and continue with getClassAttribs).

I'm actually not sure why getClassAttribs gives up on byref cls handles, but I remember I hit it somewhere else too once.

Copy link
Member

@EgorBo EgorBo left a comment

Choose a reason for hiding this comment

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

LGTM assuming CI passes

@AndyAyersMS
Copy link
Member Author

@MihuBot

@AndyAyersMS AndyAyersMS merged commit 006ad21 into dotnet:main Apr 24, 2025
108 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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.

2 participants