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

704 commits

Author SHA1 Message Date
Andy Wingo
b839233282 peval uses effort counters, propagates lambdas more effectively
* module/language/tree-il/optimize.scm (code-contains-calls?): Remove
  this helper, we will deal with recursion when it happens, not after
  the fact.
  (peval): Add keyword args for various size and effort limits.  Instead
  of keeping a call stack, keep a chain of <counter> records, each with
  an abort continuation.  If ever an inlining attempt is taking too
  long, measured in terms of number of trips through the main loop, the
  counter will abort.  Add new contexts, `operator' and `operand'.  They
  have different default size limits.  In the future we should actually
  use the size counter, instead of these heuristics.

  The <lexical-ref> case is smarter now, and tries to avoid propagating
  too much data.  Perhaps it should be dumber though, and use a
  counter.  That would require changes to the environment structure.

  Inline <lambda> applications to <let>, so that we allow residual
  lexical references to have bindings.  Add a `for-operand' helper, and
  use it for the RHS of `let' expressions.  A `let' is an inlined
  `lambda'.

  `Let' and company no longer elide bindings if the result is a
  constant, as the arguments could have effects.  Peval will still do as
  much as it can, though.

* test-suite/tests/tree-il.test ("partial evaluation"): Update the tests
  for the new expectations.  They are uniformly awesomer, with the
  exception of two cases in which pure but not constant data is not
  propagated.
2011-09-25 02:49:02 +02:00
Andy Wingo
fab137869e prune unused letrec bindings
* module/language/tree-il/optimize.scm (peval): Prune unused `letrec'
  bindings.
2011-09-24 20:37:12 +02:00
Andy Wingo
062bf3aa44 more peval refactoring
* module/language/tree-il/optimize.scm (peval): Rename `var-table' to
  `store', as we're going to put some more things in it.  Rename
  `record-lexical-bindings' to `record-source-expression', which also
  takes the original, pre-renaming expression.  Keep a mapping from new
  expressions to original expressions, available using the
  `source-expression' helper.
2011-09-24 20:34:43 +02:00
Andy Wingo
8018dfdc02 add helpers for effort counters
* module/language/tree-il/optimize.scm (<counter>, abort-counter)
  (record-effort!, record-size!, find-counter, make-top-counter)
  (make-nested-counter, make-recursive-counter): New helpers, as yet
  unused, but which will implement fixed effort bounds on the inlining
  algorithm.
2011-09-24 20:34:33 +02:00
Andy Wingo
ded8ad84a7 peval refactor
* module/language/tree-il/optimize.scm (peval): Add for-value, for-test,
  for-effect, and for-tail helpers.  Use them.
2011-09-24 20:34:12 +02:00
Andy Wingo
f6123e4fda attempt to prune unreferenced bindings
* module/language/tree-il/optimize.scm (peval): Rename `record-lexicals'
  to `record-lexical-bindings'.  Record residualized lexical
  references.  Record lexical references in maybe-unlambda.
  Unfortunately this has the disadvantage that the speculative mapping
  of lambda expressions to lexical references records that reference,
  even if we are not going to residualize it.  After processing a `let',
  prune pure unreferenced bindings.  (We can do better than this in the
  future: we can simply process them for effect.)

* test-suite/tests/tree-il.test (pass-if-peval): More debugging.
  ("partial evaluation"): Update to reflect the fact that the `y'
  binding won't be emitted.
2011-09-24 19:08:09 +02:00
Andy Wingo
e43921a982 peval handles lexical-set
* module/language/tree-il/optimize.scm (alpha-rename, peval): Add
  support for lexical-set, while avoiding copy propagation and pruning
  of assigned variables.
2011-09-24 19:02:53 +02:00
Andy Wingo
b8a2b628e9 peval: pre-analyze mutated or reffed-once lexicals
* module/language/tree-il/optimize.scm (<var>, build-var-table, peval):
  Before going into peval, build a table indicating refcounts and a set?
  flag for all lexicals.  Add to the table when introducing new bindings
  (via alpha-renaming).
2011-09-24 19:00:53 +02:00
Andy Wingo
1eb4886ffa peval: don't propagate expressions that access memory
* module/language/tree-il/optimize.scm (peval): Rename
  `pure-expression?' to `constant-expression?', in the sense of GCC's
  `pure' and `const'.  A <toplevel-ref> is not constant, because it can
  be mutated.  A <dynref> isn't constant either, for the same reason.

