Commit f4bd6f6b authored by Toon Verwaest's avatar Toon Verwaest Committed by Commit Bot

[interpreter] Speed up the BytecodeArrayAccessor through direct memory access

This speeds up the sparkplug compiler by >20%.

Change-Id: I9dece7209b0ddf53d876df9aca4b2feede6259c5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2682644
Auto-Submit: Toon Verwaest <verwaest@chromium.org>
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72624}
parent 880f2fac
......@@ -5377,7 +5377,7 @@ bool JSHeapBroker::StackHasOverflowed() const {
}
OffHeapBytecodeArray::OffHeapBytecodeArray(BytecodeArrayRef bytecode_array)
: array_(bytecode_array) {}
: AbstractBytecodeArray(false), array_(bytecode_array) {}
int OffHeapBytecodeArray::length() const { return array_.length(); }
......@@ -5385,10 +5385,6 @@ int OffHeapBytecodeArray::parameter_count() const {
return array_.parameter_count();
}
uint8_t OffHeapBytecodeArray::get(int index) const { return array_.get(index); }
void OffHeapBytecodeArray::set(int index, uint8_t value) { UNREACHABLE(); }
Address OffHeapBytecodeArray::GetFirstBytecodeAddress() const {
return array_.GetFirstBytecodeAddress();
}
......
......@@ -463,12 +463,11 @@ class OffHeapBytecodeArray final : public interpreter::AbstractBytecodeArray {
int length() const override;
int parameter_count() const override;
uint8_t get(int index) const override;
void set(int index, uint8_t value) override;
Address GetFirstBytecodeAddress() const override;
Handle<Object> GetConstantAtIndex(int index, Isolate* isolate) const override;
bool IsConstantAtIndexSmi(int index) const override;
Smi GetConstantAtIndexAsSmi(int index) const override;
LocalHeap* local_heap() const override { UNREACHABLE(); }
private:
BytecodeArrayRef array_;
......
......@@ -135,7 +135,8 @@ bool LocalHeap::IsParked() {
void LocalHeap::Park() {
base::MutexGuard guard(&state_mutex_);
CHECK(state_ == ThreadState::Running);
CHECK_EQ(ThreadState::Running, state_);
DCHECK(gc_epilogue_callbacks_.empty());
state_ = ThreadState::Parked;
state_change_.NotifyAll();
}
......@@ -208,6 +209,7 @@ Address LocalHeap::PerformCollectionAndAllocateAgain(
void LocalHeap::AddGCEpilogueCallback(GCEpilogueCallback* callback,
void* data) {
DCHECK(!IsParked());
std::pair<GCEpilogueCallback*, void*> callback_and_data(callback, data);
DCHECK_EQ(std::find(gc_epilogue_callbacks_.begin(),
gc_epilogue_callbacks_.end(), callback_and_data),
......@@ -217,6 +219,7 @@ void LocalHeap::AddGCEpilogueCallback(GCEpilogueCallback* callback,
void LocalHeap::RemoveGCEpilogueCallback(GCEpilogueCallback* callback,
void* data) {
DCHECK(!IsParked());
std::pair<GCEpilogueCallback*, void*> callback_and_data(callback, data);
auto it = std::find(gc_epilogue_callbacks_.begin(),
gc_epilogue_callbacks_.end(), callback_and_data);
......
......@@ -19,18 +19,18 @@ namespace {
class OnHeapBytecodeArray final : public AbstractBytecodeArray {
public:
explicit OnHeapBytecodeArray(Handle<BytecodeArray> bytecode_array)
: array_(bytecode_array) {}
: AbstractBytecodeArray(true), array_(bytecode_array) {
// TODO(verwaest): Pass the LocalHeap into the constructor.
local_heap_ = LocalHeap::Current();
if (!local_heap_) {
local_heap_ = Isolate::Current()->main_thread_local_heap();
}
}
int length() const override { return array_->length(); }
int parameter_count() const override { return array_->parameter_count(); }
uint8_t get(int index) const override { return array_->get(index); }
void set(int index, uint8_t value) override {
return array_->set(index, value);
}
Address GetFirstBytecodeAddress() const override {
return array_->GetFirstBytecodeAddress();
}
......@@ -48,7 +48,10 @@ class OnHeapBytecodeArray final : public AbstractBytecodeArray {
return Smi::cast(array_->constant_pool().get(index));
}
LocalHeap* local_heap() const override { return local_heap_; }
private:
LocalHeap* local_heap_;
Handle<BytecodeArray> array_;
};
......@@ -57,10 +60,16 @@ class OnHeapBytecodeArray final : public AbstractBytecodeArray {
BytecodeArrayAccessor::BytecodeArrayAccessor(
std::unique_ptr<AbstractBytecodeArray> bytecode_array, int initial_offset)
: bytecode_array_(std::move(bytecode_array)),
bytecode_length_(bytecode_array_->length()),
bytecode_offset_(initial_offset),
start_(reinterpret_cast<uint8_t*>(
bytecode_array_->GetFirstBytecodeAddress())),
end_(start_ + bytecode_array_->length()),
cursor_(start_ + initial_offset),
operand_scale_(OperandScale::kSingle),
prefix_offset_(0) {
prefix_size_(0) {
if (bytecode_array_->can_move()) {
bytecode_array_->local_heap()->AddGCEpilogueCallback(UpdatePointersCallback,
this);
}
UpdateOperandScale();
}
......@@ -70,8 +79,17 @@ BytecodeArrayAccessor::BytecodeArrayAccessor(
std::make_unique<OnHeapBytecodeArray>(bytecode_array),
initial_offset) {}
BytecodeArrayAccessor::~BytecodeArrayAccessor() {
if (bytecode_array_->can_move()) {
bytecode_array_->local_heap()->RemoveGCEpilogueCallback(
UpdatePointersCallback, this);
}
}
void BytecodeArrayAccessor::SetOffset(int offset) {
bytecode_offset_ = offset;
if (offset < 0) return;
cursor_ = reinterpret_cast<uint8_t*>(
bytecode_array()->GetFirstBytecodeAddress() + offset);
UpdateOperandScale();
}
......@@ -79,45 +97,16 @@ void BytecodeArrayAccessor::ApplyDebugBreak() {
// Get the raw bytecode from the bytecode array. This may give us a
// scaling prefix, which we can patch with the matching debug-break
// variant.
interpreter::Bytecode bytecode =
interpreter::Bytecodes::FromByte(bytecode_array()->get(bytecode_offset_));
uint8_t* cursor = cursor_ - prefix_size_;
interpreter::Bytecode bytecode = interpreter::Bytecodes::FromByte(*cursor);
if (interpreter::Bytecodes::IsDebugBreak(bytecode)) return;
interpreter::Bytecode debugbreak =
interpreter::Bytecodes::GetDebugBreak(bytecode);
bytecode_array()->set(bytecode_offset_,
interpreter::Bytecodes::ToByte(debugbreak));
}
void BytecodeArrayAccessor::UpdateOperandScale() {
if (OffsetInBounds()) {
uint8_t current_byte = bytecode_array()->get(bytecode_offset_);
Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
if (Bytecodes::IsPrefixScalingBytecode(current_bytecode)) {
operand_scale_ =
Bytecodes::PrefixBytecodeToOperandScale(current_bytecode);
prefix_offset_ = 1;
} else {
operand_scale_ = OperandScale::kSingle;
prefix_offset_ = 0;
}
}
}
bool BytecodeArrayAccessor::OffsetInBounds() const {
return bytecode_offset_ >= 0 && bytecode_offset_ < bytecode_length_;
}
Bytecode BytecodeArrayAccessor::current_bytecode() const {
DCHECK(OffsetInBounds());
uint8_t current_byte =
bytecode_array()->get(bytecode_offset_ + current_prefix_offset());
Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
DCHECK(!Bytecodes::IsPrefixScalingBytecode(current_bytecode));
return current_bytecode;
*cursor = interpreter::Bytecodes::ToByte(debugbreak);
}
int BytecodeArrayAccessor::current_bytecode_size() const {
return current_prefix_offset() +
return prefix_size_ +
Bytecodes::Size(current_bytecode(), current_operand_scale());
}
......@@ -129,8 +118,7 @@ uint32_t BytecodeArrayAccessor::GetUnsignedOperand(
Bytecodes::GetOperandType(current_bytecode(), operand_index));
DCHECK(Bytecodes::IsUnsignedOperandType(operand_type));
Address operand_start =
bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
current_prefix_offset() +
reinterpret_cast<Address>(cursor_) +
Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
current_operand_scale());
return BytecodeDecoder::DecodeUnsignedOperand(operand_start, operand_type,
......@@ -145,8 +133,7 @@ int32_t BytecodeArrayAccessor::GetSignedOperand(
Bytecodes::GetOperandType(current_bytecode(), operand_index));
DCHECK(!Bytecodes::IsUnsignedOperandType(operand_type));
Address operand_start =
bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
current_prefix_offset() +
reinterpret_cast<Address>(cursor_) +
Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
current_operand_scale());
return BytecodeDecoder::DecodeSignedOperand(operand_start, operand_type,
......@@ -207,8 +194,7 @@ Register BytecodeArrayAccessor::GetRegisterOperand(int operand_index) const {
OperandType operand_type =
Bytecodes::GetOperandType(current_bytecode(), operand_index);
Address operand_start =
bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
current_prefix_offset() +
reinterpret_cast<Address>(cursor_) +
Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
current_operand_scale());
return BytecodeDecoder::DecodeRegisterOperand(operand_start, operand_type,
......@@ -312,18 +298,11 @@ JumpTableTargetOffsets BytecodeArrayAccessor::GetJumpTableTargetOffsets()
}
int BytecodeArrayAccessor::GetAbsoluteOffset(int relative_offset) const {
return current_offset() + relative_offset + current_prefix_offset();
}
bool BytecodeArrayAccessor::OffsetWithinBytecode(int offset) const {
return current_offset() <= offset &&
offset < current_offset() + current_bytecode_size();
return current_offset() + relative_offset + prefix_size_;
}
std::ostream& BytecodeArrayAccessor::PrintTo(std::ostream& os) const {
const uint8_t* bytecode_addr = reinterpret_cast<const uint8_t*>(
bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_);
return BytecodeDecoder::Decode(os, bytecode_addr,
return BytecodeDecoder::Decode(os, cursor_ - prefix_size_,
bytecode_array()->parameter_count());
}
......
......@@ -69,10 +69,9 @@ class V8_EXPORT_PRIVATE JumpTableTargetOffsets final {
class V8_EXPORT_PRIVATE AbstractBytecodeArray {
public:
explicit AbstractBytecodeArray(bool can_move) : can_move_(can_move) {}
virtual int length() const = 0;
virtual int parameter_count() const = 0;
virtual uint8_t get(int index) const = 0;
virtual void set(int index, uint8_t value) = 0;
virtual Address GetFirstBytecodeAddress() const = 0;
virtual Handle<Object> GetConstantAtIndex(int index,
......@@ -81,6 +80,11 @@ class V8_EXPORT_PRIVATE AbstractBytecodeArray {
virtual Smi GetConstantAtIndexAsSmi(int index) const = 0;
virtual ~AbstractBytecodeArray() = default;
virtual LocalHeap* local_heap() const = 0;
bool can_move() const { return can_move_; }
private:
bool can_move_;
};
class V8_EXPORT_PRIVATE BytecodeArrayAccessor {
......@@ -90,19 +94,31 @@ class V8_EXPORT_PRIVATE BytecodeArrayAccessor {
BytecodeArrayAccessor(Handle<BytecodeArray> bytecode_array,
int initial_offset);
~BytecodeArrayAccessor();
BytecodeArrayAccessor(const BytecodeArrayAccessor&) = delete;
BytecodeArrayAccessor& operator=(const BytecodeArrayAccessor&) = delete;
inline void Advance() {
cursor_ += Bytecodes::Size(current_bytecode(), current_operand_scale());
UpdateOperandScale();
}
void SetOffset(int offset);
void ApplyDebugBreak();
Bytecode current_bytecode() const;
inline Bytecode current_bytecode() const {
DCHECK(!done());
uint8_t current_byte = *cursor_;
Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
DCHECK(!Bytecodes::IsPrefixScalingBytecode(current_bytecode));
return current_bytecode;
}
int current_bytecode_size() const;
int current_offset() const { return bytecode_offset_; }
int current_offset() const {
return static_cast<int>(cursor_ - start_ - prefix_size_);
}
OperandScale current_operand_scale() const { return operand_scale_; }
int current_prefix_offset() const { return prefix_offset_; }
AbstractBytecodeArray* bytecode_array() const {
return bytecode_array_.get();
}
......@@ -143,26 +159,55 @@ class V8_EXPORT_PRIVATE BytecodeArrayAccessor {
// from the current bytecode.
int GetAbsoluteOffset(int relative_offset) const;
bool OffsetWithinBytecode(int offset) const;
std::ostream& PrintTo(std::ostream& os) const;
int bytecode_length() const { return bytecode_length_; }
static void UpdatePointersCallback(void* accessor) {
reinterpret_cast<BytecodeArrayAccessor*>(accessor)->UpdatePointers();
}
private:
bool OffsetInBounds() const;
void UpdatePointers() {
DisallowGarbageCollection no_gc;
uint8_t* start =
reinterpret_cast<uint8_t*>(bytecode_array_->GetFirstBytecodeAddress());
if (start != start_) {
start_ = start;
uint8_t* end = start + bytecode_array_->length();
size_t distance_to_end = end_ - cursor_;
cursor_ = end - distance_to_end;
end_ = end;
}
}
inline bool done() const { return cursor_ >= end_; }
private:
uint32_t GetUnsignedOperand(int operand_index,
OperandType operand_type) const;
int32_t GetSignedOperand(int operand_index, OperandType operand_type) const;
void UpdateOperandScale();
inline void UpdateOperandScale() {
if (done()) return;
uint8_t current_byte = *cursor_;
Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
if (Bytecodes::IsPrefixScalingBytecode(current_bytecode)) {
operand_scale_ =
Bytecodes::PrefixBytecodeToOperandScale(current_bytecode);
++cursor_;
prefix_size_ = 1;
} else {
operand_scale_ = OperandScale::kSingle;
prefix_size_ = 0;
}
}
std::unique_ptr<AbstractBytecodeArray> bytecode_array_;
const int bytecode_length_;
int bytecode_offset_;
uint8_t* start_;
uint8_t* end_;
// The cursor always points to the active bytecode. If there's a prefix, the
// prefix is at (cursor - 1).
uint8_t* cursor_;
OperandScale operand_scale_;
int prefix_offset_;
int prefix_size_;
};
} // namespace interpreter
......
......@@ -18,14 +18,6 @@ BytecodeArrayIterator::BytecodeArrayIterator(
Handle<BytecodeArray> bytecode_array)
: BytecodeArrayAccessor(bytecode_array, 0) {}
void BytecodeArrayIterator::Advance() {
SetOffset(current_offset() + current_bytecode_size());
}
bool BytecodeArrayIterator::done() const {
return current_offset() >= bytecode_length();
}
} // namespace interpreter
} // namespace internal
} // namespace v8
......@@ -22,9 +22,6 @@ class V8_EXPORT_PRIVATE BytecodeArrayIterator final
BytecodeArrayIterator(const BytecodeArrayIterator&) = delete;
BytecodeArrayIterator& operator=(const BytecodeArrayIterator&) = delete;
void Advance();
bool done() const;
};
} // namespace interpreter
......
......@@ -22,7 +22,7 @@ void BytecodeArrayRandomIterator::Initialize() {
// bytecode.
while (current_offset() < bytecode_array()->length()) {
offsets_.push_back(current_offset());
SetOffset(current_offset() + current_bytecode_size());
Advance();
}
GoToStart();
}
......
......@@ -118,19 +118,14 @@ class GCEpilogue {
class BackgroundThreadForGCEpilogue final : public v8::base::Thread {
public:
explicit BackgroundThreadForGCEpilogue(Heap* heap, bool parked,
GCEpilogue* epilogue)
explicit BackgroundThreadForGCEpilogue(Heap* heap, GCEpilogue* epilogue)
: v8::base::Thread(base::Thread::Options("BackgroundThread")),
heap_(heap),
parked_(parked),
epilogue_(epilogue) {}
void Run() override {
LocalHeap lh(heap_, ThreadKind::kBackground);
base::Optional<UnparkedScope> unparked_scope;
if (!parked_) {
unparked_scope.emplace(&lh);
}
UnparkedScope unparked_scope(&lh);
epilogue_->NotifyStarted();
lh.AddGCEpilogueCallback(&GCEpilogue::Callback, epilogue_);
while (!epilogue_->StopRequested()) {
......@@ -140,7 +135,6 @@ class BackgroundThreadForGCEpilogue final : public v8::base::Thread {
}
Heap* heap_;
bool parked_;
GCEpilogue* epilogue_;
};
......@@ -149,12 +143,13 @@ class BackgroundThreadForGCEpilogue final : public v8::base::Thread {
TEST_F(LocalHeapTest, GCEpilogue) {
Heap* heap = i_isolate()->heap();
LocalHeap lh(heap, ThreadKind::kMain);
UnparkedScope unparked_scope(&lh);
std::array<GCEpilogue, 3> epilogue;
lh.AddGCEpilogueCallback(&GCEpilogue::Callback, &epilogue[0]);
auto thread1 =
std::make_unique<BackgroundThreadForGCEpilogue>(heap, true, &epilogue[1]);
auto thread2 = std::make_unique<BackgroundThreadForGCEpilogue>(heap, false,
&epilogue[2]);
std::make_unique<BackgroundThreadForGCEpilogue>(heap, &epilogue[1]);
auto thread2 =
std::make_unique<BackgroundThreadForGCEpilogue>(heap, &epilogue[2]);
CHECK(thread1->Start());
CHECK(thread2->Start());
epilogue[1].WaitUntilStarted();
......
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