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