Skip to content

Commit

Permalink
Remove MergeContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
sisshiki1969 committed Jan 30, 2025
1 parent b9013a2 commit d18817a
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 113 deletions.
82 changes: 40 additions & 42 deletions monoruby/src/compiler/jitgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ mod variables;

struct ContinuationInfo {
from: BBContext,
to: MergeContext,
to: BBContext,
pc: BytecodePtr,
}

impl ContinuationInfo {
fn new(from: BBContext, to: MergeContext, pc: BytecodePtr) -> Self {
fn new(from: BBContext, to: BBContext, pc: BytecodePtr) -> Self {
Self { from, to, pc }
}
}
Expand All @@ -64,20 +64,37 @@ enum CompileResult {
#[derive(Debug, Clone, Copy)]
struct JitLabel(usize);

#[derive(Debug, PartialEq)]
enum BranchMode {
///
/// continuation branch.
/// 'continuation' means the destination is adjacent to the source basic block on the bytecode.
///
Continue,
///
/// side branch. (conditional branch)
///
Side,
///
/// branch. (unconditional branch)
///
Branch,
}

///
/// The information for branches.
///
#[derive(Debug)]
struct BranchEntry {
/// source instruction index of the branch.
src_idx: BcIndex,
/// source BasicBlockId of the branch.
src_bb: BasicBlockId,
/// context of the source basic block.
bbctx: BBContext,
/// `DestLabel` for the destination basic block.
branch_dest: JitLabel,
/// true if the branch is a continuation branch.
/// 'continuation' means the destination is adjacent to the source basic block on the bytecode.
cont: bool,
cont: BranchMode,
}

pub(crate) fn conv(reg: SlotId) -> i32 {
Expand Down Expand Up @@ -122,8 +139,8 @@ impl BBContext {
}
}

fn set_guard_from(&mut self, merger: &MergeContext) {
self.slot_state.set_guard_from(&merger.0)
fn set_guard_from(&mut self, merger: &BBContext) {
self.slot_state.set_guard_from(merger)
}

fn pc(&self) -> BytecodePtr {
Expand All @@ -142,16 +159,16 @@ impl BBContext {
self.class_version_guarded = false;
}

fn union(entries: &[BranchEntry]) -> MergeContext {
let mut merge_ctx = MergeContext::new(&entries.last().unwrap().bbctx);
fn union(entries: &[BranchEntry]) -> Self {
let mut merge_ctx = entries.last().unwrap().bbctx.clone();
for BranchEntry {
src_idx: _src_idx,
src_bb: _src_bb,
bbctx,
..
} in entries.iter()
{
#[cfg(feature = "jit-debug")]
eprintln!(" <-{:?}:[{:?}] {:?}", _src_idx, bbctx.sp, bbctx.slot_state);
eprintln!(" <-{:?}:[{:?}] {:?}", _src_bb, bbctx.sp, bbctx.slot_state);
merge_ctx.merge(bbctx);
}
#[cfg(feature = "jit-debug")]
Expand Down Expand Up @@ -583,36 +600,6 @@ enum LinkMode {
Accumulator,
}

#[derive(Debug, Clone)]
struct MergeContext(BBContext);

impl std::ops::Deref for MergeContext {
type Target = BBContext;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::ops::DerefMut for MergeContext {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl MergeContext {
fn new(bb: &BBContext) -> Self {
MergeContext(bb.clone())
}

fn get(self) -> BBContext {
self.0
}

fn remove_unused(&mut self, unused: &[SlotId]) {
unused.iter().for_each(|reg| self.discard(*reg));
}
}

impl Codegen {
pub(super) fn jit_compile(
&mut self,
Expand Down Expand Up @@ -720,8 +707,19 @@ impl Codegen {
let pair = self.get_address_pair();

// generate machine code for a main context
for ir in std::mem::take(&mut ctx.ir).into_iter() {
for (bbid, ir) in std::mem::take(&mut ctx.ir).into_iter() {
self.gen_asm(ir, store, &mut ctx, None);
// generate machine code for bridges
if let Some(bbid) = bbid
&& let Some((ir, exit)) = ctx.continue_bridges.remove(&bbid)
{
self.gen_asm(ir, store, &mut ctx, None);
let exit = ctx.get_bb_label(exit);
let exit = ctx.resolve_label(&mut self.jit, exit);
monoasm! { &mut self.jit,
jmp exit;
}
}
}

// generate machine code for bridges
Expand Down
4 changes: 1 addition & 3 deletions monoruby/src/compiler/jitgen/asmir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,10 +1396,8 @@ impl Codegen {
}
}

if entry_exit.is_some() {
self.jit.select_page(1);
}
if let Some((entry, _)) = entry_exit {
self.jit.select_page(1);
self.jit.bind_label(entry);
}

Expand Down
52 changes: 23 additions & 29 deletions monoruby/src/compiler/jitgen/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl JitContext {
ir.push(AsmInst::Preparation);

assert!(self.ir.is_empty());
self.ir.push(ir);
self.ir.push((None, ir));

let start_pos = func.get_pc_index(self.position());

Expand All @@ -47,10 +47,10 @@ impl JitContext {
self.branch_map.insert(
bb_begin,
vec![BranchEntry {
src_idx: BcIndex(0),
src_bb: BasicBlockId(0),
bbctx,
branch_dest,
cont: true,
cont: BranchMode::Continue,
}],
);

Expand All @@ -64,7 +64,7 @@ impl JitContext {

for bbid in bb_begin..=bb_end {
let ir = self.compile_basic_block(store, func, self.position(), bbid);
self.ir.push(ir);
self.ir.push((Some(bbid), ir));
}

self.backedge_branches(func);
Expand Down Expand Up @@ -132,32 +132,28 @@ impl JitContext {
ctx.branch_map.insert(
loop_start,
vec![BranchEntry {
src_idx: BcIndex(0),
src_bb: BasicBlockId(0),
bbctx,
branch_dest,
cont: true,
cont: BranchMode::Continue,
}],
);

for bbid in loop_start..=loop_end {
ctx.analyse_basic_block(store, func, &mut liveness, bbid);
}

let mut backedge: Option<MergeContext> = None;
let mut backedge: Option<BBContext> = None;
if let Some(branches) = ctx.branch_map.remove(&loop_start) {
for BranchEntry { src_idx, bbctx, .. } in branches {
let src_bb = func.bb_info.get_bb_id(src_idx);
if src_bb > loop_start {
// backegde
if let Some(ctx) = &mut backedge {
ctx.merge(&bbctx);
} else {
backedge = Some(MergeContext::new(&bbctx));
}
for BranchEntry { src_bb, bbctx, .. } in branches {
liveness.merge(&bbctx);
assert!(src_bb > loop_start);
// backegde
if let Some(ctx) = &mut backedge {
ctx.merge(&bbctx);
} else {
panic!()
backedge = Some(bbctx);
}
liveness.merge(bbctx);
}
}

Expand Down Expand Up @@ -187,7 +183,7 @@ impl JitContext {
CompileResult::Continue => {}
CompileResult::Branch => return,
CompileResult::Leave | CompileResult::Recompile | CompileResult::ExitLoop => {
liveness.merge(bbctx);
liveness.merge(&bbctx);
return;
}
}
Expand All @@ -207,8 +203,7 @@ impl JitContext {
) -> Option<BBContext> {
if let Some(bb) = self.target_ctx.remove(&bbid) {
Some(bb)
} else if let Some(bb) = self.incoming_context(func, bbid) {
self.gen_continuation(ir);
} else if let Some(bb) = self.incoming_context(ir, func, bbid) {
Some(bb)
} else {
None
Expand All @@ -220,8 +215,7 @@ impl JitContext {
if let Some(next_bbid) = func.bb_info.is_bb_head(next_idx) {
let label = self.label();
self.new_continue(func, end, next_bbid, bbctx, label);
if let Some(target_ctx) = self.incoming_context(func, next_bbid) {
self.gen_continuation(ir);
if let Some(target_ctx) = self.incoming_context(ir, func, next_bbid) {
assert!(self.target_ctx.insert(next_bbid, target_ctx).is_none());
}
} else {
Expand Down Expand Up @@ -518,7 +512,7 @@ impl JitContext {
bbctx.discard(info.dst);
bbctx.clear_above_next_sp();
ir.float_cmp_br(mode, kind, brkind, branch_dest);
self.new_branch(func, index, dest, bbctx.clone(), branch_dest);
self.new_side_branch(func, index, dest, bbctx.clone(), branch_dest);
}
TraceIr::ICmpBr {
kind,
Expand All @@ -530,7 +524,7 @@ impl JitContext {
let index = bc_pos + 1;
let branch_dest = self.label();
bbctx.gen_cmpbr_integer(ir, kind, mode, brkind, branch_dest);
self.new_branch(func, index, dest, bbctx.clone(), branch_dest);
self.new_side_branch(func, index, dest, bbctx.clone(), branch_dest);
}
TraceIr::GCmpBr {
kind,
Expand Down Expand Up @@ -801,15 +795,15 @@ impl JitContext {
let branch_dest = self.label();
bbctx.fetch(ir, cond_, GP::Rax);
ir.push(AsmInst::NilBr(branch_dest));
self.new_branch(func, bc_pos, dest_idx, bbctx.clone(), branch_dest);
self.new_side_branch(func, bc_pos, dest_idx, bbctx.clone(), branch_dest);
}
}
TraceIr::CondBr(_, _, true, _) => {}
TraceIr::CheckLocal(local, dest_idx) => {
let branch_dest = self.label();
bbctx.fetch(ir, local, GP::Rax);
ir.push(AsmInst::CheckLocal(branch_dest));
self.new_branch(func, bc_pos, dest_idx, bbctx.clone(), branch_dest);
self.new_side_branch(func, bc_pos, dest_idx, bbctx.clone(), branch_dest);
}
TraceIr::OptCase {
cond,
Expand All @@ -821,7 +815,7 @@ impl JitContext {
let else_idx = dest_bb[0];
for bbid in dest_bb {
let branch_dest = self.label();
self.new_branch(func, bc_pos, bbid, bbctx.clone(), branch_dest);
self.new_side_branch(func, bc_pos, bbid, bbctx.clone(), branch_dest);
}
let deopt = bbctx.new_deopt(ir);
bbctx.fetch_fixnum(ir, cond, GP::Rdi, deopt);
Expand All @@ -843,7 +837,7 @@ impl JitContext {
) {
let branch_dest = self.label();
ir.push(AsmInst::CondBr(brkind, branch_dest));
self.new_branch(func, src_idx, dest, bbctx.clone(), branch_dest);
self.new_side_branch(func, src_idx, dest, bbctx.clone(), branch_dest);
}

fn cmpkind_to_id(kind: CmpKind) -> IdentId {
Expand Down
Loading

0 comments on commit d18817a

Please sign in to comment.