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
Import isinst/castclass Nullable as underlying type #87241
Merged
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
168f833
Import isinst Nullable as underlying type
huoyaoyuan e0cd0e2
Update JIT interface for getCastingHelper
huoyaoyuan 8522424
Revert
huoyaoyuan c722484
Resolve nullable as underlying type when CORINFO_TOKENKIND_Casting
huoyaoyuan 8e5392a
Simplify token validation
huoyaoyuan 3ddd0ea
Switch exhaustiveness
huoyaoyuan 3e58229
IL test coverage for castclass
huoyaoyuan ab9520f
Remove unused comment
huoyaoyuan 1ab940b
Fix il
huoyaoyuan 85ba6f2
Revert implementation
huoyaoyuan 2b10425
Implement at JIT side
huoyaoyuan 3855226
Fix IL test
huoyaoyuan e71042b
Avoid unnecessary JIT to EE call
huoyaoyuan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5422,9 +5422,19 @@ GenTree* Compiler::impCastClassOrIsInstToTree( | |
{ | ||
assert(op1->TypeGet() == TYP_REF); | ||
|
||
// Import isinst Nullable<V> as isinst V | ||
CORINFO_CLASS_HANDLE hClass = info.compCompHnd->getTypeForBox(pResolvedToken->hClass); | ||
|
||
if (hClass != pResolvedToken->hClass) | ||
{ | ||
// Convert nullable to underlying type | ||
assert(op2->OperIsConst()); | ||
op2 = gtNewIconEmbClsHndNode(hClass); | ||
} | ||
|
||
// Optimistically assume the jit should expand this as an inline test | ||
bool shouldExpandInline = true; | ||
bool isClassExact = impIsClassExact(pResolvedToken->hClass); | ||
bool isClassExact = impIsClassExact(hClass); | ||
|
||
// Profitability check. | ||
// | ||
|
@@ -5459,7 +5469,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree( | |
// Pessimistically assume the jit cannot expand this as an inline test | ||
bool canExpandInline = false; | ||
bool partialExpand = false; | ||
const CorInfoHelpFunc helper = info.compCompHnd->getCastingHelper(pResolvedToken, isCastClass); | ||
const CorInfoHelpFunc helper = info.compCompHnd->getCastingHelper(hClass, isCastClass); | ||
|
||
CORINFO_CLASS_HANDLE exactCls = NO_CLASS_HANDLE; | ||
|
||
|
@@ -5472,7 +5482,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree( | |
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) && | ||
(info.compCompHnd->getExactClasses(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, | ||
|
@@ -5490,7 +5500,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree( | |
exactCls = actualImplCls; | ||
|
||
JITDUMP("'%s' interface has a single implementation - '%s', using that to inline isinst/castclass.", | ||
eeGetClassName(pResolvedToken->hClass), eeGetClassName(actualImplCls)); | ||
eeGetClassName(hClass), eeGetClassName(actualImplCls)); | ||
} | ||
else if (isCastClass) | ||
{ | ||
|
@@ -5500,7 +5510,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree( | |
// 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) & | ||
const bool isAbstract = (info.compCompHnd->getClassAttribs(hClass) & | ||
(CORINFO_FLG_INTERFACE | CORINFO_FLG_ABSTRACT)) != 0; | ||
canExpandInline = !isAbstract; | ||
} | ||
|
@@ -5543,7 +5553,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree( | |
if ((likelyCls != NO_CLASS_HANDLE) && | ||
(likelyClass.likelihood > (UINT32)JitConfig.JitGuardedDevirtualizationChainLikelihood())) | ||
{ | ||
if ((info.compCompHnd->compareTypesForCast(likelyCls, pResolvedToken->hClass) == | ||
if ((info.compCompHnd->compareTypesForCast(likelyCls, hClass) == | ||
TypeCompareState::Must)) | ||
{ | ||
bool isAbstract = (info.compCompHnd->getClassAttribs(likelyCls) & | ||
|
@@ -5710,7 +5720,7 @@ GenTree* Compiler::impCastClassOrIsInstToTree( | |
assert(lclDsc->lvSingleDef == 0); | ||
lclDsc->lvSingleDef = 1; | ||
JITDUMP("Marked V%02u as a single def temp\n", tmp); | ||
lvaSetClass(tmp, pResolvedToken->hClass); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any observable effect for changing the return type of the expression? |
||
lvaSetClass(tmp, hClass); | ||
return gtNewLclvNode(tmp, TYP_REF); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If all you want to do is to convert
Nullable<T>
intoT
for casing helpers, the best place to do it isresolveToken
on the VM side.resolveToken
on VM side does similar conversion forNewArr
here: https://github.com/dotnet/runtime/blob/main/src/coreclr/vm/jitinterface.cpp#L1170. You can do similar conversion for tokens that come from isinst/castclass.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conversion has to be also taken into account when embedding the handle (look for other places that check for
CORINFO_TOKENKIND_Newarr
in the VM). I think your current change is missing this part.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's null check difference for castclass, so the nullable type should be visible to JIT if we want to do optimization for castclass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please elaborate? Both
isinst
on null andcastclass
on null return null.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I misunderstood. NRE for casting to value type is thrown by unbox.