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

1799 commits

Author SHA1 Message Date
Andy Wingo
bb7fa5bdc2 Add jtable instruction
* doc/ref/vm.texi (Instruction Set): Document new v32-x8-l24 instruction
  kind.
  (Branch Instructions): Document jtable.
* libguile/instructions.c (FOR_EACH_INSTRUCTION_WORD_TYPE): Add
  V32_X8_L24.
* libguile/jit.c (compile_jtable, compile_jtable_slow):
  (COMPILE_X8_S24__V32_X8_L24, analyze): Add stub JIT compiler
  implementation.
* libguile/vm-engine.c (jtable): New instruction.
* module/language/bytecode.scm (instruction-arity): Deprecate.
* module/system/vm/assembler.scm (encoder, assembler): Add V32_X8_L24
  case.
* module/system/vm/disassembler.scm (u32-ref, s32-ref): Move definitions
  to expansion-time only.
  (define-op-handlers): New definition, replacing visit-opcodes.
  (disassemblers, jump-parsers, stack-effect-parsers, clobber-parsers):
  Rework in terms of define-op-handlers.  Default case becomes #f, and
  add support for jtable.
  (disassemble-one, instruction-relative-jump-targets)
  (instruction-stack-size-after, instruction-slot-clobbers): Inline
  default case in the lookup procedure, not copied in the handler
  vector.
  (compute-labels): Add jtable case.
  (instruction-lengths-vector, instruction-length): Rework to allow
  variable-length instructions, and mark jtable as being
  variable-length.
  (instruction-has-fallthrough?): Add jtable to the no-fallthrough
  set.
2020-07-23 12:24:11 +02:00
Ludovic Courtès
a0b9d86638 Tree-IL-to-CPS compiler delays calls to 'target-most-positive-fixnum'.
Fixes a bug whereby, for example, "guild compile --target=i686-linux-gnu"
running on x86_64 would generate invalid code for 'bytevector-u32-native-set!'
because 'target-most-positive-fixnum' was called from the top-level
when (language tree-il compile-cps) was loaded.

Consequently, the .go files under prebuilt/ would be invalid, leading to
build failures on 32-bit platforms.

This issue became apparent with cb8cabe85f.

* module/language/tree-il/compile-cps.scm (bytevector-ref-converter)[tag]:
Turn into a lambda so that 'target-most-positive-fixnum' is called in
the right context.
(bytevector-set-converter)[integer-unboxer]: Likewise.
2020-06-19 16:00:29 +02:00
Andy Wingo
fc139231a6 Make CSE more robust
* module/language/cps/cse.scm (eliminate-common-subexpressions-in-fun):
  I think it's possible to get an orphan loop, with predecessors
  after successors in the original RPO.  Handle that here.
2020-06-12 15:58:54 +02:00
Andy Wingo
e2066d2e63 Change -O1 compiler to use baseline and also resolve primitives
* bootstrap/Makefile.am (GUILE_OPTIMIZATIONS): Change to just -O1.
* module/language/tree-il/spec.scm (choose-compiler): Use CPS for -O2
  and higher.
* module/system/base/optimize.scm (available-optimizations): CPS for -O2
  and higher, but -Oresolve-primitives now at -O1 also.
2020-06-01 21:15:39 +02:00
Andy Wingo
4c10ea0e57 Fix bug where private bindings would never be sealed
* module/language/tree-il/optimize.scm (make-optimizer): Fix plumbing of
-Oseal-private-bindings / -O3.
2020-05-31 22:05:41 +02:00
Andy Wingo
411954c961 Fix typo causing performance problems!!!
* module/language/cps/cse.scm (eliminate-common-subexpressions-in-fun):
  Amazingly this typo causes Guile to be 10% slower or so.  Fixed!
2020-05-29 16:59:08 +02:00
Andy Wingo
4c59ff7e95 CSE forward-propagates changes to CFG
* module/language/cps/cse.scm (propagate-analysis): New helper.
  (eliminate-common-subexpressions-in-fun): Recompute avail and bool set
  in response to simplifications in predecessor CFG.  Allows much better
  compilation of pattern-matching idioms!
