Skip to content

Inlined GC poll for methods marked with SuppressGCTransitionAttribute #13582

Closed

Description

@AaronRobinsonMSFT

The code responsible for creating and inserting GC_Poll instances appears to have degraded since porting over from .NET Framework.

void Compiler::fgCreateGCPolls()

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.

Additional issues:

/cc @briansull @dotnet/jit-contrib

category:cq
theme:optimization
skill-level:beginner
cost:medium

Activity

  1. 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
  2. jkotas commented on Oct 15, 2019

    @jkotas
    Member

    The SuppressGCTransitionAttribute optimization leaves performance on the table because of this issue.

  3. briansull commented on Oct 15, 2019

    @briansull
    Contributor

    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.

  4. jkotas commented on Oct 15, 2019

    @jkotas
    Member

    How you recommend that we implement the inlined GC poll for SuppressGCTransitionAttribute? Ignore what is there today and just do it from scratch?

  5. AaronRobinsonMSFT commented on Oct 15, 2019

    @AaronRobinsonMSFT
    MemberAuthor

    @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.

    Inline PInvoke in an inlineable function.

       public static int GetTime()
       {
           return InlineablePInvokeWithGCSuppression();
       }

    PInvoke in non-inlined function

       public static int GetAccTime()
       {
           int acc = 0;
           foreach (...)
           {
              acc += InlineablePInvokeWithGCSuppression();
           }
    
           return acc;
       }

    Then there are scenarios involving CrossGen which can't use GCPOLL_INLINE at the moment, but requires GCPOLL_CALL - see https://github.com/dotnet/coreclr/issues/27206.

  6. briansull commented on Oct 15, 2019

    @briansull
    Contributor

    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.

  7. transferred this issue fromdotnet/coreclron Jan 31, 2020
  8. added this to the 5.0 milestone on Jan 31, 2020
  9. BruceForstall commented on Apr 28, 2020

    @BruceForstall
    Contributor

    @briansull You fixed some of the GCPOLL code with #34837.

    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.

  10. briansull commented on Apr 28, 2020

    @briansull
    Contributor

    I will take a look at making this change and seeing what asm diffs there are with it.

  11. BruceForstall commented on Apr 28, 2020

    @BruceForstall
    Contributor

    @AaronRobinsonMSFT @jkotas Is there a benchmark where we could measure a performance improvement by implementing inlined GC polls for this scenario?

  12. AaronRobinsonMSFT commented on Apr 28, 2020

    @AaronRobinsonMSFT
    MemberAuthor

    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.

  13. VSadov commented on Apr 28, 2020

    @VSadov
    Member

    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:

    • GC poll is inlined
    • We do something smart with GC polls in a block
      No need to poll for every function when user does:
    Math.Acos(Math.Cos(Math.Acos(Math.Cos(Math.Acos(Math.Cos(Math.Cos(x)))))))

    I assume the first part is now fixed?

  14. VSadov commented on Apr 28, 2020

    @VSadov
    Member

    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.

  15. 17 remaining items

  16. erozenfeld commented on Jul 6, 2020

    @erozenfeld
    Contributor

    Yes, the placement will be deterministic.

  17. erozenfeld commented on Jul 7, 2020

    @erozenfeld
    Contributor

    @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?

  18. AaronRobinsonMSFT commented on Jul 7, 2020

    @AaronRobinsonMSFT
    MemberAuthor

    @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?

  19. jkotas commented on Jul 7, 2020

    @jkotas
    Member

    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.

  20. VSadov commented on Jul 7, 2020

    @VSadov
    Member

    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.

  21. erozenfeld commented on Jul 7, 2020

    @erozenfeld
    Contributor

    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

    bool Compiler::impCanPInvokeInlineCallSite(BasicBlock* block)

  22. jkotas commented on Jul 7, 2020

    @jkotas
    Member

    Yes, it is intentional. The non-inlined PInvoke is going to have the GC poll in the PInvoke stub.

  23. erozenfeld commented on Jul 7, 2020

    @erozenfeld
    Contributor

    Ok, then I don't have to worry about inserting polls in handlers since we'll never have inlined pinvokes there.

  24. erozenfeld commented on Jul 14, 2020

    @erozenfeld
    Contributor

    Fixed by #39111.

  25. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions