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