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

Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 38 additions & 6 deletions src/coreclr/jit/fgbasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,12 +915,14 @@ void Compiler::fgFindJumpTargets(const BYTE* codeAddr, IL_OFFSET codeSize, Fixed
var_types varType = DUMMY_INIT(TYP_UNDEF); // TYP_ type
bool typeIsNormed = false;
FgStack pushedStack;
const bool isForceInline = (info.compFlags & CORINFO_FLG_FORCEINLINE) != 0;
const bool isInlining = compIsForInlining();
unsigned retBlocks = 0;
int prefixFlags = 0;
bool preciseScan = makeInlineObservations && compInlineResult->GetPolicy()->RequiresPreciseScan();
const bool resolveTokens = preciseScan;
const bool isForceInline = (info.compFlags & CORINFO_FLG_FORCEINLINE) != 0;
const bool isInlining = compIsForInlining();
unsigned retBlocks = 0;
int prefixFlags = 0;
bool preciseScan = makeInlineObservations && compInlineResult->GetPolicy()->RequiresPreciseScan();
const bool resolveTokens = preciseScan;
bool isReturnsArrayKnown = false;
bool returnsArray = false;

// Track offsets where IL instructions begin in DEBUG builds. Used to
// validate debug info generated by the JIT.
Expand Down Expand Up @@ -2441,6 +2443,36 @@ void Compiler::fgFindJumpTargets(const BYTE* codeAddr, IL_OFFSET codeSize, Fixed
retBlocks++;
break;

case CEE_NEWARR:

if (makeInlineObservations)
{
if (!isReturnsArrayKnown)
{
if (info.compRetType == TYP_REF)
{
CORINFO_CLASS_HANDLE retClass = info.compMethodInfo->args.retTypeClass;
if (retClass != NO_CLASS_HANDLE)
{
uint32_t retClassAttribs = info.compCompHnd->getClassAttribs(retClass);
returnsArray = (retClassAttribs & CORINFO_FLG_ARRAY) != 0;
}
}
isReturnsArrayKnown = true;
}

if (returnsArray && pushedStack.IsStackAtLeastOneDeep())
{
FgStack::FgSlot slot0 = pushedStack.GetSlot0();

if (FgStack::IsConstantOrConstArg(slot0, impInlineInfo))
{
compInlineResult->Note(InlineObservation::CALLEE_MAY_RETURN_SMALL_ARRAY);
}
}
}
break;

default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/inline.def
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ INLINE_OBSERVATION(IS_SIZE_DECREASING_INLINE, bool, "size decreasing inline",
INLINE_OBSERVATION(LOG_REPLAY_ACCEPT, bool, "accepted by log replay", INFORMATION, CALLEE)
INLINE_OBSERVATION(LOOKS_LIKE_WRAPPER, bool, "thin wrapper around a call", INFORMATION, CALLEE)
INLINE_OBSERVATION(MAXSTACK, int, "maxstack", INFORMATION, CALLEE)
INLINE_OBSERVATION(MAY_RETURN_SMALL_ARRAY, bool, "may return a small new array", INFORMATION, CALLEE)
INLINE_OBSERVATION(OPCODE, int, "next opcode in IL stream", INFORMATION, CALLEE)
INLINE_OBSERVATION(OPCODE_NORMED, int, "next opcode in IL stream", INFORMATION, CALLEE)
INLINE_OBSERVATION(NUMBER_OF_ARGUMENTS, int, "number of arguments", INFORMATION, CALLEE)
Expand Down
11 changes: 11 additions & 0 deletions src/coreclr/jit/inlinepolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,10 @@ void ExtendedDefaultPolicy::NoteBool(InlineObservation obs, bool value)
m_ArgUnboxExact++;
break;

case InlineObservation::CALLEE_MAY_RETURN_SMALL_ARRAY:
m_MayReturnSmallArray = true;
break;

default:
DefaultPolicy::NoteBool(obs, value);
break;
Expand Down Expand Up @@ -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.

{
multiplier += 4.0;
JITDUMP("\nInline candidate may return small known-size array. Multiplier increased to %g.", multiplier);
}

if (m_HasProfileWeights)
{
// There are cases when Profile Data can be misleading or polluted:
Expand Down Expand Up @@ -1889,6 +1899,7 @@ void ExtendedDefaultPolicy::OnDumpXml(FILE* file, unsigned indent) const
XATTR_B(m_IsCallsiteInNoReturnRegion)
XATTR_B(m_HasProfileWeights)
XATTR_B(m_InsideThrowBlock)
XATTR_B(m_MayReturnSmallArray)
}
#endif

Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/inlinepolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class ExtendedDefaultPolicy : public DefaultPolicy
, m_NonGenericCallsGeneric(false)
, m_IsCallsiteInNoReturnRegion(false)
, m_HasProfileWeights(false)
, m_MayReturnSmallArray(false)
{
// Empty
}
Expand Down Expand Up @@ -281,6 +282,7 @@ class ExtendedDefaultPolicy : public DefaultPolicy
bool m_NonGenericCallsGeneric : 1;
bool m_IsCallsiteInNoReturnRegion : 1;
bool m_HasProfileWeights : 1;
bool m_MayReturnSmallArray : 1;
};

// DiscretionaryPolicy is a variant of the default policy. It
Expand Down
Loading