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