-
Notifications
You must be signed in to change notification settings - Fork 5.1k
JIT: enable stack allocation of more arrays in R2R #116651
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
base: main
Are you sure you want to change the base?
Conversation
In particular arrays that are created via `CORINFO_HELP_READYTORUN_NEWARR_1`. Also: * move inlining boost for "returns small array" to the default policy as well as the extended default policy. This addresses part of dotnet#113236. * enable the stack allocation test for R2R, by marking certain methods where particular inlines are needed for non-escape that won't happen in R2R as aggressive opt. Fixes dotnet#109314. Contributes to dotnet#104936
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Enables stack allocation of arrays created via the CORINFO_HELP_READYTORUN_NEWARR_1
helper, moves the inlining boost for small-array returns into the default inline policy, and re-enables the corresponding R2R stack-allocation tests.
- Introduce a new allocation kind (
OAT_NEWARR_R2R
) and handle it inObjectAllocator
and helper expansion. - Promote the
CALLEE_MAY_RETURN_SMALL_ARRAY
observation into the default inline policy with a higher inline multiplier. - Remove the test exclusion for
ObjectStackAllocationTests
and tag test methods withAggressiveOptimization
so they run under R2R.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
src/tests/issues.targets | Re-enable ObjectStackAllocationTests by removing the exclude. |
src/tests/JIT/opt/ObjectStackAllocation/ObjectStackAllocationTests.cs | Added AggressiveOptimization to force inlining in R2R. |
src/coreclr/jit/objectalloc.h | Added OAT_NEWARR_R2R enum value. |
src/coreclr/jit/objectalloc.cpp | Treat OAT_NEWARR_R2R like OAT_NEWARR and dump a debug message. |
src/coreclr/jit/inlinepolicy.h | Added m_MayReturnSmallArray field to DefaultPolicy . |
src/coreclr/jit/inlinepolicy.cpp | Handle CALLEE_MAY_RETURN_SMALL_ARRAY in NoteBool and boost multiplier. |
src/coreclr/jit/helperexpansion.cpp | Support CORINFO_HELP_READYTORUN_NEWARR_1 in fgExpandStackArrayAllocation . |
@@ -2829,6 +2829,10 @@ bool Compiler::fgExpandStackArrayAllocation(BasicBlock* block, Statement* stmt, | |||
typeArgIndex = 0; | |||
break; | |||
|
|||
case CORINFO_HELP_READYTORUN_NEWARR_1: | |||
lengthArgIndex = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the READYTORUN_NEWARR_1
case, typeArgIndex
is never initialized, leading to undefined behavior when later checking its value; consider setting typeArgIndex = -1
explicitly.
lengthArgIndex = 0; | |
lengthArgIndex = 0; | |
typeArgIndex = -1; |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is initialized to -1
just above.
src/coreclr/jit/objectalloc.cpp
Outdated
if ((call->gtArgs.CountUserArgs() == 1) && call->gtArgs.GetUserArgByIndex(0)->GetNode()->IsCnsIntOrI()) | ||
{ | ||
allocType = OAT_NEWARR_R2R; | ||
JITDUMP("Checking if we can stack allocate array [%06u])\n", comp->dspTreeID(call)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The debug log string has a mismatched parenthesis ([%06u])
), which may confuse readers; consider correcting it to [%06u]
.
JITDUMP("Checking if we can stack allocate array [%06u])\n", comp->dspTreeID(call)); | |
JITDUMP("Checking if we can stack allocate array [%06u]\n", comp->dspTreeID(call)); |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
fyi @dotnet/jit-contrib Note the handle embedding call may fail and so cause R2R compilation of a method to be abandoned. We are probably better off not stack allocating in such cases since the method will end up being jitted at Tier0 and allocate anyways. But I don't think there is any way to tell if an embedding request will fail and if so avoid making it. This doesn't quite fix all of #113236, as the (One might hope that "argument is a small locally allocated array" might be a reasonable inlining boost, but these arrays are span captured so not passed directly). |
Failures in readytorun/tests/mainv1/mainv1.cmd and similar are likely methods unexpectedly dropping out of R2R because of the embedding issue (still need to verify). |
@@ -1449,6 +1476,30 @@ bool ObjectAllocator::MorphAllocObjNodeHelperArr(AllocationCandidate& candidate) | |||
return false; | |||
} | |||
|
|||
// Under R2R, for array allocations that would require handle embedding, check if the embedding will succeed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see what's special about arrays that prevents the array type handle from getting embedded. This sounds like a bug in crossgen2 driver. Do you happen to know where the embedding fails? I am sorry I did not have a chance to take a look at this in detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll work up a simple repro and get add more details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple repro (requires this PR)
class EmbedIssue
{
public static int Main()
{
int[] a = new int[10];
for (int i = 0; i < a.Length; i++)
{
a[i] = i;
}
int sum = 0;
for (int i = 0; i < a.Length; i++)
{
sum += a[i];
}
return sum;
}
}
The JIT proves new int[10]
can't escape; invokes embedClassHandle
on int32[]
, this isn't in the version checked cache, it is a parameterized type, so the next check is check for int32
, this is in the cache and is set to false
, so the whole embedding request fails. I don't know how int32
gets into the cache.
With the latest commit for this PR we try embedding early, catch the failure in the JIT, and continue compiling without stack allocating the array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious, from where is embedClassHandle
called normally that fails the compilation?
Importation of CEE_NEWARR
takes care not to embed the handle. Instead, a call to a new array helper is generated by R2R that only embeds the element type. The array type shows up nowhere in the IL, so there is no token to represent it. I'm not sure that R2R has a fixup required to be able to embed the array class handle, so probably a new one would need to be added.
Edit: although I guess for int[]
specifically it should always be possible to encode its signature...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second call is in helper expansion. If we stack allocate the array we store the method table pointer.
Now it turns out currently that jitted code actually never looks at the method table pointer of a on stack array. But I have been saving it there because the debugger then displays the local properly, and someday we may add support in the runtime helpers for handling on-stack objects so we may see some non-jitted code accesses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think I misunderstood that. The new array helper does embed the array type signature successfully, it seems. It doesn't embed the element type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that R2R has a fixup required to be able to embed the array class handle,
R2R does have a fixup to embed type handles (READYTORUN_FIXUP_TypeHandle
). It is used in other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Importation of CEE_NEWARR takes care not to embed the handle.
This was meant to be an optimization to make the code a bit smaller and to allow building the runtime type only once it is really needed. It is debatable whether this is optimization is worth it. Dynamically emitting the little helper is more expensive than what it used to be when this optimization was introduced b/c WriteXorExecute, and relatively expensive FlushInstructionCache needed on Arm64.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think I understand that better now. The NewArr fixup takes a signature of the array type and crossgen2 ought to be able to pass the exact same signature to the TypeHandle fixup instead.
@dotnet/jit-contrib ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, assuming we don't want to wait for the crossgen2 fix
I would prefer to wait for the proper fix in crossgen2. |
Sounds good, then we can pare back the If you give me some pointers on what to look at here for fixing crossgen2, I can give it a try. |
I think the VersionswWithType check in embedClassHandle is superfluous. Trying to delete in #116808 |
Per @davidwrighton a fix is not likely before the preview 7 snap. |
|
In particular arrays that are created via
CORINFO_HELP_READYTORUN_NEWARR_1
.Also:
Fixes #109314.
Contributes to #104936