[前][次][番号順一覧][スレッド一覧]

ruby-changes:74238

From: Takashi <ko1@a...>
Date: Tue, 25 Oct 2022 01:21:12 +0900 (JST)
Subject: [ruby-changes:74238] 120b747b7d (master): YJIT: Lazily enable YJIT after prelude (#6597)

https://git.ruby-lang.org/ruby.git/commit/?id=120b747b7d

From 120b747b7df25722c982ff4ba0f8be9a1463bd78 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Mon, 24 Oct 2022 09:20:44 -0700
Subject: YJIT: Lazily enable YJIT after prelude (#6597)

* YJIT: Lazily enable YJIT after prelude

* Update dependencies

* Use a bit field for opt->yjit
---
 common.mk             |  1 +
 internal/cmdlineopt.h |  3 +++
 ruby.c                | 33 +++++++++++++++------------------
 version.c             | 13 ++++++++++---
 4 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/common.mk b/common.mk
index 934939ea4d..d6cbffe6d6 100644
--- a/common.mk
+++ b/common.mk
@@ -16771,6 +16771,7 @@ version.$(OBJEXT): $(hdrdir)/ruby.h https://github.com/ruby/ruby/blob/trunk/common.mk#L16771
 version.$(OBJEXT): $(hdrdir)/ruby/ruby.h
 version.$(OBJEXT): $(hdrdir)/ruby/version.h
 version.$(OBJEXT): $(top_srcdir)/internal/array.h
+version.$(OBJEXT): $(top_srcdir)/internal/cmdlineopt.h
 version.$(OBJEXT): $(top_srcdir)/internal/compilers.h
 version.$(OBJEXT): $(top_srcdir)/internal/gc.h
 version.$(OBJEXT): $(top_srcdir)/internal/imemo.h
diff --git a/internal/cmdlineopt.h b/internal/cmdlineopt.h
index 71568a8745..bf52f1214b 100644
--- a/internal/cmdlineopt.h
+++ b/internal/cmdlineopt.h
@@ -36,6 +36,9 @@ typedef struct ruby_cmdline_options { https://github.com/ruby/ruby/blob/trunk/internal/cmdlineopt.h#L36
     unsigned int do_split: 1;
     unsigned int do_search: 1;
     unsigned int setids: 2;
+#if USE_YJIT
+    unsigned int yjit: 1;
+#endif
 } ruby_cmdline_options_t;
 
 struct ruby_opt_message {
diff --git a/ruby.c b/ruby.c
index 640386fbda..fac0865302 100644
--- a/ruby.c
+++ b/ruby.c
@@ -73,7 +73,7 @@ STATIC_ASSERT(Qundef_1bit_from_Qnil, singlebit_only_p(Qundef^Qnil)); https://github.com/ruby/ruby/blob/trunk/ruby.c#L73
 # define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
 #endif
 
-void Init_ruby_description(void);
+void Init_ruby_description(ruby_cmdline_options_t *opt);
 
 #ifndef HAVE_STDLIB_H
 char *getenv();
@@ -1614,13 +1614,8 @@ ruby_opt_init(ruby_cmdline_options_t *opt) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1614
 
 #if USE_MJIT
     // rb_call_builtin_inits depends on RubyVM::MJIT.enabled?
-    if (opt->mjit.on) {
+    if (opt->mjit.on)
         mjit_enabled = true;
-        // rb_threadptr_root_fiber_setup for the initial thread is called before rb_yjit_enabled_p()
-        // or mjit_enabled becomes true, meaning jit_cont_new is skipped for the initial root fiber.
-        // Therefore we need to call this again here to set the initial root fiber's jit_cont.
-        rb_jit_cont_init(); // must be after mjit_enabled = true
-    }
 #endif
 
     Init_ext(); /* load statically linked extensions before rubygems */
@@ -1628,11 +1623,20 @@ ruby_opt_init(ruby_cmdline_options_t *opt) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1623
     rb_call_builtin_inits();
     ruby_init_prelude();
 
+    // Initialize JITs after prelude because JITing prelude is typically not optimal.
 #if USE_MJIT
-    // mjit_init is safe only after rb_call_builtin_inits defines RubyVM::MJIT::Compiler
+    // Also, mjit_init is safe only after rb_call_builtin_inits() defines RubyVM::MJIT::Compiler.
     if (opt->mjit.on)
         mjit_init(&opt->mjit);
 #endif
+#if USE_YJIT
+    if (opt->yjit)
+        rb_yjit_init();
+#endif
+    // rb_threadptr_root_fiber_setup for the initial thread is called before rb_yjit_enabled_p()
+    // or mjit_enabled becomes true, meaning jit_cont_new is skipped for the initial root fiber.
+    // Therefore we need to call this again here to set the initial root fiber's jit_cont.
+    rb_jit_cont_init(); // must be after mjit_enabled = true and rb_yjit_init()
 
     ruby_set_script_name(opt->script_name);
     require_libraries(&opt->req_list);
@@ -1916,22 +1920,15 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1920
 
 #if USE_MJIT
     if (FEATURE_SET_P(opt->features, mjit)) {
-        opt->mjit.on = true; // set mjit.on for ruby_show_version() API and check to call mjit_init()
+        opt->mjit.on = true; // set opt->mjit.on for Init_ruby_description() and calling mjit_init()
     }
 #endif
 #if USE_YJIT
     if (FEATURE_SET_P(opt->features, yjit)) {
-        rb_yjit_init();
-        // rb_threadptr_root_fiber_setup for the initial thread is called before rb_yjit_enabled_p()
-        // or mjit_enabled becomes true, meaning jit_cont_new is skipped for the initial root fiber.
-        // Therefore we need to call this again here to set the initial root fiber's jit_cont.
-        rb_jit_cont_init(); // must be after rb_yjit_init(), but before parsing options raises an exception.
+        opt->yjit = true; // set opt->yjit for Init_ruby_description() and calling rb_yjit_init()
     }
 #endif
-#if USE_MJIT
-    mjit_opts.on = opt->mjit.on; /* used by Init_ruby_description(). mjit_init() still can't be called here. */
-#endif
-    Init_ruby_description();
+    Init_ruby_description(opt);
     if (opt->dump & (DUMP_BIT(version) | DUMP_BIT(version_v))) {
         ruby_show_version();
         if (opt->dump & DUMP_BIT(version)) return Qtrue;
diff --git a/version.c b/version.c
index 5ccc4cc508..a2d278f2db 100644
--- a/version.c
+++ b/version.c
@@ -9,6 +9,7 @@ https://github.com/ruby/ruby/blob/trunk/version.c#L9
 
 **********************************************************************/
 
+#include "internal/cmdlineopt.h"
 #include "ruby/ruby.h"
 #include "version.h"
 #include "vm_core.h"
@@ -121,13 +122,19 @@ Init_version(void) https://github.com/ruby/ruby/blob/trunk/version.c#L122
 }
 
 #if USE_MJIT
-#define MJIT_OPTS_ON mjit_opts.on
+#define MJIT_OPTS_ON opt->mjit.on
 #else
 #define MJIT_OPTS_ON 0
 #endif
 
+#if USE_YJIT
+#define YJIT_OPTS_ON opt->yjit
+#else
+#define YJIT_OPTS_ON 0
+#endif
+
 void
-Init_ruby_description(void)
+Init_ruby_description(ruby_cmdline_options_t *opt)
 {
     VALUE description;
 
@@ -135,7 +142,7 @@ Init_ruby_description(void) https://github.com/ruby/ruby/blob/trunk/version.c#L142
         rb_dynamic_description = ruby_description_with_mjit;
         description = MKSTR(description_with_mjit);
     }
-    else if (rb_yjit_enabled_p()) {
+    else if (YJIT_OPTS_ON) {
         rb_dynamic_description = ruby_description_with_yjit;
         description = MKSTR(description_with_yjit);
     }
-- 
cgit v1.2.3


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]