Skip to content

Commit 30416f3

Browse files
authored
[llvm-c] Improve TargetMachine bindings (llvm#70806)
This PR exposes four APIs to C/OCaml bindings for `TargetMachine`: ``` /** Enable fast-path instruction selection. */ void LLVMSetTargetMachineFastISel(LLVMTargetMachineRef T, LLVMBool Enable); /** Enable global instruction selection. */ void LLVMSetTargetMachineGlobalISel(LLVMTargetMachineRef T, LLVMBool Enable); /** Set abort behaviour when global instruction selection fails to lower/select * an instruction. */ void LLVMSetTargetMachineGlobalISelAbort(LLVMTargetMachineRef T, LLVMGlobalISelAbortMode Mode); /** Enable the MachineOutliner pass. */ void LLVMSetTargetMachineMachineOutliner(LLVMTargetMachineRef T, LLVMBool Enable); ``` Fixes llvm#70666.
1 parent 455098d commit 30416f3

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

llvm/bindings/ocaml/target/llvm_target.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ module CodeGenFileType = struct
4444
| ObjectFile
4545
end
4646

47+
module GlobalISelAbortMode = struct
48+
type t =
49+
| Enable
50+
| Disable
51+
| DisableWithDiag
52+
end
53+
4754
exception Error of string
4855

4956
let () = Callback.register_exception "Llvm_target.Error" (Error "")
@@ -124,6 +131,14 @@ module TargetMachine = struct
124131
= "llvm_targetmachine_data_layout"
125132
external set_verbose_asm : bool -> t -> unit
126133
= "llvm_targetmachine_set_verbose_asm"
134+
external set_fast_isel : bool -> t -> unit
135+
= "llvm_targetmachine_set_fast_isel"
136+
external set_global_isel : bool -> t -> unit
137+
= "llvm_targetmachine_set_global_isel"
138+
external set_global_isel_abort : ?mode:GlobalISelAbortMode.t -> t -> unit
139+
= "llvm_targetmachine_set_global_isel_abort"
140+
external set_machine_outliner : bool -> t -> unit
141+
= "llvm_targetmachine_set_machine_outliner"
127142
external emit_to_file : Llvm.llmodule -> CodeGenFileType.t -> string ->
128143
t -> unit
129144
= "llvm_targetmachine_emit_to_file"

llvm/bindings/ocaml/target/llvm_target.mli

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ module CodeGenFileType : sig
4949
| ObjectFile
5050
end
5151

52+
module GlobalISelAbortMode : sig
53+
type t =
54+
| Enable
55+
| Disable
56+
| DisableWithDiag
57+
end
58+
5259
(** {6 Exceptions} *)
5360

5461
exception Error of string
@@ -204,6 +211,22 @@ module TargetMachine : sig
204211
See [llvm::TargetMachine::setAsmVerbosity]. *)
205212
val set_verbose_asm : bool -> t -> unit
206213

214+
(** Enable fast-path instruction selection.
215+
See [llvm::TargetMachine::setFastISel]. *)
216+
val set_fast_isel : bool -> t -> unit
217+
218+
(** Enable global instruction selection.
219+
See [llvm::TargetMachine::setGlobalISel]. *)
220+
val set_global_isel : bool -> t -> unit
221+
222+
(** Set abort behaviour when global instruction selection fails to lower/select an instruction.
223+
See [llvm::TargetMachine::setGlobalISelAbort]. *)
224+
val set_global_isel_abort : ?mode:GlobalISelAbortMode.t -> t -> unit
225+
226+
(** Enable the MachineOutliner pass.
227+
See [llvm::TargetMachine::setMachineOutliner]. *)
228+
val set_machine_outliner : bool -> t -> unit
229+
207230
(** Emits assembly or object data for the given module to the given
208231
file or raise [Error]. *)
209232
val emit_to_file : Llvm.llmodule -> CodeGenFileType.t -> string -> t -> unit

llvm/bindings/ocaml/target/target_ocaml.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,35 @@ value llvm_targetmachine_set_verbose_asm(value Verb, value Machine) {
301301
return Val_unit;
302302
}
303303

