Commit graph

148 commits

Author SHA1 Message Date
Leonardo de Moura
a863f1b8a3 fix: fixes #616 2021-08-07 07:29:54 -07:00
Sebastian Ullrich
2ca4d2693f feat: trace.compiler.inline 2021-06-17 11:25:58 +02:00
Leonardo de Moura
597635c074 fix: is_recursive_fn 2021-02-25 19:48:12 -08:00
Leonardo de Moura
d494756d00 fix: inline loop 2021-02-04 17:17:51 -08:00
Leonardo de Moura
d87b8b876f chore: add temporary workaround
cc @Kha
2020-10-28 16:57:46 -07:00
Leonardo de Moura
827625a377 perf: add temporary hack for performance issue
The compiler frontend implemented in C++ is eagerly inlining local
functions. The new test would take an absurd amount of time without
the new hack.
We remove this hack when we re-implement the compiler frontend in Lean.
2020-10-15 13:37:29 -07:00
Leonardo de Moura
e301d57b36 chore: fix comment 2020-08-10 08:39:22 -07:00
Leonardo de Moura
7c76a19885 chore: fix includes 2020-05-22 14:17:25 -07:00
Leonardo de Moura
8bdca35282 chore: use #include <lean/runtime/...> for runtime .h files 2020-05-18 11:30:07 -07:00
Leonardo de Moura
bc57faff08 fix: ensure compiler representation invariants are preserved 2020-04-23 13:54:14 -07:00
Leonardo de Moura
271b753f30 feat: optimization for Bool+Decidable code 2020-02-27 16:59:18 -08:00
Leonardo de Moura
0cf226220c fix: remove incorrect assertion
Note that `get_cases_on_minors_range` is now parametric on `m_before_erasure`:
`get_cases_on_minors_range(env(), const_name(fn), m_before_erasure)`
2020-02-13 18:26:12 -08:00
Leonardo de Moura
66304d83a0 chore(library/init/lean/compiler): export as C functions 2019-08-17 06:58:36 -07:00
Leonardo de Moura
f1eaebba31 fix(library/compiler/csimp): bug at float_cases_on
The scope of the expr2ctor cache updates was incorrect.
This bug affects code of the form
```
let x := C.cases_on y ...; K[x]
```
when we try to float the `cases_on` application, and the continuation
`K[x]` contains another `cases_on` application with major `y`.
The new test exposes the bug.
This commit also fixes the case where the continuation `K[x]` projects `y`.

Fixes #26
2019-08-05 13:23:27 -07:00
Leonardo de Moura
99e90f0410 chore(library/compiler): add trace.compiler.simp_float_cases option 2019-08-05 13:13:18 -07:00
Sebastian Ullrich
55104ad0c6 chore(library/compiler/csimp): remove wrong assertion 2019-07-30 17:52:43 -07:00
Leonardo de Moura
1cdadba4b2 fix(library/compiler/csimp): type error
@kha This is another instance of a problem we discussed last summer.
I guess there are many more instances like this one that we are not handling.
Recall that we want to preserve types at `csimp` because we eventually
want to allow users to use equational theorems as rewriting rules during
compilation.
However, some of the transformations that `csimp` perform do not
preserve typeability in CIC.
Moreover, some of the optimizations require type inference.

I see the possible long term solutions:

1- Erase types and proofs as soon as possible. The main drawback here is
that we would have to develop an approach for translating Lean theorems
into valid rewriting rules for lambda pure. For example, the following
theorem should not be used as a rewriting rule after we erase types.
```
forall (xs : List Unit) (f : Unit -> Unit), List.map f xs = List.map id xs
```
BTW, I don't want to abandon the idea of using lemmas as rewriting rules in
the compiler.

2- Go over `csimp` and other compiler steps and make sure they work
properly even when `type_checker::infer_type` fails.
2019-06-03 17:36:40 -07:00
Leonardo de Moura
f1d16c261d fix(library/compiler/csimp): do not inline constants with [init] attribute 2019-05-11 17:51:46 -07:00
Leonardo de Moura
ee0851921b fix(library/compiler/csimp): missing simplification opportunity 2019-04-22 13:15:08 -07:00
Leonardo de Moura
b76deb4f0d fix(tests/playground/parser/syntax): another issue with float let inwards
@kha I keep finding problems with the float `let` inwards
transformation. It is always a nasty interaction between this
transformation and the `reset/reuse` insertion procedure.

