llvm.org GIT mirror llvm / 4ebe64a
[OCaml] implement Llvm_passmgr_builder, bindings for PassManagerBuilder git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193968 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Zotov 7 years ago
7 changed file(s) with 291 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
6161 archive(native) = "llvm_vectorize.cmxa"
6262 )
6363
64 package "passmgr_builder" (
65 requires = "llvm"
66 version = "@PACKAGE_VERSION@"
67 description = "Pass Manager Builder for LLVM"
68 archive(byte) = "llvm_passmgr_builder.cma"
69 archive(native) = "llvm_passmgr_builder.cmxa"
70 )
71
6472 package "target" (
6573 requires = "llvm"
6674 version = "@PACKAGE_VERSION@"
77 ##===----------------------------------------------------------------------===##
88
99 LEVEL := ../../..
10 DIRS = scalar ipo vectorize
10 DIRS = scalar ipo vectorize passmgr_builder
1111
1212 ocamldoc:
1313 $(Verb) for i in $(DIRS) ; do \
0 ##===- bindings/ocaml/transforms/passmgr_builder/Makefile --*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 #
9 # This is the makefile for the Objective Caml Llvm_passmgr_builder interface.
10 #
11 ##===----------------------------------------------------------------------===##
12
13 LEVEL := ../../../..
14 LIBRARYNAME := llvm_passmgr_builder
15 UsedComponents := ipo
16 UsedOcamlInterfaces := llvm
17
18 include ../../Makefile.ocaml
0 (*===-- llvm_passmgr_builder.ml - LLVM OCaml Interface --------*- OCaml -*-===*
1 *
2 * The LLVM Compiler Infrastructure
3 *
4 * This file is distributed under the University of Illinois Open Source
5 * License. See LICENSE.TXT for details.
6 *
7 *===----------------------------------------------------------------------===*)
8
9 type t
10
11 external create : unit -> t
12 = "llvm_pmbuilder_create"
13 external set_opt_level : int -> t -> unit
14 = "llvm_pmbuilder_set_opt_level"
15 external set_size_level : int -> t -> unit
16 = "llvm_pmbuilder_set_size_level"
17 external set_disable_unit_at_a_time : bool -> t -> unit
18 = "llvm_pmbuilder_set_disable_unit_at_a_time"
19 external set_disable_unroll_loops : bool -> t -> unit
20 = "llvm_pmbuilder_set_disable_unroll_loops"
21 external use_inliner_with_threshold : int -> t -> unit
22 = "llvm_pmbuilder_use_inliner_with_threshold"
23 external populate_function_pass_manager
24 : [ `Function ] Llvm.PassManager.t -> t -> unit
25 = "llvm_pmbuilder_populate_function_pass_manager"
26 external populate_module_pass_manager
27 : [ `Module ] Llvm.PassManager.t -> t -> unit
28 = "llvm_pmbuilder_populate_module_pass_manager"
29 external populate_lto_pass_manager
30 : [ `Module ] Llvm.PassManager.t -> internalize:bool -> run_inliner:bool -> t -> unit
31 = "llvm_pmbuilder_populate_lto_pass_manager"
0 (*===-- llvm_passmgr_builder.mli - LLVM OCaml Interface -------*- OCaml -*-===*
1 *
2 * The LLVM Compiler Infrastructure
3 *
4 * This file is distributed under the University of Illinois Open Source
5 * License. See LICENSE.TXT for details.
6 *
7 *===----------------------------------------------------------------------===*)
8
9 (** Pass Manager Builder.
10
11 This interface provides an OCaml API for LLVM pass manager builder
12 from the [LLVMCore] library. *)
13
14 type t
15
16 (** See [llvm::PassManagerBuilder]. *)
17 external create : unit -> t
18 = "llvm_pmbuilder_create"
19
20 (** See [llvm::PassManagerBuilder::OptLevel]. *)
21 external set_opt_level : int -> t -> unit
22 = "llvm_pmbuilder_set_opt_level"
23
24 (** See [llvm::PassManagerBuilder::SizeLevel]. *)
25 external set_size_level : int -> t -> unit
26 = "llvm_pmbuilder_set_size_level"
27
28 (** See [llvm::PassManagerBuilder::DisableUnitAtATime]. *)
29 external set_disable_unit_at_a_time : bool -> t -> unit
30 = "llvm_pmbuilder_set_disable_unit_at_a_time"
31
32 (** See [llvm::PassManagerBuilder::DisableUnrollLoops]. *)
33 external set_disable_unroll_loops : bool -> t -> unit
34 = "llvm_pmbuilder_set_disable_unroll_loops"
35
36 (** See [llvm::PassManagerBuilder::Inliner]. *)
37 external use_inliner_with_threshold : int -> t -> unit
38 = "llvm_pmbuilder_use_inliner_with_threshold"
39
40 (** See [llvm::PassManagerBuilder::populateFunctionPassManager]. *)
41 external populate_function_pass_manager
42 : [ `Function ] Llvm.PassManager.t -> t -> unit
43 = "llvm_pmbuilder_populate_function_pass_manager"
44
45 (** See [llvm::PassManagerBuilder::populateModulePassManager]. *)
46 external populate_module_pass_manager
47 : [ `Module ] Llvm.PassManager.t -> t -> unit
48 = "llvm_pmbuilder_populate_module_pass_manager"
49
50 (** See [llvm::PassManagerBuilder::populateLTOPassManager]. *)
51 external populate_lto_pass_manager
52 : [ `Module ] Llvm.PassManager.t -> internalize:bool -> run_inliner:bool -> t -> unit
53 = "llvm_pmbuilder_populate_lto_pass_manager"
0 /*===-- passmgr_builder_ocaml.c - LLVM OCaml Glue ---------------*- C++ -*-===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file glues LLVM's OCaml interface to its C interface. These functions *|
10 |* are by and large transparent wrappers to the corresponding C functions. *|
11 |* *|
12 |* Note that these functions intentionally take liberties with the CAMLparamX *|
13 |* macros, since most of the parameters are not GC heap objects. *|
14 |* *|
15 \*===----------------------------------------------------------------------===*/
16
17 #include "llvm-c/Transforms/PassManagerBuilder.h"
18 #include "caml/mlvalues.h"
19 #include "caml/custom.h"
20 #include "caml/misc.h"
21
22 #define PMBuilder_val(v) (*(LLVMPassManagerBuilderRef *)(Data_custom_val(v)))
23
24 static void llvm_finalize_pmbuilder(value PMB) {
25 LLVMPassManagerBuilderDispose(PMBuilder_val(PMB));
26 }
27
28 static struct custom_operations pmbuilder_ops = {
29 (char *) "LLVMPassManagerBuilder",
30 llvm_finalize_pmbuilder,
31 custom_compare_default,
32 custom_hash_default,
33 custom_serialize_default,
34 custom_deserialize_default
35 #ifdef custom_compare_ext_default
36 , custom_compare_ext_default
37 #endif
38 };
39
40 static value alloc_pmbuilder(LLVMPassManagerBuilderRef Ref) {
41 value Val = alloc_custom(&pmbuilder_ops,
42 sizeof(LLVMPassManagerBuilderRef), 0, 1);
43 PMBuilder_val(Val) = Ref;
44 return Val;
45 }
46
47 /* t -> unit */
48 CAMLprim value llvm_pmbuilder_create(value Unit) {
49 return alloc_pmbuilder(LLVMPassManagerBuilderCreate());
50 }
51
52 /* int -> t -> unit */
53 CAMLprim value llvm_pmbuilder_set_opt_level(value OptLevel, value PMB) {
54 LLVMPassManagerBuilderSetOptLevel(PMBuilder_val(PMB), Int_val(OptLevel));
55 return Val_unit;
56 }
57
58 /* int -> t -> unit */
59 CAMLprim value llvm_pmbuilder_set_size_level(value SizeLevel, value PMB) {
60 LLVMPassManagerBuilderSetSizeLevel(PMBuilder_val(PMB), Int_val(SizeLevel));
61 return Val_unit;
62 }
63
64 /* int -> t -> unit */
65 CAMLprim value llvm_pmbuilder_use_inliner_with_threshold(
66 value Threshold, value PMB) {
67 LLVMPassManagerBuilderSetOptLevel(PMBuilder_val(PMB), Int_val(Threshold));
68 return Val_unit;
69 }
70
71 /* bool -> t -> unit */
72 CAMLprim value llvm_pmbuilder_set_disable_unit_at_a_time(
73 value DisableUnitAtATime, value PMB) {
74 LLVMPassManagerBuilderSetDisableUnitAtATime(
75 PMBuilder_val(PMB), Bool_val(DisableUnitAtATime));
76 return Val_unit;
77 }
78
79 /* bool -> t -> unit */
80 CAMLprim value llvm_pmbuilder_set_disable_unroll_loops(
81 value DisableUnroll, value PMB) {
82 LLVMPassManagerBuilderSetDisableUnrollLoops(
83 PMBuilder_val(PMB), Bool_val(DisableUnroll));
84 return Val_unit;
85 }
86
87 /* [ `Function ] Llvm.PassManager.t -> t -> unit */
88 CAMLprim value llvm_pmbuilder_populate_function_pass_manager(
89 LLVMPassManagerRef PM, value PMB) {
90 LLVMPassManagerBuilderPopulateFunctionPassManager(
91 PMBuilder_val(PMB), PM);
92 return Val_unit;
93 }
94
95 /* [ `Module ] Llvm.PassManager.t -> t -> unit */
96 CAMLprim value llvm_pmbuilder_populate_module_pass_manager(
97 LLVMPassManagerRef PM, value PMB) {
98 LLVMPassManagerBuilderPopulateModulePassManager(
99 PMBuilder_val(PMB), PM);
100 return Val_unit;
101 }
102
103 /* [ `Module ] Llvm.PassManager.t ->
104 internalize:bool -> run_inliner:bool -> t -> unit */
105 CAMLprim value llvm_pmbuilder_populate_lto_pass_manager(
106 LLVMPassManagerRef PM, value Internalize, value RunInliner,
107 value PMB) {
108 LLVMPassManagerBuilderPopulateLTOPassManager(
109 PMBuilder_val(PMB), PM,
110 Bool_val(Internalize), Bool_val(RunInliner));
111 return Val_unit;
112 }
0 (* RUN: rm -rf %t.builddir
1 * RUN: mkdir -p %t.builddir
2 * RUN: cp %s %t.builddir
3 * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_passmgr_builder.cmxa %t.builddir/passmgr_builder.ml -o %t
4 * RUN: %t %t.bc
5 * XFAIL: vg_leak
6 *)
7
8 (* Note: It takes several seconds for ocamlopt to link an executable with
9 libLLVMCore.a, so it's better to write a big test than a bunch of
10 little ones. *)
11
12 open Llvm
13 open Llvm_passmgr_builder
14
15 let context = global_context ()
16 let void_type = Llvm.void_type context
17
18 (* Tiny unit test framework - really just to help find which line is busted *)
19 let print_checkpoints = false
20
21 let suite name f =
22 if print_checkpoints then
23 prerr_endline (name ^ ":");
24 f ()
25
26
27 (*===-- Fixture -----------------------------------------------------------===*)
28
29 let filename = Sys.argv.(1)
30 let m = create_module context filename
31
32
33 (*===-- Pass Manager Builder ----------------------------------------------===*)
34
35 let test_pmbuilder () =
36 let (++) x f = ignore (f x); x in
37
38 let module_passmgr = PassManager.create () in
39 let func_passmgr = PassManager.create_function m in
40 let lto_passmgr = PassManager.create () in
41
42 ignore (Llvm_passmgr_builder.create ()
43 ++ set_opt_level 3
44 ++ set_size_level 1
45 ++ set_disable_unit_at_a_time false
46 ++ set_disable_unroll_loops false
47 ++ use_inliner_with_threshold 10
48 ++ populate_function_pass_manager func_passmgr
49 ++ populate_module_pass_manager module_passmgr
50 ++ populate_lto_pass_manager lto_passmgr
51 ~internalize:false ~run_inliner:false);
52 Gc.compact ();
53
54 PassManager.dispose module_passmgr;
55 PassManager.dispose func_passmgr;
56 PassManager.dispose lto_passmgr
57
58
59 (*===-- Driver ------------------------------------------------------------===*)
60
61 let _ =
62 suite "pass manager builder" test_pmbuilder;
63 dispose_module m