16#include "internal/class.h"
17#include "internal/cmdlineopt.h"
18#include "internal/cont.h"
19#include "internal/file.h"
20#include "internal/hash.h"
21#include "internal/process.h"
22#include "internal/warnings.h"
24#include "ractor_core.h"
27#define __EXTENSIONS__ 1
31#include "vm_callinfo.h"
33#include "ruby_assert.h"
39#include "insns_info.inc"
40#include "internal/compile.h"
41#include "internal/gc.h"
50#ifdef HAVE_SYS_PARAM_H
51# include <sys/param.h>
66struct rb_rjit_options rb_rjit_opts;
69bool rb_rjit_enabled =
false;
71bool rb_rjit_stats_enabled =
false;
73bool rb_rjit_trace_exits_enabled =
false;
76bool rb_rjit_call_p =
false;
78static bool rjit_cancel_p =
false;
84static bool rjit_stats_p =
false;
87static VALUE rb_mRJIT = 0;
89static VALUE rb_mRJITC = 0;
91static VALUE rb_RJITCompiler = 0;
93static VALUE rb_cRJITIseqPtr = 0;
95static VALUE rb_cRJITCfpPtr = 0;
97static VALUE rb_mRJITHooks = 0;
100VALUE rb_rjit_raw_samples = 0;
102VALUE rb_rjit_line_samples = 0;
108#define DEFAULT_CALL_THRESHOLD 10
110#define DEFAULT_EXEC_MEM_SIZE 64
112#define opt_match_noarg(s, l, name) \
113 opt_match(s, l, name) && (*(s) ? (rb_warn("argument to --rjit-" name " is ignored"), 1) : 1)
114#define opt_match_arg(s, l, name) \
115 opt_match(s, l, name) && (*(s) ? 1 : (rb_raise(rb_eRuntimeError, "--rjit-" name " needs an argument"), 0))
118rb_rjit_setup_options(
const char *s,
struct rb_rjit_options *rjit_opt)
120 const size_t l = strlen(s);
124 else if (opt_match_arg(s, l,
"exec-mem-size")) {
125 rjit_opt->exec_mem_size = atoi(s + 1);
127 else if (opt_match_arg(s, l,
"call-threshold")) {
128 rjit_opt->call_threshold = atoi(s + 1);
130 else if (opt_match_noarg(s, l,
"stats")) {
131 rjit_opt->stats =
true;
133 else if (opt_match_noarg(s, l,
"disable")) {
134 rjit_opt->disable =
true;
136 else if (opt_match_noarg(s, l,
"trace")) {
137 rjit_opt->trace =
true;
139 else if (opt_match_noarg(s, l,
"trace-exits")) {
140 rjit_opt->trace_exits =
true;
142 else if (opt_match_noarg(s, l,
"dump-disasm")) {
143 rjit_opt->dump_disasm =
true;
145 else if (opt_match_noarg(s, l,
"verify-ctx")) {
146 rjit_opt->verify_ctx =
true;
150 "invalid RJIT option '%s' (--help will show valid RJIT options)", s);
154#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
156 M(
"--rjit-exec-mem-size=num",
"",
"Size of executable memory block in MiB (default: " STRINGIZE(DEFAULT_EXEC_MEM_SIZE)
")."),
157 M(
"--rjit-call-threshold=num",
"",
"Number of calls to trigger JIT (default: " STRINGIZE(DEFAULT_CALL_THRESHOLD)
")."),
158 M(
"--rjit-stats",
"",
"Enable collecting RJIT statistics."),
159 M(
"--rjit-disable",
"",
"Disable RJIT for lazily enabling it with RubyVM::RJIT.enable."),
160 M(
"--rjit-trace",
"",
"Allow TracePoint during JIT compilation."),
161 M(
"--rjit-trace-exits",
"",
"Trace side exit locations."),
162#ifdef HAVE_LIBCAPSTONE
163 M(
"--rjit-dump-disasm",
"",
"Dump all JIT code"),
169struct rb_rjit_runtime_counters rb_rjit_counters = { 0 };
171extern VALUE rb_gc_enable(
void);
172extern VALUE rb_gc_disable(
void);
173extern uint64_t rb_vm_insns_count;
176#define WITH_RJIT_ISOLATED_USING_PC(using_pc, stmt) do { \
177 VALUE was_disabled = rb_gc_disable(); \
179 rb_hook_list_t *global_hooks = rb_ec_ractor_hooks(GET_EC()); \
180 rb_rjit_global_events = global_hooks->events; \
182 const VALUE *pc = NULL; \
183 if (rb_rjit_opts.trace) { \
184 pc = GET_EC()->cfp->pc; \
185 if (!using_pc) GET_EC()->cfp->pc = 0; \
187 else global_hooks->events = 0; \
189 bool original_call_p = rb_rjit_call_p; \
190 rb_rjit_call_p = false; \
192 rjit_stats_p = false; \
193 uint64_t insns_count = rb_vm_insns_count; \
195 VALUE err = rb_errinfo(); \
199 rb_set_errinfo(err); \
201 rb_vm_insns_count = insns_count; \
202 rjit_stats_p = rb_rjit_opts.stats; \
204 rb_rjit_call_p = (rjit_cancel_p ? false : original_call_p); \
206 if (rb_rjit_opts.trace) GET_EC()->cfp->pc = pc; \
207 else global_hooks->events = rb_rjit_global_events; \
209 if (!was_disabled) rb_gc_enable(); \
211#define WITH_RJIT_ISOLATED(stmt) WITH_RJIT_ISOLATED_USING_PC(false, stmt)
214rb_rjit_cancel_all(
const char *reason)
216 if (!rb_rjit_enabled)
219 rb_rjit_call_p =
false;
220 rjit_cancel_p =
true;
224rb_rjit_bop_redefined(
int redefined_flag,
enum ruby_basic_operators bop)
226 if (!rb_rjit_call_p)
return;
227 rb_rjit_call_p =
false;
231rjit_cme_invalidate(
void *data)
233 if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks)
return;
244 if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks)
return;
246 rb_workqueue_register(0, rjit_cme_invalidate, (
void *)cme);
250rb_rjit_before_ractor_spawn(
void)
252 if (!rb_rjit_call_p)
return;
253 rb_rjit_call_p =
false;
257rjit_constant_state_changed(
void *data)
259 if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks)
return;
264 rb_funcall(rb_mRJITHooks, rb_intern(
"on_constant_state_changed"), 1,
SIZET2NUM((
size_t)data));
271rb_rjit_constant_state_changed(
ID id)
273 if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks)
return;
275 rb_workqueue_register(0, rjit_constant_state_changed, (
void *)
id);
279rb_rjit_constant_ic_update(
const rb_iseq_t *
const iseq,
IC ic,
unsigned insn_idx)
281 if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks)
return;
287 rb_funcall(rb_mRJITHooks, rb_intern(
"on_constant_ic_update"), 3,
297 if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks)
return;
299 rb_funcall(rb_mRJITHooks, rb_intern(
"on_tracing_invalidate_all"), 1,
UINT2NUM(new_iseq_events));
304rjit_iseq_update_references(
void *data)
306 if (!rb_rjit_enabled || !rb_rjit_call_p || !rb_mRJITHooks)
return;
308 rb_funcall(rb_mRJITHooks, rb_intern(
"on_update_references"), 0);
315 if (!rb_rjit_enabled)
return;
317 if (body->rjit_blocks) {
318 body->rjit_blocks = rb_gc_location(body->rjit_blocks);
328rb_rjit_iseq_mark(
VALUE rjit_blocks)
330 if (!rb_rjit_enabled)
return;
335 rb_gc_mark_movable(rjit_blocks);
343 if (!rb_rjit_enabled)
345 RUBY_MARK_ENTER(
"rjit");
348 rb_gc_mark(rb_RJITCompiler);
349 rb_gc_mark(rb_cRJITIseqPtr);
350 rb_gc_mark(rb_cRJITCfpPtr);
351 rb_gc_mark(rb_mRJITHooks);
352 rb_gc_mark(rb_rjit_raw_samples);
353 rb_gc_mark(rb_rjit_line_samples);
355 RUBY_MARK_LEAVE(
"rjit");
377 WITH_RJIT_ISOLATED_USING_PC(
true, {
380 rb_funcall(rb_RJITCompiler, rb_intern(
"compile"), 2, iseq_ptr, cfp_ptr);
387rb_rjit_entry_stub_hit(
VALUE branch_stub)
396 WITH_RJIT_ISOLATED_USING_PC(
true, {
398 result =
rb_funcall(rb_RJITCompiler, rb_intern(
"entry_stub_hit"), 2, branch_stub, cfp_ptr);
407rb_rjit_branch_stub_hit(
VALUE branch_stub,
int sp_offset,
int target0_p)
415 cfp->sp += sp_offset;
419 result =
rb_funcall(rb_RJITCompiler, rb_intern(
"branch_stub_hit"), 3, branch_stub, cfp_ptr, RBOOL(target0_p));
422 cfp->sp -= sp_offset;
430rb_rjit_init(
const struct rb_rjit_options *opts)
432 VM_ASSERT(rb_rjit_enabled);
435 rb_rjit_opts = *opts;
436 if (rb_rjit_opts.exec_mem_size == 0)
437 rb_rjit_opts.exec_mem_size = DEFAULT_EXEC_MEM_SIZE;
438 if (rb_rjit_opts.call_threshold == 0)
439 rb_rjit_opts.call_threshold = DEFAULT_CALL_THRESHOLD;
440#ifndef HAVE_LIBCAPSTONE
441 if (rb_rjit_opts.dump_disasm)
442 rb_warn(
"libcapstone has not been linked. Ignoring --rjit-dump-disasm.");
448 rb_warn(
"Disabling RJIT because RubyVM::RJIT::Compiler is not defined");
449 rb_rjit_enabled =
false;
453 if (rjit_iseq_update_references_pjob == POSTPONED_JOB_HANDLE_INVALID) {
454 rb_bug(
"Could not preregister postponed job for RJIT");
458 rb_RJITCompiler =
rb_funcall(rb_cRJITCompiler, rb_intern(
"new"), 0);
459 rb_cRJITIseqPtr =
rb_funcall(rb_mRJITC, rb_intern(
"rb_iseq_t"), 0);
460 rb_cRJITCfpPtr =
rb_funcall(rb_mRJITC, rb_intern(
"rb_control_frame_t"), 0);
461 rb_mRJITHooks =
rb_const_get(rb_mRJIT, rb_intern(
"Hooks"));
462 if (rb_rjit_opts.trace_exits) {
463 rb_rjit_raw_samples = rb_ary_new();
464 rb_rjit_line_samples = rb_ary_new();
468 rb_rjit_call_p = !rb_rjit_opts.disable;
469 rjit_stats_p = rb_rjit_opts.stats;
480 return RBOOL(rb_rjit_stats_enabled);
487 return RBOOL(rb_rjit_trace_exits_enabled);
494 rb_rjit_call_p =
false;
495 rjit_stats_p =
false;
unsigned int rb_postponed_job_handle_t
The type of a handle returned from rb_postponed_job_preregister and passed to rb_postponed_job_trigge...
void rb_postponed_job_trigger(rb_postponed_job_handle_t h)
Triggers a pre-registered job registered with rb_postponed_job_preregister, scheduling it for executi...
rb_postponed_job_handle_t rb_postponed_job_preregister(unsigned int flags, rb_postponed_job_func_t func, void *data)
Pre-registers a func in Ruby's postponed job preregistration table, returning an opaque handle which ...
void(* rb_postponed_job_func_t)(void *arg)
Type of postponed jobs.
uint32_t rb_event_flag_t
Represents event(s).
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define NUM2SIZET
Old name of RB_NUM2SIZE.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.