* test-suite/tests/tree-il.test ("partial evaluation"): Add a test, and
  update existing tests that assumed that toplevel-ref would propagate.
2011-09-24 18:57:59 +02:00
Andy Wingo
8d06538e82 context-specific folding for peval in test and effect contexts
* module/language/tree-il/optimize.scm (peval): Add a "test" context,
  which folds statically decidable values to <const>.  Fold pure
  expressions to <void> in "effect" contexts.  Adapt the <conditional>
  and <sequence> tests to simply look for <const> or <void> expressions,
  respectively.
2011-09-24 17:17:13 +02:00
Andy Wingo
e535a37db8 thread a context through peval
* module/language/tree-il/optimize.scm (peval): Thread a "context"
  through the evaluator.
2011-09-24 17:17:11 +02:00
Andy Wingo
250991010f peval: various bugfixes
* module/language/tree-il/optimize.scm (alpha-rename): Rename the
  init
  expressions of a <lambda-case>.
  (peval): Coalesce the <let-values> clauses.
  Fix pure-expression? matching of <lambda> clauses.
  Loop over and maybe-unconst the inits of a <lambda-case>.
2011-09-24 17:15:32 +02:00
Andy Wingo
9e8a5b6637 tree-il-any bugfix
* module/language/tree-il/optimize.scm (tree-il-any): Fix to be called
  on all values, including leaves.  It didn't matter for the use case,
  though.
2011-09-24 17:09:40 +02:00
Andy Wingo
dd7ab5d8a4 minor peval style tweak
* module/language/tree-il/optimize.scm (peval): Minor refactor to
  <lexical-ref> copy propagation.
2011-09-24 17:09:40 +02:00
Andy Wingo
c829531a46 fix alpha-rename for kwargs
* module/language/tree-il/optimize.scm (alpha-rename): Fix
  alpha-renaming of keyword arguments.
2011-09-24 17:09:40 +02:00
Ludovic Courtès
ec6e09bee7 peval: Rectify style.
* module/language/tree-il/optimize.scm (peval): Rename `src' to
  `lv-src', and `src2' to `src'; pass `make-let-values' the right source
  locations.  Reindent `let*'.
2011-09-23 18:12:28 +02:00
Andy Wingo
d851e32fdc prevent propagation for memory-dependent operations like `car'
* module/language/tree-il/primitives.scm (*primitive-constructors*):
  Record car, cdr, vector-ref, and struct-ref as "constructors".
  Comment to come later.
  (*effect-free-primitives*): Update.

* test-suite/tests/tree-il.test ("partial evaluation"): Add tests.
2011-09-23 18:02:05 +02:00
Andy Wingo
40bd6a7e57 peval comment & reindentation
* module/language/tree-il/optimize.scm (peval): Add a comment regarding
  failure modes, and reindent one clause.
2011-09-21 08:58:27 +02:00
Andy Wingo
9581febbb0 fix comment regarding alpha-renaming
* module/language/tree-il/optimize.scm (peval): Fix comment regarding
  alpha-renaming: it's not simply the allocator that needs unique names;
  rather, all transformations depend on it.
2011-09-21 08:58:27 +02:00
Andy Wingo
2605b6ba27 better pure-expression?
* module/language/tree-il/optimize.scm (peval): Allow dynref, fix, and
  let-values to be pure expressions.
2011-09-21 08:58:27 +02:00
Andy Wingo
ddbee5c00f more alpha-rename robustness
* module/language/tree-il/optimize.scm (alpha-rename): Handle all kinds
  of tree-il, with the current exceptions of lexical set!, prompt, and
  abort.
