The OCaml bindings to LLVM 15 have been released on opam!
This is a milestone in work contracted by the OCaml Software Foundation to ensure the OCaml LLVM bindings remain up to date with both changes in OCaml and LLVM. First, I would like to thank @jjb for supervising me, and @Kit-ty-kate and @Kakadu on GitHub for their valuable help.
While previous versions of OCaml have supported naked pointers (pointers to outside the OCaml heap as OCaml values), OCaml 5’s new multicore runtime disallows naked pointers. Furthermore, OCaml 5 deprecated various runtime API functions. I had previously written ⚙ D136400 [llvm-ocaml] Migrate from naked pointers to prepare for OCaml 5 to adapt the LLVM bindings on the main branch to these changes, but LLVM 15 had already been released by then, with naked pointers in use. Therefore, I backported D136400 to LLVM 15 on my fork, and this fork is what is published on opam. Therefore, the version of LLVM 15 on opam is compatible with OCaml 5.
Furthermore, the LLVM IR has been migrating from pointers which carry the types of their pointees to untyped “opaque pointers.” As a result, many LLVM API functions have been deprecated and replaced. For example,
build_call has been deprecated with the replacement being
build_call2. I have added deprecation alerts to the relevant functions and added missing bindings to construct opaque pointers.
The changes between the upstream LLVM 15 branch and the fork published on opam can be viewed at Comparing llvm:release/15.x...alan-j-hu:aa44040b6096870d6bdecc7cc7c9e554f7b25a54 · llvm/llvm-project · GitHub.
Independently, @Kit-ty-kate had been working on building the LLVM bindings with Dune instead of the in-tree OCamlfind-based system at GitHub - kit-ty-kate/llvm-dune: The official LLVM OCaml binding but built using dune. Her repo has a Git submodule that points to my fork of LLVM, and her repo contains the Dune scaffolding that the LLVM 15 release on opam uses. This change of build systems may have implications for selecting between dynamic and static linking to LLVM:
- Prior opam releases of LLVM use OCamlfind, which is supported via the in-tree META file. The package had a script that installed versions of the bindings that link statically or dynamically to LLVM (if each was available) and patched the META file to use the
llvm.staticpredicate to choose between them.
llvm.staticpredicate can be set using OCamlfind but cannot be set from Dune.
- The LLVM 15 release uses Dune, which it supports by running a script that generates Dune files in the source tree. The script’s generated Dune files use the virtual library feature to define versions of the library that link statically or dynamically to LLVM (if each is available).
- OCamlfind-based packages don’t understand the Dune virtual library feature and therefore cannot use the “default implementation” and must select the static or dynamic implementation explicitly.
- @kit-ty-kate believes that the package builds faster and is easier to maintain with Dune.
Please let me know if you have any questions.