MJIT: Move Primitive helpers to mjit_c.rb

This commit is contained in:
Takashi Kokubun 2022-11-29 00:33:49 -08:00
parent 27f34eeb5a
commit f19deef323
2 changed files with 35 additions and 52 deletions

View File

@ -9,12 +9,25 @@ module RubyVM::MJIT
Primitive.cexpr! 'rb_hash_values((VALUE)NUM2PTR(cdhash_addr))'
end
def builtin_compiler(f, bf, index, stack_size, builtin_inline_p)
Primitive.builtin_compile(f, bf.to_i, index, stack_size, builtin_inline_p)
def builtin_compiler(buf, bf_ptr, index, stack_size, builtin_inline_p)
bf_addr = bf_ptr.to_i
# Call "mjit_compile_invokebuiltin_for_#{func}" in mk_builtin_loader.rb
Primitive.cstmt! %{
RB_BUILTIN bf = (RB_BUILTIN)NUM2PTR(bf_addr);
bf->compiler(buf, NIL_P(index) ? -1 : NUM2LONG(index), NUM2UINT(stack_size), RTEST(builtin_inline_p));
return Qnil;
}
end
def has_cache_for_send(cc, insn)
Primitive.has_cache_for_send(cc.to_i, insn)
def has_cache_for_send(cc_ptr, insn)
cc_addr = cc_ptr.to_i
Primitive.cstmt! %{
extern bool rb_vm_opt_cfunc_p(CALL_CACHE cc, int insn);
CALL_CACHE cc = (CALL_CACHE)NUM2PTR(cc_addr);
bool has_cache = vm_cc_cme(cc) != NULL &&
!(vm_cc_cme(cc)->def->type == VM_METHOD_TYPE_CFUNC && rb_vm_opt_cfunc_p(cc, NUM2INT(insn)));
return RBOOL(has_cache);
}
end
def rb_shape_get_shape_by_id(shape_id)
@ -57,11 +70,24 @@ module RubyVM::MJIT
}
end
def fastpath_applied_iseq_p(ci, cc, iseq)
_ci_addr = ci.to_i
_cc_addr = cc.to_i
_iseq_addr = iseq.to_i
Primitive.cexpr! 'RBOOL(fastpath_applied_iseq_p((CALL_INFO)NUM2PTR(_ci_addr), (CALL_CACHE)NUM2PTR(_cc_addr), (rb_iseq_t *)NUM2PTR(_iseq_addr)))'
# Returns true if iseq can use fastpath for setup, otherwise NULL. This becomes true in the same condition
# as CC_SET_FASTPATH (in vm_callee_setup_arg) is called from vm_call_iseq_setup.
def fastpath_applied_iseq_p(ci_ptr, cc_ptr, iseq_ptr)
ci_addr = ci_ptr.to_i
cc_addr = cc_ptr.to_i
iseq_addr = iseq_ptr.to_i
Primitive.cstmt! %q{
extern bool rb_simple_iseq_p(const rb_iseq_t *iseq);
CALL_INFO ci = (CALL_INFO)NUM2PTR(ci_addr);
CALL_CACHE cc = (CALL_CACHE)NUM2PTR(cc_addr);
const rb_iseq_t *iseq = (rb_iseq_t *)NUM2PTR(iseq_addr);
bool result = iseq != NULL
&& !(vm_ci_flag(ci) & VM_CALL_KW_SPLAT) && rb_simple_iseq_p(iseq) // Top of vm_callee_setup_arg. In this case, opt_pc is 0.
&& vm_ci_argc(ci) == (unsigned int)ISEQ_BODY(iseq)->param.lead_num // exclude argument_arity_error (assumption: `calling->argc == ci->orig_argc` in send insns)
&& vm_call_iseq_optimizable_p(ci, cc); // CC_SET_FASTPATH condition
return RBOOL(result);
}
end
def mjit_opts

View File

@ -27,25 +27,6 @@
#include "insns.inc"
#include "insns_info.inc"
// Returns true if call cache is still not obsoleted and vm_cc_cme(cc)->def->type is available.
static bool
has_valid_method_type(CALL_CACHE cc)
{
return vm_cc_cme(cc) != NULL;
}
// Returns true if iseq can use fastpath for setup, otherwise NULL. This becomes true in the same condition
// as CC_SET_FASTPATH (in vm_callee_setup_arg) is called from vm_call_iseq_setup.
static bool
fastpath_applied_iseq_p(const CALL_INFO ci, const CALL_CACHE cc, const rb_iseq_t *iseq)
{
extern bool rb_simple_iseq_p(const rb_iseq_t *iseq);
return iseq != NULL
&& !(vm_ci_flag(ci) & VM_CALL_KW_SPLAT) && rb_simple_iseq_p(iseq) // Top of vm_callee_setup_arg. In this case, opt_pc is 0.
&& vm_ci_argc(ci) == (unsigned int)ISEQ_BODY(iseq)->param.lead_num // exclude argument_arity_error (assumption: `calling->argc == ci->orig_argc` in send insns)
&& vm_call_iseq_optimizable_p(ci, cc); // CC_SET_FASTPATH condition
}
#include "mjit_compile_attr.inc"
#if SIZEOF_LONG == SIZEOF_VOIDP
@ -76,30 +57,6 @@ mjit_compile(FILE *f, const rb_iseq_t *iseq, const char *funcname, int id)
return !NIL_P(src);
}
//
// Primitive.methods
//
static VALUE
builtin_compile(rb_execution_context_t *ec, VALUE self, VALUE buf, VALUE bf_addr, VALUE index, VALUE stack_size, VALUE builtin_inline_p)
{
// Call "mjit_compile_invokebuiltin_for_#{func}" in mk_builtin_loader.rb
RB_BUILTIN bf = (RB_BUILTIN)NUM2PTR(bf_addr);
bf->compiler(buf, NIL_P(index) ? -1 : NUM2LONG(index), NUM2UINT(stack_size), RTEST(builtin_inline_p));
return Qnil;
}
// Returns true if MJIT thinks this cc's opt_* insn may fallback to opt_send_without_block.
static VALUE
has_cache_for_send(rb_execution_context_t *ec, VALUE self, VALUE cc_addr, VALUE insn)
{
extern bool rb_vm_opt_cfunc_p(CALL_CACHE cc, int insn);
CALL_CACHE cc = (CALL_CACHE)NUM2PTR(cc_addr);
bool has_cache = has_valid_method_type(cc) &&
!(vm_cc_cme(cc)->def->type == VM_METHOD_TYPE_CFUNC && rb_vm_opt_cfunc_p(cc, NUM2INT(insn)));
return RBOOL(has_cache);
}
// An offsetof implementation that works for unnamed struct and union.
// Multiplying 8 for compatibility with libclang's offsetof.
#define OFFSETOF(ptr, member) RB_SIZE2NUM(((char *)&ptr.member - (char*)&ptr) * 8)