2020-05-29 16:33:48 +02:00
Andy Wingo
d9143c32c5 CSE forwards branch predecessors where the branch folds
* module/language/cps/cse.scm (forward-cont, forward-branch)
  (compute-avail-and-bool-edge): New helpers.
  (add-equivalent-expression!): Allow idempotent adds; can happen now
  when revisiting a cont after changes to its predecessors.
  (fold-branch): New helper.
  (eliminate-common-subexpressions-in-fun): Allow for reductions to
  branch predecessors.  In that case, revisit the branch, as the CFG
  will have changed.
2020-05-29 16:33:48 +02:00
Andy Wingo
4837e68315 CSE refactor
* module/language/cps/cse.scm (eliminate-common-subexpressions-in-fun):
  Separate the paths for handling expressions and branches.
2020-05-29 16:33:48 +02:00
Andy Wingo
19ab4d6947 Use intmaps in CSE equivalent expression table
* module/language/cps/cse.scm (make-equivalent-expression-table)
  (intmap-select, add-equivalent-expression!)
  (lookup-equivalent-expressions): New helpers.
  (eliminate-common-subexpressions-in-fun): Adapt.
2020-05-29 16:33:48 +02:00
Andy Wingo
a92c623a66 Eager graph pruning in CSE
* module/language/cps/cse.scm (elide-predecessor, prune-branch)
  (prune-successors, term-successors): New helpers.
  (eliminate-common-subexpressions-in-fun): When we modify the CFG,
  update the analysis.  Also, thread the substs map through CSE so that
  closures in high-level CPS can take advantage of eliminated variables.
  (fold-renumbered-functions): Take multiple seeds.
  (eliminate-common-subexpressions): Thread var substs map through CSE.
2020-05-29 16:33:48 +02:00
Andy Wingo
1baed76349 Macro fix to CPS build-term
* module/language/cps.scm (build-term): Match on $branch and $throw as
  literals.
2020-05-29 16:33:48 +02:00
Andy Wingo
3c4d4acbd4 Thread flow analysis through CSE pass
* module/language/cps/cse.scm (<analysis>): New data type, grouping
  available expression analysis, predecessor map, etc.
  (eliminate-common-subexpressions-in-fun): Instead of having a static
  analysis, thread it through the CSE pass so that we can update the CFG
  as we go.
2020-05-29 16:33:48 +02:00
Andy Wingo
6fb0635358 CSE eliminates expressions at continuations
* module/language/cps/cse.scm (compute-available-expressions): Take a
  clobber map instead of an effects map.
  (compute-singly-referenced): Remove unused function.
  (eliminate-common-subexpressions-in-fun): Keep a preds map.  Use it
  add entries to the equiv-set and var-substs at expression
  continuations instead of at the expression terms themselves.
2020-05-29 16:33:48 +02:00
Andy Wingo
2318e7238f Refactor CSE to analyze and transform in a single pass
* module/language/cps/cse.scm (compute-truthy-expressions): Operate on a
  single function.
  (eliminate-common-subexpressions-in-fun): Instead of computing a set
  of labels to eliminate, go ahead and do the elimination as we go.
  (fold-renumbered-functions): Can just use a single seed now.
  (eliminate-common-subexpressions): Simplify to just fold over
  functions, building up renamed output as we go.
2020-05-29 16:33:48 +02:00
Andy Wingo
6e91173334 Refactor CSE to take advantage of RPO numbering
* module/language/cps/cse.scm (fold-renumbered-functions): New helper.
  (compute-equivalent-expressions): Use new helper.
  (compute-equivalent-expressions-in-fun): Lift to top-level.
  (eliminate-common-subexpressions): Adapt.
2020-05-29 16:33:48 +02:00
Andy Wingo
cf948e0f6f Renumber before CSE
* module/language/cps/cse.scm (compute-equivalent-subexpressions):
  Assume renumbered program.
  (eliminate-common-subexpressions): Renumber.  Will allow optimizations
  later.
2020-05-29 16:33:48 +02:00
Andy Wingo
d36b9f0422 Slight optimization to invert-bijectiono
* module/language/cps/graphs.scm (invert-bijection): Use a transient
  intmap.
2020-05-28 10:58:09 +02:00
Andy Wingo
11878e1603 Fix truthy expression analysis in CSE
* module/language/cps/cse.scm (compute-truthy-expressions): The "meet"
  function here is union, not intersection.
