Skip to content

Commit 77fca82

Browse files
committed
[Coverage] Check decl location for generated source
Previously for a property initializer, we would check whether the initialization expr is generated code, and skip profiling the property in that case. However if the property itself isn't generated, profiling is still reasonable. Switch the check here to look at the SILDeclRef's location.
1 parent de1d139 commit 77fca82

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/SIL/IR/SILProfiler.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,18 @@ static bool shouldProfile(SILDeclRef Constant) {
9595
<< "Skipping ASTNode: invalid start/end locations\n");
9696
return false;
9797
}
98+
}
9899

99-
// Do not profile generated code. This includes macro expansions, which we
100-
// otherwise consider to be "written by the user", because they wrote the
101-
// macro attribute or expr. We may want to revist this in the future. We'll
102-
// need to figure out how we'll be writing out the macro expansions though,
103-
// such that they can be referenced by llvm-cov.
104-
// Note we check `getSourceFileContainingLocation` instead of
105-
// `getParentSourceFile` to make sure initializer exprs are correctly
106-
// handled.
100+
// Do not profile generated code. This includes macro expansions, which we
101+
// otherwise consider to be "written by the user", because they wrote the
102+
// macro attribute or expr. We may want to revist this in the future. We'll
103+
// need to figure out how we'll be writing out the macro expansions though,
104+
// such that they can be referenced by llvm-cov.
105+
// Note we check `getSourceFileContainingLocation` instead of
106+
// `getParentSourceFile` to make sure properties are correctly handled.
107+
if (auto Loc = Constant.getAsRegularLocation()) {
107108
auto *M = DC->getParentModule();
108-
if (auto *SF = M->getSourceFileContainingLocation(N.getStartLoc())) {
109+
if (auto *SF = M->getSourceFileContainingLocation(Loc.getSourceLoc())) {
109110
auto &SM = M->getASTContext().SourceMgr;
110111
if (SM.hasGeneratedSourceInfo(*SF->getBufferID())) {
111112
LLVM_DEBUG(llvm::dbgs() << "Skipping ASTNode: generated code\n");

test/Profiler/coverage_macros.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ macro BodyMacroWithControlFlow() = #externalMacro(module: "MacroDefinition", typ
3434
@freestanding(declaration, names: arbitrary)
3535
macro declMacroWithControlFlow() = #externalMacro(module: "MacroDefinition", type: "DeclMacroWithControlFlow")
3636

37+
// This needs to be matched up here due to the sorting of the SIL; just make
38+
// sure the counter '2' is for the initialization.
39+
// CHECK-LABEL: sil hidden [lazy_getter] [noinline] @$s15coverage_macros2S1V2z3SiSgvg : $@convention(method) (@inout S1) -> Optional<Int>
40+
// CHECK: switch_enum {{%[0-9]+}} : $Optional<Optional<Int>>, case #Optional.some!enumelt: {{bb[0-9]}}, case #Optional.none!enumelt: [[INITBB:bb[0-9]]]
41+
// CHECK: [[INITBB]]
42+
// CHECK-NEXT: increment_profiler_counter 2
43+
3744
// This needs to be matched up here due to the sorting of the SIL; just make
3845
// sure we emit the counter increments for the error branches.
3946
// CHECK-LABEL: sil hidden @$s15coverage_macros5test2Si_SityKF
@@ -56,9 +63,18 @@ struct S1 {
5663
// CHECK-LABEL: sil_coverage_map{{.*}}variable initialization expression of coverage_macros.S1.y
5764
var y: Int = 0
5865

59-
// FIXME: This shouldn't require parens
6066
// CHECK-LABEL: sil_coverage_map{{.*}}variable initialization expression of coverage_macros.S1.z
61-
var z: Int = (#id(.random() ? 3 : 4)) // CHECK-NEXT: [[@LINE]]:16 -> [[@LINE]]:40 : 0
67+
var z: Int = #id(.random() ? 3 : 4) // CHECK-NEXT: [[@LINE]]:16 -> [[@LINE]]:38 : 0
68+
69+
// CHECK-LABEL: sil_coverage_map{{.*}}variable initialization expression of coverage_macros.S1.z1
70+
var z1: Int? = try? #id(throwingFn()) // CHECK-NEXT: [[@LINE]]:18 -> [[@LINE]]:40 : 0
71+
72+
// CHECK-LABEL: sil_coverage_map{{.*}}variable initialization expression of coverage_macros.S1.z2
73+
var z2: Int? = #id(try? throwingFn()) // CHECK-NEXT: [[@LINE]]:18 -> [[@LINE]]:40 : 0
74+
75+
// The counter for the initialization region is '2', see the match of s15coverage_macros2S1V2z3SiSgvg above.
76+
// CHECK-LABEL: sil_coverage_map{{.*}}coverage_macros.S1.z3.getter
77+
lazy var z3: Int? = #id(try? throwingFn()) // CHECK-NEXT: [[@LINE]]:23 -> [[@LINE]]:45 : 2
6278
}
6379

6480
@addMembers

0 commit comments

Comments
 (0)