add jacob explanation
[libreriscv.git] / simple_v_extension.mdwn
1 # Variable-width Variable-packed SIMD / Simple-V / Parallelism Extension Proposal
2
3 This proposal exists so as to be able to satisfy several disparate
4 requirements: power-conscious, area-conscious, and performance-conscious
5 designs all pull an ISA and its implementation in different conflicting
6 directions, as do the specific intended uses for any given implementation.
7
8 Additionally, the existing P (SIMD) proposal and the V (Vector) proposals,
9 whilst each extremely powerful in their own right and clearly desirable,
10 are also:
11
12 * Clearly independent in their origins (Cray and AndeStar v3 respectively)
13 so need work to adapt to the RISC-V ethos and paradigm
14 * Are sufficiently large so as to make adoption (and exploration for
15 analysis and review purposes) prohibitively expensive
16 * Both contain partial duplication of pre-existing RISC-V instructions
17 (an undesirable characteristic)
18 * Both have independent and disparate methods for introducing parallelism
19 at the instruction level.
20 * Both require that their respective parallelism paradigm be implemented
21 along-side and integral to their respective functionality *or not at all*.
22 * Both independently have methods for introducing parallelism that
23 could, if separated, benefit
24 *other areas of RISC-V not just DSP or Floating-point respectively*.
25
26 Therefore it makes a huge amount of sense to have a means and method
27 of introducing instruction parallelism in a flexible way that provides
28 implementors with the option to choose exactly where they wish to offer
29 performance improvements and where they wish to optimise for power
30 and/or area (and if that can be offered even on a per-operation basis that
31 would provide even more flexibility).
32
33 Additionally it makes sense to *split out* the parallelism inherent within
34 each of P and V, and to see if each of P and V then, in *combination* with
35 a "best-of-both" parallelism extension, would work well.
36
37 **TODO**: reword this to better suit this document:
38
39 Having looked at both P and V as they stand, they're _both_ very much
40 "separate engines" that, despite both their respective merits and
41 extremely powerful features, don't really cleanly fit into the RV design
42 ethos (or the flexible extensibility) and, as such, are both in danger
43 of not being widely adopted. I'm inclined towards recommending:
44
45 * splitting out the DSP aspects of P-SIMD to create a single-issue DSP
46 * splitting out the polymorphism, esoteric data types (GF, complex
47 numbers) and unusual operations of V to create a single-issue "Esoteric
48 Floating-Point" extension
49 * splitting out the loop-aspects, vector aspects and data-width aspects
50 of both P and V to a *new* "P-SIMD / Simple-V" and requiring that they
51 apply across *all* Extensions, whether those be DSP, M, Base, V, P -
52 everything.
53
54 # Analysis and discussion of Vector vs SIMD
55
56 There are four combined areas between the two proposals that help with
57 parallelism without over-burdening the ISA with a huge proliferation of
58 instructions:
59
60 * Fixed vs variable parallelism (fixed or variable "M" in SIMD)
61 * Implicit vs fixed instruction bit-width (integral to instruction or not)
62 * Implicit vs explicit type-conversion (compounded on bit-width)
63 * Implicit vs explicit inner loops.
64 * Masks / tagging (selecting/preventing certain indexed elements from execution)
65
66 The pros and cons of each are discussed and analysed below.
67
68 ## Fixed vs variable parallelism length
69
70 In David Patterson and Andrew Waterman's analysis of SIMD and Vector
71 ISAs, the analysis comes out clearly in favour of (effectively) variable
72 length SIMD. As SIMD is a fixed width, typically 4, 8 or in extreme cases
73 16 or 32 simultaneous operations, the setup, teardown and corner-cases of SIMD
74 are extremely burdensome except for applications whose requirements
75 *specifically* match the *precise and exact* depth of the SIMD engine.
76
77 Thus, SIMD, no matter what width is chosen, is never going to be acceptable
78 for general-purpose computation, and in the context of developing a
79 general-purpose ISA, is never going to satisfy 100 percent of implementors.
80
81 That basically leaves "variable-length vector" as the clear *general-purpose*
82 winner, at least in terms of greatly simplifying the instruction set,
83 reducing the number of instructions required for any given task, and thus
84 reducing power consumption for the same.
85
86 ## Implicit vs fixed instruction bit-width
87
88 SIMD again has a severe disadvantage here, over Vector: huge proliferation
89 of specialist instructions that target 8-bit, 16-bit, 32-bit, 64-bit, and
90 have to then have operations *for each and between each*. It gets very
91 messy, very quickly.
92
93 The V-Extension on the other hand proposes to set the bit-width of
94 future instructions on a per-register basis, such that subsequent instructions
95 involving that register are *implicitly* of that particular bit-width until
96 otherwise changed or reset.
97
98 This has some extremely useful properties, without being particularly
99 burdensome to implementations, given that instruction decode already has
100 to direct the operation to a correctly-sized width ALU engine, anyway.
101
102 Not least: in places where an ISA was previously constrained (due for
103 whatever reason, including limitations of the available operand spcace),
104 implicit bit-width allows the meaning of certain operations to be
105 type-overloaded *without* pollution or alteration of frozen and immutable
106 instructions, in a fully backwards-compatible fashion.
107
108 ## Implicit and explicit type-conversion
109
110 The Draft 2.3 V-extension proposal has (deprecated) polymorphism to help
111 deal with over-population of instructions, such that type-casting from
112 integer (and floating point) of various sizes is automatically inferred
113 due to "type tagging" that is set with a special instruction. A register
114 will be *specifically* marked as "16-bit Floating-Point" and, if added
115 to an operand that is specifically tagged as "32-bit Integer" an implicit
116 type-conversion will take placce *without* requiring that type-conversion
117 to be explicitly done with its own separate instruction.
118
119 However, implicit type-conversion is not only quite burdensome to
120 implement (explosion of inferred type-to-type conversion) but also is
121 never really going to be complete. It gets even worse when bit-widths
122 also have to be taken into consideration.
123
124 Overall, type-conversion is generally best to leave to explicit
125 type-conversion instructions, or in definite specific use-cases left to
126 be part of an actual instruction (DSP or FP)
127
128 ## Zero-overhead loops vs explicit loops
129
130 The initial Draft P-SIMD Proposal by Chuanhua Chang of Andes Technology
131 contains an extremely interesting feature: zero-overhead loops. This
132 proposal would basically allow an inner loop of instructions to be
133 repeated indefinitely, a fixed number of times.
134
135 Its specific advantage over explicit loops is that the pipeline in a
136 DSP can potentially be kept completely full *even in an in-order
137 implementation*. Normally, it requires a superscalar architecture and
138 out-of-order execution capabilities to "pre-process" instructions in order
139 to keep ALU pipelines 100% occupied.
140
141 This very simple proposal offers a way to increase pipeline activity in the
142 one key area which really matters: the inner loop.
143
144 ## Mask and Tagging
145
146 *TODO: research masks as they can be superb and extremely powerful.
147 If B-Extension is implemented and provides Bit-Gather-Scatter it
148 becomes really cool and easy to switch out certain indexed values
149 from an array of data, but actually BGS **on its own** might be
150 sufficient. Bottom line, this is complex, and needs a proper analysis.
151 The other sections are pretty straightforward.*
152
153 ## Conclusions
154
155 In the above sections the four different ways where parallel instruction
156 execution has closely and loosely inter-related implications for the ISA and
157 for implementors, were outlined. The pluses and minuses came out as
158 follows:
159
160 * Fixed vs variable parallelism: <b>variable</b>
161 * Implicit (indirect) vs fixed (integral) instruction bit-width: <b>indirect</b>
162 * Implicit vs explicit type-conversion: <b>explicit</b>
163 * Implicit vs explicit inner loops: <b>implicit</b>
164 * Tag or no-tag: <b>TODO</b>
165
166 In particular: variable-length vectors came out on top because of the
167 high setup, teardown and corner-cases associated with the fixed width
168 of SIMD. Implicit bit-width helps to extend the ISA to escape from
169 former limitations and restrictions (in a backwards-compatible fashion),
170 and implicit (zero-overhead) loops provide a means to keep pipelines
171 potentially 100% occupied *without* requiring a super-scalar or out-of-order
172 architecture.
173
174 Constructing a SIMD/Simple-Vector proposal based around even only these four
175 (five?) requirements would therefore seem to be a logical thing to do.
176
177 # Instruction Format
178
179 **TODO** *basically borrow from both P and V, which should be quite simple
180 to do, with the exception of Tag/no-tag, which needs a bit more
181 thought. V's Section 17.19 of Draft V2.3 spec is reminiscent of B's BGS
182 gather-scatterer, and, if implemented, could actually be a really useful
183 way to span 8-bit up to 64-bit groups of data, where BGS as it stands
184 and described by Clifford does **bits** of up to 16 width. Lots to
185 look at and investigate!*
186
187 # Note on implementation of parallelism
188
189 One extremely important aspect of this proposal is to respect and support
190 implementors desire to focus on power, area or performance. In that regard,
191 it is proposed that implementors be free to choose whether to implement
192 the Vector (or variable-width SIMD) parallelism as sequential operations
193 with a single ALU, fully parallel (if practical) with multiple ALUs, or
194 a hybrid combination of both.
195
196 In Broadcom's Videocore-IV, they chose hybrid, and called it "Virtual
197 Parallelism". They achieve a 16-way SIMD at an **instruction** level
198 by providing a combination of a 4-way parallel ALU *and* an externally
199 transparent loop that feeds 4 sequential sets of data into each of the
200 4 ALUs.
201
202 Also in the same core, it is worth noting that particularly uncommon
203 but essential operations (Reciprocal-Square-Root for example) are
204 *not* part of the 4-way parallel ALU but instead have a *single* ALU.
205 Under the proposed Vector (varible-width SIMD) implementors would
206 be free to do precisely that: i.e. free to choose *on a per operation
207 basis* whether and how much "Virtual Parallelism" to deploy.
208
209 It is absolutely critical to note that it is proposed that such choices MUST
210 be **entirely transparent** to the end-user and the compiler. Whilst
211 a Vector (varible-width SIM) may not precisely match the width of the
212 parallelism within the implementation, the end-user **should not care**
213 and in this way the performance benefits are gained but the ISA remains
214 simple. All that happens at the end of an instruction run is: some
215 parallel units (if there are any) would remain offline, completely
216 transparently to the ISA, the program, and the compiler.
217
218 The "SIMD considered harmful" trap of having huge complexity and extra
219 instructions to deal with corner-cases is thus avoided, and implementors
220 get to choose precisely where to focus and target the benefits of their
221 implementationefforts..
222
223 # V-Extension to Simple-V Comparative Analysis
224
225 This section covers the ways in which Simple-V is comparable
226 to, or more flexible than, V-Extension (V2.3-draft). Also covered is
227 one major weak-point (register files are fixed size, where V is
228 arbitrary length), and how best to deal with that, should V be adapted
229 to be on top of Simple-V.
230
231 The first stages of this section go over each of the sections of V2.3-draft V
232 where appropriate
233
234 ## 17.3 Shape Encoding
235
236 Simple-V's proposed means of expressing whether a register (from the
237 standard integer or the standard floating-point file) is a scalar or
238 a vector is to simply set the vector length to 1. The instruction
239 would however have to specify which register file (integer or FP) that
240 the vector-length was to be applied to.
241
242 Extended shapes (2-D etc) would not be part of Simple-V at all.
243
244 ## 17.4 Representation Encoding
245
246 Simple-V would not have representation-encoding. This is part of
247 polymorphism, which is considered too complex to implement (TODO: confirm?)
248
249 ## 17.5 Element Bitwidth
250
251 This is directly equivalent to Simple-V's "Packed", and implies that
252 integer (or floating-point) are divided down into vector-indexable
253 chunks of size Bitwidth.
254
255 In this way it becomes possible to have ADD effectively and implicitly
256 turn into ADDb (8-bit add), ADDw (16-bit add) and so on, and where
257 vector-length has been set to greater than 1, it becomes a "Packed"
258 (SIMD) instruction.
259
260 It remains to be decided what should be done when RV32 / RV64 ADD (sized)
261 opcodes are used. One useful idea would be, on an RV64 system where
262 a 32-bit-sized ADD was performed, to simply use the least significant
263 32-bits of the register (exactly as is currently done) but at the same
264 time to *respect the packed bitwidth as well*.
265
266 The extended encoding (Table 17.6) would not be part of Simple-V.
267
268 ## 17.6 Base Vector Extension Supported Types
269
270 TODO: analyse. probably exactly the same.
271
272 ## 17.7 Maximum Vector Element Width
273
274 No equivalent in Simple-V
275
276 ## 17.8 Vector Configuration Registers
277
278 TODO: analyse.
279
280 ## 17.9 Legal Vector Unit Configurations
281
282 TODO: analyse
283
284 ## 17.10 Vector Unit CSRs
285
286 TODO: analyse
287
288 ## 17.11 Maximum Vector Length (MVL)
289
290 Basically implicitly this is set to the maximum size of the register
291 file multiplied by the number of 8-bit packed ints that can fit into
292 a register (4 for RV32, 8 for RV64 and 16 for RV128).
293
294 ## !7.12 Vector Instruction Formats
295
296 No equivalent in Simple-V because *all* instructions of *all* Extensions
297 are implicitly parallelised (and packed).
298
299 ## 17.13 Polymorphic Vector Instructions
300
301 Polymorphism (implicit type-casting) is deliberately not supported
302 in Simple-V.
303
304 ## 17.14 Rapid Configuration Instructions
305
306 TODO: analyse if this is useful to have an equivalent in Simple-V
307
308 ## 17.15 Vector-Type-Change Instructions
309
310 TODO: analyse if this is useful to have an equivalent in Simple-V
311
312 ## 17.16 Vector Length
313
314 Has a direct corresponding equivalent.
315
316 ## 17.17 Predicated Execution
317
318 Predicated Execution is another name for "masking" or "tagging". Masked
319 (or tagged) implies that there is a bit field which is indexed, and each
320 bit associated with the corresponding indexed offset register within
321 the "Vector". If the tag / mask bit is 1, when a parallel operation is
322 issued, the indexed element of the vector has the operation carried out.
323 However if the tag / mask bit is *zero*, that particular indexed element
324 of the vector does *not* have the requested operation carried out.
325
326 In V2.3-draft V, there is a significant (not recommended) difference:
327 the zero-tagged elements are *set to zero*. This loses a *significant*
328 advantage of mask / tagging, particularly if the entire mask register
329 is itself a general-purpose register, as that general-purpose register
330 can be inverted, shifted, and'ed, or'ed and so on. In other words
331 it becomes possible, especially if Carry/Overflow from each vector
332 operation is also accessible, to do conditional (step-by-step) vector
333 operations including things like turn vectors into 1024-bit or greater
334 operands with very few instructions, by treating the "carry" from
335 one instruction as a way to do "Conditional add of 1 to the register
336 next door". If V2.3-draft V sets zero-tagged elements to zero, such
337 extremely powerful techniques are simply not possible.
338
339 It is noted that there is no mention of an equivalent to BEXT (element
340 skipping) which would be particularly fascinating and powerful to have.
341 In this mode, the "mask" would skip elements where its mask bit was zero
342 in either the source or the destination operand.
343
344 Lots to be discussed.
345
346 ## 17.18 Vector Load/Store Instructions
347
348 These may not have a direct equivalent in Simple-V, except if mask/tagging
349 is to be deployed.
350
351 To be discussed.
352
353 ## 17.19 Vector Register Gather
354
355 TODO
356
357 ## TODO, sort
358
359 > However, there are also several features that go beyond simply attaching VL
360 > to a scalar operation and are crucial to being able to vectorize a lot of
361 > code. To name a few:
362 > - Conditional execution (i.e., predicated operations)
363 > - Inter-lane data movement (e.g. SLIDE, SELECT)
364 > - Reductions (e.g., VADD with a scalar destination)
365
366 Ok so the Conditional and also the Reductions is one of the reasons
367 why as part of SimpleV / variable-SIMD / parallelism (gah gotta think
368 of a decent name) i proposed that it be implemented as "if you say r0
369 is to be a vector / SIMD that means operations actually take place on
370 r0,r1,r2... r(N-1)".
371
372 Consequently any parallel operation could be paused (or... more
373 specifically: vectors disabled by resetting it back to a default /
374 scalar / vector-length=1) yet the results would actually be in the
375 *main register file* (integer or float) and so anything that wasn't
376 possible to easily do in "simple" parallel terms could be done *out*
377 of parallel "mode" instead.
378
379 I do appreciate that the above does imply that there is a limit to the
380 length that SimpleV (whatever) can be parallelised, namely that you
381 run out of registers! my thought there was, "leave space for the main
382 V-Ext proposal to extend it to the length that V currently supports".
383 Honestly i had not thought through precisely how that would work.
384
385 Inter-lane (SELECT) i saw 17.19 in V2.3-Draft p117, I liked that,
386 it reminds me of the discussion with Clifford on bit-manipulation
387 (gather-scatter except not Bit Gather Scatter, *data* gather scatter): if
388 applied "globally and outside of V and P" SLIDE and SELECT might become
389 an extremely powerful way to do fast memory copy and reordering [2[.
390
391 However I haven't quite got my head round how that would work: i am
392 used to the concept of register "tags" (the modern term is "masks")
393 and i *think* if "masks" were applied to a Simple-V-enhanced LOAD /
394 STORE you would get the exact same thing as SELECT.
395
396 SLIDE you could do simply by setting say r0 vector-length to say 16
397 (meaning that if referred to in any operation it would be an implicit
398 parallel operation on *all* registers r0 through r15), and temporarily
399 set say.... r7 vector-length to say... 5. Do a LOAD on r7 and it would
400 implicitly mean "load from memory into r7 through r11". Then you go
401 back and do an operation on r0 and ta-daa, you're actually doing an
402 operation on a SLID {SLIDED?) vector.
403
404 The advantage of Simple-V (whatever) over V would be that you could
405 actually do *operations* in the middle of vectors (not just SLIDEs)
406 simply by (as above) setting r0 vector-length to 16 and r7 vector-length
407 to 5. There would be nothing preventing you from doing an ADD on r0
408 (which meant do an ADD on r0 through r15) followed *immediately in the
409 next instruction with no setup cost* a MUL on r7 (which actually meant
410 "do a parallel MUL on r7 through r11").
411
412 btw it's worth mentioning that you'd get scalar-vector and vector-scalar
413 implicitly by having one of the source register be vector-length 1
414 (the default) and one being N > 1. but without having special opcodes
415 to do it. i *believe* (or more like "logically infer or deduce" as
416 i haven't got access to the spec) that that would result in a further
417 opcode reduction when comparing [draft] V-Ext to [proposed] Simple-V.
418
419 Also, Reduction *might* be possible by specifying that the destination be
420 a scalar (vector-length=1) whilst the source be a vector. However... it
421 would be an awful lot of work to go through *every single instruction*
422 in *every* Extension, working out which ones could be parallelised (ADD,
423 MUL, XOR) and those that definitely could not (DIV, SUB). Is that worth
424 the effort? maybe. Would it result in huge complexity? probably.
425 Could an implementor just go "I ain't doing *that* as parallel!
426 let's make it virtual-parallelism (sequential reduction) instead"?
427 absolutely. So, now that I think it through, Simple-V (whatever)
428 covers Reduction as well. huh, that's a surprise.
429
430
431 > - Vector-length speculation (making it possible to vectorize some loops with
432 > unknown trip count) - I don't think this part of the proposal is written
433 > down yet.
434
435 Now that _is_ an interesting concept. A little scary, i imagine, with
436 the possibility of putting a processor into a hard infinite execution
437 loop... :)
438
439
440 > Also, note the vector ISA consumes relatively little opcode space (all the
441 > arithmetic fits in 7/8ths of a major opcode). This is mainly because data
442 > type and size is a function of runtime configuration, rather than of opcode.
443
444 yes. i love that aspect of V, i am a huge fan of polymorphism [1]
445 which is why i am keen to advocate that the same runtime principle be
446 extended to the rest of the RISC-V ISA [3]
447
448 Yikes that's a lot. I'm going to need to pull this into the wiki to
449 make sure it's not lost.
450
451 [1] inherent data type conversion: 25 years ago i designed a hypothetical
452 hyper-hyper-hyper-escape-code-sequencing ISA based around 2-bit
453 (escape-extended) opcodes and 2-bit (escape-extended) operands that
454 only required a fixed 8-bit instruction length. that relied heavily
455 on polymorphism and runtime size configurations as well. At the time
456 I thought it would have meant one HELL of a lot of CSRs... but then I
457 met RISC-V and was cured instantly of that delusion^Wmisapprehension :)
458
459 [2] Interestingly if you then also add in the other aspect of Simple-V
460 (the data-size, which is effectively functionally orthogonal / identical
461 to "Packed" of Packed-SIMD), masked and packed *and* vectored LOAD / STORE
462 operations become byte / half-word / word augmenters of B-Ext's proposed
463 "BGS" i.e. where B-Ext's BGS dealt with bits, masked-packed-vectored
464 LOAD / STORE would deal with 8 / 16 / 32 bits at a time. Where it
465 would get really REALLY interesting would be masked-packed-vectored
466 B-Ext BGS instructions. I can't even get my head fully round that,
467 which is a good sign that the combination would be *really* powerful :)
468
469 [3] ok sadly maybe not the polymorphism, it's too complicated and I
470 think would be much too hard for implementors to easily "slide in" to an
471 existing non-Simple-V implementation.  i say that despite really *really*
472 wanting IEEE 704 FP Half-precision to end up somewhere in RISC-V in some
473 fashion, for optimising 3D Graphics.  *sigh*.
474
475 ## TODO: sort
476
477 > I suspect that the "hardware loop" in question is actually a zero-overhead
478 > loop unit that diverts execution from address X to address Y if a certain
479 > condition is met.
480
481  not quite.  The zero-overhead loop unit interestingly would be at
482 an [independent] level above vector-length.  The distinctions are
483 as follows:
484
485 * Vector-length issues *virtual* instructions where the register
486 operands are *specifically* altered (to cover a range of registers),
487 whereas zero-overhead loops *specifically* do *NOT* alter the operands
488 in *ANY* way.
489
490 * Vector-length-driven "virtual" instructions are driven by *one*
491 and *only* one instruction (whether it be a LOAD, STORE, or pure
492 one/two/three-operand opcode) whereas zero-overhead loop units
493 specifically apply to *multiple* instructions.
494
495 Where vector-length-driven "virtual" instructions might get conceptually
496 blurred with zero-overhead loops is LOAD / STORE.  In the case of LOAD /
497 STORE, to actually be useful, vector-length-driven LOAD / STORE should
498 increment the LOAD / STORE memory address to correspondingly match the
499 increment in the register bank.  example:
500
501 * set vector-length for r0 to 4
502 * issue RV32 LOAD from addr 0x1230 to r0
503
504 translates effectively to:
505
506 * RV32 LOAD from addr 0x1230 to r0
507 * ...
508 * ...
509 * RV32 LOAD from addr 0x123B to r3
510
511
512 # References
513
514 * SIMD considered harmful <https://www.sigarch.org/simd-instructions-considered-harmful/>
515 * Link to first proposal <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/GuukrSjgBH8>
516 * Recommendation by Jacob Bachmeyer to make zero-overhead loop an
517 "implicit program-counter" <https://groups.google.com/a/groups.riscv.org/d/msg/isa-dev/vYVi95gF2Mo/SHz6a4_lAgAJ>
518 * Re-continuing P-Extension proposal <https://groups.google.com/a/groups.riscv.org/forum/#!msg/isa-dev/IkLkQn3HvXQ/SEMyC9IlAgAJ>
519 * First Draft P-SIMD (DSP) proposal <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/vYVi95gF2Mo>
520 * B-Extension discussion <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/zi_7B15kj6s>
521 * Broadcom VideoCore-IV <https://docs.broadcom.com/docs/12358545>
522 Figure 2 P17 and Section 3 on P16.