Skip to content

Commit 40c9e26

Browse files
authored
Merge pull request #26843 from gottesmm/pr-a843e44d9369751277d4b497534c4656934224b8
[silgen] Add SILGen support for emitting copy_unmanaged_value instead…
2 parents 1bf5631 + 6a54531 commit 40c9e26

File tree

5 files changed

+34
-48
lines changed

5 files changed

+34
-48
lines changed

lib/SILGen/SILGenBuilder.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,14 @@ ManagedValue SILGenBuilder::createCopyValue(SILLocation loc,
175175
SILValue result = createCopy##Name##Value(loc, originalValue.getValue()); \
176176
return SGF.emitManagedRValueWithCleanup(result); \
177177
}
178-
#define UNCHECKED_REF_STORAGE(Name, ...) \
179-
ManagedValue \
180-
SILGenBuilder::createUnsafeCopy##Name##Value(SILLocation loc, \
181-
ManagedValue originalValue) { \
182-
/* *NOTE* The reason why this is unsafe is that we are converting and */ \
183-
/* unconditionally retaining, rather than before converting from */ \
184-
/* type->ref checking that our value is not yet uninitialized. */ \
185-
auto type = originalValue.getType().getAs<Name##StorageType>(); \
186-
SILValue result = create##Name##ToRef( \
187-
loc, originalValue.getValue(), \
188-
SILType::getPrimitiveObjectType(type.getReferentType())); \
189-
result = createCopyValue(loc, result); \
190-
return SGF.emitManagedRValueWithCleanup(result); \
178+
#define UNCHECKED_REF_STORAGE(Name, ...) \
179+
ManagedValue SILGenBuilder::createCopy##Name##Value( \
180+
SILLocation loc, ManagedValue originalValue) { \
181+
/* *NOTE* The reason why this is unsafe is that we are converting and */ \
182+
/* unconditionally retaining, rather than before converting from */ \
183+
/* type->ref checking that our value is not yet uninitialized. */ \
184+
SILValue result = createCopy##Name##Value(loc, originalValue.getValue()); \
185+
return SGF.emitManagedRValueWithCleanup(result); \
191186
}
192187
#include "swift/AST/ReferenceStorage.def"
193188

lib/SILGen/SILGenBuilder.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ class SILGenBuilder : public SILBuilder {
118118
using SILBuilder::createCopy##Name##Value; \
119119
ManagedValue createCopy##Name##Value(SILLocation loc, \
120120
ManagedValue originalValue);
121-
#define UNCHECKED_REF_STORAGE(Name, ...) \
122-
ManagedValue createUnsafeCopy##Name##Value(SILLocation loc, \
123-
ManagedValue originalValue);
121+
#define UNCHECKED_REF_STORAGE(Name, ...) \
122+
using SILBuilder::createCopy##Name##Value; \
123+
ManagedValue createCopy##Name##Value(SILLocation loc, \
124+
ManagedValue originalValue);
124125
#include "swift/AST/ReferenceStorage.def"
125126

126127
ManagedValue createOwnedPhiArgument(SILType type);

lib/SILGen/SILGenLValue.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3617,15 +3617,12 @@ SILValue SILGenFunction::emitConversionToSemanticRValue(SILLocation loc,
36173617
ResilienceExpansion::Maximal)); \
36183618
return B.createCopy##Name##Value(loc, src); \
36193619
}
3620-
#define UNCHECKED_REF_STORAGE(Name, ...) \
3621-
case ReferenceOwnership::Name: { \
3622-
/* For static reference storage types, we need to strip the box and */ \
3623-
/* then do an (unsafe) retain. */ \
3624-
auto type = storageType.castTo<Name##StorageType>(); \
3625-
auto result = B.create##Name##ToRef(loc, src, \
3626-
SILType::getPrimitiveObjectType(type.getReferentType())); \
3627-
/* SEMANTIC ARC TODO: Does this need a cleanup? */ \
3628-
return B.createCopyValue(loc, result); \
3620+
#define UNCHECKED_REF_STORAGE(Name, ...) \
3621+
case ReferenceOwnership::Name: { \
3622+
/* For static reference storage types, we need to strip the box and */ \
3623+
/* then do an (unsafe) retain. */ \
3624+
auto type = storageType.castTo<Name##StorageType>(); \
3625+
return B.createCopy##Name##Value(loc, src); \
36293626
}
36303627
#include "swift/AST/ReferenceStorage.def"
36313628
}
@@ -3647,10 +3644,10 @@ ManagedValue SILGenFunction::emitConversionToSemanticRValue(
36473644
case ReferenceOwnership::Name: \
36483645
/* Generate a strong retain and strip the box. */ \
36493646
return B.createCopy##Name##Value(loc, src);
3650-
#define UNCHECKED_REF_STORAGE(Name, ...) \
3651-
case ReferenceOwnership::Name: \
3652-
/* Strip the box and then do an (unsafe) retain. */ \
3653-
return B.createUnsafeCopy##Name##Value(loc, src);
3647+
#define UNCHECKED_REF_STORAGE(Name, ...) \
3648+
case ReferenceOwnership::Name: \
3649+
/* Strip the box and then do an (unsafe) retain. */ \
3650+
return B.createCopy##Name##Value(loc, src);
36543651
#include "swift/AST/ReferenceStorage.def"
36553652
}
36563653
llvm_unreachable("impossible");
@@ -3702,15 +3699,12 @@ static SILValue emitLoadOfSemanticRValue(SILGenFunction &SGF,
37023699
} \
37033700
ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE_HELPER(Name) \
37043701
}
3705-
#define UNCHECKED_REF_STORAGE(Name, ...) \
3706-
case ReferenceOwnership::Name: { \
3707-
/* For static reference storage types, we need to strip the box. */ \
3708-
auto type = storageType.castTo<Name##StorageType>(); \
3709-
auto value = SGF.B.createLoad(loc, src, LoadOwnershipQualifier::Trivial); \
3710-
auto result = SGF.B.create##Name##ToRef(loc, value, \
3711-
SILType::getPrimitiveObjectType(type.getReferentType())); \
3712-
/* SEMANTIC ARC TODO: Does this need a cleanup? */ \
3713-
return SGF.B.createCopyValue(loc, result); \
3702+
#define UNCHECKED_REF_STORAGE(Name, ...) \
3703+
case ReferenceOwnership::Name: { \
3704+
/* For static reference storage types, we need to strip the box. */ \
3705+
auto type = storageType.castTo<Name##StorageType>(); \
3706+
auto value = SGF.B.createLoad(loc, src, LoadOwnershipQualifier::Trivial); \
3707+
return SGF.B.createCopy##Name##Value(loc, value); \
37143708
}
37153709
#include "swift/AST/ReferenceStorage.def"
37163710
#undef ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE_HELPER