2011-09-21 08:58:27 +02:00
Andy Wingo
5d5e4f399a more robust alpha-renaming
* module/language/tree-il/optimize.scm (fresh-gensyms): New helper.
  (alpha-rename): Name the new gensyms using the old names as templates,
  not the old gensyms.  This prevents accidental collisions between
  gensyms, if #{x 1}# becomes #{x 12}# instead of #{x 2}#.
2011-09-21 08:58:27 +02:00
Andy Wingo
4f33b47591 peval: inlining of let-values
* module/language/tree-il/optimize.scm (peval): Add support for
  let-values.  Try to inline the consumer into the body of the producer,
  if there is only one return point, and we can figure out how many
  values are being returned, and that number is compatible with the
  consumer.
2011-09-21 08:58:09 +02:00
Andy Wingo
65a3265525 peval support for more forms
* module/language/tree-il/optimize.scm (peval): Add support for fix,
  dynwind, dynlet, dynref, module-set, and toplevel-set.  (Mutating a
  variable directly is similar to calling a function that does so behind
  our backs, so this presents no additional problem.)
2011-09-21 08:56:09 +02:00
Andy Wingo
03026d0fb8 add singly-valued-primitive?
* module/language/tree-il/primitives.scm (singly-valued-primitive?): New
  predicate, for primitives that return exactly one value.
2011-09-21 08:53:06 +02:00
Andy Wingo
d111abd0f6 more optimize.scm factoring
* module/language/tree-il/optimize.scm (vlist-any): New helper.
  (peval): Use it here.
2011-09-21 05:37:07 +02:00
Andy Wingo
0c448ef47b optimize.scm refactor
* module/language/tree-il/optimize.scm (let/ec, tree-il-any): New
  helpers.
  (code-contains-calls?): Use them here.
2011-09-21 05:37:07 +02:00
Daniel Llorens
78295f242a Fix compilation of untyped arrays of rank not 1
* module/language/glil/compile-assembly.scm: vector-fold2 expects vector.
2011-09-21 05:37:07 +02:00
Ludovic Courtès
72b2ca55f6 peval: Abort inlining when the residual code contains recursive calls.
* module/language/tree-il/optimize.scm (code-contains-calls?): New
  procedure.
  (peval): Use it and abort inlining if the residual code of a procedure
  application contains recursive calls.  Suggested by Wingo, Waddell,
  and Dybvig.  Fixes <http://debbugs.gnu.org/9542>.

* test-suite/tests/tree-il.test ("partial evaluation"): Update 2 tests
  that relied on the previous behavior.  Add 1 another test.
2011-09-18 23:01:51 +02:00
Ludovic Courtès
2b0b09fed4 peval: Typo.
* module/language/tree-il/optimize.scm (peval)[maybe-unlambda]: Fix
  typo in comment.
2011-09-17 16:53:23 +02:00
Ludovic Courtès
2ae0775e40 peval: Alpha-rename anonymous lambdas that are duplicated.
* module/language/tree-il/optimize.scm (alpha-rename): New procedure.
  (peval)[maybe-unlambda]: Use it.

* test-suite/tests/tree-il.test ("partial evaluation"): Add two test
  cases for <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html>.
2011-09-17 16:49:41 +02:00
Ludovic Courtès
3f2d6efc7b peval: Use `resolve-primitives!'.
* module/language/tree-il/optimize.scm (peval): Add `cenv' optional
  argument; caller updated.
  Use `resolve-primitives!' to resolve <primitive-ref> expressions.
2011-09-13 23:34:07 +02:00
Ludovic Courtès
af1c6e424f peval: Clarify `pure-expression?'.
* module/language/tree-il/optimize.scm (peval)[pure-expression?]:
  Clarify the comment.
2011-09-13 23:30:22 +02:00
Ludovic Courtès
735249513a peval: Inline thunks.
* module/language/tree-il/optimize.scm (peval): Inline thunks.

