Skip to content

Commit 39e63b4

Browse files
committed
[sil] Make sure that we do not flag ignored_use as dead code when used after a try_apply or begin_apply.
1 parent cf2cdbd commit 39e63b4

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
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
}

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)