pytorch / cpuinfo

CPU INFOrmation library (x86/x86-64/ARM/ARM64, Linux/Windows/Android/macOS/iOS)
BSD 2-Clause "Simplified" License
970 stars 308 forks source link

Compile fails with clang 16 #171

Open jmontleon opened 11 months ago

jmontleon commented 11 months ago

I am seeing the following trying to build on Fedora 38 ppc64;e and s390x. I do not have this problem on x86_64 or aarch64.

I believe it is coming from the change in behavior to Wimplicit-function-declaration. https://www.redhat.com/en/blog/new-warnings-and-errors-clang-16

[ 11%] Building C object confu-deps/cpuinfo/CMakeFiles/cpuinfo.dir/src/api.c.o
cd /builddir/build/BUILD/pytorch-v2.0.1.gitf81f9093/redhat-linux-build/confu-deps/cpuinfo && /usr/bin/clang -DCPUINFO_LOG_LEVEL=2 -I/builddir/build/BUILD/pytorch-v2.0.1.gitf81f9093/third_party/cpuinfo/src -I/builddir/build/BUILD/pytorch-v2.0.1.gitf81f9093/third_party/cpuinfo/include -I/builddir/build/BUILD/pytorch-v2.0.1.gitf81f9093/third_party/cpuinfo/deps/clog/include -O2 -flto=thin -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS --config /usr/lib/rpm/redhat/redhat-hardened-clang.cfg -fstack-protector-strong   -m64 -march=z13 -mtune=z14 -fasynchronous-unwind-tables -fstack-clash-protection -DNDEBUG -std=c99 -fPIC -MD -MT confu-deps/cpuinfo/CMakeFiles/cpuinfo.dir/src/api.c.o -MF CMakeFiles/cpuinfo.dir/src/api.c.o.d -o CMakeFiles/cpuinfo.dir/src/api.c.o -c /builddir/build/BUILD/pytorch-v2.0.1.gitf81f9093/third_party/cpuinfo/src/api.c
/builddir/build/BUILD/pytorch-v2.0.1.gitf81f9093/third_party/cpuinfo/src/api.c:319:23: error: call to undeclared function 'syscall'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                if CPUINFO_UNLIKELY(syscall(__NR_getcpu, &cpu, NULL, NULL) != 0) {
                                    ^
/builddir/build/BUILD/pytorch-v2.0.1.gitf81f9093/third_party/cpuinfo/src/api.c:338:23: error: call to undeclared function 'syscall'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                if CPUINFO_UNLIKELY(syscall(__NR_getcpu, &cpu, NULL, NULL) != 0) {
                                    ^
2 errors generated.

A full build log can be found here: https://download.copr.fedorainfracloud.org/results/jmontleon/pytorch/fedora-38-s390x/06239794-pytorch/builder-live.log.gz

Maratyszcza commented 11 months ago

Why is syscall undeclared on Red Hat?

Maratyszcza commented 11 months ago

Also, cpuinfo doesn't support ppc64 and s390x

jmontleon commented 11 months ago

Is there a way to build pytorch without it?

Maratyszcza commented 11 months ago

I don't know, you should ask PyTorch devs

jmontleon commented 11 months ago

Did I file this in the wrong place? cpuinfo is included in the third_party deps of pytorch and this repo is within the pytorch org. I had assumed you all talk occasionally?

The only option I see available in pytorch is -DUSE_SYSTEM_PYTORCH to use a version provided by the system rather than bundled in the third party repo.

And the cmake warning when building on unsupported architectures:

CMake Warning at third_party/cpuinfo/CMakeLists.txt:81 (MESSAGE):
  Target processor architecture "s390x" is not supported in cpuinfo.  cpuinfo
  will compile, but cpuinfo_initialize() will always fail.
jmontleon commented 11 months ago

To you question, "Why is syscall undeclared on Red Hat?", I don't think this is distribution specific. As mentioned I don't have this problem on amd64 or aarch64 using the same version of clang. Rather it seems a difference in architectures.

One of the explanations I see is: "#define _GNU_SOURCE before including unistd.h or any other header as syscall(2) is not POSIX." https://stackoverflow.com/questions/42469583/problems-calling-syscall-function-in-c

It's also present in the example within the man page https://man7.org/linux/man-pages/man2/syscall.2.html#EXAMPLES

Putting this define at the top of the file does seem to resolve the build failure. I don't have a strong enough understanding at present to say why it's necessary for some architectures and not others.

bhbruce commented 7 months ago

@Maratyszcza syscall is not a standard function in posix.

It is implemented in glibc. It looks like the Clang has strict constraints (compared to GCC) ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration].

The man page https://linux.die.net/man/2/syscall also show that we should add #define _GNU_SOURCE before including headers.