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