From df76a0585f2badb30aa90ccfb52fa9fa33a00784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20S=C3=B8rvig?= Date: Fri, 26 Aug 2022 09:49:52 +0200 Subject: wasm: set ALLOW_MEMORY_GROWTH for multi-threaded builds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unify the settings for single-threaded and multi-threaded builds; Qt now always enables heap growth by default. This means we don't have to reserve a large (1GB) fixed memory size, but can instead set the smaller (50 MB) initial memory size, like the single-threaded build does. Enabling threads + memory growth can potentially cause a performance regression when accessing heap memory from JavaScript (https://github.com/WebAssembly/design/issues/1271). We leave it for the application to decide if this applies, and if the switch to fixed memory should be made. Change-Id: I96988b072506456685086e55aca4007a146bd70f Reviewed-by: Alexandru Croitor Reviewed-by: MikoĊ‚aj Boc Reviewed-by: Lorn Potter --- cmake/QtWasmHelpers.cmake | 4 ++-- mkspecs/features/wasm/wasm.prf | 19 +++---------------- src/corelib/Qt6WasmMacros.cmake | 21 +++------------------ 3 files changed, 8 insertions(+), 36 deletions(-) diff --git a/cmake/QtWasmHelpers.cmake b/cmake/QtWasmHelpers.cmake index 198ebbff23..07de3751ef 100644 --- a/cmake/QtWasmHelpers.cmake +++ b/cmake/QtWasmHelpers.cmake @@ -35,10 +35,10 @@ function (qt_internal_setup_wasm_target_properties wasmTarget) if (QT_FEATURE_thread) target_compile_options("${wasmTarget}" INTERFACE "SHELL:-pthread") target_link_options("${wasmTarget}" INTERFACE "SHELL:-pthread") - else() - target_link_options("${wasmTarget}" INTERFACE "SHELL:-s ALLOW_MEMORY_GROWTH=1") endif() + target_link_options("${wasmTarget}" INTERFACE "SHELL:-s ALLOW_MEMORY_GROWTH") + # debug add_compile_options if ("QT_WASM_SOURCE_MAP=1" IN_LIST QT_QMAKE_DEVICE_OPTIONS) set(WASM_SOURCE_MAP_BASE "http://localhost:8000/") diff --git a/mkspecs/features/wasm/wasm.prf b/mkspecs/features/wasm/wasm.prf index 5e6501ed99..5b7dbaf493 100644 --- a/mkspecs/features/wasm/wasm.prf +++ b/mkspecs/features/wasm/wasm.prf @@ -29,29 +29,16 @@ exists($$QMAKE_QT_CONFIG) { message("Setting PTHREAD_POOL_SIZE to" $$POOL_SIZE) EMCC_LFLAGS += -s PTHREAD_POOL_SIZE=$$POOL_SIZE - } else { - EMCC_LFLAGS += -s ALLOW_MEMORY_GROWTH=1 } + # Set memory options + EMCC_LFLAGS += -sALLOW_MEMORY_GROWTH isEmpty(QT_WASM_INITIAL_MEMORY) { - # Hardcode wasm memory size. - - qtConfig(thread) { - # Pthreads and ALLOW_MEMORY_GROWTH can cause javascript wasm memory access to - # be slow. Instead, we specify the memory size - # at build time. Further, browsers limit the maximum initial memory size to 1GB. - # https://github.com/WebAssembly/design/issues/1271 - INITIAL_MEMORY = 1GB - } else { - INITIAL_MEMORY = 50MB # emscripten default is 16MB, we need slightly more - } + INITIAL_MEMORY = 50MB # emscripten default is 16MB, we need slightly more } else { - # QT_WASM_INITIAL_MEMORY must be a multiple of 64KB (i.e. 65536) INITIAL_MEMORY = $$QT_WASM_INITIAL_MEMORY - message("Setting INITIAL_MEMORY to" $$INITIAL_MEMORY) } EMCC_LFLAGS += -s INITIAL_MEMORY=$$INITIAL_MEMORY - message("Setting INITIAL_MEMORY to" $$INITIAL_MEMORY) qtConfig(sse2) { QMAKE_CFLAGS += -O2 -msimd128 -msse -msse2 diff --git a/src/corelib/Qt6WasmMacros.cmake b/src/corelib/Qt6WasmMacros.cmake index 59f69174db..379229bc6a 100644 --- a/src/corelib/Qt6WasmMacros.cmake +++ b/src/corelib/Qt6WasmMacros.cmake @@ -48,29 +48,14 @@ function(_qt_internal_wasm_add_target_helpers target) message(DEBUG "Setting PTHREAD_POOL_SIZE to ${POOL_SIZE} for ${target}") endif() - # Hardcode wasm memory size. + # Set initial memory size, either from user setting or to a minimum amount required by Qt. get_target_property(_tmp_initialMemory "${target}" QT_WASM_INITIAL_MEMORY) if(_tmp_initialMemory) set(QT_WASM_INITIAL_MEMORY "${_tmp_initialMemory}") elseif(NOT DEFINED QT_WASM_INITIAL_MEMORY) - if(QT_FEATURE_thread) - # Pthreads and ALLOW_MEMORY_GROWTH can cause javascript wasm memory access to - # be slow and having to update HEAP* views. Instead, we specify the memory size - # at build time. Further, browsers limit the maximum initial memory size to 1GB. - # https://github.com/WebAssembly/design/issues/1271 - set(QT_WASM_INITIAL_MEMORY "1GB") - else() - # emscripten default is 16MB, we need slightly more sometimes - set(QT_WASM_INITIAL_MEMORY "50MB") - endif() - endif() - - if(DEFINED QT_WASM_INITIAL_MEMORY) - # QT_WASM_INITIAL_MEMORY must be a multiple of 65536 - target_link_options("${target}" - PRIVATE "SHELL:-s INITIAL_MEMORY=${QT_WASM_INITIAL_MEMORY}") - message(DEBUG "-- Setting INITIAL_MEMORY to ${QT_WASM_INITIAL_MEMORY} for ${target}") + set(QT_WASM_INITIAL_MEMORY "50MB") endif() + target_link_options("${target}" PRIVATE "SHELL:-s INITIAL_MEMORY=${QT_WASM_INITIAL_MEMORY}") endif() endfunction() -- cgit v1.2.3