public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] linux: Allow avoiding va_list for generic syscall and use it for LoongArch
@ 2023-03-24  7:27 Xi Ruoyao
  2023-03-24  9:47 ` Florian Weimer
  0 siblings, 1 reply; 4+ messages in thread
From: Xi Ruoyao @ 2023-03-24  7:27 UTC (permalink / raw)
  To: libc-alpha
  Cc: caiyinyu, Wang Xuerui, Adhemerval Zanella Netto, Andreas Schwab,
	Xi Ruoyao

Currently GCC generates highly sub-optimal code on architectures where
the calling convention prefers registers for arugment passing.  This is
GCC PR100955.  While it's technically a missed-optimization in GCC, it
seems not trivial to fix (I've not seen any compiler which can optimize
this properly yet).

As the generic Linux syscall actually uses a fixed number of arguments,
we can avoid va_list if possible and make the compiler do right thing.

Add a macro __ASSUME_SYSCALL_NAMED_WORKS which should be defined if:

* The generic Linux syscall implementation will be used.
* The calling convention has at least 7 GARs.
* The kernel ABI uses registers for syscall numbers and arguments.

LoongArch is benefited from this (saving about 430 CPU cycles per
syscall, though I won't call it a significant improvement because
syscall is "slow" in nature).  And in the future we may switch more
ports to use the generic syscall without a performance regression,
reducing the number of target-specific syscall.{c,S} files we need to
maintain.
---
 .../sysv/linux/loongarch/kernel-features.h    | 25 +++++++++++++
 sysdeps/unix/sysv/linux/syscall.c             | 35 ++++++++++++++-----
 2 files changed, 52 insertions(+), 8 deletions(-)
 create mode 100644 sysdeps/unix/sysv/linux/loongarch/kernel-features.h

diff --git a/sysdeps/unix/sysv/linux/loongarch/kernel-features.h b/sysdeps/unix/sysv/linux/loongarch/kernel-features.h
new file mode 100644
index 0000000000..4a1c115831
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/loongarch/kernel-features.h
@@ -0,0 +1,25 @@
+/* Set flags signalling availability of kernel features based on given
+   kernel version number.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library.  If not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include_next <kernel-features.h>
+
+/* Define this if the calling convention for passing 7 named arguments is
+   same as passing one named argument and 6 variable arguments, and the
+   kernel ABI uses registers for syscall number and arguments.  */
+#define __ASSUME_SYSCALL_NAMED_WORKS	1
diff --git a/sysdeps/unix/sysv/linux/syscall.c b/sysdeps/unix/sysv/linux/syscall.c
index a5a2843b73..ed5ad5afd5 100644
--- a/sysdeps/unix/sysv/linux/syscall.c
+++ b/sysdeps/unix/sysv/linux/syscall.c
@@ -16,9 +16,33 @@
    License along with the GNU C Library.  If not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <stdarg.h>
 #include <sysdep.h>
 
+#ifndef __ASSUME_SYSCALL_NAMED_WORKS
+#include <stdarg.h>
+#endif
+
+static inline long int
+__syscall (long int number, long int a0, long int a1, long int a2, long int a3,
+	   long int a4, long int a5)
+{
+  long int r = INTERNAL_SYSCALL_NCS_CALL (number, a0, a1, a2, a3, a4, a5);
+  if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (r)))
+    {
+      __set_errno (-r);
+      return -1;
+    }
+  return r;
+}
+
+#ifdef __ASSUME_SYSCALL_NAMED_WORKS
+long int
+syscall (long int number, long int a0, long int a1, long int a2, long int a3,
+	 long int a4, long int a5)
+{
+  return __syscall (number, a0, a1, a2, a3, a4, a5);
+}
+#else
 long int
 syscall (long int number, ...)
 {
@@ -33,11 +57,6 @@ syscall (long int number, ...)
   long int a5 = va_arg (args, long int);
   va_end (args);
 
-  long int r = INTERNAL_SYSCALL_NCS_CALL (number, a0, a1, a2, a3, a4, a5);
-  if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (r)))
-    {
-      __set_errno (-r);
-      return -1;
-    }
-  return r;
+  return __syscall (number, a0, a1, a2, a3, a4, a5);
 }
+#endif
-- 
2.39.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] linux: Allow avoiding va_list for generic syscall and use it for LoongArch
  2023-03-24  7:27 [PATCH] linux: Allow avoiding va_list for generic syscall and use it for LoongArch Xi Ruoyao
@ 2023-03-24  9:47 ` Florian Weimer
  2023-03-24 10:36   ` Xi Ruoyao
  0 siblings, 1 reply; 4+ messages in thread
From: Florian Weimer @ 2023-03-24  9:47 UTC (permalink / raw)
  To: Xi Ruoyao via Libc-alpha
  Cc: Xi Ruoyao, caiyinyu, Wang Xuerui, Adhemerval Zanella Netto,
	Andreas Schwab

* Xi Ruoyao via Libc-alpha:

> Currently GCC generates highly sub-optimal code on architectures where
> the calling convention prefers registers for arugment passing.  This is

Typo: ar[gu]ment

> LoongArch is benefited from this (saving about 430 CPU cycles per
> syscall, though I won't call it a significant improvement because
> syscall is "slow" in nature).  And in the future we may switch more
> ports to use the generic syscall without a performance regression,
> reducing the number of target-specific syscall.{c,S} files we need to
> maintain.

Does this impact the open* and fcntl* wrappers as well?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] linux: Allow avoiding va_list for generic syscall and use it for LoongArch
  2023-03-24  9:47 ` Florian Weimer
