Skip to content

Commit 2cd10de

Browse files
authored
ZJIT: Prepare for sharing JIT hooks with ZJIT (#14044)
1 parent 4263c49 commit 2cd10de

File tree

20 files changed

+141
-117
lines changed

20 files changed

+141
-117
lines changed

array.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def fetch_values(*indexes, &block)
212212
indexes
213213
end
214214

215-
with_yjit do
215+
with_jit do
216216
if Primitive.rb_builtin_basic_definition_p(:each)
217217
undef :each
218218

common.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,8 +1236,9 @@ BUILTIN_RB_SRCS = \
12361236
$(srcdir)/nilclass.rb \
12371237
$(srcdir)/prelude.rb \
12381238
$(srcdir)/gem_prelude.rb \
1239+
$(srcdir)/jit_hook.rb \
1240+
$(srcdir)/jit_undef.rb \
12391241
$(srcdir)/yjit.rb \
1240-
$(srcdir)/yjit_hook.rb \
12411242
$(srcdir)/zjit.rb \
12421243
$(empty)
12431244
BUILTIN_RB_INCS = $(BUILTIN_RB_SRCS:.rb=.rbinc)

depend

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9196,6 +9196,8 @@ miniinit.$(OBJEXT): {$(VPATH)}internal/warning_push.h
91969196
miniinit.$(OBJEXT): {$(VPATH)}internal/xmalloc.h
91979197
miniinit.$(OBJEXT): {$(VPATH)}io.rb
91989198
miniinit.$(OBJEXT): {$(VPATH)}iseq.h
9199+
miniinit.$(OBJEXT): {$(VPATH)}jit_hook.rb
9200+
miniinit.$(OBJEXT): {$(VPATH)}jit_undef.rb
91999201
miniinit.$(OBJEXT): {$(VPATH)}kernel.rb
92009202
miniinit.$(OBJEXT): {$(VPATH)}marshal.rb
92019203
miniinit.$(OBJEXT): {$(VPATH)}method.h
@@ -9232,7 +9234,6 @@ miniinit.$(OBJEXT): {$(VPATH)}vm_core.h
92329234
miniinit.$(OBJEXT): {$(VPATH)}vm_opts.h
92339235
miniinit.$(OBJEXT): {$(VPATH)}warning.rb
92349236
miniinit.$(OBJEXT): {$(VPATH)}yjit.rb
9235-
miniinit.$(OBJEXT): {$(VPATH)}yjit_hook.rb
92369237
miniinit.$(OBJEXT): {$(VPATH)}zjit.rb
92379238
namespace.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
92389239
namespace.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h
@@ -18755,6 +18756,8 @@ vm.$(OBJEXT): {$(VPATH)}internal/variable.h
1875518756
vm.$(OBJEXT): {$(VPATH)}internal/warning_push.h
1875618757
vm.$(OBJEXT): {$(VPATH)}internal/xmalloc.h
1875718758
vm.$(OBJEXT): {$(VPATH)}iseq.h
18759+
vm.$(OBJEXT): {$(VPATH)}jit_hook.rbinc
18760+
vm.$(OBJEXT): {$(VPATH)}jit_undef.rbinc
1875818761
vm.$(OBJEXT): {$(VPATH)}method.h
1875918762
vm.$(OBJEXT): {$(VPATH)}missing.h
1876018763
vm.$(OBJEXT): {$(VPATH)}node.h
@@ -18797,7 +18800,6 @@ vm.$(OBJEXT): {$(VPATH)}vm_opts.h
1879718800
vm.$(OBJEXT): {$(VPATH)}vm_sync.h
1879818801
vm.$(OBJEXT): {$(VPATH)}vmtc.inc
1879918802
vm.$(OBJEXT): {$(VPATH)}yjit.h
18800-
vm.$(OBJEXT): {$(VPATH)}yjit_hook.rbinc
1880118803
vm.$(OBJEXT): {$(VPATH)}zjit.h
1880218804
vm_backtrace.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
1880318805
vm_backtrace.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h

inits.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ void
8888
rb_call_builtin_inits(void)
8989
{
9090
#define BUILTIN(n) CALL(builtin_##n)
91-
BUILTIN(kernel);
91+
BUILTIN(jit_hook);
9292
BUILTIN(yjit);
93+
BUILTIN(zjit);
94+
BUILTIN(kernel);
9395
BUILTIN(gc);
9496
BUILTIN(ractor);
9597
BUILTIN(numeric);
@@ -107,8 +109,7 @@ rb_call_builtin_inits(void)
107109
BUILTIN(thread_sync);
108110
BUILTIN(nilclass);
109111
BUILTIN(marshal);
110-
BUILTIN(zjit);
111-
BUILTIN(yjit_hook);
112+
BUILTIN(jit_undef);
112113
Init_builtin_prelude();
113114
}
114115
#undef CALL

internal/cmdlineopt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ typedef struct ruby_cmdline_options {
2323
ruby_features_t warn;
2424
unsigned int dump;
2525
long backtrace_length_limit;
26-
#if USE_ZJIT
27-
void *zjit;
28-
#endif
2926

3027
const char *crash_report;
3128

@@ -42,6 +39,9 @@ typedef struct ruby_cmdline_options {
4239
#if USE_YJIT
4340
unsigned int yjit: 1;
4441
#endif
42+
#if USE_ZJIT
43+
unsigned int zjit: 1;
44+
#endif
4545
} ruby_cmdline_options_t;
4646

4747
struct ruby_opt_message {

jit_hook.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Module
2+
# Internal helper for built-in initializations to define methods only when JIT is enabled.
3+
# This method is removed in jit_undef.rb.
4+
private def with_jit(&block) # :nodoc:
5+
# ZJIT currently doesn't compile Array#each properly, so it's disabled for now.
6+
if defined?(RubyVM::ZJIT) && Primitive.rb_zjit_option_enabled_p && false # TODO: remove `&& false` (Shopify/ruby#667)
7+
# We don't support lazily enabling ZJIT yet, so we can call the block right away.
8+
block.call
9+
elsif defined?(RubyVM::YJIT)
10+
RubyVM::YJIT.send(:add_jit_hook, block)
11+
end
12+
end
13+
end

jit_undef.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Remove the helper defined in jit_hook.rb
2+
class Module
3+
undef :with_jit
4+
end

kernel.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,3 @@ def Integer(arg, base = 0, exception: true)
291291
end
292292
end
293293
end
294-
295-
class Module
296-
# Internal helper for built-in initializations to define methods only when YJIT is enabled.
297-
# This method is removed in yjit_hook.rb.
298-
private def with_yjit(&block) # :nodoc:
299-
if defined?(RubyVM::YJIT)
300-
RubyVM::YJIT.send(:add_yjit_hook, block)
301-
end
302-
end
303-
end

numeric.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def denominator
322322
1
323323
end
324324

325-
with_yjit do
325+
with_jit do
326326
if Primitive.rb_builtin_basic_definition_p(:downto)
327327
undef :downto
328328

ruby.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,14 +1196,12 @@ setup_yjit_options(const char *s)
11961196

11971197
#if USE_ZJIT
11981198
static void
1199-
setup_zjit_options(ruby_cmdline_options_t *opt, const char *s)
1199+
setup_zjit_options(const char *s)
12001200
{
12011201
// The option parsing is done in zjit/src/options.rs
1202-
extern void *rb_zjit_init_options(void);
1203-
extern bool rb_zjit_parse_option(void *options, const char *s);
1202+
extern bool rb_zjit_parse_option(const char *s);
12041203

1205-
if (!opt->zjit) opt->zjit = rb_zjit_init_options();
1206-
if (!rb_zjit_parse_option(opt->zjit, s)) {
1204+
if (!rb_zjit_parse_option(s)) {
12071205
rb_raise(rb_eRuntimeError, "invalid ZJIT option '%s' (--help will show valid zjit options)", s);
12081206
}
12091207
}
@@ -1481,7 +1479,7 @@ proc_long_options(ruby_cmdline_options_t *opt, const char *s, long argc, char **
14811479
else if (is_option_with_optarg("zjit", '-', true, false, false)) {
14821480
#if USE_ZJIT
14831481
FEATURE_SET(opt->features, FEATURE_BIT(zjit));
1484-
setup_zjit_options(opt, s);
1482+
setup_zjit_options(s);
14851483
#else
14861484
rb_warn("Ruby was built without ZJIT support."
14871485
" You may need to install rustc to build Ruby with ZJIT.");
@@ -1828,8 +1826,8 @@ ruby_opt_init(ruby_cmdline_options_t *opt)
18281826
#endif
18291827
#if USE_ZJIT
18301828
if (opt->zjit) {
1831-
extern void rb_zjit_init(void *options);
1832-
rb_zjit_init(opt->zjit);
1829+
extern void rb_zjit_init(void);
1830+
rb_zjit_init();
18331831
}
18341832
#endif
18351833

@@ -2370,8 +2368,9 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
23702368
#endif
23712369
#if USE_ZJIT
23722370
if (FEATURE_SET_P(opt->features, zjit) && !opt->zjit) {
2373-
extern void *rb_zjit_init_options(void);
2374-
opt->zjit = rb_zjit_init_options();
2371+
extern void rb_zjit_prepare_options(void);
2372+
rb_zjit_prepare_options();
2373+
opt->zjit = true;
23752374
}
23762375
#endif
23772376

0 commit comments

Comments
 (0)