summaryrefslogtreecommitdiffstats
path: root/toolkit/components/ml/content/Utils.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:35:37 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:35:37 +0000
commita90a5cba08fdf6c0ceb95101c275108a152a3aed (patch)
tree532507288f3defd7f4dcf1af49698bcb76034855 /toolkit/components/ml/content/Utils.sys.mjs
parentAdding debian version 126.0.1-1. (diff)
downloadfirefox-a90a5cba08fdf6c0ceb95101c275108a152a3aed.tar.xz
firefox-a90a5cba08fdf6c0ceb95101c275108a152a3aed.zip
Merging upstream version 127.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/ml/content/Utils.sys.mjs')
-rw-r--r--toolkit/components/ml/content/Utils.sys.mjs77
1 files changed, 77 insertions, 0 deletions
diff --git a/toolkit/components/ml/content/Utils.sys.mjs b/toolkit/components/ml/content/Utils.sys.mjs
new file mode 100644
index 0000000000..b3a25e84d7
--- /dev/null
+++ b/toolkit/components/ml/content/Utils.sys.mjs
@@ -0,0 +1,77 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+/**
+ * Converts an ArrayBuffer to a Blob URL.
+ *
+ * @param {ArrayBuffer} buffer - The ArrayBuffer to convert.
+ * @returns {string} The Blob URL.
+ */
+export function arrayBufferToBlobURL(buffer) {
+ let blob = new Blob([buffer], { type: "application/wasm" });
+ return URL.createObjectURL(blob);
+}
+
+/**
+ * Validate some simple Wasm that uses a SIMD operation.
+ */
+function detectSimdSupport() {
+ return WebAssembly.validate(
+ new Uint8Array(
+ // ```
+ // ;; Detect SIMD support.
+ // ;; Compile by running: wat2wasm --enable-all simd-detect.wat
+ //
+ // (module
+ // (func (result v128)
+ // i32.const 0
+ // i8x16.splat
+ // i8x16.popcnt
+ // )
+ // )
+ // ```
+
+ // prettier-ignore
+ [
+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, 0x00,
+ 0x01, 0x7b, 0x03, 0x02, 0x01, 0x00, 0x0a, 0x0a, 0x01, 0x08, 0x00, 0x41, 0x00,
+ 0xfd, 0x0f, 0xfd, 0x62, 0x0b
+ ]
+ )
+ );
+}
+
+let cachedRuntimeWasmFilename = null;
+
+/**
+ * Determines the appropriate WebAssembly (Wasm) filename based on the runtime capabilities of the browser.
+ * This function considers both SIMD and multi-threading support.
+ * It returns a filename that matches the browser's capabilities, ensuring the most optimized version of the Wasm file is used.
+ *
+ * The result is cached to avoid re-computation.
+ *
+ * @param {Window|null} browsingContext - The browsing context to use for feature detection.
+ * @returns {string} The filename of the Wasm file best suited for the current browser's capabilities.
+ */
+export function getRuntimeWasmFilename(browsingContext = null) {
+ if (cachedRuntimeWasmFilename != null) {
+ return cachedRuntimeWasmFilename;
+ }
+
+ // The cross-origin isolation flag is used to determine if we have multi-threading support.
+ const hasMultiThreadSupport = browsingContext
+ ? browsingContext.crossOriginIsolated
+ : false;
+
+ let res;
+ if (detectSimdSupport()) {
+ res = hasMultiThreadSupport
+ ? "ort-wasm-simd-threaded.wasm"
+ : "ort-wasm-simd.wasm";
+ } else {
+ res = hasMultiThreadSupport ? "ort-wasm-threaded.wasm" : "ort-wasm.wasm";
+ }
+ cachedRuntimeWasmFilename = res;
+ return res;
+}