[llvm-bugs] [Bug 48459] New: __cpu_indicator_init() runs too late on macOS

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Dec 9 09:09:50 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48459

            Bug ID: 48459
           Summary: __cpu_indicator_init() runs too late on macOS
           Product: compiler-rt
           Version: 11.0
          Hardware: PC
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: builtins
          Assignee: unassignedbugs at nondot.org
          Reporter: chfast at gmail.com
                CC: llvm-bugs at lists.llvm.org

The usage of __builtin_cpu_supports() requires CPU information being
initialized up front. This is done by __cpu_indicator_init() which is
implemented in compiler-rt/builtins as __attribute__((constructor)). From the
GCC documentation and this comment in code:
https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/builtins/cpu_model.c#L709-L713,
the __cpu_indicator_init() should be invoked with high priority and calling it
directly from other "constructors" should only be needed only in very specific
cases like IFUNC resolving.

However this promise is not fulfilled on macOS. From my experiments the
__cpu_indicator_init() is actually the last in __mod_init_func section of
Mach-O.

For example program cpu_check.c:


int puts(const char *str);

__attribute__((constructor)) static void check_cpu()
{
    if (__builtin_cpu_supports("sse2"))
        puts("SSE2 supported");
    else
        puts("SSE2 not supported");
}

int main() {}


I get the following:

> objdump -macho -section=__mod_init_func ./a.out
./a.out:
Contents of (__DATA,__mod_init_func) section
0x0000000100002020 0x00000001000014d0 _check_cpu
0x0000000100002060 0x0000000100001510 ___cpu_indicator_init


It does not matter if I set any explicit priority value for my constructor.


In the compiler-rt the __cpu_indicator_init() is declared with this attribute:

#if defined(HAVE_INIT_PRIORITY)
#define CONSTRUCTOR_ATTRIBUTE __attribute__((__constructor__ 101))
#elif __has_attribute(__constructor__)
#define CONSTRUCTOR_ATTRIBUTE __attribute__((__constructor__))
#else

https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/builtins/cpu_model.c#L16-L20

I haven't built it for macOS, but at least from searching on GitHub the
HAVE_INIT_PRIORITY is never defined therefore it does not have the priority
set.
Furthermore, the 101 priority value does not look correct as "user" priorities
start from 100.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201209/17600ae3/attachment-0001.html>


More information about the llvm-bugs mailing list