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

1722 commits

Author SHA1 Message Date
Andy Wingo
03998db647 Add new pass to optimize branch chains to table dispatch
* module/language/cps/switch.scm: New pass.

* module/Makefile.am (SOURCES):
* am/bootstrap.am (SOURCES): Add switch.scm.

* module/system/base/optimize.scm (available-optimizations):
* module/language/cps/optimize.scm (optimize-first-order-cps): Run
  switch optimization at level 2.

* libguile/hash.c (JENKINS_LOOKUP3_HASHWORD2): Add note regarding
  cross-compilation.

* module/language/cps/graphs.scm (intmap-select): New definition.
* module/language/cps/utils.scm (compute-singly-referenced-labels): Move
  here, from various places.  Doesn't take a body intset argument.

* module/language/cps/contification.scm:
* module/language/cps/closure-conversion.scm:
* module/language/cps/simplify.scm: Use compute-singly-referenced-labels
  from utils.

* module/language/cps/effects-analysis.scm (annotation->memory-kind*):
  (annotation->memory-kind): Add symbol annotation cases.
2020-08-12 23:30:08 +02:00
Andy Wingo
cd5ab6377b Add $switch CPS term kind
* module/language/cps.scm ($switch): New term.
* doc/ref/compiler.texi (CPS in Guile): Add documentation.

* module/language/cps.scm (build-term, parse-cps, unparse-cps)
* module/language/cps/closure-conversion.scm (compute-non-operator-uses)
  (compute-singly-referenced-labels, rewrite-shared-closure-calls)
  (compute-free-vars, convert-one)
* module/language/cps/compile-bytecode.scm (compile-function)
* module/language/cps/contification.scm (compute-singly-referenced-labels)
  (compute-contification-candidates, apply-contification)
* module/language/cps/cse.scm (compute-truthy-expressions)
  (forward-cont, term-successors, eliminate-common-subexpressions-in-fun)
* module/language/cps/dce.scm (compute-known-allocations)
  (compute-live-code, process-eliminations)
* module/language/cps/devirtualize-integers.scm (compute-use-counts)
  (peel-trace)
* module/language/cps/effects-analysis.scm (compute-effects)
* module/language/cps/licm.scm (hoist-one, hoist-in-loop)
* module/language/cps/loop-instrumentation.scm (compute-loop-headers)
* module/language/cps/peel-loops.scm (rename-cont)
* module/language/cps/renumber.scm (sort-labels-locally, renumber)
* module/language/cps/rotate-loops.scm (rotate-loop)
  (rotate-loops-in-function)
* module/language/cps/self-references.scm (resolve-self-references)
* module/language/cps/simplify.scm (compute-singly-referenced-vars)
  (eta-reduce, compute-singly-referenced-labels, beta-reduce)
* module/language/cps/slot-allocation.scm (compute-defs-and-uses)
  (add-prompt-control-flow-edges, compute-var-representations)
* module/language/cps/specialize-numbers.scm (compute-significant-bits)
* module/language/cps/split-rec.scm (compute-free-vars)
* module/language/cps/type-fold.scm (local-type-fold)
* module/language/cps/types.scm (successor-count, infer-types)
* module/language/cps/utils.scm (compute-function-body)
  (compute-successors, compute-predecessors)
* module/language/cps/verify.scm (compute-available-definitions)
  (check-valid-var-uses, check-arities): Add support for new term.
2020-08-12 23:30:08 +02:00
Andy Wingo
a7f4a6f1c4 Fold eqv? to eq? on exact integers according to target fixnum range
* module/language/tree-il/peval.scm (peval): Fix folding to only reduce
  to eq? for values within both host and target range.
2020-08-12 23:30:08 +02:00
Andy Wingo
d238566d0e CPS compiler reduces eq? on constant to eq-constant?
* module/language/cps/compile-bytecode.scm (compile-function): Expect
  eq-constant? instead of eq-null?, etc.
