blob: 15a656678135d0cdd05e6064a7dd8c2a13ee8f5f [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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/ADT/SmallVector.h"
17#include "llvm/ExecutionEngine/ObjectCache.h"
Andrew Scull0372a572018-11-16 15:47:06 +000018#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019#include "llvm/IR/LegacyPassManager.h"
20#include "llvm/Object/Binary.h"
21#include "llvm/Object/ObjectFile.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/MemoryBuffer.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010025#include "llvm/Support/SmallVectorMemoryBuffer.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026#include "llvm/Support/raw_ostream.h"
27#include "llvm/Target/TargetMachine.h"
28#include <algorithm>
29#include <memory>
30
31namespace llvm {
32
33class MCContext;
34class Module;
35
36namespace orc {
37
Andrew Scullcdfcccc2018-10-05 20:58:37 +010038/// Simple compile functor: Takes a single IR module and returns an ObjectFile.
39/// This compiler supports a single compilation thread and LLVMContext only.
Andrew Walbran16937d02019-10-22 13:54:20 +010040/// For multithreaded compilation, use ConcurrentIRCompiler below.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041class SimpleCompiler {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042public:
43 using CompileResult = std::unique_ptr<MemoryBuffer>;
44
Andrew Scullcdfcccc2018-10-05 20:58:37 +010045 /// Construct a simple compile functor with the given target.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046 SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
47 : TM(TM), ObjCache(ObjCache) {}
48
Andrew Scullcdfcccc2018-10-05 20:58:37 +010049 /// Set an ObjectCache to query before compiling.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
51
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052 /// Compile a Module to an ObjectFile.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053 CompileResult operator()(Module &M) {
54 CompileResult CachedObject = tryToLoadFromObjectCache(M);
55 if (CachedObject)
56 return CachedObject;
57
58 SmallVector<char, 0> ObjBufferSV;
59
60 {
61 raw_svector_ostream ObjStream(ObjBufferSV);
62
63 legacy::PassManager PM;
64 MCContext *Ctx;
65 if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
66 llvm_unreachable("Target does not support MC emission.");
67 PM.run(M);
68 }
69
70 auto ObjBuffer =
71 llvm::make_unique<SmallVectorMemoryBuffer>(std::move(ObjBufferSV));
72 auto Obj =
73 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
74
75 if (Obj) {
76 notifyObjectCompiled(M, *ObjBuffer);
77 return std::move(ObjBuffer);
78 }
79
80 // TODO: Actually report errors helpfully.
81 consumeError(Obj.takeError());
82 return nullptr;
83 }
84
85private:
86
87 CompileResult tryToLoadFromObjectCache(const Module &M) {
88 if (!ObjCache)
89 return CompileResult();
90
91 return ObjCache->getObject(&M);
92 }
93
94 void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer) {
95 if (ObjCache)
96 ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
97 }
98
99 TargetMachine &TM;
100 ObjectCache *ObjCache = nullptr;
101};
102
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100103/// A thread-safe version of SimpleCompiler.
104///
105/// This class creates a new TargetMachine and SimpleCompiler instance for each
106/// compile.
Andrew Walbran16937d02019-10-22 13:54:20 +0100107class ConcurrentIRCompiler {
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100108public:
Andrew Walbran16937d02019-10-22 13:54:20 +0100109 ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
110 ObjectCache *ObjCache = nullptr)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100111 : JTMB(std::move(JTMB)), ObjCache(ObjCache) {}
112
113 void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
114
115 std::unique_ptr<MemoryBuffer> operator()(Module &M) {
116 auto TM = cantFail(JTMB.createTargetMachine());
117 SimpleCompiler C(*TM, ObjCache);
118 return C(M);
119 }
120
121private:
122 JITTargetMachineBuilder JTMB;
123 ObjectCache *ObjCache = nullptr;
124};
125
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100126} // end namespace orc
127
128} // end namespace llvm
129
130#endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H