[ruby/win32ole] Undefine allocator of WIN32OLE_VARIABLE to get rid of warning
[ruby-80x24.org.git] / mjit.h
blob4e91decb011b027918b2b418e12056f8b95e6af8
1 #ifndef RUBY_MJIT_H
2 #define RUBY_MJIT_H 1
3 /**********************************************************************
5 mjit.h - Interface to MRI method JIT compiler for Ruby's main thread
7 Copyright (C) 2017 Vladimir Makarov <vmakarov@redhat.com>.
9 **********************************************************************/
11 #include "ruby/internal/config.h" // defines USE_MJIT
12 #include "ruby/internal/stdbool.h"
13 #include "vm_core.h"
15 # if USE_MJIT
17 #include "debug_counter.h"
18 #include "ruby.h"
19 #include "vm_core.h"
20 #include "yjit.h"
22 // Special address values of a function generated from the
23 // corresponding iseq by MJIT:
24 enum 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
37 // MJIT options which can be defined on the MRI command line.
38 struct 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;
65 // State of optimization switches
66 struct rb_mjit_compile_info {
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;
79 typedef VALUE (*mjit_func_t)(rb_execution_context_t *, rb_control_frame_t *);
81 RUBY_SYMBOL_EXPORT_BEGIN
82 RUBY_EXTERN struct mjit_options mjit_opts;
83 RUBY_EXTERN bool mjit_call_p;
85 extern void rb_mjit_add_iseq_to_process(const rb_iseq_t *iseq);
86 extern VALUE rb_mjit_wait_call(rb_execution_context_t *ec, struct rb_iseq_constant_body *body);
87 extern struct rb_mjit_compile_info* rb_mjit_iseq_compile_info(const struct rb_iseq_constant_body *body);
88 extern void rb_mjit_recompile_send(const rb_iseq_t *iseq);
89 extern void rb_mjit_recompile_ivar(const rb_iseq_t *iseq);
90 extern void rb_mjit_recompile_exivar(const rb_iseq_t *iseq);
91 extern void rb_mjit_recompile_inlining(const rb_iseq_t *iseq);
92 extern void rb_mjit_recompile_const(const rb_iseq_t *iseq);
93 RUBY_SYMBOL_EXPORT_END
95 extern void mjit_cancel_all(const char *reason);
96 extern bool mjit_compile(FILE *f, const rb_iseq_t *iseq, const char *funcname, int id);
97 extern void mjit_init(const struct mjit_options *opts);
98 extern void mjit_gc_start_hook(void);
99 extern void mjit_gc_exit_hook(void);
100 extern void mjit_free_iseq(const rb_iseq_t *iseq);
101 extern void mjit_update_references(const rb_iseq_t *iseq);
102 extern void mjit_mark(void);
103 extern struct mjit_cont *mjit_cont_new(rb_execution_context_t *ec);
104 extern void mjit_cont_free(struct mjit_cont *cont);
105 extern void mjit_mark_cc_entries(const struct rb_iseq_constant_body *const body);
107 # ifdef MJIT_HEADER
108 NOINLINE(static COLDFUNC VALUE mjit_exec_slowpath(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body));
109 # else
110 static inline VALUE mjit_exec_slowpath(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body);
111 # endif
112 static VALUE
113 mjit_exec_slowpath(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_iseq_constant_body *body)
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);
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;
136 return Qundef;
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.
142 static inline VALUE
143 mjit_exec(rb_execution_context_t *ec)
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
154 if (mjit_call_p || yjit_enabled) {
155 body->total_calls++;
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;
166 #endif
168 if (!(mjit_call_p || yjit_enabled))
169 return Qundef;
171 RB_DEBUG_COUNTER_INC(mjit_exec);
173 mjit_func_t func = body->jit_func;
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;
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);
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);
202 void mjit_child_after_fork(void);
204 # ifdef MJIT_HEADER
205 #define mjit_enabled true
206 # else // MJIT_HEADER
207 extern bool mjit_enabled;
208 # endif // MJIT_HEADER
209 VALUE mjit_pause(bool wait_p);
210 VALUE mjit_resume(void);
211 void mjit_finish(bool close_handle_p);
213 # else // USE_MJIT
215 static inline void mjit_cancel_all(const char *reason){}
216 static inline struct mjit_cont *mjit_cont_new(rb_execution_context_t *ec){return NULL;}
217 static inline void mjit_cont_free(struct mjit_cont *cont){}
218 static inline void mjit_gc_start_hook(void){}
219 static inline void mjit_gc_exit_hook(void){}
220 static inline void mjit_free_iseq(const rb_iseq_t *iseq){}
221 static inline void mjit_mark(void){}
222 static inline VALUE mjit_exec(rb_execution_context_t *ec) { return Qundef; /* unreachable */ }
223 static inline void mjit_child_after_fork(void){}
225 #define mjit_enabled false
226 static inline VALUE mjit_pause(bool wait_p){ return Qnil; } // unreachable
227 static inline VALUE mjit_resume(void){ return Qnil; } // unreachable
228 static inline void mjit_finish(bool close_handle_p){}
230 # endif // USE_MJIT
231 #endif // RUBY_MJIT_H