JITTargetMachineBuilder.cpp 4.1 KB

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