/* Called to start OSR. Switches us over to logging runs of spesh'd code, to * collect extra type info. */ void MVM_spesh_osr(MVMThreadContext *tc) { MVMSpeshCandidate *specialized; MVMint32 osr_index; /* Check OSR is enabled. */ if (!tc->instance->spesh_osr_enabled) return; /* Ensure that we are in a position to specialize. */ if (!tc->cur_frame->caller) return; if (!tc->cur_frame->params.callsite->is_interned) return; if (tc->cur_frame->static_info->body.num_spesh_candidates == MVM_SPESH_LIMIT) return; /* Produce logging spesh candidate. */ specialized = MVM_spesh_candidate_setup(tc, tc->cur_frame->static_info, tc->cur_frame->params.callsite, tc->cur_frame->params.args, 1); if (specialized) { /* Set up frame to point to specialized logging code. */ tc->cur_frame->effective_bytecode = specialized->bytecode; tc->cur_frame->effective_handlers = specialized->handlers; tc->cur_frame->effective_spesh_slots = specialized->spesh_slots; tc->cur_frame->spesh_log_slots = specialized->log_slots; tc->cur_frame->spesh_cand = specialized; tc->cur_frame->spesh_log_idx = 0; specialized->log_enter_idx = 1; /* Work out deopt index that applies, and move interpreter into the * logging version of the code. */ osr_index = get_osr_deopt_index(tc, specialized); *(tc->interp_bytecode_start) = specialized->bytecode; *(tc->interp_cur_op) = specialized->bytecode + specialized->deopts[2 * osr_index + 1] + 2; /* Pass over sp_osrfianlize this first time */; } }
/* Does the jump into the optimized code. */ void perform_osr(MVMThreadContext *tc, MVMSpeshCandidate *specialized) { MVMJitCode *jit_code; MVMint32 num_locals; /* Work out the OSR deopt index, to locate the entry point. */ MVMint32 osr_index = get_osr_deopt_index(tc, specialized); #if MVM_LOG_OSR fprintf(stderr, "Performing OSR of frame '%s' (cuid: %s) at index %d\n", MVM_string_utf8_encode_C_string(tc, tc->cur_frame->static_info->body.name), MVM_string_utf8_encode_C_string(tc, tc->cur_frame->static_info->body.cuuid), osr_index); #endif jit_code = specialized->jitcode; num_locals = jit_code && jit_code->local_types ? jit_code->num_locals : specialized->num_locals; /* Resize work area if needed. */ if (specialized->work_size > tc->cur_frame->allocd_work) { /* Resize work area. */ MVMRegister *new_work = MVM_fixed_size_alloc_zeroed(tc, tc->instance->fsa, specialized->work_size); MVMRegister *new_args = new_work + num_locals; memcpy(new_work, tc->cur_frame->work, tc->cur_frame->static_info->body.num_locals * sizeof(MVMRegister)); memcpy(new_args, tc->cur_frame->args, tc->cur_frame->static_info->body.cu->body.max_callsite_size * sizeof(MVMRegister)); MVM_fixed_size_free(tc, tc->instance->fsa, tc->cur_frame->allocd_work, tc->cur_frame->work); tc->cur_frame->work = new_work; tc->cur_frame->allocd_work = specialized->work_size; tc->cur_frame->args = new_args; #if MVM_LOG_OSR fprintf(stderr, "OSR resized work area of frame '%s' (cuid: %s)\n", MVM_string_utf8_encode_C_string(tc, tc->cur_frame->static_info->body.name), MVM_string_utf8_encode_C_string(tc, tc->cur_frame->static_info->body.cuuid)); #endif } else if (specialized->work_size > tc->cur_frame->static_info->body.work_size) { size_t keep_bytes = tc->cur_frame->static_info->body.num_locals * sizeof(MVMRegister); size_t to_null = specialized->work_size - keep_bytes; memset((char *)tc->cur_frame->work + keep_bytes, 0, to_null); } /* Resize environment if needed. */ if (specialized->num_lexicals > tc->cur_frame->static_info->body.num_lexicals) { MVMRegister *new_env = MVM_fixed_size_alloc_zeroed(tc, tc->instance->fsa, specialized->env_size); if (tc->cur_frame->allocd_env) { memcpy(new_env, tc->cur_frame->env, tc->cur_frame->static_info->body.num_lexicals * sizeof(MVMRegister)); MVM_fixed_size_free(tc, tc->instance->fsa, tc->cur_frame->allocd_env, tc->cur_frame->env); } tc->cur_frame->env = new_env; tc->cur_frame->allocd_env = specialized->env_size; #if MVM_LOG_OSR fprintf(stderr, "OSR resized environment of frame '%s' (cuid: %s)\n", MVM_string_utf8_encode_C_string(tc, tc->cur_frame->static_info->body.name), MVM_string_utf8_encode_C_string(tc, tc->cur_frame->static_info->body.cuuid)); #endif } else if (specialized->env_size > tc->cur_frame->static_info->body.env_size) { size_t keep_bytes = tc->cur_frame->static_info->body.num_lexicals * sizeof(MVMRegister); size_t to_null = specialized->env_size - keep_bytes; memset((char *)tc->cur_frame->env + keep_bytes, 0, to_null); } /* Set up frame to point to spesh candidate/slots. */ tc->cur_frame->effective_spesh_slots = specialized->spesh_slots; tc->cur_frame->spesh_cand = specialized; /* Move into the optimized (and maybe JIT-compiled) code. */ if (jit_code && jit_code->num_deopts) { MVMint32 i; *(tc->interp_bytecode_start) = jit_code->bytecode; *(tc->interp_cur_op) = jit_code->bytecode; for (i = 0; i < jit_code->num_deopts; i++) { if (jit_code->deopts[i].idx == osr_index) { tc->cur_frame->jit_entry_label = jit_code->labels[jit_code->deopts[i].label]; break; } } if (i == jit_code->num_deopts) MVM_oops(tc, "JIT: Could not find OSR label"); if (tc->instance->profiling) MVM_profiler_log_osr(tc, 1); } else { *(tc->interp_bytecode_start) = specialized->bytecode; *(tc->interp_cur_op) = specialized->bytecode + specialized->deopts[2 * osr_index + 1]; if (tc->instance->profiling) MVM_profiler_log_osr(tc, 0); } *(tc->interp_reg_base) = tc->cur_frame->work; }