Skip to content

Commit

Permalink
Base commit for using EMSDK 3.1.74 (LTO disabled)
Browse files Browse the repository at this point in the history
  • Loading branch information
fs-eire committed Jan 10, 2025
1 parent 1ce5957 commit a85fe10
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
[submodule "cmake/external/emsdk"]
path = cmake/external/emsdk
url = https://github.com/emscripten-core/emsdk.git
branch = 3.1.59
branch = 3.1.74
2 changes: 1 addition & 1 deletion cgmanifests/generated/cgmanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"component": {
"type": "git",
"git": {
"commitHash": "d52c46520124845b1e0e0525f2759299d840143f",
"commitHash": "3d6d8ee910466516a53e665b86458faa81dae9ba",
"repositoryUrl": "https://github.com/emscripten-core/emsdk.git"
},
"comments": "git submodule at cmake/external/emsdk"
Expand Down
4 changes: 4 additions & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ else()
check_cxx_compiler_flag(-Wdeprecated-builtins HAS_DEPRECATED_BUILTINS)
check_cxx_compiler_flag(-Wdeprecated-copy HAS_DEPRECATED_COPY)
check_cxx_compiler_flag(-Wdeprecated-declarations HAS_DEPRECATED_DECLARATIONS)
check_cxx_compiler_flag(-Wdeprecated-literal-operator HAS_DEPRECATED_LITERAL_OPERATOR)
check_cxx_compiler_flag(-Wdeprecated-this-capture HAS_DEPRECATED_THIS_CAPTURE)
check_cxx_compiler_flag(-Wenum-constexpr-conversion HAS_ENUM_CONSTEXPR_CONVERSION)
check_cxx_compiler_flag(-Wformat-truncation HAS_FORMAT_TRUNCATION)
Expand Down Expand Up @@ -735,6 +736,9 @@ else()
if (HAS_DEPRECATED_BUILTINS)
list(APPEND ORT_WARNING_FLAGS -Wno-deprecated-builtins)
endif()
if (HAS_DEPRECATED_LITERAL_OPERATOR)
list(APPEND ORT_WARNING_FLAGS -Wno-deprecated-literal-operator)
endif()
#see:https://reviews.llvm.org/D131307
#It was intended that the 'enum-constexpr-conversion' type warnings can not be silenced by -w
if(HAS_ENUM_CONSTEXPR_CONVERSION AND NOT Protobuf_FOUND)
Expand Down
4 changes: 2 additions & 2 deletions cmake/adjust_global_compile_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
# (2) "-flto=thin" does not work correctly for wasm-ld.
# we don't set onnxruntime_ENABLE_LTO because it appends flag "-flto=thin"
# instead, we manually set CMAKE_CXX_FLAGS "-flto"
string(APPEND CMAKE_C_FLAGS " -flto")
string(APPEND CMAKE_CXX_FLAGS " -flto")
#string(APPEND CMAKE_C_FLAGS " -flto=thin")
#string(APPEND CMAKE_CXX_FLAGS " -flto=thin")
endif()

if (onnxruntime_ENABLE_WEBASSEMBLY_DEBUG_INFO)
Expand Down
2 changes: 1 addition & 1 deletion cmake/external/emsdk
Submodule emsdk updated 43 files
+31 −37 .circleci/config.yml
+17 −17 .flake8
+39 −0 .github/workflows/create-release.yml
+78 −0 .github/workflows/tag-release.yml
+4 −3 README.md
+35 −9 bazel/README.md
+23 −6 bazel/deps.bzl
+19 −17 bazel/emscripten_deps.bzl
+3 −3 bazel/emscripten_toolchain/BUILD.bazel
+0 −0 bazel/emscripten_toolchain/default_config
+7 −6 bazel/emscripten_toolchain/link_wrapper.py
+13 −3 bazel/emscripten_toolchain/toolchain.bzl
+22 −13 bazel/emscripten_toolchain/wasm_cc_binary.bzl
+105 −0 bazel/revisions.bzl
+1 −0 bazel/test_secondary_lto_cache/.bazelrc
+4 −0 bazel/test_secondary_lto_cache/.gitignore
+19 −0 bazel/test_secondary_lto_cache/BUILD
+1 −0 bazel/test_secondary_lto_cache/MODULE.bazel
+30 −0 bazel/test_secondary_lto_cache/WORKSPACE
+6 −0 bazel/test_secondary_lto_cache/hello-world.cc
+95 −1 bazel/toolchains.bzl
+2 −2 docker/Dockerfile
+31 −1 emscripten-releases-tags.json
+1 −11 emsdk
+0 −21 emsdk.bat
+1 −9 emsdk.ps1
+67 −35 emsdk.py
+2 −0 emsdk_env.fish
+30 −324 emsdk_manifest.json
+16 −8 scripts/create_release.py
+44 −0 scripts/get_emscripten_revision_info.py
+25 −0 scripts/get_release_info.py
+1 −1 scripts/update_bazel_workspace.py
+15 −11 scripts/update_node.py
+12 −22 scripts/update_python.py
+19 −0 scripts/zip.py
+1 −1 test/test.bat
+7 −7 test/test.py
+0 −7 test/test_activation.ps1
+6 −0 test/test_bazel.ps1
+7 −6 test/test_bazel.sh
+7 −6 test/test_bazel_mac.sh
+0 −3 test/test_path_preservation.ps1
1 change: 1 addition & 0 deletions cmake/onnxruntime_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#cmakedefine HAS_CLASS_MEMACCESS
#cmakedefine HAS_DEPRECATED_COPY
#cmakedefine HAS_DEPRECATED_DECLARATIONS
#cmakedefine HAS_DEPRECATED_LITERAL_OPERATOR
#cmakedefine HAS_DEPRECATED_THIS_CAPTURE
#cmakedefine HAS_FORMAT_TRUNCATION
#cmakedefine HAS_IGNORED_ATTRIBUTES
Expand Down
8 changes: 0 additions & 8 deletions cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,11 @@ function(AddTest)
)
else()
set(TEST_NODE_FLAGS)
if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
list(APPEND TEST_NODE_FLAGS "--experimental-wasm-threads")
endif()
if (onnxruntime_ENABLE_WEBASSEMBLY_SIMD)
list(APPEND TEST_NODE_FLAGS "--experimental-wasm-simd")
endif()

