[llvm] [bindings] Add OCaml binding to `LLVMGlobalSetMetadata` (PR #131583)

Rynco Maekawa via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 17 19:52:43 PDT 2025


https://github.com/lynzrand updated https://github.com/llvm/llvm-project/pull/131583

>From 0bf3e74a492894f7fb10227797d5d7df73001b08 Mon Sep 17 00:00:00 2001
From: Rynco Maekawa <i at rynco.me>
Date: Mon, 13 Jan 2025 17:13:12 +0800
Subject: [PATCH 1/2] [bindings] Add `global_set_metadata` for function
 debuginfo

---
 llvm/bindings/ocaml/llvm/llvm.ml      | 1 +
 llvm/bindings/ocaml/llvm/llvm.mli     | 6 ++++++
 llvm/bindings/ocaml/llvm/llvm_ocaml.c | 7 +++++++
 3 files changed, 14 insertions(+)

diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index 63931bac940e6..0bd07aacec5fd 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -700,6 +700,7 @@ external global_copy_all_metadata : llvalue -> (llmdkind * llmetadata) array
 external is_global_constant : llvalue -> bool = "llvm_is_global_constant"
 external set_global_constant : bool -> llvalue -> unit
                              = "llvm_set_global_constant"
+external global_set_metadata : llvalue -> llmdkind -> llmetadata -> unit = "llvm_global_set_metadata"
 
 (*--... Operations on global variables .....................................--*)
 external declare_global : lltype -> string -> llmodule -> llvalue
diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index 8a85d672db774..e203961c6d7dd 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -1358,6 +1358,12 @@ val is_global_constant : llvalue -> bool
     See the method [llvm::GlobalVariable::setConstant]. *)
 val set_global_constant : bool -> llvalue -> unit
 
+(** [global_set_metadata g k md] sets the metadata attachment of the global
+    value [g] to the metadata [md] for the given kind [k], erasing the existing
+    metadata attachment if it already exists for the given kind.
+    See the method [llvm::GlobalObject::setMetadata]. *)
+val global_set_metadata : llvalue -> llmdkind -> llmetadata -> unit
+
 (** [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
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index 904dc114d307d..7d32b8b2f65c4 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -1546,6 +1546,13 @@ value llvm_set_global_constant(value Flag, value GlobalVar) {
   return Val_unit;
 }
 
+/* llvalue -> llmdkind -> llmetadata -> unit */
+value llvm_global_set_metadata(value Value, value MetadataKind, value Metadata) {
+  LLVMGlobalSetMetadata(Value_val(Value), (unsigned int)Int_val(MetadataKind),
+                        Metadata_val(Metadata));
+  return Val_unit;
+}
+
 /*--... Operations on aliases ..............................................--*/
 
 /* llmodule -> lltype -> int -> llvalue -> string -> llvalue */

>From 2d4484f7e631e32382cbb273dbe5c3f04bfbd972 Mon Sep 17 00:00:00 2001
From: Rynco Maekawa <i at rynco.me>
Date: Tue, 18 Mar 2025 09:55:09 +0800
Subject: [PATCH 2/2] fixup! [bindings] Add `global_set_metadata`

Format code according to formatter requirements
---
 llvm/bindings/ocaml/llvm/llvm_ocaml.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index 7d32b8b2f65c4..472519ccfab30 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -15,16 +15,16 @@
 |*                                                                            *|
 \*===----------------------------------------------------------------------===*/
 
-#include <assert.h>
-#include <stdlib.h>
-#include <string.h>
+#include "llvm_ocaml.h"
+#include "caml/callback.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
 #include "llvm-c/Core.h"
 #include "llvm-c/Support.h"
 #include "llvm/Config/llvm-config.h"
-#include "caml/memory.h"
-#include "caml/fail.h"
-#include "caml/callback.h"
-#include "llvm_ocaml.h"
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
 
 #if OCAML_VERSION < 41200
 value caml_alloc_some(value v) {
@@ -1547,7 +1547,8 @@ value llvm_set_global_constant(value Flag, value GlobalVar) {
 }
 
 /* llvalue -> llmdkind -> llmetadata -> unit */
-value llvm_global_set_metadata(value Value, value MetadataKind, value Metadata) {
+value llvm_global_set_metadata(value Value, value MetadataKind,
+                               value Metadata) {
   LLVMGlobalSetMetadata(Value_val(Value), (unsigned int)Int_val(MetadataKind),
                         Metadata_val(Metadata));
   return Val_unit;



More information about the llvm-commits mailing list