* test-suite/tests/tree-il.test ("partial evaluation"): Add test.
2011-09-13 18:54:01 +02:00
Ludovic Courtès
61237fa4b9 peval: Use the right scope when replacing a lambda by a lexical-ref.
* module/language/tree-il/optimize.scm (peval)[maybe-unlambda]: New
  procedures.
  Use it to de-duplicate named lambdas.  This fixes the scoping bug
  described at <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html>.

* test-suite/tests/tree-il.test ("partial evaluation"): Add tests to
  reproduce the bug.
2011-09-13 18:25:09 +02:00
Ludovic Courtès
1e8ace33d1 peval: Add tests for inlining with both static & dynamic arguments.
* module/language/tree-il/optimize.scm (peval): Improve comment on the
  inlining heuristics.

* test-suite/tests/tree-il.test ("partial evaluation"): Add two tests.
2011-09-13 16:00:24 +02:00
Ludovic Courtès
870dfc609b peval: Propagate only pure expressions to lambdas.
* module/language/tree-il/optimize.scm (peval): Propagate ARGS to BODY
  only when all of ARGS are pure.  Change APP to use `maybe-unconst' for
  its arguments.

* test-suite/tests/tree-il.test ("partial evaluation"): Add tests for
  mutability preservation and non-propagation of non-constant arguments
  to lambdas.
2011-09-11 00:43:23 +02:00
Ludovic Courtès
89436781e8 peval: Try hard to preserve mutability.
* module/language/tree-il/optimize.scm (peval)[make-values]: Distinguish
  between 1 or another number of values.
  [mutable?, make-value-construction, maybe-unconst]: New procedures.
  Use it in <let>, <letrec>, <toplevel-define>, and <lambda-case>.

* test-suite/tests/tree-il.test ("partial evaluation"): Add tests
  for mutability preservation.
2011-09-11 00:43:23 +02:00
Ludovic Courtès
11671bbacb Add a partial evaluator for use in the compiler.
Thanks to William R. Cook for his excellent tutorial,
<http://softlang.uni-koblenz.de/dsl11/>.

* module/language/tree-il/optimize.scm (optimize!): Call `peval' unless
  the #:partial-eval? option asks otherwise.
  (peval): New procedure.

* module/language/tree-il/inline.scm: Add comment.

* module/language/tree-il/primitives.scm (*primitive-constructors*): New
  variable.
  (*effect-free-primitives*): Use it.
  (constructor-primitive?): New primitive.

* test-suite/tests/tree-il.test (assert-tree-il->glil): Extend to
  support `with-partial-evaluation', `without-partial-evaluation', and
  `with-options'.
  (peval): New binding.
  (pass-if-peval): New macro.
  ("lexical refs"): Run tests without partial evaluation.
  ("letrec"): Likewise.
  ("the or hack"): Likewise.
  ("conditional"): Likewise, for some tests.
  ("sequence"): Adjust to new generated code.
  ("partial evaluation"): New test prefix.
2011-09-09 00:05:34 +02:00
Ludovic Courtès
16a3b31611 Use (ice-9 match) instead of `record-case' where it improves readability.
* module/language/tree-il/analyze.scm (goops-toplevel-definition, const-fmt):
  Replace `record-case' by `match'.
  (format-analysis): Likewise, partially.
2011-09-06 00:18:36 +02:00
Andy Wingo
0c65f52c6d more define-syntax-rule usage
* module/ice-9/boot-9.scm:
* module/ice-9/control.scm:
* module/ice-9/futures.scm:
* module/ice-9/optargs.scm:
* module/ice-9/poll.scm:
* module/ice-9/receive.scm:
* module/ice-9/threads.scm:
* module/ice-9/vlist.scm:
* module/language/assembly/compile-bytecode.scm:
* module/language/ecmascript/compile-tree-il.scm:
* module/language/tree-il.scm:
* module/oop/goops.scm:
* module/oop/goops/simple.scm:
* module/oop/goops/stklos.scm:
* module/srfi/srfi-1.scm:
* module/srfi/srfi-35.scm:
* module/srfi/srfi-39.scm:
* module/srfi/srfi-45.scm:
* module/srfi/srfi-67/compare.scm:
* module/sxml/match.scm:
* module/system/repl/error-handling.scm:
* module/system/repl/repl.scm:
* module/system/vm/inspect.scm:
* module/texinfo.scm:
* module/web/server.scm: Use define-syntax-rule, where it makes sense.
2011-09-02 11:36:14 +02:00
Andy Wingo
8b66aa8f54 Merge remote-tracking branch 'origin/stable-2.0'
Conflicts:
	libguile/bytevectors.c
	libguile/bytevectors.h
	libguile/objcodes.c
	libguile/r6rs-ports.c
	libguile/strings.c
	libguile/vm.c
