Commit 890ee74c authored by Seth Brenith's avatar Seth Brenith Committed by V8 LUCI CQ

Allow no-op background merges to complete

It is possible, though unlikely, that V8 will deserialize code cache
data, decide to merge that new data with an existing script from the
Isolate compilation cache, and subsequently do nothing in the background
portion of the merge (make no heap changes, and request no follow-up
changes on the main thread). In this case, the most optimal outcome is
to reuse the script from the Isolate compilation cache, not to use the
newly deserialized script.

CodeSerializer::FinishOffThreadDeserialize uses
BackgroundMergeTask::HasPendingForegroundWork to determine whether it
should complete the merge and use the Script from the compilation cache
or complete the deserialization and use the newly deserialized Script.
This change updates HasPendingForegroundWork so that it will return true
even if the merge was a no-op.

Bug: v8:12808
Change-Id: I08fcb814e797218e5be2b4ce4f45bd4e0637ec80
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3916270
Commit-Queue: Seth Brenith <seth.brenith@microsoft.com>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/main@{#83439}
parent 7bf63faf
...@@ -35,7 +35,7 @@ class V8_EXPORT_PRIVATE BackgroundMergeTask { ...@@ -35,7 +35,7 @@ class V8_EXPORT_PRIVATE BackgroundMergeTask {
// Step 2: on the background thread, update pointers in the new Script's // Step 2: on the background thread, update pointers in the new Script's
// object graph to point to corresponding objects from the cached Script where // object graph to point to corresponding objects from the cached Script where
// appropriate. May only be called if HasCachedScript returned true. // appropriate. May only be called if HasPendingBackgroundWork returned true.
void BeginMergeInBackground(LocalIsolate* isolate, Handle<Script> new_script); void BeginMergeInBackground(LocalIsolate* isolate, Handle<Script> new_script);
// Step 3: on the main thread again, complete the merge so that all relevant // Step 3: on the main thread again, complete the merge so that all relevant
...@@ -45,10 +45,11 @@ class V8_EXPORT_PRIVATE BackgroundMergeTask { ...@@ -45,10 +45,11 @@ class V8_EXPORT_PRIVATE BackgroundMergeTask {
Handle<SharedFunctionInfo> CompleteMergeInForeground( Handle<SharedFunctionInfo> CompleteMergeInForeground(
Isolate* isolate, Handle<Script> new_script); Isolate* isolate, Handle<Script> new_script);
bool HasCachedScript() const { return !cached_script_.is_null(); } bool HasPendingBackgroundWork() const {
return state_ == kPendingBackgroundWork;
}
bool HasPendingForegroundWork() const { bool HasPendingForegroundWork() const {
return !used_new_sfis_.empty() || return state_ == kPendingForegroundWork;
!new_compiled_data_for_cached_sfis_.empty();
} }
private: private:
...@@ -81,6 +82,14 @@ class V8_EXPORT_PRIVATE BackgroundMergeTask { ...@@ -81,6 +82,14 @@ class V8_EXPORT_PRIVATE BackgroundMergeTask {
Handle<FeedbackMetadata> feedback_metadata; Handle<FeedbackMetadata> feedback_metadata;
}; };
std::vector<NewCompiledDataForCachedSfi> new_compiled_data_for_cached_sfis_; std::vector<NewCompiledDataForCachedSfi> new_compiled_data_for_cached_sfis_;
enum State {
kNotStarted,
kPendingBackgroundWork,
kPendingForegroundWork,
kDone,
};
State state_ = kNotStarted;
}; };
} // namespace internal } // namespace internal
......
...@@ -1965,13 +1965,18 @@ void BackgroundMergeTask::SetUpOnMainThread(Isolate* isolate, ...@@ -1965,13 +1965,18 @@ void BackgroundMergeTask::SetUpOnMainThread(Isolate* isolate,
Handle<String> source_text, Handle<String> source_text,
const ScriptDetails& script_details, const ScriptDetails& script_details,
LanguageMode language_mode) { LanguageMode language_mode) {
DCHECK_EQ(state_, kNotStarted);
HandleScope handle_scope(isolate); HandleScope handle_scope(isolate);
CompilationCacheScript::LookupResult lookup_result = CompilationCacheScript::LookupResult lookup_result =
isolate->compilation_cache()->LookupScript(source_text, script_details, isolate->compilation_cache()->LookupScript(source_text, script_details,
language_mode); language_mode);
Handle<Script> script; Handle<Script> script;
if (!lookup_result.script().ToHandle(&script)) return; if (!lookup_result.script().ToHandle(&script)) {
state_ = kDone;
return;
}
// Any data sent to the background thread will need to be a persistent handle. // Any data sent to the background thread will need to be a persistent handle.
persistent_handles_ = std::make_unique<PersistentHandles>(isolate); persistent_handles_ = std::make_unique<PersistentHandles>(isolate);
...@@ -1982,15 +1987,19 @@ void BackgroundMergeTask::SetUpOnMainThread(Isolate* isolate, ...@@ -1982,15 +1987,19 @@ void BackgroundMergeTask::SetUpOnMainThread(Isolate* isolate,
// from the cache, assuming the top-level SFI is still compiled by then. // from the cache, assuming the top-level SFI is still compiled by then.
// Thus, there is no need to keep the Script pointer for background merging. // Thus, there is no need to keep the Script pointer for background merging.
// Do nothing in this case. // Do nothing in this case.
state_ = kDone;
} else { } else {
DCHECK(lookup_result.toplevel_sfi().is_null()); DCHECK(lookup_result.toplevel_sfi().is_null());
// A background merge is required. // A background merge is required.
state_ = kPendingBackgroundWork;
cached_script_ = persistent_handles_->NewHandle(*script); cached_script_ = persistent_handles_->NewHandle(*script);
} }
} }
void BackgroundMergeTask::BeginMergeInBackground(LocalIsolate* isolate, void BackgroundMergeTask::BeginMergeInBackground(LocalIsolate* isolate,
Handle<Script> new_script) { Handle<Script> new_script) {
DCHECK_EQ(state_, kPendingBackgroundWork);
LocalHeap* local_heap = isolate->heap(); LocalHeap* local_heap = isolate->heap();
local_heap->AttachPersistentHandles(std::move(persistent_handles_)); local_heap->AttachPersistentHandles(std::move(persistent_handles_));
LocalHandleScope handle_scope(local_heap); LocalHandleScope handle_scope(local_heap);
...@@ -2063,10 +2072,14 @@ void BackgroundMergeTask::BeginMergeInBackground(LocalIsolate* isolate, ...@@ -2063,10 +2072,14 @@ void BackgroundMergeTask::BeginMergeInBackground(LocalIsolate* isolate,
if (forwarder.HasAnythingToForward()) { if (forwarder.HasAnythingToForward()) {
forwarder.IterateAndForwardPointers(); forwarder.IterateAndForwardPointers();
} }
state_ = kPendingForegroundWork;
} }
Handle<SharedFunctionInfo> BackgroundMergeTask::CompleteMergeInForeground( Handle<SharedFunctionInfo> BackgroundMergeTask::CompleteMergeInForeground(
Isolate* isolate, Handle<Script> new_script) { Isolate* isolate, Handle<Script> new_script) {
DCHECK_EQ(state_, kPendingForegroundWork);
HandleScope handle_scope(isolate); HandleScope handle_scope(isolate);
ConstantPoolPointerForwarder forwarder(isolate, ConstantPoolPointerForwarder forwarder(isolate,
isolate->main_thread_local_heap()); isolate->main_thread_local_heap());
...@@ -2125,10 +2138,7 @@ Handle<SharedFunctionInfo> BackgroundMergeTask::CompleteMergeInForeground( ...@@ -2125,10 +2138,7 @@ Handle<SharedFunctionInfo> BackgroundMergeTask::CompleteMergeInForeground(
SharedFunctionInfo::cast(maybe_toplevel_sfi.GetHeapObjectAssumeWeak()), SharedFunctionInfo::cast(maybe_toplevel_sfi.GetHeapObjectAssumeWeak()),
isolate); isolate);
// Abandon the persistent handles from the background thread, so that state_ = kDone;
// future calls to HasPendingForegroundWork return false.
used_new_sfis_.clear();
new_compiled_data_for_cached_sfis_.clear();
return handle_scope.CloseAndEscape(result); return handle_scope.CloseAndEscape(result);
} }
...@@ -2309,14 +2319,14 @@ void BackgroundCompileTask::SourceTextAvailable( ...@@ -2309,14 +2319,14 @@ void BackgroundCompileTask::SourceTextAvailable(
bool BackgroundDeserializeTask::ShouldMergeWithExistingScript() const { bool BackgroundDeserializeTask::ShouldMergeWithExistingScript() const {
DCHECK(v8_flags.merge_background_deserialized_script_with_compilation_cache); DCHECK(v8_flags.merge_background_deserialized_script_with_compilation_cache);
return background_merge_task_.HasCachedScript() && return background_merge_task_.HasPendingBackgroundWork() &&
off_thread_data_.HasResult(); off_thread_data_.HasResult();
} }
bool BackgroundCompileTask::ShouldMergeWithExistingScript() const { bool BackgroundCompileTask::ShouldMergeWithExistingScript() const {
DCHECK(v8_flags.stress_background_compile); DCHECK(v8_flags.stress_background_compile);
DCHECK(!script_.is_null()); DCHECK(!script_.is_null());
return background_merge_task_.HasCachedScript() && return background_merge_task_.HasPendingBackgroundWork() &&
jobs_to_retry_finalization_on_main_thread_.empty(); jobs_to_retry_finalization_on_main_thread_.empty();
} }
......
...@@ -633,4 +633,86 @@ TEST_F(MergeDeserializedCodeTest, Regress1360024) { ...@@ -633,4 +633,86 @@ TEST_F(MergeDeserializedCodeTest, Regress1360024) {
true); // lazy_should_be_compiled true); // lazy_should_be_compiled
} }
TEST_F(MergeDeserializedCodeTest, MergeWithNoFollowUpWork) {
i::v8_flags.merge_background_deserialized_script_with_compilation_cache =
true;
std::unique_ptr<v8::ScriptCompiler::CachedData> cached_data;
IsolateAndContextScope scope(this);
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate());
ScriptOrigin default_origin(isolate(), NewString(""));
constexpr char kSourceCode[] = "function f() {}";
Local<Script> original_script;
// Compile the script for the first time, to both populate the Isolate
// compilation cache and produce code cache data.
{
v8::EscapableHandleScope handle_scope(isolate());
Local<Script> script =
Script::Compile(context(), NewString(kSourceCode), &default_origin)
.ToLocalChecked();
cached_data.reset(
ScriptCompiler::CreateCodeCache(script->GetUnboundScript()));
// Retain the v8::Script (a JSFunction) so we can run it later.
original_script = handle_scope.Escape(script);
}
// Age the top-level bytecode so that the Isolate compilation cache will
// contain only the Script.
i::BytecodeArray bytecode =
GetSharedFunctionInfo(original_script).GetBytecodeArray(i_isolate);
const int kAgingThreshold = 6;
for (int j = 0; j < kAgingThreshold; ++j) {
bytecode.MakeOlder();
}
i_isolate->heap()->CollectAllGarbage(i::Heap::kNoGCFlags,
i::GarbageCollectionReason::kTesting);
// A second round of GC is necessary in case incremental marking had already
// started before the bytecode was aged.
i_isolate->heap()->CollectAllGarbage(i::Heap::kNoGCFlags,
i::GarbageCollectionReason::kTesting);
DeserializeThread deserialize_thread(ScriptCompiler::StartConsumingCodeCache(
isolate(), std::make_unique<ScriptCompiler::CachedData>(
cached_data->data, cached_data->length,
ScriptCompiler::CachedData::BufferNotOwned)));
CHECK(deserialize_thread.Start());
deserialize_thread.Join();
std::unique_ptr<ScriptCompiler::ConsumeCodeCacheTask> task =
deserialize_thread.TakeTask();
// At this point, the cached script's top-level SFI is not compiled, so a
// background merge is recommended.
task->SourceTextAvailable(isolate(), NewString(kSourceCode), default_origin);
CHECK(task->ShouldMergeWithExistingScript());
// Run the original script, which will cause its top-level SFI to become
// compiled again, and make the SFI for the nested function exist.
CHECK(!original_script->Run(context()).IsEmpty());
// The background merge does nothing and requests no follow-up work on the
// main thread because the original script has the same SFIs at the same level
// of compiledness.
MergeThread merge_thread(task.get());
CHECK(merge_thread.Start());
merge_thread.Join();
// Complete compilation on the main thread. Even though no follow-up work is
// required, this step should reuse the original script.
ScriptCompiler::Source source(NewString(kSourceCode), default_origin,
cached_data.release(), task.release());
Local<Script> script =
ScriptCompiler::Compile(context(), &source,
ScriptCompiler::kConsumeCodeCache)
.ToLocalChecked();
CHECK_EQ(GetSharedFunctionInfo(script),
GetSharedFunctionInfo(original_script));
}
} // namespace v8 } // namespace v8
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