Skip to content

Commit

Permalink
NativeAOT: devirtualize isinst/castclass for monomorphic cases (#80831)
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo authored Jan 19, 2023
1 parent f429780 commit 4544bdd
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5716,7 +5716,30 @@ GenTree* Compiler::impCastClassOrIsInstToTree(
// Check legality only if an inline expansion is desirable.
if (shouldExpandInline)
{
if (isCastClass)
CORINFO_CLASS_HANDLE actualImplCls = NO_CLASS_HANDLE;
if (this->IsTargetAbi(CORINFO_NATIVEAOT_ABI) &&
((helper == CORINFO_HELP_ISINSTANCEOFINTERFACE) || (helper == CORINFO_HELP_CHKCASTINTERFACE)) &&
(info.compCompHnd->getExactClasses(pResolvedToken->hClass, 1, &actualImplCls) == 1) &&
(actualImplCls != NO_CLASS_HANDLE) && impIsClassExact(actualImplCls))
{
// if an interface has a single implementation on NativeAOT where we won't load new types,
// we can assume that our object is always of that implementation's type, e.g.:
//
// var case1 = obj is IMyInterface;
// var case2 = (IMyInterface)obj;
//
// can be optimized to:
//
// var case1 = o is not null && o.GetType() == typeof(MyInterfaceImpl);
// var case2 = (o is null || o.GetType() == typeof(MyInterfaceImpl)) ? o : HELPER_CALL(o);
//
canExpandInline = true;
exactCls = actualImplCls;

JITDUMP("'%s' interface has a single implementation - '%s', using that to inline isinst/castclass.",
eeGetClassName(pResolvedToken->hClass), eeGetClassName(actualImplCls));
}
else if (isCastClass)
{
// Jit can only inline expand CHKCASTCLASS and CHKCASTARRAY helpers.
canExpandInline = (helper == CORINFO_HELP_CHKCASTCLASS) || (helper == CORINFO_HELP_CHKCASTARRAY);
Expand Down

0 comments on commit 4544bdd

Please sign in to comment.