# prefer Node from emsdk so the version is more deterministic
if (DEFINED ENV{EMSDK_NODE})
set(NODE_EXECUTABLE $ENV{EMSDK_NODE})
else()
# warning as we don't know what node version is being used and whether things like the TEST_NODE_FLAGS
# will be valid. e.g. "--experimental-wasm-simd" is not valid with node v20 or later.
message(WARNING "EMSDK_NODE environment variable was not set. Falling back to system `node`.")
set(NODE_EXECUTABLE node)
endif()
Expand Down
64 changes: 62 additions & 2 deletions cmake/onnxruntime_webassembly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,15 @@ jsepDownload:_pp_")
"SHELL:--pre-js \"${ONNXRUNTIME_ROOT}/wasm/pre-jsep.js\""
"SHELL:-s ASYNCIFY=1"
"SHELL:-s ASYNCIFY_STACK_SIZE=65536"
"SHELL:-s ASYNCIFY_EXPORTS=['OrtRun']"
"SHELL:-s ASYNCIFY_IMPORTS=['Module.jsepCopy','Module.jsepCopyAsync','jsepDownload']"
)
set_target_properties(onnxruntime_webassembly PROPERTIES LINK_DEPENDS ${ONNXRUNTIME_ROOT}/wasm/pre-jsep.js)

if (onnxruntime_ENABLE_WEBASSEMBLY_MEMORY64)
target_link_options(onnxruntime_webassembly PRIVATE
"SHELL:-s ASYNCIFY_EXPORTS=['OrtRun']"
"SHELL:-s ASYNCIFY_IMPORTS=['Module.jsepCopy','Module.jsepCopyAsync','jsepDownload']"
)
endif()
endif()

if (onnxruntime_EMSCRIPTEN_SETTINGS)
Expand Down Expand Up @@ -461,4 +466,59 @@ jsepDownload:_pp_")
endif()

set_target_properties(onnxruntime_webassembly PROPERTIES OUTPUT_NAME ${target_name} SUFFIX ".mjs")

