Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Opt send new #544

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Draft: opt_new instruction
  • Loading branch information
byroot committed Nov 4, 2023
commit 84205f7cb01644a2e6657eb65effe49386803055
13 changes: 10 additions & 3 deletions compile.c
Original file line number Diff line number Diff line change
@@ -1442,7 +1442,7 @@ new_insn_body(rb_iseq_t *iseq, const NODE *const line_node, enum ruby_vminsn_typ
}

static const struct rb_callinfo *
new_callinfo(rb_iseq_t *iseq, ID mid, int argc, unsigned int flag, struct rb_callinfo_kwarg *kw_arg, int has_blockiseq)
new_callinfo(rb_iseq_t *iseq, ID mid, int argc, unsigned int flag, const struct rb_callinfo_kwarg *kw_arg, int has_blockiseq)
{
VM_ASSERT(argc >= 0);

@@ -3930,8 +3930,15 @@ iseq_specialized_instruction(rb_iseq_t *iseq, INSN *iobj)
}

if ((vm_ci_flag(ci) & VM_CALL_ARGS_BLOCKARG) == 0 && blockiseq == NULL) {
iobj->insn_id = BIN(opt_send_without_block);
iobj->operand_size = insn_len(iobj->insn_id) - 1;
switch (vm_ci_mid(ci)) {
case idNew:
iobj->insn_id = BIN(opt_new);
iobj->operands[1] = (VALUE)new_callinfo(iseq, idInitialize, vm_ci_argc(ci), vm_ci_flag(ci) | VM_CALL_FCALL, vm_ci_kwarg(ci), FALSE);
break;
default:
iobj->insn_id = BIN(opt_send_without_block);
iobj->operand_size = insn_len(iobj->insn_id) - 1;
}
}
}
#undef SP_INSN
1 change: 1 addition & 0 deletions defs/id.def
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ firstline, predefined = __LINE__+1, %[\
hash
freeze
nil?
new
inspect
intern
object_id
35 changes: 35 additions & 0 deletions insns.def
Original file line number Diff line number Diff line change
@@ -828,6 +828,41 @@ opt_send_without_block
}
}

/* Invoke constructor */
DEFINE_INSN
opt_new
(CALL_DATA cd, CALL_DATA cd_initialize)
(...)
(VALUE val)
// attr bool handles_sp = true;
// attr rb_snum_t sp_inc = sp_inc_of_sendish(cd->ci);
// attr rb_snum_t comptime_sp_inc = sp_inc_of_sendish(ci);
{
VALUE recv = TOPN(vm_ci_argc(cd->ci));

val = vm_opt_new_alloc(GET_ISEQ(), recv, cd);
if (val != Qundef) {
TOPN(vm_ci_argc(cd->ci)) = val;
fprintf(stderr, "opt!\n");
if (vm_sendish(ec, GET_CFP(), cd_initialize, VM_BLOCK_HANDLER_NONE, mexp_search_method) == Qundef) {
val = Qundef;
}
}

if (val == Qundef) {
fprintf(stderr, "de-opt!\n");

VALUE bh = VM_BLOCK_HANDLER_NONE;
val = vm_sendish(ec, GET_CFP(), cd, bh, mexp_search_method);
JIT_EXEC(ec, val);

if (val == Qundef) {
RESTORE_REGS();
NEXT_INSN();
}
}
}

/* Convert object to string using to_s or equivalent. */
DEFINE_INSN
objtostring
1 change: 1 addition & 0 deletions internal/object.h
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ int rb_bool_expected(VALUE, const char *, int raise);
static inline void RBASIC_CLEAR_CLASS(VALUE obj);
static inline void RBASIC_SET_CLASS_RAW(VALUE obj, VALUE klass);
static inline void RBASIC_SET_CLASS(VALUE obj, VALUE klass);
VALUE rb_class_alloc(VALUE klass);

RUBY_SYMBOL_EXPORT_BEGIN
/* object.c (export) */
2 changes: 1 addition & 1 deletion object.c
Original file line number Diff line number Diff line change
@@ -2063,7 +2063,7 @@ rb_class_alloc_m(VALUE klass)
return class_call_alloc_func(allocator, klass);
}

static VALUE
VALUE
rb_class_alloc(VALUE klass)
{
rb_alloc_func_t allocator = class_get_alloc_func(klass);
9 changes: 9 additions & 0 deletions vm_insnhelper.c
Original file line number Diff line number Diff line change
@@ -6114,6 +6114,15 @@ vm_opt_mod(VALUE recv, VALUE obj)
}
}

static VALUE
vm_opt_new_alloc(const rb_iseq_t *iseq, VALUE recv, CALL_DATA cd)
{
if (RB_TYPE_P(recv, T_CLASS) && vm_method_cfunc_is(iseq, cd, recv, rb_class_new_instance_pass_kw)) {
return rb_class_alloc(recv);
}
return Qundef;
}

static VALUE
vm_opt_neq(const rb_iseq_t *iseq, CALL_DATA cd, CALL_DATA cd_eq, VALUE recv, VALUE obj)
{