summaryrefslogtreecommitdiff
path: root/mjit.h
diff options
context:
space:
mode:
authork0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-04 06:58:09 (GMT)
committerk0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-02-04 06:58:09 (GMT)
commitfd44a5777fbca386a78cccff9aa02eca155b0752 (patch)
tree04b2f3ccf229322a3047969614423389153f6c41 /mjit.h
parentb2de4e0bee1cab74882dffd114a506cd67966c48 (diff)
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@redhat.com>. After he invented great method JIT infrastructure for MRI as MJIT, Lars Kanis <lars@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@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@redhat.com>. Contributors: Takashi Kokubun <takashikkbn@gmail.com>. wanabe <s.wanabe@gmail.com>. Lars Kanis <lars@greiz-reinsdorf.de>. Part of Feature 12589 and 14235. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'mjit.h')
-rw-r--r--mjit.h138
1 files changed, 138 insertions, 0 deletions
diff --git a/mjit.h b/mjit.h
new file mode 100644
index 0000000..452110e
--- /dev/null
+++ b/mjit.h
@@ -0,0 +1,138 @@
+/**********************************************************************
+
+ mjit.h - Interface to MRI method JIT compiler
+
+ Copyright (C) 2017 Vladimir Makarov <vmakarov@redhat.com>.
+
+**********************************************************************/
+
+#ifndef RUBY_MJIT_H
+#define RUBY_MJIT_H 1
+
+#include "ruby.h"
+
+/* Special address values of a function generated from the
+ corresponding iseq by MJIT: */
+enum rb_mjit_iseq_func {
+ /* ISEQ was not queued yet for the machine code generation */
+ NOT_ADDED_JIT_ISEQ_FUNC = 0,
+ /* ISEQ is already queued for the machine code generation but the
+ code is not ready yet for the execution */
+ NOT_READY_JIT_ISEQ_FUNC = 1,
+ /* ISEQ included not compilable insn or some assertion failed */
+ NOT_COMPILABLE_JIT_ISEQ_FUNC = 2,
+ /* End mark */
+ LAST_JIT_ISEQ_FUNC = 3,
+};
+
+/* C compiler used to generate native code. */
+enum rb_mjit_cc {
+ /* Not selected */
+ MJIT_CC_DEFAULT = 0,
+ /* GNU Compiler Collection */
+ MJIT_CC_GCC = 1,
+ /* LLVM/Clang */
+ MJIT_CC_CLANG = 2,
+};
+
+/* MJIT options which can be defined on the MRI command line. */
+struct mjit_options {
+ char on; /* flag of MJIT usage */
+ /* Default: clang for macOS, cl for Windows, gcc for others. */
+ enum rb_mjit_cc cc;
+ /* Save temporary files after MRI finish. The temporary files
+ include the pre-compiled header, C code file generated for ISEQ,
+ and the corresponding object file. */
+ char save_temps;
+ /* Print MJIT warnings to stderr. */
+ char warnings;
+ /* Disable compiler optimization and add debug symbols. It can be
+ very slow. */
+ char debug;
+ /* If not 0, all ISeqs are synchronously compiled. For testing. */
+ unsigned int wait;
+ /* Number of calls to trigger JIT compilation. For testing. */
+ unsigned int min_calls;
+ /* Force printing info about MJIT work of level VERBOSE or
+ less. 0=silence, 1=medium, 2=verbose. */
+ int verbose;
+ /* Maximal permitted number of iseq JIT codes in a MJIT memory
+ cache. */
+ int max_cache_size;
+};
+
+typedef VALUE (*mjit_func_t)(rb_execution_context_t *, rb_control_frame_t *);
+
+RUBY_SYMBOL_EXPORT_BEGIN
+extern struct mjit_options mjit_opts;
+extern int mjit_init_p;
+
+extern void mjit_add_iseq_to_process(const rb_iseq_t *iseq);
+extern mjit_func_t mjit_get_iseq_func(const struct rb_iseq_constant_body *body);
+RUBY_SYMBOL_EXPORT_END
+
+extern int mjit_compile(FILE *f, const struct rb_iseq_constant_body *body, const char *funcname);
+extern void mjit_init(struct mjit_options *opts);
+extern void mjit_finish(void);
+extern void mjit_gc_start_hook(void);
+extern void mjit_gc_finish_hook(void);
+extern void mjit_free_iseq(const rb_iseq_t *iseq);
+extern void mjit_mark(void);
+extern struct mjit_cont *mjit_cont_new(rb_execution_context_t *ec);
+extern void mjit_cont_free(struct mjit_cont *cont);
+
+/* A threshold used to reject long iseqs from JITting as such iseqs
+ takes too much time to be compiled. */
+#define JIT_ISEQ_SIZE_THRESHOLD 1000
+
+/* Return TRUE if given ISeq body should be compiled by MJIT */
+static inline int
+mjit_target_iseq_p(struct rb_iseq_constant_body *body)
+{
+ return (body->type == ISEQ_TYPE_METHOD || body->type == ISEQ_TYPE_BLOCK)
+ && body->iseq_size < JIT_ISEQ_SIZE_THRESHOLD;
+}
+
+/* Try to execute the current iseq in ec. Use JIT code if it is ready.
+ If it is not, add ISEQ to the compilation queue and return Qundef. */
+static inline VALUE
+mjit_exec(rb_execution_context_t *ec)
+{
+ const rb_iseq_t *iseq;
+ struct rb_iseq_constant_body *body;
+ long unsigned total_calls;
+ mjit_func_t func;
+
+ if (!mjit_init_p)
+ return Qundef;
+
+ iseq = ec->cfp->iseq;
+ body = iseq->body;
+ total_calls = ++body->total_calls;
+
+ func = body->jit_func;
+ if (UNLIKELY(mjit_opts.wait && mjit_opts.min_calls == total_calls && mjit_target_iseq_p(body)
+ && (enum rb_mjit_iseq_func)func == NOT_ADDED_JIT_ISEQ_FUNC)) {
+ mjit_add_iseq_to_process(iseq);
+ func = mjit_get_iseq_func(body);
+ }
+
+ if (UNLIKELY((ptrdiff_t)func <= (ptrdiff_t)LAST_JIT_ISEQ_FUNC)) {
+ switch ((enum rb_mjit_iseq_func)func) {
+ case NOT_ADDED_JIT_ISEQ_FUNC:
+ if (total_calls == mjit_opts.min_calls && mjit_target_iseq_p(body)) {
+ mjit_add_iseq_to_process(iseq);
+ }
+ return Qundef;
+ case NOT_READY_JIT_ISEQ_FUNC:
+ case NOT_COMPILABLE_JIT_ISEQ_FUNC:
+ return Qundef;
+ default: /* to avoid warning with LAST_JIT_ISEQ_FUNC */
+ break;
+ }
+ }
+
+ return func(ec, ec->cfp);
+}
+
+#endif /* RUBY_MJIT_H */