blob: 213a59124c8512c620db83c2cd0b5c77658ad616 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- CompileUtils.h - Utilities for compiling IR in the 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 utilities for compiling IR to object files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
15#define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
16
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ExecutionEngine/ObjectCache.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010019#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010020#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"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010026#include "llvm/Support/SmallVectorMemoryBuffer.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027#include "llvm/Support/raw_ostream.h"
28#include "llvm/Target/TargetMachine.h"
29#include <algorithm>
30#include <memory>
31
32namespace llvm {
33
34class MCContext;
35class Module;
36
37namespace orc {
38
Andrew Scullcdfcccc2018-10-05 20:58:37 +010039/// 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.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042class SimpleCompiler {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010043public:
44 using CompileResult = std::unique_ptr<MemoryBuffer>;
45
Andrew Scullcdfcccc2018-10-05 20:58:37 +010046 /// Construct a simple compile functor with the given target.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010047 SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
48 : TM(TM), ObjCache(ObjCache) {}
49
Andrew Scullcdfcccc2018-10-05 20:58:37 +010050 /// Set an ObjectCache to query before compiling.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
52
Andrew Scullcdfcccc2018-10-05 20:58:37 +010053 /// Compile a Module to an ObjectFile.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054 CompileResult operator()(Module &M) {
55 CompileResult CachedObject = tryToLoadFromObjectCache(M);
56 if (CachedObject)
57 return CachedObject;
58
59 SmallVector<char, 0> ObjBufferSV;
60
61 {
62 raw_svector_ostream ObjStream(ObjBufferSV);
63
64 legacy::PassManager PM;
65 MCContext *Ctx;
66 if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
67 llvm_unreachable("Target does not support MC emission.");
68 PM.run(M);
69 }
70
71 auto ObjBuffer =
72 llvm::make_unique<SmallVectorMemoryBuffer>(std::move(ObjBufferSV));
73 auto Obj =
74 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
75
76 if (Obj) {
77 notifyObjectCompiled(M, *ObjBuffer);
78 return std::move(ObjBuffer);
79 }
80
81 // TODO: Actually report errors helpfully.
82 consumeError(Obj.takeError());
83 return nullptr;
84 }
85
86private:
87
88 CompileResult tryToLoadFromObjectCache(const Module &M) {
89 if (!ObjCache)
90 return CompileResult();
91
92 return ObjCache->getObject(&M);
93 }
94
95 void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer) {
96 if (ObjCache)
97 ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
98 }
99
100 TargetMachine &TM;
101 ObjectCache *ObjCache = nullptr;
102};
103
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100104/// A thread-safe version of SimpleCompiler.
105///
106/// This class creates a new TargetMachine and SimpleCompiler instance for each
107/// compile.
108class MultiThreadedSimpleCompiler {
109public:
110 MultiThreadedSimpleCompiler(JITTargetMachineBuilder JTMB,
111 ObjectCache *ObjCache = nullptr)
112 : JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
113
114 void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
115
116 std::unique_ptr<MemoryBuffer> operator()(Module &M) {
117 auto TM = cantFail(JTMB.createTargetMachine());
118 SimpleCompiler C(*TM, ObjCache);
119 return C(M);
120 }
121
122private:
123 JITTargetMachineBuilder JTMB;
124 ObjectCache *ObjCache = nullptr;
125};
126
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100127} // end namespace orc
128
129} // end namespace llvm
130
131#endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H