Commit e8ff83dd authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[wasm] Split adding code from publishing

Adding code can happen in parallel (it includes copying the code to the
code region and relocation it). Publishing happens under one lock per
native module though. We eventually want to avoid blocking on this lock
for too long. This CL prepares that by splitting the actions of adding
and publishing code.

R=ahaas@chromium.org

Bug: v8:10330, v8:10387
Change-Id: Iddbdadfe32e691bbf5e7b387ea947579bc3376f3
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2134372
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66979}
parent 2a303b1c
......@@ -258,7 +258,8 @@ void WasmCompilationUnit::CompileWasmFunction(Isolate* isolate,
isolate->counters(), detected);
if (result.succeeded()) {
WasmCodeRefScope code_ref_scope;
native_module->AddCompiledCode(std::move(result));
native_module->PublishCode(
native_module->AddCompiledCode(std::move(result)));
} else {
native_module->compilation_state()->SetError();
}
......
......@@ -923,7 +923,8 @@ bool CompileLazy(Isolate* isolate, NativeModule* native_module,
}
WasmCodeRefScope code_ref_scope;
WasmCode* code = native_module->AddCompiledCode(std::move(result));
WasmCode* code = native_module->PublishCode(
native_module->AddCompiledCode(std::move(result)));
DCHECK_EQ(func_index, code->index());
if (WasmCode::ShouldBeLogged(isolate)) code->LogCode(isolate);
......@@ -1053,9 +1054,14 @@ bool ExecuteCompilationUnits(
"num_results", results_to_publish.size());
if (results_to_publish.empty()) return;
WasmCodeRefScope code_ref_scope;
std::vector<WasmCode*> code_vector =
std::vector<std::unique_ptr<WasmCode>> unpublished_code =
compile_scope->native_module()->AddCompiledCode(
VectorOf(results_to_publish));
// TODO(clemensb): Avoid blocking to publish code
// (https://crbug.com/v8/10330).
std::vector<WasmCode*> code_vector =
compile_scope->native_module()->PublishCode(
VectorOf(std::move(unpublished_code)));
// For import wrapper compilation units, add result to the cache.
const NativeModule* native_module = compile_scope->native_module();
......
......@@ -1010,6 +1010,18 @@ WasmCode* NativeModule::PublishCode(std::unique_ptr<WasmCode> code) {
return PublishCodeLocked(std::move(code));
}
std::vector<WasmCode*> NativeModule::PublishCode(
Vector<std::unique_ptr<WasmCode>> codes) {
std::vector<WasmCode*> published_code;
published_code.reserve(codes.size());
base::MutexGuard lock(&allocation_mutex_);
// The published code is put into the top-most surrounding {WasmCodeRefScope}.
for (auto& code : codes) {
published_code.push_back(PublishCodeLocked(std::move(code)));
}
return published_code;
}
WasmCode::Kind GetCodeKind(const WasmCompilationResult& result) {
switch (result.kind) {
case WasmCompilationResult::kWasmToJsWrapper:
......@@ -1763,11 +1775,13 @@ void NativeModule::SampleCodeSize(
histogram->AddSample(code_size_mb);
}
WasmCode* NativeModule::AddCompiledCode(WasmCompilationResult result) {
return AddCompiledCode({&result, 1})[0];
std::unique_ptr<WasmCode> NativeModule::AddCompiledCode(
WasmCompilationResult result) {
std::vector<std::unique_ptr<WasmCode>> code = AddCompiledCode({&result, 1});
return std::move(code[0]);
}
std::vector<WasmCode*> NativeModule::AddCompiledCode(
std::vector<std::unique_ptr<WasmCode>> NativeModule::AddCompiledCode(
Vector<WasmCompilationResult> results) {
DCHECK(!results.empty());
// First, allocate code space for all the results.
......@@ -1799,17 +1813,7 @@ std::vector<WasmCode*> NativeModule::AddCompiledCode(
}
DCHECK_EQ(0, code_space.size());
// Under the {allocation_mutex_}, publish the code. The published code is put
// into the top-most surrounding {WasmCodeRefScope} by {PublishCodeLocked}.
std::vector<WasmCode*> code_vector;
code_vector.reserve(results.size());
{
base::MutexGuard lock(&allocation_mutex_);
for (auto& result : generated_code)
code_vector.push_back(PublishCodeLocked(std::move(result)));
}
return code_vector;
return generated_code;
}
bool NativeModule::IsRedirectedToInterpreter(uint32_t func_index) {
......
......@@ -440,10 +440,9 @@ class V8_EXPORT_PRIVATE NativeModule final {
// {PublishCode} makes the code available to the system by entering it into
// the code table and patching the jump table. It returns a raw pointer to the
// given {WasmCode} object.
// given {WasmCode} object. Ownership is transferred to the {NativeModule}.
WasmCode* PublishCode(std::unique_ptr<WasmCode>);
// Hold the {allocation_mutex_} when calling {PublishCodeLocked}.
WasmCode* PublishCodeLocked(std::unique_ptr<WasmCode>);
std::vector<WasmCode*> PublishCode(Vector<std::unique_ptr<WasmCode>>);
WasmCode* AddDeserializedCode(
int index, Vector<const byte> instructions, int stack_slots,
......@@ -565,8 +564,10 @@ class V8_EXPORT_PRIVATE NativeModule final {
enum CodeSamplingTime : int8_t { kAfterBaseline, kAfterTopTier, kSampling };
void SampleCodeSize(Counters*, CodeSamplingTime) const;
WasmCode* AddCompiledCode(WasmCompilationResult);
std::vector<WasmCode*> AddCompiledCode(Vector<WasmCompilationResult>);
V8_WARN_UNUSED_RESULT std::unique_ptr<WasmCode> AddCompiledCode(
WasmCompilationResult);
V8_WARN_UNUSED_RESULT std::vector<std::unique_ptr<WasmCode>> AddCompiledCode(
Vector<WasmCompilationResult>);
// Allows to check whether a function has been redirected to the interpreter
// by publishing an entry stub with the {Kind::kInterpreterEntry} code kind.
......@@ -636,6 +637,9 @@ class V8_EXPORT_PRIVATE NativeModule final {
void AddCodeSpace(base::AddressRegion,
const WasmCodeAllocator::OptionalLock&);
// Hold the {allocation_mutex_} when calling {PublishCodeLocked}.
WasmCode* PublishCodeLocked(std::unique_ptr<WasmCode>);
// Hold the {allocation_mutex_} when calling this method.
bool has_interpreter_redirection(uint32_t func_index) {
DCHECK_LT(func_index, num_functions());
......
......@@ -715,7 +715,9 @@ class DebugInfoImpl {
DCHECK_NOT_NULL(debug_sidetable);
WasmCodeRefScope wasm_code_ref_scope;
WasmCode* new_code = native_module_->AddCompiledCode(std::move(result));
WasmCode* new_code = native_module_->PublishCode(
native_module_->AddCompiledCode(std::move(result)));
bool added =
debug_side_tables_.emplace(new_code, std::move(debug_sidetable)).second;
DCHECK(added);
......
......@@ -586,7 +586,8 @@ void WasmFunctionCompiler::Build(const byte* start, const byte* end) {
isolate()->wasm_engine(), &env,
native_module->compilation_state()->GetWireBytesStorage(),
isolate()->counters(), &unused_detected_features);
WasmCode* code = native_module->AddCompiledCode(std::move(result));
WasmCode* code = native_module->PublishCode(
native_module->AddCompiledCode(std::move(result)));
DCHECK_NOT_NULL(code);
if (WasmCode::ShouldBeLogged(isolate())) code->LogCode(isolate());
}
......
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