Skip to content

Commit d6a039d

Browse files
authored
Merge pull request #4573 from eeckstein/builtin-arc
AliasAnalysis: improve the may-decrement-ref-count check for builtins.
2 parents 75f4c58 + fb4b2f9 commit d6a039d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/SILOptimizer/Analysis/AliasAnalysis.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,13 @@ bool AliasAnalysis::canApplyDecrementRefCount(FullApplySite FAS, SILValue Ptr) {
673673

674674
bool AliasAnalysis::canBuiltinDecrementRefCount(BuiltinInst *BI, SILValue Ptr) {
675675
for (SILValue Arg : BI->getArguments()) {
676+
677+
// Exclude some types of arguments where Ptr can never escape to.
678+
if (isa<MetatypeInst>(Arg))
679+
continue;
680+
if (Arg->getType().is<BuiltinIntegerType>())
681+
continue;
682+
676683
// A builtin can only release an object if it can escape to one of the
677684
// builtin's arguments.
678685
if (EA->canEscapeToValue(Ptr, Arg))

test/SILOptimizer/retain_release_code_motion.sil

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,28 @@ bb3:
256256
return %5 : $()
257257
}
258258

259+
final class MyArrayBuffer {
260+
@sil_stored var dummyElements: Int32
261+
init()
262+
}
263+
264+
// CHECK-LABEL: sil @builtin_does_not_block_locally_allocated_ref
265+
// CHECK: builtin
266+
// CHECK-NEXT: strong_retain
267+
// CHECK-NEXT: strong_release
268+
// CHECK: return
269+
sil @builtin_does_not_block_locally_allocated_ref : $@convention(thin) () -> @owned MyArrayBuffer {
270+
bb0:
271+
%3 = integer_literal $Builtin.Word, 3
272+
%8 = alloc_ref $MyArrayBuffer
273+
%74 = metatype $@thick String.Type
274+
%67 = ref_element_addr %8 : $MyArrayBuffer, #MyArrayBuffer.dummyElements
275+
%68 = address_to_pointer %67 : $*Int32 to $Builtin.RawPointer
276+
strong_retain %8 : $MyArrayBuffer
277+
%77 = builtin "destroyArray"<String>(%74 : $@thick String.Type, %68 : $Builtin.RawPointer, %3 : $Builtin.Word) : $()
278+
strong_release %8 : $MyArrayBuffer
279+
return %8 : $MyArrayBuffer
280+
}
259281
// CHECK-LABEL: sil @hoist_release_partially_available_retain
260282
// CHECK: bb0
261283
// CHECK: cond_br undef, bb1, bb2

0 commit comments

Comments
 (0)