dart-sdk/runtime/vm/bitmap.cc
Alexander Markov 5f985eb3e6 Reland "[vm/compiler] Initial implementation of IL binary serialization"
This is a reland of commit 9700458975

Original change's description:
> [vm/compiler] Initial implementation of IL binary serialization
>
> This change adds binary serialization/deserialization of flow graphs.
> It supports all IL instructions and certain objects which can be
> referenced from IL instructions. IL binary serialization is a useful
> machanism which would allow us to split compilation into multiple parts
> in order to parallelize AOT compilation.
>
> The program structure (libraries/classes/functions/fields) is not
> serialized. It is assumed that reader and writer use the same
> program structure.
>
> Caveats:
> * FFI callbacks are not supported yet.
> * Closure functions are not re-created when reading flow graph.
> * Flow graph should be in SSA form (unoptimized flow graphs are not
>   supported).
> * JIT mode is not supported (serializer currently assumes lazy
>   linking of native methods and empty ICData).
>
> In order to test IL serialization, --test_il_serialization VM option is
> added to serialize and deserialize flow graph before generating code.

TEST=vm/dart/splay_test now runs with --test_il_serialization.
TEST=Manual run of vm-kernel-precomp-linux-debug-x64-try with
--test_il_serialization enabled (only ffi tests failed).
TEST=gcc build on dart-sdk-linux-try bot.

Issue: https://github.com/dart-lang/sdk/issues/43299

> Change-Id: I7bbfd9e3a301e00c9cfbffa06b8f1f6c78a78470
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254941
> Reviewed-by: Ryan Macnak <rmacnak@google.com>
> Commit-Queue: Alexander Markov <alexmarkov@google.com>
> Reviewed-by: Slava Egorov <vegorov@google.com>

Change-Id: I64ff9747f761496a096371e490ef070a14023256
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255840
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
2022-08-22 15:07:47 +00:00

