Ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e0ba6b95ab71a441357ed5484e33498)
mjit.h
1#ifndef RUBY_MJIT_H
2#define RUBY_MJIT_H 1
3/**********************************************************************
4
5 mjit.h - Interface to MRI method JIT compiler for Ruby's main thread
6
7 Copyright (C) 2017 Vladimir Makarov <vmakarov@redhat.com>.
8
9**********************************************************************/
10
11#include "ruby/internal/config.h" // defines USE_MJIT
13#include "vm_core.h"
14
15# if USE_MJIT
16
17#include "debug_counter.h"
18#include "ruby.h"
19#include "vm_core.h"
20#include "yjit.h"
21
22// Special address values of a function generated from the
23// corresponding iseq by MJIT:
24enum rb_mjit_iseq_func {
25 // ISEQ has never been enqueued to unit_queue yet
26 NOT_ADDED_JIT_ISEQ_FUNC = 0,
27 // ISEQ is already queued for the machine code generation but the
28 // code is not ready yet for the execution
29 NOT_READY_JIT_ISEQ_FUNC = 1,
30 // ISEQ included not compilable insn, some internal assertion failed
31 // or the unit is unloaded
32 NOT_COMPILED_JIT_ISEQ_FUNC = 2,
33 // End mark
34 LAST_JIT_ISEQ_FUNC = 3
35};
36
37// MJIT options which can be defined on the MRI command line.
38struct mjit_options {
39 // Converted from "jit" feature flag to tell the enablement
40 // information to ruby_show_version().
41 char on;
42 // Save temporary files after MRI finish. The temporary files
43 // include the pre-compiled header, C code file generated for ISEQ,
44 // and the corresponding object file.
45 char save_temps;
46 // Print MJIT warnings to stderr.
47 char warnings;
48 // Disable compiler optimization and add debug symbols. It can be
49 // very slow.
50 char debug;
51 // Add arbitrary cflags.
52 char* debug_flags;
53 // If not 0, all ISeqs are synchronously compiled. For testing.
54 unsigned int wait;
55 // Number of calls to trigger JIT compilation. For testing.
56 unsigned int min_calls;
57 // Force printing info about MJIT work of level VERBOSE or
58 // less. 0=silence, 1=medium, 2=verbose.
59 int verbose;
60 // Maximal permitted number of iseq JIT codes in a MJIT memory
61 // cache.
62 int max_cache_size;
63};
64
65// State of optimization switches
67 // Disable getinstancevariable/setinstancevariable optimizations based on inline cache (T_OBJECT)
68 bool disable_ivar_cache;
69 // Disable getinstancevariable/setinstancevariable optimizations based on inline cache (FL_EXIVAR)
70 bool disable_exivar_cache;
71 // Disable send/opt_send_without_block optimizations based on inline cache
72 bool disable_send_cache;
73 // Disable method inlining
74 bool disable_inlining;
75 // Disable opt_getinlinecache inlining
76 bool disable_const_cache;
77};
78
79typedef VALUE (*mjit_func_t)(rb_execution_context_t *, rb_control_frame_t *);
80
81RUBY_SYMBOL_EXPORT_BEGIN
82RUBY_EXTERN struct mjit_options mjit_opts;
83RUBY_EXTERN bool mjit_call_p;
84
85extern void rb_mjit_add_iseq_to_process(const rb_iseq_t *iseq);
86extern VALUE rb_mjit_wait_call(rb_execution_context_t *ec, struct rb_iseq_constant_body *body);
87extern struct rb_mjit_compile_info* rb_mjit_iseq_compile_info(const struct rb_iseq_constant_body *body);
88extern void rb_mjit_recompile_send(const rb_iseq_t *iseq);
89extern void rb_mjit_recompile_ivar(const rb_iseq_t *iseq);
90extern void rb_mjit_recompile_exivar(const rb_iseq_t *iseq);
91extern void rb_mjit_recompile_inlining(const rb_iseq_t *iseq);
92extern void rb_mjit_recompile_const(const rb_iseq_t *iseq);
93RUBY_SYMBOL_EXPORT_END
94
95extern void mjit_cancel_all(const char *reason);
96extern bool mjit_compile(FILE *f, const rb_iseq_t *iseq, const char *funcname, int id);
97extern void mjit_init(const struct mjit_options *opts);
98extern void mjit_gc_start_hook(void);
99extern void mjit_gc_exit_hook(void);
100extern void mjit_free_iseq(const rb_iseq_t *iseq);
101extern void mjit_update_references(const rb_iseq_t *iseq);
102extern void mjit_mark(void);
103extern struct mjit_cont *mjit_cont_new(rb_execution_context_t *ec);
104extern void mjit_cont_free(struct mjit_cont *cont);
105extern void mjit_mark_cc_entries(const struct rb_iseq_constant_body *const body);
106
107# ifdef MJIT_HEADER
108NOINLINE(static COLDFUNC VALUE mjit_exec_slowpath(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body));
109# else
110static inline VALUE mjit_exec_slowpath(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body);
111# endif
112static VALUE
113mjit_exec_slowpath(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body)
114{
115 uintptr_t func_i = (uintptr_t)(body->jit_func);
116 ASSUME(func_i <= LAST_JIT_ISEQ_FUNC);
117 switch ((enum rb_mjit_iseq_func)func_i) {
118 case NOT_ADDED_JIT_ISEQ_FUNC:
119 RB_DEBUG_COUNTER_INC(mjit_exec_not_added);
120 if (body->total_calls == mjit_opts.min_calls) {
121 rb_mjit_add_iseq_to_process(iseq);
122 if (UNLIKELY(mjit_opts.wait)) {
123 return rb_mjit_wait_call(ec, body);
124 }
125 }
126 break;
127 case NOT_READY_JIT_ISEQ_FUNC:
128 RB_DEBUG_COUNTER_INC(mjit_exec_not_ready);
129 break;
130 case NOT_COMPILED_JIT_ISEQ_FUNC:
131 RB_DEBUG_COUNTER_INC(mjit_exec_not_compiled);
132 break;
133 default: // to avoid warning with LAST_JIT_ISEQ_FUNC
134 break;
135 }
136 return Qundef;
137}
138
139// Try to execute the current iseq in ec. Use JIT code if it is ready.
140// If it is not, add ISEQ to the compilation queue and return Qundef for MJIT.
141// YJIT compiles on the thread running the iseq.
142static inline VALUE
143mjit_exec(rb_execution_context_t *ec)
144{
145 const rb_iseq_t *iseq = ec->cfp->iseq;
146 struct rb_iseq_constant_body *body = iseq->body;
147 bool yjit_enabled = false;
148#ifndef MJIT_HEADER
149 // Don't want to compile with YJIT or use code generated by YJIT
150 // when running inside code generated by MJIT.
151 yjit_enabled = rb_yjit_enabled_p();
152#endif
153
154 if (mjit_call_p || yjit_enabled) {
155 body->total_calls++;
156 }
157
158#ifndef MJIT_HEADER
159 if (yjit_enabled && !mjit_call_p && body->total_calls == rb_yjit_call_threshold()) {
160 // If we couldn't generate any code for this iseq, then return
161 // Qundef so the interpreter will handle the call.
162 if (!rb_yjit_compile_iseq(iseq, ec)) {
163 return Qundef;
164 }
165 }
166#endif
167
168 if (!(mjit_call_p || yjit_enabled))
169 return Qundef;
170
171 RB_DEBUG_COUNTER_INC(mjit_exec);
172
173 mjit_func_t func = body->jit_func;
174
175 // YJIT tried compiling this function once before and couldn't do
176 // it, so return Qundef so the interpreter handles it.
177 if (yjit_enabled && func == 0) {
178 return Qundef;
179 }
180
181 if (UNLIKELY((uintptr_t)func <= LAST_JIT_ISEQ_FUNC)) {
182# ifdef MJIT_HEADER
183 RB_DEBUG_COUNTER_INC(mjit_frame_JT2VM);
184# else
185 RB_DEBUG_COUNTER_INC(mjit_frame_VM2VM);
186# endif
187 return mjit_exec_slowpath(ec, iseq, body);
188 }
189
190# ifdef MJIT_HEADER
191 RB_DEBUG_COUNTER_INC(mjit_frame_JT2JT);
192# else
193 RB_DEBUG_COUNTER_INC(mjit_frame_VM2JT);
194# endif
195 RB_DEBUG_COUNTER_INC(mjit_exec_call_func);
196 // Under SystemV x64 calling convention
197 // ec -> RDI
198 // cfp -> RSI
199 return func(ec, ec->cfp);
200}
201
202void mjit_child_after_fork(void);
203
204# ifdef MJIT_HEADER
205#define mjit_enabled true
206# else // MJIT_HEADER
207extern bool mjit_enabled;
208# endif // MJIT_HEADER
209VALUE mjit_pause(bool wait_p);
210VALUE mjit_resume(void);
211void mjit_finish(bool close_handle_p);
212
213# else // USE_MJIT
214
215static inline void mjit_cancel_all(const char *reason){}
216static inline struct mjit_cont *mjit_cont_new(rb_execution_context_t *ec){return NULL;}
217static inline void mjit_cont_free(struct mjit_cont *cont){}
218static inline void mjit_gc_start_hook(void){}
219static inline void mjit_gc_exit_hook(void){}
220static inline void mjit_free_iseq(const rb_iseq_t *iseq){}
221static inline void mjit_mark(void){}
222static inline VALUE mjit_exec(rb_execution_context_t *ec) { return Qundef; /* unreachable */ }
223static inline void mjit_child_after_fork(void){}
224
225#define mjit_enabled false
226static inline VALUE mjit_pause(bool wait_p){ return Qnil; } // unreachable
227static inline VALUE mjit_resume(void){ return Qnil; } // unreachable
228static inline void mjit_finish(bool close_handle_p){}
229
230# endif // USE_MJIT
231#endif // RUBY_MJIT_H
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition: dllexport.h:47
#define Qundef
Old name of RUBY_Qundef.
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition: assume.h:29
#define Qnil
Old name of RUBY_Qnil.
C99 shim for <stdbool.h>
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40