Skip to content

Commit

Permalink
InstrProf: Emit the counter for falling out of a do-catch immediately…
Browse files Browse the repository at this point in the history
… after the body

We were emitting the counter for falling out of a do block at the end
of the entire construct's scope, but this can cause us to miss the
increment if the insertion point isn't valid there. Move the increment
to immediately after we emit the body.

rdar://problem/22346924

Swift SVN r31356
  • Loading branch information
bogner committed Aug 20, 2015
1 parent 032f753 commit 0604d88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/SILGen/SILGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ static SILBasicBlock *getOrEraseBlock(SILGenFunction &SGF, JumpDest &dest) {

/// emitOrDeleteBlock - If there are branches to the specified JumpDest,
/// emit it per emitBlock. If there aren't, then just delete the block - it
/// turns out to have not been needed. Returns true if the block was emitted.
static bool emitOrDeleteBlock(SILGenFunction &SGF, JumpDest &dest,
/// turns out to have not been needed.
static void emitOrDeleteBlock(SILGenFunction &SGF, JumpDest &dest,
SILLocation BranchLoc) {
// If we ever add a single-use optimization here (to just continue
// the predecessor instead of branching to a separate block), we'll
Expand All @@ -131,11 +131,8 @@ static bool emitOrDeleteBlock(SILGenFunction &SGF, JumpDest &dest,
// doesn't leave us emitting the rest of the function in the
// postmatter section.
SILBasicBlock *BB = getOrEraseBlock(SGF, dest);
if (BB != nullptr) {
if (BB != nullptr)
SGF.B.emitBlock(BB, BranchLoc);
return true;
}
return false;
}

Condition SILGenFunction::emitCondition(Expr *E,
Expand Down Expand Up @@ -537,6 +534,9 @@ void StmtEmitter::visitDoCatchStmt(DoCatchStmt *S) {
llvm::SaveAndRestore<JumpDest> savedThrowDest(SGF.ThrowDest, throwDest);

visit(S->getBody());
// We emit the counter for exiting the do-block here, as we may not have a
// valid insertion point when falling out.
SGF.emitProfilerIncrement(S);
}

// Emit the catch clauses, but only if the body of the function
Expand Down Expand Up @@ -570,8 +570,7 @@ void StmtEmitter::visitDoCatchStmt(DoCatchStmt *S) {
// left in the original function section after this. So if
// emitOrDeleteBlock ever learns to just continue in the
// predecessor, we'll need to suppress that here.
if (emitOrDeleteBlock(SGF, endDest, CleanupLocation(S->getBody())))
SGF.emitProfilerIncrement(S);
emitOrDeleteBlock(SGF, endDest, CleanupLocation(S->getBody()));
}

void StmtEmitter::visitCatchStmt(CatchStmt *S) {
Expand Down
2 changes: 2 additions & 0 deletions test/SILGen/coverage_exceptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func foo() -> Int32 {
try baz { () throws -> () in throw SomeErr.Err1 }
} catch _ {}

try! baz { () throws -> () in return }

return x
}

Expand Down
20 changes: 20 additions & 0 deletions test/SILGen/instrprof_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,23 @@ func basic(a : Int32) {

// CHECK-NOT: builtin "int_instrprof_increment"
}

// CHECK: sil hidden @[[F_THROWING_NOP:.*throwing_nop.*]] :
func throwing_nop() throws {}
// CHECK: sil hidden @[[F_EXCEPTIONS:.*exceptions.*]] :
// CHECK: %[[NAME:.*]] = string_literal utf8 "[[F_EXCEPTIONS]]"
// CHECK: %[[HASH:.*]] = integer_literal $Builtin.Int64,
// CHECK: %[[NCOUNTS:.*]] = integer_literal $Builtin.Int32, 3
// CHECK: %[[INDEX:.*]] = integer_literal $Builtin.Int32, 0
// CHECK: builtin "int_instrprof_increment"(%[[NAME]] : {{.*}}, %[[HASH]] : {{.*}}, %[[NCOUNTS]] : {{.*}}, %[[INDEX]] : {{.*}})
func exceptions() {
do {
try throwing_nop()
// CHECK: builtin "int_instrprof_increment"
} catch {
// CHECK: builtin "int_instrprof_increment"
return
}

// CHECK-NOT: builtin "int_instrprof_increment"
}

0 comments on commit 0604d88

Please sign in to comment.