Skip to content

Commit e1bb0bb

Browse files
authored
Merge pull request #60621 from hamishknight/pogo
2 parents 595dfdc + 68c80c5 commit e1bb0bb

File tree

1 file changed

+65
-76
lines changed

1 file changed

+65
-76
lines changed

lib/SIL/IR/SILProfiler.cpp

Lines changed: 65 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -463,21 +463,35 @@ class SourceMappingRegion {
463463
};
464464

465465
/// 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.
466470
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;
469473

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.
471478
llvm::DenseMap<ASTNode, ProfileCounter> &LoadedCounterMap;
472-
llvm::Expected<llvm::InstrProfRecord> &LoadedCounts;
473479
llvm::DenseMap<ASTNode, ASTNode> &CondToParentMap;
474-
llvm::DenseMap<ASTNode, unsigned> CounterMap;
475480

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,
478484
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+
}
481495

482496
unsigned getParentCounter() const {
483497
if (Parent.isNull())
@@ -516,50 +530,53 @@ struct PGOMapping : public ASTWalker {
516530
"region does not have an associated counter");
517531

518532
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;
520544
}
521545

522546
bool walkToDeclPre(Decl *D) override {
523547
if (isUnmapped(D))
524548
return false;
525549
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
526550
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());
531552
});
532553
}
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+
539557
return true;
540558
}
541559

542560
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override {
543561
unsigned parent = getParentCounter();
562+
auto parentCount = LoadedCounts.Counts[parent];
544563
if (auto *IS = dyn_cast<IfStmt>(S)) {
545564
auto thenStmt = IS->getThenStmt();
546-
CounterMap[thenStmt] = NextCounter++;
547565
auto thenCount = loadExecutionCount(thenStmt);
548-
LoadedCounterMap[thenStmt] = thenCount;
566+
setExecutionCount(thenStmt, thenCount);
549567
if (auto elseStmt = IS->getElseStmt()) {
550-
CounterMap[elseStmt] = parent;
551-
auto count = loadExecutionCount(elseStmt);
568+
auto count = parentCount;
552569
if (!parent) {
553570
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];
556573
if (cCount > thenVal) {
557574
count = cCount;
558575
break;
559576
}
560577
}
561578
}
562-
LoadedCounterMap[elseStmt] = subtract(count, thenCount);
579+
setExecutionCount(elseStmt, subtract(count, thenCount));
563580
auto Cond = IS->getCond();
564581
for (const auto &elt : Cond) {
565582
if (elt.getKind() ==
@@ -568,47 +585,24 @@ struct PGOMapping : public ASTWalker {
568585
}
569586
}
570587
}
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();
574590
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));
579593
} 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);
587596
} 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);
595599
} 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);
603602
} else if (auto *SS = dyn_cast<SwitchStmt>(S)) {
604-
CounterMap[SS] = NextCounter++;
605-
auto ssCount = loadExecutionCount(SS);
606-
LoadedCounterMap[SS] = ssCount;
603+
setKnownExecutionCount(SS);
607604
} 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));
612606
}
613607
return {true, S};
614608
}
@@ -624,32 +618,27 @@ struct PGOMapping : public ASTWalker {
624618

625619
unsigned parent = getParentCounter();
626620

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);
632623

633624
if (auto *IE = dyn_cast<IfExpr>(E)) {
634625
auto thenExpr = IE->getThenExpr();
635-
CounterMap[thenExpr] = NextCounter++;
636626
auto thenCount = loadExecutionCount(thenExpr);
637-
LoadedCounterMap[thenExpr] = thenCount;
627+
setExecutionCount(thenExpr, thenCount);
638628
auto elseExpr = IE->getElseExpr();
639629
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];
642631
if (!parent) {
643632
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];
646635
if (cCount > thenVal) {
647636
count = cCount;
648637
break;
649638
}
650639
}
651640
}
652-
LoadedCounterMap[elseExpr] = subtract(count, thenCount);
641+
setExecutionCount(elseExpr, subtract(count, thenCount));
653642
}
654643
return {true, E};
655644
}
@@ -1186,8 +1175,8 @@ void SILProfiler::assignRegionCounters() {
11861175
llvm::dbgs() << PGOFuncName << "\n";
11871176
return;
11881177
}
1189-
PGOMapping pgoMapper(RegionLoadedCounterMap, LoadedCounts,
1190-
RegionCondToParentMap);
1178+
PGOMapping pgoMapper(RegionCounterMap, LoadedCounts.get(),
1179+
RegionLoadedCounterMap, RegionCondToParentMap);
11911180
Root.walk(pgoMapper);
11921181
}
11931182
}

0 commit comments

Comments
 (0)