forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinlining-decider.cpp
More file actions
482 lines (404 loc) · 15.1 KB
/
Copy pathinlining-decider.cpp
File metadata and controls
482 lines (404 loc) · 15.1 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2014 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/jit/inlining-decider.h"
#include "hphp/runtime/base/arch.h"
#include "hphp/runtime/base/runtime-option.h"
#include "hphp/runtime/ext/ext_generator.h"
#include "hphp/runtime/vm/bytecode.h"
#include "hphp/runtime/vm/func.h"
#include "hphp/runtime/vm/hhbc.h"
#include "hphp/runtime/vm/srckey.h"
#include "hphp/runtime/vm/jit/normalized-instruction.h"
#include "hphp/runtime/vm/jit/region-selection.h"
#include "hphp/runtime/vm/jit/irgen.h"
#include "hphp/util/trace.h"
#include <vector>
namespace HPHP { namespace jit {
///////////////////////////////////////////////////////////////////////////////
TRACE_SET_MOD(inlining);
namespace {
///////////////////////////////////////////////////////////////////////////////
bool traceRefusal(const Func* caller, const Func* callee, const char* why) {
if (Trace::enabled) {
UNUSED auto calleeName = callee ? callee->fullName()->data()
: "(unknown)";
assert(caller);
FTRACE(1, "InliningDecider: refusing {}() <- {}{}\t<reason: {}>\n",
caller->fullName()->data(), calleeName, callee ? "()" : "", why);
}
return false;
}
std::atomic<bool> hasCalledDisableInliningIntrinsic;
hphp_hash_set<const StringData*,
string_data_hash,
string_data_isame> forbiddenInlinees;
SimpleMutex forbiddenInlineesLock;
bool inliningIsForbiddenFor(const Func* callee) {
if (!hasCalledDisableInliningIntrinsic.load()) return false;
SimpleLock locker(forbiddenInlineesLock);
return forbiddenInlinees.find(callee->fullName()) != forbiddenInlinees.end();
}
///////////////////////////////////////////////////////////////////////////////
// canInlineAt() helpers.
/*
* Check if the funcd of `inst' has any characteristics which prevent inlining,
* without peeking into its bytecode or regions.
*/
bool isCalleeInlinable(SrcKey callSK, const Func* callee) {
auto refuse = [&] (const char* why) {
return traceRefusal(callSK.func(), callee, why);
};
if (!callee) {
return refuse("callee not known");
}
if (inliningIsForbiddenFor(callee)) {
return refuse("inlining disabled for callee");
}
if (callee == callSK.func()) {
return refuse("call is recursive");
}
if (callee->hasVariadicCaptureParam()) {
return refuse("callee has variadic capture");
}
if (callee->numIterators() != 0) {
return refuse("callee has iterators");
}
if (callee->isMagic() || Func::isSpecial(callee->name())) {
return refuse("special or magic callee");
}
if (callee->isResumable()) {
return refuse("callee is resumable");
}
if (callee->maxStackCells() >= kStackCheckLeafPadding) {
return refuse("function stack depth too deep");
}
if (callee->isMethod() && callee->cls() == c_Generator::classof()) {
return refuse("generator member function");
}
return true;
}
/*
* Check that we don't have any missing or extra arguments.
*/
bool checkNumArgs(SrcKey callSK, const Func* callee) {
assert(callee);
auto refuse = [&] (const char* why) {
return traceRefusal(callSK.func(), callee, why);
};
auto pc = reinterpret_cast<const Op*>(callSK.pc());
auto const numArgs = getImm(pc, 0).u_IVA;
auto const numParams = callee->numParams();
if (numArgs > numParams) {
return refuse("callee called with too many arguments");
}
// It's okay if we passed fewer arguments than there are parameters as long
// as the gap can be filled in by DV funclets.
for (auto i = numArgs; i < numParams; ++i) {
auto const& param = callee->params()[i];
if (!param.hasDefaultValue()) {
return refuse("callee called with too few arguments");
}
}
return true;
}
/*
* Check that the FPI region is suitable for inlining.
*
* We refuse to inline if the corresponding FPush is not found in the same
* region as the FCall, or if other calls are made between the two.
*/
bool checkFPIRegion(SrcKey callSK, const Func* callee,
const RegionDesc& region) {
assert(callee);
auto refuse = [&] (const char* why) {
return traceRefusal(callSK.func(), callee, why);
};
// Check that the FPush instruction is in the same region, and that our FCall
// is reachable from it.
//
// TODO(#4603302) Fix this once SrcKeys can appear multiple times in a region.
auto fpi = callSK.func()->findFPI(callSK.offset());
const SrcKey pushSK { callSK.func(),
fpi->m_fpushOff,
callSK.resumed() };
int pushBlock = -1;
auto const& blocks = region.blocks();
for (unsigned i = 0; i < blocks.size(); ++i) {
if (blocks[i]->contains(pushSK)) {
pushBlock = i;
break;
}
}
if (pushBlock == -1) {
return refuse("FPush* is not in the current region");
}
// Check that we have an acceptable FPush.
switch (pushSK.op()) {
case OpFPushClsMethodD:
if (callee->mayHaveThis()) {
return refuse("callee may have $this pointer");
}
// fallthrough
case OpFPushFuncD:
case OpFPushObjMethodD:
case OpFPushCtorD:
case OpFPushCtor:
break;
default:
return refuse(folly::format("unsupported push op {}",
opcodeToName(pushSK.op())).str().c_str());
}
// Calls invalidate all live SSATmps, so don't allow any in the FPI region.
for (unsigned i = pushBlock; i < blocks.size(); ++i) {
auto const& block = *blocks[i];
auto iterSK = (i == pushBlock ? pushSK.advanced()
: block.start());
while (iterSK <= block.last()) {
// We're all set once we've hit the to-be-inlined FCall.
if (iterSK == callSK) return true;
auto op = iterSK.op();
if (isFCallStar(op) || op == Op::FCallBuiltin) {
return refuse("FPI region contains another call");
}
iterSK.advance();
}
}
not_reached();
}
///////////////////////////////////////////////////////////////////////////////
}
void InliningDecider::forbidInliningOf(const Func* callee) {
hasCalledDisableInliningIntrinsic.store(true);
SimpleLock locker(forbiddenInlineesLock);
forbiddenInlinees.insert(callee->fullName());
}
bool InliningDecider::canInlineAt(SrcKey callSK, const Func* callee,
const RegionDesc& region) const {
if (!RuntimeOption::RepoAuthoritative ||
!RuntimeOption::EvalHHIREnableGenTimeInlining) {
return false;
}
// If inlining was disabled... don't inline.
if (m_disabled) return false;
// TODO(#3331014): We have this hack until more ARM codegen is working.
if (arch() == Arch::ARM) return false;
// We can only inline at normal FCalls.
if (callSK.op() != Op::FCall &&
callSK.op() != Op::FCallD) {
return false;
}
// Don't inline from resumed functions. The inlining mechanism doesn't have
// support for these---it has no way to redefine stack pointers relative to
// the frame pointer, because in a resumed function the frame pointer points
// into the heap instead of into the eval stack.
if (callSK.resumed()) return false;
// TODO(#4238160): Inlining into pseudomain callsites is still buggy.
if (callSK.func()->isPseudoMain()) return false;
if (!isCalleeInlinable(callSK, callee) ||
!checkNumArgs(callSK, callee) ||
!checkFPIRegion(callSK, callee, region)) {
return false;
}
return true;
}
namespace {
///////////////////////////////////////////////////////////////////////////////
// shouldInline() helpers.
/*
* Check if a builtin is inlinable.
*/
bool isInlinableCPPBuiltin(const Func* f) {
assert(f->isCPPBuiltin());
// The callee needs to be callable with FCallBuiltin, because NativeImpl
// requires a frame.
if (f->attrs() & AttrNoFCallBuiltin ||
f->numParams() > Native::maxFCallBuiltinArgs() ||
!f->nativeFuncPtr()) {
return false;
}
// ARM currently can't handle floating point returns.
if (f->returnType() == KindOfDouble &&
!Native::allowFCallBuiltinDoubles()) {
return false;
}
if (auto const info = f->methInfo()) {
if (info->attribute & (ClassInfo::NoFCallBuiltin |
ClassInfo::VariableArguments |
ClassInfo::RefVariableArguments)) {
return false;
}
// Note: there is no need for a similar-to-the-above check for HNI
// builtins---they'll just have a nullptr nativeFuncPtr (if they were
// declared as needing an ActRec).
}
// For now, don't inline when we'd need to adjust ObjectData pointers.
if (f->cls() && f->cls()->preClass()->builtinODOffset() != 0) {
return false;
}
// TODO: Static methods need to be passed their class, which we don't
// support yet. (t5360661)
if (f->isMethod() && (f->attrs() & AttrStatic)) {
return false;
}
return true;
}
/*
* Conservative whitelist for HHBC opcodes we know are safe to inline, even if
* the entire callee body required a AttrMayUseVV.
*
* This affects cases where we're able to eliminate control flow while inlining
* due to the parameter types, and the AttrMayUseVV flag was due to something
* happening in a block we won't inline.
*/
bool isInliningVVSafe(Op op) {
switch (op) {
case Op::Null:
case Op::PopC:
case Op::CGetL:
case Op::SetL:
case Op::IsTypeL:
case Op::JmpNS:
case Op::JmpNZ:
case Op::JmpZ:
case Op::AssertRATL:
case Op::AssertRATStk:
case Op::VerifyParamType:
case Op::VerifyRetTypeC:
case Op::RetC:
return true;
default:
break;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
}
bool InliningDecider::shouldInline(const Func* callee,
const RegionDesc& region) {
auto sk = region.empty() ? SrcKey() : region.start();
assert(callee);
assert(sk.func() == callee);
int cost = 0;
// Tracing return lambdas.
auto refuse = [&] (const char* why) {
return traceRefusal(m_topFunc, callee, why);
};
auto accept = [&, this] (const char* kind) {
FTRACE(1, "InliningDecider: inlining {}() <- {}()\t<reason: {}>\n",
m_topFunc->fullName()->data(), callee->fullName()->data(), kind);
// Update our context.
m_costStack.push_back(cost);
m_cost += cost;
m_callDepth += 1;
m_stackDepth += callee->maxStackCells();
return true;
};
// Check inlining depths.
if (m_callDepth + 1 >= RuntimeOption::EvalHHIRInliningMaxDepth) {
return refuse("inlining call depth limit exceeded");
}
if (m_stackDepth + callee->maxStackCells() >= kStackCheckLeafPadding) {
return refuse("inlining stack depth limit exceeded");
}
// Even if the func contains NativeImpl we may have broken the trace before
// we hit it.
auto containsNativeImpl = [&] {
for (auto block : region.blocks()) {
if (!block->empty() && block->last().op() == OpNativeImpl) return true;
}
return false;
};
// Try to inline CPP builtin functions.
// The NativeImpl opcode may appear later in the function because of Asserts
// generated in hhbbc
if (callee->isCPPBuiltin() && containsNativeImpl()) {
if (isInlinableCPPBuiltin(callee)) {
return accept("inlinable CPP builtin");
}
return refuse("non-inlinable CPP builtin");
}
// If the function may use a VarEnv (which is stored in the ActRec) or may be
// variadic, we restrict inlined callees to certain whitelisted instructions
// which we know won't actually require these features.
const bool needsCheckVVSafe = callee->attrs() & AttrMayUseVV;
// We measure the cost of inlining each callstack and stop when it exceeds a
// certain threshold. (Note that we do not measure the total cost of all the
// inlined calls for a given caller---just the cost of each nested stack.)
const int maxCost = RuntimeOption::EvalHHIRInliningMaxCost - m_cost;
// We only inline callee regions that have exactly one return.
//
// NOTE: Currently, the tracelet selector uses the first Ret in the child's
// region to determine when to stop inlining. However, the safety of this
// behavior should not be considered guaranteed by InliningDecider; the
// "right" way to decide when inlining ends is to inline all of `region'.
int numRets = 0;
// Iterate through the region, checking its suitability for inlining.
for (auto const& block : region.blocks()) {
sk = block->start();
for (auto i = 0, n = block->length(); i < n; ++i, sk.advance()) {
auto op = sk.op();
// We don't allow inlined functions in the region. The client is
// expected to disable inlining for the region it gives us to peek.
if (sk.func() != callee) {
return refuse("got region with inlined calls");
}
// Restrict to VV-safe opcodes if necessary.
if (needsCheckVVSafe && !isInliningVVSafe(op)) {
return refuse(folly::format("{} may use dynamic environment",
opcodeToName(op)).str().c_str());
}
// Count the returns.
if (isRet(op) || op == Op::NativeImpl) {
if (++numRets > 1) {
return refuse("region has too many returns");
}
continue;
}
// We can't inline FCallArray. XXX: Why?
if (op == Op::FCallArray) {
return refuse("can't inline FCallArray");
}
// Assert opcodes don't contribute to the inlining cost.
if (op == Op::AssertRATL || op == Op::AssertRATStk) continue;
cost += 1;
// Add the size of immediate vectors to the cost.
auto const pc = reinterpret_cast<const Op*>(sk.pc());
if (hasMVector(op)) {
cost += getMVector(pc).size();
} else if (hasImmVector(op)) {
cost += getImmVector(pc).size();
}
// Refuse if the cost exceeds our thresholds.
if (cost > maxCost) {
return refuse("too expensive");
}
}
}
if (numRets != 1) {
return refuse("region has no returns");
}
return accept("small region with single return");
}
///////////////////////////////////////////////////////////////////////////////
void InliningDecider::registerEndInlining(const Func* callee) {
auto cost = m_costStack.back();
m_costStack.pop_back();
m_cost -= cost;
m_callDepth -= 1;
m_stackDepth -= callee->maxStackCells();
}
///////////////////////////////////////////////////////////////////////////////
}}