Skip to content

Commit 7d86d6f

Browse files
committed
UnsafeGuaranteedPeephole: Use RCIdentityFunctionInfo when matching release
1 parent 17953bf commit 7d86d6f

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

lib/SILOptimizer/Transforms/UnsafeGuaranteedPeephole.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "swift/SIL/SILFunction.h"
3737
#include "swift/SIL/SILInstruction.h"
3838
#include "swift/SIL/SILModule.h"
39+
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
3940
#include "swift/SILOptimizer/PassManager/Transforms.h"
4041
#include "swift/SILOptimizer/Utils/Local.h"
4142

@@ -92,18 +93,21 @@ getSingleUnsafeGuaranteedValueResult(BuiltinInst *BI) {
9293
/// strong_release %6 : $Foo // Ignore.
9394
/// %12 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
9495
///
95-
static SILBasicBlock::iterator
96-
findReleaseToMatchUnsafeGuaranteedValue(SILInstruction *UnsafeGuaranteedEndI,
97-
SILValue UnsafeGuaranteedValue,
98-
SILBasicBlock &BB) {
96+
static SILBasicBlock::iterator findReleaseToMatchUnsafeGuaranteedValue(
97+
SILInstruction *UnsafeGuaranteedEndI, SILValue UnsafeGuaranteedValue,
98+
SILBasicBlock &BB, RCIdentityFunctionInfo &RCIA) {
99+
99100
auto UnsafeGuaranteedEndIIt = SILBasicBlock::iterator(UnsafeGuaranteedEndI);
100101
if (UnsafeGuaranteedEndIIt == BB.begin())
101102
return BB.end();
102103
auto LastReleaseIt = std::prev(UnsafeGuaranteedEndIIt);
104+
auto UnsafeGuaranteedRCIdentityRoot =
105+
RCIA.getRCIdentityRoot(UnsafeGuaranteedValue);
103106
while (LastReleaseIt != BB.begin() &&
104107
(((isa<StrongReleaseInst>(*LastReleaseIt) ||
105108
isa<ReleaseValueInst>(*LastReleaseIt)) &&
106-
LastReleaseIt->getOperand(0) != UnsafeGuaranteedValue &&
109+
RCIA.getRCIdentityRoot(LastReleaseIt->getOperand(0)) !=
110+
UnsafeGuaranteedRCIdentityRoot &&
107111
LastReleaseIt->getOperand(0) !=
108112
cast<SILInstruction>(UnsafeGuaranteedValue)->getOperand(0)) ||
109113
!LastReleaseIt->mayHaveSideEffects() ||
@@ -112,7 +116,8 @@ findReleaseToMatchUnsafeGuaranteedValue(SILInstruction *UnsafeGuaranteedEndI,
112116
--LastReleaseIt;
113117
if ((!isa<StrongReleaseInst>(*LastReleaseIt) &&
114118
!isa<ReleaseValueInst>(*LastReleaseIt)) ||
115-
(LastReleaseIt->getOperand(0) != UnsafeGuaranteedValue &&
119+
(RCIA.getRCIdentityRoot(LastReleaseIt->getOperand(0)) !=
120+
UnsafeGuaranteedRCIdentityRoot &&
116121
LastReleaseIt->getOperand(0) !=
117122
cast<SILInstruction>(UnsafeGuaranteedValue)->getOperand(0))) {
118123
return BB.end();
@@ -122,7 +127,8 @@ findReleaseToMatchUnsafeGuaranteedValue(SILInstruction *UnsafeGuaranteedEndI,
122127

123128
/// Remove retain/release pairs around builtin "unsafeGuaranteed" instruction
124129
/// sequences.
125-
static bool removeGuaranteedRetainReleasePairs(SILFunction &F) {
130+
static bool removeGuaranteedRetainReleasePairs(SILFunction &F,
131+
RCIdentityFunctionInfo &RCIA) {
126132
bool Changed = false;
127133
for (auto &BB : F) {
128134
auto It = BB.begin(), End = BB.end();
@@ -216,7 +222,8 @@ static bool removeGuaranteedRetainReleasePairs(SILFunction &F) {
216222
// Find the release to match with the unsafeGuaranteedValue.
217223
auto &UnsafeGuaranteedEndBB = *UnsafeGuaranteedEndI->getParent();
218224
auto LastReleaseIt = findReleaseToMatchUnsafeGuaranteedValue(
219-
UnsafeGuaranteedEndI, UnsafeGuaranteedValue, UnsafeGuaranteedEndBB);
225+
UnsafeGuaranteedEndI, UnsafeGuaranteedValue, UnsafeGuaranteedEndBB,
226+
RCIA);
220227
if (LastReleaseIt == UnsafeGuaranteedEndBB.end()) {
221228
DEBUG(llvm::dbgs() << " no release before unsafeGuaranteedEnd found\n");
222229
continue;
@@ -258,7 +265,8 @@ namespace {
258265
class UnsafeGuaranteedPeephole : public swift::SILFunctionTransform {
259266

260267
void run() override {
261-
if (removeGuaranteedRetainReleasePairs(*getFunction()))
268+
auto &RCIA = *getAnalysis<RCIdentityAnalysis>()->get(getFunction());
269+
if (removeGuaranteedRetainReleasePairs(*getFunction(), RCIA))
262270
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
263271
}
264272

test/SILOptimizer/unsafe_guaranteed_peephole.sil

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ sil_stage canonical
44
import Builtin
55
import Swift
66

7-
public class Foo {
7+
public class Base {
88
public func beep()
99
}
1010

11+
public class Foo : Base {
12+
public override func beep()
13+
}
14+
1115
sil @beep : $@convention(thin) () -> ()
1216
sil @beep2 : $@convention(thin) (@owned Foo) -> ()
1317

@@ -367,3 +371,34 @@ bb0(%0 : $Foo, %1: $*Builtin.Int32, %2: $Builtin.Int32):
367371
%17 = tuple ()
368372
return %17 : $()
369373
}
374+
375+
// CHECK-LABEL: sil @testUnsafeGuaranteed_cast_inst
376+
// CHECK: bb0([[P:%.*]] : $Foo
377+
// CHECK-NOT: retain
378+
// CHECK-NOT: unsafeGuaranteed
379+
// CHECK: [[U:%.*]] = upcast [[P]] : $Foo to $Base
380+
// CHECK: [[M:%.*]] = class_method [[U]] : $Base, #Base.beep
381+
// CHECK: apply [[M]]([[U]])
382+
// CHECK-NOT: release
383+
// CHECK-NOT: unsafeGuaranteedEnd
384+
// CHECK: [[T:%.*]] = tuple ()
385+
// CHECK: return [[T]]
386+
// CHECK: }
387+
sil @testUnsafeGuaranteed_cast_inst : $@convention(method) (@guaranteed Foo, Builtin.Int32) -> () {
388+
bb0(%0 : $Foo, %1: $Builtin.Int32):
389+
strong_retain %0 : $Foo
390+
debug_value %0 : $Foo
391+
%3 = struct $MyInt(%1 : $Builtin.Int32)
392+
%4 = builtin "unsafeGuaranteed"<Foo>(%0 : $Foo) : $(Foo, Builtin.Int8)
393+
%5 = tuple_extract %4 : $(Foo, Builtin.Int8), 0
394+
%6 = tuple_extract %4 : $(Foo, Builtin.Int8), 1
395+
%7 = upcast %5 : $Foo to $Base
396+
%19 = class_method %7 : $Base, #Base.beep!1 : (Base) -> () -> () , $@convention(method) (@guaranteed Base) -> ()
397+
%20 = apply %19(%7) : $@convention(method) (@guaranteed Base) -> ()
398+
strong_release %7 : $Base
399+
%21 = struct $MyInt(%1 : $Builtin.Int32)
400+
debug_value %5 : $Foo
401+
%16 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
402+
%17 = tuple ()
403+
return %17 : $()
404+
}

0 commit comments

Comments
 (0)