add associated links
[libreriscv.git] / simple_v_extension / abridged_spec.mdwn
1 # Simple-V (Parallelism Extension Proposal) Specification (Abridged)
2
3 * Copyright (C) 2017, 2018, 2019 Luke Kenneth Casson Leighton
4 * Status: DRAFTv0.6
5 * Last edited: 27 jun 2019
6 * See: main [[specification]] and [[appendix]]
7
8 [[!toc ]]
9
10 # Introduction
11
12 Simple-V is a uniform parallelism API for RISC-V hardware that allows
13 the Program Counter to enter "sub-contexts" in which, ultimately, standard
14 RISC-V scalar opcodes are executed.
15
16 Regardless of the actual amount of hardware parallelism (if any is
17 added at all by the implementor),
18 in direct contrast to SIMD
19 hardware parallelism is entirely transparent to software.
20
21 The sub-context execution is "nested" in "re-entrant" form, in the
22 following order:
23
24 * Main standard RISC-V Program Counter (PC)
25 * VBLOCK sub-execution context (PCVBLK increments whilst PC is paused).
26 * VL element loops (STATE srcoffs and destoffs increment, PC and PCVBLK pause).
27 Predication bits may be individually applied per element.
28 * Optional SUBVL element loops (STATE svdestoffs increments, VL pauses).
29 Individual predicate bits from VL loops apply to the *group* of SUBVL
30 elements.
31
32 An ancillary "SVPrefix" Format (P48/P64) [[sv_prefix_proposal]] may run
33 its own VL/SUBVL "loops" and specifies its own Register and Predication
34 format on the 32-bit RV scalar opcode embedded within it.
35
36 The [[vblock_format]] specifies how VBLOCK sub-execution contexts
37 operate.
38
39 SV is never actually switched "off". VL or SUBVL may be equal to 1,
40 and Register or Predicate over-ride tables may be empty: under such
41 circumstances the behaviour becomes effectively identical to standard
42 RV execution, however SV is never truly actually "off".
43
44 Note: **there are *no* new vector opcodes**. The scheme works *entirely*
45 on hidden context that augments (nests) *scalar* RISC-V instructions.
46 Thus it may cover existing, future and custom scalar extensions, turning
47 all existing, all future and all custom scalar operations parallel,
48 without requiring any special (identical, parallel variant) opcodes to do so.
49
50 Associated proposals for use with 3D and HPC:
51
52 * [[sv.setvl]] - replaces the use of CSRs to set VL (saves 32 bits)
53 * [[mv.x]] - provides MV.swizzle and MVX (reg[rd] = reg[reg[rs]])
54 * [[ztrans_proposal]] - provides trigonometric and transcendental operations
55
56 # CSRs <a name="csrs"></a>
57
58 There are five CSRs, available in any privilege level:
59
60 * MVL (the Maximum Vector Length)
61 * VL (which has different characteristics from standard CSRs)
62 * SUBVL (effectively a kind of SIMD)
63 * STATE (containing copies of MVL, VL and SUBVL as well as context information)
64 * PCVBLK (the current operation being executed within a VBLOCK Group)
65
66 For Privilege Levels (trap handling) there are the following CSRs,
67 where x may be u, m, s or h for User, Machine, Supervisor or Hypervisor
68 Modes respectively:
69
70 * (x)ePCVBLK (a copy of the sub-execution Program Counter, that is relative
71 to the start of the current VBLOCK Group, set on a trap).
72 * (x)eSTATE (useful for saving and restoring during context switch,
73 and for providing fast transitions)
74
75 The u/m/s CSRs are treated and handled exactly like their (x)epc
76 equivalents. On entry to or exit from a privilege level, the contents
77 of its (x)eSTATE are swapped with STATE.
78
79 (x)EPCVBLK CSRs must be treated exactly like their corresponding (x)epc
80 equivalents. See VBLOCK section for details.
81
82 ## MAXVECTORLENGTH (MVL) <a name="mvl" />
83
84 MAXVECTORLENGTH is the same concept as MVL in RVV, except that it
85 is variable length and may be dynamically set. MVL is
86 however limited to the regfile bitwidth XLEN (1-32 for RV32,
87 1-64 for RV64 and so on).
88
89 ## Vector Length (VL) <a name="vl" />
90
91 VSETVL is slightly different from RVV. Similar to RVV, VL is set to be within
92 the range 1 <= VL <= MVL (where MVL in turn is limited to 1 <= MVL <= XLEN)
93
94 VL = rd = MIN(vlen, MVL)
95
96 where 1 <= MVL <= XLEN
97
98 ## SUBVL - Sub Vector Length
99
100 This is a "group by quantity" that effectively asks each iteration
101 of the hardware loop to load SUBVL elements of width elwidth at a
102 time. Effectively, SUBVL is like a SIMD multiplier: instead of just 1
103 operation issued, SUBVL operations are issued.
104
105 The main effect of SUBVL is that predication bits are applied per
106 **group**, rather than by individual element. Legal values are 1 to 4.
107 Illegal values raise an exception.
108
109 ## STATE
110
111 This is a standard CSR that contains sufficient information for a
112 full context save/restore. It contains (and permits setting of):
113
114 * MVL
115 * VL
116 * destoffs - the destination element offset of the current parallel
117 instruction being executed
118 * srcoffs - for twin-predication, the source element offset as well.
119 * SUBVL
120 * dsvoffs - the subvector destination element offset of the current
121 parallel instruction being executed
122
123 The format of the STATE CSR is as follows:
124
125 | (31..28) | (27..26) | (25..24) | (23..18) | (17..12) | (11..6) | (5...0) |
126 | -------- | -------- | -------- | -------- | -------- | ------- | ------- |
127 | rsvd | dsvoffs | subvl | destoffs | srcoffs | vl | maxvl |
128
129 The relationship between SUBVL and the subvl field is:
130
131 | SUBVL | (25..24) |
132 | ----- | -------- |
133 | 1 | 0b00 |
134 | 2 | 0b01 |
135 | 3 | 0b10 |
136 | 4 | 0b11 |
137
138 Notes:
139
140 * The entries are truncated to be within range. Attempts to set VL to
141 greater than MAXVL will truncate VL.
142 * Both VL and MAXVL are stored offset by one. 0b000000 represents VL=1,
143 0b000001 represents VL=2. This allows the full range 1 to XLEN instead
144 of 0 to only 63.
145
146 ## VL, MVL and SUBVL instruction aliases
147
148 This table contains pseudo-assembly instruction aliases. Note the
149 subtraction of 1 from the CSRRWI pseudo variants, to compensate for the
150 reduced range of the 5 bit immediate.
151
152 | alias | CSR |
153 | - | - |
154 | SETVL rd, rs | CSRRW VL, rd, rs |
155 | SETVLi rd, #n | CSRRWI VL, rd, #n-1 |
156 | GETVL rd | CSRRW VL, rd, x0 |
157 | SETMVL rd, rs | CSRRW MVL, rd, rs |
158 | SETMVLi rd, #n | CSRRWI MVL,rd, #n-1 |
159 | GETMVL rd | CSRRW MVL, rd, x0 |
160
161 Note: CSRRC and other bitsetting may still be used, they are however
162 not particularly useful (very obscure).
163
164 ## Register key-value (CAM) table <a name="regcsrtable" />
165
166 The purpose of the Register table is to mark which registers change
167 behaviour if used in a "Standard" (normally scalar) opcode.
168
169 [[!inline raw="yes" pages="simple_v_extension/reg_table_format" ]]
170
171 Fields:
172
173 * i/f is set to "1" to indicate that the redirection/tag entry is to
174 be applied to integer registers; 0 indicates that it is relevant to
175 floating-point registers.
176 * isvec indicates that the register (whether a src or dest) is to progress
177 incrementally forward on each loop iteration. this gives the "effect"
178 of vectorisation. isvec is zero indicates "do not progress", giving
179 the "effect" of that register being scalar.
180 * vew overrides the operation's default width. See table below
181 * regkey is the register which, if encountered in an op (as src or dest)
182 is to be "redirected"
183 * in the 16-bit format, regidx is the *actual* register to be used
184 for the operation (note that it is 7 bits wide)
185
186 | vew | bitwidth |
187 | --- | ------------------- |
188 | 00 | default (XLEN/FLEN) |
189 | 01 | 8 bit |
190 | 10 | 16 bit |
191 | 11 | 32 bit |
192
193 As the above table is a CAM (key-value store) it may be appropriate
194 (faster, less gates, implementation-wise) to expand it as follows:
195
196 [[!inline raw="yes" pages="simple_v_extension/reg_table" ]]
197
198 ## Predication Table <a name="predication_csr_table"></a>
199
200 The Predication Table is a key-value store indicating whether, if a
201 given destination register (integer or floating-point) is referred to
202 in an instruction, it is to be predicated. Like the Register table, it
203 is an indirect lookup that allows the RV opcodes to not need modification.
204
205 * regidx is the register that in combination with the
206 i/f flag, if that integer or floating-point register is referred to in a
207 (standard RV) instruction results in the lookup table being referenced
208 to find the predication mask to use for this operation.
209 * predidx is the *actual* (full, 7 bit) register to be used for the
210 predication mask.
211 * inv indicates that the predication mask bits are to be inverted
212 prior to use *without* actually modifying the contents of the
213 register from which those bits originated.
214 * zeroing is either 1 or 0, and if set to 1, the operation must
215 place zeros in any element position where the predication mask is
216 set to zero. If zeroing is set to 0, unpredicated elements *must*
217 be left alone (unaltered), even when elwidth != default.
218 * ffirst is a special mode that stops sequential element processing when
219 a data-dependent condition occurs, whether a trap or a conditional test.
220 The handling of each (trap or conditional test) is slightly different:
221 see Instruction sections for further details
222
223 [[!inline raw="yes" pages="simple_v_extension/pred_table_format" ]]
224
225 Pseudocode for predication:
226
227 [[!inline raw="yes" pages="simple_v_extension/pred_table" ]]
228 [[!inline raw="yes" pages="simple_v_extension/get_pred_value" ]]
229
230 ## Swizzle Table <a name="swizzle_table"></a>
231
232 The swizzle table is a key-value store that indicates (if a given
233 register is used, and SUBVL is 2, 3 or 4) that the sub-elements are to
234 be re-ordered according to the indices in the Swizzle format.
235 Like the Predication Table, it is an indirect lookup: use of a
236 source or destination register in any given operation, if that register
237 occurs in the table, "activates" sub-vector element swizzling for
238 that register. Note that the target is taken from the "Register Table"
239 (regidx).
240
241 Source vectors are free to have the swizzle indices point to the same
242 sub-vector element. However when using swizzling on destination vectors,
243 the swizzle **must** be a permutation (no two swizzle indices point to
244 the same sub-element). An illegal instruction exception must be raised
245 if this occurs.
246
247 [[!inline raw="yes" pages="simple_v_extension/swizzle_table_format" ]]
248
249 Simplified pseudocode example, when SUBVL=4 and swizzle is set on rd:
250
251 # default indices if no swizzling table entry present
252 x, y, z, w = 0, 1, 2, 3
253
254 # lookup swizzling in table for rd
255 if swizzle_table[rd].active:
256 swizzle = swizzle_table[rd].swizzle
257
258 # decode the swizzle table entry for rd
259 x = swizzle[0:1] # sub-element 0
260 y = swizzle[2:3] # sub-element 1
261 z = swizzle[4:5] # sub-element 2
262 w = swizzle[6:7] # sub-element 3
263
264 # redirect register numbers through Register Table
265 rd = int_vec[rd ].isvector ? int_vec[rd ].regidx : rd;
266 rs1 = int_vec[rs1].isvector ? int_vec[rs1].regidx : rs1;
267 rs2 = int_vec[rs2].isvector ? int_vec[rs2].regidx : rs2;
268
269 # loop on VL: SUBVL loop is unrolled (SUBVL=4)
270 for (i in 0; i < VL; i++)
271 ireg[rd+i*4+x] = OPERATION(ireg[rs1+i*4+0], ireg[rs2+i*4+0])
272 ireg[rd+i*4+y] = OPERATION(ireg[rs1+i*4+1], ireg[rs2+i*4+1])
273 ireg[rd+i*4+z] = OPERATION(ireg[rs1+i*4+2], ireg[rs2+i*4+2])
274 ireg[rd+i*4+w] = OPERATION(ireg[rs1+i*4+3], ireg[rs2+i*4+3])
275
276 For more information on swizzling, see the Khronos wiki page
277 <https://www.khronos.org/opengl/wiki/Data_Type_(GLSL)#Swizzling>
278
279 ## Fail-on-First Mode <a name="ffirst-mode"></a>
280
281 ffirst is a special data-dependent predicate mode. There are two
282 variants: one is for faults: typically for LOAD/STORE operations, which
283 may encounter end of page faults during a series of operations. The other
284 variant is comparisons, and anything that returns "zero" or "fail". Note:
285 no instruction may operate in both fault mode and "condition fail" mode.
286
287 Fail on first critically relies on the program order being sequential,
288 even for elements. Out of order designs must *commit* in-order, and are
289 required to cancel elements at and beyond the fail point.
290
291 See [[appendix]] for more details on fail-on-first modes.
292
293 # Simplified Pseudo-code example
294
295 A greatly simplified example illustrating (just) the VL hardware for-loop
296 is as follows:
297
298 [[!inline raw="yes" pages="simple_v_extension/simple_add_example" ]]
299
300 Note that zeroing, elwidth handling, SUBVL and PCVLIW have all been
301 left out, for clarity. For examples on how to handle each, see
302 [[appendix]].
303
304 # Vector Block Format <a name="vliw-format"></a>
305
306 The Vector Block format uses the RISC-V 80-192 bit format from Section 1.5
307 of the RISC-V Spec. It permits an optional VL/MVL/SUBVL block, up to 4
308 16-bit (or 8 8-bit) Register Table entries, the same for Predicate Entries,
309 and the rest of the instruction may be either standard RV opcodes or the
310 SVPrefix opcodes ([[sv_prefix_proposal]])
311
312 [[!inline raw="yes" pages="simple_v_extension/vblock_format_table" ]]
313
314 For full details see ancillary resource: [[vblock_format]]
315
316 # Exceptions
317
318 Exception handling **MUST** be precise, in-order, and exactly
319 like Standard RISC-V as far as the instruction execution order is
320 concerned, regardless of whether it is PC, PCVBLK, VL or SUBVL that
321 is currently being incremented.
322
323 This.is extremely important. Exceptions
324 **MUST** be raised one at a time and in
325 strict sequential program order.
326
327 No instructions are permitted to be out of
328 sequence, therefore no exceptions are permitted to be, either.
329
330 # Hints
331
332 With Simple-V being capable of issuing *parallel* instructions where
333 rd=x0, the space for possible HINTs is expanded considerably. VL
334 could be used to indicate different hints. In addition, if predication
335 is set, the predication register itself could hypothetically be passed
336 in as a *parameter* to the HINT operation.
337
338 No specific hints are yet defined in Simple-V
339
340 # Subsets of RV functionality
341
342 It is permitted to only implement SVprefix and not the VBLOCK instruction
343 format option, and vice-versa. UNIX Platforms **MUST** raise illegal
344 instruction on seeing an unsupported VBLOCK or SVprefix opcode, so that
345 traps may emulate the format.
346
347 It is permitted in SVprefix to either not implement VL or not implement
348 SUBVL (see [[sv_prefix_proposal]] for full details. Again, UNIX Platforms
349 *MUST* raise illegal instruction on implementations that do not support
350 VL or SUBVL.
351
352 It is permitted to limit the size of either (or both) the register files
353 down to the original size of the standard RV architecture. However, below
354 the mandatory limits set in the RV standard will result in non-compliance
355 with the SV Specification.
356