From 23459e4dbb736b28873a85637d141f77167cacee Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Wed, 15 Jun 2022 10:19:33 -0700 Subject: [PATCH] Move RubyVM::MJIT to builtin Ruby just less C code to maintain --- common.mk | 4 ++++ inits.c | 3 +++ mjit.c | 10 +++++++--- mjit.rb | 13 +++++++++++++ vm.c | 39 --------------------------------------- 5 files changed, 27 insertions(+), 42 deletions(-) create mode 100644 mjit.rb diff --git a/common.mk b/common.mk index 56139211de..c36326333d 100644 --- a/common.mk +++ b/common.mk @@ -1054,6 +1054,7 @@ BUILTIN_RB_SRCS = \ $(srcdir)/numeric.rb \ $(srcdir)/io.rb \ $(srcdir)/marshal.rb \ + $(srcdir)/mjit.rb \ $(srcdir)/pack.rb \ $(srcdir)/trace_point.rb \ $(srcdir)/warning.rb \ @@ -9430,6 +9431,7 @@ miniinit.$(OBJEXT): {$(VPATH)}mini_builtin.c miniinit.$(OBJEXT): {$(VPATH)}miniinit.c miniinit.$(OBJEXT): {$(VPATH)}miniprelude.c miniinit.$(OBJEXT): {$(VPATH)}missing.h +miniinit.$(OBJEXT): {$(VPATH)}mjit.rb miniinit.$(OBJEXT): {$(VPATH)}nilclass.rb miniinit.$(OBJEXT): {$(VPATH)}node.h miniinit.$(OBJEXT): {$(VPATH)}numeric.rb @@ -9652,6 +9654,8 @@ mjit.$(OBJEXT): {$(VPATH)}method.h mjit.$(OBJEXT): {$(VPATH)}missing.h mjit.$(OBJEXT): {$(VPATH)}mjit.c mjit.$(OBJEXT): {$(VPATH)}mjit.h +mjit.$(OBJEXT): {$(VPATH)}mjit.rb +mjit.$(OBJEXT): {$(VPATH)}mjit.rbinc mjit.$(OBJEXT): {$(VPATH)}mjit_config.h mjit.$(OBJEXT): {$(VPATH)}mjit_worker.c mjit.$(OBJEXT): {$(VPATH)}node.h diff --git a/inits.c b/inits.c index 8c230c6df0..f41e88d838 100644 --- a/inits.c +++ b/inits.c @@ -101,6 +101,9 @@ rb_call_builtin_inits(void) BUILTIN(yjit); BUILTIN(nilclass); BUILTIN(marshal); +#if USE_MJIT + BUILTIN(mjit); +#endif Init_builtin_prelude(); } #undef CALL diff --git a/mjit.c b/mjit.c index 93e3d823e4..2e471146b8 100644 --- a/mjit.c +++ b/mjit.c @@ -375,10 +375,12 @@ mjit_notify_waitpid(int status) // Return true if given ISeq body should be compiled by MJIT static inline int -mjit_target_iseq_p(struct rb_iseq_constant_body *body) +mjit_target_iseq_p(const rb_iseq_t *iseq) { + struct rb_iseq_constant_body *body = ISEQ_BODY(iseq); return (body->type == ISEQ_TYPE_METHOD || body->type == ISEQ_TYPE_BLOCK) - && !body->builtin_inline_p; + && !body->builtin_inline_p + && strcmp("", RSTRING_PTR(rb_iseq_path(iseq))); } // If recompile_p is true, the call is initiated by mjit_recompile. @@ -389,7 +391,7 @@ mjit_add_iseq_to_process(const rb_iseq_t *iseq, const struct rb_mjit_compile_inf // TODO: Support non-main Ractors if (!mjit_enabled || pch_status == PCH_FAILED || !rb_ractor_main_p()) return; - if (!mjit_target_iseq_p(ISEQ_BODY(iseq))) { + if (!mjit_target_iseq_p(iseq)) { ISEQ_BODY(iseq)->jit_func = (mjit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC; // skip mjit_wait return; } @@ -1104,4 +1106,6 @@ mjit_mark_cc_entries(const struct rb_iseq_constant_body *const body) } } +#include "mjit.rbinc" + #endif // USE_MJIT diff --git a/mjit.rb b/mjit.rb new file mode 100644 index 0000000000..baa107d6dc --- /dev/null +++ b/mjit.rb @@ -0,0 +1,13 @@ +module RubyVM::MJIT + def self.enabled? + Primitive.cexpr! 'RBOOL(mjit_enabled)' + end + + def self.pause(wait: true) + Primitive.cexpr! 'mjit_pause(RTEST(wait))' + end + + def self.resume + Primitive.cexpr! 'mjit_resume()' + end +end diff --git a/vm.c b/vm.c index 76041b0b91..0d74521b37 100644 --- a/vm.c +++ b/vm.c @@ -3408,36 +3408,6 @@ core_hash_merge_kwd(VALUE hash, VALUE kw) return hash; } -/* Returns true if JIT is enabled */ -static VALUE -mjit_enabled_p(VALUE _) -{ - return RBOOL(mjit_enabled); -} - -static VALUE -mjit_pause_m(int argc, VALUE *argv, RB_UNUSED_VAR(VALUE self)) -{ - VALUE options = Qnil; - VALUE wait = Qtrue; - rb_scan_args(argc, argv, "0:", &options); - - if (!NIL_P(options)) { - static ID keyword_ids[1]; - if (!keyword_ids[0]) - keyword_ids[0] = rb_intern("wait"); - rb_get_kwargs(options, keyword_ids, 0, 1, &wait); - } - - return mjit_pause(RTEST(wait)); -} - -static VALUE -mjit_resume_m(VALUE _) -{ - return mjit_resume(); -} - extern VALUE *rb_gc_stack_start; extern size_t rb_gc_stack_maxsize; @@ -3618,15 +3588,6 @@ Init_VM(void) rb_gc_register_mark_object(fcore); rb_mRubyVMFrozenCore = fcore; - /* ::RubyVM::MJIT - * Provides access to the Method JIT compiler of MRI. - * Of course, this module is MRI specific. - */ - VALUE mjit = rb_define_module_under(rb_cRubyVM, "MJIT"); - rb_define_singleton_method(mjit, "enabled?", mjit_enabled_p, 0); - rb_define_singleton_method(mjit, "pause", mjit_pause_m, -1); - rb_define_singleton_method(mjit, "resume", mjit_resume_m, 0); - /* * Document-class: Thread *