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 mimalloc allocator for static build #7378

Merged
merged 4 commits into from
Mar 19, 2025
Merged
Changes from all 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/ci.yml
Original file line number Diff line number Diff line change
@@ -250,7 +250,7 @@ jobs:

- name: cmake
run: |
./alpine.sh cmake . -G Ninja -DCMAKE_INSTALL_PREFIX=out/install -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static" -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIB=ON -DCMAKE_INSTALL_PREFIX=install
./alpine.sh cmake . -G Ninja -DCMAKE_INSTALL_PREFIX=out/install -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static" -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIB=ON -DMIMALLOC_STATIC=ON -DCMAKE_INSTALL_PREFIX=install

- name: build
run: |
2 changes: 1 addition & 1 deletion .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
@@ -140,7 +140,7 @@ jobs:

- name: cmake
run: |
./alpine.sh cmake . -G Ninja -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static" -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIB=ON -DCMAKE_INSTALL_PREFIX=install
./alpine.sh cmake . -G Ninja -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static" -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIB=ON -DMIMALLOC_STATIC=ON -DCMAKE_INSTALL_PREFIX=install

- name: build
run: |
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -7,3 +7,6 @@
[submodule "third_party/fuzztest"]
path = third_party/fuzztest
url = https://github.com/google/fuzztest
[submodule "third_party/mimalloc"]
path = third_party/mimalloc
url = https://github.com/microsoft/mimalloc.git
29 changes: 22 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -49,14 +49,21 @@ endif()

option(BUILD_STATIC_LIB "Build as a static library" OFF)
if(MSVC)
# We don't have dllexport declarations set up for windows yet.
# We don't have dllexport declarations set up for Windows yet.
set(BUILD_STATIC_LIB ON)
endif()

# Turn this off to install only tools and not static/dynamic libs
# Advised to turn on when statically linking against musl libc (e.g., in the
# Alpine Linux build we use for producing official Linux binaries), because
# musl libc's allocator has very bad performance on heavily multi-threaded
# workloads / high core count machines.
# See https://github.com/WebAssembly/binaryen/issues/5561.
option(MIMALLOC_STATIC "Build with statically linked mimalloc allocator" OFF)

# Turn this off to install only tools and not static/dynamic libs.
option(INSTALL_LIBS "Install libraries" ON)

# Turn this on to build only the subset of tools needed for emscripten
# Turn this on to build only the subset of tools needed for Emscripten.
option(BUILD_EMSCRIPTEN_TOOLS_ONLY "Build only tools needed by emscripten" OFF)

# Turn this on to build binaryen.js as ES5, with additional compatibility configuration for js_of_ocaml.
@@ -345,11 +352,11 @@ if(EMSCRIPTEN)
# in opt builds, LTO helps so much (>20%) it's worth slow compile times
add_nondebug_compile_flag("-flto")
endif()
if(EMSCRIPTEN_ENABLE_WASM64)
add_compile_flag("-sMEMORY64 -Wno-experimental")
add_link_flag("-sMEMORY64")
endif()
if(EMSCRIPTEN_ENABLE_WASM64)
add_compile_flag("-sMEMORY64 -Wno-experimental")
add_link_flag("-sMEMORY64")
endif()
endif()

# clang doesn't print colored diagnostics when invoked from Ninja
if(UNIX AND CMAKE_GENERATOR STREQUAL "Ninja")
@@ -455,6 +462,14 @@ if(BUILD_LLVM_DWARF)
target_link_libraries(binaryen llvm_dwarf)
endif()

if(MIMALLOC_STATIC)
if(NOT(LINUX AND BUILD_STATIC_LIB) OR EMSCRIPTEN)
message(FATAL_ERROR "Statically linking mimalloc is only supported when building as a native, statically linked library on Linux.")
endif()
message(STATUS "Building with statically linked mimalloc allocator.")
target_link_libraries(binaryen mimalloc-static)
endif()

add_subdirectory(src/ir)
add_subdirectory(src/asmjs)
add_subdirectory(src/cfg)
15 changes: 15 additions & 0 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -15,3 +15,18 @@ endif()
if(BUILD_LLVM_DWARF)
add_subdirectory(llvm-project)
endif()

if(MIMALLOC_STATIC)
# Using a C++ compiler avoids warnings about -fno-rtti with a C compiler.
set(MI_USE_CXX ON)
# We only need the static library, nothing else.
set(MI_BUILD_STATIC ON)
set(MI_BUILD_SHARED OFF)
set(MI_BUILD_OBJECT OFF)
set(MI_BUILD_TESTS OFF)
# Do not show debug and warning messages of the allocator by default.
# (They can still be enabled via MIMALLOC_VERBOSE=1 wasm-opt ...)
add_compile_definitions(MI_DEBUG=0)

add_subdirectory(mimalloc)
endif()
1 change: 1 addition & 0 deletions third_party/mimalloc
Submodule mimalloc added at e11230