#
# The following POST_BUILD script is a workaround for enabling:
# - using onnxruntime-web with Multi-threading enabled when import from CDN
# - using onnxruntime-web when consumed in some frameworks like Vite
#
# In the use case mentioned above, the file name of the script may be changed. So we need to replace the line:
# `new Worker(new URL("ort-wasm-*.mjs", import.meta.url),`
# with
# `new Worker(new URL(import.meta.url),`
#
# This behavior is introduced in https://github.com/emscripten-core/emscripten/pull/22165. Since it's unlikely to be
# reverted, and there is no config to disable this behavior, we have to use a post-build script to workaround it.
#

# Generate a script to do the post-build work
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/wasm_post_build.js "
const fs = require('fs');
const path = require('path');
// node wasm_post_build.js <mjsFilePath>
const mjsFilePath = process.argv[2];
let contents = fs.readFileSync(mjsFilePath).toString();
const regex = 'new Worker\\\\(new URL\\\\(\".+?\", ?import\\\\.meta\\\\.url\\\\),';
const matches = [...contents.matchAll(new RegExp(regex, 'g'))];
if (matches.length !== 1) {
throw new Error(
`Unexpected number of matches for \"${regex}\" in \"${filepath}\": ${matches.length}.`,
);
}
// Replace the only occurrence.
contents = contents.replace(
new RegExp(regex),
`new Worker(new URL(import.meta.url),`,
);
fs.writeFileSync(mjsFilePath, contents);
"
)

find_program(NODE_EXECUTABLE node required)
if (NOT NODE_EXECUTABLE)
message(FATAL_ERROR "Node is required to run the post-build script")
endif()

add_custom_command(
TARGET onnxruntime_webassembly
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "Backup file at $<TARGET_FILE_NAME:onnxruntime_webassembly>.bak"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE_NAME:onnxruntime_webassembly>" "$<TARGET_FILE_NAME:onnxruntime_webassembly>.bak"
COMMAND ${CMAKE_COMMAND} -E echo "Performing workaround for $<TARGET_FILE_NAME:onnxruntime_webassembly>"
COMMAND ${NODE_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR}/wasm_post_build.js" "$<TARGET_FILE_NAME:onnxruntime_webassembly>"
)
endif()
8 changes: 4 additions & 4 deletions include/onnxruntime/core/framework/float16.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,19 @@ struct BFloat16 : onnxruntime_float16::BFloat16Impl<BFloat16> {
// initializers with MLFloat16 and BFloat16 from unsigned short
// E.g 10_f16 or 10_b16
#if !defined(__CUDACC__) && !defined(__HIPCC__)
inline MLFloat16 operator"" _f16(unsigned long long int v) noexcept {
inline MLFloat16 operator""_f16(unsigned long long int v) noexcept {

Check warning on line 264 in include/onnxruntime/core/framework/float16.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4] Raw Output: include/onnxruntime/core/framework/float16.h:264: Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]
return MLFloat16::FromBits(narrow<uint16_t>(v));
}

inline MLFloat16 operator"" _fp16(long double v) noexcept {
inline MLFloat16 operator""_fp16(long double v) noexcept {
return MLFloat16(static_cast<float>(v));
}

inline BFloat16 operator"" _b16(unsigned long long int v) noexcept {
inline BFloat16 operator""_b16(unsigned long long int v) noexcept {

Check warning on line 272 in include/onnxruntime/core/framework/float16.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4] Raw Output: include/onnxruntime/core/framework/float16.h:272: Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]
return BFloat16::FromBits((narrow<uint16_t>(v)));
}

