Skip to content

Commit 67af3a1

Browse files
authored
[SIL Opaque Value] Fix SILGen to not generate store_borrow for +0 opaque values (#42488)
1 parent ccbfdbe commit 67af3a1

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

lib/SILGen/ManagedValue.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,21 @@ ManagedValue ManagedValue::materialize(SILGenFunction &SGF,
206206
StoreOwnershipQualifier::Init);
207207

208208
// SEMANTIC SIL TODO: This should really be called a temporary LValue.
209-
return ManagedValue::forOwnedAddressRValue(temporary,
210-
SGF.enterDestroyCleanup(temporary));
211-
} else {
212-
auto object = SGF.emitManagedBeginBorrow(loc, getValue());
213-
SGF.emitManagedStoreBorrow(loc, object.getValue(), temporary);
214-
return ManagedValue::forBorrowedAddressRValue(temporary);
209+
return ManagedValue::forOwnedAddressRValue(
210+
temporary, SGF.enterDestroyCleanup(temporary));
211+
}
212+
auto &lowering = SGF.getTypeLowering(getType());
213+
if (lowering.isAddressOnly()) {
214+
assert(!SGF.silConv.useLoweredAddresses());
215+
auto copy = SGF.B.createCopyValue(loc, getValue());
216+
SGF.B.emitStoreValueOperation(loc, copy, temporary,
217+
StoreOwnershipQualifier::Init);
218+
return ManagedValue::forOwnedAddressRValue(
219+
temporary, SGF.enterDestroyCleanup(temporary));
215220
}
221+
auto object = SGF.emitManagedBeginBorrow(loc, getValue());
222+
SGF.emitManagedStoreBorrow(loc, object.getValue(), temporary);
223+
return ManagedValue::forBorrowedAddressRValue(temporary);
216224
}
217225

218226
void ManagedValue::print(raw_ostream &os) const {

test/SILGen/opaque_values_silgen.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ func forEachStmt() {
6262
// ---
6363
// CHECK-LABEL: sil hidden [ossa] @$s20opaque_values_silgen12openExistBoxySSs5Error_pF : $@convention(thin) (@guaranteed Error) -> @owned String {
6464
// CHECK: bb0([[ARG:%.*]] : @guaranteed $Error):
65-
// HECK: [[OPAQUE_ARG:%.*]] = open_existential_box_value [[ARG]] : $Error to $@opened({{.*}}) Error
66-
// HECK: [[ALLOC_OPEN:%.*]] = alloc_stack $@opened({{.*}}) Error
67-
// HECK: store_borrow [[OPAQUE_ARG]] to [[ALLOC_OPEN]]
68-
// HECK: dealloc_stack [[ALLOC_OPEN]]
65+
// CHECK: [[OPAQUE_ARG:%.*]] = open_existential_box_value [[ARG]] : $Error to $@opened({{.*}}) Error
66+
// CHECK: [[ALLOC_OPEN:%.*]] = alloc_stack $@opened({{.*}}) Error
67+
// CHECK: [[COPY:%.*]] = copy_value [[OPAQUE_ARG]]
68+
// CHECK: store [[COPY]] to [init] [[ALLOC_OPEN]]
69+
// CHECK: destroy_addr [[ALLOC_OPEN]]
70+
// CHECK: dealloc_stack [[ALLOC_OPEN]]
6971
// CHECK-NOT: destroy_value [[ARG]] : $Error
70-
// HECK: return {{.*}} : $String
72+
// CHECK: return {{.*}} : $String
7173
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen12openExistBoxySSs5Error_pF'
7274
func openExistBox(_ x: Error) -> String {
7375
return x._domain

0 commit comments

Comments
 (0)