guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 02/06: Add instrument-call, instrument-loop VM instructi


From: Andy Wingo
Subject: [Guile-commits] 02/06: Add instrument-call, instrument-loop VM instructions
Date: Sun, 29 Jul 2018 10:10:57 -0400 (EDT)

wingo pushed a commit to branch lightning
in repository guile.

commit 87da1c8d20591a9a7dad28eb106161c4f508a2b6
Author: Andy Wingo <address@hidden>
Date:   Mon Jul 23 12:43:30 2018 +0200

    Add instrument-call, instrument-loop VM instructions
    
    * libguile/jit.h (struct scm_jit_function_data)
      (enum scm_jit_counter_value): New data types.
    * libguile/jit.c (scm_jit_compute_mcode, scm_jit_enter_mcode): New
      function stubs.  Adapt label/offset compilers to take pointers.
    * libguile/vm-engine.c (instrument-call, instrument-loop): New
      instructions.
    * libguile/vm.c: Add jit.h include.
    * module/system/vm/assembler.scm (emit-instrument-call)
      (emit-instrument-loop): New exports.
---
 libguile/jit.c                 | 82 +++++++++++++++++++++++++++---------------
 libguile/jit.h                 | 29 +++++++++++++++
 libguile/vm-engine.c           | 68 +++++++++++++++++++++++++++++++----
 libguile/vm.c                  |  1 +
 module/system/vm/assembler.scm |  2 ++
 5 files changed, 147 insertions(+), 35 deletions(-)

diff --git a/libguile/jit.c b/libguile/jit.c
index 04e2810..92df1d6 100644
--- a/libguile/jit.c
+++ b/libguile/jit.c
@@ -34,12 +34,12 @@
 typedef struct {
   scm_thread *thread;
   const uint32_t *start;
-  const uint32_t *ip;
+  uint32_t *ip;
   const uint32_t *end;
 } scm_jit_state;
 
 static void
-bad_instruction(scm_jit_state *j)
+bad_instruction (scm_jit_state *j)
 {
   abort ();
 }
@@ -56,7 +56,7 @@ compile_call (scm_jit_state *j, uint32_t a, uint32_t b)
 }
 
 static void
-compile_call_label (scm_jit_state *j, uint32_t a, uint32_t b, int32_t offset)
+compile_call_label (scm_jit_state *j, uint32_t a, uint32_t b, const uint32_t 
*vcode)
 {
 }
 
@@ -66,7 +66,17 @@ compile_tail_call (scm_jit_state *j)
 }
 
 static void
-compile_tail_call_label (scm_jit_state *j, int32_t offset)
+compile_tail_call_label (scm_jit_state *j, const uint32_t *vcode)
+{
+}
+
+static void
+compile_instrument_call (scm_jit_state *j, void *data)
+{
+}
+
+static void
+compile_instrument_loop (scm_jit_state *j, void *data)
 {
 }
 
@@ -131,12 +141,12 @@ compile_throw (scm_jit_state *j, uint16_t a, uint16_t b)
 }
 
 static void
-compile_throw_value (scm_jit_state *j, uint32_t a, int32_t offset)
+compile_throw_value (scm_jit_state *j, uint32_t a, const void *data)
 {
 }
 
 static void
-compile_throw_value_and_data (scm_jit_state *j, uint32_t a, int32_t offset)
+compile_throw_value_and_data (scm_jit_state *j, uint32_t a, const void *data)
 {
 }
 
@@ -191,7 +201,7 @@ compile_expand_apply_argument (scm_jit_state *j)
 }
 
 static void
-compile_bind_kwargs (scm_jit_state *j, uint32_t a, uint8_t b, uint32_t c, 
uint32_t d, int32_t offset)
+compile_bind_kwargs (scm_jit_state *j, uint32_t a, uint8_t b, uint32_t c, 
uint32_t d, const void *data)
 {
 }
 
@@ -336,32 +346,32 @@ compile_make_long_long_immediate (scm_jit_state *j, 
uint32_t dst, SCM a)
 }
 
 static void
