Skip to content

Commit f447491

Browse files
authored
Merge pull request swiftlang#22262 from gottesmm/pr-c04b3ed7386cee8a7c8ef42a42bca529a0ef459c
[ownership] Add support for cloning unmanaged_{retain,release}_value …
2 parents bad0eb9 + 70cb465 commit f447491

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,13 @@ template <typename ImplClass>
16101610
void SILCloner<ImplClass>::visitUnmanagedRetainValueInst(
16111611
UnmanagedRetainValueInst *Inst) {
16121612
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1613+
if (!getBuilder().hasOwnership()) {
1614+
return recordClonedInstruction(
1615+
Inst, getBuilder().createRetainValue(getOpLocation(Inst->getLoc()),
1616+
getOpValue(Inst->getOperand()),
1617+
Inst->getAtomicity()));
1618+
}
1619+
16131620
recordClonedInstruction(Inst, getBuilder().createUnmanagedRetainValue(
16141621
getOpLocation(Inst->getLoc()),
16151622
getOpValue(Inst->getOperand()),
@@ -1653,6 +1660,12 @@ template <typename ImplClass>
16531660
void SILCloner<ImplClass>::visitUnmanagedReleaseValueInst(
16541661
UnmanagedReleaseValueInst *Inst) {
16551662
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1663+
if (!getBuilder().hasOwnership()) {
1664+
return recordClonedInstruction(
1665+
Inst, getBuilder().createReleaseValue(getOpLocation(Inst->getLoc()),
1666+
getOpValue(Inst->getOperand()),
1667+
Inst->getAtomicity()));
1668+
}
16561669
recordClonedInstruction(Inst, getBuilder().createUnmanagedReleaseValue(
16571670
getOpLocation(Inst->getLoc()),
16581671
getOpValue(Inst->getOperand()),

test/SILOptimizer/mandatory_inlining_ossa_to_non_ossa.sil

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,30 @@ bb0(%0 : $Builtin.NativeObject):
204204
%9999 = tuple()
205205
return %9999 : $()
206206
}
207+
208+
// Test out functionality that we use to work around weird semantics of
209+
// destructors.
210+
sil [ossa] [transparent] @unmanaged_rr_callee : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
211+
bb0(%0 : @guaranteed $Builtin.NativeObject):
212+
unmanaged_retain_value %0 : $Builtin.NativeObject
213+
unmanaged_release_value %0 : $Builtin.NativeObject
214+
%9999 = tuple()
215+
return %9999 : $()
216+
}
217+
218+
// CHECK-LABEL: sil @unmanaged_rr_caller : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
219+
// CHECK: bb0([[ARG:%.*]] :
220+
// CHECK-NEXT: retain_value [[ARG]]
221+
// CHECK-NEXT: release_value [[ARG]]
222+
// CHECK-NEXT: tuple
223+
// CHECK-NEXT: tuple
224+
// CHECK-NEXT: return
225+
// CHECK: } // end sil function 'unmanaged_rr_caller'
226+
sil @unmanaged_rr_caller : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
227+
bb0(%0 : $Builtin.NativeObject):
228+
%1 = function_ref @unmanaged_rr_callee : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
229+
apply %1(%0) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
230+
%9999 = tuple()
231+
return %9999 : $()
232+
}
233+

0 commit comments

Comments
 (0)