test/SILGen/unmanaged.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ func get(holder holder: inout Holder) -> C {
3939
// CHECK-NEXT: [[READ:%.*]] = begin_access [read] [static] [[ADDR]] : $*Holder
4040
// CHECK-NEXT: [[T0:%.*]] = struct_element_addr [[READ]] : $*Holder, #Holder.value
4141
// CHECK-NEXT: [[T1:%.*]] = load [[T0]] : $*@sil_unmanaged C
42-
// CHECK-NEXT: [[T2:%.*]] = unmanaged_to_ref [[T1]]
43-
// CHECK-NEXT: strong_retain [[T2]]
42+
// CHECK-NEXT: [[T2:%.*]] = copy_unmanaged_value [[T1]]
4443
// CHECK-NEXT: end_access [[READ]] : $*Holder
4544
// CHECK-NEXT: return [[T2]]
4645

@@ -52,6 +51,5 @@ func project(fn fn: () -> Holder) -> C {
5251
// CHECK-NEXT: debug_value
5352
// CHECK-NEXT: [[T0:%.*]] = apply [[FN]]()
5453
// CHECK-NEXT: [[T1:%.*]] = struct_extract [[T0]] : $Holder, #Holder.value
55-
// CHECK-NEXT: [[T2:%.*]] = unmanaged_to_ref [[T1]]
56-
// CHECK-NEXT: strong_retain [[T2]]
54+
// CHECK-NEXT: [[T2:%.*]] = copy_unmanaged_value [[T1]]
5755
// CHECK-NEXT: return [[T2]]

test/SILGen/unmanaged_ownership.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ func get(holder holder: inout Holder) -> C {
5050
// CHECK-NEXT: [[READ:%.*]] = begin_access [read] [unknown] [[ADDR]] : $*Holder
5151
// CHECK-NEXT: [[T0:%.*]] = struct_element_addr [[READ]] : $*Holder, #Holder.value
5252
// CHECK-NEXT: [[T1:%.*]] = load [trivial] [[T0]] : $*@sil_unmanaged C
53-
// CHECK-NEXT: [[T2:%.*]] = unmanaged_to_ref [[T1]]
54-
// CHECK-NEXT: [[T2_COPY:%.*]] = copy_value [[T2]]
53+
// CHECK-NEXT: [[T2:%.*]] = copy_unmanaged_value [[T1]]
5554
// CHECK-NEXT: end_access [[READ]] : $*Holder
56-
// CHECK-NEXT: return [[T2_COPY]]
55+
// CHECK-NEXT: return [[T2]]
5756

5857
func project(fn fn: () -> Holder) -> C {
5958
return fn().value
@@ -63,7 +62,6 @@ func project(fn fn: () -> Holder) -> C {
6362
// CHECK-NEXT: debug_value
6463
// CHECK-NEXT: [[T0:%.*]] = apply [[FN]]()
6564
// CHECK-NEXT: [[T1:%.*]] = struct_extract [[T0]] : $Holder, #Holder.value
66-
// CHECK-NEXT: [[T2:%.*]] = unmanaged_to_ref [[T1]]
67-
// CHECK-NEXT: [[COPIED_T2:%.*]] = copy_value [[T2]]
65+
// CHECK-NEXT: [[T2:%.*]] = copy_unmanaged_value [[T1]]
6866
// CHECK-NOT: destroy_value [[BORROWED_FN_COPY]]
69-
// CHECK-NEXT: return [[COPIED_T2]]
67+
// CHECK-NEXT: return [[T2]]

0 commit comments

Comments
 (0)