Skip to content

Commit 91669ce

Browse files
authored
Merge pull request #27573 from vedantk/master
2 parents 4055530 + 0cef7b0 commit 91669ce

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

lib/SIL/SILProfiler.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ struct MapRegionCounters : public ASTWalker {
247247
if (skipExpr(E))
248248
return {true, E};
249249

250+
// Profiling for closures should be handled separately. Do not visit
251+
// closure expressions twice.
252+
if (isa<AbstractClosureExpr>(E) && !Parent.isNull())
253+
return {false, E};
254+
250255
// If AST visitation begins with an expression, the counter map must be
251256
// empty. Set up a counter for the root.
252257
if (Parent.isNull()) {
@@ -430,6 +435,20 @@ class SourceMappingRegion {
430435
assert(EndLoc && "Region has no end location");
431436
return *EndLoc;
432437
}
438+
439+
void print(llvm::raw_ostream &OS, const SourceManager &SM) const {
440+
OS << "[";
441+
if (hasStartLoc())
442+
getStartLoc().print(OS, SM);
443+
else
444+
OS << "?";
445+
OS << ", ";
446+
if (hasEndLoc())
447+
getEndLoc().print(OS, SM);
448+
else
449+
OS << "?";
450+
OS << "]";
451+
}
433452
};
434453

435454
/// An ASTWalker that maps ASTNodes to profiling counters.
@@ -591,6 +610,11 @@ struct PGOMapping : public ASTWalker {
591610
if (skipExpr(E))
592611
return {true, E};
593612

613+
// Profiling for closures should be handled separately. Do not visit
614+
// closure expressions twice.
615+
if (isa<AbstractClosureExpr>(E) && !Parent.isNull())
616+
return {false, E};
617+
594618
unsigned parent = getParentCounter();
595619

596620
if (Parent.isNull()) {
@@ -754,6 +778,11 @@ struct CoverageMapping : public ASTWalker {
754778
void pushRegion(ASTNode Node) {
755779
RegionStack.emplace_back(Node, getCounter(Node), Node.getStartLoc(),
756780
getEndLoc(Node));
781+
LLVM_DEBUG({
782+
llvm::dbgs() << "Pushed region: ";
783+
RegionStack.back().print(llvm::dbgs(), SM);
784+
llvm::dbgs() << "\n";
785+
});
757786
}
758787

759788
/// Replace the current region's count by pushing an incomplete region.
@@ -780,6 +809,8 @@ struct CoverageMapping : public ASTWalker {
780809
auto ParentIt = I;
781810
SourceLoc EndLoc = ParentIt->getEndLoc();
782811

812+
unsigned FirstPoppedIndex = SourceRegions.size();
813+
(void)FirstPoppedIndex;
783814
SourceRegions.push_back(std::move(*I++));
784815
for (; I != E; ++I) {
785816
if (!I->hasStartLoc())
@@ -789,6 +820,14 @@ struct CoverageMapping : public ASTWalker {
789820
SourceRegions.push_back(std::move(*I));
790821
}
791822

823+
LLVM_DEBUG({
824+
for (unsigned Idx = FirstPoppedIndex; Idx < SourceRegions.size(); ++Idx) {
825+
llvm::dbgs() << "Popped region: ";
826+
SourceRegions[Idx].print(llvm::dbgs(), SM);
827+
llvm::dbgs() << "\n";
828+
}
829+
});
830+
792831
RegionStack.erase(ParentIt, E);
793832
}
794833

@@ -1017,6 +1056,11 @@ struct CoverageMapping : public ASTWalker {
10171056
if (skipExpr(E))
10181057
return {true, E};
10191058

1059+
// Profiling for closures should be handled separately. Do not visit
1060+
// closure expressions twice.
1061+
if (isa<AbstractClosureExpr>(E) && !Parent.isNull())
1062+
return {false, E};
1063+
10201064
if (!RegionStack.empty())
10211065
extendRegion(E);
10221066

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -profile-generate -profile-coverage-mapping -emit-sil -module-name coverage_empty %s | %FileCheck %s
2+
3+
// Skip the sil prologue, which reproduces the swift source of this file
4+
// and confounds FileCheck.
5+
// CHECK-LABEL: sil @main
6+
7+
func singleDefaultArgument(i: Int = {
8+
// CHECK: sil_coverage_map {{.*}}closure #1 () -> Swift.Int in default argument 0
9+
// CHECK-NEXT: [[@LINE-2]]:37 -> [[@LINE+6]]:2 : 0
10+
// CHECK-NEXT: }
11+
struct SingleDefaultArgumentStruct {
12+
let sdasi: Int
13+
}
14+
return 2
15+
}()) {
16+
// CHECK: sil_coverage_map {{.*}}singleDefaultArgument(i: Swift.Int) -> ()
17+
// CHECK-NEXT: [[@LINE-2]]:6 -> [[@LINE+3]]:2 : 0
18+
// CHECK-NEXT: }
19+
print(i)
20+
}

0 commit comments

Comments
 (0)