1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 20:00:19 +02:00
Commit graph

64 commits

Author SHA1 Message Date
Andy Wingo
d00150303d JIT compilation tweaks
* libguile/jit.c (emit_alloc_frame_for_sp, compile_current_thread): Fix
  some ldxr/ldxi stxr/stxi confusions.
  (compile_alloc_frame): Omit if the frame size is already correct.
  (compile, compute_mcode, scm_sys_jit_compile, scm_jit_enter_mcode):
  Add a bit more debugging.
2018-08-20 11:43:46 +02:00
Andy Wingo
dca1e9d7bd Manual JIT interface via %jit-compile
* libguile/init.c (scm_i_init_guile): Call scm_init_jit ().
* libguile/jit.c (enter_mcode, exit_mcode): New static members; code
  pointers for the JIT trampoline.
  (emit_exit): New helper.  The Lightning tramp/frame mechanism that we
  use needs to exit via a jmp instead of a return.  Adapt callers of
  jit_ret.
  (emit_entry_trampoline): Use the "frame" mechanism to enter the JIT.
  (compile1): Add missing "break" after case statements.  Oops!
  (compile): Add prolog and "tramp" to compiled functions.
  (initialize_jit): New local routine to init the JIT on demand.
  (compute_mcode): New helper, to compile a function.
  (scm_sys_jit_compile): New function, exported to Scheme as
  %jit-compile.
  (scm_jit_compute_mcode): Return the existing mcode if the function is
  at the start.
  (scm_jit_enter_mcode): Call the enter_mcode trampoline.
* libguile/jit.h (struct scm_jit_state): Declare, so we can make
  pointers to it.
* libguile/threads.h (struct scm_thread): Add jit_state member.
* libguile/threads.c (on_thread_exit): Free the jit state.
2018-08-20 08:56:35 +02:00
Andy Wingo
698bff8748 First implementation of a template JIT
* libguile/jit.c: Implement most of a JIT.  Untested and still needs to
  be wired up.
2018-08-19 17:39:16 +02:00
Andy Wingo
d4abe8bbed Avoid needless 64-bit args on 32-bit machines for some intrinsics
* libguile/intrinsics.h:
* libguile/intrinsics.c (string_set_x): Change to take size_t and u32 as
  args.
  (allocate_words): Change to take size_t as arg.
* libguile/vm.c (expand_apply_argument): Rename from rest_arg_length,
  and also handle the stack manipulation.
* libguile/vm-engine.c (expand-apply-argument): Update for intrinsic
  change.
  (call-scm-sz-u32): Rename from call-scm-u64-u64, as it matches its
  uses and will compile better on 32-bit systems.
* module/system/vm/assembler.scm (define-scm-sz-u32-intrinsic):
  (string-set!): Update for new instrinsic call inst.
* libguile/jit.c (compile_call_scm_sz_u32): Adapt.
2018-08-13 14:30:01 +02:00
Andy Wingo
5df43b60a9 Adapt JIT calling convention; continuations take mra from stack
* libguile/intrinsics.h (SCM_FOR_ALL_VM_INTRINSICS): Update prototype of
  capture-continuation.
* libguile/jit.h:
* libguile/jit.c (scm_jit_enter_mcode): Return void, not the vra.
  Instead, we expect the code to set vp->ip for the vra.
* libguile/vm-engine.c (instrument-entry, instrument-loop)
  (return-values, abort): Adapt scm_jit_enter_mcode calling convention.
  (capture-continuation): No need to pass an mra; the intrinsic will
  read it from the stack.
* libguile/vm.c (capture_continuation): Remove mra arg, as we take mra
  from the continuation.
  (scm_call_n): Adapt to scm_jit_enter_mcode change.
2018-08-13 10:32:13 +02:00
Andy Wingo
a20feea43e Continuations capture machine code address
* libguile/continuations.c (scm_i_continuation_to_frame): Adapt to vra
  field renaming.
  (scm_i_reinstate_continuation, grow_stack, copy_stack_and_call)
  (scm_dynthrow): Take mra of continuation.  Set on the vp before the
  longjmp.
