3 * <https://bugs.libre-soc.org/show_bug.cgi?id=213>
5 # svstep: Vertical-First Stepping and status reporting
9 * svstep RT,SVi,vf (Rc=0)
10 * svstep. RT,SVi,vf (Rc=1)
12 | 0-5|6-10|11.15|16..22| 23-25 | 26-30 |31| Form |
13 |----|----|-----|------|----------|-------|--|--------- |
14 |PO | RT | / | SVi | / / vf | XO |Rc| SVL-Form |
19 if SVi[3:4] = 0b11 then
20 # store pack and unpack in SVSTATE
23 RT <- [0]*62 || SVSTATE[53:54]
25 # Vertical-First explicit stepping.
26 step <- SVSTATE_NEXT(SVi, vf)
30 Special Registers Altered:
36 svstep may be used to enquire about the REMAP Schedule and it may be
37 used to alter Vectorization State. When `vf=1` then stepping occurs.
38 When `vf=0` the enquiry is performed without altering internal state.
39 If `SVi=0, Rc=0, vf=0` the instruction is a `nop`.
41 The following Modes exist:
43 * `SVi=0`: appropriately step srcstep, dststep, subsrcstep and subdststep
44 to the next element, taking pack and unpack into consideration.
45 * When `SVi` is 1-4 the REMAP Schedule for a given SVSHAPE may be
46 returned in `RT`. SVi=1 selects SVSHAPE0 current state,
47 through to SVi=4 selects SVSHAPE3.
48 * When `SVi` is 5, `SVSTATE.srcstep` is returned.
49 * When `SVi` is 6, `SVSTATE.dststep` is returned.
50 * When `SVi` is 7, `SVSTATE.ssubstep` is returned.
51 * When `SVi` is 8, `SVSTATE.dsubstep` is returned.
52 * When `SVi` is 0b1100 pack/unpack in SVSTATE is cleared
53 * When `SVi` is 0b1101 pack in SVSTATE is set, unpack is cleared
54 * When `SVi` is 0b1110 unpack in SVSTATE is set, pack is cleared
55 * When `SVi` is 0b1111 pack/unpack in SVSTATE are set
57 As this is a Single-Predicated (1P) instruction, predication may be applied
58 to skip (or zero) elements.
60 * Vertical-First Mode will return the requested index
61 (and move to the next state if `vf=1`)
62 * Horizontal-First Mode can be used to return all indices,
63 i.e. walks through all possible states.
65 **Vectorization of svstep itself**
67 As a 32-bit instruction, `svstep` may be itself be Vector-Prefixed, as
68 `sv.svstep`. This will work perfectly well in Horizontal-First
69 as it will in Vertical-First Mode although there are caveats for
70 the Deterministic use of looping with Sub-Vectors in Vertical-First mode.
72 Example: to obtain the full set of possible computed element
73 indices use `sv.svstep *RT,SVi,1` which will store all computed element
74 indices, starting from RT. If Rc=1 then a co-result Vector of CR Fields
75 will also be returned, comprising the "loop end-points" of each of the inner
76 loops when either Matrix Mode or DCT/FFT is set. In other words,
77 for example, when the `xdim` inner loop reaches the end and on the next
78 iteration it will begin again at zero, the CR Field `EQ` will be set.
79 With a maximum of three loops within both Matrix and DCT/FFT Modes,
80 the CR Field's EQ bit will be set at the end of the first inner loop,
81 the LE bit for the second, the GT bit for the outermost loop and the
82 SO bit set on the very last element, when all loops reach their maximum
85 *Programmer's note: VL in some situations, particularly larger
86 Matrices (5x7x3 will set MAXVL=105), will cause `sv.svstep` to return a
87 considerable number of values. Under such circumstances `sv.svstep/ew=8`
90 *Programmer's note: having conveniently obtained a pre-computed Schedule
91 with `sv.svstep`, it may then be used as the input to Indexed REMAP
92 Mode to achieve the exact same Schedule. It is evident however that
93 before use some of the Indices may be arbitrarily altered as desired.
94 `sv.svstep` helps the programmer avoid having to manually recreate
95 Indices for certain types of common Loop patterns. In its simplest form,
96 without REMAP (SVi=5 or SVi=6), is equivalent to the `iota` instruction
97 found in other Vector ISAs*
99 **Vertical First Mode**
101 Vertical First is effectively like an implicit single bit predicate
102 applied to every SVP64 instruction. **ONLY** one element in each SVP64
103 Vector instruction is executed; srcstep and dststep do **not** increment
104 automatically on completion of one instruction, and the Program Counter
105 progresses **immediately** to the next instruction just as it would for
106 any standard scalar v3.0B instruction.
108 A mode of srcstep (SVi=0) is called which can move srcstep and dststep
109 on to the next element, still respecting predicate masks.
111 In other words, where normal SVP64 Vectorization acts "horizontally"
112 by looping first through 0 to VL-1 and only then moving the PC to the
113 next instruction, Vertical-First moves the PC onwards (vertically)
114 through multiple instructions **with the same srcstep and dststep**,
115 then an explict instruction used to advance srcstep/dststep. An outer
116 loop is expected to be used (branch instruction) which completes a series
117 of Vector operations.
119 Testing any end condition of any loop of any REMAP state allows branches
120 to be used to create loops.
122 *Programmer's note: when Predicate Non-Zeroing is used this indicates to
123 the underlying hardware that any masked-out element must be skipped.
124 *This includes in Vertical-First Mode*, and programmers should be
125 keenly aware that srcstep or dststep or both *may* jump by more than
126 one as a result, because the actual request under these circumstances
127 was to execute on the first available next *non-masked-out* element.
128 It should be evident that it is the `sv.svstep` instruction that must
129 be Predicated in order for the **entire** loop to use the Predicate
130 correctly, and it is strongly recommended for all instructions within
131 the same Vertical-First Loop to utilise the exact same Predicate Mask(s).*
133 Programmers should be aware that VL, srcstep and dststep and the SUBVL
134 substeps are global in nature. Nested looping with different schedules
135 is perfectly possible, as is calling of functions, however SVSTATE
136 (and any associated SVSHAPEs if REMAP is being used) should obviously
137 be stored on the stack in order to achieve this benefit not normally
138 found in Vector ISAs.
140 **Use of svstep with Vertical-First sub-vectors**
142 Incrementing and iteration through subvector state ssubstep and dsubstep is
143 possible with `sv.svstep/vecN` where as expected N may be 2/3/4. However it is necessary
144 to use the exact same Sub-Vector qualifier on any Prefixed
145 instructions, within any given Vertical-First loop: `vec2/3/4` is **not**
146 automatically applied to all instructions, it must be explicitly applied on
147 a per-instruction basis. Also valid
148 is not specifying a Sub-vector
149 qualifier at all, but it is critically important to note that
150 operations will be repeated. For example if `sv.svstep/vec2`
151 is not used on `sv.addi` then each Vector element operation is
152 repeated twice. The reason is that whilst svstep will be
153 iterating through both the SUBVL and VL loops, the addi instruction
154 only uses `srcstep` and `dststep` (not ssubstep or dsubstep) Illustrated below:
158 for step in range(VL):
159 for substep in range(SUBVL=2):
161 for i, j in offset():
162 vec2_offs = i * SUBVL + j # calculate vec2 offset
163 addi RT+i, RA+i, 1 # but sv.addi is not vec2!
164 muli/vec2 RT+vec2_offs, RA+vec2_offs, 2 # this is
167 Actual assembler would be:
172 sv.addi *RT, *RA, 1 # no vec2
173 sv.muli/vec2 *RT, *RA, 2 # vec2
174 sv.svstep/vec2 # must match the muli
175 sv.bc CTRmode, loop # subtracts VL from CTR
178 This illustrates the correct but seemingly-anomalous behaviour: `sv.svstep/vec2`
179 is being requested to update `SVSTATE` to follow a vec2 loop construct. The anomalous
180 `sv.addi` is not prohibited as it may in fact be desirable to execute operations twice,
181 or to re-load data that was overwritten, and many other possibilities.
191 Note that `srcstep` and `ssubstep` are not the absolute final Element
192 (and Sub-Element) offsets. `srcstep` still has to go through individual
193 `REMAP` translation before becoming a per-operand (RA, RB, RC, RT, RS)
194 Element-level Source offset.
196 Note also critically that `PACK` mode simply inverts the outer/order
197 loops making SUBVL the outer loop and VL the inner.
200 # source-stepping iterator
201 subvl = SVSTATE.subvl
204 unpack = SVSTATE.unpack
205 ssubstep = SVSTATE.ssubstep
206 end_ssub = ssubstep == subvl
207 end_src = SVSTATE.srcstep == vl-1
209 srcstep = SVSTATE.srcstep
211 # sz - from RM.MODE, source-zeroing
212 # srcmask - from RM.MODE, the source predicate
214 # pack advances subvl in *outer* loop
216 assert srcstep <= vl-1
217 end_src = srcstep == vl-1
222 SVSTATE.ssubstep += 1
226 srcstep += 1 # advance srcstep
229 if ((1 << srcstep) & srcmask) != 0:
232 # advance subvl in *inner* loop
235 assert srcstep <= vl-1
236 end_src = srcstep == vl-1
237 if end_src: # end-point
245 if ((1 << srcstep) & srcmask) != 0:
248 log(" sskip", bin(srcmask), bin(1 << srcstep))
249 SVSTATE.ssubstep = 0b00 # reset
252 SVSTATE.ssubstep += 1
254 SVSTATE.srcstep = srcstep
263 Note that `dststep` and `dsubstep` are not the absolute final Element
264 (and Sub-Element) offsets. `dststep` still has to go through individual
265 `REMAP` translation before becoming a per-operand (RT, RS/EA) destination
266 Element-level offset, and `dsubstep` may also go through `(f)mv.swizzle`
269 Note also critically that `UNPACK` mode simply inverts the outer/order
270 loops making SUBVL the outer loop and VL the inner.
275 subvl = SVSTATE.subvl
276 unpack = SVSTATE.unpack
277 dsubstep = SVSTATE.dsubstep
278 end_dsub = dsubstep == subvl
279 dststep = SVSTATE.dststep
280 end_dst = dststep == vl-1
282 # dz - from RM.MODE, destination-zeroing
283 # dstmask - from RM.MODE, the destination predicate
285 # unpack advances subvl in *outer* loop
287 assert dststep <= vl-1
288 end_dst = dststep == vl-1
293 SVSTATE.dsubstep += 1
297 dststep += 1 # advance dststep
300 if ((1 << dststep) & dstmask) != 0:
303 # advance subvl in *inner* loop
306 assert dststep <= vl-1
307 end_dst = dststep == vl-1
308 if end_dst: # end-point
316 if ((1 << dststep) & dstmask) != 0:
318 SVSTATE.dsubstep = 0b00 # reset
321 SVSTATE.dsubstep += 1
323 SVSTATE.dststep = dststep
333 if SVi = 1 then return REMAP SVSHAPE0 current offset
334 if SVi = 2 then return REMAP SVSHAPE1 current offset
335 if SVi = 3 then return REMAP SVSHAPE2 current offset
336 if SVi = 4 then return REMAP SVSHAPE3 current offset
337 if SVi = 5 then return SVSTATE.srcstep # VL source step
338 if SVi = 6 then return SVSTATE.dststep # VL dest step
339 if SVi = 7 then return SVSTATE.ssubstep # SUBVL source step
340 if SVi = 8 then return SVSTATE.dsubstep # SUBVL dest step
342 # SVi=0, explicit iteration requezted
350 Both Vertical-First and Horizontal-First may use this algorithm to
351 determine if the "end-of-looping" (end of Sub-Program-Counter) has
352 been reached. Horizontal-First Mode will immediately move to the
353 next instruction, where `svstep.` will set `CR0.EQ` to 1.
356 # tells if this is the last possible element.
357 subvl = SVSTATE.subvl
359 end_ssub = SVSTATE.ssubstep == subvl
360 end_dsub = SVSTATE.dsubstep == subvl
361 if SVSTATE.srcstep == vl-1 and end_ssub:
363 if SVSTATE.dststep == vl-1 and end_dsub: