blob: a7f9416475880dbfb8c16d13d2d9de3f721ca7c8 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- IRCompileLayer.h -- Eagerly compile IR for JIT -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Contains the definition for a basic, eagerly compiling layer of the JIT.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
15#define LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
16
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ExecutionEngine/JITSymbol.h"
19#include "llvm/ExecutionEngine/Orc/Core.h"
20#include "llvm/Support/Error.h"
21#include <memory>
22#include <string>
23
24namespace llvm {
25
26class Module;
27
28namespace orc {
29
30/// @brief Eager IR compiling layer.
31///
32/// This layer immediately compiles each IR module added via addModule to an
33/// object file and adds this module file to the layer below, which must
34/// implement the object layer concept.
35template <typename BaseLayerT, typename CompileFtor>
36class IRCompileLayer {
37public:
38 /// @brief Callback type for notifications when modules are compiled.
39 using NotifyCompiledCallback =
40 std::function<void(VModuleKey K, std::unique_ptr<Module>)>;
41
42 /// @brief Construct an IRCompileLayer with the given BaseLayer, which must
43 /// implement the ObjectLayer concept.
44 IRCompileLayer(
45 BaseLayerT &BaseLayer, CompileFtor Compile,
46 NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback())
47 : BaseLayer(BaseLayer), Compile(std::move(Compile)),
48 NotifyCompiled(std::move(NotifyCompiled)) {}
49
50 /// @brief Get a reference to the compiler functor.
51 CompileFtor& getCompiler() { return Compile; }
52
53 /// @brief (Re)set the NotifyCompiled callback.
54 void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled) {
55 this->NotifyCompiled = std::move(NotifyCompiled);
56 }
57
58 /// @brief Compile the module, and add the resulting object to the base layer
59 /// along with the given memory manager and symbol resolver.
60 Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
61 if (auto Err = BaseLayer.addObject(std::move(K), Compile(*M)))
62 return Err;
63 if (NotifyCompiled)
64 NotifyCompiled(std::move(K), std::move(M));
65 return Error::success();
66 }
67
68 /// @brief Remove the module associated with the VModuleKey K.
69 Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); }
70
71 /// @brief Search for the given named symbol.
72 /// @param Name The name of the symbol to search for.
73 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
74 /// @return A handle for the given named symbol, if it exists.
75 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
76 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
77 }
78
79 /// @brief Get the address of the given symbol in compiled module represented
80 /// by the handle H. This call is forwarded to the base layer's
81 /// implementation.
82 /// @param K The VModuleKey for the module to search in.
83 /// @param Name The name of the symbol to search for.
84 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
85 /// @return A handle for the given named symbol, if it is found in the
86 /// given module.
87 JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
88 bool ExportedSymbolsOnly) {
89 return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
90 }
91
92 /// @brief Immediately emit and finalize the module represented by the given
93 /// handle.
94 /// @param K The VModuleKey for the module to emit/finalize.
95 Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
96
97private:
98 BaseLayerT &BaseLayer;
99 CompileFtor Compile;
100 NotifyCompiledCallback NotifyCompiled;
101};
102
103} // end namespace orc
104
105} // end namespace llvm
106
107#endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H