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