(no commit message)
[libreriscv.git] / openpower / sv / remap.mdwn
1 # REMAP <a name="remap" />
2
3 <!-- hide -->
4 * <https://bugs.libre-soc.org/show_bug.cgi?id=143> matrix multiply
5 * <https://bugs.libre-soc.org/show_bug.cgi?id=867> add svindex
6 * <https://bugs.libre-soc.org/show_bug.cgi?id=885> svindex in simulator
7 * <https://bugs.libre-soc.org/show_bug.cgi?id=911> offset svshape option
8 * <https://bugs.libre-soc.org/show_bug.cgi?id=864> parallel reduction
9 * <https://bugs.libre-soc.org/show_bug.cgi?id=930> DCT/FFT "strides"
10 * see [[sv/remap/appendix]] for examples and usage
11 * see [[sv/propagation]] for a future way to apply REMAP
12 * [[remap/discussion]]
13 <!-- show -->
14
15 REMAP is an advanced form of Vector "Structure Packing" that provides
16 hardware-level support for commonly-used *nested* loop patterns that would
17 otherwise require full inline loop unrolling. For more general reordering
18 an Indexed REMAP mode is available (a RISC-paradigm
19 abstracted analog to `xxperm`).
20
21 REMAP allows the usual sequential vector loop `0..VL-1` to be "reshaped"
22 (re-mapped) from a linear form to a 2D or 3D transposed form, or "offset"
23 to permit arbitrary access to elements, independently on each
24 Vector src or dest register. Up to four separate independent REMAPs may be applied
25 to the registers of any instruction.
26
27 A normal Vector Add:
28
29 ```
30  for i in range(VL):
31  GPR[RT+i] <= GPR[RA+i] + GPR[RB+i];
32 ```
33
34 A Hardware-assisted REMAP Vector Add:
35
36 ```
37 for i in range(VL):
38 GPR[RT+remap1(i)] <= GPR[RA+remap2(i)] + GPR[RB+remap3(i)];
39 ```
40
41 Aside from
42 Indexed REMAP this is entirely Hardware-accelerated reordering and
43 consequently not costly in terms of register access for the Indices. It will however
44 place a burden on Multi-Issue systems but no more than if the equivalent
45 Scalar instructions were explicitly loop-unrolled without SVP64, and
46 some advanced implementations may even find the Deterministic nature of
47 the Scheduling to be easier on resources.
48
49 *Hardware note: in its general form, REMAP is quite expensive to set up, and on some
50 implementations may introduce latency, so should realistically be used
51 only where it is worthwhile. Given that even with latency the fact
52 that up to 127 operations can be Deterministically issued (from a single
53 instruction) it should be clear that REMAP should not be dismissed
54 for *possible* latency alone. Commonly-used patterns such as Matrix
55 Multiply, DCT and FFT have helper instruction options which make REMAP
56 easier to use.*
57
58 There are five types of REMAP:
59
60 * **Matrix**, also known as 2D and 3D reshaping, can perform in-place
61 Matrix transpose and rotate. The Shapes are set up for an "Outer Product"
62 Matrix Multiply (a future variant may introduce Inner Product).
63 * **FFT/DCT**, with full triple-loop in-place support: limited to
64 Power-2 RADIX
65 * **Indexing**, for any general-purpose reordering, also includes
66 limited 2D reshaping as well as Element "offsetting".
67 * **Parallel Reduction**, for scheduling a sequence of operations
68 in a Deterministic fashion, in a way that may be parallelised,
69 to reduce a Vector down to a single value.
70 * **Parallel Prefix Sum**, implemented as a work-efficient Schedule,
71 has several key Computer Science uses. Again Prefix Sum is 100%
72 Deterministic.
73
74 Best implemented on top of a Multi-Issue Out-of-Order Micro-architecture,
75 REMAP Schedules are 100% Deterministic **including Indexing** and are
76 designed to be incorporated in between the Decode and Issue phases,
77 directly into Register Hazard Management.
78
79 As long as the SVSHAPE SPRs
80 are not written to directly, Hardware may treat REMAP as 100%
81 Deterministic: all REMAP Management instructions take static
82 operands (no dynamic register operands)
83 with the exception of Indexed Mode, and even then
84 Architectural State is permitted to assume that the Indices
85 are cacheable from the point at which the `svindex` instruction
86 is executed.
87
88 Further details on the Deterministic Precise-Interruptible algorithms
89 used in these Schedules is found in the [[sv/remap/appendix]].
90
91 *Future specification note: future versions of the REMAP Management instructions
92 will extend to EXT1xx Prefixed variants. This will overcome some of the limitations
93 present in the 32-bit variants of the REMAP Management instructions that at
94 present require direct writing to SVSHAPE0-3 SPRs. Additional
95 REMAP Modes may also be introduced at that time.*
96
97 ## Determining Register Hazards (hphint)
98
99 For high-performance (Multi-Issue, Out-of-Order) systems it is critical
100 to be able to statically determine the extent of Vectors in order to
101 allocate pre-emptive Hazard protection. The next task is to eliminate
102 masked-out elements using predicate bits, freeing up the associated
103 Hazards.
104
105 For non-REMAP situations `VL` is sufficient to ascertain early
106 Hazard coverage, and with SVSTATE being a high priority cached
107 quantity at the same level of MSR and PC this is not a problem.
108
109 The problems come when REMAP is enabled. Indexed REMAP must instead
110 use `MAXVL` as the earliest (simplest)
111 batch-level Hazard Reservation indicator (after taking element-width
112 overriding on the Index source into consideration),
113 but Matrix, FFT and Parallel Reduction must all use completely different
114 schemes. The reason is that VL is used to step through the total
115 number of *operations*, not the number of registers.
116 The "Saving Grace" is that all of the REMAP Schedules are 100% Deterministic.
117
118 Advance-notice Parallel computation and subsequent cacheing
119 of all of these complex Deterministic REMAP Schedules is
120 *strongly recommended*, thus allowing clear and precise multi-issue
121 batched Hazard coverage to be deployed, *even for Indexed Mode*.
122 This is only possible for Indexed due to the strict guidelines
123 given to Programmers.
124
125 In short, there exists solutions to the problem of Hazard Management,
126 with varying degrees of refinement possible at correspondingly
127 increasing levels of complexity in hardware.
128
129 A reminder: when Rc=1 each result register (element) has an associated
130 co-result CR Field (one per result element). Thus above when determining
131 the Write-Hazards for result registers the corresponding Write-Hazards for the
132 corresponding associated co-result CR Field must not be forgotten, *including* when
133 Predication is used.
134
135 **Horizontal-Parallelism Hint**
136
137 To help further in reducing Hazards,
138 `SVSTATE.hphint` is an indicator to hardware of how many elements are 100%
139 fully independent. Hardware is permitted to assume that groups of elements
140 up to `hphint` in size need not have Register (or Memory) Hazards created
141 between them, including when `hphint > VL`, which greatly aids simplification of
142 Multi-Issue implementations.
143
144 If care is not taken in setting `hphint` correctly it may wreak havoc.
145 For example Matrix Outer Product relies on the innermost loop computations
146 being independent. If `hphint` is set to greater than the Outer Product
147 depth then data corruption is guaranteed to occur.
148
149 Likewise on FFTs it is assumed that each layer of the RADIX2 triple-loop
150 is independent, but that there is strict *inter-layer* Register Hazards.
151 Therefore if `hphint` is set to greater than the RADIX2 width of the FFT,
152 data corruption is guaranteed.
153
154 Thus the key message is that setting `hphint` requires in-depth knowledge
155 of the REMAP Algorithm Schedules, given in the Appendix.
156
157 ## REMAP area of SVSTATE SPR <a name="svstate_remap_area"> </>
158
159 The following bits of the SVSTATE SPR are used for REMAP:
160
161 ```
162 |32:33|34:35|36:37|38:39|40:41| 42:46 | 62 |
163 | -- | -- | -- | -- | -- | ----- | ------ |
164 |mi0 |mi1 |mi2 |mo0 |mo1 | SVme | RMpst |
165 ```
166
167 mi0-2 and mo0-1 each select SVSHAPE0-3 to apply to a given register.
168 mi0-2 apply to RA, RB, RC respectively, as input registers, and
169 likewise mo0-1 apply to output registers (RT/FRT, RS/FRS) respectively.
170 SVme is 5 bits (one for each of mi0-2/mo0-1) and indicates whether the
171 SVSHAPE is actively applied or not, and if so, to which registers.
172
173 * bit 4 of SVme indicates if mi0 is applied to source RA / FRA / BA / BFA / RT / FRT
174 * bit 3 of SVme indicates if mi1 is applied to source RB / FRB / BB
175 * bit 2 of SVme indicates if mi2 is applied to source RC / FRC / BC
176 * bit 1 of SVme indicates if mo0 is applied to result RT / FRT / BT / BF
177 * bit 0 of SVme indicates if mo1 is applied to result Effective Address / FRS / RS
178 (LD/ST-with-update has an implicit 2nd write register, RA)
179
180 The "persistence" bit if set will result in all Active REMAPs being applied
181 indefinitely.
182
183 -----------
184
185 \newpage{}
186
187 # svremap instruction <a name="svremap"> </a>
188
189 SVRM-Form:
190
191 |0 |6 |11 |13 |15 |17 |19 |21 | 22:25 |26:31 |
192 | -- | -- | -- | -- | -- | -- | -- | -- | ---- | ----- |
193 | PO | SVme |mi0 | mi1 | mi2 | mo0 | mo1 | pst | rsvd | XO |
194
195 * svremap SVme,mi0,mi1,mi2,mo0,mo1,pst
196
197 Pseudo-code:
198
199 ```
200 # registers RA RB RC RT EA/FRS SVSHAPE0-3 indices
201 SVSTATE[32:33] <- mi0
202 SVSTATE[34:35] <- mi1
203 SVSTATE[36:37] <- mi2
204 SVSTATE[38:39] <- mo0
205 SVSTATE[40:41] <- mo1
206 # enable bit for RA RB RC RT EA/FRS
207 SVSTATE[42:46] <- SVme
208 # persistence bit (applies to more than one instruction)
209 SVSTATE[62] <- pst
210 ```
211
212 Special Registers Altered:
213
214 ```
215 SVSTATE
216 ```
217
218 `svremap` establishes the connection between registers and SVSHAPE SPRs.
219 The bitmask `SVme` determines which registers have a REMAP applied, and mi0-mo1
220 determine which shape is applied to an activated register. the `pst` bit if
221 cleared indicated that the REMAP operation shall only apply to the immediately-following
222 instruction. If set then REMAP remains permanently enabled until such time as it is
223 explicitly disabled, either by `setvl` setting a new MAXVL, or with another
224 `svremap` instruction. `svindex` and `svshape2` are also capable of setting or
225 clearing persistence, as well as partially covering a subset of the capability of
226 `svremap` to set register-to-SVSHAPE relationships.
227
228 Programmer's Note: applying non-persistent `svremap` to an instruction that has
229 no REMAP enabled or is a Scalar operation will obviously have no effect but
230 the bits 32 to 46 will at least have been set in SVSTATE. This may prove useful
231 when using `svindex` or `svshape2`.
232
233 Hardware Architectural Note: when persistence is not set it is critically important
234 to treat the `svremap` and the immediately-following SVP64 instruction as an
235 indivisible fused operation.
236 *No state* is stored in the SVSTATE SPR in order to allow continuation should an
237 Interrupt occur between the two instructions. Thus, Interrupts must be prohibited
238 from occurring or other workaround deployed. When persistence is set this issue
239 is moot.
240
241 It is critical to note that if persistence is clear then `svremap` is the *only* way
242 to activate REMAP on any given (following) instruction. If persistence is set however then
243 **all** SVP64 instructions go through REMAP as long as `SVme` is non-zero.
244
245 -------------
246
247 \newpage{}
248
249 # SHAPE Remapping SPRs
250
251 There are four "shape" SPRs, SHAPE0-3, 32-bits in each,
252 which have the same format. It is possible to write directly to these
253 SPRs but it is recommended to use the Management instructions
254 `svshape`, `svshape2` or `svindex`.
255
256 When SHAPE is set entirely to zeros, remapping is
257 disabled: the register's elements are a linear (1D) vector.
258
259 |0:5 |6:11 | 12:17 | 18:20 | 21:23 |24:27 |28:29 |30:31| Mode |
260 |----- |----- | ------- | ------- | ------ |------|------ |---- | ----- |
261 |xdimsz|ydimsz| zdimsz | permute | invxyz |offset|skip |mode |Matrix |
262 |xdimsz|ydimsz|SVGPR | 11/ |sk1/invxy|offset|elwidth|0b00 |Indexed|
263 |xdimsz|mode | zdimsz | submode2| invxyz |offset|submode|0b01 |DCT/FFT|
264 | rsvd |rsvd |xdimsz | rsvd | invxyz |offset|submode|0b10 |Red/Sum|
265 | | | | | | | |0b11 |rsvd |
266
267 `mode` sets different behaviours (straight matrix multiply, FFT, DCT).
268
269 * **mode=0b00** sets straight Matrix Mode
270 * **mode=0b00** with permute=0b110 or 0b111 sets Indexed Mode
271 * **mode=0b01** sets "FFT/DCT" mode and activates submodes
272 * **mode=0b10** sets "Parallel Reduction or Prefix-Sum" Schedules.
273
274 *Architectural Resource Allocation note: the four SVSHAPE SPRs are best
275 allocated sequentially and contiguously in order that `sv.mtspr` may
276 be used. This is safe to do as long as `SVSTATE.SVme=0`*
277
278 ## Parallel Reduction / Prefix-Sum Mode
279
280 Creates the Schedules for Parallel Tree Reduction and Prefix-Sum
281
282 * **submode=0b00** selects the left operand index for Reduction
283 * **submode=0b01** selects the right operand index for Reduction
284 * **submode=0b10** selects the left operand index for Prefix-Sum
285 * **submode=0b11** selects the right operand index for Prefix-Sum
286
287 * When bit 0 of `invxyz` is set, the order of the indices
288 in the inner for-loop are reversed. This has the side-effect
289 of placing the final reduced result in the last-predicated element.
290 It also has the indirect side-effect of swapping the source
291 registers: Left-operand index numbers will always exceed
292 Right-operand indices.
293 When clear, the reduced result will be in the first-predicated
294 element, and Left-operand indices will always be *less* than
295 Right-operand ones.
296 * When bit 1 of `invxyz` is set, the order of the outer loop
297 step is inverted: stepping begins at the nearest power-of two
298 to half of the vector length and reduces by half each time.
299 When clear the step will begin at 2 and double on each
300 inner loop.
301
302 **Parallel Prefix Sum**
303
304 This is a work-efficient Parallel Schedule that for example produces Trangular
305 or Factorial number sequences. Half of the Prefix Sum Schedule is near-identical
306 to Parallel Reduction. Whilst the Arithmetic mapreduce Mode (`/mr`) may achieve the same
307 end-result, implementations may only implement Mapreduce in serial form (or give
308 the appearance to Programmers of the same). The Parallel Prefix Schedule is
309 *required* to be implemented in such a way that its Deterministic Schedule may be
310 parallelised. Like the Reduction Schedule it is 100% Deterministic and consequently
311 may be used with non-commutative operations.
312 The Schedule Algorithm may be found in the [[sv/remap/appendix]]
313
314 **Parallel Reduction**
315
316 Vector Reduce Mode issues a deterministic tree-reduction schedule to the underlying micro-architecture. Like Scalar reduction, the "Scalar Base"
317 (Power ISA v3.0B) operation is leveraged, unmodified, to give the
318 *appearance* and *effect* of Reduction. Parallel Reduction is not limited
319 to Power-of-two but is limited as usual by the total number of
320 element operations (127) as well as available register file size.
321
322 In Horizontal-First Mode, Vector-result reduction **requires**
323 the destination to be a Vector, which will be used to store
324 intermediary results, in order to achieve a correct final
325 result.
326
327 Given that the tree-reduction schedule is deterministic,
328 Interrupts and exceptions
329 can therefore also be precise. The final result will be in the first
330 non-predicate-masked-out destination element, but due again to
331 the deterministic schedule programmers may find uses for the intermediate
332 results, even for non-commutative Defined Word-instruction operations.
333 Additionally, because the intermediate results are always written out
334 it is possible to service Precise Interrupts without affecting latency
335 (a common limitation of Vector ISAs implementing explicit
336 Parallel Reduction instructions, because their Architectural State cannot
337 hold the partial results).
338
339 When Rc=1 a corresponding Vector of co-resultant CRs is also
340 created. No special action is taken: the result *and its CR Field*
341 are stored "as usual" exactly as all other SVP64 Rc=1 operations.
342
343 Note that the Schedule only makes sense on top of certain instructions:
344 X-Form with a Register Profile of `RT,RA,RB` is fine because two sources
345 and the destination are all the same type. Like Scalar
346 Reduction, nothing is prohibited:
347 the results of execution on an unsuitable instruction may simply
348 not make sense. With care, even 3-input instructions (madd, fmadd, ternlogi)
349 may be used, and whilst it is down to the Programmer to walk through the
350 process the Programmer can be confident that the Parallel-Reduction is
351 guaranteed 100% Deterministic.
352
353 Critical to note regarding use of Parallel-Reduction REMAP is that,
354 exactly as with all REMAP Modes, the `svshape` instruction *requests*
355 a certain Vector Length (number of elements to reduce) and then
356 sets VL and MAXVL at the number of **operations** needed to be
357 carried out. Thus, equally as importantly, like Matrix REMAP
358 the total number of operations
359 is restricted to 127. Any Parallel-Reduction requiring more operations
360 will need to be done manually in batches (hierarchical
361 recursive Reduction).
362
363 Also important to note is that the Deterministic Schedule is arranged
364 so that some implementations *may* parallelise it (as long as doing so
365 respects Program Order and Register Hazards). Performance (speed)
366 of any given
367 implementation is neither strictly defined or guaranteed. As with
368 the Vulkan(tm) Specification, strict compliance is paramount whilst
369 performance is at the discretion of Implementors.
370
371 **Parallel-Reduction with Predication**
372
373 To avoid breaking the strict RISC-paradigm, keeping the Issue-Schedule
374 completely separate from the actual element-level (scalar) operations,
375 Move operations are **not** included in the Schedule. This means that
376 the Schedule leaves the final (scalar) result in the first-non-masked
377 element of the Vector used. With the predicate mask being dynamic
378 (but deterministic) at a superficial glance it seems this result
379 could be anywhere.
380
381 If that result is needed to be moved to a (single) scalar register
382 then a follow-up `sv.mv/sm=predicate rt, *ra` instruction will be
383 needed to get it, where the predicate is the exact same predicate used
384 in the prior Parallel-Reduction instruction.
385
386 * If there was only a single
387 bit in the predicate then the result will not have moved or been altered
388 from the source vector prior to the Reduction
389 * If there was more than one bit the result will be in the
390 first element with a predicate bit set.
391
392 In either case the result is in the element with the first bit set in
393 the predicate mask. Thus, no move/copy *within the Reduction itself* was needed.
394
395 Programmer's Note: For *some* hardware implementations
396 the vector-to-scalar copy may be a slow operation, as may the Predicated
397 Parallel Reduction itself.
398 It may be better to perform a pre-copy
399 of the values, compressing them (VREDUCE-style) into a contiguous block,
400 which will guarantee that the result goes into the very first element
401 of the destination vector, in which case clearly no follow-up
402 predicated vector-to-scalar MV operation is needed. A VREDUCE effect
403 is achieved by setting just a source predicate mask on Twin-Predicated
404 operations.
405
406 **Usage conditions**
407
408 The simplest usage is to perform an overwrite, specifying all three
409 register operands the same.
410
411 ```
412 svshape parallelreduce, 6
413 sv.add *8, *8, *8
414 ```
415
416 The Reduction Schedule will issue the Parallel Tree Reduction spanning
417 registers 8 through 13, by adjusting the offsets to RT, RA and RB as
418 necessary (see "Parallel Reduction algorithm" in a later section).
419
420 A non-overwrite is possible as well but just as with the overwrite
421 version, only those destination elements necessary for storing
422 intermediary computations will be written to: the remaining elements
423 will **not** be overwritten and will **not** be zero'd.
424
425 ```
426 svshape parallelreduce, 6
427 sv.add *0, *8, *8
428 ```
429
430 However it is critical to note that if the source and destination are
431 not the same then the trick of using a follow-up vector-scalar MV will
432 not work.
433
434 **Sub-Vector Horizontal Reduction**
435
436 To achieve Sub-Vector Horizontal Reduction, Pack/Unpack should be enabled,
437 which will turn the Schedule around such that issuing of the Scalar
438 Defined Word-instructions is done with SUBVL looping as the inner loop not the
439 outer loop. Rc=1 with Sub-Vectors (SUBVL=2,3,4) is `UNDEFINED` behaviour.
440
441 *Programmer's Note: Overwrite Parallel Reduction with Sub-Vectors
442 will clearly result in data corruption. It may be best to perform
443 a Pack/Unpack Transposing copy of the data first*
444
445 ## FFT/DCT mode
446
447 submode2=0 is for FFT. For FFT submode the following schedules may be
448 selected:
449
450 * **submode=0b00** selects the ``j`` offset of the innermost for-loop
451 of Tukey-Cooley
452 * **submode=0b10** selects the ``j+halfsize`` offset of the innermost for-loop
453 of Tukey-Cooley
454 * **submode=0b11** selects the ``k`` of exptable (which coefficient)
455
456 When submode2 is 1 or 2, for DCT inner butterfly submode the following
457 schedules may be selected. When submode2 is 1, additional bit-reversing
458 is also performed.
459
460 * **submode=0b00** selects the ``j`` offset of the innermost for-loop,
461 in-place
462 * **submode=0b010** selects the ``j+halfsize`` offset of the innermost for-loop,
463 in reverse-order, in-place
464 * **submode=0b10** selects the ``ci`` count of the innermost for-loop,
465 useful for calculating the cosine coefficient
466 * **submode=0b11** selects the ``size`` offset of the outermost for-loop,
467 useful for the cosine coefficient ``cos(ci + 0.5) * pi / size``
468
469 When submode2 is 3 or 4, for DCT outer butterfly submode the following
470 schedules may be selected. When submode is 3, additional bit-reversing
471 is also performed.
472
473 * **submode=0b00** selects the ``j`` offset of the innermost for-loop,
474 * **submode=0b01** selects the ``j+1`` offset of the innermost for-loop,
475
476 `zdimsz` is used as an in-place "Stride", particularly useful for
477 column-based in-place DCT/FFT.
478
479 ## Matrix Mode
480
481 In Matrix Mode, skip allows dimensions to be skipped from being included
482 in the resultant output index. This allows sequences to be repeated:
483 ```0 0 0 1 1 1 2 2 2 ...``` or in the case of skip=0b11 this results in
484 modulo ```0 1 2 0 1 2 ...```
485
486 * **skip=0b00** indicates no dimensions to be skipped
487 * **skip=0b01** sets "skip 1st dimension"
488 * **skip=0b10** sets "skip 2nd dimension"
489 * **skip=0b11** sets "skip 3rd dimension"
490
491 invxyz will invert the start index of each of x, y or z. If invxyz[0] is
492 zero then x-dimensional counting begins from 0 and increments, otherwise
493 it begins from xdimsz-1 and iterates down to zero. Likewise for y and z.
494
495 offset will have the effect of offsetting the result by ```offset``` elements:
496
497 ```
498 for i in 0..VL-1:
499 GPR(RT + remap(i) + SVSHAPE.offset) = ....
500 ```
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 ```
516 array[xdimsz+1][ydimsz+1][zdimsz+1]
517 ```
518
519 However whilst illustrative of the dimensionality, that does not take the
520 "permute" setting into account. "permute" may be any one of six values
521 (0-5, with values of 6 and 7 indicating "Indexed" Mode). The table
522 below shows how the permutation dimensionality order works:
523
524 | permute | order | array format |
525 | ------- | ----- | ------------------------ |
526 | 000 | 0,1,2 | (xdim+1)(ydim+1)(zdim+1) |
527 | 001 | 0,2,1 | (xdim+1)(zdim+1)(ydim+1) |
528 | 010 | 1,0,2 | (ydim+1)(xdim+1)(zdim+1) |
529 | 011 | 1,2,0 | (ydim+1)(zdim+1)(xdim+1) |
530 | 100 | 2,0,1 | (zdim+1)(xdim+1)(ydim+1) |
531 | 101 | 2,1,0 | (zdim+1)(ydim+1)(xdim+1) |
532 | 110 | 0,1 | Indexed (xdim+1)(ydim+1) |
533 | 111 | 1,0 | Indexed (ydim+1)(xdim+1) |
534
535 In other words, the "permute" option changes the order in which
536 nested for-loops over the array would be done. See executable
537 python reference code for further details.
538
539 *Note: permute=0b110 and permute=0b111 enable Indexed REMAP Mode,
540 described below*
541
542 With all these options it is possible to support in-place transpose,
543 in-place rotate, Matrix Multiply and Convolutions, without being
544 limited to Power-of-Two dimension sizes.
545
546 **Limitations and caveats**
547
548 Limitations of Matrix REMAP are that the Vector Length (VL) is currently
549 restricted to 127: up to 127 FMAs (or other operation)
550 may be performed in total.
551 Also given that it is in-registers only at present some care has to be
552 taken on regfile resource utilisation. However it is perfectly possible
553 to utilise Matrix REMAP to perform the three inner-most "kernel" loops of
554 the usual 6-level "Tiled" large Matrix Multiply, without the usual
555 difficulties associated with SIMD.
556
557 Also the `svshape` instruction only provides access to *part* of the
558 Matrix REMAP capability. Rotation and mirroring need to be done by
559 programming the SVSHAPE SPRs directly, which can take a lot more
560 instructions. Future versions of SVP64 will
561 provide more comprehensive capacity and
562 mitigate the need to write direct to the SVSHAPE SPRs.
563
564 Additionally there is not yet a way to set Matrix sizes from registers
565 with `svshape`: this was an intentional decision to simplify Hardware, that
566 may be corrected in a future version of SVP64. The limitation may presently
567 be overcome by direct programming of the SVSHAPE SPRs.
568
569 *Hardware Architectural note: with the Scheduling applying as a Phase between
570 Decode and Issue in a Deterministic fashion the Register Hazards may be
571 easily computed and a standard Out-of-Order Micro-Architecture exploited to good
572 effect. Even an In-Order system may observe that for large Outer Product
573 Schedules there will be no stalls, but if the Matrices are particularly
574 small size an In-Order system would have to stall, just as it would if
575 the operations were loop-unrolled without Simple-V. Thus: regardless
576 of the Micro-Architecture the Hardware Engineer should first consider
577 how best to process the exact same equivalent loop-unrolled instruction
578 stream. Once solved Matrix REMAP will fit naturally.*
579
580 ## Indexed Mode
581
582 Indexed Mode activates reading of the element indices from the GPR
583 and includes optional limited 2D reordering.
584 In its simplest form (without elwidth overrides or other modes):
585
586 ```
587 def index_remap(i):
588 return GPR((SVSHAPE.SVGPR<<1)+i) + SVSHAPE.offset
589
590 for i in 0..VL-1:
591 element_result = ....
592 GPR(RT + indexed_remap(i)) = element_result
593 ```
594
595 With element-width overrides included, and using the pseudocode
596 from the SVP64 [[sv/svp64/appendix#elwidth]] elwidth section
597 this becomes:
598
599 ```
600 def index_remap(i):
601 svreg = SVSHAPE.SVGPR << 1
602 srcwid = elwid_to_bitwidth(SVSHAPE.elwid)
603 offs = SVSHAPE.offset
604 return get_polymorphed_reg(svreg, srcwid, i) + offs
605
606 for i in 0..VL-1:
607 element_result = ....
608 rt_idx = indexed_remap(i)
609 set_polymorphed_reg(RT, destwid, rt_idx, element_result)
610 ```
611
612 Matrix-style reordering still applies to the indices, except limited
613 to up to 2 Dimensions (X,Y). Ordering is therefore limited to (X,Y) or
614 (Y,X) for in-place Transposition.
615 Only one dimension may optionally be skipped. Inversion of either
616 X or Y or both is possible (2D mirroring). Pseudocode for Indexed Mode (including elwidth
617 overrides) may be written in terms of Matrix Mode, specifically
618 purposed to ensure that the 3rd dimension (Z) has no effect:
619
620 ```
621 def index_remap(ISHAPE, i):
622 MSHAPE.skip = 0b0 || ISHAPE.sk1
623 MSHAPE.invxyz = 0b0 || ISHAPE.invxy
624 MSHAPE.xdimsz = ISHAPE.xdimsz
625 MSHAPE.ydimsz = ISHAPE.ydimsz
626 MSHAPE.zdimsz = 0 # disabled
627 if ISHAPE.permute = 0b110 # 0,1
628 MSHAPE.permute = 0b000 # 0,1,2
629 if ISHAPE.permute = 0b111 # 1,0
630 MSHAPE.permute = 0b010 # 1,0,2
631 el_idx = remap_matrix(MSHAPE, i)
632 svreg = ISHAPE.SVGPR << 1
633 srcwid = elwid_to_bitwidth(ISHAPE.elwid)
634 offs = ISHAPE.offset
635 return get_polymorphed_reg(svreg, srcwid, el_idx) + offs
636 ```
637
638 The most important observation above is that the Matrix-style
639 remapping occurs first and the Index lookup second. Thus it
640 becomes possible to perform in-place Transpose of Indices which
641 may have been costly to set up or costly to duplicate
642 (waste register file space). In other words: it is fine for two or more
643 SVSHAPEs to simultaneously use the same
644 Indices (use the same GPRs), even if one SVSHAPE has different
645 2D dimensions and ordering from the others.
646
647 **Caveats and Limitations**
648
649 The purpose of Indexing is to provide a generalised version of
650 Vector ISA "Permute" instructions, such as VSX `vperm`. The
651 Indexing is abstracted out and may be applied to much more
652 than an element move/copy, and is not limited for example
653 to the number of bytes that can fit into a VSX register.
654 Indexing may be applied to LD/ST (even on Indexed LD/ST
655 instructions such as `sv.lbzx`), arithmetic operations,
656 extsw: there is no artificial limit.
657
658 The only major caveat is that the registers to be used as
659 Indices must not be modified by any instruction after Indexed Mode
660 is established, and neither must MAXVL be altered. Additionally,
661 no register used as an Index may exceed MAXVL-1.
662
663 Failure to observe
664 these conditions results in `UNDEFINED` behaviour.
665 These conditions allow a Read-After-Write (RAW) Hazard to be created on
666 the entire range of Indices to be subsequently used, but a corresponding
667 Write-After-Read Hazard by any instruction that modifies the Indices
668 **does not have to be created**. Given the large number of registers
669 involved in Indexing this is a huge resource saving and reduction
670 in micro-architectural complexity. MAXVL is likewise
671 included in the RAW Hazards because it is involved in calculating
672 how many registers are to be considered Indices.
673
674 With these Hazard Mitigations in place, high-performance implementations
675 may read-cache the Indices at the point where a given `svindex` instruction
676 is called (or SVSHAPE SPRs - and MAXVL - directly altered) by issuing
677 background GPR register file reads whilst other instructions are being
678 issued and executed.
679
680 Indexed REMAP **does not prevent conflicts** (overlapping
681 destinations), which on a superficial analysis may be perceived to be a
682 problem, until it is recalled that, firstly, Simple-V is designed specifically
683 to require Program Order to be respected, and that Matrix, DCT and FFT
684 all *already* critically depend on overlapping Reads/Writes: Matrix
685 uses overlapping registers as accumulators. Thus the Register Hazard
686 Management needed by Indexed REMAP *has* to be in place anyway.
687
688 *Programmer's Note: `hphint` may be used to help hardware identify
689 parallelism opportunities but it is critical to remember that the
690 groupings are by `FLOOR(step/MAXVL)` not `FLOOR(REMAP(step)/MAXVL)`.*
691
692 The cost compared to Matrix and other REMAPs (and Pack/Unpack) is
693 clearly that of the additional reading of the GPRs to be used as Indices,
694 plus the setup cost associated with creating those same Indices.
695 If any Deterministic REMAP can cover the required task, clearly it
696 is adviseable to use it instead.
697
698 *Programmer's note: some algorithms may require skipping of Indices exceeding
699 VL-1, not MAXVL-1. This may be achieved programmatically by performing
700 an `sv.cmp *BF,*RA,RB` where RA is the same GPRs used in the Indexed REMAP,
701 and RB contains the value of VL returned from `setvl`. The resultant
702 CR Fields may then be used as Predicate Masks to exclude those operations
703 with an Index exceeding VL-1.*
704
705 -------------
706
707 \newpage{}
708
709 # svshape instruction <a name="svshape"> </a>
710
711 SVM-Form
712
713 svshape SVxd,SVyd,SVzd,SVRM,vf
714
715 | 0:5|6:10 |11:15 |16:20 | 21:24 | 25 | 26:31 | name |
716 | -- | -- | --- | ----- | ------ | -- | ------| -------- |
717 |PO | SVxd | SVyd | SVzd | SVRM | vf | XO | svshape |
718
719 See [[sv/remap/appendix]] for `svshape` pseudocode
720
721 Special Registers Altered:
722
723 ```
724 SVSTATE, SVSHAPE0-3
725 ```
726
727 `svshape` is a convenience instruction that reduces instruction
728 count for common usage patterns, particularly Matrix, DCT and FFT. It sets up
729 (overwrites) all required SVSHAPE SPRs and also modifies SVSTATE
730 including VL and MAXVL. Using `svshape` therefore does not also
731 require `setvl`.
732
733 Fields:
734
735 * **SVxd** - SV REMAP "xdim" (X-dimension)
736 * **SVyd** - SV REMAP "ydim" (Y-dimension, sometimes used for sub-mode selection)
737 * **SVzd** - SV REMAP "zdim" (Z-dimension)
738 * **SVRM** - SV REMAP Mode (0b00000 for Matrix, 0b00001 for FFT etc.)
739 * **vf** - sets "Vertical-First" mode
740 * **XO** - standard 6-bit XO field
741
742 *Note: SVxd, SVyz and SVzd are all stored "off-by-one". In the assembler
743 mnemonic the values `1-32` are stored in binary as `0b00000..0b11111`*
744
745 There are 12 REMAP Modes (2 Modes are RESERVED for `svshape2`, 2 Modes
746 are RESERVED)
747
748 | SVRM | Remap Mode description |
749 | -- | -- |
750 | 0b0000 | Matrix 1/2/3D |
751 | 0b0001 | FFT Butterfly |
752 | 0b0010 | reserved |
753 | 0b0011 | DCT Outer butterfly |
754 | 0b0100 | DCT Inner butterfly, on-the-fly (Vertical-First Mode) |
755 | 0b0101 | DCT COS table index generation |
756 | 0b0110 | DCT half-swap |
757 | 0b0111 | Parallel Reduction and Prefix Sum |
758 | 0b1000 | reserved for svshape2 |
759 | 0b1001 | reserved for svshape2 |
760 | 0b1010 | reserved |
761 | 0b1011 | iDCT Outer butterfly |
762 | 0b1100 | iDCT Inner butterfly, on-the-fly (Vertical-First Mode) |
763 | 0b1101 | iDCT COS table index generation |
764 | 0b1110 | iDCT half-swap |
765 | 0b1111 | FFT half-swap |
766
767 Examples showing how all of these Modes operate exists in the online
768 [SVP64 unit tests](https://git.libre-soc.org/?p=openpower-isa.git;a=tree;f=src/openpower/decoder/isa;hb=HEAD). Explaining
769 these Modes further in detail is beyond the scope of this document.
770
771 In Indexed Mode, there are only 5 bits available to specify the GPR
772 to use, out of 128 GPRs (7 bit numbering). Therefore, only the top
773 5 bits are given in the `SVxd` field: the bottom two implicit bits
774 will be zero (`SVxd || 0b00`).
775
776 `svshape` has *limited applicability* due to being a 32-bit instruction.
777 The full capability of SVSHAPE SPRs may be accessed by directly writing
778 to SVSHAPE0-3 with `mtspr`. Circumstances include Matrices with dimensions
779 larger than 32, and in-place Transpose. Potentially a future
780 instruction may extend the capability here.
781
782 Programmer's Note: Parallel Reduction Mode is selected by setting `SVRM=7,SVyd=1`.
783 Prefix Sum Mode is selected by setting `SVRM=7,SVyd=3`:
784
785 ```
786 # Vector length of 8.
787 svshape 8, 3, 1, 0x7, 0
788 # activate SVSHAPE0 (prefix-sum lhs) for RA
789 # activate SVSHAPE1 (prefix-sum rhs) for RT and RB
790 svremap 7, 0, 1, 0, 1, 0, 0
791 sv.add *10, *10, *10
792 ```
793
794 *Architectural Resource Allocation note: the SVRM field is carefully
795 crafted to allocate two Modes, corresponding to bits 21-23 within the
796 instruction being set to the value `0b100`, to `svshape2` (not
797 `svshape`). These two Modes are
798 considered "RESERVED" within the context of `svshape` but it is
799 absolutely critical to allocate the exact same pattern in XO for
800 both instructions in bits 26-31.*
801
802 -------------
803
804 \newpage{}
805
806
807 # svindex instruction <a name="svindex"> </a>
808
809 SVI-Form
810
811 | 0:5|6:10 |11:15 |16:20 | 21:25 | 26:31 | Form |
812 | -- | -- | --- | ---- | ----------- | ------| -------- |
813 | PO | SVG | rmm | SVd | ew/yx/mm/sk | XO | SVI-Form |
814
815 * svindex SVG,rmm,SVd,ew,SVyx,mm,sk
816
817 See [[sv/remap/appendix]] for `svindex` pseudocode
818
819 Special Registers Altered:
820
821 ```
822 SVSTATE, SVSHAPE0-3
823 ```
824
825 `svindex` is a convenience instruction that reduces instruction count
826 for Indexed REMAP Mode. It sets up (overwrites) all required SVSHAPE
827 SPRs and **unlike** `svshape` can modify the REMAP area of the SVSTATE
828 SPR as well, including setting persistence. The relevant SPRs *may*
829 be directly programmed with `mtspr` however it is laborious to do so:
830 svindex saves instructions covering much of Indexed REMAP capability.
831
832 Fields:
833
834 * **SVd** - SV REMAP x/y dim
835 * **rmm** - REMAP mask: sets remap mi0-2/mo0-1 and SVSHAPEs,
836 controlled by mm
837 * **ew** - sets element width override on the Indices
838 * **SVG** - GPR SVG<<2 to be used for Indexing
839 * **yx** - 2D reordering to be used if yx=1
840 * **mm** - mask mode. determines how `rmm` is interpreted.
841 * **sk** - Dimension skipping enabled
842
843 *Note: SVd, like SVxd, SVyz and SVzd of `svshape`, are all stored
844 "off-by-one". In the assembler
845 mnemonic the values `1-32` are stored in binary as `0b00000..0b11111`*.
846
847 *Note: when `yx=1,sk=0` the second dimension is calculated as
848 `CEIL(MAXVL/SVd)`*.
849
850 When `mm=0`:
851
852 * `rmm`, like REMAP.SVme, has bit 0
853 correspond to mi0, bit 1 to mi1, bit 2 to mi2,
854 bit 3 to mo0 and bit 4 to mi1
855 * all SVSHAPEs and the REMAP parts of SVSHAPE are first reset (initialised to zero)
856 * for each bit set in the 5-bit `rmm`, in order, the first
857 as-yet-unset SVSHAPE will be updated
858 with the other operands in the instruction, and the REMAP
859 SPR set.
860 * If all 5 bits of `rmm` are set then both mi0 and mo1 use SVSHAPE0.
861 * SVSTATE persistence bit is cleared
862 * No other alterations to SVSTATE are carried out
863
864 Example 1: if rmm=0b00110 then SVSHAPE0 and SVSHAPE1 are set up,
865 and the REMAP SPR set so that mi1 uses SVSHAPE0 and mi2
866 uses mi2. REMAP.SVme is also set to 0b00110, REMAP.mi1=0
867 (SVSHAPE0) and REMAP.mi2=1 (SVSHAPE1)
868
869 Example 2: if rmm=0b10001 then again SVSHAPE0 and SVSHAPE1
870 are set up, but the REMAP SPR is set so that mi0 uses SVSHAPE0
871 and mo1 uses SVSHAPE1. REMAP.SVme=0b10001, REMAP.mi0=0, REMAP.mo1=1
872
873 Rough algorithmic form:
874
875 ```
876 marray = [mi0, mi1, mi2, mo0, mo1]
877 idx = 0
878 for bit = 0 to 4:
879 if not rmm[bit]: continue
880 setup(SVSHAPE[idx])
881 SVSTATE{marray[bit]} = idx
882 idx = (idx+1) modulo 4
883 ```
884
885 When `mm=1`:
886
887 * bits 0-2 (MSB0 numbering) of `rmm` indicate an index selecting mi0-mo1
888 * bits 3-4 (MSB0 numbering) of `rmm` indicate which SVSHAPE 0-3 shall
889 be updated
890 * only the selected SVSHAPE is overwritten
891 * only the relevant bits in the REMAP area of SVSTATE are updated
892 * REMAP persistence bit is set.
893
894 Example 1: if `rmm`=0b01110 then bits 0-2 (MSB0) are 0b011 and
895 bits 3-4 are 0b10. thus, mo0 is selected and SVSHAPE2
896 to be updated. REMAP.SVme[3] will be set high and REMAP.mo0
897 set to 2 (SVSHAPE2).
898
899 Example 2: if `rmm`=0b10011 then bits 0-2 (MSB0) are 0b100 and
900 bits 3-4 are 0b11. thus, mo1 is selected and SVSHAPE3
901 to be updated. REMAP.SVme[4] will be set high and REMAP.mo1
902 set to 3 (SVSHAPE3).
903
904 Rough algorithmic form:
905
906 ```
907 marray = [mi0, mi1, mi2, mo0, mo1]
908 bit = rmm[0:2]
909 idx = rmm[3:4]
910 setup(SVSHAPE[idx])
911 SVSTATE{marray[bit]} = idx
912 SVSTATE.pst = 1
913 ```
914
915 In essence, `mm=0` is intended for use to set as much of the
916 REMAP State SPRs as practical with a single instruction,
917 whilst `mm=1` is intended to be a little more refined.
918
919 **Usage guidelines**
920
921 * **Disable 2D mapping**: to only perform Indexing without
922 reordering use `SVd=1,sk=0,yx=0` (or set SVd to a value larger
923 or equal to VL)
924 * **Modulo 1D mapping**: to perform Indexing cycling through the
925 first N Indices use `SVd=N,sk=0,yx=0` where `VL>N`. There is
926 no requirement to set VL equal to a multiple of N.
927 * **Modulo 2D transposed**: `SVd=M,sk=0,yx=1`, sets
928 `xdim=M,ydim=CEIL(MAXVL/M)`.
929
930 Beyond these mappings it becomes necessary to write directly to
931 the SVSTATE SPRs manually.
932
933 -------------
934
935 \newpage{}
936
937
938 # svshape2 (offset-priority) <a name="svshape2"> </a>
939
940 SVM2-Form
941
942 | 0:5|6:9 |10|11:15 |16:20 | 21:24 | 25 | 26:31 | Form |
943 | -- |----|--| --- | ----- | ------ | -- | ------| -------- |
944 | PO |offs|yx| rmm | SVd | 100/mm | sk | XO | SVM2-Form |
945
946 * svshape2 offs,yx,rmm,SVd,sk,mm
947
948 See [[sv/remap/appendix]] for `svshape2` pseudocode
949
950 Special Registers Altered:
951
952 ```
953 SVSTATE, SVSHAPE0-3
954 ```
955
956 `svshape2` is an additional convenience instruction that prioritises
957 setting `SVSHAPE.offset`. Its primary purpose is for use when
958 element-width overrides are used. It has identical capabilities to `svindex`
959 in terms of both options (skip, etc.) and ability to activate REMAP
960 (rmm, mask mode) but unlike `svindex` it does not set GPR REMAP:
961 only a 1D or 2D `svshape`, and
962 unlike `svshape` it can set an arbitrary `SVSHAPE.offset` immediate.
963
964 One of the limitations of Simple-V is that Vector elements start on the boundary
965 of the Scalar regfile, which is fine when element-width overrides are not
966 needed. If the starting point of a Vector with smaller elwidths must begin
967 in the middle of a register, normally there would be no way to do so except
968 through costly LD/ST. `SVSHAPE.offset` caters for this scenario and `svshape2`
969 makes it easier to access.
970
971 **Operand Fields**:
972
973 * **offs** (4 bits) - unsigned offset
974 * **yx** (1 bit) - swap XY to YX
975 * **SVd** dimension size
976 * **rmm** REMAP mask
977 * **mm** mask mode
978 * **sk** (1 bit) skips 1st dimension if set
979
980 Dimensions are calculated exactly as `svindex`. `rmm` and
981 `mm` are as per `svindex`.
982
983 *Programmer's Note: offsets for `svshape2` may be specified in the range
984 0-15. Given that the principle of Simple-V is to fit on top of
985 byte-addressable register files and that GPR and FPR are 64-bit (8 bytes)
986 it should be clear that the offset may, when `elwidth=8`, begin an
987 element-level operation starting element zero at any arbitrary byte.
988 On cursory examination attempting to go beyond the range 0-7 seems
989 unnecessary given that the **next GPR or FPR** is an
990 alias for an offset in the range 8-15. Thus by simply increasing
991 the starting Vector point of the operation to the next register it
992 can be seen that the offset of 0-7 would be sufficient. Unfortunately
993 however some operations are EXTRA2-encoded it is **not possible**
994 to increase the GPR/FPR register number by one, because EXTRA2-encoding
995 of GPR/FPR Vector numbers are restricted to even numbering.
996 For CR Fields the EXTRA2 encoding is even more sparse.
997 The additional offset range (8-15) helps overcome these limitations.*
998
999 *Hardware Implementor's note: with the offsets only being immediates
1000 and with register numbering being entirely immediate as well it is
1001 possible to correctly compute Register Hazards without requiring
1002 reading the contents of any SPRs. If however there are
1003 instructions that have directly written to the SVSTATE or SVSHAPE
1004 SPRs and those instructions are still in-flight then this position
1005 is clearly **invalid**. This is why Programmers are strongly
1006 discouraged from directly writing to these SPRs.*
1007
1008 *Architectural Resource Allocation note: this instruction shares
1009 the space of `svshape`. Therefore it is critical that the two
1010 instructions, `svshape` and `svshape2` have the exact same XO
1011 in bits 26 thru 31. It is also critical that for `svshape2`,
1012 bit 21 of XO is a 1, bit 22 of XO is a 0, and bit 23 of XO is a 0.*
1013
1014 [[!tag standards]]
1015
1016 -------------
1017
1018 \newpage{}
1019