aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm14/include/llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h
blob: 1bb7fae54fbc73c9a4672a93669a4a44aac11c7b (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===- JITTargetMachineBuilder.h - Build TargetMachines for JIT -*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// A utitily for building TargetMachines for JITs.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H
#define LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H

#include "llvm/ADT/Optional.h"
#include "llvm/ADT/Triple.h"
#include "llvm/MC/SubtargetFeature.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Error.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <memory>
#include <string>
#include <vector>

namespace llvm {

class raw_ostream;

namespace orc {

/// A utility class for building TargetMachines for JITs.
class JITTargetMachineBuilder {
#ifndef NDEBUG
  friend class JITTargetMachineBuilderPrinter;
#endif
public:
  /// Create a JITTargetMachineBuilder based on the given triple.
  ///
  /// Note: TargetOptions is default-constructed, then EmulatedTLS and
  /// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
  /// required, these values should be reset before calling
  /// createTargetMachine.
  JITTargetMachineBuilder(Triple TT);

  /// Create a JITTargetMachineBuilder for the host system.
  ///
  /// Note: TargetOptions is default-constructed, then EmulatedTLS and
  /// ExplicitEmulatedTLS are set to true. If EmulatedTLS is not
  /// required, these values should be reset before calling
  /// createTargetMachine.
  static Expected<JITTargetMachineBuilder> detectHost();

  /// Create a TargetMachine.
  ///
  /// This operation will fail if the requested target is not registered,
  /// in which case see llvm/Support/TargetSelect.h. To JIT IR the Target and
  /// the target's AsmPrinter must both be registered. To JIT assembly
  /// (including inline and module level assembly) the target's AsmParser must
  /// also be registered.
  Expected<std::unique_ptr<TargetMachine>> createTargetMachine();

  /// Get the default DataLayout for the target.
  ///
  /// Note: This is reasonably expensive, as it creates a temporary
  /// TargetMachine instance under the hood. It is only suitable for use during
  /// JIT setup.
  Expected<DataLayout> getDefaultDataLayoutForTarget() {
    auto TM = createTargetMachine();
    if (!TM)
      return TM.takeError();
    return (*TM)->createDataLayout();
  }

  /// Set the CPU string.
  JITTargetMachineBuilder &setCPU(std::string CPU) {
    this->CPU = std::move(CPU);
    return *this;
  }

  /// Returns the CPU string.
  const std::string &getCPU() const { return CPU; }

  /// Set the relocation model.
  JITTargetMachineBuilder &setRelocationModel(Optional<Reloc::Model> RM) {
    this->RM = std::move(RM);
    return *this;
  }

  /// Get the relocation model.
  const Optional<Reloc::Model> &getRelocationModel() const { return RM; }

  /// Set the code model.
  JITTargetMachineBuilder &setCodeModel(Optional<CodeModel::Model> CM) {
    this->CM = std::move(CM);
    return *this;
  }

  /// Get the code model.
  const Optional<CodeModel::Model> &getCodeModel() const { return CM; }

  /// Set the LLVM CodeGen optimization level.
  JITTargetMachineBuilder &setCodeGenOptLevel(CodeGenOpt::Level OptLevel) {
    this->OptLevel = OptLevel;
    return *this;
  }

  /// Set subtarget features.
  JITTargetMachineBuilder &setFeatures(StringRef FeatureString) {
    Features = SubtargetFeatures(FeatureString);
    return *this;
  }

  /// Add subtarget features.
  JITTargetMachineBuilder &
  addFeatures(const std::vector<std::string> &FeatureVec);

  /// Access subtarget features.
  SubtargetFeatures &getFeatures() { return Features; }

  /// Access subtarget features.
  const SubtargetFeatures &getFeatures() const { return Features; }

  /// Set TargetOptions.
  ///
  /// Note: This operation will overwrite any previously configured options,
  /// including EmulatedTLS and ExplicitEmulatedTLS which
  /// the JITTargetMachineBuilder sets by default. Clients are responsible
  /// for re-enabling these overwritten options.
  JITTargetMachineBuilder &setOptions(TargetOptions Options) {
    this->Options = std::move(Options);
    return *this;
  }

  /// Access TargetOptions.
  TargetOptions &getOptions() { return Options; }

  /// Access TargetOptions.
  const TargetOptions &getOptions() const { return Options; }

  /// Access Triple.
  Triple &getTargetTriple() { return TT; }

  /// Access Triple.
  const Triple &getTargetTriple() const { return TT; }

private:
  Triple TT;
  std::string CPU;
  SubtargetFeatures Features;
  TargetOptions Options;
  Optional<Reloc::Model> RM;
  Optional<CodeModel::Model> CM;
  CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
};

#ifndef NDEBUG
class JITTargetMachineBuilderPrinter {
public:
  JITTargetMachineBuilderPrinter(JITTargetMachineBuilder &JTMB,
                                 StringRef Indent)
      : JTMB(JTMB), Indent(Indent) {}
  void print(raw_ostream &OS) const;

  friend raw_ostream &operator<<(raw_ostream &OS,
                                 const JITTargetMachineBuilderPrinter &JTMBP) {
    JTMBP.print(OS);
    return OS;
  }

private:
  JITTargetMachineBuilder &JTMB;
  StringRef Indent;
};
#endif // NDEBUG

} // end namespace orc
} // end namespace llvm

#endif // LLVM_EXECUTIONENGINE_ORC_JITTARGETMACHINEBUILDER_H

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif