Skip to content

Commit 252a57a

Browse files
authored
Merge pull request #78096 from meg-gupta/removeunreachable
Remove unreachable blocks after inlining
2 parents 1eb0ad2 + 984f9f6 commit 252a57a

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
2020
#include "swift/SILOptimizer/PassManager/Passes.h"
2121
#include "swift/SILOptimizer/PassManager/Transforms.h"
22+
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
2223
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
2324
#include "swift/SILOptimizer/Utils/Devirtualize.h"
2425
#include "swift/SILOptimizer/Utils/Generics.h"
@@ -1372,6 +1373,7 @@ class SILPerformanceInlinerPass : public SILFunctionTransform {
13721373
// can further optimize this function before attempting to inline
13731374
// in it again.
13741375
if (Inliner.inlineCallsIntoFunction(getFunction())) {
1376+
removeUnreachableBlocks(*getFunction());
13751377
invalidateAnalysis(SILAnalysis::InvalidationKind::FunctionBody);
13761378
restartPassPipeline();
13771379
}

test/SILOptimizer/inline_begin_apply.sil

-11
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,6 @@ entry:
505505
// CHECK-NEXT: [[MARKER:%.*]] = function_ref @marker : $@convention(thin) (Builtin.Int32) -> ()
506506
// CHECK-NEXT: apply [[MARKER]]([[T0]]) : $@convention(thin) (Builtin.Int32) -> ()
507507
// CHECK-NEXT: unreachable
508-
// CHECK: bb1:
509-
// CHECK-NEXT: // function_ref
510-
// CHECK-NEXT: [[USE:%.*]] = function_ref @use : $@convention(thin) (@in Builtin.Int8) -> ()
511-
// CHECK-NEXT: apply [[USE]](undef) : $@convention(thin) (@in Builtin.Int8) -> ()
512-
// CHECK-NEXT: unreachable
513-
// CHECK: bb2:
514-
// CHECK-NEXT: [[T0:%.*]] = tuple ()
515-
// CHECK-NEXT: return [[T0]] : $()
516508
// CHECK: }
517509
sil [ossa] @test_simple_call_no_yields : $() -> () {
518510
entry:
@@ -618,11 +610,8 @@ bb0:
618610
// CHECK: [[A16:%.*]] = alloc_stack $Builtin.Int16
619611
// CHECK: [[A32:%.*]] = alloc_stack $Builtin.Int32
620612
// CHECK: bb1:
621-
// CHECK: dealloc_stack [[A32]] : $*Builtin.Int32
622613
// CHECK: unreachable
623614
// CHECK: bb2:
624-
// CHECK: unreachable
625-
// CHECK: bb3:
626615
// CHECK: dealloc_stack [[A32]] : $*Builtin.Int32
627616
// CHECK: dealloc_stack [[A16]] : $*Builtin.Int16
628617
// CHECK: return

test/SILOptimizer/inline_lifetime.sil

-13
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,6 @@ bb0(%instance : @owned $C):
684684
// CHECK: destroy_value [[INSTANCE]]
685685
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
686686
// CHECK: return [[RETVAL]]
687-
// CHECK: bb1:
688-
// CHECK: destroy_addr [[ADDR]]
689-
// CHECK: dealloc_stack [[ADDR]]
690-
// CHECK: unreachable
691687
// CHECK-LABEL: } // end sil function 'caller_owned_callee_coro_guaranteed'
692688
sil [ossa] @caller_owned_callee_coro_guaranteed : $@convention(thin) (@owned C) -> () {
693689
bb0(%instance : @owned $C):
@@ -724,10 +720,6 @@ bb0(%instance : @guaranteed $C):
724720
// CHECK-NOT: end_borrow
725721
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
726722
// CHECK: return [[RETVAL]]
727-
// CHECK: bb1:
728-
// CHECK: destroy_addr [[ADDR]]
729-
// CHECK: dealloc_stack [[ADDR]]
730-
// CHECK: unreachable
731723
// CHECK-LABEL: } // end sil function 'caller_guaranteed_callee_coro_guaranteed'
732724
sil [ossa] @caller_guaranteed_callee_coro_guaranteed : $@convention(thin) (@guaranteed C) -> () {
733725
bb0(%instance : @guaranteed $C):
@@ -746,9 +738,6 @@ bb0(%instance : @guaranteed $C):
746738
// CHECK: [[REGISTER_4:%[^,]+]] = tuple ()
747739
// CHECK: [[REGISTER_5:%[^,]+]] = tuple ()
748740
// CHECK: return [[REGISTER_5]] : $()
749-
// CHECK: {{bb[0-9]+}}:
750-
// CHECK: dealloc_stack [[REGISTER_1]] : $*S
751-
// CHECK: unreachable
752741
// CHECK-LABEL: } // end sil function 'caller_trivial_callee_coro_trivial'
753742
sil hidden [ossa] @caller_trivial_callee_coro_trivial : $@convention(thin) (S) -> () {
754743
bb0(%instance : $S):
@@ -764,8 +753,6 @@ bb0(%instance : $S):
764753
// CHECK: {{%[^,]+}} = tuple ()
765754
// CHECK: [[RETVAL:%[^,]+]] = tuple ()
766755
// CHECK: return [[RETVAL]]
767-
// CHECK: {{bb[^,]+}}:
768-
// CHECK: unreachable
769756
// CHECK-LABEL: } // end sil function 'caller_in_callee_coro_in'
770757
sil hidden [ossa] @caller_in_callee_coro_in : $@convention(thin) (@in S) -> () {
771758
bb0(%instance : $*S):

test/SILOptimizer/inline_tryApply.sil

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import SwiftShims
99

1010
//CHECK-LABEL: sil @caller_function
1111
//CHECK-NOT: try_apply
12-
//CHECK: throw {{.*}} : $any Error
1312
sil @caller_function : $@convention(thin) () -> @error Error {
1413
bb0:
1514
// function_ref main.inner () throws -> ()
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: %target-sil-opt -inline -mem2reg %s | %FileCheck %s
2+
3+
import Builtin
4+
import Swift
5+
6+
class Klass {
7+
}
8+
9+
sil shared [ossa] @callee : $@convention(method) (@guaranteed Klass) -> @error any Error {
10+
bb0(%0 : @guaranteed $Klass):
11+
%1 = tuple ()
12+
return %1
13+
}
14+
15+
// CHECK-LABEL: sil shared [ossa] @caller :
16+
// CHECK-NOT: try_apply
17+
// CHECK-NOT: alloc_stack
18+
// CHECK-LABEL: } // end sil function 'caller'
19+
sil shared [ossa] @caller : $@convention(thin) (@guaranteed Klass) -> @error any Error {
20+
bb0(%0 : @guaranteed $Klass):
21+
%1 = copy_value %0
22+
%2 = alloc_stack $Klass
23+
store %1 to [init] %2
24+
%4 = function_ref @callee : $@convention(method) (@guaranteed Klass) -> @error any Error
25+
%5 = load_borrow %2
26+
try_apply %4(%5) : $@convention(method) (@guaranteed Klass) -> @error any Error, normal bb1, error bb2
27+
28+
bb1(%7 : $()):
29+
end_borrow %5
30+
destroy_addr %2
31+
dealloc_stack %2
32+
%11 = tuple ()
33+
return %11
34+
35+
bb2(%13 : @owned $any Error):
36+
end_borrow %5
37+
destroy_addr %2
38+
dealloc_stack %2
39+
throw %13
40+
}
41+

0 commit comments

Comments
 (0)