@@ -463,21 +463,35 @@ 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
- // / The map of statements to counters.
474
+ // / The loaded counter data.
475
+ const llvm::InstrProfRecord &LoadedCounts;
476
+
477
+ // / The output map of statements to counters.
471
478
llvm::DenseMap<ASTNode, ProfileCounter> &LoadedCounterMap;
472
- llvm::Expected<llvm::InstrProfRecord> &LoadedCounts;
473
479
llvm::DenseMap<ASTNode, ASTNode> &CondToParentMap;
474
- llvm::DenseMap<ASTNode, unsigned > CounterMap;
475
480
476
- PGOMapping (llvm::DenseMap<ASTNode, ProfileCounter> &LoadedCounterMap,
477
- llvm::Expected<llvm::InstrProfRecord> &LoadedCounts,
481
+ PGOMapping (const llvm::DenseMap<ASTNode, unsigned > &CounterMap,
482
+ const llvm::InstrProfRecord &LoadedCounts,
483
+ llvm::DenseMap<ASTNode, ProfileCounter> &LoadedCounterMap,
478
484
llvm::DenseMap<ASTNode, ASTNode> &RegionCondToParentMap)
479
- : NextCounter(0 ), LoadedCounterMap(LoadedCounterMap),
480
- LoadedCounts (LoadedCounts), CondToParentMap(RegionCondToParentMap) {}
485
+ : CounterMap(CounterMap), LoadedCounts(LoadedCounts),
486
+ LoadedCounterMap (LoadedCounterMap),
487
+ CondToParentMap(RegionCondToParentMap) {}
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
+ }
481
495
482
496
unsigned getParentCounter () const {
483
497
if (Parent.isNull ())
@@ -516,50 +530,53 @@ struct PGOMapping : public ASTWalker {
516
530
" region does not have an associated counter" );
517
531
518
532
unsigned CounterIndexForFunc = CounterIt->second ;
519
- return LoadedCounts->Counts [CounterIndexForFunc];
533
+ return LoadedCounts.Counts [CounterIndexForFunc];
534
+ }
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;
520
544
}
521
545
522
546
bool walkToDeclPre (Decl *D) override {
523
547
if (isUnmapped (D))
524
548
return false ;
525
549
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
526
550
return visitFunctionDecl (*this , AFD, [&] {
527
- auto node = AFD->getBody ();
528
- CounterMap[node] = NextCounter++;
529
- auto count = loadExecutionCount (node);
530
- LoadedCounterMap[node] = count;
551
+ setKnownExecutionCount (AFD->getBody ());
531
552
});
532
553
}
533
- if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
534
- auto node = TLCD->getBody ();
535
- CounterMap[node] = NextCounter++;
536
- auto count = loadExecutionCount (node);
537
- LoadedCounterMap[node] = count;
538
- }
554
+ if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D))
555
+ setKnownExecutionCount (TLCD->getBody ());
556
+
539
557
return true ;
540
558
}
541
559
542
560
std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override {
543
561
unsigned parent = getParentCounter ();
562
+ auto parentCount = LoadedCounts.Counts [parent];
544
563
if (auto *IS = dyn_cast<IfStmt>(S)) {
545
564
auto thenStmt = IS->getThenStmt ();
546
- CounterMap[thenStmt] = NextCounter++;
547
565
auto thenCount = loadExecutionCount (thenStmt);
548
- LoadedCounterMap[ thenStmt] = thenCount;
566
+ setExecutionCount ( thenStmt, thenCount) ;
549
567
if (auto elseStmt = IS->getElseStmt ()) {
550
- CounterMap[elseStmt] = parent;
551
- auto count = loadExecutionCount (elseStmt);
568
+ auto count = parentCount;
552
569
if (!parent) {
553
570
auto thenVal = thenCount.getValue ();
554
- for (auto pCount = NextCounter - 1 ; pCount > 0 ; --pCount) {
555
- auto cCount = LoadedCounts-> Counts [pCount];
571
+ for (auto pCount = getCounterIndex (thenStmt) ; pCount > 0 ; --pCount) {
572
+ auto cCount = LoadedCounts. Counts [pCount];
556
573
if (cCount > thenVal) {
557
574
count = cCount;
558
575
break ;
559
576
}
560
577
}
561
578
}
562
- LoadedCounterMap[ elseStmt] = subtract (count, thenCount);
579
+ setExecutionCount ( elseStmt, subtract (count, thenCount) );
563
580
auto Cond = IS->getCond ();
564
581
for (const auto &elt : Cond) {
565
582
if (elt.getKind () ==
@@ -568,47 +585,24 @@ struct PGOMapping : public ASTWalker {
568
585
}
569
586
}
570
587
}
571
- } else if (auto *US = dyn_cast<GuardStmt>(S)) {
572
- auto guardBody = US->getBody ();
573
- CounterMap[guardBody] = NextCounter++;
588
+ } else if (auto *GS = dyn_cast<GuardStmt>(S)) {
589
+ auto guardBody = GS->getBody ();
574
590
auto guardCount = loadExecutionCount (guardBody);
575
- LoadedCounterMap[guardBody] = guardCount;
576
- CounterMap[US] = parent;
577
- auto count = loadExecutionCount (US);
578
- LoadedCounterMap[US] = subtract (count, guardCount);
591
+ setExecutionCount (guardBody, guardCount);
592
+ setExecutionCount (GS, subtract (parentCount, guardCount));
579
593
} else if (auto *WS = dyn_cast<WhileStmt>(S)) {
580
- auto whileBody = WS->getBody ();
581
- CounterMap[whileBody] = NextCounter++;
582
- auto whileCount = loadExecutionCount (whileBody);
583
- LoadedCounterMap[whileBody] = whileCount;
584
- CounterMap[WS] = parent;
585
- auto count = loadExecutionCount (WS);
586
- LoadedCounterMap[WS] = count;
594
+ setKnownExecutionCount (WS->getBody ());
595
+ setExecutionCount (WS, parentCount);
587
596
} else if (auto *RWS = dyn_cast<RepeatWhileStmt>(S)) {
588
- auto rwsBody = RWS->getBody ();
589
- CounterMap[rwsBody] = NextCounter++;
590
- auto rwsBodyCount = loadExecutionCount (rwsBody);
591
- LoadedCounterMap[rwsBody] = rwsBodyCount;
592
- CounterMap[RWS] = parent;
593
- auto count = loadExecutionCount (RWS);
594
- LoadedCounterMap[RWS] = count;
597
+ setKnownExecutionCount (RWS->getBody ());
598
+ setExecutionCount (RWS, parentCount);
595
599
} else if (auto *FES = dyn_cast<ForEachStmt>(S)) {
596
- auto fesBody = FES->getBody ();
597
- CounterMap[fesBody] = NextCounter++;
598
- auto fesCount = loadExecutionCount (fesBody);
599
- LoadedCounterMap[fesBody] = fesCount;
600
- CounterMap[FES] = parent;
601
- auto count = loadExecutionCount (FES);
602
- LoadedCounterMap[FES] = count;
600
+ setKnownExecutionCount (FES->getBody ());
601
+ setExecutionCount (FES, parentCount);
603
602
} else if (auto *SS = dyn_cast<SwitchStmt>(S)) {
604
- CounterMap[SS] = NextCounter++;
605
- auto ssCount = loadExecutionCount (SS);
606
- LoadedCounterMap[SS] = ssCount;
603
+ setKnownExecutionCount (SS);
607
604
} else if (auto *CS = dyn_cast<CaseStmt>(S)) {
608
- auto stmt = getProfilerStmtForCase (CS);
609
- CounterMap[stmt] = NextCounter++;
610
- auto csCount = loadExecutionCount (stmt);
611
- LoadedCounterMap[stmt] = csCount;
605
+ setKnownExecutionCount (getProfilerStmtForCase (CS));
612
606
}
613
607
return {true , S};
614
608
}
@@ -624,32 +618,27 @@ struct PGOMapping : public ASTWalker {
624
618
625
619
unsigned parent = getParentCounter ();
626
620
627
- if (Parent.isNull ()) {
628
- CounterMap[E] = NextCounter++;
629
- auto eCount = loadExecutionCount (E);
630
- LoadedCounterMap[E] = eCount;
631
- }
621
+ if (Parent.isNull ())
622
+ setKnownExecutionCount (E);
632
623
633
624
if (auto *IE = dyn_cast<IfExpr>(E)) {
634
625
auto thenExpr = IE->getThenExpr ();
635
- CounterMap[thenExpr] = NextCounter++;
636
626
auto thenCount = loadExecutionCount (thenExpr);
637
- LoadedCounterMap[ thenExpr] = thenCount;
627
+ setExecutionCount ( thenExpr, thenCount) ;
638
628
auto elseExpr = IE->getElseExpr ();
639
629
assert (elseExpr && " An if-expr must have an else subexpression" );
640
- CounterMap[elseExpr] = parent;
641
- auto count = loadExecutionCount (elseExpr);
630
+ auto count = LoadedCounts.Counts [parent];
642
631
if (!parent) {
643
632
auto thenVal = thenCount.getValue ();
644
- for (auto pCount = NextCounter - 1 ; pCount > 0 ; --pCount) {
645
- auto cCount = LoadedCounts-> Counts [pCount];
633
+ for (auto pCount = getCounterIndex (thenExpr) ; pCount > 0 ; --pCount) {
634
+ auto cCount = LoadedCounts. Counts [pCount];
646
635
if (cCount > thenVal) {
647
636
count = cCount;
648
637
break ;
649
638
}
650
639
}
651
640
}
652
- LoadedCounterMap[ elseExpr] = subtract (count, thenCount);
641
+ setExecutionCount ( elseExpr, subtract (count, thenCount) );
653
642
}
654
643
return {true , E};
655
644
}
@@ -1186,8 +1175,8 @@ void SILProfiler::assignRegionCounters() {
1186
1175
llvm::dbgs () << PGOFuncName << " \n " ;
1187
1176
return ;
1188
1177
}
1189
- PGOMapping pgoMapper (RegionLoadedCounterMap , LoadedCounts,
1190
- RegionCondToParentMap);
1178
+ PGOMapping pgoMapper (RegionCounterMap , LoadedCounts. get () ,
1179
+ RegionLoadedCounterMap, RegionCondToParentMap);
1191
1180
Root.walk (pgoMapper);
1192
1181
}
1193
1182
}
0 commit comments