add SHAPE / REMAP area to VBLOCK2
[libreriscv.git] / simple_v_extension / vblock_format.mdwn
1 [[!tag standards]]
2
3 # Simple-V (Parallelism Extension Proposal) Vector Block Format
4
5 * Copyright (C) 2017, 2018, 2019 Luke Kenneth Casson Leighton
6 * Status: DRAFTv0.7.1
7 * Last edited: 2 sep 2019
8
9 [[!toc ]]
10
11 # Vector Block Format <a name="vliw-format"></a>
12
13 This is a way to give Vector and Predication Context to a group of
14 standard scalar RISC-V instructions, in a highly compact form. Program Execution Order is still preserved (unlike VLIW), just with "context" that would otherwise require much longer instructions.
15
16 The format is:
17
18 * the standard RISC-V 80 to 192 bit encoding sequence, with bits
19 defining the options to follow within the block
20 * An optional VL Block (16-bit)
21 * Optional predicate entries (8/16-bit blocks: see Predicate Table, above)
22 * Optional register entries (8/16-bit blocks: see Register Table, above)
23 * finally some 16/32/48 bit standard RV or SVPrefix opcodes follow.
24
25 Thus, the variable-length format from Section 1.5 of the RISC-V ISA is used
26 as follows:
27
28 [[!inline raw="yes" pages="simple_v_extension/vblock_format_table" ]]
29
30 Note: The VL Block format is similar to that used in [[sv_prefix_proposal]].
31
32 * Mode 0b00: set VL to the immediate, truncated to not exceed
33 MVL. Register rd is also set to the same value, if not x0.
34 * Mode 0b01: follow [[specification/sv.setvl]] rules, with RVC
35 style registers in the range x8-x15 for rs1 and rd.
36 * Mode 0b10: set both MVL and VL to the immediate. Register rd is also
37 set if not x0.
38 * Mode 0b11: reserved. All fields must be zero.
39
40 Mode 0b01 will typically be used to start vectorised loops, where
41 the VBLOCK instruction effectively embeds an optional "SETSUBVL, SETVL"
42 sequence (in compact form).
43
44 Modes 0b00 and 0b10 will typically not be used so much for loops as they
45 will be for one-off instructions such as saving the entire register file
46 to the stack with a single one-off Vectorised and predicated LD/ST,
47 or as a way to save or restore registers in a function call with a
48 single instruction.
49
50 Unlike in RVV, VL is set (within the limits of MVL) to exactly the value
51 requested, specifically so that LD/ST-MULTI style behaviour can be done
52 in a single instruction.
53
54 # VBLOCK Prefix
55
56 The purpose of the VBLOCK Prefix is to specify the context in which a
57 block of RV Scalar instructions are "vectorised" and/or predicated.
58
59 As there are not very many bits available without going into a prefix
60 format longer than 16 bits, some abbreviations are used. Two bits are
61 dedicated to specifying whether the Register and Predicate formats are
62 16 or 8 bit.
63
64 Also, the number of entries in each table is specified with an unusual
65 encoding, on the basis that if registers are to be Vectorised, it is
66 highly likely that they will be predicated as well.
67
68 The VL Block is optional and also only 16 bits: this because an RVC
69 opcode is limited by comparison.
70
71 The format is explained as follows:
72
73 * Bit 7 specifies if the register prefix block format is the full 16 bit format
74 (1) or the compact less expressive format (0).
75 * 8 bit format predicate numbering is implicit and begins from x9. Thus
76 it is critical to put blocks in the correct order as required.
77 * Bit 8 specifies if the predicate block format is 16 bit (1) or 8 bit
78 (0).
79 * Bit 15 specifies if the VL Block is present. If set to 1, the VL Block
80 immediately follows the VBLOCK instruction Prefix
81 * Bits 10 and 11 define how many RegCam entries (0,1,2,4 if bit 7 is 1,
82 otherwise 0,2,4,8) follow the (optional) VL Block.
83 * Bit 9 define how many PredCam entries follow the (optional) RegCam block.
84 If pplen is 1, it is equal to rplen. Otherwise, half rplen, rounded up.
85 * If the exact number of entries are not required, PredCam and RegCam
86 entries may be set to all zero to indicate "unused" (no effect).
87 * Bits 14 to 12 (IL) define the actual length of the instruction: total
88 number of bits is 80 + 16 times IL. Standard RV32, RVC and also
89 SVPrefix (P32C/P48/64-\*-Type) instructions fit into this space, after the
90 (optional) VL / RegCam / PredCam entries
91 * In any RVC or 32 Bit opcode, any registers within the VBLOCK-prefixed
92 format *MUST* have the RegCam and PredCam entries applied to the
93 operation (and the Vectorisation loop activated)
94 * P48 and P64 opcodes do **not** take their Register or predication
95 context from the VBLOCK tables: they do however have VL or SUBVL
96 applied (overridden when VLtyp or svlen are set).
97 * At the end of the VBLOCK Group, the RegCam and PredCam entries
98 *no longer apply*. VL, MAXVL and SUBVL on the other hand remain at
99 the values set by the last instruction (whether a CSRRW or the VL
100 Block header).
101 * Although an inefficient use of resources, it is fine to set the MAXVL,
102 VL and SUBVL CSRs with standard CSRRW instructions, within a VBLOCK.
103
104 All this would greatly reduce the amount of space utilised by Vectorised
105 instructions, given that 64-bit CSRRW requires 3, even 4 32-bit opcodes:
106 the CSR itself, a LI, and the setting up of the value into the RS
107 register of the CSR, which, again, requires a LI / LUI to get the 32
108 bit data into the CSR. To get 64-bit data into the register in order
109 to put it into the CSR(s), LOAD operations from memory are needed!
110
111 Given that each 64-bit CSR can hold only 4x PredCAM entries (or 4 RegCAM
112 entries), that's potentially 6 to eight 32-bit instructions, just to
113 establish the Vector State!
114
115 Not only that: even CSRRW on VL and MAXVL requires 64-bits (even more
116 bits if VL needs to be set to greater than 32). Bear in mind that in SV,
117 both MAXVL and VL need to be set.
118
119 By contrast, the VBLOCK prefix is only 16 bits, the VL/MAX/SubVL block is
120 only 16 bits, and as long as not too many predicates and register vector
121 qualifiers are specified, several 32-bit and 16-bit opcodes can fit into
122 the format. If the full flexibility of the 16 bit block formats are not
123 needed, more space is saved by using the 8 bit formats.
124
125 In this light, embedding the VL/MAXVL, PredCam and RegCam CSR entries
126 into a VBLOCK format makes a lot of sense.
127
128 Bear in mind the warning in an earlier section that use of VLtyp or svlen
129 in a P48 or P64 opcode within a VBLOCK Group will result in corruption
130 (use) of the STATE CSR, as the STATE CSR is shared with SVPrefix. To
131 avoid this situation, the STATE CSR may be copied into a temp register
132 and restored afterwards.
133
134 # Register Table Format
135
136 The register table format is covered in the main [[specification]],
137 included here for convenience:
138
139 [[!inline raw="yes" pages="simple_v_extension/reg_table_format" ]]
140
141 # Predicate Table Format
142
143 The predicate table format is covered in the main [[specification]],
144 included here for convenience:
145
146 [[!inline raw="yes" pages="simple_v_extension/pred_table_format" ]]
147
148 # Swizzle Table Format<a name="swizzle_format"></a>
149
150 The swizzle table format is included here for convenience:
151
152 [[!inline raw="yes" pages="simple_v_extension/swizzle_table_format" ]]
153
154 Swizzle blocks are only accessible using the "VBLOCK2" format.
155
156 # REMAP Area Format<a name="remap_format"></a>
157
158 The REMAP area is divided into two areas:
159
160 * Register-to-SHAPE. This defines which registers have which shapes.
161 Each entry is 8-bits in length.
162 * SHAPE Table entries. These are 32-bits in length and are aligned
163 to (start on) a 16 bit boundary.
164
165 REMAP Table Entries:
166
167 | 7:5 | 4:0 |
168 | -------- | ------ |
169 | shapeidx | regnum |
170
171 When both shapeidx and regnum are zero, this indicates the end of the
172 REMAP Register-to-SHAPE section. The REMAP Table section size is then
173 aligned to a 16-bit boundary. 32-bit SHAPE Table Entries then fill the
174 remainder of the REMAP area, and are indexed in order by shapeidx.
175
176 In this way, multiple registers may share the same "shape" characteristics.
177
178 # SHAPE Table Format<a name="shape_format"></a>
179
180 The shape table format is included here for convenience:
181
182 [[!inline raw="yes" pages="simple_v_extension/shape_table_format" ]]
183
184 See [[simple_v_extension/remap]] for full details on how SHAPE applies,
185 including pseudo-code.
186
187 REMAP Shape blocks are only accessible using the "VBLOCK2" format.
188
189 # CSRs:
190
191 The CSRs needed, in addition to those from the main [[specification]] are:
192
193 * pcvblk
194 * mepcvblk
195 * sepcvblk
196 * uepcvblk
197 * hepcvblk
198
199 To greatly simplify implementations, which would otherwise require a
200 way to track (cache) VBLOCK instructions, it is required to treat the
201 VBLOCK group as a separate sub-program with its own separate PC. The
202 sub-pc advances separately whilst the main PC remains "frozen", pointing
203 at the beginning of the VBLOCK instruction (not to be confused with how
204 VL works, which is exactly the same principle, except it is VStart in
205 the STATE CSR that increments).
206
207 This has implications, namely that a new set of CSRs identical to (x)epc
208 (mepc, srpc, hepc and uepc) must be created and managed and respected
209 as being a sub extension of the (x)epc set of CSRs. Thus, (x)epcvblk CSRs
210 must be context switched and saved / restored in traps.
211
212 The srcoffs and destoffs indices in the STATE CSR may be similarly
213 regarded as another sub-execution context, giving in effect two sets of
214 nested sub-levels of the RISCV Program Counter (actually, three including
215 SUBVL and ssvoffs).
216
217 # PCVBLK CSR Format
218
219 Using PCVBLK to store the progression of decoding and subsequent execution
220 of opcodes in a VBLOCK allows a simple single issue design to only need to
221 fetch 32 or 64 bits from the instruction cache on any given clock cycle.
222
223 *(This approach also alleviates one of the main concerns with the VBLOCK
224 Format: unlike a VLIW engine, a FSM no longer requires full buffering
225 of the entire VBLOCK opcode in order to begin execution. Future versions
226 may therefore potentially lift the 192 bit limit).*
227
228 To support this option (where more complex implementations may skip some
229 of these phases), VBLOCK contains partial decode state, that allows a
230 trap to occur even part-way through decode, in order to reduce latency.
231
232 The format is as follows:
233
234 | 31:30 | 29 | 28:26 | 25:24 | 23:22 | 21 | 20:5 | 4:0 |
235 |--------|-------|-------|-------|-------|------|---------|-------|
236 | status | vlset | 16xil | pplen | rplen | mode | vblock2 | opptr |
237 | 2 | 1 | 3 | 2 | 2 | 1 | 16 | 5 |
238
239 * status is the key field that effectively exposes the inner FSM (Finite
240 State Machine) directly.
241 * status = 0b00 indicates that the processor is not in "VBLOCK Mode". It
242 is instead in standard RV Scalar opcode execution mode. The processor
243 will leave this mode only after it encounters the beginning of a valid
244 VBLOCK opcode.
245 * status=0b01 indicates that vlset, 16xil, pplen, rplen and mode have
246 all been copied directly from the VBLOCK so that they do not need to be
247 read again from the instruction stream, and that VBLOCK2 has also been
248 read and stored, if 16xil was equal to 0b111.
249 * status=0b10 indicates that the VL Block has been read from the instruction
250 stream and actioned. (This means that a SETVL instruction has been
251 created and executed). It also indicates that reading of the
252 Predicate, Register and Swizzle Blocks are now being read.
253 * status=0b11 indicates that the Predicate and Register Blocks have been
254 read from the instruction stream (and put into internal Vector Context)
255 Simpler implementations are permitted to reset status back to 0b10 and
256 re-read the data after return from a trap that happened to occur in the
257 middle of a VBLOCK. They are not however permitted to destroy opptr in
258 the process, and after re-reading the Predicate and Register Blocks must
259 resume execution pointed to by opptr.
260 * opptr points to where instructions begin in the VBLOCK. 0 indicates
261 the start of the opcodes
262 (not the start of the VBLOCK),
263 and is in multiples of 16 bits (2 bytes).
264 This is the equivalent of a Program Counter, for VBLOCKs.
265 * at the end of a VBLOCK, when the last instruction executes (assuming it
266 does not change opptr to earlier in the block), status is reset to 0b00
267 to indicate exit from the VBLOCK FSM, and the current Vector Predicate
268 and Register Context destroyed (Note: the STATE CSR is **not** altered
269 purely by exit from a VBLOCK Context).
270
271 During the transition from status=0b00 to status=0b01, it is assumed
272 that the instruction stream is being read at a mininum of 32 bits at
273 a time. Therefore it is reasonable to expect that VBLOCK2 would be
274 successfully read simultaneously with the initial VBLOCK header.
275 For this reason there is no separate state in the FSM for updating
276 of the vblock2 field in PCVBLK.
277
278 When the transition from status=0b01 to status=0b10 occurs, actioning the
279 VL Block state *actually* and literally **must** be as if a SETVL instruction
280 had occurred. This can result in updating of the VL and MVL CSRs (and
281 the VL destination register target). Note, below, that this means that
282 a context-switch may save/restore VL and MVL (and the integer register file),
283 where the remaining tables have no such opportunity.
284
285 When status=0b10, and before status=0b11, there is no external indicator
286 as to how far the hardware has got in the process of reading the
287 Predicate, Register, and Swizzle Blocks. Implementations are free to use
288 any internal means to track progress, however given that if a trap occurs
289 the read process will need to be restarted (in simpler implementations),
290 there is no point having external indicators of progress. By complete
291 contrast, given that a SETVL actually writes to VL (and MVL), the VL
292 Block state *has* been actioned and thus would be successfully restored
293 by a context-switch.
294
295 When status=0b11, opptr may be written to using CSRRWI. Doing so will
296 cause execution to jump within the block, exactly as if PC had been set
297 in normal RISC-V execution. Writing a value outside of the range of the
298 instruction block will cause an illegal instruction exception. Writing
299 a value (any value) when status is not 0b11 likewise causes an illegal
300 instruction exception. To be clear: CSRRWI PCVBLK does **not** have the same
301 behaviour as CSRRW PCVBLK.
302
303 In privileged modes, obviously the above rules do not apply to the completely
304 separate (x)ePCVBLK CSRs because these are (inactive) *copies* of state,
305 not the actual active PCVBLK. Writing to PCVBLK during a trap however,
306 clearly the rules must apply.
307
308 If PCVBLK is written to with CSRRW, the same rules apply, however the
309 entire register in rs1 is treated as the new opptr.
310
311 Note that the value returned in the register rd is the *full* PCVBLK,
312 not just the opptr part.
313
314 # Limitations on instructions
315
316 As the pcvblk CSR is relative to the beginning of the VBLOCK, branch
317 and jump opcodes MUST NOT be used to point to a location inside a block:
318 only at the beginning of an opcode (including another VBLOCK, including
319 the current one). However, setting the PCVBLK CSR is permitted, to
320 unconditionally jump to any opcode within a block.
321
322 Also: calling subroutines is likewise not permitted, because PCVBLK
323 context cannot be atomically reestablished on return from the function.
324
325 ECALL, on the other hand, which will cause a trap that saves and restores
326 the full state, is permitted.
327
328 Prohibited instructions will cause an illegal instruction trap. If at
329 that point, software is capable of then working out how to emulate a
330 branch or function call successfully, by manipulating (x)ePCVBLK and
331 other state, it is not prohibited from doing so.
332
333 To reiterate: a normal jump, normal conditional branch and a normal
334 function call may only be taken by letting the VBLOCK group finish,
335 returning to "normal" standard RV mode, and then using standard RVC,
336 32 bit or P48/64-\*-type opcodes.
337
338 The exception to this rule is if the branch or jump within the VBLOCK is back to the start of the same VBLOCK. If this is used, the VBLOCK is, clearly, to be re-executed, including any optional VL blocks and any predication, register table context etc.
339
340 Given however that the tables are already established, it is only the VL block that needs to be re-run. The other tables may be left as-is.
341
342 # Links
343
344 * <https://groups.google.com/d/msg/comp.arch/yIFmee-Cx-c/jRcf0evSAAAJ>
345 * <http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2019-June/001824.html>
346 * <http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2019-June/001880.html>
347
348 # Open Questions:
349
350 * Is it necessary to stick to the RISC-V 1.5 format? Why not go with
351 using the 15th bit to allow 80 + 16\*0bnnnn bits? Perhaps to be sane,
352 limit to 256 bits (16 times 0-11).
353 * Could a "hint" be used to set which operations are parallel and which
354 are sequential?
355 * Could a new sub-instruction opcode format be used, one that does not
356 conform precisely to RISC-V rules, but *unpacks* to RISC-V opcodes?
357 no need for byte or bit-alignment
358 * Could a hardware compression algorithm be deployed? Quite likely,
359 because of the sub-execution context (sub-VBLOCK PC)
360