forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregion-selection.h
571 lines (490 loc) · 18.5 KB
/
region-selection.h
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
*/
#pragma once
#include <memory>
#include <utility>
#include <vector>
#include <string>
#include <boost/container/flat_map.hpp>
#include <folly/Format.h>
#include <folly/Hash.h>
#include "hphp/runtime/vm/jit/containers.h"
#include "hphp/runtime/vm/jit/location.h"
#include "hphp/runtime/vm/jit/stack-offsets.h"
#include "hphp/runtime/vm/jit/type.h"
#include "hphp/runtime/vm/jit/types.h"
#include "hphp/runtime/vm/func.h"
#include "hphp/runtime/vm/resumable.h"
#include "hphp/runtime/vm/srckey.h"
namespace HPHP::jit {
struct ProfData;
struct TransCFG;
//////////////////////////////////////////////////////////////////////
enum class PGORegionMode {
Hottrace, // Select a long region, using profile counters to guide the trace
Hotblock, // Select a single block
HotCFG, // Select arbitrary CFG using profile counters to prune cold paths
WholeCFG, // Select the entire CFG that has been profiled
};
//////////////////////////////////////////////////////////////////////
/*
* RegionDesc is a description of a code region. This includes the
* list of blocks in the region, and also the list of control-flow
* arcs within the region.
*
* It consists of a list of unique SrcKey ranges, with type
* annotations that may come from profiling or other sources.
*
* The first block is the entry point, and the remaining blocks must
* be sorted in a reverse post order.
*/
struct RegionDesc {
struct Block;
struct Arc;
struct TypedLocation;
struct GuardedLocation;
using BlockPtr = std::shared_ptr<Block>;
using BlockId = TransID;
// BlockId Encoding:
// - Non-negative numbers are blocks that correspond
// to the start of a TransProfile translation, and therefore can
// be used to index into ProfData.
// - Negative numbers are used for other blocks, which correspond
// to blocks created by inlining and which don't correspond to
// the beginning of a profiling translation.
using BlockIdSet = boost::container::flat_set<BlockId>;
using BlockIdVec = std::vector<BlockId>;
using BlockVec = std::vector<BlockPtr>;
bool empty() const;
SrcKey start() const;
BlockPtr entry() const;
const BlockVec& blocks() const;
BlockPtr block(BlockId id) const;
const BlockIdSet& succs(BlockId id) const;
const BlockIdSet& preds(BlockId id) const;
bool isExit(BlockId id) const;
/*
* Returns the set of blocks that got merged into block `id' by
* guard relaxation.
*/
const BlockIdSet& merged(BlockId id) const;
const BlockIdSet* incoming() const;
void incoming(BlockIdSet&& ids);
/*
* Modify this RegionDesc so that its list of blocks is sorted in a reverse
* post order.
*/
void sortBlocks();
/*
* Returns the last BC offset in the region that corresponds to the
* function where the region starts. This will normally be the offset
* of the last instruction in the last block, except if the function
* ends with an inlined call. In this case, the offset of the
* corresponding FCall* in the function that starts the region is
* returned.
*
* Note that the notion of "last BC offset" only makes sense for
* regions that are linear traces.
*/
SrcKey lastSrcKey() const;
/*
* Returns the profile count associated with block `bid'. In case other
* blocks have been merged into this block, the returned count includes the
* counts of these other blocks as well.
*/
int64_t blockProfCount(BlockId bid) const;
double blockProfCountScale(BlockId bid) const;
Block* addBlock(SrcKey sk, int length, SBInvOffset spOffset);
void addBlock(BlockPtr newBlock);
bool hasBlock(BlockId id) const;
void replaceBlock(BlockId bid, BlockPtr newBlock);
void deleteBlock(BlockId bid);
BlockVec::iterator deleteBlock(RegionDesc::BlockVec::iterator it);
void renumberBlock(BlockId oldId, BlockId newId);
void addArc(BlockId src, BlockId dst);
void removeArc(BlockId src, BlockId dst);
void addMerged(BlockId fromId, BlockId intoId);
Optional<BlockId> prevRetrans(BlockId id) const;
Optional<BlockId> nextRetrans(BlockId id) const;
void clearPrevRetrans(BlockId id);
void clearNextRetrans(BlockId id);
void setNextRetrans(BlockId id, BlockId next);
void append(const RegionDesc& other);
void prepend(const RegionDesc& other);
void chainRetransBlocks();
void setBlockProfCountScale(BlockId, double);
uint32_t instrSize() const;
std::string toString() const;
void setHotWeight(uint64_t weight) { m_hotWeight = weight; }
Optional<uint64_t> getHotWeight() const { return m_hotWeight; }
const std::vector<Type>& inlineInputTypes() const {
return m_inlineInputTypes;
}
Type inlineCtxType() const { return m_inlineCtxType; }
void setInlineContext(Type ctx, const std::vector<Type>& args) {
m_inlineCtxType = ctx;
m_inlineInputTypes = args;
}
template<class Work>
void forEachArc(Work w) const;
private:
struct BlockData {
BlockPtr block;
BlockIdSet preds;
BlockIdSet succs;
BlockIdSet merged; // other blocks that got merged into this
BlockId prevRetransId{kInvalidTransID};
BlockId nextRetransId{kInvalidTransID};
double profCountScale{1.0};
bool hasIncoming{false};
explicit BlockData(BlockPtr b = nullptr) : block(b) {}
};
BlockData& data(BlockId id);
const BlockData& data(BlockId id) const {
return const_cast<RegionDesc*>(this)->data(id);
}
void copyBlocksFrom(const RegionDesc& other,
BlockVec::iterator where);
void copyArcsFrom(const RegionDesc& other);
void postOrderSort(RegionDesc::BlockId bid,
RegionDesc::BlockIdSet& visited,
RegionDesc::BlockIdVec& outVec);
std::vector<BlockPtr> m_blocks;
hphp_hash_map<BlockId, BlockData> m_data;
// When optimizing, we may know what a "hot weight" for this region would be
// relative to other regions. Pass this information down to vasm-layout.
Optional<uint64_t> m_hotWeight;
// For regions selected for inlining, track the types of input arguments
std::vector<Type> m_inlineInputTypes;
Type m_inlineCtxType;
};
using RegionDescPtr = std::shared_ptr<RegionDesc>;
using RegionVec = std::vector<RegionDescPtr>;
using RegionSet = hphp_hash_set<
RegionDescPtr,
smart_pointer_hash<RegionDescPtr>
>;
struct RegionDesc::Arc {
BlockId src;
BlockId dst;
};
/*
* A type for somewhere in the middle of or start of a region.
*
* All types annotated in the RegionDesc are things that need to be guarded on
* or checked and then side-exited on. Types from ahead-of-time static analysis
* are encoded in the bytecode stream.
*/
struct RegionDesc::TypedLocation {
Location location;
Type type;
};
inline bool operator==(const RegionDesc::TypedLocation& a,
const RegionDesc::TypedLocation& b) {
return a.location == b.location && a.type == b.type;
}
/*
* Information about a location that is guarded in a RegionDesc.
* Includes the type and the DataTypeCategory.
*/
struct RegionDesc::GuardedLocation {
Location location;
Type type;
DataTypeCategory category;
};
inline bool operator==(const RegionDesc::GuardedLocation& a,
const RegionDesc::GuardedLocation& b) {
return a.location == b.location &&
a.type == b.type &&
a.category == b.category;
}
/*
* PostConditions are known type information for locals and stack
* locations at the end of profiling translation.
*
* These are kept for blocks that end a profiling translation in order
* to enable better region selection. This information is used to
* prevent profiling translations with incompatible types from being
* stitched together in a larger, optimizing translation.
*
* The PostConditions are kept in two distinct sets:
*
* - the 'changed' set includes locations that may have been
* modified by the corresponding translation;
*
* - the 'refined' set includes locations that are not modified but
* that some information is learned about them during the
* corresponding translation, typically due to type checks or
* asserts.
*/
using TypedLocations = jit::vector<RegionDesc::TypedLocation>;
using GuardedLocations = jit::vector<RegionDesc::GuardedLocation>;
struct PostConditions {
TypedLocations changed;
TypedLocations refined;
PostConditions& operator|=(const PostConditions& other);
};
/*
* A basic block in the region.
*/
struct RegionDesc::Block {
Block(BlockId id, SrcKey start, int length, SBInvOffset initSpOff);
Block& operator=(const Block&) = delete;
/*
* Accessors for the func, unit, length (in HHBC instructions), and
* starting SrcKey of this Block.
*/
BlockId id() const { return m_id; }
const Unit* unit() const { return m_start.unit(); }
const Func* func() const { return m_start.func(); }
SrcKey start() const { return m_start; }
SrcKey last() const { return m_last; }
int length() const { return m_length; }
bool empty() const { return length() == 0; }
SBInvOffset initialSpOffset() const { return m_initialSpOffset; }
TransID profTransID() const { return m_profTransID; }
void setID(BlockId id) { m_id = id; }
void setProfTransID(TransID ptid) { m_profTransID = ptid; }
void setInitialSpOffset(SBInvOffset sp) { m_initialSpOffset = sp; }
/*
* Increase the length of the Block by 1.
*/
void addInstruction();
/*
* Remove all instructions after sk from the block.
*/
void truncateAfter(SrcKey sk);
/*
* Add a precondition type to this block. Preconditions have no effects on
* correctness, but entering a block with a known type that violates a
* precondition is likely to result in a side exit after little to no
* forward progress.
*/
void addPreCondition(const GuardedLocation&);
/*
* Set the post-conditions for this Block.
*/
void setPostConds(const PostConditions&);
/*
* Clears the block's type pre-conditions.
*/
void clearPreConditions();
/*
* The following getters return references to the metadata maps holding the
* information added using the add* and set* methods above. The best way to
* iterate over the information is using a MapWalker, since they're all
* backed by a sorted map.
*/
const GuardedLocations& typePreConditions() const {
return m_typePreConditions;
}
const PostConditions& postConds() const {
return m_postConds;
}
private:
void checkInstructions() const;
void checkInstruction(SrcKey sk) const;
void checkMetadata() const;
private:
const SrcKey m_start;
SrcKey m_last;
BlockId m_id;
int m_length;
SBInvOffset m_initialSpOffset;
TransID m_profTransID;
GuardedLocations m_typePreConditions;
PostConditions m_postConds;
};
//////////////////////////////////////////////////////////////////////
/*
* Information about the context in which we are selecting a region.
*
* Right now this is a source location, plus information about the live
* types that we need to be compiling for. There is no implication that
* the region selected will necessarily be specialized for those types.
*/
struct RegionContext {
struct LiveType;
RegionContext(SrcKey sk, SBInvOffset spOff)
: sk(sk), spOffset(spOff) {}
SrcKey sk;
jit::vector<LiveType> liveTypes;
SBInvOffset spOffset;
};
/*
* Live information about the type of a local or stack slot.
*/
struct RegionContext::LiveType {
Location location;
Type type;
};
//////////////////////////////////////////////////////////////////////
template<class Work> inline
void RegionDesc::forEachArc(Work w) const {
for (auto& src : m_blocks) {
auto srcId = src->id();
for (auto dstId : succs(srcId)) {
w(srcId, dstId);
}
}
}
//////////////////////////////////////////////////////////////////////
/**
* A key struct that uniquely identifies a single translation that can be
* published at any point in time. This includes both the SrcKey for the start
* of the translation and the list of its entry guards.
*/
struct RegionEntryKey {
RegionEntryKey(SrcKey srcKey, const GuardedLocations& guards)
: m_srcKey(srcKey)
, m_guards(guards)
{ }
explicit RegionEntryKey(const RegionDesc& region)
: m_srcKey(region.entry()->start())
, m_guards(region.entry()->typePreConditions())
{ }
SrcKey srcKey() const { return m_srcKey; }
const GuardedLocations& guards() const { return m_guards; }
struct Eq {
bool operator()(const RegionEntryKey& key1,
const RegionEntryKey& key2) const {
return key1.m_srcKey == key2.m_srcKey &&
key1.m_guards == key2.m_guards;
}
};
struct Hash {
size_t operator()(const RegionEntryKey& key) const {
size_t h = key.m_srcKey.toAtomicInt();
Location::Hash lochash;
using data_category_t = std::underlying_type<DataTypeCategory>::type;
for (auto& guard : key.m_guards) {
h = folly::hash::hash_combine(
h, lochash(guard.location),
guard.type.hash(),
static_cast<data_category_t>(guard.category)
);
}
return h;
}
};
private:
SrcKey m_srcKey;
GuardedLocations m_guards;
};
//////////////////////////////////////////////////////////////////////
/*
* Select a compilation region corresponding to the given context.
* The shape of the region selected is controlled by
* RuntimeOption::EvalJitRegionSelector.
*
* This function may return nullptr.
*
* For now this is hooked up in mcgen::translateWork, and
* returning nullptr causes it to use the current level 0 tracelet
* analyzer. Eventually we'd like this to completely replace analyze.
*/
RegionDescPtr selectRegion(const RegionContext& context, TransKind kind);
/*
* Select a compilation region based on profiling information. This
* is used in JitPGO mode. Argument transId specifies the profiling
* translation that triggered the profiling-based region selection.
*/
RegionDescPtr selectHotRegion(TransID transId);
/*
* Select a compilation region as long as possible using the given context.
* The region will be broken before the first instruction that attempts to
* consume an input with an insufficiently precise type, or after most control
* flow instructions. This uses roughly the same heuristics as the old
* analyze() framework.
*
* May return a null region if the given RegionContext doesn't have enough
* information to translate at least one instruction.
*
* The `allowInlining' flag should be disabled when we are selecting a tracelet
* whose shape will be analyzed by the inlining decider.
*/
RegionDescPtr selectTracelet(const RegionContext& ctx, TransKind kind,
int32_t maxBCInstrs, bool inlining = false);
struct HotTransContext {
TransIDSet entries;
TransCFG* cfg;
const ProfData* profData;
int32_t maxBCInstrs;
bool inlining{false};
std::vector<Type>* inputTypes{nullptr};
};
/*
* Select the hottest trace with the given context (starting at ctx.tid).
*/
RegionDescPtr selectHotTrace(HotTransContext& ctx);
/*
* Create a region with the given context ctx (starting at ctx.tid) that
* includes as much of the TransCFG as possible (in "wholecfg" mode), but that
* can be pruned to eliminate cold/unlikely code as well (in "hotcfg" mode).
* Returns in *truncated whether or not the region had to be truncated due to
* the maximum bytecode limit.
*/
RegionDescPtr selectHotCFG(HotTransContext& ctx, bool* truncated = nullptr);
/*
* Checks whether the type pre-conditions at the beginning of block
* satisfy the post-conditions in prevPostConds.
*/
bool preCondsAreSatisfied(const RegionDesc::BlockPtr& block,
const TypedLocations& prevPostConds);
/*
* This function returns true for control-flow bytecode instructions that
* are not supported in the middle of a region yet.
*/
bool breaksRegion(SrcKey sk);
/*
* Creates regions covering all existing profile translations for func.
*/
RegionVec regionizeFunc(const Func* func, std::string& transCFGAnnot);
/*
* Optimize the guards of `region' based on profiling data.
*
* The `region' must have its retranslation blocks already chained.
*/
void optimizeProfiledGuards(RegionDesc& region, const ProfData& profData);
/*
* Returns the PGO region selector to be used for the given `func'.
* This depends on the value of RuntimeOption::EvalJitPGORegionSelector
* and the given `func'.
*/
PGORegionMode pgoRegionMode(const Func& func);
/*
* Functions to map BlockIds to the TransIDs used when the block was
* profiled.
*/
bool hasTransID(RegionDesc::BlockId blockId);
TransID getTransID(RegionDesc::BlockId blockId);
/*
* Checks if the given region is well-formed.
*/
bool check(const RegionDesc& region, std::string& error);
/*
* Debug stringification for various things.
*/
std::string show(RegionDesc::TypedLocation);
std::string show(const RegionDesc::GuardedLocation&);
std::string show(const GuardedLocations&);
std::string show(const PostConditions&);
std::string show(RegionContext::LiveType);
std::string show(const RegionContext&);
std::string show(const RegionDesc::Block&);
std::string show(const RegionDesc&);
//////////////////////////////////////////////////////////////////////
}