JITTargetMachineBuilder.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //===- JITTargetMachineBuilder.h - Build TargetMachines for JIT -*- C++ -*-===//
  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. // A utitily for building TargetMachines for JITs.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
  13. #define LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
  14. #include "llvm/ADT/Optional.h"
  15. #include "llvm/ADT/Triple.h"
  16. #include "llvm/MC/SubtargetFeature.h"
  17. #include "llvm/Support/CodeGen.h"
  18. #include "llvm/Support/Error.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. #include "llvm/Target/TargetOptions.h"
  21. #include <memory>
  22. #include <string>
  23. #include <vector>
  24. namespace llvm {
  25. class raw_ostream;
  26. namespace orc {
  27. /// A utility class for building TargetMachines for JITs.
  28. class JITTargetMachineBuilder {
  29. #ifndef NDEBUG
  30. friend class JITTargetMachineBuilderPrinter;
  31. #endif
  32. public:
  33. /// Create a JITTargetMachineBuilder based on the given triple.
  34. ///
  35. /// Note: TargetOptions is default-constructed, then EmulatedTLS and
  36. /// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
  37. /// required, these values should be reset before calling
  38. /// createTargetMachine.
  39. JITTargetMachineBuilder(Triple TT);
  40. /// Create a JITTargetMachineBuilder for the host system.
  41. ///
  42. /// Note: TargetOptions is default-constructed, then EmulatedTLS and
  43. /// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
  44. /// required, these values should be reset before calling
  45. /// createTargetMachine.
  46. static Expected<JITTargetMachineBuilder> detectHost();
  47. /// Create a TargetMachine.
  48. ///
  49. /// This operation will fail if the requested target is not registered,
  50. /// in which case see llvm/Support/TargetSelect.h. To JIT IR the Target and
  51. /// the target's AsmPrinter must both be registered. To JIT assembly
  52. /// (including inline and module level assembly) the target's AsmParser must
  53. /// also be registered.
  54. Expected<std::unique_ptr<TargetMachine>> createTargetMachine();
  55. /// Get the default DataLayout for the target.
  56. ///
  57. /// Note: This is reasonably expensive, as it creates a temporary
  58. /// TargetMachine instance under the hood. It is only suitable for use during
  59. /// JIT setup.
  60. Expected<DataLayout> getDefaultDataLayoutForTarget() {
  61. auto TM = createTargetMachine();
  62. if (!TM)
  63. return TM.takeError();
  64. return (*TM)->createDataLayout();
  65. }
  66. /// Set the CPU string.
  67. JITTargetMachineBuilder &setCPU(std::string CPU) {
  68. this->CPU = std::move(CPU);
  69. return *this;
  70. }
  71. /// Returns the CPU string.
  72. const std::string &getCPU() const { return CPU; }
  73. /// Set the relocation model.
  74. JITTargetMachineBuilder &setRelocationModel(Optional<Reloc::Model> RM) {
  75. this->RM = std::move(RM);
  76. return *this;
  77. }
  78. /// Get the relocation model.
  79. const Optional<Reloc::Model> &getRelocationModel() const { return RM; }
  80. /// Set the code model.
  81. JITTargetMachineBuilder &setCodeModel(Optional<CodeModel::Model> CM) {
  82. this->CM = std::move(CM);
  83. return *this;
  84. }
  85. /// Get the code model.
  86. const Optional<CodeModel::Model> &getCodeModel() const { return CM; }
  87. /// Set the LLVM CodeGen optimization level.
  88. JITTargetMachineBuilder &setCodeGenOptLevel(CodeGenOpt::Level OptLevel) {
  89. this->OptLevel = OptLevel;
  90. return *this;
  91. }
  92. /// Set subtarget features.
  93. JITTargetMachineBuilder &setFeatures(StringRef FeatureString) {
  94. Features = SubtargetFeatures(FeatureString);
  95. return *this;
  96. }
  97. /// Add subtarget features.
  98. JITTargetMachineBuilder &
  99. addFeatures(const std::vector<std::string> &FeatureVec);
  100. /// Access subtarget features.
  101. SubtargetFeatures &getFeatures() { return Features; }
  102. /// Access subtarget features.
  103. const SubtargetFeatures &getFeatures() const { return Features; }
  104. /// Set TargetOptions.
  105. ///
  106. /// Note: This operation will overwrite any previously configured options,
  107. /// including EmulatedTLS and ExplicitEmulatedTLS which
  108. /// the JITTargetMachineBuilder sets by default. Clients are responsible
  109. /// for re-enabling these overwritten options.
  110. JITTargetMachineBuilder &setOptions(TargetOptions Options) {
  111. this->Options = std::move(Options);
  112. return *this;
  113. }
  114. /// Access TargetOptions.
  115. TargetOptions &getOptions() { return Options; }
  116. /// Access TargetOptions.
  117. const TargetOptions &getOptions() const { return Options; }
  118. /// Access Triple.
  119. Triple &getTargetTriple() { return TT; }
  120. /// Access Triple.
  121. const Triple &getTargetTriple() const { return TT; }
  122. private:
  123. Triple TT;
  124. std::string CPU;
  125. SubtargetFeatures Features;
  126. TargetOptions Options;
  127. Optional<Reloc::Model> RM;
  128. Optional<CodeModel::Model> CM;
  129. CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
  130. };
  131. #ifndef NDEBUG
  132. class JITTargetMachineBuilderPrinter {
  133. public:
  134. JITTargetMachineBuilderPrinter(JITTargetMachineBuilder &JTMB,
  135. StringRef Indent)
  136. : JTMB(JTMB), Indent(Indent) {}
  137. void print(raw_ostream &OS) const;
  138. friend raw_ostream &operator<<(raw_ostream &OS,
  139. const JITTargetMachineBuilderPrinter &JTMBP) {
  140. JTMBP.print(OS);
  141. return OS;
  142. }
  143. private:
  144. JITTargetMachineBuilder &JTMB;
  145. StringRef Indent;
  146. };
  147. #endif // NDEBUG
  148. } // end namespace orc
  149. } // end namespace llvm
  150. #endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H