Skip to content

[4.0] Let Character literals, which fit into 64 bits, be folded into a single integer constant. #9353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions stdlib/public/core/CTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ extension CVaListPointer : CustomDebugStringConvertible {
}
}

@_versioned
@_inlineable
func _memcpy(
dest destination: UnsafeMutableRawPointer,
src: UnsafeMutableRawPointer,
Expand Down
4 changes: 4 additions & 0 deletions stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public struct Character :
UTF32.self, input: CollectionOfOne(UInt32(value))))
}

// Inlining ensures that the whole constructor can be folded away to a single
// integer constant in case of small character literals.
@inline(__always)
@effects(readonly)
public init(
_builtinExtendedGraphemeClusterLiteral start: Builtin.RawPointer,
Expand Down Expand Up @@ -195,6 +198,7 @@ public struct Character :

/// Creates a Character from a String that is already known to require the
/// large representation.
@_versioned
internal init(_largeRepresentationString s: String) {
if let native = s._core.nativeBuffer,
native.start == s._core._baseAddress! {
Expand Down
32 changes: 32 additions & 0 deletions test/SILOptimizer/character_literals.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %target-swift-frontend -parse-as-library -O -emit-ir %s | %FileCheck %s
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib,CPU=x86_64

// This is an end-to-end test to ensure that the optimizer generates
// a simple literal for character literals.

// CHECK-LABEL: define {{.*}}charArray
// CHECK-NOT: {{^}}[^ ]
// CHECK: store i64 9223372036854775649, i64*
// CHECK-NOT: {{^}}[^ ]
// CHECK: store i64 9223372036854775650, i64*
// CHECK-NOT: {{^}}[^ ]
// CHECK: store i64 9223372036854775651, i64*
// CHECK-NOT: {{^}}[^ ]
// CHECK: store i64 9223372036854775652, i64*
// CHECK-NOT: {{^}}[^ ]
// CHECK: ret
public func charArray(_ i: Int) -> [Character] {
return [ "a", "b", "c", "d" ]
}

// CHECK-LABEL: define {{.*}}singleChar
// CHECK: ret {{.*}} 9223372036854775649
public func singleChar() -> Character {
return "a"
}

// CHECK-LABEL: define {{.*}}singleNonAsciiChar
// CHECK: ret {{.*}} 9223372036848850918
public func singleNonAsciiChar() -> Character {
return "日"
}