2020-05-28 10:02:41 +02:00
Andy Wingo
2c07a32ad8 Tree-IL lowerer only loads needed optimizations
* module/language/tree-il/optimize.scm (make-optimizer): New procedure,
  to compute an optimizer given options, lazily loading optimization
  modules.
  (optimize): Use make-optimizer.
  (make-lowerer): Use make-optimizer, so as to only load needed
  optimizations.  Speeds up bootstrap times.
2020-05-14 10:24:08 +02:00
Andy Wingo
7df3f3414b More robust reduction of equal? and eqv?
* module/language/tree-il/primitives.scm (expand-eq): Just expand out to
  binary comparisons.  Also expand eq?, which was missing.  Leave
  strength reduction to peval.
  (character-comparison-expander): Move down, as it depends on <, <=,
  and so on.
* module/language/tree-il/peval.scm (peval): Robustly reduce equal? and
  eqv?.
* test-suite/tests/peval.test ("partial evaluation"): Expect fixnum
  comparison to reduce to eq?.
  ("eqv?", "equal?"): A new battery of tests.
* test-suite/tests/tree-il.test ("primitives"): Remove reduction tests.
2020-05-13 15:51:58 +02:00
Andy Wingo
498428fbef Add with-lexicals helper; fix bug in (equal? #t (foo) #t)
* module/language/tree-il.scm (with-lexicals): New public helper.
* .dir-locals.el (with-lexicals): Add indentation rule.
* module/language/tree-il/compile-bytecode.scm (canonicalize): Use
  with-lexicals.
* module/language/tree-il/compile-cps.scm (canonicalize): Use
  with-lexicals from tree-il.
* module/language/tree-il/primitives.scm (chained-comparison-expander):
  Remove duplicate expander definitions for <, <=, and so on.
* module/language/tree-il/primitives.scm (maybe-simplify-to-eq): Avoid
  inadvertent code duplication by using with-lexicals.
  (expand-chained-comparisons): Likewise.
  (call-with-prompt): Simplify to use with-lexicals.
2020-05-13 14:51:56 +02:00
Andy Wingo
c5da9d65a7 Fix optimizers after language lowerer refactor
* module/language/cps/optimize.scm (define-optimizer):
  (optimize-higher-order-cps, optimize-first-order-cps):
  (make-cps-lowerer):
* module/language/tree-il/optimize.scm (optimize, make-lowerer): In an
  embarrassing bug, after parsing optimization arguments, we were
  aconsing them instead of the expected cons*.  This meant the bootstrap
  was running all Tree-IL optimizations!  Change to have optimizers not
  have defaults and use alists after parsing.
2020-05-13 09:04:16 +02:00
Andy Wingo
32eef3dd14 Slight optimization to baseline compiler
* module/language/tree-il/compile-bytecode.scm (compile-closure):
  for-value-at and for-values-at take indexes instead of environments to
  denote destination.
2020-05-13 09:04:16 +02:00
Andy Wingo
9c4c6322f5 Whoops, fix bug in tree-il choose-compiler
* module/language/tree-il/spec.scm (choose-compiler): Fix spelling of
  "compile-cps" :)
2020-05-12 10:12:21 +02:00
Andy Wingo
cb8cabe85f Delay loading CPS unless CPS compiler used
* module/language/tree-il/spec.scm: Remove #:compilers declaration;
  instead rely on choose-compiler.
  (choose-compiler): Load compilers on demand.
* module/system/base/compile.scm (find-language-joint): Use next-pass
  instead of lookup-compilation-order, to avoid loading unused
  compilers.
  (read-and-compile): Adapt to find-language-joint change.
  (compute-compiler): Export.
* module/scripts/compile.scm (compile): Use compute-compiler to load
  compiler modules.
2020-05-12 09:54:19 +02:00
Andy Wingo
44ad8fbde5 Baseline FIXME's are done :)
* module/language/tree-il/compile-bytecode.scm: Remove FIXME comments.
2020-05-11 22:03:08 +02:00
Andy Wingo
b02a889659 Baseline compiler emits source locations
* module/language/tree-il/compile-bytecode.scm (compile-closure): Emit
  source annotations where we have them.
2020-05-11 16:53:23 +02:00
Andy Wingo
f66111a203 Baseline O(1) access to frame-base env
* module/language/tree-il/compile-bytecode.scm (compile-closure): No
  need to search for frame-base.
