-
Notifications
You must be signed in to change notification settings - Fork 1
/
Slides-part2_packetization.tex
379 lines (266 loc) · 11.2 KB
/
Slides-part2_packetization.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
\talksection{Packetization Stage}
\begin{frame}{Packetization Overview}
\begin{itemize}
\item Stage that does the actual vectorization: $ F \xrightarrow{N} VF_N$
\begin{itemize}
\item Calling $VF_N$ is like calling $F$, but $N$ times ($N$: SIMD width)
\item Straightforward thanks to preparation from previous stages
\end{itemize}
\item This is done per-instruction, for the whole function
\begin{itemize}
\item Instructions that define a value: define $N$ values, one for each instance
\item Instructions with side effects: perform side effects for each instance
%\item New instruction has the same opcode but different types
\end{itemize}
\item Only \varying{varying} instructions need packetization
\begin{itemize}
\item \uniform{Uniform} instructions can remain scalar, executed once per work-group
\item Depends on UVA to know which instructions to vectorize
\end{itemize}
\end{itemize}
\vspace{1.5ex}
\hspace{1em}\includegraphics[scale=0.55]{images/stages-packet.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Uniform Value Analysis}
Example that combines \uniform{uniform} and \varying{varying} values:
\begin{codebox}[commandchars=\\\[\]]
kernel void add_uniform(int *\uniform[dst], int *\uniform[src], int \uniform[alpha]) {
int \varying[tid] = \varying[get_global_id](0);
\uniform[dst]\idx[\varying[tid]] = \uniform[src]\idx[\varying[tid]] + (\uniform[alpha] - \uniform[1]);
}
\end{codebox}
\begin{codebox}[commandchars=\\\[\]]
define void @add_uniform(i32* \uniform[%dst], i32* \uniform[%src], i32 \uniform[%alpha]) {
entry:
\varying[%tid] = i32 \varying[@get_global_id(i32 0)]
\varying[%arrayidx] = getelementptr i32* \uniform[%src], i32 \varying[%tid]
\varying[%tmp] = load i32* \varying[%arrayidx], align 4
\uniform[%sub] = sub i32 \uniform[%alpha], \uniform[1]
\varying[%add] = add i32 \uniform[%sub], \varying[%tmp]
\varying[%arrayidx2] = getelementptr i32* \uniform[%dst], i32 \varying[%tid]
store i32 \varying[%add1], i32* \varying[%arrayidx2], align 4
ret void
}
\end{codebox}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Uniform Value Analysis}
\begin{itemize}
\item Finds 'root' values
\begin{itemize}
\item \varying{Varying} values with no \varying{varying} operand
\item Example: \varying{get\_global\_id(0)} has a different value for each instance
\end{itemize}
\item Marks each IR value as \uniform{uniform} or \varying{varying}
\begin{itemize}
\item All values start as \uniform{uniform}
\item Marking a value as \varying{varying} causes all users to also be marked \varying{varying}
\item Marking is done recursively, starting with roots
\item Values are marked before their users, to avoid cycles (phi nodes)
\end{itemize}
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]{UVA Example: Start}
\fullpagediagram{0.55}{images/uva-example-start.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]{UVA Example: Propagation}
\fullpagediagram{0.55}{images/uva-example-interm.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]{UVA Example: End}
\fullpagediagram{0.55}{images/uva-example-end.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Memory Addressing}
\begin{minipage}[t]{0.60\linewidth}\begin{itemize}
% XXX: Graph fragment with load and gep and gid
\item Packetization depends on the addressing mode
\begin{itemize}
\item Each memory operation can access $N$ elements
\item Address usually has the form `\uniform{base} + \varying{offset}'
\item Need to evaluate the offset for each of the $N$ lanes
\end{itemize}
\item How are these elements laid out in memory?
\begin{itemize}
\item The layout affects how operations are packetized
\item Most layouts can be described with a single \emph{stride}
\end{itemize}
\item Stride is the distance between successive elements
\begin{itemize}
\item Expressed in number of elements
%\item "How many elements are skipped to get to the next one"
\item One means elements are consecutive
\item Negative means memory offsets are decreasing
\end{itemize}
%\item Not all kinds of memory operations can be expressed in IR
%\begin{itemize}
% \item Generate calls to internal builtins
% \item Internal builtins can be implemented for each target as supported
%\end{itemize}
\end{itemize}
\end{minipage}\begin{minipage}[t]{0.39\linewidth}
\vspace{0.1ex}
\center{\includegraphics[scale=0.50]{images/load-addressing.pdf}}
\end{minipage}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Uniform Memory Addressing}
\begin{itemize}
\item Packetized offset is \uniform{uniform} (e.g. $<3, 3, 3, 3>$)
\item Constant $Stride = 0$
\item Transformed to a regular \emph{scalar} load or store
\end{itemize}
\begin{minipage}[t]{0.40\linewidth}
\vspace{0.1ex}
\begin{codebox}[commandchars=\\\[\]]
int \uniform[*src];
int \uniform[x] = \uniform[src]\idx[\uniform[3]];
\end{codebox}
\end{minipage}
\hspace{2em}
\begin{minipage}[t]{0.49\linewidth}
\vspace{0.6ex}
\begin{figure}
\includegraphics[width=1.0\textwidth]{images/uniform-access.pdf}
\end{figure}
\end{minipage}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Sequential Memory Addressing}
\begin{itemize}
\item Packetized offset is a sequence like $<2, 3, 4, 5>$
\item Constant $Stride = 1$
\item Transformed to a regular \emph{vector} load or store
\end{itemize}
\begin{minipage}[t]{0.40\linewidth}
\vspace{0.1ex}
\begin{codebox}[commandchars=\\\[\]]
int \uniform[*src];
int \varying[tid] = \varying[get_global_id(0)];
int \varying[x] = \uniform[src]\idx[\varying[tid] + \uniform[2]];
\end{codebox}
\end{minipage}
\hspace{2em}
\begin{minipage}[t]{0.49\linewidth}
\vspace{0.6ex}
\begin{figure}
\includegraphics[width=1.0\textwidth]{images/sequential-access.pdf}
\end{figure}
\end{minipage}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Interleaved Memory Addressing}
\begin{itemize}
\item Packetized offset is a sequence like $<0, 2, 4, 6>$
\item Constant $Stride > 1$
\item Transformed to an \emph{interleaved} load or store
\end{itemize}
\begin{minipage}[t]{0.40\linewidth}
\vspace{0.1ex}
\begin{codebox}[commandchars=\\\[\]]
int \uniform[*src];
int \varying[tid] = \varying[get_global_id(0)];
int \varying[even] = \uniform[src]\idx[\varying[tid] * \uniform[2]];
int \varying[odd] = \uniform[src]\idx[(\varying[tid] * \uniform[2]) + \uniform[1]];
\end{codebox}
\end{minipage}
\hspace{2em}
\begin{minipage}[t]{0.49\linewidth}
\vspace{0.6ex}
\begin{figure}
\includegraphics[width=1.0\textwidth]{images/interleaved-access.pdf}
\end{figure}
\end{minipage}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Arbitrary Memory Addressing}
\begin{itemize}
\item Packetized offset can be any sequence (e.g. $<5, 3, 7, 3>$)
\item Variable stride
\item Transformed to a \emph{gather} load or \emph{scatter} store
\end{itemize}
\begin{minipage}[t]{0.40\linewidth}
\vspace{0.1ex}
\begin{codebox}[commandchars=\\\[\]]
int \uniform[*src];
int \uniform[*map]; // {5, 3, 7, 3};
int \varying[tid] = \varying[get_global_id(0)];
int \varying[x] = \uniform[src]\idx[\uniform[map]\idx[\varying[tid]]];
\end{codebox}
\end{minipage}
\hspace{2em}
\begin{minipage}[t]{0.49\linewidth}
\vspace{0.6ex}
\begin{figure}
\includegraphics[width=1.0\textwidth]{images/arbitrary-access.pdf}
\end{figure}
\end{minipage}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Packetization Process}
\begin{itemize}
\item Find leaves
\begin{itemize}
\item Leaves allow \varying{varying} values to 'escape' from the function, they are:
\item Store instructions (\varying{varying} operand)
\item Call instructions (\varying{varying} operand, or call has no use)
\item Return instructions
\end{itemize}
\end{itemize}
\begin{itemize}
\item Recursively packetize leaves and their operands
\begin{itemize}
\item Broadcast \uniform{uniform} values (e.g. argument, constants)
\item Replace \varying{get\_global\_id(0)} with a vector of IDs
\item Packetize operands first, then instruction (top-down)
\item Cache packetized values to prevent duplication
\end{itemize}
\end{itemize}
\begin{itemize}
\item Delete original scalar instructions if dead
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]{Packetization Example}
\fullpagediagram{0.55}{images/uva-example-end.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]{Packetization Example}
\fullpagediagram{0.55}{images/packetization-1.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]{Packetization Example}
\fullpagediagram{0.55}{images/packetization-2.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]{Packetization Example}
\fullpagediagram{0.55}{images/packetization-3.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[c]{Packetization Example}
\fullpagediagram{0.55}{images/packetization-4.pdf}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Packetization Example}
\begin{codebox}[commandchars=\\\[\]]
define void @__v4_add_uniform(i32* \uniform[%dst], i32* \uniform[%src], i32 \uniform[%alpha]) {
entry:
\varying[%tid] = call i32 \varying[@get_global_id(i32 0)]
\varying[%arrayidx] = getelementptr i32* \uniform[%src], i32 \varying[%tid]
\varying[%0] = bitcast i32* \varying[%arrayidx] to <4 x i32>*
\varying[%1] = load <4 x i32>* \varying[%0], align 4
; Broadcast (alpha - 1) to a vector
\uniform[%sub] = sub i32 \uniform[%alpha], \uniform[1]
\uniform[%insert] = insertelement <4 x i32> undef, i32 \uniform[%sub], i32 0
\uniform[%broadcast_sub] = shufflevector <4 x i32> \uniform[%insert], ...
\varying[%add] = add nsw <4 x i32> \uniform[%broadcast_sub], \varying[%1]
\varying[%arrayidx2] = getelementptr i32* \uniform[%dst], i32 \varying[%tid]
\varying[%2] = bitcast i32* \varying[%arrayidx2] to <4 x i32>*
store <4 x i32> \varying[%add], <4 x i32>* \varying[%2], align 4
ret void
}
\end{codebox}
\end{frame}