[llvm] 30416f3 - [llvm-c] Improve TargetMachine bindings (#70806)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 31 20:44:23 PDT 2023


Author: Yingwei Zheng
Date: 2023-11-01T11:44:19+08:00
New Revision: 30416f39be326b403e19f23da387009736483119

URL: https://github.com/llvm/llvm-project/commit/30416f39be326b403e19f23da387009736483119
DIFF: https://github.com/llvm/llvm-project/commit/30416f39be326b403e19f23da387009736483119.diff

LOG: [llvm-c] Improve TargetMachine bindings (#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 #70666.

Added: 
    

Modified: 
    llvm/bindings/ocaml/target/llvm_target.ml
    llvm/bindings/ocaml/target/llvm_target.mli
    llvm/bindings/ocaml/target/target_ocaml.c
    llvm/include/llvm-c/TargetMachine.h
    llvm/lib/Target/TargetMachineC.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/bindings/ocaml/target/llvm_target.ml b/llvm/bindings/ocaml/target/llvm_target.ml
index 29af0187f940b21..ff899f7b6d01a9b 100644
--- a/llvm/bindings/ocaml/target/llvm_target.ml
+++ b/llvm/bindings/ocaml/target/llvm_target.ml
@@ -44,6 +44,13 @@ module CodeGenFileType = struct
   | ObjectFile
 end
 
+module GlobalISelAbortMode = struct
+  type t =
+  | Enable
+  | Disable
+  | DisableWithDiag
+end
+
 exception Error of string
 
 let () = Callback.register_exception "Llvm_target.Error" (Error "")
@@ -124,6 +131,14 @@ module TargetMachine = struct
                        = "llvm_targetmachine_data_layout"
   external set_verbose_asm : bool -> t -> unit
                            = "llvm_targetmachine_set_verbose_asm"
+  external set_fast_isel : bool -> t -> unit
+                           = "llvm_targetmachine_set_fast_isel"
+  external set_global_isel : bool -> t -> unit
+                           = "llvm_targetmachine_set_global_isel"
+  external set_global_isel_abort : ?mode:GlobalISelAbortMode.t -> t -> unit
+                                 = "llvm_targetmachine_set_global_isel_abort"
+  external set_machine_outliner : bool -> t -> unit
+                                = "llvm_targetmachine_set_machine_outliner"
   external emit_to_file : Llvm.llmodule -> CodeGenFileType.t -> string ->
                           t -> unit
                         = "llvm_targetmachine_emit_to_file"

diff  --git a/llvm/bindings/ocaml/target/llvm_target.mli b/llvm/bindings/ocaml/target/llvm_target.mli
index 56ecb2d908dd374..4df15154feeb97f 100644
--- a/llvm/bindings/ocaml/target/llvm_target.mli
+++ b/llvm/bindings/ocaml/target/llvm_target.mli
@@ -49,6 +49,13 @@ module CodeGenFileType : sig
   | ObjectFile
 end
 
+module GlobalISelAbortMode : sig
+  type t =
+  | Enable
+  | Disable
+  | DisableWithDiag
+end
+
 (** {6 Exceptions} *)
 
 exception Error of string
@@ -204,6 +211,22 @@ module TargetMachine : sig
       See [llvm::TargetMachine::setAsmVerbosity]. *)
   val set_verbose_asm : bool -> t -> unit
 
+  (** Enable fast-path instruction selection.
+      See [llvm::TargetMachine::setFastISel]. *)
+  val set_fast_isel : bool -> t -> unit
+
+  (** Enable global instruction selection.
+      See [llvm::TargetMachine::setGlobalISel]. *)
+  val set_global_isel : bool -> t -> unit
+
+  (** Set abort behaviour when global instruction selection fails to lower/select an instruction.
+      See [llvm::TargetMachine::setGlobalISelAbort]. *)
+  val set_global_isel_abort : ?mode:GlobalISelAbortMode.t -> t -> unit
+
+  (** Enable the MachineOutliner pass.
+      See [llvm::TargetMachine::setMachineOutliner]. *)
+  val set_machine_outliner : bool -> t -> unit
+
   (** Emits assembly or object data for the given module to the given
       file or raise [Error]. *)
   val emit_to_file : Llvm.llmodule -> CodeGenFileType.t -> string -> t -> unit

diff  --git a/llvm/bindings/ocaml/target/target_ocaml.c b/llvm/bindings/ocaml/target/target_ocaml.c
index e80072d5eb6354a..94cf437b9348631 100644
--- a/llvm/bindings/ocaml/target/target_ocaml.c
+++ b/llvm/bindings/ocaml/target/target_ocaml.c
@@ -301,6 +301,35 @@ value llvm_targetmachine_set_verbose_asm(value Verb, value Machine) {
   return Val_unit;
 }
 
+/* bool -> TargetMachine.t -> unit */
+value llvm_targetmachine_set_fast_isel(value Enable, value Machine) {
+  LLVMSetTargetMachineFastISel(TargetMachine_val(Machine), Bool_val(Enable));
+  return Val_unit;
+}
+
+/* bool -> TargetMachine.t -> unit */
+value llvm_targetmachine_set_global_isel(value Enable, value Machine) {
+  LLVMSetTargetMachineGlobalISel(TargetMachine_val(Machine), Bool_val(Enable));
+  return Val_unit;
+}
+
+/* ?mode:GlobalISelAbortMode.t -> TargetMachine.t -> unit */
+value llvm_targetmachine_set_global_isel_abort(value Mode, value Machine) {
+  LLVMGlobalISelAbortMode AbortModeEnum = LLVMGlobalISelAbortEnable;
+  if (Mode != Val_int(0))
+    AbortModeEnum = Int_val(Field(Mode, 0));
+  LLVMSetTargetMachineGlobalISelAbort(TargetMachine_val(Machine),
+                                      AbortModeEnum);
+  return Val_unit;
+}
+
+/* bool -> TargetMachine.t -> unit */
+value llvm_targetmachine_set_machine_outliner(value Enable, value Machine) {
+  LLVMSetTargetMachineMachineOutliner(TargetMachine_val(Machine),
+                                      Bool_val(Enable));
+  return Val_unit;
+}
+
 /* Llvm.llmodule -> CodeGenFileType.t -> string -> TargetMachine.t -> unit */
 value llvm_targetmachine_emit_to_file(value Module, value FileType,
                                       value FileName, value Machine) {

diff  --git a/llvm/include/llvm-c/TargetMachine.h b/llvm/include/llvm-c/TargetMachine.h
index 0612fd0a19d6ed2..cbe8913803e0204 100644
--- a/llvm/include/llvm-c/TargetMachine.h
+++ b/llvm/include/llvm-c/TargetMachine.h
@@ -67,6 +67,12 @@ typedef enum {
     LLVMObjectFile
 } LLVMCodeGenFileType;
 
+typedef enum {
+  LLVMGlobalISelAbortEnable,
+  LLVMGlobalISelAbortDisable,
+  LLVMGlobalISelAbortDisableWithDiag,
+} LLVMGlobalISelAbortMode;
+
 /** Returns the first llvm::Target in the registered targets list. */
 LLVMTargetRef LLVMGetFirstTarget(void);
 /** Returns the next llvm::Target given a previous one (or null if there's none) */
@@ -182,6 +188,21 @@ LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T);
 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
                                       LLVMBool VerboseAsm);
 