* module/language/cps/effects-analysis.scm: Likewise.
* module/language/cps/reify-primitives.scm (reify-primitives): For
  eq-constant?, reify a $const unless the constant is an immediate whose
  encoding fits in 16 bits.
* module/language/cps/type-fold.scm (materialize-constant): Helper to
  make a constant from a type, min, and max.
  (fold-eq-constant?): New helper.
  (eq-constant?): New folder.
  (undefined?): Define specifically.
  (define-nullish-predicate-folder): Renamd from
  define-special-immediate-predicate-folder.  Use only for null?, false,
  and nil?.
  (*branch-reducers*): New mechanism.  Reduce eq? to eq-constant? if
  possible.
  (local-type-fold): Refactor to use materialize-constant, and to allow
  reducing branches.
* module/language/cps/types.scm (constant-type): Return three values
  instead of a type entry.
  (constant-type-entry): New function that returns a type entry.  Adapt
  callers.
  (infer-constant-comparison): New helper.
  (eq-constant?): New inferrer.
  (undefined?): New inferrer.
* module/language/tree-il/compile-bytecode.scm (eq-constant?): Fix
  truncate-bits signed arg.
  (define-immediate-type-predicate): Adapt to visit-immediate-tags
  change.
* module/language/tree-il/compile-cps.scm (convert): Convert eq? to
  constant to eq-constant?.  Advantaged is that it gets fixnums and
  chars in addition to special immediates.
* module/language/tree-il/cps-primitives.scm (define-immediate-type-predicate):
  Adapt to allow #f as pred.
* module/system/base/types/internal.scm (immediate-tags): Use #f as pred
  for false, nil, etc.
  (immediate-bits->scm): Adapt.
* module/system/vm/assembler.scm (emit-eq-null?, emit-eq-nil?)
  (emit-eq-false?, emit-eq-true?, emit-unspecified?, emit-eof-object?):
  Remove specialized emitters.
* module/system/vm/assembler.scm (define-immediate-tag=?-macro-assembler):
  Allow for pred to be #f.
* module/system/vm/disassembler.scm (define-immediate-tag-annotation):
  Adapt to pred being #f.
2020-08-03 22:19:18 +02:00
Andy Wingo
1ee99d97db Baseline compiler emits eq-immediate? as appropriate
* module/language/tree-il/compile-bytecode.scm (eq?): Define
  eq-immediate? as immediate emitter.
  (canonicalize): Don't fuss so much about eq?; only if commutation is
  needed.  (Perhaps a more generic commutation pass is needed.)
  (compile-closure): Add support for emit/immediate for branches.
2020-08-03 22:19:18 +02:00
Andy Wingo
172e5ccfc1 Add sign-extending make-immediate instruction
* doc/ref/vm.texi (Instruction Set, Constant Instructions): Document new
  instruction.
* libguile/instructions.c (FOR_EACH_INSTRUCTION_WORD_TYPE): New first
  word kind with zi16 operand.
* libguile/jit.c (compile_make_immediate, compile_make_immediate_slow):
  New compilers.
  (COMPILE_X8_S8_ZI16): New operand kind.
* libguile/vm-engine.c (make-immediate): New instruction.
* module/language/bytecode.scm:
* module/system/vm/assembler.scm (encode-X8_S8_ZI16<-/shuffle):
  (signed-bits, load-constant): Support the new instruction kind.
* module/system/vm/disassembler.scm (disassemblers)
  (sign-extended-immediate, code-annotation): Support for zi16
  operands.
2020-07-30 17:36:11 +02:00
Andy Wingo
f13b27a4cc Improve code generation for eq? on immediates
* module/language/tree-il/compile-bytecode.scm (canonicalize):
* module/language/tree-il/compile-cps.scm (canonicalize): Specialize
  eq-false? and similar predicates.
2020-07-30 16:30:23 +02:00
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