@ 2023-03-24 10:36   ` Xi Ruoyao
  2023-03-24 10:45     ` Florian Weimer
  0 siblings, 1 reply; 4+ messages in thread
From: Xi Ruoyao @ 2023-03-24 10:36 UTC (permalink / raw)
  To: Florian Weimer, Xi Ruoyao via Libc-alpha
  Cc: caiyinyu, Wang Xuerui, Adhemerval Zanella Netto, Andreas Schwab

On Fri, 2023-03-24 at 10:47 +0100, Florian Weimer wrote:
> * Xi Ruoyao via Libc-alpha:
> 
> > Currently GCC generates highly sub-optimal code on architectures
> > where
> > the calling convention prefers registers for arugment passing.  This
> > is
> 
> Typo: ar[gu]ment
> 
> > LoongArch is benefited from this (saving about 430 CPU cycles per
> > syscall, though I won't call it a significant improvement because
> > syscall is "slow" in nature).  And in the future we may switch more
> > ports to use the generic syscall without a performance regression,
> > reducing the number of target-specific syscall.{c,S} files we need
> > to
> > maintain.
> 
> Does this impact the open* and fcntl* wrappers as well?

Yes, on LoongArch all GARs are saved :(.  I'll see if it's possible to
avoid using va_list for them too.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] linux: Allow avoiding va_list for generic syscall and use it for LoongArch
  2023-03-24 10:36   ` Xi Ruoyao
@ 2023-03-24 10:45     ` Florian Weimer
  0 siblings, 0 replies; 4+ messages in thread
From: Florian Weimer @ 2023-03-24 10:45 UTC (permalink / raw)
  To: Xi Ruoyao
  Cc: Xi Ruoyao via Libc-alpha, caiyinyu, Wang Xuerui,
	Adhemerval Zanella Netto, Andreas Schwab

* Xi Ruoyao:

> On Fri, 2023-03-24 at 10:47 +0100, Florian Weimer wrote:
>> * Xi Ruoyao via Libc-alpha:
>> 
>> > Currently GCC generates highly sub-optimal code on architectures
>> > where
>> > the calling convention prefers registers for arugment passing.  This
>> > is
>> 
>> Typo: ar[gu]ment
>> 
>> > LoongArch is benefited from this (saving about 430 CPU cycles per
>> > syscall, though I won't call it a significant improvement because
>> > syscall is "slow" in nature).  And in the future we may switch more
>> > ports to use the generic syscall without a performance regression,
>> > reducing the number of target-specific syscall.{c,S} files we need
>> > to
>> > maintain.
>> 
>> Does this impact the open* and fcntl* wrappers as well?
>
> Yes, on LoongArch all GARs are saved :(.  I'll see if it's possible to
> avoid using va_list for them too.

If we had overread the argument list in open*, that certainly would
have avoided all the grief we had with O_TMPFILE (where the original
glibc implementation did not pass the mode argument to the kernel).  I
think fcntl* already overreads the argument list, so it should be
safe.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-03-24 10:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-24  7:27 [PATCH] linux: Allow avoiding va_list for generic syscall and use it for LoongArch Xi Ruoyao
2023-03-24  9:47 ` Florian Weimer
2023-03-24 10:36   ` Xi Ruoyao
2023-03-24 10:45     ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).