53dda30b8109f46e38c63de84eb3affba5527b3e
[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 effectivrly 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 ## Fail-on-First Mode <a name="ffirst-mode"></a>
225
226 ffirst is a special data-dependent predicate mode. There are two
227 variants: one is for faults: typically for LOAD/STORE operations, which
228 may encounter end of page faults during a series of operations. The other
229 variant is comparisons, and anything that returns "zero" or "fail". Note:
230 no instruction may operate in both fault mode and "condition fail" mode.
231
232 Fail on first critically relies on the program order being sequential,
233 even for elements. Out of order designs must *commit* in-order, and are
234 required to cancel elements at and beyond the fail point.
235
236 See [[appendix]] for more details on fail-on-first modes.
237
238 # Simplified Pseudo-code example
239
240 A greatly simplified example illustrating (just) the VL hardware for-loop
241 is as follows:
242
243 [[!inline raw="yes" pages="simple_v_extension/simple_add_example" ]]
244
245 Note that zeroing, elwidth handling, SUBVL and PCVLIW have all been
246 left out, for clarity. For examples on how to handle each, see
247 [[appendix]].
248
249 # Vector Block Format <a name="vliw-format"></a>
250
251 The Vector Block format uses the RISC-V 80-192 bit format from Section 1.5
252 of the RISC-V Spec. It permits an optional VL/MVL/SUBVL block, up to 4
253 16-bit (or 8 8-bit) Register Table entries, the same for Predicate Entries,
254 and the rest of the instruction may be either standard RV opcodes or the
255 SVPrefix opcodes ([[sv_prefix_proposal]])
256
257 [[!inline raw="yes" pages="simple_v_extension/vblock_format_table" ]]
258
259 For full details see ancillary resource: [[vblock_format]]
260
261 # Exceptions
262
263 Exception handling **MUST** be precise, in-order, and exactly
264 like Standard RISC-V as far as the instruction execution order is
265 concerned, regardless of whether it is PC, PCVBLK, VL or SUBVL that
266 is currently being incremented.
267
268 This.is extremely important. Exceptions
269 **MUST** be raised one at a time and in
270 strict sequential program order.
271
272 No instructions are permitted to be out of
273 sequence, therefore no exceptions are permitted to be, either.
274
275 # Hints
276
277 With Simple-V being capable of issuing *parallel* instructions where
278 rd=x0, the space for possible HINTs is expanded considerably. VL
279 could be used to indicate different hints. In addition, if predication
280 is set, the predication register itself could hypothetically be passed
281 in as a *parameter* to the HINT operation.
282
283 No specific hints are yet defined in Simple-V
284
285 # Subsets of RV functionality
286
287 It is permitted to only implement SVprefix and not the VBLOCK instruction
288 format option, and vice-versa. UNIX Platforms **MUST** raise illegal
289 instruction on seeing an unsupported VBLOCK or SVprefix opcode, so that
290 traps may emulate the format.
291
292 It is permitted in SVprefix to either not implement VL or not implement
293 SUBVL (see [[sv_prefix_proposal]] for full details. Again, UNIX Platforms
294 *MUST* raise illegal instruction on implementations that do not support
295 VL or SUBVL.
296
297 It is permitted to limit the size of either (or both) the register files
298 down to the original size of the standard RV architecture. However, below
299 the mandatory limits set in the RV standard will result in non-compliance
300 with the SV Specification.
301