From b5974020b883bfe2354899000be714f5a207fe9f Mon Sep 17 00:00:00 2001 From: Josh Berdine Date: Wed, 24 Mar 2021 14:17:30 -0700 Subject: [PATCH] [sledge][llvm] Handle nullptr in Llvm.global_initializer Summary: LLVMGetInitializer returns nullptr in case there is no initializer. There is not much that can be done with nullptr in OCaml, not even test if it is null. Also, there does not seem to be a C or OCaml API to test if there is an initializer. So this diff changes Llvm.global_initializer to return an option. Reviewed By: jvillard Differential Revision: D27188302 fbshipit-source-id: 3474ec840 --- .../llvm-project/llvm/bindings/ocaml/llvm/llvm.ml | 2 +- .../llvm-project/llvm/bindings/ocaml/llvm/llvm.mli | 2 +- .../llvm/bindings/ocaml/llvm/llvm_ocaml.c | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm.ml b/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm.ml index 2c5bcd506..d54ec6f1f 100644 --- a/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm.ml +++ b/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm.ml @@ -719,7 +719,7 @@ external define_qualified_global : string -> llvalue -> int -> llmodule -> 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" diff --git a/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm.mli b/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm.mli index a6e8d504e..0a5915a7d 100644 --- a/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm.mli +++ b/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm.mli @@ -1487,7 +1487,7 @@ 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 +val global_initializer : llvalue -> llvalue option (** [set_initializer c gv] sets the initializer for the global variable [gv] to the constant [c]. diff --git a/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm_ocaml.c index e0c6e7c35..11eca907e 100644 --- a/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm_ocaml.c +++ b/sledge/vendor/llvm-dune/llvm-project/llvm/bindings/ocaml/llvm/llvm_ocaml.c @@ -1382,6 +1382,18 @@ CAMLprim value llvm_delete_global(LLVMValueRef GlobalVar) { 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) {