2011-08-31 09:34:54 +02:00
Andy Wingo
335c8a89a2 fix tree-il->scheme test
* module/language/tree-il.scm (tree-il->scheme): Fix incorporation of
  `lambda' in a `case-lambda'.

* test-suite/tests/tree-il.test ("tree-il->scheme"): Add a test.
2011-08-04 19:23:49 +02:00
Ludovic Courtès
86e449a699 Fix typo in (language tree-il spec).
* module/language/tree-il/spec.scm (join): Use EXPS in the error case.
2011-07-07 00:57:58 +02:00
Andy Wingo
78f0ef20a7 Merge remote-tracking branch 'local-2.0/stable-2.0'
Conflicts:
	module/ice-9/psyntax-pp.scm
	module/language/tree-il/compile-glil.scm
2011-06-18 01:07:20 +02:00
Andy Wingo
25711fa4ca fix tree-il->scheme bug for seq
* module/language/tree-il.scm (tree-il->scheme): Fix bug for `seq'.

* module/ice-9/psyntax-pp.scm: Regenerate correctly.
2011-06-18 00:38:52 +02:00
Andy Wingo
b88fef5519 fix invalid transformation of (values x) -> x, (+ x) -> x, etc
* module/language/tree-il/primitives.scm (+, *, cons*): In the case of
  just one argument (the identity case), expand to (values x) instead of
  just x.  Fixes values truncation in that case.
  (values): Likewise remove (values x) -> x translation, as the compiler
  will do it for us, and this fixes (values (values 1 2)).

* module/language/tree-il/compile-glil.scm (flatten-lambda-case): Handle
  `values' in a push context here.

* test-suite/tests/tree-il.test ("values"): Add some tests.
2011-06-17 19:42:55 +02:00
Andy Wingo
0083cb5ec4 fix self tail recursion to different case-lambda clauses
http://savannah.gnu.org/bugs/?33362

* module/language/tree-il/compile-glil.scm (flatten-lambda-case): Rename
  from flatten, as it really just takes a particular case.  Instead of
  iteratively compiling lambda cases through `comp', tail-call through
  flatten-lambda-case.  This allows code to see which case it's being
  compiled in.  Take advantage of that to limit the self-tail-call
  optimization to self-calls to the same case -- otherwise we might be
  jumping to a label without having reserved the right number of
  locals.
  (flatten-lambda): Adapt the caller.

* test-suite/tests/compiler.test ("case-lambda"): Add a test.
2011-06-17 17:08:06 +02:00
Andy Wingo
46da7909e1 Merge remote-tracking branch 'local-2.0/stable-2.0'
Conflicts:
	meta/Makefile.am
2011-06-02 22:49:33 +02:00
Andy Wingo
6fc3eae477 sequence of expressions -> seq of head and tail
* libguile/expand.h:
* module/language/tree-il.scm: Rename "sequence" to "seq", and instead
  of taking a list of expressions, take a head and a tail.

* module/language/tree-il/analyze.scm:
* module/language/tree-il/compile-glil.scm:
* module/language/tree-il/fix-letrec.scm:
* module/language/tree-il/spec.scm:
* module/language/elisp/compile-tree-il.scm:
* module/ice-9/psyntax.scm:
* module/ice-9/psyntax-pp.scm:
* module/ice-9/eval.scm:
* libguile/memoize.h:
* libguile/memoize.c:
* libguile/expand.c:
* libguile/eval.c: Adapt to the new seq format.
2011-06-02 19:13:32 +02:00