JITTargetMachineBuilder.h 5.7 KB

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