[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAKwvOdm_y6UOnxFrAiDxou2jc8CRUvyhfH9kAdc3PG0=bEvduw@mail.gmail.com>
Date: Fri, 25 Aug 2023 17:52:15 -0700
From: Nick Desaulniers <ndesaulniers@...gle.com>
To: Linus Torvalds <torvalds@...ux-foundation.org>
Cc: Bill Wendling <morbo@...gle.com>, Helge Deller <deller@....de>,
Nathan Chancellor <nathan@...nel.org>,
linux-kernel@...r.kernel.org, linux-parisc@...r.kernel.org,
Andrew Morton <akpm@...ux-foundation.org>,
Chanho Min <chanho.min@....com>,
Geert Uytterhoeven <geert@...ux-m68k.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Kees Cook <keescook@...omium.org>,
clang-built-linux <llvm@...ts.linux.dev>,
"maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" <x86@...nel.org>
Subject: Re: [PATCH] lib/clz_ctz.c: Fix __clzdi2() and __ctzdi2() for 32-bit kernels
On Fri, Aug 25, 2023 at 4:35 PM Linus Torvalds
<torvalds@...ux-foundation.org> 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
Powered by blists - more mailing lists