Skip to content

[silgen] Add SILGen support for emitting copy_unmanaged_value instead… #26843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions lib/SILGen/SILGenBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,14 @@ ManagedValue SILGenBuilder::createCopyValue(SILLocation loc,
SILValue result = createCopy##Name##Value(loc, originalValue.getValue()); \
return SGF.emitManagedRValueWithCleanup(result); \
}
#define UNCHECKED_REF_STORAGE(Name, ...) \
ManagedValue \
SILGenBuilder::createUnsafeCopy##Name##Value(SILLocation loc, \
ManagedValue originalValue) { \
/* *NOTE* The reason why this is unsafe is that we are converting and */ \
/* unconditionally retaining, rather than before converting from */ \
/* type->ref checking that our value is not yet uninitialized. */ \
auto type = originalValue.getType().getAs<Name##StorageType>(); \
SILValue result = create##Name##ToRef( \
loc, originalValue.getValue(), \
SILType::getPrimitiveObjectType(type.getReferentType())); \
result = createCopyValue(loc, result); \
return SGF.emitManagedRValueWithCleanup(result); \
#define UNCHECKED_REF_STORAGE(Name, ...) \
ManagedValue SILGenBuilder::createCopy##Name##Value( \
SILLocation loc, ManagedValue originalValue) { \
/* *NOTE* The reason why this is unsafe is that we are converting and */ \
/* unconditionally retaining, rather than before converting from */ \
/* type->ref checking that our value is not yet uninitialized. */ \
SILValue result = createCopy##Name##Value(loc, originalValue.getValue()); \
return SGF.emitManagedRValueWithCleanup(result); \
}
#include "swift/AST/ReferenceStorage.def"

Expand Down
7 changes: 4 additions & 3 deletions lib/SILGen/SILGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ class SILGenBuilder : public SILBuilder {
using SILBuilder::createCopy##Name##Value; \
ManagedValue createCopy##Name##Value(SILLocation loc, \
ManagedValue originalValue);
#define UNCHECKED_REF_STORAGE(Name, ...) \
ManagedValue createUnsafeCopy##Name##Value(SILLocation loc, \
ManagedValue originalValue);
#define UNCHECKED_REF_STORAGE(Name, ...) \
using SILBuilder::createCopy##Name##Value; \
ManagedValue createCopy##Name##Value(SILLocation loc, \
ManagedValue originalValue);
#include "swift/AST/ReferenceStorage.def"

