LLVM  9.0.0svn
CompileUtils.h
Go to the documentation of this file.
1 //===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===//
2 //
3 // 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
6 //
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"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Object/ObjectFile.h"
22 #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 /// Simple compile functor: Takes a single IR module and returns an ObjectFile.
39 /// This compiler supports a single compilation thread and LLVMContext only.
40 /// For multithreaded compilation, use ConcurrentIRCompiler below.
42 public:
43  using CompileResult = std::unique_ptr<MemoryBuffer>;
44 
45  /// Construct a simple compile functor with the given target.
46  SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
47  : TM(TM), ObjCache(ObjCache) {}
48 
49  /// Set an ObjectCache to query before compiling.
50  void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
51 
52  /// Compile a Module to an ObjectFile.
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 
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 
85 private:
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 
103 /// A thread-safe version of SimpleCompiler.
104 ///
105 /// This class creates a new TargetMachine and SimpleCompiler instance for each
106 /// compile.
108 public:
110  ObjectCache *ObjCache = nullptr)
111  : 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 
121 private:
123  ObjectCache *ObjCache = nullptr;
124 };
125 
126 } // end namespace orc
127 
128 } // end namespace llvm
129 
130 #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:703
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:23
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:113
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
Definition: BitVector.h:937
A thread-safe version of SimpleCompiler.
Definition: CompileUtils.h:107
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:41
Context object for machine code objects.
Definition: MCContext.h:62
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:981
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:109
MemoryBufferRef getMemBufferRef() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
Definition: CompileUtils.h:50
std::unique_ptr< MemoryBuffer > operator()(Module &M)
Definition: CompileUtils.h:115
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:22
std::unique_ptr< MemoryBuffer > CompileResult
Definition: CompileUtils.h:43
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache=nullptr)
Construct a simple compile functor with the given target.
Definition: CompileUtils.h:46
A utility class for building TargetMachines for JITs.