Commit 7a9e7f50 authored by Thibaud Michaud's avatar Thibaud Michaud Committed by Commit Bot

[wasm] Trigger top tier callback for cached modules

Ensure that the top tier streaming callback is called, even when the
native module is actually fetched from the cache rather than compiled.
This preserves the old behavior, such that the cache stays an
implementation detail to clients of the streaming compilation API.

R=clemensb@chromium.org

Bug: v8:10165
Change-Id: Ib243c97529900f76b7b9cd5d1d57f259bc38225c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2035875
Commit-Queue: Thibaud Michaud <thibaudm@chromium.org>
Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66117}
parent edaec0cb
......@@ -114,6 +114,7 @@ class CompilationState {
const;
void AddCallback(callback_t);
void NotifyTopTierReady(callback_t);
bool failed() const;
V8_EXPORT_PRIVATE bool baseline_compilation_finished() const;
......
......@@ -389,6 +389,10 @@ class CompilationStateImpl {
// events. The callback object must support being deleted from any thread.
void AddCallback(CompilationState::callback_t);
// If the module is already tiered up, trigger the callback immediately with a
// {kFinishedTopTierCompilation} event. Otherwise, behave as {AddCallback}.
void NotifyTopTierReady(CompilationState::callback_t);
// Inserts new functions to compile and kicks off compilation.
void AddCompilationUnits(
Vector<WasmCompilationUnit> baseline_units,
......@@ -597,6 +601,11 @@ void CompilationState::AddCallback(CompilationState::callback_t callback) {
return Impl(this)->AddCallback(std::move(callback));
}
void CompilationState::NotifyTopTierReady(
CompilationState::callback_t callback) {
return Impl(this)->NotifyTopTierReady(std::move(callback));
}
bool CompilationState::failed() const { return Impl(this)->failed(); }
bool CompilationState::baseline_compilation_finished() const {
......@@ -1584,8 +1593,6 @@ void AsyncCompileJob::CreateNativeModule(
native_module_ = isolate_->wasm_engine()->NewNativeModule(
isolate_, enabled_features_, std::move(module), code_size_estimate);
native_module_->SetWireBytes({std::move(bytes_copy_), wire_bytes_.length()});
if (stream_) stream_->NotifyNativeModuleCreated(native_module_);
}
bool AsyncCompileJob::GetOrCreateNativeModule(
......@@ -1620,10 +1627,12 @@ void AsyncCompileJob::PrepareRuntimeObjects() {
void AsyncCompileJob::FinishCompile(bool is_after_cache_hit) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm"),
"AsyncCompileJob::FinishCompile");
// TODO(v8:10165): Ensure that the stream's top tier callback is eventually
// triggered even if the native module comes from the cache.
bool is_after_deserialization = !module_object_.is_null();
auto compilation_state = Impl(native_module_->compilation_state());
if (!is_after_deserialization) {
if (stream_) {
stream_->NotifyNativeModuleCreated(native_module_);
}
PrepareRuntimeObjects();
}
......@@ -1650,8 +1659,6 @@ void AsyncCompileJob::FinishCompile(bool is_after_cache_hit) {
isolate_->debug()->OnAfterCompile(script);
}
auto compilation_state =
Impl(module_object_->native_module()->compilation_state());
// TODO(bbudge) Allow deserialization without wrapper compilation, so we can
// just compile wrappers here.
if (!is_after_deserialization) {
......@@ -1989,7 +1996,8 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
void RunInForeground(AsyncCompileJob* job) override {
TRACE_COMPILE("(2) Prepare and start compile...\n");
if (job->stream_ != nullptr) {
const bool streaming = job->wire_bytes_.length() == 0;
if (streaming) {
// Streaming compilation already checked for cache hits.
job->CreateNativeModule(module_, code_size_estimate_);
} else if (job->GetOrCreateNativeModule(std::move(module_),
......@@ -2325,7 +2333,6 @@ void AsyncStreamingProcessor::OnFinishedStream(OwnedVector<uint8_t> bytes) {
if (prefix_cache_hit_) {
// Restart as an asynchronous, non-streaming compilation. Most likely
// {PrepareAndStartCompile} will get the native module from the cache.
job_->stream_ = nullptr;
size_t code_size_estimate =
wasm::WasmCodeManager::EstimateNativeModuleCodeSize(
result.value().get(), FLAG_liftoff);
......@@ -2538,6 +2545,16 @@ void CompilationStateImpl::AddCallback(CompilationState::callback_t callback) {
callbacks_.emplace_back(std::move(callback));
}
void CompilationStateImpl::NotifyTopTierReady(
CompilationState::callback_t callback) {
base::MutexGuard callbacks_guard(&callbacks_mutex_);
if (!compilation_progress_.empty() && outstanding_top_tier_functions_ == 0) {
callback(CompilationEvent::kFinishedTopTierCompilation);
return;
}
callbacks_.emplace_back(std::move(callback));
}
void CompilationStateImpl::AddCompilationUnits(
Vector<WasmCompilationUnit> baseline_units,
Vector<WasmCompilationUnit> top_tier_units,
......
......@@ -154,7 +154,7 @@ void StreamingDecoder::NotifyNativeModuleCreated(
const std::shared_ptr<NativeModule>& native_module) {
if (!module_compiled_callback_) return;
auto* comp_state = native_module->compilation_state();
comp_state->AddCallback(TopTierCompiledCallback{
comp_state->NotifyTopTierReady(TopTierCompiledCallback{
std::move(native_module), std::move(module_compiled_callback_)});
module_compiled_callback_ = {};
}
......
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