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

Late cast expansion: more improvements #2 #97480

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
102 changes: 62 additions & 40 deletions src/coreclr/jit/helperexpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1797,31 +1797,12 @@ bool Compiler::fgVNBasedIntrinsicExpansionForCall_ReadUtf8(BasicBlock** pBlock,
//
PhaseStatus Compiler::fgLateCastExpansion()
{
if (!doesMethodHaveExpandableCasts())
if (!doesMethodHaveExpandableCasts() || opts.OptimizationDisabled())
{
// Nothing to expand in the current method
return PhaseStatus::MODIFIED_NOTHING;
}

if (!opts.IsOptimizedWithProfile())
{
// Currently, we're only interested in expanding cast helpers using profile data
return PhaseStatus::MODIFIED_NOTHING;
}

if (JitConfig.JitConsumeProfileForCasts() == 0)
{
return PhaseStatus::MODIFIED_NOTHING;
}

const bool preferSize = opts.jitFlags->IsSet(JitFlags::JIT_FLAG_SIZE_OPT);
if (preferSize)
{
// The optimization comes with a codegen size increase
JITDUMP("Optimized for size - bail out.\n");
return PhaseStatus::MODIFIED_NOTHING;
}

// TODO-InlineCast: should we still inline some trivial cases even in cold blocks?
const bool skipForRarelyRunBlocks = true;
return fgExpandHelper<&Compiler::fgLateCastExpansionForCall>(skipForRarelyRunBlocks);
Expand All @@ -1831,6 +1812,7 @@ enum class TypeCheckFailedAction
{
ReturnNull,
CallHelper,
CallHelper_Specialized,
CallHelper_AlwaysThrows
};

Expand Down Expand Up @@ -1947,14 +1929,25 @@ static CORINFO_CLASS_HANDLE PickCandidateForTypeCheck(Compiler* com
const bool isCastToExact = comp->info.compCompHnd->isExactType(castToCls);
if (isCastToExact && ((helper == CORINFO_HELP_CHKCASTCLASS) || (helper == CORINFO_HELP_CHKCASTARRAY)))
{
// (string)obj
// (string[])obj
result = castToCls;

// obj is string
// obj is string[]
//
// Fallbacks for these expansions always throw InvalidCastException
*typeCheckFailed = TypeCheckFailedAction::CallHelper_AlwaysThrows;
if ((helper == CORINFO_HELP_CHKCASTCLASS))
{
// (string)obj
//
// Fallback for this expansion always throws InvalidCastException
// TODO: can we do the same for string[]? (importer did not)
*typeCheckFailed = TypeCheckFailedAction::CallHelper_AlwaysThrows;

// Assume that exceptions are rare
*likelihood = 100;

// Assume that exceptions are rare
*likelihood = 100;
// Update the common denominator class to be more exact
*commonCls = result;
}

// We're done, there is no need in consulting with PGO data
}
Expand All @@ -1965,39 +1958,55 @@ static CORINFO_CLASS_HANDLE PickCandidateForTypeCheck(Compiler* com
// obj is string[]
//
// Fallbacks for these expansions simply return null
// TODO: should we keep the helper call for ISINSTANCEOFARRAY like we do for CHKCASTARRAY above?
// The logic is copied from the importer.
*typeCheckFailed = TypeCheckFailedAction::ReturnNull;

// We're done, there is no need in consulting with PGO data
result = castToCls;
}
else
{
CORINFO_CLASS_HANDLE exactClass = NO_CLASS_HANDLE;
// 2) If VM can tell us the exact class for this "cast to" class - use it.
// Just make sure the class is truly exact.
if ((comp->info.compCompHnd->getExactClasses(castToCls, 1, &result) == 1) &&
comp->info.compCompHnd->isExactType(result))
if ((comp->info.compCompHnd->getExactClasses(castToCls, 1, &exactClass) == 1) &&
comp->info.compCompHnd->isExactType(exactClass))
{
if (isCastClass)
result = exactClass;

if ((helper == CORINFO_HELP_CHKCASTINTERFACE) || (helper == CORINFO_HELP_CHKCASTCLASS))
{
// Fallback call is only needed for castclass and only to throw InvalidCastException
*typeCheckFailed = TypeCheckFailedAction::CallHelper_AlwaysThrows;

// Assume that exceptions are rare
*likelihood = 100;

// Update the common denominator class to be more exact
*commonCls = result;
}
else
else if ((helper == CORINFO_HELP_ISINSTANCEOFINTERFACE) || (helper == CORINFO_HELP_ISINSTANCEOFCLASS))
{
// Fallback for isinst simply returns null here
*typeCheckFailed = TypeCheckFailedAction::ReturnNull;
}

// Update the common denominator class to be more exact
*commonCls = result;
// Update the common denominator class to be more exact
*commonCls = result;
}
}
else
{
// 3) Consult with PGO data
LikelyClassMethodRecord likelyClasses[MAX_GDV_TYPE_CHECKS];
unsigned likelyClassCount =
getLikelyClasses(likelyClasses, MAX_GDV_TYPE_CHECKS, comp->fgPgoSchema, comp->fgPgoSchemaCount,
comp->fgPgoData, (int)castHelper->gtCastHelperILOffset);
unsigned likelyClassCount = 0;

if (comp->opts.IsOptimizedWithProfile() && (JitConfig.JitConsumeProfileForCasts() != 0))
{
const int ilOffset = (int)castHelper->gtCastHelperILOffset;
likelyClassCount = getLikelyClasses(likelyClasses, MAX_GDV_TYPE_CHECKS, comp->fgPgoSchema,
comp->fgPgoSchemaCount, comp->fgPgoData, ilOffset);
}

if (likelyClassCount != 0)
{
Expand Down Expand Up @@ -2152,10 +2161,12 @@ static CORINFO_CLASS_HANDLE PickCandidateForTypeCheck(Compiler* com
unreached();
}

if (isCastClass && (result == castToCls) && (*typeCheckFailed == TypeCheckFailedAction::CallHelper))
if ((helper == CORINFO_HELP_CHKCASTCLASS) && (result == castToCls) &&
(*typeCheckFailed == TypeCheckFailedAction::CallHelper))
{
// TODO-InlineCast: Change helper to faster CORINFO_HELP_CHKCASTCLASS_SPECIAL
// it won't check for null and castToCls assuming we've already done it inline.
// A small optimization - use a slightly faster fallback which assumes that we've already checked
// for null and for castToCls itself so it won't do it again.
*typeCheckFailed = TypeCheckFailedAction::CallHelper_Specialized;
}

assert(result != NO_CLASS_HANDLE);
Expand Down Expand Up @@ -2268,7 +2279,18 @@ bool Compiler::fgLateCastExpansionForCall(BasicBlock** pBlock, Statement* stmt,
}
else
{
GenTree* fallbackTree = gtNewTempStore(tmpNum, call);
GenTreeCall* helperCall = call;
if (typeCheckFailedAction == TypeCheckFailedAction::CallHelper_Specialized)
{
// Re-create the call with a specialized helper
// For JIT we probably can just change the gtCallMethHnd in the existing call, but let's unify with AOT
helperCall = gtNewHelperCallNode(CORINFO_HELP_CHKCASTCLASS_SPECIAL, call->TypeGet(),
helperCall->gtArgs.GetUserArgByIndex(0)->GetNode(),
helperCall->gtArgs.GetUserArgByIndex(1)->GetNode());
fgMorphCall(helperCall);
gtSetEvalOrder(helperCall);
Copy link
Member

Choose a reason for hiding this comment

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

Just curious -- how is NAOT and JIT different here?

Copy link
Contributor

Choose a reason for hiding this comment

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

AOT needs you to set the entry point. This could be done by mutating the node, theoretically.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, but what is setting the entry point here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point - nothing, so that is not the reason then.

Copy link
Member Author

@EgorBo EgorBo Jan 27, 2024

Choose a reason for hiding this comment

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

Ah, looks like it's needed if I were changing entrypoint for CORINFO_HELP_READYTORUN_CHKCAST. Removed and temporarily disabled importer's phase to stress-test the new phase in 8d699cd.

}
GenTree* fallbackTree = gtNewTempStore(tmpNum, helperCall);
fallbackBb = fgNewBBFromTreeAfter(BBJ_ALWAYS, typeCheckBb, fallbackTree, debugInfo, lastBb, true);
}

Expand Down
9 changes: 2 additions & 7 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5462,7 +5462,6 @@ GenTree* Compiler::impCastClassOrIsInstToTree(

// Pessimistically assume the jit cannot expand this as an inline test
bool canExpandInline = false;
bool partialExpand = false;
bool reversedMTCheck = false;
const CorInfoHelpFunc helper = info.compCompHnd->getCastingHelper(pResolvedToken, isCastClass);

Expand Down Expand Up @@ -5559,7 +5558,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree(
//

GenTree* op2Var = op2;
if (isCastClass && !partialExpand && (exactCls == NO_CLASS_HANDLE))
if (isCastClass && (exactCls == NO_CLASS_HANDLE))
{
// if exactCls is not null we won't have to clone op2 (it will be used only for the fallback)
op2Var = fgInsertCommaFormTemp(&op2);
Expand Down Expand Up @@ -5597,11 +5596,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree(
// use the special helper that skips the cases checked by our inlined cast
specialHelper = CORINFO_HELP_CHKCASTCLASS_SPECIAL;
}
condTrue = gtNewHelperCallNode(specialHelper, TYP_REF, partialExpand ? op2 : op2Var, gtClone(op1));
}
else if (partialExpand)
{
condTrue = gtNewHelperCallNode(helper, TYP_REF, op2, gtClone(op1));
condTrue = gtNewHelperCallNode(specialHelper, TYP_REF, op2Var, gtClone(op1));
}
else
{
Expand Down