Skip to content

Commit

Permalink
Experiment Remove musl/libc dependency from RTS (#4577)
Browse files Browse the repository at this point in the history
* Remove MUSL/LIBC dependency from RTS
* Update benchmark result
  • Loading branch information
luc-blaeser authored Jun 25, 2024
1 parent 09c06e6 commit d9cc976
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 77 deletions.
78 changes: 5 additions & 73 deletions rts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,7 @@ TOMMATHFILES = \
s_mp_mul_digs_fast s_mp_mul_digs mp_init_multi mp_clear_multi mp_mul_2 mp_div_2 mp_div_3 mp_lshd mp_incr \
mp_decr mp_add_d mp_sub_d

MUSLFILES = \
pow_data exp_data log_data \
floor scalbn frexp strlen strnlen memcpy memset memchr memcmp \
__math_oflow __math_uflow __math_xflow __math_divzero __math_invalid \
__rem_pio2 __rem_pio2_large __sin __cos __tan \
stubs

TOMMATHSRC ?= $(CURDIR)/../../libtommath
MUSLSRC ?= $(CURDIR)/../../wasi-libc/libc-top-half/musl
MUSL_WASI_SYSROOT ?= $(MUSLSRC)/../../sysroot

#
# Various libtommath flags, in particular telling it to use our own memory
Expand All @@ -52,49 +43,6 @@ TOMMATH_FLAGS = \
# Note: the above __STDC_IEC_559__ define is somewhat of a misnomer
# as only IEEE 754 features are used.

#
# Various musl flags, in particular telling it to not have long doubles
# and exclude <errno.h>, which pulls in too many dependencies
#
# Note: we use a bit of magic to get rid of invocations to __fwritex (and similar)
# - the headers contain a declaration, we rename it to (__fwritex ## __COUNTER__)
# - similarly the invocation becomes __fwritex_2(...) which we inline immediately
# Be aware that upon bumps of the musl sources the number of occurrences may jump a bit
# and will need tweaks/additions below.
# Similarly we define include guards (to suppress certain headers), but those should be
# pretty stable.
# TODO: run `wasm2wat --enable-memory64 mo-rts.wasm | grep -F '(import' | grep __fwritex_` expecting empty.
#
# See also https://stackoverflow.com/questions/1597007/creating-c-macro-with-and-line-token-concatenation-with-positioning-macr

# libc does not yet have a 64-bit port, using 32-bit version instead.
MUSL_FLAGS = \
-isystem $(MUSLSRC)/arch/wasm32 \
-isystem $(MUSLSRC)/src/include \
-isystem $(MUSLSRC)/src/internal \
-isystem $(MUSL_WASI_SYSROOT)/include \
-I $(MUSLSRC)/../headers/private \
-I $(MUSLSRC)/src/include \
-D_ERRNO_H -DEOVERFLOW=75 -DEINVAL=22 \
-Derrno='(*({ static int bla = 0; &bla; }))' \
-DNL_ARGMAX=9 \
-D'TOKENPASTE0(x, y)=x \#\# y' \
-D'TOKENPASTE(x, y)=TOKENPASTE0(x, y)' \
-D'__fwritex=TOKENPASTE(__fwritex_,__COUNTER__)' \
-D'__fwritex_2(s, l, f)=(f->write((f), (s), (l)))' \
-D'__towrite=TOKENPASTE(__towrite_,__COUNTER__)' \
-D'__towrite_3(f)=(0)' \
-D__wasilibc_printscan_no_long_double \
-D__wasilibc_printscan_full_support_option='""' \
-D__wasi__ \
-D__NEED_va_list \
-D__NEED_off_t \
-D__NEED_locale_t \
-Dsqrt=__builtin_sqrt \
-Dfabs=__builtin_fabs \
-mbulk-memory \
-DBULK_MEMORY_THRESHOLD=32

#
# clang flags
#
Expand Down Expand Up @@ -132,10 +80,10 @@ RTS_EMSCRIPTEN_DEBUG=$(RTS_EMSCRIPTEN)
RTS_EMSCRIPTEN_RELEASE=$(RTS_EMSCRIPTEN) -O2

#
# Let make automatically search these directorys (tommath and musl) for .c files
# Let make automatically search this directorys (tommath) for .c files
#

vpath %.c $(MUSLSRC)/src/math $(MUSLSRC)/src/stdio $(MUSLSRC)/src/string $(MUSLSRC)/src/ctype $(TOMMATHSRC)
vpath %.c $(TOMMATHSRC)

#
# Building the libtommath files
Expand All @@ -151,23 +99,6 @@ $(TOMMATH_WASM_A): $(TOMMATH_WASM_O)
llvm-ar rcs $@ $^
llvm-ranlib $@

#
# Building the musl files
#

MUSL_WASM_O=$(MUSLFILES:%=_build/wasm/musl_%.o)
MUSL_WASM_A=_build/libmusl.a

_build/wasm/musl_%.bc: %.c | _build/wasm
$(WASM_CLANG) $(CLANG_FLAGS) $(MUSL_FLAGS) -emit-llvm $< --output $@

_build/wasm/musl_%.o: _build/wasm/musl_%.bc
$(RTS_EMSCRIPTEN_RELEASE) -o $@ $<

$(MUSL_WASM_A): $(MUSL_WASM_O)
llvm-ar rcs $@ $^
llvm-ranlib $@

#
# The rust code code of the RTS
#
Expand Down Expand Up @@ -323,9 +254,10 @@ test: $(TEST_DEPENDENCIES)
# Putting it all together
#

# These symbols from musl are used by the code generator directly
# These symbols are used by the code generator directly
EXPORTED_SYMBOLS=\
__wasm_call_ctors \
memcpy \
memcmp \
tan \
asin \
Expand All @@ -339,7 +271,7 @@ EXPORTED_SYMBOLS=\
fmod \
log \

WASM_A_DEPENDENCIES=$(TOMMATH_WASM_A) $(MUSL_WASM_A)
WASM_A_DEPENDENCIES=$(TOMMATH_WASM_A)
LINKER_OPTIONS=\
-mwasm64 --import-memory --shared --no-entry --gc-sections \
$(EXPORTED_SYMBOLS:%=--export=%) \
Expand Down
1 change: 0 additions & 1 deletion rts/motoko-rts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ ic = []
memory_check = []

[dependencies]
# libc = { version = "0.2.139", default_features = false }
motoko-rts-macros = { path = "../motoko-rts-macros" }

[profile.dev]
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ module RTS = struct
E.add_func_import env "rts" "set_static_variable" [I64Type; I64Type] [];
E.add_func_import env "rts" "set_upgrade_instructions" [I64Type] [];
E.add_func_import env "rts" "get_upgrade_instructions" [] [I64Type];
E.add_func_import env "rts" "memcpy" [I64Type; I64Type; I64Type] [I64Type]; (* standard libc memcpy *)
E.add_func_import env "rts" "memcmp" [I64Type; I64Type; I64Type] [I32Type];
E.add_func_import env "rts" "version" [] [I64Type];
E.add_func_import env "rts" "parse_idl_header" [I32Type; I64Type; I64Type; I64Type; I64Type] [];
Expand Down Expand Up @@ -1367,7 +1368,7 @@ module Heap = struct

(* Convenience functions related to memory *)
(* Copying bytes (works on unskewed memory addresses) *)
let memcpy env = G.i MemoryCopy
let memcpy env = E.call_import env "rts" "memcpy" ^^ G.i Drop
(* Comparing bytes (works on unskewed memory addresses) *)
let memcmp env = E.call_import env "rts" "memcmp" ^^ G.i (Convert (Wasm_exts.Values.I64 I64Op.ExtendUI32))

Expand Down
2 changes: 1 addition & 1 deletion test/bench/ok/bignum.drun-run.ok
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a000000000000000001
ingress Completed: Reply: 0x4449444c0000
debug.print: {cycles = 3_093_961; size = +25_528}
ingress Completed: Reply: 0x4449444c0000
debug.print: {cycles = 51_910_521; size = +830_464}
debug.print: {cycles = 51_915_248; size = +830_464}
ingress Completed: Reply: 0x4449444c0000
2 changes: 1 addition & 1 deletion test/bench/ok/candid-subtype-cost.drun-run.ok
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101
ingress Completed: Reply: 0x4449444c0000
debug.print: {cycles = 1_102_518; heap_bytes = +33_872}
debug.print: {cycles = 1_030_461; heap_bytes = +33_872}
ingress Completed: Reply: 0x4449444c0000

0 comments on commit d9cc976

Please sign in to comment.