Commit 6921a490 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Make StreamingDecoder abstract, introduce AsyncStreamingDecoder

The implementation of the StreamingDecoder depends on async compilation.
However, when the --single-threaded flag is set, async compilation is
not available. Therefore V8 does not support streaming compilation at
the moment if the --single-threaded flag is set.

This CL is the first step to support streaming compilation in
--single-threaded mode. This CL makes the StreamingDecoder an abstract
class, and the current implementation a sub-class called
AsyncStreamingDecoder. A follow-up CL will provided a second sub-class
implementation for streaming compilation in --single-threaded mode.

Bug: v8:10548
Change-Id: Ice5c01340d3df18f836a4a05d30571207ca8ccf6
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2208869
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarThibaud Michaud <thibaudm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67931}
parent 6b228044
...@@ -1591,8 +1591,9 @@ class AsyncStreamingProcessor final : public StreamingProcessor { ...@@ -1591,8 +1591,9 @@ class AsyncStreamingProcessor final : public StreamingProcessor {
std::shared_ptr<StreamingDecoder> AsyncCompileJob::CreateStreamingDecoder() { std::shared_ptr<StreamingDecoder> AsyncCompileJob::CreateStreamingDecoder() {
DCHECK_NULL(stream_); DCHECK_NULL(stream_);
stream_.reset(new StreamingDecoder(std::make_unique<AsyncStreamingProcessor>( stream_ = StreamingDecoder::CreateAsyncStreamingDecoder(
this, isolate_->async_counters(), isolate_->allocator()))); std::make_unique<AsyncStreamingProcessor>(
this, isolate_->async_counters(), isolate_->allocator()));
return stream_; return stream_;
} }
......
This diff is collapsed.
...@@ -66,220 +66,35 @@ class V8_EXPORT_PRIVATE StreamingProcessor { ...@@ -66,220 +66,35 @@ class V8_EXPORT_PRIVATE StreamingProcessor {
// and function bodies. // and function bodies.
class V8_EXPORT_PRIVATE StreamingDecoder { class V8_EXPORT_PRIVATE StreamingDecoder {
public: public:
explicit StreamingDecoder(std::unique_ptr<StreamingProcessor> processor); virtual ~StreamingDecoder() = default;
// The buffer passed into OnBytesReceived is owned by the caller. // The buffer passed into OnBytesReceived is owned by the caller.
void OnBytesReceived(Vector<const uint8_t> bytes); virtual void OnBytesReceived(Vector<const uint8_t> bytes) = 0;
void Finish(); virtual void Finish() = 0;
void Abort(); virtual void Abort() = 0;
// Notify the StreamingDecoder that compilation ended and the // Notify the StreamingDecoder that compilation ended and the
// StreamingProcessor should not be called anymore. // StreamingProcessor should not be called anymore.
void NotifyCompilationEnded() { Fail(); } virtual void NotifyCompilationEnded() = 0;
// Caching support. // Caching support.
// Sets the callback that is called after the module is fully compiled. // Sets the callback that is called after the module is fully compiled.
using ModuleCompiledCallback = using ModuleCompiledCallback =
std::function<void(const std::shared_ptr<NativeModule>&)>; std::function<void(const std::shared_ptr<NativeModule>&)>;
void SetModuleCompiledCallback(ModuleCompiledCallback callback); virtual void SetModuleCompiledCallback(ModuleCompiledCallback callback) = 0;
// Passes previously compiled module bytes from the embedder's cache. // Passes previously compiled module bytes from the embedder's cache.
bool SetCompiledModuleBytes(Vector<const uint8_t> compiled_module_bytes); virtual bool SetCompiledModuleBytes(
Vector<const uint8_t> compiled_module_bytes) = 0;
void NotifyNativeModuleCreated( virtual void NotifyNativeModuleCreated(
const std::shared_ptr<NativeModule>& native_module); const std::shared_ptr<NativeModule>& native_module) = 0;
Vector<const char> url() { return VectorOf(url_); } virtual Vector<const char> url() = 0;
void SetUrl(Vector<const char> url) { virtual void SetUrl(Vector<const char> url) = 0;
url_.assign(url.begin(), url.length()); static std::unique_ptr<StreamingDecoder> CreateAsyncStreamingDecoder(
} std::unique_ptr<StreamingProcessor> processor);
private:
// TODO(ahaas): Put the whole private state of the StreamingDecoder into the
// cc file (PIMPL design pattern).
// The SectionBuffer is the data object for the content of a single section.
// It stores all bytes of the section (including section id and section
// length), and the offset where the actual payload starts.
class SectionBuffer : public WireBytesStorage {
public:
// id: The section id.
// payload_length: The length of the payload.
// length_bytes: The section length, as it is encoded in the module bytes.
SectionBuffer(uint32_t module_offset, uint8_t id, size_t payload_length,
Vector<const uint8_t> length_bytes)
: // ID + length + payload
module_offset_(module_offset),
bytes_(OwnedVector<uint8_t>::New(1 + length_bytes.length() +
payload_length)),
payload_offset_(1 + length_bytes.length()) {
bytes_.start()[0] = id;
memcpy(bytes_.start() + 1, &length_bytes.first(), length_bytes.length());
}
SectionCode section_code() const {
return static_cast<SectionCode>(bytes_.start()[0]);
}
Vector<const uint8_t> GetCode(WireBytesRef ref) const final {
DCHECK_LE(module_offset_, ref.offset());
uint32_t offset_in_code_buffer = ref.offset() - module_offset_;
return bytes().SubVector(offset_in_code_buffer,
offset_in_code_buffer + ref.length());
}
uint32_t module_offset() const { return module_offset_; }
Vector<uint8_t> bytes() const { return bytes_.as_vector(); }
Vector<uint8_t> payload() const { return bytes() + payload_offset_; }
size_t length() const { return bytes_.size(); }
size_t payload_offset() const { return payload_offset_; }
private:
const uint32_t module_offset_;
const OwnedVector<uint8_t> bytes_;
const size_t payload_offset_;
};
// The decoding of a stream of wasm module bytes is organized in states. Each
// state provides a buffer to store the bytes required for the current state,
// information on how many bytes have already been received, how many bytes
// are needed, and a {Next} function which starts the next state once all
// bytes of the current state were received.
//
// The states change according to the following state diagram:
//
// Start
// |
// |
// v
// DecodeModuleHeader
// | _________________________________________
// | | |
// v v |
// DecodeSectionID --> DecodeSectionLength --> DecodeSectionPayload
// A |
// | | (if the section id == code)
// | v
// | DecodeNumberOfFunctions -- > DecodeFunctionLength
// | A |
// | | |
// | (after all functions were read) | v
// ------------------------------------- DecodeFunctionBody
//
class DecodingState {
public:
virtual ~DecodingState() = default;
// Reads the bytes for the current state and returns the number of read
// bytes.
virtual size_t ReadBytes(StreamingDecoder* streaming,
Vector<const uint8_t> bytes);
// Returns the next state of the streaming decoding.
virtual std::unique_ptr<DecodingState> Next(
StreamingDecoder* streaming) = 0;
// The buffer to store the received bytes.
virtual Vector<uint8_t> buffer() = 0;
// The number of bytes which were already received.
size_t offset() const { return offset_; }
void set_offset(size_t value) { offset_ = value; }
// A flag to indicate if finishing the streaming decoder is allowed without
// error.
virtual bool is_finishing_allowed() const { return false; }
private:
size_t offset_ = 0;
};
// Forward declarations of the concrete states. This is needed so that they
// can access private members of the StreamingDecoder.
class DecodeVarInt32;
class DecodeModuleHeader;
class DecodeSectionID;
class DecodeSectionLength;
class DecodeSectionPayload;
class DecodeNumberOfFunctions;
class DecodeFunctionLength;
class DecodeFunctionBody;
// Creates a buffer for the next section of the module.
SectionBuffer* CreateNewBuffer(uint32_t module_offset, uint8_t section_id,
size_t length,
Vector<const uint8_t> length_bytes);
std::unique_ptr<DecodingState> Error(const WasmError& error) {
if (ok()) processor_->OnError(error);
Fail();
return std::unique_ptr<DecodingState>(nullptr);
}
std::unique_ptr<DecodingState> Error(std::string message) {
return Error(WasmError{module_offset_ - 1, std::move(message)});
}
void ProcessModuleHeader() {
if (!ok()) return;
if (!processor_->ProcessModuleHeader(state_->buffer(), 0)) Fail();
}
void ProcessSection(SectionBuffer* buffer) {
if (!ok()) return;
if (!processor_->ProcessSection(
buffer->section_code(), buffer->payload(),
buffer->module_offset() +
static_cast<uint32_t>(buffer->payload_offset()))) {
Fail();
}
}
void StartCodeSection(int num_functions,
std::shared_ptr<WireBytesStorage> wire_bytes_storage,
int code_section_length) {
if (!ok()) return;
// The offset passed to {ProcessCodeSectionHeader} is an error offset and
// not the start offset of a buffer. Therefore we need the -1 here.
if (!processor_->ProcessCodeSectionHeader(
num_functions, module_offset() - 1, std::move(wire_bytes_storage),
code_section_length)) {
Fail();
}
}
void ProcessFunctionBody(Vector<const uint8_t> bytes,
uint32_t module_offset) {
if (!ok()) return;
if (!processor_->ProcessFunctionBody(bytes, module_offset)) Fail();
}
void Fail() {
// We reset the {processor_} field to represent failure. This also ensures
// that we do not accidentally call further methods on the processor after
// failure.
processor_.reset();
}
bool ok() const { return processor_ != nullptr; }
uint32_t module_offset() const { return module_offset_; }
bool deserializing() const { return !compiled_module_bytes_.empty(); }
std::unique_ptr<StreamingProcessor> processor_;
std::unique_ptr<DecodingState> state_;
std::vector<std::shared_ptr<SectionBuffer>> section_buffers_;
bool code_section_processed_ = false;
uint32_t module_offset_ = 0;
size_t total_size_ = 0;
std::string url_;
// Caching support.
ModuleCompiledCallback module_compiled_callback_ = nullptr;
// We need wire bytes in an array for deserializing cached modules.
std::vector<uint8_t> wire_bytes_for_deserializing_;
Vector<const uint8_t> compiled_module_bytes_;
DISALLOW_COPY_AND_ASSIGN(StreamingDecoder);
}; };
} // namespace wasm } // namespace wasm
......
...@@ -99,11 +99,11 @@ class WasmStreamingDecoderTest : public ::testing::Test { ...@@ -99,11 +99,11 @@ class WasmStreamingDecoderTest : public ::testing::Test {
size_t expected_functions) { size_t expected_functions) {
for (int split = 0; split <= data.length(); ++split) { for (int split = 0; split <= data.length(); ++split) {
MockStreamingResult result; MockStreamingResult result;
StreamingDecoder stream( auto stream = StreamingDecoder::CreateAsyncStreamingDecoder(
std::make_unique<MockStreamingProcessor>(&result)); std::make_unique<MockStreamingProcessor>(&result));
stream.OnBytesReceived(data.SubVector(0, split)); stream->OnBytesReceived(data.SubVector(0, split));
stream.OnBytesReceived(data.SubVector(split, data.length())); stream->OnBytesReceived(data.SubVector(split, data.length()));
stream.Finish(); stream->Finish();
EXPECT_TRUE(result.ok()); EXPECT_TRUE(result.ok());
EXPECT_EQ(expected_sections, result.num_sections); EXPECT_EQ(expected_sections, result.num_sections);
EXPECT_EQ(expected_functions, result.num_functions); EXPECT_EQ(expected_functions, result.num_functions);
...@@ -115,11 +115,11 @@ class WasmStreamingDecoderTest : public ::testing::Test { ...@@ -115,11 +115,11 @@ class WasmStreamingDecoderTest : public ::testing::Test {
const char* message) { const char* message) {
for (int split = 0; split <= data.length(); ++split) { for (int split = 0; split <= data.length(); ++split) {
MockStreamingResult result; MockStreamingResult result;
StreamingDecoder stream( auto stream = StreamingDecoder::CreateAsyncStreamingDecoder(
std::make_unique<MockStreamingProcessor>(&result)); std::make_unique<MockStreamingProcessor>(&result));
stream.OnBytesReceived(data.SubVector(0, split)); stream->OnBytesReceived(data.SubVector(0, split));
stream.OnBytesReceived(data.SubVector(split, data.length())); stream->OnBytesReceived(data.SubVector(split, data.length()));
stream.Finish(); stream->Finish();
EXPECT_FALSE(result.ok()); EXPECT_FALSE(result.ok());
EXPECT_EQ(error_offset, result.error.offset()); EXPECT_EQ(error_offset, result.error.offset());
EXPECT_EQ(message, result.error.message()); EXPECT_EQ(message, result.error.message());
...@@ -129,8 +129,9 @@ class WasmStreamingDecoderTest : public ::testing::Test { ...@@ -129,8 +129,9 @@ class WasmStreamingDecoderTest : public ::testing::Test {
TEST_F(WasmStreamingDecoderTest, EmptyStream) { TEST_F(WasmStreamingDecoderTest, EmptyStream) {
MockStreamingResult result; MockStreamingResult result;
StreamingDecoder stream(std::make_unique<MockStreamingProcessor>(&result)); auto stream = StreamingDecoder::CreateAsyncStreamingDecoder(
stream.Finish(); std::make_unique<MockStreamingProcessor>(&result));
stream->Finish();
EXPECT_FALSE(result.ok()); EXPECT_FALSE(result.ok());
} }
...@@ -138,9 +139,10 @@ TEST_F(WasmStreamingDecoderTest, IncompleteModuleHeader) { ...@@ -138,9 +139,10 @@ TEST_F(WasmStreamingDecoderTest, IncompleteModuleHeader) {
const uint8_t data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion)}; const uint8_t data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion)};
{ {
MockStreamingResult result; MockStreamingResult result;
StreamingDecoder stream(std::make_unique<MockStreamingProcessor>(&result)); auto stream = StreamingDecoder::CreateAsyncStreamingDecoder(
stream.OnBytesReceived(VectorOf(data, 1)); std::make_unique<MockStreamingProcessor>(&result));
stream.Finish(); stream->OnBytesReceived(VectorOf(data, 1));
stream->Finish();
EXPECT_FALSE(result.ok()); EXPECT_FALSE(result.ok());
} }
for (uint32_t length = 1; length < sizeof(data); ++length) { for (uint32_t length = 1; length < sizeof(data); ++length) {
......
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