Skip to content

Commit 1c85d0c

Browse files
committed
[sil-opaque-values] Add support for SILGen emitOpenExistential for the Error type.
1 parent c15c671 commit 1c85d0c

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

lib/SILGen/SILGenConvert.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -907,12 +907,20 @@ SILGenFunction::emitOpenExistential(
907907

908908
existentialType = existentialValue.getType();
909909
assert(existentialType.isObject());
910+
if (loweredOpenedType.isAddress()) {
911+
archetypeMV = ManagedValue::forUnmanaged(
912+
B.createOpenExistentialBox(loc, existentialValue.getValue(),
913+
loweredOpenedType));
914+
} else {
915+
assert(!silConv.useLoweredAddresses());
916+
archetypeMV = ManagedValue::forUnmanaged(
917+
B.createOpenExistentialBoxValue(loc, existentialValue.getValue(),
918+
loweredOpenedType));
919+
}
910920
// NB: Don't forward the cleanup, because consuming a boxed value won't
911921
// consume the box reference.
912-
archetypeMV = ManagedValue::forUnmanaged(B.createOpenExistentialBox(
913-
loc, existentialValue.getValue(), loweredOpenedType.getAddressType()));
914-
// The boxed value can't be assumed to be uniquely referenced. We can never
915-
// consume it.
922+
// The boxed value can't be assumed to be uniquely referenced.
923+
// We can never consume it.
916924
// TODO: We could use isUniquelyReferenced to shorten the duration of
917925
// the box to the point that the opaque value is copied out.
918926
isUnique = false;

lib/SILGen/SILGenExpr.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ ManagedValue SILGenFunction::emitManagedLoadCopy(SILLocation loc, SILValue v,
8686
return ManagedValue::forUnmanaged(v);
8787
if (v.getOwnershipKind() == ValueOwnershipKind::Trivial)
8888
return ManagedValue::forUnmanaged(v);
89-
assert(!lowering.isAddressOnly() && "cannot retain an unloadable type");
89+
assert((!lowering.isAddressOnly() || !silConv.useLoweredAddresses()) &&
90+
"cannot retain an unloadable type");
9091
return emitManagedRValueWithCleanup(v, lowering);
9192
}
9293

@@ -105,7 +106,8 @@ SILGenFunction::emitManagedLoadBorrow(SILLocation loc, SILValue v,
105106
return ManagedValue::forUnmanaged(v);
106107
}
107108

108-
assert(!lowering.isAddressOnly() && "cannot retain an unloadable type");
109+
assert((!lowering.isAddressOnly() || !silConv.useLoweredAddresses()) &&
110+
"cannot retain an unloadable type");
109111
auto *lbi = B.createLoadBorrow(loc, v);
110112
return emitManagedBorrowedRValueWithCleanup(v, lbi, lowering);
111113
}
@@ -124,7 +126,8 @@ ManagedValue SILGenFunction::emitManagedStoreBorrow(
124126
lowering.emitStore(B, loc, v, addr, StoreOwnershipQualifier::Trivial);
125127
return ManagedValue::forUnmanaged(v);
126128
}
127-
assert(!lowering.isAddressOnly() && "cannot retain an unloadable type");
129+
assert((!lowering.isAddressOnly() || !silConv.useLoweredAddresses()) &&
130+
"cannot retain an unloadable type");
128131
auto *sbi = B.createStoreBorrow(loc, v, addr);
129132
return emitManagedBorrowedRValueWithCleanup(sbi->getSrc(), sbi, lowering);
130133
}

test/SILGen/opaque_values_silgen.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,9 @@ func s210______compErasure(_ x: Foo & Error) -> Error {
489489
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s220_____openExistBoxSSs5Error_pF : $@convention(thin) (@owned Error) -> @owned String {
490490
// CHECK: bb0([[ARG:%.*]] : $Error):
491491
// CHECK: [[BORROWED_ARG:%.*]] = begin_borrow [[ARG]]
492-
// CHECK: [[OPAQUE_ARG:%.*]] = open_existential_box [[BORROWED_ARG]] : $Error to $*@opened({{.*}}) Error
493-
// CHECK: [[LOAD_ALLOC:%.*]] = load [copy] [[OPAQUE_ARG]]
492+
// CHECK: [[OPAQUE_ARG:%.*]] = open_existential_box_value [[BORROWED_ARG]] : $Error to $@opened({{.*}}) Error
494493
// CHECK: [[ALLOC_OPEN:%.*]] = alloc_stack $@opened({{.*}}) Error
495-
// CHECK: store [[LOAD_ALLOC]] to [init] [[ALLOC_OPEN]]
494+
// CHECK: store_borrow [[OPAQUE_ARG]] to [[ALLOC_OPEN]]
496495
// CHECK: dealloc_stack [[ALLOC_OPEN]]
497496
// CHECK: end_borrow [[BORROWED_ARG]] from [[ARG]]
498497
// CHECK: destroy_value [[ARG]] : $Error
@@ -1003,6 +1002,21 @@ func s470________nativeToC(fromAny any: Any) -> AnyObject {
10031002
}
10041003
#endif
10051004

1005+
// Test emitOpenExistential
1006+
// ---
1007+
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s480_________getErroryps0F0_p04someF0_tF : $@convention(thin) (@owned Error) -> @out Any {
1008+
// CHECK: bb0(%0 : $Error):
1009+
// CHECK: [[BORROW:%.*]] = begin_borrow %0 : $Error
1010+
// CHECK: [[VAL:%.*]] = open_existential_box_value [[BORROW]] : $Error to $@opened("{{.*}}") Error
1011+
// CHECK: [[COPY:%.*]] = copy_value [[VAL]] : $@opened("{{.*}}") Error
1012+
// CHECK: [[ANY:%.*]] = init_existential_value [[COPY]] : $@opened("{{.*}}") Error, $@opened("{{.*}}") Error, $Any
1013+
// CHECK: destroy_value %0 : $Error
1014+
// CHECK: return [[ANY]] : $Any
1015+
// CHECK-LABEL: } // end sil function '_T020opaque_values_silgen21s480_________getErroryps0F0_p04someF0_tF'
1016+
func s480_________getError(someError: Error) -> Any {
1017+
return someError
1018+
}
1019+
10061020
// Tests conditional value casts and correspondingly generated reabstraction thunk, with <T> types
10071021
// ---
10081022
// CHECK-LABEL: sil hidden @_T020opaque_values_silgen21s999_____condTFromAnyyyp_xtlF : $@convention(thin) <T> (@in Any, @in T) -> () {

0 commit comments

Comments
 (0)