ruby/yjit_core.c

938 строки
27 KiB
C
Исходник Обычный вид История

#include "ruby/ruby.h"
#include "internal.h"
#include "vm_sync.h"
#include "builtin.h"
#include "yjit_asm.h"
#include "yjit_utils.h"
#include "yjit_iface.h"
#include "yjit_core.h"
#include "yjit_codegen.h"
2020-12-10 08:06:10 +03:00
2021-01-27 21:02:55 +03:00
// Maximum number of versions per block
#define MAX_VERSIONS 4
2020-12-17 01:07:18 +03:00
// Maximum number of branch instructions we can track
#define MAX_BRANCHES 100000
2020-12-17 01:07:18 +03:00
// Registered branch entries
branch_t branch_entries[MAX_BRANCHES];
uint32_t num_branches = 0;
2020-12-10 08:06:10 +03:00
/*
Get an operand for the adjusted stack pointer address
*/
x86opnd_t
ctx_sp_opnd(ctx_t* ctx, int32_t offset_bytes)
{
2021-02-10 00:24:06 +03:00
int32_t offset = (ctx->sp_offset * sizeof(VALUE)) + offset_bytes;
return mem_opnd(64, REG_SP, offset);
}
/*
Push one new value on the temp stack
Return a pointer to the new stack top
*/
x86opnd_t
2021-03-31 22:54:46 +03:00
ctx_stack_push(ctx_t* ctx, val_type_t type)
{
// Keep track of the type of the value
2021-03-31 22:54:46 +03:00
if (ctx->stack_size < MAX_TEMP_TYPES) {
ctx->temp_mapping[ctx->stack_size] = MAP_STACK;
ctx->temp_types[ctx->stack_size] = type;
2021-03-31 22:54:46 +03:00
}
ctx->stack_size += 1;
ctx->sp_offset += 1;
// SP points just above the topmost value
int32_t offset = (ctx->sp_offset - 1) * sizeof(VALUE);
return mem_opnd(64, REG_SP, offset);
}
/*
Push the self value on the stack
*/
x86opnd_t
ctx_stack_push_self(ctx_t* ctx)
{
// Keep track of the type of the value
if (ctx->stack_size < MAX_TEMP_TYPES) {
ctx->temp_mapping[ctx->stack_size] = MAP_SELF;
ctx->temp_types[ctx->stack_size] = ctx->self_type;
}
ctx->stack_size += 1;
2021-02-10 00:24:06 +03:00
ctx->sp_offset += 1;
// SP points just above the topmost value
2021-02-10 00:24:06 +03:00
int32_t offset = (ctx->sp_offset - 1) * sizeof(VALUE);
return mem_opnd(64, REG_SP, offset);
}
2021-04-06 19:00:09 +03:00
/*
Push a local variable on the stack
*/
x86opnd_t
ctx_stack_push_local(ctx_t* ctx, size_t local_idx)
{
// Keep track of the type of the value
if (ctx->stack_size < MAX_TEMP_TYPES && local_idx < MAX_LOCAL_TYPES) {
ctx->temp_mapping[ctx->stack_size] = (temp_mapping_t){ .kind = TEMP_LOCAL, .idx = local_idx };
}
ctx->stack_size += 1;
ctx->sp_offset += 1;
// SP points just above the topmost value
int32_t offset = (ctx->sp_offset - 1) * sizeof(VALUE);
return mem_opnd(64, REG_SP, offset);
}
/*
Pop N values off the stack
Return a pointer to the stack top before the pop operation
*/
x86opnd_t
ctx_stack_pop(ctx_t* ctx, size_t n)
{
RUBY_ASSERT(n <= ctx->stack_size);
// SP points just above the topmost value
2021-02-10 00:24:06 +03:00
int32_t offset = (ctx->sp_offset - 1) * sizeof(VALUE);
x86opnd_t top = mem_opnd(64, REG_SP, offset);
// Clear the types of the popped values
for (size_t i = 0; i < n; ++i)
{
size_t idx = ctx->stack_size - i - 1;
2021-03-31 22:54:46 +03:00
if (idx < MAX_TEMP_TYPES) {
ctx->temp_types[idx] = TYPE_UNKNOWN;
ctx->temp_mapping[idx] = MAP_STACK;
}
}
2020-12-10 08:06:10 +03:00
ctx->stack_size -= n;
2021-02-10 00:24:06 +03:00
ctx->sp_offset -= n;
return top;
}
/**
Get an operand pointing to a slot on the temp stack
*/
x86opnd_t
ctx_stack_opnd(ctx_t* ctx, int32_t idx)
{
// SP points just above the topmost value
2021-02-10 00:24:06 +03:00
int32_t offset = (ctx->sp_offset - 1 - idx) * sizeof(VALUE);
x86opnd_t opnd = mem_opnd(64, REG_SP, offset);
return opnd;
}
2020-12-11 00:59:13 +03:00
/**
2021-03-31 22:54:46 +03:00
Get the type of a value on the temp stack
Returns T_NONE if unknown
*/
2021-03-31 22:54:46 +03:00
val_type_t
ctx_get_temp_type(const ctx_t* ctx, size_t idx)
{
2021-03-31 22:54:46 +03:00
RUBY_ASSERT(idx < ctx->stack_size);
if (ctx->stack_size > MAX_TEMP_TYPES)
2021-03-31 22:54:46 +03:00
return TYPE_UNKNOWN;
temp_mapping_t mapping = ctx->temp_mapping[ctx->stack_size - 1 - idx];
switch (mapping.kind)
{
case TEMP_SELF:
2021-03-31 22:54:46 +03:00
return ctx->self_type;
case TEMP_STACK:
2021-03-31 22:54:46 +03:00
return ctx->temp_types[ctx->stack_size - 1 - idx];
case TEMP_LOCAL:
RUBY_ASSERT(mapping.idx < MAX_LOCAL_TYPES);
return ctx->local_types[mapping.idx];
}
rb_bug("unreachable");
}
/**
Set the type of a value in the temporary stack
*/
void ctx_set_temp_type(ctx_t* ctx, size_t idx, val_type_t type)
{
RUBY_ASSERT(idx < ctx->stack_size);
if (ctx->stack_size > MAX_TEMP_TYPES)
return;
temp_mapping_t mapping = ctx->temp_mapping[ctx->stack_size - 1 - idx];
switch (mapping.kind)
{
case TEMP_SELF:
ctx->self_type = type;
break;
case TEMP_STACK:
ctx->temp_types[ctx->stack_size - 1 - idx] = type;
break;
case TEMP_LOCAL:
RUBY_ASSERT(mapping.idx < MAX_LOCAL_TYPES);
ctx->local_types[mapping.idx] = type;
break;
}
}
/**
Set the type of a local variable
*/
void ctx_set_local_type(ctx_t* ctx, size_t idx, val_type_t type)
{
if (ctx->stack_size > MAX_LOCAL_TYPES)
return;
ctx->local_types[idx] = type;
2021-03-31 22:54:46 +03:00
}
/*
Compute a difference between two value types
Returns 0 if the two are the same
Returns > 0 if different but compatible
Returns INT_MAX if incompatible
*/
int type_diff(val_type_t src, val_type_t dst)
{
RUBY_ASSERT(!src.is_heap || !src.is_imm);
RUBY_ASSERT(!dst.is_heap || !dst.is_imm);
// If dst assumes heap but src doesn't
if (dst.is_heap && !src.is_heap)
2021-03-31 22:54:46 +03:00
return INT_MAX;
// If dst assumes imm but src doesn't
if (dst.is_imm && !src.is_imm)
2021-03-31 22:54:46 +03:00
return INT_MAX;
// If dst assumes known type different from src
if (dst.type != ETYPE_UNKNOWN && dst.type != src.type)
2021-03-31 22:54:46 +03:00
return INT_MAX;
if (dst.is_heap != src.is_heap)
2021-03-31 22:54:46 +03:00
return 1;
if (dst.is_imm != src.is_imm)
2021-03-31 22:54:46 +03:00
return 1;
if (dst.type != src.type)
2021-03-31 22:54:46 +03:00
return 1;
return 0;
}
/**
Compute a difference score for two context objects
Returns 0 if the two contexts are the same
Returns > 0 if different but compatible
Returns INT_MAX if incompatible
*/
int ctx_diff(const ctx_t* src, const ctx_t* dst)
{
// Can only lookup the first version in the chain
if (dst->chain_depth != 0)
return INT_MAX;
// Blocks with depth > 0 always produce new versions
// Sidechains cannot overlap
if (src->chain_depth != 0)
return INT_MAX;
if (dst->stack_size != src->stack_size)
return INT_MAX;
2021-02-10 00:24:06 +03:00
if (dst->sp_offset != src->sp_offset)
return INT_MAX;
// Difference sum
int diff = 0;
2021-03-31 22:54:46 +03:00
// Check the type of self
int self_diff = type_diff(src->self_type, dst->self_type);
if (self_diff == INT_MAX)
return INT_MAX;
diff += self_diff;
// TODO: when we track local types, need to check them too
// For each value on the temp stack
for (size_t i = 0; i < src->stack_size; ++i)
{
2021-03-31 22:54:46 +03:00
val_type_t t_src = ctx_get_temp_type(src, i);
val_type_t t_dst = ctx_get_temp_type(dst, i);
int temp_diff = type_diff(t_src, t_dst);
if (temp_diff == INT_MAX)
return INT_MAX;
diff += temp_diff;
}
return diff;
}
// Get all blocks for a particular place in an iseq.
static rb_yjit_block_array_t
2021-03-04 20:05:18 +03:00
get_version_array(const rb_iseq_t *iseq, unsigned idx)
{
struct rb_iseq_constant_body *body = iseq->body;
2021-03-04 20:05:18 +03:00
if (rb_darray_size(body->yjit_blocks) == 0) {
return NULL;
}
2021-03-04 20:05:18 +03:00
RUBY_ASSERT((unsigned)rb_darray_size(body->yjit_blocks) == body->iseq_size);
return rb_darray_get(body->yjit_blocks, idx);
}
2021-03-04 20:05:18 +03:00
// Count the number of block versions matching a given blockid
static size_t get_num_versions(blockid_t blockid)
{
return rb_darray_size(get_version_array(blockid.iseq, blockid.idx));
}
// Keep track of a block version. Block should be fully constructed.
static void
add_block_version(blockid_t blockid, block_t* block)
{
// Function entry blocks must have stack size 0
RUBY_ASSERT(!(block->blockid.idx == 0 && block->ctx.stack_size > 0));
const rb_iseq_t *iseq = block->blockid.iseq;
struct rb_iseq_constant_body *body = iseq->body;
// Ensure yjit_blocks is initialized for this iseq
if (rb_darray_size(body->yjit_blocks) == 0) {
// Initialize yjit_blocks to be as wide as body->iseq_encoded
2021-02-17 05:03:20 +03:00
int32_t casted = (int32_t)body->iseq_size;
if ((unsigned)casted != body->iseq_size) {
rb_bug("iseq too large");
}
if (!rb_darray_make(&body->yjit_blocks, casted)) {
2021-02-17 05:03:20 +03:00
rb_bug("allocation failed");
}
2021-02-16 19:15:29 +03:00
#if RUBY_DEBUG
2021-02-16 19:15:29 +03:00
// First block compiled for this iseq
rb_compiled_iseq_count++;
#endif
}
RUBY_ASSERT((int32_t)blockid.idx < rb_darray_size(body->yjit_blocks));
rb_yjit_block_array_t *block_array_ref = rb_darray_ref(body->yjit_blocks, blockid.idx);
// Add the new block
if (!rb_darray_append(block_array_ref, block)) {
rb_bug("allocation failed");
}
{
// By writing the new block to the iseq, the iseq now
// contains new references to Ruby objects. Run write barriers.
2021-03-18 02:07:20 +03:00
RB_OBJ_WRITTEN(iseq, Qundef, block->receiver_klass);
RB_OBJ_WRITTEN(iseq, Qundef, block->callee_cme);
// Run write barriers for all objects in generated code.
uint32_t *offset_element;
rb_darray_foreach(block->gc_object_offsets, offset_idx, offset_element) {
uint32_t offset_to_value = *offset_element;
uint8_t *value_address = cb_get_ptr(cb, offset_to_value);
VALUE object;
memcpy(&object, value_address, SIZEOF_VALUE);
RB_OBJ_WRITTEN(iseq, Qundef, object);
}
}
}
2020-12-17 01:07:18 +03:00
// Retrieve a basic block version for an (iseq, idx) tuple
block_t* find_block_version(blockid_t blockid, const ctx_t* ctx)
2020-12-17 01:07:18 +03:00
{
rb_yjit_block_array_t versions = get_version_array(blockid.iseq, blockid.idx);
2020-12-17 01:07:18 +03:00
// Best match found
block_t* best_version = NULL;
int best_diff = INT_MAX;
// For each version matching the blockid
rb_darray_for(versions, idx) {
block_t *version = rb_darray_get(versions, idx);
int diff = ctx_diff(ctx, &version->ctx);
2021-03-04 20:05:18 +03:00
// Note that we always prefer the first matching
// version because of inline-cache chains
if (diff < best_diff) {
best_version = version;
best_diff = diff;
}
}
return best_version;
2020-12-17 01:07:18 +03:00
}
2021-01-27 21:02:55 +03:00
void
yjit_branches_update_references(void)
{
for (uint32_t i = 0; i < num_branches; i++) {
branch_entries[i].targets[0].iseq = (const void *)rb_gc_location((VALUE)branch_entries[i].targets[0].iseq);
branch_entries[i].targets[1].iseq = (const void *)rb_gc_location((VALUE)branch_entries[i].targets[1].iseq);
}
}
// Compile a new block version immediately
block_t* gen_block_version(blockid_t blockid, const ctx_t* start_ctx, rb_execution_context_t* ec)
{
// Copy the context to avoid mutating it
ctx_t ctx_copy = *start_ctx;
ctx_t* ctx = &ctx_copy;
// Allocate a new block version object
block_t* first_block = calloc(1, sizeof(block_t));
first_block->blockid = blockid;
memcpy(&first_block->ctx, ctx, sizeof(ctx_t));
// Block that is currently being compiled
block_t* block = first_block;
// Generate code for the first block
yjit_gen_block(ctx, block, ec);
// Keep track of the new block version
add_block_version(block->blockid, block);
// For each successor block to compile
for (;;) {
// If no branches were generated, stop
if (num_branches == 0) {
break;
}
// Get the last branch entry
uint32_t branch_idx = num_branches - 1;
branch_t* last_branch = &branch_entries[num_branches - 1];
// If there is no next block to compile, stop
if (last_branch->dst_addrs[0] || last_branch->dst_addrs[1]) {
break;
}
if (last_branch->targets[0].iseq == NULL) {
rb_bug("invalid target for last branch");
}
// Use the context from the branch
*ctx = last_branch->target_ctxs[0];
// Allocate a new block version object
block = calloc(1, sizeof(block_t));
block->blockid = last_branch->targets[0];
memcpy(&block->ctx, ctx, sizeof(ctx_t));
// Generate code for the current block
yjit_gen_block(ctx, block, ec);
// Keep track of the new block version
add_block_version(block->blockid, block);
// Patch the last branch address
last_branch->dst_addrs[0] = cb_get_ptr(cb, block->start_pos);
2021-02-20 00:04:23 +03:00
rb_darray_append(&block->incoming, branch_idx);
RUBY_ASSERT(block->start_pos == last_branch->end_pos);
}
return first_block;
}
// Generate a block version that is an entry point inserted into an iseq
uint8_t* gen_entry_point(const rb_iseq_t *iseq, uint32_t insn_idx, rb_execution_context_t *ec)
{
// The entry context makes no assumptions about types
blockid_t blockid = { iseq, insn_idx };
// Write the interpreter entry prologue
uint8_t* code_ptr = yjit_entry_prologue();
// Try to generate code for the entry block
block_t* block = gen_block_version(blockid, &DEFAULT_CTX, ec);
// If we couldn't generate any code
if (block->end_idx == insn_idx)
{
return NULL;
}
return code_ptr;
}
2020-12-17 05:45:51 +03:00
// Called by the generated code when a branch stub is executed
// Triggers compilation of branches and code patching
static uint8_t *
2021-04-06 18:03:50 +03:00
branch_stub_hit(const uint32_t branch_idx, const uint32_t target_idx, rb_execution_context_t* ec)
2020-12-17 05:45:51 +03:00
{
uint8_t* dst_addr;
ctx_t generic_ctx;
2021-04-06 17:36:00 +03:00
// Stop other ractors since we are going to patch machine code.
// This is how the GC does it.
RB_VM_LOCK_ENTER();
2021-04-06 17:36:00 +03:00
rb_vm_barrier();
2021-03-23 03:12:34 +03:00
RUBY_ASSERT(branch_idx < num_branches);
RUBY_ASSERT(target_idx < 2);
branch_t *branch = &branch_entries[branch_idx];
blockid_t target = branch->targets[target_idx];
2021-01-27 21:02:55 +03:00
const ctx_t* target_ctx = &branch->target_ctxs[target_idx];
// If this branch has already been patched, return the dst address
// Note: ractors can cause the same stub to be hit multiple times
if (branch->dst_patched & (1 << target_idx)) {
dst_addr = branch->dst_addrs[target_idx];
}
else
{
//fprintf(stderr, "\nstub hit, branch idx: %d, target idx: %d\n", branch_idx, target_idx);
//fprintf(stderr, "blockid.iseq=%p, blockid.idx=%d\n", target.iseq, target.idx);
//fprintf(stderr, "chain_depth=%d\n", target_ctx->chain_depth);
// :stub-sp-flush:
// Generated code do stack operations without modifying cfp->sp, while the
// cfp->sp tells the GC what values on the stack to root. Generated code
// generally takes care of updating cfp->sp when it calls runtime routines that
// could trigger GC, but for the case of branch stubs, it's inconvenient. So
// we do it here.
VALUE *const original_interp_sp = ec->cfp->sp;
ec->cfp->sp += target_ctx->sp_offset;
// Update the PC in the current CFP, because it
// may be out of sync in JITted code
ec->cfp->pc = iseq_pc_at_idx(target.iseq, target.idx);
// Try to find an existing compiled version of this block
block_t* p_block = find_block_version(target, target_ctx);
// If this block hasn't yet been compiled
if (!p_block) {
// Limit the number of block versions
if (target_ctx->chain_depth == 0) { // guard chains implement limits individually
if (get_num_versions(target) >= MAX_VERSIONS - 1) {
//fprintf(stderr, "version limit hit in branch_stub_hit\n");
generic_ctx = DEFAULT_CTX;
generic_ctx.stack_size = target_ctx->stack_size;
generic_ctx.sp_offset = target_ctx->sp_offset;
target_ctx = &generic_ctx;
}
2021-03-31 00:05:20 +03:00
}
// If the new block can be generated right after the branch (at cb->write_pos)
if (cb->write_pos == branch->end_pos) {
// Change the branch shape to indicate the target block will be placed next
branch->shape = (uint8_t)target_idx;
// Rewrite the branch with the new, potentially more compact shape
cb_set_pos(cb, branch->start_pos);
branch->gen_fn(cb, branch->dst_addrs[0], branch->dst_addrs[1], branch->shape);
RUBY_ASSERT(cb->write_pos <= branch->end_pos && "can't enlarge branches");
branch->end_pos = cb->write_pos;
}
2021-04-06 19:19:45 +03:00
p_block = gen_block_version(target, target_ctx, ec);
RUBY_ASSERT(p_block);
RUBY_ASSERT(!(branch->shape == (uint8_t)target_idx && p_block->start_pos != branch->end_pos));
2021-04-06 19:19:45 +03:00
}
// Add this branch to the list of incoming branches for the target
rb_darray_append(&p_block->incoming, branch_idx);
2020-12-17 05:45:51 +03:00
// Update the branch target address
dst_addr = cb_get_ptr(cb, p_block->start_pos);
branch->dst_addrs[target_idx] = dst_addr;
// Rewrite the branch with the new jump target address
RUBY_ASSERT(branch->dst_addrs[0] != NULL);
uint32_t cur_pos = cb->write_pos;
cb_set_pos(cb, branch->start_pos);
branch->gen_fn(cb, branch->dst_addrs[0], branch->dst_addrs[1], branch->shape);
RUBY_ASSERT(cb->write_pos == branch->end_pos && "branch can't change size");
cb_set_pos(cb, cur_pos);
// Mark this branch target as patched (no longer a stub)
branch->dst_patched |= (1 << target_idx);
// Restore interpreter sp, since the code hitting the stub expects the original.
ec->cfp->sp = original_interp_sp;
}
2020-12-17 05:45:51 +03:00
RB_VM_LOCK_LEAVE();
2020-12-17 05:45:51 +03:00
// Return a pointer to the compiled block version
return dst_addr;
2020-12-17 05:45:51 +03:00
}
2020-12-17 01:07:18 +03:00
// Get a version or stub corresponding to a branch target
uint8_t* get_branch_target(
blockid_t target,
const ctx_t* ctx,
uint32_t branch_idx,
uint32_t target_idx
)
2020-12-17 01:07:18 +03:00
{
2021-01-19 21:28:52 +03:00
//fprintf(stderr, "get_branch_target, block (%p, %d)\n", target.iseq, target.idx);
block_t* p_block = find_block_version(target, ctx);
2020-12-17 01:07:18 +03:00
if (p_block)
{
// Add an incoming branch for this version
2021-02-20 00:04:23 +03:00
rb_darray_append(&p_block->incoming, branch_idx);
2021-02-20 00:04:23 +03:00
// Return a pointer to the compiled code
return cb_get_ptr(cb, p_block->start_pos);
}
2020-12-17 01:07:18 +03:00
// Generate an outlined stub that will call
// branch_stub_hit(uint32_t branch_idx, uint32_t target_idx)
uint8_t* stub_addr = cb_get_ptr(ocb, ocb->write_pos);
// Save the yjit registers
push(ocb, REG_CFP);
push(ocb, REG_EC);
push(ocb, REG_SP);
push(ocb, REG_SP);
// Call branch_stub_hit(branch_idx, target_idx, ec)
mov(ocb, C_ARG_REGS[2], REG_EC);
mov(ocb, C_ARG_REGS[1], imm_opnd(target_idx));
mov(ocb, C_ARG_REGS[0], imm_opnd(branch_idx));
2020-12-17 05:45:51 +03:00
call_ptr(ocb, REG0, (void *)&branch_stub_hit);
2020-12-17 01:07:18 +03:00
// Restore the yjit registers
pop(ocb, REG_SP);
pop(ocb, REG_SP);
pop(ocb, REG_EC);
pop(ocb, REG_CFP);
2020-12-17 05:45:51 +03:00
// Jump to the address returned by the
// branch_stub_hit call
jmp_rm(ocb, RAX);
2020-12-17 01:07:18 +03:00
return stub_addr;
}
void gen_branch(
const ctx_t* src_ctx,
2021-03-10 19:18:17 +03:00
blockid_t target0,
const ctx_t* ctx0,
2021-03-10 19:18:17 +03:00
blockid_t target1,
const ctx_t* ctx1,
branchgen_fn gen_fn
)
2020-12-17 01:07:18 +03:00
{
RUBY_ASSERT(target0.iseq != NULL);
2021-02-10 00:24:06 +03:00
//RUBY_ASSERT(target1.iseq != NULL);
RUBY_ASSERT(num_branches < MAX_BRANCHES);
uint32_t branch_idx = num_branches++;
// Get the branch targets or stubs
uint8_t* dst_addr0 = get_branch_target(target0, ctx0, branch_idx, 0);
2021-02-10 00:24:06 +03:00
uint8_t* dst_addr1 = ctx1? get_branch_target(target1, ctx1, branch_idx, 1):NULL;
// Call the branch generation function
uint32_t start_pos = cb->write_pos;
gen_fn(cb, dst_addr0, dst_addr1, SHAPE_DEFAULT);
uint32_t end_pos = cb->write_pos;
// Register this branch entry
branch_t branch_entry = {
start_pos,
end_pos,
*src_ctx,
{ target0, target1 },
2021-02-10 00:24:06 +03:00
{ *ctx0, ctx1? *ctx1:DEFAULT_CTX },
{ dst_addr0, dst_addr1 },
gen_fn,
SHAPE_DEFAULT
};
branch_entries[branch_idx] = branch_entry;
}
void
gen_jump_branch(codeblock_t* cb, uint8_t* target0, uint8_t* target1, uint8_t shape)
{
switch (shape)
{
case SHAPE_NEXT0:
break;
case SHAPE_NEXT1:
RUBY_ASSERT(false);
break;
case SHAPE_DEFAULT:
jmp_ptr(cb, target0);
break;
}
}
void gen_direct_jump(
const ctx_t* ctx,
blockid_t target0
)
{
RUBY_ASSERT(target0.iseq != NULL);
RUBY_ASSERT(num_branches < MAX_BRANCHES);
ctx_t generic_ctx;
uint32_t branch_idx = num_branches++;
// Branch targets or stub adddress
uint8_t* dst_addr0;
// Shape of the branch
uint8_t branch_shape;
// Branch start and end positions
uint32_t start_pos;
uint32_t end_pos;
block_t* p_block = find_block_version(target0, ctx);
// If the version already exists
if (p_block)
{
2021-02-20 00:04:23 +03:00
rb_darray_append(&p_block->incoming, branch_idx);
dst_addr0 = cb_get_ptr(cb, p_block->start_pos);
branch_shape = SHAPE_DEFAULT;
// Call the branch generation function
start_pos = cb->write_pos;
gen_jump_branch(cb, dst_addr0, NULL, branch_shape);
end_pos = cb->write_pos;
}
else
{
2021-03-31 00:05:20 +03:00
// Limit the number of block versions
if (get_num_versions(target0) >= MAX_VERSIONS - 1)
{
//fprintf(stderr, "version limit hit in gen_direct_jump\n");
generic_ctx = DEFAULT_CTX;
generic_ctx.stack_size = ctx->stack_size;
generic_ctx.sp_offset = ctx->sp_offset;
2021-03-31 00:05:20 +03:00
ctx = &generic_ctx;
}
// The target block will follow next
// It will be compiled in gen_block_version()
dst_addr0 = NULL;
branch_shape = SHAPE_NEXT0;
start_pos = cb->write_pos;
end_pos = cb->write_pos;
}
2020-12-17 01:07:18 +03:00
// Register this branch entry
branch_t branch_entry = {
start_pos,
end_pos,
*ctx,
{ target0, BLOCKID_NULL },
{ *ctx, *ctx },
{ dst_addr0, NULL },
gen_jump_branch,
branch_shape
2020-12-17 01:07:18 +03:00
};
branch_entries[branch_idx] = branch_entry;
2021-01-13 01:03:54 +03:00
}
// Create a stub to force the code up to this point to be executed
void defer_compilation(
block_t* block,
uint32_t insn_idx,
ctx_t* cur_ctx
)
{
//fprintf(stderr, "defer compilation at (%p, %d) depth=%d\n", block->blockid.iseq, insn_idx, cur_ctx->chain_depth);
if (cur_ctx->chain_depth != 0) {
rb_bug("double defer");
}
ctx_t next_ctx = *cur_ctx;
if (next_ctx.chain_depth >= UINT8_MAX) {
rb_bug("max block version chain depth reached");
}
next_ctx.chain_depth += 1;
RUBY_ASSERT(num_branches < MAX_BRANCHES);
uint32_t branch_idx = num_branches++;
// Get the branch targets or stubs
blockid_t target0 = (blockid_t){ block->blockid.iseq, insn_idx };
uint8_t* dst_addr0 = get_branch_target(target0, &next_ctx, branch_idx, 0);
// Call the branch generation function
uint32_t start_pos = cb->write_pos;
gen_jump_branch(cb, dst_addr0, NULL, SHAPE_DEFAULT);
uint32_t end_pos = cb->write_pos;
// Register this branch entry
branch_t branch_entry = {
start_pos,
end_pos,
*cur_ctx,
{ target0, BLOCKID_NULL },
{ next_ctx, next_ctx },
{ dst_addr0, NULL },
gen_jump_branch,
SHAPE_DEFAULT
};
branch_entries[branch_idx] = branch_entry;
}
// Remove all references to a block then free it.
void
yjit_free_block(block_t *block)
{
yjit_unlink_method_lookup_dependency(block);
yjit_block_assumptions_free(block);
2021-02-20 00:04:23 +03:00
rb_darray_free(block->incoming);
2021-02-19 23:49:23 +03:00
rb_darray_free(block->gc_object_offsets);
free(block);
}
2021-03-04 20:05:18 +03:00
// Remove a block version without reordering the version array
static bool
block_array_remove(rb_yjit_block_array_t block_array, block_t *block)
2021-03-04 20:05:18 +03:00
{
bool after_target = false;
2021-03-04 20:05:18 +03:00
block_t **element;
rb_darray_foreach(block_array, idx, element) {
if (after_target) {
2021-03-04 20:05:18 +03:00
rb_darray_set(block_array, idx - 1, *element);
}
else if (*element == block) {
after_target = true;
}
2021-03-04 20:05:18 +03:00
}
if (after_target) rb_darray_pop_back(block_array);
return after_target;
2021-03-04 20:05:18 +03:00
}
2021-01-13 01:03:54 +03:00
// Invalidate one specific block version
2021-01-29 20:07:18 +03:00
void
invalidate_block_version(block_t* block)
2021-01-13 01:03:54 +03:00
{
ASSERT_vm_locking();
rb_vm_barrier(); // Stop other ractors since we are going to patch machine code.
const rb_iseq_t *iseq = block->blockid.iseq;
// fprintf(stderr, "invalidating block (%p, %d)\n", block->blockid.iseq, block->blockid.idx);
// fprintf(stderr, "block=%p\n", block);
2021-03-04 20:05:18 +03:00
// Remove this block from the version array
rb_yjit_block_array_t versions = get_version_array(iseq, block->blockid.idx);
2021-03-04 20:05:18 +03:00
RB_UNUSED_VAR(bool removed);
removed = block_array_remove(versions, block);
RUBY_ASSERT(removed);
2021-01-13 01:03:54 +03:00
// Get a pointer to the generated code for this block
uint8_t* code_ptr = cb_get_ptr(cb, block->start_pos);
2021-01-13 01:03:54 +03:00
// For each incoming branch
2021-02-20 00:04:23 +03:00
uint32_t* branch_idx;
rb_darray_foreach(block->incoming, i, branch_idx)
{
2021-02-20 00:04:23 +03:00
//uint32_t branch_idx = block->incoming[i];
branch_t* branch = &branch_entries[*branch_idx];
uint32_t target_idx = (branch->dst_addrs[0] == code_ptr)? 0:1;
2021-01-19 21:28:52 +03:00
//fprintf(stderr, "branch_idx=%d, target_idx=%d\n", branch_idx, target_idx);
//fprintf(stderr, "blockid.iseq=%p, blockid.idx=%d\n", block->blockid.iseq, block->blockid.idx);
// Create a stub for this branch target
branch->dst_addrs[target_idx] = get_branch_target(
block->blockid,
&block->ctx,
2021-02-20 00:04:23 +03:00
*branch_idx,
target_idx
);
// Mark this target as being a stub
branch->dst_patched &= ~(1 << target_idx);
// Check if the invalidated block immediately follows
bool target_next = block->start_pos == branch->end_pos;
if (target_next)
{
2021-01-19 21:28:52 +03:00
// The new block will no longer be adjacent
branch->shape = SHAPE_DEFAULT;
}
2021-01-13 01:03:54 +03:00
// Rewrite the branch with the new jump target address
RUBY_ASSERT(branch->dst_addrs[0] != NULL);
uint32_t cur_pos = cb->write_pos;
cb_set_pos(cb, branch->start_pos);
branch->gen_fn(cb, branch->dst_addrs[0], branch->dst_addrs[1], branch->shape);
branch->end_pos = cb->write_pos;
cb_set_pos(cb, cur_pos);
2021-01-13 01:03:54 +03:00
if (target_next && branch->end_pos > block->end_pos)
{
rb_bug("yjit invalidate rewrote branch past block end");
}
}
2021-01-13 01:03:54 +03:00
uint32_t idx = block->blockid.idx;
// FIXME: the following says "if", but it's unconditional.
// If the block is an entry point, it needs to be unmapped from its iseq
VALUE* entry_pc = iseq_pc_at_idx(iseq, idx);
int entry_opcode = opcode_at_pc(iseq, entry_pc);
2021-01-13 01:03:54 +03:00
// TODO: unmap_addr2insn in yjit_iface.c? Maybe we can write a function to encompass this logic?
// Should check how it's used in exit and side-exit
const void * const *handler_table = rb_vm_get_insns_address_table();
void* handler_addr = (void*)handler_table[entry_opcode];
iseq->body->iseq_encoded[idx] = (VALUE)handler_addr;
2021-01-13 01:03:54 +03:00
// TODO:
// May want to recompile a new entry point (for interpreter entry blocks)
// This isn't necessary for correctness
// FIXME:
// Call continuation addresses on the stack can also be atomically replaced by jumps going to the stub.
2021-01-13 01:03:54 +03:00
yjit_free_block(block);
2021-01-19 21:28:52 +03:00
// fprintf(stderr, "invalidation done\n");
2020-12-17 01:07:18 +03:00
}
2020-12-11 00:59:13 +03:00
void
yjit_init_core(void)
2020-12-11 00:59:13 +03:00
{
// Nothing yet
2020-12-11 00:59:13 +03:00
}