diff options
author | Takashi Kokubun <takashikkbn@gmail.com> | 2022-12-06 21:27:46 -0800 |
---|---|---|
committer | Takashi Kokubun <takashikkbn@gmail.com> | 2022-12-06 21:36:58 -0800 |
commit | e6b63b382c30cb665d24853f745586a157ae58f3 (patch) | |
tree | b5e32f5aac0760b86c9cdf3a2fbb5a9ca42814b2 | |
parent | 8684904cb418df3f6d15ea0b0ad5c7c357d7ef06 (diff) | |
download | ruby-e6b63b382c30cb665d24853f745586a157ae58f3.tar.gz |
MJIT: Refactor the jit_func enum for MJIT
All values should have a MJIT_ prefix. We could address the warning for
the end mark if we just define the macro for the check next to the enum.
It even simplifies some code for checking the enum.
-rw-r--r-- | mjit.c | 28 | ||||
-rw-r--r-- | mjit.h | 12 | ||||
-rw-r--r-- | vm.c | 15 |
3 files changed, 27 insertions, 28 deletions
@@ -343,13 +343,13 @@ remove_file(const char *filename) // 3) Freeing lists on `mjit_finish()`. // // `jit_func` value does not matter for 1 and 3 since the unit won't be used anymore. -// For the situation 2, this sets the ISeq's JIT state to NOT_COMPILED_JIT_ISEQ_FUNC +// For the situation 2, this sets the ISeq's JIT state to MJIT_FUNC_FAILED // to prevent the situation that the same methods are continuously compiled. static void free_unit(struct rb_mjit_unit *unit) { if (unit->iseq) { // ISeq is not GCed - ISEQ_BODY(unit->iseq)->jit_func = (jit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC; + ISEQ_BODY(unit->iseq)->jit_func = (jit_func_t)MJIT_FUNC_FAILED; ISEQ_BODY(unit->iseq)->jit_unit = NULL; } if (unit->cc_entries) { @@ -794,7 +794,7 @@ load_func_from_so(const char *so_file, const char *funcname, struct rb_mjit_unit handle = dlopen(so_file, RTLD_NOW); if (handle == NULL) { mjit_warning("failure in loading code from '%s': %s", so_file, dlerror()); - return (void *)NOT_COMPILED_JIT_ISEQ_FUNC; + return (void *)MJIT_FUNC_FAILED; } func = dlsym(handle, funcname); @@ -841,7 +841,7 @@ compile_prelude(FILE *f) } // Compile ISeq in UNIT and return function pointer of JIT-ed code. -// It may return NOT_COMPILED_JIT_ISEQ_FUNC if something went wrong. +// It may return MJIT_FUNC_FAILED if something went wrong. static bool mjit_compile_unit(struct rb_mjit_unit *unit) { @@ -1176,7 +1176,7 @@ check_unit_queue(void) current_cc_pid = start_c_compile_unit(unit); if (current_cc_pid == -1) { // JIT failure current_cc_pid = 0; - current_cc_unit->iseq->body->jit_func = (jit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC; // TODO: consider unit->compact_p + current_cc_unit->iseq->body->jit_func = (jit_func_t)MJIT_FUNC_FAILED; // TODO: consider unit->compact_p current_cc_unit = NULL; } } @@ -1257,7 +1257,7 @@ mjit_notify_waitpid(int exit_code) if (exit_code != 0) { verbose(2, "Failed to generate so"); if (!current_cc_unit->compact_p) { - current_cc_unit->iseq->body->jit_func = (jit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC; + current_cc_unit->iseq->body->jit_func = (jit_func_t)MJIT_FUNC_FAILED; } free_unit(current_cc_unit); current_cc_unit = NULL; @@ -1284,7 +1284,7 @@ mjit_notify_waitpid(int exit_code) // Set the jit_func if successful if (current_cc_unit->iseq != NULL) { // mjit_free_iseq could nullify this rb_iseq_t *iseq = current_cc_unit->iseq; - if ((uintptr_t)func > (uintptr_t)LAST_JIT_ISEQ_FUNC) { + if (!MJIT_FUNC_STATE_P(func)) { double end_time = real_ms_time(); verbose(1, "JIT success (%.1fms): %s@%s:%d -> %s", end_time - current_cc_ms, RSTRING_PTR(ISEQ_BODY(iseq)->location.label), @@ -1355,12 +1355,12 @@ mjit_add_iseq_to_process(const rb_iseq_t *iseq, const struct rb_mjit_compile_inf return; } if (!mjit_target_iseq_p(iseq)) { - ISEQ_BODY(iseq)->jit_func = (jit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC; // skip mjit_wait + ISEQ_BODY(iseq)->jit_func = (jit_func_t)MJIT_FUNC_FAILED; // skip mjit_wait return; } RB_DEBUG_COUNTER_INC(mjit_add_iseq_to_process); - ISEQ_BODY(iseq)->jit_func = (jit_func_t)NOT_READY_JIT_ISEQ_FUNC; + ISEQ_BODY(iseq)->jit_func = (jit_func_t)MJIT_FUNC_COMPILING; create_unit(iseq); if (ISEQ_BODY(iseq)->jit_unit == NULL) // Failure in creating the unit. @@ -1398,11 +1398,11 @@ mjit_wait(struct rb_iseq_constant_body *body) struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 1000; - while (body == NULL ? current_cc_pid == initial_pid : body->jit_func == (jit_func_t)NOT_READY_JIT_ISEQ_FUNC) { // TODO: refactor this + while (body == NULL ? current_cc_pid == initial_pid : body->jit_func == (jit_func_t)MJIT_FUNC_COMPILING) { // TODO: refactor this tries++; if (tries / 1000 > MJIT_WAIT_TIMEOUT_SECONDS || pch_status == PCH_FAILED) { if (body != NULL) { - body->jit_func = (jit_func_t) NOT_COMPILED_JIT_ISEQ_FUNC; // JIT worker seems dead. Give up. + body->jit_func = (jit_func_t)MJIT_FUNC_FAILED; // JIT worker seems dead. Give up. } mjit_warning("timed out to wait for JIT finish"); break; @@ -1424,7 +1424,7 @@ mjit_wait_unit(struct rb_mjit_unit *unit) } // Wait for JIT compilation finish for --jit-wait, and call the function pointer -// if the compiled result is not NOT_COMPILED_JIT_ISEQ_FUNC. +// if the compiled result is not MJIT_FUNC_FAILED. VALUE rb_mjit_wait_call(rb_execution_context_t *ec, struct rb_iseq_constant_body *body) { @@ -1432,7 +1432,7 @@ rb_mjit_wait_call(rb_execution_context_t *ec, struct rb_iseq_constant_body *body return Qundef; mjit_wait(body); - if ((uintptr_t)body->jit_func <= (uintptr_t)LAST_JIT_ISEQ_FUNC) { + if (MJIT_FUNC_STATE_P(body->jit_func)) { return Qundef; } return body->jit_func(ec, ec->cfp); @@ -1448,7 +1448,7 @@ rb_mjit_iseq_compile_info(const struct rb_iseq_constant_body *body) static void mjit_recompile(const rb_iseq_t *iseq) { - if ((uintptr_t)ISEQ_BODY(iseq)->jit_func <= (uintptr_t)LAST_JIT_ISEQ_FUNC) + if (MJIT_FUNC_STATE_P(ISEQ_BODY(iseq)->jit_func)) return; verbose(1, "JIT recompile: %s@%s:%d", RSTRING_PTR(ISEQ_BODY(iseq)->location.label), @@ -20,18 +20,18 @@ // Special address values of a function generated from the // corresponding iseq by MJIT: -enum rb_mjit_iseq_func { +enum rb_mjit_func_state { // ISEQ has never been enqueued to unit_queue yet - NOT_ADDED_JIT_ISEQ_FUNC = 0, + MJIT_FUNC_NOT_QUEUED = 0, // ISEQ is already queued for the machine code generation but the // code is not ready yet for the execution - NOT_READY_JIT_ISEQ_FUNC = 1, + MJIT_FUNC_COMPILING = 1, // ISEQ included not compilable insn, some internal assertion failed // or the unit is unloaded - NOT_COMPILED_JIT_ISEQ_FUNC = 2, - // End mark - LAST_JIT_ISEQ_FUNC = 3 + MJIT_FUNC_FAILED = 2, }; +// Return true if jit_func is part of enum rb_mjit_func_state +#define MJIT_FUNC_STATE_P(jit_func) ((uintptr_t)(jit_func) <= (uintptr_t)MJIT_FUNC_FAILED) // MJIT options which can be defined on the MRI command line. struct mjit_options { @@ -388,19 +388,18 @@ static VALUE mjit_check_iseq(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body) { uintptr_t func_i = (uintptr_t)(body->jit_func); - ASSUME(func_i <= LAST_JIT_ISEQ_FUNC); - switch ((enum rb_mjit_iseq_func)func_i) { - case NOT_ADDED_JIT_ISEQ_FUNC: + ASSUME(MJIT_FUNC_STATE_P(func_i)); + switch ((enum rb_mjit_func_state)func_i) { + case MJIT_FUNC_NOT_QUEUED: if (body->total_calls == mjit_opts.call_threshold) { rb_mjit_add_iseq_to_process(iseq); - if (UNLIKELY(mjit_opts.wait && (uintptr_t)body->jit_func > LAST_JIT_ISEQ_FUNC)) { + if (UNLIKELY(mjit_opts.wait && !MJIT_FUNC_STATE_P(body->jit_func))) { return body->jit_func(ec, ec->cfp); } } break; - case NOT_READY_JIT_ISEQ_FUNC: - case NOT_COMPILED_JIT_ISEQ_FUNC: - default: // to avoid warning with LAST_JIT_ISEQ_FUNC + case MJIT_FUNC_COMPILING: + case MJIT_FUNC_FAILED: break; } return Qundef; @@ -439,7 +438,7 @@ jit_exec(rb_execution_context_t *ec) return Qundef; } } - else if (UNLIKELY((uintptr_t)(func = body->jit_func) <= LAST_JIT_ISEQ_FUNC)) { + else if (UNLIKELY(MJIT_FUNC_STATE_P(func = body->jit_func))) { return mjit_check_iseq(ec, iseq, body); } |