Re: [PATCH -tip 2/2] x86/hweight: Use POPCNT when available with X86_NATIVE_CPU option

From: Ingo Molnar
Date: Sun Mar 30 2025 - 15:20:38 EST



* Borislav Petkov <bp@xxxxxxxxx> wrote:

> On Sun, Mar 30, 2025 at 08:47:31PM +0200, Ingo Molnar wrote:
> > +#ifdef __POPCNT__
> > + asm_inline (ASM_FORCE_CLR "popcntl %[val], %[cnt]"
> > + : [cnt] "=&r" (res)
> > + : [val] ASM_INPUT_RM (w));
> > +#else
> > asm_inline (ALTERNATIVE(ANNOTATE_IGNORE_ALTERNATIVE
> > "call __sw_hweight32",
> > ASM_CLR "popcntl %[val], %[cnt]",
> > X86_FEATURE_POPCNT)
> > : [cnt] "=a" (res), ASM_CALL_CONSTRAINT
> > : [val] REG_IN (w));
> > -
> > +#endif
>
> That ifdeffery.
>
> The alternative only is fine as this is the usual way we do those
> insns.
>
> The ifdeffery around it is ugly and is pushing it and it would be
> fine if it would bring anything but it doesn't. It is making the code
> ugly for no good reason whatsoever.

Tangible code size reduction, if it can be realized, is definitely
'something', so your claim is simply false.

> > Which is 3 straightforward lines of assembly code and a
> > straightforward #ifdef.
>
> And they bring what exactly?
>
> I haven't seen anything besides some super minor, completely
> pointless, hm, "savings". So much so that the uglification of the
> function is not worth it in the *least*.

Even 0.5K of .text reduction is a tangible benefit.

The kernel's 35 years long history comprises of literally over a
million patches, which were small and inconsequential 99% of the time.

> > My main objection is different: if __POPCNT__ isn't defined during
> > the kernel build of major Linux distros, then this optimization
> > almost doesn't exist to our users. And I don't think it's defined.
>
> Yah, that too.
>
> This whole effort is a total waste of time and energy.

We don't know yet for sure, but I don't think an absolutist "can't do"
approach is very productive.

Thanks,

Ingo