Search Unity

Feedback There should be an intrinsic to give branch prediction hints to Burst like GCC's __builtin_expect

Discussion in 'Burst' started by SamOld, Jun 24, 2020.

  1. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    333
    GCC supports
    __builtin_expect
    and C++ supports
    [[likely]]
    and
    [[unlikely]]
    . Is there a way to achieve something similar in Burst?
     
    DreamingImLatios likes this.
  2. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    333
    I believe the answer is no, so I'm changing this to a feedback and request thread. I would very much like to see something like this, probably something in
    Unity.Burst.Intrinsics.Common
    .

    I won't try to justify this request in too much detail as I lack the expertise. It's useful for performance to be able to hint the likely path when branching on data with known statistics or optimising for a specific fast path. This can allow assembly optimisations, like making the common path free of jumps. For a more in depth argument, look to the reasons GCC supports this and C++20 is adding language support.
     
  3. sheredom

    sheredom

    Unity Technologies

    Joined:
    Jul 15, 2019
    Posts:
    300
    I'll file an issue internally so we can track this.

    We do make use of 'cold' branches already for anything that could end up with an exception thrown, and I did see some good instruction cache behaviour as a result, so this is worth considering.
     
    SamOld likes this.
  4. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    333
    Thanks very much @sheredom!

    The situation that motivated this for me personally is a custom hashset implementation that I've been playing around with. I know statistically that it will almost always find the correct bucket on the first or second attempt, but the probing code needs to check every single bucket in the worst case. I'd like to persuade the compiler of this fact and have the normal path assume that it finds it quickly and the longer probing search be a cold branch that it jumps to.

    I don't have enough expertise with this level of optimisation to know whether this will actually help, but I'm sure that others will make great use of it even if I don't.