[llvm] [SDag][ARM][RISCV] Allow lowering CTPOP into a libcall (PR #101786)
Nathan Chancellor via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 25 07:00:48 PDT 2025
nathanchance wrote:
> > I could try to push for something like this in the kernel:
>
> I don't mind sharing the headache 😅. And, who knows, maybe this patch will be welcomed?
>
> > 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 continue trying to fix the issue on the compiler side.
Heh, I [sent a message to Linus yesterday](https://lore.kernel.org/20250425003342.GA795313@ax162/) and [the reaction](https://lore.kernel.org/CAHk-=whfT3A8K2Z+WbieGG5Hhc9QAT5s3qsbB19O0Roj2G5tfA@mail.gmail.com/) was generally what I expected.
His suggestion was effectively
```diff
diff --git a/lib/hweight.c b/lib/hweight.c
index c94586b62551..00b2ff970203 100644
--- a/lib/hweight.c
+++ b/lib/hweight.c
@@ -66,3 +66,8 @@ unsigned long __sw_hweight64(__u64 w)
#endif
}
EXPORT_SYMBOL(__sw_hweight64);
+
+int __popcountsi2(unsigned int w) __alias(__sw_hweight32);
+EXPORT_SYMBOL(__popcountsi2);
+int __popcountdi2(__u64 w) __alias(__sw_hweight64);
+EXPORT_SYMBOL(__popcountdi2);
```
which does also resolve the error but I realized that neither of these suggestions I have proposed will work in their current form because (at least for the RISC-V configuration that I chcked) `__sw_hweight64` gets transformed into a call to `__popcountdi2` so trying to reuse either `__arch_hweight64` or `__sw_hweight64` to define `__popcountdi2` will just result in an infinite loop…
```
00000000000000be <__sw_hweight64>:
be: 1141 addi sp, sp, -0x10
c0: e406 sd ra, 0x8(sp)
c2: e022 sd s0, 0x0(sp)
c4: 0800 addi s0, sp, 0x10
c6: 00000097 auipc ra, 0x0
00000000000000c6: R_RISCV_CALL_PLT __popcountdi2
00000000000000c6: R_RISCV_RELAX *ABS*
ca: 000080e7 jalr ra <__sw_hweight64+0x8>
ce: 60a2 ld ra, 0x8(sp)
d0: 6402 ld s0, 0x0(sp)
d2: 0141 addi sp, sp, 0x10
d4: 8082 ret
```
https://github.com/llvm/llvm-project/pull/101786
More information about the llvm-commits
mailing list