Skip to content

[Coverage] Do not emit increments in certain implicitly-generated reg… #2334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction

/// Emit code to increment a counter for profiling.
void emitProfilerIncrement(ASTNode N) {
if (SGM.Profiler)
if (SGM.Profiler && SGM.Profiler->hasRegionCounters())
SGM.Profiler->emitCounterIncrement(B, N);
}

Expand Down
17 changes: 13 additions & 4 deletions lib/SILGen/SILGenProfiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@
using namespace swift;
using namespace Lowering;

static bool isUnmappedDecl(Decl *D) {
if (isa<ConstructorDecl>(D) || isa<DestructorDecl>(D))
return false;

return D->isImplicit() || isa<EnumCaseDecl>(D);
}

ProfilerRAII::ProfilerRAII(SILGenModule &SGM, AbstractFunctionDecl *D)
: SGM(SGM) {
: SGM(SGM), PreviousProfiler(std::move(SGM.Profiler)) {
const auto &Opts = SGM.M.getOptions();
if (!Opts.GenerateProfile)
if (!Opts.GenerateProfile || isUnmappedDecl(D))
return;
SGM.Profiler =
llvm::make_unique<SILGenProfiling>(SGM, Opts.EmitProfileCoverageMapping);
SGM.Profiler->assignRegionCounters(D);
}

ProfilerRAII::~ProfilerRAII() { SGM.Profiler = nullptr; }
ProfilerRAII::~ProfilerRAII() { SGM.Profiler = std::move(PreviousProfiler); }

namespace {

Expand All @@ -55,6 +62,8 @@ struct MapRegionCounters : public ASTWalker {
: NextCounter(0), CounterMap(CounterMap) {}

bool walkToDeclPre(Decl *D) override {
if (isUnmappedDecl(D))
return false;
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D))
CounterMap[AFD->getBody()] = NextCounter++;
return true;
Expand Down Expand Up @@ -435,7 +444,7 @@ struct CoverageMapping : public ASTWalker {
}

bool walkToDeclPre(Decl *D) override {
if (D->isImplicit())
if (isUnmappedDecl(D))
return false;
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D))
assignCounter(AFD->getBody());
Expand Down
23 changes: 15 additions & 8 deletions lib/SILGen/SILGenProfiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ namespace Lowering {
class SILGenModule;
class SILGenBuilder;

/// RAII object to set up profiling for a function.
struct ProfilerRAII {
SILGenModule &SGM;
ProfilerRAII(SILGenModule &SGM, AbstractFunctionDecl *D);
~ProfilerRAII();
};
struct ProfilerRAII;

/// Profiling state.
class SILGenProfiling {
Expand All @@ -57,11 +52,23 @@ class SILGenProfiling {

bool hasRegionCounters() const { return NumRegionCounters != 0; }

/// Emit SIL to increment the counter for \c Node.
void emitCounterIncrement(SILGenBuilder &Builder, ASTNode Node);

private:
/// Map counters to ASTNodes and set them up for profiling the given function.
void assignRegionCounters(AbstractFunctionDecl *Root);

/// Emit SIL to increment the counter for \c Node.
void emitCounterIncrement(SILGenBuilder &Builder, ASTNode Node);
friend struct ProfilerRAII;
};

/// RAII object to set up profiling for a function.
struct ProfilerRAII {
SILGenModule &SGM;
std::unique_ptr<SILGenProfiling> PreviousProfiler;

ProfilerRAII(SILGenModule &SGM, AbstractFunctionDecl *D);
~ProfilerRAII();
};

} // end namespace Lowering
Expand Down
11 changes: 11 additions & 0 deletions test/SILGen/coverage_smoke.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ func main() {
let _ = Class1()
}

// rdar://problem/22761498 - enum declaration suppresses coverage
func foo() {
var x : Int32 = 0 // CHECK-COV: 1|{{.*}}[[@LINE]]
enum ETy { case A } // CHECK-COV: 1|{{.*}}[[@LINE]]
repeat { // CHECK-COV: 1|{{.*}}[[@LINE]]
x += 1 // CHECK-COV: 1|{{.*}}[[@LINE]]
} while x == 0 // CHECK-COV: 1|{{.*}}[[@LINE]]
x += 1 // CHECK-COV: 1|{{.*}}[[@LINE]]
}

main()
foo()