LLVM  6.0.0svn
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"
28 #include <algorithm>
29 #include <memory>
30 
31 namespace llvm {
32 
33 class MCContext;
34 class Module;
35 
36 namespace orc {
37 
38 /// @brief Simple compile functor: Takes a single IR module and returns an
39 /// ObjectFile.
41 public:
42 
44 
45  /// @brief Construct a simple compile functor with the given target.
46  SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
47  : TM(TM), ObjCache(ObjCache) {}
48 
49  /// @brief Set an ObjectCache to query before compiling.
50  void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
51 
52  /// @brief Compile a Module to an ObjectFile.
54  CompileResult CachedObject = tryToLoadFromObjectCache(M);
55  if (CachedObject.getBinary())
56  return CachedObject;
57 
58  SmallVector<char, 0> ObjBufferSV;
59  raw_svector_ostream ObjStream(ObjBufferSV);
60 
62  MCContext *Ctx;
63  if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
64  llvm_unreachable("Target does not support MC emission.");
65  PM.run(M);
66  std::unique_ptr<MemoryBuffer> ObjBuffer(
67  new ObjectMemoryBuffer(std::move(ObjBufferSV)));
69  object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
70  if (Obj) {
71  notifyObjectCompiled(M, *ObjBuffer);
72  return CompileResult(std::move(*Obj), std::move(ObjBuffer));
73  }
74  // TODO: Actually report errors helpfully.
75  consumeError(Obj.takeError());
76  return CompileResult(nullptr, nullptr);
77  }
78 
79 private:
80 
81  CompileResult tryToLoadFromObjectCache(const Module &M) {
82  if (!ObjCache)
83  return CompileResult();
84 
85  std::unique_ptr<MemoryBuffer> ObjBuffer = ObjCache->getObject(&M);
86  if (!ObjBuffer)
87  return CompileResult();
88 
90  object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
91  if (!Obj) {
92  // TODO: Actually report errors helpfully.
93  consumeError(Obj.takeError());
94  return CompileResult();
95  }
96 
97  return CompileResult(std::move(*Obj), std::move(ObjBuffer));
98  }
99 
100  void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer) {
101  if (ObjCache)
102  ObjCache->notifyObjectCompiled(&M, ObjBuffer.getMemBufferRef());
103  }
104 
105  TargetMachine &TM;
106  ObjectCache *ObjCache = nullptr;
107 };
108 
109 } // end namespace orc
110 
111 } // end namespace llvm
112 
113 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:152
Compute iterated dominance frontiers using a linear time algorithm.
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:63
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...
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:489
Error takeError()
Take ownership of the stored error.
Definition: Error.h:537
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:40
Context object for machine code objects.
Definition: MCContext.h:59
object::OwningBinary< object::ObjectFile > CompileResult
Definition: CompileUtils.h:43
PassManager manages ModulePassManagers.
CompileResult operator()(Module &M)
Compile a Module to an ObjectFile.
Definition: CompileUtils.h:53
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:962
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.
MemoryBufferRef getMemBufferRef() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:40
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
Definition: CompileUtils.h:50
SmallVector-backed MemoryBuffer instance.
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:23
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:57
SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache=nullptr)
Construct a simple compile functor with the given target.
Definition: CompileUtils.h:46