[wasm] Initialize compilation progress before compilation units

Initializing the compilation progress first, and making it an explicit
step, allows us to share the wrapper compilation code between async
compilation and streaming compilation. In the future it will also allow
us to initialize compilation units based on the compilation progress,
which will allow us to share more code between async compilation and
deserialization.

R=clemensb@chromium.org

Bug: v8:11862
Change-Id: If8394f3068840f759b8325ba806549dbd208f195
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2978258
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#75380}
This commit is contained in:
Andreas Haas 2021-06-24 17:36:48 +02:00 committed by V8 LUCI CQ
parent a56a581b45
commit 3161106e77

View File

@ -1424,13 +1424,28 @@ int AddImportWrapperUnits(NativeModule* native_module,
return static_cast<int>(keys.size());
}
void InitializeCompilationUnits(Isolate* isolate, NativeModule* native_module) {
std::unique_ptr<CompilationUnitBuilder> InitializeCompilation(
Isolate* isolate, NativeModule* native_module) {
CompilationStateImpl* compilation_state =
Impl(native_module->compilation_state());
const bool lazy_module = IsLazyModule(native_module->module());
ModuleWireBytes wire_bytes(native_module->wire_bytes());
CompilationUnitBuilder builder(native_module);
auto builder = std::make_unique<CompilationUnitBuilder>(native_module);
int num_import_wrappers = AddImportWrapperUnits(native_module, builder.get());
int num_export_wrappers =
AddExportWrapperUnits(isolate, native_module, builder.get(),
WasmFeatures::FromIsolate(isolate));
compilation_state->InitializeCompilationProgress(
lazy_module, num_import_wrappers, num_export_wrappers);
return builder;
}
void InitializeCompilationUnits(
NativeModule* native_module,
std::unique_ptr<CompilationUnitBuilder> builder) {
const bool lazy_module = IsLazyModule(native_module->module());
auto* module = native_module->module();
const bool prefer_liftoff = native_module->IsTieredDown();
uint32_t start = module->num_imported_functions;
@ -1438,13 +1453,13 @@ void InitializeCompilationUnits(Isolate* isolate, NativeModule* native_module) {
base::Optional<CodeSpaceWriteScope> lazy_code_space_write_scope;
for (uint32_t func_index = start; func_index < end; func_index++) {
if (prefer_liftoff) {
builder.AddRecompilationUnit(func_index, ExecutionTier::kLiftoff);
builder->AddRecompilationUnit(func_index, ExecutionTier::kLiftoff);
continue;
}
CompileStrategy strategy = GetCompileStrategy(
module, native_module->enabled_features(), func_index, lazy_module);
if (strategy == CompileStrategy::kEager) {
builder.AddUnits(func_index);
builder->AddUnits(func_index);
} else {
// Open a single scope for all following calls to {UseLazyStub()}, instead
// of flipping page permissions for each {func_index} individually.
@ -1455,17 +1470,12 @@ void InitializeCompilationUnits(Isolate* isolate, NativeModule* native_module) {
native_module->UseLazyStub(func_index);
} else {
DCHECK_EQ(strategy, CompileStrategy::kLazyBaselineEagerTopTier);
builder.AddTopTierUnit(func_index);
builder->AddTopTierUnit(func_index);
native_module->UseLazyStub(func_index);
}
}
}
int num_import_wrappers = AddImportWrapperUnits(native_module, &builder);
int num_export_wrappers = AddExportWrapperUnits(
isolate, native_module, &builder, WasmFeatures::FromIsolate(isolate));
compilation_state->InitializeCompilationProgress(
lazy_module, num_import_wrappers, num_export_wrappers);
builder.Commit();
builder->Commit();
}
bool MayCompriseLazyFunctions(const WasmModule* module,
@ -1597,7 +1607,9 @@ void CompileNativeModule(Isolate* isolate,
}
// Initialize the compilation units and kick off background compile tasks.
InitializeCompilationUnits(isolate, native_module.get());
std::unique_ptr<CompilationUnitBuilder> builder =
InitializeCompilation(isolate, native_module.get());
InitializeCompilationUnits(native_module.get(), std::move(builder));
compilation_state->WaitForCompilationEvent(
CompilationEvent::kFinishedExportWrappers);
@ -2368,7 +2380,9 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
// then DoAsync would do the same as NextStep already.
// Add compilation units and kick off compilation.
InitializeCompilationUnits(job->isolate(), job->native_module_.get());
std::unique_ptr<CompilationUnitBuilder> builder =
InitializeCompilation(job->isolate(), job->native_module_.get());
InitializeCompilationUnits(job->native_module_.get(), std::move(builder));
// We are in single-threaded mode, so there are no worker tasks that will
// do the compilation. We call {WaitForCompilationEvent} here so that the
// main thread paticipates and finishes the compilation.
@ -2599,23 +2613,12 @@ bool AsyncStreamingProcessor::ProcessCodeSectionHeader(
auto* compilation_state = Impl(job_->native_module_->compilation_state());
compilation_state->SetWireBytesStorage(std::move(wire_bytes_storage));
DCHECK_EQ(job_->native_module_->module()->origin, kWasmOrigin);
const bool lazy_module = job_->wasm_lazy_compilation_;
// Set outstanding_finishers_ to 2, because both the AsyncCompileJob and the
// AsyncStreamingProcessor have to finish.
job_->outstanding_finishers_.store(2);
compilation_unit_builder_.reset(
new CompilationUnitBuilder(job_->native_module_.get()));
NativeModule* native_module = job_->native_module_.get();
int num_import_wrappers =
AddImportWrapperUnits(native_module, compilation_unit_builder_.get());
int num_export_wrappers = AddExportWrapperUnits(
job_->isolate_, native_module, compilation_unit_builder_.get(),
job_->enabled_features_);
compilation_state->InitializeCompilationProgress(
lazy_module, num_import_wrappers, num_export_wrappers);
compilation_unit_builder_ =
InitializeCompilation(job_->isolate(), job_->native_module_.get());
return true;
}