@@ -463,25 +463,36 @@ 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.
466470struct 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
470474 // / The loaded counter data.
471475 const llvm::InstrProfRecord &LoadedCounts;
472476
473- // / The map of statements to counters.
477+ // / The output map of statements to counters.
474478 llvm::DenseMap<ASTNode, ProfileCounter> &LoadedCounterMap;
475479 llvm::DenseMap<ASTNode, ASTNode> &CondToParentMap;
476- llvm::DenseMap<ASTNode, unsigned > CounterMap;
477480
478- PGOMapping (llvm::DenseMap<ASTNode, ProfileCounter > &LoadedCounterMap ,
481+ PGOMapping (const llvm::DenseMap<ASTNode, unsigned > &CounterMap ,
479482 const llvm::InstrProfRecord &LoadedCounts,
483+ llvm::DenseMap<ASTNode, ProfileCounter> &LoadedCounterMap,
480484 llvm::DenseMap<ASTNode, ASTNode> &RegionCondToParentMap)
481- : NextCounter( 0 ), LoadedCounts(LoadedCounts),
485+ : CounterMap(CounterMap ), LoadedCounts(LoadedCounts),
482486 LoadedCounterMap (LoadedCounterMap),
483487 CondToParentMap(RegionCondToParentMap) {}
484488
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+
485496 unsigned getParentCounter () const {
486497 if (Parent.isNull ())
487498 return 0 ;
@@ -522,47 +533,50 @@ struct PGOMapping : public ASTWalker {
522533 return LoadedCounts.Counts [CounterIndexForFunc];
523534 }
524535
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+
525546 bool walkToDeclPre (Decl *D) override {
526547 if (isUnmapped (D))
527548 return false ;
528549 if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
529550 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 ());
534552 });
535553 }
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+
542557 return true ;
543558 }
544559
545560 std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override {
546561 unsigned parent = getParentCounter ();
562+ auto parentCount = LoadedCounts.Counts [parent];
547563 if (auto *IS = dyn_cast<IfStmt>(S)) {
548564 auto thenStmt = IS->getThenStmt ();
549- CounterMap[thenStmt] = NextCounter++;
550565 auto thenCount = loadExecutionCount (thenStmt);
551- LoadedCounterMap[ thenStmt] = thenCount;
566+ setExecutionCount ( thenStmt, thenCount) ;
552567 if (auto elseStmt = IS->getElseStmt ()) {
553- CounterMap[elseStmt] = parent;
554- auto count = loadExecutionCount (elseStmt);
568+ auto count = parentCount;
555569 if (!parent) {
556570 auto thenVal = thenCount.getValue ();
557- for (auto pCount = NextCounter - 1 ; pCount > 0 ; --pCount) {
571+ for (auto pCount = getCounterIndex (thenStmt) ; pCount > 0 ; --pCount) {
558572 auto cCount = LoadedCounts.Counts [pCount];
559573 if (cCount > thenVal) {
560574 count = cCount;
561575 break ;
562576 }
563577 }
564578 }
565- LoadedCounterMap[ elseStmt] = subtract (count, thenCount);
579+ setExecutionCount ( elseStmt, subtract (count, thenCount) );
566580 auto Cond = IS->getCond ();
567581 for (const auto &elt : Cond) {
568582 if (elt.getKind () ==
@@ -571,47 +585,24 @@ struct PGOMapping : public ASTWalker {
571585 }
572586 }
573587 }
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 ();
577590 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));
582593 } 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);
590596 } 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);
598599 } 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);
606602 } else if (auto *SS = dyn_cast<SwitchStmt>(S)) {
607- CounterMap[SS] = NextCounter++;
608- auto ssCount = loadExecutionCount (SS);
609- LoadedCounterMap[SS] = ssCount;
603+ setKnownExecutionCount (SS);
610604 } 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));
615606 }
616607 return {true , S};
617608 }
@@ -627,32 +618,27 @@ struct PGOMapping : public ASTWalker {
627618
628619 unsigned parent = getParentCounter ();
629620
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);
635623
636624 if (auto *IE = dyn_cast<IfExpr>(E)) {
637625 auto thenExpr = IE->getThenExpr ();
638- CounterMap[thenExpr] = NextCounter++;
639626 auto thenCount = loadExecutionCount (thenExpr);
640- LoadedCounterMap[ thenExpr] = thenCount;
627+ setExecutionCount ( thenExpr, thenCount) ;
641628 auto elseExpr = IE->getElseExpr ();
642629 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];
645631 if (!parent) {
646632 auto thenVal = thenCount.getValue ();
647- for (auto pCount = NextCounter - 1 ; pCount > 0 ; --pCount) {
633+ for (auto pCount = getCounterIndex (thenExpr) ; pCount > 0 ; --pCount) {
648634 auto cCount = LoadedCounts.Counts [pCount];
649635 if (cCount > thenVal) {
650636 count = cCount;
651637 break ;
652638 }
653639 }
654640 }
655- LoadedCounterMap[ elseExpr] = subtract (count, thenCount);
641+ setExecutionCount ( elseExpr, subtract (count, thenCount) );
656642 }
657643 return {true , E};
658644 }
@@ -1189,8 +1175,8 @@ void SILProfiler::assignRegionCounters() {
11891175 llvm::dbgs () << PGOFuncName << " \n " ;
11901176 return ;
11911177 }
1192- PGOMapping pgoMapper (RegionLoadedCounterMap , LoadedCounts.get (),
1193- RegionCondToParentMap);
1178+ PGOMapping pgoMapper (RegionCounterMap , LoadedCounts.get (),
1179+ RegionLoadedCounterMap, RegionCondToParentMap);
11941180 Root.walk (pgoMapper);
11951181 }
11961182}
0 commit comments