Skip to content

Commit 99f40ee

Browse files
committed
Translate the optimizer design document from RST to Markdown.
1 parent 3579402 commit 99f40ee

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

docs/Optimizer.rst renamed to docs/OptimizerDesign.md

+25-32
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
:orphan:
21

3-
Design of the Swift optimizer
4-
=============================
2+
### Design of the Swift optimizer
53

64
This document describes the design of the Swift Optimizer. It is intended for
75
developers who wish to debug, improve or simply understand what the Swift
86
optimizer does. Basic familiarity with the Swift programming language and
97
knowledge of compiler optimizations is required.
108

119

12-
Optimization pipeline overview
13-
===============================
10+
### Optimization pipeline overview
1411

1512
The Swift compiler translates textual swift programs into LLVM-IR and uses
1613
multiple representations in between. The Swift frontend is responsible for
@@ -50,8 +47,8 @@ higher-level optimizations. For example, the ARC optimizer and devirtualizer
5047
need SSA representation to analyze the program, and dead-code-elimination is a
5148
prerequisite to the array optimizations.
5249

53-
The Swift Pass Manager
54-
======================
50+
### The Swift Pass Manager
51+
5552
The Swift pass manager is the unit that executes optimization
5653
passes on the functions in the swift module. Unlike the LLVM optimizer, the
5754
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
7471
each pass.
7572

7673

77-
Optimization passes
78-
===================
74+
### Optimization passes
75+
7976
There are two kind of optimization passes in Swift: Function passes, and Module
8077
passes. Function passes can inspect the entire module but can only modify a
8178
single function. Function passes can't control the order in which functions in
@@ -92,8 +89,7 @@ type information down the call graph.
9289

9390
This is the structure of a simple function pass:
9491

95-
::
96-
92+
```
9793
class CSE : public SILFunctionTransform {
9894
void run() override {
9995
// .. do stuff ..
@@ -103,10 +99,10 @@ This is the structure of a simple function pass:
10399
return "CSE";
104100
}
105101
};
102+
```
106103

107104

108-
Analysis Invalidation
109-
=====================
105+
### Analysis Invalidation
110106

111107
Swift Analysis are very different from LLVM analysis. Swift analysis are simply
112108
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
118114
can query the analysis.
119115

120116
The code below requests access to the Dominance analysis.
121-
::
122117

118+
```
123119
DominanceAnalysis* DA = getAnalysis<DominanceAnalysis>();
124-
120+
```
125121

126122
Passes that transform the IR are required to invalidate the analysis. However,
127123
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
138134
some instructions (that are not branches or calls) were modified in the function
139135
that the current function pass is processing.
140136

141-
.. code-block:: cpp
142-
137+
```
143138
if (Changed) {
144139
invalidateAnalysis(InvalidationKind::Instructions);
145140
}
146-
141+
```
147142

148143
The code below is a part of an analysis that responds to invalidation messages.
149144
The analysis checks if any calls in the program were modified and invalidates
150145
the cache for the function that was modified.
151146

152-
.. code-block:: cpp
153-
147+
```
154148
virtual void invalidate(SILFunction *F,
155149
InvalidationKind K) override {
156150
if (K & InvalidationKind::Calls) {
157151
Storage[F].clear();
158152
}
159153
}
160-
154+
```
161155

162156
The invalidation traits that passes can invalidate are are:
163157
1. Instructions - some instructions were added, deleted or moved.
164158
2. Calls - some call sites were added or deleted.
165159
3. Branches - branches in the code were added, deleted or modified.
166160
4. Functions - Some functions were added or deleted.
167161

168-
Semantic Tags
169-
=============
162+
### Semantic Tags
170163

171164
The Swift optimizer has optimization passes that target specific data structures
172165
in the Swift standard library. For example, one optimization can remove the
@@ -176,14 +169,14 @@ optimization can remove array access bounds checks.
176169
The Swift optimizer can detect code in the standard library if it is marked with
177170
special attributes @_semantics, that identifies the functions.
178171

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:
182173

174+
```
183175
@public @_semantics("array.count")
184176
func getCount() -> Int {
185177
return _buffer.count
186178
}
179+
```
187180

188181
Notice that as soon as we inline functions that have the @_semantics attribute
189182
the attribute is lost and the optimizer can't analyze the content of the
@@ -198,14 +191,14 @@ pipeline.
198191

199192
Please refer to the document “High-Level SIL Optimizations” for more details.
200193

201-
Debugging the optimizer
202-
=======================
194+
### Debugging the optimizer
195+
203196
TODO.
204197

205-
Whole Module Optimizations
206-
==========================
198+
### Whole Module Optimizations
199+
207200
TODO.
208201

209-
List of passes
210-
==============
202+
### List of passes
203+
211204
The updated list of passes is available in the file “Passes.def”.

0 commit comments

Comments
 (0)