forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfgprofilesynthesis.cpp
1075 lines (938 loc) · 34.4 KB
/
fgprofilesynthesis.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 "fgprofilesynthesis.h"
// TODO
//
// * faster way of doing fgGetPredForBlock
// * vet against some real data
// * IR based heuristics (perhaps)
// * During Cp, avoid repeatedly propagating through nested loops
// * Fake BB0 or always force scratch BB
// * Reconcile with our other loop finding stuff
// * Stop the upweight/downweight of loops in rest of jit
// * Durable edge properties (exit, back)
// * Tweak RunRarely to be at or near zero
// * OSR entry weight
// * Special handling for deep nests?
// * Plan for irreducible cases -- MoveNext's
//------------------------------------------------------------------------
// fgProfileSynthesis: update edge likelihoods and block counts based
// on various strategies
//
// Arguments:
// options - options to control synthesis
//
void ProfileSynthesis::Run(ProfileSynthesisOption option)
{
m_dfsTree = m_comp->fgComputeDfs();
m_loops = FlowGraphNaturalLoops::Find(m_dfsTree);
// Retain or compute edge likelihood information
//
switch (option)
{
case ProfileSynthesisOption::AssignLikelihoods:
AssignLikelihoods();
break;
case ProfileSynthesisOption::RetainLikelihoods:
break;
case ProfileSynthesisOption::BlendLikelihoods:
BlendLikelihoods();
break;
case ProfileSynthesisOption::ResetAndSynthesize:
ClearLikelihoods();
AssignLikelihoods();
break;
case ProfileSynthesisOption::ReverseLikelihoods:
ReverseLikelihoods();
break;
case ProfileSynthesisOption::RandomLikelihoods:
RandomizeLikelihoods();
break;
case ProfileSynthesisOption::RepairLikelihoods:
RepairLikelihoods();
break;
default:
assert(!"unexpected profile synthesis option");
break;
}
// Determine cyclic probabilities
//
ComputeCyclicProbabilities();
// Assign weights to entry points in the flow graph
//
AssignInputWeights(option);
// Compute the block weights given the inputs and edge likelihoods
//
ComputeBlockWeights();
// Update pgo info
//
const bool hadPgoWeights = m_comp->fgPgoHaveWeights;
ICorJitInfo::PgoSource newSource = ICorJitInfo::PgoSource::Synthesis;
if (option == ProfileSynthesisOption::RepairLikelihoods)
{
newSource = m_comp->fgPgoSource;
}
else if (hadPgoWeights && (option == ProfileSynthesisOption::BlendLikelihoods))
{
newSource = ICorJitInfo::PgoSource::Blend;
}
m_comp->fgPgoHaveWeights = true;
m_comp->fgPgoSource = newSource;
#ifdef DEBUG
if (JitConfig.JitCheckSynthesizedCounts() > 0)
{
// Verify consistency, provided we didn't see any improper headers
// or cap any Cp values.
//
if ((m_improperLoopHeaders == 0) && (m_cappedCyclicProbabilities == 0))
{
// verify likely weights, assert on failure, check all blocks
m_comp->fgDebugCheckProfileWeights(ProfileChecks::CHECK_LIKELY | ProfileChecks::RAISE_ASSERT |
ProfileChecks::CHECK_ALL_BLOCKS);
}
}
#endif
}
//------------------------------------------------------------------------
// AssignLikelihoods: update edge likelihoods and block counts based
// entirely on heuristics.
//
// Notes:
// any existing likelihoods are removed in favor of heuristic
// likelihoods
//
void ProfileSynthesis::AssignLikelihoods()
{
JITDUMP("Assigning edge likelihoods based on heuristics\n");
for (BasicBlock* const block : m_comp->Blocks())
{
switch (block->GetKind())
{
case BBJ_THROW:
case BBJ_RETURN:
case BBJ_EHFINALLYRET:
case BBJ_EHFAULTRET:
// No successor cases
// (todo: finally ret may have succs)
break;
case BBJ_CALLFINALLY:
// Single successor next cases
//
// Note we handle flow to the finally
// specially; this represents return
// from the finally.
AssignLikelihoodNext(block);
break;
case BBJ_ALWAYS:
case BBJ_CALLFINALLYRET:
case BBJ_LEAVE:
case BBJ_EHCATCHRET:
case BBJ_EHFILTERRET:
// Single successor jump cases
AssignLikelihoodJump(block);
break;
case BBJ_COND:
// Two successor cases
AssignLikelihoodCond(block);
break;
case BBJ_SWITCH:
// N successor cases
AssignLikelihoodSwitch(block);
break;
default:
unreached();
}
}
}
//------------------------------------------------------------------------
// AssignLikelihoodNext: update edge likelihood for block that always
// transfers control to bbNext
//
// Arguments;
// block -- block in question
//
void ProfileSynthesis::AssignLikelihoodNext(BasicBlock* block)
{
FlowEdge* const edge = m_comp->fgGetPredForBlock(block->Next(), block);
edge->setLikelihood(1.0);
}
//------------------------------------------------------------------------
// AssignLikelihoodJump: update edge likelihood for a block that always
// transfers control to bbTarget
//
// Arguments;
// block -- block in question
//
void ProfileSynthesis::AssignLikelihoodJump(BasicBlock* block)
{
FlowEdge* const edge = m_comp->fgGetPredForBlock(block->GetTarget(), block);
edge->setLikelihood(1.0);
}
//------------------------------------------------------------------------
// AssignLikelihoodCond: update edge likelihood for a block that
// ends in a conditional branch
//
// Arguments;
// block -- block in question (BBJ_COND)
//
void ProfileSynthesis::AssignLikelihoodCond(BasicBlock* block)
{
BasicBlock* const jump = block->GetTrueTarget();
BasicBlock* const next = block->GetFalseTarget();
// Watch for degenerate case
//
if (jump == next)
{
AssignLikelihoodNext(block);
return;
}
FlowEdge* const jumpEdge = m_comp->fgGetPredForBlock(jump, block);
FlowEdge* const nextEdge = m_comp->fgGetPredForBlock(next, block);
// THROW heuristic
//
bool const isJumpThrow = jump->KindIs(BBJ_THROW);
bool const isNextThrow = next->KindIs(BBJ_THROW);
if (isJumpThrow != isNextThrow)
{
if (isJumpThrow)
{
jumpEdge->setLikelihood(0.0);
nextEdge->setLikelihood(1.0);
}
else
{
jumpEdge->setLikelihood(1.0);
nextEdge->setLikelihood(0.0);
}
return;
}
// LOOP BACK EDGE heuristic
//
bool const isJumpEdgeBackEdge = m_loops->IsLoopBackEdge(jumpEdge);
bool const isNextEdgeBackEdge = m_loops->IsLoopBackEdge(nextEdge);
if (isJumpEdgeBackEdge != isNextEdgeBackEdge)
{
if (isJumpEdgeBackEdge)
{
JITDUMP(FMT_BB "->" FMT_BB " is loop back edge\n", block->bbNum, jump->bbNum);
jumpEdge->setLikelihood(loopBackLikelihood);
nextEdge->setLikelihood(1.0 - loopBackLikelihood);
}
else
{
JITDUMP(FMT_BB "->" FMT_BB " is loop back edge\n", block->bbNum, next->bbNum);
jumpEdge->setLikelihood(1.0 - loopBackLikelihood);
nextEdge->setLikelihood(loopBackLikelihood);
}
return;
}
// LOOP EXIT EDGE heuristic
//
// Consider: adjust probability if loop has multiple exit edges, so that
// overall exit probability is around 0.1.
//
bool const isJumpEdgeExitEdge = m_loops->IsLoopExitEdge(jumpEdge);
bool const isNextEdgeExitEdge = m_loops->IsLoopExitEdge(nextEdge);
if (isJumpEdgeExitEdge != isNextEdgeExitEdge)
{
if (isJumpEdgeExitEdge)
{
JITDUMP(FMT_BB "->" FMT_BB " is loop exit edge\n", block->bbNum, jump->bbNum);
jumpEdge->setLikelihood(1.0 - loopExitLikelihood);
nextEdge->setLikelihood(loopExitLikelihood);
}
else
{
JITDUMP(FMT_BB "->" FMT_BB " is loop exit edge\n", block->bbNum, next->bbNum);
jumpEdge->setLikelihood(loopExitLikelihood);
nextEdge->setLikelihood(1.0 - loopExitLikelihood);
}
return;
}
// RETURN heuristic
//
bool const isJumpReturn = jump->KindIs(BBJ_RETURN);
bool const isNextReturn = next->KindIs(BBJ_RETURN);
if (isJumpReturn != isNextReturn)
{
if (isJumpReturn)
{
jumpEdge->setLikelihood(returnLikelihood);
nextEdge->setLikelihood(1.0 - returnLikelihood);
}
else
{
jumpEdge->setLikelihood(1.0 - returnLikelihood);
nextEdge->setLikelihood(returnLikelihood);
}
return;
}
// IL OFFSET heuristic
//
// Give slight preference to bbNext
//
jumpEdge->setLikelihood(1.0 - ilNextLikelihood);
nextEdge->setLikelihood(ilNextLikelihood);
}
//------------------------------------------------------------------------
// AssignLikelihoodSwitch: update edge likelihood for a block that
// ends in a switch
//
// Arguments;
// block -- block in question (BBJ_SWITCH)
//
void ProfileSynthesis::AssignLikelihoodSwitch(BasicBlock* block)
{
// Assume each switch case is equally probable
//
const unsigned n = block->NumSucc();
assert(n != 0);
// Check for divide by zero to avoid compiler warnings for Release builds (above assert is removed)
const weight_t p = (n != 0) ? (1 / (weight_t)n) : 0;
// Each unique edge gets some multiple of that basic probability
//
for (BasicBlock* const succ : block->Succs(m_comp))
{
FlowEdge* const edge = m_comp->fgGetPredForBlock(succ, block);
edge->setLikelihood(p * edge->getDupCount());
}
}
//------------------------------------------------------------------------
// SumOutgoingLikelihoods: sum existing likelihoods for exiting a block
//
// Arguments:
// block -- block in question
// likelihoods -- [optional, out] vector to fill in with the outgoing likelihoods
//
// Returns:
// Sum of likelihoods of each successor
//
weight_t ProfileSynthesis::SumOutgoingLikelihoods(BasicBlock* block, WeightVector* likelihoods)
{
weight_t sum = 0;
if (likelihoods != nullptr)
{
likelihoods->clear();
}
for (BasicBlock* const succ : block->Succs(m_comp))
{
FlowEdge* const edge = m_comp->fgGetPredForBlock(succ, block);
weight_t likelihood = edge->getLikelihood();
if (likelihoods != nullptr)
{
likelihoods->push_back(likelihood);
}
sum += likelihood;
}
return sum;
}
//------------------------------------------------------------------------
// RepairLikelihoods: find nodes with inconsistent or missing likelihoods
// and update them with heuristics.
//
// Notes:
// Existing likelihoods are retained, if consistent.
//
void ProfileSynthesis::RepairLikelihoods()
{
JITDUMP("Repairing inconsistent or missing edge likelihoods\n");
for (BasicBlock* const block : m_comp->Blocks())
{
switch (block->GetKind())
{
case BBJ_THROW:
case BBJ_RETURN:
case BBJ_EHFINALLYRET:
case BBJ_EHFAULTRET:
// No successor cases
// Nothing to do.
break;
case BBJ_CALLFINALLY:
// Single successor next cases.
// Just assign 1.0
AssignLikelihoodNext(block);
break;
case BBJ_ALWAYS:
case BBJ_CALLFINALLYRET:
case BBJ_LEAVE:
case BBJ_EHCATCHRET:
case BBJ_EHFILTERRET:
// Single successor jump cases
// Just assign 1.0
AssignLikelihoodJump(block);
break;
case BBJ_COND:
case BBJ_SWITCH:
{
// Repair if either likelihoods are inconsistent or block weight is zero.
//
weight_t const sum = SumOutgoingLikelihoods(block);
bool const consistent = Compiler::fgProfileWeightsEqual(sum, 1.0, epsilon);
bool const zero = Compiler::fgProfileWeightsEqual(block->bbWeight, 0.0, epsilon);
if (consistent && !zero)
{
// Leave as is.
break;
}
JITDUMP("Repairing likelihoods in " FMT_BB, block->bbNum);
if (!consistent)
{
JITDUMP("; existing likelihood sum: " FMT_WT, sum);
}
if (zero)
{
JITDUMP("; zero weight block");
}
JITDUMP("\n");
if (block->KindIs(BBJ_COND))
{
AssignLikelihoodCond(block);
}
else
{
AssignLikelihoodSwitch(block);
}
break;
}
default:
unreached();
}
}
}
//------------------------------------------------------------------------
// BlendLikelihoods: if a node has existing likelihoods that differ from
// the heuristic likelihoods, blend the heuristic prediction with the
// current prediction
//
// Notes:
//
// Blend is weighted 95% existing / 5% heuristics
//
// If a node's existing likelihoods don't sum to 1.0:
// * if sum is zero, just run heuristics
// * if sum is nonzero, scale that up and then blend
//
void ProfileSynthesis::BlendLikelihoods()
{
JITDUMP("Blending existing likelihoods with heuristics\n");
WeightVector likelihoods(m_comp->getAllocator(CMK_Pgo));
for (BasicBlock* const block : m_comp->Blocks())
{
weight_t sum = SumOutgoingLikelihoods(block, &likelihoods);
switch (block->GetKind())
{
case BBJ_THROW:
case BBJ_RETURN:
case BBJ_EHFINALLYRET:
case BBJ_EHFAULTRET:
// No successor cases
// Nothing to do.
break;
case BBJ_CALLFINALLY:
// Single successor next cases.
// Just assign 1.0
AssignLikelihoodNext(block);
break;
case BBJ_ALWAYS:
case BBJ_CALLFINALLYRET:
case BBJ_LEAVE:
case BBJ_EHCATCHRET:
case BBJ_EHFILTERRET:
// Single successor jump cases
// Just assign 1.0
AssignLikelihoodJump(block);
break;
case BBJ_COND:
case BBJ_SWITCH:
{
// Capture the existing weights and assign new likelihoods based on synthesis.
//
weight_t const sum = SumOutgoingLikelihoods(block, &likelihoods);
bool const unlikely = Compiler::fgProfileWeightsEqual(sum, 0.0, epsilon);
bool const consistent = Compiler::fgProfileWeightsEqual(sum, 1.0, epsilon);
bool const zero = Compiler::fgProfileWeightsEqual(block->bbWeight, 0.0, epsilon);
if (block->KindIs(BBJ_COND))
{
AssignLikelihoodCond(block);
}
else
{
AssignLikelihoodSwitch(block);
}
if (unlikely || zero)
{
// Existing likelihood was zero, or profile weight was zero. Just use synthesis likelihoods.
//
JITDUMP("%s in " FMT_BB " was zero, using synthesized likelihoods\n",
unlikely ? "Existing likelihood" : "Block weight", block->bbNum);
break;
}
WeightVector::iterator iter;
if (!Compiler::fgProfileWeightsEqual(sum, 1.0, epsilon))
{
// Existing likelihood was too low or too high. Scale.
//
weight_t scale = 1.0 / sum;
JITDUMP("Scaling old likelihoods in " FMT_BB " by " FMT_WT "\n", block->bbNum, scale);
for (iter = likelihoods.begin(); iter != likelihoods.end(); iter++)
{
*iter *= scale;
}
}
// Blend
//
JITDUMP("Blending likelihoods in " FMT_BB " with blend factor " FMT_WT " \n", block->bbNum,
blendFactor);
iter = likelihoods.begin();
for (BasicBlock* const succ : block->Succs(m_comp))
{
FlowEdge* const edge = m_comp->fgGetPredForBlock(succ, block);
weight_t newLikelihood = edge->getLikelihood();
weight_t oldLikelihood = *iter;
edge->setLikelihood((blendFactor * oldLikelihood) + ((1.0 - blendFactor) * newLikelihood));
JITDUMP(FMT_BB " -> " FMT_BB " was " FMT_WT " now " FMT_WT "\n", block->bbNum, succ->bbNum,
oldLikelihood, edge->getLikelihood());
iter++;
}
break;
}
default:
unreached();
}
}
}
//------------------------------------------------------------------------
// ClearLikelihoods: unset likelihoods on all edges
//
void ProfileSynthesis::ClearLikelihoods()
{
for (BasicBlock* const block : m_comp->Blocks())
{
for (BasicBlock* const succ : block->Succs(m_comp))
{
FlowEdge* const edge = m_comp->fgGetPredForBlock(succ, block);
edge->clearLikelihood();
}
}
}
//------------------------------------------------------------------------
// ReverseLikelihoods: for all blocks, reverse likelihoods on all edges
// from the block
//
void ProfileSynthesis::ReverseLikelihoods()
{
#ifdef DEBUG
JITDUMP("Reversing likelihoods\n");
WeightVector likelihoods(m_comp->getAllocator(CMK_Pgo));
for (BasicBlock* const block : m_comp->Blocks())
{
for (BasicBlock* const succ : block->Succs(m_comp))
{
weight_t sum = SumOutgoingLikelihoods(block, &likelihoods);
if (likelihoods.size() < 2)
{
continue;
}
for (size_t i = 0; i < likelihoods.size() / 2; i++)
{
size_t j = likelihoods.size() - i - 1;
weight_t t = likelihoods[i];
likelihoods[i] = likelihoods[j];
likelihoods[j] = t;
}
}
}
#endif // DEBUG
}
//------------------------------------------------------------------------
// RandomizeLikelihoods: for all blocks, randomize likelihoods on all edges
// from the block
//
// Notes:
// total outgoing likelihood for each block remains at 1.0
//
void ProfileSynthesis::RandomizeLikelihoods()
{
#ifdef DEBUG
// todo: external seed
JITDUMP("Randomizing likelihoods\n");
WeightVector likelihoods(m_comp->getAllocator(CMK_Pgo));
CLRRandom random;
random.Init(m_comp->info.compMethodHash());
for (BasicBlock* const block : m_comp->Blocks())
{
unsigned const N = block->NumSucc(m_comp);
if (N < 2)
{
continue;
}
likelihoods.clear();
likelihoods.reserve(N);
weight_t sum = 0;
unsigned i = 0;
// Consider: something other than uniform distribution.
// As is, this will rarely set likelihoods to zero.
//
for (i = 0; i < N; i++)
{
likelihoods[i] = (weight_t)random.NextDouble();
sum += likelihoods[i];
}
i = 0;
for (BasicBlock* const succ : block->Succs(m_comp))
{
FlowEdge* const edge = m_comp->fgGetPredForBlock(succ, block);
edge->setLikelihood(likelihoods[i++] / sum);
}
}
#endif // DEBUG
}
//------------------------------------------------------------------------
// FindCyclicProbabilities: for each loop, compute how much flow returns
// to the loop head given one external count.
//
void ProfileSynthesis::ComputeCyclicProbabilities()
{
m_cyclicProbabilities = nullptr;
if (m_loops->NumLoops() == 0)
{
return;
}
m_cyclicProbabilities = new (m_comp, CMK_Pgo) weight_t[m_loops->NumLoops()];
// Walk loops in post order to visit inner loops before outer loops.
for (FlowGraphNaturalLoop* loop : m_loops->InPostOrder())
{
ComputeCyclicProbabilities(loop);
}
}
//------------------------------------------------------------------------
// FindCyclicProbabilities: for a given loop, compute how much flow returns
// to the loop head given one external count.
//
void ProfileSynthesis::ComputeCyclicProbabilities(FlowGraphNaturalLoop* loop)
{
// Initialize
//
loop->VisitLoopBlocks([](BasicBlock* loopBlock) {
loopBlock->bbWeight = 0.0;
return BasicBlockVisit::Continue;
});
// Process loop blocks in RPO. Just takes one pass through the loop blocks
// as any cyclic contributions are handled by cyclic probabilities.
//
loop->VisitLoopBlocksReversePostOrder([=](BasicBlock* block) {
// Loop head gets external count of 1
//
if (block == loop->GetHeader())
{
JITDUMP("ccp: " FMT_BB " :: 1.0\n", block->bbNum);
block->bbWeight = 1.0;
}
else
{
FlowGraphNaturalLoop* const nestedLoop = m_loops->GetLoopByHeader(block);
if (nestedLoop != nullptr)
{
// We should have figured this out already.
//
assert(m_cyclicProbabilities[nestedLoop->GetIndex()] != 0);
// Sum entry edges, multply by Cp
//
weight_t newWeight = 0.0;
for (FlowEdge* const edge : nestedLoop->EntryEdges())
{
if (BasicBlock::sameHndRegion(block, edge->getSourceBlock()))
{
newWeight += edge->getLikelyWeight();
}
}
newWeight *= m_cyclicProbabilities[nestedLoop->GetIndex()];
block->bbWeight = newWeight;
JITDUMP("ccp (nested header): " FMT_BB " :: " FMT_WT "\n", block->bbNum, newWeight);
}
else
{
weight_t newWeight = 0.0;
for (FlowEdge* const edge : block->PredEdges())
{
if (BasicBlock::sameHndRegion(block, edge->getSourceBlock()))
{
newWeight += edge->getLikelyWeight();
}
}
block->bbWeight = newWeight;
JITDUMP("ccp: " FMT_BB " :: " FMT_WT "\n", block->bbNum, newWeight);
}
}
return BasicBlockVisit::Continue;
});
// Now look at cyclic flow back to the head block.
//
weight_t cyclicWeight = 0;
bool capped = false;
for (FlowEdge* const edge : loop->BackEdges())
{
JITDUMP("ccp backedge " FMT_BB " (" FMT_WT ") -> " FMT_BB " likelihood " FMT_WT "\n",
edge->getSourceBlock()->bbNum, edge->getSourceBlock()->bbWeight, loop->GetHeader()->bbNum,
edge->getLikelihood());
cyclicWeight += edge->getLikelyWeight();
}
// Allow for a bit of rounding error, but not too much.
// (todo: decrease loop gain if we are in a deep nest?)
// assert(cyclicWeight <= 1.01);
//
if (cyclicWeight > cappedLikelihood)
{
JITDUMP("Cyclic weight " FMT_WT " > " FMT_WT "(cap) -- will reduce to cap\n", cyclicWeight, cappedLikelihood);
capped = true;
cyclicWeight = cappedLikelihood;
m_cappedCyclicProbabilities++;
}
// Note this value is not actually a probability; it is the expected
// iteration count of the loop.
//
weight_t const cyclicProbability = 1.0 / (1.0 - cyclicWeight);
JITDUMP("For loop at " FMT_BB " cyclic weight is " FMT_WT " cyclic probability is " FMT_WT "%s\n",
loop->GetHeader()->bbNum, cyclicWeight, cyclicProbability, capped ? " [capped]" : "");
m_cyclicProbabilities[loop->GetIndex()] = cyclicProbability;
// Try and adjust loop exit likelihood to reflect capping.
// If there are multiple exits we just adjust the first one we can. This is somewhat arbitrary.
// If there are no exits, there's nothing we can do.
//
if (capped && (loop->ExitEdges().size() > 0))
{
// Figure out how much flow exits the loop with the capped probability
// and current block frequencies and exit likelihoods.
//
weight_t cappedExitWeight = 0.0;
for (FlowEdge* const exitEdge : loop->ExitEdges())
{
BasicBlock* const exitBlock = exitEdge->getSourceBlock();
weight_t const exitBlockFrequency = exitBlock->bbWeight;
weight_t const exitBlockWeight = exitBlockFrequency * cyclicProbability;
weight_t const exitWeight = exitEdge->getLikelihood() * exitBlockWeight;
cappedExitWeight += exitWeight;
JITDUMP("Exit from " FMT_BB " has weight " FMT_WT "\n", exitBlock->bbNum, exitWeight);
}
JITDUMP("Total exit weight " FMT_WT "\n", cappedExitWeight);
// We should end up with a value less than one since we input one unit of flow into the
// loop and are artificially capping the iteration count of the loop, so less weight is
// now flowing out than in. However because of rounding we might end up near or a bit over 1.0.
//
if ((cappedExitWeight + epsilon) < 1.0)
{
// We want to increase the exit likelihood of one exit block to create
// additional flow out of the loop. Figure out how much we need.
//
weight_t const missingExitWeight = 1.0 - cappedExitWeight;
JITDUMP("Loop exit flow deficit from capping is " FMT_WT "\n", missingExitWeight);
bool adjustedExit = false;
for (FlowEdge* const exitEdge : loop->ExitEdges())
{
// Does this block have enough weight that it can supply all the missing weight?
//
BasicBlock* const exitBlock = exitEdge->getSourceBlock();
weight_t const exitBlockFrequency = exitBlock->bbWeight;
weight_t const exitBlockWeight = exitBlockFrequency * cyclicProbability;
weight_t const currentExitWeight = exitEdge->getLikelihood() * exitBlockWeight;
// TODO: we might also want to exclude edges that are exiting from child loops here,
// or think harder about what might be appropriate in those cases. Seems like we ought
// to adjust an edge's likelihoods at most once.
//
// Currently we don't know which edges do this.
//
if (exitBlock->KindIs(BBJ_COND) && (exitBlockWeight > (missingExitWeight + currentExitWeight)))
{
JITDUMP("Will adjust likelihood of the exit edge from loop exit block " FMT_BB
" to reflect capping; current likelihood is " FMT_WT "\n",
exitBlock->bbNum, exitEdge->getLikelihood());
BasicBlock* const jump = exitBlock->GetTrueTarget();
BasicBlock* const next = exitBlock->GetFalseTarget();
FlowEdge* const jumpEdge = m_comp->fgGetPredForBlock(jump, exitBlock);
FlowEdge* const nextEdge = m_comp->fgGetPredForBlock(next, exitBlock);
weight_t const exitLikelihood = (missingExitWeight + currentExitWeight) / exitBlockWeight;
weight_t const continueLikelihood = 1.0 - exitLikelihood;
// We are making it more likely that the loop exits, so the new exit likelihood
// should be greater than the old.
//
assert(exitLikelihood > exitEdge->getLikelihood());
if (jumpEdge == exitEdge)
{
jumpEdge->setLikelihood(exitLikelihood);
nextEdge->setLikelihood(continueLikelihood);
}
else
{
assert(nextEdge == exitEdge);
jumpEdge->setLikelihood(continueLikelihood);
nextEdge->setLikelihood(exitLikelihood);
}
adjustedExit = true;
JITDUMP("New likelihood is " FMT_WT "\n", exitEdge->getLikelihood());
break;
}
}
if (!adjustedExit)
{
// Possibly we could have fixed things up by adjusting more than one exit?
//
JITDUMP("Unable to find suitable exit to carry off capped flow\n");
}
}
else
{
JITDUMP("Exit weight comparable or above 1.0, leaving as is\n");
}
}
}
//------------------------------------------------------------------------
// fgAssignInputWeights: provide initial profile weights for all blocks
//
// Arguments:
// option - profile synthesis option
//
// Notes:
// For finallys we will pick up new entry weights when we process
// the subtree that can invoke them normally.
//
// Option is used to determine the entry weight, so that the
// absolute values of weights does not change dramatically.
//
// Some parts of the jit are sensitive to the absolute weights.
//
void ProfileSynthesis::AssignInputWeights(ProfileSynthesisOption option)
{
// Determine input weight for entire method.
//
BasicBlock* const entryBlock = m_comp->fgFirstBB;
weight_t entryWeight = BB_UNITY_WEIGHT;
switch (option)
{
case ProfileSynthesisOption::BlendLikelihoods:
case ProfileSynthesisOption::RepairLikelihoods:
{
// Try and retain fgEntryBB's weight.
// Easiest to do when the block has no preds.
//
if (entryBlock->hasProfileWeight())
{
weight_t currentEntryWeight = entryBlock->bbWeight;
if (!Compiler::fgProfileWeightsEqual(currentEntryWeight, 0.0, epsilon))
{
if (entryBlock->bbPreds == nullptr)
{
entryWeight = currentEntryWeight;
}
else
{
// TODO: something similar to how we compute fgCalledCount;
// try and sum return weights?
}
}
else
{
// Entry weight was zero or nearly zero, just use default
}
}
else
{
// Entry was unprofiled, just use default
}
break;
}
default:
break;
}
// Determine input weight for EH regions.
//
const weight_t ehWeight = entryWeight * exceptionScale;
for (BasicBlock* block : m_comp->Blocks())
{
block->setBBProfileWeight(0.0);
}
entryBlock->setBBProfileWeight(entryWeight);
if (!m_comp->compIsForInlining())
{
for (EHblkDsc* const HBtab : EHClauses(m_comp))
{
if (HBtab->HasFilter())
{
HBtab->ebdFilter->setBBProfileWeight(ehWeight);
}
HBtab->ebdHndBeg->setBBProfileWeight(ehWeight);
}
}
}
//------------------------------------------------------------------------