[llvm] 5d54043 - [bindings] Add `global_set_metadata` for function debuginfo

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 22 13:36:40 PDT 2025


Author: Rynco Maekawa
Date: 2025-03-22T16:36:36-04:00
New Revision: 5d540430069795fe149f953bc1f892cf3e77b2ee

URL: https://github.com/llvm/llvm-project/commit/5d540430069795fe149f953bc1f892cf3e77b2ee
DIFF: https://github.com/llvm/llvm-project/commit/5d540430069795fe149f953bc1f892cf3e77b2ee.diff

LOG: [bindings] Add `global_set_metadata` for function debuginfo

Reviewed by: Alan Hu <ahulambda at gmail.com>

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index 63931bac940e6..c205fafbbbc16 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -700,6 +700,8 @@ 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..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) {
@@ -1546,6 +1546,14 @@ 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 */

diff  --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index aeefd28c54bc0..9f999be3e9cce 100644
--- a/llvm/test/Bindings/OCaml/core.ml
+++ b/llvm/test/Bindings/OCaml/core.ml
@@ -434,8 +434,19 @@ let test_global_values () =
   group "dll_storage_class";
   let g = define_global "GVal06" zero32 m ++
           set_dll_storage_class DLLStorageClass.DLLExport in
-  insist (DLLStorageClass.DLLExport = dll_storage_class g)
+  insist (DLLStorageClass.DLLExport = dll_storage_class g);
 
+  (* CHECK: GVal07{{.*}}!test !0
+   * See metadata check at the end of the file.
+   *)
+  group "metadata";
+  let g = define_global "GVal07" zero32 m in
+  let md_string = mdstring context "global test metadata" in
+  let md_node = mdnode context [| zero32; md_string |] |> value_as_metadata in
+  let mdkind_test = mdkind_id context "test" in
+  global_set_metadata g mdkind_test md_node;
+  let md' = global_copy_all_metadata g in
+  insist (md' = [| mdkind_test, md_node |])
 
 (*===-- Global Variables --------------------------------------------------===*)
 
@@ -1115,8 +1126,8 @@ let test_builder () =
   end;
 
   group "metadata"; begin
-    (* CHECK: %metadata = add i32 %P1, %P2, !test !1
-     * !1 is metadata emitted at EOF.
+    (* CHECK: %metadata = add i32 %P1, %P2, !test !2
+     * !2 is metadata emitted at EOF.
      *)
     let i = build_add p1 p2 "metadata" atentry in
     insist ((has_metadata i) = false);
@@ -1432,9 +1443,10 @@ let test_builder () =
   end
 
 (* End-of-file checks for things like metdata and attributes.
- * CHECK: !llvm.module.flags = !{!0}
- * CHECK: !0 = !{i32 1, !"Debug Info Version", i32 3}
- * CHECK: !1 = !{i32 1, !"metadata test"}
+ * CHECK: !llvm.module.flags = !{!1}
+ * CHECK: !0 = !{i32 0, !"global test metadata"}
+ * CHECK: !1 = !{i32 1, !"Debug Info Version", i32 3}
+ * CHECK: !2 = !{i32 1, !"metadata test"}
  *)
 
 


        


More information about the llvm-commits mailing list