Commit 61cc328b authored by Ng Zhi An's avatar Ng Zhi An Committed by Commit Bot

Revert "[wasm] Perform NativeModule tier down in parallel."

This reverts commit 3352fcc9.

Reason for revert: Causing wasm/tier-down-to-liftoff.js to be flaky, https://crbug.com/v8/10086

Original change's description:
> [wasm] Perform NativeModule tier down in parallel.
> 
> Reuse logic in {CompileNativeModule} function in module-compiler.cc:
> initialize parallel compile jobs, then wait for them to finish while
> taking part in this compilation.
> 
> Bug: v8:9654
> Change-Id: I9974d9f8b516e9faec716a592c7c0ee9c7077d8e
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1977041
> Commit-Queue: Z Nguyen-Huu <duongn@microsoft.com>
> Reviewed-by: Clemens Backes <clemensb@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#65763}

TBR=rmcilroy@chromium.org,clemensb@chromium.org,duongn@microsoft.com

Change-Id: Ie3a0a3b2315879b6c19ef25f435fdc83c297b23b
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: v8:9654
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2002692Reviewed-by: 's avatarZhi An Ng <zhin@chromium.org>
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65797}
parent 875fe59b
......@@ -92,8 +92,7 @@ class WireBytesStorage {
enum class CompilationEvent : uint8_t {
kFinishedBaselineCompilation,
kFinishedTopTierCompilation,
kFailedCompilation,
kFinishedRecompilation
kFailedCompilation
};
// The implementation of {CompilationState} lives in module-compiler.cc.
......
......@@ -381,9 +381,6 @@ class CompilationStateImpl {
// is invoked which triggers background compilation.
void InitializeCompilationProgress(bool lazy_module, int num_wrappers);
// Initialize compilation progress for recompilation of the whole module.
void InitializeRecompilationProgress(ExecutionTier tier);
// Add the callback function to be called on compilation events. Needs to be
// set before {AddCompilationUnits} is run to ensure that it receives all
// events. The callback object must support being deleted from any thread.
......@@ -407,8 +404,7 @@ class CompilationStateImpl {
void OnFinishedUnits(Vector<WasmCode*>);
void OnFinishedJSToWasmWrapperUnits(int num);
void TriggerCallbacks(bool completes_baseline_compilation,
bool completes_top_tier_compilation,
bool completes_recompilation = false);
bool completes_top_tier_compilation);
void OnBackgroundTaskStopped(int task_id, const WasmFeatures& detected);
void UpdateDetectedFeatures(const WasmFeatures& detected);
......@@ -540,7 +536,6 @@ class CompilationStateImpl {
int outstanding_top_tier_functions_ = 0;
std::vector<uint8_t> compilation_progress_;
int outstanding_recompilation_functions_ = 0;
// End of fields protected by {callbacks_mutex_}.
//////////////////////////////////////////////////////////////////////////////
......@@ -752,10 +747,6 @@ class CompilationUnitBuilder {
tiering_units_.emplace_back(func_index, tiers.top_tier);
}
void AddBaselineUnit(int func_index) {
baseline_units_.emplace_back(func_index, ExecutionTier::kLiftoff);
}
bool Commit() {
if (baseline_units_.empty() && tiering_units_.empty() &&
js_to_wasm_wrapper_units_.empty()) {
......@@ -1204,19 +1195,6 @@ void InitializeCompilationUnits(Isolate* isolate, NativeModule* native_module) {
builder.Commit();
}
void AddBaselineCompilationUnits(NativeModule* native_module) {
CompilationUnitBuilder builder(native_module);
auto* module = native_module->module();
uint32_t start = module->num_imported_functions;
uint32_t end = start + module->num_declared_functions;
for (uint32_t func_index = start; func_index < end; func_index++) {
builder.AddBaselineUnit(func_index);
}
builder.Commit();
}
bool MayCompriseLazyFunctions(const WasmModule* module,
const WasmFeatures& enabled_features,
bool lazy_module) {
......@@ -1410,40 +1388,6 @@ std::shared_ptr<NativeModule> CompileToNativeModule(
return native_module;
}
void RecompileNativeModule(Isolate* isolate, NativeModule* native_module,
ExecutionTier tier) {
// Install a callback to notify us once background recompilation finished.
auto recompilation_finished_semaphore = std::make_shared<base::Semaphore>(0);
auto* compilation_state = Impl(native_module->compilation_state());
DCHECK_EQ(tier, ExecutionTier::kLiftoff);
// The callback captures a shared ptr to the semaphore.
compilation_state->AddCallback(
[recompilation_finished_semaphore](CompilationEvent event) {
if (event == CompilationEvent::kFinishedRecompilation) {
recompilation_finished_semaphore->Signal();
}
});
// Initialize the compilation units and kick off background compile tasks.
compilation_state->InitializeRecompilationProgress(tier);
AddBaselineCompilationUnits(native_module);
// The main thread contributes to the compilation, except if we need
// deterministic compilation; in that case, the single background task will
// execute all compilation.
if (!NeedsDeterministicCompile()) {
while (ExecuteCompilationUnits(
compilation_state->background_compile_token(), isolate->counters(),
kMainThreadTaskId, kBaselineOnly)) {
// Continue executing compilation units.
}
}
// Now wait until baseline recompilation finished.
recompilation_finished_semaphore->Wait();
DCHECK(!compilation_state->failed());
}
AsyncCompileJob::AsyncCompileJob(
Isolate* isolate, const WasmFeatures& enabled,
std::unique_ptr<byte[]> bytes_copy, size_t length, Handle<Context> context,
......@@ -1679,14 +1623,13 @@ class AsyncCompileJob::CompilationStateCallback {
// At this point, the job will already be gone, thus do not access it
// here.
break;
case CompilationEvent::kFailedCompilation:
case CompilationEvent::kFailedCompilation: {
DCHECK(!last_event_.has_value());
if (job_->DecrementAndCheckFinisherCount()) {
job_->DoSync<CompileFailed>();
}
break;
case CompilationEvent::kFinishedRecompilation:
break;
}
default:
UNREACHABLE();
}
......@@ -1988,7 +1931,6 @@ class SampleTopTierCodeSizeCallback {
void operator()(CompilationEvent event) {
// This callback is registered after baseline compilation finished, so the
// only possible event to follow is {kFinishedTopTierCompilation}.
if (event == CompilationEvent::kFinishedRecompilation) return;
DCHECK_EQ(CompilationEvent::kFinishedTopTierCompilation, event);
if (std::shared_ptr<NativeModule> native_module = native_module_.lock()) {
native_module->engine()->SampleTopTierCodeSizeInAllIsolates(
......@@ -2378,37 +2320,6 @@ void CompilationStateImpl::InitializeCompilationProgress(bool lazy_module,
}
}
void CompilationStateImpl::InitializeRecompilationProgress(ExecutionTier tier) {
DCHECK(!failed());
auto* module = native_module_->module();
base::MutexGuard guard(&callbacks_mutex_);
// Ensure that we don't trigger recompilation if another recompilation is
// already happening.
DCHECK_EQ(0, outstanding_recompilation_functions_);
int start = module->num_imported_functions;
int end = start + module->num_declared_functions;
for (int function_index = start; function_index < end; function_index++) {
int slot_index = function_index - start;
DCHECK_LT(slot_index, compilation_progress_.size());
ExecutionTier reached_tier =
ReachedTierField::decode(compilation_progress_[slot_index]);
if (reached_tier != tier) {
outstanding_recompilation_functions_++;
}
}
DCHECK_LE(0, outstanding_recompilation_functions_);
DCHECK_LE(outstanding_recompilation_functions_,
module->num_declared_functions);
// Trigger callbacks if module needs no recompilation.
if (outstanding_recompilation_functions_ == 0) {
for (auto& callback : callbacks_) {
callback(CompilationEvent::kFinishedRecompilation);
}
}
}
void CompilationStateImpl::AddCallback(CompilationState::callback_t callback) {
base::MutexGuard callbacks_guard(&callbacks_mutex_);
callbacks_.emplace_back(std::move(callback));
......@@ -2480,8 +2391,7 @@ void CompilationStateImpl::OnFinishedUnits(Vector<WasmCode*> code_vector) {
// This is especially important for lazy modules that were deserialized.
// Compilation progress was not set up in these cases.
if (outstanding_baseline_units_ == 0 &&
outstanding_top_tier_functions_ == 0 &&
outstanding_recompilation_functions_ == 0) {
outstanding_top_tier_functions_ == 0) {
return;
}
......@@ -2497,7 +2407,6 @@ void CompilationStateImpl::OnFinishedUnits(Vector<WasmCode*> code_vector) {
bool completes_baseline_compilation = false;
bool completes_top_tier_compilation = false;
bool completes_recompilation = false;
for (WasmCode* code : code_vector) {
DCHECK_NOT_NULL(code);
......@@ -2545,34 +2454,17 @@ void CompilationStateImpl::OnFinishedUnits(Vector<WasmCode*> code_vector) {
}
}
// If there is recompilation in progress, we would only count the
// functions which are not Liftoff already, and would only decrement the
// counter once a function reaches Liftoff.
if (outstanding_recompilation_functions_ > 0) {
// TODO(duongn): extend this logic for tier up.
if (code->tier() == ExecutionTier::kLiftoff &&
reached_tier != ExecutionTier::kLiftoff) {
outstanding_recompilation_functions_--;
// Update function's compilation progress.
compilation_progress_[slot_index] = ReachedTierField::update(
compilation_progress_[slot_index], code->tier());
if (outstanding_recompilation_functions_ == 0) {
completes_recompilation = true;
}
}
} else {
// Update function's compilation progress.
if (code->tier() > reached_tier) {
compilation_progress_[slot_index] = ReachedTierField::update(
compilation_progress_[slot_index], code->tier());
}
// Update function's compilation progress.
if (code->tier() > reached_tier) {
compilation_progress_[slot_index] = ReachedTierField::update(
compilation_progress_[slot_index], code->tier());
}
DCHECK_LE(0, outstanding_baseline_units_);
}
}
TriggerCallbacks(completes_baseline_compilation,
completes_top_tier_compilation, completes_recompilation);
completes_top_tier_compilation);
}
void CompilationStateImpl::OnFinishedJSToWasmWrapperUnits(int num) {
......@@ -2583,14 +2475,8 @@ void CompilationStateImpl::OnFinishedJSToWasmWrapperUnits(int num) {
TriggerCallbacks(completes_baseline_compilation, false);
}
void CompilationStateImpl::TriggerCallbacks(bool completes_baseline_compilation,
bool completes_top_tier_compilation,
bool completes_recompilation) {
if (completes_recompilation) {
for (auto& callback : callbacks_) {
callback(CompilationEvent::kFinishedRecompilation);
}
}
void CompilationStateImpl::TriggerCallbacks(
bool completes_baseline_compilation, bool completes_top_tier_compilation) {
if (completes_baseline_compilation) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm"), "BaselineFinished");
for (auto& callback : callbacks_) {
......
......@@ -44,9 +44,6 @@ std::shared_ptr<NativeModule> CompileToNativeModule(
std::shared_ptr<const WasmModule> module, const ModuleWireBytes& wire_bytes,
Handle<FixedArray>* export_wrappers_out);
void RecompileNativeModule(Isolate* isolate, NativeModule* native_module,
ExecutionTier tier);
V8_EXPORT_PRIVATE
void CompileJsToWasmWrappers(Isolate* isolate, const WasmModule* module,
Handle<FixedArray>* export_wrappers_out);
......
......@@ -1818,8 +1818,13 @@ void NativeModule::TierDown(Isolate* isolate) {
tier_down_ = true;
}
// Tier down all functions.
isolate->wasm_engine()->RecompileAllFunctions(isolate, this,
ExecutionTier::kLiftoff);
// TODO(duongn): parallelize this eventually.
for (uint32_t index = module_->num_imported_functions;
index < num_functions(); index++) {
isolate->wasm_engine()->CompileFunction(isolate, this, index,
ExecutionTier::kLiftoff);
DCHECK(!compilation_state()->failed());
}
}
void NativeModule::TierUp(Isolate* isolate) {
......
......@@ -429,12 +429,6 @@ void WasmEngine::CompileFunction(Isolate* isolate, NativeModule* native_module,
&native_module->module()->functions[function_index], tier);
}
void WasmEngine::RecompileAllFunctions(Isolate* isolate,
NativeModule* native_module,
ExecutionTier tier) {
RecompileNativeModule(isolate, native_module, tier);
}
std::shared_ptr<NativeModule> WasmEngine::ExportNativeModule(
Handle<WasmModuleObject> module_object) {
return module_object->shared_native_module();
......
......@@ -111,10 +111,6 @@ class V8_EXPORT_PRIVATE WasmEngine {
void CompileFunction(Isolate* isolate, NativeModule* native_module,
uint32_t function_index, ExecutionTier tier);
// Recompiles all functions at a specific compilation tier.
void RecompileAllFunctions(Isolate* isolate, NativeModule* native_module,
ExecutionTier tier);
// Exports the sharable parts of the given module object so that they can be
// transferred to a different Context/Isolate using the same engine.
std::shared_ptr<NativeModule> ExportNativeModule(
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax --liftoff --wasm-tier-up --no-stress-opt
// Flags: --allow-natives-syntax --wasm-tier-up
load('test/mjsunit/wasm/wasm-module-builder.js');
......
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