Skip to content

[ownership] Do not lower copy_unowned_value to strong_retain_unowned. #17567

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
2 changes: 1 addition & 1 deletion include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t VERSION_MINOR = 421; // Last change: track whether xrefs come from Clang
const uint16_t VERSION_MINOR = 422; // Last change: {strong_retain,copy}_unowned

using DeclIDField = BCFixed<31>;

Expand Down
16 changes: 13 additions & 3 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,9 +965,7 @@ class IRGenSILFunction :
void visitRetainValueInst(RetainValueInst *i);
void visitRetainValueAddrInst(RetainValueAddrInst *i);
void visitCopyValueInst(CopyValueInst *i);
void visitCopyUnownedValueInst(CopyUnownedValueInst *i) {
llvm_unreachable("unimplemented");
}
void visitCopyUnownedValueInst(CopyUnownedValueInst *i);
void visitReleaseValueInst(ReleaseValueInst *i);
void visitReleaseValueAddrInst(ReleaseValueAddrInst *i);
void visitDestroyValueInst(DestroyValueInst *i);
Expand Down Expand Up @@ -3841,6 +3839,18 @@ visitStrongRetainUnownedInst(swift::StrongRetainUnownedInst *i) {
: irgen::Atomicity::NonAtomic);
}

void IRGenSILFunction::visitCopyUnownedValueInst(
swift::CopyUnownedValueInst *i) {
Explosion in = getLoweredExplosion(i->getOperand());
auto &ti = getReferentTypeInfo(*this, i->getOperand()->getType());
ti.strongRetainUnowned(*this, in, irgen::Atomicity::Atomic);
// Semantically we are just passing through the input parameter but as a
// strong reference... at LLVM IR level these type differences don't
// matter. So just set the lowered explosion appropriately.
Explosion output = getLoweredExplosion(i->getOperand());
setLoweredExplosion(i, output);
}

void IRGenSILFunction::visitUnownedRetainInst(swift::UnownedRetainInst *i) {
Explosion lowered = getLoweredExplosion(i->getOperand());
auto &ti = getReferentTypeInfo(*this, i->getOperand()->getType());
Expand Down
3 changes: 2 additions & 1 deletion lib/SIL/SILBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ static bool couldReduceStrongRefcount(SILInstruction *Inst) {
isa<RetainValueInst>(Inst) || isa<UnownedRetainInst>(Inst) ||
isa<UnownedReleaseInst>(Inst) || isa<StrongRetainUnownedInst>(Inst) ||
isa<StoreWeakInst>(Inst) || isa<StrongRetainInst>(Inst) ||
isa<AllocStackInst>(Inst) || isa<DeallocStackInst>(Inst))
isa<AllocStackInst>(Inst) || isa<DeallocStackInst>(Inst) ||
isa<CopyUnownedValueInst>(Inst))
return false;

// Assign and copyaddr of trivial types cannot drop refcounts, and 'inits'
Expand Down
5 changes: 2 additions & 3 deletions lib/SIL/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,9 +1868,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
"Operand of unowned_retain");
require(unownedType->isLoadable(ResilienceExpansion::Maximal),
"unowned_retain requires unowned type to be loadable");
require(F.hasQualifiedOwnership(),
"copy_unowned_value is only valid in functions with qualified "
"ownership");
// *NOTE* We allow copy_unowned_value to be used throughout the entire
// pipeline even though it is a higher level instruction.
}

