add clarification
[libreriscv.git] / simple_v_extension.mdwn
1 # Variable-width Variable-packed SIMD / Simple-V / Parallelism Extension Proposal
2
3 Key insight: Simple-V is intended as an abstraction layer to provide
4 a consistent "API" to parallelisation of existing *and future* operations.
5 *Actual* internal hardware-level parallelism is *not* required, such
6 that Simple-V may be viewed as providing a "compact" or "consolidated"
7 means of issuing multiple near-identical arithmetic instructions to an
8 instruction queue (FIFO), pending execution.
9
10 *Actual* parallelism, if added independently of Simple-V in the form
11 of Out-of-order restructuring (including parallel ALU lanes) or VLIW
12 implementations, or SIMD, or anything else, would then benefit from
13 the uniformity of a consistent API.
14
15 [[!toc ]]
16
17 # Introduction
18
19 This proposal exists so as to be able to satisfy several disparate
20 requirements: power-conscious, area-conscious, and performance-conscious
21 designs all pull an ISA and its implementation in different conflicting
22 directions, as do the specific intended uses for any given implementation.
23
24 The existing P (SIMD) proposal and the V (Vector) proposals,
25 whilst each extremely powerful in their own right and clearly desirable,
26 are also:
27
28 * Clearly independent in their origins (Cray and AndesStar v3 respectively)
29 so need work to adapt to the RISC-V ethos and paradigm
30 * Are sufficiently large so as to make adoption (and exploration for
31 analysis and review purposes) prohibitively expensive
32 * Both contain partial duplication of pre-existing RISC-V instructions
33 (an undesirable characteristic)
34 * Both have independent, incompatible and disparate methods for introducing
35 parallelism at the instruction level
36 * Both require that their respective parallelism paradigm be implemented
37 along-side and integral to their respective functionality *or not at all*.
38 * Both independently have methods for introducing parallelism that
39 could, if separated, benefit
40 *other areas of RISC-V not just DSP or Floating-point respectively*.
41
42 There are also key differences between Vectorisation and SIMD (full
43 details outlined in the Appendix), the key points being:
44
45 * SIMD has an extremely seductively compelling ease of implementation argument:
46 each operation is passed to the ALU, which is where the parallelism
47 lies. There is *negligeable* (if any) impact on the rest of the core
48 (with life instead being made hell for compiler writers and applications
49 writers due to extreme ISA proliferation).
50 * By contrast, Vectorisation has quite some complexity (for considerable
51 flexibility, reduction in opcode proliferation and much more).
52 * Vectorisation typically includes much more comprehensive memory load
53 and store schemes (unit stride, constant-stride and indexed), which
54 in turn have ramifications: virtual memory misses (TLB cache misses)
55 and even multiple page-faults... all caused by a *single instruction*,
56 yet with a clear benefit that the regularisation of LOAD/STOREs can
57 be optimised for minimal impact on caches and maximised throughput.
58 * By contrast, SIMD can use "standard" memory load/stores (32-bit aligned
59 to pages), and these load/stores have absolutely nothing to do with the
60 SIMD / ALU engine, no matter how wide the operand. Simplicity but with
61 more impact on instruction and data caches.
62
63 Overall it makes a huge amount of sense to have a means and method
64 of introducing instruction parallelism in a flexible way that provides
65 implementors with the option to choose exactly where they wish to offer
66 performance improvements and where they wish to optimise for power
67 and/or area (and if that can be offered even on a per-operation basis that
68 would provide even more flexibility).
69
70 Additionally it makes sense to *split out* the parallelism inherent within
71 each of P and V, and to see if each of P and V then, in *combination* with
72 a "best-of-both" parallelism extension, could be added on *on top* of
73 this proposal, to topologically provide the exact same functionality of
74 each of P and V. Each of P and V then can focus on providing the best
75 operations possible for their respective target areas, without being
76 hugely concerned about the actual parallelism.
77
78 Furthermore, an additional goal of this proposal is to reduce the number
79 of opcodes utilised by each of P and V as they currently stand, leveraging
80 existing RISC-V opcodes where possible, and also potentially allowing
81 P and V to make use of Compressed Instructions as a result.
82
83 # Analysis and discussion of Vector vs SIMD
84
85 There are six combined areas between the two proposals that help with
86 parallelism (increased performance, reduced power / area) without
87 over-burdening the ISA with a huge proliferation of
88 instructions:
89
90 * Fixed vs variable parallelism (fixed or variable "M" in SIMD)
91 * Implicit vs fixed instruction bit-width (integral to instruction or not)
92 * Implicit vs explicit type-conversion (compounded on bit-width)
93 * Implicit vs explicit inner loops.
94 * Single-instruction LOAD/STORE.
95 * Masks / tagging (selecting/preventing certain indexed elements from execution)
96
97 The pros and cons of each are discussed and analysed below.
98
99 ## Fixed vs variable parallelism length
100
101 In David Patterson and Andrew Waterman's analysis of SIMD and Vector
102 ISAs, the analysis comes out clearly in favour of (effectively) variable
103 length SIMD. As SIMD is a fixed width, typically 4, 8 or in extreme cases
104 16 or 32 simultaneous operations, the setup, teardown and corner-cases of SIMD
105 are extremely burdensome except for applications whose requirements
106 *specifically* match the *precise and exact* depth of the SIMD engine.
107
108 Thus, SIMD, no matter what width is chosen, is never going to be acceptable
109 for general-purpose computation, and in the context of developing a
110 general-purpose ISA, is never going to satisfy 100 percent of implementors.
111
112 To explain this further: for increased workloads over time, as the
113 performance requirements increase for new target markets, implementors
114 choose to extend the SIMD width (so as to again avoid mixing parallelism
115 into the instruction issue phases: the primary "simplicity" benefit of
116 SIMD in the first place), with the result that the entire opcode space
117 effectively doubles with each new SIMD width that's added to the ISA.
118
119 That basically leaves "variable-length vector" as the clear *general-purpose*
120 winner, at least in terms of greatly simplifying the instruction set,
121 reducing the number of instructions required for any given task, and thus
122 reducing power consumption for the same.
123
124 ## Implicit vs fixed instruction bit-width
125
126 SIMD again has a severe disadvantage here, over Vector: huge proliferation
127 of specialist instructions that target 8-bit, 16-bit, 32-bit, 64-bit, and
128 have to then have operations *for each and between each*. It gets very
129 messy, very quickly: *six* separate dimensions giving an O(N^6) instruction
130 proliferation profile.
131
132 The V-Extension on the other hand proposes to set the bit-width of
133 future instructions on a per-register basis, such that subsequent instructions
134 involving that register are *implicitly* of that particular bit-width until
135 otherwise changed or reset.
136
137 This has some extremely useful properties, without being particularly
138 burdensome to implementations, given that instruction decode already has
139 to direct the operation to a correctly-sized width ALU engine, anyway.
140
141 Not least: in places where an ISA was previously constrained (due for
142 whatever reason, including limitations of the available operand space),
143 implicit bit-width allows the meaning of certain operations to be
144 type-overloaded *without* pollution or alteration of frozen and immutable
145 instructions, in a fully backwards-compatible fashion.
146
147 ## Implicit and explicit type-conversion
148
149 The Draft 2.3 V-extension proposal has (deprecated) polymorphism to help
150 deal with over-population of instructions, such that type-casting from
151 integer (and floating point) of various sizes is automatically inferred
152 due to "type tagging" that is set with a special instruction. A register
153 will be *specifically* marked as "16-bit Floating-Point" and, if added
154 to an operand that is specifically tagged as "32-bit Integer" an implicit
155 type-conversion will take place *without* requiring that type-conversion
156 to be explicitly done with its own separate instruction.
157
158 However, implicit type-conversion is not only quite burdensome to
159 implement (explosion of inferred type-to-type conversion) but also is
160 never really going to be complete. It gets even worse when bit-widths
161 also have to be taken into consideration. Each new type results in
162 an increased O(N^2) conversion space that, as anyone who has examined
163 python's source code (which has built-in polymorphic type-conversion),
164 knows that the task is more complex than it first seems.
165
166 Overall, type-conversion is generally best to leave to explicit
167 type-conversion instructions, or in definite specific use-cases left to
168 be part of an actual instruction (DSP or FP)
169
170 ## Zero-overhead loops vs explicit loops
171
172 The initial Draft P-SIMD Proposal by Chuanhua Chang of Andes Technology
173 contains an extremely interesting feature: zero-overhead loops. This
174 proposal would basically allow an inner loop of instructions to be
175 repeated indefinitely, a fixed number of times.
176
177 Its specific advantage over explicit loops is that the pipeline in a DSP
178 can potentially be kept completely full *even in an in-order single-issue
179 implementation*. Normally, it requires a superscalar architecture and
180 out-of-order execution capabilities to "pre-process" instructions in
181 order to keep ALU pipelines 100% occupied.
182
183 By bringing that capability in, this proposal could offer a way to increase
184 pipeline activity even in simpler implementations in the one key area
185 which really matters: the inner loop.
186
187 However when looking at much more comprehensive schemes
188 "A portable specification of zero-overhead loop control hardware
189 applied to embedded processors" (ZOLC), optimising only the single
190 inner loop seems inadequate, tending to suggest that ZOLC may be
191 better off being proposed as an entirely separate Extension.
192
193 ## Single-instruction LOAD/STORE
194
195 In traditional Vector Architectures there are instructions which
196 result in multiple register-memory transfer operations resulting
197 from a single instruction. They're complicated to implement in hardware,
198 yet the benefits are a huge consistent regularisation of memory accesses
199 that can be highly optimised with respect to both actual memory and any
200 L1, L2 or other caches. In Hwacha EECS-2015-263 it is explicitly made
201 clear the consequences of getting this architecturally wrong:
202 L2 cache-thrashing at the very least.
203
204 Complications arise when Virtual Memory is involved: TLB cache misses
205 need to be dealt with, as do page faults. Some of the tradeoffs are
206 discussed in <http://people.eecs.berkeley.edu/~krste/thesis.pdf>, Section
207 4.6, and an article by Jeff Bush when faced with some of these issues
208 is particularly enlightening
209 <https://jbush001.github.io/2015/11/03/lost-in-translation.html>
210
211 Interestingly, none of this complexity is faced in SIMD architectures...
212 but then they do not get the opportunity to optimise for highly-streamlined
213 memory accesses either.
214
215 With the "bang-per-buck" ratio being so high and the indirect improvement
216 in L1 Instruction Cache usage (reduced instruction count), as well as
217 the opportunity to optimise L1 and L2 cache usage, the case for including
218 Vector LOAD/STORE is compelling.
219
220 ## Mask and Tagging (Predication)
221
222 Tagging (aka Masks aka Predication) is a pseudo-method of implementing
223 simplistic branching in a parallel fashion, by allowing execution on
224 elements of a vector to be switched on or off depending on the results
225 of prior operations in the same array position.
226
227 The reason for considering this is simple: by *definition* it
228 is not possible to perform individual parallel branches in a SIMD
229 (Single-Instruction, **Multiple**-Data) context. Branches (modifying
230 of the Program Counter) will result in *all* parallel data having
231 a different instruction executed on it: that's just the definition of
232 SIMD, and it is simply unavoidable.
233
234 So these are the ways in which conditional execution may be implemented:
235
236 * explicit compare and branch: BNE x, y -> offs would jump offs
237 instructions if x was not equal to y
238 * explicit store of tag condition: CMP x, y -> tagbit
239 * implicit (condition-code) such as ADD results in a carry, carry bit
240 implicitly (or sometimes explicitly) goes into a "tag" (mask) register
241
242 The first of these is a "normal" branch method, which is flat-out impossible
243 to parallelise without look-ahead and effectively rewriting instructions.
244 This would defeat the purpose of RISC.
245
246 The latter two are where parallelism becomes easy to do without complexity:
247 every operation is modified to be "conditionally executed" (in an explicit
248 way directly in the instruction format *or* implicitly).
249
250 RVV (Vector-Extension) proposes to have *explicit* storing of the compare
251 in a tag/mask register, and to *explicitly* have every vector operation
252 *require* that its operation be "predicated" on the bits within an
253 explicitly-named tag/mask register.
254
255 SIMD (P-Extension) has not yet published precise documentation on what its
256 schema is to be: there is however verbal indication at the time of writing
257 that:
258
259 > The "compare" instructions in the DSP/SIMD ISA proposed by Andes will
260 > be executed using the same compare ALU logic for the base ISA with some
261 > minor modifications to handle smaller data types. The function will not
262 > be duplicated.
263
264 This is an *implicit* form of predication as the base RV ISA does not have
265 condition-codes or predication. By adding a CSR it becomes possible
266 to also tag certain registers as "predicated if referenced as a destination".
267 Example:
268
269 // in future operations from now on, if r0 is the destination use r5 as
270 // the PREDICATION register
271 SET_IMPLICIT_CSRPREDICATE r0, r5
272 // store the compares in r5 as the PREDICATION register
273 CMPEQ8 r5, r1, r2
274 // r0 is used here. ah ha! that means it's predicated using r5!
275 ADD8 r0, r1, r3
276
277 With enough registers (and in RISC-V there are enough registers) some fairly
278 complex predication can be set up and yet still execute without significant
279 stalling, even in a simple non-superscalar architecture.
280
281 (For details on how Branch Instructions would be retro-fitted to indirectly
282 predicated equivalents, see Appendix)
283
284 ## Conclusions
285
286 In the above sections the five different ways where parallel instruction
287 execution has closely and loosely inter-related implications for the ISA and
288 for implementors, were outlined. The pluses and minuses came out as
289 follows:
290
291 * Fixed vs variable parallelism: <b>variable</b>
292 * Implicit (indirect) vs fixed (integral) instruction bit-width: <b>indirect</b>
293 * Implicit vs explicit type-conversion: <b>explicit</b>
294 * Implicit vs explicit inner loops: <b>implicit but best done separately</b>
295 * Single-instruction Vector LOAD/STORE: <b>Complex but highly beneficial</b>
296 * Tag or no-tag: <b>Complex but highly beneficial</b>
297
298 In particular:
299
300 * variable-length vectors came out on top because of the high setup, teardown
301 and corner-cases associated with the fixed width of SIMD.
302 * Implicit bit-width helps to extend the ISA to escape from
303 former limitations and restrictions (in a backwards-compatible fashion),
304 whilst also leaving implementors free to simmplify implementations
305 by using actual explicit internal parallelism.
306 * Implicit (zero-overhead) loops provide a means to keep pipelines
307 potentially 100% occupied in a single-issue in-order implementation
308 i.e. *without* requiring a super-scalar or out-of-order architecture,
309 but doing a proper, full job (ZOLC) is an entirely different matter.
310
311 Constructing a SIMD/Simple-Vector proposal based around four of these six
312 requirements would therefore seem to be a logical thing to do.
313
314 # Note on implementation of parallelism
315
316 One extremely important aspect of this proposal is to respect and support
317 implementors desire to focus on power, area or performance. In that regard,
318 it is proposed that implementors be free to choose whether to implement
319 the Vector (or variable-width SIMD) parallelism as sequential operations
320 with a single ALU, fully parallel (if practical) with multiple ALUs, or
321 a hybrid combination of both.
322
323 In Broadcom's Videocore-IV, they chose hybrid, and called it "Virtual
324 Parallelism". They achieve a 16-way SIMD at an **instruction** level
325 by providing a combination of a 4-way parallel ALU *and* an externally
326 transparent loop that feeds 4 sequential sets of data into each of the
327 4 ALUs.
328
329 Also in the same core, it is worth noting that particularly uncommon
330 but essential operations (Reciprocal-Square-Root for example) are
331 *not* part of the 4-way parallel ALU but instead have a *single* ALU.
332 Under the proposed Vector (varible-width SIMD) implementors would
333 be free to do precisely that: i.e. free to choose *on a per operation
334 basis* whether and how much "Virtual Parallelism" to deploy.
335
336 It is absolutely critical to note that it is proposed that such choices MUST
337 be **entirely transparent** to the end-user and the compiler. Whilst
338 a Vector (varible-width SIMD) may not precisely match the width of the
339 parallelism within the implementation, the end-user **should not care**
340 and in this way the performance benefits are gained but the ISA remains
341 straightforward. All that happens at the end of an instruction run is: some
342 parallel units (if there are any) would remain offline, completely
343 transparently to the ISA, the program, and the compiler.
344
345 To make that clear: should an implementor choose a particularly wide
346 SIMD-style ALU, each parallel unit *must* have predication so that
347 the parallel SIMD ALU may emulate variable-length parallel operations.
348 Thus the "SIMD considered harmful" trap of having huge complexity and extra
349 instructions to deal with corner-cases is thus avoided, and implementors
350 get to choose precisely where to focus and target the benefits of their
351 implementation efforts, without "extra baggage".
352
353 In addition, implementors will be free to choose whether to provide an
354 absolute bare minimum level of compliance with the "API" (software-traps
355 when vectorisation is detected), all the way up to full supercomputing
356 level all-hardware parallelism. Options are covered in the Appendix.
357
358 # CSRs <a name="csrs"></a>
359
360 There are two CSR tables needed to create lookup tables which are used at
361 the register decode phase.
362
363 * Integer Register N is Vector
364 * Integer Register N is of implicit bitwidth M (M=default,8,16,32,64)
365 * Floating-point Register N is Vector of length M: r(N) -> r(N..N+M-1)
366 * Floating-point Register N is of implicit bitwidth M (M=default,8,16,32,64)
367 * Integer Register N is a Predication Register (note: a key-value store)
368
369 Also (see Appendix, "Context Switch Example") it may turn out to be important
370 to have a separate (smaller) set of CSRs for M-Mode (and S-Mode) so that
371 Vectorised LOAD / STORE may be used to load and store multiple registers:
372 something that is missing from the Base RV ISA.
373
374 Notes:
375
376 * for the purposes of LOAD / STORE, Integer Registers which are
377 marked as a Vector will result in a Vector LOAD / STORE.
378 * Vector Lengths are *not* the same as vsetl but are an integral part
379 of vsetl.
380 * Actual vector length is *multipled* by how many blocks of length
381 "bitwidth" may fit into an XLEN-sized register file.
382 * Predication is a key-value store due to the implicit referencing,
383 as opposed to having the predicate register explicitly in the instruction.
384 * Whilst the predication CSR is a key-value store it *generates* easier-to-use
385 state information.
386 * TODO: assess whether the same technique could be applied to the other
387 Vector CSRs, particularly as pointed out in Section 17.8 (Draft RV 0.4,
388 V2.3-Draft ISA Reference) it becomes possible to greatly reduce state
389 needed for context-switches (empty slots need never be stored).
390
391 ## Predication CSR <a name="predication_csr_table"></a>
392
393 The Predication CSR is a key-value store indicating whether, if a given
394 destination register (integer or floating-point) is referred to in an
395 instruction, it is to be predicated. However it is important to note
396 that the *actual* register is *different* from the one that ends up
397 being used, due to the level of indirection through the lookup table.
398 This includes (in the future) redirecting to a *second* bank of
399 integer registers (as a future option)
400
401 * regidx is the actual register that in combination with the
402 i/f flag, if that integer or floating-point register is referred to,
403 results in the lookup table being referenced to find the predication
404 mask to use on the operation in which that (regidx) register has
405 been used
406 * predidx (in combination with the bank bit in the future) is the
407 *actual* register to be used for the predication mask. Note:
408 in effect predidx is actually a 6-bit register address, as the bank
409 bit is the MSB (and is nominally set to zero for now).
410 * inv indicates that the predication mask bits are to be inverted
411 prior to use *without* actually modifying the contents of the
412 register itself.
413 * zeroing is either 1 or 0, and if set to 1, the operation must
414 place zeros in any element position where the predication mask is
415 set to zero. If zeroing is set to 1, unpredicated elements *must*
416 be left alone. Some microarchitectures may choose to interpret
417 this as skipping the operation entirely. Others which wish to
418 stick more closely to a SIMD architecture may choose instead to
419 interpret unpredicated elements as an internal "copy element"
420 operation (which would be necessary in SIMD microarchitectures
421 that perform register-renaming)
422
423 | PrCSR | 13 | 12 | 11 | 10 | (9..5) | (4..0) |
424 | ----- | - | - | - | - | ------- | ------- |
425 | 0 | bank0 | zero0 | inv0 | i/f | regidx | predidx |
426 | 1 | bank1 | zero1 | inv1 | i/f | regidx | predidx |
427 | .. | bank.. | zero.. | inv.. | i/f | regidx | predidx |
428 | 15 | bank15 | zero15 | inv15 | i/f | regidx | predidx |
429
430 The Predication CSR Table is a key-value store, so implementation-wise
431 it will be faster to turn the table around (maintain topologically
432 equivalent state):
433
434 struct pred {
435 bool zero;
436 bool inv;
437 bool bank; // 0 for now, 1=rsvd
438 bool enabled;
439 int predidx; // redirection: actual int register to use
440 }
441
442 struct pred fp_pred_reg[32]; // 64 in future (bank=1)
443 struct pred int_pred_reg[32]; // 64 in future (bank=1)
444
445 for (i = 0; i < 16; i++)
446 tb = int_pred_reg if CSRpred[i].type == 0 else fp_pred_reg;
447 idx = CSRpred[i].regidx
448 tb[idx].zero = CSRpred[i].zero
449 tb[idx].inv = CSRpred[i].inv
450 tb[idx].bank = CSRpred[i].bank
451 tb[idx].predidx = CSRpred[i].predidx
452 tb[idx].enabled = true
453
454 So when an operation is to be predicated, it is the internal state that
455 is used. In Section 6.4.2 of Hwacha's Manual (EECS-2015-262) the following
456 pseudo-code for operations is given, where p is the explicit (direct)
457 reference to the predication register to be used:
458
459 for (int i=0; i<vl; ++i)
460 if ([!]preg[p][i])
461 (d ? vreg[rd][i] : sreg[rd]) =
462 iop(s1 ? vreg[rs1][i] : sreg[rs1],
463 s2 ? vreg[rs2][i] : sreg[rs2]); // for insts with 2 inputs
464
465 This instead becomes an *indirect* reference using the *internal* state
466 table generated from the Predication CSR key-value store, which iwws used
467 as follows.
468
469 if type(iop) == INT:
470 preg = int_pred_reg[rd]
471 else:
472 preg = fp_pred_reg[rd]
473
474 for (int i=0; i<vl; ++i)
475 predidx = preg[rd].predidx; // the indirection takes place HERE
476 if (!preg[rd].enabled)
477 predicate = ~0x0; // all parallel ops enabled
478 else:
479 predicate = intregfile[predidx]; // get actual reg contents HERE
480 if (preg[rd].inv) // invert if requested
481 predicate = ~predicate;
482 if (predicate && (1<<i))
483 (d ? regfile[rd+i] : regfile[rd]) =
484 iop(s1 ? regfile[rs1+i] : regfile[rs1],
485 s2 ? regfile[rs2+i] : regfile[rs2]); // for insts with 2 inputs
486 else if (preg[rd].zero)
487 // TODO: place zero in dest reg
488
489 Note:
490
491 * d, s1 and s2 are booleans indicating whether destination,
492 source1 and source2 are vector or scalar
493 * key-value CSR-redirection of rd, rs1 and rs2 have NOT been included
494 above, for clarity. rd, rs1 and rs2 all also must ALSO go through
495 register-level redirection (from the Register CSR table) if they are
496 vectors.
497
498 If written as a function, obtaining the predication mask (but not whether
499 zeroing takes place) may be done as follows:
500
501 def get_pred_val(bool is_fp_op, int reg):
502 tb = int_pred if is_fp_op else fp_pred
503 if (!tb[reg].enabled):
504 return ~0x0 // all ops enabled
505 predidx = tb[reg].predidx // redirection occurs HERE
506 predicate = intreg[predidx] // actual predicate HERE
507 if (tb[reg].inv):
508 predicate = ~predicate // invert ALL bits
509 return predicate
510
511 ## MAXVECTORLENGTH
512
513 MAXVECTORLENGTH is the same concept as MVL in RVV. However in Simple-V,
514 given that its primary (base, unextended) purpose is for 3D, Video and
515 other purposes (not requiring supercomputing capability), it makes sense
516 to limit MAXVECTORDEPTH to the regfile bitwidth (32 for RV32, 64 for RV64
517 and so on).
518
519 The reason for setting this limit is so that predication registers, when
520 marked as such, may fit into a single register as opposed to fanning out
521 over several registers. This keeps the implementation a little simpler.
522 Note also (as also described in the VSETVL section) that the *minimum*
523 for MAXVECTORDEPTH must be the total number of registers (15 for RV32E
524 and 31 for RV32 or RV64).
525
526 Note that RVV on top of Simple-V may choose to over-ride this decision.
527
528 ## Register CSR key-value (CAM) table
529
530 The purpose of the Register CSR table is four-fold:
531
532 * To mark integer and floating-point registers as requiring "redirection"
533 if it is ever used as a source or destination in any given operation.
534 This involves a level of indirection through a 5-to-6-bit lookup table
535 (where the 6th bit - bank - is always set to 0 for now).
536 * To indicate whether, after redirection through the lookup table, the
537 register is a vector (or remains a scalar).
538 * To over-ride the implicit or explicit bitwidth that the operation would
539 normally give the register.
540 * To indicate if the register is to be interpreted as "packed" (SIMD)
541 i.e. containing multiple contiguous elements of size equal to "bitwidth".
542
543 | RgCSR | 15 | 14 | 13 | (12..11) | 10 | (9..5) | (4..0) |
544 | ----- | - | - | - | - | - | ------- | ------- |
545 | 0 | simd0 | bank0 | isvec0 | vew0 | i/f | regidx | predidx |
546 | 1 | simd1 | bank1 | isvec1 | vew1 | i/f | regidx | predidx |
547 | .. | simd.. | bank.. | isvec.. | vew.. | i/f | regidx | predidx |
548 | 15 | simd15 | bank15 | isvec15 | vew15 | i/f | regidx | predidx |
549
550 vew may be one of the following (giving a table "bytestable", used below):
551
552 | vew | bitwidth |
553 | --- | --------- |
554 | 00 | default |
555 | 01 | default/2 |
556 | 10 | 8 |
557 | 11 | 16 |
558
559 Extending this table (with extra bits) is covered in the section
560 "Implementing RVV on top of Simple-V".
561
562 As the above table is a CAM (key-value store) it may be appropriate
563 to expand it as follows:
564
565 struct vectorised fp_vec[32], int_vec[32]; // 64 in future
566
567 for (i = 0; i < 16; i++) // 16 CSRs?
568 tb = int_vec if CSRvec[i].type == 0 else fp_vec
569 idx = CSRvec[i].regkey // INT/FP src/dst reg in opcode
570 tb[idx].elwidth = CSRvec[i].elwidth
571 tb[idx].regidx = CSRvec[i].regidx // indirection
572 tb[idx].isvector = CSRvec[i].isvector // 0=scalar
573 tb[idx].packed = CSRvec[i].packed // SIMD or not
574 tb[idx].bank = CSRvec[i].bank // 0 (1=rsvd)
575
576 TODO: move elsewhere
577
578 # TODO: use elsewhere (retire for now)
579 vew = CSRbitwidth[rs1]
580 if (vew == 0)
581 bytesperreg = (XLEN/8) # or FLEN as appropriate
582 elif (vew == 1)
583 bytesperreg = (XLEN/4) # or FLEN/2 as appropriate
584 else:
585 bytesperreg = bytestable[vew] # 8 or 16
586 simdmult = (XLEN/8) / bytesperreg # or FLEN as appropriate
587 vlen = CSRvectorlen[rs1] * simdmult
588 CSRvlength = MIN(MIN(vlen, MAXVECTORDEPTH), rs2)
589
590 The reason for multiplying the vector length by the number of SIMD elements
591 (in each individual register) is so that each SIMD element may optionally be
592 predicated.
593
594 An example of how to subdivide the register file when bitwidth != default
595 is given in the section "Bitwidth Virtual Register Reordering".
596
597 # Instructions
598
599 Despite being a 98% complete and accurate topological remap of RVV
600 concepts and functionality, the only instructions needed are VSETVL
601 and VGETVL. *All* RVV instructions can be re-mapped, however xBitManip
602 becomes a critical dependency for efficient manipulation of predication
603 masks (as a bit-field). Despite the removal of all but VSETVL and VGETVL,
604 *all instructions from RVV are topologically re-mapped and retain their
605 complete functionality, intact*.
606
607 Three instructions, VSELECT, VCLIP and VCLIPI, do not have RV Standard
608 equivalents, so are left out of Simple-V. VSELECT could be included if
609 there existed a MV.X instruction in RV (MV.X is a hypothetical
610 non-immediate variant of MV that would allow another register to
611 specify which register was to be copied). Note that if any of these three
612 instructions are added to any given RV extension, their functionality
613 will be inherently parallelised.
614
615 ## Instruction Format
616
617 The instruction format for Simple-V does not actually have *any* explicit
618 compare operations, *any* arithmetic, floating point or *any*
619 memory instructions.
620 Instead it *overloads* pre-existing branch operations into predicated
621 variants, and implicitly overloads arithmetic operations, MV,
622 FCVT, and LOAD/STORE
623 depending on CSR configurations for bitwidth and
624 predication. **Everything** becomes parallelised. *This includes
625 Compressed instructions* as well as any
626 future instructions and Custom Extensions.
627
628 * For analysis of RVV see [[v_comparative_analysis]] which begins to
629 outline topologically-equivalent mappings of instructions
630 * Also see Appendix "Retro-fitting Predication into branch-explicit ISA"
631 for format of Branch opcodes.
632
633 **TODO**: *analyse and decide whether the implicit nature of predication
634 as proposed is or is not a lot of hassle, and if explicit prefixes are
635 a better idea instead. Parallelism therefore effectively may end up
636 as always being 64-bit opcodes (32 for the prefix, 32 for the instruction)
637 with some opportunities for to use Compressed bringing it down to 48.
638 Also to consider is whether one or both of the last two remaining Compressed
639 instruction codes in Quadrant 1 could be used as a parallelism prefix,
640 bringing parallelised opcodes down to 32-bit (when combined with C)
641 and having the benefit of being explicit.*
642
643 ## VSETVL
644
645 NOTE TODO: 28may2018: VSETVL may need to be *really* different from RVV,
646 with the instruction format remaining the same.
647
648 VSETVL is slightly different from RVV in that the minimum vector length
649 is required to be at least the number of registers in the register file,
650 and no more than XLEN. This allows vector LOAD/STORE to be used to switch
651 the entire bank of registers using a single instruction (see Appendix,
652 "Context Switch Example"). The reason for limiting VSETVL to XLEN is
653 down to the fact that predication bits fit into a single register of length
654 XLEN bits.
655
656 The second change is that when VSETVL is requested to be stored
657 into x0, it is *ignored* silently (VSETVL x0, x5, #4)
658
659 The third change is that there is an additional immediate added to VSETVL,
660 to which VL is set after first going through MIN-filtering.
661 So When using the "vsetl rs1, rs2, #vlen" instruction, it becomes:
662
663 VL = MIN(MIN(vlen, MAXVECTORDEPTH), rs2)
664
665 where RegfileLen <= MAXVECTORDEPTH < XLEN
666
667 This has implication for the microarchitecture, as VL is required to be
668 set (limits from MAXVECTORDEPTH notwithstanding) to the actual value
669 requested in the #immediate parameter. RVV has the option to set VL
670 to an arbitrary value that suits the conditions and the micro-architecture:
671 SV does *not* permit that.
672
673 The reason is so that if SV is to be used for a context-switch or as a
674 substitute for LOAD/STORE-Multiple, the operation can be done with only
675 2-3 instructions (setup of the CSRs, VSETVL x0, x0, #{regfilelen-1},
676 single LD/ST operation). If VL does *not* get set to the register file
677 length when VSETVL is called, then a software-loop would be needed.
678 To avoid this need, VL *must* be set to exactly what is requested
679 (limits notwithstanding).
680
681 Therefore, in turn, unlike RVV, implementors *must* provide
682 pseudo-parallelism (using sequential loops in hardware) if actual
683 hardware-parallelism in the ALUs is not deployed. A hybrid is also
684 permitted (as used in Broadcom's VideoCore-IV) however this must be
685 *entirely* transparent to the ISA.
686
687 ## Branch Instruction:
688
689 Branch operations use standard RV opcodes that are reinterpreted to
690 be "predicate variants" in the instance where either of the two src
691 registers are marked as vectors (isvector=1). When this reinterpretation
692 is enabled the "immediate" field of the branch operation is taken to be a
693 predication target register, rs3. The predicate target register rs3 is
694 to be treated as a bitfield (up to a maximum of XLEN bits corresponding
695 to a maximum of XLEN elements).
696
697 If either of src1 or src2 are scalars (CSRvectorlen[src] == 0) the comparison
698 goes ahead as vector-scalar or scalar-vector. Implementors should note that
699 this could require considerable multi-porting of the register file in order
700 to parallelise properly, so may have to involve the use of register cacheing
701 and transparent copying (see Multiple-Banked Register File Architectures
702 paper).
703
704 In instances where no vectorisation is detected on either src registers
705 the operation is treated as an absolutely standard scalar branch operation.
706
707 This is the overloaded table for Integer-base Branch operations. Opcode
708 (bits 6..0) is set in all cases to 1100011.
709
710 [[!table data="""
711 31 .. 25 |24 ... 20 | 19 15 | 14 12 | 11 .. 8 | 7 | 6 ... 0 |
712 imm[12,10:5]| rs2 | rs1 | funct3 | imm[4:1] | imm[11] | opcode |
713 7 | 5 | 5 | 3 | 4 | 1 | 7 |
714 reserved | src2 | src1 | BPR | predicate rs3 || BRANCH |
715 reserved | src2 | src1 | 000 | predicate rs3 || BEQ |
716 reserved | src2 | src1 | 001 | predicate rs3 || BNE |
717 reserved | src2 | src1 | 010 | predicate rs3 || rsvd |
718 reserved | src2 | src1 | 011 | predicate rs3 || rsvd |
719 reserved | src2 | src1 | 100 | predicate rs3 || BLE |
720 reserved | src2 | src1 | 101 | predicate rs3 || BGE |
721 reserved | src2 | src1 | 110 | predicate rs3 || BLTU |
722 reserved | src2 | src1 | 111 | predicate rs3 || BGEU |
723 """]]
724
725 Note that just as with the standard (scalar, non-predicated) branch
726 operations, BLT, BGT, BLEU and BTGU may be synthesised by inverting
727 src1 and src2.
728
729 Below is the overloaded table for Floating-point Predication operations.
730 Interestingly no change is needed to the instruction format because
731 FP Compare already stores a 1 or a zero in its "rd" integer register
732 target, i.e. it's not actually a Branch at all: it's a compare.
733 The target needs to simply change to be a predication bitfield (done
734 implicitly).
735
736 As with
737 Standard RVF/D/Q, Opcode (bits 6..0) is set in all cases to 1010011.
738 Likewise Single-precision, fmt bits 26..25) is still set to 00.
739 Double-precision is still set to 01, whilst Quad-precision
740 appears not to have a definition in V2.3-Draft (but should be unaffected).
741
742 It is however noted that an entry "FNE" (the opposite of FEQ) is missing,
743 and whilst in ordinary branch code this is fine because the standard
744 RVF compare can always be followed up with an integer BEQ or a BNE (or
745 a compressed comparison to zero or non-zero), in predication terms that
746 becomes more of an impact. To deal with this, SV's predication has
747 had "invert" added to it.
748
749 [[!table data="""
750 31 .. 27| 26 .. 25 |24 ... 20 | 19 15 | 14 12 | 11 .. 7 | 6 ... 0 |
751 funct5 | fmt | rs2 | rs1 | funct3 | rd | opcode |
752 5 | 2 | 5 | 5 | 3 | 4 | 7 |
753 10100 | 00/01/11 | src2 | src1 | 010 | pred rs3 | FEQ |
754 10100 | 00/01/11 | src2 | src1 | **011**| pred rs3 | rsvd |
755 10100 | 00/01/11 | src2 | src1 | 001 | pred rs3 | FLT |
756 10100 | 00/01/11 | src2 | src1 | 000 | pred rs3 | FLE |
757 """]]
758
759 Note (**TBD**): floating-point exceptions will need to be extended
760 to cater for multiple exceptions (and statuses of the same). The
761 usual approach is to have an array of status codes and bit-fields,
762 and one exception, rather than throw separate exceptions for each
763 Vector element.
764
765 In Hwacha EECS-2015-262 Section 6.7.2 the following pseudocode is given
766 for predicated compare operations of function "cmp":
767
768 for (int i=0; i<vl; ++i)
769 if ([!]preg[p][i])
770 preg[pd][i] = cmp(s1 ? vreg[rs1][i] : sreg[rs1],
771 s2 ? vreg[rs2][i] : sreg[rs2]);
772
773 With associated predication, vector-length adjustments and so on,
774 and temporarily ignoring bitwidth (which makes the comparisons more
775 complex), this becomes:
776
777 if I/F == INT: # integer type cmp
778 preg = int_pred_reg[rd]
779 reg = int_regfile
780 else:
781 preg = fp_pred_reg[rd]
782 reg = fp_regfile
783
784 s1 = reg_is_vectorised(src1);
785 s2 = reg_is_vectorised(src2);
786 if (!s2 && !s1) goto branch;
787 for (int i = 0; i < VL; ++i)
788 if (cmp(s1 ? reg[src1+i]:reg[src1],
789 s2 ? reg[src2+i]:reg[src2])
790 preg[rs3] |= 1<<i; # bitfield not vector
791
792 Notes:
793
794 * Predicated SIMD comparisons would break src1 and src2 further down
795 into bitwidth-sized chunks (see Appendix "Bitwidth Virtual Register
796 Reordering") setting Vector-Length times (number of SIMD elements) bits
797 in Predicate Register rs3 as opposed to just Vector-Length bits.
798 * Predicated Branches do not actually have an adjustment to the Program
799 Counter, so all of bits 25 through 30 in every case are not needed.
800 * There are plenty of reserved opcodes for which bits 25 through 30 could
801 be put to good use if there is a suitable use-case.
802 FLT and FLE may be inverted to FGT and FGE if needed by swapping
803 src1 and src2 (likewise the integer counterparts).
804
805 ## Compressed Branch Instruction:
806
807 Compressed Branch instructions are likewise re-interpreted as predicated
808 2-register operations, with the result going into rs3. All the bits of
809 the immediate are re-interpreted for different purposes, to extend the
810 number of comparator operations to beyond the original specification,
811 but also to cater for floating-point comparisons as well as integer ones.
812
813 [[!table data="""
814 15..13 | 12...10 | 9..7 | 6..5 | 4..2 | 1..0 | name |
815 funct3 | imm | rs10 | imm | | op | |
816 3 | 3 | 3 | 2 | 3 | 2 | |
817 C.BPR | pred rs3 | src1 | I/F B | src2 | C1 | |
818 110 | pred rs3 | src1 | I/F 0 | src2 | C1 | P.EQ |
819 111 | pred rs3 | src1 | I/F 0 | src2 | C1 | P.NE |
820 110 | pred rs3 | src1 | I/F 1 | src2 | C1 | P.LT |
821 111 | pred rs3 | src1 | I/F 1 | src2 | C1 | P.LE |
822 """]]
823
824 Notes:
825
826 * Bits 5 13 14 and 15 make up the comparator type
827 * Bit 6 indicates whether to use integer or floating-point comparisons
828 * In both floating-point and integer cases there are four predication
829 comparators: EQ/NEQ/LT/LE (with GT and GE being synthesised by inverting
830 src1 and src2).
831
832 ## LOAD / STORE Instructions <a name="load_store"></a>
833
834 For full analysis of topological adaptation of RVV LOAD/STORE
835 see [[v_comparative_analysis]]. All three types (LD, LD.S and LD.X)
836 may be implicitly overloaded into the one base RV LOAD instruction,
837 and likewise for STORE.
838
839 Revised LOAD:
840
841 [[!table data="""
842 31 | 30 | 29 25 | 24 20 | 19 15 | 14 12 | 11 7 | 6 0 |
843 imm[11:0] |||| rs1 | funct3 | rd | opcode |
844 1 | 1 | 5 | 5 | 5 | 3 | 5 | 7 |
845 ? | s | rs2 | imm[4:0] | base | width | dest | LOAD |
846 """]]
847
848 The exact same corresponding adaptation is also carried out on the single,
849 double and quad precision floating-point LOAD-FP and STORE-FP operations,
850 which fit the exact same instruction format. Thus all three types
851 (unit, stride and indexed) may be fitted into FLW, FLD and FLQ,
852 as well as FSW, FSD and FSQ.
853
854 Notes:
855
856 * LOAD remains functionally (topologically) identical to RVV LOAD
857 (for both integer and floating-point variants).
858 * Predication CSR-marking register is not explicitly shown in instruction, it's
859 implicit based on the CSR predicate state for the rd (destination) register
860 * rs2, the source, may *also be marked as a vector*, which implicitly
861 is taken to indicate "Indexed Load" (LD.X)
862 * Bit 30 indicates "element stride" or "constant-stride" (LD or LD.S)
863 * Bit 31 is reserved (ideas under consideration: auto-increment)
864 * **TODO**: include CSR SIMD bitwidth in the pseudo-code below.
865 * **TODO**: clarify where width maps to elsize
866
867 Pseudo-code (excludes CSR SIMD bitwidth for simplicity):
868
869 if (unit-strided) stride = elsize;
870 else stride = areg[as2]; // constant-strided
871
872 preg = int_pred_reg[rd]
873
874 for (int i=0; i<vl; ++i)
875 if ([!]preg[rd] & 1<<i)
876 for (int j=0; j<seglen+1; j++)
877 {
878 if CSRvectorised[rs2])
879 offs = vreg[rs2+i]
880 else
881 offs = i*(seglen+1)*stride;
882 vreg[rd+j][i] = mem[sreg[base] + offs + j*stride];
883 }
884
885 Taking CSR (SIMD) bitwidth into account involves using the vector
886 length and register encoding according to the "Bitwidth Virtual Register
887 Reordering" scheme shown in the Appendix (see function "regoffs").
888
889 A similar instruction exists for STORE, with identical topological
890 translation of all features. **TODO**
891
892 ## Compressed LOAD / STORE Instructions
893
894 Compressed LOAD and STORE are of the same format, where bits 2-4 are
895 a src register instead of dest:
896
897 [[!table data="""
898 15 13 | 12 10 | 9 7 | 6 5 | 4 2 | 1 0 |
899 funct3 | imm | rs10 | imm | rd0 | op |
900 3 | 3 | 3 | 2 | 3 | 2 |
901 C.LW | offset[5:3] | base | offset[2|6] | dest | C0 |
902 """]]
903
904 Unfortunately it is not possible to fit the full functionality
905 of vectorised LOAD / STORE into C.LD / C.ST: the "X" variants (Indexed)
906 require another operand (rs2) in addition to the operand width
907 (which is also missing), offset, base, and src/dest.
908
909 However a close approximation may be achieved by taking the top bit
910 of the offset in each of the five types of LD (and ST), reducing the
911 offset to 4 bits and utilising the 5th bit to indicate whether "stride"
912 is to be enabled. In this way it is at least possible to introduce
913 that functionality.
914
915 (**TODO**: *assess whether the loss of one bit from offset is worth having
916 "stride" capability.*)
917
918 We also assume (including for the "stride" variant) that the "width"
919 parameter, which is missing, is derived and implicit, just as it is
920 with the standard Compressed LOAD/STORE instructions. For C.LW, C.LD
921 and C.LQ, the width is implicitly 4, 8 and 16 respectively, whilst for
922 C.FLW and C.FLD the width is implicitly 4 and 8 respectively.
923
924 Interestingly we note that the Vectorised Simple-V variant of
925 LOAD/STORE (Compressed and otherwise), due to it effectively using the
926 standard register file(s), is the direct functional equivalent of
927 standard load-multiple and store-multiple instructions found in other
928 processors.
929
930 In Section 12.3 riscv-isa manual V2.3-draft it is noted the comments on
931 page 76, "For virtual memory systems some data accesses could be resident
932 in physical memory and some not". The interesting question then arises:
933 how does RVV deal with the exact same scenario?
934 Expired U.S. Patent 5895501 (Filing Date Sep 3 1996) describes a method
935 of detecting early page / segmentation faults and adjusting the TLB
936 in advance, accordingly: other strategies are explored in the Appendix
937 Section "Virtual Memory Page Faults".
938
939 ## Vectorised Copy/Move (and conversion) instructions
940
941 There is a series of 2-operand instructions involving copying (and
942 alteration): C.MV, FMV, FNEG, FABS, FCVT, FSGNJ. These operations all
943 follow the same pattern, as it is *both* the source *and* destination
944 predication masks that are taken into account. This is different from
945 the three-operand arithmetic instructions, where the predication mask
946 is taken from the *destination* register, and applied uniformly to the
947 elements of the source register(s), element-for-element.
948
949 ### C.MV Instruction <a name="c_mv"></a>
950
951 There is no MV instruction in RV however there is a C.MV instruction.
952 It is used for copying integer-to-integer registers (vectorised FMV
953 is used for copying floating-point).
954
955 If either the source or the destination register are marked as vectors
956 C.MV is reinterpreted to be a vectorised (multi-register) predicated
957 move operation. The actual instruction's format does not change:
958
959 [[!table data="""
960 15 12 | 11 7 | 6 2 | 1 0 |
961 funct4 | rd | rs | op |
962 4 | 5 | 5 | 2 |
963 C.MV | dest | src | C0 |
964 """]]
965
966 A simplified version of the pseudocode for this operation is as follows:
967
968 function op_mv(rd, rs) # MV not VMV!
969  rd = int_vec[rd].isvector ? int_vec[rd].regidx : rd;
970  rs = int_vec[rs].isvector ? int_vec[rs].regidx : rs;
971  ps = get_pred_val(FALSE, rs); # predication on src
972  pd = get_pred_val(FALSE, rd); # ... AND on dest
973  for (int i = 0, int j = 0; i < VL && j < VL;):
974 if (int_vec[rs].isvec) while (!(ps & 1<<i)) i++;
975 if (int_vec[rd].isvec) while (!(pd & 1<<j)) j++;
976 ireg[rd+j] <= ireg[rs+i];
977 if (int_vec[rs].isvec) i++;
978 if (int_vec[rd].isvec) j++;
979
980 Note that:
981
982 * elwidth (SIMD) is not covered above
983 * ending the loop early in scalar cases (VINSERT, VEXTRACT) is also
984 not covered
985
986 There are several different instructions from RVV that are covered by
987 this one opcode:
988
989 [[!table data="""
990 src | dest | predication | op |
991 scalar | vector | none | VSPLAT |
992 scalar | vector | destination | sparse VSPLAT |
993 scalar | vector | 1-bit dest | VINSERT |
994 vector | scalar | 1-bit? src | VEXTRACT |
995 vector | vector | none | VCOPY |
996 vector | vector | src | Vector Gather |
997 vector | vector | dest | Vector Scatter |
998 vector | vector | src & dest | Gather/Scatter |
999 vector | vector | src == dest | sparse VCOPY |
1000 """]]
1001
1002 Also, VMERGE may be implemented as back-to-back (macro-op fused) C.MV
1003 operations with inversion on the src and dest predication for one of the
1004 two C.MV operations.
1005
1006 Note that in the instance where the Compressed Extension is not implemented,
1007 MV may be used, but that is a pseudo-operation mapping to addi rd, x0, rs.
1008 Note that the behaviour is **different** from C.MV because with addi the
1009 predication mask to use is taken **only** from rd and is applied against
1010 all elements: rs[i] = rd[i].
1011
1012 ### FMV, FNEG and FABS Instructions
1013
1014 These are identical in form to C.MV, except covering floating-point
1015 register copying. The same double-predication rules also apply.
1016 However when elwidth is not set to default the instruction is implicitly
1017 and automatic converted to a (vectorised) floating-point type conversion
1018 operation of the appropriate size covering the source and destination
1019 register bitwidths.
1020
1021 (Note that FMV, FNEG and FABS are all actually pseudo-instructions)
1022
1023 ### FVCT Instructions
1024
1025 These are again identical in form to C.MV, except that they cover
1026 floating-point to integer and integer to floating-point. When element
1027 width in each vector is set to default, the instructions behave exactly
1028 as they are defined for standard RV (scalar) operations, except vectorised
1029 in exactly the same fashion as outlined in C.MV.
1030
1031 However when the source or destination element width is not set to default,
1032 the opcode's explicit element widths are *over-ridden* to new definitions,
1033 and the opcode's element width is taken as indicative of the SIMD width
1034 (if applicable i.e. if packed SIMD is requested) instead.
1035
1036 For example FCVT.S.L would normally be used to convert a 64-bit
1037 integer in register rs1 to a 64-bit floating-point number in rd.
1038 If however the source rs1 is set to be a vector, where elwidth is set to
1039 default/2 and "packed SIMD" is enabled, then the first 32 bits of
1040 rs1 are converted to a floating-point number to be stored in rd's
1041 first element and the higher 32-bits *also* converted to floating-point
1042 and stored in the second. The 32 bit size comes from the fact that
1043 FCVT.S.L's integer width is 64 bit, and with elwidth on rs1 set to
1044 divide that by two it means that rs1 element width is to be taken as 32.
1045
1046 Similar rules apply to the destination register.
1047
1048 # Exceptions
1049
1050 > What does an ADD of two different-sized vectors do in simple-V?
1051
1052 * if the two source operands are not the same, throw an exception.
1053 * if the destination operand is also a vector, and the source is longer
1054 than the destination, throw an exception.
1055
1056 > And what about instructions like JALR? 
1057 > What does jumping to a vector do?
1058
1059 * Throw an exception. Whether that actually results in spawning threads
1060 as part of the trap-handling remains to be seen.
1061
1062 # Under consideration <a name="issues"></a>
1063
1064 From the Chennai 2018 slides the following issues were raised.
1065 Efforts to analyse and answer these questions are below.
1066
1067 * Should future extra bank be included now?
1068 * How many Register and Predication CSRs should there be?
1069 (and how many in RV32E)
1070 * How many in M-Mode (for doing context-switch)?
1071 * Should use of registers be allowed to "wrap" (x30 x31 x1 x2)?
1072 * Can CLIP be done as a CSR (mode, like elwidth)
1073 * SIMD saturation (etc.) also set as a mode?
1074 * Include src1/src2 predication on Comparison Ops?
1075 (same arrangement as C.MV, with same flexibility/power)
1076 * 8/16-bit ops is it worthwhile adding a "start offset"?
1077 (a bit like misaligned addressing... for registers)
1078 or just use predication to skip start?
1079
1080 ## Future (extra) bank be included (made mandatory)
1081
1082 The implications of expanding the *standard* register file from
1083 32 entries per bank to 64 per bank is quite an extensive architectural
1084 change. Also it has implications for context-switching.
1085
1086 Therefore, on balance, it is not recommended and certainly should
1087 not be made a *mandatory* requirement for the use of SV. SV's design
1088 ethos is to be minimally-disruptive for implementors to shoe-horn
1089 into an existing design.
1090
1091 ## How large should the Register and Predication CSR key-value stores be?
1092
1093 This is something that definitely needs actual evaluation and for
1094 code to be run and the results analysed. At the time of writing
1095 (12jul2018) that is too early to tell. An approximate best-guess
1096 however would be 16 entries.
1097
1098 RV32E however is a special case, given that it is highly unlikely
1099 (but not outside the realm of possibility) that it would be used
1100 for performance reasons but instead for reducing instruction count.
1101 The number of CSR entries therefore has to be considered extremely
1102 carefully.
1103
1104 ## How many CSR entries in M-Mode or S-Mode (for context-switching)?
1105
1106 The minimum required CSR entries would be 1 for each register-bank:
1107 one for integer and one for floating-point. However, as shown
1108 in the "Context Switch Example" section, for optimal efficiency
1109 (minimal instructions in a low-latency situation) the CSRs for
1110 the context-switch should be set up *and left alone*.
1111
1112 This means that it is not really a good idea to touch the CSRs
1113 used for context-switching in the M-Mode (or S-Mode) trap, so
1114 if there is ever demonstrated a need for vectors then there would
1115 need to be *at least* one more free. However just one does not make
1116 much sense (as it one only covers scalar-vector ops) so it is more
1117 likely that at least two extra would be needed.
1118
1119 This *in addition* - in the RV32E case - if an RV32E implementation
1120 happens also to support U/S/M modes. This would be considered quite
1121 rare but not outside of the realm of possibility.
1122
1123 Conclusion: all needs careful analysis and future work.
1124
1125 ## Should use of registers be allowed to "wrap" (x30 x31 x1 x2)?
1126
1127 On balance it's a neat idea however it does seem to be one where the
1128 benefits are not really clear. It would however obviate the need for
1129 an exception to be raised if the VL runs out of registers to put
1130 things in (gets to x31, tries a non-existent x32 and fails), however
1131 the "fly in the ointment" is that x0 is hard-coded to "zero". The
1132 increment therefore would need to be double-stepped to skip over x0.
1133 Some microarchitectures could run into difficulties (SIMD-like ones
1134 in particular) so it needs a lot more thought.
1135
1136 ## Can CLIP be done as a CSR (mode, like elwidth)
1137
1138 RVV appears to be going this way. At the time of writing (12jun2018)
1139 it's noted that in V2.3-Draft V0.4 RVV Chapter, RVV intends to do
1140 clip by way of exactly this method: setting a "clip mode" in a CSR.
1141
1142 No details are given however the most sensible thing to have would be
1143 to extend the 16-bit Register CSR table to 24-bit (or 32-bit) and have
1144 extra bits specifying the type of clipping to be carried out, on
1145 a per-register basis. Other bits may be used for other purposes
1146 (see SIMD saturation below)
1147
1148 ## SIMD saturation (etc.) also set as a mode?
1149
1150 Similar to "CLIP" as an extension to the CSR key-value store, "saturate"
1151 may also need extra details (what the saturation maximum is for example).
1152
1153 ## Include src1/src2 predication on Comparison Ops?
1154
1155 In the C.MV (and other ops - see "C.MV Instruction"), the decision
1156 was taken, unlike in ADD (etc.) which are 3-operand ops, to use
1157 *both* the src *and* dest predication masks to give an extremely
1158 powerful and flexible instruction that covers a huge number of
1159 "traditional" vector opcodes.
1160
1161 The natural question therefore to ask is: where else could this
1162 flexibility be deployed? What about comparison operations?
1163
1164 Unfortunately, C.MV is basically "regs[dest] = regs[src]" whilst
1165 predicated comparison operations are actually a *three* operand
1166 instruction:
1167
1168 regs[pred] |= 1<< (cmp(regs[src1], regs[src2]) ? 1 : 0)
1169
1170 Therefore at first glance it does not make sense to use src1 and src2
1171 predication masks, as it breaks the rule of 3-operand instructions
1172 to use the *destination* predication register.
1173
1174 In this case however, the destination *is* a predication register
1175 as opposed to being a predication mask that is applied *to* the
1176 (vectorised) operation, element-at-a-time on src1 and src2.
1177
1178 Thus the question is directly inter-related to whether the modification
1179 of the predication mask should *itself* be predicated.
1180
1181 It is quite complex, in other words, and needs careful consideration.
1182
1183 ## 8/16-bit ops is it worthwhile adding a "start offset"?
1184
1185 The idea here is to make it possible, particularly in a "Packed SIMD"
1186 case, to be able to avoid doing unaligned Load/Store operations
1187 by specifying that operations, instead of being carried out
1188 element-for-element, are offset by a fixed amount *even* in 8 and 16-bit
1189 element Packed SIMD cases.
1190
1191 For example rather than take 2 32-bit registers divided into 4 8-bit
1192 elements and have them ADDed element-for-element as follows:
1193
1194 r3[0] = add r4[0], r6[0]
1195 r3[1] = add r4[1], r6[1]
1196 r3[2] = add r4[2], r6[2]
1197 r3[3] = add r4[3], r6[3]
1198
1199 an offset of 1 would result in four operations as follows, instead:
1200
1201 r3[0] = add r4[1], r6[0]
1202 r3[1] = add r4[2], r6[1]
1203 r3[2] = add r4[3], r6[2]
1204 r3[3] = add r5[0], r6[3]
1205
1206 In non-packed-SIMD mode there is no benefit at all, as a vector may
1207 be created using a different CSR that has the offset built-in. So this
1208 leaves just the packed-SIMD case to consider.
1209
1210 Two ways in which this could be implemented / emulated (without special
1211 hardware):
1212
1213 * bit-manipulation that shuffles the data along by one byte (or one word)
1214 either prior to or as part of the operation requiring the offset.
1215 * just use an unaligned Load/Store sequence, even if there are performance
1216 penalties for doing so.
1217
1218 The question then is whether the performance hit is worth the extra hardware
1219 involving byte-shuffling/shifting the data by an arbitrary offset. On
1220 balance given that there are two reasonable instruction-based options, the
1221 hardware-offset option should be left out for the initial version of SV,
1222 with the option to consider it in an "advanced" version of the specification.
1223
1224 # Impementing V on top of Simple-V
1225
1226 With Simple-V converting the original RVV draft concept-for-concept
1227 from explicit opcodes to implicit overloading of existing RV Standard
1228 Extensions, certain features were (deliberately) excluded that need
1229 to be added back in for RVV to reach its full potential. This is
1230 made slightly complicated by the fact that RVV itself has two
1231 levels: Base and reserved future functionality.
1232
1233 * Representation Encoding is entirely left out of Simple-V in favour of
1234 implicitly taking the exact (explicit) meaning from RV Standard Extensions.
1235 * VCLIP and VCLIPI do not have corresponding RV Standard Extension
1236 opcodes (and are the only such operations).
1237 * Extended Element bitwidths (1 through to 24576 bits) were left out
1238 of Simple-V as, again, there is no corresponding RV Standard Extension
1239 that covers anything even below 32-bit operands.
1240 * Polymorphism was entirely left out of Simple-V due to the inherent
1241 complexity of automatic type-conversion.
1242 * Vector Register files were specifically left out of Simple-V in favour
1243 of fitting on top of the integer and floating-point files. An
1244 "RVV re-retro-fit" needs to be able to mark (implicitly marked)
1245 registers as being actually in a separate *vector* register file.
1246 * Fortunately in RVV (Draft 0.4, V2.3-Draft), the "base" vector
1247 register file size is 5 bits (32 registers), whilst the "Extended"
1248 variant of RVV specifies 8 bits (256 registers) and has yet to
1249 be published.
1250 * One big difference: Sections 17.12 and 17.17, there are only two possible
1251 predication registers in RVV "Base". Through the "indirect" method,
1252 Simple-V provides a key-value CSR table that allows (arbitrarily)
1253 up to 16 (TBD) of either the floating-point or integer registers to
1254 be marked as "predicated" (key), and if so, which integer register to
1255 use as the predication mask (value).
1256
1257 **TODO**
1258
1259 # Implementing P (renamed to DSP) on top of Simple-V
1260
1261 * Implementors indicate chosen bitwidth support in Vector-bitwidth CSR
1262 (caveat: anything not specified drops through to software-emulation / traps)
1263 * TODO
1264
1265 # Appendix
1266
1267 ## V-Extension to Simple-V Comparative Analysis
1268
1269 This section has been moved to its own page [[v_comparative_analysis]]
1270
1271 ## P-Ext ISA
1272
1273 This section has been moved to its own page [[p_comparative_analysis]]
1274
1275 ## Comparison of "Traditional" SIMD, Alt-RVP, Simple-V and RVV Proposals <a name="parallelism_comparisons"></a>
1276
1277 This section compares the various parallelism proposals as they stand,
1278 including traditional SIMD, in terms of features, ease of implementation,
1279 complexity, flexibility, and die area.
1280
1281 ### [[harmonised_rvv_rvp]]
1282
1283 This is an interesting proposal under development to retro-fit the AndesStar
1284 P-Ext into V-Ext.
1285
1286 ### [[alt_rvp]]
1287
1288 Primary benefit of Alt-RVP is the simplicity with which parallelism
1289 may be introduced (effective multiplication of regfiles and associated ALUs).
1290
1291 * plus: the simplicity of the lanes (combined with the regularity of
1292 allocating identical opcodes multiple independent registers) meaning
1293 that SRAM or 2R1W can be used for entire regfile (potentially).
1294 * minus: a more complex instruction set where the parallelism is much
1295 more explicitly directly specified in the instruction and
1296 * minus: if you *don't* have an explicit instruction (opcode) and you
1297 need one, the only place it can be added is... in the vector unit and
1298 * minus: opcode functions (and associated ALUs) duplicated in Alt-RVP are
1299 not useable or accessible in other Extensions.
1300 * plus-and-minus: Lanes may be utilised for high-speed context-switching
1301 but with the down-side that they're an all-or-nothing part of the Extension.
1302 No Alt-RVP: no fast register-bank switching.
1303 * plus: Lane-switching would mean that complex operations not suited to
1304 parallelisation can be carried out, followed by further parallel Lane-based
1305 work, without moving register contents down to memory (and back)
1306 * minus: Access to registers across multiple lanes is challenging. "Solution"
1307 is to drop data into memory and immediately back in again (like MMX).
1308
1309 ### Simple-V
1310
1311 Primary benefit of Simple-V is the OO abstraction of parallel principles
1312 from actual (internal) parallel hardware. It's an API in effect that's
1313 designed to be slotted in to an existing implementation (just after
1314 instruction decode) with minimum disruption and effort.
1315
1316 * minus: the complexity (if full parallelism is to be exploited)
1317 of having to use register renames, OoO, VLIW, register file cacheing,
1318 all of which has been done before but is a pain
1319 * plus: transparent re-use of existing opcodes as-is just indirectly
1320 saying "this register's now a vector" which
1321 * plus: means that future instructions also get to be inherently
1322 parallelised because there's no "separate vector opcodes"
1323 * plus: Compressed instructions may also be (indirectly) parallelised
1324 * minus: the indirect nature of Simple-V means that setup (setting
1325 a CSR register to indicate vector length, a separate one to indicate
1326 that it is a predicate register and so on) means a little more setup
1327 time than Alt-RVP or RVV's "direct and within the (longer) instruction"
1328 approach.
1329 * plus: shared register file meaning that, like Alt-RVP, complex
1330 operations not suited to parallelisation may be carried out interleaved
1331 between parallelised instructions *without* requiring data to be dropped
1332 down to memory and back (into a separate vectorised register engine).
1333 * plus-and-maybe-minus: re-use of integer and floating-point 32-wide register
1334 files means that huge parallel workloads would use up considerable
1335 chunks of the register file. However in the case of RV64 and 32-bit
1336 operations, that effectively means 64 slots are available for parallel
1337 operations.
1338 * plus: inherent parallelism (actual parallel ALUs) doesn't actually need to
1339 be added, yet the instruction opcodes remain unchanged (and still appear
1340 to be parallel). consistent "API" regardless of actual internal parallelism:
1341 even an in-order single-issue implementation with a single ALU would still
1342 appear to have parallel vectoristion.
1343 * hard-to-judge: if actual inherent underlying ALU parallelism is added it's
1344 hard to say if there would be pluses or minuses (on die area). At worse it
1345 would be "no worse" than existing register renaming, OoO, VLIW and register
1346 file cacheing schemes.
1347
1348 ### RVV (as it stands, Draft 0.4 Section 17, RISC-V ISA V2.3-Draft)
1349
1350 RVV is extremely well-designed and has some amazing features, including
1351 2D reorganisation of memory through LOAD/STORE "strides".
1352
1353 * plus: regular predictable workload means that implementations may
1354 streamline effects on L1/L2 Cache.
1355 * plus: regular and clear parallel workload also means that lanes
1356 (similar to Alt-RVP) may be used as an implementation detail,
1357 using either SRAM or 2R1W registers.
1358 * plus: separate engine with no impact on the rest of an implementation
1359 * minus: separate *complex* engine with no RTL (ALUs, Pipeline stages) reuse
1360 really feasible.
1361 * minus: no ISA abstraction or re-use either: additions to other Extensions
1362 do not gain parallelism, resulting in prolific duplication of functionality
1363 inside RVV *and out*.
1364 * minus: when operations require a different approach (scalar operations
1365 using the standard integer or FP regfile) an entire vector must be
1366 transferred out to memory, into standard regfiles, then back to memory,
1367 then back to the vector unit, this to occur potentially multiple times.
1368 * minus: will never fit into Compressed instruction space (as-is. May
1369 be able to do so if "indirect" features of Simple-V are partially adopted).
1370 * plus-and-slight-minus: extended variants may address up to 256
1371 vectorised registers (requires 48/64-bit opcodes to do it).
1372 * minus-and-partial-plus: separate engine plus complexity increases
1373 implementation time and die area, meaning that adoption is likely only
1374 to be in high-performance specialist supercomputing (where it will
1375 be absolutely superb).
1376
1377 ### Traditional SIMD
1378
1379 The only really good things about SIMD are how easy it is to implement and
1380 get good performance. Unfortunately that makes it quite seductive...
1381
1382 * plus: really straightforward, ALU basically does several packed operations
1383 at once. Parallelism is inherent at the ALU, making the addition of
1384 SIMD-style parallelism an easy decision that has zero significant impact
1385 on the rest of any given architectural design and layout.
1386 * plus (continuation): SIMD in simple in-order single-issue designs can
1387 therefore result in superb throughput, easily achieved even with a very
1388 simple execution model.
1389 * minus: ridiculously complex setup and corner-cases that disproportionately
1390 increase instruction count on what would otherwise be a "simple loop",
1391 should the number of elements in an array not happen to exactly match
1392 the SIMD group width.
1393 * minus: getting data usefully out of registers (if separate regfiles
1394 are used) means outputting to memory and back.
1395 * minus: quite a lot of supplementary instructions for bit-level manipulation
1396 are needed in order to efficiently extract (or prepare) SIMD operands.
1397 * minus: MASSIVE proliferation of ISA both in terms of opcodes in one
1398 dimension and parallelism (width): an at least O(N^2) and quite probably
1399 O(N^3) ISA proliferation that often results in several thousand
1400 separate instructions. all requiring separate and distinct corner-case
1401 algorithms!
1402 * minus: EVEN BIGGER proliferation of SIMD ISA if the functionality of
1403 8, 16, 32 or 64-bit reordering is built-in to the SIMD instruction.
1404 For example: add (high|low) 16-bits of r1 to (low|high) of r2 requires
1405 four separate and distinct instructions: one for (r1:low r2:high),
1406 one for (r1:high r2:low), one for (r1:high r2:high) and one for
1407 (r1:low r2:low) *per function*.
1408 * minus: EVEN BIGGER proliferation of SIMD ISA if there is a mismatch
1409 between operand and result bit-widths. In combination with high/low
1410 proliferation the situation is made even worse.
1411 * minor-saving-grace: some implementations *may* have predication masks
1412 that allow control over individual elements within the SIMD block.
1413
1414 ## Comparison *to* Traditional SIMD: Alt-RVP, Simple-V and RVV Proposals <a name="simd_comparison"></a>
1415
1416 This section compares the various parallelism proposals as they stand,
1417 *against* traditional SIMD as opposed to *alongside* SIMD. In other words,
1418 the question is asked "How can each of the proposals effectively implement
1419 (or replace) SIMD, and how effective would they be"?
1420
1421 ### [[alt_rvp]]
1422
1423 * Alt-RVP would not actually replace SIMD but would augment it: just as with
1424 a SIMD architecture where the ALU becomes responsible for the parallelism,
1425 Alt-RVP ALUs would likewise be so responsible... with *additional*
1426 (lane-based) parallelism on top.
1427 * Thus at least some of the downsides of SIMD ISA O(N^5) proliferation by
1428 at least one dimension are avoided (architectural upgrades introducing
1429 128-bit then 256-bit then 512-bit variants of the exact same 64-bit
1430 SIMD block)
1431 * Thus, unfortunately, Alt-RVP would suffer the same inherent proliferation
1432 of instructions as SIMD, albeit not quite as badly (due to Lanes).
1433 * In the same discussion for Alt-RVP, an additional proposal was made to
1434 be able to subdivide the bits of each register lane (columns) down into
1435 arbitrary bit-lengths (RGB 565 for example).
1436 * A recommendation was given instead to make the subdivisions down to 32-bit,
1437 16-bit or even 8-bit, effectively dividing the registerfile into
1438 Lane0(H), Lane0(L), Lane1(H) ... LaneN(L) or further. If inter-lane
1439 "swapping" instructions were then introduced, some of the disadvantages
1440 of SIMD could be mitigated.
1441
1442 ### RVV
1443
1444 * RVV is designed to replace SIMD with a better paradigm: arbitrary-length
1445 parallelism.
1446 * However whilst SIMD is usually designed for single-issue in-order simple
1447 DSPs with a focus on Multimedia (Audio, Video and Image processing),
1448 RVV's primary focus appears to be on Supercomputing: optimisation of
1449 mathematical operations that fit into the OpenCL space.
1450 * Adding functions (operations) that would normally fit (in parallel)
1451 into a SIMD instruction requires an equivalent to be added to the
1452 RVV Extension, if one does not exist. Given the specialist nature of
1453 some SIMD instructions (8-bit or 16-bit saturated or halving add),
1454 this possibility seems extremely unlikely to occur, even if the
1455 implementation overhead of RVV were acceptable (compared to
1456 normal SIMD/DSP-style single-issue in-order simplicity).
1457
1458 ### Simple-V
1459
1460 * Simple-V borrows hugely from RVV as it is intended to be easy to
1461 topologically transplant every single instruction from RVV (as
1462 designed) into Simple-V equivalents, with *zero loss of functionality
1463 or capability*.
1464 * With the "parallelism" abstracted out, a hypothetical SIMD-less "DSP"
1465 Extension which contained the basic primitives (non-parallelised
1466 8, 16 or 32-bit SIMD operations) inherently *become* parallel,
1467 automatically.
1468 * Additionally, standard operations (ADD, MUL) that would normally have
1469 to have special SIMD-parallel opcodes added need no longer have *any*
1470 of the length-dependent variants (2of 32-bit ADDs in a 64-bit register,
1471 4of 32-bit ADDs in a 128-bit register) because Simple-V takes the
1472 *standard* RV opcodes (present and future) and automatically parallelises
1473 them.
1474 * By inheriting the RVV feature of arbitrary vector-length, then just as
1475 with RVV the corner-cases and ISA proliferation of SIMD is avoided.
1476 * Whilst not entirely finalised, registers are expected to be
1477 capable of being subdivided down to an implementor-chosen bitwidth
1478 in the underlying hardware (r1 becomes r1[31..24] r1[23..16] r1[15..8]
1479 and r1[7..0], or just r1[31..16] r1[15..0]) where implementors can
1480 choose to have separate independent 8-bit ALUs or dual-SIMD 16-bit
1481 ALUs that perform twin 8-bit operations as they see fit, or anything
1482 else including no subdivisions at all.
1483 * Even though implementors have that choice even to have full 64-bit
1484 (with RV64) SIMD, they *must* provide predication that transparently
1485 switches off appropriate units on the last loop, thus neatly fitting
1486 underlying SIMD ALU implementations *into* the arbitrary vector-length
1487 RVV paradigm, keeping the uniform consistent API that is a key strategic
1488 feature of Simple-V.
1489 * With Simple-V fitting into the standard register files, certain classes
1490 of SIMD operations such as High/Low arithmetic (r1[31..16] + r2[15..0])
1491 can be done by applying *Parallelised* Bit-manipulation operations
1492 followed by parallelised *straight* versions of element-to-element
1493 arithmetic operations, even if the bit-manipulation operations require
1494 changing the bitwidth of the "vectors" to do so. Predication can
1495 be utilised to skip high words (or low words) in source or destination.
1496 * In essence, the key downside of SIMD - massive duplication of
1497 identical functions over time as an architecture evolves from 32-bit
1498 wide SIMD all the way up to 512-bit, is avoided with Simple-V, through
1499 vector-style parallelism being dropped on top of 8-bit or 16-bit
1500 operations, all the while keeping a consistent ISA-level "API" irrespective
1501 of implementor design choices (or indeed actual implementations).
1502
1503 ### Example Instruction translation: <a name="example_translation"></a>
1504
1505 Instructions "ADD r2 r4 r4" would result in three instructions being
1506 generated and placed into the FIFO:
1507
1508 * ADD r2 r4 r4
1509 * ADD r2 r5 r5
1510 * ADD r2 r6 r6
1511
1512 ## Example of vector / vector, vector / scalar, scalar / scalar => vector add
1513
1514 register CSRvectorlen[XLEN][4]; # not quite decided yet about this one...
1515 register CSRpredicate[XLEN][4]; # 2^4 is max vector length
1516 register CSRreg_is_vectorised[XLEN]; # just for fun support scalars as well
1517 register x[32][XLEN];
1518
1519 function op_add(rd, rs1, rs2, predr)
1520 {
1521    /* note that this is ADD, not PADD */
1522    int i, id, irs1, irs2;
1523    # checks CSRvectorlen[rd] == CSRvectorlen[rs] etc. ignored
1524    # also destination makes no sense as a scalar but what the hell...
1525    for (i = 0, id=0, irs1=0, irs2=0; i<CSRvectorlen[rd]; i++)
1526       if (CSRpredicate[predr][i]) # i *think* this is right...
1527          x[rd+id] <= x[rs1+irs1] + x[rs2+irs2];
1528       # now increment the idxs
1529       if (CSRreg_is_vectorised[rd]) # bitfield check rd, scalar/vector?
1530          id += 1;
1531       if (CSRreg_is_vectorised[rs1]) # bitfield check rs1, scalar/vector?
1532          irs1 += 1;
1533       if (CSRreg_is_vectorised[rs2]) # bitfield check rs2, scalar/vector?
1534          irs2 += 1;
1535 }
1536
1537 ## Retro-fitting Predication into branch-explicit ISA <a name="predication_retrofit"></a>
1538
1539 One of the goals of this parallelism proposal is to avoid instruction
1540 duplication. However, with the base ISA having been designed explictly
1541 to *avoid* condition-codes entirely, shoe-horning predication into it
1542 bcomes quite challenging.
1543
1544 However what if all branch instructions, if referencing a vectorised
1545 register, were instead given *completely new analogous meanings* that
1546 resulted in a parallel bit-wise predication register being set? This
1547 would have to be done for both C.BEQZ and C.BNEZ, as well as BEQ, BNE,
1548 BLT and BGE.
1549
1550 We might imagine that FEQ, FLT and FLT would also need to be converted,
1551 however these are effectively *already* in the precise form needed and
1552 do not need to be converted *at all*! The difference is that FEQ, FLT
1553 and FLE *specifically* write a 1 to an integer register if the condition
1554 holds, and 0 if not. All that needs to be done here is to say, "if
1555 the integer register is tagged with a bit that says it is a predication
1556 register, the **bit** in the integer register is set based on the
1557 current vector index" instead.
1558
1559 There is, in the standard Conditional Branch instruction, more than
1560 adequate space to interpret it in a similar fashion:
1561
1562 [[!table data="""
1563 31 |30 ..... 25 |24..20|19..15| 14...12| 11.....8 | 7 | 6....0 |
1564 imm[12] | imm[10:5] |rs2 | rs1 | funct3 | imm[4:1] | imm[11] | opcode |
1565 1 | 6 | 5 | 5 | 3 | 4 | 1 | 7 |
1566 offset[12,10:5] || src2 | src1 | BEQ | offset[11,4:1] || BRANCH |
1567 """]]
1568
1569 This would become:
1570
1571 [[!table data="""
1572 31 | 30 .. 25 |24 ... 20 | 19 15 | 14 12 | 11 .. 8 | 7 | 6 ... 0 |
1573 imm[12] | imm[10:5]| rs2 | rs1 | funct3 | imm[4:1] | imm[11] | opcode |
1574 1 | 6 | 5 | 5 | 3 | 4 | 1 | 7 |
1575 reserved || src2 | src1 | BEQ | predicate rs3 || BRANCH |
1576 """]]
1577
1578 Similarly the C.BEQZ and C.BNEZ instruction format may be retro-fitted,
1579 with the interesting side-effect that there is space within what is presently
1580 the "immediate offset" field to reinterpret that to add in not only a bit
1581 field to distinguish between floating-point compare and integer compare,
1582 not only to add in a second source register, but also use some of the bits as
1583 a predication target as well.
1584
1585 [[!table data="""
1586 15..13 | 12 ....... 10 | 9...7 | 6 ......... 2 | 1 .. 0 |
1587 funct3 | imm | rs10 | imm | op |
1588 3 | 3 | 3 | 5 | 2 |
1589 C.BEQZ | offset[8,4:3] | src | offset[7:6,2:1,5] | C1 |
1590 """]]
1591
1592 Now uses the CS format:
1593
1594 [[!table data="""
1595 15..13 | 12 . 10 | 9 .. 7 | 6 .. 5 | 4..2 | 1 .. 0 |
1596 funct3 | imm | rs10 | imm | | op |
1597 3 | 3 | 3 | 2 | 3 | 2 |
1598 C.BEQZ | pred rs3 | src1 | I/F B | src2 | C1 |
1599 """]]
1600
1601 Bit 6 would be decoded as "operation refers to Integer or Float" including
1602 interpreting src1 and src2 accordingly as outlined in Table 12.2 of the
1603 "C" Standard, version 2.0,
1604 whilst Bit 5 would allow the operation to be extended, in combination with
1605 funct3 = 110 or 111: a combination of four distinct (predicated) comparison
1606 operators. In both floating-point and integer cases those could be
1607 EQ/NEQ/LT/LE (with GT and GE being synthesised by inverting src1 and src2).
1608
1609 ## Register reordering <a name="register_reordering"></a>
1610
1611 ### Register File
1612
1613 | Reg Num | Bits |
1614 | ------- | ---- |
1615 | r0 | (32..0) |
1616 | r1 | (32..0) |
1617 | r2 | (32..0) |
1618 | r3 | (32..0) |
1619 | r4 | (32..0) |
1620 | r5 | (32..0) |
1621 | r6 | (32..0) |
1622 | r7 | (32..0) |
1623 | .. | (32..0) |
1624 | r31| (32..0) |
1625
1626 ### Vectorised CSR
1627
1628 May not be an actual CSR: may be generated from Vector Length CSR:
1629 single-bit is less burdensome on instruction decode phase.
1630
1631 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
1632 | - | - | - | - | - | - | - | - |
1633 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 |
1634
1635 ### Vector Length CSR
1636
1637 | Reg Num | (3..0) |
1638 | ------- | ---- |
1639 | r0 | 2 |
1640 | r1 | 0 |
1641 | r2 | 1 |
1642 | r3 | 1 |
1643 | r4 | 3 |
1644 | r5 | 0 |
1645 | r6 | 0 |
1646 | r7 | 1 |
1647
1648 ### Virtual Register Reordering
1649
1650 This example assumes the above Vector Length CSR table
1651
1652 | Reg Num | Bits (0) | Bits (1) | Bits (2) |
1653 | ------- | -------- | -------- | -------- |
1654 | r0 | (32..0) | (32..0) |
1655 | r2 | (32..0) |
1656 | r3 | (32..0) |
1657 | r4 | (32..0) | (32..0) | (32..0) |
1658 | r7 | (32..0) |
1659
1660 ### Bitwidth Virtual Register Reordering
1661
1662 This example goes a little further and illustrates the effect that a
1663 bitwidth CSR has been set on a register. Preconditions:
1664
1665 * RV32 assumed
1666 * CSRintbitwidth[2] = 010 # integer r2 is 16-bit
1667 * CSRintvlength[2] = 3 # integer r2 is a vector of length 3
1668 * vsetl rs1, 5 # set the vector length to 5
1669
1670 This is interpreted as follows:
1671
1672 * Given that the context is RV32, ELEN=32.
1673 * With ELEN=32 and bitwidth=16, the number of SIMD elements is 2
1674 * Therefore the actual vector length is up to *six* elements
1675 * However vsetl sets a length 5 therefore the last "element" is skipped
1676
1677 So when using an operation that uses r2 as a source (or destination)
1678 the operation is carried out as follows:
1679
1680 * 16-bit operation on r2(15..0) - vector element index 0
1681 * 16-bit operation on r2(31..16) - vector element index 1
1682 * 16-bit operation on r3(15..0) - vector element index 2
1683 * 16-bit operation on r3(31..16) - vector element index 3
1684 * 16-bit operation on r4(15..0) - vector element index 4
1685 * 16-bit operation on r4(31..16) **NOT** carried out due to length being 5
1686
1687 Predication has been left out of the above example for simplicity, however
1688 predication is ANDed with the latter stages (vsetl not equal to maximum
1689 capacity).
1690
1691 Note also that it is entirely an implementor's choice as to whether to have
1692 actual separate ALUs down to the minimum bitwidth, or whether to have something
1693 more akin to traditional SIMD (at any level of subdivision: 8-bit SIMD
1694 operations carried out 32-bits at a time is perfectly acceptable, as is
1695 8-bit SIMD operations carried out 16-bits at a time requiring two ALUs).
1696 Regardless of the internal parallelism choice, *predication must
1697 still be respected*, making Simple-V in effect the "consistent public API".
1698
1699 vew may be one of the following (giving a table "bytestable", used below):
1700
1701 | vew | bitwidth | bytestable |
1702 | --- | -------- | ---------- |
1703 | 000 | default | XLEN/8 |
1704 | 001 | 8 | 1 |
1705 | 010 | 16 | 2 |
1706 | 011 | 32 | 4 |
1707 | 100 | 64 | 8 |
1708 | 101 | 128 | 16 |
1709 | 110 | rsvd | rsvd |
1710 | 111 | rsvd | rsvd |
1711
1712 Pseudocode for vector length taking CSR SIMD-bitwidth into account:
1713
1714 vew = CSRbitwidth[rs1]
1715 if (vew == 0)
1716 bytesperreg = (XLEN/8) # or FLEN as appropriate
1717 else:
1718 bytesperreg = bytestable[vew] # 1 2 4 8 16
1719 simdmult = (XLEN/8) / bytesperreg # or FLEN as appropriate
1720 vlen = CSRvectorlen[rs1] * simdmult
1721
1722 To index an element in a register rnum where the vector element index is i:
1723
1724 function regoffs(rnum, i):
1725 regidx = floor(i / simdmult) # integer-div rounded down
1726 byteidx = i % simdmult # integer-remainder
1727 return rnum + regidx, # actual real register
1728 byteidx * 8, # low
1729 byteidx * 8 + (vew-1), # high
1730
1731 ### Insights
1732
1733 SIMD register file splitting still to consider. For RV64, benefits of doubling
1734 (quadrupling in the case of Half-Precision IEEE754 FP) the apparent
1735 size of the floating point register file to 64 (128 in the case of HP)
1736 seem pretty clear and worth the complexity.
1737
1738 64 virtual 32-bit F.P. registers and given that 32-bit FP operations are
1739 done on 64-bit registers it's not so conceptually difficult.  May even
1740 be achieved by *actually* splitting the regfile into 64 virtual 32-bit
1741 registers such that a 64-bit FP scalar operation is dropped into (r0.H
1742 r0.L) tuples.  Implementation therefore hidden through register renaming.
1743
1744 Implementations intending to introduce VLIW, OoO and parallelism
1745 (even without Simple-V) would then find that the instructions are
1746 generated quicker (or in a more compact fashion that is less heavy
1747 on caches). Interestingly we observe then that Simple-V is about
1748 "consolidation of instruction generation", where actual parallelism
1749 of underlying hardware is an implementor-choice that could just as
1750 equally be applied *without* Simple-V even being implemented.
1751
1752 ## Analysis of CSR decoding on latency <a name="csr_decoding_analysis"></a>
1753
1754 It could indeed have been logically deduced (or expected), that there
1755 would be additional decode latency in this proposal, because if
1756 overloading the opcodes to have different meanings, there is guaranteed
1757 to be some state, some-where, directly related to registers.
1758
1759 There are several cases:
1760
1761 * All operands vector-length=1 (scalars), all operands
1762 packed-bitwidth="default": instructions are passed through direct as if
1763 Simple-V did not exist.  Simple-V is, in effect, completely disabled.
1764 * At least one operand vector-length > 1, all operands
1765 packed-bitwidth="default": any parallel vector ALUs placed on "alert",
1766 virtual parallelism looping may be activated.
1767 * All operands vector-length=1 (scalars), at least one
1768 operand packed-bitwidth != default: degenerate case of SIMD,
1769 implementation-specific complexity here (packed decode before ALUs or
1770 *IN* ALUs)
1771 * At least one operand vector-length > 1, at least one operand
1772 packed-bitwidth != default: parallel vector ALUs (if any)
1773 placed on "alert", virtual parallelsim looping may be activated,
1774 implementation-specific SIMD complexity kicks in (packed decode before
1775 ALUs or *IN* ALUs).
1776
1777 Bear in mind that the proposal includes that the decision whether
1778 to parallelise in hardware or whether to virtual-parallelise (to
1779 dramatically simplify compilers and also not to run into the SIMD
1780 instruction proliferation nightmare) *or* a transprent combination
1781 of both, be done on a *per-operand basis*, so that implementors can
1782 specifically choose to create an application-optimised implementation
1783 that they believe (or know) will sell extremely well, without having
1784 "Extra Standards-Mandated Baggage" that would otherwise blow their area
1785 or power budget completely out the window.
1786
1787 Additionally, two possible CSR schemes have been proposed, in order to
1788 greatly reduce CSR space:
1789
1790 * per-register CSRs (vector-length and packed-bitwidth)
1791 * a smaller number of CSRs with the same information but with an *INDEX*
1792 specifying WHICH register in one of three regfiles (vector, fp, int)
1793 the length and bitwidth applies to.
1794
1795 (See "CSR vector-length and CSR SIMD packed-bitwidth" section for details)
1796
1797 In addition, LOAD/STORE has its own associated proposed CSRs that
1798 mirror the STRIDE (but not yet STRIDE-SEGMENT?) functionality of
1799 V (and Hwacha).
1800
1801 Also bear in mind that, for reasons of simplicity for implementors,
1802 I was coming round to the idea of permitting implementors to choose
1803 exactly which bitwidths they would like to support in hardware and which
1804 to allow to fall through to software-trap emulation.
1805
1806 So the question boils down to:
1807
1808 * whether either (or both) of those two CSR schemes have significant
1809 latency that could even potentially require an extra pipeline decode stage
1810 * whether there are implementations that can be thought of which do *not*
1811 introduce significant latency
1812 * whether it is possible to explicitly (through quite simply
1813 disabling Simple-V-Ext) or implicitly (detect the case all-vlens=1,
1814 all-simd-bitwidths=default) switch OFF any decoding, perhaps even to
1815 the extreme of skipping an entire pipeline stage (if one is needed)
1816 * whether packed bitwidth and associated regfile splitting is so complex
1817 that it should definitely, definitely be made mandatory that implementors
1818 move regfile splitting into the ALU, and what are the implications of that
1819 * whether even if that *is* made mandatory, is software-trapped
1820 "unsupported bitwidths" still desirable, on the basis that SIMD is such
1821 a complete nightmare that *even* having a software implementation is
1822 better, making Simple-V have more in common with a software API than
1823 anything else.
1824
1825 Whilst the above may seem to be severe minuses, there are some strong
1826 pluses:
1827
1828 * Significant reduction of V's opcode space: over 95%.
1829 * Smaller reduction of P's opcode space: around 10%.
1830 * The potential to use Compressed instructions in both Vector and SIMD
1831 due to the overloading of register meaning (implicit vectorisation,
1832 implicit packing)
1833 * Not only present but also future extensions automatically gain parallelism.
1834 * Already mentioned but worth emphasising: the simplification to compiler
1835 writers and assembly-level writers of having the same consistent ISA
1836 regardless of whether the internal level of parallelism (number of
1837 parallel ALUs) is only equal to one ("virtual" parallelism), or is
1838 greater than one, should not be underestimated.
1839
1840 ## Reducing Register Bank porting
1841
1842 This looks quite reasonable.
1843 <https://www.princeton.edu/~rblee/ELE572Papers/MultiBankRegFile_ISCA2000.pdf>
1844
1845 The main details are outlined on page 4.  They propose a 2-level register
1846 cache hierarchy, note that registers are typically only read once, that
1847 you never write back from upper to lower cache level but always go in a
1848 cycle lower -> upper -> ALU -> lower, and at the top of page 5 propose
1849 a scheme where you look ahead by only 2 instructions to determine which
1850 registers to bring into the cache.
1851
1852 The nice thing about a vector architecture is that you *know* that
1853 *even more* registers are going to be pulled in: Hwacha uses this fact
1854 to optimise L1/L2 cache-line usage (avoid thrashing), strangely enough
1855 by *introducing* deliberate latency into the execution phase.
1856
1857 ## Overflow registers in combination with predication
1858
1859 **TODO**: propose overflow registers be actually one of the integer regs
1860 (flowing to multiple regs).
1861
1862 **TODO**: propose "mask" (predication) registers likewise. combination with
1863 standard RV instructions and overflow registers extremely powerful, see
1864 Aspex ASP.
1865
1866 When integer overflow is stored in an easily-accessible bit (or another
1867 register), parallelisation turns this into a group of bits which can
1868 potentially be interacted with in predication, in interesting and powerful
1869 ways. For example, by taking the integer-overflow result as a predication
1870 field and shifting it by one, a predicated vectorised "add one" can emulate
1871 "carry" on arbitrary (unlimited) length addition.
1872
1873 However despite RVV having made room for floating-point exceptions, neither
1874 RVV nor base RV have taken integer-overflow (carry) into account, which
1875 makes proposing it quite challenging given that the relevant (Base) RV
1876 sections are frozen. Consequently it makes sense to forgo this feature.
1877
1878 ## Context Switch Example <a name="context_switch"></a>
1879
1880 An unusual side-effect of Simple-V mapping onto the standard register files
1881 is that LOAD-multiple and STORE-multiple are accidentally available, as long
1882 as it is acceptable that the register(s) to be loaded/stored are contiguous
1883 (per instruction). An additional accidental benefit is that Compressed LD/ST
1884 may also be used.
1885
1886 To illustrate how this works, here is some example code from FreeRTOS
1887 (GPLv2 licensed, portasm.S):
1888
1889 /* Macro for saving task context */
1890 .macro portSAVE_CONTEXT
1891 .global pxCurrentTCB
1892 /* make room in stack */
1893 addi sp, sp, -REGBYTES * 32
1894
1895 /* Save Context */
1896 STORE x1, 0x0(sp)
1897 STORE x2, 1 * REGBYTES(sp)
1898 STORE x3, 2 * REGBYTES(sp)
1899 ...
1900 ...
1901 STORE x30, 29 * REGBYTES(sp)
1902 STORE x31, 30 * REGBYTES(sp)
1903
1904 /* Store current stackpointer in task control block (TCB) */
1905 LOAD t0, pxCurrentTCB //pointer
1906 STORE sp, 0x0(t0)
1907 .endm
1908
1909 /* Saves current error program counter (EPC) as task program counter */
1910 .macro portSAVE_EPC
1911 csrr t0, mepc
1912 STORE t0, 31 * REGBYTES(sp)
1913 .endm
1914
1915 /* Saves current return adress (RA) as task program counter */
1916 .macro portSAVE_RA
1917 STORE ra, 31 * REGBYTES(sp)
1918 .endm
1919
1920 /* Macro for restoring task context */
1921 .macro portRESTORE_CONTEXT
1922
1923 .global pxCurrentTCB
1924 /* Load stack pointer from the current TCB */
1925 LOAD sp, pxCurrentTCB
1926 LOAD sp, 0x0(sp)
1927
1928 /* Load task program counter */
1929 LOAD t0, 31 * REGBYTES(sp)
1930 csrw mepc, t0
1931
1932 /* Run in machine mode */
1933 li t0, MSTATUS_PRV1
1934 csrs mstatus, t0
1935
1936 /* Restore registers,
1937 Skip global pointer because that does not change */
1938 LOAD x1, 0x0(sp)
1939 LOAD x4, 3 * REGBYTES(sp)
1940 LOAD x5, 4 * REGBYTES(sp)
1941 ...
1942 ...
1943 LOAD x30, 29 * REGBYTES(sp)
1944 LOAD x31, 30 * REGBYTES(sp)
1945
1946 addi sp, sp, REGBYTES * 32
1947 mret
1948 .endm
1949
1950 The important bits are the Load / Save context, which may be replaced
1951 with firstly setting up the Vectors and secondly using a *single* STORE
1952 (or LOAD) including using C.ST or C.LD, to indicate that the entire
1953 bank of registers is to be loaded/saved:
1954
1955 /* a few things are assumed here: (a) that when switching to
1956 M-Mode an entirely different set of CSRs is used from that
1957 which is used in U-Mode and (b) that the M-Mode x1 and x4
1958 vectors are also not used anywhere else in M-Mode, consequently
1959 only need to be set up just the once.
1960 */
1961 .macroVectorSetup
1962 MVECTORCSRx1 = 31, defaultlen
1963 MVECTORCSRx4 = 28, defaultlen
1964
1965 /* Save Context */
1966 SETVL x0, x0, 31 /* x0 ignored silently */
1967 STORE x1, 0x0(sp) // x1 marked as 31-long vector of default bitwidth
1968
1969 /* Restore registers,
1970 Skip global pointer because that does not change */
1971 LOAD x1, 0x0(sp)
1972 SETVL x0, x0, 28 /* x0 ignored silently */
1973 LOAD x4, 3 * REGBYTES(sp) // x4 marked as 28-long default bitwidth
1974
1975 Note that although it may just be a bug in portasm.S, x2 and x3 appear not
1976 to be being restored. If however this is a bug and they *do* need to be
1977 restored, then the SETVL call may be moved to *outside* the Save / Restore
1978 Context assembly code, into the macroVectorSetup, as long as vectors are
1979 never used anywhere else (i.e. VL is never altered by M-Mode).
1980
1981 In effect the entire bank of repeated LOAD / STORE instructions is replaced
1982 by one single (compressed if it is available) instruction.
1983
1984 ## Virtual Memory page-faults on LOAD/STORE
1985
1986
1987 ### Notes from conversations
1988
1989 > I was going through the C.LOAD / C.STORE section 12.3 of V2.3-Draft
1990 > riscv-isa-manual in order to work out how to re-map RVV onto the standard
1991 > ISA, and came across an interesting comments at the bottom of pages 75
1992 > and 76:
1993
1994 > " A common mechanism used in other ISAs to further reduce save/restore
1995 > code size is load- multiple and store-multiple instructions. "
1996
1997 > Fascinatingly, due to Simple-V proposing to use the *standard* register
1998 > file, both C.LOAD / C.STORE *and* LOAD / STORE would in effect be exactly
1999 > that: load-multiple and store-multiple instructions. Which brings us
2000 > on to this comment:
2001
2002 > "For virtual memory systems, some data accesses could be resident in
2003 > physical memory and
2004 > some could not, which requires a new restart mechanism for partially
2005 > executed instructions."
2006
2007 > Which then of course brings us to the interesting question: how does RVV
2008 > cope with the scenario when, particularly with LD.X (Indexed / indirect
2009 > loads), part-way through the loading a page fault occurs?
2010
2011 > Has this been noted or discussed before?
2012
2013 For applications-class platforms, the RVV exception model is
2014 element-precise (that is, if an exception occurs on element j of a
2015 vector instruction, elements 0..j-1 have completed execution and elements
2016 j+1..vl-1 have not executed).
2017
2018 Certain classes of embedded platforms where exceptions are always fatal
2019 might choose to offer resumable/swappable interrupts but not precise
2020 exceptions.
2021
2022
2023 > Is RVV designed in any way to be re-entrant?
2024
2025 Yes.
2026
2027
2028 > What would the implications be for instructions that were in a FIFO at
2029 > the time, in out-of-order and VLIW implementations, where partial decode
2030 > had taken place?
2031
2032 The usual bag of tricks for maintaining precise exceptions applies to
2033 vector machines as well. Register renaming makes the job easier, and
2034 it's relatively cheaper for vectors, since the control cost is amortized
2035 over longer registers.
2036
2037
2038 > Would it be reasonable at least to say *bypass* (and freeze) the
2039 > instruction FIFO (drop down to a single-issue execution model temporarily)
2040 > for the purposes of executing the instructions in the interrupt (whilst
2041 > setting up the VM page), then re-continue the instruction with all
2042 > state intact?
2043
2044 This approach has been done successfully, but it's desirable to be
2045 able to swap out the vector unit state to support context switches on
2046 exceptions that result in long-latency I/O.
2047
2048
2049 > Or would it be better to switch to an entirely separate secondary
2050 > hyperthread context?
2051
2052 > Does anyone have any ideas or know if there is any academic literature
2053 > on solutions to this problem?
2054
2055 The Vector VAX offered imprecise but restartable and swappable exceptions:
2056 http://mprc.pku.edu.cn/~liuxianhua/chn/corpus/Notes/articles/isca/1990/VAX%20vector%20architecture.pdf
2057
2058 Sec. 4.6 of Krste's dissertation assesses some of
2059 the tradeoffs and references a bunch of related work:
2060 http://people.eecs.berkeley.edu/~krste/thesis.pdf
2061
2062
2063 ----
2064
2065 Started reading section 4.6 of Krste's thesis, noted the "IEE85 F.P
2066 exceptions" and thought, "hmmm that could go into a CSR, must re-read
2067 the section on FP state CSRs in RVV 0.4-Draft again" then i suddenly
2068 thought, "ah ha! what if the memory exceptions were, instead of having
2069 an immediate exception thrown, were simply stored in a type of predication
2070 bit-field with a flag "error this element failed"?
2071
2072 Then, *after* the vector load (or store, or even operation) was
2073 performed, you could *then* raise an exception, at which point it
2074 would be possible (yes in software... I know....) to go "hmmm, these
2075 indexed operations didn't work, let's get them into memory by triggering
2076 page-loads", then *re-run the entire instruction* but this time with a
2077 "memory-predication CSR" that stops the already-performed operations
2078 (whether they be loads, stores or an arithmetic / FP operation) from
2079 being carried out a second time.
2080
2081 This theoretically could end up being done multiple times in an SMP
2082 environment, and also for LD.X there would be the remote outside annoying
2083 possibility that the indexed memory address could end up being modified.
2084
2085 The advantage would be that the order of execution need not be
2086 sequential, which potentially could have some big advantages.
2087 Am still thinking through the implications as any dependent operations
2088 (particularly ones already decoded and moved into the execution FIFO)
2089 would still be there (and stalled). hmmm.
2090
2091 ----
2092
2093 > > # assume internal parallelism of 8 and MAXVECTORLEN of 8
2094 > > VSETL r0, 8
2095 > > FADD x1, x2, x3
2096 >
2097 > > x3[0]: ok
2098 > > x3[1]: exception
2099 > > x3[2]: ok
2100 > > ...
2101 > > ...
2102 > > x3[7]: ok
2103 >
2104 > > what happens to result elements 2-7?  those may be *big* results
2105 > > (RV128)
2106 > > or in the RVV-Extended may be arbitrary bit-widths far greater.
2107 >
2108 >  (you replied:)
2109 >
2110 > Thrown away.
2111
2112 discussion then led to the question of OoO architectures
2113
2114 > The costs of the imprecise-exception model are greater than the benefit.
2115 > Software doesn't want to cope with it.  It's hard to debug.  You can't
2116 > migrate state between different microarchitectures--unless you force all
2117 > implementations to support the same imprecise-exception model, which would
2118 > greatly limit implementation flexibility.  (Less important, but still
2119 > relevant, is that the imprecise model increases the size of the context
2120 > structure, as the microarchitectural guts have to be spilled to memory.)
2121
2122 ## Zero/Non-zero Predication
2123
2124 >> >  it just occurred to me that there's another reason why the data
2125 >> > should be left instead of zeroed.  if the standard register file is
2126 >> > used, such that vectorised operations are translated to mean "please
2127 >> > insert multiple register-contiguous operations into the instruction
2128 >> > FIFO" and predication is used to *skip* some of those, then if the
2129 >> > next "vector" operation uses the (standard) registers that were masked
2130 >> > *out* of the previous operation it may proceed without blocking.
2131 >> >
2132 >> >  if however zeroing is made mandatory then that optimisation becomes
2133 >> > flat-out impossible to deploy.
2134 >> >
2135 >> >  whilst i haven't fully thought through the full implications, i
2136 >> > suspect RVV might also be able to benefit by being able to fit more
2137 >> > overlapping operations into the available SRAM by doing something
2138 >> > similar.
2139 >
2140 >
2141 > Luke, this is called density time masking. It doesn’t apply to only your
2142 > model with the “standard register file” is used. it applies to any
2143 > architecture that attempts to speed up by skipping computation and writeback
2144 > of masked elements.
2145 >
2146 > That said, the writing of zeros need not be explicit. It is possible to add
2147 > a “zero bit” per element that, when set, forces a zero to be read from the
2148 > vector (although the underlying storage may have old data). In this case,
2149 > there may be a way to implement DTM as well.
2150
2151
2152 ## Implementation detail for scalar-only op detection <a name="scalar_detection"></a>
2153
2154 Note 1: this idea is a pipeline-bypass concept, which may *or may not* be
2155 worthwhile.
2156
2157 Note 2: this is just one possible implementation. Another implementation
2158 may choose to treat *all* operations as vectorised (including treating
2159 scalars as vectors of length 1), choosing to add an extra pipeline stage
2160 dedicated to *all* instructions.
2161
2162 This section *specifically* covers the implementor's freedom to choose
2163 that they wish to minimise disruption to an existing design by detecting
2164 "scalar-only operations", bypassing the vectorisation phase (which may
2165 or may not require an additional pipeline stage)
2166
2167 [[scalardetect.png]]
2168
2169 >> For scalar ops an implementation may choose to compare 2-3 bits through an
2170 >> AND gate: are src & dest scalar? Yep, ok send straight to ALU  (or instr
2171 >> FIFO).
2172
2173 > Those bits cannot be known until after the registers are decoded from the
2174 > instruction and a lookup in the "vector length table" has completed.
2175 > Considering that one of the reasons RISC-V keeps registers in invariant
2176 > positions across all instructions is to simplify register decoding, I expect
2177 > that inserting an SRAM read would lengthen the critical path in most
2178 > implementations.
2179
2180 reply:
2181
2182 > briefly: the trick i mentioned about ANDing bits together to check if
2183 > an op was fully-scalar or not was to be read out of a single 32-bit
2184 > 3R1W SRAM (64-bit if FPU exists). the 32/64-bit SRAM contains 1 bit per
2185 > register indicating "is register vectorised yes no". 3R because you need
2186 > to check src1, src2 and dest simultaneously. the entries are *generated*
2187 > from the CSRs and are an optimisation that on slower embedded systems
2188 > would likely not be needed.
2189
2190 > is there anything unreasonable that anyone can foresee about that?
2191 > what are the down-sides?
2192
2193 ## C.MV predicated src, predicated dest
2194
2195 > Can this be usefully defined in such a way that it is
2196 > equivalent to vector gather-scatter on each source, followed by a
2197 > non-predicated vector-compare, followed by vector gather-scatter on the
2198 > result?
2199
2200 ## element width conversion: restrict or remove?
2201
2202 summary: don't restrict / remove. it's fine.
2203
2204 > > it has virtually no cost/overhead as long as you specify
2205 > > that inputs can only upconvert, and operations are always done at the
2206 > > largest size, and downconversion only happens at the output.
2207 >
2208 > okaaay.  so that's a really good piece of implementation advice.
2209 > algorithms do require data size conversion, so at some point you need to
2210 > introduce the feature of upconverting and downconverting.
2211 >
2212 > > for int and uint, this is dead simple and fits well within the RVV pipeline
2213 > > without any critical path, pipeline depth, or area implications.
2214
2215 <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/g3feFnAoKIM>
2216
2217 ## Under review / discussion: remove CSR vector length, use VSETVL <a name="vsetvl"></a>
2218
2219 **DECISION: 11jun2018 - CSR vector length removed, VSETVL determines
2220 length on all regs**. This section kept for historical reasons.
2221
2222 So the issue is as follows:
2223
2224 * CSRs are used to set the "span" of a vector (how many of the standard
2225 register file to contiguously use)
2226 * VSETVL in RVV works as follows: it sets the vector length (copy of which
2227 is placed in a dest register), and if the "required" length is longer
2228 than the *available* length, the dest reg is set to the MIN of those
2229 two.
2230 * **HOWEVER**... in SV, *EVERY* vector register has its own separate
2231 length and thus there is no way (at the time that VSETVL is called) to
2232 know what to set the vector length *to*.
2233 * At first glance it seems that it would be perfectly fine to just limit
2234 the vector operation to the length specified in the destination
2235 register's CSR, at the time that each instruction is issued...
2236 except that that cannot possibly be guaranteed to match
2237 with the value *already loaded into the target register from VSETVL*.
2238
2239 Therefore a different approach is needed.
2240
2241 Possible options include:
2242
2243 * Removing the CSR "Vector Length" and always using the value from
2244 VSETVL. "VSETVL destreg, counterreg, #lenimmed" will set VL *and*
2245 destreg equal to MIN(counterreg, lenimmed), with register-based
2246 variant "VSETVL destreg, counterreg, lenreg" doing the same.
2247 * Keeping the CSR "Vector Length" and having the lenreg version have
2248 a "twist": "if lengreg is vectorised, read the length from the CSR"
2249 * Other (TBD)
2250
2251 The first option (of the ones brainstormed so far) is a lot simpler.
2252 It does however mean that the length set in VSETVL will apply across-the-board
2253 to all src1, src2 and dest vectorised registers until it is otherwise changed
2254 (by another VSETVL call). This is probably desirable behaviour.
2255
2256 ## Implementation Paradigms <a name="implementation_paradigms"></a>
2257
2258 TODO: assess various implementation paradigms. These are listed roughly
2259 in order of simplicity (minimum compliance, for ultra-light-weight
2260 embedded systems or to reduce design complexity and the burden of
2261 design implementation and compliance, in non-critical areas), right the
2262 way to high-performance systems.
2263
2264 * Full (or partial) software-emulated (via traps): full support for CSRs
2265 required, however when a register is used that is detected (in hardware)
2266 to be vectorised, an exception is thrown.
2267 * Single-issue In-order, reduced pipeline depth (traditional SIMD / DSP)
2268 * In-order 5+ stage pipelines with instruction FIFOs and mild register-renaming
2269 * Out-of-order with instruction FIFOs and aggressive register-renaming
2270 * VLIW
2271
2272 Also to be taken into consideration:
2273
2274 * "Virtual" vectorisation: single-issue loop, no internal ALU parallelism
2275 * Comphrensive vectorisation: FIFOs and internal parallelism
2276 * Hybrid Parallelism
2277
2278 ### Full or partial software-emulation
2279
2280 The absolute, absolute minimal implementation is to provide the full
2281 set of CSRs and detection logic for when any of the source or destination
2282 registers are vectorised. On detection, a trap is thrown, whether it's
2283 a branch, LOAD, STORE, or an arithmetic operation.
2284
2285 Implementors are entirely free to choose whether to allow absolutely every
2286 single operation to be software-emulated, or whether to provide some emulation
2287 and some hardware support. In particular, for an RV32E implementation
2288 where fast context-switching is a requirement (see "Context Switch Example"),
2289 it makes no sense to allow Vectorised-LOAD/STORE to be implemented as an
2290 exception, as every context-switch will result in double-traps.
2291
2292 # TODO Research
2293
2294 > For great floating point DSPs check TI’s C3x, C4X, and C6xx DSPs
2295
2296 Idea: basic simple butterfly swap on a few element indices, primarily targetted
2297 at SIMD / DSP. High-byte low-byte swapping, high-word low-word swapping,
2298 perhaps allow reindexing of permutations up to 4 elements? 8? Reason:
2299 such operations are less costly than a full indexed-shuffle, which requires
2300 a separate instruction cycle.
2301
2302 Predication "all zeros" needs to be "leave alone". Detection of
2303 ADD r1, rs1, rs0 cases result in nop on predication index 0, whereas
2304 ADD r0, rs1, rs2 is actually a desirable copy from r2 into r0.
2305 Destruction of destination indices requires a copy of the entire vector
2306 in advance to avoid.
2307
2308 TBD: floating-point compare and other exception handling
2309
2310 # References
2311
2312 * SIMD considered harmful <https://www.sigarch.org/simd-instructions-considered-harmful/>
2313 * Link to first proposal <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/GuukrSjgBH8>
2314 * Recommendation by Jacob Bachmeyer to make zero-overhead loop an
2315 "implicit program-counter" <https://groups.google.com/a/groups.riscv.org/d/msg/isa-dev/vYVi95gF2Mo/SHz6a4_lAgAJ>
2316 * Re-continuing P-Extension proposal <https://groups.google.com/a/groups.riscv.org/forum/#!msg/isa-dev/IkLkQn3HvXQ/SEMyC9IlAgAJ>
2317 * First Draft P-SIMD (DSP) proposal <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/vYVi95gF2Mo>
2318 * B-Extension discussion <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/zi_7B15kj6s>
2319 * Broadcom VideoCore-IV <https://docs.broadcom.com/docs/12358545>
2320 Figure 2 P17 and Section 3 on P16.
2321 * Hwacha <https://www2.eecs.berkeley.edu/Pubs/TechRpts/2015/EECS-2015-262.html>
2322 * Hwacha <https://www2.eecs.berkeley.edu/Pubs/TechRpts/2015/EECS-2015-263.html>
2323 * Vector Workshop <http://riscv.org/wp-content/uploads/2015/06/riscv-vector-workshop-june2015.pdf>
2324 * Predication <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/XoP4BfYSLXA>
2325 * Branch Divergence <https://jbush001.github.io/2014/12/07/branch-divergence-in-parallel-kernels.html>
2326 * Life of Triangles (3D) <https://jbush001.github.io/2016/02/27/life-of-triangle.html>
2327 * Videocore-IV <https://github.com/hermanhermitage/videocoreiv/wiki/VideoCore-IV-3d-Graphics-Pipeline>
2328 * Discussion proposing CSRs that change ISA definition
2329 <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/InzQ1wr_3Ak>
2330 * Zero-overhead loops <https://pdfs.semanticscholar.org/dbaa/66985cc730d4b44d79f519e96ec9c43ab5b7.pdf>
2331 * Multi-ported VLIW Register File Implementation <https://ce-publications.et.tudelft.nl/publications/1517_multiple_contexts_in_a_multiported_vliw_register_file_impl.pdf>
2332 * Fast context save/restore proposal <https://groups.google.com/a/groups.riscv.org/d/msgid/isa-dev/57F823FA.6030701%40gmail.com>
2333 * Register File Bank Cacheing <https://www.princeton.edu/~rblee/ELE572Papers/MultiBankRegFile_ISCA2000.pdf>
2334 * Expired Patent on Vector Virtual Memory solutions
2335 <https://patentimages.storage.googleapis.com/fc/f6/e2/2cbee92fcd8743/US5895501.pdf>
2336 * Discussion on RVV "re-entrant" capabilities allowing operations to be
2337 restarted if an exception occurs (VM page-table miss)
2338 <https://groups.google.com/a/groups.riscv.org/d/msg/isa-dev/IuNFitTw9fM/CCKBUlzsAAAJ>
2339 * Dot Product Vector <https://people.eecs.berkeley.edu/~biancolin/papers/arith17.pdf>
2340 * RVV slides 2017 <https://content.riscv.org/wp-content/uploads/2017/12/Wed-1330-RISCVRogerEspasaVEXT-v4.pdf>
2341 * Wavefront skipping using BRAMS <http://www.ece.ubc.ca/~lemieux/publications/severance-fpga2015.pdf>
2342 * Streaming Pipelines <http://www.ece.ubc.ca/~lemieux/publications/severance-fpga2014.pdf>
2343 * Barcelona SIMD Presentation <https://content.riscv.org/wp-content/uploads/2018/05/09.05.2018-9.15-9.30am-RISCV201805-Andes-proposed-P-extension.pdf>
2344 * <http://www.ece.ubc.ca/~lemieux/publications/severance-fpga2015.pdf>
2345 * Full Description (last page) of RVV instructions
2346 <https://inst.eecs.berkeley.edu/~cs152/sp18/handouts/lab4-1.0.pdf>