(no commit message)
[libreriscv.git] / openpower / sv / remap.mdwn
1 [[!tag standards]]
2
3 # REMAP <a name="remap" />
4
5 * <https://bugs.libre-soc.org/show_bug.cgi?id=143> matrix multiply
6 * <https://bugs.libre-soc.org/show_bug.cgi?id=867> add svindex
7 * <https://bugs.libre-soc.org/show_bug.cgi?id=885> svindex in simulator
8 * see [[sv/remap/appendix]] for examples and usage
9 * see [[sv/propagation]] for a future way to apply REMAP
10
11 REMAP is an advanced form of Vector "Structure Packing" that
12 provides hardware-level support for commonly-used *nested* loop patterns.
13 For more general reordering an Indexed REMAP mode is available.
14
15 REMAP allows the usual vector loop `0..VL-1` to be "reshaped" (re-mapped)
16 from a linear form to a 2D or 3D transposed form, or "offset" to permit
17 arbitrary access to elements (when elwidth overrides are used),
18 independently on each Vector src or dest
19 register.
20
21 The initial primary motivation of REMAP was for Matrix Multiplication, reordering of sequential
22 data in-place: in-place DCT and FFT were easily justified given the
23 high usage in Computer Science.
24 Four SPRs are provided so that a single FMAC may be
25 used in a single loop to perform for example 4x4 times 4x4 Matrix multiplication,
26 generating 64 FMACs. Additional uses include regular "Structure Packing"
27 such as RGB pixel data extraction and reforming.
28
29 REMAP, like all of SV, is abstracted out, meaning that unlike traditional
30 Vector ISAs which would typically only have a limited set of instructions
31 that can be structure-packed (LD/ST typically), REMAP may be applied to
32 literally any instruction: CRs, Arithmetic, Logical, LD/ST, anything.
33
34 Note that REMAP does not *directly* apply to sub-vector elements: that
35 is what swizzle is for. Swizzle *can* however be applied to the same
36 instruction as REMAP. As explained in [[sv/mv.swizzle]], [[sv/mv.vec]] and the [[svp64/appendix]], Pack and Unpack EXTRA Mode bits
37 can extend down into Sub-vector elements to perform vec2/vec3/vec4
38 sequential reordering, but even here, REMAP is not extended down to
39 the actual sub-vector elements themselves.
40
41 In its general form, REMAP is quite expensive to set up, and on some
42 implementations may introduce
43 latency, so should realistically be used only where it is worthwhile.
44 Commonly-used patterns such as Matrix Multiply, DCT and FFT have
45 helper instruction options which make REMAP easier to use.
46
47 There are three types of REMAP:
48
49 * **Matrix**, also known as 2D and 3D reshaping, can perform in-place
50 Matrix transpose and rotate.
51 * **FFT/DCT**, with full triple-loop in-place support: limited to
52 Power-2 RADIX
53 * **Indexing**, for any general-purpose reordering, also includes
54 limited 2D reshaping.
55
56 # Basic principle
57
58 * normal vector element read/write of operands would be sequential
59 (0 1 2 3 ....)
60 * this is not appropriate for (e.g.) Matrix multiply which requires
61 accessing elements in alternative sequences (0 3 6 1 4 7 ...)
62 * normal Vector ISAs use either Indexed-MV or Indexed-LD/ST to "cope"
63 with this. both are expensive (copy large vectors, spill through memory)
64 and very few Packed SIMD ISAs cope with non-Power-2.
65 * REMAP **redefines** the order of access according to set "Schedules".
66 * The Schedules are not necessarily restricted to power-of-two boundaries
67 making it unnecessary to have for example specialised 3x4 transpose
68 instructions.
69
70 Only the most commonly-used algorithms in computer science have REMAP
71 support, due to the high cost in both the ISA and in hardware. For
72 arbitrary remapping the `Indexed` REMAP may be used.
73
74 # Executive Summary Usage
75
76 * `svshape` to set the type of reordering to be applied to an
77 otherwise usual `0..VL-1` hardware for-loop
78 * `svremap` to set which registers a given reordering is to apply to
79 (RA, RT etc)
80 * `sv.{instruction}` where any Vectorised register marked by `svremap`
81 will have its ordering REMAPPED according to the schedule set
82 by `svshape`.
83
84 The following illustrative example multiplies a 3x4 and a 5x3
85 matrix to create
86 a 5x4 result:
87
88 svshape 5, 4, 3, 0, 0
89 svremap 15, 1, 2, 3, 0, 0, 0, 0
90 sv.fmadds 0.v, 8.v, 16.v, 0.v
91
92 * svshape sets up the four SVSHAPE SPRS for a Matrix Schedule
93 * svremap activates four out of five registers RA RB RC RT RS (15)
94 * svremap requests:
95 - RA to use SVSHAPE1
96 - RB to use SVSHAPE2
97 - RC to use SVSHAPE3
98 - RT to use SVSHAPE0
99 - RS Remapping to not be activated
100 * sv.fmadds has RT=0.v, RA=8.v, RB=16.v, RC=0.v
101 * With REMAP being active each register's element index is
102 *independently* transformed using the specified SHAPEs.
103
104 Thus the Vector Loop is arranged such that the use of
105 the multiply-and-accumulate instruction executes precisely the required
106 Schedule to perform an in-place in-registers Matrix Multiply with no
107 need to perform additional Transpose or register copy instructions.
108 The example above may be executed as a unit test and demo,
109 [here](https://git.libre-soc.org/?p=openpower-isa.git;a=blob;f=src/openpower/decoder/isa/test_caller_svp64_matrix.py;h=c15479db9a36055166b6b023c7495f9ca3637333;hb=a17a252e474d5d5bf34026c25a19682e3f2015c3#l94)
110
111 # REMAP types
112
113 This section summarises the motivation for each REMAP Schedule
114 and briefly goes over their characteristics and limitations.
115
116 ## Matrix (1D/2D/3D shaping)
117
118 Matrix Multiplication is a huge part of High-Performance Compute,
119 and 3D.
120 In many PackedSIMD as well as Scalable Vector ISAs, non-power-of-two
121 Matrix sizes are a serious challenge. PackedSIMD ISAs, in order to
122 cope with for example 3x4 Matrices, recommend rolling data-repetition and loop-unrolling.
123 Aside from the cost of the load on the L1 I-Cache, the trick only
124 works if one of the dimensions X or Y are power-two. Prime Numbers
125 (5x7, 3x5) become deeply problematic to unroll.
126
127 Even traditional Scalable Vector ISAs have issues with Matrices, often
128 having to perform data Transpose by pushing out through Memory and back,
129 or computing Transposition Indices (costly) then copying to another
130 Vector (costly).
131
132 Matrix REMAP was thus designed to solve these issues by providing Hardware
133 Assisted
134 "Schedules" that can view what would otherwise be limited to a strictly
135 linear Vector as instead being 2D (even 3D) *in-place* reordered.
136 With both Transposition and non-power-two being supported the issues
137 faced by other ISAs are mitigated.
138
139 Limitations of Matrix REMAP are that the Vector Length (VL) is currently
140 restricted to 127: up to 127 FMAs may be performed in total (potentially
141 127 vec2/3/4 FMAs may be used but this requires additional research).
142 Also given that it is in-registers only at present some care has to be
143 taken on regfile resource utilisation. However it is perfectly possible
144 to utilise Matrix REMAP to perform the three inner-most "kernel" loops of
145 the usual 6-level large Matrix Multiply, without the usual difficulties
146 associated with SIMD.
147
148 Also the `svshape` instruction only provides access to part of the
149 Matrix REMAP capability. Rotation and mirroring need to be done by
150 programming the SVSHAPE SPRs directly, which can take a lot more
151 instructions.
152
153 ## FFT/DCT Triple Loop
154
155 DCT and FFT are some of the most astonishingly used algorithms in
156 Computer Science. Radar, Audio, Video, R.F. Baseband and dozens more. At least
157 two DSPs, TMS320 and Hexagon, have VLIW instructions specially tailored
158 to FFT.
159
160 An in-depth analysis showed that it is possible to do in-place in-register
161 DCT and FFT as long as twin-result "butterfly" instructions are provided.
162 These can be found in the [[openpower/isa/svfparith]] page if performing
163 IEEE754 FP transforms. *(For fixed-point transforms, equivalent 3-in 2-out
164 integer operations would be required)*. These "butterfly" instructions
165 avoid the need for a temporary register because the two array positions
166 being overwritten will be "in-flight" in any In-Order or Out-of-Order
167 micro-architecture.
168
169 DCT and FFT Schedules are currently limited to RADIX2 sizes and do not
170 accept predicate masks. Given that it is common to perform recursive
171 convolutions
172 combining smaller Power-2 DCT/FFT to create larger DCT/FFTs in practice the RADIX2
173 limit is not a problem. A Bluestein convolution to compute arbitrary
174 length is demonstrated
175 by [Project Nayuki](https://www.nayuki.io/res/free-small-fft-in-multiple-languages/fft.py)
176
177 ## Indexed
178
179 The purpose of Indexing is to provide a generalised version of
180 Vector ISA "Permute" instructions, such as VSX `vperm`. The
181 Indexing is abstracted out and may be applied to much more
182 than an element move/copy, and is not limited for example
183 to the number of bytes that can fit into a VSX register.
184 Indexing may be applied to LD/ST (even on Indexed LD/ST
185 instructions such as `sv.lbzx`), arithmetic operations,
186 extsw: there is no artificial limit.
187
188 The only major caveat is that the registers to be used as
189 Indices must not be modified by any instruction after Indexed Mode
190 is established, and neither must MAXVL be altered. Failure to observe
191 these conditions results in `UNDEFINED` behaviour.
192 These conditions allow a Read-After-Write (RAW) Hazard to be created on
193 the entire range of Indices to be subsequently used, but a corresponding
194 Write-After-Read Hazard by any instruction that modifies the Indices
195 **does not have to be created**. Given the large number of registers
196 involved in Indexing this is a huge resource saving and reduction
197 in micro-architectural complexity. MAXVL is likewise
198 included in the RAW Hazards because it is involved in calculating
199 how many registers are to be considered Indices.
200
201 With these restrictions in place high-performance implementations
202 may read-cache the Indices from the point where a given `svindex` instruction
203 is called or SVSHAPE SPRs altered.
204
205 # REMAP area of SVSTATE
206
207 The following bits of the SVSTATE SPR are used for REMAP:
208
209 |32.33|34.35|36.37|38.39|40.41| 42.46 | 62 |
210 | -- | -- | -- | -- | -- | ----- | ------ |
211 |mi0 |mi1 |mi2 |mo0 |mo1 | SVme | RMpst |
212
213 mi0-2 and mo0-1 each select SVSHAPE0-3 to apply to a given register.
214 mi0-2 apply to RA, RB, RC respectively, as input registers, and
215 likewise mo0-1 apply to output registers (RT/FRT, RS/FRS) respectively.
216 SVme is 5 bits (one for each of mi0-2/mo0-1) and indicates whether the
217 SVSHAPE is actively applied or not.
218
219 * bit 0 of SVme indicates if mi0 is applied to RA / FRA
220 * bit 1 of SVme indicates if mi1 is applied to RB / FRB
221 * bit 2 of SVme indicates if mi2 is applied to RC / FRC
222 * bit 3 of SVme indicates if mo0 is applied to RT / FRT
223 * bit 4 of SVme indicates if mo1 is applied to Effective Address / FRS / RS
224 (LD/ST-with-update has an implicit 2nd write register, RA)
225
226 # svremap instruction <a name="svremap"> </a>
227
228 There is also a corresponding SVRM-Form for the svremap
229 instruction which matches the above SPR:
230
231 svremap SVme,mi0,mi1,mi2,mo0,mo2,pst
232
233 |0 |6 |11 |13 |15 |17 |19 |21 | 22.25 |26..31 |
234 | -- | -- | -- | -- | -- | -- | -- | -- | ---- | ----- |
235 | PO | SVme |mi0 | mi1 | mi2 | mo0 | mo1 | pst | rsvd | XO |
236
237 # SHAPE Remapping SPRs
238
239 There are four "shape" SPRs, SHAPE0-3, 32-bits in each,
240 which have the same format.
241
242 [[!inline pages="openpower/sv/shape_table_format" raw="yes" ]]
243
244 # svshape instruction <a name="svshape"> </a>
245
246 `svshape` is a convenience instruction that reduces instruction
247 count for common usage patterns, particularly Matrix, DCT and FFT. It sets up
248 (overwrites) all required SVSHAPE SPRs and also modifies SVSTATE
249 including VL and MAXVL. Using `svshape` therefore does not also
250 require `setvl`.
251
252 Form: SVM-Form SV "Matrix" Form (see [[isatables/fields.text]])
253
254 svshape SVxd,SVyd,SVzd,SVRM,vf
255
256 | 0.5|6.10 |11.15 |16..20 | 21..25 | 25 | 26..31| name |
257 | -- | -- | --- | ----- | ------ | -- | ------| -------- |
258 |OPCD| SVxd | SVyd | SVzd | SVRM | vf | XO | svstate |
259
260 Fields:
261
262 * **SVxd** - SV REMAP "xdim"
263 * **SVyd** - SV REMAP "ydim"
264 * **SVzd** - SV REMAP "zdim"
265 * **SVRM** - SV REMAP Mode (0b00000 for Matrix, 0b00001 for FFT etc.)
266 * **vf** - sets "Vertical-First" mode
267 * **XO** - standard 6-bit XO field
268
269 *Note: SVxd, SVyz and SVzd are all stored "off-by-one". In the assembler
270 mnemonic the values `1-32` are stored in binary as `0b00000..0b11111`*
271
272 | SVRM | Remap Mode description |
273 | -- | -- |
274 | 0b0000 | Matrix 1/2/3D |
275 | 0b0001 | FFT Butterfly |
276 | 0b0010 | DCT Inner butterfly, pre-calculated coefficients |
277 | 0b0011 | DCT Outer butterfly |
278 | 0b0100 | DCT Inner butterfly, on-the-fly (Vertical-First Mode) |
279 | 0b0101 | DCT COS table index generation |
280 | 0b0110 | DCT half-swap |
281 | 0b0111 | reserved |
282 | 0b1000 | reserved |
283 | 0b1001 | reserved |
284 | 0b1010 | iDCT Inner butterfly, pre-calculated coefficients |
285 | 0b1011 | iDCT Outer butterfly |
286 | 0b1100 | iDCT Inner butterfly, on-the-fly (Vertical-First Mode) |
287 | 0b1101 | iDCT COS table index generation |
288 | 0b1110 | iDCT half-swap |
289 | 0b1111 | FFT half-swap |
290
291 Examples showing how all of these Modes operate exists in the online
292 [SVP64 unit tests](https://git.libre-soc.org/?p=openpower-isa.git;a=tree;f=src/openpower/decoder/isa;hb=HEAD)
293 and the full pseudocode setting up all SPRs
294 is in the [[openpower/isa/simplev]] page.
295
296 In Indexed Mode, there are only 5 bits available to specify the GPR
297 to use, out of 128 GPRs (7 bit numbering). Therefore, only the top
298 5 bits are given in the `SVxd` field: the bottom two implicit bits
299 will be zero (`SVxd || 0b00`).
300
301 `svshape` has *limited applicability* due to being a 32-bit instruction.
302 The full capability of SVSHAPE SPRs may be accessed by directly writing
303 to SVSHAPE0-3 with `mtspr`. Circumstances include Matrices with dimensions
304 larger than 32, and in-place Transpose. Potentially a future v3.1 Prefixed
305 instruction, `psvshape`, may extend the capability here.
306
307 # svindex instruction <a name="svindex"> </a>
308
309 `svindex` is a convenience instruction that reduces instruction
310 count for Indexed REMAP Mode. It sets up
311 (overwrites) all required SVSHAPE SPRs and can modify the REMAP
312 SPR as well. The relevant SPRs *may* be directly programmed with
313 `mtspr` however it is laborious to do so: svindex saves instructions
314 covering much of Indexed REMAP capability.
315
316 Form: SVI-Form SV "Indexed" Form (see [[isatables/fields.text]])
317
318 svindex SVG,rmm,SVd,ew,yx,mr,sk
319
320 | 0.5|6.10 |11.15 |16.20 | 21..25 | 26..31| name | Form |
321 | -- | -- | --- | ---- | ----------- | ------| -------- | ---- |
322 |OPCD| SVG | rmm | SVd | ew/yx/mm/sk | XO | svindex | SVI-Form |
323
324 Fields:
325
326 * **SVd** - SV REMAP x/y dim
327 * **rmm** - REMAP mask: sets remap mi0-2/mo0-1 and SVSHAPEs,
328 controlled by mm
329 * **ew** - sets element width override on RS
330 * **SVG** - GPR SVG<<2 to be used for Indexing
331 * **yx** - 2D reordering to be used if yx=1
332 * **mm** - mask mode. determines how `rmm` is interpreted.
333 * **sk** - Dimension skipping enabled
334 * **XO** - standard 6-bit XO field
335
336 *Note: SVd, like SVxd, SVyz and SVzd of `svshape`, are all stored
337 "off-by-one". In the assembler
338 mnemonic the values `1-32` are stored in binary as `0b00000..0b11111`*.
339
340 *Note: when `yx=1,sk=0` the second dimension is calculated as
341 `CEIL(MAXVL/SVd)`*.
342
343 When `mm=0`:
344
345 * `rmm`, like REMAP.SVme, has bit 0
346 correspond to mi0, bit 1 to mi1, bit 2 to mi2,
347 bit 3 to mo0 and bit 4 to mi1
348 * all SVSHAPEs and the REMAP parts of SVSHAPE are first reset (initialised to zero)
349 * for each bit set in the 5-bit `rmm`, in order, the first
350 as-yet-unset SVSHAPE will be updated
351 with the other operands in the instruction, and the REMAP
352 SPR set.
353 * If all 5 bits of `rmm` are set then both mi0 and mo1 use SVSHAPE0.
354 * SVSTATE persistence bit is cleared
355 * No other alterations to SVSTATE are carried out
356
357 Example 1: if rmm=0b00110 then SVSHAPE0 and SVSHAPE1 are set up,
358 and the REMAP SPR set so that mi1 uses SVSHAPE0 and mi2
359 uses mi2. REMAP.SVme is also set to 0b00110, REMAP.mi1=0
360 (SVSHAPE0) and REMAP.mi2=1 (SVSHAPE1)
361
362 Example 2: if rmm=0b10001 then again SVSHAPE0 and SVSHAPE1
363 are set up, but the REMAP SPR is set so that mi0 uses SVSHAPE0
364 and mo1 uses SVSHAPE1. REMAP.SVme=0b10001, REMAP.mi0=0, REMAP.mo1=1
365
366 Rough algorithmic form:
367
368 marray = [mi0, mi1, mi2, mo0, mo1]
369 idx = 0
370 for bit = 0 to 4:
371 if not rmm[bit]: continue
372 setup(SVSHAPE[idx])
373 SVSTATE{marray[bit]} = idx
374 idx = (idx+1) modulo 4
375
376 When `mm=1`:
377
378 * bits 0-2 (MSB0 numbering) of `rmm` indicate an index selecting mi0-mo1
379 * bits 3-4 (MSB0 numbering) of `rmm` indicate which SVSHAPE 0-3 shall
380 be updated
381 * only the selected SVSHAPE is overwritten
382 * only the relevant bits in the REMAP area of SVSTATE are updated
383 * REMAP persistence bit is set.
384
385 Example 1: if `rmm`=0b01110 then bits 0-2 (MSB0) are 0b011 and
386 bits 3-4 are 0b10. thus, mo0 is selected and SVSHAPE2
387 to be updated. REMAP.SVme[3] will be set high and REMAP.mo0
388 set to 2 (SVSHAPE2).
389
390 Example 2: if `rmm`=0b10011 then bits 0-2 (MSB0) are 0b100 and
391 bits 3-4 are 0b11. thus, mo1 is selected and SVSHAPE3
392 to be updated. REMAP.SVme[4] will be set high and REMAP.mo1
393 set to 3 (SVSHAPE3).
394
395 Rough algorithmic form:
396
397 marray = [mi0, mi1, mi2, mo0, mo1]
398 bit = rmm[0:2]
399 idx = rmm[3:4]
400 setup(SVSHAPE[idx])
401 SVSTATE{marray[bit]} = idx
402 SVSTATE.pst = 1
403
404 In essence, `mm=0` is intended for use to set as much of the
405 REMAP State SPRs as practical with a single instruction,
406 whilst `mm=1` is intended to be a little more refined.
407
408 **Usage guidelines**
409
410 * **Disable 2D mapping**: to only perform Indexing without
411 reordering use `SVd=1,sk=0,yx=0` (or set SVd to a value larger
412 or equal to VL)
413 * **Modulo 1D mapping**: to perform Indexing cycling through the
414 first N Indices use `SVd=N,sk=0,yx=0` where `VL>N`. There is
415 no requirement to set VL equal to a multiple of N.
416 * **Modulo 2D transposed**: `SVd=M,sk=0,yx=1`, sets
417 `xdim=M,ydim=CEIL(MAXVL/M)`.
418
419 Beyond these mappings it becomes necessary to write directly to
420 the SVSTATE SPRs manually.
421
422 # TODO
423
424 * investigate https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6879380/#!po=19.6429
425 in https://bugs.libre-soc.org/show_bug.cgi?id=653
426 * Triangular REMAP
427 * Cross-Product REMAP (actually, skew Matrix: https://en.m.wikipedia.org/wiki/Skew-symmetric_matrix)
428 * Convolution REMAP