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

Fold BOX+ISINST to null if possible #88989

Merged
merged 9 commits into from
Jul 18, 2023
21 changes: 21 additions & 0 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2911,6 +2911,27 @@ int Compiler::impBoxPatternMatch(CORINFO_RESOLVED_TOKEN* pResolvedToken,
case CEE_ISINST:
if (codeAddr + 1 + sizeof(mdToken) + 1 <= codeEndp)
{
// First, let's see if we can fold BOX+ISINST to just null if ISINST is known to return null
// for the given argument. Don't make inline observations for this case.
if ((opts != BoxPatterns::MakeInlineObservation) &&
((impStackTop().val->gtFlags & GTF_SIDE_EFFECT) == 0))
{
if ((opts == BoxPatterns::IsByRefLike) ||
Copy link
Member

Choose a reason for hiding this comment

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

Should we do this for byref-like types? Note that the exact patterns that are supported for them is documented here and I think it is expected that other patterns continue throwing (or, alternatively, that the pattern is documented).

A separate question -- doesn't this logic essentially subsume the logic that happens below?

Copy link
Member Author

@EgorBo EgorBo Jul 18, 2023

Choose a reason for hiding this comment

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

A separate question -- doesn't this logic essentially subsume the logic that happens below?

Sort of, in this case we only handle MustNot case while the logic below handles both Must and MustNot - I tried to share them but decided that it's not worth it, also, removed nullable<> support since it didn't add any diffs

Should we do this for byref-like types?

Let me remove it then and keep only the patterns listed there then

(info.compCompHnd->getBoxHelper(pResolvedToken->hClass) == CORINFO_HELP_BOX))
{
CORINFO_RESOLVED_TOKEN isInstTok;
impResolveToken(codeAddr + 1, &isInstTok, CORINFO_TOKENKIND_Casting);
if (info.compCompHnd->compareTypesForCast(pResolvedToken->hClass, isInstTok.hClass) ==
TypeCompareState::MustNot)
{
JITDUMP("\n Importing BOX; ISINST; as null\n");
impPopStack();
impPushOnStack(gtNewNull(), typeInfo(TYP_REF));
return 1 + sizeof(mdToken);
}
}
}

const BYTE* nextCodeAddr = codeAddr + 1 + sizeof(mdToken);

switch (nextCodeAddr[0])
Expand Down