LLVM  12.0.0git
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 
19 #include <memory>
20 
21 namespace llvm {
22 
23 class MCContext;
24 class MemoryBuffer;
25 class Module;
26 class ObjectCache;
27 class TargetMachine;
28 
29 namespace orc {
30 
31 class JITTargetMachineBuilder;
32 
33 IRSymbolMapper::ManglingOptions
34 irManglingOptionsFromTargetOptions(const TargetOptions &Opts);
35 
36 /// Simple compile functor: Takes a single IR module and returns an ObjectFile.
37 /// This compiler supports a single compilation thread and LLVMContext only.
38 /// For multithreaded compilation, use ConcurrentIRCompiler below.
40 public:
41  using CompileResult = std::unique_ptr<MemoryBuffer>;
42 
43  /// Construct a simple compile functor with the given target.
44  SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
45  : IRCompiler(irManglingOptionsFromTargetOptions(TM.Options)), TM(TM),
46  ObjCache(ObjCache) {}
47 
48  /// Set an ObjectCache to query before compiling.
49  void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
50 
51  /// Compile a Module to an ObjectFile.
53 
54 private:
56  manglingOptionsForTargetMachine(const TargetMachine &TM);
57 
58  CompileResult tryToLoadFromObjectCache(const Module &M);
59  void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
60 
61  TargetMachine &TM;
62  ObjectCache *ObjCache = nullptr;
63 };
64 
65 /// A SimpleCompiler that owns its TargetMachine.
66 ///
67 /// This convenient for clients who don't want to own their TargetMachines,
68 /// e.g. LLJIT.
70 public:
71  TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM,
72  ObjectCache *ObjCache = nullptr)
73  : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {}
74 
75 private:
76  // FIXME: shared because std::functions (and consequently
77  // IRCompileLayer::CompileFunction) are not moveable.
78  std::shared_ptr<llvm::TargetMachine> TM;
79 };
80 
81 /// A thread-safe version of SimpleCompiler.
82 ///
83 /// This class creates a new TargetMachine and SimpleCompiler instance for each
84 /// compile.
86 public:
88  ObjectCache *ObjCache = nullptr);
89 
90  void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
91 
93 
94 private:
96  ObjectCache *ObjCache = nullptr;
97 };
98 
99 } // end namespace orc
100 
101 } // end namespace llvm
102 
103 #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
A SimpleCompiler that owns its TargetMachine.
Definition: CompileUtils.h:69
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:67
Machine Debugify Module
void setObjectCache(ObjectCache *ObjCache)
Definition: CompileUtils.h:90
Definition: BitVector.h:959
A thread-safe version of SimpleCompiler.
Definition: CompileUtils.h:85
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:39
TMOwningSimpleCompiler(std::unique_ptr< TargetMachine > TM, ObjectCache *ObjCache=nullptr)
Definition: CompileUtils.h:71
Expected< CompileResult > operator()(Module &M) override
Compile a Module to an ObjectFile.
IRSymbolMapper::ManglingOptions irManglingOptionsFromTargetOptions(const TargetOptions &Opts)
IRCompiler(IRSymbolMapper::ManglingOptions MO)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:52
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
Definition: CompileUtils.h:49
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:41
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:44
A utility class for building TargetMachines for JITs.