forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinlinepolicy.h
555 lines (468 loc) · 16.4 KB
/
inlinepolicy.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Inlining Policies
//
// This file contains class definitions for various inlining
// policies used by the jit.
//
// -- CLASSES --
//
// LegalPolicy - partial class providing common legality checks
// DefaultPolicy - default inliner policy
// ExtendedDefaltPolicy - a more aggressive and profile-driven variation of DefaultPolicy
// DiscretionaryPolicy - default variant with uniform size policy
// ModelPolicy - policy based on statistical modelling
// ProfilePolicy - policy based on statistical modelling and profile feedback
//
// These experimental policies are available only in
// DEBUG or release+INLINE_DATA builds of the jit.
//
// RandomPolicy - randomized inlining
// FullPolicy - inlines everything up to size and depth limits
// SizePolicy - tries not to increase method sizes
//
// The default policy in use is the DefaultPolicy.
#ifndef _INLINE_POLICY_H_
#define _INLINE_POLICY_H_
#include "jit.h"
#include "inline.h"
// LegalPolicy is a partial policy that encapsulates the common
// legality and ability checks the inliner must make.
//
// Generally speaking, the legal policy expects the inlining attempt
// to fail fast when a fatal or equivalent observation is made. So
// once an observation causes failure, no more observations are
// expected. However for the prejit scan case (where the jit is not
// actually inlining, but is assessing a method's general
// inlinability) the legal policy allows multiple failing
// observations provided they have the same impact. Only the first
// observation that puts the policy into a failing state is
// remembered. Transitions from failing states to candidate or success
// states are not allowed.
class LegalPolicy : public InlinePolicy
{
public:
// Constructor
LegalPolicy(bool isPrejitRoot) : InlinePolicy(isPrejitRoot)
{
// empty
}
// Handle an observation that must cause inlining to fail.
void NoteFatal(InlineObservation obs) override;
#if defined(DEBUG) || defined(INLINE_DATA)
// Record observation for prior failure
void NotePriorFailure(InlineObservation obs) override;
#endif // defined(DEBUG) || defined(INLINE_DATA)
protected:
// Helper methods
void NoteInternal(InlineObservation obs);
void SetCandidate(InlineObservation obs);
void SetFailure(InlineObservation obs);
void SetNever(InlineObservation obs);
};
// Forward declaration for the state machine class used by the
// DefaultPolicy
class CodeSeqSM;
// DefaultPolicy implements the default inlining policy for the jit.
class DefaultPolicy : public LegalPolicy
{
public:
// Construct a DefaultPolicy
DefaultPolicy(Compiler* compiler, bool isPrejitRoot)
: LegalPolicy(isPrejitRoot)
, m_RootCompiler(compiler)
, m_StateMachine(nullptr)
, m_Multiplier(0.0)
, m_CodeSize(0)
, m_CallsiteFrequency(InlineCallsiteFrequency::UNUSED)
, m_CallsiteDepth(0)
, m_InstructionCount(0)
, m_LoadStoreCount(0)
, m_ArgFeedsTest(0)
, m_ArgFeedsConstantTest(0)
, m_ArgFeedsRangeCheck(0)
, m_ConstantArgFeedsConstantTest(0)
, m_CalleeNativeSizeEstimate(0)
, m_CallsiteNativeSizeEstimate(0)
, m_IsForceInline(false)
, m_IsForceInlineKnown(false)
, m_IsInstanceCtor(false)
, m_IsFromPromotableValueClass(false)
, m_HasSimd(false)
, m_LooksLikeWrapperMethod(false)
, m_MethodIsMostlyLoadStore(false)
, m_CallsiteIsInTryRegion(false)
, m_CallsiteIsInLoop(false)
, m_IsNoReturn(false)
, m_IsNoReturnKnown(false)
, m_ConstArgFeedsIsKnownConst(false)
, m_ArgFeedsIsKnownConst(false)
{
// empty
}
// Policy observations
void NoteSuccess() override;
void NoteBool(InlineObservation obs, bool value) override;
void NoteInt(InlineObservation obs, int value) override;
void NoteDouble(InlineObservation obs, double value) override;
// Policy determinations
void DetermineProfitability(CORINFO_METHOD_INFO* methodInfo) override;
bool BudgetCheck() const override;
// Policy policies
bool PropagateNeverToRuntime() const override;
// Policy estimates
int CodeSizeEstimate() override;
#if defined(DEBUG) || defined(INLINE_DATA)
void OnDumpXml(FILE* file, unsigned indent = 0) const override;
const char* GetName() const override
{
return "DefaultPolicy";
}
#endif // (DEBUG) || defined(INLINE_DATA)
protected:
// Constants
enum
{
MAX_BASIC_BLOCKS = 5,
SIZE_SCALE = 10
};
// Helper methods
virtual double DetermineMultiplier();
int DetermineNativeSizeEstimate();
int DetermineCallsiteNativeSizeEstimate(CORINFO_METHOD_INFO* methodInfo);
// Data members
Compiler* m_RootCompiler; // root compiler instance
CodeSeqSM* m_StateMachine;
double m_Multiplier;
unsigned m_CodeSize;
InlineCallsiteFrequency m_CallsiteFrequency;
unsigned m_CallsiteDepth;
unsigned m_InstructionCount;
unsigned m_LoadStoreCount;
unsigned m_ArgFeedsTest;
unsigned m_ArgFeedsConstantTest;
unsigned m_ArgFeedsRangeCheck;
unsigned m_ConstantArgFeedsConstantTest;
int m_CalleeNativeSizeEstimate;
int m_CallsiteNativeSizeEstimate;
bool m_IsForceInline : 1;
bool m_IsForceInlineKnown : 1;
bool m_IsInstanceCtor : 1;
bool m_IsFromPromotableValueClass : 1;
bool m_HasSimd : 1;
bool m_LooksLikeWrapperMethod : 1;
bool m_MethodIsMostlyLoadStore : 1;
bool m_CallsiteIsInTryRegion : 1;
bool m_CallsiteIsInLoop : 1;
bool m_IsNoReturn : 1;
bool m_IsNoReturnKnown : 1;
bool m_ConstArgFeedsIsKnownConst : 1;
bool m_ArgFeedsIsKnownConst : 1;
};
// ExtendedDefaultPolicy is a slightly more aggressive variant of
// DefaultPolicy with an extended list of observations including profile data.
class ExtendedDefaultPolicy : public DefaultPolicy
{
public:
ExtendedDefaultPolicy(Compiler* compiler, bool isPrejitRoot)
: DefaultPolicy(compiler, isPrejitRoot)
, m_ProfileFrequency(0.0)
, m_BinaryExprWithCns(0)
, m_ArgCasted(0)
, m_ArgIsStructByValue(0)
, m_FldAccessOverArgStruct(0)
, m_FoldableBox(0)
, m_Intrinsic(0)
, m_BackwardJump(0)
, m_ThrowBlock(0)
, m_ArgIsExactCls(0)
, m_ArgIsExactClsSigIsNot(0)
, m_ArgIsConst(0)
, m_ArgIsBoxedAtCallsite(0)
, m_FoldableIntrinsic(0)
, m_FoldableExpr(0)
, m_FoldableExprUn(0)
, m_FoldableBranch(0)
, m_FoldableSwitch(0)
, m_Switch(0)
, m_DivByCns(0)
, m_ReturnsStructByValue(false)
, m_IsFromValueClass(false)
, m_NonGenericCallsGeneric(false)
, m_IsCallsiteInNoReturnRegion(false)
, m_HasProfile(false)
{
// Empty
}
void NoteBool(InlineObservation obs, bool value) override;
void NoteInt(InlineObservation obs, int value) override;
void NoteDouble(InlineObservation obs, double value) override;
double DetermineMultiplier() override;
bool RequiresPreciseScan() override
{
return true;
}
#if defined(DEBUG) || defined(INLINE_DATA)
void OnDumpXml(FILE* file, unsigned indent = 0) const override;
const char* GetName() const override
{
return "ExtendedDefaultPolicy";
}
#endif // defined(DEBUG) || defined(INLINE_DATA)
protected:
double m_ProfileFrequency;
unsigned m_BinaryExprWithCns;
unsigned m_ArgCasted;
unsigned m_ArgIsStructByValue;
unsigned m_FldAccessOverArgStruct;
unsigned m_FoldableBox;
unsigned m_Intrinsic;
unsigned m_BackwardJump;
unsigned m_ThrowBlock;
unsigned m_ArgIsExactCls;
unsigned m_ArgIsExactClsSigIsNot;
unsigned m_ArgIsConst;
unsigned m_ArgIsBoxedAtCallsite;
unsigned m_FoldableIntrinsic;
unsigned m_FoldableExpr;
unsigned m_FoldableExprUn;
unsigned m_FoldableBranch;
unsigned m_FoldableSwitch;
unsigned m_Switch;
unsigned m_DivByCns;
bool m_ReturnsStructByValue : 1;
bool m_IsFromValueClass : 1;
bool m_NonGenericCallsGeneric : 1;
bool m_IsCallsiteInNoReturnRegion : 1;
bool m_HasProfile : 1;
};
// DiscretionaryPolicy is a variant of the default policy. It
// differs in that there is no ALWAYS_INLINE class, there is no IL
// size limit, and in prejit mode, discretionary failures do not
// propagate the "NEVER" inline bit to the runtime.
//
// It is useful for gathering data about inline costs.
class DiscretionaryPolicy : public DefaultPolicy
{
public:
// Construct a DiscretionaryPolicy
DiscretionaryPolicy(Compiler* compiler, bool isPrejitRoot);
// Policy observations
void NoteBool(InlineObservation obs, bool value) override;
void NoteInt(InlineObservation obs, int value) override;
void NoteDouble(InlineObservation obs, double value) override;
// Policy policies
bool PropagateNeverToRuntime() const override;
// Policy determinations
void DetermineProfitability(CORINFO_METHOD_INFO* methodInfo) override;
// Policy estimates
int CodeSizeEstimate() override;
#if defined(DEBUG) || defined(INLINE_DATA)
// Externalize data
void DumpData(FILE* file) const override;
void DumpSchema(FILE* file) const override;
// Miscellaneous
const char* GetName() const override
{
return "DiscretionaryPolicy";
}
#endif // defined(DEBUG) || defined(INLINE_DATA)
protected:
void ComputeOpcodeBin(OPCODE opcode);
void EstimateCodeSize();
void EstimatePerformanceImpact();
void MethodInfoObservations(CORINFO_METHOD_INFO* methodInfo);
enum
{
MAX_ARGS = 6
};
double m_ProfileFrequency;
unsigned m_BlockCount;
unsigned m_Maxstack;
unsigned m_ArgCount;
CorInfoType m_ArgType[MAX_ARGS];
size_t m_ArgSize[MAX_ARGS];
unsigned m_LocalCount;
CorInfoType m_ReturnType;
size_t m_ReturnSize;
unsigned m_ArgAccessCount;
unsigned m_LocalAccessCount;
unsigned m_IntConstantCount;
unsigned m_FloatConstantCount;
unsigned m_IntLoadCount;
unsigned m_FloatLoadCount;
unsigned m_IntStoreCount;
unsigned m_FloatStoreCount;
unsigned m_SimpleMathCount;
unsigned m_ComplexMathCount;
unsigned m_OverflowMathCount;
unsigned m_IntArrayLoadCount;
unsigned m_FloatArrayLoadCount;
unsigned m_RefArrayLoadCount;
unsigned m_StructArrayLoadCount;
unsigned m_IntArrayStoreCount;
unsigned m_FloatArrayStoreCount;
unsigned m_RefArrayStoreCount;
unsigned m_StructArrayStoreCount;
unsigned m_StructOperationCount;
unsigned m_ObjectModelCount;
unsigned m_FieldLoadCount;
unsigned m_FieldStoreCount;
unsigned m_StaticFieldLoadCount;
unsigned m_StaticFieldStoreCount;
unsigned m_LoadAddressCount;
unsigned m_ThrowCount;
unsigned m_ReturnCount;
unsigned m_CallCount;
unsigned m_CallSiteWeight;
int m_ModelCodeSizeEstimate;
int m_PerCallInstructionEstimate;
bool m_HasProfile;
bool m_IsClassCtor;
bool m_IsSameThis;
bool m_CallerHasNewArray;
bool m_CallerHasNewObj;
bool m_CalleeHasGCStruct;
};
// ModelPolicy is an experimental policy that uses the results
// of data modelling to make estimates.
class ModelPolicy : public DiscretionaryPolicy
{
public:
// Construct a ModelPolicy
ModelPolicy(Compiler* compiler, bool isPrejitRoot);
// Policy observations
void NoteInt(InlineObservation obs, int value) override;
// Policy determinations
void DetermineProfitability(CORINFO_METHOD_INFO* methodInfo) override;
// Policy policies
bool PropagateNeverToRuntime() const override
{
return true;
}
#if defined(DEBUG) || defined(INLINE_DATA)
// Miscellaneous
const char* GetName() const override
{
return "ModelPolicy";
}
#endif // defined(DEBUG) || defined(INLINE_DATA)
};
// ProfilePolicy is an experimental policy that uses the results
// of data modelling and profile feedback to make estimates.
class ProfilePolicy : public DiscretionaryPolicy
{
public:
// Construct a ProfilePolicy
ProfilePolicy(Compiler* compiler, bool isPrejitRoot);
// Policy observations
void NoteInt(InlineObservation obs, int value) override;
// Policy determinations
void DetermineProfitability(CORINFO_METHOD_INFO* methodInfo) override;
#if defined(DEBUG) || defined(INLINE_DATA)
// Miscellaneous
const char* GetName() const override
{
return "ProfilePolicy";
}
#endif // defined(DEBUG) || defined(INLINE_DATA)
};
#if defined(DEBUG) || defined(INLINE_DATA)
// RandomPolicy implements a policy that inlines at random.
// It is mostly useful for stress testing.
class RandomPolicy : public DiscretionaryPolicy
{
public:
// Construct a RandomPolicy
RandomPolicy(Compiler* compiler, bool isPrejitRoot);
// Policy observations
void NoteInt(InlineObservation obs, int value) override;
// Policy determinations
void DetermineProfitability(CORINFO_METHOD_INFO* methodInfo) override;
const char* GetName() const override
{
return "RandomPolicy";
}
private:
// Data members
CLRRandom* m_Random;
};
#endif // defined(DEBUG) || defined(INLINE_DATA)
#if defined(DEBUG) || defined(INLINE_DATA)
// FullPolicy is an experimental policy that will always inline if
// possible, subject to externally settable depth and size limits.
//
// It's useful for uncovering the full set of possible inlines for
// methods.
class FullPolicy : public DiscretionaryPolicy
{
public:
// Construct a FullPolicy
FullPolicy(Compiler* compiler, bool isPrejitRoot);
// Policy determinations
void DetermineProfitability(CORINFO_METHOD_INFO* methodInfo) override;
bool BudgetCheck() const override;
// Miscellaneous
const char* GetName() const override
{
return "FullPolicy";
}
};
// SizePolicy is an experimental policy that will inline as much
// as possible without increasing the (estimated) method size.
//
// It may be useful down the road as a policy to use for methods
// that are rarely executed (eg class constructors).
class SizePolicy : public DiscretionaryPolicy
{
public:
// Construct a SizePolicy
SizePolicy(Compiler* compiler, bool isPrejitRoot);
// Policy determinations
void DetermineProfitability(CORINFO_METHOD_INFO* methodInfo) override;
// Miscellaneous
const char* GetName() const override
{
return "SizePolicy";
}
};
// The ReplayPolicy performs only inlines specified by an external
// inline replay log.
class ReplayPolicy : public DiscretionaryPolicy
{
public:
// Construct a ReplayPolicy
ReplayPolicy(Compiler* compiler, bool isPrejitRoot);
// Policy observations
void NoteBool(InlineObservation obs, bool value) override;
// Optional observations
void NoteContext(InlineContext* context) override
{
m_InlineContext = context;
}
void NoteOffset(IL_OFFSET offset) override
{
m_Offset = offset;
}
// Policy determinations
void DetermineProfitability(CORINFO_METHOD_INFO* methodInfo) override;
// Miscellaneous
const char* GetName() const override
{
return "ReplayPolicy";
}
static void FinalizeXml();
private:
bool FindMethod();
bool FindContext(InlineContext* context);
bool FindInline(CORINFO_METHOD_HANDLE callee);
bool FindInline(unsigned token, unsigned hash, unsigned offset);
static bool s_WroteReplayBanner;
static FILE* s_ReplayFile;
static CritSecObject s_XmlReaderLock;
InlineContext* m_InlineContext;
IL_OFFSET m_Offset;
bool m_WasForceInline;
};
#endif // defined(DEBUG) || defined(INLINE_DATA)
#endif // _INLINE_POLICY_H_