Skip to content

Commit 5e607cf

Browse files
authored
YJIT: Use a boxed slice for outgoing branches and cme dependencies (#7409)
YJIT: Use a boxed slice for outgoing branches and cme dependencies
1 parent 0d415a3 commit 5e607cf

File tree

3 files changed

+50
-30
lines changed

3 files changed

+50
-30
lines changed

yjit/src/codegen.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ pub struct JITState {
6666
// Whether we need to record the code address at
6767
// the end of this bytecode instruction for global invalidation
6868
record_boundary_patch_point: bool,
69+
70+
// The block's outgoing branches
71+
outgoing: Vec<BranchRef>,
72+
73+
// The block's CME dependencies
74+
cme_dependencies: Vec<CmePtr>,
6975
}
7076

7177
impl JITState {
@@ -79,6 +85,8 @@ impl JITState {
7985
side_exit_for_pc: None,
8086
ec: None,
8187
record_boundary_patch_point: false,
88+
outgoing: Vec::new(),
89+
cme_dependencies: Vec::new(),
8290
}
8391
}
8492

@@ -166,6 +174,16 @@ impl JITState {
166174
*ep.offset(VM_ENV_DATA_INDEX_SPECVAL as isize)
167175
}
168176
}
177+
178+
// Push an outgoing branch ref
179+
pub fn push_outgoing(&mut self, branch: BranchRef) {
180+
self.outgoing.push(branch);
181+
}
182+
183+
// Push a CME dependency
184+
pub fn push_cme_dependency(&mut self, cme: CmePtr) {
185+
self.cme_dependencies.push(cme);
186+
}
169187
}
170188

