Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Enable R2R compilation/inlining of PInvoke stubs where no marshalling is required#22560

Merged
fadimounir merged 10 commits into
dotnet:masterfrom
fadimounir:enable_some_pinvokes
Apr 1, 2019
Merged

Enable R2R compilation/inlining of PInvoke stubs where no marshalling is required#22560
fadimounir merged 10 commits into
dotnet:masterfrom
fadimounir:enable_some_pinvokes

Conversation

@fadimounir

@fadimounir fadimounir commented Feb 13, 2019

Copy link
Copy Markdown

These changes enable the inlining of some PInvokes that do not require any marshalling. With inlined pinvokes, R2R performance should become slightly better, since we'll avoid jitting some of the pinvoke IL stubs that we jit today for S.P.CoreLib. Performance gains not yet measured.

Added JIT_PInvokeBegin/End helpers for all architectures. Linux stubs not yet implemented
Add INLINE_GETTHREAD for arm/arm64
Set CORJIT_FLAG_USE_PINVOKE_HELPERS jit flag for ReadyToRun compilations

@fadimounir fadimounir added the * NO MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Feb 13, 2019
Comment thread src/vm/arm/PInvokeStubs.asm Outdated
Comment thread src/vm/dllimport.cpp Outdated
Comment thread src/vm/dllimport.cpp Outdated
Comment thread src/inc/readytorun.h
Comment thread src/jit/compiler.h Outdated
@fadimounir fadimounir removed the * NO MERGE * The PR is not ready for merge yet (see discussion for detailed reasons) label Feb 27, 2019
@fadimounir fadimounir changed the title [WIP] Enable some pinvokes - Do not merge Enable R2R compilation/inlining of PInvoke stubs where no marshalling is required Feb 27, 2019
@fadimounir

Copy link
Copy Markdown
Author

@jkotas PTAL. I'm still going to run the P0 tests with crossgen enabled, for verification, and will get some perf measurements.

Comment thread src/jit/lower.cpp Outdated
Comment thread src/vm/amd64/PInvokeStubs.asm
Comment thread src/jit/compiler.hpp Outdated
Comment thread src/vm/amd64/PInvokeStubs.asm Outdated
Comment thread src/vm/amd64/PInvokeStubs.asm Outdated
Comment thread src/vm/jithelpers.cpp Outdated
@fadimounir

Copy link
Copy Markdown
Author

@dotnet-bot test Windows_NT x64 Checked CoreFX Tests

@fadimounir

Copy link
Copy Markdown
Author

@jkotas PTAL at the new changes I submitted

@jkotas

jkotas commented Mar 11, 2019

Copy link
Copy Markdown
Member

The delta looks reasonable to me. Have you done any R2R specific testing on this?

@fadimounir

Copy link
Copy Markdown
Author

I have run the P0 tests using the 'crossgen' command as described in this doc: https://github.com/dotnet/coreclr/blob/master/Documentation/building/windows-test-instructions.md
Results were clean.

@jkotas

jkotas commented Mar 11, 2019

Copy link
Copy Markdown
Member

These helpers have tight interaction with the GC. I would also do some crossgen+GC stress testing (with tiered compilation disabled).

@fadimounir

Copy link
Copy Markdown
Author

Sounds good. I'll look into it

@fadimounir

Copy link
Copy Markdown
Author

@jkotas crossgen testing with and without gc stress was clean with regards to these changes (x64 only). For the other architectures, my targeted pinvoke test case had some form of GC stress enabled, and was passing.

@fadimounir

Copy link
Copy Markdown
Author

I'm still waiting on the perf job to complete to see what the impact of the changes are.

@fadimounir

Copy link
Copy Markdown
Author

/cc @sergiy-k

Comment thread src/zap/zapinfo.cpp
Comment thread src/vm/amd64/PInvokeStubs.asm Outdated
Comment thread src/vm/amd64/PInvokeStubs.asm
@fadimounir fadimounir force-pushed the enable_some_pinvokes branch from cdc492b to 76a9115 Compare March 19, 2019 20:04
@fadimounir

fadimounir commented Mar 21, 2019

Copy link
Copy Markdown
Author

Hmm... Doesn't look like we're getting noticable startup perf wins I expected: http://benchview/compare?jobid=158586&comparejobids=[158569]&testid=61944&
There are some scenarios that actually seem slower now. I'll need to dig in further.. I do see some good wins under the "Inlining" category here, with 11% faster execution. CscBench is also 1% faster. There are just some tests mainly under BenchmarksGame and Benchstone that seem slightly slower. Could it be noise?

