forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg-meta.cpp
316 lines (262 loc) · 8.57 KB
/
cg-meta.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/jit/cg-meta.h"
#include "hphp/runtime/vm/debug/debug.h"
#include "hphp/runtime/vm/jit/code-cache.h"
#include "hphp/runtime/vm/jit/fixup.h"
#include "hphp/runtime/vm/jit/func-order.h"
#include "hphp/runtime/vm/jit/prof-data.h"
#include "hphp/runtime/vm/jit/tc.h"
#include "hphp/runtime/vm/tread-hash-map.h"
#include "hphp/util/atomic-vector.h"
namespace HPHP::jit {
TRACE_SET_MOD(mcg);
namespace {
std::atomic<IFrameID> s_nextFrameKey;
// Map from integral literals to their location in the TC data section.
using LiteralMap = TreadHashMap<uint64_t,const uint64_t*,std::hash<uint64_t>>;
LiteralMap s_literals{128};
// Landingpads for TC catch traces; used by the unwinder.
using CatchTraceMap = TreadHashMap<uint32_t, uint32_t, std::hash<uint32_t>>;
CatchTraceMap s_catchTraceMap{128};
using AbortReasonMap = TreadHashMap<uint32_t, Reason, std::hash<uint32_t>>;
AbortReasonMap s_trapReasonMap{128};
using InlineStackMap = TreadHashMap<uint32_t, IStack, std::hash<uint32_t>>;
InlineStackMap s_inlineStacks{1024};
using InlineFrameVec = AtomicVector<IFrame>;
InlineFrameVec s_inlineFrames{4096,IFrame{}};
constexpr uint32_t kInvalidCatchTrace = 0x0;
constexpr uint32_t kInvalidFrameID = -1;
IFrameID insertFrames(const std::vector<IFrame>& frames) {
auto const start = s_nextFrameKey.fetch_add(frames.size());
s_inlineFrames.ensureSize(start + frames.size());
for (IFrameID i = 0; i < frames.size(); ++i) {
auto const& f = frames[i];
auto const parent = f.parent != kRootIFrameID
? f.parent + start : kRootIFrameID;
auto newFrame = IFrame{f.func, f.callOff, f.sbToRootSbOff, parent};
s_inlineFrames.exchange(start + i, newFrame);
}
return start;
}
bool isFakeAddr(CTCA addr) {
return (int64_t)addr < 0;
}
Offset fromFakeAddr(CTCA addr) {
return (int32_t)reinterpret_cast<int64_t>(addr);
}
Offset stackAddrToOffset(CTCA addr) {
return isFakeAddr(addr) ? fromFakeAddr(addr) : tc::addrToOffset(addr);
}
void insertStacks(
IFrameID start, const std::vector<std::pair<TCA,IStack>>& stacks
) {
for (auto& stk : stacks) {
assertx(stk.second.frame != stk.second.pubFrame);
auto off = stackAddrToOffset(stk.first);
auto val = stk.second;
val.frame += start;
if (val.pubFrame != kRootIFrameID) val.pubFrame += start;
if (auto pos = s_inlineStacks.find(off)) {
*pos = val;
} else {
s_inlineStacks.insert(off, val);
}
}
}
void processInlineFrames(const CGMeta& cm) {
auto const start = insertFrames(cm.inlineFrames);
insertStacks(start, cm.inlineStacks);
}
}
Optional<IStack> inlineStackAt(CTCA addr) {
if (!addr) return std::nullopt;
auto off = stackAddrToOffset(addr);
if (auto pos = s_inlineStacks.find(off)) {
if (pos->frame != kInvalidFrameID) return *pos;
}
return std::nullopt;
}
IFrame getInlineFrame(IFrameID id) {
return s_inlineFrames[id];
}
void eraseInlineStack(CTCA addr) {
if (auto stk = s_inlineStacks.find(tc::addrToOffset(addr))) {
stk->frame = kInvalidFrameID;
}
}
void eraseInlineStacksInRange(CTCA start, CTCA end) {
auto const start_offset = tc::addrToOffset(start);
auto const end_offset = tc::addrToOffset(end);
s_inlineStacks.filter_keys([&](const uint32_t offset) {
return start_offset <= offset && offset < end_offset;
});
}
const uint64_t* addrForLiteral(uint64_t val) {
if (auto it = s_literals.find(val)) {
assertx(**it == val);
return *it;
}
return nullptr;
}
size_t numCatchTraces() {
return s_catchTraceMap.size();
}
void eraseCatchTrace(CTCA addr) {
if (auto ct = s_catchTraceMap.find(tc::addrToOffset(addr))) {
*ct = kInvalidCatchTrace;
}
}
Optional<TCA> getCatchTrace(CTCA ip) {
auto const found = s_catchTraceMap.find(tc::addrToOffset(ip));
if (found && *found != kInvalidCatchTrace) return tc::offsetToAddr(*found);
return std::nullopt;
}
Reason* getTrapReason(CTCA addr) {
return s_trapReasonMap.find(tc::addrToOffset(addr));
}
void poolLiteral(CodeBlock& cb, CGMeta& meta, uint64_t val, uint8_t width,
bool smashable) {
meta.literalsToPool.emplace_back(
CGMeta::PoolLiteralMeta {
val,
cb.frontier(),
smashable,
width
}
);
}
void addVeneer(CGMeta& meta, TCA source, TCA target) {
FTRACE(5, "addVeneer: source = {}, target = {}\n", source, target);
meta.veneers.emplace_back(CGMeta::VeneerData{source, target});
}
////////////////////////////////////////////////////////////////////////////////
void CGMeta::setJmpTransID(TCA jmp, TransID transID, TransKind kind) {
if (kind != TransKind::Profile) return;
assertx(transID != kInvalidTransID);
FTRACE(5, "setJmpTransID: adding {} => {}\n", jmp, transID);
jmpTransIDs.emplace_back(jmp, transID);
}
void CGMeta::setCallFuncId(TCA callRetAddr, FuncId funcId, TransKind kind) {
// This is only used when profiling optimized code via jumpstart.
if (kind != TransKind::Optimize || !isJitSerializing()) return;
callFuncIds.emplace_back(callRetAddr, funcId);
}
void CGMeta::process(
GrowableVector<IncomingBranch>* inProgressTailBranches
) {
process_only(inProgressTailBranches);
clear();
}
void CGMeta::process_literals() {
assertx(literalsToPool.empty());
for (auto& pair : literalAddrs) {
if (s_literals.find(pair.first)) {
// TreadHashMap doesn't allow re-inserting existing keys
continue;
}
s_literals.insert(pair.first, pair.second);
}
literalAddrs.clear();
}
void CGMeta::process_only(
GrowableVector<IncomingBranch>* inProgressTailBranches
) {
tc::assertOwnsMetadataLock();
for (auto const& pair : fixups) {
assertx(tc::isValidCodeAddress(pair.first));
FixupMap::recordFixup(pair.first, pair.second);
}
fixups.clear();
processInlineFrames(*this);
inlineFrames.clear();
inlineStacks.clear();
for (auto const& ct : catches) {
auto const key = tc::addrToOffset(ct.first);
auto const val = tc::addrToOffset(ct.second);
if (auto pos = s_catchTraceMap.find(key)) {
*pos = val;
} else {
s_catchTraceMap.insert(key, val);
}
}
catches.clear();
if (auto profData = jit::profData()) {
for (auto const& elm : jmpTransIDs) {
profData->setJmpTransID(elm.first, elm.second);
}
}
jmpTransIDs.clear();
for (auto const& elm : callFuncIds) {
FuncOrder::setCallFuncId(elm.first, elm.second);
}
for (auto const& pair : trapReasons) {
auto addr = tc::addrToOffset(pair.first);
if (auto r = s_trapReasonMap.find(addr)) {
*r = pair.second;
} else {
s_trapReasonMap.insert(addr, pair.second);
}
}
trapReasons.clear();
process_literals();
if (inProgressTailBranches) {
inProgressTailJumps.swap(*inProgressTailBranches);
}
assertx(inProgressTailJumps.empty());
}
void CGMeta::clear() {
watchpoints.clear();
fixups.clear();
catches.clear();
inlineFrames.clear();
inlineStacks.clear();
jmpTransIDs.clear();
callFuncIds.clear();
literalsToPool.clear();
literalAddrs.clear();
veneers.clear();
alignments.clear();
addressImmediates.clear();
fallthru.reset();
codePointers.clear();
inProgressTailJumps.clear();
bcMap.clear();
smashableBinds.clear();
smashableCallData.clear();
}
bool CGMeta::empty() const {
return
watchpoints.empty() &&
fixups.empty() &&
catches.empty() &&
inlineFrames.empty() &&
inlineStacks.empty() &&
jmpTransIDs.empty() &&
callFuncIds.empty() &&
literalsToPool.empty() &&
literalAddrs.empty() &&
veneers.empty() &&
alignments.empty() &&
addressImmediates.empty() &&
!fallthru.has_value() &&
codePointers.empty() &&
inProgressTailJumps.empty() &&
bcMap.empty() &&
smashableBinds.empty() &&
smashableCallData.empty();
}
}