|
| 1 | +// RUN: %target-swift-frontend -Xllvm -swiftmergefunc-threshold=0 -parse-as-library -O -target-cpu core2 -emit-ir %s | %FileCheck %s |
| 2 | +// REQUIRES: optimized_stdlib,CPU=x86_64 |
| 3 | + |
| 4 | +// This is an end-to-end test to ensure that the optimizer generates |
| 5 | +// good code for various ways to create a String from a Sequence of UTF-8 |
| 6 | +// bytes for which a fast path exists. |
| 7 | + |
| 8 | +// Please note: this test targets "core2" to ensure consistent output |
| 9 | +// on all x86 host processors. |
| 10 | + |
| 11 | +@inline(never) |
| 12 | +func blackhole<T>(_ value: T) {} |
| 13 | + |
| 14 | +// UnsafeBufferPointer<UInt8> |
| 15 | +// ========================== |
| 16 | +// CHECK-LABEL: define {{.*}}swiftcc {{.*}}s22utf8_decoding_fastpath15decodeUBPAsUTF8ySSSRys5UInt8VGF{{.*}} |
| 17 | +// CHECK-NOT: _fromCodeUnits |
| 18 | +// CHECK: {{.*}} = call swiftcc {{.*}} @"$sSS18_fromUTF8Repairing{{.*}} |
| 19 | +// CHECK-NOT: _fromCodeUnits |
| 20 | +// CHECK-LAST: ret |
| 21 | +public func decodeUBPAsUTF8(_ ptr: UnsafeBufferPointer<UInt8>) -> String { |
| 22 | + return String(decoding: ptr, as: Unicode.UTF8.self) |
| 23 | +} |
| 24 | + |
| 25 | +// UnsafeMutableBufferPointer<UInt8> |
| 26 | +// ================================= |
| 27 | +// CHECK-LABEL: define {{.*}}swiftcc {{.*}}s22utf8_decoding_fastpath16decodeUMBPAsUTF8ySSSrys5UInt8VGF{{.*}} |
| 28 | +// CHECK-NOT: _fromCodeUnits |
| 29 | +// CHECK: {{.*}} = call swiftcc {{.*}} @"$sSS18_fromUTF8Repairing{{.*}} |
| 30 | +// CHECK-NOT: _fromCodeUnits |
| 31 | +// CHECK-LAST: ret |
| 32 | +public func decodeUMBPAsUTF8(_ ptr: UnsafeMutableBufferPointer<UInt8>) -> String { |
| 33 | + return String(decoding: ptr, as: Unicode.UTF8.self) |
| 34 | +} |
| 35 | + |
| 36 | +// Array<UInt8> |
| 37 | +// ============ |
| 38 | +// CHECK-LABEL: define {{.*}}swiftcc {{.*}}decodeArrayAsUTF8{{.*}} |
| 39 | +// CHECK-NOT: _fromCodeUnits |
| 40 | +// CHECK: {{.*}} = call swiftcc {{.*}} @"$sSS18_fromUTF8Repairing{{.*}} |
| 41 | +// CHECK-NOT: _fromCodeUnits |
| 42 | +// CHECK-LAST: ret |
| 43 | +public func decodeArrayAsUTF8(_ ptr: [UInt8]) -> String { |
| 44 | + return String(decoding: ptr, as: Unicode.UTF8.self) |
| 45 | +} |
| 46 | + |
| 47 | +// UnsafeRawBufferPointer |
| 48 | +// ====================== |
| 49 | +// CHECK-LABEL: define {{.*}}swiftcc {{.*}}decodeURBPAsUTF8{{.*}} |
| 50 | +// CHECK-NOT: _fromCodeUnits |
| 51 | +// CHECK: {{.*}} = call swiftcc {{.*}} @"$sSS18_fromUTF8Repairing{{.*}} |
| 52 | +// CHECK-NOT: _fromCodeUnits |
| 53 | +// CHECK-LAST: ret |
| 54 | +public func decodeURBPAsUTF8(_ ptr: UnsafeRawBufferPointer) -> String { |
| 55 | + return String(decoding: ptr, as: Unicode.UTF8.self) |
| 56 | +} |
| 57 | + |
| 58 | +// UnsafeMutableRawBufferPointer |
| 59 | +// ============================= |
| 60 | +// CHECK-LABEL: define {{.*}}swiftcc {{.*}}decodeUMRBPAsUTF8{{.*}} |
| 61 | +// CHECK-NOT: _fromCodeUnits |
| 62 | +// CHECK: {{.*}} = call swiftcc {{.*}} @"$sSS18_fromUTF8Repairing{{.*}} |
| 63 | +// CHECK-NOT: _fromCodeUnits |
| 64 | +// CHECK-LAST: ret |
| 65 | +public func decodeUMRBPAsUTF8(_ ptr: UnsafeMutableRawBufferPointer) -> String { |
| 66 | + return String(decoding: ptr, as: Unicode.UTF8.self) |
| 67 | +} |
| 68 | + |
| 69 | +// String.UTF8View |
| 70 | +// =============== |
| 71 | +// CHECK-LABEL: define {{.*}}swiftcc {{.*}}decodeStringUTF8ViewAs{{.*}} |
| 72 | +// CHECK-NOT: _fromCodeUnits |
| 73 | +// CHECK: {{.*}} = call swiftcc {{.*}} @"$sSS18_fromUTF8Repairing{{.*}} |
| 74 | +// CHECK-NOT: _fromCodeUnits |
| 75 | +// CHECK-LAST: br |
| 76 | +public func decodeStringUTF8ViewAsUTF8(_ ptr: String.UTF8View) -> String { |
| 77 | + return String(decoding: ptr, as: Unicode.UTF8.self) |
| 78 | +} |
| 79 | + |
| 80 | +// Substring.UTF8View |
| 81 | +// ================== |
| 82 | +// CHECK-LABEL: define {{.*}}swiftcc {{.*}}decodeSubstringUTF8ViewAs{{.*}} |
| 83 | +// CHECK-NOT: _fromCodeUnits |
| 84 | +// CHECK: {{.*}} = call swiftcc {{.*}} @"$sSS18_fromUTF8Repairing{{.*}} |
| 85 | +// CHECK-NOT: _fromCodeUnits |
| 86 | +// CHECK-LAST: br |
| 87 | +public func decodeSubstringUTF8ViewAsUTF8(_ ptr: Substring.UTF8View) -> String { |
| 88 | + return String(decoding: ptr, as: Unicode.UTF8.self) |
| 89 | +} |
| 90 | + |
| 91 | +// Slice<UBP> |
| 92 | +// ========== |
| 93 | +// CHECK-LABEL: define {{.*}}swiftcc {{.*}}decodeUBPSliceAsUTF8{{.*}} |
| 94 | +// CHECK-NOT: _fromCodeUnits |
| 95 | +// CHECK: {{.*}} = call swiftcc {{.*}} @"$sSS18_fromUTF8Repairing{{.*}} |
| 96 | +// CHECK-NOT: _fromCodeUnits |
| 97 | +// CHECK-LAST: br |
| 98 | +public func decodeUBPSliceAsUTF8(_ ptr: Slice<UnsafeBufferPointer<UInt8>>) -> String { |
| 99 | + return String(decoding: ptr, as: Unicode.UTF8.self) |
| 100 | +} |
| 101 | + |
| 102 | +// Slice<URBP> |
| 103 | +// =========== |
| 104 | +// CHECK-LABEL: define {{.*}}swiftcc {{.*}}decodeURBPSliceAsUTF8{{.*}} |
| 105 | +// CHECK-NOT: _fromCodeUnits |
| 106 | +// CHECK: {{.*}} = call swiftcc {{.*}} @"$sSS18_fromUTF8Repairing{{.*}} |
| 107 | +// CHECK-NOT: _fromCodeUnits |
| 108 | +// CHECK: ret |
| 109 | +public func decodeURBPSliceAsUTF8(_ ptr: Slice<UnsafeBufferPointer<UInt8>>) -> String { |
| 110 | + blackhole("foo") // otherwise it just jumps into the Slice<UBP> version |
| 111 | + return String(decoding: ptr, as: Unicode.UTF8.self) |
| 112 | +} |
0 commit comments