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