ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.85k stars 2.55k forks source link

Missing `__cpu_indicator_init` and `__cpu_model` in compiler-rt #18074

Open lacc97 opened 11 months ago

lacc97 commented 11 months ago

Zig Version

0.12.0-dev.1685+994e19164

Steps to Reproduce and Observed Behavior

Trying to compile a C file where a function has target_clone attribute fails. e.g. for targets.c:

#include <stdio.h>

int foo(void) __attribute__ ((__target_clones__("default,avx2")));

int main() {
    return foo();
}

int __attribute__ ((__target_clones__("default,avx2"))) foo(void) {
    return 0;
}

Running

zig cc -march=baseline -o targets targets.c

fails with

ld.lld: error: undefined symbol: __cpu_indicator_init
>>> referenced by targets.c
>>>               /home/luisc/.cache/zig/o/15708c62c5fddecc0c537adadb7a1a1b/targets.o:(foo.ifunc)

ld.lld: error: undefined symbol: __cpu_model
>>> referenced by targets.c
>>>               /home/luisc/.cache/zig/o/15708c62c5fddecc0c537adadb7a1a1b/targets.o:(foo.ifunc)

Expected Behavior

This should compile successfully.

lacc97 commented 11 months ago

These symbols are defined in compiler-rt in LLVM: https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/builtins/cpu_model.c

Also, maybe related to https://github.com/ziglang/zig/issues/4591.

matu3ba commented 11 months ago

These symbols are defined in compiler-rt in LLVM: https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/builtins/cpu_model.c

only for those targets:

#if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) ||           \
     defined(_M_X64)) &&                                                       \
    (defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER))

aarch64 and other platforms use different symbols/strategies.

matu3ba commented 11 months ago

Exporting the symbols should be rather straightforward, since the x86 and x86_64 feature detection is already implemented. See https://github.com/ziglang/zig/issues/4591#issuecomment-1837271967.

matu3ba commented 11 months ago

@lacc97 Do you want to take a shot at it?

lacc97 commented 11 months ago

Sure I'll give it a shot.

lacc97 commented 11 months ago

@matu3ba PR here :) https://github.com/ziglang/zig/pull/18193