Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)
zjit.c
1#include "internal.h"
2#include "internal/sanitizers.h"
3#include "internal/string.h"
4#include "internal/hash.h"
5#include "internal/variable.h"
6#include "internal/compile.h"
7#include "internal/class.h"
8#include "internal/fixnum.h"
9#include "internal/numeric.h"
10#include "internal/gc.h"
11#include "internal/vm.h"
12#include "yjit.h"
13#include "vm_core.h"
14#include "vm_callinfo.h"
15#include "builtin.h"
16#include "insns.inc"
17#include "insns_info.inc"
18#include "zjit.h"
19#include "vm_insnhelper.h"
20#include "probes.h"
21#include "probes_helper.h"
22#include "iseq.h"
23#include "ruby/debug.h"
24#include "internal/cont.h"
25
26// This build config impacts the pointer tagging scheme and we only want to
27// support one scheme for simplicity.
28STATIC_ASSERT(pointer_tagging_scheme, USE_FLONUM);
29
30enum zjit_struct_offsets {
31 ISEQ_BODY_OFFSET_PARAM = offsetof(struct rb_iseq_constant_body, param)
32};
33
34// For a given raw_sample (frame), set the hash with the caller's
35// name, file, and line number. Return the hash with collected frame_info.
36static void
37rb_zjit_add_frame(VALUE hash, VALUE frame)
38{
39 VALUE frame_id = PTR2NUM(frame);
40
41 if (RTEST(rb_hash_aref(hash, frame_id))) {
42 return;
43 }
44 else {
45 VALUE frame_info = rb_hash_new();
46 // Full label for the frame
48 // Absolute path of the frame from rb_iseq_realpath
50 // Line number of the frame
52
53 // If absolute path isn't available use the rb_iseq_path
54 if (NIL_P(file)) {
55 file = rb_profile_frame_path(frame);
56 }
57
58 rb_hash_aset(frame_info, ID2SYM(rb_intern("name")), name);
59 rb_hash_aset(frame_info, ID2SYM(rb_intern("file")), file);
60 rb_hash_aset(frame_info, ID2SYM(rb_intern("samples")), INT2NUM(0));
61 rb_hash_aset(frame_info, ID2SYM(rb_intern("total_samples")), INT2NUM(0));
62 rb_hash_aset(frame_info, ID2SYM(rb_intern("edges")), rb_hash_new());
63 rb_hash_aset(frame_info, ID2SYM(rb_intern("lines")), rb_hash_new());
64
65 if (line != INT2FIX(0)) {
66 rb_hash_aset(frame_info, ID2SYM(rb_intern("line")), line);
67 }
68
69 rb_hash_aset(hash, frame_id, frame_info);
70 }
71}
72
73// Parses the ZjitExitLocations raw_samples and line_samples collected by
74// rb_zjit_record_exit_stack and turns them into 3 hashes (raw, lines, and frames) to
75// be used by RubyVM::ZJIT.exit_locations. zjit_raw_samples represents the raw frames information
76// (without name, file, and line), and zjit_line_samples represents the line information
77// of the iseq caller.
79rb_zjit_exit_locations_dict(VALUE *zjit_raw_samples, int *zjit_line_samples, int samples_len)
80{
81 VALUE result = rb_hash_new();
82 VALUE raw_samples = rb_ary_new_capa(samples_len);
83 VALUE line_samples = rb_ary_new_capa(samples_len);
84 VALUE frames = rb_hash_new();
85 int idx = 0;
86
87 // While the index is less than samples_len, parse zjit_raw_samples and
88 // zjit_line_samples, then add casted values to raw_samples and line_samples array.
89 while (idx < samples_len) {
90 int num = (int)zjit_raw_samples[idx];
91 int line_num = (int)zjit_line_samples[idx];
92 idx++;
93
94 // + 1 as we append an additional sample for the insn
95 rb_ary_push(raw_samples, SIZET2NUM(num + 1));
96 rb_ary_push(line_samples, INT2NUM(line_num + 1));
97
98 // Loop through the length of samples_len and add data to the
99 // frames hash. Also push the current value onto the raw_samples
100 // and line_samples array respectively.
101 for (int o = 0; o < num; o++) {
102 rb_zjit_add_frame(frames, zjit_raw_samples[idx]);
103 rb_ary_push(raw_samples, SIZET2NUM(zjit_raw_samples[idx]));
104 rb_ary_push(line_samples, INT2NUM(zjit_line_samples[idx]));
105 idx++;
106 }
107
108 rb_ary_push(raw_samples, SIZET2NUM(zjit_raw_samples[idx]));
109 rb_ary_push(line_samples, INT2NUM(zjit_line_samples[idx]));
110 idx++;
111
112 rb_ary_push(raw_samples, SIZET2NUM(zjit_raw_samples[idx]));
113 rb_ary_push(line_samples, INT2NUM(zjit_line_samples[idx]));
114 idx++;
115 }
116
117 // Set add the raw_samples, line_samples, and frames to the results
118 // hash.
119 rb_hash_aset(result, ID2SYM(rb_intern("raw")), raw_samples);
120 rb_hash_aset(result, ID2SYM(rb_intern("lines")), line_samples);
121 rb_hash_aset(result, ID2SYM(rb_intern("frames")), frames);
122
123 return result;
124}
125
126void rb_zjit_profile_disable(const rb_iseq_t *iseq);
127
128void
129rb_zjit_compile_iseq(const rb_iseq_t *iseq, bool jit_exception)
130{
131 RB_VM_LOCKING() {
132 rb_vm_barrier();
133
134 // Compile a block version starting at the current instruction
135 uint8_t *rb_zjit_iseq_gen_entry_point(const rb_iseq_t *iseq, bool jit_exception); // defined in Rust
136 uintptr_t code_ptr = (uintptr_t)rb_zjit_iseq_gen_entry_point(iseq, jit_exception);
137
138 if (jit_exception) {
139 iseq->body->jit_exception = (rb_jit_func_t)code_ptr;
140 }
141 else {
142 iseq->body->jit_entry = (rb_jit_func_t)code_ptr;
143 }
144 }
145}
146
147extern VALUE *rb_vm_base_ptr(struct rb_control_frame_struct *cfp);
148
149bool
150rb_zjit_constcache_shareable(const struct iseq_inline_constant_cache_entry *ice)
151{
152 return (ice->flags & IMEMO_CONST_CACHE_SHAREABLE) != 0;
153}
154
155// Convert a given ISEQ's instructions to zjit_* instructions
156void
157rb_zjit_profile_enable(const rb_iseq_t *iseq)
158{
159 // This table encodes an opcode into the instruction's address
160 const void *const *insn_table = rb_vm_get_insns_address_table();
161
162 unsigned int insn_idx = 0;
163 while (insn_idx < iseq->body->iseq_size) {
164 int insn = rb_vm_insn_addr2opcode((void *)iseq->body->iseq_encoded[insn_idx]);
165 int zjit_insn = vm_bare_insn_to_zjit_insn(insn);
166 if (insn != zjit_insn) {
167 iseq->body->iseq_encoded[insn_idx] = (VALUE)insn_table[zjit_insn];
168 }
169 insn_idx += insn_len(insn);
170 }
171}
172
173// Convert a given ISEQ's ZJIT instructions to bare instructions
174void
175rb_zjit_profile_disable(const rb_iseq_t *iseq)
176{
177 // This table encodes an opcode into the instruction's address
178 const void *const *insn_table = rb_vm_get_insns_address_table();
179
180 unsigned int insn_idx = 0;
181 while (insn_idx < iseq->body->iseq_size) {
182 int insn = rb_vm_insn_addr2opcode((void *)iseq->body->iseq_encoded[insn_idx]);
183 int bare_insn = vm_zjit_insn_to_bare_insn(insn);
184 if (insn != bare_insn) {
185 iseq->body->iseq_encoded[insn_idx] = (VALUE)insn_table[bare_insn];
186 }
187 insn_idx += insn_len(insn);
188 }
189}
190
191// Update a YARV instruction to a given opcode (to disable ZJIT profiling).
192void
193rb_zjit_iseq_insn_set(const rb_iseq_t *iseq, unsigned int insn_idx, enum ruby_vminsn_type bare_insn)
194{
195#if RUBY_DEBUG
196 int insn = rb_vm_insn_addr2opcode((void *)iseq->body->iseq_encoded[insn_idx]);
197 RUBY_ASSERT(vm_zjit_insn_to_bare_insn(insn) == (int)bare_insn);
198#endif
199 const void *const *insn_table = rb_vm_get_insns_address_table();
200 iseq->body->iseq_encoded[insn_idx] = (VALUE)insn_table[bare_insn];
201}
202
203// Get profiling information for ISEQ
204void *
205rb_iseq_get_zjit_payload(const rb_iseq_t *iseq)
206{
207 RUBY_ASSERT_ALWAYS(IMEMO_TYPE_P(iseq, imemo_iseq));
208 if (iseq->body) {
209 return iseq->body->zjit_payload;
210 }
211 else {
212 // Body is NULL when constructing the iseq.
213 return NULL;
214 }
215}
216
217// Set profiling information for ISEQ
218void
219rb_iseq_set_zjit_payload(const rb_iseq_t *iseq, void *payload)
220{
221 RUBY_ASSERT_ALWAYS(IMEMO_TYPE_P(iseq, imemo_iseq));
222 RUBY_ASSERT_ALWAYS(iseq->body);
223 RUBY_ASSERT_ALWAYS(NULL == iseq->body->zjit_payload);
224 iseq->body->zjit_payload = payload;
225}
226
227void
228rb_zjit_print_exception(void)
229{
230 VALUE exception = rb_errinfo();
231 rb_set_errinfo(Qnil);
232 assert(RTEST(exception));
233 rb_warn("Ruby error: %"PRIsVALUE"", rb_funcall(exception, rb_intern("full_message"), 0));
234}
235
236bool
237rb_zjit_singleton_class_p(VALUE klass)
238{
239 return RCLASS_SINGLETON_P(klass);
240}
241
242VALUE
243rb_zjit_defined_ivar(VALUE obj, ID id, VALUE pushval)
244{
245 VALUE result = rb_ivar_defined(obj, id);
246 return result ? pushval : Qnil;
247}
248
249bool
250rb_zjit_method_tracing_currently_enabled(void)
251{
252 rb_event_flag_t tracing_events;
253 if (rb_multi_ractor_p()) {
254 tracing_events = ruby_vm_event_enabled_global_flags;
255 }
256 else {
257 // At the time of writing, events are never removed from
258 // ruby_vm_event_enabled_global_flags so always checking using it would
259 // mean we don't compile even after tracing is disabled.
260 tracing_events = rb_ec_ractor_hooks(GET_EC())->events;
261 }
262
263 return tracing_events & (RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN);
264}
265
266bool
267rb_zjit_insn_leaf(int insn, const VALUE *opes)
268{
269 return insn_leaf(insn, opes);
270}
271
272ID
273rb_zjit_local_id(const rb_iseq_t *iseq, unsigned idx)
274{
275 return ISEQ_BODY(iseq)->local_table[idx];
276}
277
278bool rb_zjit_cme_is_cfunc(const rb_callable_method_entry_t *me, const void *func);
279
281rb_zjit_vm_search_method(VALUE cd_owner, struct rb_call_data *cd, VALUE recv);
282
283bool
284rb_zjit_class_initialized_p(VALUE klass)
285{
286 return RCLASS_INITIALIZED_P(klass);
287}
288
289rb_alloc_func_t rb_zjit_class_get_alloc_func(VALUE klass);
290
291VALUE rb_class_allocate_instance(VALUE klass);
292
293bool
294rb_zjit_class_has_default_allocator(VALUE klass)
295{
296 assert(RCLASS_INITIALIZED_P(klass));
297 assert(!RCLASS_SINGLETON_P(klass));
298 rb_alloc_func_t alloc = rb_zjit_class_get_alloc_func(klass);
299 return alloc == rb_class_allocate_instance;
300}
301
302
303VALUE rb_vm_get_untagged_block_handler(rb_control_frame_t *reg_cfp);
304
305void
306rb_zjit_writebarrier_check_immediate(VALUE recv, VALUE val)
307{
308 if (!RB_SPECIAL_CONST_P(val)) {
309 rb_gc_writebarrier(recv, val);
310 }
311}
312
313// Primitives used by zjit.rb. Don't put other functions below, which wouldn't use them.
314VALUE rb_zjit_enable(rb_execution_context_t *ec, VALUE self);
315VALUE rb_zjit_assert_compiles(rb_execution_context_t *ec, VALUE self);
316VALUE rb_zjit_stats(rb_execution_context_t *ec, VALUE self, VALUE target_key);
317VALUE rb_zjit_reset_stats_bang(rb_execution_context_t *ec, VALUE self);
318VALUE rb_zjit_stats_enabled_p(rb_execution_context_t *ec, VALUE self);
319VALUE rb_zjit_print_stats_p(rb_execution_context_t *ec, VALUE self);
320VALUE rb_zjit_get_stats_file_path_p(rb_execution_context_t *ec, VALUE self);
321VALUE rb_zjit_trace_exit_locations_enabled_p(rb_execution_context_t *ec, VALUE self);
322VALUE rb_zjit_get_exit_locations(rb_execution_context_t *ec, VALUE self);
323
324// Preprocessed zjit.rb generated during build
325#include "zjit.rbinc"
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
Definition assert.h:199
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
VALUE rb_profile_frame_full_label(VALUE frame)
Identical to rb_profile_frame_label(), except it returns a qualified result.
VALUE rb_profile_frame_absolute_path(VALUE frame)
Identical to rb_profile_frame_path(), except it tries to expand the returning path.
VALUE rb_profile_frame_path(VALUE frame)
Queries the path of the passed backtrace.
VALUE rb_profile_frame_first_lineno(VALUE frame)
Queries the first line of the method of the passed frame pointer.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
Definition event.h:43
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition event.h:44
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define NIL_P
Old name of RB_NIL_P.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
VALUE rb_errinfo(void)
This is the same as $! in Ruby.
Definition eval.c:2045
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition hash.c:1464
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
Definition variable.c:2109
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
Definition vm.h:219
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
#define USE_FLONUM
Definition vm_core.h:261
Definition method.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40