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