Ruby: mjit.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

mjit.h
1#ifndef RUBY_MJIT_H
2#define RUBY_MJIT_H 1
3/**********************************************************************
4
5 mjit.h - Interface to MRI method JIT compiler
6
7 Copyright (C) 2017 Vladimir Makarov <vmakarov@redhat.com>.
8 Copyright (C) 2017 Takashi Kokubun <k0kubun@ruby-lang.org>.
9
10**********************************************************************/
11
12#include "ruby/internal/config.h" // defines USE_MJIT
13#include "ruby/internal/stdbool.h"
14#include "vm_core.h"
15
16# if USE_MJIT
17
18#include "ruby.h"
19#include "vm_core.h"
20
21// Special address values of a function generated from the
22// corresponding iseq by MJIT:
23enum rb_mjit_func_state {
24 // ISEQ has not been compiled yet
25 MJIT_FUNC_NOT_COMPILED = 0,
26 // ISEQ is already queued for the machine code generation but the
27 // code is not ready yet for the execution
28 MJIT_FUNC_COMPILING = 1,
29 // ISEQ included not compilable insn, some internal assertion failed
30 // or the unit is unloaded
31 MJIT_FUNC_FAILED = 2,
32};
33// Return true if jit_func is part of enum rb_mjit_func_state
34#define MJIT_FUNC_STATE_P(jit_func) ((uintptr_t)(jit_func) <= (uintptr_t)MJIT_FUNC_FAILED)
35
36// MJIT options which can be defined on the MRI command line.
37struct mjit_options {
38 // Converted from "jit" feature flag to tell the enablement
39 // information to ruby_show_version().
40 bool on;
41 // Save temporary files after MRI finish. The temporary files
42 // include the pre-compiled header, C code file generated for ISEQ,
43 // and the corresponding object file.
44 bool save_temps;
45 // Print MJIT warnings to stderr.
46 bool warnings;
47 // Disable compiler optimization and add debug symbols. It can be
48 // very slow.
49 bool debug;
50 // Add arbitrary cflags.
51 char* debug_flags;
52 // If true, all ISeqs are synchronously compiled. For testing.
53 bool wait;
54 // Number of calls to trigger JIT compilation. For testing.
55 unsigned int call_threshold;
56 // Force printing info about MJIT work of level VERBOSE or
57 // less. 0=silence, 1=medium, 2=verbose.
58 int verbose;
59 // Maximal permitted number of iseq JIT codes in a MJIT memory
60 // cache.
61 int max_cache_size;
62 // [experimental] Do not start MJIT until MJIT.resume is called.
63 bool pause;
64 // [experimental] Call custom RubyVM::MJIT.compile instead of MJIT.
65 bool custom;
66};
67
68// State of optimization switches
69struct rb_mjit_compile_info {
70 // Disable getinstancevariable/setinstancevariable optimizations based on inline cache (T_OBJECT)
71 bool disable_ivar_cache;
72 // Disable getinstancevariable/setinstancevariable optimizations based on inline cache (FL_EXIVAR)
73 bool disable_exivar_cache;
74 // Disable send/opt_send_without_block optimizations based on inline cache
75 bool disable_send_cache;
76 // Disable method inlining
77 bool disable_inlining;
78 // Disable opt_getinlinecache inlining
79 bool disable_const_cache;
80};
81
82typedef VALUE (*jit_func_t)(rb_execution_context_t *, rb_control_frame_t *);
83
84RUBY_SYMBOL_EXPORT_BEGIN
85 RUBY_EXTERN struct mjit_options mjit_opts;
86 RUBY_EXTERN bool mjit_call_p;
87
88extern void rb_mjit_add_iseq_to_process(const rb_iseq_t *iseq);
89extern struct rb_mjit_compile_info* rb_mjit_iseq_compile_info(const struct rb_iseq_constant_body *body);
90extern void rb_mjit_recompile_send(const rb_iseq_t *iseq);
91extern void rb_mjit_recompile_ivar(const rb_iseq_t *iseq);
92extern void rb_mjit_recompile_exivar(const rb_iseq_t *iseq);
93extern void rb_mjit_recompile_inlining(const rb_iseq_t *iseq);
94extern void rb_mjit_recompile_const(const rb_iseq_t *iseq);
95RUBY_SYMBOL_EXPORT_END
96
97extern void mjit_cancel_all(const char *reason);
98extern bool mjit_compile(FILE *f, const rb_iseq_t *iseq, const char *funcname, int id);
99extern void mjit_init(const struct mjit_options *opts);
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 void mjit_mark_cc_entries(const struct rb_iseq_constant_body *const body);
104extern void mjit_notify_waitpid(int exit_code);
105
106extern void rb_mjit_bop_redefined(int redefined_flag, enum ruby_basic_operators bop);
107extern void rb_mjit_cme_invalidate(rb_callable_method_entry_t *cme);
108extern void rb_mjit_before_ractor_spawn(void);
109extern void rb_mjit_constant_state_changed(ID id);
110extern void rb_mjit_constant_ic_update(const rb_iseq_t *const iseq, IC ic, unsigned insn_idx);
111extern void rb_mjit_tracing_invalidate_all(rb_event_flag_t new_iseq_events);
112
113void mjit_child_after_fork(void);
114
115# ifdef MJIT_HEADER
116#define mjit_enabled true
117# else // MJIT_HEADER
118extern bool mjit_enabled;
119# endif // MJIT_HEADER
120 VALUE mjit_pause(bool wait_p);
121 VALUE mjit_resume(void);
122void mjit_finish(bool close_handle_p);
123
124# else // USE_MJIT
125
126static inline void mjit_cancel_all(const char *reason){}
127static inline void mjit_free_iseq(const rb_iseq_t *iseq){}
128static inline void mjit_mark(void){}
129static inline VALUE jit_exec(rb_execution_context_t *ec) { return Qundef; /* unreachable */ }
130static inline void mjit_child_after_fork(void){}
131
132static inline void rb_mjit_bop_redefined(int redefined_flag, enum ruby_basic_operators bop) {}
133static inline void rb_mjit_cme_invalidate(rb_callable_method_entry_t *cme) {}
134static inline void rb_mjit_before_ractor_spawn(void) {}
135static inline void rb_mjit_constant_state_changed(ID id) {}
136static inline void rb_mjit_constant_ic_update(const rb_iseq_t *const iseq, IC ic, unsigned insn_idx) {}
137static inline void rb_mjit_tracing_invalidate_all(rb_event_flag_t new_iseq_events) {}
138
139#define mjit_enabled false
140static inline VALUE mjit_pause(bool wait_p){ return Qnil; } // unreachable
141static inline VALUE mjit_resume(void){ return Qnil; } // unreachable
142static inline void mjit_finish(bool close_handle_p){}
143
144# endif // USE_MJIT
145#endif // RUBY_MJIT_H
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:47
rb_event_flag_t
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:103
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
stdbool.h
C99 shim for <stdbool.h>
__sFILE
Definition vsnprintf.c:160
iseq_inline_constant_cache
Definition vm_core.h:259
rb_callable_method_entry_struct
Definition method.h:62
rb_control_frame_struct
Definition vm_core.h:818
rb_execution_context_struct
Definition vm_core.h:899
rb_iseq_constant_body
Definition vm_core.h:371
rb_iseq_struct
Definition vm_core.h:526
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40