All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yury Norov <ynorov@nvidia.com>
To: Tzu-Ching Yang <shingekinocore@gmail.com>
Cc: Max Filippov <jcmvbkbc@gmail.com>,
	Chris Zankel <chris@zankel.net>,
	Yury Norov <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] xtensa: bitops: use the builtins for __ffs(), ffs(), ffz() and __fls()
Date: Wed, 2 Dec 2026 21:31:06 -0500	[thread overview]
Message-ID: <axDUaobzJVd2s5nz@yury> (raw)
In-Reply-To: <20261201142253.1986402-1-shingekinocore@gmail.com>

On Tue, Dec 01, 2026 at 10:22:53PM +0800, Tzu-Ching Yang wrote:
> Let GCC compute __ffs(), ffs(), ffz() and __fls() at compile time when
> the argument is a constant, on cores with the NSA option, by using the
> compiler builtins instead of the nsau asm wrappers; GCC implements the
> builtins with the same nsau instruction on these cores. The nsau
> wrappers come from the initial port in 2005, commit 9a8fd5589902
> ("[PATCH] xtensa: Architecture support for Tensilica Xtensa Part 6"),
> and from 2009 for __fls(), so they predate the asm-generic builtin
> versions, which were added in 2012.
> 
> Use the asm-generic builtin versions of __ffs(), ffs() and __fls()
> inside the existing XCHAL_HAVE_NSA block, and use the generic ffz(),
> which is __ffs(~x), with and without XCHAL_HAVE_NSA instead of only
> without it. fls() keeps its asm, as the builtin version of fls() needs
> a test for zero that nsau makes unnecessary by returning 32 for 0.
> 
> With GCC 8 to 16, the functions of each vmlinux tested are smaller in
> total with this patch than without it, by at least 1346 and at most
> 10002 bytes (scripts/bloat-o-meter -t; 37 builds on xtensa-for-next: 8
> defconfigs, each with at least two of these compilers).
> 
> With GCC 8 to 13, none of the __ffs()-based functions of
> lib/find_bit.c that find_bit_benchmark tests executes more
> instructions than without this patch. With GCC 14 to 16, each of these
> functions executes one instruction more per call that finds a bit than
> without this patch (two more in __find_nth_bit() with GCC 15 and 16,
> one of them a register move), as these compilers need one instruction
> more than GCC 13 to add __builtin_ctzl() to a value in a register [1].
> 
> The generic ffz() lets GCC 10 to 16 inline next_signal() into
> dequeue_signal(), which they do not do without this patch. For
> generic_kc705_defconfig with GCC 16.2 for the dc233c core, a call of
> dequeue_signal() then executes 2 to 22 instructions fewer than without
> this patch, counted along the disassembly, and dequeue_signal() is 163
> bytes larger. On the fd allocation and block flush paths, counted in
> three builds, a call executes between three instructions fewer and
> four more than without this patch.
> 
> ffs(0) still returns 0; __ffs(0), __fls(0) and ffz(~0UL) remain
> undefined. __ffs() and __fls() now return unsigned int instead of
> unsigned long, and ffz() unsigned int instead of int, like the
> asm-generic versions that the !XCHAL_HAVE_NSA case already uses;
> unsigned int and unsigned long are 32 bits wide on xtensa. ffs() and
> ffz() become macros, for __builtin_ffs() and __ffs(~(x)).
> 
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Link: https://lore.kernel.org/all/affe7da1-41aa-4710-bee0-5e3c2b8a8dfc@app.fastmail.com/
> Link: https://gcc.gnu.org/PR129142 [1]
> Signed-off-by: Tzu-Ching Yang <shingekinocore@gmail.com>
> ---
> I have no xtensa hardware. On QEMU kc705, the ffs and bitops KUnit tests
> pass before and after, with the dc233c core and GCC 13.5 to 16.2 built
> for that core, and with the test_kc705_be core and the kernel.org GCC
> 8.1 to 16.2. Under QEMU user mode, the old and new __ffs(), ffs(), ffz()
> and __fls() return the same values for every defined 32-bit input with
> GCC 8 to 16. Max, could you run the ffs and bitops KUnit tests and
> lib/find_bit_benchmark.c before and after on hardware, for example the
> KC705 board with dc233c?
> 
> In virt_defconfig with GCC 16.2 for dc233c, the number of nsau
> instructions whose operand is a constant (loaded by movi or l32r earlier
> in the same basic block) goes from 178 without this patch to 9 with it,
> and napi_pp_put_page() shrinks from 284 to 146 bytes because
> PP_MAGIC_MASK, which uses __fls() and __ffs(PAGE_OFFSET), now folds.
> 
> Executed instructions per iteration of four find_bit_benchmark tests
> on QEMU kc705 with dc233c, counted by a TCG plugin (random bitmap,
> generic_kc705_defconfig with the benchmark built in, the same bitmaps
> before and after, written through the QEMU gdbstub):
> 
>                      GCC 13.5            GCC 16.2
>                      before    after     before    after
>   ------------------------------------------------------
>   find_next_bit        30.2     30.2       26.3     27.3
>   find_first_bit     2587.1   2587.1     2586.1   2587.1
>   find_nth_bit      17482.5  17482.5    19017.7  19019.7
>   find_last_bit        32.2     32.2       29.2     29.2
> 
> Instructions per call with this patch minus without it (negative:
> fewer with this patch) on paths that use ffz(), counted along the
> disassembly:
> 
>                                  kc705      kc705      iss
>                                  GCC 16.2   GCC 13.5   kernel.org
>                                  dc233c     dc233c     GCC 16.2
>   ---------------------------------------------------------------
>   dequeue_signal(), five cases   -2 to -22  -5 to -26  -3 to -22
>   get_signal(), no sync. signal  +1          0          0
>   alloc_fd(), fast path          -1         -3         +2
>   blk_flush_complete_seq()       -2 to +3   -3 to +4   -2 to +1
>   flush_end_io()                 -1 to +1   -1 to +1   +1 to +3
>     and per request it ends       0         +1          0
> 
> kc705 is generic_kc705_defconfig, iss is iss_defconfig. The five cases
> are no signal pending and one signal in either word of the thread's
> or the shared pending set. With the kernel.org GCC 10 to 15
> (iss_defconfig), the five cases take 1 to 27 instructions fewer than
> without this patch, except one signal in word 0 of the thread's set
> with GCC 12.5, which takes 2 more.
> 
> The builtins need a GCC built for a core with NSA, as the rest of
> such a kernel already does (entry.S uses nsau); with a GCC built
> without NSA, this patch fails to link (undefined __ffssi2).

