From e572794eeb3cf9b8bc9c043c5944a9ed00e155f0 Mon Sep 17 00:00:00 2001 From: Maxime Chevalier-Boisvert Date: Fri, 19 Feb 2021 16:04:23 -0500 Subject: [PATCH] Use darray for incoming branches --- ujit_codegen.c | 8 ++++---- ujit_core.c | 33 ++++++++++++--------------------- ujit_core.h | 5 +++-- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/ujit_codegen.c b/ujit_codegen.c index 9bad5f31e4d9ec..2c5478bc9a2aca 100644 --- a/ujit_codegen.c +++ b/ujit_codegen.c @@ -1034,7 +1034,7 @@ gen_jump(jitstate_t* jit, ctx_t* ctx) } static bool -gen_opt_swb_cfunc(jitstate_t* jit, ctx_t* ctx, struct rb_call_data * cd, const rb_callable_method_entry_t *cme, int32_t argc) +gen_oswb_cfunc(jitstate_t* jit, ctx_t* ctx, struct rb_call_data * cd, const rb_callable_method_entry_t *cme, int32_t argc) { const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(cme->def, body.cfunc); @@ -1260,7 +1260,7 @@ gen_return_branch(codeblock_t* cb, uint8_t* target0, uint8_t* target1, uint8_t s } static bool -gen_opt_swb_iseq(jitstate_t* jit, ctx_t* ctx, struct rb_call_data * cd, const rb_callable_method_entry_t *cme, int32_t argc) +gen_oswb_iseq(jitstate_t* jit, ctx_t* ctx, struct rb_call_data * cd, const rb_callable_method_entry_t *cme, int32_t argc) { const rb_iseq_t *iseq = def_iseq_ptr(cme->def); const VALUE* start_pc = iseq->body->iseq_encoded; @@ -1463,13 +1463,13 @@ gen_opt_send_without_block(jitstate_t* jit, ctx_t* ctx) // If this is a C call if (cme->def->type == VM_METHOD_TYPE_CFUNC) { - return gen_opt_swb_cfunc(jit, ctx, cd, cme, argc); + return gen_oswb_cfunc(jit, ctx, cd, cme, argc); } // If this is a Ruby call if (cme->def->type == VM_METHOD_TYPE_ISEQ) { - return gen_opt_swb_iseq(jit, ctx, cd, cme, argc); + return gen_oswb_iseq(jit, ctx, cd, cme, argc); } return false; diff --git a/ujit_core.c b/ujit_core.c index 1313cfcfabcc41..2777ed2682870c 100644 --- a/ujit_core.c +++ b/ujit_core.c @@ -198,17 +198,6 @@ add_block_version(blockid_t blockid, block_t* block) } } -// Add an incoming branch for a given block version -static void add_incoming(block_t* p_block, uint32_t branch_idx) -{ - // Add this branch to the list of incoming branches for the target - uint32_t* new_list = malloc(sizeof(uint32_t) * (p_block->num_incoming + 1)); - memcpy(new_list, p_block->incoming, sizeof(uint32_t) * p_block->num_incoming); - new_list[p_block->num_incoming] = branch_idx; - p_block->incoming = new_list; - p_block->num_incoming += 1; -} - // Count the number of block versions matching a given blockid static size_t count_block_versions(blockid_t blockid) { @@ -322,7 +311,8 @@ block_t* gen_block_version(blockid_t blockid, const ctx_t* start_ctx) // Patch the last branch address last_branch->dst_addrs[0] = cb_get_ptr(cb, block->start_pos); - add_incoming(block, branch_idx); + rb_darray_append(&block->incoming, branch_idx); + RUBY_ASSERT(block->start_pos == last_branch->end_pos); } @@ -399,7 +389,7 @@ uint8_t* branch_stub_hit(uint32_t branch_idx, uint32_t target_idx) } // Add this branch to the list of incoming branches for the target - add_incoming(p_block, branch_idx); + rb_darray_append(&p_block->incoming, branch_idx); // Update the branch target address dst_addr = cb_get_ptr(cb, p_block->start_pos); @@ -435,8 +425,9 @@ uint8_t* get_branch_target( if (p_block) { // Add an incoming branch for this version - add_incoming(p_block, branch_idx); + rb_darray_append(&p_block->incoming, branch_idx); + // Return a pointer to the compiled code return cb_get_ptr(cb, p_block->start_pos); } @@ -557,7 +548,7 @@ void gen_direct_jump( // If the version already exists if (p_block) { - add_incoming(p_block, branch_idx); + rb_darray_append(&p_block->incoming, branch_idx); dst_addr0 = cb_get_ptr(cb, p_block->start_pos); branch_shape = SHAPE_DEFAULT; @@ -596,8 +587,7 @@ void ujit_free_block(block_t *block) { ujit_unlink_method_lookup_dependency(block); - - free(block->incoming); + rb_darray_free(block->incoming); free(block); } @@ -634,10 +624,11 @@ invalidate_block_version(block_t* block) uint8_t* code_ptr = cb_get_ptr(cb, block->start_pos); // For each incoming branch - for (uint32_t i = 0; i < block->num_incoming; ++i) + uint32_t* branch_idx; + rb_darray_foreach(block->incoming, i, branch_idx) { - uint32_t branch_idx = block->incoming[i]; - branch_t* branch = &branch_entries[branch_idx]; + //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; //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); @@ -646,7 +637,7 @@ invalidate_block_version(block_t* block) branch->dst_addrs[target_idx] = get_branch_target( block->blockid, &block->ctx, - branch_idx, + *branch_idx, target_idx ); diff --git a/ujit_core.h b/ujit_core.h index c537c5e7b96201..1d556b2a491178 100644 --- a/ujit_core.h +++ b/ujit_core.h @@ -98,6 +98,8 @@ typedef struct BranchEntry } branch_t; +typedef rb_darray(uint32_t) int32_array_t; + /** Basic block version Represents a portion of an iseq compiled with a given context @@ -119,8 +121,7 @@ typedef struct ujit_block_version uint32_t end_pos; // List of incoming branches indices - uint32_t *incoming; - uint32_t num_incoming; + int32_array_t incoming; // Next block version for this blockid (singly-linked list) struct ujit_block_version *next;