-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[mono] Append -sroa and -instcombine to LLVM-JIT pass manager #37458
Merged
Conversation
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
Tagging subscribers to this area: @lewurm |
We don't inline get_Arr () because it hits the inlining cost limit of 60. |
imhameed
approved these changes
Jun 5, 2020
monojenkins
pushed a commit
to monojenkins/mono
that referenced
this pull request
Jun 6, 2020
Mono's inliner is quite conservative (see dotnet/runtime#34286) so we have to workaround some limitations, e.g. ```csharp static ReadOnlySpan<byte> Arr => new byte[] {1, 2, 3, 4}; // access example: byte GetByte(int i) => Arr[i]; ``` #### Current codegen for `GetByte` (with dotnet/runtime#37458 included) ```asm 0000000000000000 <gram_GetByte__int_>: <BB>:1 0: 48 83 ec 18 sub $0x18,%rsp 4: c5 f8 57 c0 vxorps %xmm0,%xmm0,%xmm0 8: c5 f8 29 04 24 vmovaps %xmm0,(%rsp) d: 48 b8 50 22 73 d2 6e movabs $0x556ed2732250,%rax 14: 55 00 00 17: 48 89 e7 mov %rsp,%rdi 1a: ff 10 callq *(%rax) ;;;; get_Arr() is not inlined! 1c: 83 7c 24 08 00 cmpl $0x0,0x8(%rsp) 21: 74 0b je 2e <gram_GetByte__int_+0x2e> 23: 48 8b 04 24 mov (%rsp),%rax 27: 8a 00 mov (%rax),%al 29: 48 83 c4 18 add $0x18,%rsp 2d: c3 retq 2e: 48 b8 68 22 73 d2 6e movabs $0x556ed2732268,%rax 35: 55 00 00 38: bf 02 01 00 00 mov $0x102,%edi 3d: ff 10 callq *(%rax) ``` As you can see, `get_Arr()` is not inlined - it happened because the calculated size is 60 and the major contributor is `ReadOnlySpan<>.ctor` which is marked with [AggressiveInlining], so Mono inlined that `.ctor` into `get_Arr` and the total cost for `get_Arr` increased to 60. #### Codegen with this PR: ```asm 0000000000000000 <gram_GetByte__int_>: <BB>:1 0: 48 b8 00 6e 1f 91 11 movabs $0x5611911f6e00,%rax 7: 56 00 00 a: 8a 00 mov (%rax),%al c: c3 retq ``` Thus, the fix helped to inline more and even reduced function size. There are other places where we append method call costs to `inline_costs` but I decided to start with this one for constructors and only for LLVM to minimize possible regressions. Possible regressions match current CoreCLR behavior, bad case: [sharplab.io](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKGIAYACY8gOgCUBXAOwwEt8YLAMIR8AB14AbGFADKMgG68wMXAG4aNYgGYm5JE1IMhNAN4MLNCxYDaAWRgYAFhAAmASXGSAFA+dvPMUkAeTE+CC5cFgBBAHNY2FxcXgUYdy5JXi4s2IBKAF0rax09A2IUBgAhXli/F1dvXKKLU2brC2YATm8AIgAJHtyNanb2rt6YQeHR4vJunskptrG53sWh5dn5iCWRmb1ujb2Z8Z6AdV39g96do6vTqEv90/Xp59We1yeTj4BCb4YAF9NMcmLpmGUKtVao56mcoNgxGIZI02q1Qe0APSYqo1OpuRoMXi4IkZLIwVwMPAMGAIZFgDAUhhwBgQADWmws0PxDTu1mB1DaJQhTAqABVVBhUaD0VdsQx3FT8AwuBAMDS6TAGTlcTD/JSMBAGMAYKTMlwmVlDQwJbgpU0MdZ5cAOOreG6SSzVer2ZzdTz4YjkVBGm8LALAUA===)
EgorBo
added a commit
to mono/mono
that referenced
this pull request
Jun 18, 2020
#19930) Mono's inliner is quite conservative (see dotnet/runtime#34286) so we have to workaround some limitations, e.g. ```csharp static ReadOnlySpan<byte> Arr => new byte[] {1, 2, 3, 4}; // access example: byte GetByte(int i) => Arr[i]; ``` #### Current codegen for `GetByte` (with dotnet/runtime#37458 included) ```asm 0000000000000000 <gram_GetByte__int_>: <BB>:1 0: 48 83 ec 18 sub $0x18,%rsp 4: c5 f8 57 c0 vxorps %xmm0,%xmm0,%xmm0 8: c5 f8 29 04 24 vmovaps %xmm0,(%rsp) d: 48 b8 50 22 73 d2 6e movabs $0x556ed2732250,%rax 14: 55 00 00 17: 48 89 e7 mov %rsp,%rdi 1a: ff 10 callq *(%rax) ;;;; get_Arr() is not inlined! 1c: 83 7c 24 08 00 cmpl $0x0,0x8(%rsp) 21: 74 0b je 2e <gram_GetByte__int_+0x2e> 23: 48 8b 04 24 mov (%rsp),%rax 27: 8a 00 mov (%rax),%al 29: 48 83 c4 18 add $0x18,%rsp 2d: c3 retq 2e: 48 b8 68 22 73 d2 6e movabs $0x556ed2732268,%rax 35: 55 00 00 38: bf 02 01 00 00 mov $0x102,%edi 3d: ff 10 callq *(%rax) ``` As you can see, `get_Arr()` is not inlined - it happened because the calculated size is 60 and the major contributor is `ReadOnlySpan<>.ctor` which is marked with [AggressiveInlining], so Mono inlined that `.ctor` into `get_Arr` and the total cost for `get_Arr` increased to 60. #### Codegen with this PR: ```asm 0000000000000000 <gram_GetByte__int_>: <BB>:1 0: 48 b8 00 6e 1f 91 11 movabs $0x5611911f6e00,%rax 7: 56 00 00 a: 8a 00 mov (%rax),%al c: c3 retq ``` Thus, the fix helped to inline more and even reduced function size. There are other places where we append method call costs to `inline_costs` but I decided to start with this one for constructors and only for LLVM to minimize possible regressions. Possible regressions match current CoreCLR behavior, bad case: [sharplab.io](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKGIAYACY8gOgCUBXAOwwEt8YLAMIR8AB14AbGFADKMgG68wMXAG4aNYgGYm5JE1IMhNAN4MLNCxYDaAWRgYAFhAAmASXGSAFA+dvPMUkAeTE+CC5cFgBBAHNY2FxcXgUYdy5JXi4s2IBKAF0rax09A2IUBgAhXli/F1dvXKKLU2brC2YATm8AIgAJHtyNanb2rt6YQeHR4vJunskptrG53sWh5dn5iCWRmb1ujb2Z8Z6AdV39g96do6vTqEv90/Xp59We1yeTj4BCb4YAF9NMcmLpmGUKtVao56mcoNgxGIZI02q1Qe0APSYqo1OpuRoMXi4IkZLIwVwMPAMGAIZFgDAUhhwBgQADWmws0PxDTu1mB1DaJQhTAqABVVBhUaD0VdsQx3FT8AwuBAMDS6TAGTlcTD/JSMBAGMAYKTMlwmVlDQwJbgpU0MdZ5cAOOreG6SSzVer2ZzdTz4YjkVBGm8LALAUA===) Co-authored-by: EgorBo <[email protected]>
ghost
locked as resolved and limited conversation to collaborators
Dec 8, 2020
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Partially fixes #37449
Codegen for GetByte() in LLVM-JIT mode:
Codegen after I appended
-sroa -instcombine
(makes sense to add/move extra-simplifycfg
for hygiene) to the end of LLVM passes list:This https://godbolt.org/z/YKjzsV link explains motivation (on the left is the "final" LLVM IR our llvm-jit produces after the default optimizations). Zoltan noticed that LLVM-AOT where we use
opt -O2
instead of custom pass order optimized that code perfectly.The other issue remains: for some reason we don't inline
get_Arr()
without AggressiveInlining, coreclr does inline it: