LLJIT.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===----- LLJIT.h -- An ORC-based JIT for compiling LLVM IR ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // An ORC-based JIT for compiling LLVM IR.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_LLJIT_H
  18. #define LLVM_EXECUTIONENGINE_ORC_LLJIT_H
  19. #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
  20. #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
  21. #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
  22. #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
  23. #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
  24. #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
  25. #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Support/ThreadPool.h"
  28. namespace llvm {
  29. namespace orc {
  30. class LLJITBuilderState;
  31. class LLLazyJITBuilderState;
  32. class ObjectTransformLayer;
  33. class TargetProcessControl;
  34. /// A pre-fabricated ORC JIT stack that can serve as an alternative to MCJIT.
  35. ///
  36. /// Create instances using LLJITBuilder.
  37. class LLJIT {
  38. template <typename, typename, typename> friend class LLJITBuilderSetters;
  39. friend void setUpGenericLLVMIRPlatform(LLJIT &J);
  40. public:
  41. /// Initializer support for LLJIT.
  42. class PlatformSupport {
  43. public:
  44. virtual ~PlatformSupport();
  45. virtual Error initialize(JITDylib &JD) = 0;
  46. virtual Error deinitialize(JITDylib &JD) = 0;
  47. protected:
  48. static void setInitTransform(LLJIT &J,
  49. IRTransformLayer::TransformFunction T);
  50. };
  51. /// Destruct this instance. If a multi-threaded instance, waits for all
  52. /// compile threads to complete.
  53. ~LLJIT();
  54. /// Returns the ExecutionSession for this instance.
  55. ExecutionSession &getExecutionSession() { return *ES; }
  56. /// Returns a reference to the triple for this instance.
  57. const Triple &getTargetTriple() const { return TT; }
  58. /// Returns a reference to the DataLayout for this instance.
  59. const DataLayout &getDataLayout() const { return DL; }
  60. /// Returns a reference to the JITDylib representing the JIT'd main program.
  61. JITDylib &getMainJITDylib() { return *Main; }
  62. /// Returns the JITDylib with the given name, or nullptr if no JITDylib with
  63. /// that name exists.
  64. JITDylib *getJITDylibByName(StringRef Name) {
  65. return ES->getJITDylibByName(Name);
  66. }
  67. /// Create a new JITDylib with the given name and return a reference to it.
  68. ///
  69. /// JITDylib names must be unique. If the given name is derived from user
  70. /// input or elsewhere in the environment then the client should check
  71. /// (e.g. by calling getJITDylibByName) that the given name is not already in
  72. /// use.
  73. Expected<JITDylib &> createJITDylib(std::string Name) {
  74. return ES->createJITDylib(std::move(Name));
  75. }
  76. /// Adds an IR module with the given ResourceTracker.
  77. Error addIRModule(ResourceTrackerSP RT, ThreadSafeModule TSM);
  78. /// Adds an IR module to the given JITDylib.
  79. Error addIRModule(JITDylib &JD, ThreadSafeModule TSM);
  80. /// Adds an IR module to the Main JITDylib.
  81. Error addIRModule(ThreadSafeModule TSM) {
  82. return addIRModule(*Main, std::move(TSM));
  83. }
  84. /// Adds an object file to the given JITDylib.
  85. Error addObjectFile(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> Obj);
  86. /// Adds an object file to the given JITDylib.
  87. Error addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj);
  88. /// Adds an object file to the given JITDylib.
  89. Error addObjectFile(std::unique_ptr<MemoryBuffer> Obj) {
  90. return addObjectFile(*Main, std::move(Obj));
  91. }
  92. /// Look up a symbol in JITDylib JD by the symbol's linker-mangled name (to
  93. /// look up symbols based on their IR name use the lookup function instead).
  94. Expected<JITEvaluatedSymbol> lookupLinkerMangled(JITDylib &JD,
  95. SymbolStringPtr Name);
  96. /// Look up a symbol in JITDylib JD by the symbol's linker-mangled name (to
  97. /// look up symbols based on their IR name use the lookup function instead).
  98. Expected<JITEvaluatedSymbol> lookupLinkerMangled(JITDylib &JD,
  99. StringRef Name) {
  100. return lookupLinkerMangled(JD, ES->intern(Name));
  101. }
  102. /// Look up a symbol in the main JITDylib by the symbol's linker-mangled name
  103. /// (to look up symbols based on their IR name use the lookup function
  104. /// instead).
  105. Expected<JITEvaluatedSymbol> lookupLinkerMangled(StringRef Name) {
  106. return lookupLinkerMangled(*Main, Name);
  107. }
  108. /// Look up a symbol in JITDylib JD based on its IR symbol name.
  109. Expected<JITEvaluatedSymbol> lookup(JITDylib &JD, StringRef UnmangledName) {
  110. return lookupLinkerMangled(JD, mangle(UnmangledName));
  111. }
  112. /// Look up a symbol in the main JITDylib based on its IR symbol name.
  113. Expected<JITEvaluatedSymbol> lookup(StringRef UnmangledName) {
  114. return lookup(*Main, UnmangledName);
  115. }
  116. /// Set the PlatformSupport instance.
  117. void setPlatformSupport(std::unique_ptr<PlatformSupport> PS) {
  118. this->PS = std::move(PS);
  119. }
  120. /// Get the PlatformSupport instance.
  121. PlatformSupport *getPlatformSupport() { return PS.get(); }
  122. /// Run the initializers for the given JITDylib.
  123. Error initialize(JITDylib &JD) {
  124. DEBUG_WITH_TYPE("orc", {
  125. dbgs() << "LLJIT running initializers for JITDylib \"" << JD.getName()
  126. << "\"\n";
  127. });
  128. assert(PS && "PlatformSupport must be set to run initializers.");
  129. return PS->initialize(JD);
  130. }
  131. /// Run the deinitializers for the given JITDylib.
  132. Error deinitialize(JITDylib &JD) {
  133. DEBUG_WITH_TYPE("orc", {
  134. dbgs() << "LLJIT running deinitializers for JITDylib \"" << JD.getName()
  135. << "\"\n";
  136. });
  137. assert(PS && "PlatformSupport must be set to run initializers.");
  138. return PS->deinitialize(JD);
  139. }
  140. /// Returns a reference to the ObjLinkingLayer
  141. ObjectLayer &getObjLinkingLayer() { return *ObjLinkingLayer; }
  142. /// Returns a reference to the object transform layer.
  143. ObjectTransformLayer &getObjTransformLayer() { return *ObjTransformLayer; }
  144. /// Returns a reference to the IR transform layer.
  145. IRTransformLayer &getIRTransformLayer() { return *TransformLayer; }
  146. /// Returns a reference to the IR compile layer.
  147. IRCompileLayer &getIRCompileLayer() { return *CompileLayer; }
  148. /// Returns a linker-mangled version of UnmangledName.
  149. std::string mangle(StringRef UnmangledName) const;
  150. /// Returns an interned, linker-mangled version of UnmangledName.
  151. SymbolStringPtr mangleAndIntern(StringRef UnmangledName) const {
  152. return ES->intern(mangle(UnmangledName));
  153. }
  154. protected:
  155. static Expected<std::unique_ptr<ObjectLayer>>
  156. createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES);
  157. static Expected<std::unique_ptr<IRCompileLayer::IRCompiler>>
  158. createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB);
  159. /// Create an LLJIT instance with a single compile thread.
  160. LLJIT(LLJITBuilderState &S, Error &Err);
  161. Error applyDataLayout(Module &M);
  162. void recordCtorDtors(Module &M);
  163. std::unique_ptr<ExecutionSession> ES;
  164. std::unique_ptr<PlatformSupport> PS;
  165. JITDylib *Main = nullptr;
  166. DataLayout DL;
  167. Triple TT;
  168. std::unique_ptr<ThreadPool> CompileThreads;
  169. std::unique_ptr<ObjectLayer> ObjLinkingLayer;
  170. std::unique_ptr<ObjectTransformLayer> ObjTransformLayer;
  171. std::unique_ptr<IRCompileLayer> CompileLayer;
  172. std::unique_ptr<IRTransformLayer> TransformLayer;
  173. std::unique_ptr<IRTransformLayer> InitHelperTransformLayer;
  174. };
  175. /// An extended version of LLJIT that supports lazy function-at-a-time
  176. /// compilation of LLVM IR.
  177. class LLLazyJIT : public LLJIT {
  178. template <typename, typename, typename> friend class LLJITBuilderSetters;
  179. public:
  180. /// Sets the partition function.
  181. void
  182. setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition) {
  183. CODLayer->setPartitionFunction(std::move(Partition));
  184. }
  185. /// Returns a reference to the on-demand layer.
  186. CompileOnDemandLayer &getCompileOnDemandLayer() { return *CODLayer; }
  187. /// Add a module to be lazily compiled to JITDylib JD.
  188. Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M);
  189. /// Add a module to be lazily compiled to the main JITDylib.
  190. Error addLazyIRModule(ThreadSafeModule M) {
  191. return addLazyIRModule(*Main, std::move(M));
  192. }
  193. private:
  194. // Create a single-threaded LLLazyJIT instance.
  195. LLLazyJIT(LLLazyJITBuilderState &S, Error &Err);
  196. std::unique_ptr<LazyCallThroughManager> LCTMgr;
  197. std::unique_ptr<CompileOnDemandLayer> CODLayer;
  198. };
  199. class LLJITBuilderState {
  200. public:
  201. using ObjectLinkingLayerCreator =
  202. std::function<Expected<std::unique_ptr<ObjectLayer>>(ExecutionSession &,
  203. const Triple &)>;
  204. using CompileFunctionCreator =
  205. std::function<Expected<std::unique_ptr<IRCompileLayer::IRCompiler>>(
  206. JITTargetMachineBuilder JTMB)>;
  207. using PlatformSetupFunction = std::function<Error(LLJIT &J)>;
  208. std::unique_ptr<ExecutionSession> ES;
  209. Optional<JITTargetMachineBuilder> JTMB;
  210. Optional<DataLayout> DL;
  211. ObjectLinkingLayerCreator CreateObjectLinkingLayer;
  212. CompileFunctionCreator CreateCompileFunction;
  213. PlatformSetupFunction SetUpPlatform;
  214. unsigned NumCompileThreads = 0;
  215. TargetProcessControl *TPC = nullptr;
  216. /// Called prior to JIT class construcion to fix up defaults.
  217. Error prepareForConstruction();
  218. };
  219. template <typename JITType, typename SetterImpl, typename State>
  220. class LLJITBuilderSetters {
  221. public:
  222. /// Set an ExecutionSession for this instance.
  223. SetterImpl &setExecutionSession(std::unique_ptr<ExecutionSession> ES) {
  224. impl().ES = std::move(ES);
  225. return impl();
  226. }
  227. /// Set the JITTargetMachineBuilder for this instance.
  228. ///
  229. /// If this method is not called, JITTargetMachineBuilder::detectHost will be
  230. /// used to construct a default target machine builder for the host platform.
  231. SetterImpl &setJITTargetMachineBuilder(JITTargetMachineBuilder JTMB) {
  232. impl().JTMB = std::move(JTMB);
  233. return impl();
  234. }
  235. /// Return a reference to the JITTargetMachineBuilder.
  236. ///
  237. Optional<JITTargetMachineBuilder> &getJITTargetMachineBuilder() {
  238. return impl().JTMB;
  239. }
  240. /// Set a DataLayout for this instance. If no data layout is specified then
  241. /// the target's default data layout will be used.
  242. SetterImpl &setDataLayout(Optional<DataLayout> DL) {
  243. impl().DL = std::move(DL);
  244. return impl();
  245. }
  246. /// Set an ObjectLinkingLayer creation function.
  247. ///
  248. /// If this method is not called, a default creation function will be used
  249. /// that will construct an RTDyldObjectLinkingLayer.
  250. SetterImpl &setObjectLinkingLayerCreator(
  251. LLJITBuilderState::ObjectLinkingLayerCreator CreateObjectLinkingLayer) {
  252. impl().CreateObjectLinkingLayer = std::move(CreateObjectLinkingLayer);
  253. return impl();
  254. }
  255. /// Set a CompileFunctionCreator.
  256. ///
  257. /// If this method is not called, a default creation function wil be used
  258. /// that will construct a basic IR compile function that is compatible with
  259. /// the selected number of threads (SimpleCompiler for '0' compile threads,
  260. /// ConcurrentIRCompiler otherwise).
  261. SetterImpl &setCompileFunctionCreator(
  262. LLJITBuilderState::CompileFunctionCreator CreateCompileFunction) {
  263. impl().CreateCompileFunction = std::move(CreateCompileFunction);
  264. return impl();
  265. }
  266. /// Set up an PlatformSetupFunction.
  267. ///
  268. /// If this method is not called then setUpGenericLLVMIRPlatform
  269. /// will be used to configure the JIT's platform support.
  270. SetterImpl &
  271. setPlatformSetUp(LLJITBuilderState::PlatformSetupFunction SetUpPlatform) {
  272. impl().SetUpPlatform = std::move(SetUpPlatform);
  273. return impl();
  274. }
  275. /// Set the number of compile threads to use.
  276. ///
  277. /// If set to zero, compilation will be performed on the execution thread when
  278. /// JITing in-process. If set to any other number N, a thread pool of N
  279. /// threads will be created for compilation.
  280. ///
  281. /// If this method is not called, behavior will be as if it were called with
  282. /// a zero argument.
  283. SetterImpl &setNumCompileThreads(unsigned NumCompileThreads) {
  284. impl().NumCompileThreads = NumCompileThreads;
  285. return impl();
  286. }
  287. /// Set a TargetProcessControl object.
  288. ///
  289. /// If the platform uses ObjectLinkingLayer by default and no
  290. /// ObjectLinkingLayerCreator has been set then the TargetProcessControl
  291. /// object will be used to supply the memory manager for the
  292. /// ObjectLinkingLayer.
  293. SetterImpl &setTargetProcessControl(TargetProcessControl &TPC) {
  294. impl().TPC = &TPC;
  295. return impl();
  296. }
  297. /// Create an instance of the JIT.
  298. Expected<std::unique_ptr<JITType>> create() {
  299. if (auto Err = impl().prepareForConstruction())
  300. return std::move(Err);
  301. Error Err = Error::success();
  302. std::unique_ptr<JITType> J(new JITType(impl(), Err));
  303. if (Err)
  304. return std::move(Err);
  305. return std::move(J);
  306. }
  307. protected:
  308. SetterImpl &impl() { return static_cast<SetterImpl &>(*this); }
  309. };
  310. /// Constructs LLJIT instances.
  311. class LLJITBuilder
  312. : public LLJITBuilderState,
  313. public LLJITBuilderSetters<LLJIT, LLJITBuilder, LLJITBuilderState> {};
  314. class LLLazyJITBuilderState : public LLJITBuilderState {
  315. friend class LLLazyJIT;
  316. public:
  317. using IndirectStubsManagerBuilderFunction =
  318. std::function<std::unique_ptr<IndirectStubsManager>()>;
  319. Triple TT;
  320. JITTargetAddress LazyCompileFailureAddr = 0;
  321. std::unique_ptr<LazyCallThroughManager> LCTMgr;
  322. IndirectStubsManagerBuilderFunction ISMBuilder;
  323. Error prepareForConstruction();
  324. };
  325. template <typename JITType, typename SetterImpl, typename State>
  326. class LLLazyJITBuilderSetters
  327. : public LLJITBuilderSetters<JITType, SetterImpl, State> {
  328. public:
  329. /// Set the address in the target address to call if a lazy compile fails.
  330. ///
  331. /// If this method is not called then the value will default to 0.
  332. SetterImpl &setLazyCompileFailureAddr(JITTargetAddress Addr) {
  333. this->impl().LazyCompileFailureAddr = Addr;
  334. return this->impl();
  335. }
  336. /// Set the lazy-callthrough manager.
  337. ///
  338. /// If this method is not called then a default, in-process lazy callthrough
  339. /// manager for the host platform will be used.
  340. SetterImpl &
  341. setLazyCallthroughManager(std::unique_ptr<LazyCallThroughManager> LCTMgr) {
  342. this->impl().LCTMgr = std::move(LCTMgr);
  343. return this->impl();
  344. }
  345. /// Set the IndirectStubsManager builder function.
  346. ///
  347. /// If this method is not called then a default, in-process
  348. /// IndirectStubsManager builder for the host platform will be used.
  349. SetterImpl &setIndirectStubsManagerBuilder(
  350. LLLazyJITBuilderState::IndirectStubsManagerBuilderFunction ISMBuilder) {
  351. this->impl().ISMBuilder = std::move(ISMBuilder);
  352. return this->impl();
  353. }
  354. };
  355. /// Constructs LLLazyJIT instances.
  356. class LLLazyJITBuilder
  357. : public LLLazyJITBuilderState,
  358. public LLLazyJITBuilderSetters<LLLazyJIT, LLLazyJITBuilder,
  359. LLLazyJITBuilderState> {};
  360. /// Configure the LLJIT instance to scrape modules for llvm.global_ctors and
  361. /// llvm.global_dtors variables and (if present) build initialization and
  362. /// deinitialization functions. Platform specific initialization configurations
  363. /// should be preferred where available.
  364. void setUpGenericLLVMIRPlatform(LLJIT &J);
  365. /// Configure the LLJIT instance to use MachOPlatform support.
  366. ///
  367. /// Warning: MachOPlatform *requires* that LLJIT be configured to use
  368. /// ObjectLinkingLayer (default on platforms supported by JITLink). If
  369. /// MachOPlatform is used with RTDyldObjectLinkingLayer it will result in
  370. /// undefined behavior).
  371. ///
  372. /// MachOPlatform installs an ObjectLinkingLayer plugin to scrape initializers
  373. /// from the __mod_inits section. It also provides interposes for the dlfcn
  374. /// functions (dlopen, dlclose, dlsym, dlerror) that work for JITDylibs as
  375. /// well as regular libraries (JITDylibs will be preferenced, so make sure
  376. /// your JITDylib names do not shadow any real library paths).
  377. Error setUpMachOPlatform(LLJIT &J);
  378. } // End namespace orc
  379. } // End namespace llvm
  380. #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H
  381. #ifdef __GNUC__
  382. #pragma GCC diagnostic pop
  383. #endif