Invalidate everything on GC.compact

This commit is contained in:
Takashi Kokubun 2023-02-24 13:19:42 -08:00
parent 44c4a2d80d
commit 63d96ccbcd
Notes: git 2023-03-06 07:29:54 +00:00
5 changed files with 62 additions and 29 deletions

2
iseq.c
View File

@ -357,7 +357,7 @@ rb_iseq_mark_and_move(rb_iseq_t *iseq, bool reference_updating)
if (reference_updating) {
#if USE_MJIT
mjit_update_references(iseq);
rb_mjit_iseq_update_references(iseq);
#endif
#if USE_YJIT
rb_yjit_iseq_update_references(body->yjit_payload);

View File

@ -26,5 +26,9 @@ module RubyVM::MJIT
def self.on_tracing_invalidate_all(_new_iseq_events)
Invariants.on_tracing_invalidate_all
end
def self.on_update_references
Invariants.on_update_references
end
end
end

View File

@ -78,26 +78,11 @@ module RubyVM::MJIT
end
def on_tracing_invalidate_all
# On-Stack Replacement
@patches.each do |address, target|
# TODO: assert patches don't overlap each other
@cb.with_write_addr(address) do
asm = Assembler.new
asm.comment('on_tracing_invalidate_all')
asm.jmp(target)
@cb.write(asm)
end
end
invalidate_all
end
# Avoid reusing past code
Compiler.reset_blocks
C.mjit_for_each_iseq do |iseq|
# Disable entering past code
iseq.body.jit_func = 0
# Compile this again if not converted to trace_* insns
iseq.body.total_calls = 0
end
def on_update_references
invalidate_all
end
# @param jit [RubyVM::MJIT::JITState]
@ -111,6 +96,32 @@ module RubyVM::MJIT
end
end
end
private
def invalidate_all
# On-Stack Replacement
@patches.each do |address, target|
# TODO: assert patches don't overlap each other
@cb.with_write_addr(address) do
asm = Assembler.new
asm.comment('on_tracing_invalidate_all')
asm.jmp(target)
@cb.write(asm)
end
end
@patches.clear
# Avoid reusing past code
Compiler.reset_blocks
C.mjit_for_each_iseq do |iseq|
# Disable entering past code
iseq.body.jit_func = 0
# Compile this again if not converted to trace_* insns
iseq.body.total_calls = 0
end
end
end
end
end

34
mjit.c
View File

@ -113,12 +113,6 @@ mjit_cancel_all(const char *reason)
}
}
void
mjit_update_references(const rb_iseq_t *iseq)
{
// TODO: remove this
}
void
mjit_free_iseq(const rb_iseq_t *iseq)
{
@ -348,12 +342,13 @@ mjit_cme_invalidate(void *data)
});
}
extern int rb_workqueue_register(unsigned flags, rb_postponed_job_func_t func, void *data);
void
rb_mjit_cme_invalidate(rb_callable_method_entry_t *cme)
{
if (!mjit_enabled || !mjit_call_p || !rb_mMJITHooks) return;
// Asynchronously hook the Ruby code since running Ruby in the middle of cme invalidation is dangerous.
extern int rb_workqueue_register(unsigned flags, rb_postponed_job_func_t func, void *data);
rb_workqueue_register(0, mjit_cme_invalidate, (void *)cme);
}
@ -389,6 +384,28 @@ rb_mjit_tracing_invalidate_all(rb_event_flag_t new_iseq_events)
});
}
static void
mjit_iseq_update_references(void *data)
{
if (!mjit_enabled || !mjit_call_p || !rb_mMJITHooks) return;
WITH_MJIT_ISOLATED({
rb_funcall(rb_mMJITHooks, rb_intern("on_update_references"), 0);
});
}
void
rb_mjit_iseq_update_references(const rb_iseq_t *iseq)
{
if (!mjit_enabled) return;
// TODO: update mjit_blocks
// Asynchronously hook the Ruby code to avoid allocation during GC.compact.
// Using _one because it's too slow to invalidate all for each ISEQ. Thus
// not giving an ISEQ pointer.
rb_postponed_job_register_one(0, mjit_iseq_update_references, NULL);
}
// TODO: Use this in more places
VALUE
rb_mjit_iseq_new(rb_iseq_t *iseq)
@ -442,10 +459,11 @@ mjit_mark(void)
return;
RUBY_MARK_ENTER("mjit");
// Mark objects used by the MJIT compiler
// Pin object pointers used in this file
rb_gc_mark(rb_MJITCompiler);
rb_gc_mark(rb_cMJITIseqPtr);
rb_gc_mark(rb_cMJITCfpPtr);
rb_gc_mark(rb_mMJITHooks);
RUBY_MARK_LEAVE("mjit");
}

2
mjit.h
View File

@ -106,7 +106,7 @@ extern void mjit_cancel_all(const char *reason);
extern bool mjit_compile(FILE *f, const rb_iseq_t *iseq, const char *funcname, int id);
extern void mjit_init(const struct mjit_options *opts);
extern void mjit_free_iseq(const rb_iseq_t *iseq);
extern void mjit_update_references(const rb_iseq_t *iseq);
extern void rb_mjit_iseq_update_references(const rb_iseq_t *iseq);
extern void mjit_mark(void);
extern void mjit_mark_cc_entries(const struct rb_iseq_constant_body *const body);
extern void mjit_notify_waitpid(int exit_code);