1 //===- CompileUtils.h - Utilities for compiling IR in the 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 utilities for compiling IR to object files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H 14 #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H 15 16 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" 17 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" 18 #include "llvm/ExecutionEngine/Orc/Layer.h" 19 #include <memory> 20 21 namespace llvm { 22 23 class MCContext; 24 class MemoryBuffer; 25 class Module; 26 class ObjectCache; 27 class TargetMachine; 28 29 namespace orc { 30 31 class JITTargetMachineBuilder; 32 33 IRMaterializationUnit::ManglingOptions 34 irManglingOptionsFromTargetOptions(const TargetOptions &Opts); 35 36 /// Simple compile functor: Takes a single IR module and returns an ObjectFile. 37 /// This compiler supports a single compilation thread and LLVMContext only. 38 /// For multithreaded compilation, use ConcurrentIRCompiler below. 39 class SimpleCompiler : public IRCompileLayer::IRCompiler { 40 public: 41 using CompileResult = std::unique_ptr<MemoryBuffer>; 42 43 /// Construct a simple compile functor with the given target. 44 SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr) 45 : IRCompiler(irManglingOptionsFromTargetOptions(TM.Options)), TM(TM), 46 ObjCache(ObjCache) {} 47 48 /// Set an ObjectCache to query before compiling. 49 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; } 50 51 /// Compile a Module to an ObjectFile. 52 Expected<CompileResult> operator()(Module &M) override; 53 54 private: 55 IRMaterializationUnit::ManglingOptions 56 manglingOptionsForTargetMachine(const TargetMachine &TM); 57 58 CompileResult tryToLoadFromObjectCache(const Module &M); 59 void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer); 60 61 TargetMachine &TM; 62 ObjectCache *ObjCache = nullptr; 63 }; 64 65 /// A SimpleCompiler that owns its TargetMachine. 66 /// 67 /// This convenient for clients who don't want to own their TargetMachines, 68 /// e.g. LLJIT. 69 class TMOwningSimpleCompiler : public SimpleCompiler { 70 public: 71 TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM, 72 ObjectCache *ObjCache = nullptr) 73 : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {} 74 75 private: 76 // FIXME: shared because std::functions (and consequently 77 // IRCompileLayer::CompileFunction) are not moveable. 78 std::shared_ptr<llvm::TargetMachine> TM; 79 }; 80 81 /// A thread-safe version of SimpleCompiler. 82 /// 83 /// This class creates a new TargetMachine and SimpleCompiler instance for each 84 /// compile. 85 class ConcurrentIRCompiler : public IRCompileLayer::IRCompiler { 86 public: 87 ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, 88 ObjectCache *ObjCache = nullptr); 89 90 void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; } 91 92 Expected<std::unique_ptr<MemoryBuffer>> operator()(Module &M) override; 93 94 private: 95 JITTargetMachineBuilder JTMB; 96 ObjectCache *ObjCache = nullptr; 97 }; 98 99 } // end namespace orc 100 101 } // end namespace llvm 102 103 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H 104