(no commit message)
[libreriscv.git] / simple_v_extension / specification.mdwn
1 # Simple-V (Parallelism Extension Proposal) Specification
2
3 * Copyright (C) 2017, 2018 Luke Kenneth Casson Leighton
4 * Status: DRAFTv0.4
5 * Last edited: 14 nov 2018
6 * Ancillary resource: [[opcodes]]
7
8 With thanks to:
9
10 * Allen Baum
11 * Bruce Hoult
12 * comp.arch
13 * Jacob Bachmeyer
14 * Guy Lemurieux
15 * Jacob Lifshay
16 * Terje Mathisen
17 * The RISC-V Founders, without whom this all would not be possible.
18
19 [[!toc ]]
20
21 # Summary and Background: Rationale
22
23 Simple-V is a uniform parallelism API for RISC-V hardware that has several
24 unplanned side-effects including code-size reduction, expansion of
25 HINT space and more. The reason for
26 creating it is to provide a manageable way to turn a pre-existing design
27 into a parallel one, in a step-by-step incremental fashion, allowing
28 the implementor to focus on adding hardware where it is needed and necessary.
29 The primary target is for mobile-class 3D GPUs and VPUs, with secondary
30 goals being to reduce executable size and reduce context-switch latency.
31
32 Critically: **No new instructions are added**. The parallelism (if any
33 is implemented) is implicitly added by tagging *standard* scalar registers
34 for redirection. When such a tagged register is used in any instruction,
35 it indicates that the PC shall **not** be incremented; instead a loop
36 is activated where *multiple* instructions are issued to the pipeline
37 (as determined by a length CSR), with contiguously incrementing register
38 numbers starting from the tagged register. When the last "element"
39 has been reached, only then is the PC permitted to move on. Thus
40 Simple-V effectively sits (slots) *in between* the instruction decode phase
41 and the ALU(s).
42
43 The barrier to entry with SV is therefore very low. The minimum
44 compliant implementation is software-emulation (traps), requiring
45 only the CSRs and CSR tables, and that an exception be thrown if an
46 instruction's registers are detected to have been tagged. The looping
47 that would otherwise be done in hardware is thus carried out in software,
48 instead. Whilst much slower, it is "compliant" with the SV specification,
49 and may be suited for implementation in RV32E and also in situations
50 where the implementor wishes to focus on certain aspects of SV, without
51 unnecessary time and resources into the silicon, whilst also conforming
52 strictly with the API. A good area to punt to software would be the
53 polymorphic element width capability for example.
54
55 Hardware Parallelism, if any, is therefore added at the implementor's
56 discretion to turn what would otherwise be a sequential loop into a
57 parallel one.
58
59 To emphasise that clearly: Simple-V (SV) is *not*:
60
61 * A SIMD system
62 * A SIMT system
63 * A Vectorisation Microarchitecture
64 * A microarchitecture of any specific kind
65 * A mandary parallel processor microarchitecture of any kind
66 * A supercomputer extension
67
68 SV does **not** tell implementors how or even if they should implement
69 parallelism: it is a hardware "API" (Application Programming Interface)
70 that, if implemented, presents a uniform and consistent way to *express*
71 parallelism, at the same time leaving the choice of if, how, how much,
72 when and whether to parallelise operations **entirely to the implementor**.
73
74 # Basic Operation
75
76 The principle of SV is as follows:
77
78 * CSRs indicating which registers are "tagged" as "vectorised"
79 (potentially parallel, depending on the microarchitecture)
80 must be set up
81 * A "Vector Length" CSR is set, indicating the span of any future
82 "parallel" operations.
83 * A **scalar** operation, just after the decode phase and before the
84 execution phase, checks the CSR register tables to see if any of
85 its registers have been marked as "vectorised"
86 * If so, a hardware "macro-unrolling loop" is activated, of length
87 VL, that effectively issues **multiple** identical instructions
88 using contiguous sequentially-incrementing registers.
89 **Whether they be executed sequentially or in parallel or a
90 mixture of both or punted to software-emulation in a trap handler
91 is entirely up to the implementor**.
92
93 In this way an entire scalar algorithm may be vectorised with
94 the minimum of modification to the hardware and to compiler toolchains.
95 There are **no** new opcodes.
96
97 # CSRs <a name="csrs"></a>
98
99 For U-Mode there are two CSR key-value stores needed to create lookup
100 tables which are used at the register decode phase.
101
102 * A register CSR key-value table (typically 8 32-bit CSRs of 2 16-bits each)
103 * A predication CSR key-value table (again, 8 32-bit CSRs of 2 16-bits each)
104 * Small U-Mode and S-Mode register and predication CSR key-value tables
105 (2 32-bit CSRs of 2x 16-bit entries each).
106 * An optional "reshaping" CSR key-value table which remaps from a 1D
107 linear shape to 2D or 3D, including full transposition.
108
109 There are also four additional CSRs for User-Mode:
110
111 * CFG subsets the CSR tables
112 * MVL (the Maximum Vector Length)
113 * VL (which has different characteristics from standard CSRs)
114 * STATE (useful for saving and restoring during context switch,
115 and for providing fast transitions)
116
117 There are also three additional CSRs for Supervisor-Mode:
118
119 * SMVL
120 * SVL
121 * SSTATE
122
123 And likewise for M-Mode:
124
125 * MMVL
126 * MVL
127 * MSTATE
128
129 Both Supervisor and M-Mode have their own (small) CSR register and
130 predication tables of only 4 entries each.
131
132 The access pattern for these groups of CSRs in each mode follows the
133 same pattern for other CSRs that have M-Mode and S-Mode "mirrors":
134
135 * In M-Mode, the S-Mode and U-Mode CSRs are separate and distinct.
136 * In S-Mode, accessing and changing of the M-Mode CSRs is identical
137 to changing the S-Mode CSRs. Accessing and changing the U-Mode
138 CSRs is permitted.
139 * In U-Mode, accessing and changing of the S-Mode and U-Mode CSRs
140 is prohibited.
141
142 In M-Mode, only the M-Mode CSRs are in effect, i.e. it is only the
143 M-Mode MVL, the M-Mode STATE and so on that influences the processor
144 behaviour. Likewise for S-Mode, and likewise for U-Mode.
145
146 This has the interesting benefit of allowing M-Mode (or S-Mode)
147 to be set up, for context-switching to take place, and, on return
148 back to the higher privileged mode, the CSRs of that mode will be
149 exactly as they were. Thus, it becomes possible for example to
150 set up CSRs suited best to aiding and assisting low-latency fast
151 context-switching *once and only once*, without the need for
152 re-initialising the CSRs needed to do so.
153
154 ## CFG
155
156 This CSR may be used to switch between subsets of the CSR Register and
157 Predication Tables: it is kept to 5 bits so that a single CSRRWI instruction
158 can be used. A setting of all ones is reserved to indicate that SimpleV
159 is disabled.
160
161 | (4..3) | (2...0) |
162 | ------ | ------- |
163 | size | bank |
164
165 Bank is 3 bits in size, and indicates the starting index of the CSR
166 Register and Predication Table entries that are "enabled". Given that
167 each CSR table row is 16 bits and contains 2 CAM entries each, there
168 are only 8 CSRs to cover in each table, so 8 bits is sufficient.
169
170 Size is 2 bits. With the exception of when bank == 7 and size == 3,
171 the number of elements enabled is taken by right-shifting 2 by size:
172
173 | size | elements |
174 | ------ | -------- |
175 | 0 | 2 |
176 | 1 | 4 |
177 | 2 | 8 |
178 | 3 | 16 |
179
180 Given that there are 2 16-bit CAM entries per CSR table row, this
181 may also be viewed as the number of CSR rows to enable, by raising size to
182 the power of 2.
183
184 Examples:
185
186 * When bank = 0 and size = 3, SVREGCFG0 through to SVREGCFG7 are
187 enabled, and SVPREDCFG0 through to SVPREGCFG7 are enabled.
188 * When bank = 1 and size = 3, SVREGCFG1 through to SVREGCFG7 are
189 enabled, and SVPREDCFG1 through to SVPREGCFG7 are enabled.
190 * When bank = 3 and size = 0, SVREGCFG3 and SVPREDCFG3 are enabled.
191 * When bank = 3 and size = 1, SVREGCFG3-4 and SVPREDCFG3-4 are enabled.
192 * When bank = 7 and size = 1, SVREGCFG7 and SVPREDCFG7 are enabled
193 (because there are only 8 32-bit CSRs there does not exist a
194 SVREGCFG8 or SVPREDCFG8 to enable).
195 * When bank = 7 and size = 3, SimpleV is entirely disabled.
196
197 In this way it is possible to enable and disable SimpleV with a
198 single instruction, and, furthermore, on context-switching the quantity
199 of CSRs to be saved and restored is greatly reduced.
200
201 ## MAXVECTORLENGTH (MVL) <a name="mvl" />
202
203 MAXVECTORLENGTH is the same concept as MVL in RVV, except that it
204 is variable length and may be dynamically set. MVL is
205 however limited to the regfile bitwidth XLEN (1-32 for RV32,
206 1-64 for RV64 and so on).
207
208 The reason for setting this limit is so that predication registers, when
209 marked as such, may fit into a single register as opposed to fanning out
210 over several registers. This keeps the implementation a little simpler.
211
212 The other important factor to note is that the actual MVL is **offset
213 by one**, so that it can fit into only 6 bits (for RV64) and still cover
214 a range up to XLEN bits. So, when setting the MVL CSR to 0, this actually
215 means that MVL==1. When setting the MVL CSR to 3, this actually means
216 that MVL==4, and so on. This is expressed more clearly in the "pseudocode"
217 section, where there are subtle differences between CSRRW and CSRRWI.
218
219 ## Vector Length (VL) <a name="vl" />
220
221 VSETVL is slightly different from RVV. Like RVV, VL is set to be within
222 the range 1 <= VL <= MVL (where MVL in turn is limited to 1 <= MVL <= XLEN)
223
224 VL = rd = MIN(vlen, MVL)
225
226 where 1 <= MVL <= XLEN
227
228 However just like MVL it is important to note that the range for VL has
229 subtle design implications, covered in the "CSR pseudocode" section
230
231 The fixed (specific) setting of VL allows vector LOAD/STORE to be used
232 to switch the entire bank of registers using a single instruction (see
233 Appendix, "Context Switch Example"). The reason for limiting VL to XLEN
234 is down to the fact that predication bits fit into a single register of
235 length XLEN bits.
236
237 The second change is that when VSETVL is requested to be stored
238 into x0, it is *ignored* silently (VSETVL x0, x5)
239
240 The third and most important change is that, within the limits set by
241 MVL, the value passed in **must** be set in VL (and in the
242 destination register).
243
244 This has implication for the microarchitecture, as VL is required to be
245 set (limits from MVL notwithstanding) to the actual value
246 requested. RVV has the option to set VL to an arbitrary value that suits
247 the conditions and the micro-architecture: SV does *not* permit this.
248
249 The reason is so that if SV is to be used for a context-switch or as a
250 substitute for LOAD/STORE-Multiple, the operation can be done with only
251 2-3 instructions (setup of the CSRs, VSETVL x0, x0, #{regfilelen-1},
252 single LD/ST operation). If VL does *not* get set to the register file
253 length when VSETVL is called, then a software-loop would be needed.
254 To avoid this need, VL *must* be set to exactly what is requested
255 (limits notwithstanding).
256
257 Therefore, in turn, unlike RVV, implementors *must* provide
258 pseudo-parallelism (using sequential loops in hardware) if actual
259 hardware-parallelism in the ALUs is not deployed. A hybrid is also
260 permitted (as used in Broadcom's VideoCore-IV) however this must be
261 *entirely* transparent to the ISA.
262
263 The fourth change is that VSETVL is implemented as a CSR, where the
264 behaviour of CSRRW (and CSRRWI) must be changed to specifically store
265 the *new* value in the destination register, **not** the old value.
266 Where context-load/save is to be implemented in the usual fashion
267 by using a single CSRRW instruction to obtain the old value, the
268 *secondary* CSR must be used (SVSTATE). This CSR behaves
269 exactly as standard CSRs, and contains more than just VL.
270
271 One interesting side-effect of using CSRRWI to set VL is that this
272 may be done with a single instruction, useful particularly for a
273 context-load/save. There are however limitations: CSRWI's immediate
274 is limited to 0-31 (representing VL=1-32).
275
276 Note that when VL is set to 1, all parallel operations cease: the
277 hardware loop is reduced to a single element: scalar operations.
278
279 ## STATE
280
281 This is a standard CSR that contains sufficient information for a
282 full context save/restore. It contains (and permits setting of)
283 MVL, VL, CFG, the destination element offset of the current parallel
284 instruction being executed, and, for twin-predication, the source
285 element offset as well. Interestingly it may hypothetically
286 also be used to make the immediately-following instruction to skip a
287 certain number of elements, however the recommended method to do
288 this is predication or using the offset mode of the REMAP CSRs.
289
290 Setting destoffs and srcoffs is realistically intended for saving state
291 so that exceptions (page faults in particular) may be serviced and the
292 hardware-loop that was being executed at the time of the trap, from
293 user-mode (or Supervisor-mode), may be returned to and continued from
294 where it left off. The reason why this works is because setting
295 User-Mode STATE will not change (not be used) in M-Mode or S-Mode
296 (and is entirely why M-Mode and S-Mode have their own STATE CSRs).
297
298 The format of the STATE CSR is as follows:
299
300 | (28..27) | (26..24) | (23..18) | (17..12) | (11..6) | (5...0) |
301 | -------- | -------- | -------- | -------- | ------- | ------- |
302 | size | bank | destoffs | srcoffs | vl | maxvl |
303
304 When setting this CSR, the following characteristics will be enforced:
305
306 * **MAXVL** will be truncated (after offset) to be within the range 1 to XLEN
307 * **VL** will be truncated (after offset) to be within the range 1 to MAXVL
308 * **srcoffs** will be truncated to be within the range 0 to VL-1
309 * **destoffs** will be truncated to be within the range 0 to VL-1
310
311 ## MVL, VL and CSR Pseudocode
312
313 The pseudo-code for get and set of VL and MVL are as follows:
314
315 set_mvl_csr(value, rd):
316 regs[rd] = MVL
317 MVL = MIN(value, MVL)
318
319 get_mvl_csr(rd):
320 regs[rd] = VL
321
322 set_vl_csr(value, rd):
323 VL = MIN(value, MVL)
324 regs[rd] = VL # yes returning the new value NOT the old CSR
325
326 get_vl_csr(rd):
327 regs[rd] = VL
328
329 Note that where setting MVL behaves as a normal CSR, unlike standard CSR
330 behaviour, setting VL will return the **new** value of VL **not** the old
331 one.
332
333 For CSRRWI, the range of the immediate is restricted to 5 bits. In order to
334 maximise the effectiveness, an immediate of 0 is used to set VL=1,
335 an immediate of 1 is used to set VL=2 and so on:
336
337 CSRRWI_Set_MVL(value):
338 set_mvl_csr(value+1, x0)
339
340 CSRRWI_Set_VL(value):
341 set_vl_csr(value+1, x0)
342
343 However for CSRRW the following pseudocide is used for MVL and VL,
344 where setting the value to zero will cause an exception to be raised.
345 The reason is that if VL or MVL are set to zero, the STATE CSR is
346 not capable of returning that value.
347
348 CSRRW_Set_MVL(rs1, rd):
349 value = regs[rs1]
350 if value == 0:
351 raise Exception
352 set_mvl_csr(value, rd)
353
354 CSRRW_Set_VL(rs1, rd):
355 value = regs[rs1]
356 if value == 0:
357 raise Exception
358 set_vl_csr(value, rd)
359
360 In this way, when CSRRW is utilised with a loop variable, the value
361 that goes into VL (and into the destination register) may be used
362 in an instruction-minimal fashion:
363
364 CSRvect1 = {type: F, key: a3, val: a3, elwidth: dflt}
365 CSRvect2 = {type: F, key: a7, val: a7, elwidth: dflt}
366 CSRRWI MVL, 3 # sets MVL == **4** (not 3)
367 j zerotest # in case loop counter a0 already 0
368 loop:
369 CSRRW VL, t0, a0 # vl = t0 = min(mvl, a0)
370 ld a3, a1 # load 4 registers a3-6 from x
371 slli t1, t0, 3 # t1 = vl * 8 (in bytes)
372 ld a7, a2 # load 4 registers a7-10 from y
373 add a1, a1, t1 # increment pointer to x by vl*8
374 fmadd a7, a3, fa0, a7 # v1 += v0 * fa0 (y = a * x + y)
375 sub a0, a0, t0 # n -= vl (t0)
376 st a7, a2 # store 4 registers a7-10 to y
377 add a2, a2, t1 # increment pointer to y by vl*8
378 zerotest:
379 bnez a0, loop # repeat if n != 0
380
381 With the STATE CSR, just like with CSRRWI, in order to maximise the
382 utilisation of the limited bitspace, "000000" in binary represents
383 VL==1, "00001" represents VL==2 and so on (likewise for MVL):
384
385 CSRRW_Set_SV_STATE(rs1, rd):
386 value = regs[rs1]
387 get_state_csr(rd)
388 MVL = set_mvl_csr(value[11:6]+1)
389 VL = set_vl_csr(value[5:0]+1)
390 CFG = value[28:24]>>24
391 destoffs = value[23:18]>>18
392 srcoffs = value[23:18]>>12
393
394 get_state_csr(rd):
395 regs[rd] = (MVL-1) | (VL-1)<<6 | (srcoffs)<<12 |
396 (destoffs)<<18 | (CFG)<<24
397 return regs[rd]
398
399 In both cases, whilst CSR read of VL and MVL return the exact values
400 of VL and MVL respectively, reading and writing the STATE CSR returns
401 those values **minus one**. This is absolutely critical to implement
402 if the STATE CSR is to be used for fast context-switching.
403
404 ## Register CSR key-value (CAM) table <a name="regcsrtable" />
405
406 The purpose of the Register CSR table is four-fold:
407
408 * To mark integer and floating-point registers as requiring "redirection"
409 if it is ever used as a source or destination in any given operation.
410 This involves a level of indirection through a 5-to-7-bit lookup table,
411 such that **unmodified** operands with 5 bit (3 for Compressed) may
412 access up to **128** registers.
413 * To indicate whether, after redirection through the lookup table, the
414 register is a vector (or remains a scalar).
415 * To over-ride the implicit or explicit bitwidth that the operation would
416 normally give the register.
417
418 16 bit format:
419
420 | RegCAM | | 15 | (14..8) | 7 | (6..5) | (4..0) |
421 | ------ | | - | - | - | ------ | ------- |
422 | 0 | | isvec0 | regidx0 | i/f | vew0 | regkey |
423 | 1 | | isvec1 | regidx1 | i/f | vew1 | regkey |
424 | .. | | isvec.. | regidx.. | i/f | vew.. | regkey |
425 | 15 | | isvec15 | regidx15 | i/f | vew15 | regkey |
426
427 8 bit format:
428
429 | RegCAM | | 7 | (6..5) | (4..0) |
430 | ------ | | - | ------ | ------- |
431 | 0 | | i/f | vew0 | regnum |
432
433 i/f is set to "1" to indicate that the redirection/tag entry is to be applied
434 to integer registers; 0 indicates that it is relevant to floating-point
435 registers.
436
437 The 8 bit format is used for a much more compact expression. "isvec" is implicit and, as in [[sv-prefix-proposal]], the target vector is "regnum<<2", implicitly. Contrast this with the 16-bit format where the target vector is *explicitly* named in bits 8 to 14, and bit 15 may optionally set "scalar" mode.
438
439 vew has the following meanings, indicating that the instruction's
440 operand size is "over-ridden" in a polymorphic fashion:
441
442 | vew | bitwidth |
443 | --- | ------------------- |
444 | 00 | default (XLEN/FLEN) |
445 | 01 | 8 bit |
446 | 10 | 16 bit |
447 | 11 | 32 bit |
448
449 As the above table is a CAM (key-value store) it may be appropriate
450 (faster, implementation-wise) to expand it as follows:
451
452 struct vectorised fp_vec[32], int_vec[32];
453
454 for (i = 0; i < 16; i++) // 16 CSRs?
455 tb = int_vec if CSRvec[i].type == 0 else fp_vec
456 idx = CSRvec[i].regkey // INT/FP src/dst reg in opcode
457 tb[idx].elwidth = CSRvec[i].elwidth
458 tb[idx].regidx = CSRvec[i].regidx // indirection
459 tb[idx].isvector = CSRvec[i].isvector // 0=scalar
460 tb[idx].packed = CSRvec[i].packed // SIMD or not
461
462 The actual size of the CSR Register table depends on the platform
463 and on whether other Extensions are present (RV64G, RV32E, etc.).
464 For details see "Subsets" section.
465
466 There are two CSRs (per privilege level) for adding to and removing
467 entries from the table, which, conceptually may be viewed as either
468 a register window (similar to SPARC) or as the "top of a stack".
469
470 * SVREGTOP will push or pop entries onto the top of the "stack"
471 (highest non-zero indexed entry in the table)
472 * SVREGBOT will push or pop entries from the bottom (always
473 element indexed as zero.
474
475 In addition, note that CSRRWI behaviour is completely different
476 from CSRRW when writing to these two CSR registers. The CSRRW
477 behaviour: the src register is subdivided into 16-bit chunks,
478 and each non-zero chunk is pushed/popped separately. The
479 CSRRWI behaviour: the immediate indicates the number of
480 entries in the table to be popped.
481
482 CSRRWI:
483
484 * The src register indicates how many entries to pop from the
485 CAM table.
486 * "CSRRWI SVREGTOP, 3" indicates that the top 3
487 entries are to be zero'd and returned as the CSR return
488 result. The top entry is returned in bits 0-15, the
489 next entry down in bits 16-31, and when XLEN==64, an
490 extra 2 entries are also returned.
491 * "CSRRWI SVREGBOT, 3" indicates that the bottom 3 entries are
492 to be returned, and the entries with indices above 3 are
493 to be shuffled down. The first entry to be popped off the
494 bottom is returned in bits 0-15, the second entry as bits
495 16-31 and so on.
496 * If XLEN==32, only a maximum of 2 entries may be returned
497 (and shuffled). If XLEN==64, only a maximum of 4 entries
498 may be returned
499 * If however the destination register is x0 (zero), then
500 the exact number of entries requested will be removed
501 (shuffled down).
502
503 CSRRW when src == 0:
504
505 * When the src register is all zeros, this is a request to
506 pop one and only one 16-bit element from the table.
507 * "CSRRW SVREGTOP, 0" will return (and clear) the highest
508 non-zero 16-bit entry in the table
509 * "CSRRW SVREGBOT, 0" will return (and clear) the zero'th
510 16-bit entry in the table, and will shuffle down all
511 other entries (if any) by one index.
512
513 CSRRW when src != 0:
514
515 All other CSRRW behaviours are a "loop", taking 16-bits
516 at a time from the src register. Obviously, for XLEN=32
517 that can only be up to 2 16-bit entries, however for XLEN=64
518 it can be up to 4.
519
520 * When the src 16-bit chunk is non-zero and there already exists
521 an entry with the exact same "regkey" (bits 0-4), the
522 entry is **updated**. No other modifications are made.
523 * When the 16-bit chunk is non-zero and there does not exist
524 an entry, the new value will be placed at the end
525 (in the highest non-zero slot), or at the beginning
526 (shuffling up all other entries to make room).
527 * If there is not enough room, the entry at the opposite
528 end will become part of the CSR return result.
529 * The process is repeated for the next 16-bit chunk (starting
530 with bits 0-15 and moving next to 16-31 and so on), until
531 the limit of XLEN is reached or a chunk is all-zeros, at
532 which point the looping stops.
533 * Any 16-bit entries that are pushed out of the stack
534 (from either end) are concatenated in order (first entry
535 pushed out is bits 0-15 of the return result).
536
537 What this behaviour basically does is allow the CAM table to
538 effectively be like the top entries of a stack. Entries that
539 get returned from CSRRW SVREGTOP can be *actually* stored on the stack,
540 such that after a function call exits, CSRRWI SVREGTOP may be used
541 to delete the callee's CAM entries, and the caller's entries may then
542 be pushed *back*, using CSRRW SVREGBOT.
543
544 Context-switching may be carried out in a loop, where CSRRWI may
545 be called to "pop" values that are tested for being non-zero, and
546 transferred onto the stack with C.SWSP using only around 4-5 instructions.
547 CSRRW may then be used in combination with C.LWSP to get the CAM entries
548 off the stack and back into the CAM table, again with a loop using
549 only around 4-5 instructions.
550
551 Contrast this with needing around 6-7 instructions (8-9 without SV on
552 RV64, and 16-17 on RV32) to do a context-switch of fixed-address CSRs:
553 a sequence of fixed-address C.LWSP with fixed offsets plus fixed-address
554 CSRRWs, and that is without testing if any of the entries are zero
555 or not.
556
557 ## Predication CSR <a name="predication_csr_table"></a>
558
559 TODO: update CSR tables, now 7-bit for regidx
560
561 The Predication CSR is a key-value store indicating whether, if a given
562 destination register (integer or floating-point) is referred to in an
563 instruction, it is to be predicated. Tt is particularly important to note
564 that the *actual* register used can be *different* from the one that is
565 in the instruction, due to the redirection through the lookup table.
566
567 * regidx is the actual register that in combination with the
568 i/f flag, if that integer or floating-point register is referred to,
569 results in the lookup table being referenced to find the predication
570 mask to use on the operation in which that (regidx) register has
571 been used
572 * predidx (in combination with the bank bit in the future) is the
573 *actual* register to be used for the predication mask. Note:
574 in effect predidx is actually a 6-bit register address, as the bank
575 bit is the MSB (and is nominally set to zero for now).
576 * inv indicates that the predication mask bits are to be inverted
577 prior to use *without* actually modifying the contents of the
578 register itself.
579 * zeroing is either 1 or 0, and if set to 1, the operation must
580 place zeros in any element position where the predication mask is
581 set to zero. If zeroing is set to 0, unpredicated elements *must*
582 be left alone. Some microarchitectures may choose to interpret
583 this as skipping the operation entirely. Others which wish to
584 stick more closely to a SIMD architecture may choose instead to
585 interpret unpredicated elements as an internal "copy element"
586 operation (which would be necessary in SIMD microarchitectures
587 that perform register-renaming)
588 * "packed" indicates if the register is to be interpreted as SIMD
589 i.e. containing multiple contiguous elements of size equal to "bitwidth".
590 (Note: in earlier drafts this was in the Register CSR table.
591 However after extending to 7 bits there was not enough space.
592 To use "unpredicated" packed SIMD, set the predicate to x0 and
593 set "invert". This has the effect of setting a predicate of all 1s)
594
595 | PrCSR | (15..11) | 10 | 9 | 8 | (7..1) | 0 |
596 | ----- | - | - | - | - | ------- | ------- |
597 | 0 | predkey | zero0 | inv0 | i/f | regidx | packed0 |
598 | 1 | predkey | zero1 | inv1 | i/f | regidx | packed1 |
599 | ... | predkey | ..... | .... | i/f | ....... | ....... |
600 | 15 | predkey | zero15 | inv15 | i/f | regidx | packed15|
601
602 The Predication CSR Table is a key-value store, so implementation-wise
603 it will be faster to turn the table around (maintain topologically
604 equivalent state):
605
606 struct pred {
607 bool zero;
608 bool inv;
609 bool enabled;
610 int predidx; // redirection: actual int register to use
611 }
612
613 struct pred fp_pred_reg[32]; // 64 in future (bank=1)
614 struct pred int_pred_reg[32]; // 64 in future (bank=1)
615
616 for (i = 0; i < 16; i++)
617 tb = int_pred_reg if CSRpred[i].type == 0 else fp_pred_reg;
618 idx = CSRpred[i].regidx
619 tb[idx].zero = CSRpred[i].zero
620 tb[idx].inv = CSRpred[i].inv
621 tb[idx].predidx = CSRpred[i].predidx
622 tb[idx].enabled = true
623
624 So when an operation is to be predicated, it is the internal state that
625 is used. In Section 6.4.2 of Hwacha's Manual (EECS-2015-262) the following
626 pseudo-code for operations is given, where p is the explicit (direct)
627 reference to the predication register to be used:
628
629 for (int i=0; i<vl; ++i)
630 if ([!]preg[p][i])
631 (d ? vreg[rd][i] : sreg[rd]) =
632 iop(s1 ? vreg[rs1][i] : sreg[rs1],
633 s2 ? vreg[rs2][i] : sreg[rs2]); // for insts with 2 inputs
634
635 This instead becomes an *indirect* reference using the *internal* state
636 table generated from the Predication CSR key-value store, which iwws used
637 as follows.
638
639 if type(iop) == INT:
640 preg = int_pred_reg[rd]
641 else:
642 preg = fp_pred_reg[rd]
643
644 for (int i=0; i<vl; ++i)
645 predicate, zeroing = get_pred_val(type(iop) == INT, rd):
646 if (predicate && (1<<i))
647 (d ? regfile[rd+i] : regfile[rd]) =
648 iop(s1 ? regfile[rs1+i] : regfile[rs1],
649 s2 ? regfile[rs2+i] : regfile[rs2]); // for insts with 2 inputs
650 else if (zeroing)
651 (d ? regfile[rd+i] : regfile[rd]) = 0
652
653 Note:
654
655 * d, s1 and s2 are booleans indicating whether destination,
656 source1 and source2 are vector or scalar
657 * key-value CSR-redirection of rd, rs1 and rs2 have NOT been included
658 above, for clarity. rd, rs1 and rs2 all also must ALSO go through
659 register-level redirection (from the Register CSR table) if they are
660 vectors.
661
662 If written as a function, obtaining the predication mask (and whether
663 zeroing takes place) may be done as follows:
664
665 def get_pred_val(bool is_fp_op, int reg):
666 tb = int_reg if is_fp_op else fp_reg
667 if (!tb[reg].enabled):
668 return ~0x0, False // all enabled; no zeroing
669 tb = int_pred if is_fp_op else fp_pred
670 if (!tb[reg].enabled):
671 return ~0x0, False // all enabled; no zeroing
672 predidx = tb[reg].predidx // redirection occurs HERE
673 predicate = intreg[predidx] // actual predicate HERE
674 if (tb[reg].inv):
675 predicate = ~predicate // invert ALL bits
676 return predicate, tb[reg].zero
677
678 Note here, critically, that **only** if the register is marked
679 in its CSR **register** table entry as being "active" does the testing
680 proceed further to check if the CSR **predicate** table entry is
681 also active.
682
683 Note also that this is in direct contrast to branch operations
684 for the storage of comparisions: in these specific circumstances
685 the requirement for there to be an active CSR *register* entry
686 is removed.
687
688 ## REMAP CSR <a name="remap" />
689
690 (Note: both the REMAP and SHAPE sections are best read after the
691 rest of the document has been read)
692
693 There is one 32-bit CSR which may be used to indicate which registers,
694 if used in any operation, must be "reshaped" (re-mapped) from a linear
695 form to a 2D or 3D transposed form, or "offset" to permit arbitrary
696 access to elements within a register.
697
698 The 32-bit REMAP CSR may reshape up to 3 registers:
699
700 | 29..28 | 27..26 | 25..24 | 23 | 22..16 | 15 | 14..8 | 7 | 6..0 |
701 | ------ | ------ | ------ | -- | ------- | -- | ------- | -- | ------- |
702 | shape2 | shape1 | shape0 | 0 | regidx2 | 0 | regidx1 | 0 | regidx0 |
703
704 regidx0-2 refer not to the Register CSR CAM entry but to the underlying
705 *real* register (see regidx, the value) and consequently is 7-bits wide.
706 When set to zero (referring to x0), clearly reshaping x0 is pointless,
707 so is used to indicate "disabled".
708 shape0-2 refers to one of three SHAPE CSRs. A value of 0x3 is reserved.
709 Bits 7, 15, 23, 30 and 31 are also reserved, and must be set to zero.
710
711 It is anticipated that these specialist CSRs not be very often used.
712 Unlike the CSR Register and Predication tables, the REMAP CSRs use
713 the full 7-bit regidx so that they can be set once and left alone,
714 whilst the CSR Register entries pointing to them are disabled, instead.
715
716 ## SHAPE 1D/2D/3D vector-matrix remapping CSRs
717
718 (Note: both the REMAP and SHAPE sections are best read after the
719 rest of the document has been read)
720
721 There are three "shape" CSRs, SHAPE0, SHAPE1, SHAPE2, 32-bits in each,
722 which have the same format. When each SHAPE CSR is set entirely to zeros,
723 remapping is disabled: the register's elements are a linear (1D) vector.
724
725 | 26..24 | 23 | 22..16 | 15 | 14..8 | 7 | 6..0 |
726 | ------- | -- | ------- | -- | ------- | -- | ------- |
727 | permute | offs[2] | zdimsz | offs[1] | ydimsz | offs[0] | xdimsz |
728
729 offs is a 3-bit field, spread out across bits 7, 15 and 23, which
730 is added to the element index during the loop calculation.
731
732 xdimsz, ydimsz and zdimsz are offset by 1, such that a value of 0 indicates
733 that the array dimensionality for that dimension is 1. A value of xdimsz=2
734 would indicate that in the first dimension there are 3 elements in the
735 array. The format of the array is therefore as follows:
736
737 array[xdim+1][ydim+1][zdim+1]
738
739 However whilst illustrative of the dimensionality, that does not take the
740 "permute" setting into account. "permute" may be any one of six values
741 (0-5, with values of 6 and 7 being reserved, and not legal). The table
742 below shows how the permutation dimensionality order works:
743
744 | permute | order | array format |
745 | ------- | ----- | ------------------------ |
746 | 000 | 0,1,2 | (xdim+1)(ydim+1)(zdim+1) |
747 | 001 | 0,2,1 | (xdim+1)(zdim+1)(ydim+1) |
748 | 010 | 1,0,2 | (ydim+1)(xdim+1)(zdim+1) |
749 | 011 | 1,2,0 | (ydim+1)(zdim+1)(xdim+1) |
750 | 100 | 2,0,1 | (zdim+1)(xdim+1)(ydim+1) |
751 | 101 | 2,1,0 | (zdim+1)(ydim+1)(xdim+1) |
752
753 In other words, the "permute" option changes the order in which
754 nested for-loops over the array would be done. The algorithm below
755 shows this more clearly, and may be executed as a python program:
756
757 # mapidx = REMAP.shape2
758 xdim = 3 # SHAPE[mapidx].xdim_sz+1
759 ydim = 4 # SHAPE[mapidx].ydim_sz+1
760 zdim = 5 # SHAPE[mapidx].zdim_sz+1
761
762 lims = [xdim, ydim, zdim]
763 idxs = [0,0,0] # starting indices
764 order = [1,0,2] # experiment with different permutations, here
765 offs = 0 # experiment with different offsets, here
766
767 for idx in range(xdim * ydim * zdim):
768 new_idx = offs + idxs[0] + idxs[1] * xdim + idxs[2] * xdim * ydim
769 print new_idx,
770 for i in range(3):
771 idxs[order[i]] = idxs[order[i]] + 1
772 if (idxs[order[i]] != lims[order[i]]):
773 break
774 print
775 idxs[order[i]] = 0
776
777 Here, it is assumed that this algorithm be run within all pseudo-code
778 throughout this document where a (parallelism) for-loop would normally
779 run from 0 to VL-1 to refer to contiguous register
780 elements; instead, where REMAP indicates to do so, the element index
781 is run through the above algorithm to work out the **actual** element
782 index, instead. Given that there are three possible SHAPE entries, up to
783 three separate registers in any given operation may be simultaneously
784 remapped:
785
786 function op_add(rd, rs1, rs2) # add not VADD!
787 ...
788 ...
789  for (i = 0; i < VL; i++)
790 if (predval & 1<<i) # predication uses intregs
791    ireg[rd+remap(id)] <= ireg[rs1+remap(irs1)] +
792 ireg[rs2+remap(irs2)];
793 if (!int_vec[rd ].isvector) break;
794 if (int_vec[rd ].isvector)  { id += 1; }
795 if (int_vec[rs1].isvector)  { irs1 += 1; }
796 if (int_vec[rs2].isvector)  { irs2 += 1; }
797
798 By changing remappings, 2D matrices may be transposed "in-place" for one
799 operation, followed by setting a different permutation order without
800 having to move the values in the registers to or from memory. Also,
801 the reason for having REMAP separate from the three SHAPE CSRs is so
802 that in a chain of matrix multiplications and additions, for example,
803 the SHAPE CSRs need only be set up once; only the REMAP CSR need be
804 changed to target different registers.
805
806 Note that:
807
808 * Over-running the register file clearly has to be detected and
809 an illegal instruction exception thrown
810 * When non-default elwidths are set, the exact same algorithm still
811 applies (i.e. it offsets elements *within* registers rather than
812 entire registers).
813 * If permute option 000 is utilised, the actual order of the
814 reindexing does not change!
815 * If two or more dimensions are set to zero, the actual order does not change!
816 * The above algorithm is pseudo-code **only**. Actual implementations
817 will need to take into account the fact that the element for-looping
818 must be **re-entrant**, due to the possibility of exceptions occurring.
819 See MSTATE CSR, which records the current element index.
820 * Twin-predicated operations require **two** separate and distinct
821 element offsets. The above pseudo-code algorithm will be applied
822 separately and independently to each, should each of the two
823 operands be remapped. *This even includes C.LDSP* and other operations
824 in that category, where in that case it will be the **offset** that is
825 remapped (see Compressed Stack LOAD/STORE section).
826 * Offset is especially useful, on its own, for accessing elements
827 within the middle of a register. Without offsets, it is necessary
828 to either use a predicated MV, skipping the first elements, or
829 performing a LOAD/STORE cycle to memory.
830 With offsets, the data does not have to be moved.
831 * Setting the total elements (xdim+1) times (ydim+1) times (zdim+1) to
832 less than MVL is **perfectly legal**, albeit very obscure. It permits
833 entries to be regularly presented to operands **more than once**, thus
834 allowing the same underlying registers to act as an accumulator of
835 multiple vector or matrix operations, for example.
836
837 Clearly here some considerable care needs to be taken as the remapping
838 could hypothetically create arithmetic operations that target the
839 exact same underlying registers, resulting in data corruption due to
840 pipeline overlaps. Out-of-order / Superscalar micro-architectures with
841 register-renaming will have an easier time dealing with this than
842 DSP-style SIMD micro-architectures.
843
844 # Instruction Execution Order
845
846 Simple-V behaves as if it is a hardware-level "macro expansion system",
847 substituting and expanding a single instruction into multiple sequential
848 instructions with contiguous and sequentially-incrementing registers.
849 As such, it does **not** modify - or specify - the behaviour and semantics of
850 the execution order: that may be deduced from the **existing** RV
851 specification in each and every case.
852
853 So for example if a particular micro-architecture permits out-of-order
854 execution, and it is augmented with Simple-V, then wherever instructions
855 may be out-of-order then so may the "post-expansion" SV ones.
856
857 If on the other hand there are memory guarantees which specifically
858 prevent and prohibit certain instructions from being re-ordered
859 (such as the Atomicity Axiom, or FENCE constraints), then clearly
860 those constraints **MUST** also be obeyed "post-expansion".
861
862 It should be absolutely clear that SV is **not** about providing new
863 functionality or changing the existing behaviour of a micro-architetural
864 design, or about changing the RISC-V Specification.
865 It is **purely** about compacting what would otherwise be contiguous
866 instructions that use sequentially-increasing register numbers down
867 to the **one** instruction.
868
869 # Instructions <a name="instructions" />
870
871 Despite being a 98% complete and accurate topological remap of RVV
872 concepts and functionality, no new instructions are needed.
873 Compared to RVV: *All* RVV instructions can be re-mapped, however xBitManip
874 becomes a critical dependency for efficient manipulation of predication
875 masks (as a bit-field). Despite the removal of all operations,
876 with the exception of CLIP and VSELECT.X
877 *all instructions from RVV Base are topologically re-mapped and retain their
878 complete functionality, intact*. Note that if RV64G ever had
879 a MV.X added as well as FCLIP, the full functionality of RVV-Base would
880 be obtained in SV.
881
882 Three instructions, VSELECT, VCLIP and VCLIPI, do not have RV Standard
883 equivalents, so are left out of Simple-V. VSELECT could be included if
884 there existed a MV.X instruction in RV (MV.X is a hypothetical
885 non-immediate variant of MV that would allow another register to
886 specify which register was to be copied). Note that if any of these three
887 instructions are added to any given RV extension, their functionality
888 will be inherently parallelised.
889
890 With some exceptions, where it does not make sense or is simply too
891 challenging, all RV-Base instructions are parallelised:
892
893 * CSR instructions, whilst a case could be made for fast-polling of
894 a CSR into multiple registers, or for being able to copy multiple
895 contiguously addressed CSRs into contiguous registers, and so on,
896 are the fundamental core basis of SV. If parallelised, extreme
897 care would need to be taken. Additionally, CSR reads are done
898 using x0, and it is *really* inadviseable to tag x0.
899 * LUI, C.J, C.JR, WFI, AUIPC are not suitable for parallelising so are
900 left as scalar.
901 * LR/SC could hypothetically be parallelised however their purpose is
902 single (complex) atomic memory operations where the LR must be followed
903 up by a matching SC. A sequence of parallel LR instructions followed
904 by a sequence of parallel SC instructions therefore is guaranteed to
905 not be useful. Not least: the guarantees of a Multi-LR/SC
906 would be impossible to provide if emulated in a trap.
907 * EBREAK, NOP, FENCE and others do not use registers so are not inherently
908 paralleliseable anyway.
909
910 All other operations using registers are automatically parallelised.
911 This includes AMOMAX, AMOSWAP and so on, where particular care and
912 attention must be paid.
913
914 Example pseudo-code for an integer ADD operation (including scalar operations).
915 Floating-point uses fp csrs.
916
917 function op_add(rd, rs1, rs2) # add not VADD!
918  int i, id=0, irs1=0, irs2=0;
919  predval = get_pred_val(FALSE, rd);
920  rd = int_vec[rd ].isvector ? int_vec[rd ].regidx : rd;
921  rs1 = int_vec[rs1].isvector ? int_vec[rs1].regidx : rs1;
922  rs2 = int_vec[rs2].isvector ? int_vec[rs2].regidx : rs2;
923  for (i = 0; i < VL; i++)
924 if (predval & 1<<i) # predication uses intregs
925    ireg[rd+id] <= ireg[rs1+irs1] + ireg[rs2+irs2];
926 if (!int_vec[rd ].isvector) break;
927 if (int_vec[rd ].isvector)  { id += 1; }
928 if (int_vec[rs1].isvector)  { irs1 += 1; }
929 if (int_vec[rs2].isvector)  { irs2 += 1; }
930
931 Note that for simplicity there is quite a lot missing from the above
932 pseudo-code: element widths, zeroing on predication, dimensional
933 reshaping and offsets and so on. However it demonstrates the basic
934 principle. Augmentations that produce the full pseudo-code are covered in
935 other sections.
936
937 ## Instruction Format
938
939 It is critical to appreciate that there are
940 **no operations added to SV, at all**.
941
942 Instead, by using CSRs to tag registers as an indication of "changed behaviour",
943 SV *overloads* pre-existing branch operations into predicated
944 variants, and implicitly overloads arithmetic operations, MV,
945 FCVT, and LOAD/STORE depending on CSR configurations for bitwidth
946 and predication. **Everything** becomes parallelised. *This includes
947 Compressed instructions* as well as any future instructions and Custom
948 Extensions.
949
950 Note: CSR tags to change behaviour of instructions is nothing new, including
951 in RISC-V. UXL, SXL and MXL change the behaviour so that XLEN=32/64/128.
952 FRM changes the behaviour of the floating-point unit, to alter the rounding
953 mode. Other architectures change the LOAD/STORE byte-order from big-endian
954 to little-endian on a per-instruction basis. SV is just a little more...
955 comprehensive in its effect on instructions.
956
957 ## Branch Instructions
958
959 ### Standard Branch <a name="standard_branch"></a>
960
961 Branch operations use standard RV opcodes that are reinterpreted to
962 be "predicate variants" in the instance where either of the two src
963 registers are marked as vectors (active=1, vector=1).
964
965 Note that the predication register to use (if one is enabled) is taken from
966 the *first* src register, and that this is used, just as with predicated
967 arithmetic operations, to mask whether the comparison operations take
968 place or not. The target (destination) predication register
969 to use (if one is enabled) is taken from the *second* src register.
970
971 If either of src1 or src2 are scalars (whether by there being no
972 CSR register entry or whether by the CSR entry specifically marking
973 the register as "scalar") the comparison goes ahead as vector-scalar
974 or scalar-vector.
975
976 In instances where no vectorisation is detected on either src registers
977 the operation is treated as an absolutely standard scalar branch operation.
978 Where vectorisation is present on either or both src registers, the
979 branch may stil go ahead if any only if *all* tests succeed (i.e. excluding
980 those tests that are predicated out).
981
982 Note that when zero-predication is enabled (from source rs1),
983 a cleared bit in the predicate indicates that the result
984 of the compare is set to "false", i.e. that the corresponding
985 destination bit (or result)) be set to zero. Contrast this with
986 when zeroing is not set: bits in the destination predicate are
987 only *set*; they are **not** cleared. This is important to appreciate,
988 as there may be an expectation that, going into the hardware-loop,
989 the destination predicate is always expected to be set to zero:
990 this is **not** the case. The destination predicate is only set
991 to zero if **zeroing** is enabled.
992
993 Note that just as with the standard (scalar, non-predicated) branch
994 operations, BLE, BGT, BLEU and BTGU may be synthesised by inverting
995 src1 and src2.
996
997 In Hwacha EECS-2015-262 Section 6.7.2 the following pseudocode is given
998 for predicated compare operations of function "cmp":
999
1000 for (int i=0; i<vl; ++i)
1001 if ([!]preg[p][i])
1002 preg[pd][i] = cmp(s1 ? vreg[rs1][i] : sreg[rs1],
1003 s2 ? vreg[rs2][i] : sreg[rs2]);
1004
1005 With associated predication, vector-length adjustments and so on,
1006 and temporarily ignoring bitwidth (which makes the comparisons more
1007 complex), this becomes:
1008
1009 s1 = reg_is_vectorised(src1);
1010 s2 = reg_is_vectorised(src2);
1011
1012 if not s1 && not s2
1013 if cmp(rs1, rs2) # scalar compare
1014 goto branch
1015 return
1016
1017 preg = int_pred_reg[rd]
1018 reg = int_regfile
1019
1020 ps = get_pred_val(I/F==INT, rs1);
1021 rd = get_pred_val(I/F==INT, rs2); # this may not exist
1022
1023 if not exists(rd) or zeroing:
1024 result = 0
1025 else
1026 result = preg[rd]
1027
1028 for (int i = 0; i < VL; ++i)
1029 if (zeroing)
1030 if not (ps & (1<<i))
1031 result &= ~(1<<i);
1032 else if (ps & (1<<i))
1033 if (cmp(s1 ? reg[src1+i]:reg[src1],
1034 s2 ? reg[src2+i]:reg[src2])
1035 result |= 1<<i;
1036 else
1037 result &= ~(1<<i);
1038
1039 if not exists(rd)
1040 if result == ps
1041 goto branch
1042 else
1043 preg[rd] = result # store in destination
1044 if preg[rd] == ps
1045 goto branch
1046
1047 Notes:
1048
1049 * Predicated SIMD comparisons would break src1 and src2 further down
1050 into bitwidth-sized chunks (see Appendix "Bitwidth Virtual Register
1051 Reordering") setting Vector-Length times (number of SIMD elements) bits
1052 in Predicate Register rd, as opposed to just Vector-Length bits.
1053 * The execution of "parallelised" instructions **must** be implemented
1054 as "re-entrant" (to use a term from software). If an exception (trap)
1055 occurs during the middle of a vectorised
1056 Branch (now a SV predicated compare) operation, the partial results
1057 of any comparisons must be written out to the destination
1058 register before the trap is permitted to begin. If however there
1059 is no predicate, the **entire** set of comparisons must be **restarted**,
1060 with the offset loop indices set back to zero. This is because
1061 there is no place to store the temporary result during the handling
1062 of traps.
1063
1064 TODO: predication now taken from src2. also branch goes ahead
1065 if all compares are successful.
1066
1067 Note also that where normally, predication requires that there must
1068 also be a CSR register entry for the register being used in order
1069 for the **predication** CSR register entry to also be active,
1070 for branches this is **not** the case. src2 does **not** have
1071 to have its CSR register entry marked as active in order for
1072 predication on src2 to be active.
1073
1074 Also note: SV Branch operations are **not** twin-predicated
1075 (see Twin Predication section). This would require three
1076 element offsets: one to track src1, one to track src2 and a third
1077 to track where to store the accumulation of the results. Given
1078 that the element offsets need to be exposed via CSRs so that
1079 the parallel hardware looping may be made re-entrant on traps
1080 and exceptions, the decision was made not to make SV Branches
1081 twin-predicated.
1082
1083 ### Floating-point Comparisons
1084
1085 There does not exist floating-point branch operations, only compare.
1086 Interestingly no change is needed to the instruction format because
1087 FP Compare already stores a 1 or a zero in its "rd" integer register
1088 target, i.e. it's not actually a Branch at all: it's a compare.
1089
1090 In RV (scalar) Base, a branch on a floating-point compare is
1091 done via the sequence "FEQ x1, f0, f5; BEQ x1, x0, #jumploc".
1092 This does extend to SV, as long as x1 (in the example sequence given)
1093 is vectorised. When that is the case, x1..x(1+VL-1) will also be
1094 set to 0 or 1 depending on whether f0==f5, f1==f6, f2==f7 and so on.
1095 The BEQ that follows will *also* compare x1==x0, x2==x0, x3==x0 and
1096 so on. Consequently, unlike integer-branch, FP Compare needs no
1097 modification in its behaviour.
1098
1099 In addition, it is noted that an entry "FNE" (the opposite of FEQ) is missing,
1100 and whilst in ordinary branch code this is fine because the standard
1101 RVF compare can always be followed up with an integer BEQ or a BNE (or
1102 a compressed comparison to zero or non-zero), in predication terms that
1103 becomes more of an impact. To deal with this, SV's predication has
1104 had "invert" added to it.
1105
1106 Also: note that FP Compare may be predicated, using the destination
1107 integer register (rd) to determine the predicate. FP Compare is **not**
1108 a twin-predication operation, as, again, just as with SV Branches,
1109 there are three registers involved: FP src1, FP src2 and INT rd.
1110
1111 ### Compressed Branch Instruction
1112
1113 Compressed Branch instructions are, just like standard Branch instructions,
1114 reinterpreted to be vectorised and predicated based on the source register
1115 (rs1s) CSR entries. As however there is only the one source register,
1116 given that c.beqz a10 is equivalent to beqz a10,x0, the optional target
1117 to store the results of the comparisions is taken from CSR predication
1118 table entries for **x0**.
1119
1120 The specific required use of x0 is, with a little thought, quite obvious,
1121 but is counterintuitive. Clearly it is **not** recommended to redirect
1122 x0 with a CSR register entry, however as a means to opaquely obtain
1123 a predication target it is the only sensible option that does not involve
1124 additional special CSRs (or, worse, additional special opcodes).
1125
1126 Note also that, just as with standard branches, the 2nd source
1127 (in this case x0 rather than src2) does **not** have to have its CSR
1128 register table marked as "active" in order for predication to work.
1129
1130 ## Vectorised Dual-operand instructions
1131
1132 There is a series of 2-operand instructions involving copying (and
1133 sometimes alteration):
1134
1135 * C.MV
1136 * FMV, FNEG, FABS, FCVT, FSGNJ, FSGNJN and FSGNJX
1137 * C.LWSP, C.SWSP, C.LDSP, C.FLWSP etc.
1138 * LOAD(-FP) and STORE(-FP)
1139
1140 All of these operations follow the same two-operand pattern, so it is
1141 *both* the source *and* destination predication masks that are taken into
1142 account. This is different from
1143 the three-operand arithmetic instructions, where the predication mask
1144 is taken from the *destination* register, and applied uniformly to the
1145 elements of the source register(s), element-for-element.
1146
1147 The pseudo-code pattern for twin-predicated operations is as
1148 follows:
1149
1150 function op(rd, rs):
1151  rd = int_csr[rd].active ? int_csr[rd].regidx : rd;
1152  rs = int_csr[rs].active ? int_csr[rs].regidx : rs;
1153  ps = get_pred_val(FALSE, rs); # predication on src
1154  pd = get_pred_val(FALSE, rd); # ... AND on dest
1155  for (int i = 0, int j = 0; i < VL && j < VL;):
1156 if (int_csr[rs].isvec) while (!(ps & 1<<i)) i++;
1157 if (int_csr[rd].isvec) while (!(pd & 1<<j)) j++;
1158 reg[rd+j] = SCALAR_OPERATION_ON(reg[rs+i])
1159 if (int_csr[rs].isvec) i++;
1160 if (int_csr[rd].isvec) j++; else break
1161
1162 This pattern covers scalar-scalar, scalar-vector, vector-scalar
1163 and vector-vector, and predicated variants of all of those.
1164 Zeroing is not presently included (TODO). As such, when compared
1165 to RVV, the twin-predicated variants of C.MV and FMV cover
1166 **all** standard vector operations: VINSERT, VSPLAT, VREDUCE,
1167 VEXTRACT, VSCATTER, VGATHER, VCOPY, and more.
1168
1169 Note that:
1170
1171 * elwidth (SIMD) is not covered in the pseudo-code above
1172 * ending the loop early in scalar cases (VINSERT, VEXTRACT) is also
1173 not covered
1174 * zero predication is also not shown (TODO).
1175
1176 ### C.MV Instruction <a name="c_mv"></a>
1177
1178 There is no MV instruction in RV however there is a C.MV instruction.
1179 It is used for copying integer-to-integer registers (vectorised FMV
1180 is used for copying floating-point).
1181
1182 If either the source or the destination register are marked as vectors
1183 C.MV is reinterpreted to be a vectorised (multi-register) predicated
1184 move operation. The actual instruction's format does not change:
1185
1186 [[!table data="""
1187 15 12 | 11 7 | 6 2 | 1 0 |
1188 funct4 | rd | rs | op |
1189 4 | 5 | 5 | 2 |
1190 C.MV | dest | src | C0 |
1191 """]]
1192
1193 A simplified version of the pseudocode for this operation is as follows:
1194
1195 function op_mv(rd, rs) # MV not VMV!
1196  rd = int_csr[rd].active ? int_csr[rd].regidx : rd;
1197  rs = int_csr[rs].active ? int_csr[rs].regidx : rs;
1198  ps = get_pred_val(FALSE, rs); # predication on src
1199  pd = get_pred_val(FALSE, rd); # ... AND on dest
1200  for (int i = 0, int j = 0; i < VL && j < VL;):
1201 if (int_csr[rs].isvec) while (!(ps & 1<<i)) i++;
1202 if (int_csr[rd].isvec) while (!(pd & 1<<j)) j++;
1203 ireg[rd+j] <= ireg[rs+i];
1204 if (int_csr[rs].isvec) i++;
1205 if (int_csr[rd].isvec) j++; else break
1206
1207 There are several different instructions from RVV that are covered by
1208 this one opcode:
1209
1210 [[!table data="""
1211 src | dest | predication | op |
1212 scalar | vector | none | VSPLAT |
1213 scalar | vector | destination | sparse VSPLAT |
1214 scalar | vector | 1-bit dest | VINSERT |
1215 vector | scalar | 1-bit? src | VEXTRACT |
1216 vector | vector | none | VCOPY |
1217 vector | vector | src | Vector Gather |
1218 vector | vector | dest | Vector Scatter |
1219 vector | vector | src & dest | Gather/Scatter |
1220 vector | vector | src == dest | sparse VCOPY |
1221 """]]
1222
1223 Also, VMERGE may be implemented as back-to-back (macro-op fused) C.MV
1224 operations with inversion on the src and dest predication for one of the
1225 two C.MV operations.
1226
1227 Note that in the instance where the Compressed Extension is not implemented,
1228 MV may be used, but that is a pseudo-operation mapping to addi rd, x0, rs.
1229 Note that the behaviour is **different** from C.MV because with addi the
1230 predication mask to use is taken **only** from rd and is applied against
1231 all elements: rs[i] = rd[i].
1232
1233 ### FMV, FNEG and FABS Instructions
1234
1235 These are identical in form to C.MV, except covering floating-point
1236 register copying. The same double-predication rules also apply.
1237 However when elwidth is not set to default the instruction is implicitly
1238 and automatic converted to a (vectorised) floating-point type conversion
1239 operation of the appropriate size covering the source and destination
1240 register bitwidths.
1241
1242 (Note that FMV, FNEG and FABS are all actually pseudo-instructions)
1243
1244 ### FVCT Instructions
1245
1246 These are again identical in form to C.MV, except that they cover
1247 floating-point to integer and integer to floating-point. When element
1248 width in each vector is set to default, the instructions behave exactly
1249 as they are defined for standard RV (scalar) operations, except vectorised
1250 in exactly the same fashion as outlined in C.MV.
1251
1252 However when the source or destination element width is not set to default,
1253 the opcode's explicit element widths are *over-ridden* to new definitions,
1254 and the opcode's element width is taken as indicative of the SIMD width
1255 (if applicable i.e. if packed SIMD is requested) instead.
1256
1257 For example FCVT.S.L would normally be used to convert a 64-bit
1258 integer in register rs1 to a 64-bit floating-point number in rd.
1259 If however the source rs1 is set to be a vector, where elwidth is set to
1260 default/2 and "packed SIMD" is enabled, then the first 32 bits of
1261 rs1 are converted to a floating-point number to be stored in rd's
1262 first element and the higher 32-bits *also* converted to floating-point
1263 and stored in the second. The 32 bit size comes from the fact that
1264 FCVT.S.L's integer width is 64 bit, and with elwidth on rs1 set to
1265 divide that by two it means that rs1 element width is to be taken as 32.
1266
1267 Similar rules apply to the destination register.
1268
1269 ## LOAD / STORE Instructions and LOAD-FP/STORE-FP <a name="load_store"></a>
1270
1271 An earlier draft of SV modified the behaviour of LOAD/STORE (modified
1272 the interpretation of the instruction fields). This
1273 actually undermined the fundamental principle of SV, namely that there
1274 be no modifications to the scalar behaviour (except where absolutely
1275 necessary), in order to simplify an implementor's task if considering
1276 converting a pre-existing scalar design to support parallelism.
1277
1278 So the original RISC-V scalar LOAD/STORE and LOAD-FP/STORE-FP functionality
1279 do not change in SV, however just as with C.MV it is important to note
1280 that dual-predication is possible.
1281
1282 In vectorised architectures there are usually at least two different modes
1283 for LOAD/STORE:
1284
1285 * Read (or write for STORE) from sequential locations, where one
1286 register specifies the address, and the one address is incremented
1287 by a fixed amount. This is usually known as "Unit Stride" mode.
1288 * Read (or write) from multiple indirected addresses, where the
1289 vector elements each specify separate and distinct addresses.
1290
1291 To support these different addressing modes, the CSR Register "isvector"
1292 bit is used. So, for a LOAD, when the src register is set to
1293 scalar, the LOADs are sequentially incremented by the src register
1294 element width, and when the src register is set to "vector", the
1295 elements are treated as indirection addresses. Simplified
1296 pseudo-code would look like this:
1297
1298 function op_ld(rd, rs) # LD not VLD!
1299  rdv = int_csr[rd].active ? int_csr[rd].regidx : rd;
1300  rsv = int_csr[rs].active ? int_csr[rs].regidx : rs;
1301  ps = get_pred_val(FALSE, rs); # predication on src
1302  pd = get_pred_val(FALSE, rd); # ... AND on dest
1303  for (int i = 0, int j = 0; i < VL && j < VL;):
1304 if (int_csr[rs].isvec) while (!(ps & 1<<i)) i++;
1305 if (int_csr[rd].isvec) while (!(pd & 1<<j)) j++;
1306 if (int_csr[rd].isvec)
1307 # indirect mode (multi mode)
1308 srcbase = ireg[rsv+i];
1309 else
1310 # unit stride mode
1311 srcbase = ireg[rsv] + i * XLEN/8; # offset in bytes
1312 ireg[rdv+j] <= mem[srcbase + imm_offs];
1313 if (!int_csr[rs].isvec &&
1314 !int_csr[rd].isvec) break # scalar-scalar LD
1315 if (int_csr[rs].isvec) i++;
1316 if (int_csr[rd].isvec) j++;
1317
1318 Notes:
1319
1320 * For simplicity, zeroing and elwidth is not included in the above:
1321 the key focus here is the decision-making for srcbase; vectorised
1322 rs means use sequentially-numbered registers as the indirection
1323 address, and scalar rs is "offset" mode.
1324 * The test towards the end for whether both source and destination are
1325 scalar is what makes the above pseudo-code provide the "standard" RV
1326 Base behaviour for LD operations.
1327 * The offset in bytes (XLEN/8) changes depending on whether the
1328 operation is a LB (1 byte), LH (2 byes), LW (4 bytes) or LD
1329 (8 bytes), and also whether the element width is over-ridden
1330 (see special element width section).
1331
1332 ## Compressed Stack LOAD / STORE Instructions <a name="c_ld_st"></a>
1333
1334 C.LWSP / C.SWSP and floating-point etc. are also source-dest twin-predicated,
1335 where it is implicit in C.LWSP/FLWSP etc. that x2 is the source register.
1336 It is therefore possible to use predicated C.LWSP to efficiently
1337 pop registers off the stack (by predicating x2 as the source), cherry-picking
1338 which registers to store to (by predicating the destination). Likewise
1339 for C.SWSP. In this way, LOAD/STORE-Multiple is efficiently achieved.
1340
1341 The two modes ("unit stride" and multi-indirection) are still supported,
1342 as with standard LD/ST. Essentially, the only difference is that the
1343 use of x2 is hard-coded into the instruction.
1344
1345 **Note**: it is still possible to redirect x2 to an alternative target
1346 register. With care, this allows C.LWSP / C.SWSP (and C.FLWSP) to be used as
1347 general-purpose LOAD/STORE operations.
1348
1349 ## Compressed LOAD / STORE Instructions
1350
1351 Compressed LOAD and STORE are again exactly the same as scalar LOAD/STORE,
1352 where the same rules apply and the same pseudo-code apply as for
1353 non-compressed LOAD/STORE. Again: setting scalar or vector mode
1354 on the src for LOAD and dest for STORE switches mode from "Unit Stride"
1355 to "Multi-indirection", respectively.
1356
1357 # Element bitwidth polymorphism <a name="elwidth"></a>
1358
1359 Element bitwidth is best covered as its own special section, as it
1360 is quite involved and applies uniformly across-the-board. SV restricts
1361 bitwidth polymorphism to default, 8-bit, 16-bit and 32-bit.
1362
1363 The effect of setting an element bitwidth is to re-cast each entry
1364 in the register table, and for all memory operations involving
1365 load/stores of certain specific sizes, to a completely different width.
1366 Thus In c-style terms, on an RV64 architecture, effectively each register
1367 now looks like this:
1368
1369 typedef union {
1370 uint8_t b[8];
1371 uint16_t s[4];
1372 uint32_t i[2];
1373 uint64_t l[1];
1374 } reg_t;
1375
1376 // integer table: assume maximum SV 7-bit regfile size
1377 reg_t int_regfile[128];
1378
1379 where the CSR Register table entry (not the instruction alone) determines
1380 which of those union entries is to be used on each operation, and the
1381 VL element offset in the hardware-loop specifies the index into each array.
1382
1383 However a naive interpretation of the data structure above masks the
1384 fact that setting VL greater than 8, for example, when the bitwidth is 8,
1385 accessing one specific register "spills over" to the following parts of
1386 the register file in a sequential fashion. So a much more accurate way
1387 to reflect this would be:
1388
1389 typedef union {
1390 uint8_t actual_bytes[8]; // 8 for RV64, 4 for RV32, 16 for RV128
1391 uint8_t b[0]; // array of type uint8_t
1392 uint16_t s[0];
1393 uint32_t i[0];
1394 uint64_t l[0];
1395 uint128_t d[0];
1396 } reg_t;
1397
1398 reg_t int_regfile[128];
1399
1400 where when accessing any individual regfile[n].b entry it is permitted
1401 (in c) to arbitrarily over-run the *declared* length of the array (zero),
1402 and thus "overspill" to consecutive register file entries in a fashion
1403 that is completely transparent to a greatly-simplified software / pseudo-code
1404 representation.
1405 It is however critical to note that it is clearly the responsibility of
1406 the implementor to ensure that, towards the end of the register file,
1407 an exception is thrown if attempts to access beyond the "real" register
1408 bytes is ever attempted.
1409
1410 Now we may modify pseudo-code an operation where all element bitwidths have
1411 been set to the same size, where this pseudo-code is otherwise identical
1412 to its "non" polymorphic versions (above):
1413
1414 function op_add(rd, rs1, rs2) # add not VADD!
1415 ...
1416 ...
1417  for (i = 0; i < VL; i++)
1418 ...
1419 ...
1420 // TODO, calculate if over-run occurs, for each elwidth
1421 if (elwidth == 8) {
1422    int_regfile[rd].b[id] <= int_regfile[rs1].i[irs1] +
1423     int_regfile[rs2].i[irs2];
1424 } else if elwidth == 16 {
1425    int_regfile[rd].s[id] <= int_regfile[rs1].s[irs1] +
1426     int_regfile[rs2].s[irs2];
1427 } else if elwidth == 32 {
1428    int_regfile[rd].i[id] <= int_regfile[rs1].i[irs1] +
1429     int_regfile[rs2].i[irs2];
1430 } else { // elwidth == 64
1431    int_regfile[rd].l[id] <= int_regfile[rs1].l[irs1] +
1432     int_regfile[rs2].l[irs2];
1433 }
1434 ...
1435 ...
1436
1437 So here we can see clearly: for 8-bit entries rd, rs1 and rs2 (and registers
1438 following sequentially on respectively from the same) are "type-cast"
1439 to 8-bit; for 16-bit entries likewise and so on.
1440
1441 However that only covers the case where the element widths are the same.
1442 Where the element widths are different, the following algorithm applies:
1443
1444 * Analyse the bitwidth of all source operands and work out the
1445 maximum. Record this as "maxsrcbitwidth"
1446 * If any given source operand requires sign-extension or zero-extension
1447 (ldb, div, rem, mul, sll, srl, sra etc.), instead of mandatory 32-bit
1448 sign-extension / zero-extension or whatever is specified in the standard
1449 RV specification, **change** that to sign-extending from the respective
1450 individual source operand's bitwidth from the CSR table out to
1451 "maxsrcbitwidth" (previously calculated), instead.
1452 * Following separate and distinct (optional) sign/zero-extension of all
1453 source operands as specifically required for that operation, carry out the
1454 operation at "maxsrcbitwidth". (Note that in the case of LOAD/STORE or MV
1455 this may be a "null" (copy) operation, and that with FCVT, the changes
1456 to the source and destination bitwidths may also turn FVCT effectively
1457 into a copy).
1458 * If the destination operand requires sign-extension or zero-extension,
1459 instead of a mandatory fixed size (typically 32-bit for arithmetic,
1460 for subw for example, and otherwise various: 8-bit for sb, 16-bit for sw
1461 etc.), overload the RV specification with the bitwidth from the
1462 destination register's elwidth entry.
1463 * Finally, store the (optionally) sign/zero-extended value into its
1464 destination: memory for sb/sw etc., or an offset section of the register
1465 file for an arithmetic operation.
1466
1467 In this way, polymorphic bitwidths are achieved without requiring a
1468 massive 64-way permutation of calculations **per opcode**, for example
1469 (4 possible rs1 bitwidths times 4 possible rs2 bitwidths times 4 possible
1470 rd bitwidths). The pseudo-code is therefore as follows:
1471
1472 typedef union {
1473 uint8_t b;
1474 uint16_t s;
1475 uint32_t i;
1476 uint64_t l;
1477 } el_reg_t;
1478
1479 bw(elwidth):
1480 if elwidth == 0:
1481 return xlen
1482 if elwidth == 1:
1483 return xlen / 2
1484 if elwidth == 2:
1485 return xlen * 2
1486 // elwidth == 3:
1487 return 8
1488
1489 get_max_elwidth(rs1, rs2):
1490 return max(bw(int_csr[rs1].elwidth), # default (XLEN) if not set
1491 bw(int_csr[rs2].elwidth)) # again XLEN if no entry
1492
1493 get_polymorphed_reg(reg, bitwidth, offset):
1494 el_reg_t res;
1495 res.l = 0; // TODO: going to need sign-extending / zero-extending
1496 if bitwidth == 8:
1497 reg.b = int_regfile[reg].b[offset]
1498 elif bitwidth == 16:
1499 reg.s = int_regfile[reg].s[offset]
1500 elif bitwidth == 32:
1501 reg.i = int_regfile[reg].i[offset]
1502 elif bitwidth == 64:
1503 reg.l = int_regfile[reg].l[offset]
1504 return res
1505
1506 set_polymorphed_reg(reg, bitwidth, offset, val):
1507 if (!int_csr[reg].isvec):
1508 # sign/zero-extend depending on opcode requirements, from
1509 # the reg's bitwidth out to the full bitwidth of the regfile
1510 val = sign_or_zero_extend(val, bitwidth, xlen)
1511 int_regfile[reg].l[0] = val
1512 elif bitwidth == 8:
1513 int_regfile[reg].b[offset] = val
1514 elif bitwidth == 16:
1515 int_regfile[reg].s[offset] = val
1516 elif bitwidth == 32:
1517 int_regfile[reg].i[offset] = val
1518 elif bitwidth == 64:
1519 int_regfile[reg].l[offset] = val
1520
1521 maxsrcwid = get_max_elwidth(rs1, rs2) # source element width(s)
1522 destwid = int_csr[rs1].elwidth # destination element width
1523  for (i = 0; i < VL; i++)
1524 if (predval & 1<<i) # predication uses intregs
1525 // TODO, calculate if over-run occurs, for each elwidth
1526 src1 = get_polymorphed_reg(rs1, maxsrcwid, irs1)
1527 // TODO, sign/zero-extend src1 and src2 as operation requires
1528 if (op_requires_sign_extend_src1)
1529 src1 = sign_extend(src1, maxsrcwid)
1530 src2 = get_polymorphed_reg(rs2, maxsrcwid, irs2)
1531 result = src1 + src2 # actual add here
1532 // TODO, sign/zero-extend result, as operation requires
1533 if (op_requires_sign_extend_dest)
1534 result = sign_extend(result, maxsrcwid)
1535 set_polymorphed_reg(rd, destwid, ird, result)
1536 if (!int_vec[rd].isvector) break
1537 if (int_vec[rd ].isvector)  { id += 1; }
1538 if (int_vec[rs1].isvector)  { irs1 += 1; }
1539 if (int_vec[rs2].isvector)  { irs2 += 1; }
1540
1541 Whilst specific sign-extension and zero-extension pseudocode call
1542 details are left out, due to each operation being different, the above
1543 should be clear that;
1544
1545 * the source operands are extended out to the maximum bitwidth of all
1546 source operands
1547 * the operation takes place at that maximum source bitwidth (the
1548 destination bitwidth is not involved at this point, at all)
1549 * the result is extended (or potentially even, truncated) before being
1550 stored in the destination. i.e. truncation (if required) to the
1551 destination width occurs **after** the operation **not** before.
1552 * when the destination is not marked as "vectorised", the **full**
1553 (standard, scalar) register file entry is taken up, i.e. the
1554 element is either sign-extended or zero-extended to cover the
1555 full register bitwidth (XLEN) if it is not already XLEN bits long.
1556
1557 Implementors are entirely free to optimise the above, particularly
1558 if it is specifically known that any given operation will complete
1559 accurately in less bits, as long as the results produced are
1560 directly equivalent and equal, for all inputs and all outputs,
1561 to those produced by the above algorithm.
1562
1563 ## Polymorphic floating-point operation exceptions and error-handling
1564
1565 For floating-point operations, conversion takes place without
1566 raising any kind of exception. Exactly as specified in the standard
1567 RV specification, NAN (or appropriate) is stored if the result
1568 is beyond the range of the destination, and, again, exactly as
1569 with the standard RV specification just as with scalar
1570 operations, the floating-point flag is raised (FCSR). And, again, just as
1571 with scalar operations, it is software's responsibility to check this flag.
1572 Given that the FCSR flags are "accrued", the fact that multiple element
1573 operations could have occurred is not a problem.
1574
1575 Note that it is perfectly legitimate for floating-point bitwidths of
1576 only 8 to be specified. However whilst it is possible to apply IEEE 754
1577 principles, no actual standard yet exists. Implementors wishing to
1578 provide hardware-level 8-bit support rather than throw a trap to emulate
1579 in software should contact the author of this specification before
1580 proceeding.
1581
1582 ## Polymorphic shift operators
1583
1584 A special note is needed for changing the element width of left and right
1585 shift operators, particularly right-shift. Even for standard RV base,
1586 in order for correct results to be returned, the second operand RS2 must
1587 be truncated to be within the range of RS1's bitwidth. spike's implementation
1588 of sll for example is as follows:
1589
1590 WRITE_RD(sext_xlen(zext_xlen(RS1) << (RS2 & (xlen-1))));
1591
1592 which means: where XLEN is 32 (for RV32), restrict RS2 to cover the
1593 range 0..31 so that RS1 will only be left-shifted by the amount that
1594 is possible to fit into a 32-bit register. Whilst this appears not
1595 to matter for hardware, it matters greatly in software implementations,
1596 and it also matters where an RV64 system is set to "RV32" mode, such
1597 that the underlying registers RS1 and RS2 comprise 64 hardware bits
1598 each.
1599
1600 For SV, where each operand's element bitwidth may be over-ridden, the
1601 rule about determining the operation's bitwidth *still applies*, being
1602 defined as the maximum bitwidth of RS1 and RS2. *However*, this rule
1603 **also applies to the truncation of RS2**. In other words, *after*
1604 determining the maximum bitwidth, RS2's range must **also be truncated**
1605 to ensure a correct answer. Example:
1606
1607 * RS1 is over-ridden to a 16-bit width
1608 * RS2 is over-ridden to an 8-bit width
1609 * RD is over-ridden to a 64-bit width
1610 * the maximum bitwidth is thus determined to be 16-bit - max(8,16)
1611 * RS2 is **truncated to a range of values from 0 to 15**: RS2 & (16-1)
1612
1613 Pseudocode (in spike) for this example would therefore be:
1614
1615 WRITE_RD(sext_xlen(zext_16bit(RS1) << (RS2 & (16-1))));
1616
1617 This example illustrates that considerable care therefore needs to be
1618 taken to ensure that left and right shift operations are implemented
1619 correctly. The key is that
1620
1621 * The operation bitwidth is determined by the maximum bitwidth
1622 of the *source registers*, **not** the destination register bitwidth
1623 * The result is then sign-extend (or truncated) as appropriate.
1624
1625 ## Polymorphic MULH/MULHU/MULHSU
1626
1627 MULH is designed to take the top half MSBs of a multiply that
1628 does not fit within the range of the source operands, such that
1629 smaller width operations may produce a full double-width multiply
1630 in two cycles. The issue is: SV allows the source operands to
1631 have variable bitwidth.
1632
1633 Here again special attention has to be paid to the rules regarding
1634 bitwidth, which, again, are that the operation is performed at
1635 the maximum bitwidth of the **source** registers. Therefore:
1636
1637 * An 8-bit x 8-bit multiply will create a 16-bit result that must
1638 be shifted down by 8 bits
1639 * A 16-bit x 8-bit multiply will create a 24-bit result that must
1640 be shifted down by 16 bits (top 8 bits being zero)
1641 * A 16-bit x 16-bit multiply will create a 32-bit result that must
1642 be shifted down by 16 bits
1643 * A 32-bit x 16-bit multiply will create a 48-bit result that must
1644 be shifted down by 32 bits
1645 * A 32-bit x 8-bit multiply will create a 40-bit result that must
1646 be shifted down by 32 bits
1647
1648 So again, just as with shift-left and shift-right, the result
1649 is shifted down by the maximum of the two source register bitwidths.
1650 And, exactly again, truncation or sign-extension is performed on the
1651 result. If sign-extension is to be carried out, it is performed
1652 from the same maximum of the two source register bitwidths out
1653 to the result element's bitwidth.
1654
1655 If truncation occurs, i.e. the top MSBs of the result are lost,
1656 this is "Officially Not Our Problem", i.e. it is assumed that the
1657 programmer actually desires the result to be truncated. i.e. if the
1658 programmer wanted all of the bits, they would have set the destination
1659 elwidth to accommodate them.
1660
1661 ## Polymorphic elwidth on LOAD/STORE <a name="elwidth_loadstore"></a>
1662
1663 Polymorphic element widths in vectorised form means that the data
1664 being loaded (or stored) across multiple registers needs to be treated
1665 (reinterpreted) as a contiguous stream of elwidth-wide items, where
1666 the source register's element width is **independent** from the destination's.
1667
1668 This makes for a slightly more complex algorithm when using indirection
1669 on the "addressed" register (source for LOAD and destination for STORE),
1670 particularly given that the LOAD/STORE instruction provides important
1671 information about the width of the data to be reinterpreted.
1672
1673 Let's illustrate the "load" part, where the pseudo-code for elwidth=default
1674 was as follows, and i is the loop from 0 to VL-1:
1675
1676 srcbase = ireg[rs+i];
1677 return mem[srcbase + imm]; // returns XLEN bits
1678
1679 Instead, when elwidth != default, for a LW (32-bit LOAD), elwidth-wide
1680 chunks are taken from the source memory location addressed by the current
1681 indexed source address register, and only when a full 32-bits-worth
1682 are taken will the index be moved on to the next contiguous source
1683 address register:
1684
1685 bitwidth = bw(elwidth); // source elwidth from CSR reg entry
1686 elsperblock = 32 / bitwidth // 1 if bw=32, 2 if bw=16, 4 if bw=8
1687 srcbase = ireg[rs+i/(elsperblock)]; // integer divide
1688 offs = i % elsperblock; // modulo
1689 return &mem[srcbase + imm + offs]; // re-cast to uint8_t*, uint16_t* etc.
1690
1691 Note that the constant "32" above is replaced by 8 for LB, 16 for LH, 64 for LD
1692 and 128 for LQ.
1693
1694 The principle is basically exactly the same as if the srcbase were pointing
1695 at the memory of the *register* file: memory is re-interpreted as containing
1696 groups of elwidth-wide discrete elements.
1697
1698 When storing the result from a load, it's important to respect the fact
1699 that the destination register has its *own separate element width*. Thus,
1700 when each element is loaded (at the source element width), any sign-extension
1701 or zero-extension (or truncation) needs to be done to the *destination*
1702 bitwidth. Also, the storing has the exact same analogous algorithm as
1703 above, where in fact it is just the set\_polymorphed\_reg pseudocode
1704 (completely unchanged) used above.
1705
1706 One issue remains: when the source element width is **greater** than
1707 the width of the operation, it is obvious that a single LB for example
1708 cannot possibly obtain 16-bit-wide data. This condition may be detected
1709 where, when using integer divide, elsperblock (the width of the LOAD
1710 divided by the bitwidth of the element) is zero.
1711
1712 The issue is "fixed" by ensuring that elsperblock is a minimum of 1:
1713
1714 elsperblock = min(1, LD_OP_BITWIDTH / element_bitwidth)
1715
1716 The elements, if the element bitwidth is larger than the LD operation's
1717 size, will then be sign/zero-extended to the full LD operation size, as
1718 specified by the LOAD (LDU instead of LD, LBU instead of LB), before
1719 being passed on to the second phase.
1720
1721 As LOAD/STORE may be twin-predicated, it is important to note that
1722 the rules on twin predication still apply, except where in previous
1723 pseudo-code (elwidth=default for both source and target) it was
1724 the *registers* that the predication was applied to, it is now the
1725 **elements** that the predication is applied to.
1726
1727 Thus the full pseudocode for all LD operations may be written out
1728 as follows:
1729
1730 function LBU(rd, rs):
1731 load_elwidthed(rd, rs, 8, true)
1732 function LB(rd, rs):
1733 load_elwidthed(rd, rs, 8, false)
1734 function LH(rd, rs):
1735 load_elwidthed(rd, rs, 16, false)
1736 ...
1737 ...
1738 function LQ(rd, rs):
1739 load_elwidthed(rd, rs, 128, false)
1740
1741 # returns 1 byte of data when opwidth=8, 2 bytes when opwidth=16..
1742 function load_memory(rs, imm, i, opwidth):
1743 elwidth = int_csr[rs].elwidth
1744 bitwidth = bw(elwidth);
1745 elsperblock = min(1, opwidth / bitwidth)
1746 srcbase = ireg[rs+i/(elsperblock)];
1747 offs = i % elsperblock;
1748 return mem[srcbase + imm + offs]; # 1/2/4/8/16 bytes
1749
1750 function load_elwidthed(rd, rs, opwidth, unsigned):
1751 destwid = int_csr[rd].elwidth # destination element width
1752  rd = int_csr[rd].active ? int_csr[rd].regidx : rd;
1753  rs = int_csr[rs].active ? int_csr[rs].regidx : rs;
1754  ps = get_pred_val(FALSE, rs); # predication on src
1755  pd = get_pred_val(FALSE, rd); # ... AND on dest
1756  for (int i = 0, int j = 0; i < VL && j < VL;):
1757 if (int_csr[rs].isvec) while (!(ps & 1<<i)) i++;
1758 if (int_csr[rd].isvec) while (!(pd & 1<<j)) j++;
1759 val = load_memory(rs, imm, i, opwidth)
1760 if unsigned:
1761 val = zero_extend(val, min(opwidth, bitwidth))
1762 else:
1763 val = sign_extend(val, min(opwidth, bitwidth))
1764 set_polymorphed_reg(rd, bitwidth, j, val)
1765 if (int_csr[rs].isvec) i++;
1766 if (int_csr[rd].isvec) j++; else break;
1767
1768 Note:
1769
1770 * when comparing against for example the twin-predicated c.mv
1771 pseudo-code, the pattern of independent incrementing of rd and rs
1772 is preserved unchanged.
1773 * just as with the c.mv pseudocode, zeroing is not included and must be
1774 taken into account (TODO).
1775 * that due to the use of a twin-predication algorithm, LOAD/STORE also
1776 take on the same VSPLAT, VINSERT, VREDUCE, VEXTRACT, VGATHER and
1777 VSCATTER characteristics.
1778 * that due to the use of the same set\_polymorphed\_reg pseudocode,
1779 a destination that is not vectorised (marked as scalar) will
1780 result in the element being fully sign-extended or zero-extended
1781 out to the full register file bitwidth (XLEN). When the source
1782 is also marked as scalar, this is how the compatibility with
1783 standard RV LOAD/STORE is preserved by this algorithm.
1784
1785 ### Example Tables showing LOAD elements
1786
1787 This section contains examples of vectorised LOAD operations, showing
1788 how the two stage process works (three if zero/sign-extension is included).
1789
1790
1791 #### Example: LD x8, x5(0), x8 CSR-elwidth=32, x5 CSR-elwidth=16, VL=7
1792
1793 This is:
1794
1795 * a 64-bit load, with an offset of zero
1796 * with a source-address elwidth of 16-bit
1797 * into a destination-register with an elwidth of 32-bit
1798 * where VL=7
1799 * from register x5 (actually x5-x6) to x8 (actually x8 to half of x11)
1800 * RV64, where XLEN=64 is assumed.
1801
1802 First, the memory table, which, due to the
1803 element width being 16 and the operation being LD (64), the 64-bits
1804 loaded from memory are subdivided into groups of **four** elements.
1805 And, with VL being 7 (deliberately to illustrate that this is reasonable
1806 and possible), the first four are sourced from the offset addresses pointed
1807 to by x5, and the next three from the ofset addresses pointed to by
1808 the next contiguous register, x6:
1809
1810 [[!table data="""
1811 addr | byte 0 | byte 1 | byte 2 | byte 3 | byte 4 | byte 5 | byte 6 | byte 7 |
1812 @x5 | elem 0 || elem 1 || elem 2 || elem 3 ||
1813 @x6 | elem 4 || elem 5 || elem 6 || not loaded ||
1814 """]]
1815
1816 Next, the elements are zero-extended from 16-bit to 32-bit, as whilst
1817 the elwidth CSR entry for x5 is 16-bit, the destination elwidth on x8 is 32.
1818
1819 [[!table data="""
1820 byte 3 | byte 2 | byte 1 | byte 0 |
1821 0x0 | 0x0 | elem0 ||
1822 0x0 | 0x0 | elem1 ||
1823 0x0 | 0x0 | elem2 ||
1824 0x0 | 0x0 | elem3 ||
1825 0x0 | 0x0 | elem4 ||
1826 0x0 | 0x0 | elem5 ||
1827 0x0 | 0x0 | elem6 ||
1828 0x0 | 0x0 | elem7 ||
1829 """]]
1830
1831 Lastly, the elements are stored in contiguous blocks, as if x8 was also
1832 byte-addressable "memory". That "memory" happens to cover registers
1833 x8, x9, x10 and x11, with the last 32 "bits" of x11 being **UNMODIFIED**:
1834
1835 [[!table data="""
1836 reg# | byte 7 | byte 6 | byte 5 | byte 4 | byte 3 | byte 2 | byte 1 | byte 0 |
1837 x8 | 0x0 | 0x0 | elem 1 || 0x0 | 0x0 | elem 0 ||
1838 x9 | 0x0 | 0x0 | elem 3 || 0x0 | 0x0 | elem 2 ||
1839 x10 | 0x0 | 0x0 | elem 5 || 0x0 | 0x0 | elem 4 ||
1840 x11 | **UNMODIFIED** |||| 0x0 | 0x0 | elem 6 ||
1841 """]]
1842
1843 Thus we have data that is loaded from the **addresses** pointed to by
1844 x5 and x6, zero-extended from 16-bit to 32-bit, stored in the **registers**
1845 x8 through to half of x11.
1846 The end result is that elements 0 and 1 end up in x8, with element 8 being
1847 shifted up 32 bits, and so on, until finally element 6 is in the
1848 LSBs of x11.
1849
1850 Note that whilst the memory addressing table is shown left-to-right byte order,
1851 the registers are shown in right-to-left (MSB) order. This does **not**
1852 imply that bit or byte-reversal is carried out: it's just easier to visualise
1853 memory as being contiguous bytes, and emphasises that registers are not
1854 really actually "memory" as such.
1855
1856 ## Why SV bitwidth specification is restricted to 4 entries
1857
1858 The four entries for SV element bitwidths only allows three over-rides:
1859
1860 * default bitwidth for a given operation *divided* by two
1861 * default bitwidth for a given operation *multiplied* by two
1862 * 8-bit
1863
1864 At first glance this seems completely inadequate: for example, RV64
1865 cannot possibly operate on 16-bit operations, because 64 divided by
1866 2 is 32. However, the reader may have forgotten that it is possible,
1867 at run-time, to switch a 64-bit application into 32-bit mode, by
1868 setting UXL. Once switched, opcodes that formerly had 64-bit
1869 meanings now have 32-bit meanings, and in this way, "default/2"
1870 now reaches **16-bit** where previously it meant "32-bit".
1871
1872 There is however an absolutely crucial aspect oF SV here that explicitly
1873 needs spelling out, and it's whether the "vectorised" bit is set in
1874 the Register's CSR entry.
1875
1876 If "vectorised" is clear (not set), this indicates that the operation
1877 is "scalar". Under these circumstances, when set on a destination (RD),
1878 then sign-extension and zero-extension, whilst changed to match the
1879 override bitwidth (if set), will erase the **full** register entry
1880 (64-bit if RV64).
1881
1882 When vectorised is *set*, this indicates that the operation now treats
1883 **elements** as if they were independent registers, so regardless of
1884 the length, any parts of a given actual register that are not involved
1885 in the operation are **NOT** modified, but are **PRESERVED**.
1886
1887 SIMD micro-architectures may implement this by using predication on
1888 any elements in a given actual register that are beyond the end of
1889 multi-element operation.
1890
1891 Example:
1892
1893 * rs1, rs2 and rd are all set to 8-bit
1894 * VL is set to 3
1895 * RV64 architecture is set (UXL=64)
1896 * add operation is carried out
1897 * bits 0-23 of RD are modified to be rs1[23..16] + rs2[23..16]
1898 concatenated with similar add operations on bits 15..8 and 7..0
1899 * bits 24 through 63 **remain as they originally were**.
1900
1901 Example SIMD micro-architectural implementation:
1902
1903 * SIMD architecture works out the nearest round number of elements
1904 that would fit into a full RV64 register (in this case: 8)
1905 * SIMD architecture creates a hidden predicate, binary 0b00000111
1906 i.e. the bottom 3 bits set (VL=3) and the top 5 bits clear
1907 * SIMD architecture goes ahead with the add operation as if it
1908 was a full 8-wide batch of 8 adds
1909 * SIMD architecture passes top 5 elements through the adders
1910 (which are "disabled" due to zero-bit predication)
1911 * SIMD architecture gets the 5 unmodified top 8-bits back unmodified
1912 and stores them in rd.
1913
1914 This requires a read on rd, however this is required anyway in order
1915 to support non-zeroing mode.
1916
1917 ## Polymorphic floating-point
1918
1919 Standard scalar RV integer operations base the register width on XLEN,
1920 which may be changed (UXL in USTATUS, and the corresponding MXL and
1921 SXL in MSTATUS and SSTATUS respectively). Integer LOAD, STORE and
1922 arithmetic operations are therefore restricted to an active XLEN bits,
1923 with sign or zero extension to pad out the upper bits when XLEN has
1924 been dynamically set to less than the actual register size.
1925
1926 For scalar floating-point, the active (used / changed) bits are
1927 specified exclusively by the operation: ADD.S specifies an active
1928 32-bits, with the upper bits of the source registers needing to
1929 be all 1s ("NaN-boxed"), and the destination upper bits being
1930 *set* to all 1s (including on LOAD/STOREs).
1931
1932 Where elwidth is set to default (on any source or the destination)
1933 it is obvious that this NaN-boxing behaviour can and should be
1934 preserved. When elwidth is non-default things are less obvious,
1935 so need to be thought through. Here is a normal (scalar) sequence,
1936 assuming an RV64 which supports Quad (128-bit) FLEN:
1937
1938 * FLD loads 64-bit wide from memory. Top 64 MSBs are set to all 1s
1939 * ADD.D performs a 64-bit-wide add. Top 64 MSBs of destination set to 1s.
1940 * FSD stores lowest 64-bits from the 128-bit-wide register to memory:
1941 top 64 MSBs ignored.
1942
1943 Therefore it makes sense to mirror this behaviour when, for example,
1944 elwidth is set to 32. Assume elwidth set to 32 on all source and
1945 destination registers:
1946
1947 * FLD loads 64-bit wide from memory as **two** 32-bit single-precision
1948 floating-point numbers.
1949 * ADD.D performs **two** 32-bit-wide adds, storing one of the adds
1950 in bits 0-31 and the second in bits 32-63.
1951 * FSD stores lowest 64-bits from the 128-bit-wide register to memory
1952
1953 Here's the thing: it does not make sense to overwrite the top 64 MSBs
1954 of the registers either during the FLD **or** the ADD.D. The reason
1955 is that, effectively, the top 64 MSBs actually represent a completely
1956 independent 64-bit register, so overwriting it is not only gratuitous
1957 but may actually be harmful for a future extension to SV which may
1958 have a way to directly access those top 64 bits.
1959
1960 The decision is therefore **not** to touch the upper parts of floating-point
1961 registers whereever elwidth is set to non-default values, including
1962 when "isvec" is false in a given register's CSR entry. Only when the
1963 elwidth is set to default **and** isvec is false will the standard
1964 RV behaviour be followed, namely that the upper bits be modified.
1965
1966 Ultimately if elwidth is default and isvec false on *all* source
1967 and destination registers, a SimpleV instruction defaults completely
1968 to standard RV scalar behaviour (this holds true for **all** operations,
1969 right across the board).
1970
1971 The nice thing here is that ADD.S, ADD.D and ADD.Q when elwidth are
1972 non-default values are effectively all the same: they all still perform
1973 multiple ADD operations, just at different widths. A future extension
1974 to SimpleV may actually allow ADD.S to access the upper bits of the
1975 register, effectively breaking down a 128-bit register into a bank
1976 of 4 independently-accesible 32-bit registers.
1977
1978 In the meantime, although when e.g. setting VL to 8 it would technically
1979 make no difference to the ALU whether ADD.S, ADD.D or ADD.Q is used,
1980 using ADD.Q may be an easy way to signal to the microarchitecture that
1981 it is to receive a higher VL value. On a superscalar OoO architecture
1982 there may be absolutely no difference, however on simpler SIMD-style
1983 microarchitectures they may not necessarily have the infrastructure in
1984 place to know the difference, such that when VL=8 and an ADD.D instruction
1985 is issued, it completes in 2 cycles (or more) rather than one, where
1986 if an ADD.Q had been issued instead on such simpler microarchitectures
1987 it would complete in one.
1988
1989 ## Specific instruction walk-throughs
1990
1991 This section covers walk-throughs of the above-outlined procedure
1992 for converting standard RISC-V scalar arithmetic operations to
1993 polymorphic widths, to ensure that it is correct.
1994
1995 ### add
1996
1997 Standard Scalar RV32/RV64 (xlen):
1998
1999 * RS1 @ xlen bits
2000 * RS2 @ xlen bits
2001 * add @ xlen bits
2002 * RD @ xlen bits
2003
2004 Polymorphic variant:
2005
2006 * RS1 @ rs1 bits, zero-extended to max(rs1, rs2) bits
2007 * RS2 @ rs2 bits, zero-extended to max(rs1, rs2) bits
2008 * add @ max(rs1, rs2) bits
2009 * RD @ rd bits. zero-extend to rd if rd > max(rs1, rs2) otherwise truncate
2010
2011 Note here that polymorphic add zero-extends its source operands,
2012 where addw sign-extends.
2013
2014 ### addw
2015
2016 The RV Specification specifically states that "W" variants of arithmetic
2017 operations always produce 32-bit signed values. In a polymorphic
2018 environment it is reasonable to assume that the signed aspect is
2019 preserved, where it is the length of the operands and the result
2020 that may be changed.
2021
2022 Standard Scalar RV64 (xlen):
2023
2024 * RS1 @ xlen bits
2025 * RS2 @ xlen bits
2026 * add @ xlen bits
2027 * RD @ xlen bits, truncate add to 32-bit and sign-extend to xlen.
2028
2029 Polymorphic variant:
2030
2031 * RS1 @ rs1 bits, sign-extended to max(rs1, rs2) bits
2032 * RS2 @ rs2 bits, sign-extended to max(rs1, rs2) bits
2033 * add @ max(rs1, rs2) bits
2034 * RD @ rd bits. sign-extend to rd if rd > max(rs1, rs2) otherwise truncate
2035
2036 Note here that polymorphic addw sign-extends its source operands,
2037 where add zero-extends.
2038
2039 This requires a little more in-depth analysis. Where the bitwidth of
2040 rs1 equals the bitwidth of rs2, no sign-extending will occur. It is
2041 only where the bitwidth of either rs1 or rs2 are different, will the
2042 lesser-width operand be sign-extended.
2043
2044 Effectively however, both rs1 and rs2 are being sign-extended (or truncated),
2045 where for add they are both zero-extended. This holds true for all arithmetic
2046 operations ending with "W".
2047
2048 ### addiw
2049
2050 Standard Scalar RV64I:
2051
2052 * RS1 @ xlen bits, truncated to 32-bit
2053 * immed @ 12 bits, sign-extended to 32-bit
2054 * add @ 32 bits
2055 * RD @ rd bits. sign-extend to rd if rd > 32, otherwise truncate.
2056
2057 Polymorphic variant:
2058
2059 * RS1 @ rs1 bits
2060 * immed @ 12 bits, sign-extend to max(rs1, 12) bits
2061 * add @ max(rs1, 12) bits
2062 * RD @ rd bits. sign-extend to rd if rd > max(rs1, 12) otherwise truncate
2063
2064 # Predication Element Zeroing
2065
2066 The introduction of zeroing on traditional vector predication is usually
2067 intended as an optimisation for lane-based microarchitectures with register
2068 renaming to be able to save power by avoiding a register read on elements
2069 that are passed through en-masse through the ALU. Simpler microarchitectures
2070 do not have this issue: they simply do not pass the element through to
2071 the ALU at all, and therefore do not store it back in the destination.
2072 More complex non-lane-based micro-architectures can, when zeroing is
2073 not set, use the predication bits to simply avoid sending element-based
2074 operations to the ALUs, entirely: thus, over the long term, potentially
2075 keeping all ALUs 100% occupied even when elements are predicated out.
2076
2077 SimpleV's design principle is not based on or influenced by
2078 microarchitectural design factors: it is a hardware-level API.
2079 Therefore, looking purely at whether zeroing is *useful* or not,
2080 (whether less instructions are needed for certain scenarios),
2081 given that a case can be made for zeroing *and* non-zeroing, the
2082 decision was taken to add support for both.
2083
2084 ## Single-predication (based on destination register)
2085
2086 Zeroing on predication for arithmetic operations is taken from
2087 the destination register's predicate. i.e. the predication *and*
2088 zeroing settings to be applied to the whole operation come from the
2089 CSR Predication table entry for the destination register.
2090 Thus when zeroing is set on predication of a destination element,
2091 if the predication bit is clear, then the destination element is *set*
2092 to zero (twin-predication is slightly different, and will be covered
2093 next).
2094
2095 Thus the pseudo-code loop for a predicated arithmetic operation
2096 is modified to as follows:
2097
2098  for (i = 0; i < VL; i++)
2099 if not zeroing: # an optimisation
2100 while (!(predval & 1<<i) && i < VL)
2101 if (int_vec[rd ].isvector)  { id += 1; }
2102 if (int_vec[rs1].isvector)  { irs1 += 1; }
2103 if (int_vec[rs2].isvector)  { irs2 += 1; }
2104 if i == VL:
2105 break
2106 if (predval & 1<<i)
2107 src1 = ....
2108 src2 = ...
2109 else:
2110 result = src1 + src2 # actual add (or other op) here
2111 set_polymorphed_reg(rd, destwid, ird, result)
2112 if (!int_vec[rd].isvector) break
2113 else if zeroing:
2114 result = 0
2115 set_polymorphed_reg(rd, destwid, ird, result)
2116 if (int_vec[rd ].isvector)  { id += 1; }
2117 else if (predval & 1<<i) break;
2118 if (int_vec[rs1].isvector)  { irs1 += 1; }
2119 if (int_vec[rs2].isvector)  { irs2 += 1; }
2120
2121 The optimisation to skip elements entirely is only possible for certain
2122 micro-architectures when zeroing is not set. However for lane-based
2123 micro-architectures this optimisation may not be practical, as it
2124 implies that elements end up in different "lanes". Under these
2125 circumstances it is perfectly fine to simply have the lanes
2126 "inactive" for predicated elements, even though it results in
2127 less than 100% ALU utilisation.
2128
2129 ## Twin-predication (based on source and destination register)
2130
2131 Twin-predication is not that much different, except that that
2132 the source is independently zero-predicated from the destination.
2133 This means that the source may be zero-predicated *or* the
2134 destination zero-predicated *or both*, or neither.
2135
2136 When with twin-predication, zeroing is set on the source and not
2137 the destination, if a predicate bit is set it indicates that a zero
2138 data element is passed through the operation (the exception being:
2139 if the source data element is to be treated as an address - a LOAD -
2140 then the data returned *from* the LOAD is zero, rather than looking up an
2141 *address* of zero.
2142
2143 When zeroing is set on the destination and not the source, then just
2144 as with single-predicated operations, a zero is stored into the destination
2145 element (or target memory address for a STORE).
2146
2147 Zeroing on both source and destination effectively result in a bitwise
2148 NOR operation of the source and destination predicate: the result is that
2149 where either source predicate OR destination predicate is set to 0,
2150 a zero element will ultimately end up in the destination register.
2151
2152 However: this may not necessarily be the case for all operations;
2153 implementors, particularly of custom instructions, clearly need to
2154 think through the implications in each and every case.
2155
2156 Here is pseudo-code for a twin zero-predicated operation:
2157
2158 function op_mv(rd, rs) # MV not VMV!
2159  rd = int_csr[rd].active ? int_csr[rd].regidx : rd;
2160  rs = int_csr[rs].active ? int_csr[rs].regidx : rs;
2161  ps, zerosrc = get_pred_val(FALSE, rs); # predication on src
2162  pd, zerodst = get_pred_val(FALSE, rd); # ... AND on dest
2163  for (int i = 0, int j = 0; i < VL && j < VL):
2164 if (int_csr[rs].isvec && !zerosrc) while (!(ps & 1<<i)) i++;
2165 if (int_csr[rd].isvec && !zerodst) while (!(pd & 1<<j)) j++;
2166 if ((pd & 1<<j))
2167 if ((pd & 1<<j))
2168 sourcedata = ireg[rs+i];
2169 else
2170 sourcedata = 0
2171 ireg[rd+j] <= sourcedata
2172 else if (zerodst)
2173 ireg[rd+j] <= 0
2174 if (int_csr[rs].isvec)
2175 i++;
2176 if (int_csr[rd].isvec)
2177 j++;
2178 else
2179 if ((pd & 1<<j))
2180 break;
2181
2182 Note that in the instance where the destination is a scalar, the hardware
2183 loop is ended the moment a value *or a zero* is placed into the destination
2184 register/element. Also note that, for clarity, variable element widths
2185 have been left out of the above.
2186
2187 # Exceptions
2188
2189 TODO: expand. Exceptions may occur at any time, in any given underlying
2190 scalar operation. This implies that context-switching (traps) may
2191 occur, and operation must be returned to where it left off. That in
2192 turn implies that the full state - including the current parallel
2193 element being processed - has to be saved and restored. This is
2194 what the **STATE** CSR is for.
2195
2196 The implications are that all underlying individual scalar operations
2197 "issued" by the parallelisation have to appear to be executed sequentially.
2198 The further implications are that if two or more individual element
2199 operations are underway, and one with an earlier index causes an exception,
2200 it may be necessary for the microarchitecture to **discard** or terminate
2201 operations with higher indices.
2202
2203 This being somewhat dissatisfactory, an "opaque predication" variant
2204 of the STATE CSR is being considered.
2205
2206 # Hints
2207
2208 A "HINT" is an operation that has no effect on architectural state,
2209 where its use may, by agreed convention, give advance notification
2210 to the microarchitecture: branch prediction notification would be
2211 a good example. Usually HINTs are where rd=x0.
2212
2213 With Simple-V being capable of issuing *parallel* instructions where
2214 rd=x0, the space for possible HINTs is expanded considerably. VL
2215 could be used to indicate different hints. In addition, if predication
2216 is set, the predication register itself could hypothetically be passed
2217 in as a *parameter* to the HINT operation.
2218
2219 No specific hints are yet defined in Simple-V
2220
2221 # VLIW Format <a name="vliw-format"></a>
2222
2223 One issue with SV is the setup and teardown time of the CSRs. The cost
2224 of the use of a full CSRRW (requiring LI) is quite high. A VLIW format
2225 therefore makes sense.
2226
2227 A suitable prefix, which fits the Expanded Instruction-Length encoding
2228 for "(80 + 16 times instruction_length)", as defined in Section 1.5
2229 of the RISC-V ISA, is as follows:
2230
2231 | 15 | 14:12 | 11:10 | 9:8 | 7 | 6:0 |
2232 | - | ----- | ----- | ----- | --- | ------- |
2233 | rsvd | 16xil | pplen | rplen | S/VL| 1111111 |
2234
2235 Optional VL/MAXVL/SubVL Block:
2236
2237 | 15 | 14:12 | 11:6 | 5:0 |
2238 | - | ----- | ------ | ------- |
2239 | rsvd | SubVL | MAXVL | VLEN |
2240
2241 Reminder of the variable-length format from Section 1.5 of the RISC-V ISA:
2242
2243 | base+4 .. base+2 | base | number of bits |
2244 | ------------------------- | ---------------- | -------------------------- |
2245 | ..xxxx xxxxxxxxxxxxxxxx | xnnnxxxxx1111111 | (80+16\*nnn)-bit, nnn!=111 |
2246 | {ops}{Pred}{Reg}{VL} | SV Prefix | |
2247
2248 Notes:
2249
2250 * Bit 7 does not specify the VL, it specifies if VL, MAXVL and Sub-Vector
2251 Length are to be in an optional block immediately following the prefix.
2252 * Bits 8 and 9 define how many RegCam entries (0 to 3) follow (after
2253 the optional VL block)
2254 * Bits 10 and 11 define how many PredCam entries (0 to 3) follow after
2255 the (optional) RegCam entries
2256 * Bits 14 to 12 (IL) define the actual length of the instruction: total
2257 number of bits is 80 + 16 times IL. Standard RV32, RVC and also
2258 SVPrefix (P48-\*-Type) instructions fit into this space, after the
2259 (optional) RegCam / PredCam entries
2260 * Anything - any registers - within the VLIW-prefixed format *MUST* have the
2261 RegCam and PredCam CSRs applied to it.
2262 * At the end of the VLIW Group, the RegCam and PredCam CSRs *no longer apply*.
2263
2264 This would greatly reduce the amount of space utilised by Vectorised
2265 instructions, given that 64-bit CSRRW requires 3, even 4 32-bit opcodes: the
2266 CSR itself, a LI, and the setting up of the value into the RS register
2267 of the CSR, which, again, requires a LI / LUI to get the 32 bit
2268 data into the CSR. To get 64-bit data into the register in order to put
2269 it into the CSR(s), LOAD operations from memory are needed!
2270
2271 Given that each 64-bit CSR can hold only 4x PredCAM entries (or 4 RegCAM
2272 entries), that's potentially 6 to eight 32-bit instructions, just to
2273 establish the Vector State!
2274
2275 Not only that: even CSRRW on VL and MAXVL requires 64-bits (even more bits if
2276 VL needs to be set to greater than 32). Bear in mind that in SV, both MAXVL
2277 and VL need to be set.
2278
2279 By contrast, the VLIW prefix is only 16 bits, the VL/MAX/SubVL block is
2280 only 16 bits, and as long as not too many predicates and register vector
2281 qualifiers are specified, several 32-bit and 16-bit opcodes can fit into the
2282 format.
2283 In this light, embedding the VL/MAXVL, PredCam and RegCam CSR entries into
2284 a VLIW format makes a lot of sense.
2285
2286 Open Questions:
2287
2288 * is there a way to create a much more compact (compressed) version of the
2289 PredCAM and RegCAM entries?
2290 * Is it necessary to stick to the RISC-V 1.5 format? Why not go with
2291 using the 15th bit to allow 80 + 16\*0bnnnn bits? Perhaps to be sane,
2292 limit to 256 bits (16 times 0-11).
2293
2294 # Subsets of RV functionality
2295
2296 This section describes the differences when SV is implemented on top of
2297 different subsets of RV.
2298
2299 ## Common options
2300
2301 It is permitted to limit the size of either (or both) the register files
2302 down to the original size of the standard RV architecture. However, below
2303 the mandatory limits set in the RV standard will result in non-compliance
2304 with the SV Specification.
2305
2306 ## RV32 / RV32F
2307
2308 When RV32 or RV32F is implemented, XLEN is set to 32, and thus the
2309 maximum limit for predication is also restricted to 32 bits. Whilst not
2310 actually specifically an "option" it is worth noting.
2311
2312 ## RV32G
2313
2314 Normally in standard RV32 it does not make much sense to have
2315 RV32G, The critical instructions that are missing in standard RV32
2316 are those for moving data to and from the double-width floating-point
2317 registers into the integer ones, as well as the FCVT routines.
2318
2319 In an earlier draft of SV, it was possible to specify an elwidth
2320 of double the standard register size: this had to be dropped,
2321 and may be reintroduced in future revisions.
2322
2323 ## RV32 (not RV32F / RV32G) and RV64 (not RV64F / RV64G)
2324
2325 When floating-point is not implemented, the size of the User Register and
2326 Predication CSR tables may be halved, to only 4 2x16-bit CSRs (8 entries
2327 per table).
2328
2329 ## RV32E
2330
2331 In embedded scenarios the User Register and Predication CSRs may be
2332 dropped entirely, or optionally limited to 1 CSR, such that the combined
2333 number of entries from the M-Mode CSR Register table plus U-Mode
2334 CSR Register table is either 4 16-bit entries or (if the U-Mode is
2335 zero) only 2 16-bit entries (M-Mode CSR table only). Likewise for
2336 the Predication CSR tables.
2337
2338 RV32E is the most likely candidate for simply detecting that registers
2339 are marked as "vectorised", and generating an appropriate exception
2340 for the VL loop to be implemented in software.
2341
2342 ## RV128
2343
2344 RV128 has not been especially considered, here, however it has some
2345 extremely large possibilities: double the element width implies
2346 256-bit operands, spanning 2 128-bit registers each, and predication
2347 of total length 128 bit given that XLEN is now 128.
2348
2349 # Under consideration <a name="issues"></a>
2350
2351 for element-grouping, if there is unused space within a register
2352 (3 16-bit elements in a 64-bit register for example), recommend:
2353
2354 * For the unused elements in an integer register, the used element
2355 closest to the MSB is sign-extended on write and the unused elements
2356 are ignored on read.
2357 * The unused elements in a floating-point register are treated as-if
2358 they are set to all ones on write and are ignored on read, matching the
2359 existing standard for storing smaller FP values in larger registers.
2360
2361 ---
2362
2363 info register,
2364
2365 > One solution is to just not support LR/SC wider than a fixed
2366 > implementation-dependent size, which must be at least 
2367 >1 XLEN word, which can be read from a read-only CSR
2368 > that can also be used for info like the kind and width of 
2369 > hw parallelism supported (128-bit SIMD, minimal virtual 
2370 > parallelism, etc.) and other things (like maybe the number 
2371 > of registers supported). 
2372
2373 > That CSR would have to have a flag to make a read trap so
2374 > a hypervisor can simulate different values.
2375
2376 ----
2377
2378 > And what about instructions like JALR? 
2379
2380 answer: they're not vectorised, so not a problem
2381
2382 ----
2383
2384 * if opcode is in the RV32 group, rd, rs1 and rs2 bitwidth are
2385 XLEN if elwidth==default
2386 * if opcode is in the RV32I group, rd, rs1 and rs2 bitwidth are
2387 *32* if elwidth == default
2388
2389 ---
2390
2391 TODO: update elwidth to be default / 8 / 16 / 32
2392
2393 ---
2394
2395 TODO: document different lengths for INT / FP regfiles, and provide
2396 as part of info register. 00=32, 01=64, 10=128, 11=reserved.
2397
2398 ---
2399
2400 push/pop of vector config state:
2401 <https://groups.google.com/d/msg/comp.arch/bGBeaNjAKvc/z2d_cST7AgAJ>
2402
2403 when Bank in CFG is altered, shift the "addressing" of Reg and
2404 Pred CSRs to match. i.e. treat the Reg and Pred CSRs as a
2405 "mini stack".