Skip to content

Commit 75100a9

Browse files
committed
UnsafeGuaranteedPeephole: Also skip sideeffect free instructions
1 parent f723b86 commit 75100a9

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lib/SILOptimizer/Transforms/UnsafeGuaranteedPeephole.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ findReleaseToMatchUnsafeGuaranteedValue(SILInstruction *UnsafeGuaranteedEndI,
106106
LastReleaseIt->getOperand(0) != UnsafeGuaranteedValue &&
107107
LastReleaseIt->getOperand(0) !=
108108
cast<SILInstruction>(UnsafeGuaranteedValue)->getOperand(0)) ||
109+
!LastReleaseIt->mayHaveSideEffects() ||
109110
isa<DebugValueInst>(*LastReleaseIt) ||
110111
isa<DebugValueInst>(*LastReleaseIt)))
111112
--LastReleaseIt;
@@ -156,6 +157,7 @@ static bool removeGuaranteedRetainReleasePairs(SILFunction &F) {
156157
while (NextInstIter != BB.end() && &*NextInstIter != CurInst &&
157158
(isa<RetainValueInst>(*NextInstIter) ||
158159
isa<StrongRetainInst>(*NextInstIter) ||
160+
!NextInstIter->mayHaveSideEffects() ||
159161
isa<DebugValueInst>(*NextInstIter) ||
160162
isa<DebugValueAddrInst>(*NextInstIter)))
161163
++NextInstIter;

test/SILOptimizer/unsafe_guaranteed_peephole.sil

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,80 @@ bb0(%0 : $Foo):
290290
%17 = tuple ()
291291
return %17 : $()
292292
}
293+
294+
struct MyInt {
295+
@sil_stored var val: Builtin.Int32
296+
}
297+
298+
// CHECK-LABEL: sil @testUnsafeGuaranteed_sideeffectfree_inst
299+
// CHECK: bb0([[P:%.*]] : $Foo
300+
// CHECK-NOT: retain
301+
// CHECK-NOT: unsafeGuaranteed
302+
// CHECK: [[M:%.*]] = class_method [[P]] : $Foo, #Foo.beep
303+
// CHECK: apply [[M]]([[P]])
304+
// CHECK-NOT: release
305+
// CHECK-NOT: unsafeGuaranteedEnd
306+
// CHECK: [[T:%.*]] = tuple ()
307+
// CHECK: return [[T]]
308+
// CHECK: }
309+
sil @testUnsafeGuaranteed_sideeffectfree_inst : $@convention(method) (@guaranteed Foo, Builtin.Int32) -> () {
310+
bb0(%0 : $Foo, %1: $Builtin.Int32):
311+
strong_retain %0 : $Foo
312+
debug_value %0 : $Foo
313+
%3 = struct $MyInt(%1 : $Builtin.Int32)
314+
%4 = builtin "unsafeGuaranteed"<Foo>(%0 : $Foo) : $(Foo, Builtin.Int8)
315+
%5 = tuple_extract %4 : $(Foo, Builtin.Int8), 0
316+
%6 = tuple_extract %4 : $(Foo, Builtin.Int8), 1
317+
%19 = class_method %5 : $Foo, #Foo.beep!1 : (Foo) -> () -> () , $@convention(method) (@guaranteed Foo) -> ()
318+
%20 = apply %19(%5) : $@convention(method) (@guaranteed Foo) -> ()
319+
strong_release %5 : $Foo
320+
%21 = struct $MyInt(%1 : $Builtin.Int32)
321+
debug_value %5 : $Foo
322+
%16 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
323+
%17 = tuple ()
324+
return %17 : $()
325+
}
326+
327+
// CHECK-LABEL: sil @testUnsafeGuaranteed_sideeffect_inst
328+
// CHECK: bb0([[P:%.*]] : $Foo
329+
// CHECK: retain
330+
// CHECK: unsafeGuaranteed
331+
// CHECK: release
332+
// CHECK: unsafeGuaranteedEnd
333+
// CHECK: }
334+
sil @testUnsafeGuaranteed_sideeffect_inst : $@convention(method) (@guaranteed Foo, @inout Builtin.Int32, Builtin.Int32) -> () {
335+
bb0(%0 : $Foo, %1: $*Builtin.Int32, %2: $Builtin.Int32):
336+
strong_retain %0 : $Foo
337+
store %2 to %1 : $*Builtin.Int32
338+
%4 = builtin "unsafeGuaranteed"<Foo>(%0 : $Foo) : $(Foo, Builtin.Int8)
339+
%5 = tuple_extract %4 : $(Foo, Builtin.Int8), 0
340+
%6 = tuple_extract %4 : $(Foo, Builtin.Int8), 1
341+
%19 = class_method %5 : $Foo, #Foo.beep!1 : (Foo) -> () -> () , $@convention(method) (@guaranteed Foo) -> ()
342+
%20 = apply %19(%5) : $@convention(method) (@guaranteed Foo) -> ()
343+
strong_release %5 : $Foo
344+
%16 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
345+
%17 = tuple ()
346+
return %17 : $()
347+
}
348+
349+
// CHECK-LABEL: sil @testUnsafeGuaranteed_sideeffect_inst2
350+
// CHECK: bb0([[P:%.*]] : $Foo
351+
// CHECK: retain
352+
// CHECK: unsafeGuaranteed
353+
// CHECK: release
354+
// CHECK: unsafeGuaranteedEnd
355+
// CHECK: }
356+
sil @testUnsafeGuaranteed_sideeffect_inst2 : $@convention(method) (@guaranteed Foo, @inout Builtin.Int32, Builtin.Int32) -> () {
357+
bb0(%0 : $Foo, %1: $*Builtin.Int32, %2: $Builtin.Int32):
358+
strong_retain %0 : $Foo
359+
%4 = builtin "unsafeGuaranteed"<Foo>(%0 : $Foo) : $(Foo, Builtin.Int8)
360+
%5 = tuple_extract %4 : $(Foo, Builtin.Int8), 0
361+
%6 = tuple_extract %4 : $(Foo, Builtin.Int8), 1
362+
%19 = class_method %5 : $Foo, #Foo.beep!1 : (Foo) -> () -> () , $@convention(method) (@guaranteed Foo) -> ()
363+
%20 = apply %19(%5) : $@convention(method) (@guaranteed Foo) -> ()
364+
strong_release %5 : $Foo
365+
store %2 to %1 : $*Builtin.Int32
366+
%16 = builtin "unsafeGuaranteedEnd"(%6 : $Builtin.Int8) : $()
367+
%17 = tuple ()
368+
return %17 : $()
369+
}

0 commit comments

Comments
 (0)