Skip to content

Commit 5d54043

Browse files
authored
[bindings] Add global_set_metadata for function debuginfo
Reviewed by: Alan Hu <[email protected]>
1 parent f3e8e80 commit 5d54043

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

llvm/bindings/ocaml/llvm/llvm.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ external global_copy_all_metadata : llvalue -> (llmdkind * llmetadata) array
700700
external is_global_constant : llvalue -> bool = "llvm_is_global_constant"
701701
external set_global_constant : bool -> llvalue -> unit
702702
= "llvm_set_global_constant"
703+
external global_set_metadata : llvalue -> llmdkind -> llmetadata -> unit
704+
= "llvm_global_set_metadata"
703705

704706
(*--... Operations on global variables .....................................--*)
705707
external declare_global : lltype -> string -> llmodule -> llvalue

llvm/bindings/ocaml/llvm/llvm.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,12 @@ val is_global_constant : llvalue -> bool
13581358
See the method [llvm::GlobalVariable::setConstant]. *)
13591359
val set_global_constant : bool -> llvalue -> unit
13601360

1361+
(** [global_set_metadata g k md] sets the metadata attachment of the global
1362+
value [g] to the metadata [md] for the given kind [k], erasing the existing
1363+
metadata attachment if it already exists for the given kind.
1364+
See the method [llvm::GlobalObject::setMetadata]. *)
1365+
val global_set_metadata : llvalue -> llmdkind -> llmetadata -> unit
1366+
13611367
(** [global_initializer gv] If global variable [gv] has an initializer it is returned,
13621368
otherwise returns [None]. See the method [llvm::GlobalVariable::getInitializer]. *)
13631369
val global_initializer : llvalue -> llvalue option

llvm/bindings/ocaml/llvm/llvm_ocaml.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
|* *|
1616
\*===----------------------------------------------------------------------===*/
1717

18-
#include <assert.h>
19-
#include <stdlib.h>
20-
#include <string.h>
18+
#include "llvm_ocaml.h"
19+
#include "caml/callback.h"
20+
#include "caml/fail.h"
21+
#include "caml/memory.h"
2122
#include "llvm-c/Core.h"
2223
#include "llvm-c/Support.h"
2324
#include "llvm/Config/llvm-config.h"
24-
#include "caml/memory.h"
25-
#include "caml/fail.h"
26-
#include "caml/callback.h"
27-
#include "llvm_ocaml.h"
25+
#include <assert.h>
26+
#include <stdlib.h>
27+
#include <string.h>
2828

2929
#if OCAML_VERSION < 41200
3030
value caml_alloc_some(value v) {
@@ -1546,6 +1546,14 @@ value llvm_set_global_constant(value Flag, value GlobalVar) {
15461546
return Val_unit;
15471547
}
15481548

1549+
/* llvalue -> llmdkind -> llmetadata -> unit */
1550+
value llvm_global_set_metadata(value Value, value MetadataKind,
1551+
value Metadata) {
1552+
LLVMGlobalSetMetadata(Value_val(Value), (unsigned int)Int_val(MetadataKind),
1553+
Metadata_val(Metadata));
1554+
return Val_unit;
1555+
}
1556+
15491557
/*--... Operations on aliases ..............................................--*/
15501558

15511559
/* llmodule -> lltype -> int -> llvalue -> string -> llvalue */

llvm/test/Bindings/OCaml/core.ml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,19 @@ let test_global_values () =
434434
group "dll_storage_class";
435435
let g = define_global "GVal06" zero32 m ++
436436
set_dll_storage_class DLLStorageClass.DLLExport in
437-
insist (DLLStorageClass.DLLExport = dll_storage_class g)
437+
insist (DLLStorageClass.DLLExport = dll_storage_class g);
438438

439+
(* CHECK: GVal07{{.*}}!test !0
440+
* See metadata check at the end of the file.
441+
*)
442+
group "metadata";
443+
let g = define_global "GVal07" zero32 m in
444+
let md_string = mdstring context "global test metadata" in
445+
let md_node = mdnode context [| zero32; md_string |] |> value_as_metadata in
446+
let mdkind_test = mdkind_id context "test" in
447+
global_set_metadata g mdkind_test md_node;
448+
let md' = global_copy_all_metadata g in
449+
insist (md' = [| mdkind_test, md_node |])
439450

440451
(*===-- Global Variables --------------------------------------------------===*)
441452

@@ -1115,8 +1126,8 @@ let test_builder () =
11151126
end;
11161127

11171128
group "metadata"; begin
1118-
(* CHECK: %metadata = add i32 %P1, %P2, !test !1
1119-
* !1 is metadata emitted at EOF.
1129+
(* CHECK: %metadata = add i32 %P1, %P2, !test !2
1130+
* !2 is metadata emitted at EOF.
11201131
*)
11211132
let i = build_add p1 p2 "metadata" atentry in
11221133
insist ((has_metadata i) = false);
@@ -1432,9 +1443,10 @@ let test_builder () =
14321443
end
14331444

14341445
(* End-of-file checks for things like metdata and attributes.
1435-
* CHECK: !llvm.module.flags = !{!0}
1436-
* CHECK: !0 = !{i32 1, !"Debug Info Version", i32 3}
1437-
* CHECK: !1 = !{i32 1, !"metadata test"}
1446+
* CHECK: !llvm.module.flags = !{!1}
1447+
* CHECK: !0 = !{i32 0, !"global test metadata"}
1448+
* CHECK: !1 = !{i32 1, !"Debug Info Version", i32 3}
1449+
* CHECK: !2 = !{i32 1, !"metadata test"}
14381450
*)
14391451

14401452

0 commit comments

Comments
 (0)