[PATCH] D136400: [llvm-ocaml] Migrate from naked pointers to prepare for OCaml 5

Alan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 14 12:41:39 PST 2023


alan updated this revision to Diff 497414.
alan added a comment.

Assume LLVM pointers are 2-bit aligned, fix alloc_temp call, fix debuginfo test


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136400/new/

https://reviews.llvm.org/D136400

Files:
  llvm/bindings/ocaml/llvm/llvm_ocaml.c
  llvm/bindings/ocaml/llvm/llvm_ocaml.h
  llvm/test/Bindings/OCaml/debuginfo.ml


Index: llvm/test/Bindings/OCaml/debuginfo.ml
===================================================================
--- llvm/test/Bindings/OCaml/debuginfo.ml
+++ llvm/test/Bindings/OCaml/debuginfo.ml
@@ -290,7 +290,7 @@
   *)
   let arg0 = (Llvm.params f).(0) in
   let arg_var = Llvm_debuginfo.dibuild_create_parameter_variable dibuilder ~scope:fun_di
-    ~name:"my_arg" ~argno:0 ~file:file_di ~line:10 ~ty
+    ~name:"my_arg" ~argno:1 ~file:file_di ~line:10 ~ty
     ~always_preserve:false flags_zero
   in
   let argdi = Llvm_debuginfo.dibuild_insert_declare_before dibuilder ~storage:arg0
Index: llvm/bindings/ocaml/llvm/llvm_ocaml.h
===================================================================
--- llvm/bindings/ocaml/llvm/llvm_ocaml.h
+++ llvm/bindings/ocaml/llvm/llvm_ocaml.h
@@ -33,6 +33,10 @@
 value caml_alloc_some(value);
 #endif
 
+/* to_val and from_val assume the pointer, which comes from LLVM and points
+   outside the OCaml heap, is at least 2-bit aligned. to_val sets the low bit
+   so that OCaml treats the pointer as an unboxed value. from_val does the
+   inverse. */
 value to_val(void *ptr);
 
 void *from_val(value v);
Index: llvm/bindings/ocaml/llvm/llvm_ocaml.c
===================================================================
--- llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -33,21 +33,13 @@
 #endif
 
 value to_val(void *ptr) {
-  if ((((value)ptr) & 1) == 0) {
-    return ((value)ptr) + 1;
-  } else {
-    value v = caml_alloc(1, Abstract_tag);
-    *((void **)Data_abstract_val(v)) = ptr;
-    return v;
-  }
+  assert((((value)ptr) & 1) == 0);
+  return ((value)ptr) | 1;
 }
 
 void *from_val(value v) {
-  if (Is_long(v)) {
-    return (void *)(v - 1);
-  } else {
-    return *((void **)Data_abstract_val(v));
-  }
+  assert(Is_long(v));
+  return (void *)(v ^ 1);
 }
 
 value llvm_string_of_message(char *Message) {
@@ -665,7 +657,7 @@
   CAMLparam1(StructTy);
   CAMLlocal1(Tys);
   unsigned Length = LLVMCountStructElementTypes(Type_val(StructTy));
-  LLVMTypeRef *Temp = alloc_temp(sizeof(LLVMTypeRef) * Length);
+  LLVMTypeRef *Temp = alloc_temp(Length);
   LLVMGetStructElementTypes(Type_val(StructTy), Temp);
   Tys = caml_alloc_tuple(Length);
   for (unsigned I = 0; I < Length; ++I) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136400.497414.patch
Type: text/x-patch
Size: 2277 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230214/e871426a/attachment.bin>


More information about the llvm-commits mailing list