Skip to content

Commit d172129

Browse files
committed
Improve @guaranteed args handling in ARCSequenceOpts
@guaranteed args are treated conservatively in ARCSequenceOpts. A function call with a @guaranteed arg is assumed to decrement refcount of the @guaranteed arg. With this change, use swift::mayDecrementRefCount which uses EscapeAnalysis to determine if we need to transition conservatively at the instruction being visited.
1 parent 9bbe6e7 commit d172129

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/SILOptimizer/ARC/RefCountState.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,14 @@ bool BottomUpRefCountState::handlePotentialGuaranteedUser(
365365
if (!mayGuaranteedUseValue(PotentialGuaranteedUser, getRCRoot(), AA))
366366
return false;
367367

368+
// If we can prove that the pointer we are tracking cannot be decremented,
369+
// return. On return, BottomUpRefCountState::handlePotentialUser can correctly
370+
// handle transition of refcount state. It transitions from a Decrement
371+
// refcount state to a MighBeUsed refcount state
372+
if (!mayDecrementRefCount(PotentialGuaranteedUser, getRCRoot(), AA)) {
373+
return false;
374+
}
375+
368376
// Instructions that we do not recognize (and thus will not move) and that
369377
// *must* use RCIdentity, implies we are always known safe as long as meet
370378
// over all path constraints are satisfied.
@@ -816,6 +824,13 @@ bool TopDownRefCountState::handlePotentialGuaranteedUser(
816824
if (!mayGuaranteedUseValue(PotentialGuaranteedUser, getRCRoot(), AA))
817825
return false;
818826

827+
// If we can prove that the pointer we are tracking cannot be decremented,
828+
// return. On return, TopDownRefCountState::handlePotentialUser can correctly
829+
// handle transition of refcount state.
830+
if (!mayDecrementRefCount(PotentialGuaranteedUser, getRCRoot(), AA)) {
831+
return false;
832+
}
833+
819834
// Otherwise, update our step given that we have a potential decrement.
820835
return handleGuaranteedUser(PotentialGuaranteedUser, getRCRoot(),
821836
SetFactory, AA);

test/SILOptimizer/arcsequenceopts.sil

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,53 @@ bb0(%0 : $*Builtin.Int32):
395395
return %4: $()
396396
}
397397

398+
struct Int {
399+
var value : Builtin.Int64
400+
}
401+
402+
sil @guaranteed_call : $@convention(thin) (@guaranteed <τ_0_0> { var τ_0_0 } <Builtin.Int64>) -> () {
403+
bb0(%0 : $<τ_0_0> { var τ_0_0 } <Builtin.Int64>):
404+
%1 = tuple ()
405+
return %1 : $()
406+
}
407+
408+
// CHECK_LABEL: sil hidden [noinline] @$test_guaranteed_call :
409+
// CHECK: bb1{{.*}}:
410+
// CHECK-NOT: strong_retain
411+
// CHECK: apply
412+
// CHECK-NOT: strong_release
413+
// CHECK: bb2:
414+
// CHECK_LABEL: } // end sil function '$test_guaranteed_call'
415+
sil hidden [noinline] @$test_guaranteed_call : $@convention(thin) () -> () {
416+
bb0:
417+
%box = alloc_box $<τ_0_0> { var τ_0_0 } <Builtin.Int64>
418+
%proj = project_box %box : $<τ_0_0> { var τ_0_0 } <Builtin.Int64>, 0
419+
%0 = integer_literal $Builtin.Int64, 1
420+
%1 = integer_literal $Builtin.Int64, 100
421+
%funcref = function_ref @guaranteed_call : $@convention(thin) (@guaranteed <τ_0_0> { var τ_0_0 } <Builtin.Int64>) -> ()
422+
%3 = struct $Int (%0 : $Builtin.Int64)
423+
%6 = integer_literal $Builtin.Int1, -1
424+
br bb1(%0 : $Builtin.Int64)
425+
426+
bb1(%8 : $Builtin.Int64):
427+
%9 = builtin "sadd_with_overflow_Int64"(%8 : $Builtin.Int64, %0 : $Builtin.Int64, %6 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
428+
%10 = tuple_extract %9 : $(Builtin.Int64, Builtin.Int1), 0
429+
%11 = struct $Int (%10 : $Builtin.Int64)
430+
%12 = builtin "cmp_eq_Int64"(%10 : $Builtin.Int64, %1 : $Builtin.Int64) : $Builtin.Int1
431+
strong_retain %box : $<τ_0_0> { var τ_0_0 } <Builtin.Int64>
432+
apply %funcref (%box) : $@convention(thin) (@guaranteed <τ_0_0> { var τ_0_0 } <Builtin.Int64>) -> ()
433+
strong_release %box : $<τ_0_0> { var τ_0_0 } <Builtin.Int64>
434+
cond_br %12, bb3, bb2
435+
436+
bb2:
437+
br bb1(%10 : $Builtin.Int64)
438+
439+
bb3:
440+
strong_release %box : $<τ_0_0> { var τ_0_0 } <Builtin.Int64>
441+
%17 = tuple ()
442+
return %17 : $()
443+
}
444+
398445
// CHECK-LABEL: sil @silargument_retain_iterated : $@convention(thin) (@owned <τ_0_0> { var τ_0_0 } <Builtin.Int32>) -> ()
399446
// CHECK: bb0
400447
// CHECK-NEXT: function_ref user

0 commit comments

Comments
 (0)