1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
| //===-- Speculation.h - Speculative Compilation --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Contains the definition to support speculative compilation when laziness is
// enabled.
//===----------------------------------------------------------------------===//
#ifndef LLVM_EXECUTIONENGINE_ORC_SPECULATION_H
#define LLVM_EXECUTIONENGINE_ORC_SPECULATION_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/Debug.h"
#include <mutex>
#include <type_traits>
#include <utility>
#include <vector>
namespace llvm {
namespace orc {
class Speculator;
// Track the Impls (JITDylib,Symbols) of Symbols while lazy call through
// trampolines are created. Operations are guarded by locks tp ensure that Imap
// stays in consistent state after read/write
class ImplSymbolMap {
friend class Speculator;
public:
using AliaseeDetails = std::pair<SymbolStringPtr, JITDylib *>;
using Alias = SymbolStringPtr;
using ImapTy = DenseMap<Alias, AliaseeDetails>;
void trackImpls(SymbolAliasMap ImplMaps, JITDylib *SrcJD);
private:
// FIX ME: find a right way to distinguish the pre-compile Symbols, and update
// the callsite
Optional<AliaseeDetails> getImplFor(const SymbolStringPtr &StubSymbol) {
std::lock_guard<std::mutex> Lockit(ConcurrentAccess);
auto Position = Maps.find(StubSymbol);
if (Position != Maps.end())
return Position->getSecond();
else
return None;
}
std::mutex ConcurrentAccess;
ImapTy Maps;
};
// Defines Speculator Concept,
class Speculator {
public:
using TargetFAddr = JITTargetAddress;
using FunctionCandidatesMap = DenseMap<SymbolStringPtr, SymbolNameSet>;
using StubAddrLikelies = DenseMap<TargetFAddr, SymbolNameSet>;
private:
void registerSymbolsWithAddr(TargetFAddr ImplAddr,
SymbolNameSet likelySymbols) {
std::lock_guard<std::mutex> Lockit(ConcurrentAccess);
GlobalSpecMap.insert({ImplAddr, std::move(likelySymbols)});
}
void launchCompile(JITTargetAddress FAddr) {
SymbolNameSet CandidateSet;
// Copy CandidateSet is necessary, to avoid unsynchronized access to
// the datastructure.
{
std::lock_guard<std::mutex> Lockit(ConcurrentAccess);
auto It = GlobalSpecMap.find(FAddr);
if (It == GlobalSpecMap.end())
return;
CandidateSet = It->getSecond();
}
SymbolDependenceMap SpeculativeLookUpImpls;
for (auto &Callee : CandidateSet) {
auto ImplSymbol = AliaseeImplTable.getImplFor(Callee);
// try to distinguish already compiled & library symbols
if (!ImplSymbol.hasValue())
continue;
const auto &ImplSymbolName = ImplSymbol.getPointer()->first;
JITDylib *ImplJD = ImplSymbol.getPointer()->second;
auto &SymbolsInJD = SpeculativeLookUpImpls[ImplJD];
SymbolsInJD.insert(ImplSymbolName);
}
DEBUG_WITH_TYPE("orc", for (auto &I
: SpeculativeLookUpImpls) {
llvm::dbgs() << "\n In " << I.first->getName() << " JITDylib ";
for (auto &N : I.second)
llvm::dbgs() << "\n Likely Symbol : " << N;
});
// for a given symbol, there may be no symbol qualified for speculatively
// compile try to fix this before jumping to this code if possible.
for (auto &LookupPair : SpeculativeLookUpImpls)
ES.lookup(JITDylibSearchList({{LookupPair.first, true}}),
LookupPair.second, SymbolState::Ready,
[this](Expected<SymbolMap> Result) {
if (auto Err = Result.takeError())
ES.reportError(std::move(Err));
},
NoDependenciesToRegister);
}
public:
Speculator(ImplSymbolMap &Impl, ExecutionSession &ref)
: AliaseeImplTable(Impl), ES(ref), GlobalSpecMap(0) {}
Speculator(const Speculator &) = delete;
Speculator(Speculator &&) = delete;
Speculator &operator=(const Speculator &) = delete;
Speculator &operator=(Speculator &&) = delete;
/// Define symbols for this Speculator object (__orc_speculator) and the
/// speculation runtime entry point symbol (__orc_speculate_for) in the
/// given JITDylib.
Error addSpeculationRuntime(JITDylib &JD, MangleAndInterner &Mangle);
// Speculatively compile likely functions for the given Stub Address.
// destination of __orc_speculate_for jump
void speculateFor(TargetFAddr StubAddr) { launchCompile(StubAddr); }
// FIXME : Register with Stub Address, after JITLink Fix.
void registerSymbols(FunctionCandidatesMap Candidates, JITDylib *JD) {
for (auto &SymPair : Candidates) {
auto Target = SymPair.first;
auto Likely = SymPair.second;
auto OnReadyFixUp = [Likely, Target,
this](Expected<SymbolMap> ReadySymbol) {
if (ReadySymbol) {
auto RAddr = (*ReadySymbol)[Target].getAddress();
registerSymbolsWithAddr(RAddr, std::move(Likely));
} else
this->getES().reportError(ReadySymbol.takeError());
};
// Include non-exported symbols also.
ES.lookup(JITDylibSearchList({{JD, true}}), SymbolNameSet({Target}),
SymbolState::Ready, OnReadyFixUp, NoDependenciesToRegister);
}
}
ExecutionSession &getES() { return ES; }
private:
static void speculateForEntryPoint(Speculator *Ptr, uint64_t StubId);
std::mutex ConcurrentAccess;
ImplSymbolMap &AliaseeImplTable;
ExecutionSession &ES;
StubAddrLikelies GlobalSpecMap;
};
class IRSpeculationLayer : public IRLayer {
public:
using IRlikiesStrRef = Optional<DenseMap<StringRef, DenseSet<StringRef>>>;
using ResultEval = std::function<IRlikiesStrRef(Function &)>;
using TargetAndLikelies = DenseMap<SymbolStringPtr, SymbolNameSet>;
IRSpeculationLayer(ExecutionSession &ES, IRCompileLayer &BaseLayer,
Speculator &Spec, MangleAndInterner &Mangle,
ResultEval Interpreter)
: IRLayer(ES), NextLayer(BaseLayer), S(Spec), Mangle(Mangle),
QueryAnalysis(Interpreter) {}
void emit(MaterializationResponsibility R, ThreadSafeModule TSM);
private:
TargetAndLikelies
internToJITSymbols(DenseMap<StringRef, DenseSet<StringRef>> IRNames) {
assert(!IRNames.empty() && "No IRNames received to Intern?");
TargetAndLikelies InternedNames;
DenseSet<SymbolStringPtr> TargetJITNames;
for (auto &NamePair : IRNames) {
for (auto &TargetNames : NamePair.second)
TargetJITNames.insert(Mangle(TargetNames));
InternedNames[Mangle(NamePair.first)] = std::move(TargetJITNames);
}
return InternedNames;
}
IRCompileLayer &NextLayer;
Speculator &S;
MangleAndInterner &Mangle;
ResultEval QueryAnalysis;
};
} // namespace orc
} // namespace llvm
#endif // LLVM_EXECUTIONENGINE_ORC_SPECULATION_H
|