Commit d1ab9f12 authored by rmcilroy's avatar rmcilroy Committed by Commit bot

[Interpreter] Avoid allocating handles in bytecode-array-writer

Don't allocate handles in the bytecode array writer, to allow off-thread
bytecode generation.

BUG=v8:5203

Review-Url: https://codereview.chromium.org/2226333002
Cr-Commit-Position: refs/heads/master@{#38550}
parent b558894a
......@@ -254,13 +254,11 @@ void BytecodeArrayWriter::PatchJumpWith8BitOperand(size_t jump_location,
constant_array_builder()->DiscardReservedEntry(OperandSize::kByte);
bytecodes()->at(operand_location) = static_cast<uint8_t>(delta);
} else {
// TODO(5203): Remove this temporary exception.
AllowHandleAllocation allow_handles;
// The jump does not fit within the range of an Imm operand, so
// commit reservation putting the offset into the constant pool,
// and update the jump instruction and operand.
size_t entry = constant_array_builder()->CommitReservedEntry(
OperandSize::kByte, handle(Smi::FromInt(delta), isolate()));
OperandSize::kByte, Smi::FromInt(delta));
DCHECK_LE(entry, kMaxUInt32);
DCHECK_EQ(Bytecodes::SizeForUnsignedOperand(static_cast<uint32_t>(entry)),
OperandSize::kByte);
......@@ -280,12 +278,10 @@ void BytecodeArrayWriter::PatchJumpWith16BitOperand(size_t jump_location,
constant_array_builder()->DiscardReservedEntry(OperandSize::kShort);
WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(delta));
} else {
// TODO(5203): Remove this temporary exception.
AllowHandleAllocation allow_handles;
jump_bytecode = GetJumpWithConstantOperand(jump_bytecode);
bytecodes()->at(jump_location) = Bytecodes::ToByte(jump_bytecode);
size_t entry = constant_array_builder()->CommitReservedEntry(
OperandSize::kShort, handle(Smi::FromInt(delta), isolate()));
OperandSize::kShort, Smi::FromInt(delta));
WriteUnalignedUInt16(operand_bytes, static_cast<uint16_t>(entry));
}
DCHECK(bytecodes()->at(operand_location) == k8BitJumpPlaceholder &&
......
......@@ -71,7 +71,10 @@ STATIC_CONST_MEMBER_DEFINITION const size_t
ConstantArrayBuilder::k32BitCapacity;
ConstantArrayBuilder::ConstantArrayBuilder(Isolate* isolate, Zone* zone)
: isolate_(isolate), constants_map_(zone) {
: isolate_(isolate),
constants_map_(zone),
smi_map_(zone),
smi_pairs_(zone) {
idx_slice_[0] =
new (zone) ConstantArraySlice(zone, 0, k8BitCapacity, OperandSize::kByte);
idx_slice_[1] = new (zone) ConstantArraySlice(
......@@ -113,6 +116,12 @@ Handle<Object> ConstantArrayBuilder::At(size_t index) const {
}
Handle<FixedArray> ConstantArrayBuilder::ToFixedArray() {
// First insert reserved SMI values.
for (auto reserved_smi : smi_pairs_) {
InsertAllocatedEntry(reserved_smi.second,
handle(reserved_smi.first, isolate_));
}
Handle<FixedArray> fixed_array = isolate_->factory()->NewFixedArray(
static_cast<int>(size()), PretenureFlag::TENURED);
int array_index = 0;
......@@ -209,13 +218,21 @@ OperandSize ConstantArrayBuilder::CreateReservedEntry() {
return OperandSize::kNone;
}
ConstantArrayBuilder::index_t ConstantArrayBuilder::AllocateReservedEntry(
Smi* value) {
index_t index = static_cast<index_t>(AllocateEntry());
smi_map_[value] = index;
smi_pairs_.push_back(std::make_pair(value, index));
return index;
}
size_t ConstantArrayBuilder::CommitReservedEntry(OperandSize operand_size,
Handle<Object> object) {
Smi* value) {
DiscardReservedEntry(operand_size);
size_t index;
auto entry = constants_map_.find(object.address());
if (entry == constants_map_.end()) {
index = AllocateEntry(object);
auto entry = smi_map_.find(value);
if (entry == smi_map_.end()) {
index = AllocateReservedEntry(value);
} else {
ConstantArraySlice* slice = OperandSizeToSlice(operand_size);
index = entry->second;
......@@ -223,9 +240,9 @@ size_t ConstantArrayBuilder::CommitReservedEntry(OperandSize operand_size,
// The object is already in the constant array, but may have an
// index too big for the reserved operand_size. So, duplicate
// entry with the smaller operand size.
index = slice->Allocate(object);
constants_map_[object.address()] = static_cast<index_t>(index);
index = AllocateReservedEntry(value);
}
DCHECK_LE(index, slice->max_index());
}
return index;
}
......
......@@ -61,8 +61,8 @@ class ConstantArrayBuilder final BASE_EMBEDDED {
OperandSize CreateReservedEntry();
// Commit reserved entry and returns the constant pool index for the
// object.
size_t CommitReservedEntry(OperandSize operand_size, Handle<Object> object);
// SMI value.
size_t CommitReservedEntry(OperandSize operand_size, Smi* value);
// Discards constant pool reservation.
void DiscardReservedEntry(OperandSize operand_size);
......@@ -72,6 +72,7 @@ class ConstantArrayBuilder final BASE_EMBEDDED {
index_t AllocateEntry(Handle<Object> object);
index_t AllocateIndex(Handle<Object> object);
index_t AllocateReservedEntry(Smi* value);
struct ConstantArraySlice final : public ZoneObject {
ConstantArraySlice(Zone* zone, size_t start_index, size_t capacity,
......@@ -107,6 +108,8 @@ class ConstantArrayBuilder final BASE_EMBEDDED {
Isolate* isolate_;
ConstantArraySlice* idx_slice_[3];
ZoneMap<Address, index_t> constants_map_;
ZoneMap<Smi*, index_t> smi_map_;
ZoneVector<std::pair<Smi*, index_t>> smi_pairs_;
};
} // namespace interpreter
......
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