On Fri, 23 Aug 2024, 12:26 Nicholas Wilson via Digitalmars-d, <digitalmars-d@puremagic.com> wrote:
On Friday, 23 August 2024 at 01:47:37 UTC, Manu wrote:
> I'm working on microcontrollers, and many don't have a branch
> predictor. They do "static" prediction, that is, they just
> predict branch-not-taken and it's on you to write your code as
> such, and that's not always possible. (like the loop condition
> in a for loop)
>
> This leads to a lot of awkward looking if's written in
> unnatural 'backwards' terms, and also, leads to undesirable
> depth of nested scopes.
>
> The compiler backend can and should re-jig the comparisons, but
> it needs to receive a hint which way is 'likely' from the
> programmer.
>
> How can we add an attribute to the branch condition that the
> backend can take advantage of? I think it needs to be in the
> language spec...

For LDC see,
https://github.com/ldc-developers/ldc/blob/master/runtime/druntime/src/ldc/intrinsics.di#L619 and see also `llvm_assume` just beneath it.

GDC probably has something similar too.

Yes exactly, this is my point, we need something in the spec. I think there might be an opportunity to express branch prediction hints in a more user-friendly way than assume statements... But a language assume() would also be very useful!