mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yury Norov <ynorov@nvidia.com>
To: Tzu-Ching Yang <shingekinocore@gmail.com>
Cc: Brian Cain <bcain@kernel.org>,
	linux-hexagon@vger.kernel.org, Yury Norov <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-arch@vger.kernel.org, llvm@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] hexagon: bitops: use the compiler builtins for ffs(), fls() and ffz()
Date: Wed, 18 Nov 2026 14:36:21 -0500	[thread overview]
Message-ID: <av3-Nf01tdGw62lh@yury> (raw)
In-Reply-To: <20261117054939.2126980-1-shingekinocore@gmail.com>

On Tue, Nov 17, 2026 at 01:49:39PM +0800, Tzu-Ching Yang wrote:
> The hexagon inline asm for ffs(), __ffs(), fls(), __fls() and ffz() is
> not needed: Clang, the only compiler that builds hexagon kernels, emits
> the same ct0, cl0 and ct1 instructions for the compiler builtins, and
> unlike the asm, the builtins fold constant arguments. The asm dates
> from the 2011 port (a86a7ce30ac0, "Hexagon: Add bitops support"),
> before the builtin-based asm-generic helpers existed (048fa2df92c3,
> "generic: Implement generic ffs/fls using __builtin_* functions").
> 
> Replace the asm with the asm-generic builtin helpers, and ffz() with
> the generic __ffs(~x), as arm64 does.
> 
> ffs(0) and fls(0) still return 0. __ffs() and __fls() now return
> unsigned int, which is as wide as unsigned long on hexagon, and ffz()
> returns unsigned int instead of long. __ffs(0), __fls(0) and ffz(~0)
> remain undefined.
> 
> With Clang 17 to 23, the functions of a defconfig vmlinux shrink by 1972
> to 6068 bytes in total (scripts/bloat-o-meter -t). One helper in
> lib/find_bit.c gets worse: _find_last_bit() executes one instruction
> more per call that finds a bit, because LLVM rewrites 31 - clz as 31 ^
> clz once it knows clz < 32, and the hexagon backend keeps the xor, which
> needs the 31 in a register, instead of sub(#31, Rs) [1]. A few 1 <<
> ffz() sites also grow by a few instructions, as LLVM rewrites that
> expression too [2]. __find_nth_bit() executes two fewer, and the other
> helpers that find_bit_benchmark calls the same number.
> 
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Link: https://lore.kernel.org/all/affe7da1-41aa-4710-bee0-5e3c2b8a8dfc@app.fastmail.com/
> Link: https://github.com/llvm/llvm-project/issues/236764 [1]
> Link: https://github.com/llvm/llvm-project/issues/236765 [2]
> Signed-off-by: Tzu-Ching Yang <shingekinocore@gmail.com>

Reviewed-by: Yury Norov <ynorov@nvidia.com>

Would you like me to move it with the other bitops material, or leave
it to Brian?

> ---
> I have no hexagon hardware, and mainline does not boot in QEMU yet. On
> Brian's out-of-tree bcain/hexagon-qemu-virt branch (dc370baa041d,
> v7.3-rc1 plus 24 patches) with this patch on top, qemu_defconfig plus
> the ffs and bitops KUnit tests and find_bit_benchmark, built with Clang
> 17 and 23, boots under qemu-system-hexagon from qualcomm/qemu hex-next
> with -M virt,hvm=on (and under -M qcs6490-cdsp with the prebuilt H2
> hypervisor); the KUnit tests pass before and after, with the same
> .config on both sides. Under -M virt,hvm=on, with a TCG plugin that
> writes the same bitmaps into both kernels before each test and counts
> the executed instructions without interrupts, find_bit_benchmark's
> find_last_bit executes one instruction more per iteration,
> bitmap_find_next_zero_area_off 36 more on the random bitmap through its
> find_last_bit() calls, find_nth_bit and test_bitops' fns() two fewer,
> and the other tests the same.
> 
> Under qemu-hexagon (user mode), the old and new helpers return the
> same values for every defined 32-bit input, and fls64() and __ffs64()
> for selected 64-bit inputs, with Clang 17 to 23 at -O2 and -Os.
> 
> defconfig (also with the KUnit tests and find_bit_benchmark, with -Os
> and with -mv73) and allmodconfig were built with Clang 17 to 23 before
> and after, with the same .config on both sides: same W=1 warnings, no
> calls to __ctzsi2(), __clzsi2() or ffs(), and a smaller bloat-o-meter -t
> total in every defconfig-based vmlinux. allmodconfig does not link with
> Clang 17 and 18, before or after: ld.lld before 19.1.5 rejects the calls
> in head.S as out of range.
> 
> Instructions of each helper in a test function that only returns its
> result (return included), built with the kernel's flags by Clang 17
> to 22 at -O2 and -Os; Clang 23 adds an allocframe to both rows:
> 
>             ffs  __ffs  fls  __fls  ffz
>   asm         6      3    4      4    3
>   builtin     5      2    3      3    2
> 
> The instruction saved is the copy of the result to r0: the asm's
> early-clobber output ("=&r") cannot share the argument's register.
> 
> In packets, only ffs() takes one more than the asm (4 -> 5): with
> df5e4db805d5 ("hexagon: disable the LLVM packetizer") applied, each
> instruction of the builtin version gets a packet of its own, while
> the asm is bundled by hand. Without that workaround, which linux-next
> does not have yet, ffs() goes from 6 instructions in 3 packets to 5
> in 2.
> 
> Against the bcain/hexagon-fixes branch of bcain/linux.git
> (0304683b4c43); also applies to next-20261113 and to bitmap-for-next.
> 
>  arch/hexagon/include/asm/bitops.h | 100 ++----------------------------
>  1 file changed, 5 insertions(+), 95 deletions(-)
> 
> diff --git a/arch/hexagon/include/asm/bitops.h b/arch/hexagon/include/asm/bitops.h
> index b23cb13833af..975328172b7c 100644
> --- a/arch/hexagon/include/asm/bitops.h
> +++ b/arch/hexagon/include/asm/bitops.h
> @@ -194,101 +194,11 @@ arch_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr)
>  	return retval;
>  }
>  
> -/*
> - * ffz - find first zero in word.
> - * @word: The word to search
> - *
> - * Undefined if no zero exists, so code should check against ~0UL first.
> - */
> -static inline long __attribute_const__ ffz(int x)
> -{
> -	int r;
> -
> -	asm("%0 = ct1(%1);\n"
> -		: "=&r" (r)
> -		: "r" (x));
> -	return r;
> -}
> -
> -/*
> - * fls - find last (most-significant) bit set
> - * @x: the word to search
> - *
> - * This is defined the same way as ffs.
> - * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
> - */
> -static inline __attribute_const__ int fls(unsigned int x)
> -{
> -	int r;
> -
> -	asm("{ %0 = cl0(%1);}\n"
> -		"%0 = sub(#32,%0);\n"
> -		: "=&r" (r)
> -		: "r" (x)
> -		: "p0");
> -
> -	return r;
> -}
> -
> -/*
> - * ffs - find first bit set
> - * @x: the word to search
> - *
> - * 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(int x)
> -{
> -	int r;
> -
> -	asm("{ P0 = cmp.eq(%1,#0); %0 = ct0(%1);}\n"
> -		"{ if (P0) %0 = #0; if (!P0) %0 = add(%0,#1);}\n"
> -		: "=&r" (r)
> -		: "r" (x)
> -		: "p0");
> -
> -	return r;
> -}
> -
> -/*
> - * __ffs - find first bit in word.
> - * @word: The word to search
> - *
> - * Undefined if no bit exists, so code should check against 0 first.
> - *
> - * bits_per_long assumed to be 32
> - * numbering starts at 0 I think (instead of 1 like ffs)
> - */
> -static inline __attribute_const__ unsigned long __ffs(unsigned long word)
> -{
> -	int num;
> -
> -	asm("%0 = ct0(%1);\n"
> -		: "=&r" (num)
> -		: "r" (word));
> -
> -	return num;
> -}
> -
> -/*
> - * __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.
> - * bits_per_long assumed to be 32
> - */
> -static inline __attribute_const__ unsigned long __fls(unsigned long word)
> -{
> -	int num;
> -
> -	asm("%0 = cl0(%1);\n"
> -		"%0 = sub(#31,%0);\n"
> -		: "=&r" (num)
> -		: "r" (word));
> -
> -	return num;
> -}
> +#include <asm-generic/bitops/builtin-__ffs.h>
> +#include <asm-generic/bitops/builtin-ffs.h>
> +#include <asm-generic/bitops/builtin-__fls.h>
> +#include <asm-generic/bitops/builtin-fls.h>
> +#include <asm-generic/bitops/ffz.h>
>  
>  #include <asm-generic/bitops/lock.h>
>  #include <asm-generic/bitops/non-instrumented-non-atomic.h>
> 
> base-commit: 0304683b4c43048082483e9445fdd0691d088431
> -- 
> 2.34.1

      parent reply	other threads:[~2026-11-18 19:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  5:49 Tzu-Ching Yang
2026-11-17  9:16 ` Arnd Bergmann
2026-11-18 19:36 ` 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=av3-Nf01tdGw62lh@yury \
    --to=ynorov@nvidia.com \
    --cc=arnd@arndb.de \
    --cc=bcain@kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-hexagon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=llvm@lists.linux.dev \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®