Skip to content

ZJIT: Precise GC writebarriers #13935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions zjit/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::rc::Rc;
use crate::asm::Label;
use crate::backend::current::{Reg, ALLOC_REGS};
use crate::invariants::track_bop_assumption;
use crate::gc::get_or_create_iseq_payload;
use crate::gc::{get_or_create_iseq_payload, append_gc_offsets};
use crate::state::ZJITState;
use crate::{asm::CodeBlock, cruby::*, options::debug, virtualmem::CodePtr};
use crate::backend::lir::{self, asm_comment, asm_ccall, Assembler, Opnd, SideExitContext, Target, CFP, C_ARG_OPNDS, C_RET_OPND, EC, NATIVE_STACK_PTR, SP};
Expand Down Expand Up @@ -111,7 +111,7 @@ fn gen_iseq_entry_point(iseq: IseqPtr) -> *const u8 {
// Remember the block address to reuse it later
let payload = get_or_create_iseq_payload(iseq);
payload.start_ptr = Some(start_ptr);
payload.gc_offsets.extend(gc_offsets);
append_gc_offsets(iseq, &gc_offsets);

// Compile an entry point to the JIT code
(gen_entry(cb, iseq, &function, start_ptr, jit.c_stack_bytes), jit.branch_iseqs)
Expand Down Expand Up @@ -183,7 +183,7 @@ fn gen_iseq(cb: &mut CodeBlock, iseq: IseqPtr) -> Option<(CodePtr, Vec<(Rc<Branc
let result = gen_function(cb, iseq, &function);
if let Some((start_ptr, gc_offsets, jit)) = result {
payload.start_ptr = Some(start_ptr);
payload.gc_offsets.extend(gc_offsets);
append_gc_offsets(iseq, &gc_offsets);
Some((start_ptr, jit.branch_iseqs))
} else {
None
Expand Down Expand Up @@ -989,7 +989,7 @@ fn gen_guard_type(jit: &mut JITState, asm: &mut Assembler, val: lir::Opnd, guard

/// Compile an identity check with a side exit
fn gen_guard_bit_equals(jit: &mut JITState, asm: &mut Assembler, val: lir::Opnd, expected: VALUE, state: &FrameState) -> Option<lir::Opnd> {
asm.cmp(val, Opnd::UImm(expected.into()));
asm.cmp(val, Opnd::Value(expected));
asm.jnz(side_exit(jit, state, GuardBitEquals(expected))?);
Some(val)
}
Expand Down
17 changes: 17 additions & 0 deletions zjit/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ pub extern "C" fn rb_zjit_iseq_mark(payload: *mut c_void) {
}
}

/// Append a set of gc_offsets to the iseq's payload
pub fn append_gc_offsets(iseq: IseqPtr, offsets: &Vec<CodePtr>) {
let payload = get_or_create_iseq_payload(iseq);
payload.gc_offsets.extend(offsets);

// Call writebarrier on each newly added value
let cb = ZJITState::get_code_block();
for &offset in offsets.iter() {
let value_ptr: *const u8 = offset.raw_ptr(cb);
let value_ptr = value_ptr as *const VALUE;
unsafe {
let object = value_ptr.read_unaligned();
rb_gc_writebarrier(iseq.into(), object);
}
}
}

/// GC callback for updating GC objects in the per-iseq payload.
/// This is a mirror of [rb_zjit_iseq_mark].
#[unsafe(no_mangle)]
Expand Down
7 changes: 3 additions & 4 deletions zjit/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ fn profile_operands(profiler: &mut Profiler, profile: &mut IseqProfile, n: usize
for i in 0..n {
let opnd_type = Type::from_value(profiler.peek_at_stack((n - i - 1) as isize));
types[i] = types[i].union(opnd_type);
if let Some(object) = types[i].gc_object() {
unsafe { rb_gc_writebarrier(profiler.iseq.into(), object) };
}
}
// In the loop above, we probably added a new reference to the profile through
// the VALUE in Type. It's messy and relatively slow to conditionally run a
// write barrier for each Type, so just remember to re-mark the iseq.
unsafe { rb_gc_writebarrier_remember(profiler.iseq.into()) };
}

#[derive(Debug)]
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy