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