From 9947574b9cad74fbf04fa44d49647c591590c511 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Thu, 16 Mar 2023 10:41:12 -0700 Subject: [PATCH] Refactor jit_func_t and jit_exec I closed https://github.com/ruby/ruby/pull/7543, but part of the diff seems useful regardless, so I extracted it. --- insns.def | 28 ++++------------------------ vm.c | 4 ++-- vm_core.h | 6 +++--- vm_exec.h | 8 ++++++++ yjit.c | 5 +---- 5 files changed, 18 insertions(+), 33 deletions(-) diff --git a/insns.def b/insns.def index 9af64aa4b3..ba16d6365c 100644 --- a/insns.def +++ b/insns.def @@ -813,12 +813,7 @@ send { VALUE bh = vm_caller_setup_arg_block(ec, GET_CFP(), cd->ci, blockiseq, false); val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_method); - - jit_func_t func; - if (val == Qundef && (func = jit_compile(ec))) { - val = func(ec, ec->cfp); - if (ec->tag->state) THROW_EXCEPTION(val); - } + JIT_EXEC(ec, val); if (val == Qundef) { RESTORE_REGS(); @@ -838,12 +833,7 @@ opt_send_without_block { VALUE bh = VM_BLOCK_HANDLER_NONE; val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_method); - - jit_func_t func; - if (val == Qundef && (func = jit_compile(ec))) { - val = func(ec, ec->cfp); - if (ec->tag->state) THROW_EXCEPTION(val); - } + JIT_EXEC(ec, val); if (val == Qundef) { RESTORE_REGS(); @@ -946,12 +936,7 @@ invokesuper { VALUE bh = vm_caller_setup_arg_block(ec, GET_CFP(), cd->ci, blockiseq, true); val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_super); - - jit_func_t func; - if (val == Qundef && (func = jit_compile(ec))) { - val = func(ec, ec->cfp); - if (ec->tag->state) THROW_EXCEPTION(val); - } + JIT_EXEC(ec, val); if (val == Qundef) { RESTORE_REGS(); @@ -971,12 +956,7 @@ invokeblock { VALUE bh = VM_BLOCK_HANDLER_NONE; val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_invokeblock); - - jit_func_t func; - if (val == Qundef && (func = jit_compile(ec))) { - val = func(ec, ec->cfp); - if (ec->tag->state) THROW_EXCEPTION(val); - } + JIT_EXEC(ec, val); if (val == Qundef) { RESTORE_REGS(); diff --git a/vm.c b/vm.c index b4aa2a04a9..0f90c9a1e2 100644 --- a/vm.c +++ b/vm.c @@ -370,7 +370,7 @@ static VALUE vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, VALUE s #if USE_RJIT || USE_YJIT // Try to compile the current ISeq in ec. Return 0 if not compiled. -static inline jit_func_t +static inline rb_jit_func_t jit_compile(rb_execution_context_t *ec) { // Increment the ISEQ's call counter @@ -405,7 +405,7 @@ jit_compile(rb_execution_context_t *ec) static inline VALUE jit_exec(rb_execution_context_t *ec) { - jit_func_t func = jit_compile(ec); + rb_jit_func_t func = jit_compile(ec); if (func) { // Call the JIT code return func(ec, ec->cfp); diff --git a/vm_core.h b/vm_core.h index 724c8cd739..f7313205ee 100644 --- a/vm_core.h +++ b/vm_core.h @@ -373,6 +373,8 @@ enum rb_builtin_attr { BUILTIN_ATTR_NO_GC = 0x02, }; +typedef VALUE (*rb_jit_func_t)(struct rb_execution_context_struct *, struct rb_control_frame_struct *); + struct rb_iseq_constant_body { enum rb_iseq_type type; @@ -505,7 +507,7 @@ struct rb_iseq_constant_body { #if USE_RJIT || USE_YJIT // Function pointer for JIT code - VALUE (*jit_func)(struct rb_execution_context_struct *, struct rb_control_frame_struct *); + rb_jit_func_t jit_func; // Number of total calls with jit_exec() long unsigned total_calls; #endif @@ -521,8 +523,6 @@ struct rb_iseq_constant_body { #endif }; -typedef VALUE (*jit_func_t)(struct rb_execution_context_struct *, struct rb_control_frame_struct *); - /* T_IMEMO/iseq */ /* typedef rb_iseq_t is in method.h */ struct rb_iseq_struct { diff --git a/vm_exec.h b/vm_exec.h index 361516e86b..a00bd5027c 100644 --- a/vm_exec.h +++ b/vm_exec.h @@ -174,6 +174,14 @@ default: \ #define THROW_EXCEPTION(exc) return (VALUE)(exc) #endif +#define JIT_EXEC(ec, val) do { \ + rb_jit_func_t func; \ + if (val == Qundef && (func = jit_compile(ec))) { \ + val = func(ec, ec->cfp); \ + if (ec->tag->state) THROW_EXCEPTION(val); \ + } \ +} while (0) + #define SCREG(r) (reg_##r) #define VM_DEBUG_STACKOVERFLOW 0 diff --git a/yjit.c b/yjit.c index a9d1d2c38b..8e8342971e 100644 --- a/yjit.c +++ b/yjit.c @@ -1023,9 +1023,6 @@ rb_yjit_vm_unlock(unsigned int *recursive_lock_level, const char *file, int line rb_vm_lock_leave(recursive_lock_level, file, line); } -// Pointer to a YJIT entry point (machine code generated by YJIT) -typedef VALUE (*yjit_func_t)(rb_execution_context_t *, rb_control_frame_t *); - bool rb_yjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec) { @@ -1038,7 +1035,7 @@ rb_yjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec) uint8_t *code_ptr = rb_yjit_iseq_gen_entry_point(iseq, ec); if (code_ptr) { - iseq->body->jit_func = (yjit_func_t)code_ptr; + iseq->body->jit_func = (rb_jit_func_t)code_ptr; } else { iseq->body->jit_func = 0;