* libguile/continuations.h: Update scm_i_reinstate_continuation
  prototype.
* libguile/dynstack.h:
* libguile/control.c (scm_suspendable_continuation_p):
* libguile/dynstack.c (PROMPT_WORDS, PROMPT_VRA, PROMPT_MRA):
  (PROMPT_JMPBUF, scm_dynstack_push_prompt, scm_dynstack_find_prompt)
  (scm_dynstack_wind_prompt): Store both virtual and machine return
  addresses on the dynstack, for prompts.
* libguile/eval.c (eval): Pass NULL for mra.
* libguile/intrinsics.c (push_prompt): Add mra arg, and pass it to the
  dynstack.
* libguile/intrinsics.h: Update prototypes so that continuation-related
  intrinsics can save and restore the MRA.
* libguile/jit.h:
* libguile/jit.c: Return VRA when JIT code needs to tier down.
* libguile/stacks.c (find_prompt, scm_make_stack)
* libguile/throw.c (catch): Adapt find-prompt calls.
* libguile/vm-engine.c (instrument-entry, instrument-loop): Add logic to
  continue with vcode after the mcode finishes.
  (compose-continuation, capture-continuation, abort, prompt): Add logic
  to pass NULL as captured MRA, but continue with mcode from new
  continuations, if appropriate.
* libguile/vm.c (scm_i_vm_cont_to_frame, capture_stack)
  (scm_i_capture_current_stack, reinstate_continuation_x)
  (capture_continuation, compose_continuation_inner, compose_continuation)
  (capture_delimited_continuation, abort_to_prompt): Adapt to plumb
  around machine code continuations.
  (scm_call_n): Check "mra_after_abort" field for machine code
  continuation, if any.
* libguile/vm.h (struct scm_vm): Add "mra_after_abort" field.
  (struct scm_vm_cont): Rename "ra" field to "vra" and add "mra" field.
2018-08-12 15:57:53 +02:00
Andy Wingo
c3ff72cb81 Change call/cc inst to capture-continuation
* libguile/jit.c (compile_capture_continuation): Rename from call_cc now
  that the call is elsewhere.
* libguile/vm-engine.c (call, tail-call): Remove needless SYNC_IP before
  get-callee-vcode; the intrinsic can sync the ip if needed from the
  frame.
  (capture-continuation): Rename from call/cc, and leave the call itself
  to tail-call.
* libguile/vm.c (vm_builtin_call_with_current_continuation_code): Update
  to put the continuation in a local and then tail call.
  (get_callee_vcode): Sync vp->ip if we error.
2018-08-08 16:32:18 +02:00
Andy Wingo
b8a9a666f1 Rewrite subr implementation
* libguile/gsubr.c: Reimplement to store subr names and procedures in a
  side table, and to allocate fresh vcode for each subr.  This allows
  JIT of subrs, moves to a uniform all-code-starts-with-instrument-entry
  regime, and also allows statprof to distinguish between subrs based on
  IP.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_NAME): Call out to functions,
  now that these are in a side table.
  (scm_subr_function, scm_subr_name): New exports.
  (scm_i_primitive_name): New internal function, for looking up a
  primitive name based on IP.
  (scm_apply_subr): Take the subr index.
* libguile/vm-engine.c (subr-call):
* libguile/jit.c (compile_subr_call): Adapt to take index as arg.
* module/statprof.scm (sample-stack-procs, count-call):
  (stack-samples->procedure-data): Update to always record IP in stack
  samples and call counts.
* module/system/vm/frame.scm (frame-procedure-name): Simplify.
  (frame-instruction-pointer-or-primitive-procedure-name): Removed.