-compile_make_non_immediate (scm_jit_state *j, uint32_t dst, int32_t offset)
+compile_make_non_immediate (scm_jit_state *j, uint32_t dst, const void *data)
 {
 }
 
 static void
-compile_static_ref (scm_jit_state *j, uint32_t dst, int32_t offset)
+compile_static_ref (scm_jit_state *j, uint32_t dst, void *loc)
 {
 }
 
 static void
-compile_static_set (scm_jit_state *j, uint32_t a, int32_t offset)
+compile_static_set (scm_jit_state *j, uint32_t a, void *loc)
 {
 }
 
 static void
-compile_static_patch (scm_jit_state *j, int32_t a, int32_t b)
+compile_static_patch (scm_jit_state *j, void *dst, const void *src)
 {
 }
 
 static void
-compile_prompt (scm_jit_state *j, uint32_t a, uint8_t b, uint32_t c, int32_t 
offset)
+compile_prompt (scm_jit_state *j, uint32_t a, uint8_t b, uint32_t c, const 
uint32_t *vcode)
 {
 }
 
 static void
-compile_load_label (scm_jit_state *j, uint32_t dst, int32_t offset)
+compile_load_label (scm_jit_state *j, uint32_t dst, const uint32_t *vcode)
 {
 }
 
@@ -626,37 +636,37 @@ compile_eq (scm_jit_state *j, uint16_t a, uint16_t b)
 }
 
 static void
-compile_j (scm_jit_state *j, int32_t offset)
+compile_j (scm_jit_state *j, const uint32_t *vcode)
 {
 }
 
 static void
-compile_jl (scm_jit_state *j, int32_t offset)
+compile_jl (scm_jit_state *j, const uint32_t *vcode)
 {
 }
 
 static void
-compile_je (scm_jit_state *j, int32_t offset)
+compile_je (scm_jit_state *j, const uint32_t *vcode)
 {
 }
 
 static void
-compile_jnl (scm_jit_state *j, int32_t offset)
+compile_jnl (scm_jit_state *j, const uint32_t *vcode)
 {
 }
 
 static void
-compile_jne (scm_jit_state *j, int32_t offset)
+compile_jne (scm_jit_state *j, const uint32_t *vcode)
 {
 }
 
 static void
-compile_jge (scm_jit_state *j, int32_t offset)
+compile_jge (scm_jit_state *j, const uint32_t *vcode)
 {
 }
 
 static void
-compile_jnge (scm_jit_state *j, int32_t offset)
+compile_jnge (scm_jit_state *j, const uint32_t *vcode)
 {
 }
 
@@ -903,7 +913,7 @@ compile_f64_set (scm_jit_state *j, uint8_t a, uint8_t b, 
uint8_t c)
   {                                                                     \
     int32_t a = j->ip[0];                                               \
     a >>= 8; /* Sign extension.  */                                     \
-    comp (j, a);                                                        \
+    comp (j, j->ip + a);                                                \
     j->ip += 1;                                                         \
   }
 #define COMPILE_X8_C12_C12(j, comp)                                     \
@@ -958,9 +968,11 @@ compile_f64_set (scm_jit_state *j, uint8_t a, uint8_t b, 
uint8_t c)
 #define COMPILE_X32__L32(j, comp)                                       \
   {                                                                     \
     int32_t a = j->ip[1];                                               \
-    comp (j, a);                                                        \
-    j->ip += 1;                                                         \
+    comp (j, j->ip + a);                                                \
+    j->ip += 2;                                                         \
   }
+#define COMPILE_X32__N32(j, comp)                                       \
+  COMPILE_X32__L32 (j, comp)
 
 #define COMPILE_X8_C24__L32(j, comp)                                    \
   {                                                                     \
@@ -968,7 +980,7 @@ compile_f64_set (scm_jit_state *j, uint8_t a, uint8_t b, 
uint8_t c)
     int32_t b;                                                          \
     UNPACK_24 (j->ip[0], a);                                            \
     b = j->ip[1];                                                       \
-    comp (j, a, b);                                                     \
+    comp (j, a, j->ip + b);                                             \
     j->ip += 2;                                                         \
   }
 #define COMPILE_X8_S24__L32(j, comp)                                    \
