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

Expand CORINFO_HELP_CHKCASTANY cast #86728

Merged
merged 2 commits into from May 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/coreclr/jit/importer.cpp
Expand Up @@ -5487,6 +5487,14 @@ GenTree* Compiler::impCastClassOrIsInstToTree(
{
// Jit can only inline expand CHKCASTCLASS and CHKCASTARRAY helpers.
canExpandInline = (helper == CORINFO_HELP_CHKCASTCLASS) || (helper == CORINFO_HELP_CHKCASTARRAY);
Copy link
Member

Choose a reason for hiding this comment

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

This inlining can be profitable only when the type we are casting to is a class. It is not profitable when the type we are casting to is not a class. For example, it is not profitable when we are casting to IEnumerable<Canon> - it is a pure overhead in that case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Presumably the overhead is small or can be eliminated completely if we introduce a slightly stripped version of fallback?

I'll check the stats which path is more often taken in a large service like you requested

Copy link
Member

Choose a reason for hiding this comment

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

My primary point was that the inlining of the exact check is not profitable when you can statically tell at JIT time that the target is not a class.

You are right that we can go even further and introduce specialized helper that omits the exact class check. I am not sure whether it is worth it given the overall cost of the helper.

Copy link
Member

Choose a reason for hiding this comment

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

For example, we should not be inlining the exact check in:

[MethodImpl(MethodImplOptions.NoInlining)]
public IEnumrable<T> GenericCast<T>(object o) => (IEnumerable<T>)o;


// For ChkCastAny we ignore cases where the class is known to be abstract or is an interface.
if (helper == CORINFO_HELP_CHKCASTANY)
{
const bool isAbstract = (info.compCompHnd->getClassAttribs(pResolvedToken->hClass) &
(CORINFO_FLG_INTERFACE | CORINFO_FLG_ABSTRACT)) != 0;
canExpandInline = !isAbstract;
}
}
else if ((helper == CORINFO_HELP_ISINSTANCEOFCLASS) || (helper == CORINFO_HELP_ISINSTANCEOFARRAY))
jkotas marked this conversation as resolved.
Show resolved Hide resolved
{
Expand Down Expand Up @@ -5625,7 +5633,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree(
if (isCastClass)
{
assert((helper == CORINFO_HELP_CHKCASTCLASS) || (helper == CORINFO_HELP_CHKCASTARRAY) ||
(helper == CORINFO_HELP_CHKCASTINTERFACE));
(helper == CORINFO_HELP_CHKCASTANY) || (helper == CORINFO_HELP_CHKCASTINTERFACE));
jkotas marked this conversation as resolved.
Show resolved Hide resolved

CorInfoHelpFunc specialHelper = helper;
if ((helper == CORINFO_HELP_CHKCASTCLASS) &&
Expand Down