LibJS/Bytecode: Keep instruction source mappings in Executable

Instead of storing source offsets with each instruction, we now keep
them in a side table in Executable.

This shrinks each instruction by 8 bytes, further improving locality.
This commit is contained in:
Andreas Kling 2024-05-06 07:51:14 +02:00
parent 4cf4ea92a7
commit 5a08544138
Notes: sideshowbarker 2024-07-17 02:28:18 +09:00
8 changed files with 27 additions and 14 deletions

View file

@ -8,6 +8,7 @@
#include <AK/Badge.h>
#include <AK/String.h>
#include <LibJS/Bytecode/Executable.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Handle.h>
@ -45,6 +46,9 @@ public:
BasicBlock const* handler() const { return m_handler; }
BasicBlock const* finalizer() const { return m_finalizer; }
auto const& source_map() const { return m_source_map; }
void add_source_map_entry(size_t bytecode_offset, SourceRecord const& source_record) { m_source_map.set(bytecode_offset, source_record); }
private:
explicit BasicBlock(String name);
@ -53,6 +57,8 @@ private:
BasicBlock const* m_finalizer { nullptr };
String m_name;
bool m_terminated { false };
HashMap<size_t, SourceRecord> m_source_map;
};
}

View file

@ -106,11 +106,13 @@ UnrealizedSourceRange Executable::source_range_at(size_t offset) const
return {};
auto it = InstructionStreamIterator(bytecode.span().slice(offset), this);
VERIFY(!it.at_end());
auto& instruction = *it;
auto mapping = source_map.get(offset);
if (!mapping.has_value())
return {};
return UnrealizedSourceRange {
.source_code = source_code,
.start_offset = instruction.source_record().source_start_offset,
.end_offset = instruction.source_record().source_end_offset,
.start_offset = mapping->source_start_offset,
.end_offset = mapping->source_end_offset,
};
}

View file

@ -84,6 +84,8 @@ public:
Vector<ExceptionHandlers> exception_handlers;
Vector<size_t> basic_block_start_offsets;
HashMap<size_t, SourceRecord> source_map;
ByteString const& get_string(StringTableIndex index) const { return string_table->get(index); }
DeprecatedFlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); }

View file

@ -96,6 +96,8 @@ CodeGenerationErrorOr<NonnullGCPtr<Executable>> Generator::generate(VM& vm, ASTN
};
Vector<UnlinkedExceptionHandlers> unlinked_exception_handlers;
HashMap<size_t, SourceRecord> source_map;
for (auto& block : generator.m_root_basic_blocks) {
basic_block_start_offsets.append(bytecode.size());
if (block->handler() || block->finalizer()) {
@ -108,6 +110,11 @@ CodeGenerationErrorOr<NonnullGCPtr<Executable>> Generator::generate(VM& vm, ASTN
}
block_offsets.set(block.ptr(), bytecode.size());
for (auto& [offset, source_record] : block->source_map()) {
source_map.set(bytecode.size() + offset, source_record);
}
Bytecode::InstructionStreamIterator it(block->instruction_stream());
while (!it.at_end()) {
auto& instruction = const_cast<Instruction&>(*it);
@ -161,6 +168,7 @@ CodeGenerationErrorOr<NonnullGCPtr<Executable>> Generator::generate(VM& vm, ASTN
executable->exception_handlers = move(linked_exception_handlers);
executable->basic_block_start_offsets = move(basic_block_start_offsets);
executable->source_map = move(source_map);
return executable;
}

View file

@ -77,8 +77,7 @@ public:
new (slot) OpType(forward<Args>(args)...);
if constexpr (OpType::IsTerminator)
m_current_basic_block->terminate({});
auto* op = static_cast<OpType*>(slot);
op->set_source_record({ m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
}
template<typename OpType, typename ExtraSlotType, typename... Args>
@ -93,8 +92,7 @@ public:
new (slot) OpType(forward<Args>(args)...);
if constexpr (OpType::IsTerminator)
m_current_basic_block->terminate({});
auto* op = static_cast<OpType*>(slot);
op->set_source_record({ m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
m_current_basic_block->add_source_map_entry(slot_offset, { m_current_ast_node->start_offset(), m_current_ast_node->end_offset() });
}
template<typename OpType, typename... Args>

View file

@ -52,7 +52,7 @@ void Instruction::visit_labels(Function<void(JS::Bytecode::Label&)> visitor)
UnrealizedSourceRange InstructionStreamIterator::source_range() const
{
VERIFY(m_executable);
auto record = dereference().source_record();
auto record = m_executable->source_map.get(offset()).value();
return {
.source_code = m_executable->source_code,
.start_offset = record.source_start_offset,

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/Forward.h>
#include <AK/Function.h>
#include <AK/Span.h>
#include <LibJS/Bytecode/Executable.h>
#include <LibJS/Forward.h>
@ -140,17 +141,12 @@ public:
void visit_labels(Function<void(Label&)> visitor);
static void destroy(Instruction&);
// FIXME: Find a better way to organize this information
void set_source_record(SourceRecord rec) { m_source_record = rec; }
SourceRecord source_record() const { return m_source_record; }
protected:
Instruction(Type, size_t length);
void visit_labels_impl(Function<void(Label&)>) { }
private:
SourceRecord m_source_record {};
Type m_type {};
u32 m_length {};
};

View file

@ -7,10 +7,11 @@
#pragma once
#include <AK/Format.h>
#include <LibJS/Bytecode/BasicBlock.h>
namespace JS::Bytecode {
class BasicBlock;
class Label {
public:
explicit Label(BasicBlock const& block)