Commit 3ca11472 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Do not start background tasks after compilation is finished

Finishing a chunk of data during streaming compilation caused background
tasks to be restarted unconditionally. However, restarting background
tasks is not possible after compilation has already finished. With this
CL we do not allow anymore to restart background tasks after they have
been finished.

R=clemensh@chromium.org
CC=mtrofin@chromium.org

Change-Id: I4c0a9761fb627f04b254f72e05873e29e7647eb0
Reviewed-on: https://chromium-review.googlesource.com/827008
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50115}
parent a2baa60f
......@@ -104,7 +104,9 @@ class ModuleCompiler {
compiler_->counters()));
}
void Commit() {
bool Commit() {
if (units_.empty()) return false;
{
base::LockGuard<base::Mutex> guard(
&compiler_->compilation_units_mutex_);
......@@ -114,6 +116,7 @@ class ModuleCompiler {
std::make_move_iterator(units_.end()));
}
units_.clear();
return true;
}
void Clear() { units_.clear(); }
......@@ -3660,6 +3663,8 @@ class AsyncStreamingProcessor final : public StreamingProcessor {
// Finishes the AsyncCOmpileJob with an error.
void FinishAsyncCompileJobWithError(ResultBase result);
void CommitCompilationUnits();
ModuleDecoder decoder_;
AsyncCompileJob* job_;
std::unique_ptr<ModuleCompiler::CompilationUnitBuilder>
......@@ -4195,6 +4200,12 @@ bool AsyncStreamingProcessor::ProcessSection(SectionCode section_code,
Vector<const uint8_t> bytes,
uint32_t offset) {
TRACE_STREAMING("Process section %d ...\n", section_code);
if (compilation_unit_builder_) {
// We reached a section after the code section, we do not need the the
// compilation_unit_builder_ anymore.
CommitCompilationUnits();
compilation_unit_builder_.reset();
}
if (section_code == SectionCode::kUnknownSectionCode) {
// No need to decode unknown sections, even the names section. If decoding
// of the unknown section fails, compilation should succeed anyways, and
......@@ -4262,14 +4273,19 @@ bool AsyncStreamingProcessor::ProcessFunctionBody(Vector<const uint8_t> bytes,
return true;
}
void AsyncStreamingProcessor::OnFinishedChunk() {
// TRACE_STREAMING("FinishChunk...\n");
if (compilation_unit_builder_) {
compilation_unit_builder_->Commit();
void AsyncStreamingProcessor::CommitCompilationUnits() {
DCHECK(compilation_unit_builder_);
if (compilation_unit_builder_->Commit()) {
// Only restart background tasks when compilation units were committed.
job_->RestartBackgroundTasks();
}
}
void AsyncStreamingProcessor::OnFinishedChunk() {
TRACE_STREAMING("FinishChunk...\n");
if (compilation_unit_builder_) CommitCompilationUnits();
}
// Finish the processing of the stream.
void AsyncStreamingProcessor::OnFinishedStream(std::unique_ptr<uint8_t[]> bytes,
size_t length) {
......
......@@ -910,6 +910,48 @@ STREAM_TEST(TestModuleWithMultipleFunctions) {
CHECK(tester.IsPromiseFulfilled());
}
STREAM_TEST(TestModuleWithDataSection) {
StreamTester tester;
uint8_t code[] = {
U32V_1(4), // body size
U32V_1(0), // locals count
kExprGetLocal, 0, kExprEnd // body
};
const uint8_t bytes[] = {
WASM_MODULE_HEADER, // module header
kTypeSectionCode, // section code
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
U32V_1(1), // type count
SIG_ENTRY_x_x(kLocalI32, kLocalI32), // signature entry
kFunctionSectionCode, // section code
U32V_1(1 + 3), // section size
U32V_1(3), // functions count
0, // signature index
0, // signature index
0, // signature index
kCodeSectionCode, // section code
U32V_1(1 + arraysize(code) * 3), // section size
U32V_1(3), // functions count
};
const uint8_t data_section[] = {
kDataSectionCode, // section code
U32V_1(1), // section size
U32V_1(0), // data segment count
};
tester.OnBytesReceived(bytes, arraysize(bytes));
tester.OnBytesReceived(code, arraysize(code));
tester.OnBytesReceived(code, arraysize(code));
tester.OnBytesReceived(code, arraysize(code));
tester.RunCompilerTasks();
tester.OnBytesReceived(data_section, arraysize(data_section));
tester.RunCompilerTasks();
tester.FinishStream();
tester.RunCompilerTasks();
CHECK(tester.IsPromiseFulfilled());
}
// Test that all bytes arrive before doing any compilation. FinishStream is
// called immediately.
STREAM_TEST(TestModuleWithImportedFunction) {
......
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