Skip to content

Commit 004d927

Browse files
committed
[AddressLowering] Handle weak copies.
Lower the `strong_copy_weak_value` and `weak_copy_value` to `load_weak` and `store_weak` respectively.
1 parent 3cff25a commit 004d927

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3483,6 +3483,14 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
34833483
pass.deleter.forceDelete(sei);
34843484
}
34853485

3486+
void visitStrongCopyWeakValueInst(StrongCopyWeakValueInst *scwvi) {
3487+
auto sourceAddr = addrMat.materializeAddress(use->get());
3488+
SILValue value =
3489+
builder.createLoadWeak(scwvi->getLoc(), sourceAddr, IsNotTake);
3490+
scwvi->replaceAllUsesWith(value);
3491+
pass.deleter.forceDelete(scwvi);
3492+
}
3493+
34863494
// Extract from an opaque struct.
34873495
void visitStructExtractInst(StructExtractInst *extractInst);
34883496

@@ -4094,6 +4102,15 @@ class DefRewriter : SILInstructionVisitor<DefRewriter> {
40944102

40954103
rewriteUnconditionalCheckedCastInst(uncondCheckedCast, pass);
40964104
}
4105+
4106+
void visitWeakCopyValueInst(WeakCopyValueInst *wcsvi) {
4107+
auto &storage = pass.valueStorageMap.getStorage(wcsvi);
4108+
auto destAddr = addrMat.recursivelyMaterializeStorage(
4109+
storage, /*intoPhiOperand=*/false);
4110+
4111+
builder.createStoreWeak(wcsvi->getLoc(), wcsvi->getOperand(), destAddr,
4112+
IsInitialization);
4113+
}
40974114
};
40984115
} // end anonymous namespace
40994116

test/SILOptimizer/address_lowering_lib.sil

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-sil-opt -address-lowering -enable-sil-opaque-values -sil-verify-all %s | %FileCheck %s
1+
// RUN: %target-sil-opt -address-lowering -emit-sorted-sil -enable-sil-opaque-values -sil-verify-all %s | %FileCheck %s
22
//
33
import Builtin
44
import Swift
@@ -29,3 +29,34 @@ bb0(%0 : @owned $Error):
2929
%ret = tuple ()
3030
return %ret : $()
3131
}
32+
33+
struct WeakBox<T : AnyObject> {
34+
weak var t: T?
35+
}
36+
37+
// CHECK-LABEL: sil [ossa] @test_strong_copy_weak_value : {{.*}} {
38+
// CHECK: {{bb[0-9]+}}([[BOX_ADDR:%[^,]+]] :
39+
// CHECK: [[VALUE_ADDR:%[^,]+]] = struct_element_addr [[BOX_ADDR]] : {{.*}}, #WeakBox.t
40+
// CHECK: [[VALUE:%[^,]+]] = load_weak [[VALUE_ADDR]] : $*@sil_weak Optional<T>
41+
// CHECK: return [[VALUE]] : $Optional<T>
42+
// CHECK-LABEL: } // end sil function 'test_strong_copy_weak_value'
43+
sil [ossa] @test_strong_copy_weak_value : $@convention(thin) <T where T : AnyObject> (@in_guaranteed WeakBox<T>) -> @owned Optional<T> {
44+
bb0(%instance : @guaranteed $WeakBox<T>):
45+
%weak_optional = struct_extract %instance : $WeakBox<T>, #WeakBox.t
46+
%strong_optional = strong_copy_weak_value %weak_optional : $@sil_weak Optional<T>
47+
return %strong_optional : $Optional<T>
48+
}
49+
50+
// CHECK-LABEL: sil [ossa] @test_weak_copy_value_1 : {{.*}} {
51+
// CHECK: {{bb[0-9]+}}([[RETVAL:%[^,]+]] : $*WeakBox<T>, [[VALUE:%[^,]+]] :
52+
// CHECK: [[VALUE_ADDR:%[^,]+]] = struct_element_addr [[RETVAL]] : {{.*}}, #WeakBox.t
53+
// CHECK: store_weak [[VALUE]] to [init] [[VALUE_ADDR]]
54+
// CHECK: destroy_value [[VALUE]]
55+
// CHECK-LABEL: } // end sil function 'test_weak_copy_value_1'
56+
sil [ossa] @test_weak_copy_value_1 : $@convention(thin) <T where T : AnyObject> (@owned Optional<T>) -> @out WeakBox<T> {
57+
bb0(%value : @owned $Optional<T>):
58+
%weak_value = weak_copy_value %value : $Optional<T>
59+
destroy_value %value : $Optional<T>
60+
%retval = struct $WeakBox<T> (%weak_value : $@sil_weak Optional<T>)
61+
return %retval : $WeakBox<T>
62+
}

0 commit comments

Comments
 (0)