[llvm] [bindings] Add OCaml binding to `LLVMGlobalSetMetadata` (PR #131583)
Rynco Maekawa via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 21 02:24:14 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/5] [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/5] 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;
>From 4b1c3376eaf156ca8461276433221d890f422fb4 Mon Sep 17 00:00:00 2001
From: Rynco Maekawa <i at rynco.me>
Date: Tue, 18 Mar 2025 14:35:43 +0800
Subject: [PATCH 3/5] [bindings][test] Add test for `global_set_metadata`
---
llvm/test/Bindings/OCaml/core.ml | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index aeefd28c54bc0..5d33a1de7e824 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 !{{.*}}
+ * See metadata check at the end of the file.
+ *)
+ group "metdata";
+ 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,9 @@ 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 !{{[0-9]+}}
+ * Number of metadata nodes is not predictable, so we just check for
+ * the presence of metadata here
*)
let i = build_add p1 p2 "metadata" atentry in
insist ((has_metadata i) = false);
@@ -1432,9 +1444,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 = !{!{{[0-9]+}}}
+ * CHECK: !{{[0-9]+}} = !{i32 0, !"global test metadata"}
+ * CHECK: !{{[0-9]+}} = !{i32 1, !"Debug Info Version", i32 3}
+ * CHECK: !{{[0-9]+}} = !{i32 1, !"metadata test"}
*)
>From 75b272b0a00075c8efc81d6d11df28ce2824cd7c Mon Sep 17 00:00:00 2001
From: Rynco Maekawa <lynzrand at outlook.com>
Date: Fri, 21 Mar 2025 10:43:59 +0800
Subject: [PATCH 4/5] fixup! [bindings][test] Add test for global_set_metadata
Fix typo and formats
Co-authored-by: Alan <ahulambda at gmail.com>
---
llvm/bindings/ocaml/llvm/llvm.ml | 3 ++-
llvm/test/Bindings/OCaml/core.ml | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index 0bd07aacec5fd..c205fafbbbc16 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -700,7 +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"
+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/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index 5d33a1de7e824..4d45ae93a0973 100644
--- a/llvm/test/Bindings/OCaml/core.ml
+++ b/llvm/test/Bindings/OCaml/core.ml
@@ -439,7 +439,7 @@ let test_global_values () =
(* CHECK: GVal07{{.*}}!test !{{.*}}
* See metadata check at the end of the file.
*)
- group "metdata";
+ 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
>From ae10076ffd2b7bd1078ed4c09eec606d1a48cf3d Mon Sep 17 00:00:00 2001
From: Rynco Maekawa <i at rynco.me>
Date: Fri, 21 Mar 2025 17:09:28 +0800
Subject: [PATCH 5/5] fixup! [bindings][test] Add test for
`global_set_metadata`
Change test metadata IDs back to numbers
---
llvm/test/Bindings/OCaml/core.ml | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml
index 4d45ae93a0973..33e71f75c8bf9 100644
--- a/llvm/test/Bindings/OCaml/core.ml
+++ b/llvm/test/Bindings/OCaml/core.ml
@@ -436,7 +436,7 @@ let test_global_values () =
set_dll_storage_class DLLStorageClass.DLLExport in
insist (DLLStorageClass.DLLExport = dll_storage_class g);
- (* CHECK: GVal07{{.*}}!test !{{.*}}
+ (* CHECK: GVal07{{.*}}!test !0
* See metadata check at the end of the file.
*)
group "metadata";
@@ -1126,9 +1126,7 @@ let test_builder () =
end;
group "metadata"; begin
- (* CHECK: %metadata = add i32 %P1, %P2, !test !{{[0-9]+}}
- * Number of metadata nodes is not predictable, so we just check for
- * the presence of metadata here
+ (* CHECK: %metadata = add i32 %P1, %P2, !test !2
*)
let i = build_add p1 p2 "metadata" atentry in
insist ((has_metadata i) = false);
@@ -1444,10 +1442,10 @@ let test_builder () =
end
(* End-of-file checks for things like metdata and attributes.
- * CHECK: !llvm.module.flags = !{!{{[0-9]+}}}
- * CHECK: !{{[0-9]+}} = !{i32 0, !"global test metadata"}
- * CHECK: !{{[0-9]+}} = !{i32 1, !"Debug Info Version", i32 3}
- * CHECK: !{{[0-9]+}} = !{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