* libguile/programs.h:
* libguile/programs.c (scm_primitive_code_name): New function.
* module/system/vm/program.scm (primitive-code-name): New export.
2018-07-29 15:47:07 +02:00
Andy Wingo
a6b5049aa8 Emit instrument-loop in loops.
* am/bootstrap.am (SOURCES):
* module/Makefile.am (SOURCES): Handle renamve of handle-interrupts.scm
  to loop-instrumentation.scm.
* libguile/jit.h (SCM_JIT_COUNTER_ENTRY_INCREMENT): Rename from
  SCM_JIT_COUNTER_CALL_INCREMENT.
* libguile/vm-engine.c (instrument-entry): Rename from instrument-call.
* module/language/cps/compile-bytecode.scm (compile-function): Add
  handle-interrupts code before calls and returns.  Compile the
  "instrument-loop" primcall to an "instrument-loop" instruction and a
  "handle-interrupts" instruction.
  (lower-cps): Adapt to add-loop-instrumentation name change.
* module/language/cps/loop-instrumentation.scm: Rename from
  handle-interrupts.scm and just add "instrument-loop" primcalls in
  loops.  The compiler will add handle-interrupts primcalls as
  appropriate.
* module/system/vm/assembler.scm (<jit-data>): New data type, for
  emitting embedded JIT data.
  (<meta>): Add field for current JIT data.
  (make-meta): Initialize current JIT data.
  (emit-instrument-entry*, emit-instrument-loop*): New instruction
  emitters that reference the current JIT data.
  (end-program): Now that all labels are known, arrange to serialize the
  JIT data.
  (link-data): Reserve space for JIT data, and add relocs to initialize
  the "start" / "end" fields.
2018-07-29 15:47:07 +02:00
Andy Wingo
87da1c8d20 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.
2018-07-29 15:47:03 +02:00
Andy Wingo
c2a8224a63 Rework VM approach to shuffling unknown numbers of args
* libguile/vm-engine.c (shuffle-down, expand-apply-argument): New
  instructions.
  (tail-call, tail-call-label, return-values): Don't reset the frame.
  The compiler should reset the frame appropriately.
  (tail-call/shuffle, tail-apply): Remove unused instructions.
* libguile/vm.c (vm_builtin_apply_code): Use new shuffle-down and
  expand-apply-argument opcodes.
  (vm_builtin_call_with_values_code): Replace tail-call/shuffle with
  shuffle-down then tail-call.
* libguile/jit.c (compile_shuffle_down, compile_expand_apply_argument):
  Add compiler stubs
  (COMPILE_X8_F12_F12): New definition.
  (compile_tail_call_shuffle, compile_tail_apply): Remove unused
  compilers.
* module/language/cps/compile-bytecode.scm (compile-function): Emit
  reset-frame before tail calls and returns.
* module/system/vm/assembler.scm (system): Remove unbound "emit-return"
  export.
* module/system/vm/disassembler.scm (code-annotation)
  (instruction-has-fallthrough?, define-stack-effect-parser): Adapt for
  opcode changes.
2018-07-20 11:42:30 +02:00
Andy Wingo
80ba0e2cb1 Make JIT compiler skeleton more terse
* libguile/jit.c (scm_jit_state): Add "ip" member so we don't have to be
  passing it around all the time.  Rename use variables to "j" from
  "state".
2018-07-20 11:42:13 +02:00
Andy Wingo
5577392738 Begin structure for template JIT compiler
* libguile/jit.c: Begin to wire up compilers for the VM opcodes.
  Currently all that's there is a parser for each opcode's operands.
2018-07-03 13:31:29 +02:00
Andy Wingo
9338ef15c2 Wire up lightning into libguile build
* libguile/Makefile.am (AM_CPPFLAGS):
  (libguile_@GUILE_EFFECTIVE_VERSION@_la_SOURCES): If ENABLE_JIT, build
  lightning.
  (EXTRA_DIST): Add lightning files.
* libguile/lightning/lightning.am (lightning_extra_files): Add COPYING
  and related files to the dist.
* libguile/jit.c:
* libguile/jit.h: New files.
2018-07-02 11:08:57 +02:00