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