ManagedValue createOwnedPhiArgument(SILType type);
Expand Down
38 changes: 16 additions & 22 deletions lib/SILGen/SILGenLValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3617,15 +3617,12 @@ SILValue SILGenFunction::emitConversionToSemanticRValue(SILLocation loc,
ResilienceExpansion::Maximal)); \
return B.createCopy##Name##Value(loc, src); \
}
#define UNCHECKED_REF_STORAGE(Name, ...) \
case ReferenceOwnership::Name: { \
/* For static reference storage types, we need to strip the box and */ \
/* then do an (unsafe) retain. */ \
auto type = storageType.castTo<Name##StorageType>(); \
auto result = B.create##Name##ToRef(loc, src, \
SILType::getPrimitiveObjectType(type.getReferentType())); \
/* SEMANTIC ARC TODO: Does this need a cleanup? */ \
return B.createCopyValue(loc, result); \
#define UNCHECKED_REF_STORAGE(Name, ...) \
case ReferenceOwnership::Name: { \
/* For static reference storage types, we need to strip the box and */ \
/* then do an (unsafe) retain. */ \
auto type = storageType.castTo<Name##StorageType>(); \
return B.createCopy##Name##Value(loc, src); \
}
#include "swift/AST/ReferenceStorage.def"
}
Expand All @@ -3647,10 +3644,10 @@ ManagedValue SILGenFunction::emitConversionToSemanticRValue(
case ReferenceOwnership::Name: \
/* Generate a strong retain and strip the box. */ \
return B.createCopy##Name##Value(loc, src);
#define UNCHECKED_REF_STORAGE(Name, ...) \
case ReferenceOwnership::Name: \
/* Strip the box and then do an (unsafe) retain. */ \
return B.createUnsafeCopy##Name##Value(loc, src);
#define UNCHECKED_REF_STORAGE(Name, ...) \
case ReferenceOwnership::Name: \
/* Strip the box and then do an (unsafe) retain. */ \
return B.createCopy##Name##Value(loc, src);
#include "swift/AST/ReferenceStorage.def"
}
llvm_unreachable("impossible");
Expand Down Expand Up @@ -3702,15 +3699,12 @@ static SILValue emitLoadOfSemanticRValue(SILGenFunction &SGF,
} \
ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE_HELPER(Name) \
}
#define UNCHECKED_REF_STORAGE(Name, ...) \
case ReferenceOwnership::Name: { \
/* For static reference storage types, we need to strip the box. */ \
auto type = storageType.castTo<Name##StorageType>(); \
auto value = SGF.B.createLoad(loc, src, LoadOwnershipQualifier::Trivial); \
auto result = SGF.B.create##Name##ToRef(loc, value, \
SILType::getPrimitiveObjectType(type.getReferentType())); \
/* SEMANTIC ARC TODO: Does this need a cleanup? */ \
return SGF.B.createCopyValue(loc, result); \
#define UNCHECKED_REF_STORAGE(Name, ...) \
case ReferenceOwnership::Name: { \
/* For static reference storage types, we need to strip the box. */ \
auto type = storageType.castTo<Name##StorageType>(); \
auto value = SGF.B.createLoad(loc, src, LoadOwnershipQualifier::Trivial); \
return SGF.B.createCopy##Name##Value(loc, value); \
}
#include "swift/AST/ReferenceStorage.def"
#undef ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE_HELPER
Expand Down
6 changes: 2 additions & 4 deletions test/SILGen/unmanaged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func get(holder holder: inout Holder) -> C {
// CHECK-NEXT: [[READ:%.*]] = begin_access [read] [static] [[ADDR]] : $*Holder
// CHECK-NEXT: [[T0:%.*]] = struct_element_addr [[READ]] : $*Holder, #Holder.value
// CHECK-NEXT: [[T1:%.*]] = load [[T0]] : $*@sil_unmanaged C
// CHECK-NEXT: [[T2:%.*]] = unmanaged_to_ref [[T1]]
// CHECK-NEXT: strong_retain [[T2]]
// CHECK-NEXT: [[T2:%.*]] = copy_unmanaged_value [[T1]]
// CHECK-NEXT: end_access [[READ]] : $*Holder
// CHECK-NEXT: return [[T2]]

Expand All @@ -52,6 +51,5 @@ func project(fn fn: () -> Holder) -> C {
// CHECK-NEXT: debug_value
// CHECK-NEXT: [[T0:%.*]] = apply [[FN]]()
// CHECK-NEXT: [[T1:%.*]] = struct_extract [[T0]] : $Holder, #Holder.value
// CHECK-NEXT: [[T2:%.*]] = unmanaged_to_ref [[T1]]
// CHECK-NEXT: strong_retain [[T2]]
// CHECK-NEXT: [[T2:%.*]] = copy_unmanaged_value [[T1]]
// CHECK-NEXT: return [[T2]]
10 changes: 4 additions & 6 deletions test/SILGen/unmanaged_ownership.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ func get(holder holder: inout Holder) -> C {
// CHECK-NEXT: [[READ:%.*]] = begin_access [read] [unknown] [[ADDR]] : $*Holder
// CHECK-NEXT: [[T0:%.*]] = struct_element_addr [[READ]] : $*Holder, #Holder.value
// CHECK-NEXT: [[T1:%.*]] = load [trivial] [[T0]] : $*@sil_unmanaged C
// CHECK-NEXT: [[T2:%.*]] = unmanaged_to_ref [[T1]]
// CHECK-NEXT: [[T2_COPY:%.*]] = copy_value [[T2]]
// CHECK-NEXT: [[T2:%.*]] = copy_unmanaged_value [[T1]]
// CHECK-NEXT: end_access [[READ]] : $*Holder
// CHECK-NEXT: return [[T2_COPY]]
// CHECK-NEXT: return [[T2]]

func project(fn fn: () -> Holder) -> C {
return fn().value
Expand All @@ -63,7 +62,6 @@ func project(fn fn: () -> Holder) -> C {
// CHECK-NEXT: debug_value
// CHECK-NEXT: [[T0:%.*]] = apply [[FN]]()
// CHECK-NEXT: [[T1:%.*]] = struct_extract [[T0]] : $Holder, #Holder.value
// CHECK-NEXT: [[T2:%.*]] = unmanaged_to_ref [[T1]]
// CHECK-NEXT: [[COPIED_T2:%.*]] = copy_value [[T2]]
// CHECK-NEXT: [[T2:%.*]] = copy_unmanaged_value [[T1]]
// CHECK-NOT: destroy_value [[BORROWED_FN_COPY]]
// CHECK-NEXT: return [[COPIED_T2]]
// CHECK-NEXT: return [[T2]]