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

JIT: Simple importer single-block forward substitution #109482

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

hez2010
Copy link
Contributor

@hez2010 hez2010 commented Nov 2, 2024

  • Add a simple single-block forward substitution to propagate constants, typeof and FTN_ADDR in importer.
  • Import calli with resolved FTN_ADDR as regular call to enable inlining

The branch name is calli-inlining but this is done mainly for enabling typeof propagation, while fptr propagation are enabled for free so I enabled inlining for them by the way.

Example:

unsafe
{
    var t1 = typeof(string);
    var t2 = typeof(int);
    delegate*<int, Type, int> ptr = &Foo;
    Console.WriteLine("Hello");
    Console.WriteLine(t1.IsValueType);
    Console.WriteLine(t2.IsValueType);
    Console.WriteLine(CallGenericReturn(ptr, 42, t1));
    Console.WriteLine(CallGenericReturn(ptr, 42, t2));
    delegate*<int, Type, int> ptr2 = &Bar;
    ptr2(42, t1);
}

static int Foo(int v, Type w)
{
    Console.WriteLine(v);
    Console.WriteLine(w.IsValueType);
    return v + 1;
}

static T Bar<T>(T v, Type w) where T : IBinaryInteger<T>
{
    Console.WriteLine(w.TypeHandle.Value);
    return v - T.One;
}

unsafe static T CallGenericReturn<T, U>(delegate*<T, U, T> del, T v, U w)
{
    return del(v, w);
}

Before:

G_M56640_IG01:  ;; offset=0x0000
       push     rbp
       push     r15
       push     rbx
       lea      rbp, [rsp+0x10]
						;; size=9 bbWeight=1 PerfScore 3.50
G_M56640_IG02:  ;; offset=0x0009
       mov      rdi, 0x73541C297D38      ; 'Hello'
       call     [System.Console:WriteLine(System.String)]
       mov      rbx, 0x73541C295020      ; 'System.String'
       mov      rdi, rbx
       call     [System.RuntimeType:IsValueTypeImpl():ubyte:this]
       mov      edi, eax
       call     [System.Console:WriteLine(ubyte)]
       mov      r15, 0x73541C297D10      ; 'System.Int32'
       mov      rdi, r15
       call     [System.RuntimeType:IsValueTypeImpl():ubyte:this]
       mov      edi, eax
       call     [System.Console:WriteLine(ubyte)]
       mov      edi, 42
       mov      rsi, rbx
       mov      rax, 0x73541F821470      ; function address
       call     rax
       mov      edi, eax
       call     [System.Console:WriteLine(int)]
       mov      edi, 42
       mov      rsi, r15
       mov      rax, 0x73541F821470      ; function address
       call     rax
       mov      edi, eax
       call     [System.Console:WriteLine(int)]
       mov      edi, 42
       mov      rsi, rbx
       mov      rax, 0x73541F821458      ; function address
       call     rax
       nop      
						;; size=147 bbWeight=1 PerfScore 34.75
G_M56640_IG03:  ;; offset=0x009C
       pop      rbx
       pop      r15
       pop      rbp
       ret      
						;; size=5 bbWeight=1 PerfScore 2.50

After:

G_M56640_IG01:  ;; offset=0x0000
       sub      rsp, 40
                                                ;; size=4 bbWeight=1 PerfScore 0.25
G_M56640_IG02:  ;; offset=0x0004
       mov      rcx, 0x21E8030F010      ; 'Hello'
       call     [System.Console:WriteLine(System.String)]
       xor      ecx, ecx
       call     [System.Console:WriteLine(ubyte)]
       mov      ecx, 1
       call     [System.Console:WriteLine(ubyte)]
       mov      ecx, 42
       call     [System.Console:WriteLine(int)]
       xor      ecx, ecx
       call     [System.Console:WriteLine(ubyte)]
       mov      ecx, 43
       call     [System.Console:WriteLine(int)]
       mov      ecx, 42
       call     [System.Console:WriteLine(int)]
       mov      ecx, 1
       call     [System.Console:WriteLine(ubyte)]
       mov      ecx, 43
       call     [System.Console:WriteLine(int)]
       mov      rcx, 0x7FFA75FE5928
       call     [System.Console:WriteLine(long)]
       nop
                                                ;; size=115 bbWeight=1 PerfScore 32.75
G_M56640_IG03:  ;; offset=0x0077
       add      rsp, 40
       ret
                                                ;; size=5 bbWeight=1 PerfScore 1.25

Another example:

Diff for Activator.CreateInstance<MyStruct>:

 G_M000_IG01:
-       push     rbx
+       sub      rsp, 40
-       sub      rsp, 48
        xor      eax, eax
-       mov      qword ptr [rsp+0x28], rax
+       mov      qword ptr [rsp+0x20], rax
 
 G_M000_IG02:
-       mov      rbx, 0xD1FFAB1E
-       mov      rcx, rbx
-       call     [System.RuntimeType:IsValueTypeImpl():ubyte:this]
-       test     eax, eax
+       lea      rdx, [rsp+0x20]
+       mov      rcx, 0xD1FFAB1E
+       call     [System.RuntimeType:CallDefaultStructConstructor(byref):this]
+       mov      rax, gword ptr [rsp+0x20]
-       je       SHORT G_M000_IG05
-
-G_M000_IG03:
-       xor      edx, edx
-       mov      gword ptr [rsp+0x28], rdx
-       lea      rdx, [rsp+0x28]
-       mov      rcx, rbx
-       call     [System.RuntimeType:CallDefaultStructConstructor(byref):this]
-       mov      rax, gword ptr [rsp+0x28]
-
-G_M000_IG04:
-       add      rsp, 48
-       pop      rbx
-       ret
-
-G_M000_IG05:
-       mov      rcx, rbx
-       call     [System.RuntimeType:CreateInstanceOfT():System.Object:this]
-       nop
 
-G_M000_IG06:
+       add      rsp, 40
-       add      rsp, 48
-       pop      rbx
        ret

@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 Nov 2, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Nov 2, 2024
Copy link
Contributor

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

@hez2010

This comment was marked as outdated.

@hez2010

This comment was marked as outdated.

@hez2010
Copy link
Contributor Author

hez2010 commented Nov 2, 2024

@MihuBot

@EgorBo
Copy link
Member

EgorBo commented Nov 2, 2024

I guess this PR hits the same issues as Andy mentioned in #98380 (comment) from obvious issues is e.g. lack of "is address exposed" check. Also you sort of rely that def always comes before use in execution order

@hez2010
Copy link
Contributor Author

hez2010 commented Nov 3, 2024

lack of "is address exposed" check

Resolved.

Also you sort of rely that def always comes before use in execution order

Yes. If there's a use coming before the def like in a cycle, the def would be in another BB, in this case there won't be any substitution for the use as we restrict both the def and use to present in a single BB.

@hez2010
Copy link
Contributor Author

hez2010 commented Nov 3, 2024

Diffs

Tests are passing.

This PR doesn't handle typeof stored into fields.

@hez2010

This comment was marked as outdated.

@hez2010

This comment was marked as outdated.

@hez2010
Copy link
Contributor Author

hez2010 commented Nov 4, 2024

@MihuBot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants