From b06275bb3176c266362482642453c2503533c9f3 Mon Sep 17 00:00:00 2001 From: Daniel Lehmann Date: Mon, 17 Mar 2025 18:02:28 +0100 Subject: [PATCH 1/4] Use mimalloc allocator for static build Which fixes performance problems, especially on heavily multi-threaded workloads (many functions, high core count machines), see #5561. --- .gitmodules | 3 +++ CMakeLists.txt | 3 +++ third_party/CMakeLists.txt | 4 ++++ third_party/mimalloc | 1 + 4 files changed, 11 insertions(+) create mode 160000 third_party/mimalloc diff --git a/.gitmodules b/.gitmodules index 990b797a0e0..a674125e389 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 01332f33791..156ccc6cc0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -446,6 +446,9 @@ if(BUILD_STATIC_LIB) message(STATUS "Building libbinaryen as statically linked library.") add_library(binaryen STATIC) add_definitions(-DBUILD_STATIC_LIBRARY) + + message(STATUS "Using mimalloc for static build to avoid bad musl libc allocator performance.") + target_link_libraries(binaryen mimalloc-static) else() message(STATUS "Building libbinaryen as shared library.") add_library(binaryen SHARED) diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index fde5276d7da..4394a21d20e 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -15,3 +15,7 @@ endif() if(BUILD_LLVM_DWARF) add_subdirectory(llvm-project) endif() + +if(BUILD_STATIC_LIB) + add_subdirectory(mimalloc) +endif() diff --git a/third_party/mimalloc b/third_party/mimalloc new file mode 160000 index 00000000000..f11732acdfe --- /dev/null +++ b/third_party/mimalloc @@ -0,0 +1 @@ +Subproject commit f11732acdfe3e33f64f4aa3e7db657ffd80dea8f From 3b3610a596300ab5b3329b03dcca63a723880fac Mon Sep 17 00:00:00 2001 From: Daniel Lehmann Date: Tue, 18 Mar 2025 15:07:42 +0100 Subject: [PATCH 2/4] address comments --- .github/workflows/ci.yml | 2 +- .github/workflows/create_release.yml | 2 +- CMakeLists.txt | 32 +++++++++++++++++++--------- third_party/CMakeLists.txt | 10 ++++++++- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c98dfb349e..4c4a4f27248 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: | diff --git a/.github/workflows/create_release.yml b/.github/workflows/create_release.yml index a71ad6da9d2..f6b8f7802ad 100644 --- a/.github/workflows/create_release.yml +++ b/.github/workflows/create_release.yml @@ -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: | diff --git a/CMakeLists.txt b/CMakeLists.txt index 156ccc6cc0e..b48ae745dd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") @@ -446,9 +453,6 @@ if(BUILD_STATIC_LIB) message(STATUS "Building libbinaryen as statically linked library.") add_library(binaryen STATIC) add_definitions(-DBUILD_STATIC_LIBRARY) - - message(STATUS "Using mimalloc for static build to avoid bad musl libc allocator performance.") - target_link_libraries(binaryen mimalloc-static) else() message(STATUS "Building libbinaryen as shared library.") add_library(binaryen SHARED) @@ -458,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) diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index 4394a21d20e..aa5dd2f33c6 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -16,6 +16,14 @@ if(BUILD_LLVM_DWARF) add_subdirectory(llvm-project) endif() -if(BUILD_STATIC_LIB) +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) + add_subdirectory(mimalloc) endif() From a5517e2d5b5993b2b7fdb5ed0b3dd477453ef974 Mon Sep 17 00:00:00 2001 From: Daniel Lehmann Date: Tue, 18 Mar 2025 17:57:00 +0100 Subject: [PATCH 3/4] update mimalloc to v2.1.7 which is the same version that Ubuntu 24.10 has in their mimalloc2.0 package, see https://launchpad.net/ubuntu/+source/mimalloc --- third_party/mimalloc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/third_party/mimalloc b/third_party/mimalloc index f11732acdfe..8c532c32c3c 160000 --- a/third_party/mimalloc +++ b/third_party/mimalloc @@ -1 +1 @@ -Subproject commit f11732acdfe3e33f64f4aa3e7db657ffd80dea8f +Subproject commit 8c532c32c3c96e5ba1f2283e032f69ead8add00f From 3c7f959431dd5d586ed56be4163788c0c9ea368d Mon Sep 17 00:00:00 2001 From: Daniel Lehmann Date: Wed, 19 Mar 2025 14:15:04 +0100 Subject: [PATCH 4/4] Change mimalloc to stable master branch, don't show debug messages by default --- third_party/CMakeLists.txt | 3 +++ third_party/mimalloc | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index aa5dd2f33c6..9bcd9838acf 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -24,6 +24,9 @@ if(MIMALLOC_STATIC) 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() diff --git a/third_party/mimalloc b/third_party/mimalloc index 8c532c32c3c..e1123000597 160000 --- a/third_party/mimalloc +++ b/third_party/mimalloc @@ -1 +1 @@ -Subproject commit 8c532c32c3c96e5ba1f2283e032f69ead8add00f +Subproject commit e1123000597b1abe38164e09d5713652e1e82e59