Skip to content

Commit 29540f2

Browse files
committed
deps: V8: cherry-pick 5f8109fccf06 and 7bd6db9dfd8a
Original commit messages: [wasm] Add WasmModuleObject::Compile overload with compile-time imports Expose a public API to compile a Wasm module with compile-time imports with a flag type for the builtins and a specifier name for imported string constants. The existing single-argument WasmModuleObject::Compile is refactored to delegate to a shared helper, and a new overload accepts a CompileTimeImports struct mirroring the `{ builtins, importedStringConstants }` constructor options. Bug: v8:14179 Change-Id: I9877e8ea4d620152e98954c948ff9dc5eeb6577c Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7970437 Reviewed-by: Leszek Swirski <leszeks@chromium.org> Commit-Queue: Jakob Kummerow <jkummerow@chromium.org> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org> Cr-Commit-Position: refs/heads/main@{#108394} [wasm] Generalize WasmModuleObject::Compile options with source URL Replaces the recently-added WasmModuleObject::CompileTimeImports struct with a CompileOptions struct carrying the compile-time import options plus a new source_url option, threading through to the existing source_url handling of SyncCompile for the script URL. This allows embedders compiling modules synchronously from bytes to attach a meaningful URL, as already possible for streaming compilation via WasmStreaming::SetUrl, for use in stack traces and developer tooling. Refactoring now to a single options struct mirrors the JS `WebAssembly.Module(bytes, compileOptions)` API, while enabling future compatibility. Change-Id: I4052f4a97e072467c00082df388e2eb4cb504cf6 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8127096 Commit-Queue: Dan Carney <dcarney@chromium.org> Reviewed-by: Dan Carney <dcarney@chromium.org> Reviewed-by: Leszek Swirski <leszeks@chromium.org> Reviewed-by: Jakob Kummerow <jkummerow@chromium.org> Cr-Commit-Position: refs/heads/main@{#109045} The two commits are squashed since the second replaces the struct introduced by the first. Adapted to MemorySpan (pre-std::span API) and includes the optional source_url parameter of WasmEngine::SyncCompile from c0f790f1379 that the second commit depends on. Refs: v8/v8@5f8109f Refs: v8/v8@7bd6db9
1 parent 20b3a69 commit 29540f2

8 files changed

Lines changed: 297 additions & 16 deletions

File tree

‎common.gypi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
# Reset this number to 0 on major V8 upgrades.
4545
# Increment by one for each non-official patch applied to deps/v8.
46-
'v8_embedder_string': '-node.34',
46+
'v8_embedder_string': '-node.35',
4747

4848
##### V8 defaults for Node.js #####
4949

‎deps/v8/include/v8-wasm.h‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <functional>
99
#include <memory>
1010
#include <string>
11+
#include <string_view>
1112
#include <variant>
1213

1314
#include "v8-internal.h" // NOLINT(build/include_directory)
@@ -106,12 +107,48 @@ class V8_EXPORT WasmModuleObject : public Object {
106107
*/
107108
CompiledWasmModule GetCompiledModule();
108109

110+
/**
111+
* Options that influence how a Wasm module is compiled. The compile-time
112+
* import options mirror those accepted by the JS `WebAssembly.Module`
113+
* constructor (`{ builtins, importedStringConstants }`).
114+
*/
115+
struct CompileOptions {
116+
// Builtin compile-time imports, mirroring the strings accepted in the
117+
// `builtins` array of the JS `WebAssembly.Module` constructor options.
118+
// Combine values with bitwise-or to enable multiple builtins.
119+
struct Builtins {
120+
enum {
121+
kNone = 0,
122+
kJsString = 1 << 0, // "js-string"
123+
};
124+
};
125+
// Bitwise-or of `Builtins` values to enable as compile-time imports.
126+
int builtins = Builtins::kNone;
127+
// If non-null, enable imported string constants from the named module
128+
// (e.g. "wasm:js/string-constants"). The string must be null-terminated and
129+
// remain valid for the duration of the compile call.
130+
const char* imported_string_constants_module = nullptr;
131+
// If non-empty, associated with the module's script as its source URL, for
132+
// use in stack traces and developer tooling. If a script already exists in
133+
// the isolate for the same module, its existing URL is retained. The
134+
// string must remain valid for the duration of the compile call.
135+
std::string_view source_url = {};
136+
};
137+
109138
/**
110139
* Compile a Wasm module from the provided uncompiled bytes.
111140
*/
112141
static MaybeLocal<WasmModuleObject> Compile(
113142
Isolate* isolate, MemorySpan<const uint8_t> wire_bytes);
114143

144+
/**
145+
* Compile a Wasm module from the provided uncompiled bytes, applying the
146+
* given compile options.
147+
*/
148+
static MaybeLocal<WasmModuleObject> Compile(
149+
Isolate* isolate, MemorySpan<const uint8_t> wire_bytes,
150+
const CompileOptions& options);
151+
115152
V8_INLINE static WasmModuleObject* Cast(Value* value) {
116153
#ifdef V8_ENABLE_CHECKS
117154
CheckCast(value);
@@ -224,13 +261,15 @@ class V8_EXPORT WasmStreaming final {
224261
class V8_EXPORT WasmModuleCompilation final {
225262
public:
226263
using ModuleCachingCallback = WasmStreaming::ModuleCachingCallback;
264+
using CompileOptions = WasmModuleObject::CompileOptions;
227265

228266
/**
229-
* Start an asynchronous module compilation. This can be called on any thread.
267+
* Start an asynchronous module compilation, applying the given compile
268+
* options. This can be called on any thread. Providing
269+
* {CompileOptions::source_url} is equivalent to calling {SetUrl}.
230270
* TODO(clemensb): Add some way to pass enabled features.
231-
* TODO(clemensb): Add some way to pass compile time imports.
232271
*/
233-
WasmModuleCompilation();
272+
explicit WasmModuleCompilation(const CompileOptions& options = {});
234273

235274
~WasmModuleCompilation();
236275

‎deps/v8/src/api/api.cc‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8853,7 +8853,15 @@ MaybeLocal<WasmModuleObject> WasmModuleObject::FromCompiledModule(
88538853

88548854
MaybeLocal<WasmModuleObject> WasmModuleObject::Compile(
88558855
Isolate* v8_isolate, MemorySpan<const uint8_t> wire_bytes) {
8856+
return Compile(v8_isolate, wire_bytes, CompileOptions{});
8857+
}
8858+
8859+
MaybeLocal<WasmModuleObject> WasmModuleObject::Compile(
8860+
Isolate* v8_isolate, MemorySpan<const uint8_t> wire_bytes,
8861+
const CompileOptions& options) {
88568862
#if V8_ENABLE_WEBASSEMBLY
8863+
i::wasm::CompileTimeImports compile_imports =
8864+
i::wasm::CompileTimeImportsFromOptions(options);
88578865
base::OwnedVector<const uint8_t> bytes = base::OwnedCopyOf(wire_bytes);
88588866
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
88598867
// We don't check for `IsWasmCodegenAllowed` here, because this function is
@@ -8864,10 +8872,11 @@ MaybeLocal<WasmModuleObject> WasmModuleObject::Compile(
88648872
i::wasm::ErrorThrower thrower(i_isolate, "WasmModuleObject::Compile()");
88658873
auto enabled_features =
88668874
i::wasm::WasmEnabledFeatures::FromIsolate(i_isolate);
8867-
// TODO(14179): Provide an API method that supports compile options.
88688875
maybe_compiled = i::wasm::GetWasmEngine()->SyncCompile(
8869-
i_isolate, enabled_features, i::wasm::CompileTimeImports{}, &thrower,
8870-
std::move(bytes));
8876+
i_isolate, enabled_features, std::move(compile_imports), &thrower,
8877+
std::move(bytes),
8878+
base::Vector<const char>(options.source_url.data(),
8879+
options.source_url.size()));
88718880
}
88728881
CHECK_EQ(maybe_compiled.is_null(), i_isolate->has_exception());
88738882
if (maybe_compiled.is_null()) return {};

‎deps/v8/src/wasm/wasm-engine.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,8 @@ DirectHandle<WasmModuleObject> WasmEngine::FinalizeTranslatedAsmJs(
666666
MaybeDirectHandle<WasmModuleObject> WasmEngine::SyncCompile(
667667
Isolate* isolate, WasmEnabledFeatures enabled_features,
668668
CompileTimeImports compile_imports, ErrorThrower* thrower,
669-
base::OwnedVector<const uint8_t> bytes) {
669+
base::OwnedVector<const uint8_t> bytes,
670+
base::Vector<const char> source_url) {
670671
int compilation_id = next_compilation_id_.fetch_add(1);
671672
TRACE_EVENT1("v8.wasm", "wasm.SyncCompile", "id", compilation_id);
672673
v8::metrics::Recorder::ContextId context_id =
@@ -718,9 +719,8 @@ MaybeDirectHandle<WasmModuleObject> WasmEngine::SyncCompile(
718719
}
719720
#endif
720721

721-
constexpr base::Vector<const char> kNoSourceUrl;
722722
DirectHandle<Script> script =
723-
GetOrCreateScript(isolate, native_module, kNoSourceUrl);
723+
GetOrCreateScript(isolate, native_module, source_url);
724724

725725
native_module->LogWasmCodes(isolate, *script);
726726

‎deps/v8/src/wasm/wasm-engine.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ class V8_EXPORT_PRIVATE WasmEngine {
192192
MaybeDirectHandle<WasmModuleObject> SyncCompile(
193193
Isolate* isolate, WasmEnabledFeatures enabled,
194194
CompileTimeImports compile_imports, ErrorThrower* thrower,
195-
base::OwnedVector<const uint8_t> bytes);
195+
base::OwnedVector<const uint8_t> bytes,
196+
base::Vector<const char> source_url = {});
196197

197198
// Synchronously instantiate the given Wasm module with the given imports.
198199
// If the module represents an asm.js module, then the supplied {memory}

‎deps/v8/src/wasm/wasm-js.cc‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,16 @@ class WasmModuleCompilation::Impl {
232232
const std::shared_ptr<i::wasm::StreamingDecoder> streaming_decoder_;
233233
};
234234

235-
// TODO(clemensb): Pass enabled features and compile time imports.
236-
WasmModuleCompilation::WasmModuleCompilation()
237-
: impl_(std::make_unique<Impl>(WasmEnabledFeatures::FromFlags(),
238-
CompileTimeImports{})) {
235+
// TODO(clemensb): Pass enabled features.
236+
WasmModuleCompilation::WasmModuleCompilation(const CompileOptions& options)
237+
: impl_(std::make_unique<Impl>(
238+
WasmEnabledFeatures::FromFlags(),
239+
i::wasm::CompileTimeImportsFromOptions(options))) {
239240
TRACE_EVENT0("v8.wasm", "wasm.ModuleCompilation");
241+
if (!options.source_url.empty()) {
242+
impl_->SetUrl(
243+
base::VectorOf(options.source_url.data(), options.source_url.size()));
244+
}
240245
}
241246

242247
WasmModuleCompilation::~WasmModuleCompilation() = default;
@@ -3959,6 +3964,27 @@ void WasmJs::InstallResizableBufferIntegration(
39593964
wasm::WebAssemblyMemoryToResizableBuffer, 0);
39603965
}
39613966

3967+
namespace wasm {
3968+
CompileTimeImports CompileTimeImportsFromOptions(
3969+
const v8::WasmModuleObject::CompileOptions& options) {
3970+
CompileTimeImports result;
3971+
using Builtins = v8::WasmModuleObject::CompileOptions::Builtins;
3972+
if (options.builtins & Builtins::kJsString) {
3973+
result.Add(CompileTimeImport::kJsString);
3974+
}
3975+
if (options.imported_string_constants_module != nullptr) {
3976+
result.constants_module() = options.imported_string_constants_module;
3977+
result.Add(CompileTimeImport::kStringConstants);
3978+
}
3979+
// Mirror the JS `WebAssembly.Module` constructor, which disables denormal
3980+
// floats at compile time when the host FPU flushes them.
3981+
if (base::FPU::GetFlushDenormals()) {
3982+
result.Add(CompileTimeImport::kDisableDenormalFloats);
3983+
}
3984+
return result;
3985+
}
3986+
} // namespace wasm
3987+
39623988
// static
39633989
CompileTimeImports WasmJs::CompileTimeImportsFromArgument(
39643990
DirectHandle<Object> arg, Isolate* isolate,

‎deps/v8/src/wasm/wasm-js.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
#include <memory>
1313

14+
#include "include/v8-wasm.h"
1415
#include "src/common/globals.h"
1516
#include "src/wasm/wasm-features.h"
1617

1718
namespace v8 {
1819
class Value;
1920
template <typename T>
2021
class FunctionCallbackInfo;
21-
class WasmStreaming;
2222
} // namespace v8
2323

2424
namespace v8::internal {
@@ -30,6 +30,11 @@ class StreamingDecoder;
3030
V8_EXPORT_PRIVATE std::unique_ptr<WasmStreaming> StartStreamingForTesting(
3131
Isolate*, std::shared_ptr<wasm::CompilationResultResolver>);
3232

33+
// Convert compile options from the public API into compile-time imports,
34+
// including the host-FPU denormal handling applied to all compilations.
35+
V8_EXPORT_PRIVATE CompileTimeImports
36+
CompileTimeImportsFromOptions(const v8::WasmModuleObject::CompileOptions&);
37+
3338
#define WASM_JS_EXTERNAL_REFERENCE_LIST(V) \
3439
V(WebAssemblyCompile) \
3540
V(WebAssemblyException) \

0 commit comments

Comments
 (0)