Skip to content

Commit

Permalink
Switch to WASMI engine in icu_codepointtrie_builder (#4621)
Browse files Browse the repository at this point in the history
Fixes #3241 

I switched our WASM engine to `wasmi` because it is faster to compile
and has fewer dependencies. I changed how we interact with C++, too, in
order to improve the performance.
  • Loading branch information
sffc committed Feb 27, 2024
1 parent 4d3c184 commit 952a39f
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 1,217 deletions.
993 changes: 131 additions & 862 deletions Cargo.lock

Large diffs are not rendered by default.

32 changes: 4 additions & 28 deletions components/collections/codepointtrie_builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,18 @@ independent = true
[features]
# Use the wasm builder
default = ["wasm"]
wasm = ["wasmer", "wasmer-wasi", "once_cell"]
wasm = ["dep:wasmi"]
# Use the ICU4C builder
# needs the ICU4C_LIB_PATH variable set and pointing to an ICU4C lib folder
# containing dylibs. If you want to use staticlibs, set ICU4C_LINK_STATICALLY.
# Will be silently disabled if the wasm feature is enabled
icu4c = ["zerovec"]
icu4c = []

[dependencies]
icu_collections = { workspace = true, features = ["serde"] }
once_cell = { version = "1.18.0", optional = true }
toml = "0.5"
zerovec = { workspace = true, optional = true }

[dependencies.wasmer]
version = "2.2.1"
default-features = false
optional = true
features = [
# We are running on the local system (as opposed to JavaScript)
"sys",
# By default, use the Singlepass compiler for faster startup time
"default-singlepass",
# By default, use the Universal engine for in-memory compiled code
"default-universal"
]

[dependencies.wasmer-wasi]
version = "2.2.1"
default-features = false
optional = true
features = [
# We are running on the local system (as opposed to JavaScript)
"sys",
# We don't need real filesystem access, so use mem-fs
"mem-fs"
]
zerovec = { workspace = true }
wasmi = { version = "0.31.2", optional = true }

[dev-dependencies]
icu = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions components/collections/codepointtrie_builder/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ list_to_ucptrie
list_to_ucptrie.wasm
list_to_ucptrie.wat
icu4c_obj/
wasm_obj/
89 changes: 67 additions & 22 deletions components/collections/codepointtrie_builder/cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,79 @@
# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

ICU4C_SOURCE ?= $(HOME)/icu/icu4c/source
ICU4C_BUILD ?= $(ICU4C_SOURCE)
CXX := clang++
WASICXX := wasic++

.PHONY: all clean
all: list_to_ucptrie list_to_ucptrie.wasm
all: ucptrie_wrap.wasm

icu4c_obj/wasm/%.o: $(ICU4C_SOURCE)/%.cpp
mkdir -p icu4c_obj/wasm/common
mkdir -p icu4c_obj/wasm/tools/toolutil
$(WASICXX) -DTRUE=1 -c -flto -I$(ICU4C_SOURCE)/common -I$(ICU4C_SOURCE)/tools/toolutil $< -o $@
# The objects are built with the following flags:
# --target=wasm32-unknown-wasi to build with WASI for system symbols
# -DTRUE=1 to fix error use of undeclared identifier 'TRUE'
# -DU_DISABLE_RENAMING so the same WASM module works across ICU versions
# --compile to skip the linking step
# -flto to include LTO (link-time optimization) metadata

icu4c_obj_wasm_files = \
icu4c_obj/wasm/common/cmemory.o \
icu4c_obj/wasm/common/errorcode.o \
icu4c_obj/wasm/common/ucptrie.o \
icu4c_obj/wasm/common/umutablecptrie.o \
icu4c_obj/wasm/common/uobject.o \
icu4c_obj/wasm/common/utypes.o \
icu4c_obj/wasm/tools/toolutil/writesrc.o
wasm_obj/icu4c/%.o: $(ICU4C_SOURCE)/%.cpp
mkdir -p wasm_obj/icu4c/common
$(CXX) --target=wasm32-unknown-wasi \
-DTRUE=1 \
-DU_DISABLE_RENAMING=1 \
--compile \
-flto \
-I$(ICU4C_SOURCE)/common \
$< \
-o $@

list_to_ucptrie: list_to_ucptrie.cpp $(icu4c_obj_native_files)
$(CXX) -o list_to_ucptrie -ldl -I$(ICU4C_SOURCE)/common -I$(ICU4C_SOURCE)/tools/toolutil list_to_ucptrie.cpp $(ICU4C_BUILD)/lib/libicutu.a $(ICU4C_BUILD)/lib/libicuuc.a
wasm_obj/ucptrie_wrap.o: ucptrie_wrap.cpp
mkdir -p wasm_obj
$(CXX) --target=wasm32-unknown-wasi \
-I/usr/include/wasm32-wasi \
--compile \
-flto \
-I$(ICU4C_SOURCE)/common \
$< \
-o $@

list_to_ucptrie.wasm: list_to_ucptrie.cpp $(icu4c_obj_wasm_files)
$(WASICXX) -DTRUE=1 -flto -Wl,--gc-sections -Wl,--strip-all -I$(ICU4C_SOURCE)/common -I$(ICU4C_SOURCE)/tools/toolutil $(icu4c_obj_wasm_files) list_to_ucptrie.cpp -o list_to_ucptrie.wasm
wasm_obj_files = \
wasm_obj/icu4c/common/cmemory.o \
wasm_obj/icu4c/common/errorcode.o \
wasm_obj/icu4c/common/ucptrie.o \
wasm_obj/icu4c/common/umutablecptrie.o \
wasm_obj/icu4c/common/uobject.o \
wasm_obj/icu4c/common/utypes.o \
wasm_obj/ucptrie_wrap.o

# Compile as a Reactor Module so we don't need a main function.
# This also means we don't need WASI imports at runtime.
# <https://dylibso.com/blog/wasi-command-reactor/>
ucptrie_wrap.wasm: $(wasm_obj_files)
$(CXX) --target=wasm32-unknown-wasi \
-mexec-model=reactor \
-DTRUE=1 \
-flto \
-Wl,--gc-sections \
-Wl,--strip-all \
-Wl,--export=umutablecptrie_open \
-Wl,--export=umutablecptrie_set \
-Wl,--export=umutablecptrie_buildImmutable \
-Wl,--export=ucptrie_close \
-Wl,--export=umutablecptrie_close \
-Wl,--export=create_uerrorcode \
-Wl,--export=read_uerrorcode \
-Wl,--export=read_ucptrie_highStart \
-Wl,--export=read_ucptrie_shifted12HighStart \
-Wl,--export=read_ucptrie_index3NullOffset \
-Wl,--export=read_ucptrie_dataNullOffset \
-Wl,--export=read_ucptrie_nullValue \
-Wl,--export=get_index_ptr \
-Wl,--export=get_index_length \
-Wl,--export=get_data_ptr \
-Wl,--export=get_data_length \
-I$(ICU4C_SOURCE)/common \
-I$(ICU4C_SOURCE)/tools/toolutil \
$(wasm_obj_files) \
-o ucptrie_wrap.wasm

clean:
rm -rf icu4c_obj
rm -f list_to_ucptrie
rm -f list_to_ucptrie.wasm
rm -rf wasm_obj
rm -f ucptrie_wrap.wasm
97 changes: 6 additions & 91 deletions components/collections/codepointtrie_builder/cpp/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 0 additions & 112 deletions components/collections/codepointtrie_builder/cpp/list_to_ucptrie.cpp

This file was deleted.

Loading

0 comments on commit 952a39f

Please sign in to comment.