Commit 61a6fd84 authored by Clemens Backes's avatar Clemens Backes Committed by V8 LUCI CQ

[wasm] Rename "OnModuleCompiled" and related callbacks

Already after enabling Liftoff, the name did not match the semantics any
more. The callback was called after top-tier finished, not after initial
compilation of the module finished.
With dynamic tiering, the name is even less fitting.

This CL renames the "OnModuleCompiled" callback in the API to
"MoreFunctionsCanBeSerialized", which makes it more obvious what the
API should be used for. It also internally renames all related typedefs
and methods accordingly.

One call of the callback in the streaming decoder was already wrong
before this CL and is being removed.

R=jkummerow@chromium.org, cbruni@chromium.org

Bug: v8:12899
Change-Id: I95c0fc9e32442383e47e4370e31277cc065bf0fe
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3687689Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81093}
parent 67234388
......@@ -5,6 +5,7 @@
#ifndef INCLUDE_V8_WASM_H_
#define INCLUDE_V8_WASM_H_
#include <functional>
#include <memory>
#include <string>
......@@ -133,14 +134,16 @@ class V8_EXPORT WasmStreaming final {
/**
* Client to receive streaming event notifications.
*/
class Client {
class V8_DEPRECATE_SOON(
"Use SetMoreFunctionsCanBeSerializedCallback") Client {
public:
virtual ~Client() = default;
/**
* Passes the fully compiled module to the client. This can be used to
* implement code caching.
*/
virtual void OnModuleCompiled(CompiledWasmModule compiled_module) = 0;
virtual void OnModuleCompiled(CompiledWasmModule) = 0;
};
explicit WasmStreaming(std::unique_ptr<WasmStreamingImpl> impl);
......@@ -186,8 +189,16 @@ class V8_EXPORT WasmStreaming final {
* Sets the client object that will receive streaming event notifications.
* This must be called before {OnBytesReceived}, {Finish}, or {Abort}.
*/
V8_DEPRECATE_SOON("Use SetMoreFunctionsCanBeSerializedCallback")
void SetClient(std::shared_ptr<Client> client);
/**
* Sets a callback which is called whenever a significant number of new
* functions are ready for serialization.
*/
void SetMoreFunctionsCanBeSerializedCallback(
std::function<void(CompiledWasmModule)>);
/*
* Sets the UTF-8 encoded source URL for the {Script} object. This must be
* called before {Finish}.
......
......@@ -10447,6 +10447,11 @@ bool WasmStreaming::SetCompiledModuleBytes(const uint8_t* bytes, size_t size) {
void WasmStreaming::SetClient(std::shared_ptr<Client> client) { UNREACHABLE(); }
void WasmStreaming::SetMoreFunctionsCanBeSerializedCallback(
std::function<void(CompiledWasmModule)>) {
UNREACHABLE();
}
void WasmStreaming::SetUrl(const char* url, size_t length) { UNREACHABLE(); }
// static
......
......@@ -317,11 +317,12 @@ void AsyncStreamingDecoder::Abort() {
namespace {
class CompilationChunkFinishedCallback : public CompilationEventCallback {
class CallMoreFunctionsCanBeSerializedCallback
: public CompilationEventCallback {
public:
CompilationChunkFinishedCallback(
CallMoreFunctionsCanBeSerializedCallback(
std::weak_ptr<NativeModule> native_module,
AsyncStreamingDecoder::ModuleCompiledCallback callback)
AsyncStreamingDecoder::MoreFunctionsCanBeSerializedCallback callback)
: native_module_(std::move(native_module)),
callback_(std::move(callback)) {
// As a baseline we also count the modules that could be cached but
......@@ -347,7 +348,7 @@ class CompilationChunkFinishedCallback : public CompilationEventCallback {
private:
const std::weak_ptr<NativeModule> native_module_;
const AsyncStreamingDecoder::ModuleCompiledCallback callback_;
const AsyncStreamingDecoder::MoreFunctionsCanBeSerializedCallback callback_;
int cache_count_ = 0;
};
......@@ -355,12 +356,14 @@ class CompilationChunkFinishedCallback : public CompilationEventCallback {
void AsyncStreamingDecoder::NotifyNativeModuleCreated(
const std::shared_ptr<NativeModule>& native_module) {
if (!module_compiled_callback_) return;
if (!more_functions_can_be_serialized_callback_) return;
auto* comp_state = native_module->compilation_state();
comp_state->AddCallback(std::make_unique<CompilationChunkFinishedCallback>(
std::move(native_module), std::move(module_compiled_callback_)));
module_compiled_callback_ = {};
comp_state->AddCallback(
std::make_unique<CallMoreFunctionsCanBeSerializedCallback>(
native_module,
std::move(more_functions_can_be_serialized_callback_)));
more_functions_can_be_serialized_callback_ = {};
}
// An abstract class to share code among the states which decode VarInts. This
......
......@@ -86,12 +86,14 @@ class V8_EXPORT_PRIVATE StreamingDecoder {
virtual void NotifyCompilationEnded() = 0;
// Caching support.
// Sets the callback that is called after the module is fully compiled.
using ModuleCompiledCallback =
// Sets the callback that is called after a new chunk of the module is tiered
// up.
using MoreFunctionsCanBeSerializedCallback =
std::function<void(const std::shared_ptr<NativeModule>&)>;
void SetModuleCompiledCallback(ModuleCompiledCallback callback) {
module_compiled_callback_ = std::move(callback);
void SetMoreFunctionsCanBeSerializedCallback(
MoreFunctionsCanBeSerializedCallback callback) {
more_functions_can_be_serialized_callback_ = std::move(callback);
}
// Passes previously compiled module bytes from the embedder's cache.
......@@ -121,7 +123,8 @@ class V8_EXPORT_PRIVATE StreamingDecoder {
bool deserializing() const { return !compiled_module_bytes_.empty(); }
std::string url_;
ModuleCompiledCallback module_compiled_callback_;
MoreFunctionsCanBeSerializedCallback
more_functions_can_be_serialized_callback_;
// The content of `compiled_module_bytes_` shouldn't be used until
// Finish(true) is called.
base::Vector<const uint8_t> compiled_module_bytes_;
......
......@@ -68,9 +68,6 @@ class V8_EXPORT_PRIVATE SyncStreamingDecoder : public StreamingDecoder {
return;
}
Handle<WasmModuleObject> module = module_object.ToHandleChecked();
if (module_compiled_callback_) {
module_compiled_callback_(module->shared_native_module());
}
resolver_->OnCompilationSucceeded(module);
}
......
......@@ -15,6 +15,7 @@
#include "src/base/logging.h"
#include "src/base/overflowing-math.h"
#include "src/base/platform/wrappers.h"
#include "src/common/allow-deprecated.h"
#include "src/common/assert-scope.h"
#include "src/execution/execution.h"
#include "src/execution/frames-inl.h"
......@@ -89,14 +90,26 @@ class WasmStreaming::WasmStreamingImpl {
return true;
}
START_ALLOW_USE_DEPRECATED()
void SetClient(std::shared_ptr<Client> client) {
streaming_decoder_->SetModuleCompiledCallback(
streaming_decoder_->SetMoreFunctionsCanBeSerializedCallback(
[client, streaming_decoder = streaming_decoder_](
const std::shared_ptr<i::wasm::NativeModule>& native_module) {
base::Vector<const char> url = streaming_decoder->url();
auto compiled_wasm_module =
CompiledWasmModule(native_module, url.begin(), url.size());
client->OnModuleCompiled(compiled_wasm_module);
client->OnModuleCompiled(
CompiledWasmModule{native_module, url.begin(), url.size()});
});
}
END_ALLOW_USE_DEPRECATED()
void SetMoreFunctionsCanBeSerializedCallback(
std::function<void(CompiledWasmModule)> callback) {
streaming_decoder_->SetMoreFunctionsCanBeSerializedCallback(
[callback = std::move(callback),
streaming_decoder = streaming_decoder_](
const std::shared_ptr<i::wasm::NativeModule>& native_module) {
base::Vector<const char> url = streaming_decoder->url();
callback(CompiledWasmModule{native_module, url.begin(), url.size()});
});
}
......@@ -142,6 +155,11 @@ void WasmStreaming::SetClient(std::shared_ptr<Client> client) {
impl_->SetClient(client);
}
void WasmStreaming::SetMoreFunctionsCanBeSerializedCallback(
std::function<void(CompiledWasmModule)> callback) {
impl_->SetMoreFunctionsCanBeSerializedCallback(std::move(callback));
}
void WasmStreaming::SetUrl(const char* url, size_t length) {
DCHECK_EQ('\0', url[length]); // {url} is null-terminated.
TRACE_EVENT1("v8.wasm", "wasm.SetUrl", "url", url);
......
......@@ -1145,7 +1145,7 @@ STREAM_TEST(TestIncrementalCaching) {
FlagScope<int> caching_treshold(&FLAG_wasm_caching_threshold, threshold);
StreamTester tester(isolate);
int call_cache_counter = 0;
tester.stream()->SetModuleCompiledCallback(
tester.stream()->SetMoreFunctionsCanBeSerializedCallback(
[&call_cache_counter](
const std::shared_ptr<i::wasm::NativeModule>& native_module) {
call_cache_counter++;
......@@ -1321,7 +1321,7 @@ STREAM_TEST(TestFunctionSectionWithoutCodeSection) {
CHECK(tester.IsPromiseRejected());
}
STREAM_TEST(TestSetModuleCompiledCallback) {
STREAM_TEST(TestMoreFunctionsCanBeSerializedCallback) {
// The "module compiled" callback (to be renamed to "top tier chunk finished"
// or similar) will only be triggered with dynamic tiering, so skip this test
// if dynamic tiering is disabled.
......@@ -1332,7 +1332,7 @@ STREAM_TEST(TestSetModuleCompiledCallback) {
FlagScope<int> caching_treshold(&FLAG_wasm_caching_threshold, 10);
StreamTester tester(isolate);
bool callback_called = false;
tester.stream()->SetModuleCompiledCallback(
tester.stream()->SetMoreFunctionsCanBeSerializedCallback(
[&callback_called](const std::shared_ptr<NativeModule> module) {
callback_called = true;
});
......@@ -1370,7 +1370,7 @@ STREAM_TEST(TestSetModuleCompiledCallback) {
// Continue executing functions (eventually triggering tier-up) until the
// callback is called at least once.
auto* i_isolate = CcTest::i_isolate();
ErrorThrower thrower{i_isolate, "TestSetModuleCompiledCallback"};
ErrorThrower thrower{i_isolate, "TestMoreFunctionsCanBeSerializedCallback"};
Handle<WasmInstanceObject> instance =
GetWasmEngine()
->SyncInstantiate(i_isolate, &thrower, tester.module_object(), {}, {})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment