1 //===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Contains utilities for compiling IR to object files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
15 #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ExecutionEngine/ObjectCache.h"
19 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/Object/Binary.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Error.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/SmallVectorMemoryBuffer.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Target/TargetMachine.h"
39 /// Simple compile functor: Takes a single IR module and returns an ObjectFile.
40 /// This compiler supports a single compilation thread and LLVMContext only.
41 /// For multithreaded compilation, use MultiThreadedSimpleCompiler below.
42 class SimpleCompiler {
44 using CompileResult = std::unique_ptr<MemoryBuffer>;
46 /// Construct a simple compile functor with the given target.
47 SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
48 : TM(TM), ObjCache(ObjCache) {}
50 /// Set an ObjectCache to query before compiling.
51 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
53 /// Compile a Module to an ObjectFile.
54 CompileResult operator()(Module &M) {
55 CompileResult CachedObject = tryToLoadFromObjectCache(M);
59 SmallVector<char, 0> ObjBufferSV;
62 raw_svector_ostream ObjStream(ObjBufferSV);
64 legacy::PassManager PM;
66 if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
67 llvm_unreachable("Target does not support MC emission.");
72 llvm::make_unique<SmallVectorMemoryBuffer>(std::move(ObjBufferSV));
74 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
77 notifyObjectCompiled(M, *ObjBuffer);
78 return std::move(ObjBuffer);
81 // TODO: Actually report errors helpfully.
82 consumeError(Obj.takeError());
88 CompileResult tryToLoadFromObjectCache(const Module &M) {
90 return CompileResult();
92 return ObjCache->getObject(&M);
95 void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer) {
97 ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
101 ObjectCache *ObjCache = nullptr;
104 /// A thread-safe version of SimpleCompiler.
106 /// This class creates a new TargetMachine and SimpleCompiler instance for each
108 class MultiThreadedSimpleCompiler {
110 MultiThreadedSimpleCompiler(JITTargetMachineBuilder JTMB,
111 ObjectCache *ObjCache = nullptr)
112 : JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
114 void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
116 std::unique_ptr<MemoryBuffer> operator()(Module &M) {
117 auto TM = cantFail(JTMB.createTargetMachine());
118 SimpleCompiler C(*TM, ObjCache);
123 JITTargetMachineBuilder JTMB;
124 ObjectCache *ObjCache = nullptr;
127 } // end namespace orc
129 } // end namespace llvm
131 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H