Skip to content

Commit 2a8b2ce

Browse files
Merge pull request #61897 from nate-chandler/opaque-values/3/20221102
[OpaqueValues] Small fixes for AddressLowering.
2 parents 4d16353 + 646cf23 commit 2a8b2ce

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,8 @@ void OpaqueStorageAllocation::removeAllocation(SILValue value) {
11831183
storage.storageAddress = nullptr;
11841184

11851185
// It's only use should be dealloc_stacks.
1186-
for (Operand *use : allocInst->getUses()) {
1186+
SmallVector<Operand *, 4> uses(allocInst->getUses());
1187+
for (Operand *use : uses) {
11871188
pass.deleter.forceDelete(cast<DeallocStackInst>(use->getUser()));
11881189
}
11891190
pass.deleter.forceDelete(allocInst);
@@ -2650,6 +2651,13 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
26502651

26512652
void visitEndBorrowInst(EndBorrowInst *end) {}
26522653

2654+
void visitFixLifetimeInst(FixLifetimeInst *fli) {
2655+
SILValue value = fli->getOperand();
2656+
SILValue address = pass.valueStorageMap.getStorage(value).storageAddress;
2657+
builder.createFixLifetime(fli->getLoc(), address);
2658+
pass.deleter.forceDelete(fli);
2659+
}
2660+
26532661
void visitBranchInst(BranchInst *) {
26542662
pass.getPhiRewriter().materializeOperand(use);
26552663

test/SILOptimizer/address_lowering.sil

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,18 @@ bb2(%9 : $Error):
12281228
throw %9 : $Error
12291229
}
12301230

1231+
// CHECK-LABEL: sil [ossa] @fixeeLifetime : {{.*}} {
1232+
// CHECK: {{bb[0-9]+}}([[ADDR:%[^,]+]] : $*T):
1233+
// CHECK: fix_lifetime [[ADDR]]
1234+
// CHECK-LABEL: } // end sil function 'fixeeLifetime'
1235+
sil [ossa] @fixeeLifetime : $@convention(thin) <T> (@in T) -> () {
1236+
bb0(%instance : @owned $T):
1237+
fix_lifetime %instance : $T
1238+
destroy_value %instance : $T
1239+
%retval = tuple ()
1240+
return %retval : $()
1241+
}
1242+
12311243
// CHECK-LABEL: sil [ossa] @lexical_borrow_struct_extract {{.*}} {
12321244
// CHECK: [[PAIR_ADDR:%[^,]+]] = alloc_stack [lexical]
12331245
// CHECK: [[X_ADDR:%[^,]+]] = struct_element_addr [[PAIR_ADDR]]
@@ -1273,6 +1285,61 @@ entry(%instance : @owned $Pair<T>):
12731285
return %retval : $()
12741286
}
12751287

1288+
/// Verify that alloc_stacks for which there is already a dealloc stack can be
1289+
/// deleted.
1290+
// CHECK-LABEL: sil [ossa] @partition : {{.*}} {
1291+
// CHECK: {{bb[0-9]+}}([[INDEX_OUT:%[^,]+]] : $*Self.Index, [[SELF:%[^,]+]] : $*Self):
1292+
// CHECK: [[MAYBE_INDEX_ADDR:%[^,]+]] = alloc_stack $Optional<Builtin.Int64>
1293+
// CHECK: try_apply {{%[^,]+}}<Self, Builtin.Int64>([[MAYBE_INDEX_ADDR]], [[SELF]]) : $@convention(thin) <τ_0_0 where τ_0_0 : MyCollection><τ_1_0> (@inout τ_0_0) -> (@out Optional<τ_1_0>, @error any Error), normal [[YES_BLOCK:bb[0-9]+]], error [[NO_BLOCK:bb[0-9]+]]
1294+
// CHECK: [[NO_BLOCK]]([[REGISTER_16:%[^,]+]] : @owned $any Error):
1295+
// CHECK: dealloc_stack [[MAYBE_INDEX_ADDR]]
1296+
// CHECK: br [[EXIT_ERROR:bb[0-9]+]]([[REGISTER_16]] : $any Error)
1297+
// CHECK: [[EXIT_ERROR]]([[ERROR_TO_THROW:%[^,]+]] : @owned $any Error):
1298+
// CHECK: throw [[ERROR_TO_THROW]]
1299+
// CHECK: [[YES_BLOCK]]
1300+
// CHECK: [[MAYBE_INDEX:%[^,]+]] = load [trivial] [[MAYBE_INDEX_ADDR]]
1301+
// CHECK: dealloc_stack [[MAYBE_INDEX_ADDR]]
1302+
// CHECK: switch_enum [[MAYBE_INDEX]] : $Optional<Builtin.Int64>, case #Optional.some!enumelt: [[SOME_BLOCK:bb[0-9]+]], case #Optional.none!enumelt: [[NONE_BLOCK:bb[0-9]+]]
1303+
// CHECK: [[NONE_BLOCK]]:
1304+
// CHECK: apply {{%[^,]+}}<Self>([[INDEX_OUT]], [[SELF]])
1305+
// CHECK: [[SOME_BLOCK]]([[SOME_INDEX:%[^,]+]] : $Builtin.Int64):
1306+
// CHECK: apply {{%[^,]+}}<Self>([[INDEX_OUT]], [[SOME_INDEX]])
1307+
// CHECK-LABEL: } // end sil function 'partition'
1308+
protocol MyCollection {
1309+
associatedtype Index
1310+
}
1311+
sil [ossa] @impl : $@convention(method) <τ_0_0 where τ_0_0 : MyCollection> (@inout τ_0_0) -> @out τ_0_0.Index
1312+
sil [ossa] @one : $@convention(thin) <τ_0_0 where τ_0_0 : MyCollection><τ_1_0> (@inout τ_0_0) -> (@out Optional<τ_1_0>, @error any Error)
1313+
sil [ossa] @startIndex : $@convention(thin) <τ_0_0 where τ_0_0 : MyCollection> (@in_guaranteed τ_0_0) -> @out τ_0_0.Index
1314+
sil [ossa] @index : $@convention(thin) <τ_0_0 where τ_0_0 : MyCollection> (Int) -> @out τ_0_0.Index
1315+
sil [ossa] @partition : $@convention(method) <Self where Self : MyCollection> (@inout Self) -> (@out Self.Index, @error any Error) {
1316+
bb0(%1 : $*Self):
1317+
%11 = function_ref @one : $@convention(thin) <τ_0_0 where τ_0_0 : MyCollection><τ_1_0> (@inout τ_0_0) -> (@out Optional<τ_1_0>, @error any Error)
1318+
try_apply %11<Self, Int>(%1) : $@convention(thin) <τ_0_0 where τ_0_0 : MyCollection><τ_1_0> (@inout τ_0_0) -> (@out Optional<τ_1_0>, @error any Error), normal bb1, error bb6
1319+
1320+
bb1(%13 : $Optional<Int>):
1321+
switch_enum %13 : $Optional<Int>, case #Optional.some!enumelt: bb2, case #Optional.none!enumelt: bb3
1322+
1323+
bb2(%17 : $Int):
1324+
%28 = function_ref @index : $@convention(thin) <τ_0_0 where τ_0_0 : MyCollection> (Int) -> @out τ_0_0.Index
1325+
%29 = apply %28<Self>(%17) : $@convention(thin) <τ_0_0 where τ_0_0 : MyCollection> (Int) -> @out τ_0_0.Index
1326+
br exit_normal(%29 : $Self.Index)
1327+
1328+
bb3:
1329+
%34 = function_ref @impl : $@convention(method) <τ_0_0 where τ_0_0 : MyCollection> (@inout τ_0_0) -> @out τ_0_0.Index
1330+
%36 = apply %34<Self>(%1) : $@convention(method) <τ_0_0 where τ_0_0 : MyCollection> (@inout τ_0_0) -> @out τ_0_0.Index
1331+
br exit_normal(%36 : $Self.Index)
1332+
1333+
bb6(%41 : @owned $any Error):
1334+
br exit_error(%41 : $any Error)
1335+
1336+
exit_normal(%39 : @owned $Self.Index):
1337+
return %39 : $Self.Index
1338+
1339+
exit_error(%47 : @owned $any Error):
1340+
throw %47 : $any Error
1341+
}
1342+
12761343
sil hidden [ossa] @testBeginApplyDeadYield : $@convention(thin) <T> (@guaranteed TestGeneric<T>) -> () {
12771344
bb0(%0 : @guaranteed $TestGeneric<T>):
12781345
%2 = class_method %0 : $TestGeneric<T>, #TestGeneric.borrowedGeneric!read : <T> (TestGeneric<T>) -> () -> (), $@yield_once @convention(method) <τ_0_0> (@guaranteed TestGeneric<τ_0_0>) -> @yields @in_guaranteed τ_0_0
@@ -1541,3 +1608,4 @@ bb0(%0 : @guaranteed $T):
15411608
destroy_value %3 : $U
15421609
return %6 : $U
15431610
}
1611+

0 commit comments

Comments
 (0)