Skip to content

Commit

Permalink
Prepare a JIT buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Mar 6, 2023
1 parent 9c2f612 commit baa120e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/ruby_vm/mjit/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
class RubyVM::MJIT::Compiler
C = RubyVM::MJIT.const_get(:C, false)
INSNS = RubyVM::MJIT.const_get(:INSNS, false)

def initialize = freeze
# @param mem_block [Integer] JIT buffer address
def initialize(mem_block)
@mem_block = mem_block
@write_pos = 0
end

# @param iseq [RubyVM::MJIT::CPointer::Struct]
def compile(iseq)
Expand Down
13 changes: 10 additions & 3 deletions mjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static VALUE rb_mMJIT = 0;
// RubyVM::MJIT::C
static VALUE rb_mMJITC = 0;
// RubyVM::MJIT::Compiler
static VALUE rb_cMJITCompiler = 0;
static VALUE rb_MJITCompiler = 0;
// RubyVM::MJIT::CPointer::Struct_rb_iseq_t
static VALUE rb_cMJITIseqPtr = 0;
// RubyVM::MJIT::CPointer::Struct_IC
Expand Down Expand Up @@ -371,14 +371,17 @@ mjit_compile(FILE *f, const rb_iseq_t *iseq, const char *funcname, int id)
// New stuff from here
//

// TODO: Make it configurable
#define MJIT_CODE_SIZE 16 * 1024 * 1024

void
rb_mjit_compile(const rb_iseq_t *iseq)
{
bool original_call_p = mjit_call_p;
mjit_call_p = false; // Avoid impacting JIT metrics by itself

VALUE iseq_ptr = rb_funcall(rb_cMJITIseqPtr, rb_intern("new"), 1, SIZET2NUM((size_t)iseq));
rb_funcall(rb_cMJITCompiler, rb_intern("compile"), 1, iseq_ptr);
rb_funcall(rb_MJITCompiler, rb_intern("compile"), 1, iseq_ptr);

mjit_call_p = original_call_p;
}
Expand All @@ -389,6 +392,9 @@ mjit_init(const struct mjit_options *opts)
VM_ASSERT(mjit_enabled);
mjit_opts = *opts;

extern uint8_t* rb_yjit_reserve_addr_space(uint32_t mem_size);
uint8_t *mem_block = rb_yjit_reserve_addr_space(MJIT_CODE_SIZE);

// MJIT doesn't support miniruby, but it might reach here by MJIT_FORCE_ENABLE.
rb_mMJIT = rb_const_get(rb_cRubyVM, rb_intern("MJIT"));
if (!rb_const_defined(rb_mMJIT, rb_intern("Compiler"))) {
Expand All @@ -397,7 +403,8 @@ mjit_init(const struct mjit_options *opts)
return;
}
rb_mMJITC = rb_const_get(rb_mMJIT, rb_intern("C"));
rb_cMJITCompiler = rb_funcall(rb_const_get(rb_mMJIT, rb_intern("Compiler")), rb_intern("new"), 0);
VALUE rb_cMJITCompiler = rb_const_get(rb_mMJIT, rb_intern("Compiler"));
rb_MJITCompiler = rb_funcall(rb_cMJITCompiler, rb_intern("new"), 1, SIZET2NUM((size_t)mem_block));
rb_cMJITIseqPtr = rb_funcall(rb_mMJITC, rb_intern("rb_iseq_t"), 0);

mjit_call_p = true;
Expand Down

0 comments on commit baa120e

Please sign in to comment.