Commit d2a37049 authored by Ross McIlroy's avatar Ross McIlroy Committed by Commit Bot

[Compiler] Move BackgroundParsingTask to compiler.cc

Moves BackgroundParsingTask to compiler.cc and renames as BackgroundCompileTask.
This moves code out api.cc and parsing/ into compiler.cc where it belongs.

BUG=v8:7311,v8:5203

Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: I08a72ab8a6bdb480f519d42b36875d347b801ddc
Reviewed-on: https://chromium-review.googlesource.com/919481
Commit-Queue: Ross McIlroy <rmcilroy@chromium.org>
Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarMythri Alle <mythria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51421}
parent a33945a1
......@@ -1891,8 +1891,6 @@ v8_source_set("v8_base") {
"src/objects/template-objects.h",
"src/ostreams.cc",
"src/ostreams.h",
"src/parsing/background-parsing-task.cc",
"src/parsing/background-parsing-task.h",
"src/parsing/duplicate-finder.h",
"src/parsing/expression-classifier.h",
"src/parsing/expression-scope-reparenter.cc",
......
......@@ -145,7 +145,7 @@ class Heap;
class HeapObject;
class Isolate;
class Object;
struct StreamedSource;
struct ScriptStreamingData;
template<typename T> class CustomArguments;
class PropertyCallbackArguments;
class FunctionCallbackArguments;
......@@ -1406,14 +1406,14 @@ class V8_EXPORT ScriptCompiler {
// object is alive.
const CachedData* GetCachedData() const;
internal::StreamedSource* impl() const { return impl_; }
internal::ScriptStreamingData* impl() const { return impl_; }
// Prevent copying.
StreamedSource(const StreamedSource&) = delete;
StreamedSource& operator=(const StreamedSource&) = delete;
private:
internal::StreamedSource* impl_;
internal::ScriptStreamingData* impl_;
};
/**
......
......@@ -49,7 +49,6 @@
#include "src/json-stringifier.h"
#include "src/messages.h"
#include "src/objects-inl.h"
#include "src/parsing/background-parsing-task.h"
#include "src/parsing/parser.h"
#include "src/parsing/scanner-character-streams.h"
#include "src/pending-compilation-error-handler.h"
......@@ -2030,11 +2029,9 @@ bool ScriptCompiler::ExternalSourceStream::SetBookmark() { return false; }
void ScriptCompiler::ExternalSourceStream::ResetToBookmark() { UNREACHABLE(); }
ScriptCompiler::StreamedSource::StreamedSource(ExternalSourceStream* stream,
Encoding encoding)
: impl_(new i::StreamedSource(stream, encoding)) {}
: impl_(new i::ScriptStreamingData(stream, encoding)) {}
ScriptCompiler::StreamedSource::~StreamedSource() { delete impl_; }
......@@ -2552,9 +2549,11 @@ ScriptCompiler::ScriptStreamingTask* ScriptCompiler::StartStreamingScript(
if (!i::FLAG_script_streaming) {
return nullptr;
}
// We don't support other compile options on streaming background compiles.
// TODO(rmcilroy): remove CompileOptions from the API.
CHECK(options == ScriptCompiler::kNoCompileOptions);
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
return new i::BackgroundParsingTask(source->impl(), options,
i::FLAG_stack_size, isolate);
return i::Compiler::NewBackgroundCompileTask(source->impl(), isolate);
}
......@@ -2564,7 +2563,8 @@ MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
const ScriptOrigin& origin) {
PREPARE_FOR_EXECUTION(context, ScriptCompiler, Compile, Script);
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.ScriptCompiler");
i::StreamedSource* source = v8_source->impl();
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),
"V8.CompileStreamedScript");
i::Handle<i::String> str = Utils::OpenHandle(*(full_source_string));
i::Handle<i::Script> script = isolate->factory()->NewScript(str);
if (isolate->NeedsSourcePositionsForProfiling()) {
......@@ -2591,32 +2591,15 @@ MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
*Utils::OpenHandle(*(origin.SourceMapUrl())));
}
source->info->set_script(script);
source->parser->UpdateStatistics(isolate, script);
source->info->UpdateBackgroundParseStatisticsOnMainThread(isolate);
source->parser->HandleSourceURLComments(isolate, script);
i::ScriptStreamingData* streaming_data = v8_source->impl();
i::MaybeHandle<i::SharedFunctionInfo> maybe_function_info =
i::Compiler::GetSharedFunctionInfoForStreamedScript(
script, streaming_data, str->length());
i::Handle<i::SharedFunctionInfo> result;
if (source->info->literal() == nullptr) {
// Parsing has failed - report error messages.
source->info->pending_error_handler()->ReportErrors(
isolate, script, source->info->ast_value_factory());
} else {
// Parsing has succeeded - finalize compile.
if (i::FLAG_background_compile) {
result = i::Compiler::GetSharedFunctionInfoForBackgroundCompile(
script, source->info.get(), str->length(),
source->outer_function_job.get(), &source->inner_function_jobs);
} else {
result = i::Compiler::GetSharedFunctionInfoForStreamedScript(
script, source->info.get(), str->length());
}
}
has_pending_exception = result.is_null();
has_pending_exception = !maybe_function_info.ToHandle(&result);
if (has_pending_exception) isolate->ReportPendingMessages();
source->Release();
RETURN_ON_FAILED_EXECUTION(Script);
Local<UnboundScript> generic = ToApiHandle<UnboundScript>(result);
......
This diff is collapsed.
......@@ -13,6 +13,7 @@
#include "src/code-events.h"
#include "src/contexts.h"
#include "src/isolate.h"
#include "src/unicode-cache.h"
#include "src/zone/zone.h"
namespace v8 {
......@@ -23,7 +24,9 @@ class CompilationInfo;
class CompilationJob;
class JavaScriptFrame;
class ParseInfo;
class Parser;
class ScriptData;
struct ScriptStreamingData;
typedef std::forward_list<std::unique_ptr<CompilationJob>> CompilationJobList;
......@@ -53,11 +56,12 @@ class V8_EXPORT_PRIVATE Compiler : public AllStatic {
static bool CompileOptimized(Handle<JSFunction> function, ConcurrencyMode);
static MaybeHandle<JSArray> CompileForLiveEdit(Handle<Script> script);
// Compile top level code on a background thread. Should be finalized by
// GetSharedFunctionInfoForBackgroundCompile.
static std::unique_ptr<CompilationJob> CompileTopLevelOnBackgroundThread(
ParseInfo* parse_info, AccountingAllocator* allocator,
CompilationJobList* inner_function_jobs);
// Creates a new task that when run will parse and compile the streamed
// script associated with |streaming_data| and can be finalized with
// Compiler::GetSharedFunctionInfoForStreamedScript.
// Note: does not take ownership of streaming_data.
static ScriptCompiler::ScriptStreamingTask* NewBackgroundCompileTask(
ScriptStreamingData* streaming_data, Isolate* isolate);
// Generate and install code from previously queued compilation job.
static bool FinalizeCompilationJob(CompilationJob* job, Isolate* isolate);
......@@ -123,16 +127,13 @@ class V8_EXPORT_PRIVATE Compiler : public AllStatic {
MaybeHandle<FixedArray> maybe_host_defined_options);
// Create a shared function info object for a Script that has already been
// parsed while the script was being loaded from a streamed source.
static Handle<SharedFunctionInfo> GetSharedFunctionInfoForStreamedScript(
Handle<Script> script, ParseInfo* info, int source_length);
// Create a shared function info object for a Script that has already been
// compiled on a background thread.
static Handle<SharedFunctionInfo> GetSharedFunctionInfoForBackgroundCompile(
Handle<Script> script, ParseInfo* parse_info, int source_length,
CompilationJob* outer_function_job,
CompilationJobList* inner_function_jobs);
// parsed and possibly compiled on a background thread while being loaded from
// a streamed source. On return, the data held by |streaming_data| will have
// been released, however the object itself isn't freed and is still owned by
// the caller.
static MaybeHandle<SharedFunctionInfo> GetSharedFunctionInfoForStreamedScript(
Handle<Script> script, ScriptStreamingData* streaming_data,
int source_length);
// Create a shared function info object for the given function literal
// node (the code may be lazily compiled).
......@@ -237,6 +238,34 @@ class V8_EXPORT_PRIVATE CompilationJob {
}
};
// Contains all data which needs to be transmitted between threads for
// background parsing and compiling and finalizing it on the main thread.
struct ScriptStreamingData {
ScriptStreamingData(ScriptCompiler::ExternalSourceStream* source_stream,
ScriptCompiler::StreamedSource::Encoding encoding);
~ScriptStreamingData();
void Release();
// Internal implementation of v8::ScriptCompiler::StreamedSource.
std::unique_ptr<ScriptCompiler::ExternalSourceStream> source_stream;
ScriptCompiler::StreamedSource::Encoding encoding;
std::unique_ptr<ScriptCompiler::CachedData> cached_data;
// Data needed for parsing, and data needed to to be passed between thread
// between parsing and compilation. These need to be initialized before the
// compilation starts.
UnicodeCache unicode_cache;
std::unique_ptr<ParseInfo> info;
std::unique_ptr<Parser> parser;
// Data needed for finalizing compilation after background compilation.
std::unique_ptr<CompilationJob> outer_function_job;
CompilationJobList inner_function_jobs;
DISALLOW_COPY_AND_ASSIGN(ScriptStreamingData);
};
} // namespace internal
} // namespace v8
......
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/parsing/background-parsing-task.h"
#include "src/counters.h"
#include "src/objects-inl.h"
#include "src/parsing/parser.h"
#include "src/parsing/scanner-character-streams.h"
#include "src/vm-state-inl.h"
namespace v8 {
namespace internal {
void StreamedSource::Release() {
parser.reset();
info.reset();
}
BackgroundParsingTask::BackgroundParsingTask(
StreamedSource* source, ScriptCompiler::CompileOptions options,
int stack_size, Isolate* isolate)
: source_(source),
stack_size_(stack_size),
timer_(isolate->counters()->compile_script_on_background()) {
// We don't set the context to the CompilationInfo yet, because the background
// thread cannot do anything with it anyway. We set it just before compilation
// on the foreground thread.
DCHECK(options == ScriptCompiler::kNoCompileOptions ||
options == ScriptCompiler::kEagerCompile);
VMState<PARSER> state(isolate);
// Prepare the data for the internalization phase and compilation phase, which
// will happen in the main thread after parsing.
ParseInfo* info = new ParseInfo(isolate->allocator());
info->InitFromIsolate(isolate);
if (V8_UNLIKELY(FLAG_runtime_stats)) {
info->set_runtime_call_stats(new (info->zone()) RuntimeCallStats());
} else {
info->set_runtime_call_stats(nullptr);
}
info->set_toplevel();
std::unique_ptr<Utf16CharacterStream> stream(
ScannerStream::For(source->source_stream.get(), source->encoding,
info->runtime_call_stats()));
info->set_character_stream(std::move(stream));
info->set_unicode_cache(&source_->unicode_cache);
info->set_allow_lazy_parsing();
if (V8_UNLIKELY(info->block_coverage_enabled())) {
info->AllocateSourceRangeMap();
}
LanguageMode language_mode = construct_language_mode(FLAG_use_strict);
info->set_language_mode(
stricter_language_mode(info->language_mode(), language_mode));
source->info.reset(info);
allocator_ = isolate->allocator();
// Parser needs to stay alive for finalizing the parsing on the main
// thread.
source_->parser.reset(new Parser(source_->info.get()));
source_->parser->DeserializeScopeChain(source_->info.get(),
MaybeHandle<ScopeInfo>());
}
void BackgroundParsingTask::Run() {
TimedHistogramScope timer(timer_);
DisallowHeapAllocation no_allocation;
DisallowHandleAllocation no_handles;
DisallowHandleDereference no_deref;
source_->info->set_on_background_thread(true);
// Reset the stack limit of the parser to reflect correctly that we're on a
// background thread.
uintptr_t old_stack_limit = source_->info->stack_limit();
uintptr_t stack_limit = GetCurrentStackPosition() - stack_size_ * KB;
source_->info->set_stack_limit(stack_limit);
source_->parser->set_stack_limit(stack_limit);
source_->parser->ParseOnBackground(source_->info.get());
if (FLAG_background_compile && source_->info->literal() != nullptr) {
// Parsing has succeeded, compile.
source_->outer_function_job = Compiler::CompileTopLevelOnBackgroundThread(
source_->info.get(), allocator_, &source_->inner_function_jobs);
}
source_->info->EmitBackgroundParseStatisticsOnBackgroundThread();
source_->info->set_on_background_thread(false);
source_->info->set_stack_limit(old_stack_limit);
}
} // namespace internal
} // namespace v8
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_PARSING_BACKGROUND_PARSING_TASK_H_
#define V8_PARSING_BACKGROUND_PARSING_TASK_H_
#include <memory>
#include "include/v8.h"
#include "src/base/platform/platform.h"
#include "src/base/platform/semaphore.h"
#include "src/compiler.h"
#include "src/parsing/parse-info.h"
#include "src/unicode-cache.h"
namespace v8 {
namespace internal {
class Parser;
class ScriptData;
class TimedHistogram;
// Internal representation of v8::ScriptCompiler::StreamedSource. Contains all
// data which needs to be transmitted between threads for background parsing,
// finalizing it on the main thread, and compiling on the main thread.
struct StreamedSource {
StreamedSource(ScriptCompiler::ExternalSourceStream* source_stream,
ScriptCompiler::StreamedSource::Encoding encoding)
: source_stream(source_stream), encoding(encoding) {}
void Release();
// Internal implementation of v8::ScriptCompiler::StreamedSource.
std::unique_ptr<ScriptCompiler::ExternalSourceStream> source_stream;
ScriptCompiler::StreamedSource::Encoding encoding;
std::unique_ptr<ScriptCompiler::CachedData> cached_data;
// Data needed for parsing, and data needed to to be passed between thread
// between parsing and compilation. These need to be initialized before the
// compilation starts.
UnicodeCache unicode_cache;
std::unique_ptr<ParseInfo> info;
std::unique_ptr<Parser> parser;
// Data needed for finalizing compilation after background compilation.
std::unique_ptr<CompilationJob> outer_function_job;
CompilationJobList inner_function_jobs;
// Prevent copying.
StreamedSource(const StreamedSource&) = delete;
StreamedSource& operator=(const StreamedSource&) = delete;
};
class BackgroundParsingTask : public ScriptCompiler::ScriptStreamingTask {
public:
BackgroundParsingTask(StreamedSource* source,
ScriptCompiler::CompileOptions options, int stack_size,
Isolate* isolate);
virtual void Run();
private:
StreamedSource* source_; // Not owned.
int stack_size_;
AccountingAllocator* allocator_;
TimedHistogram* timer_;
};
} // namespace internal
} // namespace v8
#endif // V8_PARSING_BACKGROUND_PARSING_TASK_H_
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