Skip to content

Commit 3902e8a

Browse files
committed
tests: add an end-to-end test to check that the compiler generates optimal code for static String constants.
1 parent e8f248a commit 3902e8a

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// RUN: %target-swift-frontend -O -emit-ir %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -Osize -emit-ir %s | %FileCheck %s
3+
4+
// RUN: %empty-directory(%t)
5+
// RUN: %target-build-swift -O -module-name=test %s -o %t/a.out
6+
// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT
7+
8+
// REQUIRES: executable_test,swift_stdlib_no_asserts,optimized_stdlib
9+
// REQUIRES: CPU=arm64 || CPU=x86_64
10+
11+
// This is an end-to-end test to ensure that the optimizer generates
12+
// optimal code for static String variables.
13+
14+
public struct S {
15+
// CHECK: {{^@"}}[[SMALL:.*smallstr.*pZ]]" ={{.*}} global {{.*}} inttoptr
16+
public static let smallstr = "abc123a"
17+
// CHECK: {{^@"}}[[LARGE:.*largestr.*pZ]]" ={{.*}} global {{.*}} inttoptr {{.*}} add
18+
public static let largestr = "abc123asd3sdj3basfasdf"
19+
// CHECK: {{^@"}}[[UNICODE:.*unicodestr.*pZ]]" ={{.*}} global {{.*}} inttoptr {{.*}} add
20+
public static let unicodestr = "❄️gastroperiodyni"
21+
}
22+
23+
// unsafeMutableAddressor for S.smallstr
24+
// CHECK: define {{.*smallstr.*}}u"
25+
// CHECK-NEXT: entry:
26+
// CHECK-NEXT: ret {{.*}} @"[[SMALL]]"
27+
// CHECK-NEXT: }
28+
29+
// getter for S.smallstr
30+
// CHECK: define {{.*smallstr.*}}gZ"
31+
// CHECK-NEXT: entry:
32+
// CHECK-NEXT: ret {{.*}}
33+
// CHECK-NEXT: }
34+
35+
// unsafeMutableAddressor for S.largestr
36+
// CHECK: define {{.*largestr.*}}u"
37+
// CHECK-NEXT: entry:
38+
// CHECK-NEXT: ret {{.*}} @"[[LARGE]]"
39+
// CHECK-NEXT: }
40+
41+
// getter for S.largestr
42+
// CHECK: define {{.*largestr.*}}gZ"
43+
// CHECK-NEXT: entry:
44+
// CHECK-NEXT: ret {{.*}}
45+
// CHECK-NEXT: }
46+
47+
// unsafeMutableAddressor for S.unicodestr
48+
// CHECK: define {{.*unicodestr.*}}u"
49+
// CHECK-NEXT: entry:
50+
// CHECK-NEXT: ret {{.*}} @"[[UNICODE]]"
51+
// CHECK-NEXT: }
52+
53+
// getter for S.unicodestr
54+
// CHECK: define {{.*unicodestr.*}}gZ"
55+
// CHECK-NEXT: entry:
56+
// CHECK-NEXT: ret {{.*}}
57+
// CHECK-NEXT: }
58+
59+
// CHECK-LABEL: define {{.*}}get_smallstr
60+
// CHECK: entry:
61+
// CHECK-NEXT: ret {{.*}}
62+
// CHECK-NEXT: }
63+
@inline(never)
64+
public func get_smallstr() -> String {
65+
return S.smallstr
66+
}
67+
68+
// CHECK-LABEL: define {{.*}}get_largestr
69+
// CHECK: entry:
70+
// CHECK-NEXT: ret {{.*}}
71+
// CHECK-NEXT: }
72+
@inline(never)
73+
public func get_largestr() -> String {
74+
return S.largestr
75+
}
76+
77+
// CHECK-LABEL: define {{.*}}get_unicodestr
78+
// CHECK: entry:
79+
// CHECK-NEXT: ret {{.*}}
80+
// CHECK-NEXT: }
81+
@inline(never)
82+
public func get_unicodestr() -> String {
83+
return S.unicodestr
84+
}
85+
86+
// Also check if the generated code is correct.
87+
88+
// CHECK-OUTPUT: abc123a
89+
// CHECK-OUTPUT: abc123asd3sdj3basfasdf
90+
// CHECK-OUTPUT: ❄️gastroperiodyni
91+
print(get_smallstr())
92+
print(get_largestr())
93+
print(get_unicodestr())
94+
95+
// Really load the globals from their addresses.
96+
@_optimize(none)
97+
func print_strings_from_addressors() {
98+
print(S.smallstr)
99+
print(S.largestr)
100+
print(S.unicodestr)
101+
}
102+
103+
// CHECK-OUTPUT: abc123a
104+
// CHECK-OUTPUT: abc123asd3sdj3basfasdf
105+
// CHECK-OUTPUT: ❄️gastroperiodyni
106+
print_strings_from_addressors()
107+

0 commit comments

Comments
 (0)