Skip to content

Commit 656e6cd

Browse files
committed
SemanticARCOpts: update borrowed-from instructions when changes are made
Fixes a SIL verifier error rdar://134728428
1 parent 6c99946 commit 656e6cd

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/SILOptimizer/SemanticARC/SemanticARCOpts.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/SILOptimizer/Analysis/Analysis.h"
2222
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
2323
#include "swift/SILOptimizer/PassManager/Transforms.h"
24+
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
2425

2526
#include "llvm/Support/CommandLine.h"
2627

@@ -176,15 +177,18 @@ struct SemanticARCOpts : SILFunctionTransform {
176177
// converted to guaranteed, ignoring the phi, try convert those phis to be
177178
// guaranteed.
178179
if (tryConvertOwnedPhisToGuaranteedPhis(visitor.ctx)) {
180+
updateBorrowedFrom(getPassManager(), &f);
179181
// We return here early to save a little compile time so we do not
180182
// invalidate analyses redundantly.
181183
return invalidateAnalysis(
182184
SILAnalysis::InvalidationKind::BranchesAndInstructions);
183185
}
184186

185187
// Otherwise, we only deleted instructions and did not touch phis.
186-
if (didEliminateARCInsts)
188+
if (didEliminateARCInsts) {
189+
updateBorrowedFrom(getPassManager(), &f);
187190
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
191+
}
188192
}
189193
};
190194

test/SILOptimizer/semantic-arc-opt-owned-to-guaranteed-phi.sil

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// RUN: %target-sil-opt -update-borrowed-from -semantic-arc-opts %s | %FileCheck %s
22

3+
sil_stage canonical
4+
5+
import Builtin
6+
import Swift
7+
import SwiftShims
8+
39
class Klass {}
410

511
// CHECK-LABEL: sil [ossa] @test_owned_to_guaranteed1 :
@@ -106,3 +112,27 @@ bbret:
106112
%t = tuple ()
107113
return %t : $()
108114
}
115+
116+
// CHECK-LABEL: sil [ossa] @test_borrowed_from_updating :
117+
// CHECK-NOT: copy-var
118+
// CHECK: bb2({{.*}} : @guaranteed $Klass):
119+
// CHECK: = borrowed {{.*}}%0 : $Optional<Klass>
120+
// CHECK-LABEL: } // end sil function 'test_borrowed_from_updating'
121+
sil [ossa] @test_borrowed_from_updating : $@convention(thin) (@guaranteed Optional<Klass>) -> () {
122+
bb0(%0 : @guaranteed $Optional<Klass>):
123+
%1 = copy_value %0 : $Optional<Klass>
124+
switch_enum %1 : $Optional<Klass>, case #Optional.none!enumelt: bb1, case #Optional.some!enumelt: bb2
125+
bb1:
126+
unreachable
127+
128+
bb2(%4 : @owned $Klass):
129+
%5 = begin_borrow %4 : $Klass
130+
br bb3(%5 : $Klass)
131+
132+
bb3(%7 : @reborrow @guaranteed $Klass):
133+
%8 = borrowed %7 : $Klass from (%4 : $Klass)
134+
end_borrow %8 : $Klass
135+
destroy_value %4 : $Klass
136+
%r = tuple ()
137+
return %r : $()
138+
}

0 commit comments

Comments
 (0)