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.
This commit is contained in:
Justine Tunney 2024-08-26 12:29:59 -07:00
parent 111ec9a989
commit e7b586e7f8
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
6 changed files with 42 additions and 16 deletions

View file

@ -119,7 +119,7 @@ void __morph_begin(void) libcesque;
void __morph_end(void) libcesque; void __morph_end(void) libcesque;
void __jit_begin(void) libcesque; void __jit_begin(void) libcesque;
void __jit_end(void) libcesque; void __jit_end(void) libcesque;
void __clear_cache(void *, void *) libcesque; void __clear_cache(void *, void *);
/* portability */ /* portability */
bool32 IsGenuineBlink(void) libcesque; bool32 IsGenuineBlink(void) libcesque;
bool32 IsCygwin(void) libcesque; bool32 IsCygwin(void) libcesque;

View file

@ -44,9 +44,6 @@ $(LIBC_STR_A).pkg: \
$(LIBC_STR_A_OBJS) \ $(LIBC_STR_A_OBJS) \
$(foreach x,$(LIBC_STR_A_DIRECTDEPS),$($(x)_A).pkg) $(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/wmemset.o \
o/$(MODE)/libc/str/memset16.o \ o/$(MODE)/libc/str/memset16.o \
o/$(MODE)/libc/str/dosdatetimetounix.o: private \ o/$(MODE)/libc/str/dosdatetimetounix.o: private \

View file

@ -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 work as expected. Our modifications to GNU GCC are published under the
ISC license at <https://github.com/ahgamut/gcc/tree/portcosmo-14.1>. The ISC license at <https://github.com/ahgamut/gcc/tree/portcosmo-14.1>. The
binaries you see here were first published at binaries you see here were first published at
<https://github.com/ahgamut/superconfigure/releases/tag/z0.0.52> which <https://github.com/ahgamut/superconfigure/releases/tag/z0.0.53> which
is regularly updated. is regularly updated.
## Legal ## Legal

View file

@ -75,6 +75,15 @@ elif [ ! -d "$TMPDIR" ]; then
fi fi
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= X=
OPT= OPT=
ARGS= ARGS=
@ -93,6 +102,7 @@ FLAGS_AARCH64=
INPUT_FILE_COUNT=0 INPUT_FILE_COUNT=0
DEPENDENCY_OUTPUT= DEPENDENCY_OUTPUT=
NEED_DEPENDENCY_OUTPUT= NEED_DEPENDENCY_OUTPUT=
for x; do for x; do
if [ x"$x" != x"${x#* }" ]; then if [ x"$x" != x"${x#* }" ]; then
fatal_error "arguments containing spaces unsupported: $x" fatal_error "arguments containing spaces unsupported: $x"
@ -185,6 +195,16 @@ EOF
elif [ x"$x" = x"-moptlinux" ]; then elif [ x"$x" = x"-moptlinux" ]; then
MODE=optlinux MODE=optlinux
continue 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 elif [ x"$x" = x"-m64" ]; then
continue continue
elif [ x"$x" = x"-fomit-frame-pointer" ]; then elif [ x"$x" = x"-fomit-frame-pointer" ]; then
@ -298,7 +318,7 @@ fi
PLATFORM="-D__COSMOPOLITAN__ -D__COSMOCC__ -D__FATCOSMOCC__" PLATFORM="-D__COSMOPOLITAN__ -D__COSMOCC__ -D__FATCOSMOCC__"
PREDEF="-include libc/integral/normalize.inc" PREDEF="-include libc/integral/normalize.inc"
CPPFLAGS="-fno-pie -nostdinc -isystem $BIN/../include" 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" LDFLAGS="-static -nostdlib -no-pie -fuse-ld=bfd -Wl,-z,noexecstack -Wl,-z,norelro -Wl,--gc-sections"
PRECIOUS="-fno-omit-frame-pointer" 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" CFLAGS="$CFLAGS -fno-optimize-sibling-calls -mno-omit-leaf-frame-pointer"
fi fi
if [ x"$OPT" != x"-O3" ] && [ x"$MODE" != x"optlinux" ]; then if [ x"$OPT" != x"-O3" ] && [ x"$MODE" != x"optlinux" ]; then
if [ $CLANG -eq 0 ]; then
CFLAGS="$CFLAGS -fno-schedule-insns2" CFLAGS="$CFLAGS -fno-schedule-insns2"
fi
fi fi
if [ x"$X" = x"c" ] || [ x"$X" = x"c-header" ]; then if [ x"$X" = x"c" ] || [ x"$X" = x"c-header" ]; then
@ -320,11 +342,9 @@ else
CPLUSPLUS=0 CPLUSPLUS=0
fi fi
CC_X86_64="$BIN/x86_64-linux-cosmo-gcc"
CC_AARCH64="$BIN/aarch64-linux-cosmo-gcc"
if [ $CPLUSPLUS -eq 1 ]; then if [ $CPLUSPLUS -eq 1 ]; then
CC_X86_64="$BIN/x86_64-linux-cosmo-g++" CC_X86_64=$CXX_X86_64
CC_AARCH64="$BIN/aarch64-linux-cosmo-g++" CC_AARCH64=$CXX_AARCH64
if [ $INTENT != cpp ]; then if [ $INTENT != cpp ]; then
CFLAGS="$CFLAGS -fno-rtti -fno-exceptions -fuse-cxa-atexit" CFLAGS="$CFLAGS -fno-rtti -fno-exceptions -fuse-cxa-atexit"
fi fi
@ -461,6 +481,7 @@ build_object() {
( (
set -- \ set -- \
"$CC_X86_64" \ "$CC_X86_64" \
$TARGET_X86_64 \
-o"$OUTPUT_X86_64" \ -o"$OUTPUT_X86_64" \
$PLATFORM \ $PLATFORM \
$PREDEF \ $PREDEF \
@ -482,6 +503,7 @@ build_object() {
( (
set -- \ set -- \
"$CC_AARCH64" \ "$CC_AARCH64" \
$TARGET_AARCH64 \
-o"$OUTPUT_AARCH64" \ -o"$OUTPUT_AARCH64" \
$PLATFORM \ $PLATFORM \
$PREDEF \ $PREDEF \
@ -588,6 +610,7 @@ TEMP_FILES="${TEMP_FILES} $out2"
( (
set -- \ set -- \
"$CC_X86_64" \ "$CC_X86_64" \
$TARGET_X86_64 \
-o"$OUTPUT_X86_64"\ -o"$OUTPUT_X86_64"\
$CRT_X86_64 \ $CRT_X86_64 \
$LDFLAGS_X86_64 \ $LDFLAGS_X86_64 \
@ -605,6 +628,7 @@ pid1=$!
( (
set -- \ set -- \
"$CC_AARCH64" \ "$CC_AARCH64" \
$TARGET_AARCH64 \
-o"$OUTPUT_AARCH64"\ -o"$OUTPUT_AARCH64"\
$CRT_AARCH64 \ $CRT_AARCH64 \
$LDFLAGS_AARCH64 \ $LDFLAGS_AARCH64 \

View file

@ -182,12 +182,17 @@ fetch() {
OLD=$PWD OLD=$PWD
cd "$OUTDIR/" cd "$OUTDIR/"
if [ ! -x bin/x86_64-linux-cosmo-gcc ]; then if [ ! -x bin/x86_64-linux-cosmo-gcc ]; then
fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.52/aarch64-gcc.zip fetch https://github.com/ahgamut/superconfigure/releases/download/z0.0.53/aarch64-gcc.zip &
unzip 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 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 rm -f x86_64-gcc.zip
mv bin/clang-18 bin/cosmo-clang
fi fi
rm -f bin/*-cpp rm -f bin/*-cpp
rm -f bin/*-gcc-* rm -f bin/*-gcc-*