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
Conversation
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsI am curious if we can expand this helper too, e.g.: [Benchmark]
public void Test() => GenericCast<string>("");
[MethodImpl(MethodImplOptions.NoInlining)]
public T GenericCast<T>(object o) => (T)o; Codegen diff for ; Program.GenericCast[[System.__Canon, System.Private.CoreLib]](System.Object)
sub rsp,28
mov [rsp+20],rdx
+ mov rax,r8
+ test rax,rax
+ je short M01_L00
mov rcx,[rdx+10]
mov rcx,[rcx]
+ cmp [rax],rcx
+ je short M01_L00
mov rdx,r8
call [CastHelpers.ChkCastAny(Void*, System.Object)]
nop
add rsp,28
ret
-; Total bytes of code 31
+; Total bytes of code 44
|
@@ -5486,7 +5486,8 @@ GenTree* Compiler::impCastClassOrIsInstToTree( | |||
else if (isCastClass) | |||
{ | |||
// Jit can only inline expand CHKCASTCLASS and CHKCASTARRAY helpers. | |||
canExpandInline = (helper == CORINFO_HELP_CHKCASTCLASS) || (helper == CORINFO_HELP_CHKCASTARRAY); |
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.
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.
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.
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
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.
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.
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.
For example, we should not be inlining the exact check in:
[MethodImpl(MethodImplOptions.NoInlining)]
public IEnumrable<T> GenericCast<T>(object o) => (IEnumerable<T>)o;
It would be useful to have some evidence about how often the fast path is taken vs. not taken in something real world (e.g. Bing). |
I've reverted this PR's change and put these lame counters into the ChkCastAny: Here is the Bing's stats (after running its benchmark):
so in 18% cases the fast path is taken (0.05% for null) AvaloniaILSpy desktop app:
36% for the fast path. |
Sounds good. These percentages should be even higher once you do the expansion only in places where it is statically known to be profitable. |
I am curious if we can expand this helper too, e.g.:
Codegen diff for
GenericCast
. Basically, we use T as a fast-path check (with null check)Basically, we inline these things:
(Technically, we can introduce a new helper without the fast-paths)