File: ThinLtoJIT.h

package info (click to toggle)
llvm-toolchain-11 1%3A11.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 995,808 kB
  • sloc: cpp: 4,767,656; ansic: 760,916; asm: 477,436; python: 170,940; objc: 69,804; lisp: 29,914; sh: 23,855; f90: 18,173; pascal: 7,551; perl: 7,471; ml: 5,603; awk: 3,489; makefile: 2,573; xml: 915; cs: 573; fortran: 503; javascript: 452
file content (111 lines) | stat: -rw-r--r-- 3,380 bytes parent folder | download | duplicates (2)
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
#ifndef LLVM_EXAMPLES_THINLTOJIT_THINLTOJIT_H
#define LLVM_EXAMPLES_THINLTOJIT_THINLTOJIT_H

#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ThreadPool.h"

#include <functional>
#include <memory>
#include <string>
#include <thread>
#include <vector>

namespace llvm {
namespace orc {

class ThinLtoDiscoveryThread;
class ThinLtoInstrumentationLayer;
class ThinLtoModuleIndex;

class CompileOnDemandLayer;
class IRCompileLayer;
class RTDyldObjectLinkingLayer;

class JITDylib;
class JITTargetMachineBuilder;
class LazyCallThroughManager;
class MangleAndInterner;

class ThinLtoJIT {
public:
  using AddModuleFunction = std::function<Error(ThreadSafeModule)>;

  enum ExplicitMemoryBarrier {
    NeverFence = 0,
    FenceStaticCode = 1,
    FenceJITedCode = 2,
    AlwaysFence = 3
  };

  ThinLtoJIT(ArrayRef<std::string> InputFiles, StringRef MainFunctionName,
             unsigned LookaheadLevels, unsigned NumCompileThreads,
             unsigned NumLoadThreads, unsigned DiscoveryFlagsPerBucket,
             ExplicitMemoryBarrier MemFence, bool AllowNudgeIntoDiscovery,
             bool PrintStats, Error &Err);
  ~ThinLtoJIT();

  ThinLtoJIT(const ThinLtoJIT &) = delete;
  ThinLtoJIT &operator=(const ThinLtoJIT &) = delete;
  ThinLtoJIT(ThinLtoJIT &&) = delete;
  ThinLtoJIT &operator=(ThinLtoJIT &&) = delete;

  Expected<int> main(ArrayRef<std::string> Args) {
    auto MainSym = ES.lookup({MainJD}, MainFunctionMangled);
    if (!MainSym)
      return MainSym.takeError();

    using MainFn = int(int, char *[]);
    auto Main = jitTargetAddressToFunction<MainFn *>(MainSym->getAddress());

    return runAsMain(Main, Args, StringRef("ThinLtoJIT"));
  }

private:
  ExecutionSession ES;
  DataLayout DL{""};

  JITDylib *MainJD;
  SymbolStringPtr MainFunctionMangled;
  std::unique_ptr<ThreadPool> CompileThreads;
  std::unique_ptr<ThinLtoModuleIndex> GlobalIndex;

  AddModuleFunction AddModule;
  std::unique_ptr<RTDyldObjectLinkingLayer> ObjLinkingLayer;
  std::unique_ptr<IRCompileLayer> CompileLayer;
  std::unique_ptr<ThinLtoInstrumentationLayer> InstrumentationLayer;
  std::unique_ptr<CompileOnDemandLayer> OnDemandLayer;

  std::atomic<bool> JitRunning;
  std::thread DiscoveryThread;
  std::unique_ptr<ThinLtoDiscoveryThread> DiscoveryThreadWorker;

  std::unique_ptr<MangleAndInterner> Mangle;
  std::unique_ptr<LazyCallThroughManager> CallThroughManager;

  void setupLayers(JITTargetMachineBuilder JTMB, unsigned NumCompileThreads,
                   unsigned DiscoveryFlagsPerBucket,
                   ExplicitMemoryBarrier MemFence);
  Error setupJITDylib(JITDylib *JD, bool AllowNudge, bool PrintStats);
  void setupDiscovery(JITDylib *MainJD, unsigned LookaheadLevels,
                      bool PrintStats);
  Expected<ThreadSafeModule> setupMainModule(StringRef MainFunction);
  Expected<JITTargetMachineBuilder> setupTargetUtils(Module *M);
  Error applyDataLayout(Module *M);

  static void exitOnLazyCallThroughFailure() {
    errs() << "Compilation failed. Aborting.\n";
    exit(1);
  }
};

} // namespace orc
} // namespace llvm

#endif