Skip to content

Commit a93e8fd

Browse files
authored
Merge pull request #77752 from DougGregor/perf-diag-check-throws
[Performance diagnostics] Enable checking of throw instructions
2 parents 291822d + 1d3332d commit a93e8fd

File tree

4 files changed

+12
-49
lines changed

4 files changed

+12
-49
lines changed

include/swift/SILOptimizer/Utils/BasicBlockOptUtils.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,6 @@ class ReachingReturnBlocks {
6969
}
7070
};
7171

72-
/// Computes the set of blocks which are not used for error handling, i.e. not
73-
/// (exclusively) reachable from the error-block of a try_apply.
74-
class NonErrorHandlingBlocks {
75-
BasicBlockWorklist worklist;
76-
77-
public:
78-
NonErrorHandlingBlocks(SILFunction *function);
79-
80-
/// Returns true if there exists a path from \p block to the return-block.
81-
bool isNonErrorHandling(SILBasicBlock *block) const {
82-
return worklist.isVisited(block);
83-
}
84-
};
85-
8672
/// Remove all unreachable blocks in a function.
8773
bool removeUnreachableBlocks(SILFunction &f);
8874

lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,6 @@ bool PerformanceDiagnostics::visitFunctionEmbeddedSwift(
213213
return false;
214214
visitedFuncs[function] = PerformanceConstraints::None;
215215

216-
NonErrorHandlingBlocks neBlocks(function);
217-
218216
for (SILBasicBlock &block : *function) {
219217
for (SILInstruction &inst : block) {
220218
if (visitInst(&inst, PerformanceConstraints::None, parentLoc)) {
@@ -283,28 +281,11 @@ bool PerformanceDiagnostics::visitFunction(SILFunction *function,
283281
if (!function->isDefinition())
284282
return false;
285283

286-
NonErrorHandlingBlocks neBlocks(function);
287-
288284
for (SILBasicBlock &block : *function) {
289285
// Exclude fatal-error blocks.
290286
if (isa<UnreachableInst>(block.getTerminator()))
291287
continue;
292288

293-
// TODO: it's not yet clear how to deal with error existentials.
294-
// Ignore them for now. If we have typed throws we could ban error existentials
295-
// because typed throws would provide and alternative.
296-
if (isa<ThrowInst>(block.getTerminator()))
297-
continue;
298-
299-
// If a function has multiple throws, all throw-path branch to the single throw-block.
300-
if (SILBasicBlock *succ = block.getSingleSuccessorBlock()) {
301-
if (isa<ThrowInst>(succ->getTerminator()))
302-
continue;
303-
}
304-
305-
if (!neBlocks.isNonErrorHandling(&block))
306-
continue;
307-
308289
for (SILInstruction &inst : block) {
309290
if (visitInst(&inst, perfConstr, parentLoc)) {
310291
if (inst.getLoc().getSourceLoc().isInvalid()) {

lib/SILOptimizer/Utils/BasicBlockOptUtils.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,6 @@ ReachingReturnBlocks::ReachingReturnBlocks(SILFunction *function)
5555
}
5656
}
5757

58-
NonErrorHandlingBlocks::NonErrorHandlingBlocks(SILFunction *function)
59-
: worklist(function->getEntryBlock()) {
60-
while (SILBasicBlock *block = worklist.pop()) {
61-
if (auto ta = dyn_cast<TryApplyInst>(block->getTerminator())) {
62-
worklist.pushIfNotVisited(ta->getNormalBB());
63-
} else {
64-
for (SILBasicBlock *succ : block->getSuccessorBlocks()) {
65-
worklist.pushIfNotVisited(succ);
66-
}
67-
}
68-
}
69-
}
70-
7158
bool swift::removeUnreachableBlocks(SILFunction &f) {
7259
ReachableBlocks reachable(&f);
7360
// Visit all the blocks without doing any extra work.

test/SILOptimizer/performance-annotations.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,22 @@ func errorExistential(_ b: Bool) throws -> Int {
101101
if b {
102102
return 28
103103
}
104-
throw MyError()
104+
throw MyError() // expected-error{{Using type 'MyError' can cause metadata allocation or locks}}
105+
}
106+
107+
@_noLocks
108+
func concreteThrowsExistential(_ b: Bool) throws -> Int {
109+
if b {
110+
return 28
111+
}
112+
113+
throw ErrorEnum.tryAgain // expected-error{{Using type 'any Error' can cause metadata allocation or locks}}
105114
}
106115

107116
@_noLocks
108117
func multipleThrows(_ b1: Bool, _ b2: Bool) throws -> Int {
109118
if b1 {
110-
throw MyError()
119+
throw MyError() // expected-error{{Using type 'MyError' can cause metadata allocation or locks}}
111120
}
112121
if b2 {
113122
throw MyError2()
@@ -119,7 +128,7 @@ func multipleThrows(_ b1: Bool, _ b2: Bool) throws -> Int {
119128
func testCatch(_ b: Bool) throws -> Int? {
120129
do {
121130
return try errorExistential(true)
122-
} catch let e as MyError {
131+
} catch let e as MyError { // expected-error{{this code performs reference counting operations which can cause locking}}
123132
print(e)
124133
return nil
125134
}

0 commit comments

Comments
 (0)