1 //===- IRCompileLayer.h -- Eagerly compile IR for JIT -----------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Contains the definition for a basic, eagerly compiling layer of the JIT. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H 14 #define LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H 15 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ExecutionEngine/JITSymbol.h" 18 #include "llvm/ExecutionEngine/Orc/Layer.h" 19 #include "llvm/Support/Compiler.h" 20 #include "llvm/Support/Error.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include <functional> 23 #include <memory> 24 #include <mutex> 25 26 namespace llvm { 27 28 class Module; 29 30 namespace orc { 31 32 class LLVM_ABI IRCompileLayer : public IRLayer { 33 public: 34 class LLVM_ABI IRCompiler { 35 public: IRCompiler(IRSymbolMapper::ManglingOptions MO)36 IRCompiler(IRSymbolMapper::ManglingOptions MO) : MO(std::move(MO)) {} 37 virtual ~IRCompiler(); getManglingOptions()38 const IRSymbolMapper::ManglingOptions &getManglingOptions() const { 39 return MO; 40 } 41 virtual Expected<std::unique_ptr<MemoryBuffer>> operator()(Module &M) = 0; 42 43 protected: manglingOptions()44 IRSymbolMapper::ManglingOptions &manglingOptions() { return MO; } 45 46 private: 47 IRSymbolMapper::ManglingOptions MO; 48 }; 49 50 using NotifyCompiledFunction = std::function<void( 51 MaterializationResponsibility &R, ThreadSafeModule TSM)>; 52 53 IRCompileLayer(ExecutionSession &ES, ObjectLayer &BaseLayer, 54 std::unique_ptr<IRCompiler> Compile); 55 getCompiler()56 IRCompiler &getCompiler() { return *Compile; } 57 58 void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled); 59 60 void emit(std::unique_ptr<MaterializationResponsibility> R, 61 ThreadSafeModule TSM) override; 62 63 private: 64 mutable std::mutex IRLayerMutex; 65 ObjectLayer &BaseLayer; 66 std::unique_ptr<IRCompiler> Compile; 67 const IRSymbolMapper::ManglingOptions *ManglingOpts; 68 NotifyCompiledFunction NotifyCompiled = NotifyCompiledFunction(); 69 }; 70 71 } // end namespace orc 72 } // end namespace llvm 73 74 #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H 75