Thread overview
Can we choose branch prediction like GCC?
May 10, 2022
rempas
May 10, 2022
kinke
May 10, 2022
rempas
May 10, 2022
kinke
May 11, 2022
rempas
May 10, 2022

In GCC, there is a builtin called "__builtin_expect" that allows us to tell if we expect a branch to hit or not so the compiler can choose the branch prediction we want (unless I don't understand something correctly). Is there a way to do that in LDC?

May 10, 2022

On Tuesday, 10 May 2022 at 10:02:21 UTC, rempas wrote:

>

Is there a way to do that in LDC?

Yes: https://github.com/ldc-developers/druntime/blob/bf27af6ef6dab9e45f005f67cc42e3a3bb393623/src/ldc/intrinsics.di#L616-L624

May 10, 2022

On Tuesday, 10 May 2022 at 13:19:33 UTC, kinke wrote:

>

Yes: https://github.com/ldc-developers/druntime/blob/bf27af6ef6dab9e45f005f67cc42e3a3bb393623/src/ldc/intrinsics.di#L616-L624

Thank you for your answer! However, I'm trying to understand how it works but I cannot seem to get it. I'm trying to do something like the following but it seems that it doesn't work (as it makes sense):

import ldc.intrinsics;
import core.stdc.stdio;

extern (C) void main() {
  int x = 0;
  llvm_expect(x < 10, 0) {
    printf("x is less than 10!\n");
  }
}

I did tried for about half an hour but couldn't find anything... Any ideas how it works?

May 10, 2022

On Tuesday, 10 May 2022 at 20:31:41 UTC, rempas wrote:

>

I did tried for about half an hour but couldn't find anything... Any ideas how it works?

As with gcc's __builtin_expect, one normally uses it in an if condition: if (llvm_expect(x < 10, false)) ….

May 11, 2022

On Tuesday, 10 May 2022 at 22:34:23 UTC, kinke wrote:

>

As with gcc's __builtin_expect, one normally uses it in an if condition: if (llvm_expect(x < 10, false)) ….

Oh, how I didn't thought about trying this out....

Thank you! This will do the trick! Have an amazing day my friend!