[OCaml] Migrate from naked pointers to prepare for OCaml 5
authorAlan Hu <alanh@ccs.neu.edu>
Tue, 28 Feb 2023 22:22:27 +0000 (22:22 +0000)
committerJosh Berdine <josh@berdine.net>
Tue, 28 Feb 2023 23:52:49 +0000 (23:52 +0000)
commit30f423a543641816e367cd17dad3d4808cba8a79
treef9b04396a335bfb9beecb0ab4ca3c86491d4f11d
parent3083b65c3494b912e622a006a1b563a7e9f1d508
[OCaml] Migrate from naked pointers to prepare for OCaml 5

The OCaml bindings currently return pointers to LLVM objects as-is to
OCaml. These "naked pointers" end up appearing as values of local
variables in OCaml code, stored as part of other OCaml values,
etc. The safety of this design relies on the OCaml runtime system's
ability to distinguish these pointers from pointers to memory on the
OCaml garbage collected heap. In particular, when the OCaml GC
encounters a pointer to memory known to not be part of the OCaml heap,
it does not follow it.

In OCaml 4.02 an optimized "no naked pointers" mode was introduced
where the runtime system does not perform such checks and requires
that no such naked pointers be passed to OCaml code, instead one of
several encodings needs to be used. In OCaml 5, the no naked pointers
mode is now the only mode. This diff uses one of the potential
encodings to eliminate naked pointers, making the LLVM OCaml bindings
compatible with the "no naked pointers" mode of OCaml >= 4.02 and 5.

The encoding implemented in this diff relies on LLVM objects to be at
least 2-byte aligned, meaning that the lsb of pointers will
necessarily be clear. The encoding sets the lsb when passing LLVM
pointers to OCaml, and clears it on the return path. Setting the lsb
causes the OCaml runtime system to interpret the resulting value as a
tagged integer, which does not participate in garbage collection.

In some cases, particularly functions that receive an OCaml array of
LLVM pointers, this encoding requires allocation of a temporary array,
but otherwise this diff aims to preserve the existing performance
characteristics of the OCaml bindings.

Reviewed By: jberdine

Differential Revision: https://reviews.llvm.org/D136400
21 files changed:
llvm/bindings/ocaml/analysis/analysis_ocaml.c
llvm/bindings/ocaml/bitreader/CMakeLists.txt
llvm/bindings/ocaml/bitreader/bitreader_ocaml.c
llvm/bindings/ocaml/bitwriter/CMakeLists.txt
llvm/bindings/ocaml/bitwriter/bitwriter_ocaml.c
llvm/bindings/ocaml/debuginfo/debuginfo_ocaml.c
llvm/bindings/ocaml/debuginfo/llvm_debuginfo.ml
llvm/bindings/ocaml/executionengine/CMakeLists.txt
llvm/bindings/ocaml/executionengine/executionengine_ocaml.c
llvm/bindings/ocaml/irreader/CMakeLists.txt
llvm/bindings/ocaml/irreader/irreader_ocaml.c
llvm/bindings/ocaml/linker/CMakeLists.txt
llvm/bindings/ocaml/linker/linker_ocaml.c
llvm/bindings/ocaml/llvm/llvm.ml
llvm/bindings/ocaml/llvm/llvm_ocaml.c
llvm/bindings/ocaml/llvm/llvm_ocaml.h
llvm/bindings/ocaml/target/target_ocaml.c
llvm/bindings/ocaml/transforms/utils/CMakeLists.txt
llvm/bindings/ocaml/transforms/utils/transform_utils_ocaml.c
llvm/test/Bindings/OCaml/core.ml
llvm/test/Bindings/OCaml/debuginfo.ml