Ruby 4.1.0dev (2026-11-18 revision 9de06831e37534f8a163e598cc0c6903c7687430)
zjit.h (9de06831e37534f8a163e598cc0c6903c7687430)
1#ifndef ZJIT_H
2#define ZJIT_H 1
3//
4// This file contains definitions ZJIT exposes to the CRuby codebase
5//
6
7#include "shape.h" // for shape_id_t
8
9// ZJIT_STATS controls whether to support runtime counters in the interpreter
10#ifndef ZJIT_STATS
11# define ZJIT_STATS (USE_ZJIT && RUBY_DEBUG)
12#endif
13
14// JITFrame is defined here as the single source of truth and imported into
15// Rust via bindgen. C code reads fields directly; Rust uses an impl block.
16typedef struct zjit_jit_frame {
17 // Program counter for this frame, used for backtraces and GC.
18 // NULL for C frames (they don't have a Ruby PC).
19 const VALUE *pc;
20 // The ISEQ this frame belongs to. Marked via rb_execution_context_mark_ctx.
21 // NULL for C frames.
22 const rb_iseq_t *iseq;
23 // Stack map bit stream, decoded by zjit_materialize_frames(). Bit 0 is
24 // materialize_block_code, see ZJIT_FRAME_MATERIALIZE_BLOCK_CODE_P().
25 // The rest are ZJIT_STACK_MAP_OP_* opcodes, each followed by its operands.
26 // NULL for C frames.
27 const uint8_t *stack_map;
28} zjit_jit_frame_t;
29
30// Whether to materialize block_code when this frame is materialized.
31// True when the ISEQ doesn't contain send/invokesuper/invokeblock
32// (which write block_code themselves), so we must restore it.
33// Always false for C frames.
34static inline bool
35ZJIT_FRAME_MATERIALIZE_BLOCK_CODE_P(const zjit_jit_frame_t *jit_frame)
36{
37 // The 0th bit of the bitstream is this boolean.
38 return jit_frame->stack_map && (jit_frame->stack_map[0] & 1);
39}
40
41#if USE_ZJIT
42// JITFrame stack map bit stream format. See StackMapWriter in zjit/src/jit_frame.rs.
43#define ZJIT_STACK_MAP_VARINT_BITS 4
44#define ZJIT_STACK_MAP_OP_BITS 3
45// Opcodes in the JITFrame stack map bit stream format
46#define ZJIT_STACK_MAP_OP_END 0
47#define ZJIT_STACK_MAP_OP_VREG 1
48#define ZJIT_STACK_MAP_OP_SKIP 2
49#define ZJIT_STACK_MAP_OP_BASE_PTR 3
50#define ZJIT_STACK_MAP_OP_IMM 4
51#define ZJIT_STACK_MAP_OP_NIL 5
52#define ZJIT_STACK_MAP_OP_SKIP_VM_ENV_DATA 6
53#define ZJIT_STACK_MAP_OP_PREV_FRAME 7
54
55extern void *rb_zjit_entry;
56extern bool rb_zjit_compiling_p;
57extern const zjit_jit_frame_t rb_zjit_c_frame;
58extern unsigned int rb_zjit_call_threshold;
59extern unsigned int rb_zjit_profile_threshold;
60extern uint64_t rb_zjit_max_compile_time_ns;
61extern uint64_t rb_zjit_total_compile_time_ns;
62void rb_zjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit_exception);
63void rb_zjit_profile_insn(uint32_t insn, rb_execution_context_t *ec);
64void rb_zjit_profile_enable(const rb_iseq_t *iseq);
65void rb_zjit_bop_redefined(int redefined_flag, enum ruby_basic_operators bop);
66void rb_zjit_cme_invalidate(const rb_callable_method_entry_t *cme);
67void rb_zjit_cme_free(const rb_callable_method_entry_t *cme);
68void rb_zjit_klass_free(VALUE klass);
69void rb_zjit_invalidate_no_ep_escape(const rb_iseq_t *iseq);
70void rb_zjit_constant_state_changed(ID id);
71void rb_zjit_iseq_mark(void *payload);
72void rb_zjit_iseq_update_references(void *payload);
73void rb_zjit_mark_all_writable(void);
74void rb_zjit_mark_all_executable(void);
75void rb_zjit_iseq_free(const rb_iseq_t *iseq);
76void rb_zjit_invalidate_single_ractor(void);
77void rb_zjit_tracing_invalidate_all(void);
78void rb_zjit_invalidate_newobj_hook(void);
79void rb_zjit_invalidate_no_singleton_class(VALUE klass);
80void rb_zjit_invalidate_root_box(void);
81void rb_zjit_jit_frame_update_references(zjit_jit_frame_t *jit_frame);
82void rb_zjit_materialize_frames(const rb_execution_context_t *ec, rb_control_frame_t *cfp);
83void rb_zjit_materialize_frames_for_longjmp(const rb_execution_context_t *ec, rb_control_frame_t *cfp);
84size_t rb_zjit_hash_new_size(VALUE *flags_out, size_t size);
85VALUE rb_zjit_new_obj_shape(VALUE flags, size_t alloc_size);
86bool rb_zjit_class_allocate_instance_fastpath(VALUE klass, size_t *size_out, VALUE *flags_out);
87bool rb_zjit_str_resurrect_fastpath(VALUE str, bool chilled, size_t *size_out, VALUE *flags_out, rb_len_t *len_out, size_t *byte_size_out);
88bool rb_zjit_array_dup_can_fastpath(VALUE ary, size_t *alloc_size_out, VALUE *flags_out, rb_len_t *len_out);
89bool rb_zjit_array_new_can_fastpath(rb_len_t len, size_t *alloc_size_out, VALUE *flags_out);
90bool rb_zjit_hash_dup_can_fastpath(VALUE hash, size_t *alloc_size_out, VALUE *flags_out, VALUE *ifnone_out, rb_len_t *bound_out);
91void rb_zjit_range_new_fastpath(bool exclude_end, size_t *alloc_size_out, VALUE *flags_out);
92void rb_zjit_array_new_fastpath(size_t *alloc_size_out, VALUE *flags_out);
93bool rb_zjit_newobj_hook_enabled_p(void);
94
95// Special value for cfp->jit_return that means "this is a C method frame, use
96// rb_zjit_c_frame as the JITFrame". We don't control the native stack layout
97// for C frames, so there's no per-call JITFrame storage; we set this sentinel
98// instead of a heap-allocated JITFrame pointer.
99#define ZJIT_JIT_RETURN_C_FRAME 0x1
100
101static inline const zjit_jit_frame_t *
102CFP_ZJIT_FRAME(const rb_control_frame_t *cfp)
103{
104 if ((VALUE)cfp->jit_return == ZJIT_JIT_RETURN_C_FRAME) {
105 return &rb_zjit_c_frame;
106 }
107 else {
108 // Read JITFrame from this frame's stack slot. cfp->jit_return points at
109 // the slot reserved for this frame's inlining depth, so distinct frames in
110 // the same JIT function read distinct slots. An initial frame describing
111 // the entry PC + iseq is written by gen_entry_point() for the top-level
112 // frame and by gen_push_inline_frame() for inlined frames. That entry
113 // PC is correct only at the frame's start; because the PC this frame reports
114 // must track where execution currently is, later gen_save_pc_for_gc() calls
115 // rewrite the slot with the live PC as execution advances through the frame,
116 // before any non-leaf C call.
117 return (const zjit_jit_frame_t *)((VALUE *)cfp->jit_return)[-1];
118 }
119}
120#else
121#define rb_zjit_entry 0
122#define rb_zjit_compiling_p false
123static inline void rb_zjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit_exception) {}
124static inline void rb_zjit_profile_insn(uint32_t insn, rb_execution_context_t *ec) {}
125static inline void rb_zjit_profile_enable(const rb_iseq_t *iseq) {}
126static inline void rb_zjit_bop_redefined(int redefined_flag, enum ruby_basic_operators bop) {}
127static inline void rb_zjit_cme_invalidate(const rb_callable_method_entry_t *cme) {}
128static inline void rb_zjit_invalidate_no_ep_escape(const rb_iseq_t *iseq) {}
129static inline void rb_zjit_constant_state_changed(ID id) {}
130static inline void rb_zjit_invalidate_single_ractor(void) {}
131static inline void rb_zjit_tracing_invalidate_all(void) {}
132static inline void rb_zjit_invalidate_newobj_hook(void) {}
133static inline void rb_zjit_invalidate_no_singleton_class(VALUE klass) {}
134static inline void rb_zjit_invalidate_root_box(void) {}
135static inline void rb_zjit_jit_frame_update_references(zjit_jit_frame_t *jit_frame) {}
136static inline void rb_zjit_materialize_frames(const rb_execution_context_t *ec, rb_control_frame_t *cfp) {}
137static inline void rb_zjit_materialize_frames_for_longjmp(const rb_execution_context_t *ec, rb_control_frame_t *cfp) {}
138static inline const zjit_jit_frame_t *CFP_ZJIT_FRAME(const rb_control_frame_t *cfp) { return NULL; }
139#endif // #if USE_ZJIT
140
141#define rb_zjit_enabled_p (rb_zjit_entry != 0)
142
143// Return true if a given CFP has ZJIT's JITFrame.
144static inline bool
145CFP_ZJIT_FRAME_P(const rb_control_frame_t *cfp)
146{
147 if (!rb_zjit_enabled_p) return false;
148 return cfp->jit_return != NULL;
149}
150
151static inline const VALUE*
152CFP_PC(const rb_control_frame_t *cfp)
153{
154 if (CFP_ZJIT_FRAME_P(cfp)) {
155 return CFP_ZJIT_FRAME(cfp)->pc;
156 }
157 return cfp->pc;
158}
159
160static inline const rb_iseq_t*
161CFP_ISEQ(const rb_control_frame_t *cfp)
162{
163 if (CFP_ZJIT_FRAME_P(cfp)) {
164 return CFP_ZJIT_FRAME(cfp)->iseq;
165 }
166 return cfp->_iseq;
167}
168
169#endif // #ifndef ZJIT_H
int len
Length of the buffer.
Definition io.h:8
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
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