JITTargetMachineBuilder.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //===----- JITTargetMachineBuilder.cpp - Build TargetMachines for JIT -----===//
  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. #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
  9. #include "llvm/MC/TargetRegistry.h"
  10. #include "llvm/Support/Host.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. namespace llvm {
  13. namespace orc {
  14. JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT)
  15. : TT(std::move(TT)) {
  16. Options.EmulatedTLS = true;
  17. Options.ExplicitEmulatedTLS = true;
  18. }
  19. Expected<JITTargetMachineBuilder> JITTargetMachineBuilder::detectHost() {
  20. // FIXME: getProcessTriple is bogus. It returns the host LLVM was compiled on,
  21. // rather than a valid triple for the current process.
  22. JITTargetMachineBuilder TMBuilder((Triple(sys::getProcessTriple())));
  23. // Retrieve host CPU name and sub-target features and add them to builder.
  24. // Relocation model, code model and codegen opt level are kept to default
  25. // values.
  26. llvm::StringMap<bool> FeatureMap;
  27. llvm::sys::getHostCPUFeatures(FeatureMap);
  28. for (auto &Feature : FeatureMap)
  29. TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
  30. TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName()));
  31. return TMBuilder;
  32. }
  33. Expected<std::unique_ptr<TargetMachine>>
  34. JITTargetMachineBuilder::createTargetMachine() {
  35. std::string ErrMsg;
  36. auto *TheTarget = TargetRegistry::lookupTarget(TT.getTriple(), ErrMsg);
  37. if (!TheTarget)
  38. return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
  39. auto *TM =
  40. TheTarget->createTargetMachine(TT.getTriple(), CPU, Features.getString(),
  41. Options, RM, CM, OptLevel, /*JIT*/ true);
  42. if (!TM)
  43. return make_error<StringError>("Could not allocate target machine",
  44. inconvertibleErrorCode());
  45. return std::unique_ptr<TargetMachine>(TM);
  46. }
  47. JITTargetMachineBuilder &JITTargetMachineBuilder::addFeatures(
  48. const std::vector<std::string> &FeatureVec) {
  49. for (const auto &F : FeatureVec)
  50. Features.AddFeature(F);
  51. return *this;
  52. }
  53. #ifndef NDEBUG
  54. void JITTargetMachineBuilderPrinter::print(raw_ostream &OS) const {
  55. OS << Indent << "{\n"
  56. << Indent << " Triple = \"" << JTMB.TT.str() << "\"\n"
  57. << Indent << " CPU = \"" << JTMB.CPU << "\"\n"
  58. << Indent << " Features = \"" << JTMB.Features.getString() << "\"\n"
  59. << Indent << " Options = <not-printable>\n"
  60. << Indent << " Relocation Model = ";
  61. if (JTMB.RM) {
  62. switch (*JTMB.RM) {
  63. case Reloc::Static:
  64. OS << "Static";
  65. break;
  66. case Reloc::PIC_:
  67. OS << "PIC_";
  68. break;
  69. case Reloc::DynamicNoPIC:
  70. OS << "DynamicNoPIC";
  71. break;
  72. case Reloc::ROPI:
  73. OS << "ROPI";
  74. break;
  75. case Reloc::RWPI:
  76. OS << "RWPI";
  77. break;
  78. case Reloc::ROPI_RWPI:
  79. OS << "ROPI_RWPI";
  80. break;
  81. }
  82. } else
  83. OS << "unspecified (will use target default)";
  84. OS << "\n"
  85. << Indent << " Code Model = ";
  86. if (JTMB.CM) {
  87. switch (*JTMB.CM) {
  88. case CodeModel::Tiny:
  89. OS << "Tiny";
  90. break;
  91. case CodeModel::Small:
  92. OS << "Small";
  93. break;
  94. case CodeModel::Kernel:
  95. OS << "Kernel";
  96. break;
  97. case CodeModel::Medium:
  98. OS << "Medium";
  99. break;
  100. case CodeModel::Large:
  101. OS << "Large";
  102. break;
  103. }
  104. } else
  105. OS << "unspecified (will use target default)";
  106. OS << "\n"
  107. << Indent << " Optimization Level = ";
  108. switch (JTMB.OptLevel) {
  109. case CodeGenOpt::None:
  110. OS << "None";
  111. break;
  112. case CodeGenOpt::Less:
  113. OS << "Less";
  114. break;
  115. case CodeGenOpt::Default:
  116. OS << "Default";
  117. break;
  118. case CodeGenOpt::Aggressive:
  119. OS << "Aggressive";
  120. break;
  121. }
  122. OS << "\n" << Indent << "}\n";
  123. }
  124. #endif // NDEBUG
  125. } // End namespace orc.
  126. } // End namespace llvm.