-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
inline.cpp
1771 lines (1499 loc) · 53.8 KB
/
inline.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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "jitpch.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#include "inlinepolicy.h"
// Lookup table for inline description strings
static const char* InlineDescriptions[] = {
#define INLINE_OBSERVATION(name, type, description, impact, target) description,
#include "inline.def"
#undef INLINE_OBSERVATION
};
// Lookup table for inline targets
static const InlineTarget InlineTargets[] = {
#define INLINE_OBSERVATION(name, type, description, impact, target) InlineTarget::target,
#include "inline.def"
#undef INLINE_OBSERVATION
};
// Lookup table for inline impacts
static const InlineImpact InlineImpacts[] = {
#define INLINE_OBSERVATION(name, type, description, impact, target) InlineImpact::impact,
#include "inline.def"
#undef INLINE_OBSERVATION
};
#ifdef DEBUG
//------------------------------------------------------------------------
// InlIsValidObservation: run a validity check on an inline observation
//
// Arguments:
// obs - the observation in question
//
// Return Value:
// true if the observation is valid
bool InlIsValidObservation(InlineObservation obs)
{
return ((obs > InlineObservation::CALLEE_UNUSED_INITIAL) && (obs < InlineObservation::CALLEE_UNUSED_FINAL));
}
#endif // DEBUG
//------------------------------------------------------------------------
// InlGetObservationString: get a string describing this inline observation
//
// Arguments:
// obs - the observation in question
//
// Return Value:
// string describing the observation
const char* InlGetObservationString(InlineObservation obs)
{
assert(InlIsValidObservation(obs));
return InlineDescriptions[static_cast<int>(obs)];
}
//------------------------------------------------------------------------
// InlGetTarget: get the target of an inline observation
//
// Arguments:
// obs - the observation in question
//
// Return Value:
// enum describing the target
InlineTarget InlGetTarget(InlineObservation obs)
{
assert(InlIsValidObservation(obs));
return InlineTargets[static_cast<int>(obs)];
}
//------------------------------------------------------------------------
// InlGetTargetString: get a string describing the target of an inline observation
//
// Arguments:
// obs - the observation in question
//
// Return Value:
// string describing the target
const char* InlGetTargetString(InlineObservation obs)
{
InlineTarget t = InlGetTarget(obs);
switch (t)
{
case InlineTarget::CALLER:
return "caller";
case InlineTarget::CALLEE:
return "callee";
case InlineTarget::CALLSITE:
return "call site";
default:
return "unexpected target";
}
}
//------------------------------------------------------------------------
// InlGetImpact: get the impact of an inline observation
//
// Arguments:
// obs - the observation in question
//
// Return Value:
// enum value describing the impact
InlineImpact InlGetImpact(InlineObservation obs)
{
assert(InlIsValidObservation(obs));
return InlineImpacts[static_cast<int>(obs)];
}
//------------------------------------------------------------------------
// InlGetImpactString: get a string describing the impact of an inline observation
//
// Arguments:
// obs - the observation in question
//
// Return Value:
// string describing the impact
const char* InlGetImpactString(InlineObservation obs)
{
InlineImpact i = InlGetImpact(obs);
switch (i)
{
case InlineImpact::FATAL:
return "correctness -- fatal";
case InlineImpact::FUNDAMENTAL:
return "correctness -- fundamental limitation";
case InlineImpact::LIMITATION:
return "correctness -- jit limitation";
case InlineImpact::PERFORMANCE:
return "performance";
case InlineImpact::INFORMATION:
return "information";
default:
return "unexpected impact";
}
}
//------------------------------------------------------------------------
// InlGetCorInfoInlineDecision: translate decision into a CorInfoInline
//
// Arguments:
// d - the decision in question
//
// Return Value:
// CorInfoInline value representing the decision
CorInfoInline InlGetCorInfoInlineDecision(InlineDecision d)
{
switch (d)
{
case InlineDecision::SUCCESS:
return INLINE_PASS;
case InlineDecision::FAILURE:
return INLINE_FAIL;
case InlineDecision::NEVER:
return INLINE_NEVER;
default:
assert(!"Unexpected InlineDecision");
unreached();
}
}
//------------------------------------------------------------------------
// InlGetDecisionString: get a string representing this decision
//
// Arguments:
// d - the decision in question
//
// Return Value:
// string representing the decision
const char* InlGetDecisionString(InlineDecision d)
{
switch (d)
{
case InlineDecision::SUCCESS:
return "success";
case InlineDecision::FAILURE:
return "failed this call site";
case InlineDecision::NEVER:
return "failed this callee";
case InlineDecision::CANDIDATE:
return "candidate";
case InlineDecision::UNDECIDED:
return "undecided";
default:
assert(!"Unexpected InlineDecision");
unreached();
}
}
//------------------------------------------------------------------------
// InlDecisionIsFailure: check if this decision describes a failing inline
//
// Arguments:
// d - the decision in question
//
// Return Value:
// true if the inline is definitely a failure
bool InlDecisionIsFailure(InlineDecision d)
{
switch (d)
{
case InlineDecision::SUCCESS:
case InlineDecision::UNDECIDED:
case InlineDecision::CANDIDATE:
return false;
case InlineDecision::FAILURE:
case InlineDecision::NEVER:
return true;
default:
assert(!"Unexpected InlineDecision");
unreached();
}
}
//------------------------------------------------------------------------
// InlDecisionIsSuccess: check if this decision describes a sucessful inline
//
// Arguments:
// d - the decision in question
//
// Return Value:
// true if the inline is definitely a success
bool InlDecisionIsSuccess(InlineDecision d)
{
switch (d)
{
case InlineDecision::SUCCESS:
return true;
case InlineDecision::FAILURE:
case InlineDecision::NEVER:
case InlineDecision::UNDECIDED:
case InlineDecision::CANDIDATE:
return false;
default:
assert(!"Unexpected InlineDecision");
unreached();
}
}
//------------------------------------------------------------------------
// InlDecisionIsNever: check if this decision describes a never inline
//
// Arguments:
// d - the decision in question
//
// Return Value:
// true if the inline is a never inline case
bool InlDecisionIsNever(InlineDecision d)
{
switch (d)
{
case InlineDecision::NEVER:
return true;
case InlineDecision::FAILURE:
case InlineDecision::SUCCESS:
case InlineDecision::UNDECIDED:
case InlineDecision::CANDIDATE:
return false;
default:
assert(!"Unexpected InlineDecision");
unreached();
}
}
//------------------------------------------------------------------------
// InlDecisionIsCandidate: check if this decision describes a viable candidate
//
// Arguments:
// d - the decision in question
//
// Return Value:
// true if this inline still might happen
bool InlDecisionIsCandidate(InlineDecision d)
{
return !InlDecisionIsFailure(d);
}
//------------------------------------------------------------------------
// InlDecisionIsDecided: check if this decision has been made
//
// Arguments:
// d - the decision in question
//
// Return Value:
// true if this inline has been decided one way or another
bool InlDecisionIsDecided(InlineDecision d)
{
switch (d)
{
case InlineDecision::NEVER:
case InlineDecision::FAILURE:
case InlineDecision::SUCCESS:
return true;
case InlineDecision::UNDECIDED:
case InlineDecision::CANDIDATE:
return false;
default:
assert(!"Unexpected InlineDecision");
unreached();
}
}
//------------------------------------------------------------------------
// InlineContext: default constructor
InlineContext::InlineContext(InlineStrategy* strategy)
: m_InlineStrategy(strategy)
, m_Parent(nullptr)
, m_Child(nullptr)
, m_Sibling(nullptr)
, m_Code(nullptr)
, m_ILSize(0)
, m_ImportedILSize(0)
, m_Observation(InlineObservation::CALLEE_UNUSED_INITIAL)
, m_CodeSizeEstimate(0)
, m_Success(true)
, m_Devirtualized(false)
, m_Guarded(false)
, m_Unboxed(false)
#if defined(DEBUG) || defined(INLINE_DATA)
, m_Policy(nullptr)
, m_Callee(nullptr)
, m_TreeID(0)
, m_Ordinal(0)
, m_ActualCallOffset(BAD_IL_OFFSET)
#endif // defined(DEBUG) || defined(INLINE_DATA)
#ifdef DEBUG
, m_ILInstsSet(nullptr)
#endif
{
// Empty
}
#if defined(DEBUG) || defined(INLINE_DATA)
//------------------------------------------------------------------------
// Dump: Dump an InlineContext entry and all descendants to jitstdout
//
// Arguments:
// indent - indentation level for this node
// verbose - more verbose output if true
void InlineContext::Dump(bool verbose, unsigned indent)
{
// Handle fact that siblings are in reverse order.
if (m_Sibling != nullptr)
{
m_Sibling->Dump(verbose, indent);
}
// We may not know callee name in some of the failing cases
Compiler* compiler = m_InlineStrategy->GetCompiler();
const char* calleeName = nullptr;
if (m_Callee == nullptr)
{
assert(!m_Success);
calleeName = "<unknown>";
}
else
{
#if defined(DEBUG)
calleeName = compiler->eeGetMethodFullName(m_Callee);
#else
calleeName = "callee";
#endif // defined(DEBUG)
}
mdMethodDef calleeToken = compiler->info.compCompHnd->getMethodDefFromMethod(m_Callee);
// Dump this node
if (m_Parent == nullptr)
{
// Root method
InlinePolicy* policy = InlinePolicy::GetPolicy(compiler, true);
if (verbose)
{
printf("\nInlines into %08X [via %s] %s:\n", calleeToken, policy->GetName(), calleeName);
}
else
{
printf("\nInlines into %s:\n", calleeName);
}
}
else
{
// Inline attempt.
const char* inlineTarget = InlGetTargetString(m_Observation);
const char* inlineReason = InlGetObservationString(m_Observation);
const char* inlineResult = m_Success ? "INLINED: " : "FAILED: ";
const char* devirtualized = m_Devirtualized ? " DEVIRT" : "";
const char* guarded = m_Guarded ? " GUARDED" : "";
const char* unboxed = m_Unboxed ? " UNBOXED" : "";
IL_OFFSET offs = BAD_IL_OFFSET;
#if defined(DEBUG) || defined(INLINE_DATA)
offs = m_ActualCallOffset;
#endif
if (offs == BAD_IL_OFFSET && m_Location.IsValid())
{
offs = m_Location.GetOffset();
}
if (verbose)
{
if (offs == BAD_IL_OFFSET)
{
printf("%*s[" FMT_INL_CTX " IL=???? TR=%06u %08X] [%s%s: %s%s%s%s] %s\n", indent, "", m_Ordinal,
m_TreeID, calleeToken, inlineResult, inlineTarget, inlineReason, guarded, devirtualized, unboxed,
calleeName);
}
else
{
printf("%*s[" FMT_INL_CTX " IL=%04d TR=%06u %08X] [%s%s: %s%s%s%s] %s\n", indent, "", m_Ordinal, offs,
m_TreeID, calleeToken, inlineResult, inlineTarget, inlineReason, guarded, devirtualized, unboxed,
calleeName);
}
}
else
{
printf("%*s[%s%s%s%s%s] %s\n", indent, "", inlineResult, inlineReason, guarded, devirtualized, unboxed,
calleeName);
}
}
// Recurse to first child
if (m_Child != nullptr)
{
m_Child->Dump(verbose, indent + 2);
}
}
//------------------------------------------------------------------------
// DumpData: Dump a successful InlineContext entry, detailed data, and
// any successful descendant inlines
//
// Arguments:
// indent - indentation level for this node
void InlineContext::DumpData(unsigned indent)
{
// Handle fact that siblings are in reverse order.
if (m_Sibling != nullptr)
{
m_Sibling->DumpData(indent);
}
Compiler* compiler = m_InlineStrategy->GetCompiler();
#if defined(DEBUG)
const char* calleeName = compiler->eeGetMethodFullName(m_Callee);
#else
const char* calleeName = "callee";
#endif // defined(DEBUG)
if (m_Parent == nullptr)
{
// Root method... cons up a policy so we can display the name
InlinePolicy* policy = InlinePolicy::GetPolicy(compiler, true);
printf("\nInlines [%u] into \"%s\" [%s]\n", m_InlineStrategy->GetInlineCount(), calleeName, policy->GetName());
}
else if (m_Success)
{
const char* inlineReason = InlGetObservationString(m_Observation);
printf("%*s%u,\"%s\",\"%s\",", indent, "", GetOrdinal(), inlineReason, calleeName);
m_Policy->DumpData(jitstdout);
printf("\n");
}
// Recurse to first child
if (m_Child != nullptr)
{
m_Child->DumpData(indent + 2);
}
}
//------------------------------------------------------------------------
// EscapeNameForXml: Cheap xml quoting for values. Only < and & are
// troublemakers, but change > for symmetry.
//
// Arguments:
// name - string to escape (modifies content)
static void EscapeNameForXml(char* name)
{
int i = 0;
while (name[i] != '\0')
{
switch (name[i])
{
case '<':
name[i] = '[';
break;
case '>':
name[i] = ']';
break;
case '&':
name[i] = '#';
break;
default:
break;
}
i++;
}
}
//------------------------------------------------------------------------
// DumpXml: Dump an InlineContext entry and all descendants in xml format
//
// Arguments:
// file - file for output
// indent - indentation level for this node
void InlineContext::DumpXml(FILE* file, unsigned indent)
{
// Handle fact that siblings are in reverse order.
if (m_Sibling != nullptr)
{
m_Sibling->DumpXml(file, indent);
}
// Optionally suppress failing inline records
if ((JitConfig.JitInlineDumpXml() == 3) && !m_Success)
{
return;
}
const bool isRoot = m_Parent == nullptr;
const bool hasChild = m_Child != nullptr;
const char* inlineType = m_Success ? "Inline" : "FailedInline";
unsigned newIndent = indent;
if (!isRoot)
{
Compiler* compiler = m_InlineStrategy->GetCompiler();
mdMethodDef calleeToken = compiler->info.compCompHnd->getMethodDefFromMethod(m_Callee);
unsigned calleeHash = compiler->compMethodHash(m_Callee);
const char* inlineReason = InlGetObservationString(m_Observation);
const char* name = compiler->eeGetMethodFullName(m_Callee);
char buf[1024];
strncpy(buf, name, sizeof(buf));
buf[sizeof(buf) - 1] = 0;
EscapeNameForXml(buf);
int offset = m_Location.IsValid() ? m_Location.GetOffset() : -1;
fprintf(file, "%*s<%s>\n", indent, "", inlineType);
fprintf(file, "%*s<Token>%08x</Token>\n", indent + 2, "", calleeToken);
fprintf(file, "%*s<Hash>%08x</Hash>\n", indent + 2, "", calleeHash);
fprintf(file, "%*s<Offset>%u</Offset>\n", indent + 2, "", offset);
fprintf(file, "%*s<Reason>%s</Reason>\n", indent + 2, "", inlineReason);
fprintf(file, "%*s<Name>%s</Name>\n", indent + 2, "", buf);
fprintf(file, "%*s<ILSize>%d</ILSize>\n", indent + 2, "", m_ILSize);
fprintf(file, "%*s<Devirtualized>%s</Devirtualized>\n", indent + 2, "", m_Devirtualized ? "True" : "False");
fprintf(file, "%*s<Guarded>%s</Guarded>\n", indent + 2, "", m_Guarded ? "True" : "False");
fprintf(file, "%*s<Unboxed>%s</Unboxed>\n", indent + 2, "", m_Unboxed ? "True" : "False");
// Ask InlinePolicy if it has anything to dump as well:
if ((m_Policy != nullptr) && (JitConfig.JitInlinePolicyDumpXml() != 0))
{
m_Policy->DumpXml(file, indent + 2);
}
// Optionally, dump data about the inline
const int dumpDataSetting = JitConfig.JitInlineDumpData();
// JitInlineDumpData=1 -- dump data plus deltas for last inline only
if ((dumpDataSetting == 1) && (this == m_InlineStrategy->GetLastContext()))
{
fprintf(file, "%*s<Data>", indent + 2, "");
m_InlineStrategy->DumpDataContents(file);
fprintf(file, "</Data>\n");
}
// JitInlineDumpData=2 -- dump data for all inlines, no deltas
if ((dumpDataSetting == 2) && (m_Policy != nullptr))
{
fprintf(file, "%*s<Data>", indent + 2, "");
m_Policy->DumpData(file);
fprintf(file, "</Data>\n");
}
newIndent = indent + 2;
}
// Handle children
if (hasChild)
{
fprintf(file, "%*s<Inlines>\n", newIndent, "");
m_Child->DumpXml(file, newIndent + 2);
fprintf(file, "%*s</Inlines>\n", newIndent, "");
}
else
{
fprintf(file, "%*s<Inlines />\n", newIndent, "");
}
// Close out
if (!isRoot)
{
fprintf(file, "%*s</%s>\n", indent, "", inlineType);
}
}
#endif // defined(DEBUG) || defined(INLINE_DATA)
//------------------------------------------------------------------------
// InlineResult: Construct an InlineResult to evaluate a particular call
// for inlining.
//
// Arguments:
// compiler - the compiler instance examining a call for inlining
// call - the call in question
// stmt - statement containing the call (if known)
// description - string describing the context of the decision
InlineResult::InlineResult(Compiler* compiler, GenTreeCall* call, Statement* stmt, const char* description)
: m_RootCompiler(nullptr)
, m_Policy(nullptr)
, m_Call(call)
, m_InlineContext(nullptr)
, m_Caller(nullptr)
, m_Callee(nullptr)
, m_ImportedILSize(0)
, m_Description(description)
, m_Reported(false)
{
// Set the compiler instance
m_RootCompiler = compiler->impInlineRoot();
// Set the policy
const bool isPrejitRoot = false;
m_Policy = InlinePolicy::GetPolicy(m_RootCompiler, isPrejitRoot);
// Pass along some optional information to the policy.
if (stmt != nullptr)
{
m_InlineContext = stmt->GetDebugInfo().GetInlineContext();
m_Policy->NoteContext(m_InlineContext);
#if defined(DEBUG) || defined(INLINE_DATA)
m_Policy->NoteOffset(call->gtRawILOffset);
#else
m_Policy->NoteOffset(stmt->GetDebugInfo().GetLocation().GetOffset());
#endif // defined(DEBUG) || defined(INLINE_DATA)
}
// Get method handle for caller. Note we use the
// handle for the "immediate" caller here.
m_Caller = compiler->info.compMethodHnd;
// Get method handle for callee, if known
if (m_Call->AsCall()->gtCallType == CT_USER_FUNC)
{
m_Callee = m_Call->AsCall()->gtCallMethHnd;
}
}
//------------------------------------------------------------------------
// InlineResult: Construct an InlineResult to evaluate a particular
// method as a possible inline candidate, while prejtting.
//
// Arguments:
// compiler - the compiler instance doing the prejitting
// method - the method in question
// description - string describing the context of the decision
//
// Notes:
// Used only during prejitting to try and pre-identify methods that
// cannot be inlined, to help subsequent jit throughput.
//
// We use the inlCallee member to track the method since logically
// it is the callee here.
InlineResult::InlineResult(Compiler* compiler, CORINFO_METHOD_HANDLE method, const char* description)
: m_RootCompiler(nullptr)
, m_Policy(nullptr)
, m_Call(nullptr)
, m_InlineContext(nullptr)
, m_Caller(nullptr)
, m_Callee(method)
, m_Description(description)
, m_Reported(false)
{
// Set the compiler instance
m_RootCompiler = compiler->impInlineRoot();
// Set the policy
const bool isPrejitRoot = true;
m_Policy = InlinePolicy::GetPolicy(m_RootCompiler, isPrejitRoot);
}
//------------------------------------------------------------------------
// Report: Dump, log, and report information about an inline decision.
//
// Notes:
// Called (automatically via the InlineResult dtor) when the
// inliner is done evaluating a candidate.
//
// Dumps state of the inline candidate, and if a decision was
// reached, sends it to the log and reports the decision back to the
// EE. Optionally update the method attribute to NOINLINE if
// observation and policy warrant.
//
// All this can be suppressed if desired by calling setReported()
// before the InlineResult goes out of scope.
void InlineResult::Report()
{
#ifdef DEBUG
// If this is a failure of a specific inline candidate and we haven't captured
// a failing observation yet, do so now.
if (IsFailure() && (m_Call != nullptr))
{
// compiler should have revoked candidacy on the call by now
assert((m_Call->gtFlags & GTF_CALL_INLINE_CANDIDATE) == 0);
if (m_Call->gtInlineObservation == InlineObservation::CALLEE_UNUSED_INITIAL)
{
m_Call->gtInlineObservation = m_Policy->GetObservation();
}
}
#endif // DEBUG
// If we weren't actually inlining, user may have suppressed
// reporting via setReported(). If so, do nothing.
if (m_Reported)
{
return;
}
m_Reported = true;
#ifdef DEBUG
const char* callee = nullptr;
// Optionally dump the result
if (VERBOSE || m_RootCompiler->fgPrintInlinedMethods)
{
const char* format = "INLINER: during '%s' result '%s' reason '%s' for '%s' calling '%s'\n";
const char* caller = (m_Caller == nullptr) ? "n/a" : m_RootCompiler->eeGetMethodFullName(m_Caller);
callee = (m_Callee == nullptr) ? "n/a" : m_RootCompiler->eeGetMethodFullName(m_Callee);
JITDUMP(format, m_Description, ResultString(), ReasonString(), caller, callee);
}
#endif // DEBUG
// Was the result NEVER? If so we might want to propagate this to
// the runtime.
if (IsNever() && m_Policy->PropagateNeverToRuntime())
{
// If we know the callee, and if the observation that got us
// to this Never inline state is something *other* than
// IS_NOINLINE, then we've uncovered a reason why this method
// can't ever be inlined. Update the callee method attributes
// so that future inline attempts for this callee fail faster.
InlineObservation obs = m_Policy->GetObservation();
if ((m_Callee != nullptr) && (obs != InlineObservation::CALLEE_IS_NOINLINE))
{
JITDUMP("\nINLINER: Marking %s as NOINLINE because of %s\n", callee, InlGetObservationString(obs));
COMP_HANDLE comp = m_RootCompiler->info.compCompHnd;
comp->setMethodAttribs(m_Callee, CORINFO_FLG_BAD_INLINEE);
}
}
if (IsDecided())
{
const char* format = "INLINER: during '%s' result '%s' reason '%s'\n";
JITLOG_THIS(m_RootCompiler, (LL_INFO100000, format, m_Description, ResultString(), ReasonString()));
COMP_HANDLE comp = m_RootCompiler->info.compCompHnd;
comp->reportInliningDecision(m_Caller, m_Callee, Result(), ReasonString());
}
}
//------------------------------------------------------------------------
// InlineStrategy construtor
//
// Arguments
// compiler - root compiler instance
InlineStrategy::InlineStrategy(Compiler* compiler)
: m_Compiler(compiler)
, m_RootContext(nullptr)
, m_LastSuccessfulPolicy(nullptr)
, m_LastContext(nullptr)
, m_PrejitRootDecision(InlineDecision::UNDECIDED)
, m_CallCount(0)
, m_CandidateCount(0)
, m_AlwaysCandidateCount(0)
, m_ForceCandidateCount(0)
, m_DiscretionaryCandidateCount(0)
, m_UnprofitableCandidateCount(0)
, m_ImportCount(0)
, m_InlineCount(0)
, m_MaxInlineSize(DEFAULT_MAX_INLINE_SIZE)
, m_MaxInlineDepth(DEFAULT_MAX_INLINE_DEPTH)
, m_InitialTimeBudget(0)
, m_InitialTimeEstimate(0)
, m_CurrentTimeBudget(0)
, m_CurrentTimeEstimate(0)
, m_InitialSizeEstimate(0)
, m_CurrentSizeEstimate(0)
, m_HasForceViaDiscretionary(false)
#if defined(DEBUG) || defined(INLINE_DATA)
, m_MethodXmlFilePosition(0)
, m_Random(nullptr)
#endif // defined(DEBUG) || defined(INLINE_DATA)
{
// Verify compiler is a root compiler instance
assert(m_Compiler->impInlineRoot() == m_Compiler);
#ifdef DEBUG
// Possibly modify the max inline size.
//
// Default value of JitInlineSize is the same as our default.
// So normally this next line does not change the size.
m_MaxInlineSize = JitConfig.JitInlineSize();
// Up the max size under stress
if (m_Compiler->compInlineStress())
{
m_MaxInlineSize *= 10;
}
// But don't overdo it
if (m_MaxInlineSize > IMPLEMENTATION_MAX_INLINE_SIZE)
{
m_MaxInlineSize = IMPLEMENTATION_MAX_INLINE_SIZE;
}
// Verify: not too small, not too big.
assert(m_MaxInlineSize >= ALWAYS_INLINE_SIZE);
assert(m_MaxInlineSize <= IMPLEMENTATION_MAX_INLINE_SIZE);
// Possibly modify the max inline depth
//
// Default value of JitInlineDepth is the same as our default.
// So normally this next line does not change the size.
m_MaxInlineDepth = JitConfig.JitInlineDepth();
// But don't overdo it
if (m_MaxInlineDepth > IMPLEMENTATION_MAX_INLINE_DEPTH)
{
m_MaxInlineDepth = IMPLEMENTATION_MAX_INLINE_DEPTH;
}
#endif // DEBUG
}
//------------------------------------------------------------------------
// GetRootContext: get the InlineContext for the root method
//
// Return Value:
// Root context; describes the method being jitted.
//
// Note:
// Also initializes the jit time estimate and budget.
InlineContext* InlineStrategy::GetRootContext()
{
if (m_RootContext == nullptr)
{
// Allocate on first demand.
m_RootContext = NewRoot();
// Estimate how long the jit will take if there's no inlining
// done to this method.
m_InitialTimeEstimate = EstimateTime(m_RootContext);
m_CurrentTimeEstimate = m_InitialTimeEstimate;
// Set the initial budget for inlining. Note this is
// deliberately set very high and is intended to catch
// only pathological runaway inline cases.
m_InitialTimeBudget = BUDGET * m_InitialTimeEstimate;
m_CurrentTimeBudget = m_InitialTimeBudget;
// Estimate the code size if there's no inlining
m_InitialSizeEstimate = EstimateSize(m_RootContext);
m_CurrentSizeEstimate = m_InitialSizeEstimate;
// Sanity check
assert(m_CurrentTimeEstimate > 0);
assert(m_CurrentSizeEstimate > 0);
// Cache as the "last" context created
m_LastContext = m_RootContext;
}
return m_RootContext;
}
//------------------------------------------------------------------------
// NoteAttempt: do bookkeeping for an inline attempt
//
// Arguments:
// result -- InlineResult for successful inline candidate
void InlineStrategy::NoteAttempt(InlineResult* result)
{
assert(result->IsCandidate());
InlineObservation obs = result->GetObservation();
if (obs == InlineObservation::CALLEE_BELOW_ALWAYS_INLINE_SIZE)
{
m_AlwaysCandidateCount++;
}
else if (obs == InlineObservation::CALLEE_IS_FORCE_INLINE)
{
m_ForceCandidateCount++;
}
else
{
m_DiscretionaryCandidateCount++;
}
}
//------------------------------------------------------------------------
// DumpCsvHeader: dump header for csv inline stats
//
// Argument:
// fp -- file for dump output
void InlineStrategy::DumpCsvHeader(FILE* fp)
{
fprintf(fp, "\"InlineCalls\",");
fprintf(fp, "\"InlineCandidates\",");
fprintf(fp, "\"InlineAlways\",");
fprintf(fp, "\"InlineForce\",");
fprintf(fp, "\"InlineDiscretionary\",");
fprintf(fp, "\"InlineUnprofitable\",");
fprintf(fp, "\"InlineEarlyFail\",");
fprintf(fp, "\"InlineImport\",");
fprintf(fp, "\"InlineLateFail\",");
fprintf(fp, "\"InlineSuccess\",");
}
//------------------------------------------------------------------------
// DumpCsvData: dump data for csv inline stats
//
// Argument:
// fp -- file for dump output
void InlineStrategy::DumpCsvData(FILE* fp)
{
fprintf(fp, "%u,", m_CallCount);
fprintf(fp, "%u,", m_CandidateCount);
fprintf(fp, "%u,", m_AlwaysCandidateCount);
fprintf(fp, "%u,", m_ForceCandidateCount);
fprintf(fp, "%u,", m_DiscretionaryCandidateCount);
fprintf(fp, "%u,", m_UnprofitableCandidateCount);
// Early failures are cases where candates are rejected between
// the time the jit invokes the inlinee compiler and the time it
// starts to import the inlinee IL.
//
// So they are "cheaper" that late failures.
unsigned profitableCandidateCount = m_DiscretionaryCandidateCount - m_UnprofitableCandidateCount;
unsigned earlyFailCount =
m_CandidateCount - m_AlwaysCandidateCount - m_ForceCandidateCount - profitableCandidateCount;
fprintf(fp, "%u,", earlyFailCount);