65e0f373d126ea7c7a4267dacca0b55106397c6e
[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 16 bit format:
159
160 | RegCAM | 15 | (14..8) | 7 | (6..5) | (4..0) |
161 | ------ | - | - | - | ------ | ------- |
162 | 0 | isvec0 | regidx0 | i/f | vew0 | regkey |
163 | 1 | isvec1 | regidx1 | i/f | vew1 | regkey |
164 | 2 | isvec2 | regidx2 | i/f | vew2 | regkey |
165 | 3 | isvec3 | regidx3 | i/f | vew3 | regkey |
166
167 8 bit format:
168
169 | RegCAM | | 7 | (6..5) | (4..0) |
170 | ------ | | - | ------ | ------- |
171 | 0 | | i/f | vew0 | regnum |
172
173 Mapping the 8-bit to 16-bit format:
174
175 | RegCAM | 15 | (14..8) | 7 | (6..5) | (4..0) |
176 | ------ | - | - | - | ------ | ------- |
177 | 0 | isvec=1 | regnum0<<2 | i/f | vew0 | regnum0 |
178 | 1 | isvec=1 | regnum1<<2 | i/f | vew1 | regnum1 |
179 | 2 | isvec=1 | regnum2<<2 | i/f | vew2 | regnum2 |
180 | 3 | isvec=1 | regnum2<<2 | i/f | vew3 | regnum3 |
181
182 Fields:
183
184 * i/f is set to "1" to indicate that the redirection/tag entry is to
185 be applied to integer registers; 0 indicates that it is relevant to
186 floating-point registers.
187 * isvec indicates that the register (whether a src or dest) is to progress
188 incrementally forward on each loop iteration. this gives the "effect"
189 of vectorisation. isvec is zero indicates "do not progress", giving
190 the "effect" of that register being scalar.
191 * vew overrides the operation's default width. See table below
192 * regkey is the register which, if encountered in an op (as src or dest)
193 is to be "redirected"
194 * in the 16-bit format, regidx is the *actual* register to be used
195 for the operation (note that it is 7 bits wide)
196
197 | vew | bitwidth |
198 | --- | ------------------- |
199 | 00 | default (XLEN/FLEN) |
200 | 01 | 8 bit |
201 | 10 | 16 bit |
202 | 11 | 32 bit |
203
204 As the above table is a CAM (key-value store) it may be appropriate
205 (faster, less gates, implementation-wise) to expand it as follows:
206
207 [[!inline raw="yes" pages="simple_v_extension/reg_table" ]]
208
209 ## Predication Table <a name="predication_csr_table"></a>
210
211 The Predication Table is a key-value store indicating whether, if a
212 given destination register (integer or floating-point) is referred to
213 in an instruction, it is to be predicated. Like the Register table, it
214 is an indirect lookup that allows the RV opcodes to not need modification.
215
216 * regidx is the register that in combination with the
217 i/f flag, if that integer or floating-point register is referred to in a
218 (standard RV) instruction results in the lookup table being referenced
219 to find the predication mask to use for this operation.
220 * predidx is the *actual* (full, 7 bit) register to be used for the
221 predication mask.
222 * inv indicates that the predication mask bits are to be inverted
223 prior to use *without* actually modifying the contents of the
224 register from which those bits originated.
225 * zeroing is either 1 or 0, and if set to 1, the operation must
226 place zeros in any element position where the predication mask is
227 set to zero. If zeroing is set to 0, unpredicated elements *must*
228 be left alone (unaltered), even when elwidth != default.
229 * ffirst is a special mode that stops sequential element processing when
230 a data-dependent condition occurs, whether a trap or a conditional test.
231 The handling of each (trap or conditional test) is slightly different:
232 see Instruction sections for further details
233
234 16 bit format:
235
236 | PrCSR | (15..11) | 10 | 9 | 8 | (7..1) | 0 |
237 | ----- | - | - | - | - | ------- | ------- |
238 | 0 | predidx | zero0 | inv0 | i/f | regidx | ffirst0 |
239 | 1 | predidx | zero1 | inv1 | i/f | regidx | ffirst1 |
240 | 2 | predidx | zero2 | inv2 | i/f | regidx | ffirst2 |
241 | 3 | predidx | zero3 | inv3 | i/f | regidx | ffirst3 |
242
243 Note: predidx=x0, zero=1, inv=1 is a RESERVED encoding. Its use must
244 generate an illegal instruction trap.
245
246 8 bit format:
247
248 | PrCSR | 7 | 6 | 5 | (4..0) |
249 | ----- | - | - | - | ------- |
250 | 0 | zero0 | inv0 | i/f | regnum |
251
252 Mapping from 8 to 16 bit format, the table becomes:
253
254 | PrCSR | (15..11) | 10 | 9 | 8 | (7..1) | 0 |
255 | ----- | - | - | - | - | ------- | ------- |
256 | 0 | x9 | zero0 | inv0 | i/f | regnum | ff=0 |
257 | 1 | x10 | zero1 | inv1 | i/f | regnum | ff=0 |
258 | 2 | x11 | zero2 | inv2 | i/f | regnum | ff=0 |
259 | 3 | x12 | zero3 | inv3 | i/f | regnum | ff=0 |
260
261 Pseudocode for predication:
262
263
264 [[!inline raw="yes" pages="simple_v_extension/pred_table" ]]
265 [[!inline raw="yes" pages="simple_v_extension/get_pred_value" ]]
266
267 ## Fail-on-First Mode <a name="ffirst-mode"></a>
268
269 ffirst is a special data-dependent predicate mode. There are two
270 variants: one is for faults: typically for LOAD/STORE operations,
271 which may encounter end of page faults during a series of operations.
272 The other variant is comparisons such as FEQ (or the augmented behaviour
273 of Branch), and any operation that returns a result of zero (whether
274 integer or floating-point). In the FP case, this includes negative-zero.
275
276 Note that the execution order must "appear" to be sequential for ffirst
277 mode to work correctly. An in-order architecture must execute the element
278 operations in sequence, whilst an out-of-order architecture must *commit*
279 the element operations in sequence (giving the appearance of in-order
280 execution).
281
282 Note also, that if ffirst mode is needed without predication, a special
283 "always-on" Predicate Table Entry may be constructed by setting
284 inverse-on and using x0 as the predicate register. This
285 will have the effect of creating a mask of all ones, allowing ffirst
286 to be set.
287
288 See [[appendix]] for more details on fail-on-first modes.
289
290 # Exceptions
291
292 TODO: expand.
293
294 # Hints
295
296 With Simple-V being capable of issuing *parallel* instructions where
297 rd=x0, the space for possible HINTs is expanded considerably. VL
298 could be used to indicate different hints. In addition, if predication
299 is set, the predication register itself could hypothetically be passed
300 in as a *parameter* to the HINT operation.
301
302 No specific hints are yet defined in Simple-V
303
304 # Vector Block Format <a name="vliw-format"></a>
305
306 See ancillary resource: [[vblock_format]]
307
308 # Subsets of RV functionality
309
310 It is permitted to only implement SVprefix and not the VBLOCK instruction
311 format option, and vice-versa. UNIX Platforms **MUST** raise illegal
312 instruction on seeing an unsupported VBLOCK or SVprefix opcode, so that
313 traps may emulate the format.
314
315 It is permitted in SVprefix to either not implement VL or not implement
316 SUBVL (see [[sv_prefix_proposal]] for full details. Again, UNIX Platforms
317 *MUST* raise illegal instruction on implementations that do not support
318 VL or SUBVL.
319
320 It is permitted to limit the size of either (or both) the register files
321 down to the original size of the standard RV architecture. However, below
322 the mandatory limits set in the RV standard will result in non-compliance
323 with the SV Specification.
324