Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/bindings/ocaml/llvm/llvm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions llvm/bindings/ocaml/llvm/llvm.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions llvm/bindings/ocaml/llvm/llvm_ocaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 */
Expand Down
25 changes: 19 additions & 6 deletions llvm/test/Bindings/OCaml/core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------------------------------------===*)

Expand Down Expand Up @@ -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
Copy link
Contributor

@alan-j-hu alan-j-hu Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you were to prefer to check for a numeric regex instead of a precise ID, I don't like the wording of "predictable" in the comment. The tests are still deterministic, i.e. predictable.

Suggested change
(* 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);
Expand Down Expand Up @@ -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"}
Copy link
Contributor

@alan-j-hu alan-j-hu Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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 = !{!{{[0-9]+}}}
* CHECK: !0 = !{i32 0, !"global test metadata"}
* CHECK: !1 = !{i32 1, !"Debug Info Version", i32 3}
* CHECK: !2 = !{i32 1, !"metadata test"}

*)


Expand Down