Skip to content

Commit 49610ed

Browse files
authored
[OCaml][NPM] Add OCaml bindings to new pass manager
1 parent 4bf5fdd commit 49610ed

File tree

11 files changed

+408
-8
lines changed

11 files changed

+408
-8
lines changed

llvm/bindings/ocaml/llvm/META.llvm.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ package "transform_utils" (
6161
archive(native) = "llvm_transform_utils.cmxa"
6262
)
6363

64+
package "passbuilder" (
65+
requires = "llvm,llvm.target"
66+
version = "@PACKAGE_VERSION@"
67+
description = "Pass Manager Builder for LLVM"
68+
archive(byte) = "llvm_passbuilder.cma"
69+
archive(native) = "llvm_passbuilder.cmxa"
70+
)
71+
6472
package "target" (
6573
requires = "llvm"
6674
version = "@PACKAGE_VERSION@"

llvm/bindings/ocaml/target/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ add_ocaml_library(llvm_target
22
OCAML llvm_target
33
OCAMLDEP llvm
44
C target_ocaml
5-
CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
5+
CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/ -I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
66
LLVM Target)

llvm/bindings/ocaml/target/target_ocaml.c

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

18-
#include "llvm-c/Core.h"
19-
#include "llvm-c/Target.h"
20-
#include "llvm-c/TargetMachine.h"
18+
#include "target_ocaml.h"
2119
#include "caml/alloc.h"
20+
#include "caml/callback.h"
21+
#include "caml/custom.h"
2222
#include "caml/fail.h"
2323
#include "caml/memory.h"
24-
#include "caml/custom.h"
25-
#include "caml/callback.h"
2624
#include "llvm_ocaml.h"
25+
#include "llvm-c/Core.h"
26+
#include "llvm-c/Target.h"
27+
#include "llvm-c/TargetMachine.h"
2728

2829
void llvm_raise(value Prototype, char *Message);
2930
value llvm_string_of_message(char *Message);
@@ -210,8 +211,6 @@ value llvm_target_has_asm_backend(value Target) {
210211

211212
/*===---- Target Machine --------------------------------------------------===*/
212213

213-
#define TargetMachine_val(v) (*(LLVMTargetMachineRef *)(Data_custom_val(v)))
214-
215214
static void llvm_finalize_target_machine(value Machine) {
216215
LLVMDisposeTargetMachine(TargetMachine_val(Machine));
217216
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*===-- target_ocaml.h - LLVM OCaml Glue ------------------------*- C++ -*-===*\
2+
|* *|
3+
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4+
|* Exceptions. *|
5+
|* See https://llvm.org/LICENSE.txt for license information. *|
6+
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
7+
|* *|
8+
|*===----------------------------------------------------------------------===*|
9+
|* *|
10+
|* This file glues LLVM's OCaml interface to its C interface. These functions *|
11+
|* are by and large transparent wrappers to the corresponding C functions. *|
12+
|* *|
13+
|* Note that these functions intentionally take liberties with the CAMLparamX *|
14+
|* macros, since most of the parameters are not GC heap objects. *|
15+
|* *|
16+
\*===----------------------------------------------------------------------===*/
17+
18+
#define TargetMachine_val(v) (*(LLVMTargetMachineRef *)(Data_custom_val(v)))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
add_subdirectory(passbuilder)
12
add_subdirectory(utils)
23

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_ocaml_library(llvm_passbuilder
2+
OCAML llvm_passbuilder
3+
OCAMLDEP llvm llvm_target
4+
C passbuilder_ocaml
5+
CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../../llvm -I${CMAKE_CURRENT_SOURCE_DIR}/../../target"
6+
LLVM Passes)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
(*===-- llvm_passbuilder.ml - LLVM OCaml Interface -------------*- OCaml -*-===*
2+
*
3+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
* See https://llvm.org/LICENSE.txt for license information.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*
7+
*===----------------------------------------------------------------------===*)
8+
9+
type llpassbuilder_options
10+
11+
external run_passes
12+
: Llvm.llmodule
13+
-> string
14+
-> Llvm_target.TargetMachine.t
15+
-> llpassbuilder_options
16+
-> (unit, string) result
17+
= "llvm_run_passes"
18+
19+
external create_passbuilder_options : unit -> llpassbuilder_options =
20+
"llvm_create_passbuilder_options"
21+
22+
external passbuilder_options_set_verify_each
23+
: llpassbuilder_options -> bool -> unit =
24+
"llvm_passbuilder_options_set_verify_each"
25+
26+
external passbuilder_options_set_debug_logging
27+
: llpassbuilder_options -> bool -> unit =
28+
"llvm_passbuilder_options_set_debug_logging"
29+
30+
external passbuilder_options_set_loop_interleaving
31+
: llpassbuilder_options -> bool -> unit =
32+
"llvm_passbuilder_options_set_loop_interleaving"
33+
34+
external passbuilder_options_set_loop_vectorization
35+
: llpassbuilder_options -> bool -> unit =
36+
"llvm_passbuilder_options_set_loop_vectorization"
37+
38+
external passbuilder_options_set_slp_vectorization
39+
: llpassbuilder_options -> bool -> unit =
40+
"llvm_passbuilder_options_set_slp_vectorization"
41+
42+
external passbuilder_options_set_loop_unrolling
43+
: llpassbuilder_options -> bool -> unit =
44+
"llvm_passbuilder_options_set_loop_unrolling"
45+
46+
external passbuilder_options_set_forget_all_scev_in_loop_unroll
47+
: llpassbuilder_options -> bool -> unit =
48+
"llvm_passbuilder_options_set_forget_all_scev_in_loop_unroll"
49+
50+
external passbuilder_options_set_licm_mssa_opt_cap
51+
: llpassbuilder_options -> int -> unit =
52+
"llvm_passbuilder_options_set_licm_mssa_opt_cap"
53+
54+
external passbuilder_options_set_licm_mssa_no_acc_for_promotion_cap
55+
: llpassbuilder_options -> int -> unit =
56+
"llvm_passbuilder_options_set_licm_mssa_opt_cap"
57+
58+
external passbuilder_options_set_call_graph_profile
59+
: llpassbuilder_options -> bool -> unit =
60+
"llvm_passbuilder_options_set_call_graph_profile"
61+
62+
external passbuilder_options_set_merge_functions
63+
: llpassbuilder_options -> bool -> unit =
64+
"llvm_passbuilder_options_set_merge_functions"
65+
66+
external passbuilder_options_set_inliner_threshold
67+
: llpassbuilder_options -> int -> unit =
68+
"llvm_passbuilder_options_set_inliner_threshold"
69+
70+
external dispose_passbuilder_options : llpassbuilder_options -> unit =
71+
"llvm_dispose_passbuilder_options"
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
(*===-- llvm_passbuilder.mli - LLVM OCaml Interface ------------*- OCaml -*-===*
2+
*
3+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
* See https://llvm.org/LICENSE.txt for license information.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*
7+
*===----------------------------------------------------------------------===*)
8+
9+
type llpassbuilder_options
10+
11+
(** [run_passes m passes tm opts] runs a set of passes over a module. The
12+
format of the string [passes] is the same as opt's -passes argument for
13+
the new pass manager. Individual passes may be specified, separated by
14+
commas. Full pipelines may also be invoked. See [LLVMRunPasses]. *)
15+
val run_passes
16+
: Llvm.llmodule
17+
-> string
18+
-> Llvm_target.TargetMachine.t
19+
-> llpassbuilder_options
20+
-> (unit, string) result
21+
22+
(** Creates a new set of options for a PassBuilder. See
23+
[llvm::LLVMPassBuilderOptions::LLVMPassBuilderOptions]. *)
24+
val create_passbuilder_options : unit -> llpassbuilder_options
25+
26+
(** Toggles adding the VerifierPass for the PassBuilder. See
27+
[llvm::LLVMPassBuilderOptions::VerifyEach]. *)
28+
val passbuilder_options_set_verify_each
29+
: llpassbuilder_options -> bool -> unit
30+
31+
(** Toggles debug logging. See [llvm::LLVMPassBuilderOptions::DebugLogging]. *)
32+
val passbuilder_options_set_debug_logging
33+
: llpassbuilder_options -> bool -> unit
34+
35+
(** Tuning option to set loop interleaving on/off, set based on opt level.
36+
See [llvm::PipelineTuningOptions::LoopInterleaving]. *)
37+
val passbuilder_options_set_loop_interleaving
38+
: llpassbuilder_options -> bool -> unit
39+
40+
(** Tuning option to enable/disable loop vectorization, set based on opt level.
41+
See [llvm::PipelineTuningOptions::LoopVectorization]. *)
42+
val passbuilder_options_set_loop_vectorization
43+
: llpassbuilder_options -> bool -> unit
44+
45+
(** Tuning option to enable/disable slp loop vectorization, set based on opt
46+
level. See [llvm::PipelineTuningOptions::SLPVectorization]. *)
47+
val passbuilder_options_set_slp_vectorization
48+
: llpassbuilder_options -> bool -> unit
49+
50+
(** Tuning option to enable/disable loop unrolling. Its default value is true.
51+
See [llvm::PipelineTuningOptions::LoopUnrolling]. *)
52+
val passbuilder_options_set_loop_unrolling
53+
: llpassbuilder_options -> bool -> unit
54+
55+
(** Tuning option to forget all SCEV loops in LoopUnroll.
56+
See [llvm::PipelineTuningOptions::ForgetAllSCEVInLoopUnroll]. *)
57+
val passbuilder_options_set_forget_all_scev_in_loop_unroll
58+
: llpassbuilder_options -> bool -> unit
59+
60+
(** Tuning option to cap the number of calls to retrive clobbering accesses in
61+
MemorySSA, in LICM. See [llvm::PipelineTuningOptions::LicmMssaOptCap]. *)
62+
val passbuilder_options_set_licm_mssa_opt_cap
63+
: llpassbuilder_options -> int -> unit
64+
65+
(** Tuning option to disable promotion to scalars in LICM with MemorySSA, if
66+
the number of accesses is too large. See
67+
[llvm::PipelineTuningOptions::LicmMssaNoAccForPromotionCap]. *)
68+
val passbuilder_options_set_licm_mssa_no_acc_for_promotion_cap
69+
: llpassbuilder_options -> int -> unit
70+
71+
(** Tuning option to enable/disable call graph profile. See
72+
[llvm::PipelineTuningOptions::CallGraphProfile]. *)
73+
val passbuilder_options_set_call_graph_profile
74+
: llpassbuilder_options -> bool -> unit
75+
76+
(** Tuning option to enable/disable function merging. See
77+
[llvm::PipelineTuningOptions::MergeFunctions]. *)
78+
val passbuilder_options_set_merge_functions
79+
: llpassbuilder_options -> bool -> unit
80+
81+
(** Tuning option to override the default inliner threshold. See
82+
[llvm::PipelineTuningOptions::InlinerThreshold]. *)
83+
val passbuilder_options_set_inliner_threshold
84+
: llpassbuilder_options -> int -> unit
85+
86+
(** Disposes of the options. *)
87+
val dispose_passbuilder_options : llpassbuilder_options -> unit
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*===-- passbuilder_ocaml.c - LLVM OCaml Glue -------------------*- C++ -*-===*\
2+
|* *|
3+
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4+
|* Exceptions. *|
5+
|* See https://llvm.org/LICENSE.txt for license information. *|
6+
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
7+
|* *|
8+
|*===----------------------------------------------------------------------===*|
9+
|* *|
10+
|* This file glues LLVM's OCaml interface to its C interface. These functions *|
11+
|* are by and large transparent wrappers to the corresponding C functions. *|
12+
|* *|
13+
|* Note that these functions intentionally take liberties with the CAMLparamX *|
14+
|* macros, since most of the parameters are not GC heap objects. *|
15+
|* *|
16+
\*===----------------------------------------------------------------------===*/
17+
18+
#include "llvm_ocaml.h"
19+
#include "target_ocaml.h"
20+
#include "llvm-c/Error.h"
21+
#include "llvm-c/Transforms/PassBuilder.h"
22+
#include <caml/memory.h>
23+
24+
#define PassBuilderOptions_val(v) ((LLVMPassBuilderOptionsRef)from_val(v))
25+
26+
value llvm_run_passes(value M, value Passes, value TM, value Options) {
27+
LLVMErrorRef Err =
28+
LLVMRunPasses(Module_val(M), String_val(Passes), TargetMachine_val(TM),
29+
PassBuilderOptions_val(Options));
30+
if (Err == LLVMErrorSuccess) {
31+
value result = caml_alloc(1, 0);
32+
Store_field(result, 0, Val_unit);
33+
return result;
34+
} else {
35+
char *str = LLVMGetErrorMessage(Err);
36+
value v = caml_copy_string(str);
37+
LLVMDisposeErrorMessage(str);
38+
value result = caml_alloc(1, 1);
39+
Store_field(result, 0, v);
40+
return result;
41+
}
42+
}
43+
44+
value llvm_create_passbuilder_options(value Unit) {
45+
LLVMPassBuilderOptionsRef PBO = LLVMCreatePassBuilderOptions();
46+
return to_val(PBO);
47+
}
48+
49+
value llvm_passbuilder_options_set_verify_each(value PBO, value VerifyEach) {
50+
LLVMPassBuilderOptionsSetVerifyEach(PassBuilderOptions_val(PBO),
51+
Bool_val(VerifyEach));
52+
return Val_unit;
53+
}
54+
55+
value llvm_passbuilder_options_set_debug_logging(value PBO,
56+
value DebugLogging) {
57+
LLVMPassBuilderOptionsSetDebugLogging(PassBuilderOptions_val(PBO),
58+
Bool_val(DebugLogging));
59+
return Val_unit;
60+
}
61+
62+
value llvm_passbuilder_options_set_loop_interleaving(value PBO,
63+
value LoopInterleaving) {
64+
LLVMPassBuilderOptionsSetLoopInterleaving(PassBuilderOptions_val(PBO),
65+
Bool_val(LoopInterleaving));
66+
return Val_unit;
67+
}
68+
69+
value llvm_passbuilder_options_set_loop_vectorization(value PBO,
70+
value LoopVectorization) {
71+
LLVMPassBuilderOptionsSetLoopVectorization(PassBuilderOptions_val(PBO),
72+
Bool_val(LoopVectorization));
73+
return Val_unit;
74+
}
75+
76+
value llvm_passbuilder_options_set_slp_vectorization(value PBO,
77+
value SLPVectorization) {
78+
LLVMPassBuilderOptionsSetSLPVectorization(PassBuilderOptions_val(PBO),
79+
Bool_val(SLPVectorization));
80+
return Val_unit;
81+
}
82+
83+
value llvm_passbuilder_options_set_loop_unrolling(value PBO,
84+
value LoopUnrolling) {
85+
LLVMPassBuilderOptionsSetLoopUnrolling(PassBuilderOptions_val(PBO),
86+
Bool_val(LoopUnrolling));
87+
return Val_unit;
88+
}
89+
90+
value llvm_passbuilder_options_set_forget_all_scev_in_loop_unroll(
91+
value PBO, value ForgetAllSCEVInLoopUnroll) {
92+
LLVMPassBuilderOptionsSetForgetAllSCEVInLoopUnroll(
93+
PassBuilderOptions_val(PBO), Bool_val(ForgetAllSCEVInLoopUnroll));
94+
return Val_unit;
95+
}
96+
97+
value llvm_passbuilder_options_set_licm_mssa_opt_cap(value PBO,
98+
value LicmMssaOptCap) {
99+
LLVMPassBuilderOptionsSetLicmMssaOptCap(PassBuilderOptions_val(PBO),
100+
Int_val(LicmMssaOptCap));
101+
return Val_unit;
102+
}
103+
104+
value llvm_passbuilder_options_set_licm_mssa_no_acc_for_promotion_cap(
105+
value PBO, value LicmMssaNoAccForPromotionCap) {
106+
LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap(
107+
PassBuilderOptions_val(PBO), Int_val(LicmMssaNoAccForPromotionCap));
108+
return Val_unit;
109+
}
110+
111+
value llvm_passbuilder_options_set_call_graph_profile(value PBO,
112+
value CallGraphProfile) {
113+
LLVMPassBuilderOptionsSetCallGraphProfile(PassBuilderOptions_val(PBO),
114+
Bool_val(CallGraphProfile));
115+
return Val_unit;
116+
}
117+
118+
value llvm_passbuilder_options_set_merge_functions(value PBO,
119+
value MergeFunctions) {
120+
LLVMPassBuilderOptionsSetMergeFunctions(PassBuilderOptions_val(PBO),
121+
Bool_val(MergeFunctions));
122+
return Val_unit;
123+
}
124+
125+
value llvm_passbuilder_options_set_inliner_threshold(value PBO,
126+
value InlinerThreshold) {
127+
LLVMPassBuilderOptionsSetInlinerThreshold(PassBuilderOptions_val(PBO),
128+
Int_val(InlinerThreshold));
129+
return Val_unit;
130+
}
131+
132+
value llvm_dispose_passbuilder_options(value PBO) {
133+
LLVMDisposePassBuilderOptions(PassBuilderOptions_val(PBO));
134+
return Val_unit;
135+
}

0 commit comments

Comments
 (0)