[all-commits] [llvm/llvm-project] 30f423: [OCaml] Migrate from naked pointers to prepare for...

Alan via All-commits all-commits at lists.llvm.org
Tue Feb 28 15:54:00 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 30f423a543641816e367cd17dad3d4808cba8a79
      https://github.com/llvm/llvm-project/commit/30f423a543641816e367cd17dad3d4808cba8a79
  Author: Alan Hu <alanh at ccs.neu.edu>
  Date:   2023-02-28 (Tue, 28 Feb 2023)

  Changed paths:
    M llvm/bindings/ocaml/analysis/analysis_ocaml.c
    M llvm/bindings/ocaml/bitreader/CMakeLists.txt
    M llvm/bindings/ocaml/bitreader/bitreader_ocaml.c
    M llvm/bindings/ocaml/bitwriter/CMakeLists.txt
    M llvm/bindings/ocaml/bitwriter/bitwriter_ocaml.c
    M llvm/bindings/ocaml/debuginfo/debuginfo_ocaml.c
    M llvm/bindings/ocaml/debuginfo/llvm_debuginfo.ml
    M llvm/bindings/ocaml/executionengine/CMakeLists.txt
    M llvm/bindings/ocaml/executionengine/executionengine_ocaml.c
    M llvm/bindings/ocaml/irreader/CMakeLists.txt
    M llvm/bindings/ocaml/irreader/irreader_ocaml.c
    M llvm/bindings/ocaml/linker/CMakeLists.txt
    M llvm/bindings/ocaml/linker/linker_ocaml.c
    M llvm/bindings/ocaml/llvm/llvm.ml
    M llvm/bindings/ocaml/llvm/llvm_ocaml.c
    M llvm/bindings/ocaml/llvm/llvm_ocaml.h
    M llvm/bindings/ocaml/target/target_ocaml.c
    M llvm/bindings/ocaml/transforms/utils/CMakeLists.txt
    M llvm/bindings/ocaml/transforms/utils/transform_utils_ocaml.c
    M llvm/test/Bindings/OCaml/core.ml
    M llvm/test/Bindings/OCaml/debuginfo.ml

  Log Message:
  -----------
  [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




More information about the All-commits mailing list