@@ -463,25 +463,36 @@ class SourceMappingRegion {
463
463
};
464
464
465
465
// / An ASTWalker that maps ASTNodes to profiling counters.
466
+ // /
467
+ // / TODO: We ought to be able to leverage the CounterExprs from the
468
+ // / CoverageMapping walker to recompute the correct counter information
469
+ // / for this walker.
466
470
struct PGOMapping : public ASTWalker {
467
- // / The next counter value to assign .
468
- unsigned NextCounter ;
471
+ // / The counter indices for AST nodes .
472
+ const llvm::DenseMap<ASTNode, unsigned > &CounterMap ;
469
473
470
474
// / The loaded counter data.
471
475
const llvm::InstrProfRecord &LoadedCounts;
472
476
473
- // / The map of statements to counters.
477
+ // / The output map of statements to counters.
474
478
llvm::DenseMap<ASTNode, ProfileCounter> &LoadedCounterMap;
475
479
llvm::DenseMap<ASTNode, ASTNode> &CondToParentMap;
476
- llvm::DenseMap<ASTNode, unsigned > CounterMap;
477
480
478
- PGOMapping (llvm::DenseMap<ASTNode, ProfileCounter > &LoadedCounterMap ,
481
+ PGOMapping (const llvm::DenseMap<ASTNode, unsigned > &CounterMap ,
479
482
const llvm::InstrProfRecord &LoadedCounts,
483
+ llvm::DenseMap<ASTNode, ProfileCounter> &LoadedCounterMap,
480
484
llvm::DenseMap<ASTNode, ASTNode> &RegionCondToParentMap)
481
- : NextCounter( 0 ), LoadedCounts(LoadedCounts),
485
+ : CounterMap(CounterMap ), LoadedCounts(LoadedCounts),
482
486
LoadedCounterMap (LoadedCounterMap),
483
487
CondToParentMap(RegionCondToParentMap) {}
484
488
489
+ // / Retrieve the counter index for a leaf node.
490
+ unsigned getCounterIndex (ASTNode Node) const {
491
+ auto result = CounterMap.find (Node);
492
+ assert (result != CounterMap.end () && " Unmapped node?" );
493
+ return result->second ;
494
+ }
495
+
485
496
unsigned getParentCounter () const {
486
497
if (Parent.isNull ())
487
498
return 0 ;
@@ -522,47 +533,50 @@ struct PGOMapping : public ASTWalker {
522
533
return LoadedCounts.Counts [CounterIndexForFunc];
523
534
}
524
535
536
+ // / Record the execution count for a leaf node.
537
+ void setKnownExecutionCount (ASTNode Node) {
538
+ LoadedCounterMap[Node] = loadExecutionCount (Node);
539
+ }
540
+
541
+ // / Record a computed execution count for a node.
542
+ void setExecutionCount (ASTNode Node, ProfileCounter count) {
543
+ LoadedCounterMap[Node] = count;
544
+ }
545
+
525
546
bool walkToDeclPre (Decl *D) override {
526
547
if (isUnmapped (D))
527
548
return false ;
528
549
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
529
550
return visitFunctionDecl (*this , AFD, [&] {
530
- auto node = AFD->getBody ();
531
- CounterMap[node] = NextCounter++;
532
- auto count = loadExecutionCount (node);
533
- LoadedCounterMap[node] = count;
551
+ setKnownExecutionCount (AFD->getBody ());
534
552
});
535
553
}
536
- if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
537
- auto node = TLCD->getBody ();
538
- CounterMap[node] = NextCounter++;
539
- auto count = loadExecutionCount (node);
540
- LoadedCounterMap[node] = count;
541
- }
554
+ if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D))
555
+ setKnownExecutionCount (TLCD->getBody ());
556
+
542
557
return true ;
543
558
}
544
559
545
560
std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override {
546
561
unsigned parent = getParentCounter ();
562
+ auto parentCount = LoadedCounts.Counts [parent];
547
563
if (auto *IS = dyn_cast<IfStmt>(S)) {
548
564
auto thenStmt = IS->getThenStmt ();
549
- CounterMap[thenStmt] = NextCounter++;
550
565
auto thenCount = loadExecutionCount (thenStmt);
551
- LoadedCounterMap[ thenStmt] = thenCount;
566
+ setExecutionCount ( thenStmt, thenCount) ;
552
567
if (auto elseStmt = IS->getElseStmt ()) {
553
- CounterMap[elseStmt] = parent;
554
- auto count = loadExecutionCount (elseStmt);
568
+ auto count = parentCount;
555
569
if (!parent) {
556
570
auto thenVal = thenCount.getValue ();
557
- for (auto pCount = NextCounter - 1 ; pCount > 0 ; --pCount) {
571
+ for (auto pCount = getCounterIndex (thenStmt) ; pCount > 0 ; --pCount) {
558
572
auto cCount = LoadedCounts.Counts [pCount];
559
573
if (cCount > thenVal) {
560
574
count = cCount;
561
575
break ;
562
576
}
563
577
}
564
578
}
565
- LoadedCounterMap[ elseStmt] = subtract (count, thenCount);
579
+ setExecutionCount ( elseStmt, subtract (count, thenCount) );
566
580
auto Cond = IS->getCond ();
567
581
for (const auto &elt : Cond) {
568
582
if (elt.getKind () ==
@@ -571,47 +585,24 @@ struct PGOMapping : public ASTWalker {
571
585
}
572
586
}
573
587
}
574
- } else if (auto *US = dyn_cast<GuardStmt>(S)) {
575
- auto guardBody = US->getBody ();
576
- CounterMap[guardBody] = NextCounter++;
588
+ } else if (auto *GS = dyn_cast<GuardStmt>(S)) {
589
+ auto guardBody = GS->getBody ();
577
590
auto guardCount = loadExecutionCount (guardBody);
578
- LoadedCounterMap[guardBody] = guardCount;
579
- CounterMap[US] = parent;
580
- auto count = loadExecutionCount (US);
581
- LoadedCounterMap[US] = subtract (count, guardCount);
591
+ setExecutionCount (guardBody, guardCount);
592
+ setExecutionCount (GS, subtract (parentCount, guardCount));
582
593
} else if (auto *WS = dyn_cast<WhileStmt>(S)) {
583
- auto whileBody = WS->getBody ();
584
- CounterMap[whileBody] = NextCounter++;
585
- auto whileCount = loadExecutionCount (whileBody);
586
- LoadedCounterMap[whileBody] = whileCount;
587
- CounterMap[WS] = parent;
588
- auto count = loadExecutionCount (WS);
589
- LoadedCounterMap[WS] = count;
594
+ setKnownExecutionCount (WS->getBody ());
595
+ setExecutionCount (WS, parentCount);
590
596
} else if (auto *RWS = dyn_cast<RepeatWhileStmt>(S)) {
591
- auto rwsBody = RWS->getBody ();
592
- CounterMap[rwsBody] = NextCounter++;
593
- auto rwsBodyCount = loadExecutionCount (rwsBody);
594
- LoadedCounterMap[rwsBody] = rwsBodyCount;
595
- CounterMap[RWS] = parent;
596
- auto count = loadExecutionCount (RWS);
597
- LoadedCounterMap[RWS] = count;
597
+ setKnownExecutionCount (RWS->getBody ());
598
+ setExecutionCount (RWS, parentCount);
598
599
} else if (auto *FES = dyn_cast<ForEachStmt>(S)) {
599
- auto fesBody = FES->getBody ();
600
- CounterMap[fesBody] = NextCounter++;
601
- auto fesCount = loadExecutionCount (fesBody);
602
- LoadedCounterMap[fesBody] = fesCount;
603
- CounterMap[FES] = parent;
604
- auto count = loadExecutionCount (FES);
605
- LoadedCounterMap[FES] = count;
600
+ setKnownExecutionCount (FES->getBody ());
601
+ setExecutionCount (FES, parentCount);
606
602
} else if (auto *SS = dyn_cast<SwitchStmt>(S)) {
607
- CounterMap[SS] = NextCounter++;
608
- auto ssCount = loadExecutionCount (SS);
609
- LoadedCounterMap[SS] = ssCount;
603
+ setKnownExecutionCount (SS);
610
604
} else if (auto *CS = dyn_cast<CaseStmt>(S)) {
611
- auto stmt = getProfilerStmtForCase (CS);
612
- CounterMap[stmt] = NextCounter++;
613
- auto csCount = loadExecutionCount (stmt);
614
- LoadedCounterMap[stmt] = csCount;
605
+ setKnownExecutionCount (getProfilerStmtForCase (CS));
615
606
}
616
607
return {true , S};
617
608
}
@@ -627,32 +618,27 @@ struct PGOMapping : public ASTWalker {
627
618
628
619
unsigned parent = getParentCounter ();
629
620
630
- if (Parent.isNull ()) {
631
- CounterMap[E] = NextCounter++;
632
- auto eCount = loadExecutionCount (E);
633
- LoadedCounterMap[E] = eCount;
634
- }
621
+ if (Parent.isNull ())
622
+ setKnownExecutionCount (E);
635
623
636
624
if (auto *IE = dyn_cast<IfExpr>(E)) {
637
625
auto thenExpr = IE->getThenExpr ();
638
- CounterMap[thenExpr] = NextCounter++;
639
626
auto thenCount = loadExecutionCount (thenExpr);
640
- LoadedCounterMap[ thenExpr] = thenCount;
627
+ setExecutionCount ( thenExpr, thenCount) ;
641
628
auto elseExpr = IE->getElseExpr ();
642
629
assert (elseExpr && " An if-expr must have an else subexpression" );
643
- CounterMap[elseExpr] = parent;
644
- auto count = loadExecutionCount (elseExpr);
630
+ auto count = LoadedCounts.Counts [parent];
645
631
if (!parent) {
646
632
auto thenVal = thenCount.getValue ();
647
- for (auto pCount = NextCounter - 1 ; pCount > 0 ; --pCount) {
633
+ for (auto pCount = getCounterIndex (thenExpr) ; pCount > 0 ; --pCount) {
648
634
auto cCount = LoadedCounts.Counts [pCount];
649
635
if (cCount > thenVal) {
650
636
count = cCount;
651
637
break ;
652
638
}
653
639
}
654
640
}
655
- LoadedCounterMap[ elseExpr] = subtract (count, thenCount);
641
+ setExecutionCount ( elseExpr, subtract (count, thenCount) );
656
642
}
657
643
return {true , E};
658
644
}
@@ -1189,8 +1175,8 @@ void SILProfiler::assignRegionCounters() {
1189
1175
llvm::dbgs () << PGOFuncName << " \n " ;
1190
1176
return ;
1191
1177
}
1192
- PGOMapping pgoMapper (RegionLoadedCounterMap , LoadedCounts.get (),
1193
- RegionCondToParentMap);
1178
+ PGOMapping pgoMapper (RegionCounterMap , LoadedCounts.get (),
1179
+ RegionLoadedCounterMap, RegionCondToParentMap);
1194
1180
Root.walk (pgoMapper);
1195
1181
}
1196
1182
}
0 commit comments