JITTargetMachineBuilder.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/Optional.h"
  20. #include "llvm/ADT/Triple.h"
  21. #include "llvm/MC/SubtargetFeature.h"
  22. #include "llvm/Support/CodeGen.h"
  23. #include "llvm/Support/Error.h"
  24. #include "llvm/Target/TargetMachine.h"
  25. #include "llvm/Target/TargetOptions.h"
  26. #include <memory>
  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. public:
  35. /// Create a JITTargetMachineBuilder based on the given triple.
  36. ///
  37. /// Note: TargetOptions is default-constructed, then EmulatedTLS and
  38. /// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
  39. /// required, these values should be reset before calling
  40. /// createTargetMachine.
  41. JITTargetMachineBuilder(Triple TT);
  42. /// Create a JITTargetMachineBuilder for the host system.
  43. ///
  44. /// Note: TargetOptions is default-constructed, then EmulatedTLS and
  45. /// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
  46. /// required, these values should be reset before calling
  47. /// createTargetMachine.
  48. static Expected<JITTargetMachineBuilder> detectHost();
  49. /// Create a TargetMachine.
  50. ///
  51. /// This operation will fail if the requested target is not registered,
  52. /// in which case see llvm/Support/TargetSelect.h. To JIT IR the Target and
  53. /// the target's AsmPrinter must both be registered. To JIT assembly
  54. /// (including inline and module level assembly) the target's AsmParser must
  55. /// also be registered.
  56. Expected<std::unique_ptr<TargetMachine>> createTargetMachine();
  57. /// Get the default DataLayout for the target.
  58. ///
  59. /// Note: This is reasonably expensive, as it creates a temporary
  60. /// TargetMachine instance under the hood. It is only suitable for use during
  61. /// JIT setup.
  62. Expected<DataLayout> getDefaultDataLayoutForTarget() {
  63. auto TM = createTargetMachine();
  64. if (!TM)
  65. return TM.takeError();
  66. return (*TM)->createDataLayout();
  67. }
  68. /// Set the CPU string.
  69. JITTargetMachineBuilder &setCPU(std::string CPU) {
  70. this->CPU = std::move(CPU);
  71. return *this;
  72. }
  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. #ifndef NDEBUG
  123. /// Debug-dump a JITTargetMachineBuilder.
  124. friend raw_ostream &operator<<(raw_ostream &OS,
  125. const JITTargetMachineBuilder &JTMB);
  126. #endif
  127. private:
  128. Triple TT;
  129. std::string CPU;
  130. SubtargetFeatures Features;
  131. TargetOptions Options;
  132. Optional<Reloc::Model> RM;
  133. Optional<CodeModel::Model> CM;
  134. CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
  135. };
  136. } // end namespace orc
  137. } // end namespace llvm
  138. #endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
  139. #ifdef __GNUC__
  140. #pragma GCC diagnostic pop
  141. #endif