@@ -1070,7 +1082,7 @@ compile_f64_set (scm_jit_state *j, uint8_t a, uint8_t b, 
uint8_t c)
 #define COMPILE_X32__LO32__L32(j, comp)                                 \
   {                                                                     \
     int32_t a = j->ip[1], b = j->ip[2];                                 \
-    comp (j, a, b);                                                     \
+    comp (j, j->ip + a, j->ip + b);                                     \
     j->ip += 3;                                                         \
   }
 
@@ -1081,7 +1093,7 @@ compile_f64_set (scm_jit_state *j, uint8_t a, uint8_t b, 
uint8_t c)
     UNPACK_24 (j->ip[0], a);                                            \
     UNPACK_24 (j->ip[1], b);                                            \
     c = j->ip[2];                                                       \
-    comp (j, a, b, c);                                                  \
+    comp (j, a, b, j->ip + c);                                          \
     j->ip += 3;                                                         \
   }
 
@@ -1135,7 +1147,7 @@ compile_f64_set (scm_jit_state *j, uint8_t a, uint8_t b, 
uint8_t c)
     b = j->ip[1] & 0x1;                                                 \
     UNPACK_24 (j->ip[1], c);                                            \
     d = j->ip[2]; d >>= 8; /* Sign extension.  */                       \
-    comp (j, a, b, c, d);                                               \
+    comp (j, a, b, c, j->ip + d);                                       \
     j->ip += 3;                                                         \
   }
 
@@ -1159,7 +1171,7 @@ compile_f64_set (scm_jit_state *j, uint8_t a, uint8_t b, 
uint8_t c)
     UNPACK_8_24 (j->ip[1], b, c);                                       \
     UNPACK_24 (j->ip[2], d);                                            \
     e = j->ip[3]; e >>= 8; /* Sign extension.  */                       \
-    comp (j, a, b, c, d, e);                                            \
+    comp (j, a, b, c, d, j->ip + e);                                    \
     j->ip += 4;                                                         \
   }
 
@@ -1192,11 +1204,23 @@ compile1 (scm_jit_state *j)
 static void
 compile (scm_jit_state *j)
 {
-  j->ip = j->start;
+  j->ip = (uint32_t *) j->start;
   while (j->ip < j->end)
     compile1 (j);
 }
 
+const uint8_t *
+scm_jit_compute_mcode (scm_thread *thread, struct scm_jit_function_data *data)
+{
+  return NULL;
+}
+
+void
+scm_jit_enter_mcode (scm_thread *thread, const uint8_t *mcode)
+{
+  abort ();
+}
+
 void
 scm_init_jit (void)
 {
diff --git a/libguile/jit.h b/libguile/jit.h
index 1c27e86..5067b61 100644
--- a/libguile/jit.h
+++ b/libguile/jit.h
@@ -26,6 +26,35 @@
 
 
 
+struct scm_jit_function_data;
+
+#ifdef BUILDING_LIBGUILE
+struct scm_jit_function_data
+{
+  uint8_t *mcode;
+  uint32_t counter;
+  uint32_t start;
+  uint32_t end;
+#if SCM_SIZEOF_UINTPTR_T == 4
+#elif SCM_SIZEOF_UINTPTR_T == 8
+  uint32_t pad;
+#else
+#error unhandled sizeof(uintptr_t)
+#endif
+};
+
+enum scm_jit_counter_value
+{
+  SCM_JIT_COUNTER_CALL_INCREMENT = 15,
+  SCM_JIT_COUNTER_LOOP_INCREMENT = 1,
+  SCM_JIT_COUNTER_THRESHOLD = 50
+};
+#endif
+
+SCM_INTERNAL const uint8_t *scm_jit_compute_mcode (scm_thread *thread,
+                                                   struct 
scm_jit_function_data *data);
+SCM_INTERNAL void scm_jit_enter_mcode (scm_thread *thread, const uint8_t 
*mcode);
+
 SCM_INTERNAL void scm_init_jit (void);
 
 #endif  /* SCM_JIT_H */
diff --git a/libguile/vm-engine.c b/libguile/vm-engine.c
index fc6978e..1b0647e 100644
--- a/libguile/vm-engine.c
+++ b/libguile/vm-engine.c
@@ -468,10 +468,38 @@ VM_NAME (scm_thread *thread)
       NEXT (0);
     }
 
