Standard way to supply hints to branches

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Fri Sep 13 10:02:25 UTC 2024


On 13/09/2024 9:54 PM, Quirin Schroll wrote:
> The best thing about |pragma| is that it allows for additional 
> arguments. For example, a |bool| to enable or disable it: 
> |pragma(unlikely, false)| could be as if it’s not there. Great for 
> meta-programming. For |pragma(likely)|, a numerical probability makes 
> sense, too: |pragma(likely, 0)| is equivalent to |pragma(unlikely)| and 
> a single |pragma(likely, value)| (with |value| > 0) is |pragma(likely)|.

We can do this with a UDA.

```d
struct unlikely {
	bool activate=true;
}

if (...) @unlikely(false) {

}
```

> Generally speaking, if there are more than two branches, with two or 
> more of them tagged |likely|, they can be given weights, that may be 
> derived from abstract reasoning or profiling. That’s essentially what 
> GCC has with |__builtin_expect_with_probability|, except that it’s with 
> weights and not probabilities.

The way it works in D is if-else not if-elseif-else.

So for something like this, you are swapping the assumption from one 
path to another.

I don't think we need probability support, just because of how the IR 
will be laid out to the backend.



More information about the Digitalmars-d mailing list