Skip to content

Conversation

@clementperon
Copy link
Contributor

@clementperon clementperon commented Jun 20, 2024

RISCV glibc has decided to use 64bit time_t from get go unlike other 32bit architecture therefore aliasing __NR_futex to __NR_futex_time64 helps avoid the below errors on rv32

error: '__NR_futex' was not declared in this scope
| 21 | return (syscall (__NR_futex, uaddr, op, val, timeout, uaddr2, val3));
| |

Issue linked to NobuoTsukamoto/meta-onnxruntime#3

@m3bm3b

@m3bm3b
Copy link
Collaborator

m3bm3b commented Jun 20, 2024

Hi, thanks for the change.

Two things:

  • If we're going to fix things for 64-bit time_t on Linux on 32-bit architectures, it might be good to do it also for builds that can use 64-bit time when _TIME_BITS==64.
  • After a few changes involving executable statements inside #if/#endif blocks, code can become unreadable, and test coverage less obvious.

Does the following change work for you? If so, could you use that instead?
I have tried it on x86_64, and on x86_32 with _TIME_BITS==64 and without.

diff --git a/platform/linux/src/nsync_semaphore_futex.c b/platform/linux/src/nsync_semaphore_futex.c
index f54d1c8..b34d1a1 100644
--- a/platform/linux/src/nsync_semaphore_futex.c
+++ b/platform/linux/src/nsync_semaphore_futex.c
@@ -16,9 +16,19 @@

NSYNC_CPP_START_

+/* Linux uses 64-bit time when _TIME_BITS==64; only __NR_futex_time64 is

  • defined for some CPU types. */
    +#define NSYNC_NR_FUTEX __NR_futex
    +#if defined(__NR_futex_time64) && (defined(_TIME_BITS) || !defined(__NR_futex))
    +#if _TIME_BITS == 64 || !defined(__NR_futex)
    +#undef NSYNC_NR_FUTEX
    +#define NSYNC_NR_FUTEX __NR_futex_time64
    +#endif
    +#endif

static int futex (int *uaddr, int op, int val, const struct timespec *timeout, int *uaddr2,
int val3) {

  •   return (syscall (__NR_futex, uaddr, op, val, timeout, uaddr2, val3));
    
  •   return (syscall (NSYNC_NR_FUTEX, uaddr, op, val, timeout, uaddr2, val3));
    

}

/* Check that atomic operations on nsync_atomic_uint32_ can be applied to int. */

@m3bm3b
Copy link
Collaborator

m3bm3b commented Jun 20, 2024

Github's text processing mangled my proposed change. Here's another try:

diff --git a/platform/linux/src/nsync_semaphore_futex.c b/platform/linux/src/nsync_semaphore_futex.c
index f54d1c8..b34d1a1 100644
--- a/platform/linux/src/nsync_semaphore_futex.c
+++ b/platform/linux/src/nsync_semaphore_futex.c
@@ -16,9 +16,19 @@
 
 NSYNC_CPP_START_
 
+/* Linux uses 64-bit time when _TIME_BITS==64; only __NR_futex_time64 is
+   defined for some CPU types. */
+#define NSYNC_NR_FUTEX __NR_futex
+#if defined(__NR_futex_time64) && (defined(_TIME_BITS) || !defined(__NR_futex))
+#if _TIME_BITS == 64 || !defined(__NR_futex)
+#undef NSYNC_NR_FUTEX
+#define NSYNC_NR_FUTEX __NR_futex_time64
+#endif
+#endif
+
 static int futex (int *uaddr, int op, int val, const struct timespec *timeout, int *uaddr2,
                  int val3) {
-       return (syscall (__NR_futex, uaddr, op, val, timeout, uaddr2, val3));
+       return (syscall (NSYNC_NR_FUTEX, uaddr, op, val, timeout, uaddr2, val3));
 }
 
 /* Check that atomic operations on nsync_atomic_uint32_ can be applied to int. */
@m3bm3b
Copy link
Collaborator

m3bm3b commented Jun 21, 2024

Somehow you deleted the semicolon on the return statement (line 31).

Could you add it and perhaps confirm that the code does in fact compile for 32-bit RISC-V Linux?

Thanks.

@clementperon
Copy link
Contributor Author

@m3bm3b yes I was trying to build, I will come back as soon as its working

@m3bm3b
Copy link
Collaborator

m3bm3b commented Jun 22, 2024

I managed to test the commit (with the semicolon added) under user-mode qemu-riscv32,
using a riscv32 gcc cross-compiler toolchain.
I also verified that that without the commit, the RISCV32 build fails in the way we expect.

(I tried to boot Linux using qemu-system-riscv32, but there seem to be many rough edges
in the instructions I could find, so I contented myself with user-mode qemu.)

RISCV glibc has decided to use 64bit time_t from get go unlike
other 32bit architecture therefore aliasing __NR_futex to
__NR_futex_time64 helps avoid the below errors on rv32

error: '__NR_futex' was not declared in this scope
|    21 |         return (syscall (__NR_futex, uaddr, op, val, timeout, uaddr2, val3));
|       |
Copy link
Collaborator

@m3bm3b m3bm3b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks.

@m3bm3b m3bm3b merged commit 6948c2f into google:master Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants