You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inlined GC poll for methods marked with SuppressGCTransitionAttribute #13582
Calling the above function when either GCPOLL_CALL or GCPOLL_INLINE is set results in multiple asserts being fired. This was discovered during the suppress GC work - dotnet/coreclr#26458. The result of this was to manually insert GCPOLL_CALL instances instead of GCPOLL_INLINE.
changed the title [-]The GC_Poll Create/Insert logic appears to be flawed[/-][+]Inlined GC poll for methods marked with SuppressGCTransitionAttribute[/+]on Oct 15, 2019
This will analyze loops to prove that a managed call always occurs in the execution of one iteration.
If it proves this it will omit the GC_POLL call for the loop.
When we discussed this behavior during dotnet/coreclr#26458, you advocated for always calling the GC_POLL rather than optimizing it away.
@briansull The issue here was that during some scenarios no assert was hit with GCPOLL_INLINE, but there were cases when it would assert. Asserts also occurred when setting GCPOLL_CALL. There are obviously multiple scenarios here.
How you recommend that we implement the inlined GC poll for SuppressGCTransitionAttribute?
Yes, We should fix the codepath that we use for SuppressGCTransitionAttribute to support GCPOLL_INLINE in all cases.
The existing code fgCreateGCPolls() was last used to support the MacOS during the pre-RyuJIT time frame. That code should be deleted as it tries to eliminate the GCPOLL call when it can prove that a managed call will be made in every loop iteration, which is not what we want with this new feature.
Did you try changing the code in morph that generates the GCPOLL_CALL for the SuppressGCTransitionAttribute case to GCPOLL_INLINE? That is, here:
if (fgGlobalMorph && call->IsUnmanaged() && call->IsSuppressGCTransition())
{
// Insert a GC poll.
bool insertedBB = fgCreateGCPoll(GCPOLL_CALL, compCurBB, compCurStmt);
assert(!insertedBB); // No new block should be inserted
}
Presumably you could do this and see asm diffs where the polls are inlined in the tests Interop\PInvoke\Attributes\SuppressGCTransition\SuppressGCTransitionTest.cs and JIT\Methodical\gc_poll\InsertGCPoll.cs.
I do not know of one at present. @VSadov was investigating using this attribute and it wasn't as fast as needed for him. He may have a scenario to validate.
A simpler scenario to validate the improvements is switch something very fast to use SuppressGCTransition and measure.
Environment.TickCount cold be a good candidate - it is a very fast call on all platforms. It must be.
A lot of code uses this for cheap timestamps when precision/frequency is not a concern, so if overhead could be reduced by skipping VM, then it would be a nice change.
@jkotas@VSadov@AaronRobinsonMSFT Is it important to support emiiting gc polls when the call to a SuppressGCTransitionAttributes method is in a catch, fault, filter, or finally block? If so, does it have to be inlined?
@jkotas@VSadov@AaronRobinsonMSFT Is it important to support emiiting gc polls when the call to a SuppressGCTransitionAttributes method is in a catch, fault, filter, or finally block? If so, does it have to be inlined?
I don't think it really matters where the call is made. I could get behind the argument that in a catch, fault, or filter block it doesn't need to be inlined since the exception is so costly anyways - all of those are exceptions right? I believe finally blocks are used in non-exceptional cases so that seems like we want to make as fast as possible.
Does handling all these blocks complicate the implementation?
It would be nice to emit gc polls for these. I do not think the gc polls have to be inlined in the EH handler blocks. Regular PInvokes are not always inlined inside EH handler blocks either, so not inlining gc polls in EH handler blocks would be consistent.
Just wonder - "catch block" here means the basic block that is the body of the catch handler, in which case polling is unnecessary, but not a big deal if it is there, or call just happens to be in a catch handler - i.e. could be in a loop in a catch, then we need to poll, just for suspension pause guarantee.
For finallies, ideally, there should be no difference, running them typically does not require an exception.
I agree inlining is not very important if that simplifies the design or makes emit smaller.
Many things get slower around exception handling and it is generally possible to factor hot code out if needed.
The current implementation of GC polls that @AaronRobinsonMSFT added only inserts GC polls for inlined pinvokes to methods with [SuppressGCTransitionAttribute]. Was that intentional or should GC polls be emitted for all pinvokes to methods with [SuppressGCTransitionAttributes]?
Note that we currently don't inline pinvokes in handlers and, on 64 bit platforms, in try regions: see
Activity
[-]The GC_Poll Create/Insert logic appears to be flawed[/-][+]Inlined GC poll for methods marked with SuppressGCTransitionAttribute[/+]jkotas commentedon Oct 15, 2019
The SuppressGCTransitionAttribute optimization leaves performance on the table because of this issue.
briansull commentedon Oct 15, 2019
This will analyze loops to prove that a managed call always occurs in the execution of one iteration.
If it proves this it will omit the GC_POLL call for the loop.
When we discussed this behavior during dotnet/coreclr#26458, you advocated for always calling the GC_POLL rather than optimizing it away.
jkotas commentedon Oct 15, 2019
How you recommend that we implement the inlined GC poll for SuppressGCTransitionAttribute? Ignore what is there today and just do it from scratch?
AaronRobinsonMSFT commentedon Oct 15, 2019
@briansull The issue here was that during some scenarios no assert was hit with
GCPOLL_INLINE, but there were cases when it would assert. Asserts also occurred when settingGCPOLL_CALL. There are obviously multiple scenarios here.Inline PInvoke in an inlineable function.
PInvoke in non-inlined function
Then there are scenarios involving CrossGen which can't use
GCPOLL_INLINEat the moment, but requiresGCPOLL_CALL- see https://github.com/dotnet/coreclr/issues/27206.briansull commentedon Oct 15, 2019
Yes, We should fix the codepath that we use for SuppressGCTransitionAttribute to support GCPOLL_INLINE in all cases.
The existing code fgCreateGCPolls() was last used to support the MacOS during the pre-RyuJIT time frame. That code should be deleted as it tries to eliminate the GCPOLL call when it can prove that a managed call will be made in every loop iteration, which is not what we want with this new feature.
BruceForstall commentedon Apr 28, 2020
@briansull You fixed some of the GCPOLL code with #34837.
Did you try changing the code in morph that generates the
GCPOLL_CALLfor the SuppressGCTransitionAttribute case toGCPOLL_INLINE? That is, here:Presumably you could do this and see asm diffs where the polls are inlined in the tests Interop\PInvoke\Attributes\SuppressGCTransition\SuppressGCTransitionTest.cs and JIT\Methodical\gc_poll\InsertGCPoll.cs.
briansull commentedon Apr 28, 2020
I will take a look at making this change and seeing what asm diffs there are with it.
BruceForstall commentedon Apr 28, 2020
@AaronRobinsonMSFT @jkotas Is there a benchmark where we could measure a performance improvement by implementing inlined GC polls for this scenario?
AaronRobinsonMSFT commentedon Apr 28, 2020
I do not know of one at present. @VSadov was investigating using this attribute and it wasn't as fast as needed for him. He may have a scenario to validate.
VSadov commentedon Apr 28, 2020
I did not notice that this has merged.
One important scenario that is blocked right now is the Math functions. - #13820
That could use SuppressGCTransition, but would require that:
No need to poll for every function when user does:
I assume the first part is now fixed?
VSadov commentedon Apr 28, 2020
A simpler scenario to validate the improvements is switch something very fast to use
SuppressGCTransitionand measure.Environment.TickCountcold be a good candidate - it is a very fast call on all platforms. It must be.A lot of code uses this for cheap timestamps when precision/frequency is not a concern, so if overhead could be reduced by skipping VM, then it would be a nice change.
17 remaining items
erozenfeld commentedon Jul 6, 2020
Yes, the placement will be deterministic.
erozenfeld commentedon Jul 7, 2020
@jkotas @VSadov @AaronRobinsonMSFT Is it important to support emiiting gc polls when the call to a
SuppressGCTransitionAttributesmethod is in acatch,fault,filter, orfinallyblock? If so, does it have to be inlined?AaronRobinsonMSFT commentedon Jul 7, 2020
I don't think it really matters where the call is made. I could get behind the argument that in a
catch,fault, orfilterblock it doesn't need to be inlined since the exception is so costly anyways - all of those are exceptions right? I believefinallyblocks are used in non-exceptional cases so that seems like we want to make as fast as possible.Does handling all these blocks complicate the implementation?
jkotas commentedon Jul 7, 2020
It would be nice to emit gc polls for these. I do not think the gc polls have to be inlined in the EH handler blocks. Regular PInvokes are not always inlined inside EH handler blocks either, so not inlining gc polls in EH handler blocks would be consistent.
VSadov commentedon Jul 7, 2020
Just wonder - "catch block" here means the basic block that is the body of the catch handler, in which case polling is unnecessary, but not a big deal if it is there, or call just happens to be in a catch handler - i.e. could be in a loop in a catch, then we need to poll, just for suspension pause guarantee.
For finallies, ideally, there should be no difference, running them typically does not require an exception.
I agree inlining is not very important if that simplifies the design or makes emit smaller.
Many things get slower around exception handling and it is generally possible to factor hot code out if needed.
erozenfeld commentedon Jul 7, 2020
The current implementation of GC polls that @AaronRobinsonMSFT added only inserts GC polls for inlined pinvokes to methods with [SuppressGCTransitionAttribute]. Was that intentional or should GC polls be emitted for all pinvokes to methods with [SuppressGCTransitionAttributes]?
Note that we currently don't inline pinvokes in handlers and, on 64 bit platforms, in try regions: see
runtime/src/coreclr/src/jit/importer.cpp
Line 6615 in 6764633
jkotas commentedon Jul 7, 2020
Yes, it is intentional. The non-inlined PInvoke is going to have the GC poll in the PInvoke stub.
erozenfeld commentedon Jul 7, 2020
Ok, then I don't have to worry about inserting polls in handlers since we'll never have inlined pinvokes there.
erozenfeld commentedon Jul 14, 2020
Fixed by #39111.