Skip to content

Commit

Permalink
wasm: move user configurable settings
Browse files Browse the repository at this point in the history
Both INITIAL_MEMORY and PTHREAD_POOL_SIZE are settings users can
change, they are not interface settings.

Fixes: QTBUG-100693
Pick-to: 6.3 6.2
Change-Id: Ie1547c7f52c9fe109a313260616705728024b6b8
Reviewed-by: Leena Miettinen <[email protected]>
Reviewed-by: David Skoland <[email protected]>
  • Loading branch information
lpotter committed Feb 23, 2022
1 parent 196e10b commit af36dc7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 25 deletions.
25 changes: 0 additions & 25 deletions cmake/QtWasmHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,6 @@ function (qt_internal_setup_wasm_target_properties wasmTarget)
target_compile_options("${wasmTarget}" INTERFACE -O2 -msimd128 -msse -msse2)
endif()

# Hardcode wasm memory size. Emscripten does not currently support memory growth
# (ALLOW_MEMORY_GROWTH) in pthreads mode, and requires specifying the memory size
# at build time. Further, browsers limit the maximum initial memory size to 1GB.
# QT_WASM_INITIAL_MEMORY must be a multiple of 64KB (i.e. 65536)
if(NOT DEFINED QT_WASM_INITIAL_MEMORY)
if(QT_FEATURE_thread)
set(QT_WASM_INITIAL_MEMORY "1GB")
else()
set(QT_WASM_INITIAL_MEMORY "20MB") # emscripten default is 16MB, we need slightly more sometimes
endif()
endif()

if(DEFINED QT_WASM_INITIAL_MEMORY)
target_link_options("${wasmTarget}" INTERFACE "SHELL:-s INITIAL_MEMORY=${QT_WASM_INITIAL_MEMORY}")
message("Setting INITIAL_MEMORY to ${QT_WASM_INITIAL_MEMORY}")
endif()

if (QT_FEATURE_opengles3)
target_link_options("${wasmTarget}" INTERFACE "SHELL:-s FULL_ES3=1")

Expand All @@ -58,14 +41,6 @@ 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")

set(POOL_SIZE 4)
if(DEFINED QT_WASM_PTHREAD_POOL_SIZE)
set(POOL_SIZE ${QT_WASM_PTHREAD_POOL_SIZE})
endif()
target_link_options("${wasmTarget}" INTERFACE "SHELL:-s PTHREAD_POOL_SIZE=${POOL_SIZE}")
message("Setting PTHREAD_POOL_SIZE to ${POOL_SIZE}")

else()
target_link_options("${wasmTarget}" INTERFACE "SHELL:-s ALLOW_MEMORY_GROWTH=1")
endif()
Expand Down
2 changes: 2 additions & 0 deletions doc/global/macros.qdocconf
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ macro.cmakepropertyandroidonly = "\\note This property is used only if targeting
macro.cmakevariableandroidonly = "\\note This variable is used only if targeting the Android platform."

macro.versionlessCMakeCommandsNote = "If \\l{Versionless commands}{versionless commands} are disabled, use \\c{\1} instead. It supports the same set of arguments as this command."

macro.cmakepropertywebassemblyonly = "\\note This property is used only if targeting the WebAssembly platform."
35 changes: 35 additions & 0 deletions src/corelib/Qt6WasmMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,40 @@ function(_qt_internal_wasm_add_target_helpers target)
configure_file("${WASM_BUILD_DIR}/plugins/platforms/qtlogo.svg"
qtlogo.svg COPYONLY)

if(QT_FEATURE_thread)
set(POOL_SIZE 4)
get_target_property(_tmp_poolSize "${target}" QT_WASM_PTHREAD_POOL_SIZE)
if(_tmp_poolSize)
set(POOL_SIZE ${_tmp_poolSize})
elseif(DEFINED QT_WASM_PTHREAD_POOL_SIZE)
set(POOL_SIZE ${QT_WASM_PTHREAD_POOL_SIZE})
endif()
target_link_options("${target}" PRIVATE "SHELL:-s PTHREAD_POOL_SIZE=${POOL_SIZE}")
message(DEBUG "Setting PTHREAD_POOL_SIZE to ${POOL_SIZE} for ${target}")
endif()

# Hardcode wasm memory size. Emscripten does not currently support memory growth
# (ALLOW_MEMORY_GROWTH) in pthreads mode, and requires specifying the memory size
# at build time. Further, browsers limit the maximum initial memory size to 1GB.
# QT_WASM_INITIAL_MEMORY must be a multiple of 64KB (i.e. 65536)
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)
set(QT_WASM_INITIAL_MEMORY "1GB")
else()
# emscripten default is 16MB, we need slightly more sometimes
set(QT_WASM_INITIAL_MEMORY "20MB")
endif()
endif()

if(DEFINED QT_WASM_INITIAL_MEMORY)
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}")
endif()

endif()
endfunction()

46 changes: 46 additions & 0 deletions src/corelib/doc/src/cmake/cmake-properties.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,49 @@ the property value overrides the runtime path where the resource file is found.

\sa{The Qt Resource System}
*/

/*!
\page cmake-target-property-QT_WASM_PTHREAD_POOL_SIZE.html
\ingroup cmake-properties-qtcore
\ingroup cmake-target-properties-qtcore

\title QT_WASM_PTHREAD_POOL_SIZE
\target cmake-target-property-QT_WASM_PTHREAD_POOL_SIZE

\summary {Internal WebAssembly thread pool size.}

\cmakepropertysince 6.2.4
\preliminarycmakeproperty
\cmakepropertywebassemblyonly

Specifies the number of web workers (threads) to create at application startup.
Qt allocates a pool size of 4 by default. This means the app can use
4 additional threads besides the main thread, without the additional overhead
of creating a new web worker, which may deadlock if the main thread created it
and join()s the thread without returning control to the event loop first.
Translates into the Emscripten compiler setting of PTHREAD_POOL_SIZE.

For more information, see \l{https://emscripten.org/docs/porting/pthreads.html}{Pthreads support}.
*/

/*!
\page cmake-target-property-QT_WASM_INITIAL_MEMORY.html
\ingroup cmake-properties-qtcore
\ingroup cmake-target-properties-qtcore

\title QT_WASM_INITIAL_MEMORY
\target cmake-target-property-QT_WASM_INITIAL_MEMORY

\summary {Internal WebAssembly initial memory.}

\cmakepropertysince 6.2.4
\preliminarycmakeproperty
\cmakepropertywebassemblyonly

Specifies the initial amount of memory to use, in bytes. Using more will cause the
browser to copy the old heap into a new one. Translates into the
Emscripten compiler setting of INITIAL_MEMORY.
QT_WASM_INITIAL_MEMORY must be a multiple of 65536 bytes.

For more information, see \l{https://github.com/emscripten-core/emscripten/blob/main/src/settings.js}{Emscripten compiler settings}.
*/

0 comments on commit af36dc7

Please sign in to comment.