2020-05-11 16:43:08 +02:00
Andy Wingo
b35fd53664 Emit handle-interrupts in baseline compiler
* module/language/tree-il/compile-bytecode.scm (compile-closure): Handle
  interrupts before calls, returns, and tail calls.
2020-05-11 16:15:24 +02:00
Andy Wingo
b5108ccb2a Avoid having "guild compile" load optimizers
* module/language/cps/optimize.scm (cps-optimizations):
* module/language/tree-il/optimize.scm (tree-il-optimizations):
* module/system/base/optimize.scm (available-optimizations): Invert the
  dependency tree to hold the names and default optimization levels in a
  central place instead of in the optimizers.  It moves definitions
  farther from uses, but it avoids us having to load the CPS optimizer
  if we don't need it, which improves bootstrap times.
2020-05-11 15:42:20 +02:00
Andy Wingo
7f4bbc3dba Improve tail recursion in compiler
* module/language/tree-il/compile-bytecode.scm (compile-closure): Make
  it so that for-tail is actually tail-recursive.  Likewise improve tail
  recursion for the other helpers.
2020-05-11 14:48:45 +02:00
Andy Wingo
73a23027f5 Fix module capture for closures in <fix>
* module/language/tree-il/compile-bytecode.scm (compile-closure):
  Capture closures for <fix> if needed.
2020-05-11 14:36:00 +02:00
Andy Wingo
4c3c35c536 Fix "cons" compilation in baseline compiler in some cases
* module/language/tree-il/compile-bytecode.scm (emit-cons): Fix for case
  where car is dst but cdr isn't.
2020-05-11 13:32:39 +02:00
Andy Wingo
3b6023d66d Baseline compiler: add pop-fluid primitive.
* module/language/tree-il/compile-bytecode.scm (pop-fluid): New
  primitive.
2020-05-11 13:32:19 +02:00
Andy Wingo
527262fef0 Fix baseline compilation error for make-struct/simple
* module/language/tree-il/compile-bytecode.scm (compile-closure):
  $allocate-struct takes an SCM.
2020-05-11 13:31:40 +02:00
Andy Wingo
c0a27de50d Fix baseline miscompilation of <
* module/language/tree-il/compile-bytecode.scm: Fix < miscompilation.
2020-05-11 13:18:50 +02:00
Andy Wingo
73ac53af73 Fix free var computation in baseline compiler for lexical set!
* module/language/tree-il/compile-bytecode.scm (split-closures): Adjoin
  lexical being set to free vars.
2020-05-11 11:39:53 +02:00
Andy Wingo
ffb210567d Declare #:cps? as an available optimization
* module/language/tree-il/optimize.scm (tree-il-optimizations): Add
  #:cps?, on by default from -O1.  This enables "guild compile -O2
  -Ono-cps foo.scm".
2020-05-11 11:23:24 +02:00
Andy Wingo
2e6f2feefc Fix baseline canonicalization of reifying prompts
* module/language/tree-il/compile-bytecode.scm (canonicalize): Fix arg
  order to make-prompt.
2020-05-11 11:05:06 +02:00
Andy Wingo
f9c9e71046 Fix compilation of prompt in tail context
* module/language/tree-il/compile-bytecode.scm (compile-closure):
  Compile body of prompt in values-at context at the frame base, not the
  current env.
2020-05-11 10:57:29 +02:00
Andy Wingo
b99a63447b Fix kw initializer bug in baseline compiler
* module/language/tree-il/compile-bytecode.scm (compile-closure): The
  first sym has index 1, not 0, because of the closure.
2020-05-11 10:45:29 +02:00
Andy Wingo
dd4dc1f6c4 Use lookup, lookup-bound in baseline compiler
* module/language/tree-il/compile-bytecode.scm (emit-box-set!): Fix to
  reference by SCM, not word.
  (emit-box-ref): New helper.
  (emit-cached-module-box, emit-cached-toplevel-box, emit-toplevel-box):
  Add bound? arg.  Before these could produce #f instead of a variable,
  and unbound variable errors weren't any good as they didn't have the
  variable name.
  (compile-closure): Use more box-ref and box-set!.  Pass bound? arg to
  the helpers.
