Re: [PATCH] lib/clz_ctz.c: Fix __clzdi2() and __ctzdi2() for 32-bit kernels

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Aug 25, 2023 at 4:35 PM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> But for non-constant ones, the inline asm actually generates better
> code: gcc generatea some disgusting mess with a 'bsf' followed by a
> 'cmov' for the zero case, when we know better.
>
> See for example
>
>    https://godbolt.org/z/jKKf48Wsf
>
> I don't understand why compiler people prefer a builtin that is an
> untested special case that assumes that the compiler knows what is
> going on (and often doesn't), over a generic escape facility that is
> supported and needed anyway (inline asm).
>
> In other words: the statement "builtins generate better code" is
> simply PROVABLY NOT TRUE.
>
> Builtins have often generated *worse* code than using inline asms, to
> the point where "worse" is actively buggy crap.
>
> At least inline asms are reliable. That's a *big* deal.

So 2 concerns where "I'll do it in inline asm" can pessimize codegen:
1. You alluded to this, but what happens when one of these functions
is called with a constant? (Not just a literal value, but a value that
can be proven constant at compile time via optimizations as well?)
arch/x86/include/asm/bitops.h got this right for ffs(), but it did not
for fls()! (I think that could be `if (__builtin_constant_p(x)) return
x ? 32 - __builtin_clz(x) : 0;` but check my math; oh, good job
arch/powerpc/include/asm/bitops.h).
2. by providing the definition of a symbol typically provided by libc
(and then not building with -ffreestanding) pessimizes libcall
optimization.
example: https://godbolt.org/z/crrTKEf6G
ffs() gets this right again by using a macro, and __always_inline can
work around this somewhat (so fls() if off the hook here). But any
attempt using `static inline` would be pessimized for constants.
-- 
Thanks,
~Nick Desaulniers




[Index of Archives]     [Linux SoC]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux