(no commit message)
[libreriscv.git] / openpower / sv / svp_rewrite / svp64.mdwn
1 # Links
2
3 * <http://lists.libre-soc.org/pipermail/libre-soc-dev/2020-December/001498.html>>
4 * [[svp64/discussion]]
5 * <http://lists.libre-soc.org/pipermail/libre-soc-dev/2020-December/001650.html>
6 * <https://bugs.libre-soc.org/show_bug.cgi?id=550>
7
8 # Rewrite of SVP64 for OpenPower ISA v3.1
9
10 The plan is to create an encoding for SVP64, then to create an encoding
11 for SVP48, then to reorganize them both to improve field overlap,
12 reducing the amount of decoder hardware necessary.
13
14 All bit numbers are in MSB0 form (the bits are numbered from 0 at the MSB
15 and counting up as you move to the LSB end). All bit ranges are inclusive
16 (so `4:6` means bits 4, 5, and 6).
17
18 64-bit instructions are split into two 32-bit words, the prefix and the
19 suffix. The prefix always comes before the suffix in PC order.
20
21 | 0:5 | 6:31 | 0:31 |
22 |--------|--------------|--------------|
23 | EXT01 | v3.1B Prefix | v3.1B Suffix |
24
25 # Definition of Reserved in this spec.
26
27 For the new fields added in SVP64, instructions that have any of their
28 fields set to a reserved value must cause an illegal instruction trap,
29 to allow emulation of future instruction sets.
30
31 This is unlike OpenPower ISA v3.1, which in many instances does not require a trap if reserved fields are nonzero.
32
33 # Identity Behaviour
34
35 SVP64 is designed so that when the prefix is all zeros, and
36 VL=1, no effect or
37 influence occurs (no augmentation) such that all standard OpenPOWER
38 v3.0/1B instructions covered by the prefix are "unaltered". This is termed `scalar identity behaviour` (based on the mathematical definition for "identity", as in, "identity matrix" or better "identity transformation").
39
40 Note that this is completely different from when VL=0. VL=0 turns all operations under its influence into `nops` (regardless of the prefix)
41 whereas when VL=1 and the SV prefix is all zeros, the operation simply acts as if SV had not been applied at all to the instruction (an "identity operation").
42
43 # XER, SO and other global flags
44
45 Vector systems are expected to be high performance. This is achieved
46 through parallelism, which requires that elements in the vector be
47 independent. XER SO and other global "accumulation" flags (CR.OV) cause
48 Read-Write Hazards on single-bit global resources, having a significant
49 detrimental adverse effect.
50
51 Consequently in SV, XER.SO and CR.OV behaviour is disregarded. XER is
52 simply neither read nor written. This includes when `scalar identity behaviour` occurs. If precise OpenPOWER v3.0/1 scalar behaviour is desired then OpenPOWER v3.0/1 instructions should be used without an SV Prefix.
53
54 An interesting side-effect of this decision is that the OE flag is now free for other uses when SV Prefixing is used.
55
56 # Additional instructions: v3.0B/v3.1B alternatives
57
58 SV is primarily designed for use as an efficient hybrid 3D GPU / VPU / CPU ISA.
59
60 As mentioned above, OE=1 is not applicable in SV, freeing this bit for alternative uses. Additionally, Vectorisation of the VSX SIMD system likewise makes no sense whatsoever: SV replaces VSX and provides, at the very minimum, predication (which VSX was designed without). Thus all VSX Major Opcodes - all of them - are "unused" and must raise illegal instruction exceptions in SV Prefix Mode.
61
62 Likewise, `lq` (Load Quad), and Load/Store Multiple make no sense to have because they are not only provided by SV, the SV alternatives may be predicated as well, making them far better suited to use in function calls and context-switching.
63
64 This leaves several Major Opcodes free for use by SV to fit alternative instructions: Vector Product, Vector Normalise, [[sv/mv.swizzle]], Texture LD/ST operations, and others critical to an efficient, effective 3D GPU and VPU ISA. With such instructions being included as standard in other commercially-successful GPU ISAs it is likewise critical that a 3D GPU/VPU based on svp64 also have such instructions.
65
66 Note however that svp64 is stand-alone and is in no way critically dependent on the existence or provision of 3D GPU or VPU instructions. These should be considered extensions, and their discussion and specification is out of scope for this document.
67
68 Note, again: this is *only* under svp64 prefixing. Standard v3.0B / v3.1B is *not* altered by svp64.
69
70 # Register Naming and size
71
72 SV Registers are simply the INT, FP and CR register files extended
73 linearly to larger sizes; SV Vectorisation iterates sequentially through these registers.
74
75 Where the integer regfile in standard scalar
76 OpenPOWER v3.0B/v3.1B is r0 to r31, SV extends this as r0 to r127.
77 Likewise FP registers are extended to 128 (fp0 to fp127), and CRs are
78 extended to 64 entries, CR0 thru CR63.
79
80 The names of the registers therefore reflects a simple linear extension
81 of the OpenPOWER v3.0B / v3.1B register naming, and in hardware this
82 would be reflected by a linear increase in the size of the underlying
83 SRAM used for the regfiles.
84
85 Note: when an EXTRA field (defined below) is zero, SV is deliberately designed
86 so that the register fields are identical to as if SV was not in effect
87 i.e. under these circumstances (EXTRA=0) the register field names RA,
88 RB etc. are interpreted and treated as v3.0B / v3.1B scalar registers. This is part of
89 `scalar identity behaviour` described above.
90
91 ## Future expansion.
92
93 With the way that EXTRA fields are defined and applied to register fields,
94 future versions of SV may involve 256 or greater registers. To accommodate 256 registers, numbering of Vectors will simply shift up by one bit, without
95 requiring additional prefix bits. Backwards binary compatibility may be achieved with a PCR bit (Program Compatibility Register). Beyond this, further discussion is out of scope for this version of svp64.
96
97 # Remapped Encoding (`RM[0:23]`)
98
99 To allow relatively easy remapping of which portions of the Prefix Opcode
100 Map are used for SVP64 without needing to rewrite a large portion of the
101 SVP64 spec, a mapping is defined from the OpenPower v3.1 prefix bits to
102 a new 24-bit Remapped Encoding denoted `RM[0]` at the MSB to `RM[23]`
103 at the LSB.
104
105 The mapping from the OpenPower v3.1 prefix bits to the Remapped Encoding
106 is defined in the Prefix Fields section.
107
108 ## Prefix Opcode Map (64-bit instruction encoding) (prefix bits 6:11)
109
110 shows both PowerISA v3.1 instructions as well as new SVP instructions;
111 empty spaces are yet-to-be-allocated Illegal Instructions. The original
112 table in the v3.1B OpenPOWER ISA Spec is on p1350, Table 12.
113
114 | 6:11 | ---000 | ---001 | ---010 | ---011 | ---100 | ---101 | ---110 | ---111 |
115 |------|--------|--------|--------|--------|--------|--------|--------|--------|
116 |000---| 8LS | 8LS | 8LS | 8LS | 8LS | 8LS | 8LS | 8LS |
117 |001---| | | | | | | | |
118 |010---| 8RR | | | | `SVP64`| `SVP64`| `SVP64`| `SVP64`|
119 |011---| | | | | `SVP64`| `SVP64`| `SVP64`| `SVP64`|
120 |100---| MLS | MLS | MLS | MLS | MLS | MLS | MLS | MLS |
121 |101---| | | | | | | | |
122 |110---| MRR | | | | `SVP64`| `SVP64`| `SVP64`| `SVP64`|
123 |111---| | MMIRR | | | `SVP64`| `SVP64`| `SVP64`| `SVP64`|
124
125 ## Prefix Fields
126
127 To "activate" svp64, fields within the v3.1B Prefix Opcode Map are set
128 (see Prefix Opcode Map, above), leaving 24 bits "free" for use by SV.
129
130 | Name | Bits | Value | Description |
131 |------------|---------|-------|--------------------------------|
132 | EXT01 | `0:5` | `1` | Indicates Prefixed 64-bit |
133 | `RM[0]` | `6` | | Bit 0 of Remapped Encoding |
134 | SVP64_7 | `7` | `1` | Indicates this is SVP64 |
135 | `RM[1]` | `8` | | Bit 1 of Remapped Encoding |
136 | SVP64_9 | `9` | `1` | Indicates this is SVP64 |
137 | `RM[2:23]` | `10:31` | | Bits 2-23 of Remapped Encoding |
138
139 Laid out bitwise, this is as follows, showing how the 32-bits of the prefix
140 are constructed:
141
142 | 0:5 | 6 | 7 | 8 | 9 | 10:31 |
143 |--------|-------|---|-------|---|----------|
144 | EXT01 | RM | 1 | RM | 1 | RM |
145 | 000001 | RM[0] | 1 | RM[1] | 1 | RM]2:23] |
146
147 Following the prefix will be the suffix: this is simply a 32-bit v3.0B / v3.1B
148 instruction. That instruction is "prefixed" with the SV context: the
149 Remapped Encoding field (RM).
150
151 # Remapped Encoding Fields
152
153 Shows all fields in the Remapped Encoding `RM[0:23]` for all instruction
154 variants. There are two categories: Single and Twin Predication.
155 Due to space considerations further subdivision of Single Predication
156 is based on whether the number of src operands is 2 or 3.
157
158
159 * `RM-1P-3S1D` Single Predication dest/src1/2/3, applies to 4-operand instructions (fmadd, isel, madd).
160 * `RM-1P-2S1D` Single Predication dest/src1/2 applies to 3-operand instructions (src1 src2 dest)
161 * `RM-2P-1S1D` Twin Predication (src=1, dest=1)
162 * `RM-2P-2S1D` Twin Predication (src=2, dest=1) primarily for LDST (Indexed)
163 * `RM-2P-1S2D` Twin Predication (src=1, dest=2) primarily for LDST Update
164
165 ## RM-1P-3S1D
166
167 | Field Name | Field bits | Description |
168 |------------|------------|----------------------------------------|
169 | MASK\_KIND | `0` | Execution Mask Kind |
170 | MASK | `1:3` | Execution Mask |
171 | ELWIDTH | `4:5` | Element Width |
172 | SUBVL | `6:7` | Sub-vector length |
173 | Rdest\_EXTRA2 | `8:9` | extends Rdest (R\*\_EXTRA2 Encoding) |
174 | Rsrc1\_EXTRA2 | `10:11` | extends Rsrc1 (R\*\_EXTRA2 Encoding) |
175 | Rsrc2\_EXTRA2 | `12:13` | extends Rsrc2 (R\*\_EXTRA2 Encoding) |
176 | Rsrc3\_EXTRA2 | `14:15` | extends Rsrc3 (R\*\_EXTRA2 Encoding) |
177 | reserved | `16` | reserved |
178 | MODE | `19:23` | changes Vector behaviour |
179
180 ## RM-1P-2S1D
181
182 | Field Name | Field bits | Description |
183 |------------|------------|-------------------------------------------|
184 | MASK\_KIND | `0` | Execution Mask Kind |
185 | MASK | `1:3` | Execution Mask |
186 | ELWIDTH | `4:5` | Element Width |
187 | SUBVL | `6:7` | Sub-vector length |
188 | Rdest\_EXTRA3 | `8:10` | extends Rdest |
189 | Rsrc1\_EXTRA3 | `11:13` | extends Rsrc1 |
190 | Rsrc2\_EXTRA3 | `14:16` | extends Rsrc3 |
191 | ELWIDTH_SRC | `17:18` | Element Width for Source |
192 | MODE | `19:23` | changes Vector behaviour |
193
194 These are for 2 operand 1 dest instructions, such as `add RT, RA,
195 RB`. However also included are unusual instructions with an implicit dest
196 that is identical to its src reg, such as `rlwinmi`.
197
198 Normally, with instructions such as `rlwinmi`, the scalar v3.0B ISA would not have sufficient bit fields to allow
199 an alternative destination. With SV however this becomes possible.
200 Therefore, the fact that the dest is implicitly also a src should not
201 mislead: due to the *prefix* they are different SV regs.
202
203 * `rlwimi RA, RS, ...`
204 * Rsrc1_EXTRA3 applies to RS as the first src
205 * Rsrc2_EXTRA3 applies to RA as the secomd src
206 * Rdest_EXTRA3 applies to RA to create an **independent** dest.
207
208 With the addition of the EXTRA bits, the three registers
209 each may be *independently* made vector or scalar, and be independently
210 augmented to 7 bits in length.
211
212 Note that if ELWIDTH != ELWIDTH_SRC this may result in reduced performance or increased latency in some implementations due to lane-crossing.
213
214 ## RM-2P-1S1D
215
216 | Field Name | Field bits | Description |
217 |------------|------------|----------------------------|
218 | MASK_KIND | `0` | Execution Mask Kind |
219 | MASK | `1:3` | Execution Mask |
220 | ELWIDTH | `4:5` | Element Width |
221 | SUBVL | `6:7` | Sub-vector length |
222 | Rdest_EXTRA3 | `8:10` | extends Rdest |
223 | Rsrc1_EXTRA3 | `11:13` | extends Rsrc1 |
224 | MASK_SRC | `14:16` | Execution Mask for Source |
225 | ELWIDTH_SRC | `17:18` | Element Width for Source |
226 | MODE | `19:23` | changes Vector behaviour |
227
228 Note that if ELWIDTH != ELWIDTH_SRC this may result in reduced performance or increased latency in some implementations due to lane-crossing.
229
230 ## RM-2P-2S1D/1S2D
231
232 The primary purpose for this encoding is for Twin Predication on LOAD
233 and STORE operations. see [[sv/ldst]] for detailed anslysis.
234
235 RM-2P-2S1D:
236
237 | Field Name | Field bits | Description |
238 |------------|------------|----------------------------|
239 | MASK_KIND | `0` | Execution Mask Kind |
240 | MASK | `1:3` | Execution Mask |
241 | ELWIDTH | `4:5` | Element Width |
242 | SUBVL | `6:7` | Sub-vector length |
243 | Rdest_EXTRA2 | `8:9` | extends Rdest (R\*\_EXTRA2 Encoding) |
244 | Rsrc1_EXTRA2 | `10:11` | extends Rsrc1 (R\*\_EXTRA2 Encoding) |
245 | Rsrc2_EXTRA2 | `12:13` | extends Rsrc2 (R\*\_EXTRA2 Encoding) |
246 | MASK_SRC | `14:16` | Execution Mask for Source |
247 | ELWIDTH_SRC | `17:18` | Element Width for Source |
248 | MODE | `19:23` | changes Vector behaviour |
249
250 Note that for 1S2P the EXTRA2 dest and src names are switched (Rsrc_EXTRA2
251 is in bits 8:9, Rdest1_EXTRA2 in 10:11)
252
253 Note also that LD with update indexed, which takes 2 src and 2 dest
254 (e.g. `lhaux RT,RA,RB`), does not have room for 4 registers and also
255 Twin Predication. therefore these are treated as RM-2P-2S1D and the
256 src spec for RA is also used for the same RA as a dest.
257
258 Note that if ELWIDTH != ELWIDTH_SRC this may result in reduced performance or increased latency in some implementations due to lane-crossing.
259
260 # Mode
261
262 Mode is an augmentation of SV behaviour. Some of these alterations are element-based (saturation), others involve post-analysis (predicate result) and others are Vector-based (mapreduce, fail-on-first).
263
264 These are the modes:
265
266 * **normal** mode is straight vectorisation. no augmentations: the vector comprises an array of independently created results.
267 * **ffirst** or data-dependent fail-on-first: see separate section. the vector may be truncated depending on certain criteria.
268 *VL is altered as a result*.
269 * **sat mode** or saturation: clamps each elemrnt result to a min/max rather than overflows / wraps. allows signed and unsigned clamping.
270 * **reduce mode**. a mapreduce is performed. the result is a scalar. a result vector however is required, as the upper elements may be used to store intermediary computations. the result of the mapreduce is in the first element with a nonzero predicate bit. see separate section below.
271 note that there are comprehensive caveats when using this mode.
272 * **pred-result** will test the result (CR testing selects a bit of CR and inverts it, just like branch testing) and if the test fails it is as if the predicate bit was zero. When Rc=1 the CR element however is still stored in the CR regfile, even if the test failed. This scheme does not apply to crops (crand, cror). See appendix for details.
273
274 Note that ffirst and reduce modes are not anticipated to be high-performance in some implementations. ffirst due to interactions with VL, and reduce due to it requiring additional operations to produce a result. normal, saturate and pred-result are however independent and may easily be parallelised to give high performance, regardless of the value of VL.
275
276 The Mode table is laid out as follows:
277
278 | 0-1 | 2 | 3 4 | description |
279 | --- | --- |---------|-------------------------- |
280 | 00 | 0 | sz dz | normal mode |
281 | 00 | 1 | sz CRM | reduce mode (mapreduce), SUBVL=1 |
282 | 00 | 1 | SVM CRM | subvector reduce mode, SUBVL>1 |
283 | 01 | inv | CR-bit | Rc=1: ffirst CR sel |
284 | 01 | inv | sz dz | Rc=0: ffirst z/nonz |
285 | 10 | N | sz dz | sat mode: N=0/1 u/s |
286 | 11 | inv | CR-bit | Rc=1: pred-result CR sel |
287 | 11 | inv | sz dz | Rc=0: pred-result z/nonz |
288
289 Fields:
290
291 * **sz / dz** if predication is enabled will put zeros into the dest (or as src in the case of twin pred) when the predicate bit is zero. otherwise the element is ignored or skipped, depending on context.
292 * **inv CR bit** just as in branches (BO) these bits allow testing of a CR bit and whether it is set (inv=0) or unset (inv=1)
293 * **CRM** affects the CR on reduce mode when Rc=1
294 * **SVM** sets "subvector" reduce mode
295 * **N** sets signed/unsigned saturation.
296
297 # R\*\_EXTRA2 and R\*\_EXTRA3 Encoding
298
299 EXTRA is the means by which two things are achieved:
300
301 1. Registers are marked as either Vector *or Scalar*
302 2. Register field numbers (limited typically to 5 bit)
303 are extended in range, both for Scalar and Vector.
304
305 In the following tables register numbers are constructed from the
306 standard v3.0B / v3.1B 32 bit register field (RA, FRA) and the EXTRA2
307 or EXTRA3 field from the SV Prefix. The prefixing is arranged so that
308 interoperability between prefixing and nonprefixing of scalar registers
309 is direct and convenient (when the EXTRA field is all zeros).
310
311 A pseudocode algorithm explains the relationship, for INT/FP (see separate section for CRs)
312
313 if extra3_mode:
314 spec = EXTRA3
315 else:
316 spec = EXTRA2 << 1 # same as EXTRA3, shifted
317 if spec[2]: # vector
318 return (RA << 2) | spec[0:1]
319 else: # scalar
320 return (spec[0:1] << 5) | RA
321
322 ## INT/FP EXTRA3
323
324 alternative which is understandable and, if EXTRA3 is zero, maps to
325 "no effect" (scalar OpenPOWER ISA field naming). also, these are the
326 encodings used in the original SV Prefix scheme. the reason why they
327 were chosen is so that scalar registers in v3.0B and prefixed scalar
328 registers have access to the same 32 registers.
329
330 | R\*\_EXTRA3 | Mode | Range | MSB downto LSB |
331 |-----------|-------|---------------|---------------------|
332 | 000 | Scalar | `r0-r31` | `0b00 RA` |
333 | 001 | Scalar | `r32-r63` | `0b01 RA` |
334 | 010 | Scalar | `r64-r95` | `0b10 RA` |
335 | 011 | Scalar | `r96-r127` | `0b11 RA` |
336 | 100 | Vector | `r0-r124` | `RA 0b00` |
337 | 101 | Vector | `r1-r125` | `RA 0b01` |
338 | 110 | Vector | `r2-r126` | `RA 0b10` |
339 | 111 | Vector | `r3-r127` | `RA 0b11` |
340
341 ## INT/FP EXTRA2
342
343 alternative which is understandable and, if EXTRA2 is zero will map to
344 "no effect" i.e Scalar OpenPOWER register naming:
345
346 | R\*\_EXTRA2 | Mode | Range | MSB down to LSB |
347 |-----------|-------|---------------|---------------------|
348 | 00 | Scalar | `r0-r31` | `0b00 RA` |
349 | 01 | Scalar | `r32-r63` | `0b01 RA` |
350 | 10 | Vector | `r0-r124` | `RA 0b00` |
351 | 11 | Vector | `r2-r126` | `RA 0b10` |
352
353 ## CR EXTRA3
354
355 CR encoding is essentially the same but made more complex due to CRs being bit-based. See separate section for explanation and pseudocode.
356
357 Encoding shown MSB down to LSB
358
359 | R\*\_EXTRA3 | Mode | 7..5 | 4..2 | 1..0 |
360 |-------------|------|---------| --------|---------|
361 | 000 | Scalar | 0b000 | BA[4:2] | BA[1:0] |
362 | 001 | Scalar | 0b001 | BA[4:2] | BA[1:0] |
363 | 010 | Scalar | 0b010 | BA[4:2] | BA[1:0] |
364 | 011 | Scalar | 0b011 | BA[4:2] | BA[1:0] |
365 | 100 | Vector | BA[4:2] | 0b000 | BA[1:0] |
366 | 101 | Vector | BA[4:2] | 0b010 | BA[1:0] |
367 | 110 | Vector | BA[4:2] | 0b100 | BA[1:0] |
368 | 111 | Vector | BA[4:2] | 0b110 | BA[1:0] |
369
370 ## CR EXTRA2
371
372 CR encoding is essentially the same but made more complex due to CRs being bit-based. See separate section for explanation and pseudocode.
373
374 Encoding shown MSB down to LSB
375
376 | R\*\_EXTRA2 | Mode | 7..5 | 4..2 | 1..0 |
377 |-------------|--------|---------|---------|---------|
378 | 00 | Scalar | 0b000 | BA[4:2] | BA[1:0] |
379 | 01 | Scalar | 0b001 | BA[4:2] | BA[1:0] |
380 | 10 | Vector | BA[4:2] | 0b000 | BA[1:0] |
381 | 11 | Vector | BA[4:2] | 0b100 | BA[1:0] |
382
383 # ELWIDTH Encoding
384
385 Default behaviour is set to 0b00 so that zeros follow the convention of
386 "npt doing anything". In this case it means that elwidth overrides
387 are not applicable. Thus if a 32 bit instruction operates on 32 bit,
388 `elwidth=0b00` specifies that this behaviour is unmodified. Likewise
389 when a processor is switched from 64 bit to 32 bit mode, `elwidth=0b00`
390 states that, again, the behaviour is not to be modified.
391
392 Only when elwidth is nonzero is the element width overridden to the
393 explicitly required value.
394
395 ## Elwidth for Integers:
396
397 | Value | Mnemonic | Description |
398 |-------|----------------|------------------------------------|
399 | 00 | DEFAULT | default behaviour for operation |
400 | 01 | `ELWIDTH=b` | Byte: 8-bit integer |
401 | 10 | `ELWIDTH=h` | Halfword: 16-bit integer |
402 | 11 | `ELWIDTH=w` | Word: 32-bit integer |
403
404 ## Elwidth for FP Registers:
405
406 | Value | Mnemonic | Description |
407 |-------|----------------|------------------------------------|
408 | 00 | DEFAULT | default behaviour for FP operation |
409 | 01 | `ELWIDTH=bf16` | Reserved for `bf16` |
410 | 10 | `ELWIDTH=f16` | 16-bit IEEE 754 Half floating-point |
411 | 11 | `ELWIDTH=f32` | 32-bit IEEE 754 Single floating-point |
412
413 Note:
414 [`bf16`](https://en.wikipedia.org/wiki/Bfloat16_floating-point_format)
415 is reserved for a future implementation of SV
416
417 ## Elwidth for CRs:
418
419 TODO, important, particularly for crops, mfcr and mtcr, what elwidth
420 even means. instead it may be possible to use the bits as extra indices
421 (EXTRA6) to access the full 64 CRs. TBD, several ideas
422
423 The actual width of the CRs cannot be altered: they are 4 bit. Also,
424 for Rc=1 operations that produce a result (in RT or FRT) and corresponding CR, it is
425 the INT/FP result to which the elwidth override applies, *not* the CR.
426 This therefore inherently places Rc=1 operations firmly out of scope as far as a "meaning" for elwidth on CRs is concerned.
427
428 As mentioned TBD, this leaves crops etc. to have a meaning defined for
429 elwidth, because these ops are pure explicit CR based.
430
431 Examples: mfxm may take the extra bits and use them as extra mask bits.
432
433 # SUBVL Encoding
434
435 the default for SUBVL is 1 and its encoding is 0b00 to indicate that
436 SUBVL is effectively disabled (a SUBVL for-loop of only one element). this
437 lines up in combination with all other "default is all zeros" behaviour.
438
439 | Value | Mnemonic | Subvec | Description |
440 |-------|-----------|---------|------------------------|
441 | 00 | `SUBVL=1` | single | Sub-vector length of 1 |
442 | 01 | `SUBVL=2` | vec2 | Sub-vector length of 2 |
443 | 10 | `SUBVL=3` | vec3 | Sub-vector length of 3 |
444 | 11 | `SUBVL=4` | vec4 | Sub-vector length of 4 |
445
446 The SUBVL encoding value may be thought of as an inclusive range of a
447 sub-vector. SUBVL=2 represents a vec2, its encoding is 0b01, therefore
448 this may be considered to be elements 0b00 to 0b01 inclusive.
449
450 # MASK/MASK_SRC & MASK_KIND Encoding
451
452 One bit (`MASKMODE`) indicates the mode: CR or Int predication. The two
453 types may not be mixed.
454
455 Special note: to get default behaviour (SV disabled) this field must
456 be set to zero in combination with Integer Predication also being set
457 to 0b000. this has the effect of enabling "all 1s" in the predicate
458 mask, which is equivalent to "not having any predication at all"
459 and consequently, in combination with all other default zeros, fully
460 disables SV.
461
462 | Value | Description |
463 |-------|------------------------------------------------------|
464 | 0 | MASK/MASK_SRC are encoded using Integer Predication |
465 | 1 | MASK/MASK_SRC are encoded using CR-based Predication |
466
467 Integer Twin predication has a second set of 3 bits that uses the same
468 encoding thus allowing either the same register (r3 or r10) to be used
469 for both src and dest, or different regs (one for src, one for dest).
470
471 Likewise CR based twin predication has a second set of 3 bits, allowing
472 a different test to be applied.
473
474 ## Integer Predication (MASK_KIND=0)
475
476 When the predicate mode bit is zero the 3 bits are interpreted as below.
477 Twin predication has an identical 3 bit field similarly encoded.
478
479 | Value | Mnemonic | Element `i` enabled if: |
480 |-------|----------|------------------------------|
481 | 000 | ALWAYS | predicate effectively all 1s |
482 | 001 | 1 << R3 | `i == R3` |
483 | 010 | R3 | `R3 & (1 << i)` is non-zero |
484 | 011 | ~R3 | `R3 & (1 << i)` is zero |
485 | 100 | R10 | `R10 & (1 << i)` is non-zero |
486 | 101 | ~R10 | `R10 & (1 << i)` is zero |
487 | 110 | R30 | `R30 & (1 << i)` is non-zero |
488 | 111 | ~R30 | `R30 & (1 << i)` is zero |
489
490 ## CR-based Predication (MASK_KIND=1)
491
492 When the predicate mode bit is one the 3 bits are interpreted as below.
493 Twin predication has an identical 3 bit field similarly encoded
494
495 | Value | Mnemonic | Element `i` is enabled if |
496 |-------|----------|--------------------------|
497 | 000 | lt | `CR[offs+i].LT` is set |
498 | 001 | nl/ge | `CR[offs+i].LT` is clear |
499 | 010 | gt | `CR[offs+i].GT` is set |
500 | 011 | ng/le | `CR[offs+i].GT` is clear |
501 | 100 | eq | `CR[offs+i].EQ` is set |
502 | 101 | ne | `CR[offs+i].EQ` is clear |
503 | 110 | so/un | `CR[offs+i].FU` is set |
504 | 111 | ns/nu | `CR[offs+i].FU` is clear |
505
506 CR based predication. TODO: select alternate CR for twin predication? see
507 [[discussion]] Overlap of the two CR based predicates must be taken
508 into account, so the starting point for one of them must be suitably
509 high, or accept that for twin predication VL must not exceed the range
510 where overlap will occur, *or* that they use the same starting point
511 but select different *bits* of the same CRs
512
513 `offs` is defined as CR32 (4x8) so as to mesh cleanly with Vectorised Rc=1 operations (see below). Rc=1 operations start from CR8 (TBD).
514
515 # Appendix
516
517 ## Twin Predication
518
519 This is a novel concept that allows predication to be applied to a single
520 source and a single dest register. The following types of traditional
521 Vector operations may be encoded with it, *without requiring explicit
522 opcodes to do so*
523
524 * VSPLAT (a single scalar distributed across a vector)
525 * VEXTRACT (like LLVM IR [`extractelement`](https://releases.llvm.org/11.0.0/docs/LangRef.html#extractelement-instruction))
526 * VINSERT (like LLVM IR [`insertelement`](https://releases.llvm.org/11.0.0/docs/LangRef.html#insertelement-instruction))
527 * VCOMPRESS (like LLVM IR [`llvm.masked.compressstore.*`](https://releases.llvm.org/11.0.0/docs/LangRef.html#llvm-masked-compressstore-intrinsics))
528 * VEXPAND (like LLVM IR [`llvm.masked.expandload.*`](https://releases.llvm.org/11.0.0/docs/LangRef.html#llvm-masked-expandload-intrinsics))
529
530 Those patterns (and more) may be applied to:
531
532 * mv (the usual way that V\* ISA operations are created)
533 * exts\* sign-extension
534 * rwlinm and other RS-RA shift operations (**note**: excluding
535 those that take RA as both a src and dest. These are not
536 1-src 1-dest, they are 2-src, 1-dest)
537 * LD and ST (treating AGEN as one source)
538 * FP fclass, fsgn, fneg, fabs, fcvt, frecip, fsqrt etc.
539 * Condition Register ops mfcr, mtcr and other similar
540
541 This is a huge list that creates extremely powerful combinations,
542 particularly given that one of the predicate options is `(1<<r3)`
543
544 Additional unusual capabilities of Twin Predication include a back-to-back
545 version of VCOMPRESS-VEXPAND which is effectively the ability to do
546 sequentially ordered multiple VINSERTs. The source predicate selects a
547 sequentially ordered subset of elements to be inserted; the destination predicate specifies the sequentially ordered recipient locations.
548
549 ## Rounding, clamp and saturate
550
551 One of the issues with vector ops is that in integer DSP ops for example
552 in Audio the operation must clamp or saturate rather than overflow or
553 ignore the upper bits and become a modulo operation. This for Audio
554 is extremely important, also to provide an indicator as to whether
555 saturation occurred. see [[av_opcodes]].
556
557 To help ensure that audio quality is not compromised by overflow,
558 "saturation" is provided, as well as a way to detect when saturation
559 occurred (Rc=1). When Rc=1 there will be a *vector* of CRs, one CR per
560 element in the result (Note: this is different from VSX which has a
561 single CR per block).
562
563 When N=0 the result is saturated to within the maximum range of an
564 unsigned value. For integer ops this will be 0 to 2^elwidth-1. Similar
565 logic applies to FP operations, with the result being saturated to
566 maximum rather than returning INF.
567
568 When N=1 the same occurs except that the result is saturated to the min
569 or max of a signed result.
570
571 When Rc=1, the CR "overflow" bit is set on the CR associated with the
572 element, to indicate whether saturation occurred. Note that due to
573 the hugely detrimental effect it has on parallel processing, XER.SO is
574 **ignored** completely and is **not** brought into play here. The CR
575 overflow bit is therefore simply set to zero if saturation did not occur,
576 and to one if it did.
577
578 Post-analysis of the Vector of CRs to find out if any given element hit
579 saturation may be done using a mapreduced CR op (cror), or by using the
580 new crweird instruction, transferring the relevant CR bits to a scalar
581 integer and testing it for nonzero. see [[sv/cr_int_predication]]
582
583 Note that the operation takes place at the maximum bitwidth (max of src and dest elwidth) and that truncation occurs to the range of the dest elwidth.
584
585 ## Reduce mode
586
587 1. limited to single predicated dual src operations (add RT, RA, RB).
588 triple source operations are prohibited (fma).
589 2. limited to operations that make sense. divide is excluded, as is
590 subtract (X - Y - Z produces different answers depending on the order)
591 and asymmetric CRops (crandc, crorc). sane operations:
592 multiply, min/max, add, logical bitwise OR, most other CR ops.
593 operations that do have the same source and dest register type are
594 also excluded (isel, cmp)
595 3. the destination is a vector but the result is stored, ultimately,
596 in the first nonzero predicated element. all other nonzero predicated
597 elements are undefined. *this includes the CR vector* when Rc=1
598 4. implementations may use any ordering and any algorithm to reduce
599 down to a single result. However it must be equivalent to a straight
600 application of mapreduce. The destination vector (except masked out
601 elements) may be used for storing any intermediate results. these may
602 be left in the vector (undefined).
603 5. CRM applies when Rc=1. When CRM is zero, the CR associated with
604 the result is regarded as a "some results met standard CR result
605 criteria". When CRM is one, this changes to "all results met standard
606 CR criteria".
607 6. implementations MAY use destoffs as well as srcoffs (see [[sv/sprs]])
608 in order to store sufficient state to resume operation should an
609 interrupt occur. this is also why implementations are permitted to use
610 the destination vector to store intermediary computations
611 7. *Predication may be applied*. zeroing mode is not an option. masked-out
612 inputs are ignored; masked-out elements in the destination vector are
613 unaltered (not used for the purposes of intermediary storage); the
614 scalar result is placed in the first available unmasked element.
615
616 TODO: Rc=1 on Scalar Logical Operations? is this possible? was space
617 reserved in Logical Ops?
618
619 Pseudocode for the case where RA==RB:
620
621 result = op(iregs[RA], iregs[RA+1])
622 CR = analyse(result)
623 for i in range(2, VL):
624 result = op(result, iregs[RA+i])
625 CRnew = analyse(result)
626 if Rc=1
627 if CRM:
628 CR = CR bitwise or CRnew
629 else:
630 CR = CR bitwise AND CRnew
631
632 TODO: case where RA!=RB which involves first a vector of 2-operand
633 results followed by a mapreduce on the intermediates.
634
635 Note that when SVM is clear and SUBVL!=1 the sub-elements are *independent*, i.e. they
636 are mapreduced per *sub-element* as a result. illustration with a vec2:
637
638 result.x = op(iregs[RA].x, iregs[RA+1].x)
639 result.y = op(iregs[RA].y, iregs[RA+1].y)
640 for i in range(2, VL):
641 result.x = op(result.x, iregs[RA+i].x)
642 result.y = op(result.y, iregs[RA+i].y)
643
644 When SVM is set and SUBVL!=1, another variant is enabled.
645
646 for i in range(VL):
647 result = op(iregs[RA+i].x, iregs[RA+i].x)
648 result = op(result, iregs[RA+i].z)
649 result = op(result, iregs[RA+i].z)
650 iregs[RT+i] = result
651
652 ## Fail-on-first
653
654 Data-dependent fail-on-first has two distinct variants: one for LD/ST,
655 the other for arithmetic operations (actually, CR-driven). Note in each
656 case the assumption is that vector elements are required appear to be
657 executed in sequential Program Order, element 0 being the first.
658
659 * LD/ST ffirst treats the first LD/ST in a vector (element 0) as an
660 ordinary one. Exceptions occur "as normal". However for elements 1
661 and above, if an exception would occur, then VL is **truncated** to the
662 previous element.
663 * Data-driven (CR-driven) fail-on-first activates when Rc=1 or other
664 CR-creating operation produces a result (including cmp). Similar to
665 branch, an analysis of the CR is performed and if the test fails, the
666 vector operation terminates and discards all element operations at and
667 above the current one, and VL is truncated to the *previous* element.
668 Thus the new VL comprises a contiguous vector of results, all of which
669 pass the testing criteria (equal to zero, less than zero).
670
671 The CR-based data-driven fail-on-first is new and not found in ARM SVE
672 or RVV. It is extremely useful for reducing instruction count, however
673 requires speculative execution involving modifications of VL to get high
674 performance implementations.
675
676 In CR-based data-driven fail-on-first there is only the option to select
677 and test one bit of each CR (just as with branch BO). For more complex
678 tests this may be insufficient. If that is the case, a vectorised crops
679 (crand, cror) may be used, and ffirst applied to the crop instead of to
680 the arithmetic vector.
681
682 One extremely important aspect of ffirst is:
683
684 * LDST ffirst may never set VL equal to zero. This because on the first
685 element an exception must be raised "as normal".
686 * CR-based data-dependent ffirst on the other hand **can** set VL equal
687 to zero. This is the only means in the entirety of SV that VL may be set
688 to zero (with the exception of via the SV.STATE SPR). When VL is set
689 zero due to the first element failing the CR bit-test, all subsequent
690 vectorised operations are effectively `nops` which is
691 *precisely the desired and intended behaviour*.
692
693 ## pred-result mode
694
695 This mode merges common CR testing with predication, saving on instruction count. Below is the pseudocode excluding predicate zeroing and elwidth overrides.
696
697 for i in range(VL):
698 # predication test, skip all masked out elements.
699 if predicate_masked_out(i):
700 continue
701 result = op(iregs[RA+i], iregs[RB+i])
702 CRnew = analyse(result) # calculates eq/lt/gt
703 # Rc=1 always stores the CR
704 if Rc=1:
705 crregs[offs+i] = CRnew
706 # now test CR, similar to branch
707 if CRnew[BO[0:1]] != BO[2]:
708 continue # test failed: cancel store
709 # result optionally stored but CR always is
710 iregs[RT+i] = result
711
712 The reason for allowing the CR element to be stored is so that post-analysis
713 of the CR Vector may be carried out. For example: Saturation may have occurred (and been prevented from updating, by the test) but it is desirable to know *which* elements fail saturation.
714
715 Note that predication is still respected: predicate zeroing is slightly different: elements that fail the CR test *or* are masked out are zero'd.
716
717 ## CR Operations
718
719 CRs are slightly more involved than INT or FP registers due to the
720 possibility for indexing individual bits (crops BA/BB/BT). Again however
721 the access pattern needs to be understandable in relation to v3.0B / v3.1B
722 numbering, with a clear linear relationship and mapping existing when
723 SV is applied.
724
725 ### CR EXTRA mapping table and algorithm
726
727 Numbering relationships for CR fields are already complex due to being
728 in BE format (*the relationship is not clearly explained in the v3.0B
729 or v3.1B specification*). However with some care and consideration
730 the exact same mapping used for INT and FP regfiles may be applied,
731 just to the upper bits, as explained below.
732
733 In OpenPOWER v3.0/1, BF/BT/BA/BB are all 5 bits. The top 3 bits (2:4)
734 select one of the 8 CRs; the bottom 2 bits (0:1) select one of 4 bits
735 *in* that CR. The numbering was determined (after 4 months of
736 analysis and research) to be as follows:
737
738 CR_index = 7-(BA>>2) # top 3 bits but BE
739 bit_index = 3-(BA & 0b11) # low 2 bits but BE
740 CR_reg = CR[CR_index] # get the CR
741 # finally get the bit from the CR.
742 CR_bit = (CR_reg & (1<<bit_index)) != 0
743
744 When it comes to applying SV, it is the CR\_reg number to which SV EXTRA2/3
745 applies, **not** the CR\_bit portion (bits 0:1):
746
747 if extra3_mode:
748 spec = EXTRA3
749 else:
750 spec = EXTRA2<<1 | 0b0
751 if spec[2]:
752 # vector constructs "BA[2:4] spec[0:1] 0 BA[0:1]"
753 return ((BA >> 2)<<5) | # hi 3 bits shifted up
754 (spec[0:1]<<3) | # to make room for these
755 (BA & 0b11) # CR_bit on the end
756 else:
757 # scalar constructs "0 spec[0:1] BA[0:4]"
758 return (spec[0:1] << 5) | BA
759
760 Thus, for example, to access a given bit for a CR in SV mode, the v3.0B
761 algorithm to determin CR\_reg is modified to as follows:
762
763 CR_index = 7-(BA>>2) # top 3 bits but BE
764 if spec[2]:
765 # vector mode
766 CR_index = (CR_index<<3) | (spec[0:1] << 1)
767 else:
768 # scalar mode
769 CR_index = (spec[0:1]<<3) | CR_index
770 # same as for v3.0/v3.1 from this point onwards
771 bit_index = 3-(BA & 0b11) # low 2 bits but BE
772 CR_reg = CR[CR_index] # get the CR
773 # finally get the bit from the CR.
774 CR_bit = (CR_reg & (1<<bit_index)) != 0
775
776 Note here that the decoding pattern to determine CR\_bit does not change.
777
778 Note: high-performance implementations may read/write Vectors of CRs in
779 batches of aligned 32-bit chunks (CR0-7, CR7-15). This is to greatly
780 simplify internal design. If instructions are issued where CR Vectors
781 do not start on a 32-bit aligned boundary, performance may be affected.
782
783 ### CR fields as inputs/outputs of vector operations
784
785 CRs (or, the arithmetic operations associated with them)
786 may be marked as Vectorised or Scalar. When Rc=1 in arithmetic operations that have no explicit EXTRA to cover the CR, the CR is Vectorised if the destination is Vectorised. Likewise if the destination is scalar then so is the CR.
787
788 When vectorized, the CR inputs/outputs are sequentially read/written
789 to 4-bit CR fields. Vectorised Integer results, when Rc=1, will begin
790 writing to CR8 (TBD evaluate) and increase sequentially from there.
791 This is so that:
792
793 * implementations may rely on the Vector CRs being aligned to 8. This
794 means that CRs may be read or written in aligned batches of 32 bits
795 (8 CRs per batch), for high performance implementations.
796 * scalar Rc=1 operation (CR0, CR1) and callee-saved CRs (CR2-4) are not
797 overwritten by vector Rc=1 operations except for very large VL
798 * CR-based predication, from CR32, is also not interfered with
799 (except by large VL).
800
801 However when the SV result (destination) is marked as a scalar by the
802 EXTRA field the *standard* v3.0B behaviour applies: the accompanying
803 CR when Rc=1 is written to. This is CR0 for integer operations and CR1
804 for FP operations.
805
806 Note that yes, the CRs are genuinely Vectorised. Unlike in SIMD VSX which
807 has a single CR (CR6) for a given SIMD result, SV Vectorised OpenPOWER
808 v3.0B scalar operations produce a **tuple** of element results: the
809 result of the operation as one part of that element *and a corresponding
810 CR element*. Greatly simplified pseudocode:
811
812 for i in range(VL):
813 # calculate the vector result of an add
814 iregs[RT+i] = iregs[RA+i] + iregs[RB+i]
815 # now calculate CR bits
816 CRs[8+i].eq = iregs[RT+i] == 0
817 CRs[8+i].gt = iregs[RT+i] > 0
818 ... etc
819
820 If a "cumulated" CR based analysis of results is desired (a la VSX CR6)
821 then a followup instruction must be performed, setting "reduce" mode on
822 the Vector of CRs, using cr ops (crand, crnor)to do so. This provides far
823 more flexibility in analysing vectors than standard Vector ISAs. Normal
824 Vector ISAs are typically restricted to "were all results nonzero" and
825 "were some results nonzero". The application of mapreduce to Vectorised
826 cr operations allows far more sophisticated analysis, particularly in
827 conjunction with the new crweird operations see [[sv/cr_int_predication]].
828
829 Note in particular that the use of a separate instruction in this way
830 ensures that high performance multi-issue OoO inplementations do not
831 have the computation of the cumulative analysis CR as a bottleneck and
832 hindrance, regardless of the length of VL.
833
834 (see [[discussion]]. some alternative schemes are described there)
835
836 ### Rc=1 when SUBVL!=1
837
838 sub-vectors are effectively a form of SIMD (length 2 to 4). Only 1 bit of predicate is allocated per subvector; likewise only one CR is allocated
839 per subvector.
840
841 This leaves a conundrum as to how to apply CR computation per subvector, when normally Rc=1 is exclusively applied to scalar elements. A solution is to perform a bitwise OR or AND of the subvector tests. Given that OE is ignored, rhis field may (when available) be used to select OR or AND behavior.
842
843 ### Table of CR fields
844
845 CR[i] is the notation used by the OpenPower spec to refer to CR field #i,
846 so FP instructions with Rc=1 write to CR[1] aka SVCR1_000.
847
848 CRs are not stored in SPRs: they are registers in their own right.
849 Therefore context-switching the full set of CRs involves a Vectorised
850 mfcr or mtcr, using VL=64, elwidth=8 to do so. This is exactly as how scalar OpenPOWER context-switches CRs: it is just that there are now more of them.
851
852 The 64 SV CRs are arranged similarly to the way the 128 integer registers
853 are arranged. TODO a python program that auto-generates a CSV file
854 which can be included in a table, which is in a new page (so as not to
855 overwhelm this one). [[svp64/cr_names]]
856
857
858
859 ## Register Profiles
860
861 **NOTE THIS TABLE SHOULD NO LONGER BE HAND EDITED** see
862 <https://bugs.libre-soc.org/show_bug.cgi?id=548> for details.
863
864 Instructions are broken down by Register Profiles as listed in the
865 following auto-generated page: [[opcode_regs_deduped]]. "Non-SV"
866 indicates that the operations with this Register Profile cannot be
867 Vectorised (mtspr, bc, dcbz, twi)
868
869 TODO generate table which will be here [[svp64/reg_profiles]]
870
871 ## Assembly Annotation
872
873 Assembly code annotation is required for SV to be able to successfully
874 mark instructions as "prefixed".
875
876 A reasonable (prototype) starting point:
877
878 svp64 [field=value]*
879
880 Fields:
881
882 * ew=8/16/32 - element width
883 * sew=8/16/32 - source element width
884 * vec=2/3/4 - SUBVL
885 * mode=reduce/satu/sats/crpred
886 * pred=1\<\<3/r3/~r3/r10/~r10/r30/~r30/lt/gt/le/ge/eq/ne
887 * spred={reg spec}
888