Files
swift-mirror/test/SILOptimizer/devirt_default_case.swift
Xin Tong 51b1c0bc68 Implement retain, release code motion.
Iterative data flow retain sinking and release hoisting.

This allows us to sink retains and hoist releases across harmless loops. which is
an improvement on the SILCodeMotion retain sinking and release hoisting.

It also separates the duty of moving retain and release with the duty of eliminating them
in ASO.

This should eventually replace RR code motion in SILcodemotion and insertion point
in ARCsequence opts (ASO).

This is the performance difference i get with retain sinking and release hoisting.
After disabling retain release code motion in ASO and SILCodeMotion. we can start to take
those code out once this lands.

I see that we go from 24.5% of time spent in SILOptimizations w.r.t. the whole stdlib compilation
to 25.1%.

Improvement is better (i.e. retain sinking and hoisting releases result in performance gain).

<details open>
  <summary>Regression (7)</summary>

TEST                                                    | OLD_MIN | NEW_MIN | DELTA (%) | SPEEDUP
---                                                     | ---     | ---     | ---       | ---
SetIsSubsetOf                                           | 441     | 510     | +15.7%    | **0.86x**
SetIntersect                                            | 1041    | 1197    | +15.0%    | **0.87x**
BenchLangCallingCFunction                               | 184     | 211     | +14.7%    | **0.87x**
Sim2DArray                                              | 326     | 372     | +14.1%    | **0.88x**
SetIsSubsetOf_OfObjects                                 | 498     | 567     | +13.9%    | **0.88x**
GeekbenchGEMM                                           | 945     | 1022    | +8.2%     | **0.92x**
COWTree                                                 | 3839    | 4181    | +8.9%     | **0.92x(?)**

</details>

<details >
  <summary>Improvement (31)</summary>

TEST                                                    | OLD_MIN | NEW_MIN | DELTA (%) | SPEEDUP
---                                                     | ---     | ---     | ---       | ---
ObjectiveCBridgeFromNSDictionaryAnyObjectToString       | 174526  | 165392  | -5.2%     | **1.06x**
RGBHistogram                                            | 3128    | 2957    | -5.5%     | **1.06x**
ObjectiveCBridgeToNSDictionary                          | 16510   | 15494   | -6.2%     | **1.07x**
LuhnAlgoLazy                                            | 2294    | 2120    | -7.6%     | **1.08x**
DictionarySwapOfObjects                                 | 6477    | 5994    | -7.5%     | **1.08x**
StringRemoveDupes                                       | 1610    | 1485    | -7.8%     | **1.08x**
ObjectiveCBridgeFromNSSetAnyObjectToString              | 159358  | 147824  | -7.2%     | **1.08x**
ObjectiveCBridgeToNSSet                                 | 16191   | 14924   | -7.8%     | **1.08x**
DictionaryHashableClass                                 | 1839    | 1704    | -7.3%     | **1.08x**
DictionaryLiteral                                       | 2906    | 2678    | -7.8%     | **1.09x(?)**
StringUtilsUnderscoreCase                               | 10031   | 9187    | -8.4%     | **1.09x**
LuhnAlgoEager                                           | 2320    | 2113    | -8.9%     | **1.10x**
ObjectiveCBridgeFromNSSetAnyObjectToStringForced        | 99553   | 90348   | -9.2%     | **1.10x**
RIPEMD                                                  | 3327    | 3009    | -9.6%     | **1.11x**
Combos                                                  | 595     | 538     | -9.6%     | **1.11x**
Roman                                                   | 10      | 9       | -10.0%    | **1.11x**
StringUtilsCamelCase                                    | 10783   | 9646    | -10.5%    | **1.12x**
SetIntersect_OfObjects                                  | 2511    | 2182    | -13.1%    | **1.15x**
SwiftStructuresTrie                                     | 28331   | 24339   | -14.1%    | **1.16x**
Dictionary2OfObjects                                    | 3748    | 3115    | -16.9%    | **1.20x**
DictionaryOfObjects                                     | 2473    | 2050    | -17.1%    | **1.21x**
Dictionary                                              | 894     | 737     | -17.6%    | **1.21x**
Dictionary2                                             | 2268    | 1859    | -18.0%    | **1.22x**
StringIteration                                         | 8027    | 6344    | -21.0%    | **1.27x**
Phonebook                                               | 8207    | 6436    | -21.6%    | **1.28x**
BenchLangArray                                          | 119     | 91      | -23.5%    | **1.31x**
LinkedList                                              | 8267    | 6297    | -23.8%    | **1.31x**
StrToInt                                                | 5585    | 4180    | -25.2%    | **1.34x**
Dictionary3OfObjects                                    | 1122    | 831     | -25.9%    | **1.35x**
Dictionary3                                             | 731     | 515     | -29.6%    | **1.42x**
SuperChars                                              | 513353  | 258735  | -49.6%    | **1.98x**
2016-04-18 15:39:17 -07:00

