Do we need branch prediction?

By: Adrian (a.delete@this.acm.org), August 25, 2022 12:58 am
Room: Moderated Discussions
Kara (karaardalan.delete@this.gmail.com) on August 24, 2022 2:07 pm wrote:
>
> Since like my second year programming I stopped writing branched code totally,
> few tens of thousands lines of code in, I haven't encounter anything that
> intrinsically requires speculative branch prediction, like logically.
>
> But I write only numes.
>
> Is there any program that can't be written without a branching that can't
> be rephrased (literally, all that is is rephrasing) into branchless?
>
> Mind risc-v don't offer BPU, there's extensions but not in the requirements.



As others have also mentioned, branchless code is efficient only when replacing unpredictable branches that go in both directions with similar frequencies.

Otherwise branchless code is equivalent in energy consumption with mispredicted speculative execution, where the results are discarded.


Therefore any CPU must have both conditional branches and conditional selection or move instructions for branchless code.


For conditional branches, there is something more important than the branch predictor, which must always be implemented for acceptable performance in a pipelined CPU: speculative execution.

The same hardware that enables speculative execution beyond a conditional branch is also necessary to enable execution beyond any instruction that may generate exceptions, to provide precise exceptions.

A CPU with speculative execution beyond conditional branches does not necessarily need a branch predictor, because it might use just the static prediction of forward branches being not taken and backward branches being taken.


From my experience, I agree that there are classes of programs for which a combination of static prediction of conditional branches with branchless code for unpredictable conditions is good enough, if, and only if, the programmer provides hints for the compiler about the frequent branch direction, so that the generated code would be arranged correctly.


Most of the programs that I have written are actually of this type, where a complex branch predictor would give only very modest improvements over hints provided by the programmer.


Nevertheless, there are also many programs where the conditional branches follow complex, but predictable patterns, which can benefit greatly from a dynamic branch predictor.

More importantly, most programmers do not want to be bothered to provide frequency hints for conditions and almost all legacy programs do not have such hints, so for general-purpose computing a very good branch predictor is mandatory.

< Previous Post in ThreadNext Post in Thread >
TopicPosted ByDate
Do we need branch prediction?Kara2022/08/24 02:07 PM
  Do we need branch prediction?Rayla2022/08/24 03:04 PM
  Do we need branch prediction?Mark Roulo2022/08/24 04:25 PM
    Do we need branch prediction?Paul A. Clayton2022/08/24 06:28 PM
      Do we need branch prediction?Anon2022/08/24 06:52 PM
    Do we need branch prediction?rwessel2022/08/24 06:40 PM
    Do we need branch prediction?ananon2022/08/25 12:56 AM
      Do we need branch prediction?Carlie Coats2022/08/25 07:39 AM
        OT: You wanted rather than Mark Roulo2022/08/25 09:57 AM
          OT: You wanted "pre" rather than "code" rather than Mark Roulo2022/08/25 09:57 AM
            OT: You wanted "pre" rather than "code" rather than Carlie Coats2022/08/25 11:43 AM
    Do we need branch prediction?Kevin G2022/08/25 08:47 AM
      Do we need branch prediction?rwessel2022/08/25 11:10 AM
          Do we need branch prediction?rwessel2022/08/26 10:37 AM
  Do we need branch prediction?Geoff Langdale2022/08/24 07:18 PM
  Do we need branch prediction?Adrian2022/08/25 12:58 AM
  Do we need branch prediction?Brendan2022/08/25 07:54 AM
    Do we need branch prediction?Anon42022/08/25 11:45 AM
      Do we need branch prediction?anon22022/08/25 09:08 PM
  Predicable branching patterns (aykm; )Kara2022/08/27 01:00 PM
    Predicable branching patterns (aykm; )Jukka Larja2022/08/27 11:38 PM
    Predicable branching patterns (aykm; )Eric Fink2022/08/28 01:22 AM
      Predicable branching patterns (aykm; )Doug S2022/08/28 10:17 AM
    Predicable branching patterns (aykm; )---2022/08/28 02:16 PM
      Predicable branching patterns (aykm; )Anon2022/08/28 02:35 PM
  Q: Do we need branch prediction? A: We don't need it, but....2022/08/29 02:17 PM
    Quantum stuff.Kara2022/08/29 03:01 PM
      Q: Do we need branch prediction? A: We don't need it, but....2022/08/30 11:48 AM
    "Quantum Woo" (hint; that's NOT how it works) (NT)anonymous22022/08/29 04:19 PM
      Q: Do we need branch prediction? A: We don't need it, but....2022/08/30 12:14 PM
Reply to this Topic
Name:
Email:
Topic:
Body: No Text
How do you spell tangerine? 🍊