2020-05-11 10:34:25 +02:00
Andy Wingo
85124b0d69 Top-level lookups raise exceptions in run-time
* module/language/cps/reify-primitives.scm (reify-lookup):
* module/language/tree-il/compile-cps.scm (toplevel-box): Instead of
  checking that the result of module-variable is a variable, and
  possibly checking that it's bound, we just call intrinsics that throw
  exceptions if the variable isn't bound.  This reduces useless inlining
  that can't inform CPS optimizations, as they are tangled in diamond
  control flow.
2020-05-11 10:22:56 +02:00
Andy Wingo
4274d615cc Add new lookup, lookup-bound intrinsics
* libguile/intrinsics.h (SCM_FOR_ALL_VM_INTRINSICS):
* libguile/intrinsics.c (lookup, lookup_bound):
* libguile/intrinsics.c (scm_bootstrap_intrinsics): New intrinsics.
* module/language/cps/reify-primitives.scm (reify-primitives):
* module/language/cps/effects-analysis.scm (current-module):
* module/language/cps/compile-bytecode.scm (compile-function):
* module/system/vm/assembler.scm: Add compiler support.
2020-05-11 10:22:34 +02:00
Andy Wingo
d6b6392cfb Rename "lookup" intrinsic to "module-variable"
It can return #f, unlike scm_(module_)?lookup.

* libguile/intrinsics.h (SCM_FOR_ALL_VM_INTRINSICS):
* libguile/intrinsics.c (module_variable, scm_bootstrap_intrinsics):
  Rename.
* module/language/cps/compile-bytecode.scm (compile-function):
* module/language/cps/effects-analysis.scm (current-module):
* module/language/cps/reify-primitives.scm (reify-lookup)
  (compute-known-primitives):
* module/language/tree-il/compile-bytecode.scm (emit-cached-module-box)
  (emit-cached-toplevel-box, emit-toplevel-box):
* module/language/tree-il/compile-cps.scm (toplevel-box):
* module/system/vm/assembler.scm (module-variable): Adapt users.
2020-05-11 09:46:40 +02:00
Andy Wingo
04e3cece45 Fix baseline compiler bugs
* module/language/tree-il/compile-bytecode.scm (compile-closure): Fix
  FP-relative computation for "receive" target.  Fix value-at syntax in
  conditional.
2020-05-09 22:23:00 +02:00
Andy Wingo
b5a52b4961 Wire up the baseline compiler to -O0
* module/language/tree-il/spec.scm (join): Use match rather than
  pmatch.
  (tree-il): Declare compiler to bytecode.
  (choose-compiler): New implementation.  Note, the baseline compiler
  probably doesn't even work!!!
2020-05-08 23:07:30 +02:00
Andy Wingo
f711ab85b2 Add compiler chooser implementation; fix bugs with previous commit
* module/system/base/compile.scm (next-pass): Invoke the language's
  compiler chooser if there is more than one compiler.
  (compute-compiler): Ensure from and to are languages.
* module/system/base/language.scm (<language>): Add compiler-chooser
  field.
* module/language/brainfuck/spec.scm (choose-compiler, brainfuck):
  Define a compiler chooser.
2020-05-08 22:56:37 +02:00
Andy Wingo
4311dc9858 Define new "lowering" phase in compiler
* module/language/cps/compile-bytecode.scm (compile-bytecode):
* module/language/tree-il/compile-bytecode.scm (compile-bytecode):
* module/language/tree-il/compile-cps.scm (compile-cps): Rely on
  compiler to lower incoming term already.
* module/language/tree-il/optimize.scm (make-lowerer): New procedure.
* module/system/base/compile.scm (compute-lowerer): New procedure,
  replaceing add-default-optimizations.
  (compute-compiler): Lower before running compiler.
* module/system/base/language.scm (<language>): Change
  optimizations-for-level field to "lowerer".
* module/scripts/compile.scm (%options, compile): Parse -O0, -O1 and so
  on to #:optimization-level instead of expanding to all the
  optimization flags.
* module/language/cps/optimize.scm (lower-cps): Move here from
  compile-bytecode.scm.
  (make-cps-lowerer): New function.
* module/language/cps/spec.scm (cps): Declare lowerer.
2020-05-08 17:07:56 +02:00