[ruby-cvs:69370] k0kubun:r62189 (trunk): mjit.c: merge MJIT infrastructure

k0kubun at ruby-lang.org k0kubun at ruby-lang.org
Sun Feb 4 15:58:10 JST 2018


k0kubun	2018-02-04 15:58:09 +0900 (Sun, 04 Feb 2018)

  New Revision: 62189

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62189

  Log:
    mjit.c: merge MJIT infrastructure
    
    that allows to JIT-compile Ruby methods by generating C code and
    using C compiler.  See the first comment of mjit.c to know what this
    file does.
    
    mjit.c is authored by Vladimir Makarov <vmakarov at redhat.com>.
    After he invented great method JIT infrastructure for MRI as MJIT,
    Lars Kanis <lars at greiz-reinsdorf.de> sent the patch to support MinGW
    in MJIT. In addition to merging it, I ported pthread to Windows native
    threads. Now this MJIT infrastructure can be compiled on Visual Studio.
    
    This commit simplifies mjit.c to decrease code at initial merge. For
    example, this commit does not provide multiple JIT threads support.
    We can resurrect them later if we really want them, but I wanted to minimize
    diff to make it easier to review this patch.
    
    `/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
    developers may not know the name "mjit" and the file name should make
    sure it's from Ruby and not from some harmful programs.  TODO: it may be
    better to store this to some temporary directory which Ruby is already using
    by Tempfile, if it's not bad for performance.
    
    mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
    for triggering MJIT. This drops interface for AOT compared to the original
    MJIT.
    
    Makefile.in: define macros to let MJIT know the path of MJIT header.
    Probably we can refactor this to reduce the number of macros (TODO).
    win32/Makefile.sub: ditto.
    
    common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
    commit separates MJIT infrastructure and JIT compiler code as independent
    object files. As initial patch is NOT going to have ultra-fast JIT compiler,
    it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
    future JIT impelementations which are not public now.
    
    inits.c: define MJIT module. This is added because `MJIT.enabled?` was
    necessary for testing.
    test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
    wouldn't work with current code when JIT is enabled.
    test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
    
    ruby.c: define MJIT CLI options. As major difference from original MJIT,
    "-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
    not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
    takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
    options are allowed since some Ruby committers preferred it at Ruby
    developers Meeting on January, and some of options are renamed.
    This file also triggers to initialize MJIT thread and variables.
    eval.c: finalize MJIT worker thread and variables.
    test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
    
    thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
    functions which are used by other files.
    thread_win32.c: ditto, for Windows.  Those pthread porting is one of major
    works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
    thread.c: follow rb_ prefix changes
    
    vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
    SEGV by race between JIT and GC of ISeq. The improvement was provided by
    wanabe <s.wanabe at gmail.com>.
    In JIT compiler I created and am going to add in my next commit, I found
    that having `mjit_exec` after `vm_loop_start:` is harmful because the
    JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
    Executing non-FINISH frame is unexpected for my JIT compiler and
    `exception_handler` triggers executions of such ISeqs. So `mjit_exec`
    here should be executed only when it directly comes from `vm_exec` call.
    `RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
    some tests which don't expect JIT threads or compiler file descriptors.
    
    vm_insnhelper.h: trigger MJIT on method calls during VM execution.
    
    vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
    rb_control_frame_struct is likely to be casted to another struct. The
    last position is the safest place to add the new field.
    vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
    optimization which are done in both MJIT and YARV-MJIT. So this change
    is added in this commit. Calculating bp from ep is a little heavy work,
    so bp is kind of cache for it.
    
    iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
    is GCed to avoid SEGV.  TODO: unload some GCed units in some safe way.
    
    gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
    JIT and GC executions may cause SEGV and so we should synchronize them.
    
    cont.c: save continuation information in MJIT worker. As MJIT shouldn't
    unload JIT-ed code which is being used, MJIT wants to know full list of
    saved execution contexts for continuation and detect ISeqs in use.
    
    mjit_compile.c: added empty JIT compiler so that you can reuse this commit
    to build your own JIT compiler. This commit tries to compile ISeqs but
    all of them are considered as not supported in this commit. So you can't
    use JIT compiler in this commit yet while we added --jit option now.
    
    Patch author: Vladimir Makarov <vmakarov at redhat.com>.
    
    Contributors:
    Takashi Kokubun <takashikkbn at gmail.com>.
    wanabe <s.wanabe at gmail.com>.
    Lars Kanis <lars at greiz-reinsdorf.de>.
    
    Part of Feature 12589 and 14235.

  Added files:
    trunk/mjit.c
    trunk/mjit.h
    trunk/mjit_compile.c
  Modified files:
    trunk/Makefile.in
    trunk/common.mk
    trunk/cont.c
    trunk/eval.c
    trunk/gc.c
    trunk/iseq.c
    trunk/ruby.c
    trunk/test/lib/zombie_hunter.rb
    trunk/test/ruby/test_io.rb
    trunk/test/ruby/test_rubyoptions.rb
    trunk/thread.c
    trunk/thread_pthread.c
    trunk/thread_win32.c
    trunk/vm.c
    trunk/vm_core.h
    trunk/vm_insnhelper.h
    trunk/win32/Makefile.sub


More information about the ruby-cvs mailing list