LLVM  10.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 
17 #include <memory>
18 
19 namespace llvm {
20 
21 class MCContext;
22 class MemoryBuffer;
23 class Module;
24 class ObjectCache;
25 class TargetMachine;
26 
27 namespace orc {
28 
29 class JITTargetMachineBuilder;
30 
31 /// Simple compile functor: Takes a single IR module and returns an ObjectFile.
32 /// This compiler supports a single compilation thread and LLVMContext only.
33 /// For multithreaded compilation, use ConcurrentIRCompiler below.
35 public:
36  using CompileResult = std::unique_ptr<MemoryBuffer>;
37 
38  /// Construct a simple compile functor with the given target.
39  SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
40  : TM(TM), ObjCache(ObjCache) {}
41 
42  /// Set an ObjectCache to query before compiling.
43  void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
44 
45  /// Compile a Module to an ObjectFile.
47 
48 private:
49  CompileResult tryToLoadFromObjectCache(const Module &M);
50  void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
51 
52  TargetMachine &TM;
53  ObjectCache *ObjCache = nullptr;
54 };
55 
56 /// A SimpleCompiler that owns its TargetMachine.
57 ///
58 /// This convenient for clients who don't want to own their TargetMachines,
59 /// e.g. LLJIT.
61 public:
62  TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM,
63  ObjectCache *ObjCache = nullptr)
64  : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {}
65 
66 private:
67  // FIXME: shared because std::functions (and consequently
68  // IRCompileLayer::CompileFunction) are not moveable.
69  std::shared_ptr<llvm::TargetMachine> TM;
70 };
71 
72 /// A thread-safe version of SimpleCompiler.
73 ///
74 /// This class creates a new TargetMachine and SimpleCompiler instance for each
75 /// compile.
77 public:
79  ObjectCache *ObjCache = nullptr);
80 
81  void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
82 
83  std::unique_ptr<MemoryBuffer> operator()(Module &M);
84 
85 private:
87  ObjectCache *ObjCache = nullptr;
88 };
89 
90 } // end namespace orc
91 
92 } // end namespace llvm
93 
94 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
A SimpleCompiler that owns its TargetMachine.
Definition: CompileUtils.h:60
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
void setObjectCache(ObjectCache *ObjCache)
Definition: CompileUtils.h:81
Definition: BitVector.h:937
A thread-safe version of SimpleCompiler.
Definition: CompileUtils.h:76
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:34
TMOwningSimpleCompiler(std::unique_ptr< TargetMachine > TM, ObjectCache *ObjCache=nullptr)
Definition: CompileUtils.h:62
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:43
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:36
CompileResult operator()(Module &M)
Compile a Module to an ObjectFile.
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:39
A utility class for building TargetMachines for JITs.