Re: [PATCH 2/5] -march=native: POPCNT support

From: Alexey Dobriyan
Date: Fri Dec 08 2017 - 05:11:14 EST


On 12/8/17, H. Peter Anvin <hpa@xxxxxxxxx> wrote:
> On 12/07/17 14:41, Alexey Dobriyan wrote:
>> Mainline kernel can only generate "popcnt rax, rdi" instruction
>> with alternative masquareading as function call. Patch allows
>> to generate all POPCNT variations and inlines hweigth*() family of
>> functions.

> -march=native really would be better implemented by examining the macros
> generated by gcc which correspond to the selected -m options
> (-march=native really just selects a combination of -m options.) It
> seems bizarre to just reimplement the mechanism that already exists.

This mechanism can do feature detection part only.

Code generation tweaks (uop fusing etc) require passing -march=native,
and hardly can be expressed through defines.

Some things aren't recorded in defines (--param l1-cache-size),
it is not clear how and what to optimize base on cache sizes,
but if kernel controls CPU detection code there is no need to wait
when someone smart comes up with an idea.

Again, clang emits slightly different defines.

> Now, this specific case would be better done with alternatives; we can
> patch in a JMP to an out-of-line stub to mangle the arguments. Then you
> get the benefit on all systems and don't need to decide at compile time.
>
> The reason to use -m options for this would be to be able to use the
> __builtin_popcount() and __builtin_popcountl() intrinsics, which would
> allow gcc to schedule it and optimize arbitrarily.
>
> So, much more something like:
>
> #ifdef __POPCNT__
>
> static inline unsigned int __arch_hweight64(uint64_t x)
> {
> return __builtin_popcountll(x);

OK