public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: libc-alpha@sourceware.org
Cc: caiyinyu <caiyinyu@loongson.cn>, Wang Xuerui <i@xen0n.name>,
	Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>,
	Andreas Schwab <schwab@suse.de>, Xi Ruoyao <xry111@xry111.site>
Subject: [PATCH] linux: Allow avoiding va_list for generic syscall and use it for LoongArch
Date: Fri, 24 Mar 2023 15:27:45 +0800	[thread overview]
Message-ID: <20230324072745.4138-1-xry111@xry111.site> (raw)

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


             reply	other threads:[~2023-03-24  7:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-24  7:27 Xi Ruoyao [this message]
2023-03-24  9:47 ` Florian Weimer
2023-03-24 10:36   ` Xi Ruoyao
2023-03-24 10:45     ` Florian Weimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230324072745.4138-1-xry111@xry111.site \
    --to=xry111@xry111.site \
    --cc=adhemerval.zanella@linaro.org \
    --cc=caiyinyu@loongson.cn \
    --cc=i@xen0n.name \
    --cc=libc-alpha@sourceware.org \
    --cc=schwab@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).