invert table order in remap.mwdn (keep bitpositions)
[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 (when elwidth overrides are
24 used), independently on each Vector src or dest register. Aside from
25 Indexed REMAP this is entirely Hardware-accelerated reordering and
26 consequently not costly in terms of register access. It will however
27 place a burden on Multi-Issue systems but no more than if the equivalent
28 Scalar instructions were explicitly loop-unrolled without SVP64, and
29 some advanced implementations may even find the Deterministic nature of
30 the Scheduling to be easier on resources.
31
32 The initial primary motivation of REMAP was for Matrix Multiplication,
33 reordering of sequential data in-place: in-place DCT and FFT were
34 easily justified given the exceptionally high usage in Computer Science.
35 Four SPRs are provided which may be applied to any GPR, FPR or CR Field so
36 that for example a single FMAC may be used in a single hardware-controlled
37 100% Deterministic loop to perform 5x3 times 3x4 Matrix multiplication,
38 generating 60 FMACs *without needing explicit assembler unrolling*.
39 Additional uses include regular "Structure Packing" such as RGB pixel
40 data extraction and reforming (although less costly vec2/3/4 reshaping
41 is achievable with `PACK/UNPACK`).
42
43 Even once designed as an independent RISC-paradigm abstraction system
44 it was realised that Matrix REMAP could be applied to min/max instructions to
45 achieve Floyd-Warshall Graph computations, or to AND/OR Ternary
46 bitmanipulation to compute Warshall Transitive Closure, or
47 to perform Cryptographic Matrix operations with Galois Field
48 variants of Multiply-Accumulate and many more uses expected to be
49 discovered. This *without
50 adding actual explicit Vector opcodes for any of the same*.
51
52 Thus it should be very clear:
53 REMAP, like all of SV, is abstracted out, meaning that unlike traditional
54 Vector ISAs which would typically only have a limited set of instructions
55 that can be structure-packed (LD/ST and Move operations
56 being the most common), REMAP may be applied to
57 literally any instruction: CRs, Arithmetic, Logical, LD/ST, even
58 Vectorised Branch-Conditional.
59
60 When SUBVL is greater than 1 a given group of Subvector
61 elements are kept together: effectively the group becomes the
62 element, and with REMAP applying to elements
63 (not sub-elements) each group is REMAPed together.
64 Swizzle *can* however be applied to the same
65 instruction as REMAP, providing re-sequencing of
66 Subvector elements which REMAP cannot. Also as explained in [[sv/mv.swizzle]], [[sv/mv.vec]] and the [[svp64/appendix]], Pack and Unpack Mode bits
67 can extend down into Sub-vector elements to influence vec2/vec3/vec4
68 sequential reordering, but even here, REMAP reordering is not *individually*
69 extended down to the actual sub-vector elements themselves.
70 This keeps the relevant Predicate Mask bit applicable to the Subvector
71 group, just as it does when REMAP is not active.
72
73 In its general form, REMAP is quite expensive to set up, and on some
74 implementations may introduce latency, so should realistically be used
75 only where it is worthwhile. Given that even with latency the fact
76 that up to 127 operations can be Deterministically issued (from a single
77 instruction) it should be clear that REMAP should not be dismissed
78 for *possible* latency alone. Commonly-used patterns such as Matrix
79 Multiply, DCT and FFT have helper instruction options which make REMAP
80 easier to use.
81
82 *Future specification note: future versions of the REMAP Management instructions
83 will extend to EXT1xx Prefixed variants. This will overcome some of the limitations
84 present in the 32-bit variants of the REMAP Management instructions that at
85 present require direct writing to SVSHAPE0-3 SPRs. Additional
86 REMAP Modes may also be introduced at that time.*
87
88 There are four types of REMAP:
89
90 * **Matrix**, also known as 2D and 3D reshaping, can perform in-place
91 Matrix transpose and rotate. The Shapes are set up for an "Outer Product"
92 Matrix Multiply.
93 * **FFT/DCT**, with full triple-loop in-place support: limited to
94 Power-2 RADIX
95 * **Indexing**, for any general-purpose reordering, also includes
96 limited 2D reshaping as well as Element "offsetting".
97 * **Parallel Reduction**, for scheduling a sequence of operations
98 in a Deterministic fashion, in a way that may be parallelised,
99 to reduce a Vector down to a single value.
100
101 Best implemented on top of a Multi-Issue Out-of-Order Micro-architecture,
102 REMAP Schedules are 100% Deterministic **including Indexing** and are
103 designed to be incorporated in between the Decode and Issue phases,
104 directly into Register Hazard Management.
105
106 As long as the SVSHAPE SPRs
107 are not written to directly, Hardware may treat REMAP as 100%
108 Deterministic: all REMAP Management instructions take static
109 operands (no dynamic register operands)
110 with the exception of Indexed Mode, and even then
111 Architectural State is permitted to assume that the Indices
112 are cacheable from the point at which the `svindex` instruction
113 is executed.
114
115 Parallel Reduction is unusual in that it requires a full vector array
116 of results (not a scalar) and uses the rest of the result Vector for
117 the purposes of storing intermediary calculations. As these intermediary
118 results are Deterministically computed they may be useful.
119 Additionally, because the intermediate results are always written out
120 it is possible to service Precise Interrupts without affecting latency
121 (a common limitation of Vector ISAs implementing explicit
122 Parallel Reduction instructions, because their Architectural State cannot
123 hold the partial results).
124
125 ## Basic principle
126
127 The following illustrates why REMAP was added.
128
129 * normal vector element read/write of operands would be sequential
130 (0 1 2 3 ....)
131 * this is not appropriate for (e.g.) Matrix multiply which requires
132 accessing elements in alternative sequences (0 3 6 1 4 7 ...)
133 * normal Vector ISAs use either Indexed-MV or Indexed-LD/ST to "cope"
134 with this. both are expensive (copy large vectors, spill through memory)
135 and very few Packed SIMD ISAs cope with non-Power-2
136 (Duplicate-data inline-loop-unrolling is the costly solution)
137 * REMAP **redefines** the order of access according to set
138 (Deterministic) "Schedules".
139 * Matrix Schedules are not at all restricted to power-of-two boundaries
140 making it unnecessary to have for example specialised 3x4 transpose
141 instructions of other Vector ISAs.
142 * DCT and FFT REMAP are RADIX-2 limited but this is the case in existing Packed/Predicated
143 SIMD ISAs anyway (and Bluestein Convolution is typically deployed to
144 solve that).
145
146 Only the most commonly-used algorithms in computer science have REMAP
147 support, due to the high cost in both the ISA and in hardware. For
148 arbitrary remapping the `Indexed` REMAP may be used.
149
150 ## Example Usage
151
152 * `svshape` to set the type of reordering to be applied to an
153 otherwise usual `0..VL-1` hardware for-loop
154 * `svremap` to set which registers a given reordering is to apply to
155 (RA, RT etc)
156 * `sv.{instruction}` where any Vectorised register marked by `svremap`
157 will have its ordering REMAPPED according to the schedule set
158 by `svshape`.
159
160 The following illustrative example multiplies a 3x4 and a 5x3
161 matrix to create
162 a 5x4 result:
163
164 ```
165 svshape 5,4,3,0,0 # Outer Product 5x4 by 4x3
166 svremap 15,1,2,3,0,0,0,0 # link Schedule to registers
167 sv.fmadds *0,*32,*64,*0 # 60 FMACs get executed here
168 ```
169
170 * svshape sets up the four SVSHAPE SPRS for a Matrix Schedule
171 * svremap activates four out of five registers RA RB RC RT RS (15)
172 * svremap requests:
173 - RA to use SVSHAPE1
174 - RB to use SVSHAPE2
175 - RC to use SVSHAPE3
176 - RT to use SVSHAPE0
177 - RS Remapping to not be activated
178 * sv.fmadds has vectors RT=0, RA=32, RB=64, RC=0
179 * With REMAP being active each register's element index is
180 *independently* transformed using the specified SHAPEs.
181
182 Thus the Vector Loop is arranged such that the use of
183 the multiply-and-accumulate instruction executes precisely the required
184 Schedule to perform an in-place in-registers Outer Product
185 Matrix Multiply with no
186 need to perform additional Transpose or register copy instructions.
187 The example above may be executed as a unit test and demo,
188 [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)
189
190 *Hardware Architectural note: with the Scheduling applying as a Phase between
191 Decode and Issue in a Deterministic fashion the Register Hazards may be
192 easily computed and a standard Out-of-Order Micro-Architecture exploited to good
193 effect. Even an In-Order system may observe that for large Outer Product
194 Schedules there will be no stalls, but if the Matrices are particularly
195 small size an In-Order system would have to stall, just as it would if
196 the operations were loop-unrolled without Simple-V. Thus: regardless
197 of the Micro-Architecture the Hardware Engineer should first consider
198 how best to process the exact same equivalent loop-unrolled instruction
199 stream.*
200
201 ## REMAP types
202
203 This section summarises the motivation for each REMAP Schedule
204 and briefly goes over their characteristics and limitations.
205 Further details on the Deterministic Precise-Interruptible algorithms
206 used in these Schedules is found in the [[sv/remap/appendix]].
207
208 ### Matrix (1D/2D/3D shaping)
209
210 Matrix Multiplication is a huge part of High-Performance Compute,
211 and 3D.
212 In many PackedSIMD as well as Scalable Vector ISAs, non-power-of-two
213 Matrix sizes are a serious challenge. PackedSIMD ISAs, in order to
214 cope with for example 3x4 Matrices, recommend rolling data-repetition and loop-unrolling.
215 Aside from the cost of the load on the L1 I-Cache, the trick only
216 works if one of the dimensions X or Y are power-two. Prime Numbers
217 (5x7, 3x5) become deeply problematic to unroll.
218
219 Even traditional Scalable Vector ISAs have issues with Matrices, often
220 having to perform data Transpose by pushing out through Memory and back
221 (costly),
222 or computing Transposition Indices (costly) then copying to another
223 Vector (costly).
224
225 Matrix REMAP was thus designed to solve these issues by providing Hardware
226 Assisted
227 "Schedules" that can view what would otherwise be limited to a strictly
228 linear Vector as instead being 2D (even 3D) *in-place* reordered.
229 With both Transposition and non-power-two being supported the issues
230 faced by other ISAs are mitigated.
231
232 Limitations of Matrix REMAP are that the Vector Length (VL) is currently
233 restricted to 127: up to 127 FMAs (or other operation)
234 may be performed in total.
235 Also given that it is in-registers only at present some care has to be
236 taken on regfile resource utilisation. However it is perfectly possible
237 to utilise Matrix REMAP to perform the three inner-most "kernel" loops of
238 the usual 6-level "Tiled" large Matrix Multiply, without the usual
239 difficulties associated with SIMD.
240
241 Also the `svshape` instruction only provides access to part of the
242 Matrix REMAP capability. Rotation and mirroring need to be done by
243 programming the SVSHAPE SPRs directly, which can take a lot more
244 instructions. Future versions of SVP64 will include EXT1xx prefixed
245 variants (`psvshape`) which provide more comprehensive capacity and
246 mitigate the need to write direct to the SVSHAPE SPRs.
247
248 ### FFT/DCT Triple Loop
249
250 DCT and FFT are some of the most astonishingly used algorithms in
251 Computer Science. Radar, Audio, Video, R.F. Baseband and dozens more. At least
252 two DSPs, TMS320 and Hexagon, have VLIW instructions specially tailored
253 to FFT.
254
255 An in-depth analysis showed that it is possible to do in-place in-register
256 DCT and FFT as long as twin-result "butterfly" instructions are provided.
257 These can be found in the [[openpower/isa/svfparith]] page if performing
258 IEEE754 FP transforms. *(For fixed-point transforms, equivalent 3-in 2-out
259 integer operations would be required)*. These "butterfly" instructions
260 avoid the need for a temporary register because the two array positions
261 being overwritten will be "in-flight" in any In-Order or Out-of-Order
262 micro-architecture.
263
264 DCT and FFT Schedules are currently limited to RADIX2 sizes and do not
265 accept predicate masks. Given that it is common to perform recursive
266 convolutions combining smaller Power-2 DCT/FFT to create larger DCT/FFTs
267 in practice the RADIX2 limit is not a problem. A Bluestein convolution
268 to compute arbitrary length is demonstrated by
269 [Project Nayuki](https://www.nayuki.io/res/free-small-fft-in-multiple-languages/fft.py)
270
271 ### Indexed
272
273 The purpose of Indexing is to provide a generalised version of
274 Vector ISA "Permute" instructions, such as VSX `vperm`. The
275 Indexing is abstracted out and may be applied to much more
276 than an element move/copy, and is not limited for example
277 to the number of bytes that can fit into a VSX register.
278 Indexing may be applied to LD/ST (even on Indexed LD/ST
279 instructions such as `sv.lbzx`), arithmetic operations,
280 extsw: there is no artificial limit.
281
282 The only major caveat is that the registers to be used as
283 Indices must not be modified by any instruction after Indexed Mode
284 is established, and neither must MAXVL be altered. Additionally,
285 no register used as an Index may exceed MAXVL-1.
286
287 Failure to observe
288 these conditions results in `UNDEFINED` behaviour.
289 These conditions allow a Read-After-Write (RAW) Hazard to be created on
290 the entire range of Indices to be subsequently used, but a corresponding
291 Write-After-Read Hazard by any instruction that modifies the Indices
292 **does not have to be created**. Given the large number of registers
293 involved in Indexing this is a huge resource saving and reduction
294 in micro-architectural complexity. MAXVL is likewise
295 included in the RAW Hazards because it is involved in calculating
296 how many registers are to be considered Indices.
297
298 With these Hazard Mitigations in place, high-performance implementations
299 may read-cache the Indices at the point where a given `svindex` instruction
300 is called (or SVSHAPE SPRs - and MAXVL - directly altered) by issuing
301 background GPR register file reads whilst other instructions are being
302 issued and executed.
303
304 The original motivation for Indexed REMAP was to mitigate the need to add
305 an expensive `mv.x` to the Scalar ISA, which was likely to be rejected as
306 a stand-alone instruction
307 (`GPR(RT) <- GPR(GPR(RA))`). Usually a Vector ISA would add a non-conflicting
308 variant (as in VSX `vperm`) but it is common to need to permute by source,
309 with the risk of conflict, that has to be resolved, for example, in AVX-512
310 with `conflictd`.
311
312 Indexed REMAP on the other hand **does not prevent conflicts** (overlapping
313 destinations), which on a superficial analysis may be perceived to be a
314 problem, until it is recalled that, firstly, Simple-V is designed specifically
315 to require Program Order to be respected, and that Matrix, DCT and FFT
316 all *already* critically depend on overlapping Reads/Writes: Matrix
317 uses overlapping registers as accumulators. Thus the Register Hazard
318 Management needed by Indexed REMAP *has* to be in place anyway.
319
320 The cost compared to Matrix and other REMAPs (and Pack/Unpack) is
321 clearly that of the additional reading of the GPRs to be used as Indices,
322 plus the setup cost associated with creating those same Indices.
323 If any Deterministic REMAP can cover the required task, clearly it
324 is adviseable to use it instead.
325
326 *Programmer's note: some algorithms may require skipping of Indices exceeding
327 VL-1, not MAXVL-1. This may be achieved programmatically by performing
328 an `sv.cmp *BF,*RA,RB` where RA is the same GPRs used in the Indexed REMAP,
329 and RB contains the value of VL returned from `setvl`. The resultant
330 CR Fields may then be used as Predicate Masks to exclude those operations
331 with an Index exceeding VL-1.*
332
333 ### Parallel Reduction
334
335 Vector Reduce Mode issues a deterministic tree-reduction schedule to the underlying micro-architecture. Like Scalar reduction, the "Scalar Base"
336 (Power ISA v3.0B) operation is leveraged, unmodified, to give the
337 *appearance* and *effect* of Reduction. Parallel Reduction is not limited
338 to Power-of-two but is limited as usual by the total number of
339 element operations (127) as well as available register file size.
340
341 In Horizontal-First Mode, Vector-result reduction **requires**
342 the destination to be a Vector, which will be used to store
343 intermediary results, in order to achieve a correct final
344 result.
345
346 Given that the tree-reduction schedule is deterministic,
347 Interrupts and exceptions
348 can therefore also be precise. The final result will be in the first
349 non-predicate-masked-out destination element, but due again to
350 the deterministic schedule programmers may find uses for the intermediate
351 results.
352
353 When Rc=1 a corresponding Vector of co-resultant CRs is also
354 created. No special action is taken: the result *and its CR Field*
355 are stored "as usual" exactly as all other SVP64 Rc=1 operations.
356
357 Note that the Schedule only makes sense on top of certain instructions:
358 X-Form with a Register Profile of `RT,RA,RB` is fine because two sources
359 and the destination are all the same type. Like Scalar
360 Reduction, nothing is prohibited:
361 the results of execution on an unsuitable instruction may simply
362 not make sense. With care, even 3-input instructions (madd, fmadd, ternlogi)
363 may be used, and whilst it is down to the Programmer to walk through the
364 process the Programmer can be confident that the Parallel-Reduction is
365 guaranteed 100% Deterministic.
366
367 Critical to note regarding use of Parallel-Reduction REMAP is that,
368 exactly as with all REMAP Modes, the `svshape` instruction *requests*
369 a certain Vector Length (number of elements to reduce) and then
370 sets VL and MAXVL at the number of **operations** needed to be
371 carried out. Thus, equally as importantly, like Matrix REMAP
372 the total number of operations
373 is restricted to 127. Any Parallel-Reduction requiring more operations
374 will need to be done manually in batches (hierarchical
375 recursive Reduction).
376
377 Also important to note is that the Deterministic Schedule is arranged
378 so that some implementations *may* parallelise it (as long as doing so
379 respects Program Order and Register Hazards). Performance (speed)
380 of any given
381 implementation is neither strictly defined or guaranteed. As with
382 the Vulkan(tm) Specification, strict compliance is paramount whilst
383 performance is at the discretion of Implementors.
384
385 **Parallel-Reduction with Predication**
386
387 To avoid breaking the strict RISC-paradigm, keeping the Issue-Schedule
388 completely separate from the actual element-level (scalar) operations,
389 Move operations are **not** included in the Schedule. This means that
390 the Schedule leaves the final (scalar) result in the first-non-masked
391 element of the Vector used. With the predicate mask being dynamic
392 (but deterministic) at a superficial glance it seems this result
393 could be anywhere.
394
395 If that result is needed to be moved to a (single) scalar register
396 then a follow-up `sv.mv/sm=predicate rt, *ra` instruction will be
397 needed to get it, where the predicate is the exact same predicate used
398 in the prior Parallel-Reduction instruction.
399
400 * If there was only a single
401 bit in the predicate then the result will not have moved or been altered
402 from the source vector prior to the Reduction
403 * If there was more than one bit the result will be in the
404 first element with a predicate bit set.
405
406 In either case the result is in the element with the first bit set in
407 the predicate mask. Thus, no move/copy *within the Reduction itself* was needed.
408
409 Programmer's Note: For *some* hardware implementations
410 the vector-to-scalar copy may be a slow operation, as may the Predicated
411 Parallel Reduction itself.
412 It may be better to perform a pre-copy
413 of the values, compressing them (VREDUCE-style) into a contiguous block,
414 which will guarantee that the result goes into the very first element
415 of the destination vector, in which case clearly no follow-up
416 predicated vector-to-scalar MV operation is needed. A VREDUCE effect
417 is achieved by setting just a source predicate mask on Twin-Predicated
418 operations.
419
420 **Usage conditions**
421
422 The simplest usage is to perform an overwrite, specifying all three
423 register operands the same.
424
425 ```
426 svshape parallelreduce, 6
427 sv.add *8, *8, *8
428 ```
429
430 The Reduction Schedule will issue the Parallel Tree Reduction spanning
431 registers 8 through 13, by adjusting the offsets to RT, RA and RB as
432 necessary (see "Parallel Reduction algorithm" in a later section).
433
434 A non-overwrite is possible as well but just as with the overwrite
435 version, only those destination elements necessary for storing
436 intermediary computations will be written to: the remaining elements
437 will **not** be overwritten and will **not** be zero'd.
438
439 ```
440 svshape parallelreduce, 6
441 sv.add *0, *8, *8
442 ```
443
444 However it is critical to note that if the source and destination are
445 not the same then the trick of using a follow-up vector-scalar MV will
446 not work.
447
448 ### Sub-Vector Horizontal Reduction
449
450 To achieve Sub-Vector Horizontal Reduction, Pack/Unpack should be enabled,
451 which will turn the Schedule around such that issuing of the Scalar
452 Defined Words is done with SUBVL looping as the inner loop not the
453 outer loop. Rc=1 with Sub-Vectors (SUBVL=2,3,4) is `UNDEFINED` behaviour.
454
455 *Programmer's Note: Overwrite Parallel Reduction with Sub-Vectors
456 will clearly result in data corruption. It may be best to perform
457 a Pack/Unpack Transposing copy of the data first*
458
459 ## Determining Register Hazards
460
461 For high-performance (Multi-Issue, Out-of-Order) systems it is critical
462 to be able to statically determine the extent of Vectors in order to
463 allocate pre-emptive Hazard protection. The next task is to eliminate
464 masked-out elements using predicate bits, freeing up the associated
465 Hazards.
466
467 For non-REMAP situations `VL` is sufficient to ascertain early
468 Hazard coverage, and with SVSTATE being a high priority cached
469 quantity at the same level of MSR and PC this is not a problem.
470
471 The problems come when REMAP is enabled. Indexed REMAP must instead
472 use `MAXVL` as the earliest (simplest)
473 batch-level Hazard Reservation indicator (after taking element-width
474 overriding on the Index source into consideration),
475 but Matrix, FFT and Parallel Reduction must all use completely different
476 schemes. The reason is that VL is used to step through the total
477 number of *operations*, not the number of registers.
478 The "Saving Grace" is that all of the REMAP Schedules are 100% Deterministic.
479
480 Advance-notice Parallel computation and subsequent cacheing
481 of all of these complex Deterministic REMAP Schedules is
482 *strongly recommended*, thus allowing clear and precise multi-issue
483 batched Hazard coverage to be deployed, *even for Indexed Mode*.
484 This is only possible for Indexed due to the strict guidelines
485 given to Programmers.
486
487 In short, there exists solutions to the problem of Hazard Management,
488 with varying degrees of refinement possible at correspondingly
489 increasing levels of complexity in hardware.
490
491 A reminder: when Rc=1 each result register (element) has an associated
492 co-result CR Field (one per result element). Thus above when determining
493 the Write-Hazards for result registers the corresponding Write-Hazards for the
494 corresponding associated co-result CR Field must not be forgotten, *including* when
495 Predication is used.
496
497 ## REMAP area of SVSTATE SPR
498
499 The following bits of the SVSTATE SPR are used for REMAP:
500
501 ```
502 |32:33|34:35|36:37|38:39|40:41| 42:46 | 62 |
503 | -- | -- | -- | -- | -- | ----- | ------ |
504 |mi0 |mi1 |mi2 |mo0 |mo1 | SVme | RMpst |
505 ```
506
507 mi0-2 and mo0-1 each select SVSHAPE0-3 to apply to a given register.
508 mi0-2 apply to RA, RB, RC respectively, as input registers, and
509 likewise mo0-1 apply to output registers (RT/FRT, RS/FRS) respectively.
510 SVme is 5 bits (one for each of mi0-2/mo0-1) and indicates whether the
511 SVSHAPE is actively applied or not.
512
513 * bit 0 of SVme indicates if mi0 is applied to RA / FRA / BA / BFA
514 * bit 1 of SVme indicates if mi1 is applied to RB / FRB / BB
515 * bit 2 of SVme indicates if mi2 is applied to RC / FRC / BC
516 * bit 3 of SVme indicates if mo0 is applied to RT / FRT / BT / BF
517 * bit 4 of SVme indicates if mo1 is applied to Effective Address / FRS / RS
518 (LD/ST-with-update has an implicit 2nd write register, RA)
519
520 The "persistence" bit if set will result in all Active REMAPs being applied
521 indefinitely.
522
523 -----------
524
525 \newpage{}
526
527 # svremap instruction <a name="svremap"> </a>
528
529 SVRM-Form:
530
531 |0 |6 |11 |13 |15 |17 |19 |21 | 22:25 |26:31 |
532 | -- | -- | -- | -- | -- | -- | -- | -- | ---- | ----- |
533 | PO | SVme |mi0 | mi1 | mi2 | mo0 | mo1 | pst | rsvd | XO |
534
535 * svremap SVme,mi0,mi1,mi2,mo0,mo1,pst
536
537 Pseudo-code:
538
539 ```
540 # registers RA RB RC RT EA/FRS SVSHAPE0-3 indices
541 SVSTATE[32:33] <- mi0
542 SVSTATE[34:35] <- mi1
543 SVSTATE[36:37] <- mi2
544 SVSTATE[38:39] <- mo0
545 SVSTATE[40:41] <- mo1
546 # enable bit for RA RB RC RT EA/FRS
547 SVSTATE[42:46] <- SVme
548 # persistence bit (applies to more than one instruction)
549 SVSTATE[62] <- pst
550 ```
551
552 Special Registers Altered:
553
554 ```
555 SVSTATE
556 ```
557
558 `svremap` determines the relationship between registers and SVSHAPE SPRs.
559 The bitmask `SVme` determines which registers have a REMAP applied, and mi0-mo1
560 determine which shape is applied to an activated register. the `pst` bit if
561 cleared indicated that the REMAP operation shall only apply to the immediately-following
562 instruction. If set then REMAP remains permanently enabled until such time as it is
563 explicitly disabled, either by `setvl` setting a new MAXVL, or with another
564 `svremap` instruction. `svindex` and `svshape2` are also capable of setting or
565 clearing persistence, as well as partially covering a subset of the capability of
566 `svremap` to set register-to-SVSHAPE relationships.
567
568 Programmer's Note: applying non-persistent `svremap` to an instruction that has
569 no REMAP enabled or is a Scalar operation will obviously have no effect but
570 the bits 32 to 46 will at least have been set in SVSTATE. This may prove useful
571 when using `svindex` or `svshape2`.
572
573 Hardware Architectural Note: when persistence is not set it is critically important
574 to treat the `svremap` and the following SVP64 instruction as an indivisible fused operation.
575 *No state* is stored in the SVSTATE SPR in order to allow continuation should an
576 Interrupt occur between the two instructions. Thus, Interrupts must be prohibited
577 from occurring or other workaround deployed. When persistence is set this issue
578 is moot.
579
580 It is critical to note that if persistence is clear then `svremap` is the *only* way
581 to activate REMAP on any given (following) instruction. If persistence is set however then
582 **all** SVP64 instructions go through REMAP as long as `SVme` is non-zero.
583
584 -------------
585
586 \newpage{}
587
588 # SHAPE Remapping SPRs
589
590 There are four "shape" SPRs, SHAPE0-3, 32-bits in each,
591 which have the same format.
592
593 Shape is 32-bits. When SHAPE is set entirely to zeros, remapping is
594 disabled: the register's elements are a linear (1D) vector.
595
596 |0:5 |6:11 | 12:17 | 18:20 | 21:23 |24:27 |28:29 |30:31| Mode |
597 |----- |----- | ------- | ------- | ------ |------|------ |---- | ----- |
598 |xdimsz|ydimsz| zdimsz | permute | invxyz |offset|skip |mode |Matrix |
599 |xdimsz|ydimsz|SVGPR | 11/ |sk1/invxy|offset|elwidth|0b00 |Indexed|
600 |xdimsz|mode | zdimsz | submode2| invxyz |offset|submode|0b01 |DCT/FFT|
601 | rsvd |rsvd |xdimsz | rsvd | invxyz |offset|submode|0b10 |Preduce|
602 | | | | | | | |0b11 |rsvd |
603
604 `mode` sets different behaviours (straight matrix multiply, FFT, DCT).
605
606 * **mode=0b00** sets straight Matrix Mode
607 * **mode=0b00** with permute=0b110 or 0b111 sets Indexed Mode
608 * **mode=0b01** sets "FFT/DCT" mode and activates submodes
609 * **mode=0b10** sets "Parallel Reduction" Schedules.
610
611 *Architectural Resource Allocation note: the four SVSHAPE SPRs are best
612 allocated sequentially and contiguously in order that `sv.mtspr` may
613 be used*
614
615 ## Parallel Reduction Mode
616
617 Creates the Schedules for Parallel Tree Reduction.
618
619 * **submode=0b00** selects the left operand index
620 * **submode=0b01** selects the right operand index
621
622 * When bit 0 of `invxyz` is set, the order of the indices
623 in the inner for-loop are reversed. This has the side-effect
624 of placing the final reduced result in the last-predicated element.
625 It also has the indirect side-effect of swapping the source
626 registers: Left-operand index numbers will always exceed
627 Right-operand indices.
628 When clear, the reduced result will be in the first-predicated
629 element, and Left-operand indices will always be *less* than
630 Right-operand ones.
631 * When bit 1 of `invxyz` is set, the order of the outer loop
632 step is inverted: stepping begins at the nearest power-of two
633 to half of the vector length and reduces by half each time.
634 When clear the step will begin at 2 and double on each
635 inner loop.
636
637 ## FFT/DCT mode
638
639 submode2=0 is for FFT. For FFT submode the following schedules may be
640 selected:
641
642 * **submode=0b00** selects the ``j`` offset of the innermost for-loop
643 of Tukey-Cooley
644 * **submode=0b10** selects the ``j+halfsize`` offset of the innermost for-loop
645 of Tukey-Cooley
646 * **submode=0b11** selects the ``k`` of exptable (which coefficient)
647
648 When submode2 is 1 or 2, for DCT inner butterfly submode the following
649 schedules may be selected. When submode2 is 1, additional bit-reversing
650 is also performed.
651
652 * **submode=0b00** selects the ``j`` offset of the innermost for-loop,
653 in-place
654 * **submode=0b010** selects the ``j+halfsize`` offset of the innermost for-loop,
655 in reverse-order, in-place
656 * **submode=0b10** selects the ``ci`` count of the innermost for-loop,
657 useful for calculating the cosine coefficient
658 * **submode=0b11** selects the ``size`` offset of the outermost for-loop,
659 useful for the cosine coefficient ``cos(ci + 0.5) * pi / size``
660
661 When submode2 is 3 or 4, for DCT outer butterfly submode the following
662 schedules may be selected. When submode is 3, additional bit-reversing
663 is also performed.
664
665 * **submode=0b00** selects the ``j`` offset of the innermost for-loop,
666 * **submode=0b01** selects the ``j+1`` offset of the innermost for-loop,
667
668 `zdimsz` is used as an in-place "Stride", particularly useful for
669 column-based in-place DCT/FFT.
670
671 ## Matrix Mode
672
673 In Matrix Mode, skip allows dimensions to be skipped from being included
674 in the resultant output index. this allows sequences to be repeated:
675 ```0 0 0 1 1 1 2 2 2 ...``` or in the case of skip=0b11 this results in
676 modulo ```0 1 2 0 1 2 ...```
677
678 * **skip=0b00** indicates no dimensions to be skipped
679 * **skip=0b01** sets "skip 1st dimension"
680 * **skip=0b10** sets "skip 2nd dimension"
681 * **skip=0b11** sets "skip 3rd dimension"
682
683 invxyz will invert the start index of each of x, y or z. If invxyz[0] is
684 zero then x-dimensional counting begins from 0 and increments, otherwise
685 it begins from xdimsz-1 and iterates down to zero. Likewise for y and z.
686
687 offset will have the effect of offsetting the result by ```offset``` elements:
688
689 ```
690 for i in 0..VL-1:
691 GPR(RT + remap(i) + SVSHAPE.offset) = ....
692 ```
693
694 this appears redundant because the register RT could simply be changed by a compiler, until element width overrides are introduced. also
695 bear in mind that unlike a static compiler SVSHAPE.offset may
696 be set dynamically at runtime.
697
698 xdimsz, ydimsz and zdimsz are offset by 1, such that a value of 0 indicates
699 that the array dimensionality for that dimension is 1. any dimension
700 not intended to be used must have its value set to 0 (dimensionality
701 of 1). A value of xdimsz=2 would indicate that in the first dimension
702 there are 3 elements in the array. For example, to create a 2D array
703 X,Y of dimensionality X=3 and Y=2, set xdimsz=2, ydimsz=1 and zdimsz=0
704
705 The format of the array is therefore as follows:
706
707 ```
708 array[xdimsz+1][ydimsz+1][zdimsz+1]
709 ```
710
711 However whilst illustrative of the dimensionality, that does not take the
712 "permute" setting into account. "permute" may be any one of six values
713 (0-5, with values of 6 and 7 indicating "Indexed" Mode). The table
714 below shows how the permutation dimensionality order works:
715
716 | permute | order | array format |
717 | ------- | ----- | ------------------------ |
718 | 000 | 0,1,2 | (xdim+1)(ydim+1)(zdim+1) |
719 | 001 | 0,2,1 | (xdim+1)(zdim+1)(ydim+1) |
720 | 010 | 1,0,2 | (ydim+1)(xdim+1)(zdim+1) |
721 | 011 | 1,2,0 | (ydim+1)(zdim+1)(xdim+1) |
722 | 100 | 2,0,1 | (zdim+1)(xdim+1)(ydim+1) |
723 | 101 | 2,1,0 | (zdim+1)(ydim+1)(xdim+1) |
724 | 110 | 0,1 | Indexed (xdim+1)(ydim+1) |
725 | 111 | 1,0 | Indexed (ydim+1)(xdim+1) |
726
727 In other words, the "permute" option changes the order in which
728 nested for-loops over the array would be done. See executable
729 python reference code for further details.
730
731 *Note: permute=0b110 and permute=0b111 enable Indexed REMAP Mode,
732 described below*
733
734 With all these options it is possible to support in-place transpose,
735 in-place rotate, Matrix Multiply and Convolutions, without being
736 limited to Power-of-Two dimension sizes.
737
738 ## Indexed Mode
739
740 Indexed Mode activates reading of the element indices from the GPR
741 and includes optional limited 2D reordering.
742 In its simplest form (without elwidth overrides or other modes):
743
744 ```
745 def index_remap(i):
746 return GPR((SVSHAPE.SVGPR<<1)+i) + SVSHAPE.offset
747
748 for i in 0..VL-1:
749 element_result = ....
750 GPR(RT + indexed_remap(i)) = element_result
751 ```
752
753 With element-width overrides included, and using the pseudocode
754 from the SVP64 [[sv/svp64/appendix#elwidth]] elwidth section
755 this becomes:
756
757 ```
758 def index_remap(i):
759 svreg = SVSHAPE.SVGPR << 1
760 srcwid = elwid_to_bitwidth(SVSHAPE.elwid)
761 offs = SVSHAPE.offset
762 return get_polymorphed_reg(svreg, srcwid, i) + offs
763
764 for i in 0..VL-1:
765 element_result = ....
766 rt_idx = indexed_remap(i)
767 set_polymorphed_reg(RT, destwid, rt_idx, element_result)
768 ```
769
770 Matrix-style reordering still applies to the indices, except limited
771 to up to 2 Dimensions (X,Y). Ordering is therefore limited to (X,Y) or
772 (Y,X) for in-place Transposition.
773 Only one dimension may optionally be skipped. Inversion of either
774 X or Y or both is possible (2D mirroring). Pseudocode for Indexed Mode (including elwidth
775 overrides) may be written in terms of Matrix Mode, specifically
776 purposed to ensure that the 3rd dimension (Z) has no effect:
777
778 ```
779 def index_remap(ISHAPE, i):
780 MSHAPE.skip = 0b0 || ISHAPE.sk1
781 MSHAPE.invxyz = 0b0 || ISHAPE.invxy
782 MSHAPE.xdimsz = ISHAPE.xdimsz
783 MSHAPE.ydimsz = ISHAPE.ydimsz
784 MSHAPE.zdimsz = 0 # disabled
785 if ISHAPE.permute = 0b110 # 0,1
786 MSHAPE.permute = 0b000 # 0,1,2
787 if ISHAPE.permute = 0b111 # 1,0
788 MSHAPE.permute = 0b010 # 1,0,2
789 el_idx = remap_matrix(MSHAPE, i)
790 svreg = ISHAPE.SVGPR << 1
791 srcwid = elwid_to_bitwidth(ISHAPE.elwid)
792 offs = ISHAPE.offset
793 return get_polymorphed_reg(svreg, srcwid, el_idx) + offs
794 ```
795
796 The most important observation above is that the Matrix-style
797 remapping occurs first and the Index lookup second. Thus it
798 becomes possible to perform in-place Transpose of Indices which
799 may have been costly to set up or costly to duplicate
800 (waste register file space).
801
802 -------------
803
804 \newpage{}
805
806 # svshape instruction <a name="svshape"> </a>
807
808 SVM-Form
809
810 svshape SVxd,SVyd,SVzd,SVRM,vf
811
812 | 0:5|6:10 |11:15 |16:20 | 21:24 | 25 | 26:31 | name |
813 | -- | -- | --- | ----- | ------ | -- | ------| -------- |
814 |PO | SVxd | SVyd | SVzd | SVRM | vf | XO | svshape |
815
816 ```
817 # for convenience, VL to be calculated and stored in SVSTATE
818 vlen <- [0] * 7
819 mscale[0:5] <- 0b000001 # for scaling MAXVL
820 itercount[0:6] <- [0] * 7
821 SVSTATE[0:31] <- [0] * 32
822 # only overwrite REMAP if "persistence" is zero
823 if (SVSTATE[62] = 0b0) then
824 SVSTATE[32:33] <- 0b00
825 SVSTATE[34:35] <- 0b00
826 SVSTATE[36:37] <- 0b00
827 SVSTATE[38:39] <- 0b00
828 SVSTATE[40:41] <- 0b00
829 SVSTATE[42:46] <- 0b00000
830 SVSTATE[62] <- 0b0
831 SVSTATE[63] <- 0b0
832 # clear out all SVSHAPEs
833 SVSHAPE0[0:31] <- [0] * 32
834 SVSHAPE1[0:31] <- [0] * 32
835 SVSHAPE2[0:31] <- [0] * 32
836 SVSHAPE3[0:31] <- [0] * 32
837
838 # set schedule up for multiply
839 if (SVrm = 0b0000) then
840 # VL in Matrix Multiply is xd*yd*zd
841 xd <- (0b00 || SVxd) + 1
842 yd <- (0b00 || SVyd) + 1
843 zd <- (0b00 || SVzd) + 1
844 n <- xd * yd * zd
845 vlen[0:6] <- n[14:20]
846 # set up template in SVSHAPE0, then copy to 1-3
847 SVSHAPE0[0:5] <- (0b0 || SVxd) # xdim
848 SVSHAPE0[6:11] <- (0b0 || SVyd) # ydim
849 SVSHAPE0[12:17] <- (0b0 || SVzd) # zdim
850 SVSHAPE0[28:29] <- 0b11 # skip z
851 # copy
852 SVSHAPE1[0:31] <- SVSHAPE0[0:31]
853 SVSHAPE2[0:31] <- SVSHAPE0[0:31]
854 SVSHAPE3[0:31] <- SVSHAPE0[0:31]
855 # set up FRA
856 SVSHAPE1[18:20] <- 0b001 # permute x,z,y
857 SVSHAPE1[28:29] <- 0b01 # skip z
858 # FRC
859 SVSHAPE2[18:20] <- 0b001 # permute x,z,y
860 SVSHAPE2[28:29] <- 0b11 # skip y
861
862 # set schedule up for FFT butterfly
863 if (SVrm = 0b0001) then
864 # calculate O(N log2 N)
865 n <- [0] * 3
866 do while n < 5
867 if SVxd[4-n] = 0 then
868 leave
869 n <- n + 1
870 n <- ((0b0 || SVxd) + 1) * n
871 vlen[0:6] <- n[1:7]
872 # set up template in SVSHAPE0, then copy to 1-3
873 # for FRA and FRT
874 SVSHAPE0[0:5] <- (0b0 || SVxd) # xdim
875 SVSHAPE0[12:17] <- (0b0 || SVzd) # zdim - "striding" (2D FFT)
876 mscale <- (0b0 || SVzd) + 1
877 SVSHAPE0[30:31] <- 0b01 # Butterfly mode
878 # copy
879 SVSHAPE1[0:31] <- SVSHAPE0[0:31]
880 SVSHAPE2[0:31] <- SVSHAPE0[0:31]
881 # set up FRB and FRS
882 SVSHAPE1[28:29] <- 0b01 # j+halfstep schedule
883 # FRC (coefficients)
884 SVSHAPE2[28:29] <- 0b10 # k schedule
885
886 # set schedule up for (i)DCT Inner butterfly
887 # SVrm Mode 4 (Mode 12 for iDCT) is for on-the-fly (Vertical-First Mode)
888 if ((SVrm = 0b0100) |
889 (SVrm = 0b1100)) then
890 # calculate O(N log2 N)
891 n <- [0] * 3
892 do while n < 5
893 if SVxd[4-n] = 0 then
894 leave
895 n <- n + 1
896 n <- ((0b0 || SVxd) + 1) * n
897 vlen[0:6] <- n[1:7]
898 # set up template in SVSHAPE0, then copy to 1-3
899 # set up FRB and FRS
900 SVSHAPE0[0:5] <- (0b0 || SVxd) # xdim
901 SVSHAPE0[12:17] <- (0b0 || SVzd) # zdim - "striding" (2D DCT)
902 mscale <- (0b0 || SVzd) + 1
903 if (SVrm = 0b1100) then
904 SVSHAPE0[30:31] <- 0b11 # iDCT mode
905 SVSHAPE0[18:20] <- 0b011 # iDCT Inner Butterfly sub-mode
906 else
907 SVSHAPE0[30:31] <- 0b01 # DCT mode
908 SVSHAPE0[18:20] <- 0b001 # DCT Inner Butterfly sub-mode
909 SVSHAPE0[21:23] <- 0b001 # "inverse" on outer loop
910 SVSHAPE0[6:11] <- 0b000011 # (i)DCT Inner Butterfly mode 4
911 # copy
912 SVSHAPE1[0:31] <- SVSHAPE0[0:31]
913 SVSHAPE2[0:31] <- SVSHAPE0[0:31]
914 if (SVrm != 0b0100) & (SVrm != 0b1100) then
915 SVSHAPE3[0:31] <- SVSHAPE0[0:31]
916 # for FRA and FRT
917 SVSHAPE0[28:29] <- 0b01 # j+halfstep schedule
918 # for cos coefficient
919 SVSHAPE2[28:29] <- 0b10 # ci (k for mode 4) schedule
920 SVSHAPE2[12:17] <- 0b000000 # reset costable "striding" to 1
921 if (SVrm != 0b0100) & (SVrm != 0b1100) then
922 SVSHAPE3[28:29] <- 0b11 # size schedule
923
924 # set schedule up for (i)DCT Outer butterfly
925 if (SVrm = 0b0011) | (SVrm = 0b1011) then
926 # calculate O(N log2 N) number of outer butterfly overlapping adds
927 vlen[0:6] <- [0] * 7
928 n <- 0b000
929 size <- 0b0000001
930 itercount[0:6] <- (0b00 || SVxd) + 0b0000001
931 itercount[0:6] <- (0b0 || itercount[0:5])
932 do while n < 5
933 if SVxd[4-n] = 0 then
934 leave
935 n <- n + 1
936 count <- (itercount - 0b0000001) * size
937 vlen[0:6] <- vlen + count[7:13]
938 size[0:6] <- (size[1:6] || 0b0)
939 itercount[0:6] <- (0b0 || itercount[0:5])
940 # set up template in SVSHAPE0, then copy to 1-3
941 # set up FRB and FRS
942 SVSHAPE0[0:5] <- (0b0 || SVxd) # xdim
943 SVSHAPE0[12:17] <- (0b0 || SVzd) # zdim - "striding" (2D DCT)
944 mscale <- (0b0 || SVzd) + 1
945 if (SVrm = 0b1011) then
946 SVSHAPE0[30:31] <- 0b11 # iDCT mode
947 SVSHAPE0[18:20] <- 0b011 # iDCT Outer Butterfly sub-mode
948 SVSHAPE0[21:23] <- 0b101 # "inverse" on outer and inner loop
949 else
950 SVSHAPE0[30:31] <- 0b01 # DCT mode
951 SVSHAPE0[18:20] <- 0b100 # DCT Outer Butterfly sub-mode
952 SVSHAPE0[6:11] <- 0b000010 # DCT Butterfly mode
953 # copy
954 SVSHAPE1[0:31] <- SVSHAPE0[0:31] # j+halfstep schedule
955 SVSHAPE2[0:31] <- SVSHAPE0[0:31] # costable coefficients
956 # for FRA and FRT
957 SVSHAPE1[28:29] <- 0b01 # j+halfstep schedule
958 # reset costable "striding" to 1
959 SVSHAPE2[12:17] <- 0b000000
960
961 # set schedule up for DCT COS table generation
962 if (SVrm = 0b0101) | (SVrm = 0b1101) then
963 # calculate O(N log2 N)
964 vlen[0:6] <- [0] * 7
965 itercount[0:6] <- (0b00 || SVxd) + 0b0000001
966 itercount[0:6] <- (0b0 || itercount[0:5])
967 n <- [0] * 3
968 do while n < 5
969 if SVxd[4-n] = 0 then
970 leave
971 n <- n + 1
972 vlen[0:6] <- vlen + itercount
973 itercount[0:6] <- (0b0 || itercount[0:5])
974 # set up template in SVSHAPE0, then copy to 1-3
975 # set up FRB and FRS
976 SVSHAPE0[0:5] <- (0b0 || SVxd) # xdim
977 SVSHAPE0[12:17] <- (0b0 || SVzd) # zdim - "striding" (2D DCT)
978 mscale <- (0b0 || SVzd) + 1
979 SVSHAPE0[30:31] <- 0b01 # DCT/FFT mode
980 SVSHAPE0[6:11] <- 0b000100 # DCT Inner Butterfly COS-gen mode
981 if (SVrm = 0b0101) then
982 SVSHAPE0[21:23] <- 0b001 # "inverse" on outer loop for DCT
983 # copy
984 SVSHAPE1[0:31] <- SVSHAPE0[0:31]
985 SVSHAPE2[0:31] <- SVSHAPE0[0:31]
986 # for cos coefficient
987 SVSHAPE1[28:29] <- 0b10 # ci schedule
988 SVSHAPE2[28:29] <- 0b11 # size schedule
989
990 # set schedule up for iDCT / DCT inverse of half-swapped ordering
991 if (SVrm = 0b0110) | (SVrm = 0b1110) | (SVrm = 0b1111) then
992 vlen[0:6] <- (0b00 || SVxd) + 0b0000001
993 # set up template in SVSHAPE0
994 SVSHAPE0[0:5] <- (0b0 || SVxd) # xdim
995 SVSHAPE0[12:17] <- (0b0 || SVzd) # zdim - "striding" (2D DCT)
996 mscale <- (0b0 || SVzd) + 1
997 if (SVrm = 0b1110) then
998 SVSHAPE0[18:20] <- 0b001 # DCT opposite half-swap
999 if (SVrm = 0b1111) then
1000 SVSHAPE0[30:31] <- 0b01 # FFT mode
1001 else
1002 SVSHAPE0[30:31] <- 0b11 # DCT mode
1003 SVSHAPE0[6:11] <- 0b000101 # DCT "half-swap" mode
1004
1005 # set schedule up for parallel reduction
1006 if (SVrm = 0b0111) then
1007 # calculate the total number of operations (brute-force)
1008 vlen[0:6] <- [0] * 7
1009 itercount[0:6] <- (0b00 || SVxd) + 0b0000001
1010 step[0:6] <- 0b0000001
1011 i[0:6] <- 0b0000000
1012 do while step <u itercount
1013 newstep <- step[1:6] || 0b0
1014 j[0:6] <- 0b0000000
1015 do while (j+step <u itercount)
1016 j <- j + newstep
1017 i <- i + 1
1018 step <- newstep
1019 # VL in Parallel-Reduce is the number of operations
1020 vlen[0:6] <- i
1021 # set up template in SVSHAPE0, then copy to 1. only 2 needed
1022 SVSHAPE0[0:5] <- (0b0 || SVxd) # xdim
1023 SVSHAPE0[12:17] <- (0b0 || SVzd) # zdim - "striding" (2D DCT)
1024 mscale <- (0b0 || SVzd) + 1
1025 SVSHAPE0[30:31] <- 0b10 # parallel reduce submode
1026 # copy
1027 SVSHAPE1[0:31] <- SVSHAPE0[0:31]
1028 # set up right operand (left operand 28:29 is zero)
1029 SVSHAPE1[28:29] <- 0b01 # right operand
1030
1031 # set VL, MVL and Vertical-First
1032 m[0:12] <- vlen * mscale
1033 maxvl[0:6] <- m[6:12]
1034 SVSTATE[0:6] <- maxvl # MAVXL
1035 SVSTATE[7:13] <- vlen # VL
1036 SVSTATE[63] <- vf
1037 ```
1038
1039 Special Registers Altered:
1040
1041 ```
1042 SVSTATE, SVSHAPE0-3
1043 ```
1044
1045 `svshape` is a convenience instruction that reduces instruction
1046 count for common usage patterns, particularly Matrix, DCT and FFT. It sets up
1047 (overwrites) all required SVSHAPE SPRs and also modifies SVSTATE
1048 including VL and MAXVL. Using `svshape` therefore does not also
1049 require `setvl`.
1050
1051 Fields:
1052
1053 * **SVxd** - SV REMAP "xdim"
1054 * **SVyd** - SV REMAP "ydim"
1055 * **SVzd** - SV REMAP "zdim"
1056 * **SVRM** - SV REMAP Mode (0b00000 for Matrix, 0b00001 for FFT etc.)
1057 * **vf** - sets "Vertical-First" mode
1058 * **XO** - standard 6-bit XO field
1059
1060 *Note: SVxd, SVyz and SVzd are all stored "off-by-one". In the assembler
1061 mnemonic the values `1-32` are stored in binary as `0b00000..0b11111`*
1062
1063 There are 12 REMAP Modes (2 Modes are RESERVED for `svshape2`, 2 Modes
1064 are RESERVED)
1065
1066 | SVRM | Remap Mode description |
1067 | -- | -- |
1068 | 0b0000 | Matrix 1/2/3D |
1069 | 0b0001 | FFT Butterfly |
1070 | 0b0010 | reserved |
1071 | 0b0011 | DCT Outer butterfly |
1072 | 0b0100 | DCT Inner butterfly, on-the-fly (Vertical-First Mode) |
1073 | 0b0101 | DCT COS table index generation |
1074 | 0b0110 | DCT half-swap |
1075 | 0b0111 | Parallel Reduction |
1076 | 0b1000 | reserved for svshape2 |
1077 | 0b1001 | reserved for svshape2 |
1078 | 0b1010 | reserved |
1079 | 0b1011 | iDCT Outer butterfly |
1080 | 0b1100 | iDCT Inner butterfly, on-the-fly (Vertical-First Mode) |
1081 | 0b1101 | iDCT COS table index generation |
1082 | 0b1110 | iDCT half-swap |
1083 | 0b1111 | FFT half-swap |
1084
1085 Examples showing how all of these Modes operate exists in the online
1086 [SVP64 unit tests](https://git.libre-soc.org/?p=openpower-isa.git;a=tree;f=src/openpower/decoder/isa;hb=HEAD). Explaining
1087 these Modes further in detail is beyond the scope of this document.
1088
1089 In Indexed Mode, there are only 5 bits available to specify the GPR
1090 to use, out of 128 GPRs (7 bit numbering). Therefore, only the top
1091 5 bits are given in the `SVxd` field: the bottom two implicit bits
1092 will be zero (`SVxd || 0b00`).
1093
1094 `svshape` has *limited applicability* due to being a 32-bit instruction.
1095 The full capability of SVSHAPE SPRs may be accessed by directly writing
1096 to SVSHAPE0-3 with `mtspr`. Circumstances include Matrices with dimensions
1097 larger than 32, and in-place Transpose. Potentially a future v3.1 Prefixed
1098 instruction, `psvshape`, may extend the capability here.
1099
1100 *Architectural Resource Allocation note: the SVRM field is carefully
1101 crafted to allocate two Modes, corresponding to bits 21-23 within the
1102 instruction being set to the value `0b100`, to `svshape2` (not
1103 `svshape`). These two Modes are
1104 considered "RESERVED" within the context of `svshape` but it is
1105 absolutely critical to allocate the exact same pattern in XO for
1106 both instructions in bits 26-31.*
1107
1108 -------------
1109
1110 \newpage{}
1111
1112
1113 # svindex instruction <a name="svindex"> </a>
1114
1115 SVI-Form
1116
1117 | 0:5|6:10 |11:15 |16:20 | 21:25 | 26:31 | Form |
1118 | -- | -- | --- | ---- | ----------- | ------| -------- |
1119 | PO | SVG | rmm | SVd | ew/yx/mm/sk | XO | SVI-Form |
1120
1121 * svindex SVG,rmm,SVd,ew,SVyx,mm,sk
1122
1123 Pseudo-code:
1124
1125 ```
1126 # based on nearest MAXVL compute other dimension
1127 MVL <- SVSTATE[0:6]
1128 d <- [0] * 6
1129 dim <- SVd+1
1130 do while d*dim <u ([0]*4 || MVL)
1131 d <- d + 1
1132
1133 # set up template, then copy once location identified
1134 shape <- [0]*32
1135 shape[30:31] <- 0b00 # mode
1136 if SVyx = 0 then
1137 shape[18:20] <- 0b110 # indexed xd/yd
1138 shape[0:5] <- (0b0 || SVd) # xdim
1139 if sk = 0 then shape[6:11] <- 0 # ydim
1140 else shape[6:11] <- 0b111111 # ydim max
1141 else
1142 shape[18:20] <- 0b111 # indexed yd/xd
1143 if sk = 1 then shape[6:11] <- 0 # ydim
1144 else shape[6:11] <- d-1 # ydim max
1145 shape[0:5] <- (0b0 || SVd) # ydim
1146 shape[12:17] <- (0b0 || SVG) # SVGPR
1147 shape[28:29] <- ew # element-width override
1148 shape[21] <- sk # skip 1st dimension
1149
1150 # select the mode for updating SVSHAPEs
1151 SVSTATE[62] <- mm # set or clear persistence
1152 if mm = 0 then
1153 # clear out all SVSHAPEs first
1154 SVSHAPE0[0:31] <- [0] * 32
1155 SVSHAPE1[0:31] <- [0] * 32
1156 SVSHAPE2[0:31] <- [0] * 32
1157 SVSHAPE3[0:31] <- [0] * 32
1158 SVSTATE[32:41] <- [0] * 10 # clear REMAP.mi/o
1159 SVSTATE[42:46] <- rmm # rmm exactly REMAP.SVme
1160 idx <- 0
1161 for bit = 0 to 4
1162 if rmm[4-bit] then
1163 # activate requested shape
1164 if idx = 0 then SVSHAPE0 <- shape
1165 if idx = 1 then SVSHAPE1 <- shape
1166 if idx = 2 then SVSHAPE2 <- shape
1167 if idx = 3 then SVSHAPE3 <- shape
1168 SVSTATE[bit*2+32:bit*2+33] <- idx
1169 # increment shape index, modulo 4
1170 if idx = 3 then idx <- 0
1171 else idx <- idx + 1
1172 else
1173 # refined SVSHAPE/REMAP update mode
1174 bit <- rmm[0:2]
1175 idx <- rmm[3:4]
1176 if idx = 0 then SVSHAPE0 <- shape
1177 if idx = 1 then SVSHAPE1 <- shape
1178 if idx = 2 then SVSHAPE2 <- shape
1179 if idx = 3 then SVSHAPE3 <- shape
1180 SVSTATE[bit*2+32:bit*2+33] <- idx
1181 SVSTATE[46-bit] <- 1
1182 ```
1183
1184 Special Registers Altered:
1185
1186 ```
1187 SVSTATE, SVSHAPE0-3
1188 ```
1189
1190 `svindex` is a convenience instruction that reduces instruction count
1191 for Indexed REMAP Mode. It sets up (overwrites) all required SVSHAPE
1192 SPRs and **unlike** `svshape` can modify the REMAP area of the SVSTATE
1193 SPR as well, including setting persistence. The relevant SPRs *may*
1194 be directly programmed with `mtspr` however it is laborious to do so:
1195 svindex saves instructions covering much of Indexed REMAP capability.
1196
1197 Fields:
1198
1199 * **SVd** - SV REMAP x/y dim
1200 * **rmm** - REMAP mask: sets remap mi0-2/mo0-1 and SVSHAPEs,
1201 controlled by mm
1202 * **ew** - sets element width override on the Indices
1203 * **SVG** - GPR SVG<<2 to be used for Indexing
1204 * **yx** - 2D reordering to be used if yx=1
1205 * **mm** - mask mode. determines how `rmm` is interpreted.
1206 * **sk** - Dimension skipping enabled
1207
1208 *Note: SVd, like SVxd, SVyz and SVzd of `svshape`, are all stored
1209 "off-by-one". In the assembler
1210 mnemonic the values `1-32` are stored in binary as `0b00000..0b11111`*.
1211
1212 *Note: when `yx=1,sk=0` the second dimension is calculated as
1213 `CEIL(MAXVL/SVd)`*.
1214
1215 When `mm=0`:
1216
1217 * `rmm`, like REMAP.SVme, has bit 0
1218 correspond to mi0, bit 1 to mi1, bit 2 to mi2,
1219 bit 3 to mo0 and bit 4 to mi1
1220 * all SVSHAPEs and the REMAP parts of SVSHAPE are first reset (initialised to zero)
1221 * for each bit set in the 5-bit `rmm`, in order, the first
1222 as-yet-unset SVSHAPE will be updated
1223 with the other operands in the instruction, and the REMAP
1224 SPR set.
1225 * If all 5 bits of `rmm` are set then both mi0 and mo1 use SVSHAPE0.
1226 * SVSTATE persistence bit is cleared
1227 * No other alterations to SVSTATE are carried out
1228
1229 Example 1: if rmm=0b00110 then SVSHAPE0 and SVSHAPE1 are set up,
1230 and the REMAP SPR set so that mi1 uses SVSHAPE0 and mi2
1231 uses mi2. REMAP.SVme is also set to 0b00110, REMAP.mi1=0
1232 (SVSHAPE0) and REMAP.mi2=1 (SVSHAPE1)
1233
1234 Example 2: if rmm=0b10001 then again SVSHAPE0 and SVSHAPE1
1235 are set up, but the REMAP SPR is set so that mi0 uses SVSHAPE0
1236 and mo1 uses SVSHAPE1. REMAP.SVme=0b10001, REMAP.mi0=0, REMAP.mo1=1
1237
1238 Rough algorithmic form:
1239
1240 ```
1241 marray = [mi0, mi1, mi2, mo0, mo1]
1242 idx = 0
1243 for bit = 0 to 4:
1244 if not rmm[bit]: continue
1245 setup(SVSHAPE[idx])
1246 SVSTATE{marray[bit]} = idx
1247 idx = (idx+1) modulo 4
1248 ```
1249
1250 When `mm=1`:
1251
1252 * bits 0-2 (MSB0 numbering) of `rmm` indicate an index selecting mi0-mo1
1253 * bits 3-4 (MSB0 numbering) of `rmm` indicate which SVSHAPE 0-3 shall
1254 be updated
1255 * only the selected SVSHAPE is overwritten
1256 * only the relevant bits in the REMAP area of SVSTATE are updated
1257 * REMAP persistence bit is set.
1258
1259 Example 1: if `rmm`=0b01110 then bits 0-2 (MSB0) are 0b011 and
1260 bits 3-4 are 0b10. thus, mo0 is selected and SVSHAPE2
1261 to be updated. REMAP.SVme[3] will be set high and REMAP.mo0
1262 set to 2 (SVSHAPE2).
1263
1264 Example 2: if `rmm`=0b10011 then bits 0-2 (MSB0) are 0b100 and
1265 bits 3-4 are 0b11. thus, mo1 is selected and SVSHAPE3
1266 to be updated. REMAP.SVme[4] will be set high and REMAP.mo1
1267 set to 3 (SVSHAPE3).
1268
1269 Rough algorithmic form:
1270
1271 ```
1272 marray = [mi0, mi1, mi2, mo0, mo1]
1273 bit = rmm[0:2]
1274 idx = rmm[3:4]
1275 setup(SVSHAPE[idx])
1276 SVSTATE{marray[bit]} = idx
1277 SVSTATE.pst = 1
1278 ```
1279
1280 In essence, `mm=0` is intended for use to set as much of the
1281 REMAP State SPRs as practical with a single instruction,
1282 whilst `mm=1` is intended to be a little more refined.
1283
1284 **Usage guidelines**
1285
1286 * **Disable 2D mapping**: to only perform Indexing without
1287 reordering use `SVd=1,sk=0,yx=0` (or set SVd to a value larger
1288 or equal to VL)
1289 * **Modulo 1D mapping**: to perform Indexing cycling through the
1290 first N Indices use `SVd=N,sk=0,yx=0` where `VL>N`. There is
1291 no requirement to set VL equal to a multiple of N.
1292 * **Modulo 2D transposed**: `SVd=M,sk=0,yx=1`, sets
1293 `xdim=M,ydim=CEIL(MAXVL/M)`.
1294
1295 Beyond these mappings it becomes necessary to write directly to
1296 the SVSTATE SPRs manually.
1297
1298 -------------
1299
1300 \newpage{}
1301
1302
1303 # svshape2 (offset-priority) <a name="svshape2"> </a>
1304
1305 SVM2-Form
1306
1307 | 0:5|6:9 |10|11:15 |16:20 | 21:24 | 25 | 26:31 | Form |
1308 | -- |----|--| --- | ----- | ------ | -- | ------| -------- |
1309 | PO |offs|yx| rmm | SVd | 100/mm | sk | XO | SVM2-Form |
1310
1311 * svshape2 offs,yx,rmm,SVd,sk,mm
1312
1313 Pseudo-code:
1314
1315 ```
1316 # based on nearest MAXVL compute other dimension
1317 MVL <- SVSTATE[0:6]
1318 d <- [0] * 6
1319 dim <- SVd+1
1320 do while d*dim <u ([0]*4 || MVL)
1321 d <- d + 1
1322 # set up template, then copy once location identified
1323 shape <- [0]*32
1324 shape[30:31] <- 0b00 # mode
1325 shape[0:5] <- (0b0 || SVd) # x/ydim
1326 if SVyx = 0 then
1327 shape[18:20] <- 0b000 # ordering xd/yd(/zd)
1328 if sk = 0 then shape[6:11] <- 0 # ydim
1329 else shape[6:11] <- 0b111111 # ydim max
1330 else
1331 shape[18:20] <- 0b010 # ordering yd/xd(/zd)
1332 if sk = 1 then shape[6:11] <- 0 # ydim
1333 else shape[6:11] <- d-1 # ydim max
1334 # offset (the prime purpose of this instruction)
1335 shape[24:27] <- SVo # offset
1336 if sk = 1 then shape[28:29] <- 0b01 # skip 1st dimension
1337 else shape[28:29] <- 0b00 # no skipping
1338 # select the mode for updating SVSHAPEs
1339 SVSTATE[62] <- mm # set or clear persistence
1340 if mm = 0 then
1341 # clear out all SVSHAPEs first
1342 SVSHAPE0[0:31] <- [0] * 32
1343 SVSHAPE1[0:31] <- [0] * 32
1344 SVSHAPE2[0:31] <- [0] * 32
1345 SVSHAPE3[0:31] <- [0] * 32
1346 SVSTATE[32:41] <- [0] * 10 # clear REMAP.mi/o
1347 SVSTATE[42:46] <- rmm # rmm exactly REMAP.SVme
1348 idx <- 0
1349 for bit = 0 to 4
1350 if rmm[4-bit] then
1351 # activate requested shape
1352 if idx = 0 then SVSHAPE0 <- shape
1353 if idx = 1 then SVSHAPE1 <- shape
1354 if idx = 2 then SVSHAPE2 <- shape
1355 if idx = 3 then SVSHAPE3 <- shape
1356 SVSTATE[bit*2+32:bit*2+33] <- idx
1357 # increment shape index, modulo 4
1358 if idx = 3 then idx <- 0
1359 else idx <- idx + 1
1360 else
1361 # refined SVSHAPE/REMAP update mode
1362 bit <- rmm[0:2]
1363 idx <- rmm[3:4]
1364 if idx = 0 then SVSHAPE0 <- shape
1365 if idx = 1 then SVSHAPE1 <- shape
1366 if idx = 2 then SVSHAPE2 <- shape
1367 if idx = 3 then SVSHAPE3 <- shape
1368 SVSTATE[bit*2+32:bit*2+33] <- idx
1369 SVSTATE[46-bit] <- 1
1370 ```
1371
1372 Special Registers Altered:
1373
1374 ```
1375 SVSTATE, SVSHAPE0-3
1376 ```
1377
1378 `svshape2` is an additional convenience instruction that prioritises
1379 setting `SVSHAPE.offset`. Its primary purpose is for use when
1380 element-width overrides are used. It has identical capabilities to `svindex`
1381 in terms of both options (skip, etc.) and ability to activate REMAP
1382 (rmm, mask mode) but unlike `svindex` it does not set GPR REMAP:
1383 only a 1D or 2D `svshape`, and
1384 unlike `svshape` it can set an arbitrary `SVSHAPE.offset` immediate.
1385
1386 One of the limitations of Simple-V is that Vector elements start on the boundary
1387 of the Scalar regfile, which is fine when element-width overrides are not
1388 needed. If the starting point of a Vector with smaller elwidths must begin
1389 in the middle of a register, normally there would be no way to do so except
1390 through costly LD/ST. `SVSHAPE.offset` caters for this scenario and `svshape2`
1391 makes it easier to access.
1392
1393 **Operand Fields**:
1394
1395 * **offs** (4 bits) - unsigned offset
1396 * **yx** (1 bit) - swap XY to YX
1397 * **SVd** dimension size
1398 * **rmm** REMAP mask
1399 * **mm** mask mode
1400 * **sk** (1 bit) skips 1st dimension if set
1401
1402 Dimensions are calculated exactly as `svindex`. `rmm` and
1403 `mm` are as per `svindex`.
1404
1405 *Programmer's Note: offsets for `svshape2` may be specified in the range
1406 0-15. Given that the principle of Simple-V is to fit on top of
1407 byte-addressable register files and that GPR and FPR are 64-bit (8 bytes)
1408 it should be clear that the offset may, when `elwidth=8`, begin an
1409 element-level operation starting element zero at any arbitrary byte.
1410 On cursory examination attempting to go beyond the range 0-7 seems
1411 unnecessary given that the **next GPR or FPR** is an
1412 alias for an offset in the range 8-15. Thus by simply increasing
1413 the starting Vector point of the operation to the next register it
1414 can be seen that the offset of 0-7 would be sufficient. Unfortunately
1415 however some operations are EXTRA2-encoded it is **not possible**
1416 to increase the GPR/FPR register number by one, because EXTRA2-encoding
1417 of GPR/FPR Vector numbers are restricted to even numbering.
1418 For CR Fields the EXTRA2 encoding is even more sparse.
1419 The additional offset range (8-15) helps overcome these limitations.*
1420
1421 *Hardware Implementor's note: with the offsets only being immediates
1422 and with register numbering being entirely immediate as well it is
1423 possible to correctly compute Register Hazards without requiring
1424 reading the contents of any SPRs. If however there are
1425 instructions that have directly written to the SVSTATE or SVSHAPE
1426 SPRs and those instructions are still in-flight then this position
1427 is clearly **invalid**. This is why Programmers are strongly
1428 discouraged from directly writing to these SPRs.*
1429
1430 *Architectural Resource Allocation note: this instruction shares
1431 the space of `svshape`. Therefore it is critical that the two
1432 instructions, `svshape` and `svshape2` have the exact same XO
1433 in bits 26 thru 31. It is also critical that for `svshape2`,
1434 bit 21 of XO is a 1, bit 22 of XO is a 0, and bit 23 of XO is a 0.*
1435
1436 [[!tag standards]]
1437
1438 -------------
1439
1440 \newpage{}
1441