(no commit message)
[libreriscv.git] / openpower / sv / overview.mdwn
1 # SV Overview
2
3 [[!toc]]
4
5 This document provides an overview and introduction as to why SV exists, and how it works.
6
7 SIMD, the primary method for easy parallelism of the past 30 years in Computer Architectures, is [known to be harmful](https://www.sigarch.org/simd-instructions-considered-harmful/). SIMD provides
8 a seductive simplicity that is easy to implement in hardware. Even with predication added, SIMD only becomes more and more problematic with each power of two SIMD width increase introduced through an ISA revision. The opcode proliferation, at O(N^6), inexorably spirals out of control in the ISA, the hardware, the software and the compilers.
9
10 Cray-style variable-length Vectors on the other hand result in stunningly elegant and small loops, with no alarmingly high setup and cleanup code, where at the hardware level the microarchitecture may execute from one element right the way through to tens of thousands at a time, yet the executable remains exactly the same and the ISA remains clear, true to the RISC paradigm, and clean. Unlike in SIMD, powers of two limitations are not involved in either the hardware nor in the assembly code.
11
12 SimpleV takes the Cray style Vector principle and applies it to a Scalar ISA, in the process allowing register file size increases using "tagging" (similar to how x86 originally extended registers from 32 to 64 bit).
13
14 The fundamentals are:
15
16 * The Program Counter gains a "Sub Counter" context.
17 * Vectorisation pauses the PC and runs a loop from 0 to VL-1
18 (where VL is Vector Length). This may be thought of as a
19 "Sub-PC"
20 * Some registers may be "tagged" as Vectors
21 * During the loop, "Vector"-tagged register are incremented by
22 one with each iteration, executing the *same instruction*
23 but with *different registers*
24 * Once the loop is completed *only then* is the Program Counter
25 allowed to move to the next instruction.
26
27 In OpenPOWER ISA v3.0B pseudo-code form, an ADD operation, assuming both source and destination have been "tagged" as Vectors, is simply:
28
29 for i = 0 to VL-1:
30 GPR(RT+i) = GPR(RA+i) + GPR(RB+i)
31
32 At its heart, SimpleV really is this simple. On top of this fundamental basis further refinements can be added which build up towards an extremely powerful Vector augmentation system, with very little in the way of additional opcodes required: simply external "context".
33
34 RISC-V RVV as of version 0.9 is over 180 instructions (more than the rest of RV64G combined). Over 95% of that functionality is added to OpenPOWER v3 0B, by SimpleV augmentation, with around 5 to 8 instructions.
35
36 Even in OpenPOWER v3.0B, the Scalar Integer ISA is around 150 instructions, with IEEE754 FP adding approximately 80 more. VSX, being based on SIMD design principles, adds somewhere in the region of 600 more. SimpleV again provides over 95% of VSX functionality, simply by augmenting the *Scalar* OpenPOWER ISA, and in the process providing features such as predication, which VSX is entirely missing.
37
38 The rest of this document builds on the above simple loop to add:
39
40 * Vector-Scalar, Scalar-Vector and Scalar-Scalar operation
41 * Traditional Vector operations (VSPLAT, VINSERT, VCOMPRESS etc)
42 * Predication masks (essential for parallel if/else constructs)
43 * 8, 16 and 32 bit integer operations, and both FP16 and BF16.
44 * Fail-on-first (introduced in ARM SVE2)
45 * A new concept: Data-dependent fail-first
46 * Condition-Register based *post-result* predication (also new)
47 * A completely new concept: "Twin Predication"
48 * vec2/3/4 "Subvectors" and Swizzling (standard fare for 3D)
49
50 All of this is *without modifying the OpenPOWER v3.0B ISA*, except to add "wrapping context", similar to how v3.1B 64 Prefixes work.
51
52 In fairness to both VSX and RVV, there are things that are not provided by SimpleV:
53
54 * 128 bit or above arithmetic and other operations
55 (VSX Rijndael and SHA primitives; VSX shuffle and bitpermute operations)
56 * register files above 128
57 * Vector lengths over 64
58 * Unit-strided LD/ST and other comprehensive memory operations
59 (struct-based LD/ST from RVV for example)
60 * 32-bit instruction lengths. [[svp64]] had to be added as 64 bit.
61
62 These are not insurmountable limitations, that, over time, may well be added in future revisions of SV.
63
64 # Adding Scalar / Vector
65
66 The first augmentation to the simple loop is to add the option for all source and destinations to all be either scalar or vector. As a FSM this is where our "simple" loop gets its first complexity.
67
68 function op_add(rd, rs1, rs2) # add not VADD!
69 int id=0, irs1=0, irs2=0;
70 for i = 0 to VL-1:
71 ireg[rd+id] <= ireg[rs1+irs1] + ireg[rs2+irs2];
72 if (!rd.isvec) break;
73 if (rd.isvec) { id += 1; }
74 if (rs1.isvec) { irs1 += 1; }
75 if (rs2.isvec) { irs2 += 1; }
76
77 With some walkthroughs it is clear that the loop exits immediately after the first scalar destination result is written, and that when the destination is a Vector the loop proceeds to fill up the register file, sequentially, starting at `rd` and ending at `rd+VL-1`. The two source registers will, independently, either remain pointing at `rs1` or `rs2` respectively, or, if marked as Vectors, will march incrementally in lockstep, producing element results along the way, as the destination also progresses through elements.
78
79 In this way all the eight permutations of Scalar and Vector behaviour are covered, although without predication the scalar-destination ones are reduced in usefulness. It does however clearly illustrate the principle.
80
81 Note in particular: there is no separate Scalar add instruction and separate Vector instruction and separate Scalar-Vector instruction, *and there is no separate Vector register file*: it's all the same instruction, on the standard register file, just with a loop. Scalar happens to set that loop size to one.
82
83 # Adding single predication
84
85 The next step is to add a single predicate mask. This is where it gets interesting. Predicate masks are a bitvector, each bit specifying, in order, whether the element operation is to be skipped ("masked out") or allowed. If there is no predicate, it is set to all 1s, which is effectively the same as "no predicate".
86
87 function op_add(rd, rs1, rs2) # add not VADD!
88 int id=0, irs1=0, irs2=0;
89 predval = get_pred_val(FALSE, rd);
90 for i = 0 to VL-1:
91 if (predval & 1<<i) # predication bit test
92 ireg[rd+id] <= ireg[rs1+irs1] + ireg[rs2+irs2];
93 if (!rd.isvec) break;
94 if (rd.isvec) { id += 1; }
95 if (rs1.isvec) { irs1 += 1; }
96 if (rs2.isvec) { irs2 += 1; }
97
98 The key modification is to skip the creation and storage of the result if the relevant predicate mask bit is clear, but *not the progression through the registers*.
99
100 A particularly interesting case is if the destination is scalar, and the first few bits of the predicate are zero. The loop proceeds to increment the Scalar *source* registers until the first nonzero predicate bit is found, whereupon a single result is computed, and *then* the loop exits. This therefore uses the predicate to perform Vector source indexing. This case was not possible without the predicate mask.
101
102 If all three registers are marked as Vector then the "traditional" predicated Vector behaviour is provided. Yet, just as before, all other options are still provided, right the way back to the pure-scalar case, as if this were a straight OpenPOWER v3.0B non-augmented instruction.
103
104 Predication therefore provides several modes traditionally seen in Vector ISAs, particularly if the predicate may be set conveniently as a single bit: this gives VINSERT (VINDEX) behaviour. VSPLAT (result broadcasting) is provided by making the sources scalar and the destination a vector.
105
106 # Predicate "zeroing" mode
107
108 Sometimes with predication it is ok to leave the masked-out element alone (not modify the result) however sometimes it is better to zero the masked-out elements. This can be combined with bit-wise ORing to build up vectors from multiple predicate patterns. Our pseudocode therefore ends up as follows, to take that into account:
109
110 function op_add(rd, rs1, rs2) # add not VADD!
111 int id=0, irs1=0, irs2=0;
112 predval = get_pred_val(FALSE, rd);
113 for i = 0 to VL-1:
114 if (predval & 1<<i) # predication bit test
115 ireg[rd+id] <= ireg[rs1+irs1] + ireg[rs2+irs2];
116 if (!rd.isvec) break;
117 else if zeroing:
118 ireg[rd+id] = 0
119 if (rd.isvec) { id += 1; }
120 if (rs1.isvec) { irs1 += 1; }
121 if (rs2.isvec) { irs2 += 1; }
122
123 Many Vector systems either have zeroing or they have nonzeroing, they do not have both. This is because they usually have separate Vector register files. However SV sits on top of standard register files and consequently there are advantages to both, so both are provided.
124
125 # Element Width overrides
126
127 All good Vector ISAs have the usual bitwidths for operations: 8/16/32/64 bit integer operations, and IEEE754 FP32 and 64. Often also included is FP16 and more recently BF16. The *really* good Vector ISAs have variable-width vectors right down to bitlevel, and as high as 1024 bit arithmetic per element, as well as IEEE754 FP128.
128
129 SV has an "override" system that *changes* the bitwidth of operations that were intended by the original scalar ISA designers to have (for example) 64 bit operations (only). The override widths are 8, 16 and 32 for integer, and FP16 and FP32 for IEEE754 (with BF16 to be added in the future).
130
131 This presents a particularly intriguing conundrum given that the OpenPOWER Scalar ISA was never designed with for example 8 bit operations in mind, let alone Vectors of 8 bit.
132
133 The solution comes in terms of rethinking the definition of a Register File. The typical regfile may be considered to be a multi-ported SRAM block, 64 bits wide and usually 32 entries deep, to give 32 64 bit registers. Conceptually, to get our variable element width vectors, we may think of the regfile as insead being the following c-based data structure:
134
135 typedef union {
136 uint8_t actual_bytes[8];
137 uint8_t b[0]; // array of type uint8_t
138 uint16_t s[0];
139 uint32_t i[0];
140 uint64_t l[0]; // default OpenPOWER ISA uses this
141 } reg_t;
142
143 reg_t int_regfile[128]; // SV extends to 128 regs
144
145 Then, our simple loop, instead of accessing the array of regfile entries with a computed index, would access the appropriate element of the appropriate type. Thus we have a series of overlapping conceptual arrays that each start at what is traditionally thought of as "a register". It then helps if we have a couple of routines:
146
147 get_polymorphed_reg(reg, bitwidth, offset):
148 reg_t res = 0;
149 if (!reg.isvec): # scalar
150 offset = 0
151 if bitwidth == 8:
152 reg.b = int_regfile[reg].b[offset]
153 elif bitwidth == 16:
154 reg.s = int_regfile[reg].s[offset]
155 elif bitwidth == 32:
156 reg.i = int_regfile[reg].i[offset]
157 elif bitwidth == default: # 64
158 reg.l = int_regfile[reg].l[offset]
159 return res
160
161 set_polymorphed_reg(reg, bitwidth, offset, val):
162 if (!reg.isvec): # scalar
163 offset = 0
164 if bitwidth == 8:
165 int_regfile[reg].b[offset] = val
166 elif bitwidth == 16:
167 int_regfile[reg].s[offset] = val
168 elif bitwidth == 32:
169 int_regfile[reg].i[offset] = val
170 elif bitwidth == default: # 64
171 int_regfile[reg].l[offset] = val
172
173 These basically provide a convenient parameterised way to access the register file, at an arbitrary vector element offset and an arbitrary element width. Our first simple loop thus becomes:
174
175 for i = 0 to VL-1:
176 src1 = get_polymorphed_reg(rs1, srcwid, i)
177 src2 = get_polymorphed_reg(rs2, srcwid, i)
178 result = src1 + src2 # actual add here
179 set_polymorphed_reg(rd, destwid, i, result)
180
181 With this loop, if elwidth=16 and VL=3 the first 48 bits of the target register will contain three 16 bit addition results, and the upper 16 bits will be *unaltered*.
182
183 Note that things such as zero/sign-extension (and predication) have been left out to illustrate the elwidth concept. Also note that it turns out to be important to perform the operation at the maximum bitwidth - `max(srcwid, destwid)` - such that any truncation, rounding errors or other artefacts may all be ironed out. This turns out to be important when applying Saturation for Audio DSP workloads.
184
185 Other than that, element width overrides, which can be applied to *either* source or destination or both, are pretty straightforward, conceptually. The details, for hardware engineers, involve byte-level write-enable lines, which is exactly what is used on SRAMs anyway. Compiler writers have to alter Register Allocation Tables to byte-level granularity.
186
187 One critical thing to note: upper parts of the underlying 64 bit register are *not zero'd out* by a write involving a non-aligned Vector Length. An 8 bit operation with VL=7 will *not* overwrite the 8th byte of the destination. The only situation where a full overwrite occurs is on "default" behaviour. This is extremely important to consider the register file as a byte-level store, not a 64-bit-level store.
188
189 # Quick recap so far
190
191 The above functionality pretty much covers around 85% of Vector ISA needs. Predication is provided so that parallel if/then/else constructs can be performed: critical given that sequential if/then statements and branches simply do not translate successfully to Vector workloads. VSPLAT capability is provided which is approximately 20% of all GPU workload operations. Also covered, with elwidth overriding, is the smaller arithmetic operations that caused ISAs developed from the late 80s onwards to get themselves into a tiz when adding "Multimedia" acceleration aka "SIMD" instructions.
192
193 Experienced Vector ISA readers will however have noted that VCOMPRESS and VEXPAND are missing, as is Vector "reduce" (mapreduce) capability. Compress and Expand are covered by Twin Predication, and yet to also be covered is fail-on-first, CR-based result predication, and Subvectors and Swizzle.
194
195 ## SUBVL <a name="subvl"></a>
196
197 Adding in support for SUBVL is a matter of adding in an extra inner
198 for-loop, where register src and dest are still incremented inside the
199 inner part. Predication is still taken from the VL index, however it is applied to the whole subvector:
200
201 function op_add(rd, rs1, rs2) # add not VADD!
202  int id=0, irs1=0, irs2=0;
203  predval = get_pred_val(FALSE, rd);
204 for i = 0 to VL-1:
205 if (predval & 1<<i) # predication uses intregs
206 for (s = 0; s < SUBVL; s++)
207 sd = id*SUBVL + s
208 srs1 = irs1*SUBVL + s
209 srs2 = irs2*SUBVL + s
210 ireg[rd+sd] <= ireg[rs1+srs1] + ireg[rs2+srs2];
211 if (!rd.isvec) break;
212 if (rd.isvec) { id += 1; }
213 if (rs1.isvec) { irs1 += 1; }
214 if (rs2.isvec) { irs2 += 1; }
215
216 # Swizzle <a name="subvl"></a>
217
218 Swizzle is particularly important for 3D work. It allows in-place reordering of XYZW, ARGB etc. and access of sub-portions of the same in arbitrary order *without* requiring timeconsuming scalar mv instructions (scalar due to the convoluted offsets). With somewhere around 10% of operations in 3D Shaders involving swizzle this is a huge saving and reduces pressure on register files.
219
220 In SV given the percentage of operations that also involve initialisation to 0.0 or 1.0 into subvector elements the decision was made to include those:
221
222 swizzle = get_swizzle_immed() # 12 bits
223 for (s = 0; s < SUBVL; s++)
224 remap = (swizzle >> 3*s) & 0b111
225 if remap < 4:
226 sm = id*SUBVL + remap
227 ireg[rd+s] <= ireg[rs1+sm]
228 elif remap == 4:
229 ireg[rd+s] <= 0.0
230 elif remap == 5:
231 ireg[rd+s] <= 1.0
232
233 Note that a value of 6 (and 7) will leave the target subvector element untouched. This is equivalent to a predicate mask which is built-in, in immediate form, into the [[sv/mv.swizzle]] operation. mv.swizzle is rare in that it is one of the few instructions needed to be added that are never going to be part of a Scalar ISA. Even in High Performance Compute workloads it is unusual: it is only because SV is targetted at 3D and Video that it is being considered.
234
235 Some 3D GPU ISAs also allow for two-operand subvector swizzles. These are sufficiently unusual, and the immediate opcode space required so large, that the tradeoff balance was decided in SV to only add mv.swizzle.
236
237 # Twin Predication
238
239 Twin Predication is cool. Essentially it is a back-to-back VCOMPRESS-VEXPAND (a multiple sequentially ordered VINSERT). The compress part is covered by the source predicate and the expand part by the destination predicate. Of course, if either of those is all 1s then the ooeration degenerates *to* VCOMPRESS or VEXPAND, respectively.
240
241 function op(rd, rs):
242  ps = get_pred_val(FALSE, rs); # predication on src
243  pd = get_pred_val(FALSE, rd); # ... AND on dest
244  for (int i = 0, int j = 0; i < VL && j < VL;):
245 if (rs.isvec) while (!(ps & 1<<i)) i++;
246 if (rd.isvec) while (!(pd & 1<<j)) j++;
247 reg[rd+j] = SCALAR_OPERATION_ON(reg[rs+i])
248 if (int_csr[rs].isvec) i++;
249 if (int_csr[rd].isvec) j++; else break
250
251 Here's the interesting part: given the fact that SV is a "context" extension, the above pattern can be applied to a lot more than just MV, which is normally only what VCOMPRESS and VEXPAND do in traditional Vector ISAs: move registers. Twin Predication can be applied to `extsw` or `fcvt`, LD/ST operations and even `rlwinmi`. All of these are termed single-source, single-destination (LDST Address-generation, or AGEN, is a single source).
252
253 It also turns out that by using a single bit set in the source or destination, *all* the sequential ordered standard patterns of Vector ISAs are provided: VSPLAT, VSELECT, VINSERT, VCOMPRESS, VEXPAND.
254
255 The only one missing from the list here, because it is non-sequential, is VGATHER: moving registers by specifying a vector of register indices (`regs[rd] = regs[regs[rs]]` in a loop). This one is tricky because it typically does not exist in standard scalar ISAs. If it did it would be called [[sv/mv.x]]
256