The example I used in the new comment can be modified to a
`casesOn` with more than one branch (e.g., `Option.casesOn`).
Suppose we wrote
```
let o : Option Nat := Array.index a i in
let a              := Array.update a i none in
Option.casesOn o
  none
  (fun n, some (Array.update a i (some (n + 1))))
```
In the example above, the compiler will float
`a := Array.update a i none` inwards.
```
let o : Option Nat := Array.index a i in
Option.casesOn o
  none
  (fun n,
   let a := Array.update a i none in
   some (Array.update a i (some (n + 1))))
```
Then, adding reset/reuse:
```
let o : Option Nat := Array.index a i in
Option.casesOn o
  none
  (fun n,
   let o := reset o in
   let a := Array.update a i none in
   let n := n + 1 in
   let o := reuse o (some n)
   some (Array.update a i o))
```
Similarly to the example in the new comment, the `reset o` will fail since
the array `a` would still have a reference to `o`.

Remarks:
- Haskell also implements float `let` inwards.
- I am not sure how important the float `let` inward transformation is.
- I can see other nasty interactions after we implement user-defined
  simplification rules. For example, I guess many users would find the
  following lemma to be a good rewriting rule:
  ```
  (Array.update (Array.update a i v) i w) = (Array.update a i w)
  ```
  However, if we use this lemma in the example above, then `Array.update a i none` will be eliminated,
  and `reset o` will fail.
2019-04-19 14:52:52 -07:00
Leonardo de Moura
d22debefe3 fix(library/compiler/csimp): move to branch optimization
@kha The move `x := val` to `casesOn` branch was producing nasty
problems. I documented the issue, and implemented a simple
and sufficient condition for preventing the problem. The approach is very
similar to the one used at `push_proj_fn` at `llnf.cpp`.
I hope this change will not impact existing benchmarks :)
2019-04-18 16:34:08 -07:00
Leonardo de Moura
35d54c17bd chore(library/compiler): remove [inline2] attribute
We may add it back in the future if we find compelling applications for
it. Right now, we don't have any.
2019-04-18 13:24:20 -07:00
Leonardo de Moura
0ea944ad9f feat(library/compiler/csimp): add transformation to complement eager lambda lifting 2019-04-18 13:12:11 -07:00
Leonardo de Moura
f12e580ac2 chore(library/compiler/csimp): compilation error in debug mode 2019-04-17 07:41:48 -07:00
Leonardo de Moura
7461bf9d1d fix(library/compiler/extract_closed): do not inline closed constants that have been extracted 2019-04-11 14:12:13 -07:00
Leonardo de Moura
0c9fe3c7d4 feat(library/compiler): add [inline2] attribute, and stage2 inlining
This feature is useful since it allows us to perform inlining
after lambda lifting has been performed.
2019-04-06 08:00:58 -07:00
Leonardo de Moura
50d2488946 fix(library/compiler/csimp): cases-merging was failing when scrutinee was a constant 2019-04-05 17:24:01 -07:00
Leonardo de Moura
1e198ca72e fix(library/compiler/csimp): missing optimization opportunity 2019-04-05 16:16:24 -07:00
Leonardo de Moura
7b835f3d02 feat(library/compiler/csimp): keep simplifying if cse and elim_dead_let reduced expression
Both `cse` and `elim_dead_let` may create new simplification opportunities for `csimp`.
2019-04-05 15:39:43 -07:00
Leonardo de Moura
6c44a5d997 feat(library/compiler/csimp): add Thunk.get (Thunk.mk f) ==> f () simplification 2019-04-05 14:55:48 -07:00
Leonardo de Moura
994ca779ef feat(library/compiler/csimp): improve try_inline_instance 2019-04-05 14:16:38 -07:00
Leonardo de Moura
3d393d9b1d fix(library/compiler/csimp): bug introduced earlier today 2019-04-02 17:21:25 -07:00
Leonardo de Moura
6ab935ebf6 feat(library/compiler/csimp): merge equal casesOn branches 2019-04-02 11:06:07 -07:00
Leonardo de Moura
95ca283854 chore(library/compiler/csimp): remove m_proj2var, we can use m_var2ctor instead 2019-04-02 10:05:20 -07:00
Leonardo de Moura
005d62185d feat(library/compiler/csimp): add "case merging" optimization 2019-04-02 09:41:53 -07:00
Leonardo de Moura
8c58314b84 fix(library/compiler/csimp): lc_unreachable simplifications
The simplifications were introducing dangling free variables.
2019-03-28 17:32:41 -07:00
Leonardo de Moura
1ebdb9f1b1 fix(library/compiler): do not inline definitions using unsafe inductives
This commit also makes sure that `has_trivial_structure` returns false
for `unsafe` inductive datatypes.

See new test for further details.
2019-03-28 16:07:57 -07:00
Leonardo de Moura
42fbe3c18c chore(library/init,runtime,library/compiler): add fix primitive back
The new `partial def`s allow us to define `fix` in Lean, but the Lean
implementation is not as efficient as the native one. The native one
in C++ use weak pointers to prevent a closure allocation at every
recursive invocation.

This commit also fixes the `fixCore` helper functions that were broken
after we switched to camelCase.

We have updated the test `fix1.lean` to demonstrate the native
implementation is faster. Here are the numbers on my desktop.

```
./run.sh fix1.lean 24
721420279
Time for 'native fix': 816ms
721420279
Time for 'fix in lean': 1.34s
```
2019-03-27 17:13:53 -07:00
Leonardo de Moura
a43a40b7f5 chore(library/init): remove fix.lean
`partial def` is much more general
2019-03-27 13:11:00 -07:00
Leonardo de Moura
87cab24a1d fix(library/compiler/csimp): at_most_once at elim_jp1_fn
`elim_jp1_fn` was incorrectly expanding join points that were used more
than once. The issue is that the `foreach` combinator "may" skip nodes
that have already been visited.
2019-03-25 14:19:11 -07:00
Leonardo de Moura
590e40cb7b chore(library/compiler/csimp): leftover 2019-03-14 16:00:59 -07:00
Leonardo de Moura
d3ba9ef7fa feat(library/compiler/csimp): eliminate join points that are used only once 2019-03-14 10:43:35 -07:00
Leonardo de Moura
3fe6858a93 feat(library/compiler/csimp): make csimp simplifies unreachable branches
`let x := lc_unreachable in e` => `lc_unreachable`
`let x := e in lc_unreachable` => `lc_unreachable`
2019-03-13 11:45:40 -07:00
Leonardo de Moura
88453f037c feat(library/compiler/csimp): erase base argument from fix_core 2019-03-12 18:05:41 -07:00
Leonardo de Moura
609b8e87e5 feat(library/compiler/csimp): add fix_core_n => fix_core_m "eta-expansion-like" optimization
After this commit, `fix_1.lean` is not slower than `fix.lean` anymore.
2019-03-11 14:41:23 -07:00
Leonardo de Moura
b8cee758a5 feat(library/compiler/llnf): add push_proj_fn 2019-02-20 13:20:27 -08:00
Leonardo de Moura
54985b5a0e fix(library/compiler/csimp): accidentally removed nat.succ x ==> x + 1 transformation from csimp 2019-02-16 12:05:17 -08:00
Leonardo de Moura
e84f7744c3 feat(library/init/lean/compiler/const_folding): const fold nat.succ and char.of_nat 2019-02-16 11:15:19 -08:00
Leonardo de Moura
ab45af5936 fix(library/compiler/csimp): avoid potential expensive reduction at csimp
`whnf_core(e)` uses `whnf` to reduce the major premise of recursors and
projections, and `whnf` unfolds arbitrary definitions.
This commit adds a new option (`cheap`) to `whnf_core`. When
`whnf_core(e, true)` is used, the type checker will not unfolding
definitions when reducing the major premises.
2019-02-15 17:43:21 -08:00
Leonardo de Moura
e0fd89e165 feat(library/init/lean/compiler): fold nat predicates 2019-02-15 16:17:16 -08:00