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