Skip to content

Commit 608fe6e

Browse files
authored
ZJIT: Handle ZJIT options properly (ruby#13197)
1 parent 10fd5a6 commit 608fe6e

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

ruby.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ usage(const char *name, int help, int highlight, int columns)
307307
#define M(shortopt, longopt, desc) RUBY_OPT_MESSAGE(shortopt, longopt, desc)
308308

309309
#if USE_YJIT
310-
# define PLATFORM_JIT_OPTION "--yjit"
310+
# define DEFAULT_JIT_OPTION "--yjit"
311+
#elif USE_ZJIT
312+
# define DEFAULT_JIT_OPTION "--zjit"
311313
#endif
312314

313315
/* This message really ought to be max 23 lines.
@@ -338,13 +340,15 @@ usage(const char *name, int help, int highlight, int columns)
338340
M("-W[level=2|:category]", "", "Set warning flag ($-W):\n"
339341
"0 for silent; 1 for moderate; 2 for verbose."),
340342
M("-x[dirpath]", "", "Execute Ruby code starting from a #!ruby line."),
341-
#if USE_YJIT
342-
M("--jit", "", "Enable JIT for the platform; same as " PLATFORM_JIT_OPTION "."),
343+
#if USE_YJIT || USE_ZJIT
344+
M("--jit", "", "Enable the default JIT for the build; same as " DEFAULT_JIT_OPTION "."),
343345
#endif
344346
#if USE_YJIT
345347
M("--yjit", "", "Enable in-process JIT compiler."),
346348
#endif
347-
M("--zjit", "", "Enable in-process JIT compiler."),
349+
#if USE_ZJIT
350+
M("--zjit", "", "Enable method-based JIT compiler."),
351+
#endif
348352
M("-h", "", "Print this help message; use --help for longer message."),
349353
};
350354
STATIC_ASSERT(usage_msg_size, numberof(usage_msg) < 26);
@@ -381,6 +385,9 @@ usage(const char *name, int help, int highlight, int columns)
381385
M("frozen-string-literal", "", "Freeze all string literals (default: disabled)."),
382386
#if USE_YJIT
383387
M("yjit", "", "In-process JIT compiler (default: disabled)."),
388+
#endif
389+
#if USE_ZJIT
390+
M("zjit", "", "Method-based JIT compiler (default: disabled)."),
384391
#endif
385392
};
386393
static const struct ruby_opt_message warn_categories[] = {
@@ -419,6 +426,11 @@ usage(const char *name, int help, int highlight, int columns)
419426
printf("%s""YJIT options:%s\n", sb, se);
420427
rb_yjit_show_usage(help, highlight, w, columns);
421428
#endif
429+
#if USE_ZJIT
430+
printf("%s""ZJIT options:%s\n", sb, se);
431+
extern void rb_zjit_show_usage(int help, int highlight, unsigned int width, int columns);
432+
rb_zjit_show_usage(help, highlight, w, columns);
433+
#endif
422434
}
423435

424436
#define rubylib_path_new rb_str_new
@@ -1993,7 +2005,7 @@ copy_str(VALUE str, rb_encoding *enc, bool intern)
19932005
return rb_enc_interned_str(RSTRING_PTR(str), RSTRING_LEN(str), enc);
19942006
}
19952007

1996-
#if USE_YJIT
2008+
#if USE_YJIT || USE_ZJIT
19972009
// Check that an environment variable is set to a truthy value
19982010
static bool
19992011
env_var_truthy(const char *name)
@@ -2345,6 +2357,10 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
23452357
if (!FEATURE_USED_P(opt->features, yjit) && env_var_truthy("RUBY_YJIT_ENABLE")) {
23462358
FEATURE_SET(opt->features, FEATURE_BIT(yjit));
23472359
}
2360+
#elif USE_ZJIT
2361+
if (!FEATURE_USED_P(opt->features, zjit) && env_var_truthy("RUBY_ZJIT_ENABLE")) {
2362+
FEATURE_SET(opt->features, FEATURE_BIT(zjit));
2363+
}
23482364
#endif
23492365
}
23502366
if (MULTI_BITS_P(FEATURE_SET_BITS(opt->features) & feature_jit_mask)) {

zjit/src/options.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::{ffi::CStr, os::raw::c_char};
1+
use std::{ffi::{CStr, CString}, ptr::null};
2+
use std::os::raw::{c_char, c_int, c_uint};
23

34
/// Number of calls to start profiling YARV instructions.
45
/// They are profiled `rb_zjit_call_threshold - rb_zjit_profile_threshold` times,
@@ -34,6 +35,25 @@ pub struct Options {
3435
pub dump_disasm: bool,
3536
}
3637

38+
/// Return an Options with default values
39+
pub fn init_options() -> Options {
40+
Options {
41+
num_profiles: 1,
42+
debug: false,
43+
dump_hir_init: None,
44+
dump_hir_opt: None,
45+
dump_lir: false,
46+
dump_disasm: false,
47+
}
48+
}
49+
50+
/// `ruby --help` descriptions for user-facing options. Do not add options for ZJIT developers.
51+
/// Note that --help allows only 80 chars per line, including indentation. 80-char limit --> |
52+
pub const ZJIT_OPTIONS: &'static [(&str, &str)] = &[
53+
("--zjit-call-threshold=num", "Number of calls to trigger JIT (default: 2)."),
54+
("--zjit-num-profiles=num", "Number of profiled calls before JIT (default: 1)."),
55+
];
56+
3757
#[derive(Clone, Copy, Debug)]
3858
pub enum DumpHIR {
3959
// Dump High-level IR without Snapshot
@@ -66,18 +86,6 @@ pub extern "C" fn rb_zjit_init_options() -> *const u8 {
6686
Box::into_raw(Box::new(options)) as *const u8
6787
}
6888

69-
/// Return an Options with default values
70-
pub fn init_options() -> Options {
71-
Options {
72-
num_profiles: 1,
73-
debug: false,
74-
dump_hir_init: None,
75-
dump_hir_opt: None,
76-
dump_lir: false,
77-
dump_disasm: false,
78-
}
79-
}
80-
8189
/// Parse a --zjit* command-line flag
8290
#[unsafe(no_mangle)]
8391
pub extern "C" fn rb_zjit_parse_option(options: *const u8, str_ptr: *const c_char) -> bool {
@@ -155,6 +163,21 @@ fn update_profile_threshold(options: &Options) {
155163
}
156164
}
157165

166+
/// Print YJIT options for `ruby --help`. `width` is width of option parts, and
167+
/// `columns` is indent width of descriptions.
168+
#[unsafe(no_mangle)]
169+
pub extern "C" fn rb_zjit_show_usage(help: c_int, highlight: c_int, width: c_uint, columns: c_int) {
170+
for &(name, description) in ZJIT_OPTIONS.iter() {
171+
unsafe extern "C" {
172+
fn ruby_show_usage_line(name: *const c_char, secondary: *const c_char, description: *const c_char,
173+
help: c_int, highlight: c_int, width: c_uint, columns: c_int);
174+
}
175+
let name = CString::new(name).unwrap();
176+
let description = CString::new(description).unwrap();
177+
unsafe { ruby_show_usage_line(name.as_ptr(), null(), description.as_ptr(), help, highlight, width, columns) }
178+
}
179+
}
180+
158181
/// Macro to print a message only when --zjit-debug is given
159182
macro_rules! debug {
160183
($($msg:tt)*) => {

0 commit comments

Comments
 (0)