XNNPACK icon indicating copy to clipboard operation
XNNPACK copied to clipboard

RISC-V cpuinfo build error

Open rednoah91 opened this issue 2 years ago • 7 comments

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.)

rednoah91 avatar Apr 13 '23 10:04 rednoah91

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

Maratyszcza avatar Apr 13 '23 12:04 Maratyszcza

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 avatar Apr 14 '23 16:04 rednoah91

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

rednoah91 avatar Apr 18 '23 03:04 rednoah91

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

Maratyszcza avatar Jun 01 '23 08:06 Maratyszcza

@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.

bhbruce avatar Aug 11 '23 08:08 bhbruce

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

Maratyszcza avatar Aug 11 '23 22:08 Maratyszcza

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.

fbarchard avatar Mar 14 '24 02:03 fbarchard