Standard way to supply hints to branches
Manu
turkeyman at gmail.com
Thu Sep 12 22:59:32 UTC 2024
On Thu, 12 Sept 2024 at 18:21, Johan via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:
> On Wednesday, 11 September 2024 at 18:54:21 UTC, Walter Bright
> wrote:
> > On 9/11/2024 4:18 AM, Manu wrote:
> >> Obfuscating the contorting code is not the goal or a
> >> reasonable solution; we just want a mechanism in the language
> >> to take advantage of this general category of support in
> >> whatever architecture.
> >
> > I tend to agree, but when micro-optimizing one's code, one
> > accepts that its elegance is going to decline.
> >
> > There are at least 3 ways to organize the code to get what you
> > want. I won't claim they're beautiful, but they work.
>
> FWIW, I agree with Walter that it is not worth to add a new
> special feature to the language for the problem at hand.
>
> It is rare that you'd want to explicitly tell whether a branch is
> likely or not, and for that case the tool already exist
> (`llvm_expect` for LDC).
expect() statements are not a good time.
And something portable needs to be added anyway; so I'd seriously suggest
not using expect as an API for this.
It think it would hurt D as a whole to
> have special stuff for such a rare thing.
>
How?
And it's not 'rare'; it's 'niche'. For a microcontroller with no branch
prediction, it's common and essential.
It's literally unworkable to write code for the platform without this tool;
you can't have branches constantly mispredicting.
I do agree that it'd be good to have a common interface for all
> compilers, which can be achieved by e.g. introducing a
> `core.micro_optimization` module.
> That module could collect more of such micro optimization things,
> like telling the compiler about likely function pointers or class
> types (devirtualization). [1]
>
> Cheers,
> Johan
>
>
> [1]
> ```
> auto is_likely(alias Likely, Fptr, Args...)(Fptr fptr, Args args)
> {
> return (fptr == &Likely) ? Likely(args) : fptr(args);
> }
> // ...
> void function() fptr = get_function_ptr();
> fptr.is_likely!likely_function();
> ```
> See
> https://johanengelen.github.io/ldc/2016/04/13/PGO-in-LDC-virtual-calls.html
>
We virtually always end up with weird or butchered solutions to almost
everything for no good reason. Why can't we just do the normal and
reasonable thing for once?
Adding an attribute to a control statement is virtually zero impact; it's
such an obvious and elegant solution. This also isn't mutually exclusive
with expect(), or your suggestion above.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20240912/3fa8456f/attachment.htm>
More information about the Digitalmars-d
mailing list