(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 * <https://bugs.libre-soc.org/show_bug.cgi?id=911> offset svshape option
9 * see [[sv/remap/appendix]] for examples and usage
10 * see [[sv/propagation]] for a future way to apply REMAP
11 * [[remap/discussion]]
12
13 REMAP is an advanced form of Vector "Structure Packing" that
14 provides hardware-level support for commonly-used *nested* loop patterns.
15 For more general reordering an Indexed REMAP mode is available.
16
17 REMAP allows the usual vector loop `0..VL-1` to be "reshaped" (re-mapped)
18 from a linear form to a 2D or 3D transposed form, or "offset" to permit
19 arbitrary access to elements (when elwidth overrides are used),
20 independently on each Vector src or dest
21 register.
22
23 The initial primary motivation of REMAP was for Matrix Multiplication, reordering of sequential
24 data in-place: in-place DCT and FFT were easily justified given the
25 high usage in Computer Science.
26 Four SPRs are provided which may be applied to any GPR, FPR or CR Field
27 so that for example a single FMAC may be
28 used in a single loop to perform 5x3 times 3x4 Matrix multiplication,
29 generating 60 FMACs *without needing explicit assembler unrolling*.
30 Additional uses include regular "Structure Packing"
31 such as RGB pixel data extraction and reforming.
32
33 REMAP, like all of SV, is abstracted out, meaning that unlike traditional
34 Vector ISAs which would typically only have a limited set of instructions
35 that can be structure-packed (LD/ST typically), REMAP may be applied to
36 literally any instruction: CRs, Arithmetic, Logical, LD/ST, anything.
37
38 Note that REMAP does not *directly* apply to sub-vector elements: that
39 is what swizzle is for. Swizzle *can* however be applied to the same
40 instruction as REMAP. As explained in [[sv/mv.swizzle]], [[sv/mv.vec]] and the [[svp64/appendix]], Pack and Unpack EXTRA Mode bits
41 can extend down into Sub-vector elements to perform vec2/vec3/vec4
42 sequential reordering, but even here, REMAP is not extended down to
43 the actual sub-vector elements themselves.
44
45 In its general form, REMAP is quite expensive to set up, and on some
46 implementations may introduce
47 latency, so should realistically be used only where it is worthwhile.
48 Commonly-used patterns such as Matrix Multiply, DCT and FFT have
49 helper instruction options which make REMAP easier to use.
50
51 There are four types of REMAP:
52
53 * **Matrix**, also known as 2D and 3D reshaping, can perform in-place
54 Matrix transpose and rotate.
55 * **FFT/DCT**, with full triple-loop in-place support: limited to
56 Power-2 RADIX
57 * **Indexing**, for any general-purpose reordering, also includes
58 limited 2D reshaping.
59 * **Parallel Reduction**, for scheduling a sequence of operations
60 in a Deterministic fashion, in a way that may be parallelised,
61 to reduce a Vector down to a single value.
62
63 Best implemented on top of a Multi-Issue Out-of-Order Micro-architecture,
64 REMAP Schedules are 100% Deterministic **including Indexing** and are
65 designed to be incorporated in between the Decode and Issue phases,
66 directly into Register Hazard Management.
67
68 Parallel Reduction is unusual in that it requires a full vector array
69 of results (not a scalar) and uses the rest of the result Vector for
70 the purposes of storing intermediary calculations. As these intermediary
71 results are Deterministically computed they may be useful.
72 Additionally, because the intermediate results are always written out
73 it is possible to service Precise Interrupts without affecting latency
74 (a common limitation of Vector ISAs).
75
76 # Basic principle
77
78 * normal vector element read/write of operands would be sequential
79 (0 1 2 3 ....)
80 * this is not appropriate for (e.g.) Matrix multiply which requires
81 accessing elements in alternative sequences (0 3 6 1 4 7 ...)
82 * normal Vector ISAs use either Indexed-MV or Indexed-LD/ST to "cope"
83 with this. both are expensive (copy large vectors, spill through memory)
84 and very few Packed SIMD ISAs cope with non-Power-2.
85 * REMAP **redefines** the order of access according to set "Schedules".
86 * The Schedules are not necessarily restricted to power-of-two boundaries
87 making it unnecessary to have for example specialised 3x4 transpose
88 instructions.
89
90 Only the most commonly-used algorithms in computer science have REMAP
91 support, due to the high cost in both the ISA and in hardware. For
92 arbitrary remapping the `Indexed` REMAP may be used.
93
94 # Executive Summary Usage
95
96 * `svshape` to set the type of reordering to be applied to an
97 otherwise usual `0..VL-1` hardware for-loop
98 * `svremap` to set which registers a given reordering is to apply to
99 (RA, RT etc)
100 * `sv.{instruction}` where any Vectorised register marked by `svremap`
101 will have its ordering REMAPPED according to the schedule set
102 by `svshape`.
103
104 The following illustrative example multiplies a 3x4 and a 5x3
105 matrix to create
106 a 5x4 result:
107
108 svshape 5, 4, 3, 0, 0
109 svremap 15, 1, 2, 3, 0, 0, 0, 0
110 sv.fmadds 0.v, 8.v, 16.v, 0.v
111
112 * svshape sets up the four SVSHAPE SPRS for a Matrix Schedule
113 * svremap activates four out of five registers RA RB RC RT RS (15)
114 * svremap requests:
115 - RA to use SVSHAPE1
116 - RB to use SVSHAPE2
117 - RC to use SVSHAPE3
118 - RT to use SVSHAPE0
119 - RS Remapping to not be activated
120 * sv.fmadds has RT=0.v, RA=8.v, RB=16.v, RC=0.v
121 * With REMAP being active each register's element index is
122 *independently* transformed using the specified SHAPEs.
123
124 Thus the Vector Loop is arranged such that the use of
125 the multiply-and-accumulate instruction executes precisely the required
126 Schedule to perform an in-place in-registers Matrix Multiply with no
127 need to perform additional Transpose or register copy instructions.
128 The example above may be executed as a unit test and demo,
129 [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)
130
131 # REMAP types
132
133 This section summarises the motivation for each REMAP Schedule
134 and briefly goes over their characteristics and limitations.
135 Further details on the Deterministic Precise-Interruptible algorithms
136 used in these Schedules is found in the [[sv/remap/appendix]].
137
138 ## Matrix (1D/2D/3D shaping)
139
140 Matrix Multiplication is a huge part of High-Performance Compute,
141 and 3D.
142 In many PackedSIMD as well as Scalable Vector ISAs, non-power-of-two
143 Matrix sizes are a serious challenge. PackedSIMD ISAs, in order to
144 cope with for example 3x4 Matrices, recommend rolling data-repetition and loop-unrolling.
145 Aside from the cost of the load on the L1 I-Cache, the trick only
146 works if one of the dimensions X or Y are power-two. Prime Numbers
147 (5x7, 3x5) become deeply problematic to unroll.
148
149 Even traditional Scalable Vector ISAs have issues with Matrices, often
150 having to perform data Transpose by pushing out through Memory and back,
151 or computing Transposition Indices (costly) then copying to another
152 Vector (costly).
153
154 Matrix REMAP was thus designed to solve these issues by providing Hardware
155 Assisted
156 "Schedules" that can view what would otherwise be limited to a strictly
157 linear Vector as instead being 2D (even 3D) *in-place* reordered.
158 With both Transposition and non-power-two being supported the issues
159 faced by other ISAs are mitigated.
160
161 Limitations of Matrix REMAP are that the Vector Length (VL) is currently
162 restricted to 127: up to 127 FMAs (or other operation)
163 may be performed in total.
164 Also given that it is in-registers only at present some care has to be
165 taken on regfile resource utilisation. However it is perfectly possible
166 to utilise Matrix REMAP to perform the three inner-most "kernel" loops of
167 the usual 6-level large Matrix Multiply, without the usual difficulties
168 associated with SIMD.
169
170 Also the `svshape` instruction only provides access to part of the
171 Matrix REMAP capability. Rotation and mirroring need to be done by
172 programming the SVSHAPE SPRs directly, which can take a lot more
173 instructions.
174
175 ## FFT/DCT Triple Loop
176
177 DCT and FFT are some of the most astonishingly used algorithms in
178 Computer Science. Radar, Audio, Video, R.F. Baseband and dozens more. At least
179 two DSPs, TMS320 and Hexagon, have VLIW instructions specially tailored
180 to FFT.
181
182 An in-depth analysis showed that it is possible to do in-place in-register
183 DCT and FFT as long as twin-result "butterfly" instructions are provided.
184 These can be found in the [[openpower/isa/svfparith]] page if performing
185 IEEE754 FP transforms. *(For fixed-point transforms, equivalent 3-in 2-out
186 integer operations would be required)*. These "butterfly" instructions
187 avoid the need for a temporary register because the two array positions
188 being overwritten will be "in-flight" in any In-Order or Out-of-Order
189 micro-architecture.
190
191 DCT and FFT Schedules are currently limited to RADIX2 sizes and do not
192 accept predicate masks. Given that it is common to perform recursive
193 convolutions
194 combining smaller Power-2 DCT/FFT to create larger DCT/FFTs in practice the RADIX2
195 limit is not a problem. A Bluestein convolution to compute arbitrary
196 length is demonstrated
197 by [Project Nayuki](https://www.nayuki.io/res/free-small-fft-in-multiple-languages/fft.py)
198
199 ## Indexed
200
201 The purpose of Indexing is to provide a generalised version of
202 Vector ISA "Permute" instructions, such as VSX `vperm`. The
203 Indexing is abstracted out and may be applied to much more
204 than an element move/copy, and is not limited for example
205 to the number of bytes that can fit into a VSX register.
206 Indexing may be applied to LD/ST (even on Indexed LD/ST
207 instructions such as `sv.lbzx`), arithmetic operations,
208 extsw: there is no artificial limit.
209
210 The only major caveat is that the registers to be used as
211 Indices must not be modified by any instruction after Indexed Mode
212 is established, and neither must MAXVL be altered. Additionally,
213 no register used as an Index may exceed MAXVL-1.
214
215 Failure to observe
216 these conditions results in `UNDEFINED` behaviour.
217 These conditions allow a Read-After-Write (RAW) Hazard to be created on
218 the entire range of Indices to be subsequently used, but a corresponding
219 Write-After-Read Hazard by any instruction that modifies the Indices
220 **does not have to be created**. Given the large number of registers
221 involved in Indexing this is a huge resource saving and reduction
222 in micro-architectural complexity. MAXVL is likewise
223 included in the RAW Hazards because it is involved in calculating
224 how many registers are to be considered Indices.
225
226 With these Hazard Mitigations in place, high-performance implementations
227 may read-cache the Indices from the point where a given `svindex` instruction
228 is called (or SVSHAPE SPRs - and MAXVL- directly altered).
229
230 The original motivation for Indexed REMAP was to mitigate the need to add
231 an expensive `mv.x` to the Scalar ISA, which was likely to be rejected as
232 a stand-alone instruction. Usually a Vector ISA would add a non-conflicting
233 variant (as in VSX `vperm`) but it is common to need to permute by source,
234 with the risk of conflict, that has to be resolved, for example, in AVX-512
235 with `conflictd`.
236
237 Indexed REMAP on the other hand **does not prevent conflicts** (overlapping
238 destinations), which on a superficial analysis may be perceived to be a
239 problem, until it is recalled that, firstly, Simple-V is designed specifically
240 to require Program Order to be respected, and that Matrix, DCT and FFT
241 all *already* critically depend on overlapping Reads/Writes: Matrix
242 uses overlapping registers as accumulators. Thus the Register Hazard
243 Management needed by Indexed REMAP *has* to be in place anyway.
244
245 The cost compared to Matrix and other REMAPs (and Pack/Unpack) is
246 clearly that of the additional reading of the GPRs to be used as Indices,
247 plus the setup cost associated with creating those same Infices.
248 If any Deterministic REMAP can cover the required task, clearly it
249 is adviseable to use it instead.
250
251 *Programmer's note: some algorithms may require skipping of Indices exceeding
252 VL-1, not MAXVL-1. This may be achieved programmatically by performing
253 an `sv.cmp *BF,*RA,RB` where RA is the same GPRs used in the Indexed REMAP,
254 and RB contains the value of VL returned from `setvl`. The resultant
255 CR Fields may then be used as Predicate Masks to exclude those operations
256 with an Index exceeding VL-1.*
257
258 ## Parallel Reduction
259
260 Vector Reduce Mode issues a deterministic tree-reduction schedule to the underlying micro-architecture. Like Scalar reduction, the "Scalar Base"
261 (Power ISA v3.0B) operation is leveraged, unmodified, to give the
262 *appearance* and *effect* of Reduction.
263
264 In Horizontal-First Mode, Vector-result reduction **requires**
265 the destination to be a Vector, which will be used to store
266 intermediary results.
267
268 Given that the tree-reduction schedule is deterministic,
269 Interrupts and exceptions
270 can therefore also be precise. The final result will be in the first
271 non-predicate-masked-out destination element, but due again to
272 the deterministic schedule programmers may find uses for the intermediate
273 results.
274
275 When Rc=1 a corresponding Vector of co-resultant CRs is also
276 created. No special action is taken: the result and its CR Field
277 are stored "as usual" exactly as all other SVP64 Rc=1 operations.
278
279 Note that the Schedule only makes sense on top of certain instructions:
280 X-Form with a Register Profile of `RT,RA,RB` is fine. Like Scalar
281 Reduction, nothing is prohibited:
282 the results of execution on an unsuitable instruction may simply
283 not make sense. Many 3-input instructions (madd, fmadd) unlike Scalar
284 Reduction in particular do not make sense, but `ternlogi`, if used
285 with care, would.
286
287 Critical to note regarding use of Parallel-Reduction REMAP is that,
288 exactly as with Matrix Mode, the `svshape` instruction *requests*
289 a certain Vector Length (number of elements to reduce) and then
290 sets VL and MAXVL at the number of **operations** needed to be
291 carried out. Thus, equally as importantly, the total number of operations
292 is restricted to 127. Any Parallel-Reduction requiring more operations
293 will need to be done manually in batches.
294
295 Also important to note is that the Deterministic Schedule is arranged
296 so that some implementations *may* parallelise it, as long as doing so
297 respects Program Order and Register Hazards. Performance (speed)
298 of any given
299 implementation is neither strictly defined or guaranteed. As with
300 the Vulkan(tm) Specification, strict compliance is paramount whilst
301 performance is left to Implementors.
302
303 **Parallel-Reduction with Predication**
304
305 To avoid breaking the strict RISC-paradigm, keeping the Issue-Schedule
306 completely separate from the actual element-level (scalar) operations,
307 Move operations are **not** included in the Schedule. This means that
308 the Schedule leaves the final (scalar) result in the first-non-masked
309 element of the Vector used. With the predicate mask being dynamic
310 (but deterministic) this result could be anywhere.
311
312 If that result is needed to be moved to a (single) scalar register
313 then a follow-up `sv.mv/sm=predicate rt, *ra` instruction will be
314 needed to get it, where the predicate is the exact same predicate used
315 in the prior Parallel-Reduction instruction.
316
317 * If there was only a single
318 bit in the predicate then the result will not have moved or been altered
319 from the source vector prior to the Reduction
320 * If there was more than one bit the result will be in the
321 first element with a predicate bit set.
322
323 In either case the result is in the element with the first bit set in
324 the predicate mask.
325
326 For *some* implementations
327 the vector-to-scalar copy may be a slow operation, as may the Predicated
328 Parallel Reduction itself.
329 It may be better to perform a pre-copy
330 of the values, compressing them (VREDUCE-style) into a contiguous block,
331 which will guarantee that the result goes into the very first element
332 of the destination vector, in which case clearly no follow-up
333 vector-to-scalar MV operation is needed.
334
335 **Usage conditions**
336
337 The simplest usage is to perform an overwrite, specifying all three
338 register operands the same.
339
340 setvl VL=6
341 sv.add *8, *8, *8
342
343 The Reduction Schedule will issue the Parallel Tree Reduction spanning
344 registers 8 through 13, by adjusting the offsets to RT, RA and RB as
345 necessary (see "Parallel Reduction algorithm" in a later section).
346
347 A non-overwrite is possible as well but just as with the overwrite
348 version, only those destination elements necessary for storing
349 intermediary computations will be written to: the remaining elements
350 will **not** be overwritten and will **not** be zero'd.
351
352 setvl VL=4
353 sv.add *0, *8, *8
354
355 However it is critical to note that if the source and destination are
356 not the same then the trick of using a follow-up vector-scalar MV will
357 not work.
358
359 ## Sub-Vector Horizontal Reduction
360
361 Note that when SVM is clear and SUBVL!=1 a Parallel Reduction is performed
362 on all first Subvector elements, followed by another separate independent
363 Parallel Reduction on all the second Subvector elements and so on.
364
365 for selectsubelement in (x,y,z,w):
366 parallelreduce(0..VL-1, selectsubelement)
367
368 By contrast, when SVM is set and SUBVL!=1, a Horizontal
369 Subvector mode is enabled, applying the Parallel Reduction
370 Algorithm to the Subvector Elements. The Parallel Reduction
371 is independently applied VL times, to each group of Subvector
372 elements. Bear in mind that predication is never applied down
373 into individual Subvector elements, but will be applied
374 to select whether the *entire* Parallel Reduction on each
375 group is performed or not.
376
377  for (i = 0; i < VL; i++)
378 if (predval & 1<<i) # predication
379 el = element[i]
380 parallelreduction([el.x, el.y, el.z, el.w])
381
382 Note that as this is a Parallel Reduction, for best results
383 it should be an overwrite operation, where the result for
384 the Horizontal Reduction of each Subvector will be in the
385 first Subvector element.
386 Also note that use of Rc=1 is `UNDEFINED` behaviour.
387
388 In essence what is happening here is that Structure Packing is being
389 combined with Parallel Reduction. If the Subvector elements may be
390 laid out as a 2D matrix, with the Subvector elements on rows,
391 and Parallel Reduction is applied per row, then if `SVM` is **clear**
392 the Matrix is transposed (like Pack/Unpack)
393 before still applying the Parallel Reduction to the **row**.
394
395 # REMAP area of SVSTATE
396
397 The following bits of the SVSTATE SPR are used for REMAP:
398
399 |32.33|34.35|36.37|38.39|40.41| 42.46 | 62 |
400 | -- | -- | -- | -- | -- | ----- | ------ |
401 |mi0 |mi1 |mi2 |mo0 |mo1 | SVme | RMpst |
402
403 mi0-2 and mo0-1 each select SVSHAPE0-3 to apply to a given register.
404 mi0-2 apply to RA, RB, RC respectively, as input registers, and
405 likewise mo0-1 apply to output registers (RT/FRT, RS/FRS) respectively.
406 SVme is 5 bits (one for each of mi0-2/mo0-1) and indicates whether the
407 SVSHAPE is actively applied or not.
408
409 * bit 0 of SVme indicates if mi0 is applied to RA / FRA
410 * bit 1 of SVme indicates if mi1 is applied to RB / FRB
411 * bit 2 of SVme indicates if mi2 is applied to RC / FRC
412 * bit 3 of SVme indicates if mo0 is applied to RT / FRT
413 * bit 4 of SVme indicates if mo1 is applied to Effective Address / FRS / RS
414 (LD/ST-with-update has an implicit 2nd write register, RA)
415
416 # svremap instruction <a name="svremap"> </a>
417
418 There is also a corresponding SVRM-Form for the svremap
419 instruction which matches the above SPR:
420
421 svremap SVme,mi0,mi1,mi2,mo0,mo2,pst
422
423 |0 |6 |11 |13 |15 |17 |19 |21 | 22.25 |26..31 |
424 | -- | -- | -- | -- | -- | -- | -- | -- | ---- | ----- |
425 | PO | SVme |mi0 | mi1 | mi2 | mo0 | mo1 | pst | rsvd | XO |
426
427 # SHAPE Remapping SPRs
428
429 There are four "shape" SPRs, SHAPE0-3, 32-bits in each,
430 which have the same format.
431
432 Shape is 32-bits. When SHAPE is set entirely to zeros, remapping is
433 disabled: the register's elements are a linear (1D) vector.
434
435 |31.30|29..28 |27..24| 23..21 | 20..18 | 17..12 |11..6 |5..0 | Mode |
436 |---- |------ |------| ------ | ------- | ------- |----- |----- | ----- |
437 |0b00 |skip |offset| invxyz | permute | zdimsz |ydimsz|xdimsz|Matrix |
438 |0b00 |elwidth|offset|sk1/invxy|0b110/0b111|SVGPR|ydimsz|xdimsz|Indexed|
439 |0b01 |submode|offset| invxyz | submode2| rsvd |rsvd |xdimsz|DCT/FFT|
440 |0b10 |rsvd |offset| invxyz | rsvd | rsvd |rsvd |xdimsz|Preduce|
441 |0b11 | | | | | | | |rsvd |
442
443 mode sets different behaviours (straight matrix multiply, FFT, DCT).
444
445 * **mode=0b00** sets straight Matrix Mode
446 * **mode=0b00** with permute=0b110 or 0b111 sets Indexed Mode
447 * **mode=0b01** sets "FFT/DCT" mode and activates submodes
448 * **mode=0b10** sets "Parallel Reduction" Schedules.
449
450 ## FFT/DCT mode
451
452 submode2=0 is for FFT. For FFT submode the following schedules may be
453 selected:
454
455 * **submode=0b00** selects the ``j`` offset of the innermost for-loop
456 of Tukey-Cooley
457 * **submode=0b10** selects the ``j+halfsize`` offset of the innermost for-loop
458 of Tukey-Cooley
459 * **submode=0b11** selects the ``k`` of exptable (which coefficient)
460
461 When submode2 is 1 or 2, for DCT inner butterfly submode the following
462 schedules may be selected. When submode2 is 1, additional bit-reversing
463 is also performed.
464
465 * **submode=0b00** selects the ``j`` offset of the innermost for-loop,
466 in-place
467 * **submode=0b010** selects the ``j+halfsize`` offset of the innermost for-loop,
468 in reverse-order, in-place
469 * **submode=0b10** selects the ``ci`` count of the innermost for-loop,
470 useful for calculating the cosine coefficient
471 * **submode=0b11** selects the ``size`` offset of the outermost for-loop,
472 useful for the cosine coefficient ``cos(ci + 0.5) * pi / size``
473
474 When submode2 is 3 or 4, for DCT outer butterfly submode the following
475 schedules may be selected. When submode is 3, additional bit-reversing
476 is also performed.
477
478 * **submode=0b00** selects the ``j`` offset of the innermost for-loop,
479 * **submode=0b01** selects the ``j+1`` offset of the innermost for-loop,
480
481 ## Matrix Mode
482
483 In Matrix Mode, skip allows dimensions to be skipped from being included
484 in the resultant output index. this allows sequences to be repeated:
485 ```0 0 0 1 1 1 2 2 2 ...``` or in the case of skip=0b11 this results in
486 modulo ```0 1 2 0 1 2 ...```
487
488 * **skip=0b00** indicates no dimensions to be skipped
489 * **skip=0b01** sets "skip 1st dimension"
490 * **skip=0b10** sets "skip 2nd dimension"
491 * **skip=0b11** sets "skip 3rd dimension"
492
493 invxyz will invert the start index of each of x, y or z. If invxyz[0] is
494 zero then x-dimensional counting begins from 0 and increments, otherwise
495 it begins from xdimsz-1 and iterates down to zero. Likewise for y and z.
496
497 offset will have the effect of offsetting the result by ```offset``` elements:
498
499 for i in 0..VL-1:
500 GPR(RT + remap(i) + SVSHAPE.offset) = ....
501
502 this appears redundant because the register RT could simply be changed by a compiler, until element width overrides are introduced. also
503 bear in mind that unlike a static compiler SVSHAPE.offset may
504 be set dynamically at runtime.
505
506 xdimsz, ydimsz and zdimsz are offset by 1, such that a value of 0 indicates
507 that the array dimensionality for that dimension is 1. any dimension
508 not intended to be used must have its value set to 0 (dimensionality
509 of 1). A value of xdimsz=2 would indicate that in the first dimension
510 there are 3 elements in the array. For example, to create a 2D array
511 X,Y of dimensionality X=3 and Y=2, set xdimsz=2, ydimsz=1 and zdimsz=0
512
513 The format of the array is therefore as follows:
514
515 array[xdimsz+1][ydimsz+1][zdimsz+1]
516
517 However whilst illustrative of the dimensionality, that does not take the
518 "permute" setting into account. "permute" may be any one of six values
519 (0-5, with values of 6 and 7 indicating "Indexed" Mode). The table
520 below shows how the permutation dimensionality order works:
521
522 | permute | order | array format |
523 | ------- | ----- | ------------------------ |
524 | 000 | 0,1,2 | (xdim+1)(ydim+1)(zdim+1) |
525 | 001 | 0,2,1 | (xdim+1)(zdim+1)(ydim+1) |
526 | 010 | 1,0,2 | (ydim+1)(xdim+1)(zdim+1) |
527 | 011 | 1,2,0 | (ydim+1)(zdim+1)(xdim+1) |
528 | 100 | 2,0,1 | (zdim+1)(xdim+1)(ydim+1) |
529 | 101 | 2,1,0 | (zdim+1)(ydim+1)(xdim+1) |
530 | 110 | 0,1 | Indexed (xdim+1)(ydim+1) |
531 | 111 | 1,0 | Indexed (ydim+1)(xdim+1) |
532
533 In other words, the "permute" option changes the order in which
534 nested for-loops over the array would be done. See executable
535 python reference code for further details.
536
537 *Note: permute=0b110 and permute=0b111 enable Indexed REMAP Mode,
538 described below*
539
540 With all these options it is possible to support in-place transpose,
541 in-place rotate, Matrix Multiply and Convolutions, without being
542 limited to Power-of-Two dimension sizes.
543
544 ## Indexed Mode
545
546 Indexed Mode activates reading of the element indices from the GPR
547 and includes optional limited 2D reordering.
548 In its simplest form (without elwidth overrides or other modes):
549
550 ```
551 def index_remap(i):
552 return GPR((SVSHAPE.SVGPR<<1)+i) + SVSHAPE.offset
553
554 for i in 0..VL-1:
555 element_result = ....
556 GPR(RT + indexed_remap(i)) = element_result
557 ```
558
559 With element-width overrides included, and using the pseudocode
560 from the SVP64 [[sv/svp64/appendix#elwidth]] elwidth section
561 this becomes:
562
563 ```
564 def index_remap(i):
565 svreg = SVSHAPE.SVGPR << 1
566 srcwid = elwid_to_bitwidth(SVSHAPE.elwid)
567 offs = SVSHAPE.offset
568 return get_polymorphed_reg(svreg, srcwid, i) + offs
569
570 for i in 0..VL-1:
571 element_result = ....
572 rt_idx = indexed_remap(i)
573 set_polymorphed_reg(RT, destwid, rt_idx, element_result)
574 ```
575
576 Matrix-style reordering still applies to the indices, except limited
577 to up to 2 Dimensions (X,Y). Ordering is therefore limited to (X,Y) or
578 (Y,X). Only one dimension may optionally be skipped. Inversion of either
579 X or Y or both is possible. Pseudocode for Indexed Mode (including elwidth
580 overrides) may be written in terms of Matrix Mode, specifically
581 purposed to ensure that the 3rd dimension (Z) has no effect:
582
583 ```
584 def index_remap(ISHAPE, i):
585 MSHAPE.skip = 0b0 || ISHAPE.sk1
586 MSHAPE.invxyz = 0b0 || ISHAPE.invxy
587 MSHAPE.xdimsz = ISHAPE.xdimsz
588 MSHAPE.ydimsz = ISHAPE.ydimsz
589 MSHAPE.zdimsz = 0 # disabled
590 if ISHAPE.permute = 0b110 # 0,1
591 MSHAPE.permute = 0b000 # 0,1,2
592 if ISHAPE.permute = 0b111 # 1,0
593 MSHAPE.permute = 0b010 # 1,0,2
594 el_idx = remap_matrix(MSHAPE, i)
595 svreg = ISHAPE.SVGPR << 1
596 srcwid = elwid_to_bitwidth(ISHAPE.elwid)
597 offs = ISHAPE.offset
598 return get_polymorphed_reg(svreg, srcwid, el_idx) + offs
599 ```
600
601 The most important observation above is that the Matrix-style
602 remapping occurs first and the Index lookup second. Thus it
603 becomes possible to perform in-place Transpose of Indices which
604 may have been costly to set up or costly to duplicate
605 (waste register file space).
606
607 # svshape instruction <a name="svshape"> </a>
608
609 `svshape` is a convenience instruction that reduces instruction
610 count for common usage patterns, particularly Matrix, DCT and FFT. It sets up
611 (overwrites) all required SVSHAPE SPRs and also modifies SVSTATE
612 including VL and MAXVL. Using `svshape` therefore does not also
613 require `setvl`.
614
615 Form: SVM-Form SV "Matrix" Form (see [[isatables/fields.text]])
616
617 svshape SVxd,SVyd,SVzd,SVRM,vf
618
619 | 0.5|6.10 |11.15 |16..20 | 21..24 | 25 | 26..31| name |
620 | -- | -- | --- | ----- | ------ | -- | ------| -------- |
621 |OPCD| SVxd | SVyd | SVzd | SVRM | vf | XO | svshape |
622
623 Fields:
624
625 * **SVxd** - SV REMAP "xdim"
626 * **SVyd** - SV REMAP "ydim"
627 * **SVzd** - SV REMAP "zdim"
628 * **SVRM** - SV REMAP Mode (0b00000 for Matrix, 0b00001 for FFT etc.)
629 * **vf** - sets "Vertical-First" mode
630 * **XO** - standard 6-bit XO field
631
632 *Note: SVxd, SVyz and SVzd are all stored "off-by-one". In the assembler
633 mnemonic the values `1-32` are stored in binary as `0b00000..0b11111`*
634
635 | SVRM | Remap Mode description |
636 | -- | -- |
637 | 0b0000 | Matrix 1/2/3D |
638 | 0b0001 | FFT Butterfly |
639 | 0b0010 | DCT Inner butterfly, pre-calculated coefficients |
640 | 0b0011 | DCT Outer butterfly |
641 | 0b0100 | DCT Inner butterfly, on-the-fly (Vertical-First Mode) |
642 | 0b0101 | DCT COS table index generation |
643 | 0b0110 | DCT half-swap |
644 | 0b0111 | reserved |
645 | 0b1000 | reserved for svshape2 |
646 | 0b1001 | reserved for svshape2 |
647 | 0b1010 | iDCT Inner butterfly, pre-calculated coefficients |
648 | 0b1011 | iDCT Outer butterfly |
649 | 0b1100 | iDCT Inner butterfly, on-the-fly (Vertical-First Mode) |
650 | 0b1101 | iDCT COS table index generation |
651 | 0b1110 | iDCT half-swap |
652 | 0b1111 | FFT half-swap |
653
654 Examples showing how all of these Modes operate exists in the online
655 [SVP64 unit tests](https://git.libre-soc.org/?p=openpower-isa.git;a=tree;f=src/openpower/decoder/isa;hb=HEAD)
656 and the full pseudocode setting up all SPRs
657 is in the [[openpower/isa/simplev]] page.
658
659 In Indexed Mode, there are only 5 bits available to specify the GPR
660 to use, out of 128 GPRs (7 bit numbering). Therefore, only the top
661 5 bits are given in the `SVxd` field: the bottom two implicit bits
662 will be zero (`SVxd || 0b00`).
663
664 `svshape` has *limited applicability* due to being a 32-bit instruction.
665 The full capability of SVSHAPE SPRs may be accessed by directly writing
666 to SVSHAPE0-3 with `mtspr`. Circumstances include Matrices with dimensions
667 larger than 32, and in-place Transpose. Potentially a future v3.1 Prefixed
668 instruction, `psvshape`, may extend the capability here.
669
670 # svindex instruction <a name="svindex"> </a>
671
672 `svindex` is a convenience instruction that reduces instruction
673 count for Indexed REMAP Mode. It sets up
674 (overwrites) all required SVSHAPE SPRs and can modify the REMAP
675 SPR as well. The relevant SPRs *may* be directly programmed with
676 `mtspr` however it is laborious to do so: svindex saves instructions
677 covering much of Indexed REMAP capability.
678
679 Form: SVI-Form SV "Indexed" Form (see [[isatables/fields.text]])
680
681 svindex SVG,rmm,SVd,ew,yx,mr,sk
682
683 | 0.5|6.10 |11.15 |16.20 | 21..25 | 26..31| name | Form |
684 | -- | -- | --- | ---- | ----------- | ------| -------- | ---- |
685 |OPCD| SVG | rmm | SVd | ew/yx/mm/sk | XO | svindex | SVI-Form |
686
687 Fields:
688
689 * **SVd** - SV REMAP x/y dim
690 * **rmm** - REMAP mask: sets remap mi0-2/mo0-1 and SVSHAPEs,
691 controlled by mm
692 * **ew** - sets element width override on the Indices
693 * **SVG** - GPR SVG<<2 to be used for Indexing
694 * **yx** - 2D reordering to be used if yx=1
695 * **mm** - mask mode. determines how `rmm` is interpreted.
696 * **sk** - Dimension skipping enabled
697 * **XO** - standard 6-bit XO field
698
699 *Note: SVd, like SVxd, SVyz and SVzd of `svshape`, are all stored
700 "off-by-one". In the assembler
701 mnemonic the values `1-32` are stored in binary as `0b00000..0b11111`*.
702
703 *Note: when `yx=1,sk=0` the second dimension is calculated as
704 `CEIL(MAXVL/SVd)`*.
705
706 When `mm=0`:
707
708 * `rmm`, like REMAP.SVme, has bit 0
709 correspond to mi0, bit 1 to mi1, bit 2 to mi2,
710 bit 3 to mo0 and bit 4 to mi1
711 * all SVSHAPEs and the REMAP parts of SVSHAPE are first reset (initialised to zero)
712 * for each bit set in the 5-bit `rmm`, in order, the first
713 as-yet-unset SVSHAPE will be updated
714 with the other operands in the instruction, and the REMAP
715 SPR set.
716 * If all 5 bits of `rmm` are set then both mi0 and mo1 use SVSHAPE0.
717 * SVSTATE persistence bit is cleared
718 * No other alterations to SVSTATE are carried out
719
720 Example 1: if rmm=0b00110 then SVSHAPE0 and SVSHAPE1 are set up,
721 and the REMAP SPR set so that mi1 uses SVSHAPE0 and mi2
722 uses mi2. REMAP.SVme is also set to 0b00110, REMAP.mi1=0
723 (SVSHAPE0) and REMAP.mi2=1 (SVSHAPE1)
724
725 Example 2: if rmm=0b10001 then again SVSHAPE0 and SVSHAPE1
726 are set up, but the REMAP SPR is set so that mi0 uses SVSHAPE0
727 and mo1 uses SVSHAPE1. REMAP.SVme=0b10001, REMAP.mi0=0, REMAP.mo1=1
728
729 Rough algorithmic form:
730
731 marray = [mi0, mi1, mi2, mo0, mo1]
732 idx = 0
733 for bit = 0 to 4:
734 if not rmm[bit]: continue
735 setup(SVSHAPE[idx])
736 SVSTATE{marray[bit]} = idx
737 idx = (idx+1) modulo 4
738
739 When `mm=1`:
740
741 * bits 0-2 (MSB0 numbering) of `rmm` indicate an index selecting mi0-mo1
742 * bits 3-4 (MSB0 numbering) of `rmm` indicate which SVSHAPE 0-3 shall
743 be updated
744 * only the selected SVSHAPE is overwritten
745 * only the relevant bits in the REMAP area of SVSTATE are updated
746 * REMAP persistence bit is set.
747
748 Example 1: if `rmm`=0b01110 then bits 0-2 (MSB0) are 0b011 and
749 bits 3-4 are 0b10. thus, mo0 is selected and SVSHAPE2
750 to be updated. REMAP.SVme[3] will be set high and REMAP.mo0
751 set to 2 (SVSHAPE2).
752
753 Example 2: if `rmm`=0b10011 then bits 0-2 (MSB0) are 0b100 and
754 bits 3-4 are 0b11. thus, mo1 is selected and SVSHAPE3
755 to be updated. REMAP.SVme[4] will be set high and REMAP.mo1
756 set to 3 (SVSHAPE3).
757
758 Rough algorithmic form:
759
760 marray = [mi0, mi1, mi2, mo0, mo1]
761 bit = rmm[0:2]
762 idx = rmm[3:4]
763 setup(SVSHAPE[idx])
764 SVSTATE{marray[bit]} = idx
765 SVSTATE.pst = 1
766
767 In essence, `mm=0` is intended for use to set as much of the
768 REMAP State SPRs as practical with a single instruction,
769 whilst `mm=1` is intended to be a little more refined.
770
771 **Usage guidelines**
772
773 * **Disable 2D mapping**: to only perform Indexing without
774 reordering use `SVd=1,sk=0,yx=0` (or set SVd to a value larger
775 or equal to VL)
776 * **Modulo 1D mapping**: to perform Indexing cycling through the
777 first N Indices use `SVd=N,sk=0,yx=0` where `VL>N`. There is
778 no requirement to set VL equal to a multiple of N.
779 * **Modulo 2D transposed**: `SVd=M,sk=0,yx=1`, sets
780 `xdim=M,ydim=CEIL(MAXVL/M)`.
781
782 Beyond these mappings it becomes necessary to write directly to
783 the SVSTATE SPRs manually.
784
785 # svshape2 (offset) <a name="svshape2"> </a>
786
787 `svshape2` is an additional convenience instruction that prioritises
788 setting `SVSHAPE.offset`. Its primary purpose is for use when
789 element-width overrides are used. It has identical capabilities to `svindex` and
790 in terms of both options (skip, etc.) and ability to activate REMAP
791 (rmm, mask mode) but unlike `svindex` it does not set GPR REMAP,
792 only a 1D or 2D `svshape`, and
793 unlike `svshape` it can set an arbirrary `SVSHAPE.offset` immediate.
794
795 One of the limitations of Simple-V is that Vector elements start on the boundary
796 of the Scalar regfile, which is fine when element-width overrides are not
797 needed. If the starting point of a Vector with smaller elwidths must begin
798 in the middle of a register, normally there would be no way to do so except
799 through LD/ST. `SVSHAPE.offset` caters for this scenario and `svshape2`is
800 makes it easier.
801
802 svshape2 offs,inv,yx,rmm,SVd,sk,mm
803
804 | 0.5|6..8|9 |10|11.15 |16..20 | 21..25 | 25 | 26..31| name |
805 | -- |----|---|--| --- | ----- | ------ | -- | ------| -------- |
806 |OPCD|offs|inv|yx| rmm | SVd | 100/mm | sk | XO | svshape |
807
808 * **offs** (3 bits) - unsigned offset
809 * **yx** (1 bit) - swap XY to YX
810 * **inv** (1 bit) inverts X if yx=0, Y if yx=1
811 * **SVd** dimension size
812 * **rmm** REMAP mask
813 * **mm** mask mode
814 * **sk** (1 bit) skips 1st dimension if set
815
816 Dimensions are calculated exactly as `svindex`. `rmm` and
817 `mm` are as per `svindex`.
818
819 *Programmer's Note: offsets for `svshape2` may be specified in the range
820 0-15. Given that the principle of Simple-V is to fit on top of
821 byte-addressable register files and that GPR and FPR are 64-bit (8 bytes)
822 it should be clear that the offset may, when `elwidth=8`, begin an
823 element-level operation starting element zero at any arbitrary byte.
824 On cursory examination attempting to go beyond the range 0-7 seems
825 unnecessary given that the **next GPR or FPR** is an
826 alias for an offset in the range 8-15. Thus by simply increasing
827 the starting Vector point of the operation to the next register it
828 can be seen that the offset of 0-7 would be sufficient. Unfortunately
829 however some operations are EXTRA2-encoded it is **not possible**
830 to increase the GPR/FPR register number by one, because EXTRA2-encoding
831 of GPR/FPR Vector numbers are restricted to even numbering. The
832 additional offset range (8-15) helps overcome this limitation.*
833
834 *Hardware Implementor's note: with the offsets only being immediates
835 and with register numbering being entirely immediate as well it is
836 possible to correctly compute Register Hazards without requiring
837 reading the contents of any SPRs. If however there are
838 instructions that have directly written to the SVSTATE or SVSHAPE
839 SPRs and those instructions are still in-flight then this position
840 is clearly **invalid**.*
841
842 # TODO
843
844 * investigate https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6879380/#!po=19.6429
845 in https://bugs.libre-soc.org/show_bug.cgi?id=653
846 * UTF-8 <https://bugs.libre-soc.org/show_bug.cgi?id=794>
847 * Triangular REMAP
848 * Cross-Product REMAP (actually, skew Matrix: https://en.m.wikipedia.org/wiki/Skew-symmetric_matrix)
849 * Convolution REMAP