From 1729a8259c9835652a633bf240ccfe39d01f95cb Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Fri, 9 Sep 2022 08:59:59 -0700 Subject: [PATCH] Introduce `MODE=fastbuild` for 28% speedup --- build/config.mk | 116 ++++++++++++++++------------------ libc/intrin/_spinlock_yield.S | 24 ------- libc/intrin/getpid.c | 5 +- libc/intrin/kclocknames.S | 6 +- libc/intrin/kdos2errno.S | 1 + libc/intrin/kerrnodocs.S | 4 +- libc/intrin/kerrnonames.S | 4 +- libc/intrin/kipoptnames.S | 4 +- libc/intrin/kopenflags.S | 4 +- libc/intrin/krlimitnames.S | 6 +- libc/intrin/ksignalnames.S | 6 +- libc/intrin/ksockoptnames.S | 6 +- libc/intrin/ktcpoptnames.S | 4 +- libc/intrin/pthread.h | 3 +- 14 files changed, 82 insertions(+), 111 deletions(-) delete mode 100644 libc/intrin/_spinlock_yield.S diff --git a/build/config.mk b/build/config.mk index 6bf23fb91..bf8c73e2d 100644 --- a/build/config.mk +++ b/build/config.mk @@ -4,20 +4,35 @@ # Default Mode # # - `make` +# - Optimized # - Backtraces +# - Debuggable # - Syscall tracing # - Function tracing # - Reasonably small -# - Reasonably optimized -# - Reasonably debuggable +# ifeq ($(MODE),) -CONFIG_CCFLAGS += \ - $(BACKTRACES) \ - $(FTRACE) \ - -DSYSDEBUG \ - -O2 -TARGET_ARCH ?= \ - -msse3 +CONFIG_CCFLAGS += $(BACKTRACES) $(FTRACE) -O2 +CONFIG_CPPFLAGS += -DSYSDEBUG +TARGET_ARCH ?= -msse3 +endif + +# Fast Build Mode +# +# - `make MODE=fastbuild` +# - No debugging +# - Syscall tracing +# - Function tracing +# - Some optimizations +# - Limited Backtraces +# - Compiles 28% faster +# +ifeq ($(MODE),fastbuild) +CONFIG_CCFLAGS += $(BACKTRACES) $(FTRACE) -O +CONFIG_CPPFLAGS += -DSYSDEBUG +CONFIG_OFLAGS += -g0 +CONFIG_LDFLAGS += -S +TARGET_ARCH ?= -msse3 endif # Optimized Mode @@ -31,19 +46,11 @@ endif # - No memory corruption detection # - assert() / CHECK_xx() may leak code into binary for debuggability # - GCC 8+ hoists check fails into .text.cold, thus minimizing impact +# ifeq ($(MODE), opt) -CONFIG_CPPFLAGS += \ - -DNDEBUG \ - -msse2avx \ - -Wa,-msse2avx -CONFIG_CCFLAGS += \ - $(BACKTRACES) \ - $(FTRACE) \ - -DSYSDEBUG \ - -O3 \ - -fmerge-all-constants -TARGET_ARCH ?= \ - -march=native +CONFIG_CPPFLAGS += -DNDEBUG -DSYSDEBUG -msse2avx -Wa,-msse2avx +CONFIG_CCFLAGS += $(BACKTRACES) $(FTRACE) -O3 -fmerge-all-constants +TARGET_ARCH ?= -march=native endif # Optimized Linux Mode @@ -54,18 +61,12 @@ endif # - Turns off function tracing # - Turns off support for older cpu models # - Turns off support for other operating systems +# ifeq ($(MODE), optlinux) -CONFIG_CPPFLAGS += \ - -DNDEBUG \ - -msse2avx \ - -Wa,-msse2avx \ - -DSUPPORT_VECTOR=1 -CONFIG_CCFLAGS += \ - -O3 -fmerge-all-constants -DEFAULT_COPTS += \ - -mred-zone -TARGET_ARCH ?= \ - -march=native +CONFIG_CPPFLAGS += -DNDEBUG -msse2avx -Wa,-msse2avx -DSUPPORT_VECTOR=1 +CONFIG_CCFLAGS += -O3 -fmerge-all-constants +DEFAULT_COPTS += -mred-zone +TARGET_ARCH ?= -march=native endif # Release Mode @@ -81,16 +82,12 @@ endif # - DCHECK_xx() statements removed # - No memory corruption detection # - CHECK_xx() won't leak strings into binary +# ifeq ($(MODE), rel) -CONFIG_CPPFLAGS += \ - -DNDEBUG -CONFIG_CCFLAGS += \ - $(BACKTRACES) \ - -O2 -TARGET_ARCH ?= \ - -msse3 -PYFLAGS += \ - -O1 +CONFIG_CPPFLAGS += -DNDEBUG +CONFIG_CCFLAGS += $(BACKTRACES) -O2 +TARGET_ARCH ?= -msse3 +PYFLAGS += -O1 endif # Asan Mode @@ -103,14 +100,11 @@ endif # - Backtraces # - Debuggability # - Larger binaries +# ifeq ($(MODE), asan) -CONFIG_CCFLAGS += \ - $(BACKTRACES) \ - -O2 -CONFIG_COPTS += \ - -fsanitize=address -TARGET_ARCH ?= \ - -msse3 +CONFIG_CCFLAGS += $(BACKTRACES) -O2 +CONFIG_COPTS += -fsanitize=address +TARGET_ARCH ?= -msse3 endif # Debug Mode @@ -122,22 +116,13 @@ endif # - Stack canaries # - No optimization (TODO) # - Enormous binaries +# ifeq ($(MODE), dbg) -CONFIG_CPPFLAGS += \ - -DMODE_DBG -CONFIG_CCFLAGS += \ - $(BACKTRACES) \ - $(FTRACE) \ - -DSYSDEBUG \ - -O2 \ - -fno-inline -CONFIG_COPTS += \ - -fsanitize=address \ - -fsanitize=undefined -TARGET_ARCH ?= \ - -msse3 -OVERRIDE_CCFLAGS += \ - -fno-pie +CONFIG_CPPFLAGS += -DMODE_DBG +CONFIG_CCFLAGS += $(BACKTRACES) $(FTRACE) -DSYSDEBUG -O -fno-inline +CONFIG_COPTS += -fsanitize=address -fsanitize=undefined +TARGET_ARCH ?= -msse3 +OVERRIDE_CCFLAGS += -fno-pie endif # Tiny Mode @@ -151,6 +136,7 @@ endif # - No backtraces # - No algorithmics # - YOLO +# ifeq ($(MODE), tiny) CONFIG_CPPFLAGS += \ -DTINY \ @@ -185,6 +171,7 @@ endif # - No portability # - No algorithmics # - YOLO +# ifeq ($(MODE), tinylinux) CONFIG_CPPFLAGS += \ -DTINY \ @@ -216,6 +203,7 @@ endif # - No backtraces # - No algorithmics # - YOLO +# ifeq ($(MODE), tinylinuxbsd) CONFIG_CPPFLAGS += \ -DTINY \ @@ -246,6 +234,7 @@ endif # - No backtraces # - No algorithmics # - YOLO +# ifeq ($(MODE), tinysysv) CONFIG_CPPFLAGS += \ -DTINY \ @@ -276,6 +265,7 @@ endif # - No backtraces # - No algorithmics # - YOLO +# ifeq ($(MODE), tinynowin) CONFIG_CPPFLAGS += \ -DTINY \ diff --git a/libc/intrin/_spinlock_yield.S b/libc/intrin/_spinlock_yield.S deleted file mode 100644 index b90bccd15..000000000 --- a/libc/intrin/_spinlock_yield.S +++ /dev/null @@ -1,24 +0,0 @@ -/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ -│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2022 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/macros.internal.h" -.privileged - -_spinlock_yield: - jmp sched_yield - .endfn _spinlock_yield,globl diff --git a/libc/intrin/getpid.c b/libc/intrin/getpid.c index 5cd29b58b..1d1513bdb 100644 --- a/libc/intrin/getpid.c +++ b/libc/intrin/getpid.c @@ -27,7 +27,10 @@ * This function does not need to issue a system call. The PID is * tracked by a global variable which is updated at fork(). The only * exception is when the process is vfork()'d in which case a system - * call shall be issued. + * call shall be issued. This optimization helps make functions like + * rand64() fork-safe, however it could lead to race conditions in + * programs that mix fork() with threads. In that case, apps should + * consider using `sys_getpid().ax` instead to force a system call. * * On Linux, and only Linux, the process id is guaranteed to be the same * as gettid() for the main thread. diff --git a/libc/intrin/kclocknames.S b/libc/intrin/kclocknames.S index ab9deda44..cd3c0f120 100644 --- a/libc/intrin/kclocknames.S +++ b/libc/intrin/kclocknames.S @@ -21,13 +21,13 @@ .macro .e e s .long \e - kClockNames - .long 1f - kClockNames + .long .L\@ - kClockNames .rodata.str1.1 -1: .string "\s" +.L\@: .string "\s" .previous .endm - .section .rodata + .section .rodata,"a",@progbits .align 4 .underrun kClockNames: diff --git a/libc/intrin/kdos2errno.S b/libc/intrin/kdos2errno.S index 3bab83afd..3e472a87f 100644 --- a/libc/intrin/kdos2errno.S +++ b/libc/intrin/kdos2errno.S @@ -28,6 +28,7 @@ .endm .section .rodata,"a",@progbits + .align 4 .underrun kDos2Errno: // .e kNtErrorInvalidFunction,ENOSYS # in consts.sh diff --git a/libc/intrin/kerrnodocs.S b/libc/intrin/kerrnodocs.S index 5d0a1b25c..0920d4f9a 100644 --- a/libc/intrin/kerrnodocs.S +++ b/libc/intrin/kerrnodocs.S @@ -21,9 +21,9 @@ .macro .e e s .long \e - kErrnoDocs - .long 1f - kErrnoDocs + .long .L\@ - kErrnoDocs .rodata.str1.1 -1: .asciz "\s" +.L\@: .asciz "\s" .previous .endm diff --git a/libc/intrin/kerrnonames.S b/libc/intrin/kerrnonames.S index bbbce5c13..08a9f3073 100644 --- a/libc/intrin/kerrnonames.S +++ b/libc/intrin/kerrnonames.S @@ -21,9 +21,9 @@ .macro .e e .long \e - kErrnoNames - .long 1f - kErrnoNames + .long .L\@ - kErrnoNames .rodata.str1.1 -1: .string "\e" +.L\@: .string "\e" .previous .endm diff --git a/libc/intrin/kipoptnames.S b/libc/intrin/kipoptnames.S index cee04e63c..59290cb1a 100644 --- a/libc/intrin/kipoptnames.S +++ b/libc/intrin/kipoptnames.S @@ -21,9 +21,9 @@ .macro .e e s .long \e - kIpOptnames - .long 1f - kIpOptnames + .long .L\@ - kIpOptnames .rodata.str1.1 -1: .string "\s" +.L\@: .string "\s" .previous .endm diff --git a/libc/intrin/kopenflags.S b/libc/intrin/kopenflags.S index b2a63bee8..85efbf487 100644 --- a/libc/intrin/kopenflags.S +++ b/libc/intrin/kopenflags.S @@ -21,9 +21,9 @@ .macro .e e s .long \e - kOpenFlags - .long 1f - kOpenFlags + .long .L\@ - kOpenFlags .rodata.str1.1 -1: .string "\s" +.L\@: .string "\s" .previous .endm diff --git a/libc/intrin/krlimitnames.S b/libc/intrin/krlimitnames.S index 30874a1a3..768ae3026 100644 --- a/libc/intrin/krlimitnames.S +++ b/libc/intrin/krlimitnames.S @@ -21,14 +21,14 @@ .macro .e e s .long \e - kRlimitNames - .long 1f - kRlimitNames + .long .L\@ - kRlimitNames .rodata.str1.1 -1: .string "\s" +.L\@: .string "\s" .previous .endm .section .rodata - .align 4 + .align 4 .underrun kRlimitNames: .e RLIMIT_AS,"AS" diff --git a/libc/intrin/ksignalnames.S b/libc/intrin/ksignalnames.S index 881a08575..8336b3d5d 100644 --- a/libc/intrin/ksignalnames.S +++ b/libc/intrin/ksignalnames.S @@ -21,14 +21,14 @@ .macro .e e s .long \e - kSignalNames - .long 1f - kSignalNames + .long .L\@ - kSignalNames .rodata.str1.1 -1: .string "\s" +.L\@: .string "\s" .previous .endm .section .rodata - .align 4 + .align 4 .underrun kSignalNames: .e SIGHUP,"SIGHUP" diff --git a/libc/intrin/ksockoptnames.S b/libc/intrin/ksockoptnames.S index 8b29764a0..f25fcf212 100644 --- a/libc/intrin/ksockoptnames.S +++ b/libc/intrin/ksockoptnames.S @@ -21,14 +21,14 @@ .macro .e e s .long \e - kSockOptnames - .long 1f - kSockOptnames + .long .L\@ - kSockOptnames .rodata.str1.1 -1: .string "\s" +.L\@: .string "\s" .previous .endm .section .rodata - .align 4 + .align 4 .underrun kSockOptnames: .e SO_DEBUG,"DEBUG" # bool32 diff --git a/libc/intrin/ktcpoptnames.S b/libc/intrin/ktcpoptnames.S index 2dda61c0f..e9fb7386f 100644 --- a/libc/intrin/ktcpoptnames.S +++ b/libc/intrin/ktcpoptnames.S @@ -21,9 +21,9 @@ .macro .e e s .long \e - kTcpOptnames - .long 1f - kTcpOptnames + .long .L\@ - kTcpOptnames .rodata.str1.1 -1: .string "\s" +.L\@: .string "\s" .previous .endm diff --git a/libc/intrin/pthread.h b/libc/intrin/pthread.h index 23e4aefda..d0b81ba1d 100644 --- a/libc/intrin/pthread.h +++ b/libc/intrin/pthread.h @@ -174,7 +174,8 @@ int pthread_barrier_init(pthread_barrier_t *, const pthread_barrierattr_t *, #define pthread_spin_init(pSpin, multiprocess) ((pSpin)->lock = 0, 0) #define pthread_spin_destroy(pSpin) ((pSpin)->lock = -1, 0) -#if (__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 407 +#if (__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 407 && \ + !defined(__STRICT_ANSI__) extern const errno_t EBUSY; #define pthread_spin_lock(pSpin) \ ({ \