void checkDestroyValueInst(DestroyValueInst *I) {
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Analysis/EscapeAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ void EscapeAnalysis::analyzeInstruction(SILInstruction *I,
case SILInstructionKind::DeallocStackInst:
case SILInstructionKind::StrongRetainInst:
case SILInstructionKind::StrongRetainUnownedInst:
case SILInstructionKind::CopyUnownedValueInst:
case SILInstructionKind::RetainValueInst:
case SILInstructionKind::UnownedRetainInst:
case SILInstructionKind::BranchInst:
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Analysis/MemoryBehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class MemoryBehaviorVisitor
}
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetainInst)
REFCOUNTINC_MEMBEHAVIOR_INST(StrongRetainUnownedInst)
REFCOUNTINC_MEMBEHAVIOR_INST(CopyUnownedValueInst)
REFCOUNTINC_MEMBEHAVIOR_INST(UnownedRetainInst)
REFCOUNTINC_MEMBEHAVIOR_INST(RetainValueInst)
#undef REFCOUNTINC_MEMBEHAVIOR_INST
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Analysis/SideEffectAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ void FunctionSideEffects::analyzeInstruction(SILInstruction *I) {
return;
case SILInstructionKind::StrongRetainInst:
case SILInstructionKind::StrongRetainUnownedInst:
case SILInstructionKind::CopyUnownedValueInst:
case SILInstructionKind::RetainValueInst:
case SILInstructionKind::UnownedRetainInst:
getEffectsOn(I->getOperand(0))->Retains = true;
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Mandatory/GuaranteedARCOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static bool couldReduceStrongRefcount(SILInstruction *Inst) {
if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
isa<RetainValueInst>(Inst) || isa<UnownedRetainInst>(Inst) ||
isa<UnownedReleaseInst>(Inst) || isa<StrongRetainUnownedInst>(Inst) ||
isa<CopyUnownedValueInst>(Inst) ||
isa<StoreWeakInst>(Inst) || isa<StrongRetainInst>(Inst) ||
isa<AllocStackInst>(Inst) || isa<DeallocStackInst>(Inst) ||
isa<BeginAccessInst>(Inst) || isa<EndAccessInst>(Inst) ||
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Transforms/DeadStoreElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ static bool isDeadStoreInertInstruction(SILInstruction *Inst) {
switch (Inst->getKind()) {
case SILInstructionKind::StrongRetainInst:
case SILInstructionKind::StrongRetainUnownedInst:
case SILInstructionKind::CopyUnownedValueInst:
case SILInstructionKind::UnownedRetainInst:
case SILInstructionKind::RetainValueInst:
case SILInstructionKind::DeallocStackInst:
Expand Down
14 changes: 0 additions & 14 deletions lib/SILOptimizer/Transforms/OwnershipModelEliminator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ struct OwnershipModelEliminatorVisitor
bool visitStoreInst(StoreInst *SI);
bool visitStoreBorrowInst(StoreBorrowInst *SI);
bool visitCopyValueInst(CopyValueInst *CVI);
bool visitCopyUnownedValueInst(CopyUnownedValueInst *CVI);
bool visitDestroyValueInst(DestroyValueInst *DVI);
bool visitLoadBorrowInst(LoadBorrowInst *LBI);
bool visitBeginBorrowInst(BeginBorrowInst *BBI) {
Expand Down Expand Up @@ -160,19 +159,6 @@ bool OwnershipModelEliminatorVisitor::visitCopyValueInst(CopyValueInst *CVI) {
return true;
}

bool OwnershipModelEliminatorVisitor::visitCopyUnownedValueInst(
CopyUnownedValueInst *CVI) {
B.createStrongRetainUnowned(CVI->getLoc(), CVI->getOperand(),
B.getDefaultAtomicity());
// Users of copy_value_unowned expect an owned value. So we need to convert
// our unowned value to a ref.
auto *UTRI =
B.createUnownedToRef(CVI->getLoc(), CVI->getOperand(), CVI->getType());
CVI->replaceAllUsesWith(UTRI);
CVI->eraseFromParent();
return true;
}

bool OwnershipModelEliminatorVisitor::visitUnmanagedRetainValueInst(
UnmanagedRetainValueInst *URVI) {
// Now that we have set the unqualified ownership flag, destroy value
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Transforms/RedundantLoadElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ static bool isRLEInertInstruction(SILInstruction *Inst) {
case SILInstructionKind::IsUniqueInst:
case SILInstructionKind::IsUniqueOrPinnedInst:
case SILInstructionKind::FixLifetimeInst:
case SILInstructionKind::CopyUnownedValueInst:
return true;
default:
return false;
Expand Down
14 changes: 13 additions & 1 deletion test/IRGen/copy_value_destroy_value.sil
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ bb0(%0 : $Builtin.Int32):
// CHECK: call %swift.refcounted* @swift_retain(%swift.refcounted* returned [[VAL2]])
// CHECK: call void @swift_release(%swift.refcounted* [[VAL1]])
// CHECK: call void @swift_release(%swift.refcounted* [[VAL2]])
sil @non_trivial : $@convention(thin) (Foo) -> () {
sil @non_trivial : $@convention(thin) (@guaranteed Foo) -> () {
bb0(%0 : $Foo):
%1 = copy_value %0 : $Foo
destroy_value %1 : $Foo
%2 = tuple()
return %2 : $()
}

// CHECK: define{{( protected)?}} swiftcc void @non_trivial_unowned(
// CHECK: call %swift.refcounted* @swift_unownedRetainStrong(%swift.refcounted* returned %0)
// CHECK: call void @swift_release(%swift.refcounted* %0)
sil @non_trivial_unowned : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : $Builtin.NativeObject):
%1 = ref_to_unowned %0 : $Builtin.NativeObject to $@sil_unowned Builtin.NativeObject
%2 = copy_unowned_value %1 : $@sil_unowned Builtin.NativeObject
destroy_value %2 : $Builtin.NativeObject
%9999 = tuple()
return %9999 : $()
}
44 changes: 44 additions & 0 deletions test/Interpreter/strong_retain_unowned_mispairing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// RUN: %target-run-simple-opt-O-swift
// REQUIRES: executable_test

// We were crashing here due to not preserving rc identity.
// rdar://41328987

func takeEscaping(closure: @escaping (String) -> Void) {}

public class Helper {
weak var o: P?

@_optimize(none)
init(o: P) {
self.o = o
}
}

protocol P: class {}

public class Binding: P {
private var helper: Helper?

public init() {
helper = Helper(o: self)

// Listen to model changes
takeEscaping { [unowned self] (value: String) in
self.update()
}

takeEscaping { [unowned self] (value: String) in
self.update()
}
}

func update() {}
}

@_optimize(none)
func testCrash() {
_ = Binding()
}

testCrash()
7 changes: 4 additions & 3 deletions test/SILOptimizer/ownership_model_eliminator.sil
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ bb0(%0 : @owned $Builtin.NativeObject):
return %9999 : $()
}

// We no longer lower copy_unowned_value. So make sure that we actually don't.
//
// CHECK-LABEL: sil @copy_unowned_value_test : $@convention(thin) (@owned @sil_unowned Builtin.NativeObject) -> () {
// CHECK: bb0([[ARG:%.*]] : $@sil_unowned Builtin.NativeObject):
// CHECK-NEXT: strong_retain_unowned [[ARG]] : $@sil_unowned Builtin.NativeObject
// CHECK-NEXT: [[OWNED_ARG:%.*]] = unowned_to_ref [[ARG]] : $@sil_unowned Builtin.NativeObject to $Builtin.NativeObject
// CHECK-NEXT: strong_release [[OWNED_ARG]] : $Builtin.NativeObject
// CHECK-NEXT: [[STRONG:%.*]] = copy_unowned_value [[ARG]] : $@sil_unowned Builtin.NativeObject
// CHECK-NEXT: strong_release [[STRONG]] : $Builtin.NativeObject
// CHECK-NEXT: unowned_release [[ARG]] : $@sil_unowned Builtin.NativeObject
// CHECK-NEXT: tuple ()
// CHECK-NEXT: return
Expand Down
14 changes: 14 additions & 0 deletions test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,18 @@ if not getattr(config, 'target_run_simple_swift', None):
'%s %%t/a.out &&'
'%s %%t/a.out'
% (config.target_build_swift, mcp_opt, config.target_codesign, config.target_run))
config.target_run_simple_opt_O_swift = (
'%%empty-directory(%%t) && '
'%s %s -O %%s -o %%t/a.out -module-name main && '
'%s %%t/a.out &&'
'%s %%t/a.out'
% (config.target_build_swift, mcp_opt, config.target_codesign, config.target_run))
config.target_run_simple_opt_Osize_swift = (
'%%empty-directory(%%t) && '
'%s %s -Osize %%s -o %%t/a.out -module-name main && '
'%s %%t/a.out &&'
'%s %%t/a.out'
% (config.target_build_swift, mcp_opt, config.target_codesign, config.target_run))
config.target_run_simple_swift_swift3 = (
'%%empty-directory(%%t) && '
'%s %s %%s -o %%t/a.out -module-name main -swift-version 3 && '
Expand Down Expand Up @@ -1097,6 +1109,8 @@ config.substitutions.append(('%target-run-simple-swiftgyb-swift3', config.target
config.substitutions.append(('%target-run-simple-swiftgyb', config.target_run_simple_swiftgyb))
config.substitutions.append(('%target-run-simple-swift-swift3', config.target_run_simple_swift_swift3))
config.substitutions.append(('%target-run-simple-swift', config.target_run_simple_swift))
config.substitutions.append(('%target-run-simple-opt-O-swift', config.target_run_simple_opt_O_swift))
config.substitutions.append(('%target-run-simple-opt-Osize-swift', config.target_run_simple_opt_Osize_swift))
config.substitutions.append(('%target-run-stdlib-swiftgyb-swift3', config.target_run_stdlib_swiftgyb_swift3))
config.substitutions.append(('%target-run-stdlib-swiftgyb', config.target_run_stdlib_swiftgyb))
config.substitutions.append(('%target-run-stdlib-swift-swift3', config.target_run_stdlib_swift_swift3))
Expand Down