Skip to content

Commit d79639a

Browse files
authored
Use mimalloc allocator for Linux static build (#7378)
The new CMake flag MIMALLOC_STATIC controls this. mimalloc fixes perf problems, especially on heavily multi-threaded workloads (many functions, high core count machines) on the musl allocator, see #5561.
1 parent f3d100e commit d79639a

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ jobs:
250250

251251
- name: cmake
252252
run: |
253-
./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
253+
./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
254254
255255
- name: build
256256
run: |

.github/workflows/create_release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ jobs:
140140

141141
- name: cmake
142142
run: |
143-
./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
143+
./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
144144
145145
- name: build
146146
run: |

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "third_party/fuzztest"]
88
path = third_party/fuzztest
99
url = https://github.com/google/fuzztest
10+
[submodule "third_party/mimalloc"]
11+
path = third_party/mimalloc
12+
url = https://github.com/microsoft/mimalloc.git

CMakeLists.txt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,21 @@ endif()
4949

5050
option(BUILD_STATIC_LIB "Build as a static library" OFF)
5151
if(MSVC)
52-
# We don't have dllexport declarations set up for windows yet.
52+
# We don't have dllexport declarations set up for Windows yet.
5353
set(BUILD_STATIC_LIB ON)
5454
endif()
5555

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

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

6269
# Turn this on to build binaryen.js as ES5, with additional compatibility configuration for js_of_ocaml.
@@ -345,11 +352,11 @@ if(EMSCRIPTEN)
345352
# in opt builds, LTO helps so much (>20%) it's worth slow compile times
346353
add_nondebug_compile_flag("-flto")
347354
endif()
348-
if(EMSCRIPTEN_ENABLE_WASM64)
349-
add_compile_flag("-sMEMORY64 -Wno-experimental")
350-
add_link_flag("-sMEMORY64")
351-
endif()
355+
if(EMSCRIPTEN_ENABLE_WASM64)
356+
add_compile_flag("-sMEMORY64 -Wno-experimental")
357+
add_link_flag("-sMEMORY64")
352358
endif()
359+
endif()
353360

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

465+
if(MIMALLOC_STATIC)
466+
if(NOT(LINUX AND BUILD_STATIC_LIB) OR EMSCRIPTEN)
467+
message(FATAL_ERROR "Statically linking mimalloc is only supported when building as a native, statically linked library on Linux.")
468+
endif()
469+
message(STATUS "Building with statically linked mimalloc allocator.")
470+
target_link_libraries(binaryen mimalloc-static)
471+
endif()
472+
458473
add_subdirectory(src/ir)
459474
add_subdirectory(src/asmjs)
460475
add_subdirectory(src/cfg)

third_party/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,18 @@ endif()
1515
if(BUILD_LLVM_DWARF)
1616
add_subdirectory(llvm-project)
1717
endif()
18+
19+
if(MIMALLOC_STATIC)
20+
# Using a C++ compiler avoids warnings about -fno-rtti with a C compiler.
21+
set(MI_USE_CXX ON)
22+
# We only need the static library, nothing else.
23+
set(MI_BUILD_STATIC ON)
24+
set(MI_BUILD_SHARED OFF)
25+
set(MI_BUILD_OBJECT OFF)
26+
set(MI_BUILD_TESTS OFF)
27+
# Do not show debug and warning messages of the allocator by default.
28+
# (They can still be enabled via MIMALLOC_VERBOSE=1 wasm-opt ...)
29+
add_compile_definitions(MI_DEBUG=0)
30+
31+
add_subdirectory(mimalloc)
32+
endif()

third_party/mimalloc

Submodule mimalloc added at e112300

0 commit comments

Comments
 (0)