Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Custom Rust Targets for Shared Wasm Libraries #4683

Merged
merged 17 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/niv-updater-rare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: knl/niv-updater-action@v13
with:
# might be too noisy
whitelist: 'ic-ref,musl-wasi,ic-wasm'
whitelist: 'ic-ref,ic-wasm'
labels: |
automerge-squash
keep_updating: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/niv-updater-trial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: niv-updater-action
uses: knl/niv-updater-action@v13
with:
whitelist: 'nixpkgs,musl-wasi,ic'
whitelist: 'nixpkgs,ic'
labels: |
autoclose
keep_updating: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/niv-updater.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: knl/niv-updater-action@v13
with:
# might be too noisy
blacklist: 'nixpkgs,ic-ref,musl-wasi,ic'
blacklist: 'nixpkgs,ic-ref,ic'
labels: |
automerge-squash
keep_updating: true
Expand Down
13 changes: 0 additions & 13 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,6 @@ let ocaml_exe = name: bin: rts:
$out/bin/* --help >/dev/null
'';
};

musl-wasi-sysroot = stdenv.mkDerivation {
name = "musl-wasi-sysroot";
src = nixpkgs.sources.musl-wasi;
phases = [ "unpackPhase" "installPhase" ];
installPhase = ''
make SYSROOT="$out" include_dirs
'';
};
in

rec {
Expand Down Expand Up @@ -295,8 +286,6 @@ rec {

${llvmEnv}
export TOMMATHSRC=${nixpkgs.sources.libtommath}
export MUSLSRC=${nixpkgs.sources.musl-wasi}/libc-top-half/musl
export MUSL_WASI_SYSROOT=${musl-wasi-sysroot}
'';

doCheck = true;
Expand Down Expand Up @@ -906,8 +895,6 @@ EOF
'';
ESM=nixpkgs.sources.esm;
TOMMATHSRC = nixpkgs.sources.libtommath;
MUSLSRC = "${nixpkgs.sources.musl-wasi}/libc-top-half/musl";
MUSL_WASI_SYSROOT = musl-wasi-sysroot;
LOCALE_ARCHIVE = nixpkgs.lib.optionalString stdenv.isLinux "${nixpkgs.glibcLocales}/lib/locale/locale-archive";
MOTOKO_BASE = base-src;
CANDID_TESTS = "${nixpkgs.sources.candid}/test";
Expand Down
4 changes: 1 addition & 3 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let
};
};

# No testing of atdgen, as it pulls in python stuff, tricky on musl
# No testing of atdgen, as it pulls in python stuff
atdgen = super.ocamlPackages.atdgen.overrideAttrs { doCheck = false; };
};
}
Expand All @@ -95,9 +95,7 @@ let
in rec {
rustc-nightly = rust-channel.rust.override {
targets = [
"wasm32-unknown-emscripten"
"wasm32-wasi"
"i686-unknown-linux-gnu"
];
extensions = ["rust-src"];
};
Expand Down
13 changes: 0 additions & 13 deletions nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,6 @@
"url": "https://github.com/kritzcreek/motoko-matchers/archive/cb838c192df3328ff9ae172e2dc7338cf55e74bf.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"musl-wasi": {
"branch": "main",
"builtin": false,
"description": "WASI libc implementation for WebAssembly",
"homepage": "https://wasi.dev",
"owner": "WebAssembly",
"repo": "wasi-libc",
"rev": "c5264e2bbe532994d06b039005f2af91bedcc1a6",
"sha256": "1skw2jqzaidr2zbzbjw32b36xvqniwf4if8cr4kbpp8vjvh7j2lr",
"type": "tarball",
"url": "https://github.com/WebAssembly/wasi-libc/archive/c5264e2bbe532994d06b039005f2af91bedcc1a6.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs": {
"branch": "release-24.05",
"description": "Nix Packages collection & NixOS",
Expand Down
181 changes: 19 additions & 162 deletions rts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,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_32 = \
pow pow_data sin cos tan asin acos atan atan2 exp exp_data log log_data fmod \
floor scalbn frexp strlen strnlen memcpy memset memchr memcmp memmove \
__math_oflow __math_uflow __math_xflow __math_divzero __math_invalid \
__rem_pio2 __rem_pio2_large __sin __cos __tan

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 @@ -58,48 +50,6 @@ TOMMATH_FLAGS_64 = $(TOMMATH_FLAGS_COMMON) \
# 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-eop.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

MUSL_FLAGS_32 = \
-isystem $(MUSLSRC)/arch/wasm32 \
-isystem $(MUSLSRC)/src/include \
-isystem $(MUSLSRC)/src/internal \
-isystem $(MUSLSRC)/include \
-isystem $(MUSL_WASI_SYSROOT)/include \
-I $(MUSLSRC)/../headers/private \
-I $(MUSLSRC)/src/include \
-I $(MUSLSRC)/include \
-I musl \
-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
#
# clang flags
#
Expand All @@ -114,7 +64,7 @@ CLANG_FLAGS_COMMON = \
-resource-dir=$(wildcard $(WASM_CLANG_LIB)/lib/clang/*)

CLANG_FLAGS_32 = $(CLANG_FLAGS_COMMON) \
--target=wasm32-emscripten
--target=wasm32

CLANG_FLAGS_64 = $(CLANG_FLAGS_COMMON) \
--target=wasm64
Expand All @@ -136,30 +86,11 @@ _build/wasm64:
_build/wasm32:
mkdir -p $@

#
# Common configuration
#

# Arguments obtained by Emscripten 3.1.64:
# `emcc -c --target=wasm64-unknown-emscripten -fpic --verbose`
# while skipping unneeded arguments.
RTS_LL_64=$(CLANG) -cc1 -triple wasm64-unknown-emscripten -emit-obj \
-mrelocation-model pic -pic-level 1 -mframe-pointer=none \
-ffp-contract=on -fno-rounding-math -mconstructor-aliases \
-target-feature +mutable-globals -fvisibility=hidden \
-Werror=implicit-function-declaration \
-iwithsysroot/include/fakesdl -iwithsysroot/include/compat \
-mllvm -combiner-global-alias-analysis=false \
-mllvm -enable-emscripten-sjlj -mllvm -disable-lsr \
-Wno-override-module
RTS_LL_64_DEBUG=$(RTS_LL_64)
RTS_LL_64_RELEASE=$(RTS_LL_64) -Oz

#
# Let make automatically search these directories (tommath and musl) for .c files
# Let make automatically search the tommath directory 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 @@ -185,20 +116,6 @@ $(TOMMATH_WASM_64_A): $(TOMMATH_WASM_64_O)
llvm-ar rcs $@ $^
llvm-ranlib $@

#
# Building the musl files
#

MUSL_WASM_32_O=$(MUSLFILES_32:%=_build/wasm32/musl_%.o)
MUSL_WASM_32_A=_build/libmusl_wasm32.a

_build/wasm32/musl_%.o: %.c | _build/wasm32
$(WASM_CLANG) $(CLANG_FLAGS_32) $(MUSL_FLAGS_32) $< --output $@

$(MUSL_WASM_32_A): $(MUSL_WASM_32_O)
llvm-ar rcs $@ $^
llvm-ranlib $@

#
# The rust code code of the RTS
#
Expand Down Expand Up @@ -245,7 +162,7 @@ TOMMATH_BIND_OPTIONS = \
$(TOMMATH_BINDINGS_RS_32): | _build/wasm32
bindgen $(TOMMATHSRC)/tommath.h \
-o $@ \
--ctypes-prefix=libc \
--ctypes-prefix="crate::libc_declarations" \
$(TOMMATH_BIND_OPTIONS) \
-- $(TOMMATH_FLAGS_32)

Expand All @@ -266,11 +183,11 @@ $(TOMMATH_BINDINGS_RS_64): | _build/wasm64
# 32-bit Wasm builds

RTS_DEPENDENCIES_32=$(TOMMATH_BINDINGS_RS_32) $(RTS_RUST_FILES) $(RTS_CARGO_FILES) | _build/wasm32
RTS_BUILD_32=cd motoko-rts && cargo build --target=wasm32-unknown-emscripten -Zbuild-std=core,alloc
RTS_BUILD_32=cd motoko-rts && cargo build --target wasm32-unknown-shared.json -Zbuild-std=core,alloc
RTS_DEBUG_BUILD_32=$(RTS_BUILD_32)
RTS_RELEASE_BUILD_32=$(RTS_BUILD_32) --release -Zbuild-std-features="panic_immediate_abort,optimize_for_size"
RTS_DEBUG_TARGET_32=motoko-rts/target/wasm32-unknown-emscripten/debug/libmotoko_rts.a
RTS_RELEASE_TARGET_32=motoko-rts/target/wasm32-unknown-emscripten/release/libmotoko_rts.a
RTS_DEBUG_TARGET_32=motoko-rts/target/wasm32-unknown-shared/debug/libmotoko_rts.a
RTS_RELEASE_TARGET_32=motoko-rts/target/wasm32-unknown-shared/release/libmotoko_rts.a

RTS_RUST_NON_INCREMENTAL_WASM_32_A=_build/wasm32/libmotoko_rts.a
RTS_RUST_NON_INCREMENTAL_DEBUG_WASM_32_A=_build/wasm32/libmotoko_rts_debug.a
Expand All @@ -297,81 +214,23 @@ $(RTS_RUST_INCREMENTAL_DEBUG_WASM_32_A): $(RTS_DEPENDENCIES_32)
# 64-bit Wasm builds

RTS_DEPENDENCIES_64=$(TOMMATH_BINDINGS_RS_64) $(RTS_RUST_FILES) $(RTS_CARGO_FILES) | _build/wasm64
COMPILER_FLAGS_64=--emit=llvm-ir
RTS_BUILD_64=cd motoko-rts && RUSTFLAGS="${COMPILER_FLAGS_64}" cargo build --target=wasm64-unknown-unknown -Zbuild-std=core,alloc --features enhanced_orthogonal_persistence
RTS_BUILD_64=cd motoko-rts && cargo build --target wasm64-unknown-shared.json -Zbuild-std=core,alloc --features enhanced_orthogonal_persistence
RTS_DEBUG_BUILD_64=$(RTS_BUILD_64)
RTS_RELEASE_BUILD_64=$(RTS_BUILD_64) --release -Zbuild-std-features="panic_immediate_abort,optimize_for_size"

RTS_DEBUG_TARGET_64_FOLDER=motoko-rts/target/wasm64-unknown-unknown/debug
RTS_RELEASE_TARGET_64_FOLDER=motoko-rts/target/wasm64-unknown-unknown/release

RTS_DEBUG_TARGET_64=$(RTS_DEBUG_TARGET_64_FOLDER)/deps/motoko_rts-*.ll
RTS_RELEASE_TARGET_64=$(RTS_RELEASE_TARGET_64_FOLDER)/deps/motoko_rts-*.ll
RTS_DEBUG_TARGET_64=motoko-rts/target/wasm64-unknown-shared/debug/libmotoko_rts.a
RTS_RELEASE_TARGET_64=motoko-rts/target/wasm64-unknown-shared/release/libmotoko_rts.a

CORE_DEBUG_TARGET_64=$(RTS_DEBUG_TARGET_64_FOLDER)/deps/core-*.ll
CORE_RELEASE_TARGET_64=$(RTS_RELEASE_TARGET_64_FOLDER)/deps/core-*.ll
RTS_RUST_WASM_64_A=_build/wasm64/libmotoko_rts.a
RTS_RUST_DEBUG_WASM_64_A=_build/wasm64/libmotoko_rts_debug.a

ALLOC_DEBUG_TARGET_64=$(RTS_DEBUG_TARGET_64_FOLDER)/deps/alloc-*.ll
ALLOC_RELEASE_TARGET_64=$(RTS_RELEASE_TARGET_64_FOLDER)/deps/alloc-*.ll

COMPILER_BUILTINS_DEBUG_TARGET_64=$(RTS_DEBUG_TARGET_64_FOLDER)/deps/compiler_builtins-*.ll
COMPILER_BUILTINS_RELEASE_TARGET_64=$(RTS_RELEASE_TARGET_64_FOLDER)/deps/compiler_builtins-*.ll

BUILD_64_FOLDER=_build/wasm64

RTS_RUST_LLVM_IR_64=$(BUILD_64_FOLDER)/libmotoko_rts.ll
RTS_RUST_DEBUG_LLVM_IR_64=$(BUILD_64_FOLDER)/libmotoko_rts_debug.ll

CORE_DEBUG_LLVM_IR_64=$(BUILD_64_FOLDER)/core_debug.ll
CORE_RELEASE_LLVM_IR_64=$(BUILD_64_FOLDER)/core.ll

ALLOC_DEBUG_LLVM_IR_64=$(BUILD_64_FOLDER)/alloc_debug.ll
ALLOC_RELEASE_LLVM_IR_64=$(BUILD_64_FOLDER)/alloc.ll

COMPILER_BUILTINS_DEBUG_LLVM_IR_64=$(BUILD_64_FOLDER)/compiler_builtins_debug.ll
COMPILER_BUILTINS_RELEASE_LLVM_IR_64=$(BUILD_64_FOLDER)/compiler_builtins.ll

$(RTS_RUST_LLVM_IR_64): $(RTS_DEPENDENCIES_64)
rm -rf $(RTS_RELEASE_TARGET_64_FOLDER)
$(RTS_RUST_WASM_64_A): $(RTS_DEPENDENCIES_64)
$(RTS_RELEASE_BUILD_64)
cp $(CORE_RELEASE_TARGET_64) -T $(CORE_RELEASE_LLVM_IR_64)
cp $(ALLOC_RELEASE_TARGET_64) -T $(ALLOC_RELEASE_LLVM_IR_64)
cp $(COMPILER_BUILTINS_RELEASE_TARGET_64) -T $(COMPILER_BUILTINS_RELEASE_LLVM_IR_64)
cp $(RTS_RELEASE_TARGET_64) -T $@
cp $(RTS_RELEASE_TARGET_64) $@

$(RTS_RUST_DEBUG_LLVM_IR_64): $(RTS_DEPENDENCIES_64)
rm -rf $(RTS_DEBUG_TARGET_64_FOLDER)
$(RTS_RUST_DEBUG_WASM_64_A): $(RTS_DEPENDENCIES_64)
$(RTS_DEBUG_BUILD_64)
cp $(CORE_DEBUG_TARGET_64) -T $(CORE_DEBUG_LLVM_IR_64)
cp $(ALLOC_DEBUG_TARGET_64) -T $(ALLOC_DEBUG_LLVM_IR_64)
cp $(COMPILER_BUILTINS_DEBUG_TARGET_64) -T $(COMPILER_BUILTINS_DEBUG_LLVM_IR_64)
cp $(RTS_DEBUG_TARGET_64) -T $@

RTS_RUST_LINKED_LLVM_BC_64=$(BUILD_64_FOLDER)/libmotoko_rts.bc
RTS_RUST_DEBUG_LINKED_LLVM_BC_64=$(BUILD_64_FOLDER)/libmotoko_rts_debug.bc

$(RTS_RUST_LINKED_LLVM_BC_64): $(RTS_RUST_LLVM_IR_64)
$(LLVM_LINK) -o $@ \
$(CORE_RELEASE_LLVM_IR_64) \
$(ALLOC_RELEASE_LLVM_IR_64) \
$(COMPILER_BUILTINS_RELEASE_LLVM_IR_64) \
$+

$(RTS_RUST_DEBUG_LINKED_LLVM_BC_64): $(RTS_RUST_DEBUG_LLVM_IR_64)
$(LLVM_LINK) -o $@ \
$(CORE_DEBUG_LLVM_IR_64) \
$(ALLOC_DEBUG_LLVM_IR_64) \
$(COMPILER_BUILTINS_DEBUG_LLVM_IR_64) \
$+

RTS_RUST_WASM_64_O=$(BUILD_64_FOLDER)/libmotoko_rts.o
RTS_RUST_DEBUG_WASM_64_O=$(BUILD_64_FOLDER)/libmotoko_rts_debug.o

$(RTS_RUST_WASM_64_O): $(RTS_RUST_LINKED_LLVM_BC_64)
$(RTS_LL_64_RELEASE) -o $@ $+

$(RTS_RUST_DEBUG_WASM_64_O): $(RTS_RUST_DEBUG_LINKED_LLVM_BC_64)
$(RTS_LL_64_DEBUG) -o $@ $+
cp $(RTS_DEBUG_TARGET_64) $@

#
# The test suite
Expand Down Expand Up @@ -405,8 +264,6 @@ test64: $(TEST_DEPENDENCIES_64)
# Putting it all together
#

# These symbols from musl are used by the code generator directly

EXPORTED_SYMBOLS=\
__wasm_call_ctors \
__wasm_apply_data_relocs \
Expand All @@ -424,7 +281,7 @@ EXPORTED_SYMBOLS=\
fmod \
log

WASM_A_DEPENDENCIES_32=$(TOMMATH_WASM_32_A) $(MUSL_WASM_32_A)
WASM_A_DEPENDENCIES_32=$(TOMMATH_WASM_32_A)
LINKER_OPTIONS_32=\
--import-memory --shared --no-entry --gc-sections \
$(EXPORTED_SYMBOLS:%=--export=%) \
Expand Down Expand Up @@ -460,13 +317,13 @@ mo-rts-incremental-debug.wasm: $(RTS_RUST_INCREMENTAL_DEBUG_WASM_32_A) $(WASM_A_
$(LINKER_OPTIONS_32) \
$+

mo-rts-eop.wasm: $(RTS_RUST_WASM_64_O) $(WASM_A_DEPENDENCIES_64)
mo-rts-eop.wasm: $(RTS_RUST_WASM_64_A) $(WASM_A_DEPENDENCIES_64)
$(WASM_LD) -o $@ \
$(LINKER_OPTIONS_64) \
$+
$(WASM_OPT) $(WASM_OPT_OPTIONS) -o $@ $@

mo-rts-eop-debug.wasm: $(RTS_RUST_DEBUG_WASM_64_O) $(WASM_A_DEPENDENCIES_64)
mo-rts-eop-debug.wasm: $(RTS_RUST_DEBUG_WASM_64_A) $(WASM_A_DEPENDENCIES_64)
$(WASM_LD) -o $@ \
$(LINKER_OPTIONS_64) \
$+
Expand Down
8 changes: 8 additions & 0 deletions rts/motoko-rts/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
# std, and compiler_builtins and makes it easier to find symbols.
[build]
rustflags = ["-Crelocation-model=pic", "-Ccodegen-units=1"]

# Use the patched wasm-ld
[target.wasm32-unknown-shared]
linker = "wasm-ld"

# Use the patched wasm-ld
[target.wasm64-unknown-shared]
linker = "wasm-ld"
Loading