Skip to content

Commit f664713

Browse files
committed
ZJIT: Detect built-in functions' attributes automatically
1 parent f4952d1 commit f664713

4 files changed

Lines changed: 19 additions & 1 deletion

File tree

zjit.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ rb_iseq_set_zjit_payload(const rb_iseq_t *iseq, void *payload)
331331
iseq->body->zjit_payload = payload;
332332
}
333333

334+
unsigned int
335+
rb_zjit_get_iseq_body_builtin_attrs(const rb_iseq_t *iseq)
336+
{
337+
return iseq->body->builtin_attrs;
338+
}
339+
334340
void
335341
rb_zjit_print_exception(void)
336342
{

zjit/bindgen/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ fn main() {
334334
.allowlist_function("rb_zjit_mark_executable")
335335
.allowlist_function("rb_zjit_mark_unused")
336336
.allowlist_function("rb_zjit_get_page_size")
337+
.allowlist_function("rb_zjit_get_iseq_body_builtin_attrs")
337338
.allowlist_function("rb_zjit_iseq_builtin_attrs")
338339
.allowlist_function("rb_zjit_iseq_inspect")
339340
.allowlist_function("rb_zjit_iseq_insn_set")

zjit/src/cruby_bindings.inc.rs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zjit/src/cruby_methods.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn annotate_c_method(props_map: &mut HashMap<*mut c_void, FnProperties>, class:
6767
}
6868

6969
/// Look up a method and find its builtin function pointer by parsing its ISEQ
70-
fn annotate_builtin_method(props_map: &mut HashMap<*mut c_void, FnProperties>, class: VALUE, method_name: &'static str, props: FnProperties) {
70+
fn annotate_builtin_method(props_map: &mut HashMap<*mut c_void, FnProperties>, class: VALUE, method_name: &'static str, mut props: FnProperties) {
7171
unsafe {
7272
let method_id = rb_intern2(method_name.as_ptr().cast(), method_name.len().try_into().unwrap());
7373
let method = rb_method_entry_at(class, method_id);
@@ -93,6 +93,15 @@ fn annotate_builtin_method(props_map: &mut HashMap<*mut c_void, FnProperties>, c
9393
method_name);
9494
}
9595

96+
// Check if the ISEQ has the BUILTIN_ATTR_LEAF attribute
97+
let builtin_attrs = rb_zjit_get_iseq_body_builtin_attrs(iseq);
98+
if (builtin_attrs & BUILTIN_ATTR_LEAF) != 0 {
99+
// If the method is marked as leaf via Primitive.attr!, set leaf to true
100+
props.leaf = true;
101+
// Leaf methods don't trigger GC
102+
props.no_gc = true;
103+
}
104+
96105
// Get the size of the ISEQ in instruction units
97106
let encoded_size = rb_iseq_encoded_size(iseq);
98107

@@ -163,6 +172,7 @@ pub fn init() -> Annotations {
163172
annotate!(rb_mKernel, "nil?", types::FalseClass, no_gc, leaf, elidable);
164173

165174
// Annotate builtin functions using the new method-based approach
175+
// The leaf property will be automatically detected from Primitive.attr! :leaf in the Ruby code
166176
annotate_builtin!(rb_mKernel, "Float", types::Flonum);
167177
annotate_builtin!(rb_mKernel, "Integer", types::Integer);
168178
annotate_builtin!(rb_mKernel, "class", types::Class);

0 commit comments

Comments
 (0)