Skip to content

Commit aa93d18

Browse files
committed
[web] enable native EH for JSEP/Asyncify build
1 parent 0a478c0 commit aa93d18

File tree

9 files changed

+54
-22
lines changed

9 files changed

+54
-22
lines changed

.github/workflows/linux-wasm-ci-build-and-test-workflow.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ jobs:
9595
--use_jsep \
9696
--use_webnn \
9797
--target onnxruntime_webassembly \
98-
${{ inputs.build_config == 'Release' && '--enable_wasm_api_exception_catching' || '' }} \
98+
--enable_wasm_eh \
9999
--skip_tests
100100
working-directory: ${{ github.workspace }}
101101

@@ -108,7 +108,7 @@ jobs:
108108
--use_webgpu \
109109
--use_webnn \
110110
--target onnxruntime_webassembly \
111-
${{ inputs.build_config == 'Release' && '--enable_wasm_api_exception_catching' || '' }} \
111+
--enable_wasm_eh \
112112
--skip_tests
113113
working-directory: ${{ github.workspace }}
114114

@@ -121,6 +121,7 @@ jobs:
121121
--use_webgpu \
122122
--use_webnn \
123123
--enable_wasm_jspi \
124+
--enable_wasm_eh \
124125
--target onnxruntime_webassembly \
125126
--skip_tests
126127
working-directory: ${{ github.workspace }}

cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ option(onnxruntime_ENABLE_WEBASSEMBLY_THREADS "Enable this option to create WebA
202202
option(onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_CATCHING "Enable this option to turn on exception catching" OFF)
203203
option(onnxruntime_ENABLE_WEBASSEMBLY_API_EXCEPTION_CATCHING "Enable this option to turn on api exception catching" OFF)
204204
option(onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_THROWING "Enable this option to turn on exception throwing even if the build disabled exceptions support" OFF)
205+
option(onnxruntime_ENABLE_WEBASSEMBLY_NATIVE_EH "Enable native WebAssembly exception handling (-fwasm-exceptions) instead of legacy JavaScript-based exception support" OFF)
205206
option(onnxruntime_WEBASSEMBLY_RUN_TESTS_IN_BROWSER "Enable this option to run tests in browser instead of Node.js" OFF)
206207
option(onnxruntime_ENABLE_WEBASSEMBLY_DEBUG_INFO "Enable this option to turn on DWARF format debug info" OFF)
207208
option(onnxruntime_ENABLE_WEBASSEMBLY_PROFILING "Enable this option to turn on WebAssembly profiling and preserve function names" OFF)

cmake/adjust_global_compile_flags.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
4646
endif()
4747

4848
# Enable WebAssembly exception catching.
49-
if (onnxruntime_ENABLE_WEBASSEMBLY_JSPI)
49+
if (onnxruntime_ENABLE_WEBASSEMBLY_NATIVE_EH)
5050
string(APPEND CMAKE_C_FLAGS " -fwasm-exceptions -s WASM_LEGACY_EXCEPTIONS=0")
5151
string(APPEND CMAKE_CXX_FLAGS " -fwasm-exceptions -s WASM_LEGACY_EXCEPTIONS=0")
5252
elseif (onnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_CATCHING)

cmake/onnxruntime_webassembly.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ else()
329329
endif()
330330
endif()
331331

332-
if (NOT onnxruntime_ENABLE_WEBASSEMBLY_JSPI)
332+
if (NOT onnxruntime_ENABLE_WEBASSEMBLY_NATIVE_EH)
333333
# Set link flag to enable exceptions support, this will override default disabling exception throwing behavior when disable exceptions.
334334
target_link_options(onnxruntime_webassembly PRIVATE
335335
"SHELL:-s DISABLE_EXCEPTION_THROWING=0"

js/build_jsep.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if ["%~1"]==["d"] (
2222
)
2323
if ["%~1"]==["r"] (
2424
set CONFIG=Release
25-
set CONFIG_EXTRA_FLAG=--enable_wasm_api_exception_catching --disable_rtti --enable_wasm_profiling
25+
set CONFIG_EXTRA_FLAG=--disable_rtti --enable_wasm_profiling
2626
goto :arg2
2727
)
2828
echo Invalid configuration "%~1", must be "d"(Debug) or "r"(Release)
@@ -64,7 +64,7 @@ popd
6464

6565
set PATH=C:\Program Files\Git\usr\bin;%PATH%
6666

67-
call %ROOT%build.bat --config %CONFIG% %CONFIG_EXTRA_FLAG% --skip_submodule_sync --build_wasm --skip_tests^
67+
call %ROOT%build.bat --config %CONFIG% %CONFIG_EXTRA_FLAG% --skip_submodule_sync --build_wasm --skip_tests --enable_wasm_eh^
6868
--enable_wasm_simd --enable_wasm_threads --use_jsep --use_webnn --target onnxruntime_webassembly --build_dir %BUILD_DIR%
6969

7070
IF NOT "%ERRORLEVEL%" == "0" (

tools/ci_build/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ def generate_build_tree(
501501
+ ("ON" if args.enable_wasm_api_exception_catching else "OFF"),
502502
"-Donnxruntime_ENABLE_WEBASSEMBLY_EXCEPTION_THROWING="
503503
+ ("ON" if args.enable_wasm_exception_throwing_override else "OFF"),
504+
"-Donnxruntime_ENABLE_WEBASSEMBLY_NATIVE_EH=" + ("ON" if args.enable_wasm_eh else "OFF"),
504505
"-Donnxruntime_WEBASSEMBLY_RUN_TESTS_IN_BROWSER=" + ("ON" if args.wasm_run_tests_in_browser else "OFF"),
505506
"-Donnxruntime_ENABLE_WEBASSEMBLY_JSPI=" + ("ON" if args.enable_wasm_jspi else "OFF"),
506507
"-Donnxruntime_ENABLE_WEBASSEMBLY_THREADS=" + ("ON" if args.enable_wasm_threads else "OFF"),
@@ -619,7 +620,7 @@ def generate_build_tree(
619620
build_dir,
620621
configs,
621622
emscripten_root_path,
622-
args.enable_wasm_jspi,
623+
args.enable_wasm_eh,
623624
not args.disable_rtti,
624625
not args.disable_wasm_exception_catching,
625626
args.minimal_build is not None,

tools/ci_build/build_args.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ def add_webassembly_args(parser: argparse.ArgumentParser) -> None:
390390
action="store_true",
391391
help="Override default behavior to allow throwing exceptions even when catching is generally disabled.",
392392
)
393+
parser.add_argument(
394+
"--enable_wasm_eh",
395+
action="store_true",
396+
help="Use native WebAssembly exception handling (-fwasm-exceptions) instead of legacy JavaScript-based exception support.",
397+
)
393398
parser.add_argument("--wasm_run_tests_in_browser", action="store_true", help="Run WASM tests in a browser.")
394399
parser.add_argument(
395400
"--enable_wasm_profiling", action="store_true", help="Enable WASM profiling and preserve function names."
@@ -935,7 +940,30 @@ def convert_arg_line_to_args(self, arg_line: str) -> list[str]: # Use list[str]
935940
if args.android_ndk_path:
936941
args.android_ndk_path = os.path.normpath(args.android_ndk_path)
937942

943+
if args.enable_wasm_jspi and not args.enable_wasm_eh:
944+
raise Exception(
945+
"'--enable_wasm_jspi' requires '--enable_wasm_eh' to be specified. "
946+
"Currently we only support JSPI builds with native exception handling enabled."
947+
)
948+
938949
# Handle WASM exception logic
950+
if args.enable_wasm_eh:
951+
# Native Wasm EH is incompatible with legacy exception flags
952+
if args.disable_wasm_exception_catching:
953+
raise Exception(
954+
"'--enable_wasm_eh' cannot be used with '--disable_wasm_exception_catching'. "
955+
"Native Wasm EH replaces the legacy exception catching mechanism."
956+
)
957+
if args.enable_wasm_api_exception_catching:
958+
raise Exception(
959+
"'--enable_wasm_eh' cannot be used with '--enable_wasm_api_exception_catching'. "
960+
"Native Wasm EH replaces the legacy API-level exception catching."
961+
)
962+
if args.enable_wasm_exception_throwing_override:
963+
raise Exception(
964+
"'--enable_wasm_eh' cannot be used with '--enable_wasm_exception_throwing_override'. "
965+
"Native Wasm EH handles exception throwing natively."
966+
)
939967
if args.enable_wasm_api_exception_catching:
940968
args.disable_wasm_exception_catching = True # Catching at API level implies disabling broader catching
941969
if not args.disable_wasm_exception_catching or args.enable_wasm_api_exception_catching:

tools/ci_build/github/azure-pipelines/templates/linux-wasm-ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
buildArch: x64
5454
CommonBuildArgs: '--parallel --config ${{ parameters.BuildConfig }} --skip_submodule_sync --build_wasm --enable_wasm_simd --enable_wasm_threads ${{ parameters.ExtraBuildArgs }}'
5555
${{ if eq(parameters.BuildConfig, 'Release') }}:
56-
# Add '--enable_wasm_api_exception_catching' only for non-JSPI Release build
56+
# Add '--enable_wasm_api_exception_catching' only for basic Release build
5757
ExceptionHandlingBuildArgs: '--enable_wasm_api_exception_catching'
5858
${{ else }}:
5959
ExceptionHandlingBuildArgs: ''
@@ -128,7 +128,7 @@ jobs:
128128
${{ else }}:
129129
AdditionalKey: wasm_inferencing_jsep | ${{ parameters.BuildConfig }}
130130
CacheDir: $(ORT_CACHE_DIR)/wasm_inferencing_jsep
131-
Arguments: '$(CommonBuildArgs) --build_dir $(Build.BinariesDirectory)/wasm_inferencing_jsep --use_jsep --use_webnn --target onnxruntime_webassembly $(ExceptionHandlingBuildArgs) --skip_tests'
131+
Arguments: '$(CommonBuildArgs) --build_dir $(Build.BinariesDirectory)/wasm_inferencing_jsep --use_jsep --use_webnn --target onnxruntime_webassembly --enable_wasm_eh --skip_tests'
132132
DisplayName: 'Build (simd + threads + JSEP)'
133133
WithCache: ${{ parameters.WithCache }}
134134

@@ -141,7 +141,7 @@ jobs:
141141
${{ else }}:
142142
AdditionalKey: wasm_inferencing_webgpu | ${{ parameters.BuildConfig }}
143143
CacheDir: $(ORT_CACHE_DIR)/wasm_inferencing_webgpu
144-
Arguments: '$(CommonBuildArgs) --build_dir $(Build.BinariesDirectory)/wasm_inferencing_webgpu --use_webgpu --use_webnn --target onnxruntime_webassembly $(ExceptionHandlingBuildArgs) --skip_tests'
144+
Arguments: '$(CommonBuildArgs) --build_dir $(Build.BinariesDirectory)/wasm_inferencing_webgpu --use_webgpu --use_webnn --target onnxruntime_webassembly --enable_wasm_eh --skip_tests'
145145
DisplayName: 'Build (simd + threads + WebGPU experimental)'
146146
WithCache: ${{ parameters.WithCache }}
147147

@@ -154,7 +154,7 @@ jobs:
154154
${{ else }}:
155155
AdditionalKey: wasm_inferencing_webgpu_jspi | ${{ parameters.BuildConfig }}
156156
CacheDir: $(ORT_CACHE_DIR)/wasm_inferencing_webgpu_jspi
157-
Arguments: '$(CommonBuildArgs) --build_dir $(Build.BinariesDirectory)/wasm_inferencing_webgpu_jspi --use_webgpu --use_webnn --enable_wasm_jspi --target onnxruntime_webassembly --skip_tests'
157+
Arguments: '$(CommonBuildArgs) --build_dir $(Build.BinariesDirectory)/wasm_inferencing_webgpu_jspi --use_webgpu --use_webnn --enable_wasm_jspi --enable_wasm_eh --target onnxruntime_webassembly --skip_tests'
158158
DisplayName: 'Build (simd + threads + WebGPU experimental, JSPI)'
159159
WithCache: ${{ parameters.WithCache }}
160160

tools/python/util/vcpkg_helpers.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def generate_vcpkg_triplets_for_emscripten(
476476
configs: set[str],
477477
emscripten_root: str,
478478
# Parameters defining the specific build configuration
479-
enable_jspi: bool,
479+
enable_wasm_eh: bool, # Controls native Wasm EH (-fwasm-exceptions)
480480
enable_rtti: bool,
481481
enable_wasm_exception_catching: bool, # Controls -sDISABLE_EXCEPTION_CATCHING=...
482482
enable_minimal_onnx_build: bool, # Controls ONNX port setting AND C++ exceptions (-fno-exceptions)
@@ -493,21 +493,21 @@ def generate_vcpkg_triplets_for_emscripten(
493493
- If enable_minimal_onnx_build=False, C++ exceptions are assumed enabled (-fexceptions).
494494
495495
This supports 4 main effective EH scenarios depending on the combination of
496-
'enable_minimal_onnx_build', 'enable_jspi' and 'enable_wasm_exception_catching':
496+
'enable_minimal_onnx_build', 'enable_wasm_eh' and 'enable_wasm_exception_catching':
497497
1. No EH (-fno-exceptions, -sDISABLE_EXCEPTION_CATCHING=1):
498498
Set enable_minimal_onnx_build=True, enable_wasm_exception_catching=False
499499
2. Full EH (-fexceptions, -sDISABLE_EXCEPTION_CATCHING=0):
500500
Set enable_minimal_onnx_build=False, enable_wasm_exception_catching=True
501501
3. Throw Only EH (-fexceptions, -sDISABLE_EXCEPTION_CATCHING=1):
502502
Set enable_minimal_onnx_build=False, enable_wasm_exception_catching=False
503-
4. Use the new Wasm EH (-fwasm-exceptions -sWASM_LEGACY_EXCEPTIONS=0):
504-
Set enable_minimal_onnx_build=False, enable_jspi=True
503+
4. The new Wasm native EH (-fwasm-exceptions -sWASM_LEGACY_EXCEPTIONS=0):
504+
Set enable_minimal_onnx_build=False, enable_wasm_eh=True
505505
506506
Args:
507507
build_dir (str): The directory to save the generated triplet files.
508508
emscripten_root (str): The root path of Emscripten.
509-
enable_jspi (bool): Flag indicating if JSPI is enabled. If JSPI is enabled, the new
510-
Wasm EH will be used and enable_wasm_exception_catching is ignored.
509+
enable_wasm_eh (bool): Flag indicating if native Wasm EH should be used.
510+
If True, uses -fwasm-exceptions -sWASM_LEGACY_EXCEPTIONS=0.
511511
enable_rtti (bool): Flag indicating if RTTI is enabled for dependencies.
512512
enable_wasm_exception_catching (bool): Flag indicating if the Emscripten runtime
513513
exception catching mechanism should be enabled
@@ -525,11 +525,12 @@ def generate_vcpkg_triplets_for_emscripten(
525525
# Derive C++ exception enablement from the minimal build flag
526526
cpp_exceptions_enabled = not enable_minimal_onnx_build
527527

528-
# When JSPI is enabled, use the new Wasm EH
529-
if enable_jspi:
528+
# When native Wasm EH is enabled, ensure minimal build is not used
529+
if enable_wasm_eh:
530530
if enable_minimal_onnx_build:
531-
# TODO: support minimal build with JSPI if needed
532-
raise ValueError("Currently minimal build cannot be used with JSPI.")
531+
raise ValueError(
532+
"Using minimal build means C++ exceptions are disabled, which is incompatible with enabling native Wasm EH."
533+
)
533534

534535
for target_abi in ["wasm32", "wasm64"]:
535536
os_name = "emscripten"
@@ -574,7 +575,7 @@ def generate_vcpkg_triplets_for_emscripten(
574575

575576
# Wasm Exception Catching Runtime (-s flag, apply to Base and Linker flags)
576577
exception_catching_flag = ""
577-
if enable_jspi:
578+
if enable_wasm_eh:
578579
exception_catching_flag = "-fwasm-exceptions -sWASM_LEGACY_EXCEPTIONS=0"
579580
elif enable_wasm_exception_catching:
580581
exception_catching_flag = "-sDISABLE_EXCEPTION_CATCHING=0"

0 commit comments

Comments
 (0)