304+
/* bool -> TargetMachine.t -> unit */
305+
value llvm_targetmachine_set_fast_isel(value Enable, value Machine) {
306+
LLVMSetTargetMachineFastISel(TargetMachine_val(Machine), Bool_val(Enable));
307+
return Val_unit;
308+
}
309+
310+
/* bool -> TargetMachine.t -> unit */
311+
value llvm_targetmachine_set_global_isel(value Enable, value Machine) {
312+
LLVMSetTargetMachineGlobalISel(TargetMachine_val(Machine), Bool_val(Enable));
313+
return Val_unit;
314+
}
315+
316+
/* ?mode:GlobalISelAbortMode.t -> TargetMachine.t -> unit */
317+
value llvm_targetmachine_set_global_isel_abort(value Mode, value Machine) {
318+
LLVMGlobalISelAbortMode AbortModeEnum = LLVMGlobalISelAbortEnable;
319+
if (Mode != Val_int(0))
320+
AbortModeEnum = Int_val(Field(Mode, 0));
321+
LLVMSetTargetMachineGlobalISelAbort(TargetMachine_val(Machine),
322+
AbortModeEnum);
323+
return Val_unit;
324+
}
325+
326+
/* bool -> TargetMachine.t -> unit */
327+
value llvm_targetmachine_set_machine_outliner(value Enable, value Machine) {
328+
LLVMSetTargetMachineMachineOutliner(TargetMachine_val(Machine),
329+
Bool_val(Enable));
330+
return Val_unit;
331+
}
332+
304333
/* Llvm.llmodule -> CodeGenFileType.t -> string -> TargetMachine.t -> unit */
305334
value llvm_targetmachine_emit_to_file(value Module, value FileType,
306335
value FileName, value Machine) {

llvm/include/llvm-c/TargetMachine.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ typedef enum {
6767
LLVMObjectFile
6868
} LLVMCodeGenFileType;
6969

70+
typedef enum {
71+
LLVMGlobalISelAbortEnable,
72+
LLVMGlobalISelAbortDisable,
73+
LLVMGlobalISelAbortDisableWithDiag,
74+
} LLVMGlobalISelAbortMode;
75+
7076
/** Returns the first llvm::Target in the registered targets list. */
7177
LLVMTargetRef LLVMGetFirstTarget(void);
7278
/** Returns the next llvm::Target given a previous one (or null if there's none) */
@@ -182,6 +188,21 @@ LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T);
182188
void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
183189
LLVMBool VerboseAsm);
184190

191+
/** Enable fast-path instruction selection. */
192+
void LLVMSetTargetMachineFastISel(LLVMTargetMachineRef T, LLVMBool Enable);
193+
194+
/** Enable global instruction selection. */
195+
void LLVMSetTargetMachineGlobalISel(LLVMTargetMachineRef T, LLVMBool Enable);
196+
197+
/** Set abort behaviour when global instruction selection fails to lower/select
198+
* an instruction. */
199+
void LLVMSetTargetMachineGlobalISelAbort(LLVMTargetMachineRef T,
200+
LLVMGlobalISelAbortMode Mode);
201+
202+
/** Enable the MachineOutliner pass. */
203+
void LLVMSetTargetMachineMachineOutliner(LLVMTargetMachineRef T,
204+
LLVMBool Enable);
205+
185206
/** Emits an asm or object file for the given module to the filename. This
186207
wraps several c++ only classes (among them a file stream). Returns any
187208
error in ErrorMessage. Use LLVMDisposeMessage to dispose the message. */

llvm/lib/Target/TargetMachineC.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,40 @@ void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
252252
unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
253253
}
254254

255+
void LLVMSetTargetMachineFastISel(LLVMTargetMachineRef T, LLVMBool Enable) {
256+
unwrap(T)->setFastISel(Enable);
257+
}
258+
259+
void LLVMSetTargetMachineGlobalISel(LLVMTargetMachineRef T, LLVMBool Enable) {
260+
unwrap(T)->setGlobalISel(Enable);
261+
}
262+
263+
void LLVMSetTargetMachineGlobalISelAbort(LLVMTargetMachineRef T,
264+
LLVMGlobalISelAbortMode Mode) {
265+
GlobalISelAbortMode AM;
266+
switch (Mode) {
267+
case LLVMGlobalISelAbortDisable:
268+
AM = GlobalISelAbortMode::Disable;
269+
break;
270+
case LLVMGlobalISelAbortEnable:
271+
AM = GlobalISelAbortMode::Enable;
272+
break;
273+
case LLVMGlobalISelAbortDisableWithDiag:
274+
AM = GlobalISelAbortMode::DisableWithDiag;
275+
break;
276+
default:
277+
AM = GlobalISelAbortMode::Enable;
278+
break;
279+
}
280+
281+
unwrap(T)->setGlobalISelAbort(AM);
282+
}
283+
284+
void LLVMSetTargetMachineMachineOutliner(LLVMTargetMachineRef T,
285+
LLVMBool Enable) {
286+
unwrap(T)->setMachineOutliner(Enable);
287+
}
288+
255289
LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) {
256290
return wrap(new DataLayout(unwrap(T)->createDataLayout()));
257291
}

0 commit comments

Comments
 (0)