[PATCH] D65195: [OCaml] Handle nullptr in Llvm.global_initializer

Timotej Kapus via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 07:46:20 PDT 2019


kren1 updated this revision to Diff 222615.
kren1 added a comment.

Apologies my bad, forgot to run the tests for this one...

I fixed the LLVM (ocaml) tests. I don't believe the clang failure was my fault and check-all passes in my setup.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D65195

Files:
  llvm/bindings/ocaml/llvm/llvm.ml
  llvm/bindings/ocaml/llvm/llvm.mli
  llvm/bindings/ocaml/llvm/llvm_ocaml.c
  llvm/test/Bindings/OCaml/core.ml
  llvm/test/Bindings/OCaml/irreader.ml


Index: llvm/test/Bindings/OCaml/irreader.ml
===================================================================
--- llvm/test/Bindings/OCaml/irreader.ml
+++ llvm/test/Bindings/OCaml/irreader.ml
@@ -38,7 +38,7 @@
     let m   = parse_ir context buf in
     match lookup_global "foo" m with
     | Some foo ->
-        insist ((global_initializer foo) = (const_int (i32_type context) 42))
+        insist ((global_initializer foo) = (Some (const_int (i32_type context) 42)))
     | None ->
         failwith "global"
   end;
Index: llvm/test/Bindings/OCaml/core.ml
===================================================================
--- llvm/test/Bindings/OCaml/core.ml
+++ llvm/test/Bindings/OCaml/core.ml
@@ -540,14 +540,14 @@
            set_initializer forty_two32 in
     insist (not (is_declaration g));
     insist (not (is_declaration g2));
-    insist ((global_initializer g) == (global_initializer g2));
+    insist ((global_initializer g) = (global_initializer g2));
 
     let g = define_qualified_global "QGVar02" forty_two32 3 m in
     let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++
            set_initializer forty_two32 in
     insist (not (is_declaration g));
     insist (not (is_declaration g2));
-    insist ((global_initializer g) == (global_initializer g2));
+    insist ((global_initializer g) = (global_initializer g2));
   end;
 
   (* CHECK: GVar04{{.*}}thread_local
Index: llvm/bindings/ocaml/llvm/llvm_ocaml.c
===================================================================
--- llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -1261,6 +1261,18 @@
   return Val_unit;
 }
 
+/* llvalue -> llvalue option */
+CAMLprim value llvm_global_initializer(LLVMValueRef GlobalVar) {
+  CAMLparam0();
+  LLVMValueRef Init;
+  if ((Init = LLVMGetInitializer(GlobalVar))) {
+    value Option = alloc(1, 0);
+    Field(Option, 0) = (value) Init;
+    CAMLreturn(Option);
+  }
+  CAMLreturn(Val_int(0));
+}
+
 /* llvalue -> llvalue -> unit */
 CAMLprim value llvm_set_initializer(LLVMValueRef ConstantVal,
                                     LLVMValueRef GlobalVar) {
Index: llvm/bindings/ocaml/llvm/llvm.mli
===================================================================
--- llvm/bindings/ocaml/llvm/llvm.mli
+++ llvm/bindings/ocaml/llvm/llvm.mli
@@ -1454,9 +1454,9 @@
     See the method [llvm::GlobalVariable::setConstant]. *)
 val set_global_constant : bool -> llvalue -> unit
 
-(** [global_initializer gv] returns the initializer for the global variable
-    [gv]. See the method [llvm::GlobalVariable::getInitializer]. *)
-val global_initializer : llvalue -> llvalue
+(** [global_initializer gv] If global variable [gv] has an initializer it is returned,
+    otherwise returns [None]. See the method [llvm::GlobalVariable::getInitializer]. *)
+val global_initializer : llvalue -> llvalue option
 
 (** [set_initializer c gv] sets the initializer for the global variable
     [gv] to the constant [c].
Index: llvm/bindings/ocaml/llvm/llvm.ml
===================================================================
--- llvm/bindings/ocaml/llvm/llvm.ml
+++ llvm/bindings/ocaml/llvm/llvm.ml
@@ -710,7 +710,7 @@
 external lookup_global : string -> llmodule -> llvalue option
                        = "llvm_lookup_global"
 external delete_global : llvalue -> unit = "llvm_delete_global"
-external global_initializer : llvalue -> llvalue = "LLVMGetInitializer"
+external global_initializer : llvalue -> llvalue option = "llvm_global_initializer"
 external set_initializer : llvalue -> llvalue -> unit = "llvm_set_initializer"
 external remove_initializer : llvalue -> unit = "llvm_remove_initializer"
 external is_thread_local : llvalue -> bool = "llvm_is_thread_local"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65195.222615.patch
Type: text/x-patch
Size: 3736 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191001/3add3de4/attachment.bin>


More information about the llvm-commits mailing list