@AndyAyersMS, @jkotas what do you guys think?

/cc @brianrob

@fadimounir fadimounir force-pushed the enable_some_pinvokes branch from 76a9115 to 457d79c Compare March 27, 2019 18:46
@jkotas

jkotas commented Mar 28, 2019

Copy link
Copy Markdown
Member

This comment is closed, but I do not see a response to it. Just want to make sure you have seen it:

Another option is to move the popping of the frame on the slow path into the C helper. If you do that the need for this macro will disapper and you will have bit less of assembly code to maintain which is always goodness.

@fadimounir

fadimounir commented Mar 28, 2019

Copy link
Copy Markdown
Author

Where does Linq use PInvokes to explain this gain?

Without tiered compilation, the previous lab results were showing a 20% regression for some weird reason, even though Linq shouldn't really be impacted by pinvokes. I just wanted to dig deeper into that regression, and make sure it was bogus.

@fadimounir

Copy link
Copy Markdown
Author

Another option is to move the popping of the frame on the slow path into the C helper

Can this be done in the same JIT_RareDisableHelper method or should I add a wrapper for it? I don't know what else uses this helper, and if popping the frame from the thread at that location would have other side effects.

@fadimounir

Copy link
Copy Markdown
Author

How many of these methods are PInvoke stubs? It would be useful to get the list and see how many of them are easy to convert to blittable PInvokes as follow up.

After a second look, I just realized that the baseline measurement may have also been a partial R2R image, that's why it has more jitting. However, for a helloworld scenario, i can confirm by debugging that there are about 5 or 6 pinvokes getting inlined and invoked (JIT_PInvokeBegin/End called)

@jkotas

jkotas commented Mar 28, 2019

Copy link
Copy Markdown
Member

Can this be done in the same JIT_RareDisableHelper method

It should be separate method. I would copy&paste the code for JIT_RareDisableHelper and added the extra piece to it.

Comment thread src/inc/corinfo.h Outdated
Comment thread src/vm/amd64/PInvokeStubs.asm Outdated
Comment thread src/vm/jithelpers.cpp Outdated
Comment thread src/vm/jithelpers.cpp Outdated
fadimounir added 10 commits April 1, 2019 08:40
…e any marshalling. With inlined pinvokes, R2R performance should become slightly better, since we'll avoid jitting some of the pinvoke IL stubs that we jit today for S.P.CoreLib. Performance gains not yet measured.

Added JIT_PInvokeBegin/End helpers for all architectures. Linux stubs not yet implemented
Add INLINE_GETTHREAD for arm/arm64
Set CORJIT_FLAG_USE_PINVOKE_HELPERS jit flag for ReadyToRun compilations
Increase size reserve for InlineCallFrame
Small adjustment to the arm/arm64 INLINE_GET_THREAD macros
@fadimounir fadimounir force-pushed the enable_some_pinvokes branch from 857a3f0 to b62dcbe Compare April 1, 2019 15:41
@fadimounir fadimounir merged commit bc9248c into dotnet:master Apr 1, 2019
@AndyAyersMS

Copy link
Copy Markdown
Member

Hmm... Doesn't look like we're getting noticable startup perf wins I expected

Not too surprising; the jit-focused CoreCLR perf tests do not measure startup (or jit time, for the most part). Using ETW to look at jit time and jit requests (or using scenario startup metrics) is a better way to assess this.

Is there a follow-up plan to enable this for non-windows platforms?

@fadimounir

Copy link
Copy Markdown
Author

Is there a follow-up plan to enable this for non-windows platforms?

Yes. I'm currently working on it and will create a separate PR

@fadimounir fadimounir deleted the enable_some_pinvokes branch July 1, 2019 19:46
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
… is required (dotnet/coreclr#22560)

* These changes enable the inlining of some PInvokes that do not require any marshalling. With inlined pinvokes, R2R performance should become slightly better, since we'll avoid jitting some of the pinvoke IL stubs that we jit today for S.P.CoreLib. Performance gains not yet measured.

* Added JIT_PInvokeBegin/End helpers for all architectures. Linux stubs not yet implemented
* Add INLINE_GETTHREAD for arm/arm64
* Set CORJIT_FLAG_USE_PINVOKE_HELPERS jit flag for ReadyToRun compilations
* Updating R2RDump tool to handle pinvokes


Commit migrated from dotnet/coreclr@bc9248c
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants