(no commit message)
[libreriscv.git] / openpower / sv / branches.mdwn
1 # SVP64 Branch Conditional behaviour
2
3 **DRAFT STATUS**
4
5 Please note: SVP64 Branch instructions should be
6 considered completely separate and distinct from
7 standard scalar OpenPOWER-approved v3.0B branches.
8 **v3.0B branches are in no way impacted, altered,
9 changed or modified in any way, shape or form by
10 the SVP64 Vectorised Variants**.
11
12 Links
13
14 * <https://bugs.libre-soc.org/show_bug.cgi?id=664>
15 * <http://lists.libre-soc.org/pipermail/libre-soc-dev/2021-August/003416.html>
16 * [[openpower/isa/branch]]
17
18 Scalar 3.0B Branch Conditional operations, `bc`, `bctar` etc. test a
19 Condition Register. When doing so in a Vector Context, it is quite
20 reasonable and logical to test a *Vector* of CR Fields. In 3D Shader
21 binaries, which are inherently parallelised and predicated, testing all or
22 some results and branching based on multiple tests is extremely common,
23 and a fundamental part of Shader Compilers. Therefore, `sv.bc` and
24 other Vector-aware Branch Conditional instructions are worth including.
25
26 The `BI` field of Branch Conditional operations is five bits, in scalar
27 v3.0B this would select one bit of the 32 bit CR. In SVP64 there are
28 16 32 bit CRs, containing 128 4-bit CR Fields. Therefore, the 2 LSBs of
29 `BI` select the bit from the CR Field (EQ LT GT SO), and the top 3 bits
30 are extended to either scalar or vector and to select CR Fields 0..127
31 as specified in SVP64 [[sv/svp64/appendix]]
32
33 When considering an "array" of branches, there are two useful modes:
34
35 * Branch takes place on the first CR test to succeed
36 (a Great Big OR of all condition tests)
37 * Branch takes place only if **all** CR tests succeed:
38 a Great Big AND of all condition tests
39 (including those where the predicate is masked out
40 and the corresponding CR Field is considered to be
41 set to `SNZ`)
42
43 In SVP64 Horizontal-First Mode, the first failure in ALL mode (Great Big
44 AND) results in early exit: no more updates to CTR occur (if requested);
45 no branch occurs, and LR is not updated (if requested). Likewise for
46 non-ALL mode (Great Big Or) on first success early exit also occurs,
47 however this time with the Branch proceeding. In both cases the testing
48 of the Vector of CRs should be done in linear sequential order (or in
49 REMAP re-sequenced order): such that tests that are sequentially beyond
50 the exit point are *not* carried out. (*Note: is standard practice in
51 Programming languages to exit early from conditional tests*)
52
53 In Vertical-First Mode, the `ALL` bit should not be used. If set,
54 behaviour is `UNDEFINED`. (*The reason is that Vertical-First hints may
55 permit multiple elements up to hint length to be executed in parallel,
56 however the number is entirely up to implementors. Attempting to test
57 an arbitrary indeterminate number of Conditional tests is impossible
58 to define, and efforts to enforce such defined behaviour interfere with
59 Vertical-First mode parallel opportunistic behaviour.*)
60
61 In `svstep` mode, the whole CR Field, part of which is selected by `BI`
62 (top 3 bits), is updated based on incrementing srcstep and dststep, and
63 performing the same tests as [[sv/svstep]]. Following the step update,
64 which involved writing to the exact CR Field about to be tested, the
65 Branch Conditional instruction proceeds as normal (reading and testing
66 the CR bit just updated, if the relevant `BO` bit is set). Note that
67 the SVSTATE fields are still updated, and the CR field still updated,
68 even if the `BO` bits do not require CR testing.
69
70 Predication in both INT and CR modes may be applied to `sv.bc` and other
71 SVP64 Branch Conditional operations, exactly as they may be applied to
72 other SVP64 operations. When `sz` is zero, any masked-out Branch-element
73 operations are not executed, exactly like all other SVP64 operations.
74
75 However when `sz` is non-zero, this normally requests insertion of a zero
76 in place of the input data, when the relevant predicate mask bit is zero.
77 This would mean that a zero is inserted in place of `CR[BI+32]` for
78 testing against `BO`, which may not be desirable in all circumstances.
79 Therefore, an extra field is provided `SNZ`, which, if set, will insert
80 a **one** in place of a masked-out element instead of a zero.
81
82 (*Note: Both options are provided because it is useful to deliberately
83 cause the Branch-Conditional Vector testing to fail at a specific point,
84 controlled by the Predicate mask. This is particularly useful in `VLSET`
85 mode, which will truncate SVSTATE.VL at the point of the first failed
86 test.*)
87
88 SVP64 RM `MODE` for Branch Conditional:
89
90 | 0-1 | 2 | 3 4 | description |
91 | --- | --- |---------|-------------------------- |
92 | 00 | SNZ | ALL sz | normal mode |
93 | 01 | VLI | ALL sz | VLSET mode |
94 | 10 | SNZ | ALL sz | svstep mode |
95 | 11 | VLI | ALL sz | svstep VLSET mode, in Horizontal-First |
96 | 11 | VLI | SNZ sz | svstep VLSET mode, in Vertical-First |
97
98 Fields:
99
100 * **sz** if predication is enabled will put 4 copies of `SNZ` in place of
101 the src CR Field when the predicate bit is zero. otherwise the element
102 is ignored or skipped, depending on context.
103 * **ALL** when set, all branch conditional tests must pass in order for
104 the branch to succeed.
105 * **VLI** In VLSET mode, VL is set equal (truncated) to the first
106 branch which succeeds. If VLI (Vector Length Inclusive) is clear,
107 VL is truncated to *exclude* the current element, otherwise it is
108 included. SVSTATE.MVL is not changed.
109
110 svstep mode will run an increment of SVSTATE srcstep and dststep
111 (which is still useful in Horizontal First Mode). Unlike `svstep.`
112 however which updates only CR0 with the testing of REMAP loop progress,
113 the CR Field is taken from the branch `BI` field, and updated prior to
114 proceeding to each element branch conditional testing.
115
116 Note that, interestingly, due to the useful side-effects of `VLSET` mode
117 and `svstep` mode it is actually useful to use Branch Conditional even
118 to perform no actual branch operation, i.e to point to the instruction
119 after the branch.
120
121 In particular, svstep mode is still useful for Horizontal-First Mode
122 particularly in combination with REMAP. All "loop end" conditions
123 will be tested on a per-element basis and placed into a Vector of CRs
124 starting from the point specified by the Branch `BI` field. This Vector
125 of CR Fields may then be subsequently used as a Predicate Mask, and,
126 furthermore, if VLSET mode was requested, VL will have been set to the
127 length of one of the loop endpoints, again as specified by the bit from
128 the Branch `BI` field.
129
130 Also, the unconditional bit `BO[0]` is still relevant when Predication
131 is applied to the Branch because in `ALL` mode all nonmasked bits have
132 to be tested. Even when svstep mode or VLSET mode are not used, CTR
133 may still be decremented by the total number of nonmasked elements.
134 In short, Vectorised Branch becomes an extremely powerful tool.
135
136 Available options to combine:
137
138 * `BO[0]` to make an unconditional branch would seem irrelevant if
139 it were not for predication and for side-effects.
140 * `BO[1]` to select whether the CR bit being tested is zero or nonzero
141 * `R30` and `~R30` and other predicate mask options including CR and
142 inverted CR bit testing
143 * `sz` and `SNZ` to insert either zeros or ones in place of masked-out
144 predicate bits
145 * `ALL` or `ANY` behaviour corresponding to `AND` of all tests and
146 `OR` of all tests, respectively.
147
148 In addition to the above, it is necessary to select whether, in `svstep`
149 mode, the Vector CR Field is to be overwritten or not: in some cases
150 it is useful to know but in others all that is needed is the branch itself.
151 In the case of `sv.bc` there is no additional bitspace, so on the
152 basis that it is rarely used, the `AA`
153 field is re-interpreted instead to be `Rc`. For `sv.bclr`, there is free
154 bitspace and so bit 16 has been chosen as `Rc`.
155
156 **These interpretations are only available for sv.bc, they are NOT
157 available for Power ISA v3.0B** i.e. only when embedded in an SVP64
158 Prefix Context do these and all other parts of this specification
159 apply.
160
161 Form: B-Form (see [[isatables/fields.text]])
162
163 | 0.5|6.10|11.15|16..29| 30 |31| name |
164 | -- | -- | --- | ---- | -- |--| ------- |
165 |16 | BO | BI | BD | Rc |LK| sv.bc |
166
167 Form: XL-Form (see [[isatables/fields.text]])
168
169 | 0.5|6.10|11.15|16|17.18|19.20|21..30|31| name |
170 | -- | -- | --- |--|---- |-----|------|--| ------- |
171 |19 | BO | BI |Rc| // | BH | 19 |LK| sv.bclr |
172
173 Pseudocode for Rc in sv.bc
174
175 ```
176 # Use bit 30 as Rc, disable AA
177 Rc = AA
178 AA = 0
179 ```
180
181 Pseudocode for Rc in sv.bclr
182
183 ```
184 # use bit 16 of opcode as Rc
185 Rc = instr[16]
186 ```
187
188 Pseudocode for Horizontal-First Mode:
189
190 ```
191 cond_ok = not SVRMmode.ALL
192 for srcstep in range(VL):
193 # select predicate bit or zero/one
194 if predicate[srcstep]:
195 # get SVP64 extended CR field 0..127
196 SVCRf = SVP64EXTRA(BI>>2)
197 if svstep_mode then
198 new_srcstep, CRbits = SVSTATE_NEXT(srcstep)
199 else
200 CRbits = CR{SVCRf}
201 if Rc = 1 then # CR0 Vectorised
202 CR{0+srcstep} = CRbits
203 testbit = CRbits[BI & 0b11]
204 # testbit = CR[BI+32+srcstep*4]
205 else if not SVRMmode.sz:
206 continue
207 else
208 testbit = SVRMmode.SNZ
209 # actual element test here
210 el_cond_ok <- BO[0] | ¬(testbit ^ BO[1])
211 # merge in the test
212 if SVRMmode.ALL:
213 cond_ok &= el_cond_ok
214 else
215 cond_ok |= el_cond_ok
216 # test for VL to be set (and exit)
217 if ~el_cond_ok and VLSET
218 if SVRMmode.VLI
219 SVSTATE.VL = srcstep+1
220 else
221 SVSTATE.VL = srcstep
222 break
223 # early exit?
224 if SVRMmode.ALL:
225 if ~el_cond_ok:
226 break
227 else
228 if el_cond_ok:
229 break
230 if svstep_mode then
231 SVSTATE.srcstep = new_srcstep
232 ```
233
234 Pseudocode for Vertical-First Mode:
235
236 ```
237 # get SVP64 extended CR field 0..127
238 SVCRf = SVP64EXTRA(BI>>2)
239 if svstep_mode then
240 new_srcstep, CRbits = SVSTATE_NEXT(srcstep)
241 else
242 CRbits = CR{SVCRf}
243 # select predicate bit or zero/one
244 if predicate[srcstep]:
245 if Rc = 1 then # CR0 vectorised
246 CR{0+srcstep} = CRbits
247 testbit = CRbits[BI & 0b11]
248 else if not SVRMmode.sz:
249 SVSTATE.srcstep = new_srcstep
250 exit # no branch testing
251 else
252 testbit = SVRMmode.SNZ
253 # actual element test here
254 cond_ok <- BO[0] | ¬(testbit ^ BO[1])
255 # test for VL to be set (and exit)
256 if ~cond_ok and VLSET
257 if SVRMmode.VLI
258 SVSTATE.VL = new_srcstep+1
259 else
260 SVSTATE.VL = new_srcstep
261 if svstep_mode then
262 SVSTATE.srcstep = new_srcstep
263 ```
264
265 # Example Shader code
266
267 ```
268 while(a > 2) {
269 if(b < 5)
270 f();
271 else
272 g();
273 h();
274 }
275 ```
276
277 which compiles to something like:
278
279 ```
280 vec<i32> a, b;
281 // ...
282 pred loop_pred = a > 2;
283 while(loop_pred.any()) {
284 pred if_pred = loop_pred & (b < 5);
285 if(if_pred.any()) {
286 f(if_pred);
287 }
288 label1:
289 pred else_pred = loop_pred & ~if_pred;
290 if(else_pred.any()) {
291 g(else_pred);
292 }
293 h(loop_pred);
294 }
295 ```
296
297 which will end up as:
298
299 ```
300 sv.cmpi CR60.v a.v, 2 # vector compare a into CR60 vector
301 sv.crweird r30, CR60.GT # transfer GT vector to r30
302 while_loop:
303 sv.cmpi CR80.v, b.v, 5 # vector compare b into CR64 Vector
304 sv.bc/m=r30/~ALL/sz CR80.v.LT skip_f # skip when none
305 # only calculate loop_pred & pred_b because needed in f()
306 sv.crand CR80.v.SO, CR60.v.GT, CR80.V.LT # if = loop & pred_b
307 f(CR80.v.SO)
308 skip_f:
309 # illustrate inversion of pred_b. invert r30, test ALL
310 # rather than SOME, but masked-out zero test would FAIL,
311 # therefore masked-out instead is tested against 1 not 0
312 sv.bc/m=~r30/ALL/SNZ CR80.v.LT skip_g
313 # else = loop & ~pred_b, need this because used in g()
314 sv.crternari(A&~B) CR80.v.SO, CR60.v.GT, CR80.V.LT
315 g(CR80.v.SO)
316 skip_g:
317 # conditionally call h(r30) if any loop pred set
318 sv.bclr/m=r30/~ALL/sz BO[1]=1 h()
319 sv.bc/m=r30/~ALL/sz BO[1]=1 while_loop
320 ```