[llvm] [SDag][ARM][RISCV] Allow lowering CTPOP into a libcall (PR #101786)

Sergei Barannikov via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 24 11:27:08 PDT 2025


s-barannikov wrote:

> I could try to push for something like this in the kernel:

I don't mind sharing the headache :sweat_smile:. And, who knows, maybe this patch will be welcomed?

> If that is not palatable in the kernel, is the only way to avoid this optimization -fno-builtin-popcount?

I think this option only applies to `libc` functions, not to `libgcc` ones.

> I wonder if that matches what GCC does, which could be additional justification? I do understand the kernel is being special here so I can pursue the addition of `__popcount{d,s}i2()` in the kernel first and come back if that is not successful.

GCC generates calls to `__clzXi2`/`__ctzXi2` for the corresponding builtins. This makes sense to me, but might not be a weighty argument for others. Will see, I guess, there was [a precedent](https://github.com/llvm/llvm-project/commit/96e3cd85bd2ddf7cb059665d94f0f66887692042) once.
A much stronger argument would be "it is faster and (and less code size)". While reduced code size is trivial to prove, performance measurements require hardware handy.

> I do understand the kernel is being special here so I can pursue the addition of `__popcount{d,s}i2()` in the kernel first and come back if that is not successful.

Will be much appreciated, thank you! I'll keep trying to fix the issue on the compiler side.


https://github.com/llvm/llvm-project/pull/101786


More information about the llvm-commits mailing list