171189
use crate::codegen::JCCKinds::*;
@@ -833,6 +851,12 @@ pub fn gen_single_block(
833851
// Add the GC offsets to the block
834852
block.set_gc_obj_offsets(gc_offsets);
835853

854+
// Set CME dependencies to the block
855+
block.set_cme_dependencies(jit.cme_dependencies);
856+
857+
// Set outgoing branches to the block
858+
block.set_outgoing(jit.outgoing);
859+
836860
// Mark the end position of the block
837861
block.set_end_addr(cb.get_write_ptr());
838862

@@ -1806,7 +1830,7 @@ fn gen_checkkeyword(
18061830
// When depth_limit is exceeded, generate a jump to a side exit.
18071831
fn jit_chain_guard(
18081832
jcc: JCCKinds,
1809-
jit: &JITState,
1833+
jit: &mut JITState,
18101834
ctx: &Context,
18111835
asm: &mut Assembler,
18121836
ocb: &mut OutlinedCb,

yjit/src/core.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ struct BranchStub {
559559
/// Store info about an outgoing branch in a code segment
560560
/// Note: care must be taken to minimize the size of branch objects
561561
#[derive(Debug)]
562-
struct Branch {
562+
pub struct Branch {
563563
// Block this is attached to
564564
block: BlockRef,
565565

@@ -626,15 +626,15 @@ pub struct Block {
626626
// however, using a RefCell makes it easy to get a pointer to Branch objects
627627
//
628628
// List of outgoing branches (to successors)
629-
outgoing: Vec<BranchRef>,
629+
outgoing: Box<[BranchRef]>,
630630

631631
// FIXME: should these be code pointers instead?
632632
// Offsets for GC managed objects in the mainline code block
633633
gc_obj_offsets: Box<[u32]>,
634634

635635
// CME dependencies of this block, to help to remove all pointers to this
636636
// block in the system.
637-
cme_dependencies: Vec<CmePtr>,
637+
cme_dependencies: Box<[CmePtr]>,
638638

639639
// Code address of an exit for `ctx` and `blockid`.
640640
// Used for block invalidation.
@@ -647,7 +647,7 @@ pub struct Block {
647647
pub struct BlockRef(Rc<RefCell<Block>>);
648648

649649
/// Reference-counted pointer to a branch that can be borrowed mutably
650-
type BranchRef = Rc<RefCell<Branch>>;
650+
pub type BranchRef = Rc<RefCell<Branch>>;
651651

652652
/// List of block versions for a given blockid
653653
type VersionList = Vec<BlockRef>;
@@ -869,12 +869,12 @@ pub extern "C" fn rb_yjit_iseq_mark(payload: *mut c_void) {
869869
unsafe { rb_gc_mark_movable(block.blockid.iseq.into()) };
870870

871871
// Mark method entry dependencies
872-
for &cme_dep in &block.cme_dependencies {
872+
for &cme_dep in block.cme_dependencies.iter() {
873873
unsafe { rb_gc_mark_movable(cme_dep.into()) };
874874
}
875875

876876
// Mark outgoing branch entries
877-
for branch in &block.outgoing {
877+
for branch in block.outgoing.iter() {
878878
let branch = branch.borrow();
879879
for target in branch.targets.iter().flatten() {
880880
unsafe { rb_gc_mark_movable(target.get_blockid().iseq.into()) };
@@ -924,7 +924,7 @@ pub extern "C" fn rb_yjit_iseq_update_references(payload: *mut c_void) {
924924
block.blockid.iseq = unsafe { rb_gc_location(block.blockid.iseq.into()) }.as_iseq();
925925

926926
// Update method entry dependencies
927-
for cme_dep in &mut block.cme_dependencies {
927+
for cme_dep in block.cme_dependencies.iter_mut() {
928928
*cme_dep = unsafe { rb_gc_location((*cme_dep).into()) }.as_cme();
929929
}
930930

@@ -953,7 +953,7 @@ pub extern "C" fn rb_yjit_iseq_update_references(payload: *mut c_void) {
953953
// Update outgoing branch entries
954954
let outgoing_branches = block.outgoing.clone(); // clone to use after borrow
955955
mem::drop(block); // end mut borrow: target.set_iseq and target.get_blockid() might (mut) borrow it
956-
for branch in &outgoing_branches {
956+
for branch in outgoing_branches.iter() {
957957
let mut branch = branch.borrow_mut();
958958
for target in branch.targets.iter_mut().flatten() {
959959
target.set_iseq(unsafe { rb_gc_location(target.get_blockid().iseq.into()) }.as_iseq());
@@ -1173,9 +1173,9 @@ impl Block {
11731173
start_addr,
11741174
end_addr: None,
11751175
incoming: Vec::new(),
1176-
outgoing: Vec::new(),
1176+
outgoing: Box::new([]),
11771177
gc_obj_offsets: Box::new([]),
1178-
cme_dependencies: Vec::new(),
1178+
cme_dependencies: Box::new([]),
11791179
entry_exit: None,
11801180
};
11811181

@@ -1243,9 +1243,8 @@ impl Block {
12431243

12441244
/// Instantiate a new CmeDependency struct and add it to the list of
12451245
/// dependencies for this block.
1246-
pub fn add_cme_dependency(&mut self, callee_cme: CmePtr) {
1247-
self.cme_dependencies.push(callee_cme);
1248-
self.cme_dependencies.shrink_to_fit();
1246+
pub fn set_cme_dependencies(&mut self, cme_dependencies: Vec<CmePtr>) {
1247+
self.cme_dependencies = cme_dependencies.into_boxed_slice();
12491248
}
12501249

12511250
// Push an incoming branch ref and shrink the vector
@@ -1255,9 +1254,8 @@ impl Block {
12551254
}
12561255

12571256
// Push an outgoing branch ref and shrink the vector
1258-
fn push_outgoing(&mut self, branch: BranchRef) {
1259-
self.outgoing.push(branch);
1260-
self.outgoing.shrink_to_fit();
1257+
pub fn set_outgoing(&mut self, outgoing: Vec<BranchRef>) {
1258+
self.outgoing = outgoing.into_boxed_slice();
12611259
}
12621260

12631261
// Compute the size of the block code
@@ -1880,7 +1878,7 @@ fn regenerate_branch(cb: &mut CodeBlock, branch: &mut Branch) {
18801878
}
18811879

18821880
/// Create a new outgoing branch entry for a block
1883-
fn make_branch_entry(block: &BlockRef, gen_fn: BranchGenFn) -> BranchRef {
1881+
fn make_branch_entry(jit: &mut JITState, block: &BlockRef, gen_fn: BranchGenFn) -> BranchRef {
18841882
let branch = Branch {
18851883
// Block this is attached to
18861884
block: block.clone(),
@@ -1898,7 +1896,7 @@ fn make_branch_entry(block: &BlockRef, gen_fn: BranchGenFn) -> BranchRef {
18981896

18991897
// Add to the list of outgoing branches for the block
19001898
let branchref = Rc::new(RefCell::new(branch));
1901-
block.borrow_mut().push_outgoing(branchref.clone());
1899+
jit.push_outgoing(branchref.clone());
19021900
incr_counter!(compiled_branch_count);
19031901

19041902
return branchref;
@@ -2203,7 +2201,7 @@ impl Assembler
22032201
}
22042202

22052203
pub fn gen_branch(
2206-
jit: &JITState,
2204+
jit: &mut JITState,
22072205
asm: &mut Assembler,
22082206
ocb: &mut OutlinedCb,
22092207
target0: BlockId,
@@ -2212,7 +2210,7 @@ pub fn gen_branch(
22122210
ctx1: Option<&Context>,
22132211
gen_fn: BranchGenFn,
22142212
) {
2215-
let branchref = make_branch_entry(&jit.get_block(), gen_fn);
2213+
let branchref = make_branch_entry(jit, &jit.get_block(), gen_fn);
22162214
let branch = &mut branchref.borrow_mut();
22172215

22182216
// Get the branch targets or stubs
@@ -2232,8 +2230,8 @@ pub fn gen_branch(
22322230
asm.mark_branch_end(&branchref);
22332231
}
22342232

2235-
pub fn gen_direct_jump(jit: &JITState, ctx: &Context, target0: BlockId, asm: &mut Assembler) {
2236-
let branchref = make_branch_entry(&jit.get_block(), BranchGenFn::JumpToTarget0(BranchShape::Default));
2233+
pub fn gen_direct_jump(jit: &mut JITState, ctx: &Context, target0: BlockId, asm: &mut Assembler) {
2234+
let branchref = make_branch_entry(jit, &jit.get_block(), BranchGenFn::JumpToTarget0(BranchShape::Default));
22372235
let mut branch = branchref.borrow_mut();
22382236

22392237
let mut new_target = BranchTarget::Stub(Box::new(BranchStub {
@@ -2276,7 +2274,7 @@ pub fn gen_direct_jump(jit: &JITState, ctx: &Context, target0: BlockId, asm: &mu
22762274

22772275
/// Create a stub to force the code up to this point to be executed
22782276
pub fn defer_compilation(
2279-
jit: &JITState,
2277+
jit: &mut JITState,
22802278
cur_ctx: &Context,
22812279
asm: &mut Assembler,
22822280
ocb: &mut OutlinedCb,
@@ -2293,7 +2291,7 @@ pub fn defer_compilation(
22932291
next_ctx.chain_depth += 1;
22942292

22952293
let block_rc = jit.get_block();
2296-
let branch_rc = make_branch_entry(&jit.get_block(), BranchGenFn::JumpToTarget0(BranchShape::Default));
2294+
let branch_rc = make_branch_entry(jit, &jit.get_block(), BranchGenFn::JumpToTarget0(BranchShape::Default));
22972295
let mut branch = branch_rc.borrow_mut();
22982296
let block = block_rc.borrow();
22992297

@@ -2338,7 +2336,7 @@ fn remove_from_graph(blockref: &BlockRef) {
23382336
}
23392337

23402338
// For each outgoing branch
2341-
for out_branchref in &block.outgoing {
2339+
for out_branchref in block.outgoing.iter() {
23422340
let out_branch = out_branchref.borrow();
23432341

23442342
// For each successor block
@@ -2364,7 +2362,7 @@ pub fn free_block(blockref: &BlockRef) {
23642362
// Branches have a Rc pointing at the block housing them.
23652363
// Break the cycle.
23662364
blockref.borrow_mut().incoming.clear();
2367-
blockref.borrow_mut().outgoing.clear();
2365+
blockref.borrow_mut().outgoing = Box::new([]);
23682366

23692367
// No explicit deallocation here as blocks are ref-counted.
23702368
}

yjit/src/invariants.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ pub fn assume_method_lookup_stable(
123123
jit_ensure_block_entry_exit(jit, ocb);
124124

125125
let block = jit.get_block();
126-
block
127-
.borrow_mut()
128-
.add_cme_dependency(callee_cme);
126+
jit.push_cme_dependency(callee_cme);
129127

130128
Invariants::get_instance()
131129
.cme_validity

0 commit comments

Comments
 (0)