You're a kernel engineer. Your job is writing some code, but more
important, making your own judgement and your own analysis. Poking
AI in a hope that your reviewers will read those endless and
pointless "conclusions" and approve your work is a wrong approach.

Your work is to read it yourself, make your own decision and explain
it in your own words.

> I can send one of these two versions instead:

I'm sure you can. While your token balance is positive, you can do
everything.

> - keep the ffz() asm: the paths of the ffz() table stay as without
>   this patch; 32 of the 37 vmlinux are up to 230 bytes smaller than
>   with this patch, the other five 1 to 160 bytes larger;
> - keep the __ffs() and ffz() asm: lib/find_bit.o stays as in
>   xtensa-for-next with GCC 8 to 16, but __ffs() and ffz() of a
>   constant do not fold; the 37 vmlinux are 1370 to 9972 bytes larger
>   than with this patch (-651 to +86 against xtensa-for-next).
> 
> Against xtensa-for-next (de1aec038588); also applies to v7.4-rc5 and
> bitmap-for-next.
 
>  arch/xtensa/include/asm/bitops.h | 45 +++-----------------------------
>  1 file changed, 4 insertions(+), 41 deletions(-)
> 
> diff --git a/arch/xtensa/include/asm/bitops.h b/arch/xtensa/include/asm/bitops.h
> index f7390b6761e1..ba7e0b61ccdb 100644
> --- a/arch/xtensa/include/asm/bitops.h
> +++ b/arch/xtensa/include/asm/bitops.h
> @@ -32,36 +32,6 @@ static inline unsigned long __cntlz (unsigned long x)
>  	return lz;
>  }
>  
> -/*
> - * ffz: Find first zero in word. Undefined if no zero exists.
> - * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
> - */
> -
> -static inline int __attribute_const__ ffz(unsigned long x)
> -{
> -	return 31 - __cntlz(~x & -~x);
> -}
> -
> -/*
> - * __ffs: Find first bit set in word. Return 0 for bit 0
> - */
> -
> -static inline __attribute_const__ unsigned long __ffs(unsigned long x)
> -{
> -	return 31 - __cntlz(x & -x);
> -}
> -
> -/*
> - * ffs: Find first bit set in word. This is defined the same way as
> - * the libc and compiler builtin ffs routines, therefore
> - * differs in spirit from the above ffz (man ffs).
> - */
> -
> -static inline __attribute_const__ int ffs(unsigned long x)
> -{
> -	return 32 - __cntlz(x & -x);
> -}
> -
>  /*
>   * fls: Find last (most-significant) bit set in word.
>   * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
> @@ -72,28 +42,21 @@ static inline __attribute_const__ int fls (unsigned int x)
>  	return 32 - __cntlz(x);
>  }
>  
> -/**
> - * __fls - find last (most-significant) set bit in a long word
> - * @word: the word to search
> - *
> - * Undefined if no set bit exists, so code should check against 0 first.
> - */
> -static inline __attribute_const__ unsigned long __fls(unsigned long word)
> -{
> -	return 31 - __cntlz(word);
> -}
> +# include <asm-generic/bitops/builtin-__ffs.h>
> +# include <asm-generic/bitops/builtin-ffs.h>
> +# include <asm-generic/bitops/builtin-__fls.h>
>  #else
>  
>  /* Use the generic implementation if we don't have the nsa/nsau instructions. */
>  
>  # include <asm-generic/bitops/ffs.h>
>  # include <asm-generic/bitops/__ffs.h>
> -# include <asm-generic/bitops/ffz.h>
>  # include <asm-generic/bitops/fls.h>
>  # include <asm-generic/bitops/__fls.h>
>  
>  #endif
>  
> +#include <asm-generic/bitops/ffz.h>
>  #include <asm-generic/bitops/fls64.h>
>  
>  #if XCHAL_HAVE_EXCLUSIVE
> 
> base-commit: de1aec038588f12b1faeef9e94abe9608814d39a
> -- 
> 2.34.1

OK... The patch itself looks clean. I'll take the code in bitmap-for-next,
but not that word salad in the commit message. Please write better
commit messages next time if you want to move your patches with me.

Thanks,
Yury

      reply	other threads:[~2026-12-03  2:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:22 [PATCH] xtensa: bitops: use the builtins for __ffs(), ffs(), ffz() and __fls() Tzu-Ching Yang
2026-12-03  2:31 ` Yury Norov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axDUaobzJVd2s5nz@yury \
    --to=ynorov@nvidia.com \
    --cc=arnd@arndb.de \
    --cc=chris@zankel.net \
    --cc=jcmvbkbc@gmail.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=shingekinocore@gmail.com \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.