174 lines
5.6 KiB
C++

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/bitmap.h"
#include "platform/assert.h"
#include "vm/object.h"
#include "vm/log.h"
namespace dart {
void BitmapBuilder::SetLength(intptr_t new_length) {
// When this function is used to shorten the length, affected bits in the
// backing store need to be cleared because the implementation assumes it.
if (new_length < length_) {
// Byte offset containing the first bit to be cleared.
intptr_t byte_offset = new_length >> kBitsPerByteLog2;
if (byte_offset < data_size_in_bytes_) {
// First bit index (in the byte) to be cleared.
intptr_t bit_index = new_length & (kBitsPerByte - 1);
intptr_t mask = (1 << bit_index) - 1;
BackingStore()[byte_offset] &= mask;
// Clear the rest.
++byte_offset;
if (byte_offset < data_size_in_bytes_) {
memset(&BackingStore()[byte_offset], 0,
data_size_in_bytes_ - byte_offset);
}
}
}
length_ = new_length;
}
bool BitmapBuilder::Get(intptr_t bit_offset) const {
if (!InRange(bit_offset)) {
return false;
}
intptr_t byte_offset = bit_offset >> kBitsPerByteLog2;
// Bits not covered by the backing store are implicitly false.
return (byte_offset < data_size_in_bytes_) && GetBit(bit_offset);
}
void BitmapBuilder::Set(intptr_t bit_offset, bool value) {
if (!InRange(bit_offset)) {
length_ = bit_offset + 1;
}
// Bits not covered by the backing store are implicitly false.
// Grow the backing store if necessary.
if (value) {
if (!InBackingStore(bit_offset)) {
intptr_t byte_offset = bit_offset >> kBitsPerByteLog2;
uint8_t* old_data = BackingStore();
intptr_t old_size = data_size_in_bytes_;
data_size_in_bytes_ =
Utils::RoundUp(byte_offset + 1, kIncrementSizeInBytes);
ASSERT(data_size_in_bytes_ > 0);
// Note: do not update data_ yet because it might overwrite old_data
// contents.
uint8_t* new_data = AllocBackingStore(data_size_in_bytes_);
memmove(new_data, old_data, old_size);
memset(&new_data[old_size], 0, (data_size_in_bytes_ - old_size));
data_.ptr_ = new_data;
}
ASSERT(InBackingStore(bit_offset));
}
// Set bit if in backing store.
if (InBackingStore(bit_offset)) {
SetBit(bit_offset, value);
}
}
void BitmapBuilder::SetRange(intptr_t min, intptr_t max, bool value) {
for (intptr_t i = min; i <= max; i++) {
Set(i, value);
}
}
void BitmapBuilder::Print() const {
for (intptr_t i = 0; i < Length(); i++) {
if (Get(i)) {
THR_Print("1");
} else {
THR_Print("0");
}
}
}
void BitmapBuilder::AppendAsBytesTo(BaseWriteStream* stream) const {
// Early return if there are no bits in the payload to copy.
if (Length() == 0) return;
const intptr_t total_size =
Utils::RoundUp(Length(), kBitsPerByte) / kBitsPerByte;
intptr_t payload_size;
intptr_t extra_size;
if (total_size > data_size_in_bytes_) {
// A [BitmapBuilder] does not allocate storage for the trailing 0 bits in
// the backing store, so we need to add additional empty bytes here.
payload_size = data_size_in_bytes_;
extra_size = total_size - data_size_in_bytes_;
} else {
payload_size = total_size;
extra_size = 0;
}
#if defined(DEBUG)
// Make sure any bits in the payload beyond the bit length if we're not
// appending trailing zeroes are cleared to ensure deterministic snapshots.
if (extra_size == 0 && Length() % kBitsPerByte != 0) {
const int8_t mask = (1 << (Length() % kBitsPerByte)) - 1;
ASSERT_EQUAL(BackingStore()[payload_size - 1],
(BackingStore()[payload_size - 1] & mask));
}
#endif
stream->WriteBytes(BackingStore(), payload_size);
for (intptr_t i = 0; i < extra_size; i++) {
stream->WriteByte(0U);
}
}
bool BitmapBuilder::GetBit(intptr_t bit_offset) const {
if (!InRange(bit_offset)) {
return false;
}
intptr_t byte_offset = bit_offset >> kBitsPerByteLog2;
ASSERT(byte_offset < data_size_in_bytes_);
intptr_t bit_remainder = bit_offset & (kBitsPerByte - 1);
uint8_t mask = 1U << bit_remainder;
return ((BackingStore()[byte_offset] & mask) != 0);
}
void BitmapBuilder::SetBit(intptr_t bit_offset, bool value) {
if (!InRange(bit_offset)) {
FATAL1(
"Fatal error in BitmapBuilder::SetBit :"
" invalid bit_offset, %" Pd "\n",
bit_offset);
}
intptr_t byte_offset = bit_offset >> kBitsPerByteLog2;
ASSERT(byte_offset < data_size_in_bytes_);
intptr_t bit_remainder = bit_offset & (kBitsPerByte - 1);
uint8_t mask = 1U << bit_remainder;
if (value) {
BackingStore()[byte_offset] |= mask;
} else {
BackingStore()[byte_offset] &= ~mask;
}
}
void BitmapBuilder::Write(BaseWriteStream* stream) const {
const intptr_t payload_size =
Utils::Minimum(Utils::RoundUp(Length(), kBitsPerByte) / kBitsPerByte,
data_size_in_bytes_);
stream->Write<intptr_t>(Length());
stream->Write<intptr_t>(payload_size);
stream->WriteBytes(BackingStore(), payload_size);
}
void BitmapBuilder::Read(ReadStream* stream) {
length_ = stream->Read<intptr_t>();
const intptr_t payload_size = stream->Read<intptr_t>();
if (payload_size > data_size_in_bytes_) {
data_size_in_bytes_ = payload_size;
data_.ptr_ = AllocBackingStore(data_size_in_bytes_);
} else {
memset(BackingStore() + payload_size, 0,
data_size_in_bytes_ - payload_size);
}
stream->ReadBytes(BackingStore(), payload_size);
}
} // namespace dart