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
Merged

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Jul 17, 2023

Closes #88987

Test<int>(42);

[MethodImpl(MethodImplOptions.NoInlining)]
static void Test<T>(T value)
{
    if (value is IEnumerable enumerable)
        Consume(enumerable);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void Consume(object o) { }

Tier0 Codegen diff for Test<int>:

; Assembly listing for method Program:Test[int](int) (Tier0)
       push     rbp
       sub      rsp, 48
       lea      rbp, [rsp+30H]
       xor      eax, eax
       mov      qword ptr [rbp-08H], rax
-      mov      qword ptr [rbp-10H], rax
       mov      dword ptr [rbp+10H], ecx
-      mov      rcx, 0xD1FFAB1E      ; System.Int32
-      call     CORINFO_HELP_NEWSFAST
-      mov      gword ptr [rbp-10H], rax
-      mov      rdx, gword ptr [rbp-10H]
-      mov      ecx, dword ptr [rbp+10H]
-      mov      dword ptr [rdx+08H], ecx
-      mov      rdx, gword ptr [rbp-10H]
-      mov      rcx, 0xD1FFAB1E      ; System.Collections.IEnumerable
-      call     [CORINFO_HELP_ISINSTANCEOFINTERFACE]
-      mov      gword ptr [rbp-08H], rax
+      xor      rcx, rcx
+      mov      gword ptr [rbp-08H], rcx
       cmp      gword ptr [rbp-08H], 0
       je       SHORT G_M5413_IG03
       mov      rcx, gword ptr [rbp-08H]
       call     [Program:Consume(System.Object)]
G_M5413_IG03:
       nop      
       add      rsp, 48
       pop      rbp
       ret      
-; Total bytes of code 100
+; Total bytes of code 49

Consume call is still here but it's under if (false) branch, we just can't properly fold that never-taken branch in Tier0 (needs forward sub in tier0) - not a big deal, at least, it no longer allocates.

Some nice jit-diffs numbers locally.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jul 17, 2023
@ghost ghost assigned EgorBo Jul 17, 2023
@ghost
Copy link

ghost commented Jul 17, 2023

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

Closes #88987

Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo
Copy link
Member Author

EgorBo commented Jul 17, 2023

@MihuBot

@EgorBo
Copy link
Member Author

EgorBo commented Jul 17, 2023

@MihuBot

@EgorBo
Copy link
Member Author

EgorBo commented Jul 17, 2023

@MihuBot -tier0

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

@EgorBo EgorBo merged commit 3fafc99 into dotnet:main Jul 18, 2023
@EgorBo EgorBo deleted the fold-box-isinst branch July 18, 2023 17:30
@ghost ghost locked as resolved and limited conversation to collaborators Aug 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tier0 allocates on a simple cast (boxing)
2 participants