1
- :orphan:
2
1
3
- Design of the Swift optimizer
4
- =============================
2
+ ### Design of the Swift optimizer
5
3
6
4
This document describes the design of the Swift Optimizer. It is intended for
7
5
developers who wish to debug, improve or simply understand what the Swift
8
6
optimizer does. Basic familiarity with the Swift programming language and
9
7
knowledge of compiler optimizations is required.
10
8
11
9
12
- Optimization pipeline overview
13
- ===============================
10
+ ### Optimization pipeline overview
14
11
15
12
The Swift compiler translates textual swift programs into LLVM-IR and uses
16
13
multiple representations in between. The Swift frontend is responsible for
@@ -50,8 +47,8 @@ higher-level optimizations. For example, the ARC optimizer and devirtualizer
50
47
need SSA representation to analyze the program, and dead-code-elimination is a
51
48
prerequisite to the array optimizations.
52
49
53
- The Swift Pass Manager
54
- ======================
50
+ ### The Swift Pass Manager
51
+
55
52
The Swift pass manager is the unit that executes optimization
56
53
passes on the functions in the swift module. Unlike the LLVM optimizer, the
57
54
Swift pass manager does not schedule analysis or optimization passes. The pass
@@ -74,8 +71,8 @@ after specific optimizations and to measure how much time is spent in
74
71
each pass.
75
72
76
73
77
- Optimization passes
78
- ===================
74
+ ### Optimization passes
75
+
79
76
There are two kind of optimization passes in Swift: Function passes, and Module
80
77
passes. Function passes can inspect the entire module but can only modify a
81
78
single function. Function passes can't control the order in which functions in
@@ -92,8 +89,7 @@ type information down the call graph.
92
89
93
90
This is the structure of a simple function pass:
94
91
95
- ::
96
-
92
+ ```
97
93
class CSE : public SILFunctionTransform {
98
94
void run() override {
99
95
// .. do stuff ..
@@ -103,10 +99,10 @@ This is the structure of a simple function pass:
103
99
return "CSE";
104
100
}
105
101
};
102
+ ```
106
103
107
104
108
- Analysis Invalidation
109
- =====================
105
+ ### Analysis Invalidation
110
106
111
107
Swift Analysis are very different from LLVM analysis. Swift analysis are simply
112
108
a cache behind some utility that performs computation. For example, the
@@ -118,10 +114,10 @@ The pass manager will return a pointer to the analysis, and optimization passes
118
114
can query the analysis.
119
115
120
116
The code below requests access to the Dominance analysis.
121
- ::
122
117
118
+ ```
123
119
DominanceAnalysis* DA = getAnalysis<DominanceAnalysis>();
124
-
120
+ ```
125
121
126
122
Passes that transform the IR are required to invalidate the analysis. However,
127
123
optimization passes are not required to know about all the existing analysis.
@@ -138,35 +134,32 @@ The code below invalidates sends a message to all of the analysis saying that
138
134
some instructions (that are not branches or calls) were modified in the function
139
135
that the current function pass is processing.
140
136
141
- .. code-block :: cpp
142
-
137
+ ```
143
138
if (Changed) {
144
139
invalidateAnalysis(InvalidationKind::Instructions);
145
140
}
146
-
141
+ ```
147
142
148
143
The code below is a part of an analysis that responds to invalidation messages.
149
144
The analysis checks if any calls in the program were modified and invalidates
150
145
the cache for the function that was modified.
151
146
152
- .. code-block :: cpp
153
-
147
+ ```
154
148
virtual void invalidate(SILFunction *F,
155
149
InvalidationKind K) override {
156
150
if (K & InvalidationKind::Calls) {
157
151
Storage[F].clear();
158
152
}
159
153
}
160
-
154
+ ```
161
155
162
156
The invalidation traits that passes can invalidate are are:
163
157
1 . Instructions - some instructions were added, deleted or moved.
164
158
2 . Calls - some call sites were added or deleted.
165
159
3 . Branches - branches in the code were added, deleted or modified.
166
160
4 . Functions - Some functions were added or deleted.
167
161
168
- Semantic Tags
169
- =============
162
+ ### Semantic Tags
170
163
171
164
The Swift optimizer has optimization passes that target specific data structures
172
165
in the Swift standard library. For example, one optimization can remove the
@@ -176,14 +169,14 @@ optimization can remove array access bounds checks.
176
169
The Swift optimizer can detect code in the standard library if it is marked with
177
170
special attributes @_ semantics, that identifies the functions.
178
171
179
- This is an example of the ``@_semantics `` attribute as used by Swift Array:
180
-
181
- ::
172
+ This is an example of the * @_ semantics* attribute as used by Swift Array:
182
173
174
+ ```
183
175
@public @_semantics("array.count")
184
176
func getCount() -> Int {
185
177
return _buffer.count
186
178
}
179
+ ```
187
180
188
181
Notice that as soon as we inline functions that have the @_ semantics attribute
189
182
the attribute is lost and the optimizer can't analyze the content of the
@@ -198,14 +191,14 @@ pipeline.
198
191
199
192
Please refer to the document “High-Level SIL Optimizations” for more details.
200
193
201
- Debugging the optimizer
202
- =======================
194
+ ### Debugging the optimizer
195
+
203
196
TODO.
204
197
205
- Whole Module Optimizations
206
- ==========================
198
+ ### Whole Module Optimizations
199
+
207
200
TODO.
208
201
209
- List of passes
210
- ==============
202
+ ### List of passes
203
+
211
204
The updated list of passes is available in the file “Passes.def”.
0 commit comments