Skip to content
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

YJIT: Compress BranchGenFn and BranchShape #7401

Merged
merged 3 commits into from
Feb 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
113 changes: 8 additions & 105 deletions yjit/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1802,42 +1802,6 @@ fn gen_checkkeyword(
KeepCompiling
}

fn gen_jnz_to_target0(
asm: &mut Assembler,
target0: CodePtr,
_target1: Option<CodePtr>,
shape: BranchShape,
) {
match shape {
BranchShape::Next0 | BranchShape::Next1 => unreachable!(),
BranchShape::Default => asm.jnz(target0.into()),
}
}

fn gen_jz_to_target0(
asm: &mut Assembler,
target0: CodePtr,
_target1: Option<CodePtr>,
shape: BranchShape,
) {
match shape {
BranchShape::Next0 | BranchShape::Next1 => unreachable!(),
BranchShape::Default => asm.jz(Target::CodePtr(target0)),
}
}

fn gen_jbe_to_target0(
asm: &mut Assembler,
target0: CodePtr,
_target1: Option<CodePtr>,
shape: BranchShape,
) {
match shape {
BranchShape::Next0 | BranchShape::Next1 => unreachable!(),
BranchShape::Default => asm.jbe(Target::CodePtr(target0)),
}
}

// Generate a jump to a stub that recompiles the current YARV instruction on failure.
// When depth_limit is exceeded, generate a jump to a side exit.
fn jit_chain_guard(
Expand All @@ -1850,9 +1814,9 @@ fn jit_chain_guard(
side_exit: Target,
) {
let target0_gen_fn = match jcc {
JCC_JNE | JCC_JNZ => gen_jnz_to_target0,
JCC_JZ | JCC_JE => gen_jz_to_target0,
JCC_JBE | JCC_JNA => gen_jbe_to_target0,
JCC_JNE | JCC_JNZ => BranchGenFn::JNZToTarget0,
JCC_JZ | JCC_JE => BranchGenFn::JZToTarget0,
JCC_JBE | JCC_JNA => BranchGenFn::JBEToTarget0,
};

if (ctx.get_chain_depth() as i32) < depth_limit {
Expand All @@ -1865,7 +1829,7 @@ fn jit_chain_guard(

gen_branch(jit, asm, ocb, bid, &deeper, None, None, target0_gen_fn);
} else {
target0_gen_fn(asm, side_exit.unwrap_code_ptr(), None, BranchShape::Default);
target0_gen_fn.call(asm, side_exit.unwrap_code_ptr(), None);
}
}

Expand Down Expand Up @@ -3498,27 +3462,6 @@ fn gen_opt_case_dispatch(
}
}

fn gen_branchif_branch(
asm: &mut Assembler,
target0: CodePtr,
target1: Option<CodePtr>,
shape: BranchShape,
) {
assert!(target1 != None);
match shape {
BranchShape::Next0 => {
asm.jz(target1.unwrap().into());
}
BranchShape::Next1 => {
asm.jnz(target0.into());
}
BranchShape::Default => {
asm.jnz(target0.into());
asm.jmp(target1.unwrap().into());
}
}
}

fn gen_branchif(
jit: &mut JITState,
ctx: &mut Context,
Expand Down Expand Up @@ -3565,29 +3508,13 @@ fn gen_branchif(
ctx,
Some(next_block),
Some(ctx),
gen_branchif_branch,
BranchGenFn::BranchIf(BranchShape::Default),
);
}

EndBlock
}

fn gen_branchunless_branch(
asm: &mut Assembler,
target0: CodePtr,
target1: Option<CodePtr>,
shape: BranchShape,
) {
match shape {
BranchShape::Next0 => asm.jnz(target1.unwrap().into()),
BranchShape::Next1 => asm.jz(target0.into()),
BranchShape::Default => {
asm.jz(target0.into());
asm.jmp(target1.unwrap().into());
}
}
}

fn gen_branchunless(
jit: &mut JITState,
ctx: &mut Context,
Expand Down Expand Up @@ -3635,29 +3562,13 @@ fn gen_branchunless(
ctx,
Some(next_block),
Some(ctx),
gen_branchunless_branch,
BranchGenFn::BranchUnless(BranchShape::Default),
);
}

EndBlock
}

fn gen_branchnil_branch(
asm: &mut Assembler,
target0: CodePtr,
target1: Option<CodePtr>,
shape: BranchShape,
) {
match shape {
BranchShape::Next0 => asm.jne(target1.unwrap().into()),
BranchShape::Next1 => asm.je(target0.into()),
BranchShape::Default => {
asm.je(target0.into());
asm.jmp(target1.unwrap().into());
}
}
}

fn gen_branchnil(
jit: &mut JITState,
ctx: &mut Context,
Expand Down Expand Up @@ -3702,7 +3613,7 @@ fn gen_branchnil(
ctx,
Some(next_block),
Some(ctx),
gen_branchnil_branch,
BranchGenFn::BranchNil(BranchShape::Default),
);
}

Expand Down Expand Up @@ -5954,15 +5865,7 @@ fn gen_send_iseq(
&return_ctx,
None,
None,
|asm, target0, _target1, shape| {
match shape {
BranchShape::Default => {
asm.comment("update cfp->jit_return");
asm.mov(Opnd::mem(64, CFP, RUBY_OFFSET_CFP_JIT_RETURN), Opnd::const_ptr(target0.raw_ptr()));
}
_ => unreachable!()
}
},
BranchGenFn::JITReturn,
);

// Directly jump to the entry point of the callee
Expand Down