By: Geoff Langdale (geoff.langdale.delete@this.gmail.com), August 24, 2022 7:18 pm
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.
Anything with a side effect in a branch is problematic (the printf example here is not great, as it just prints different things - much nastier would be read on one side and write on the other).
While launching missiles is an extreme side effect, my experience with trying to elide branches (which is extensive; I have written a bit about it on the semi-moribund branchfree.org, although mostly in SIMD) has convinced me that avoiding unpredictable conditional branches is only worth doing when the branch is (1) genuinely unpredictable, (2) actually frequently going in both directions and (3) when the amount of work being saved is significant.
Having a quantitative idea of the branch misses frequency, taken vs not-taken and the relative cycle costs of the work is critical - I've tripped over my fetish for writing branch-free code more than once with clever-clever solutions that are not better than just "eating the miss penalty 10% of the time".
If you're doing a fair bit of work on both sides of the branch, branch free alternatives can get expensive fast, especially as using a cmov isn't magically making your code "no longer bound by the condition" in terms of the data that results from the computation.
>
> 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.
if (--defcon == 1) {
launch_missiles();
}
Anything with a side effect in a branch is problematic (the printf example here is not great, as it just prints different things - much nastier would be read on one side and write on the other).
While launching missiles is an extreme side effect, my experience with trying to elide branches (which is extensive; I have written a bit about it on the semi-moribund branchfree.org, although mostly in SIMD) has convinced me that avoiding unpredictable conditional branches is only worth doing when the branch is (1) genuinely unpredictable, (2) actually frequently going in both directions and (3) when the amount of work being saved is significant.
Having a quantitative idea of the branch misses frequency, taken vs not-taken and the relative cycle costs of the work is critical - I've tripped over my fetish for writing branch-free code more than once with clever-clever solutions that are not better than just "eating the miss penalty 10% of the time".
If you're doing a fair bit of work on both sides of the branch, branch free alternatives can get expensive fast, especially as using a cmov isn't magically making your code "no longer bound by the condition" in terms of the data that results from the computation.