Skip to content

Commit 8c19cd7

Browse files
authored
Merge pull request #79028 from gottesmm/more-closure-fixes
[rbi] Small tweaks to the closure patch #78837
2 parents 5a5f749 + 8c96a8d commit 8c19cd7

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

lib/SILOptimizer/Mandatory/DiagnoseUnreachable.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,9 @@ static bool simplifyBlocksWithCallsToNoReturn(SILBasicBlock &BB,
777777

778778
// If we have an ignored use whose operand is our no return call, ignore it.
779779
if (auto *i = dyn_cast<IgnoredUseInst>(currInst)) {
780-
if (auto *svi = dyn_cast<SingleValueInstruction>(i->getOperand());
781-
svi && getAsCallToNoReturn(svi)) {
780+
// This handles try_apply, apply, begin_apply.
781+
if (auto *inst = i->getOperand()->getDefiningInstructionOrTerminator();
782+
inst && inst == noReturnCall) {
782783
return false;
783784
}
784785
}

lib/SILOptimizer/Mandatory/SendNonSendable.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,16 @@ findClosureUse(Operand *initialOperand) {
294294
// immediately invoked. In such a case, we can emit a better diagnostic in
295295
// the called closure.
296296
if (auto fas = FullApplySite::isa(op->getUser())) {
297-
if (auto *f = fas.getCalleeFunction();
298-
f && f->getDeclRef().getClosureExpr()) {
299-
auto *fArg = f->getArgument(fas.getCalleeArgIndex(*op));
300-
for (auto *use : fArg->getUses()) {
301-
if (visitedOperand.insert(use).second)
302-
worklist.emplace_back(use, fArg);
297+
if (auto *f = fas.getCalleeFunction()) {
298+
auto *fArg = cast<SILFunctionArgument>(
299+
f->getArgument(fas.getCalleeArgIndex(*op)));
300+
if (fArg->isClosureCapture()) {
301+
for (auto *use : fArg->getUses()) {
302+
if (visitedOperand.insert(use).second)
303+
worklist.emplace_back(use, fArg);
304+
}
305+
continue;
303306
}
304-
continue;
305307
}
306308
}
307309

test/Concurrency/transfernonsendable_sending_params.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,12 @@ func taskIsolatedCaptureInSendingClosureLiteral(_ x: NonSendableKlass) {
524524
}()
525525
}
526526

527+
Task { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
528+
{ // expected-note {{closure captures 'x' which is accessible to code in the current task}}
529+
print($0)
530+
}(x)
531+
}
532+
527533
takeClosure { // expected-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
528534
print(x) // expected-note {{closure captures 'x' which is accessible to code in the current task}}
529535
}

test/SILOptimizer/noreturn_folding.sil

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
// RUN: %target-sil-opt -module-name Swift -enable-sil-verify-all -noreturn-folding < %s | %FileCheck %s
1+
// RUN: %target-sil-opt -enable-sil-verify-all -noreturn-folding -verify %s | %FileCheck %s
22

3+
import Swift
34
import Builtin
45

5-
enum Never {}
6-
7-
struct Int64 {
8-
var value: Builtin.Int64
9-
}
6+
sil @exit : $@convention(thin) (Builtin.Int32) -> Never
7+
sil @returnNever : $@convention(thin) () -> Never
8+
sil @returnNeverThrows : $@convention(thin) () -> (Never, @error Error)
9+
sil @returnNeverCoroutine : $@yield_once @convention(thin) () -> @yields Never
10+
sil @doSomething : $@convention(thin) () -> ()
1011

1112
// We used to crash on this IR. We would delete "%4" while there is still a
1213
// (dead) user "%7" around.
@@ -15,15 +16,17 @@ struct Int64 {
1516
// CHECK: %[[E:.+]] = function_ref @exit
1617
// CHECK: apply %[[E]]
1718
// CHECK: unreachable
18-
19+
// CHECK: } // end sil function 'unreachable_outside_block_user'
1920
sil private @unreachable_outside_block_user : $@convention(thin) () -> Int64 {
2021
bb0:
2122
%0 = integer_literal $Builtin.Int1, -1
2223
%1 = integer_literal $Builtin.Int32, 3
2324
// function_ref exit
2425
%2 = function_ref @exit : $@convention(thin) (Builtin.Int32) -> Never
2526
%3 = apply %2(%1) : $@convention(thin) (Builtin.Int32) -> Never
27+
// expected-note @-1 {{a call to a never-returning function}}
2628
%4 = integer_literal $Builtin.Int64, 7
29+
// expected-warning @-1 {{will never be executed}}
2730
cond_br %0, bb1, bb2
2831

2932
bb1:
@@ -42,4 +45,35 @@ bb3 (%11: $Int64):
4245
return %11 : $Int64
4346
}
4447

45-
sil @exit : $@convention(thin) (Builtin.Int32) -> Never
48+
// Make sure we do not emit any error here.
49+
sil @ignore_use_apply : $@convention(thin) () -> () {
50+
bb0:
51+
%0 = function_ref @returnNever : $@convention(thin) () -> Never
52+
%1 = apply %0() : $@convention(thin) () -> Never
53+
ignored_use %1 : $Never
54+
unreachable
55+
}
56+
57+
// Make sure we do not emit any error here.
58+
sil [ossa] @ignore_use_try_apply : $@convention(thin) () -> () {
59+
bb0:
60+
%0 = function_ref @returnNeverThrows : $@convention(thin) () -> (Never, @error Error)
61+
try_apply %0() : $@convention(thin) () -> (Never, @error Error), normal bb1, error bb2
62+
63+
bb1(%2 : $Never):
64+
ignored_use %2 : $Never
65+
unreachable
66+
67+
bb2(%5 : @owned $Error):
68+
%6 = builtin "unexpectedError"(%5 : $Error) : $()
69+
unreachable
70+
}
71+
72+
sil [ossa] @ignore_use_begin_apply : $@convention(thin) () -> () {
73+
bb0:
74+
%0 = function_ref @returnNeverCoroutine : $@yield_once @convention(thin) () -> @yields Never
75+
(%1, %2) = begin_apply %0() : $@yield_once @convention(thin) () -> @yields Never
76+
ignored_use %1
77+
end_apply %2 as $()
78+
unreachable
79+
}

0 commit comments

Comments
 (0)