aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm14/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
blob: 0fbf79b8a56d1cbe706257eef2bc8b8fcf5e00ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//===----- JITTargetMachineBuilder.cpp - Build TargetMachines for JIT -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"

#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/raw_ostream.h"

namespace llvm {
namespace orc {

JITTargetMachineBuilder::JITTargetMachineBuilder(Triple TT)
    : TT(std::move(TT)) {
  Options.EmulatedTLS = true;
  Options.ExplicitEmulatedTLS = true;
}

Expected<JITTargetMachineBuilder> JITTargetMachineBuilder::detectHost() {
  // FIXME: getProcessTriple is bogus. It returns the host LLVM was compiled on,
  //        rather than a valid triple for the current process.
  JITTargetMachineBuilder TMBuilder((Triple(sys::getProcessTriple())));

  // Retrieve host CPU name and sub-target features and add them to builder.
  // Relocation model, code model and codegen opt level are kept to default
  // values.
  llvm::StringMap<bool> FeatureMap;
  llvm::sys::getHostCPUFeatures(FeatureMap);
  for (auto &Feature : FeatureMap)
    TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);

  TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName()));

  return TMBuilder;
}

Expected<std::unique_ptr<TargetMachine>>
JITTargetMachineBuilder::createTargetMachine() {

  std::string ErrMsg;
  auto *TheTarget = TargetRegistry::lookupTarget(TT.getTriple(), ErrMsg);
  if (!TheTarget)
    return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());

  auto *TM =
      TheTarget->createTargetMachine(TT.getTriple(), CPU, Features.getString(),
                                     Options, RM, CM, OptLevel, /*JIT*/ true);
  if (!TM)
    return make_error<StringError>("Could not allocate target machine",
                                   inconvertibleErrorCode());

  return std::unique_ptr<TargetMachine>(TM);
}

JITTargetMachineBuilder &JITTargetMachineBuilder::addFeatures(
    const std::vector<std::string> &FeatureVec) {
  for (const auto &F : FeatureVec)
    Features.AddFeature(F);
  return *this;
}

#ifndef NDEBUG
void JITTargetMachineBuilderPrinter::print(raw_ostream &OS) const {
  OS << Indent << "{\n"
     << Indent << "  Triple = \"" << JTMB.TT.str() << "\"\n"
     << Indent << "  CPU = \"" << JTMB.CPU << "\"\n"
     << Indent << "  Features = \"" << JTMB.Features.getString() << "\"\n"
     << Indent << "  Options = <not-printable>\n"
     << Indent << "  Relocation Model = ";

  if (JTMB.RM) {
    switch (*JTMB.RM) {
    case Reloc::Static:
      OS << "Static";
      break;
    case Reloc::PIC_:
      OS << "PIC_";
      break;
    case Reloc::DynamicNoPIC:
      OS << "DynamicNoPIC";
      break;
    case Reloc::ROPI:
      OS << "ROPI";
      break;
    case Reloc::RWPI:
      OS << "RWPI";
      break;
    case Reloc::ROPI_RWPI:
      OS << "ROPI_RWPI";
      break;
    }
  } else
    OS << "unspecified (will use target default)";

  OS << "\n"
     << Indent << "  Code Model = ";

  if (JTMB.CM) {
    switch (*JTMB.CM) {
    case CodeModel::Tiny:
      OS << "Tiny";
      break;
    case CodeModel::Small:
      OS << "Small";
      break;
    case CodeModel::Kernel:
      OS << "Kernel";
      break;
    case CodeModel::Medium:
      OS << "Medium";
      break;
    case CodeModel::Large:
      OS << "Large";
      break;
    }
  } else
    OS << "unspecified (will use target default)";

  OS << "\n"
     << Indent << "  Optimization Level = ";
  switch (JTMB.OptLevel) {
  case CodeGenOpt::None:
    OS << "None";
    break;
  case CodeGenOpt::Less:
    OS << "Less";
    break;
  case CodeGenOpt::Default:
    OS << "Default";
    break;
  case CodeGenOpt::Aggressive:
    OS << "Aggressive";
    break;
  }

  OS << "\n" << Indent << "}\n";
}
#endif // NDEBUG

} // End namespace orc.
} // End namespace llvm.