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