+/** 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);
+
 /** Emits an asm or object file for the given module to the filename. This
   wraps several c++ only classes (among them a file stream). Returns any
   error in ErrorMessage. Use LLVMDisposeMessage to dispose the message. */

diff  --git a/llvm/lib/Target/TargetMachineC.cpp b/llvm/lib/Target/TargetMachineC.cpp
index 4d23f40c110d1cc..f6ed26be575595a 100644
--- a/llvm/lib/Target/TargetMachineC.cpp
+++ b/llvm/lib/Target/TargetMachineC.cpp
@@ -252,6 +252,40 @@ void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
   unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
 }
 
+void LLVMSetTargetMachineFastISel(LLVMTargetMachineRef T, LLVMBool Enable) {
+  unwrap(T)->setFastISel(Enable);
+}
+
+void LLVMSetTargetMachineGlobalISel(LLVMTargetMachineRef T, LLVMBool Enable) {
+  unwrap(T)->setGlobalISel(Enable);
+}
+
+void LLVMSetTargetMachineGlobalISelAbort(LLVMTargetMachineRef T,
+                                         LLVMGlobalISelAbortMode Mode) {
+  GlobalISelAbortMode AM;
+  switch (Mode) {
+  case LLVMGlobalISelAbortDisable:
+    AM = GlobalISelAbortMode::Disable;
+    break;
+  case LLVMGlobalISelAbortEnable:
+    AM = GlobalISelAbortMode::Enable;
+    break;
+  case LLVMGlobalISelAbortDisableWithDiag:
+    AM = GlobalISelAbortMode::DisableWithDiag;
+    break;
+  default:
+    AM = GlobalISelAbortMode::Enable;
+    break;
+  }
+
+  unwrap(T)->setGlobalISelAbort(AM);
+}
+
+void LLVMSetTargetMachineMachineOutliner(LLVMTargetMachineRef T,
+                                         LLVMBool Enable) {
+  unwrap(T)->setMachineOutliner(Enable);
+}
+
 LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) {
   return wrap(new DataLayout(unwrap(T)->createDataLayout()));
 }


        


More information about the llvm-commits mailing list