inline BFloat16 operator"" _bfp16(long double v) noexcept {
inline BFloat16 operator""_bfp16(long double v) noexcept {
return BFloat16(static_cast<float>(v));
}
#endif
Expand Down
16 changes: 8 additions & 8 deletions include/onnxruntime/core/framework/float8.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ inline ORT_HOST_DEVICE bool operator<(const Float8E4M3FN& left, const Float8E4M3
// initializers with MLFloat8E4M3FN and Float8E4M3FN from unsigned char
#if !defined(__CUDACC__) && !defined(__HIPCC__)

inline Float8E4M3FN operator"" _f8e4m3fn(unsigned long long int v) {
inline Float8E4M3FN operator""_f8e4m3fn(unsigned long long int v) {

Check warning on line 168 in include/onnxruntime/core/framework/float8.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4] Raw Output: include/onnxruntime/core/framework/float8.h:168: Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]
return Float8E4M3FN(narrow<uint8_t>(v), Float8E4M3FN::FromBits());
}

inline Float8E4M3FN operator"" _f8e4m3fnp8(long double v) {
inline Float8E4M3FN operator""_f8e4m3fnp8(long double v) {
return Float8E4M3FN(static_cast<float>(v), true);
}

Expand Down Expand Up @@ -323,11 +323,11 @@ inline ORT_HOST_DEVICE bool operator<(const Float8E4M3FNUZ& left, const Float8E4
// initializers with MLFloat8E4M3FN and Float8E4M3FN from unsigned char
#if !defined(__CUDACC__) && !defined(__HIPCC__)

inline Float8E4M3FNUZ operator"" _f8e4m3p8fnuz(unsigned long long int v) {
inline Float8E4M3FNUZ operator""_f8e4m3p8fnuz(unsigned long long int v) {

Check warning on line 326 in include/onnxruntime/core/framework/float8.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4] Raw Output: include/onnxruntime/core/framework/float8.h:326: Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]
return Float8E4M3FNUZ(narrow<uint8_t>(v), Float8E4M3FNUZ::FromBits());
}

inline Float8E4M3FNUZ operator"" _f8e4m3fnuzp8(long double v) {
inline Float8E4M3FNUZ operator""_f8e4m3fnuzp8(long double v) {
return Float8E4M3FNUZ(static_cast<float>(v), true);
}

Expand Down Expand Up @@ -493,11 +493,11 @@ inline ORT_HOST_DEVICE bool operator<(const Float8E5M2& left, const Float8E5M2&
// initializers with MLFloat8E5M2 and Float8E5M2 from unsigned char
#if !defined(__CUDACC__) && !defined(__HIPCC__)

inline Float8E5M2 operator"" _f8e5m2fn(unsigned long long int v) {
inline Float8E5M2 operator""_f8e5m2fn(unsigned long long int v) {

Check warning on line 496 in include/onnxruntime/core/framework/float8.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4] Raw Output: include/onnxruntime/core/framework/float8.h:496: Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]
return Float8E5M2(narrow<uint8_t>(v), Float8E5M2::FromBits());
}

inline Float8E5M2 operator"" _f8e5m2fnp8(long double v) {
inline Float8E5M2 operator""_f8e5m2fnp8(long double v) {
return Float8E5M2(static_cast<float>(v), true);
}

Expand Down Expand Up @@ -642,11 +642,11 @@ inline ORT_HOST_DEVICE bool operator<(const Float8E5M2FNUZ& left, const Float8E5
// initializers with MLFloat8E5M2 and Float8E5M2 from unsigned char
#if !defined(__CUDACC__) && !defined(__HIPCC__)

inline Float8E5M2FNUZ operator"" _f8e5m2fnuz(unsigned long long int v) {
inline Float8E5M2FNUZ operator""_f8e5m2fnuz(unsigned long long int v) {

Check warning on line 645 in include/onnxruntime/core/framework/float8.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4] Raw Output: include/onnxruntime/core/framework/float8.h:645: Use int16_t/int64_t/etc, rather than the C type long [runtime/int] [4]
return Float8E5M2FNUZ(narrow<uint8_t>(v), Float8E5M2FNUZ::FromBits());
}

inline Float8E5M2FNUZ operator"" _f8e5m2fnuzp8(long double v) {
inline Float8E5M2FNUZ operator""_f8e5m2fnuzp8(long double v) {
return Float8E5M2FNUZ(static_cast<float>(v), true);
}

Expand Down
14 changes: 7 additions & 7 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"mocha": "^11.0.1",
"npmlog": "^7.0.1",
"prettier": "^3.3.3",
"terser": "^5.31.0",
"terser": "^5.37.0",
"typescript": "^5.2.2"
},
"scripts": {
Expand Down
42 changes: 3 additions & 39 deletions js/web/script/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,51 +145,15 @@ async function minifyWasmModuleJsForBrowser(filepath: string): Promise<string> {
`new Worker(import.meta.url.startsWith('file:')?new URL(BUILD_DEFS.BUNDLE_FILENAME, import.meta.url):new URL(import.meta.url),`,
);

// Find the first and the only occurrence of minified function implementation of "_emscripten_thread_set_strongref":
// ```js
// _emscripten_thread_set_strongref: (thread) => {
// if (ENVIRONMENT_IS_NODE) {
// PThread.pthreads[thread].ref();
// }
// }
// ```
//
// It is minified to: (example)
// ```js
// function Pb(a){D&&N[a>>>0].ref()}
// ```

// The following code will look for the function name and mark the function call as pure, so that Terser will
// minify the code correctly.

const markedAsPure = [];
// First, try if we are working on the original (not minified) source file. This is when we are working with the
// debug build.
const isOriginal = contents.includes('PThread.pthreads[thread].ref()');
if (isOriginal) {
markedAsPure.push('PThread.pthreads[thread].ref');
} else {
// If it is not the original source file, we need to find the minified function call.
const matches = [...contents.matchAll(/\{[_a-zA-Z][_a-zA-Z0-9]*&&([_a-zA-Z][_a-zA-Z0-9]*\[.+?]\.ref)\(\)}/g)];
if (matches.length !== 1) {
throw new Error(
`Unexpected number of matches for minified "PThread.pthreads[thread].ref()" in "${filepath}": ${
matches.length
}.`,
);
}
// matches[0] is the first and the only match.
// matches[0][0] is the full matched string and matches[0][1] is the first capturing group.
markedAsPure.push(matches[0][1]);
}

// Use terser to minify the code with special configurations:
// - use `global_defs` to define `process` and `globalThis.process` as `undefined`, so terser can tree-shake the
// Node.js specific code.
const terser = await import('terser');
const result = await terser.minify(contents, {
module: true,
compress: {
passes: 2,
global_defs: { process: undefined, 'globalThis.process': undefined },
pure_funcs: markedAsPure,
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as ort from 'onnxruntime-web';

// The following line uses Vite's "Explicit URL Imports" feature to load the wasm files as asset.
// The following line uses Vite's "Explicit URL Imports" feature to load the wasm file as asset.
//
// see https://vite.dev/guide/assets.html#explicit-url-imports
//
import wasmFileUrl from '/node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm?url';
import mjsFileUrl from '/node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.mjs?url';

// wasmFileUrl is a string that contains the URL of the wasm file.
ort.env.wasm.wasmPaths = { wasm: wasmFileUrl, mjs: mjsFileUrl };
// wasmFileUrl is the URL of the wasm file. Vite will make sure it's available in both development and production.
ort.env.wasm.wasmPaths = { wasm: wasmFileUrl };

// Model data for "test_abs/model.onnx"
const testModelData =
Expand Down
2 changes: 1 addition & 1 deletion tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def convert_arg_line_to_args(self, arg_line):
# WebAssembly build
parser.add_argument("--build_wasm", action="store_true", help="Build for WebAssembly")
parser.add_argument("--build_wasm_static_lib", action="store_true", help="Build for WebAssembly static library")
parser.add_argument("--emsdk_version", default="3.1.59", help="Specify version of emsdk")
parser.add_argument("--emsdk_version", default="3.1.74", help="Specify version of emsdk")

parser.add_argument("--enable_wasm_simd", action="store_true", help="Enable WebAssembly SIMD")
parser.add_argument("--enable_wasm_threads", action="store_true", help="Enable WebAssembly multi-threads support")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ jobs:
- script: |
set -ex
cd '$(Build.SourcesDirectory)/cmake/external/emsdk'
./emsdk install 3.1.59 ccache-git-emscripten-64bit
./emsdk activate 3.1.59 ccache-git-emscripten-64bit
./emsdk install 3.1.74 ccache-git-emscripten-64bit
./emsdk activate 3.1.74 ccache-git-emscripten-64bit
displayName: 'emsdk install and activate ccache for emscripten'
- ${{if eq(parameters.WithCache, false)}}:
- script: |
set -ex
cd '$(Build.SourcesDirectory)/cmake/external/emsdk'
./emsdk install 3.1.59
./emsdk activate 3.1.59
./emsdk install 3.1.74
./emsdk activate 3.1.74
displayName: 'emsdk install and activate ccache for emscripten'
- template: build-linux-wasm-step.yml
Expand Down

0 comments on commit a85fe10

Please sign in to comment.