Files
swift-mirror/test/Interpreter/SDK/c_pointers.swift
Mark Lacey a7556c89f9 Enable transparent inlining of some generic functions.
Fixes part of <rdar://problem/16196801>.

Inline generic functions, but only when:

- There are no unbound archetypes being substituted (due to various
  assumptions in TypeSubstCloner about having all concrete types).

- When no substitution is an existential (due to
  <rdar://problem/17431105>, <rdar://problem/17544901>, and
  <rdar://problem/17714025>).

This gets things limping along, but we really need to fix the above
limitations so that mandatory inlining never fails.

This doesn't enable inlining generics in the performance inliner. There
is no reason it shouldn't work as well, but there is no compelling
reason to do so now and it could have unintended effects on performance.

Some highlights from PreCommitBench -
O0:
            old (ms)   new (ms)  delta (ms)   speedup
ForLoops    1127.00     294.00      833.00     283.3%
LinkedList   828.00     165.00      663.00     401.8%
R17315246    982.00     288.00      694.00     241.0%
SmallPT     3018.00    1388.00     1630.00     117.4%
StringWalk  1276.00      89.00     1187.00    1333.7%
-- most others improve ~10% --

O3:
            old (ms)   new (ms)  delta (ms)   speedup
Ackermann   4138.00    3724.00      414.00      11.1%
Life          59.00      64.00        5.00      -7.8%
Phonebook   2103.00    1815.00      288.00      15.9%
R17315246    430.00     582.00      152.00     -26.1%
StringWalk  1173.00    1097.00       76.00       6.9%

Ofast:
            old (ms)   new (ms)  delta (ms)   speedup
Ackermann   3505.00    3715.00      210.00      -5.7%
Life          49.00      41.00        8.00      19.5%
Memset       684.00     554.00      130.00      23.5%
Phonebook   2166.00    1769.00      397.00      22.4%
StringWalk   829.00     790.00       39.00       4.9%

I've opened the following to track remaining issues that need to be
fixed before we can inline all transparent function applications:
<rdar://problem/17431105>
<rdar://problem/17544901>
<rdar://problem/17714025>
<rdar://problem/17768777>
<rdar://problem/17768931>
<rdar://problem/17769717>

Swift SVN r20378
2014-07-23 06:29:23 +00:00

126 lines
3.4 KiB
Swift

// RUN: %target-run-simple-swift | FileCheck %s
import Foundation
#if os(OSX)
import AppKit
typealias XXColor = NSColor
#endif
#if os(iOS)
import UIKit
typealias XXColor = UIColor
#endif
// Exercise some common APIs that make use of C pointer arguments.
//
// Typed C pointers
//
let rgb = CGColorSpaceCreateDeviceRGB()
let cgRed = CGColorCreate(rgb, [1.0, 0.0, 0.0, 1.0])
let nsRed = XXColor(CGColor: cgRed)
var r: CGFloat = 0.5, g: CGFloat = 0.5, b: CGFloat = 0.5, a: CGFloat = 0.5
nsRed.getRed(&r, green: &g, blue: &b, alpha: &a)
// CHECK-LABEL: Red is:
println("Red is:")
println("<\(r) \(g) \(b) \(a)>") // CHECK-NEXT: <1.0 0.0 0.0 1.0>
//
// Void C pointers
//
// FIXME: Array type annotation should not be required
let data = NSData(bytes: [1.5, 2.25, 3.125] as [Double],
length: sizeof(Double.self) * 3)
var fromData = [0.25, 0.25, 0.25]
let notFromData = fromData
data.getBytes(&fromData, length: sizeof(Double.self) * 3)
// CHECK-LABEL: Data is:
println("Data is:")
println(fromData[0]) // CHECK-NEXT: 1.5
println(fromData[1]) // CHECK-NEXT: 2.25
println(fromData[2]) // CHECK-NEXT: 3.125
// CHECK-LABEL: Independent data is:
println("Independent data is:")
println(notFromData[0]) // CHECK-NEXT: 0.25
println(notFromData[1]) // CHECK-NEXT: 0.25
println(notFromData[2]) // CHECK-NEXT: 0.25
//
// ObjC pointers
//
class Canary: NSObject {
deinit {
println("died")
}
}
var CanaryAssocObjectHandle: UInt8 = 0
// Attach an associated object with a loud deinit so we can see that the
// error died.
func hangCanary(o: AnyObject) {
objc_setAssociatedObject(o, &CanaryAssocObjectHandle, Canary(),
objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
// CHECK-LABEL: NSError out:
println("NSError out:")
autoreleasepool {
var err: NSError? = NSError()
hangCanary(err!)
if let s = NSString.stringWithContentsOfFile("/hopefully/does/not/exist\u{1B}",
encoding: NSUTF8StringEncoding,
error: &err) {
_preconditionFailure("file should not actually exist")
} else if let err_ = err {
// The original value should have died
// CHECK-NEXT: died
println(err_.code) // CHECK-NEXT: 260
hangCanary(err_)
err = nil
} else {
_preconditionFailure("should have gotten an error")
}
}
// The result error should have died with the autorelease pool
// CHECK-NEXT: died
class DumbString: NSString {
override func characterAtIndex(x: Int) -> unichar { _preconditionFailure("nope") }
override var length: Int { return 0 }
override class func stringWithContentsOfFile(s: String,
encoding: NSStringEncoding,
error: AutoreleasingUnsafeMutablePointer<NSError?>)
-> DumbString? {
error.memory = NSError(domain: "Malicious Mischief", code: 594, userInfo: nil)
return nil
}
}
// CHECK-LABEL: NSError in:
println("NSError in:")
autoreleasepool {
var err: NSError? = nil
DumbString.stringWithContentsOfFile("foo", encoding: NSUTF8StringEncoding,
error: &err)
let err_ = err!
println(err_.domain) // CHECK-NEXT: Malicious Mischief
println(err_.code) // CHECK-NEXT: 594
hangCanary(err_)
err = nil
}
// The result error should have died with the autorelease pool
// CHECK-NEXT: died
let s = "Hello World"
puts(s)
// CHECK-NEXT: Hello World