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