[llvmlinux] [PATCH] x86/hweight: LLVMLinux: Fix __arch_hweight{32, 64}() for compilation with clang

Sedat Dilek sedat.dilek at gmail.com
Sun Sep 13 21:33:58 UTC 2015

On Sun, Sep 13, 2015 at 11:14 PM, PaX Team <pageexec at gmail.com> wrote:
> On 13 Sep 2015 at 13:25, Sanjoy Das wrote:
>> I'm an outsider to the llvmlinux project, so I cannot comment on the
>> patch as is; as I do not know how what is a good candidate for merging
>> into what tree and what isn't.  Sedat CC'ed me only because we had a
>> discussion about this issue on IRC.
>> However, I will comment that clang not supporting ALTERNATIVE is only
>> one issue here.  I think there is a bug that needs to be fixed in
>> __arch_hweight64 --
>>       asm (ALTERNATIVE("call __sw_hweight64", POPCNT64, X86_FEATURE_POPCNT)
>>                    : "="REG_OUT (res)
>>                    : REG_IN (w));
>> does not specify that the "call __sw_hweight64" instruction may clobber
>> caller saved registers (like %rcx and %rdx).
> actually, the __sw_hweight* functions may *not* clobber anything. the
> trick is in how lib/hweight.o is compiled: CFLAGS contains a set of
> -fcall-saved-REG switches (cf. ARCH_HWEIGHT_CFLAGS) that effectively
> override the function call ABI for the functions defined in this file
> so there's no need for the callers to specify any extra clobbers. this
> way the fast path execution (inlined popcnt insn) doesn't unnecessarily
> perturb the register allocator in the callers.

[ lib/Makefile ]

GCOV_PROFILE_hweight.o := n
+ifneq ($(COMPILER),clang)
CFLAGS_hweight.o = $(subst $(quote),,$(CONFIG_ARCH_HWEIGHT_CFLAGS))
obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o

[ arch/x86/Kconfig ]

default "-fcall-saved-ecx -fcall-saved-edx" if X86_32
default "-fcall-saved-rdi -fcall-saved-rsi -fcall-saved-rdx
-fcall-saved-rcx -fcall-saved-r8 -fcall-saved-r9 -fcall-saved-r10
-fcall-saved-r11" if X86_64

[1] http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/lib/Makefile?id=v4.2#n58
[2] http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/arch/x86/Kconfig?id=v4.2#n279

More information about the LLVMLinux mailing list