(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 Pseudocode for Rc in sv.bc
162
163 ```
164 # Use bit 30 as Rc, disable AA
165 Rc = AA
166 AA = 0
167 ```
168
169 Pseudocode for Rc in sv.bclr
170
171 ```
172 # use bit 16 of opcode as Rc
173 Rc = instr[16]
174 ```
175
176 Pseudocode for Horizontal-First Mode:
177
178 ```
179 cond_ok = not SVRMmode.ALL
180 for srcstep in range(VL):
181 # select predicate bit or zero/one
182 if predicate[srcstep]:
183 # get SVP64 extended CR field 0..127
184 SVCRf = SVP64EXTRA(BI>>2)
185 if svstep_mode then
186 new_srcstep, CRbits = SVSTATE_NEXT(srcstep)
187 else
188 CRbits = CR{SVCRf}
189 if Rc = 1 then # CR0 Vectorised
190 CR{0+srcstep} = CRbits
191 testbit = CRbits[BI & 0b11]
192 # testbit = CR[BI+32+srcstep*4]
193 else if not SVRMmode.sz:
194 continue
195 else
196 testbit = SVRMmode.SNZ
197 # actual element test here
198 el_cond_ok <- BO[0] | ¬(testbit ^ BO[1])
199 # merge in the test
200 if SVRMmode.ALL:
201 cond_ok &= el_cond_ok
202 else
203 cond_ok |= el_cond_ok
204 # test for VL to be set (and exit)
205 if ~el_cond_ok and VLSET
206 if SVRMmode.VLI
207 SVSTATE.VL = srcstep+1
208 else
209 SVSTATE.VL = srcstep
210 break
211 # early exit?
212 if SVRMmode.ALL:
213 if ~el_cond_ok:
214 break
215 else
216 if el_cond_ok:
217 break
218 if svstep_mode then
219 SVSTATE.srcstep = new_srcstep
220 ```
221
222 Pseudocode for Vertical-First Mode:
223
224 ```
225 # get SVP64 extended CR field 0..127
226 SVCRf = SVP64EXTRA(BI>>2)
227 if svstep_mode then
228 new_srcstep, CRbits = SVSTATE_NEXT(srcstep)
229 else
230 CRbits = CR{SVCRf}
231 # select predicate bit or zero/one
232 if predicate[srcstep]:
233 if Rc = 1 then # CR0 vectorised
234 CR{0+srcstep} = CRbits
235 testbit = CRbits[BI & 0b11]
236 else if not SVRMmode.sz:
237 SVSTATE.srcstep = new_srcstep
238 exit # no branch testing
239 else
240 testbit = SVRMmode.SNZ
241 # actual element test here
242 cond_ok <- BO[0] | ¬(testbit ^ BO[1])
243 # test for VL to be set (and exit)
244 if ~cond_ok and VLSET
245 if SVRMmode.VLI
246 SVSTATE.VL = new_srcstep+1
247 else
248 SVSTATE.VL = new_srcstep
249 if svstep_mode then
250 SVSTATE.srcstep = new_srcstep
251 ```
252
253 # Example Shader code
254
255 ```
256 while(a > 2) {
257 if(b < 5)
258 f();
259 else
260 g();
261 h();
262 }
263 ```
264
265 which compiles to something like:
266
267 ```
268 vec<i32> a, b;
269 // ...
270 pred loop_pred = a > 2;
271 while(loop_pred.any()) {
272 pred if_pred = loop_pred & (b < 5);
273 if(if_pred.any()) {
274 f(if_pred);
275 }
276 label1:
277 pred else_pred = loop_pred & ~if_pred;
278 if(else_pred.any()) {
279 g(else_pred);
280 }
281 h(loop_pred);
282 }
283 ```
284
285 which will end up as:
286
287 ```
288 sv.cmpi CR60.v a.v, 2 # vector compare a into CR60 vector
289 sv.crweird r30, CR60.GT # transfer GT vector to r30
290 while_loop:
291 sv.cmpi CR80.v, b.v, 5 # vector compare b into CR64 Vector
292 sv.bc/m=r30/~ALL/sz CR80.v.LT skip_f # skip when none
293 # only calculate loop_pred & pred_b because needed in f()
294 sv.crand CR80.v.SO, CR60.v.GT, CR80.V.LT # if = loop & pred_b
295 f(CR80.v.SO)
296 skip_f:
297 # illustrate inversion of pred_b. invert r30, test ALL
298 # rather than SOME, but masked-out zero test would FAIL,
299 # therefore masked-out instead is tested against 1 not 0
300 sv.bc/m=~r30/ALL/SNZ CR80.v.LT skip_g
301 # else = loop & ~pred_b, need this because used in g()
302 sv.crternari(A&~B) CR80.v.SO, CR60.v.GT, CR80.V.LT
303 g(CR80.v.SO)
304 skip_g:
305 # conditionally call h(r30) if any loop pred set
306 sv.bclr/m=r30/~ALL/sz BO[1]=1 h()
307 sv.bc/m=r30/~ALL/sz BO[1]=1 while_loop
308 ```