Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RISC-V cpuinfo build error #4650

Open
rednoah91 opened this issue Apr 13, 2023 · 7 comments
Open

RISC-V cpuinfo build error #4650

rednoah91 opened this issue Apr 13, 2023 · 7 comments

Comments

@rednoah91
Copy link

I am trying to cross-compile xnnpack for risc-v by using scripts/build-linux-riscv64.sh with some modification of the toolchain path in cmake/riscv64.toolchain. But I got build error message when compiling cpuinfo:

/scratch/honghsu/XNNPACK/build/linux/riscv64/cpuinfo-source/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) {
                                    ^
/scratch/honghsu/XNNPACK/build/linux/riscv64/cpuinfo-source/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.

Do you have any suggestions for how to solve this issue?
(Actually in iree project I encountered the same issue but solved it by simply disabling cpuinfo.)

@Maratyszcza
Copy link
Contributor

Are you building with some non-standard libc? Usually libc provides the syscall function

@rednoah91
Copy link
Author

I am building with riscv64-unknown-linux-gnu-clang. (The toolchain can be built from the script here)

I found it can build pass by removing -std=c99.
It might be a common issue in clang. I tried building the following code by x86 clang and got the same error with -std=c99:

#include <unistd.h>
#include <sys/syscall.h>
int main(int argc, char *argv[])
{
   unsigned cpu = 0;
   if (syscall(__NR_getcpu, &cpu, NULL, NULL) != 0) {
     return 0;
   }
}

Could you tell me which risc-v toolchain you are using?

@rednoah91
Copy link
Author

@fbarchard Could you tell me which risc-v toolchain you are using? Did you encounter the same issue?

@Maratyszcza
Copy link
Contributor

We're using the GNU cross-toolchain installed with Ubuntu. See our CI config for details.

@bhbruce
Copy link
Contributor

bhbruce commented Aug 11, 2023

@Maratyszcza This error only happens in clang.
Can we add a patch to bypass SET(CMAKE_C_STANDARD 99) setting when using clang in both XNNPACK & cpuinfo?

You can also check it with prebuilt clang toolchain from https://github.com/riscv-collab/riscv-gnu-toolchain/releases/tag/2023.07.07.

@Maratyszcza
Copy link
Contributor

XNNPack already does SET(CMAKE_C_STANDARD 99). cpuinfo does it via SET_TARGET_PROPERTIES. The issue is elsewhere, probably related to pytorch/cpuinfo#171

@fbarchard
Copy link
Contributor

I ran into the same issue on Intel I think, and hacked a solution by doing the syscall in assembly.

#if XNN_ARCH_X86_64 && defined(__linux__)
ssize_t xnn_syscall(size_t rax, size_t rdi, size_t rsi, size_t rdx) {
  __asm (
    "syscall"
    : "+a" (rax)
    : "D"(rdi), "S"(rsi), "d"(rdx)
    : "rcx", "r11", "memory"
  );
  return rax;
}
#endif

which is not the right solution but it works for x64.
The syscall breaks chromium sandbox though, so I'm looking for a solution that doesnt require a syscall at all.
And for x86, the same issue exists on other OS's... syscall is only for linux.

We use clang 16 or 17 at the moment and syscall will compile in c++ but not c99.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants