Skip to content

Commit 9afd12a

Browse files
committed
LibJS/JIT: Consolidate exits from the jitted code
Instead of emitting the "restore callee-saved registers and return" sequence again and again, just emit it once at the end of the generated code, and have everyone jump to it. This is a code size optimization that saves 207KiB on Kraken/ai-astar.js
1 parent 0768bf2 commit 9afd12a

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Userland/Libraries/LibJS/JIT/Compiler.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ void Compiler::check_exception()
337337
Assembler::Operand::Imm(0),
338338
handle_exception);
339339

340-
m_assembler.exit();
340+
jump_to_exit();
341341

342342
// handle_exception:
343343
handle_exception.link(m_assembler);
@@ -593,7 +593,7 @@ void Compiler::compile_return(Bytecode::Op::Return const&)
593593
// normal_return:
594594
normal_return.link(m_assembler);
595595
store_vm_register(Bytecode::Register::return_value(), GPR0);
596-
m_assembler.exit();
596+
jump_to_exit();
597597
}
598598

599599
static Value cxx_new_string(VM& vm, DeprecatedString const& string)
@@ -971,7 +971,7 @@ void Compiler::compile_continue_pending_unwind(Bytecode::Op::ContinuePendingUnwi
971971

972972
// finish the pending return from the try block
973973
store_vm_register(Bytecode::Register::return_value(), GPR0);
974-
m_assembler.exit();
974+
jump_to_exit();
975975
}
976976

977977
static void cxx_create_lexical_environment(VM& vm)
@@ -999,6 +999,11 @@ void Compiler::compile_leave_lexical_environment(Bytecode::Op::LeaveLexicalEnvir
999999
m_assembler.native_call((void*)cxx_leave_lexical_environment);
10001000
}
10011001

1002+
void Compiler::jump_to_exit()
1003+
{
1004+
m_assembler.jump(m_exit_label);
1005+
}
1006+
10021007
OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_executable)
10031008
{
10041009
if (!getenv("LIBJS_JIT"))
@@ -1158,9 +1163,12 @@ OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_execut
11581163
++it;
11591164
}
11601165
if (!block->is_terminated())
1161-
compiler.m_assembler.exit();
1166+
compiler.jump_to_exit();
11621167
}
11631168

1169+
compiler.m_exit_label.link(compiler.m_assembler);
1170+
compiler.m_assembler.exit();
1171+
11641172
auto* executable_memory = mmap(nullptr, compiler.m_output.size(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
11651173
if (executable_memory == MAP_FAILED) {
11661174
dbgln("mmap: {}", strerror(errno));

Userland/Libraries/LibJS/JIT/Compiler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class Compiler {
127127
void push_unwind_context(bool valid, Optional<Bytecode::Label> const& handler, Optional<Bytecode::Label> const& finalizer);
128128
void pop_unwind_context();
129129

130+
void jump_to_exit();
131+
130132
template<typename Codegen>
131133
void branch_if_int32(Assembler::Reg, Codegen);
132134

@@ -160,6 +162,7 @@ class Compiler {
160162

161163
Vector<u8> m_output;
162164
Assembler m_assembler { m_output };
165+
Assembler::Label m_exit_label;
163166
Bytecode::Executable& m_bytecode_executable;
164167
};
165168

0 commit comments

Comments
 (0)