From e7b586e7f89dc6920187acdbe065704506f1757b Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Mon, 26 Aug 2024 12:29:59 -0700 Subject: [PATCH] Add preliminary support for cosmocc -mclang C++ code compiles very slowly with cosmocc, possibly because we're using LLVM LIBCXX with GCC, and LLVM doesn't work as hard to make GCC go fast. Therefore, it should be possible, to ask cosmocc to favor Clang over GCC under the hood. On llamafile, my intention's to use this to make certain files, e.g. llama.cpp/common.cpp, go from taking 17 seconds to 5 seconds This new -mclang flag isn't ready for production yet since there's still the question of how to get Clang to generate SJLJ exception code. If you use this, then it's recommended you also pass -fno-exceptions. The tradeoff is we're adding a 121mb binary to the cosmocc distribution. There are no plans as of yet to fully migrate to Clang since GCC is very good and has always treated us well. --- libc/dlopen/stubs.c | 2 +- libc/runtime/runtime.h | 2 +- libc/str/BUILD.mk | 3 --- tool/cosmocc/README.md | 2 +- tool/cosmocc/bin/cosmocc | 36 ++++++++++++++++++++++++++++++------ tool/cosmocc/package.sh | 13 +++++++++---- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/libc/dlopen/stubs.c b/libc/dlopen/stubs.c index 57c3f0724..9a94e891b 100644 --- a/libc/dlopen/stubs.c +++ b/libc/dlopen/stubs.c @@ -27,7 +27,7 @@ * * @return null always */ -void *dlopen(const char *, int) { +void *dlopen(const char *, int) { return 0; } diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index 9b26bbbf1..452125bcb 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -119,7 +119,7 @@ void __morph_begin(void) libcesque; void __morph_end(void) libcesque; void __jit_begin(void) libcesque; void __jit_end(void) libcesque; -void __clear_cache(void *, void *) libcesque; +void __clear_cache(void *, void *); /* portability */ bool32 IsGenuineBlink(void) libcesque; bool32 IsCygwin(void) libcesque; diff --git a/libc/str/BUILD.mk b/libc/str/BUILD.mk index ab0193593..d7a655dea 100644 --- a/libc/str/BUILD.mk +++ b/libc/str/BUILD.mk @@ -44,9 +44,6 @@ $(LIBC_STR_A).pkg: \ $(LIBC_STR_A_OBJS) \ $(foreach x,$(LIBC_STR_A_DIRECTDEPS),$($(x)_A).pkg) -o/$(MODE)/libc/str/wow.o: private \ - CC = gcc - o/$(MODE)/libc/str/wmemset.o \ o/$(MODE)/libc/str/memset16.o \ o/$(MODE)/libc/str/dosdatetimetounix.o: private \ diff --git a/tool/cosmocc/README.md b/tool/cosmocc/README.md index 5edf76573..532cdcf83 100644 --- a/tool/cosmocc/README.md +++ b/tool/cosmocc/README.md @@ -417,7 +417,7 @@ statements instead, so that Cosmopolitan Libc's system constants will work as expected. Our modifications to GNU GCC are published under the ISC license at . The binaries you see here were first published at - which + which is regularly updated. ## Legal diff --git a/tool/cosmocc/bin/cosmocc b/tool/cosmocc/bin/cosmocc index 1cbb13ff1..b07dc1be5 100755 --- a/tool/cosmocc/bin/cosmocc +++ b/tool/cosmocc/bin/cosmocc @@ -75,6 +75,15 @@ elif [ ! -d "$TMPDIR" ]; then fi fi +CLANG=0 +CC_X86_64="$BIN/x86_64-linux-cosmo-gcc" +CC_AARCH64="$BIN/aarch64-linux-cosmo-gcc" +CXX_X86_64="$BIN/x86_64-linux-cosmo-g++" +CXX_AARCH64="$BIN/aarch64-linux-cosmo-g++" +FPORTCOSMO="-fportcosmo" +TARGET_X86_64= +TARGET_AARCH64= + X= OPT= ARGS= @@ -93,6 +102,7 @@ FLAGS_AARCH64= INPUT_FILE_COUNT=0 DEPENDENCY_OUTPUT= NEED_DEPENDENCY_OUTPUT= + for x; do if [ x"$x" != x"${x#* }" ]; then fatal_error "arguments containing spaces unsupported: $x" @@ -185,6 +195,16 @@ EOF elif [ x"$x" = x"-moptlinux" ]; then MODE=optlinux continue + elif [ x"$x" = x"-mclang" ]; then + CLANG=1 + CC_X86_64="$BIN/cosmo-clang" + CC_AARCH64="$BIN/cosmo-clang" + CXX_X86_64="$BIN/cosmo-clang++" + CXX_AARCH64="$BIN/cosmo-clang++" + TARGET_X86_64="--target=x86_64" + TARGET_AARCH64="--target=aarch64" + FPORTCOSMO= + continue elif [ x"$x" = x"-m64" ]; then continue elif [ x"$x" = x"-fomit-frame-pointer" ]; then @@ -298,7 +318,7 @@ fi PLATFORM="-D__COSMOPOLITAN__ -D__COSMOCC__ -D__FATCOSMOCC__" PREDEF="-include libc/integral/normalize.inc" CPPFLAGS="-fno-pie -nostdinc -isystem $BIN/../include" -CFLAGS="-fportcosmo -fno-dwarf2-cfi-asm -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-semantic-interposition" +CFLAGS="$FPORTCOSMO -fno-dwarf2-cfi-asm -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-semantic-interposition" LDFLAGS="-static -nostdlib -no-pie -fuse-ld=bfd -Wl,-z,noexecstack -Wl,-z,norelro -Wl,--gc-sections" PRECIOUS="-fno-omit-frame-pointer" @@ -307,7 +327,9 @@ if [ x"$OPT" != x"-Os" ] && [ x"$MODE" != x"tiny" ] && [ x"$MODE" != x"optlinux" CFLAGS="$CFLAGS -fno-optimize-sibling-calls -mno-omit-leaf-frame-pointer" fi if [ x"$OPT" != x"-O3" ] && [ x"$MODE" != x"optlinux" ]; then - CFLAGS="$CFLAGS -fno-schedule-insns2" + if [ $CLANG -eq 0 ]; then + CFLAGS="$CFLAGS -fno-schedule-insns2" + fi fi if [ x"$X" = x"c" ] || [ x"$X" = x"c-header" ]; then @@ -320,11 +342,9 @@ else CPLUSPLUS=0 fi -CC_X86_64="$BIN/x86_64-linux-cosmo-gcc" -CC_AARCH64="$BIN/aarch64-linux-cosmo-gcc" if [ $CPLUSPLUS -eq 1 ]; then - CC_X86_64="$BIN/x86_64-linux-cosmo-g++" - CC_AARCH64="$BIN/aarch64-linux-cosmo-g++" + CC_X86_64=$CXX_X86_64 + CC_AARCH64=$CXX_AARCH64 if [ $INTENT != cpp ]; then CFLAGS="$CFLAGS -fno-rtti -fno-exceptions -fuse-cxa-atexit" fi @@ -461,6 +481,7 @@ build_object() { ( set -- \ "$CC_X86_64" \ + $TARGET_X86_64 \ -o"$OUTPUT_X86_64" \ $PLATFORM \ $PREDEF \ @@ -482,6 +503,7 @@ build_object() { ( set -- \ "$CC_AARCH64" \ + $TARGET_AARCH64 \ -o"$OUTPUT_AARCH64" \ $PLATFORM \ $PREDEF \ @@ -588,6 +610,7 @@ TEMP_FILES="${TEMP_FILES} $out2" ( set -- \ "$CC_X86_64" \ + $TARGET_X86_64 \ -o"$OUTPUT_X86_64"\ $CRT_X86_64 \ $LDFLAGS_X86_64 \ @@ -605,6 +628,7 @@ pid1=$! ( set -- \ "$CC_AARCH64" \ + $TARGET_AARCH64 \ -o"$OUTPUT_AARCH64"\ $CRT_AARCH64 \ $LDFLAGS_AARCH64 \ diff --git a/tool/cosmocc/package.sh b/tool/cosmocc/package.sh index 17635f8de..207b3ca7a 100755 --- a/tool/cosmocc/package.sh +++ b/tool/cosmocc/package.sh @@ -182,12 +182,17 @@ fetch() { OLD=$PWD cd "$OUTDIR/" if [ ! -x bin/x86_64-linux-cosmo-gcc ]; then - fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.52/aarch64-gcc.zip - unzip aarch64-gcc.zip + fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.53/aarch64-gcc.zip & + fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.53/x86_64-gcc.zip & + fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.53/llvm.zip & + wait + unzip aarch64-gcc.zip & + unzip x86_64-gcc.zip & + unzip llvm.zip bin/clang-18 & + wait rm -f aarch64-gcc.zip - fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.52/x86_64-gcc.zip - unzip x86_64-gcc.zip rm -f x86_64-gcc.zip + mv bin/clang-18 bin/cosmo-clang fi rm -f bin/*-cpp rm -f bin/*-gcc-*