LLJIT.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 ExecutorProcessControl;
  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<ExecutorProcessControl> EPC;
  209. std::unique_ptr<ExecutionSession> ES;
  210. Optional<JITTargetMachineBuilder> JTMB;
  211. Optional<DataLayout> DL;
  212. ObjectLinkingLayerCreator CreateObjectLinkingLayer;
  213. CompileFunctionCreator CreateCompileFunction;
  214. PlatformSetupFunction SetUpPlatform;
  215. unsigned NumCompileThreads = 0;
  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 a ExecutorProcessControl for this instance.
  223. /// This should not be called if ExecutionSession has already been set.
  224. SetterImpl &
  225. setExecutorProcessControl(std::unique_ptr<ExecutorProcessControl> EPC) {
  226. assert(
  227. !impl().ES &&
  228. "setExecutorProcessControl should not be called if an ExecutionSession "
  229. "has already been set");
  230. impl().EPC = std::move(EPC);
  231. return impl();
  232. }
  233. /// Set an ExecutionSession for this instance.
  234. SetterImpl &setExecutionSession(std::unique_ptr<ExecutionSession> ES) {
  235. impl().ES = std::move(ES);
  236. return impl();
  237. }
  238. /// Set the JITTargetMachineBuilder for this instance.
  239. ///
  240. /// If this method is not called, JITTargetMachineBuilder::detectHost will be
  241. /// used to construct a default target machine builder for the host platform.
  242. SetterImpl &setJITTargetMachineBuilder(JITTargetMachineBuilder JTMB) {
  243. impl().JTMB = std::move(JTMB);
  244. return impl();
  245. }
  246. /// Return a reference to the JITTargetMachineBuilder.
  247. ///
  248. Optional<JITTargetMachineBuilder> &getJITTargetMachineBuilder() {
  249. return impl().JTMB;
  250. }
  251. /// Set a DataLayout for this instance. If no data layout is specified then
  252. /// the target's default data layout will be used.
  253. SetterImpl &setDataLayout(Optional<DataLayout> DL) {
  254. impl().DL = std::move(DL);
  255. return impl();
  256. }
  257. /// Set an ObjectLinkingLayer creation function.
  258. ///
  259. /// If this method is not called, a default creation function will be used
  260. /// that will construct an RTDyldObjectLinkingLayer.
  261. SetterImpl &setObjectLinkingLayerCreator(
  262. LLJITBuilderState::ObjectLinkingLayerCreator CreateObjectLinkingLayer) {
  263. impl().CreateObjectLinkingLayer = std::move(CreateObjectLinkingLayer);
  264. return impl();
  265. }
  266. /// Set a CompileFunctionCreator.
  267. ///
  268. /// If this method is not called, a default creation function wil be used
  269. /// that will construct a basic IR compile function that is compatible with
  270. /// the selected number of threads (SimpleCompiler for '0' compile threads,
  271. /// ConcurrentIRCompiler otherwise).
  272. SetterImpl &setCompileFunctionCreator(
  273. LLJITBuilderState::CompileFunctionCreator CreateCompileFunction) {
  274. impl().CreateCompileFunction = std::move(CreateCompileFunction);
  275. return impl();
  276. }
  277. /// Set up an PlatformSetupFunction.
  278. ///
  279. /// If this method is not called then setUpGenericLLVMIRPlatform
  280. /// will be used to configure the JIT's platform support.
  281. SetterImpl &
  282. setPlatformSetUp(LLJITBuilderState::PlatformSetupFunction SetUpPlatform) {
  283. impl().SetUpPlatform = std::move(SetUpPlatform);
  284. return impl();
  285. }
  286. /// Set the number of compile threads to use.
  287. ///
  288. /// If set to zero, compilation will be performed on the execution thread when
  289. /// JITing in-process. If set to any other number N, a thread pool of N
  290. /// threads will be created for compilation.
  291. ///
  292. /// If this method is not called, behavior will be as if it were called with
  293. /// a zero argument.
  294. SetterImpl &setNumCompileThreads(unsigned NumCompileThreads) {
  295. impl().NumCompileThreads = NumCompileThreads;
  296. return impl();
  297. }
  298. /// Set an ExecutorProcessControl object.
  299. ///
  300. /// If the platform uses ObjectLinkingLayer by default and no
  301. /// ObjectLinkingLayerCreator has been set then the ExecutorProcessControl
  302. /// object will be used to supply the memory manager for the
  303. /// ObjectLinkingLayer.
  304. SetterImpl &setExecutorProcessControl(ExecutorProcessControl &EPC) {
  305. impl().EPC = &EPC;
  306. return impl();
  307. }
  308. /// Create an instance of the JIT.
  309. Expected<std::unique_ptr<JITType>> create() {
  310. if (auto Err = impl().prepareForConstruction())
  311. return std::move(Err);
  312. Error Err = Error::success();
  313. std::unique_ptr<JITType> J(new JITType(impl(), Err));
  314. if (Err)
  315. return std::move(Err);
  316. return std::move(J);
  317. }
  318. protected:
  319. SetterImpl &impl() { return static_cast<SetterImpl &>(*this); }
  320. };
  321. /// Constructs LLJIT instances.
  322. class LLJITBuilder
  323. : public LLJITBuilderState,
  324. public LLJITBuilderSetters<LLJIT, LLJITBuilder, LLJITBuilderState> {};
  325. class LLLazyJITBuilderState : public LLJITBuilderState {
  326. friend class LLLazyJIT;
  327. public:
  328. using IndirectStubsManagerBuilderFunction =
  329. std::function<std::unique_ptr<IndirectStubsManager>()>;
  330. Triple TT;
  331. JITTargetAddress LazyCompileFailureAddr = 0;
  332. std::unique_ptr<LazyCallThroughManager> LCTMgr;
  333. IndirectStubsManagerBuilderFunction ISMBuilder;
  334. Error prepareForConstruction();
  335. };
  336. template <typename JITType, typename SetterImpl, typename State>
  337. class LLLazyJITBuilderSetters
  338. : public LLJITBuilderSetters<JITType, SetterImpl, State> {
  339. public:
  340. /// Set the address in the target address to call if a lazy compile fails.
  341. ///
  342. /// If this method is not called then the value will default to 0.
  343. SetterImpl &setLazyCompileFailureAddr(JITTargetAddress Addr) {
  344. this->impl().LazyCompileFailureAddr = Addr;
  345. return this->impl();
  346. }
  347. /// Set the lazy-callthrough manager.
  348. ///
  349. /// If this method is not called then a default, in-process lazy callthrough
  350. /// manager for the host platform will be used.
  351. SetterImpl &
  352. setLazyCallthroughManager(std::unique_ptr<LazyCallThroughManager> LCTMgr) {
  353. this->impl().LCTMgr = std::move(LCTMgr);
  354. return this->impl();
  355. }
  356. /// Set the IndirectStubsManager builder function.
  357. ///
  358. /// If this method is not called then a default, in-process
  359. /// IndirectStubsManager builder for the host platform will be used.
  360. SetterImpl &setIndirectStubsManagerBuilder(
  361. LLLazyJITBuilderState::IndirectStubsManagerBuilderFunction ISMBuilder) {
  362. this->impl().ISMBuilder = std::move(ISMBuilder);
  363. return this->impl();
  364. }
  365. };
  366. /// Constructs LLLazyJIT instances.
  367. class LLLazyJITBuilder
  368. : public LLLazyJITBuilderState,
  369. public LLLazyJITBuilderSetters<LLLazyJIT, LLLazyJITBuilder,
  370. LLLazyJITBuilderState> {};
  371. /// Configure the LLJIT instance to scrape modules for llvm.global_ctors and
  372. /// llvm.global_dtors variables and (if present) build initialization and
  373. /// deinitialization functions. Platform specific initialization configurations
  374. /// should be preferred where available.
  375. void setUpGenericLLVMIRPlatform(LLJIT &J);
  376. /// Configure the LLJIT instance to disable platform support explicitly. This is
  377. /// useful in two cases: for platforms that don't have such requirements and for
  378. /// platforms, that we have no explicit support yet and that don't work well
  379. /// with the generic IR platform.
  380. Error setUpInactivePlatform(LLJIT &J);
  381. } // End namespace orc
  382. } // End namespace llvm
  383. #endif // LLVM_EXECUTIONENGINE_ORC_LLJIT_H
  384. #ifdef __GNUC__
  385. #pragma GCC diagnostic pop
  386. #endif