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