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

Rynco Maekawa via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 17 02:08:29 PDT 2025


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

This PR adds the OCaml binding to `LLVMGlobalSetMetadata`, so that OCaml users can set metadata on global values like functions. This is useful for e.g. adding debug information to functions.

(Note that the existing [`set_metadata`](https://github.com/moonbitlang/llvm-project/blob/0bf3e74a492894f7fb10227797d5d7df73001b08/llvm/bindings/ocaml/llvm/llvm.ml#L598) function operates on _instructions_, and cannot be used to set metadata on global values.)

---

This is my first PR to LLVM, so please tell me if I have done anything wrong.

Although the contribution guide said that contributions should include a unit test, I can't find any unit test in the OCaml bindings, so I guess I can skip that? This binding has been tested in our internal projects and worked.

The code within this PR came from a branch of version 18.x. As far as I know, related C bindings haven't changed during the times, so this patch should still be relevant.

>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] [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 */



More information about the llvm-commits mailing list