[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250425003342.GA795313@ax162>
Date: Thu, 24 Apr 2025 17:33:42 -0700
From: Nathan Chancellor <nathan@...nel.org>
To: Linus Torvalds <torvalds@...ux-foundation.org>
Cc: llvm@...ts.linux.dev, linux-kernel@...r.kernel.org
Subject: Adding __popcountsi2 and __popcountdi2
Hi Linus,
Since I ran into problems at pull request time previously, I figured I
would save myself some trouble and gauge your opinion up front. How
palatable would the diff at the end of the thread be for the kernel?
Clang would like to start emitting calls to __popcountsi2 and
__popcountdi2 [1] for certain architectures (ARM and RISC-V), which
would normally be a part of the compiler runtime but obviously the
kernel does not link against it so it breaks the build. I figured added
these may not be as bad as the wcslen() case because most architectures
generally have an optimized popcount implementation and I am not sure
compiler builtins are banned entirely from the kernel but I can
understand if it is still contentious. It sounds like GCC has previously
wanted to something similar [2] and it was somewhat brought up on the
mailing lists [3] but never persued further it seems. Since this is a
compiler runtime function, '-fno-builtin' would not work to avoid this.
Cheers,
Nathan
[1]: https://github.com/llvm/llvm-project/pull/101786
[2]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105253#c10
[3]: https://lore.kernel.org/YlSb5D3rDTyCWpay@tucnak/
diff --git a/lib/Makefile b/lib/Makefile
index f07b24ce1b3f..0240fa7d6b5b 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -52,7 +52,7 @@ obj-y += lockref.o
obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \
- list_sort.o uuid.o iov_iter.o clz_ctz.o \
+ list_sort.o uuid.o iov_iter.o clz_ctz.o popcount.o \
bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
percpu-refcount.o rhashtable.o base64.o \
once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
diff --git a/lib/popcount.c b/lib/popcount.c
new file mode 100644
index 000000000000..0234961cc35e
--- /dev/null
+++ b/lib/popcount.c
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * The functions in this file aren't called directly, but may be emitted
+ * by the compiler.
+ */
+
+#include <linux/bitops.h>
+#include <linux/export.h>
+
+int __popcountsi2(unsigned int val);
+int __popcountsi2(unsigned int val)
+{
+ return __arch_hweight32(val);
+}
+EXPORT_SYMBOL(__popcountsi2);
+
+int __popcountdi2(unsigned long long val);
+int __popcountdi2(unsigned long long val)
+{
+ return __arch_hweight64(val);
+}
+EXPORT_SYMBOL(__popcountdi2);
Powered by blists - more mailing lists