add discussion with a.waterman
[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
319
320
321 > However, there are also several features that go beyond simply attaching VL
322 > to a scalar operation and are crucial to being able to vectorize a lot of
323 > code. To name a few:
324 > - Conditional execution (i.e., predicated operations)
325 > - Inter-lane data movement (e.g. SLIDE, SELECT)
326 > - Reductions (e.g., VADD with a scalar destination)
327
328 Ok so the Conditional and also the Reductions is one of the reasons
329 why as part of SimpleV / variable-SIMD / parallelism (gah gotta think
330 of a decent name) i proposed that it be implemented as "if you say r0
331 is to be a vector / SIMD that means operations actually take place on
332 r0,r1,r2... r(N-1)".
333
334 Consequently any parallel operation could be paused (or... more
335 specifically: vectors disabled by resetting it back to a default /
336 scalar / vector-length=1) yet the results would actually be in the
337 *main register file* (integer or float) and so anything that wasn't
338 possible to easily do in "simple" parallel terms could be done *out*
339 of parallel "mode" instead.
340
341 I do appreciate that the above does imply that there is a limit to the
342 length that SimpleV (whatever) can be parallelised, namely that you
343 run out of registers! my thought there was, "leave space for the main
344 V-Ext proposal to extend it to the length that V currently supports".
345 Honestly i had not thought through precisely how that would work.
346
347 Inter-lane (SELECT) i saw 17.19 in V2.3-Draft p117, I liked that,
348 it reminds me of the discussion with Clifford on bit-manipulation
349 (gather-scatter except not Bit Gather Scatter, *data* gather scatter): if
350 applied "globally and outside of V and P" SLIDE and SELECT might become
351 an extremely powerful way to do fast memory copy and reordering [2[.
352
353 However I haven't quite got my head round how that would work: i am
354 used to the concept of register "tags" (the modern term is "masks")
355 and i *think* if "masks" were applied to a Simple-V-enhanced LOAD /
356 STORE you would get the exact same thing as SELECT.
357
358 SLIDE you could do simply by setting say r0 vector-length to say 16
359 (meaning that if referred to in any operation it would be an implicit
360 parallel operation on *all* registers r0 through r15), and temporarily
361 set say.... r7 vector-length to say... 5. Do a LOAD on r7 and it would
362 implicitly mean "load from memory into r7 through r11". Then you go
363 back and do an operation on r0 and ta-daa, you're actually doing an
364 operation on a SLID {SLIDED?) vector.
365
366 The advantage of Simple-V (whatever) over V would be that you could
367 actually do *operations* in the middle of vectors (not just SLIDEs)
368 simply by (as above) setting r0 vector-length to 16 and r7 vector-length
369 to 5. There would be nothing preventing you from doing an ADD on r0
370 (which meant do an ADD on r0 through r15) followed *immediately in the
371 next instruction with no setup cost* a MUL on r7 (which actually meant
372 "do a parallel MUL on r7 through r11").
373
374 btw it's worth mentioning that you'd get scalar-vector and vector-scalar
375 implicitly by having one of the source register be vector-length 1
376 (the default) and one being N > 1. but without having special opcodes
377 to do it. i *believe* (or more like "logically infer or deduce" as
378 i haven't got access to the spec) that that would result in a further
379 opcode reduction when comparing [draft] V-Ext to [proposed] Simple-V.
380
381 Also, Reduction *might* be possible by specifying that the destination be
382 a scalar (vector-length=1) whilst the source be a vector. However... it
383 would be an awful lot of work to go through *every single instruction*
384 in *every* Extension, working out which ones could be parallelised (ADD,
385 MUL, XOR) and those that definitely could not (DIV, SUB). Is that worth
386 the effort? maybe. Would it result in huge complexity? probably.
387 Could an implementor just go "I ain't doing *that* as parallel!
388 let's make it virtual-parallelism (sequential reduction) instead"?
389 absolutely. So, now that I think it through, Simple-V (whatever)
390 covers Reduction as well. huh, that's a surprise.
391
392
393 > - Vector-length speculation (making it possible to vectorize some loops with
394 > unknown trip count) - I don't think this part of the proposal is written
395 > down yet.
396
397 Now that _is_ an interesting concept. A little scary, i imagine, with
398 the possibility of putting a processor into a hard infinite execution
399 loop... :)
400
401
402 > Also, note the vector ISA consumes relatively little opcode space (all the
403 > arithmetic fits in 7/8ths of a major opcode). This is mainly because data
404 > type and size is a function of runtime configuration, rather than of opcode.
405
406 yes. i love that aspect of V, i am a huge fan of polymorphism [1]
407 which is why i am keen to advocate that the same runtime principle be
408 extended to the rest of the RISC-V ISA [3]
409
410 Yikes that's a lot. I'm going to need to pull this into the wiki to
411 make sure it's not lost.
412
413 l.
414
415 [1] inherent data type conversion: 25 years ago i designed a hypothetical
416 hyper-hyper-hyper-escape-code-sequencing ISA based around 2-bit
417 (escape-extended) opcodes and 2-bit (escape-extended) operands that
418 only required a fixed 8-bit instruction length. that relied heavily
419 on polymorphism and runtime size configurations as well. At the time
420 I thought it would have meant one HELL of a lot of CSRs... but then I
421 met RISC-V and was cured instantly of that delusion^Wmisapprehension :)
422
423 [2] Interestingly if you then also add in the other aspect of Simple-V
424 (the data-size, which is effectively functionally orthogonal / identical
425 to "Packed" of Packed-SIMD), masked and packed *and* vectored LOAD / STORE
426 operations become byte / half-word / word augmenters of B-Ext's proposed
427 "BGS" i.e. where B-Ext's BGS dealt with bits, masked-packed-vectored
428 LOAD / STORE would deal with 8 / 16 / 32 bits at a time. Where it
429 would get really REALLY interesting would be masked-packed-vectored
430 B-Ext BGS instructions. I can't even get my head fully round that,
431 which is a good sign that the combination would be *really* powerful :)
432
433 [3] ok sadly maybe not the polymorphism, it's too complicated and I
434 think would be much too hard for implementors to easily "slide in" to an
435 existing non-Simple-V implementation.  i say that despite really *really*
436 wanting IEEE 704 FP Half-precision to end up somewhere in RISC-V in some
437 fashion, for optimising 3D Graphics.  *sigh*.
438
439 # References
440
441 * SIMD considered harmful <https://www.sigarch.org/simd-instructions-considered-harmful/>
442 * Link to first proposal <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/GuukrSjgBH8>
443 * Recommendation by Jacob Bachmeyer to make zero-overhead loop an
444 "implicit program-counter" <https://groups.google.com/a/groups.riscv.org/d/msg/isa-dev/vYVi95gF2Mo/SHz6a4_lAgAJ>
445 * Re-continuing P-Extension proposal <https://groups.google.com/a/groups.riscv.org/forum/#!msg/isa-dev/IkLkQn3HvXQ/SEMyC9IlAgAJ>
446 * First Draft P-SIMD (DSP) proposal <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/vYVi95gF2Mo>
447 * B-Extension discussion <https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/zi_7B15kj6s>
448 * Broadcom VideoCore-IV <https://docs.broadcom.com/docs/12358545>
449 Figure 2 P17 and Section 3 on P16.