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: Optimize unused array allocations #67205

Merged
merged 9 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
39 changes: 24 additions & 15 deletions src/coreclr/jit/earlyprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,33 @@ GenTree* Compiler::getArrayLengthFromAllocation(GenTree* tree DEBUGARG(BasicBloc

if (call->gtCallType == CT_HELPER)
{
if (call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_NEWARR_1_DIRECT) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_NEWARR_1_OBJ) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_NEWARR_1_VC) ||
call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_NEWARR_1_ALIGN8))
switch (eeGetHelperNum(call->gtCallMethHnd))
{
// This is an array allocation site. Grab the array length node.
arrayLength = gtArgEntryByArgNum(call, 1)->GetNode();
}
else if (call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_READYTORUN_NEWARR_1))
{
// On arm when compiling on certain platforms for ready to run, a handle will be
// inserted before the length. To handle this case, we will grab the last argument
// as that's always the length. See fgInitArgInfo for where the handle is inserted.
int arrLenArgNum = call->fgArgInfo->ArgCount() - 1;
arrayLength = gtArgEntryByArgNum(call, arrLenArgNum)->GetNode();
case CORINFO_HELP_NEWARR_1_DIRECT:
case CORINFO_HELP_NEWARR_1_OBJ:
case CORINFO_HELP_NEWARR_1_VC:
case CORINFO_HELP_NEWARR_1_ALIGN8:
{
// This is an array allocation site. Grab the array length node.
arrayLength = gtArgEntryByArgNum(call, 1)->GetNode();
break;
}

case CORINFO_HELP_READYTORUN_NEWARR_1:
{
// On arm when compiling on certain platforms for ready to run, a handle will be
// inserted before the length. To handle this case, we will grab the last argument
// as that's always the length. See fgInitArgInfo for where the handle is inserted.
int arrLenArgNum = call->fgArgInfo->ArgCount() - 1;
arrayLength = gtArgEntryByArgNum(call, arrLenArgNum)->GetNode();
break;
}

default:
break;
}
#ifdef DEBUG
if (arrayLength != nullptr)
if ((arrayLength != nullptr) && (block != nullptr))
{
optCheckFlagsAreSet(OMF_HAS_NEWARRAY, "OMF_HAS_NEWARRAY", BBF_HAS_NEWARRAY, "BBF_HAS_NEWARRAY", tree,
block);
Expand Down
22 changes: 22 additions & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,28 @@ bool GenTreeCall::HasSideEffects(Compiler* compiler, bool ignoreExceptions, bool
return true;
}

// Consider array allocators side-effect free for constant length (if it's not negative and fits into i32)
if (helperProperties.IsAllocator(helper))
{
GenTree* arrLen = compiler->getArrayLengthFromAllocation((GenTree*)this DEBUGARG(nullptr));
// if arrLen is nullptr it means it wasn't an array allocator
if ((arrLen != nullptr))
{
arrLen = arrLen->OperIsPutArg() ? arrLen->gtGetOp1() : arrLen;
if (arrLen->IsIntCnsFitsInI32())
{
// Array.MaxLength
const ssize_t MaxArraySize = 0x7FFFFFC7;

ssize_t cns = arrLen->AsIntConCommon()->IconValue();
if ((cns >= 0) && (cns <= MaxArraySize))
{
return false;
}
}
}
}

// If we also care about exceptions then check if the helper can throw
if (!ignoreExceptions && !helperProperties.NoThrow(helper))
{
Expand Down
2 changes: 1 addition & 1 deletion src/tests/GC/API/GC/GetGCMemoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void MakeTemporarySOHAllocations()
int byteArraySize = 1000;
for (int i = 0; i < (totalTempAllocBytes / byteArraySize); i++)
{
byte[] byteArray = new byte[byteArraySize];
GC.KeepAlive(new byte[byteArraySize]);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/tests/profiler/gc/gcallocate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public static int RunTest(String[] args)
{
int[] large = new int[100000];
int[] pinned = GC.AllocateArray<int>(32, true);

// don't let the jit to optimize these allocations
GC.KeepAlive(large);
GC.KeepAlive(pinned);

Console.WriteLine("Test Passed");
return 100;
}
Expand Down