LLVM  8.0.1
CompileUtils.h
Go to the documentation of this file.
1 //===- 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"
21 #include "llvm/Object/Binary.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Error.h"
29 #include <algorithm>
30 #include <memory>
31 
32 namespace llvm {
33 
34 class MCContext;
35 class Module;
36 
37 namespace orc {
38 
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 ConcurrentIRCompiler below.
43 public:
44  using CompileResult = std::unique_ptr<MemoryBuffer>;
45 
46  /// Construct a simple compile functor with the given target.
47  SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
48  : TM(TM), ObjCache(ObjCache) {}
49 
50  /// Set an ObjectCache to query before compiling.
51  void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
52 
53  /// Compile a Module to an ObjectFile.
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 
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 
86 private:
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 
104 /// A thread-safe version of SimpleCompiler.
105 ///
106 /// This class creates a new TargetMachine and SimpleCompiler instance for each
107 /// compile.
109 public:
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 
122 private:
124  ObjectCache *ObjCache = nullptr;
125 };
126 
127 } // end namespace orc
128 
129 } // end namespace llvm
130 
131 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:704
uint64_t CallInst * C
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:161
This class represents lattice values for constants.
Definition: AllocatorList.h:24
virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&, raw_pwrite_stream &, bool=true)
Add passes to the specified pass manager to get machine code emitted with the MCJIT.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
virtual std::unique_ptr< MemoryBuffer > getObject(const Module *M)=0
Returns a pointer to a newly allocated MemoryBuffer that contains the object which corresponds with M...
void setObjectCache(ObjectCache *ObjCache)
Definition: CompileUtils.h:114
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:510
Definition: BitVector.h:938
A thread-safe version of SimpleCompiler.
Definition: CompileUtils.h:108
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:42
Context object for machine code objects.
Definition: MCContext.h:63
PassManager manages ModulePassManagers.
CompileResult operator()(Module &M)
Compile a Module to an ObjectFile.
Definition: CompileUtils.h:54
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:982
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
virtual void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)=0
notifyObjectCompiled - Provides a pointer to compiled code for Module M.
ConcurrentIRCompiler(JITTargetMachineBuilder JTMB, ObjectCache *ObjCache=nullptr)
Definition: CompileUtils.h:110
MemoryBufferRef getMemBufferRef() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:42
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
Definition: CompileUtils.h:51
std::unique_ptr< MemoryBuffer > operator()(Module &M)
Definition: CompileUtils.h:116
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:23
std::unique_ptr< MemoryBuffer > CompileResult
Definition: CompileUtils.h:44
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache=nullptr)
Construct a simple compile functor with the given target.
Definition: CompileUtils.h:47
A utility class for building TargetMachines for JITs.