Skip to content

Commit 7c4d4b0

Browse files
committed
Fix an edge case in GenericCloner which was not generating end_borrows for store_borrow
Generic Cloner creates store_borrow for in_guaranteed args in OSSA. We use PrunedLiveness to compute the boundary of store_borrow for inserting end_borrows. However, if there is an escape boundary cannot be found, and we insert end_borrows at function exits. FunctionExists are populated while cloning. There is an edgecase, where the Generic Cloner is explicitly creating return instructions, these were not inserted in FunctionExits, thereby missing an end_borrow on that path. This PR fixes it. rdar://99874076
1 parent 2fb3f46 commit 7c4d4b0

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/SILOptimizer/Utils/GenericCloner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ void GenericCloner::visitTerminator(SILBasicBlock *BB) {
189189
getBuilder().createDeallocStack(ASI->getLoc(), ASI);
190190
}
191191
if (ReturnValue) {
192-
getBuilder().createReturn(RI->getLoc(), ReturnValue);
192+
auto *NewReturn = getBuilder().createReturn(RI->getLoc(), ReturnValue);
193+
FunctionExits.push_back(NewReturn);
193194
return;
194195
}
195196
} else if (OrigTermInst->isFunctionExiting()) {

test/SILOptimizer/specialize_ossa.sil

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ bb0(%0 : $*T, %1 : $*XXX<T>):
115115
return %9 : $Int32 // id: %11
116116
}
117117

118+
119+
sil [ossa] [noinline] @XXX_foo_guaranteed_generic_return : $@convention(method) <T> (@in_guaranteed T, @in XXX<T>) -> @out T {
120+
bb0(%0 : $*T, %1 : $*T, %2 : $*XXX<T>):
121+
%3 = address_to_pointer %1 : $*T to $Builtin.RawPointer
122+
%4 = alloc_stack $T
123+
%5 = struct_element_addr %2 : $*XXX<T>, #XXX.m_t
124+
copy_addr [take] %5 to [initialization] %0 : $*T
125+
dealloc_stack %4 : $*T
126+
%t = tuple ()
127+
return %t : $()
128+
}
129+
118130
// Swift.Int32._convertFromBuiltinIntegerLiteral (Swift.Int32.Type)(Builtin.IntLiteral) -> Swift.Int32
119131
sil public_external [ossa] [transparent] @$sSi33_convertFromBuiltinIntegerLiteralySiBI_cSimF : $@convention(thin) (Builtin.IntLiteral, @thin Int32.Type) -> Int32 {
120132
bb0(%0 : $Builtin.IntLiteral, %1 : $@thin Int32.Type):
@@ -231,10 +243,21 @@ bb0(%arg : @guaranteed $Builtin.NativeObject):
231243
apply %9(%15g) : $@convention(thin) (Int32) -> ()
232244
destroy_addr %11g : $*Builtin.NativeObject
233245
dealloc_stack %11g : $*Builtin.NativeObject // id: %17
234-
destroy_addr %0 : $*XXX<Builtin.NativeObject>
235-
dealloc_stack %0 : $*XXX<Builtin.NativeObject> // id: %19
236-
%18 = tuple () // user: %20
237-
return %18 : $() // id: %20
246+
247+
%17 = function_ref @XXX_foo_guaranteed_generic_return : $@convention(method) <T> (@in_guaranteed T, @in XXX<T>) -> @out T
248+
%18 = alloc_stack $Builtin.NativeObject
249+
%19 = alloc_stack $Builtin.NativeObject
250+
%arg3 = copy_value %arg : $Builtin.NativeObject
251+
store %arg3 to [init] %18 : $*Builtin.NativeObject
252+
apply %17<Builtin.NativeObject>(%19, %18, %0) : $@convention(method) <T> (@in_guaranteed T, @in XXX<T>) -> @out T
253+
destroy_addr %18 : $*Builtin.NativeObject
254+
destroy_addr %19 : $*Builtin.NativeObject
255+
dealloc_stack %19 : $*Builtin.NativeObject
256+
dealloc_stack %18 : $*Builtin.NativeObject
257+
258+
dealloc_stack %0 : $*XXX<Builtin.NativeObject>
259+
%t = tuple ()
260+
return %t : $()
238261
}
239262

240263
// specialize.useClosure <A>(fun : () -> A) -> A

0 commit comments

Comments
 (0)