-  VM_DEFINE_OP (5, unused_5, NULL, NOP)
+  /* instrument-call _:24 data:32
+   *
+   * Increase execution counter for this function and potentially tier
+   * up to the next JIT level.  DATA is an offset to raw profiler,
+   * recording execution counts and the next-level JIT code
+   * corresponding to this function.  Also run the apply hook.
+   */
+  VM_DEFINE_OP (5, instrument_call, "instrument-call", OP2 (X32, N32))
     {
-      vm_error_bad_instruction (op);
-      abort (); /* never reached */
+      int32_t data_offset = ip[1];
+      struct scm_jit_function_data *data;
+
+      data = (struct scm_jit_function_data *) (ip + data_offset);
+
+      if (data->counter > SCM_JIT_COUNTER_THRESHOLD)
+        {
+          const uint8_t *mcode;
+
+          SYNC_IP ();
+          mcode = scm_jit_compute_mcode (thread, data);
+
+          if (mcode)
+            {
+              scm_jit_enter_mcode (thread, mcode);
+              CACHE_REGISTER ();
+              NEXT (0);
+            }
+        }
+      else
+        data->counter += SCM_JIT_COUNTER_CALL_INCREMENT;
+
+      NEXT (2);
     }
 
   /* receive dst:12 proc:12 _:8 nlocals:24
@@ -675,10 +703,38 @@ VM_NAME (scm_thread *thread)
       NEXT (0);
     }
 
-  VM_DEFINE_OP (14, unused_14, NULL, NOP)
+  /* instrument-loop _:24 data:32
+   *
+   * Increase execution counter for this function and potentially tier
+   * up to the next JIT level.  DATA is an offset to raw profiler,
+   * recording execution counts and the next-level JIT code
+   * corresponding to this function.
+   */
+  VM_DEFINE_OP (14, instrument_loop, "instrument-loop", OP2 (X32, N32))
     {
-      vm_error_bad_instruction (op);
-      abort (); /* never reached */
+      int32_t data_offset = ip[1];
+      struct scm_jit_function_data *data;
+
+      data = (struct scm_jit_function_data *) (ip + data_offset);
+
+      if (data->counter > SCM_JIT_COUNTER_THRESHOLD)
+        {
+          const uint8_t *mcode;
+
+          SYNC_IP ();
+          mcode = scm_jit_compute_mcode (thread, data);
+
+          if (mcode)
+            {
+              scm_jit_enter_mcode (thread, mcode);
+              CACHE_REGISTER ();
+              NEXT (0);
+            }
+        }
+      else
+        data->counter += SCM_JIT_COUNTER_LOOP_INCREMENT;
+
+      NEXT (2);
     }
 
   /* call/cc _:24
diff --git a/libguile/vm.c b/libguile/vm.c
index bc15bae..d1e5d51 100644
--- a/libguile/vm.c
+++ b/libguile/vm.c
@@ -53,6 +53,7 @@
 #include "hooks.h"
 #include "instructions.h"
 #include "intrinsics.h"
+#include "jit.h"
 #include "keywords.h"
 #include "list.h"
 #include "loader.h"
diff --git a/module/system/vm/assembler.scm b/module/system/vm/assembler.scm
index 66d1a50..678abb3 100644
--- a/module/system/vm/assembler.scm
+++ b/module/system/vm/assembler.scm
@@ -236,6 +236,8 @@
             emit-call-label
             emit-tail-call
             emit-tail-call-label
+            emit-instrument-call
+            emit-instrument-loop
             emit-receive-values
             emit-return-values
             emit-call/cc



reply via email to

[Prev in Thread] Current Thread [Next in Thread]