[v2,1/5] linux: Add __ASSUME_SYSCALL_NAMED_WORKS to allow avoiding va_list for generic syscall

Message ID 20230325140815.4170296-2-xry111@xry111.site
State Dropped
Headers
Series linux: Avoid va_list for generic syscall wrappers if possible |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Xi Ruoyao March 25, 2023, 2:08 p.m. UTC
  Currently GCC generates highly sub-optimal code on architectures where
the calling convention prefers registers for argument 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
calling convention treats (x named arguments + y variable arguments)
exactly same as (x + y) named arguments, while each argument is either
an integer of which the width is less than or equal to "long" or a
pointer; and each argument can be fetched from the same register or the
same offset from the stack pointer no matter how many (maybe zero)
arguments are passed after it.
---
 sysdeps/unix/sysv/linux/syscall.c | 35 ++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 8 deletions(-)
  

Patch

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