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

Final steps of using the specialize keyword in compiled programs #775

Draft
wants to merge 23 commits into
base: develop
Choose a base branch
from

Conversation

adamssonj
Copy link
Contributor

@adamssonj adamssonj commented Aug 14, 2023

This PR adds support for specializing in compiled programs by adding the "JIT" compilation of the residual expression, the missing step as previously mentioned in #749. The jitting is based on Lars' sketch of dynamic linking, https://github.com/larshum/miking/tree/jit/demo.

Overview

The specialize keyword is compiled as: specialize expr jitCompile (peval (liftExpr expr)) where this PR contains the implementation of jitCompile (defined in peval/jit.mc) that is broken down into the main steps of:

  1. Compiling the MCore expression to OCaml
  2. Storing the residual OCaml code in a plugin.ml file on a format as defined by src/boot/lib/inter.ml and compiling that file
  3. Dynamically loading the plugin file using the Dynlink module of OCaml
    a. When dynamically loaded, the plugin registers itself using src/boot/lib/inter.ml
  4. Fetching the compiled expression through the interface file s.t. it can be used in the program

Steps 1 and 2 are performed in stdlib/ocaml/mcore.mc -> compileMCorePlugin. Steps 3 and 4 are defined using intrinsics

Explanation of some specifics

Note that the residual expression (produces by peval) may contain definitions of the main program, so the plugin we dynlink in may contain references to things that are only defined in the main program. So to support this a couple of things was done:

  1. Programs containing the specialize keyword are compiled to two separate files
    a. A program.ml file that contains the usual compiled program except that the last expression of the program is let-bound to a 'main' entrypoint, and a main.ml file that simply calls the main entrypoint.
    b. This split was required to ensure that the program.ml module is "loaded" before dynlinking happens (OCaml specific problem with Dynlink module)
  2. Names (identifiers) in the residual expression and the main program must be compiled to the same OCaml string. To ensure this we predefine strings for shared names that the compilation of residual expressions and the main program use.
    a. This is the reason for the "nameMap" addition to the compiler hooks

Some caveats:

  • Compiling programs containing the specialize keyword is done without dune, using only ocamlopt. I have not thoroughly tested if this works for all possible cases, but I believe it should be a compile error if it doesn't.
  • When the ocaml program is compiled, two files are created in the cwd, the executable and an interface file (program.cmi)
    • The executable requires the interface file, which will be overwritten aon if you compile something else.
      • E.g. mi compile a.mc && mi compile b.mc && ./a will not work if a.mc and b.mc contain the specialize keyword
  • One key issue that remains to be solved is that the dynamically linking may not happen before the full program.ml module has been initialized. A possible solution to this may be to segment the program module into several modules based on the location of specialize terms, and that the residuals then only depends on the modules "above" it in the program.

@adamssonj adamssonj marked this pull request as ready for review November 29, 2023 23:27
@adamssonj adamssonj marked this pull request as draft November 30, 2023 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant