Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ltac2 Compile #17521

Closed
wants to merge 2 commits into from
Closed

Ltac2 Compile #17521

wants to merge 2 commits into from

Conversation

SkySkimmer
Copy link
Contributor

@SkySkimmer SkySkimmer commented Apr 24, 2023

This makes the example from #10107 (comment) take 0.025s instead of 0.15s (using Ltac2 Compile find). Compilation time is about 0.040s.

TODO before merge:

  • changelog
  • get the correct paths for -I to pass to ocamlopt instead of hardcoding the ones for my machine
    I guess this mostly works now

Future work:

  • optimisations (especially related to primitives, as in Ltac2 Automatic compilation to OCaml #10107 (comment))
  • handle Ltac2 Backtrace and Ltac Profiling (use with_frame in some places)
  • JIT mode (when we have to eval an expression, compile it and run instead)
  • reuse previous compilation runs (currently Ltac2 Compile foo. Ltac2 Compile foo. will compile twice)
  • cache compilation results across files
  • other minor XXX left in comments

@SkySkimmer SkySkimmer added the needs: fixing The proposed code change is broken. label Apr 24, 2023
@coqbot-app coqbot-app bot added the needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. label Apr 24, 2023
Comment on lines 960 to 964
(* TODO HACK!!! TODO get these values the proper way
(NB: we need engine for proofview, kernel for KerName.equal) *)
["-I"; "/home/gaetan/dev/coq/coq/_build/default/plugins/ltac2/.ltac2_plugin.objs/byte"] @
["-I"; "/home/gaetan/dev/coq/coq/_build/default/engine/.engine.objs/byte"] @
["-I"; "/home/gaetan/dev/coq/coq/_build/default/kernel/.kernel.objs/byte"] @
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coq/build-maintainers what is the correct way to get these paths?
I see native compile gets passed -nI but while that's OK for a kernel feature it's not so nice for a plugin.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of today we have no way to get these paths, there are some hacks (native relies on a really wild one, see Nativelib.get_load_paths) but I recommend against extending those.

I think that if a plugin (like quickchick or ltac2) need an OCaml compilation context we should extend the plugin API properly to accommodate so, there are a couple of designs, IMHO it makes sense that the compilation request is pushed to the upper layer schedulers, but YMMV here.

Also, if plugins want to cache things (beyond what libobject provides) we should provide them proper API, otherwise maintenance will be hard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if a plugin (like quickchick or ltac2) need an OCaml compilation context we should extend the plugin API properly to accommodate so, there are a couple of designs, IMHO it makes sense that the compilation request is pushed to the upper layer schedulers, but YMMV here.

I don't know what this means.

Also, if plugins want to cache things (beyond what libobject provides) we should provide them proper API, otherwise maintenance will be hard.

I'm not caching anything in this PR, everything goes to /tmp

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what this means.

What it is that you don't understand?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the "couple of designs"? (Maybe "I don't know what to do with what you said" is more accurate than "I don't know what this means")

Copy link
Member

@ejgallego ejgallego Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks. I'll try to explain a bit what I mean.

So indeed our case is that the plugins are per-sentence (or per-tactic) but want to access some more global state, in this case a compilation context, terminology we use in some build systems to denote the things that are needed to compile a file, this includes not only the flags, but libraries that have to be in place, etc...

So I see two possibilities:

  • we pass to plugins the ocaml context, and they do the call to ocaml themselves
  • we pass to plugin a callback so they can request the compilation of a file, and the callback returns the file location

Each approach has (in my view) its own drawbacks and advantages. The first one is more low-level, but is way less flexible. The second one (which I lean towards) is easier to use but gives less control to the plugins (which maybe is a good thing).

Common to both approach is other problem that we should eventually tackle: in both cases the commands actually involve some async execution (of a compiler). While this can be handled (at least in Unix OS) by doing synchronous IO, IMHO that's not so nice, and it would be nice for the vernacular interpretation to allow async commands (for example Require could be async, triggering also .vo compilation, call to smt solvers, etc...)

Does this make more sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TL; DR answer to your original question is: "we don't a way to pass such flags. We could do like native, and set some global flag, or update the plugin API to pass the missing stuff"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I see two possibilities:

In the second case, whatever layer owns the callback still needs access to the ocaml context right?
So regardless of if the ocaml context lives below or above plugins, my question is how do I get it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see what you mean, for now Coq is using two ways:

  • calling ocamlfind to get it, as done in coq_rules.ml:findlib_plugin_flags, basically Findlib.package_directory.
  • nativelib.ml will call Boot.Env.native_cmi , which is way more hacky.

Neither method does guarantee that the libraries will be actually already built, so you gotta be a bit careful with the timing of the call.

Comment on lines +943 to +982
let include_dirs () =
(* TODO make this work in -boot / dev shim mode *)
let open Boot.Env in
let env = init () in
(* engine for Proofview, kernel for Names *)
List.map (fun x -> Path.to_string (native_cmi env x))
[ "kernel"; "engine"; "plugins/ltac2" ]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with an installed Coq on my machine.
As noted in the comment it doesn't work if you want to use in the stdlib (-boot) or with the dev shims.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least it's not some hardcoded paths ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add the dep to the dev shims. For the -boot thing, @Alizter and I were discussing recently how to continue #17317 as to improve this, we could start by having Boot.Env.init return an option type.

This way, if the above fail, you can workaround it (for example by assuming a configure'd env)

@SkySkimmer SkySkimmer added needs: changelog entry This should be documented in doc/changelog. and removed needs: fixing The proposed code change is broken. help wanted labels Apr 25, 2023
@SkySkimmer SkySkimmer marked this pull request as ready for review April 25, 2023 14:06
@SkySkimmer SkySkimmer requested review from a team as code owners April 25, 2023 14:06
let a := constr:($x + ltac:(exact $y)) in
constr:($a + ltac2:(let y := z in exact $y)).

Set Printing Ltac2 Extension Used Variables.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for debug, do we want to keep it?

@SkySkimmer SkySkimmer added request: full CI Use this label when you want your next push to trigger a full CI. and removed needs: changelog entry This should be documented in doc/changelog. labels Apr 25, 2023
@coqbot-app coqbot-app bot removed request: full CI Use this label when you want your next push to trigger a full CI. needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. labels Apr 25, 2023
@SkySkimmer
Copy link
Contributor Author

Did the no compilation mode change?
@coqbot bench

Copy link
Member

@jfehrle jfehrle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks useful. Perhaps the doc could say something about how roughly much of a speedup is possible or to expect.

doc/changelog/06-Ltac2-language/17521-tac2compile.rst Outdated Show resolved Hide resolved
doc/sphinx/proof-engine/ltac2.rst Outdated Show resolved Hide resolved
doc/sphinx/proof-engine/ltac2.rst Outdated Show resolved Hide resolved
doc/sphinx/proof-engine/ltac2.rst Outdated Show resolved Hide resolved
doc/sphinx/proof-engine/ltac2.rst Outdated Show resolved Hide resolved
Comment on lines +1743 to +1724
Compiled code currently does not respect :flag:`Ltac Profiling` and
:flag:`Ltac2 Backtrace`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect it will not play nicely with the debugger, either, when that becomes available. But not an issue for today.

doc/sphinx/proof-engine/ltac2.rst Outdated Show resolved Hide resolved
@coqbot-app
Copy link
Contributor

coqbot-app bot commented Apr 27, 2023

🏁 Bench results:

┌──────────────────────────────┬─────────────────────────┬───────────────────────────────────────┬───────────────────────────────────────┬─────────────────────────┐
│                              │      user time [s]      │              CPU cycles               │           CPU instructions            │  max resident mem [KB]  │
│                              │                         │                                       │                                       │                         │
│         package_name         │   NEW      OLD    PDIFF │      NEW             OLD        PDIFF │      NEW             OLD        PDIFF │   NEW      OLD    PDIFF │
├──────────────────────────────┼─────────────────────────┼───────────────────────────────────────┼───────────────────────────────────────┼─────────────────────────┤
│                 coq-compcert │  286.39   289.10  -0.94 │  1279793235099   1291205644056  -0.88 │  1966566763332   1966782770181  -0.01 │ 1127296  1128624  -0.12 │
│               coq-coquelicot │   36.80    36.96  -0.43 │   162677491408    162488995135   0.12 │   225158424941    225120906043   0.02 │  785460   782096   0.43 │
│                   coq-stdlib │  413.40   415.18  -0.43 │  1754354190268   1757378463433  -0.17 │  1453131293705   1453243976128  -0.01 │  637332   641004  -0.57 │
│                   coq-geocoq │  605.86   608.18  -0.38 │  2730785383909   2741644032578  -0.40 │  4325737554054   4325948522943  -0.00 │  908544   908348   0.02 │
│       coq-mathcomp-ssreflect │   27.59    27.69  -0.36 │   123256915390    124050591042  -0.64 │   161246790243    161313264882  -0.04 │  576616   576624  -0.00 │
│                  coq-bignums │   27.96    28.06  -0.36 │   125631476091    126394039653  -0.60 │   181059356220    181089930984  -0.02 │  485632   482524   0.64 │
│                    coq-color │  228.20   228.95  -0.33 │  1015012870875   1020474077078  -0.54 │  1485334032104   1485798724593  -0.03 │ 1136208  1134036   0.19 │
│                coq-perennial │ 5216.84  5233.20  -0.31 │ 23590713725618  23655081787639  -0.27 │ 38980415052551  38975201266404   0.01 │ 1976232  1977536  -0.07 │
│       coq-mathcomp-odd-order │  400.17   401.37  -0.30 │  1819718150613   1823304254910  -0.20 │  3058191644994   3057095964211   0.04 │ 1568460  1568340   0.01 │
│                      coq-vst │  870.07   871.64  -0.18 │  3911319053574   3922102855752  -0.27 │  6478105693537   6479133894605  -0.02 │ 2120048  2122380  -0.11 │
│                 coq-coqprime │   45.95    46.03  -0.17 │   204053670288    205084272239  -0.50 │   313130178432    313132501004  -0.00 │  777564   777636  -0.01 │
│       coq-mathcomp-character │   63.59    63.64  -0.08 │   288375067532    288446735054  -0.02 │   441489879195    441432885485   0.01 │  697876   697608   0.04 │
│         coq-metacoq-template │  126.12   126.20  -0.06 │   557630797917    557179684206   0.08 │   906289972663    906187037205   0.01 │ 1265172  1263952   0.10 │
│                     coq-hott │  149.63   149.71  -0.05 │   670315284659    670618014631  -0.05 │  1066402018583   1066378677805   0.00 │  620496   620356   0.02 │
│                coq-fourcolor │ 1506.29  1507.01  -0.05 │  6645936872242   6649852149789  -0.06 │ 12126433801604  12127125102583  -0.01 │ 1278452  1278448   0.00 │
│ coq-fiat-crypto-with-bedrock │ 6142.48  6144.07  -0.03 │ 27613500379368  27623670290059  -0.04 │ 51078932031435  51010100858144   0.13 │ 2406552  2405636   0.04 │
│          coq-metacoq-erasure │  200.48   200.50  -0.01 │   898469776396    898808879332  -0.04 │  1444056590821   1444091428323  -0.00 │ 1510660  1510228   0.03 │
│        coq-engine-bench-lite │  156.51   156.44   0.04 │   665660554111    665589786822   0.01 │  1246369526909   1244235060947   0.17 │ 1102620  1102564   0.01 │
│             coq-fiat-parsers │  332.22   332.04   0.05 │  1462512118414   1461797293991   0.05 │  2450091079959   2450530912055  -0.02 │ 3462388  3462820  -0.01 │
│                 coq-bedrock2 │  315.68   315.50   0.06 │  1424382356147   1421398512194   0.21 │  2808649915688   2808131630911   0.02 │  868468   871304  -0.33 │
│                 coq-rewriter │  353.01   352.74   0.08 │  1591240840255   1589810251325   0.09 │  2647388343821   2643436862237   0.15 │ 1332344  1298496   2.61 │
│                  coq-unimath │ 1467.68  1466.48   0.08 │  6635024766654   6632744517384   0.03 │ 12593024777596  12589417096921   0.03 │ 1330064  1330396  -0.02 │
│        coq-mathcomp-fingroup │   21.90    21.88   0.09 │    98565927657     98516565972   0.05 │   144739634211    144792741928  -0.04 │  491556   492464  -0.18 │
│   coq-performance-tests-lite │  765.29   764.58   0.09 │  3412027076590   3407467060777   0.13 │  6062150716647   6043934140224   0.30 │ 1869128  1869036   0.00 │
│             coq-math-classes │   85.93    85.83   0.12 │   384547029743    384517093638   0.01 │   539704106297    539898843701  -0.04 │  518560   518248   0.06 │
│                  coq-coqutil │   40.01    39.95   0.15 │   175625167513    176404460956  -0.44 │   256010005554    255470602571   0.21 │  558260   558340  -0.01 │
│         coq-mathcomp-algebra │   61.98    61.87   0.18 │   280162455346    279768184698   0.14 │   391649394252    391698841702  -0.01 │  578844   579264  -0.07 │
│     coq-metacoq-translations │   14.54    14.51   0.21 │    63525563113     63607875238  -0.13 │   103404557928    103457368654  -0.05 │  754064   754828  -0.10 │
│  coq-rewriter-perf-SuperFast │  749.06   747.37   0.23 │  3339361730613   3329462592185   0.30 │  5817653276069   5786307830498   0.54 │ 1310484  1311520  -0.08 │
│                    coq-verdi │   47.49    47.38   0.23 │   212452562922    212242794448   0.10 │   326604885238    326738413329  -0.04 │  524408   525620  -0.23 │
│                     coq-corn │  785.17   782.83   0.30 │  3538100886825   3525881942844   0.35 │  5518167506504   5517127465762   0.02 │  793316   793760  -0.06 │
│               coq-verdi-raft │  561.41   559.60   0.32 │  2544305894314   2535275242739   0.36 │  3993466236627   3993558723544  -0.00 │  824732   826800  -0.25 │
│                coq-fiat-core │   60.94    60.66   0.46 │   256377407138    256513594980  -0.05 │   377758721078    377720963344   0.01 │  490220   491648  -0.29 │
│           coq-mathcomp-field │   78.72    78.35   0.47 │   356382534435    354551909901   0.52 │   577807466348    577658279404   0.03 │  905856   905824   0.00 │
│            coq-iris-examples │  475.43   473.09   0.49 │  2138542902775   2128596621949   0.47 │  3265499978113   3265499844092   0.00 │ 1034620  1034456   0.02 │
│      coq-metacoq-safechecker │  210.06   209.01   0.50 │   949797562675    945085878854   0.50 │  1432046300891   1432288177927  -0.02 │ 1404848  1408524  -0.26 │
│          coq-category-theory │  727.41   723.25   0.58 │  3299568665166   3281585616084   0.55 │  5645843590633   5646034468415  -0.00 │  878288   879536  -0.14 │
│        coq-mathcomp-solvable │   76.99    76.49   0.65 │   347406582842    345829761626   0.46 │   535352875479    535364039153  -0.00 │  754096   755076  -0.13 │
│            coq-metacoq-pcuic │  554.86   549.86   0.91 │  2496364144164   2475945510734   0.82 │  3673336943010   3673207312178   0.00 │ 1900748  1902720  -0.10 │
│                     coq-core │  115.97   114.76   1.05 │   460445126087    454354400651   1.34 │   485160232163    483497700146   0.34 │  288072   289276  -0.42 │
└──────────────────────────────┴─────────────────────────┴───────────────────────────────────────┴───────────────────────────────────────┴─────────────────────────┘

🐢 Top 25 slow downs
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                              TOP 25 SLOW DOWNS                                                              │
│                                                                                                                                             │
│   OLD      NEW     DIFF    %DIFF    Ln                     FILE                                                                             │
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 59.9250  61.2550  1.3300    2.22%    27  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ToFancyWithCasts.v.html                           │
│ 20.5060  21.4980  0.9920    4.84%    40  coq-fiat-crypto-with-bedrock/src/PushButtonSynthesis/SolinasReductionReificationCache.v.html       │
│ 79.9820  80.7560  0.7740    0.97%   618  coq-fiat-crypto-with-bedrock/rupicola/bedrock2/bedrock2/src/bedrock2Examples/lightbulb.v.html      │
│ 81.0910  81.8600  0.7690    0.95%    20  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/NBE.v.html                                        │
│  4.0830   4.7760  0.6930   16.97%    22  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ArithWithCasts.v.html                             │
│ 80.2890  80.9750  0.6860    0.85%   618  coq-bedrock2/bedrock2/src/bedrock2Examples/lightbulb.v.html                                        │
│ 63.0170  63.6890  0.6720    1.07%   139  coq-fiat-parsers/src/Parsers/Refinement/SharpenedJSON.v.html                                       │
│ 10.2760  10.7490  0.4730    4.60%   304  coq-fiat-crypto-with-bedrock/src/Fancy/Montgomery256.v.html                                        │
│ 27.2390  27.6760  0.4370    1.60%    68  coq-fiat-crypto-with-bedrock/rupicola/bedrock2/deps/riscv-coq/src/riscv/Proofs/VerifyDecode.v.html │
│  7.3900   7.7850  0.3950    5.35%    35  coq-fiat-crypto-with-bedrock/src/PushButtonSynthesis/SolinasReductionReificationCache.v.html       │
│ 27.6970  28.0760  0.3790    1.37%  1387  coq-fourcolor/theories/cfmap.v.html                                                                │
│  3.2920   3.6650  0.3730   11.33%    24  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/MultiRetSplit.v.html                              │
│ 34.4710  34.8210  0.3500    1.02%    12  coq-fourcolor/theories/job001to106.v.html                                                          │
│ 10.9630  11.2880  0.3250    2.96%    55  coq-category-theory/Construction/Comma/Natural/Transformation.v.html                               │
│ 23.9720  24.2910  0.3190    1.33%    12  coq-fourcolor/theories/job511to516.v.html                                                          │
│ 29.7270  30.0310  0.3040    1.02%    12  coq-fourcolor/theories/job611to617.v.html                                                          │
│ 40.3020  40.5990  0.2970    0.74%   224  coq-performance-tests-lite/PerformanceExperiments/rewrite_lift_lets_map.v.html                     │
│ 11.3980  11.6800  0.2820    2.47%  1028  coq-unimath/UniMath/CategoryTheory/LocalizingClass.v.html                                          │
│  1.9750   2.2520  0.2770   14.03%  1336  coq-perennial/src/program_proof/aof/proof.v.html                                                   │
│ 16.4930  16.7600  0.2670    1.62%   483  coq-verdi-raft/raft-proofs/EndToEndLinearizability.v.html                                          │
│ 22.0540  22.3170  0.2630    1.19%    23  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/Arith.v.html                                      │
│  0.0290   0.2900  0.2610  900.00%   208  coq-fiat-crypto-with-bedrock/src/PushButtonSynthesis/DettmanMultiplication.v.html                  │
│  5.3530   5.6130  0.2600    4.86%     5  coq-fiat-crypto-with-bedrock/src/Assembly/Parse/Examples/fiat_p256_square_optimised_seed6.v.html   │
│  0.0000   0.2600  0.2600     inf%   461  coq-fiat-crypto-with-bedrock/src/Bedrock/Field/Synthesis/New/Signature.v.html                      │
│  0.0000   0.2570  0.2570     inf%   957  coq-fiat-crypto-with-bedrock/src/PushButtonSynthesis/Primitives.v.html                             │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

🐇 Top 25 speed ups
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                   TOP 25 SPEED UPS                                                                   │
│                                                                                                                                                      │
│   OLD       NEW      DIFF     %DIFF     Ln                      FILE                                                                                 │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 130.7530  129.6920  -1.0610    -0.81%   999  coq-performance-tests-lite/src/fiat_crypto_via_setoid_rewrite_standalone.v.html                         │
│ 130.8170  129.8680  -0.9490    -0.73%   968  coq-performance-tests-lite/src/fiat_crypto_via_setoid_rewrite_standalone.v.html                         │
│ 127.2660  126.7310  -0.5350    -0.42%    22  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ArithWithCasts.v.html                                  │
│  10.9420   10.5280  -0.4140    -3.78%   326  coq-fiat-crypto-with-bedrock/src/Fancy/Barrett256.v.html                                                │
│  30.0880   29.6870  -0.4010    -1.33%    12  coq-fourcolor/theories/job531to534.v.html                                                               │
│  17.2260   16.8300  -0.3960    -2.30%    12  coq-fourcolor/theories/job215to218.v.html                                                               │
│  29.0230   28.6560  -0.3670    -1.26%    12  coq-fourcolor/theories/job517to530.v.html                                                               │
│ 113.0070  112.6560  -0.3510    -0.31%    48  coq-fiat-crypto-with-bedrock/src/Curves/Weierstrass/AffineProofs.v.html                                 │
│   0.4260    0.0870  -0.3390   -79.58%  1332  coq-perennial/src/program_proof/aof/proof.v.html                                                        │
│  38.8880   38.5640  -0.3240    -0.83%   835  coq-fiat-crypto-with-bedrock/src/Fancy/Compiler.v.html                                                  │
│ 159.9690  159.6470  -0.3220    -0.20%   233  coq-fiat-crypto-with-bedrock/rupicola/bedrock2/deps/riscv-coq/src/riscv/Proofs/DecodeByExtension.v.html │
│   3.5890    3.2790  -0.3100    -8.64%   477  coq-perennial/src/program_proof/memkv/bank_proof.v.html                                                 │
│  28.1570   27.8630  -0.2940    -1.04%    12  coq-fourcolor/theories/job299to302.v.html                                                               │
│   0.2610    0.0000  -0.2610  -100.00%   462  coq-fiat-crypto-with-bedrock/src/Bedrock/Field/Synthesis/New/Signature.v.html                           │
│  28.0750   27.8160  -0.2590    -0.92%  1605  coq-perennial/src/program_proof/simplepb/simplelog/proof.v.html                                         │
│   0.2650    0.0080  -0.2570   -96.98%   968  coq-fiat-crypto-with-bedrock/src/PushButtonSynthesis/Primitives.v.html                                  │
│  20.8440   20.5950  -0.2490    -1.19%    12  coq-fourcolor/theories/job271to278.v.html                                                               │
│  41.3060   41.0660  -0.2400    -0.58%   236  coq-rewriter-perf-SuperFast/src/Rewriter/Rewriter/Examples/PerfTesting/LiftLetsMap.v.html               │
│   2.5220    2.2890  -0.2330    -9.24%   251  coq-perennial/src/program_proof/memkv/bank_proof.v.html                                                 │
│  41.2740   41.0440  -0.2300    -0.56%   827  coq-vst/veric/binop_lemmas4.v.html                                                                      │
│  15.3290   15.1010  -0.2280    -1.49%    81  coq-fiat-crypto-with-bedrock/rupicola/src/Rupicola/Examples/Utf8/Utf8.v.html                            │
│   8.0210    7.7950  -0.2260    -2.82%    27  coq-rewriter/src/Rewriter/Rewriter/Examples/PerfTesting/UnderLetsPlus0.v.html                           │
│   3.2840    3.0760  -0.2080    -6.33%     6  coq-fiat-crypto-with-bedrock/src/PerfTesting/PerfTestSearch.v.html                                      │
│  32.5130   32.3130  -0.2000    -0.62%    12  coq-fourcolor/theories/job107to164.v.html                                                               │
│  28.1280   27.9320  -0.1960    -0.70%    12  coq-fourcolor/theories/job291to294.v.html                                                               │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

@gares
Copy link
Member

gares commented Apr 27, 2023

While I think the ability to compile to OCaml is the natural future of Ltac2, the limitations of this PR makes me think it should be an effort with some precise planning.

At the same time the original perf problem seems to be about finding a variable in a term, and one can surely provide this feature as a builtin, and that is way simpler than compiling arbitrary code.

In a sense I've mixed feelings about solving that problem with this PR.

@coqbot-app coqbot-app bot added the needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. label Apr 27, 2023
@SkySkimmer
Copy link
Contributor Author

At the same time the original perf problem seems to be about finding a variable in a term, and one can surely provide this feature as a builtin, and that is way simpler than compiling arbitrary code.

Doing that for every feature does not sound simple at all to me.

@SkySkimmer SkySkimmer added kind: feature New user-facing feature request or implementation. kind: performance Improvements to performance and efficiency. part: ltac2 Issues and PRs related to the (in development) Ltac2 tactic langauge. labels Apr 27, 2023
@ppedrot
Copy link
Member

ppedrot commented Apr 27, 2023

At the very least this feature should be really opt-in, because we can break all kind of soundness invariants with it. I think it deserves some serious discussion before potentially merging.

@SkySkimmer
Copy link
Contributor Author

It is certainly optin, but what soundness invariants are you thinking about?

@jfehrle
Copy link
Member

jfehrle commented Apr 27, 2023

While I think the ability to compile to OCaml is the natural future of Ltac2, the limitations of this PR makes me think it should be an effort with some precise planning.

Agreed. Further thoughts:

  • If compiling makes the Ltac2 run much faster and it's not expensive, many users will want to compile all Ltac2 tactics rather than figuring out precisely what routines are slow and listing them in the Ltac2 Compile. There should be command syntax to support this case.

  • It's ironic that you can use this option to speed up your code, but once you've done that, you're running blind because you can't profile compiled code. I expect it wouldn't be too hard to support profiling compiled code, but probably in a different PR.

  • In IDEs, we should aim for a model in which the user changes various scripts, then presses a function key to save files, which builds everything that needs it (including dependencies), and notifies the user of any Coq sessions that depend on the changed files (and perhaps even tries to rerun scripts in those sessions). For compiling .v files, that's not too hard to support.

    We should try to provide similar behavior for Ltac2 Compile, which would mean keeping track of what files each compiled tactic depends on. One corner case to consider: Consider top.v that uses a compiled tactic my_tac defined in mylib.v. The user steps part way through top.v, causing compilation of my_tac, and calls it in multiple places. Then they tweak my_tac. IIUC my_tac wouldn't get recompiled automatically at all. The IDE should remind the user of the change and/or reset the session for top.v. (Having both old and new versions of my_tac applied in a session seems like a bad idea--likely to confuse the user and could make the implementation more complex.)

@SkySkimmer SkySkimmer force-pushed the tac2compile branch 2 times, most recently from 0241dc8 to 11a23e2 Compare April 28, 2023 13:38
@github-actions github-actions bot added the needs: rebase Should be rebased on the latest master to solve conflicts or have a newer CI run. label May 1, 2023
@coqbot-app coqbot-app bot removed the needs: rebase Should be rebased on the latest master to solve conflicts or have a newer CI run. label May 2, 2023
@SkySkimmer
Copy link
Contributor Author

At the very least this feature should be really opt-in, because we can break all kind of soundness invariants with it. I think it deserves some serious discussion before potentially merging.

Offline we discussed that compilation does not use Obj.magic or other ocaml nontypechecking features, so even with the possibility of compiler bugs it should still break less invariants than eg Coq module application (which doesn't check Ltac2 types)

@ppedrot ppedrot self-assigned this May 22, 2023
SkySkimmer added a commit to SkySkimmer/coq that referenced this pull request Jun 6, 2023
The core API makes it possible to register a `valexpr` for each
`ltac_constant`.

For functional `ltac_constant` (the vast majority) this will be a
closure, which in valexpr are represented as arbitrary OCaml closures.
This allows arbitrary compilation schemes, eg
- dynlinked OCaml code as in
  coq#17521 (like native_compute)
- compiling to the VM eg
  `let code = compile expr in mk_closure arity (fun args -> interpret code args)`
- partially evaluating the tacexpr and sending it back to the normal evaluator
  `let e = partial_eval e in mk_closure arity (fun args -> interp (TacApp (e, args))`

and any other scheme someone might come up with.

We also expose some convenient APIs.
@SkySkimmer
Copy link
Contributor Author

After discussion we will avoid committing to this particular compilation model and instead expose the hook APIs to make it possible as a separate plugin (non coq-core for now)

This will be useful for compilation, because it will need to rebuild
the interpretation env to interp extensions.
SkySkimmer added a commit to SkySkimmer/coq that referenced this pull request Jun 7, 2023
The core API makes it possible to register a `valexpr` for each
`ltac_constant`.

For functional `ltac_constant` (the vast majority) this will be a
closure, which in valexpr are represented as arbitrary OCaml closures.
This allows arbitrary compilation schemes, eg
- dynlinked OCaml code as in
  coq#17521 (like native_compute)
- compiling to the VM eg
  `let code = compile expr in mk_closure arity (fun args -> interpret code args)`
- partially evaluating the tacexpr and sending it back to the normal evaluator
  `let e = partial_eval e in mk_closure arity (fun args -> interp (TacApp (e, args))`

and any other scheme someone might come up with.

We also expose some convenient APIs.
@SkySkimmer
Copy link
Contributor Author

We will do #17696 instead

@SkySkimmer SkySkimmer closed this Jun 7, 2023
SkySkimmer added a commit to SkySkimmer/coq that referenced this pull request Jun 7, 2023
The core API makes it possible to register a `valexpr` for each
`ltac_constant`.

For functional `ltac_constant` (the vast majority) this will be a
closure, which in valexpr are represented as arbitrary OCaml closures.
This allows arbitrary compilation schemes, eg
- dynlinked OCaml code as in
  coq#17521 (like native_compute)
- compiling to the VM eg
  `let code = compile expr in mk_closure arity (fun args -> interpret code args)`
- partially evaluating the tacexpr and sending it back to the normal evaluator
  `let e = partial_eval e in mk_closure arity (fun args -> interp (TacApp (e, args))`

and any other scheme someone might come up with.

We also expose some convenient APIs.
@SkySkimmer SkySkimmer deleted the tac2compile branch June 8, 2023 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: feature New user-facing feature request or implementation. kind: performance Improvements to performance and efficiency. needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. part: ltac2 Issues and PRs related to the (in development) Ltac2 tactic langauge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants