LLVM  7.0.0
LLJIT.cpp
Go to the documentation of this file.
1 //===--------- LLJIT.cpp - An ORC-based JIT for compiling LLVM IR ---------===//
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 
13 #include "llvm/IR/Mangler.h"
14 
15 namespace llvm {
16 namespace orc {
17 
18 Expected<std::unique_ptr<LLJIT>>
19 LLJIT::Create(std::unique_ptr<ExecutionSession> ES,
20  std::unique_ptr<TargetMachine> TM, DataLayout DL) {
21  return std::unique_ptr<LLJIT>(
22  new LLJIT(std::move(ES), std::move(TM), std::move(DL)));
23 }
24 
26  auto InternedName = ES->getSymbolStringPool().intern(Name);
27  SymbolMap Symbols({{InternedName, Sym}});
28  return Main.define(absoluteSymbols(std::move(Symbols)));
29 }
30 
31 Error LLJIT::addIRModule(VSO &V, std::unique_ptr<Module> M) {
32  assert(M && "Can not add null module");
33 
34  if (auto Err = applyDataLayout(*M))
35  return Err;
36 
37  auto K = ES->allocateVModule();
38  return CompileLayer.add(V, K, std::move(M));
39 }
40 
42  StringRef Name) {
43  return llvm::orc::lookup({&V}, ES->getSymbolStringPool().intern(Name));
44 }
45 
46 LLJIT::LLJIT(std::unique_ptr<ExecutionSession> ES,
47  std::unique_ptr<TargetMachine> TM, DataLayout DL)
48  : ES(std::move(ES)), Main(this->ES->createVSO("main")), TM(std::move(TM)),
49  DL(std::move(DL)),
50  ObjLinkingLayer(*this->ES,
51  [this](VModuleKey K) { return getMemoryManager(K); }),
54 
55 std::shared_ptr<RuntimeDyld::MemoryManager>
57  return llvm::make_unique<SectionMemoryManager>();
58 }
59 
60 std::string LLJIT::mangle(StringRef UnmangledName) {
61  std::string MangledName;
62  {
63  raw_string_ostream MangledNameStream(MangledName);
64  Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
65  }
66  return MangledName;
67 }
68 
70  if (M.getDataLayout().isDefault())
71  M.setDataLayout(DL);
72 
73  if (M.getDataLayout() != DL)
74  return make_error<StringError>(
75  "Added modules have incompatible data layouts",
77 
78  return Error::success();
79 }
80 
84 }
85 
87 LLLazyJIT::Create(std::unique_ptr<ExecutionSession> ES,
88  std::unique_ptr<TargetMachine> TM, DataLayout DL,
89  LLVMContext &Ctx) {
90  const Triple &TT = TM->getTargetTriple();
91 
92  auto CCMgr = createLocalCompileCallbackManager(TT, *ES, 0);
93  if (!CCMgr)
94  return make_error<StringError>(
95  std::string("No callback manager available for ") + TT.str(),
97 
98  auto ISMBuilder = createLocalIndirectStubsManagerBuilder(TT);
99  if (!ISMBuilder)
100  return make_error<StringError>(
101  std::string("No indirect stubs manager builder for ") + TT.str(),
103 
104  return std::unique_ptr<LLLazyJIT>(
105  new LLLazyJIT(std::move(ES), std::move(TM), std::move(DL), Ctx,
106  std::move(CCMgr), std::move(ISMBuilder)));
107 }
108 
109 Error LLLazyJIT::addLazyIRModule(VSO &V, std::unique_ptr<Module> M) {
110  assert(M && "Can not add null module");
111 
112  if (auto Err = applyDataLayout(*M))
113  return Err;
114 
116 
117  recordCtorDtors(*M);
118 
119  auto K = ES->allocateVModule();
120  return CODLayer.add(V, K, std::move(M));
121 }
122 
123 LLLazyJIT::LLLazyJIT(
124  std::unique_ptr<ExecutionSession> ES, std::unique_ptr<TargetMachine> TM,
125  DataLayout DL, LLVMContext &Ctx,
126  std::unique_ptr<JITCompileCallbackManager> CCMgr,
127  std::function<std::unique_ptr<IndirectStubsManager>()> ISMBuilder)
128  : LLJIT(std::move(ES), std::move(TM), std::move(DL)),
129  CCMgr(std::move(CCMgr)), TransformLayer(*this->ES, CompileLayer),
130  CODLayer(*this->ES, TransformLayer, *this->CCMgr, std::move(ISMBuilder),
131  [&]() -> LLVMContext & { return Ctx; }) {}
132 
133 } // End namespace orc.
134 } // End namespace llvm.
virtual Error add(VSO &V, VModuleKey K, std::unique_ptr< Module > M)
Adds a MaterializationUnit representing the given IR to the given VSO.
Definition: Layer.cpp:20
std::enable_if< std::is_convertible< typename std::decay< UniquePtrToMaterializationUnit >::type, std::unique_ptr< MaterializationUnit > >::value, Error >::type define(UniquePtrToMaterializationUnit &&MU)
Define all symbols provided by the materialization unit to be part of the given VSO.
Definition: Core.h:621
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
std::function< std::unique_ptr< IndirectStubsManager >)> createLocalIndirectStubsManagerBuilder(const Triple &T)
Create a local indriect stubs manager builder.
Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address)
Convenience method for defining an absolute symbol.
Definition: LLJIT.cpp:25
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:544
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:355
std::map< SymbolStringPtr, JITEvaluatedSymbol > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:51
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
Definition: BitVector.h:921
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
Error addLazyIRModule(VSO &V, std::unique_ptr< Module > M)
Add a module to be lazily compiled to VSO V.
Definition: LLJIT.cpp:109
CtorDtorRunner2 DtorRunner
Definition: LLJIT.h:101
IRCompileLayer2 CompileLayer
Definition: LLJIT.h:99
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:42
RTDyldObjectLinkingLayer2 ObjLinkingLayer
Definition: LLJIT.h:98
iterator_range< CtorDtorIterator > getDestructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
std::shared_ptr< RuntimeDyld::MemoryManager > getMemoryManager(VModuleKey K)
Definition: LLJIT.cpp:56
const std::string & str() const
Definition: Triple.h:354
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
VSO & Main
Definition: LLJIT.h:93
std::string mangle(StringRef UnmangledName)
Definition: LLJIT.cpp:60
LLJIT(std::unique_ptr< ExecutionSession > ES, std::unique_ptr< TargetMachine > TM, DataLayout DL)
Definition: LLJIT.cpp:46
Expected< JITEvaluatedSymbol > lookupLinkerMangled(VSO &V, StringRef Name)
Look up a symbol in VSO V by the symbol&#39;s linker-mangled name (to look up symbols based on their IR n...
Definition: LLJIT.cpp:41
CtorDtorRunner2 CtorRunner
Definition: LLJIT.h:101
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:92
static ErrorSuccess success()
Create a success value.
Definition: Error.h:322
void recordCtorDtors(Module &M)
Definition: LLJIT.cpp:81
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols)
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
Definition: Core.h:277
Expected< SymbolMap > lookup(const VSOList &VSOs, SymbolNameSet Names)
Look up the given names in the given VSOs.
Definition: Core.cpp:1655
static Expected< std::unique_ptr< LLLazyJIT > > Create(std::unique_ptr< ExecutionSession > ES, std::unique_ptr< TargetMachine > TM, DataLayout DL, LLVMContext &Ctx)
Create an LLLazyJIT instance.
Definition: LLJIT.cpp:87
std::unique_ptr< TargetMachine > TM
Definition: LLJIT.h:95
bool isDefault() const
Test if the DataLayout was constructed from an empty string.
Definition: DataLayout.h:234
An extended version of LLJIT that supports lazy function-at-a-time compilation of LLVM IR...
Definition: LLJIT.h:106
void add(iterator_range< CtorDtorIterator > CtorDtors)
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:157
void makeAllSymbolsExternallyAccessible(Module &M)
Raise linkage types and rename as necessary to ensure that all symbols are accessible for other modul...
static Expected< std::unique_ptr< LLJIT > > Create(std::unique_ptr< ExecutionSession > ES, std::unique_ptr< TargetMachine > TM, DataLayout DL)
Create an LLJIT instance.
Definition: LLJIT.cpp:19
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
std::unique_ptr< JITCompileCallbackManager > createLocalCompileCallbackManager(const Triple &T, ExecutionSession &ES, JITTargetAddress ErrorHandlerAddress)
Create a local compile callback manager.
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:69
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:477
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable&#39;s name.
Definition: Mangler.cpp:112
print Print MemDeps of function
DataLayout DL
Definition: LLJIT.h:96
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Error addIRModule(VSO &V, std::unique_ptr< Module > M)
Adds an IR module to the given VSO.
Definition: LLJIT.cpp:31
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:73