275 lines
6.2 KiB
Swift

// RUN: %target-swift-frontend -O -module-name devirt_default_case -emit-sil %s | FileCheck -check-prefix=CHECK -check-prefix=CHECK-NORMAL %s
// RUN: %target-swift-frontend -O -module-name devirt_default_case -emit-sil -enable-testing %s | FileCheck -check-prefix=CHECK -check-prefix=CHECK-TESTABLE %s
@_silgen_name("action")
func action(_ n:Int) -> ()
// public class
public class Base1 {
@inline(never) func inner() { action(1)}
func middle() { inner() }
// Check that call to Base1.middle cannot be devirtualized
//
// CHECK-LABEL: sil @_TFC19devirt_default_case5Base15outer
// CHECK: class_method
// CHECK: }
public func outer() {
middle()
}
}
// public class
public class Derived1 : Base1 {
override func inner() { action(2) }
@inline(never) final override func middle() { inner() }
}
// private class
private class Base2 {
@inline(never) func inner() { action(3)}
func middle() { inner() }
func outer() { middle() }
}
// private class
private class Derived2 : Base2 {
override func inner() { action(4) }
@inline(never) final override func middle() { inner() }
}
// Check that call to Base2.middle can be devirtualized
//
// CHECK-LABEL: sil @_TF19devirt_default_case9callOuterFSiSi
// CHECK: function_ref @{{.*}}TFC19devirt_default_caseP33_77424841540E67CC820F5E5F7940DCB05Base25inner
// CHECK: function_ref @{{.*}}TFC19devirt_default_caseP33_77424841540E67CC820F5E5F7940DCB08Derived26middle
// CHECK-NOT: class_method
// CHECK: return
public func callOuter(_ x: Int) -> Int {
var o:Base2
if x == 1 {
o = Base2()
} else {
o = Derived2()
}
o.outer()
return x
}
// internl class
class Base3 {
@inline(never) func inner() { action(5)}
@inline(never) func middle() { inner() }
// Check that call to Base3.middle can be devirtualized when not compiling
// for testing.
//
// CHECK-LABEL: sil{{( hidden)?}} [noinline] @_TFC19devirt_default_case5Base35outer
// CHECK: function_ref @{{.*}}TFC19devirt_default_case5Base36middle
// CHECK: function_ref @{{.*}}TFC19devirt_default_caseP{{.*}}8Derived36middle
// CHECK-NORMAL-NOT: class_method
// CHECK-TESTABLE: class_method %0 : $Base3, #Base3.middle!1
// CHECK: }
@inline(never) func outer() {
middle()
}
}
// private class
private class Derived3 : Base3 {
override func inner() { action(6) }
@inline(never) final override func middle() { inner() }
override func outer() {
}
}
class A2 { @inline(never) func f() -> Int { return 0 } }
class B2 : A2 {}
class C2 : A2 {}
class D2: B2 {}
class E2 :C2 {}
class A3 { @inline(never) func f() -> Int { return 0 } }
class B3 : A3 { @inline(never) override func f() -> Int { return 1 }}
class C3 : A3 {}
class D3: C3 {}
class E3 :C3 {}
// CHECK-TESTABLE: sil{{( hidden)?}} [thunk] [always_inline] @_TF19devirt_default_case3fooFCS_2A3Si
public func testfoo1() -> Int {
return foo(E2())
}
public func testfoo3() -> Int {
return foo(E3())
}
class Base4 {
@inline(never)
func test() {
// Check that call to foo() can be devirtualized
//
// CHECK-LABEL: sil{{( hidden)?}} [noinline] @_TFC19devirt_default_case5Base44test
// CHECK: function_ref @{{.*}}TFC19devirt_default_case5Base43foo
// CHECK: function_ref @{{.*}}TFC19devirt_default_case8Derived43foo
// CHECK-NORMAL-NOT: class_method
// CHECK-TESTABLE: class_method %0 : $Base4, #Base4.foo!1
// CHECK: }
foo()
}
@inline(never) func foo() { }
}
// A, C,D,E all use the same implementation.
// B has its own implementation.
@inline(never)
func foo(_ a: A3) -> Int {
// Check that call to A3.f() can be devirtualized.
//
// CHECK-NORMAL: sil{{( hidden)?}} [noinline] @_TTSf4g___TF19devirt_default_case3fooFCS_2A3Si
// CHECK-NORMAL: function_ref @{{.*}}TFC19devirt_default_case2B31f
// CHECK-NORMAL: function_ref @{{.*}}TFC19devirt_default_case2A31f
// CHECK-NORMAL-NOT: class_method
// CHECK: }
return a.f()
}
class Derived4 : Base4 {
@inline(never) override func foo() { }
}
public class Base5 {
@inline(never)
public func test() {
foo()
}
@inline(never) public final func foo() { }
}
class Derived5 : Base5 {
}
public class C6 {
func bar() -> Int { return 1 }
}
class D6 : C6 {
override func bar() -> Int { return 2 }
}
@inline(never)
func check_static_class_devirt(_ c: C6) -> Int {
// Check that C.bar() and D.bar() are devirtualized.
//
// CHECK-LABEL: sil{{( hidden)?}} [noinline] @_TTSf4g___TF19devirt_default_case25check_static_class_devirtFCS_2C6Si
// CHECK: checked_cast_br [exact] %0 : $C6 to $C6
// CHECK: checked_cast_br [exact] %0 : $C6 to $D6
// CHECK: class_method
// CHECK: return
return c.bar()
}
public func test_check_static_class_devirt() -> Int {
return check_static_class_devirt(D6())
}
class A7 { @inline(never) func foo() -> Bool { return false } }
class B7 : A7 { @inline(never) override func foo() -> Bool { return true } }
public func test_check_call_on_downcasted_instance() -> Bool {
return check_call_on_downcasted_instance(B7())
}
@inline(never)
func callIt(_ b3: Base3, _ b4: Base4, _ b5: Base5) {
b3.outer()
b4.test()
b5.test()
}
public func externalEntryPoint() {
callIt(Base3(), Base4(), Base5())
}
public class M {
func foo() -> Int32 {
return 0
}
}
public class M1: M {
@inline(never)
override func foo() -> Int32 {
return 1
}
}
internal class M2: M1 {
@inline(never)
override func foo() -> Int32 {
return 2
}
}
internal class M3: M1 {
@inline(never)
override func foo() -> Int32 {
return 3
}
}
internal class M22: M2 {
@inline(never)
override func foo() -> Int32 {
return 22
}
}
internal class M222: M22 {
@inline(never)
override func foo() -> Int32 {
return 222
}
}
internal class M33: M3 {
@inline(never)
override func foo() -> Int32 {
return 33
}
}
// Check that speculative devirtualization tries to devirtualize the first N
// alternatives, if it has too many.
// The alternatives should be taken in a breadth-first order, starting with
// the static type of the instance.
@inline(never)
public func testSpeculativeDevirtualizationWithTooManyAlternatives(_ c:M1) -> Int32{
return c.foo()
}
@inline(never)
func foo(_ a: A2) -> Int {
return a.f()
}
@inline(never)
func check_call_on_downcasted_instance(_ a: A7) -> Bool {
if a is B7 {
return (a as! B7).foo()
}
return a.foo()
}