summaryrefslogtreecommitdiff
path: root/yjit_codegen.c
diff options
context:
space:
mode:
authorAlan Wu <XrXr@users.noreply.github.com>2021-11-26 18:00:42 -0500
committerGitHub <noreply@github.com>2021-11-26 18:00:42 -0500
commitb5b6ab4194f16e96ee5004288cc469ac1bca41a3 (patch)
treed4185b9e36c0863d9e82725234136ab082b84ed3 /yjit_codegen.c
parent59439446d9432cabf106c700943b29773b4a264f (diff)
YJIT: Add ability to exit to interpreter from stubs
Previously, YJIT assumed that it's always possible to generate a new basic block when servicing a stub in branch_stub_hit(). When YJIT is out of executable memory, for example, this assumption doesn't hold up. Add handling to branch_stub_hit() for servicing stubs without consuming more executable memory by adding a code path that exits to the interpreter at the location the branch stub represents. The new code path reconstructs interpreter state in branch_stub_hit() and then exits with a new snippet called `code_for_exit_from_stub` that returns `Qundef` from the YJIT native stack frame. As this change adds another place where we regenerate code from `branch_t`, extract the logic for it into a new function and call it regenerate_branch(). While we are at it, make the branch shrinking code path in branch_stub_hit() more explicit. This new functionality is hard to test without full support for out of memory conditions. To verify this change, I ran `RUBY_YJIT_ENABLE=1 make check -j12` with the following patch to stress test the new code path: ```diff diff --git a/yjit_core.c b/yjit_core.c index 4ab63d9806..5788b8c5ed 100644 --- a/yjit_core.c +++ b/yjit_core.c @@ -878,8 +878,12 @@ branch_stub_hit(branch_t *branch, const uint32_t target_idx, rb_execution_contex cb_set_write_ptr(cb, branch->end_addr); } +if (rand() < RAND_MAX/2) { // Compile the new block version p_block = gen_block_version(target, target_ctx, ec); +}else{ + p_block = NULL; +} if (!p_block && branch_modified) { // We couldn't generate a new block for the branch, but we modified the branch. ``` We can enable the new test along with other OOM tests once full support lands. Other small changes: * yjit_utils.c (print_str): Update to work with new native frame shape. Follow up for 8fa0ee4d404. * yjit_iface.c (rb_yjit_init): Run yjit_init_core() after yjit_init_codegen() so `cb` and `ocb` are available.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5180 Merged-By: XrXr
Diffstat (limited to 'yjit_codegen.c')
-rw-r--r--yjit_codegen.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/yjit_codegen.c b/yjit_codegen.c
index 26362a7064..2cd4fd2bda 100644
--- a/yjit_codegen.c
+++ b/yjit_codegen.c
@@ -382,6 +382,26 @@ yjit_gen_leave_exit(codeblock_t *cb)
return code_ptr;
}
+// Fill code_for_exit_from_stub. This is used by branch_stub_hit() to exit
+// to the interpreter when it cannot service a stub by generating new code.
+// Before coming here, branch_stub_hit() takes care of fully reconstructing
+// interpreter state.
+static void
+gen_code_for_exit_from_stub(void)
+{
+ codeblock_t *cb = ocb;
+ code_for_exit_from_stub = cb_get_ptr(cb, cb->write_pos);
+
+ GEN_COUNTER_INC(cb, exit_from_branch_stub);
+
+ pop(cb, REG_SP);
+ pop(cb, REG_EC);
+ pop(cb, REG_CFP);
+
+ mov(cb, RAX, imm_opnd(Qundef));
+ ret(cb);
+}
+
// :side-exit:
// Get an exit for the current instruction in the outlined block. The code
// for each instruction often begins with several guards before proceeding