Skip to content

Commit

Permalink
Revert "LibJS/Bytecode: Bring back the bytecode optimization pipeline"
Browse files Browse the repository at this point in the history
This reverts commit 5b29974.
  • Loading branch information
awesomekling committed Mar 6, 2024
1 parent b37d84b commit 5b69413
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 962 deletions.
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2512,7 +2512,7 @@ Bytecode::CodeGenerationErrorOr<Optional<Bytecode::Operand>> TryStatement::gener

auto& target_block = generator.make_block();
generator.switch_to_basic_block(saved_block);
generator.emit<Bytecode::Op::EnterUnwindContext>(Bytecode::Label { target_block }, handler_target, finalizer_target);
generator.emit<Bytecode::Op::EnterUnwindContext>(Bytecode::Label { target_block });
generator.start_boundary(Bytecode::Generator::BlockBoundaryType::Unwind);
if (m_finalizer)
generator.start_boundary(Bytecode::Generator::BlockBoundaryType::ReturnToFinally);
Expand Down
45 changes: 1 addition & 44 deletions Userland/Libraries/LibJS/Bytecode/BasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <AK/Badge.h>
#include <AK/String.h>
#include <LibJS/Bytecode/Operand.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Handle.h>

Expand Down Expand Up @@ -36,7 +35,7 @@ class BasicBlock {

void grow(size_t additional_size);

void terminate(Badge<Generator>, size_t slot_offset) { terminate(slot_offset); }
void terminate(Badge<Generator>) { m_terminated = true; }
bool is_terminated() const { return m_terminated; }

String const& name() const { return m_name; }
Expand All @@ -47,56 +46,14 @@ class BasicBlock {
BasicBlock const* handler() const { return m_handler; }
BasicBlock const* finalizer() const { return m_finalizer; }

Instruction const* terminator() const
{
VERIFY(m_terminated);
return reinterpret_cast<Instruction const*>(data() + m_terminator_offset);
}

template<typename OpType, typename... Args>
void append(u32 start_offset, u32 end_offset, Args&&... args)
{
VERIFY(!m_terminated);
size_t const slot_offset = size();
grow(sizeof(OpType));
void* slot = data() + slot_offset;
new (slot) OpType(forward<Args>(args)...);
if constexpr (OpType::IsTerminator)
terminate(slot_offset);
auto* op = static_cast<OpType*>(slot);
op->set_source_record({ start_offset, end_offset });
}

template<typename OpType, typename... Args>
void append_with_extra_operand_slots(u32 start_offset, u32 end_offset, size_t extra_operand_slots, Args&&... args)
{
VERIFY(!m_terminated);
size_t size_to_allocate = round_up_to_power_of_two(sizeof(OpType) + extra_operand_slots * sizeof(Operand), alignof(void*));
size_t slot_offset = size();
grow(size_to_allocate);
void* slot = data() + slot_offset;
new (slot) OpType(forward<Args>(args)...);
if constexpr (OpType::IsTerminator)
terminate(slot_offset);
auto* op = static_cast<OpType*>(slot);
op->set_source_record({ start_offset, end_offset });
}

private:
explicit BasicBlock(String name);

void terminate(size_t slot_offset)
{
m_terminated = true;
m_terminator_offset = slot_offset;
}

Vector<u8> m_buffer;
BasicBlock const* m_handler { nullptr };
BasicBlock const* m_finalizer { nullptr };
String m_name;
bool m_terminated { false };
size_t m_terminator_offset { 0 };
};

}
4 changes: 2 additions & 2 deletions Userland/Libraries/LibJS/Bytecode/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Generator {
void* slot = m_current_basic_block->data() + slot_offset;
new (slot) OpType(forward<Args>(args)...);
if constexpr (OpType::IsTerminator)
m_current_basic_block->terminate({}, slot_offset);
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() });
}
Expand All @@ -92,7 +92,7 @@ class Generator {
void* slot = m_current_basic_block->data() + slot_offset;
new (slot) OpType(forward<Args>(args)...);
if constexpr (OpType::IsTerminator)
m_current_basic_block->terminate({}, slot_offset);
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() });
}
Expand Down
30 changes: 0 additions & 30 deletions Userland/Libraries/LibJS/Bytecode/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,6 @@

namespace JS::Bytecode {

bool Instruction::is_terminator() const
{
#define __BYTECODE_OP(op) \
case Type::op: \
return Op::op::IsTerminator;

switch (type()) {
ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
default:
VERIFY_NOT_REACHED();
}

#undef __BYTECODE_OP
}

void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
{
#define __BYTECODE_OP(op) \
case Instruction::Type::op: \
return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);

switch (type()) {
ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
default:
VERIFY_NOT_REACHED();
}

#undef __BYTECODE_OP
}

void Instruction::destroy(Instruction& instruction)
{
#define __BYTECODE_OP(op) \
Expand Down
3 changes: 0 additions & 3 deletions Userland/Libraries/LibJS/Bytecode/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,10 @@ class alignas(void*) Instruction {
#undef __BYTECODE_OP
};

[[nodiscard]] bool is_terminator() const;

Type type() const { return m_type; }
size_t length() const { return m_length; }
ByteString to_byte_string(Bytecode::Executable const&) const;
ThrowCompletionOr<void> execute(Bytecode::Interpreter&) const;
void replace_references(BasicBlock const& from, BasicBlock const& to);
static void destroy(Instruction&);

// FIXME: Find a better way to organize this information
Expand Down
27 changes: 0 additions & 27 deletions Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <LibJS/Bytecode/Interpreter.h>
#include <LibJS/Bytecode/Label.h>
#include <LibJS/Bytecode/Op.h>
#include <LibJS/Bytecode/PassManager.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/BigInt.h>
Expand Down Expand Up @@ -569,25 +568,6 @@ void Interpreter::enter_object_environment(Object& object)
vm().running_execution_context().lexical_environment = new_object_environment(object, true, old_environment);
}

static PassManager& optimization_pipeline()
{
static auto s_optimization_pipeline = [] {
auto pm = make<PassManager>();
pm->add<Passes::GenerateCFG>();
pm->add<Passes::UnifySameBlocks>();
pm->add<Passes::GenerateCFG>();
pm->add<Passes::MergeBlocks>();
pm->add<Passes::GenerateCFG>();
pm->add<Passes::UnifySameBlocks>();
pm->add<Passes::GenerateCFG>();
pm->add<Passes::MergeBlocks>();
pm->add<Passes::GenerateCFG>();
pm->add<Passes::PlaceBlocks>();
return pm;
}();
return *s_optimization_pipeline;
}

ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM& vm, ASTNode const& node, ReadonlySpan<FunctionParameter> parameters, FunctionKind kind, DeprecatedFlyString const& name)
{
auto executable_result = Bytecode::Generator::generate(vm, node, parameters, kind);
Expand All @@ -597,13 +577,6 @@ ThrowCompletionOr<NonnullGCPtr<Bytecode::Executable>> compile(VM& vm, ASTNode co
auto bytecode_executable = executable_result.release_value();
bytecode_executable->name = name;

auto& passes = optimization_pipeline();
passes.perform(*bytecode_executable);
if constexpr (JS_BYTECODE_DEBUG) {
dbgln("Optimisation passes took {}us", passes.elapsed());
dbgln("Compiled Bytecode::Block for function '{}':", name);
}

if (Bytecode::g_dump_bytecode)
bytecode_executable->dump();

Expand Down
1 change: 0 additions & 1 deletion Userland/Libraries/LibJS/Bytecode/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
namespace JS::Bytecode {

class InstructionStreamIterator;
class PassManager;

struct CallFrame {
static NonnullOwnPtr<CallFrame> create(size_t register_count);
Expand Down
Loading

0 comments on commit 5b69413

Please sign in to comment.