Skip to content

Commit

Permalink
Merge pull request swiftlang#13707 from moiseev/strideable-pointer-perf
Browse files Browse the repository at this point in the history
🛑[stdlib] Attempt to dispatch pointer + via Strideable conformance
  • Loading branch information
moiseev authored Jan 17, 2018
2 parents b2d4bab + 7751a8c commit 6ac167d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 136 deletions.
132 changes: 7 additions & 125 deletions stdlib/public/core/UnsafePointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ extension ${Self}: Strideable {
/// `MemoryLayout<Pointee>.stride` bytes.
@_inlineable
public func successor() -> ${Self} {
return self + 1
return advanced(by: 1)
}

/// Returns a pointer to the previous consecutive instance.
Expand Down Expand Up @@ -950,7 +950,10 @@ extension ${Self}: Strideable {
/// `MemoryLayout<Pointee>.stride`.
@_inlineable
public func distance(to end: ${Self}) -> Int {
return end - self
return
Int(Builtin.sub_Word(Builtin.ptrtoint_Word(end._rawValue),
Builtin.ptrtoint_Word(_rawValue)))
/ MemoryLayout<Pointee>.stride
}

/// Returns a pointer offset from this pointer by the specified number of
Expand All @@ -970,7 +973,8 @@ extension ${Self}: Strideable {
/// `Pointee` type.
@_inlineable
public func advanced(by n: Int) -> ${Self} {
return self + n
return ${Self}(Builtin.gep_Word(
self._rawValue, n._builtinWordValue, Pointee.self))
}
}

Expand Down Expand Up @@ -1005,128 +1009,6 @@ extension ${Self} : CustomPlaygroundQuickLookable {
}
}

// - Note: The following family of operator overloads are redundant
// with Strideable. However, optimizer improvements are needed
// before they can be removed without affecting performance.
extension ${Self} {
/// Creates a new pointer, offset from a pointer by a specified number of
/// instances of the pointer's `Pointee` type.
///
/// You use the addition operator (`+`) to advance a pointer by a number of
/// contiguous instances. The resulting pointer must be within the bounds of
/// the same allocation as `lhs`.
///
/// - Parameters:
/// - lhs: A pointer.
/// - rhs: The number of strides of the pointer's `Pointee` type to offset
/// `lhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
/// - Returns: A pointer offset from `lhs` by `rhs` instances of the
/// `Pointee` type.
@_inlineable // FIXME(sil-serialize-all)
@_transparent
public static func + (lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
return ${Self}(Builtin.gep_Word(
lhs._rawValue, rhs._builtinWordValue, Pointee.self))
}

/// Creates a new pointer, offset from a pointer by a specified number of
/// instances of the pointer's `Pointee` type.
///
/// You use the addition operator (`+`) to advance a pointer by a number of
/// contiguous instances. The resulting pointer must be within the bounds of
/// the same allocation as `rhs`.
///
/// - Parameters:
/// - lhs: The number of strides of the pointer's `Pointee` type to offset
/// `rhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
/// - rhs: A pointer.
/// - Returns: A pointer offset from `rhs` by `lhs` instances of the
/// `Pointee` type.
@_inlineable // FIXME(sil-serialize-all)
@_transparent
public static func + (lhs: Int, rhs: ${Self}<Pointee>) -> ${Self}<Pointee> {
return rhs + lhs
}

/// Creates a new pointer, offset backward from a pointer by a specified
/// number of instances of the pointer's `Pointee` type.
///
/// You use the subtraction operator (`-`) to shift a pointer backward by a
/// number of contiguous instances. The resulting pointer must be within the
/// bounds of the same allocation as `lhs`.
///
/// - Parameters:
/// - lhs: A pointer.
/// - rhs: The number of strides of the pointer's `Pointee` type to offset
/// `lhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
/// - Returns: A pointer offset backward from `lhs` by `rhs` instances of
/// the `Pointee` type.
@_inlineable // FIXME(sil-serialize-all)
@_transparent
public static func - (lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
return lhs + -rhs
}

/// Returns the distance between two pointers, counted as instances of the
/// pointers' `Pointee` type.
///
/// Typed pointers are required to be properly aligned for their `Pointee`
/// type. Proper alignment ensures that the result of the subtraction
/// operator (`-`) accurately measures the distance between the two
/// pointers, counted in strides of `Pointee`. To find the distance in bytes
/// between two pointers, convert them to `UnsafeRawPointer` instances
/// before subtracting.
///
/// - Parameters:
/// - lhs: A pointer.
/// - rhs: Another pointer.
/// - Returns: The distance from `lhs` to `rhs`, in strides of the pointer's
/// `Pointee` type. To access the stride, use
/// `MemoryLayout<Pointee>.stride`.
@_inlineable // FIXME(sil-serialize-all)
@_transparent
public static func - (lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Int {
return
Int(Builtin.sub_Word(Builtin.ptrtoint_Word(lhs._rawValue),
Builtin.ptrtoint_Word(rhs._rawValue)))
/ MemoryLayout<Pointee>.stride
}

/// Advances a pointer by a specified number of instances of the pointer's
/// `Pointee` type.
///
/// You use the addition assignment operator (`+=`) to advance a pointer by a
/// number of contiguous instances. The resulting pointer must be within the
/// bounds of the same allocation as `rhs`.
///
/// - Parameters:
/// - lhs: A pointer to advance in place.
/// - rhs: The number of strides of the pointer's `Pointee` type to offset
/// `lhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
@_inlineable // FIXME(sil-serialize-all)
@_transparent
public static func += (lhs: inout ${Self}<Pointee>, rhs: Int) {
lhs = lhs + rhs
}

/// Shifts a pointer backward by a specified number of instances of the
/// pointer's `Pointee` type.
///
/// You use the subtraction assignment operator (`-=`) to shift a pointer
/// backward by a number of contiguous instances. The resulting pointer must
/// be within the bounds of the same allocation as `rhs`.
///
/// - Parameters:
/// - lhs: A pointer to advance in place.
/// - rhs: The number of strides of the pointer's `Pointee` type to offset
/// `lhs`. To access the stride, use `MemoryLayout<Pointee>.stride`.
@_inlineable // FIXME(sil-serialize-all)
@_transparent
public static func -= (lhs: inout ${Self}<Pointee>, rhs: Int) {
lhs = lhs - rhs
}
}

extension Int {
/// Creates a new value with the bit pattern of the given pointer.
///
Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/bridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ func rdar19770981(_ s: String, ns: NSString) {

// <rdar://problem/19831919> Fixit offers as! conversions that are known to always fail
func rdar19831919() {
var s1 = 1 + "str"; // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and 'String'}} expected-note{{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
var s1 = 1 + "str"; // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and 'String'}} expected-note{{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
}

// <rdar://problem/19831698> Incorrect 'as' fixits offered for invalid literal expressions
func rdar19831698() {
var v70 = true + 1 // expected-error{{binary operator '+' cannot be applied to operands of type 'Bool' and 'Int'}} expected-note {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (UnsafeMutablePointer<Pointee>, Int), (UnsafePointer<Pointee>, Int)}}
var v70 = true + 1 // expected-error{{binary operator '+' cannot be applied to operands of type 'Bool' and 'Int'}} expected-note {{expected an argument list of type '(Int, Int)'}}
var v71 = true + 1.0 // expected-error{{binary operator '+' cannot be applied to operands of type 'Bool' and 'Double'}}
// expected-note@-1{{overloads for '+'}}
var v72 = true + true // expected-error{{binary operator '+' cannot be applied to two 'Bool' operands}}
Expand Down
12 changes: 6 additions & 6 deletions test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func recArea(_ h: Int, w : Int) {
// <rdar://problem/17224804> QoI: Error In Ternary Condition is Wrong
func r17224804(_ monthNumber : Int) {
// expected-error @+2 {{binary operator '+' cannot be applied to operands of type 'String' and 'Int'}}
// expected-note @+1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (UnsafeMutablePointer<Pointee>, Int), (UnsafePointer<Pointee>, Int)}}
// expected-note @+1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
let monthString = (monthNumber <= 9) ? ("0" + monthNumber) : String(monthNumber)
}

Expand Down Expand Up @@ -452,7 +452,7 @@ func testTypeSugar(_ a : Int) {

let x = Stride(a)
x+"foo" // expected-error {{binary operator '+' cannot be applied to operands of type 'Stride' (aka 'Int') and 'String'}}
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
}

// <rdar://problem/21974772> SegFault in FailureDiagnosis::visitInOutExpr
Expand Down Expand Up @@ -922,13 +922,13 @@ let r29850459_a: Int = 0
let r29850459_b: Int = 1
func r29850459() -> Bool { return false }
let _ = (r29850459_flag ? r29850459_a : r29850459_b) + 42.0 // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'Double'}}
// expected-note@-1 {{overloads for '+' exist with these partially matching parameter lists: (Double, Double), (Int, Int), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
// expected-note@-1 {{overloads for '+' exist with these partially matching parameter lists: (Double, Double), (Int, Int)}}
let _ = ({ true }() ? r29850459_a : r29850459_b) + 42.0 // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'Double'}}
// expected-note@-1 {{overloads for '+' exist with these partially matching parameter lists: (Double, Double), (Int, Int), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
// expected-note@-1 {{overloads for '+' exist with these partially matching parameter lists: (Double, Double), (Int, Int)}}
let _ = (r29850459() ? r29850459_a : r29850459_b) + 42.0 // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'Double'}}
// expected-note@-1 {{overloads for '+' exist with these partially matching parameter lists: (Double, Double), (Int, Int), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
// expected-note@-1 {{overloads for '+' exist with these partially matching parameter lists: (Double, Double), (Int, Int)}}
let _ = ((r29850459_flag || r29850459()) ? r29850459_a : r29850459_b) + 42.0 // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'Double'}}
// expected-note@-1 {{overloads for '+' exist with these partially matching parameter lists: (Double, Double), (Int, Int), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
// expected-note@-1 {{overloads for '+' exist with these partially matching parameter lists: (Double, Double), (Int, Int)}}

// SR-6272: Tailored diagnostics with fixits for numerical conversions

Expand Down
4 changes: 2 additions & 2 deletions test/Parse/recovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ protocol FooProtocol {}
func garbage() -> () {
var a : Int
] this line is invalid, but we will stop at the keyword below... // expected-error{{expected expression}}
return a + "a" // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and 'String'}} expected-note {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
return a + "a" // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and 'String'}} expected-note {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
}

func moreGarbage() -> () {
) this line is invalid, but we will stop at the declaration... // expected-error{{expected expression}}
func a() -> Int { return 4 }
return a() + "a" // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and 'String'}} expected-note {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
return a() + "a" // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and 'String'}} expected-note {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
}


Expand Down
2 changes: 1 addition & 1 deletion test/Parse/type_expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func testFunctionCollectionTypes() {
_ = (Int) -> Int // expected-error {{expected member name or constructor call after type name}} expected-note{{use '.self' to reference the type object}}

_ = @convention(c) () -> Int // expected-error{{expected member name or constructor call after type name}} expected-note{{use '.self' to reference the type object}}
_ = 1 + (@convention(c) () -> Int).self // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and '(@convention(c) () -> Int).Type'}} // expected-note {{overloads}}
_ = 1 + (@convention(c) () -> Int).self // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and '(@convention(c) () -> Int).Type'}} // expected-note {{expected an argument list of type '(Int, Int)'}}
_ = (@autoclosure () -> Int) -> (Int, Int).2 // expected-error {{expected type after '->'}}
_ = ((@autoclosure () -> Int) -> (Int, Int)).1 // expected-error {{type '(@autoclosure () -> Int) -> (Int, Int)' has no member '1'}}
_ = ((inout Int) -> Void).self
Expand Down
2 changes: 2 additions & 0 deletions test/SILOptimizer/array_contentof_opt.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// RUN: %target-swift-frontend -O -sil-verify-all -emit-sil %s | %FileCheck %s
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib
// FIXME: <rdar://problem/36587088>
// XFAIL: linux

// This is an end-to-end test of the array(contentsOf) -> array(Element) optimization

Expand Down

0 comments on commit 6ac167d

Please sign in to comment.