1//===- 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/Support/Error.h" 20#include <memory> 21#include <string> 22 23namespace llvm { 24 25class Module; 26 27namespace orc { 28 29/// @brief Eager IR compiling layer. 30/// 31/// This layer immediately compiles each IR module added via addModule to an 32/// object file and adds this module file to the layer below, which must 33/// implement the object layer concept. 34template <typename BaseLayerT, typename CompileFtor> 35class IRCompileLayer { 36public: 37 38 /// @brief Handle to a compiled module. 39 using ModuleHandleT = typename BaseLayerT::ObjHandleT; 40 41 /// @brief Construct an IRCompileLayer with the given BaseLayer, which must 42 /// implement the ObjectLayer concept. 43 IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile) 44 : BaseLayer(BaseLayer), Compile(std::move(Compile)) {} 45 46 /// @brief Get a reference to the compiler functor. 47 CompileFtor& getCompiler() { return Compile; } 48 49 /// @brief Compile the module, and add the resulting object to the base layer 50 /// along with the given memory manager and symbol resolver. 51 /// 52 /// @return A handle for the added module. 53 Expected<ModuleHandleT> 54 addModule(std::shared_ptr<Module> M, 55 std::shared_ptr<JITSymbolResolver> Resolver) { 56 using CompileResult = decltype(Compile(*M)); 57 auto Obj = std::make_shared<CompileResult>(Compile(*M)); 58 return BaseLayer.addObject(std::move(Obj), std::move(Resolver)); 59 } 60 61 /// @brief Remove the module associated with the handle H. 62 Error removeModule(ModuleHandleT H) { 63 return BaseLayer.removeObject(H); 64 } 65 66 /// @brief Search for the given named symbol. 67 /// @param Name The name of the symbol to search for. 68 /// @param ExportedSymbolsOnly If true, search only for exported symbols. 69 /// @return A handle for the given named symbol, if it exists. 70 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) { 71 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly); 72 } 73 74 /// @brief Get the address of the given symbol in compiled module represented 75 /// by the handle H. This call is forwarded to the base layer's 76 /// implementation. 77 /// @param H The handle for the module to search in. 78 /// @param Name The name of the symbol to search for. 79 /// @param ExportedSymbolsOnly If true, search only for exported symbols. 80 /// @return A handle for the given named symbol, if it is found in the 81 /// given module. 82 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, 83 bool ExportedSymbolsOnly) { 84 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); 85 } 86 87 /// @brief Immediately emit and finalize the module represented by the given 88 /// handle. 89 /// @param H Handle for module to emit/finalize. 90 Error emitAndFinalize(ModuleHandleT H) { 91 return BaseLayer.emitAndFinalize(H); 92 } 93 94private: 95 BaseLayerT &BaseLayer; 96 CompileFtor Compile; 97}; 98 99} // end namespace orc 100 101} // end namespace llvm 102 103#endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H 104