1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/*
* Copyright © 2018 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "nir.h"
#include "nir_builder.h"
static bool
assert_ssa_def_is_not_1bit(nir_ssa_def *def, UNUSED void *unused)
{
assert(def->bit_size > 1);
return true;
}
static bool
rewrite_1bit_ssa_def_to_32bit(nir_ssa_def *def, void *_progress)
{
bool *progress = _progress;
if (def->bit_size == 1) {
def->bit_size = 32;
*progress = true;
}
return true;
}
static uint32_t
get_bool_convert_opcode(uint32_t dst_bit_size)
{
switch (dst_bit_size) {
case 32: return nir_op_i2i32;
case 16: return nir_op_i2i16;
case 8: return nir_op_i2i8;
default:
unreachable("invalid boolean bit-size");
}
}
static void
make_sources_canonical(nir_builder *b, nir_alu_instr *alu, uint32_t start_idx)
{
/* TODO: for now we take the bit-size of the first source as the canonical
* form but we could try to be smarter.
*/
const nir_op_info *op_info = &nir_op_infos[alu->op];
uint32_t bit_size = nir_src_bit_size(alu->src[start_idx].src);
for (uint32_t i = start_idx + 1; i < op_info->num_inputs; i++) {
if (nir_src_bit_size(alu->src[i].src) != bit_size) {
b->cursor = nir_before_instr(&alu->instr);
nir_op convert_op = get_bool_convert_opcode(bit_size);
nir_ssa_def *new_src =
nir_build_alu(b, convert_op, alu->src[i].src.ssa, NULL, NULL, NULL);
/* Retain the write mask and swizzle of the original instruction so
* that we don’t unnecessarily create a vectorized instruction.
*/
nir_alu_instr *conv_instr =
nir_instr_as_alu(nir_builder_last_instr(b));
conv_instr->dest.write_mask = alu->dest.write_mask;
conv_instr->dest.dest.ssa.num_components =
alu->dest.dest.ssa.num_components;
memcpy(conv_instr->src[0].swizzle,
alu->src[i].swizzle,
sizeof(conv_instr->src[0].swizzle));
nir_instr_rewrite_src(&alu->instr,
&alu->src[i].src, nir_src_for_ssa(new_src));
/* The swizzle will have been handled by the conversion instruction
* so we can reset it back to the default
*/
for (unsigned j = 0; j < NIR_MAX_VEC_COMPONENTS; j++)
alu->src[i].swizzle[j] = j;
}
}
}
static bool
lower_alu_instr(nir_builder *b, nir_alu_instr *alu)
{
const nir_op_info *op_info = &nir_op_infos[alu->op];
/* For operations that can take multiple boolean sources we need to ensure
* that all booleans have the same bit-size
*/
switch (alu->op) {
case nir_op_mov:
case nir_op_vec2:
case nir_op_vec3:
case nir_op_vec4:
case nir_op_vec5:
case nir_op_vec8:
case nir_op_vec16:
case nir_op_inot:
case nir_op_iand:
case nir_op_ior:
case nir_op_ixor:
if (nir_dest_bit_size(alu->dest.dest) > 1)
return false; /* Not a boolean instruction */
FALLTHROUGH;
case nir_op_ball_fequal2:
case nir_op_ball_fequal3:
case nir_op_ball_fequal4:
case nir_op_bany_fnequal2:
case nir_op_bany_fnequal3:
case nir_op_bany_fnequal4:
case nir_op_ball_iequal2:
case nir_op_ball_iequal3:
case nir_op_ball_iequal4:
case nir_op_bany_inequal2:
case nir_op_bany_inequal3:
case nir_op_bany_inequal4:
case nir_op_ieq:
case nir_op_ine:
make_sources_canonical(b, alu, 0);
break;
case nir_op_bcsel:
/* bcsel may be choosing between boolean sources too */
if (nir_dest_bit_size(alu->dest.dest) == 1)
make_sources_canonical(b, alu, 1);
break;
default:
break;
}
/* Now that we have a canonical boolean bit-size, go on and rewrite the
* instruction to match the canonical bit-size.
*/
uint32_t bit_size = nir_src_bit_size(alu->src[0].src);
assert(bit_size > 1);
nir_op opcode = alu->op;
switch (opcode) {
case nir_op_mov:
case nir_op_vec2:
case nir_op_vec3:
case nir_op_vec4:
case nir_op_vec5:
case nir_op_vec8:
case nir_op_vec16:
case nir_op_inot:
case nir_op_iand:
case nir_op_ior:
case nir_op_ixor:
/* Nothing to do here, we do not specialize these opcodes by bit-size */
break;
case nir_op_b2b1:
/* Since the canonical bit size is the size of the src, it's a no-op */
opcode = nir_op_mov;
break;
case nir_op_b2b32:
/* For up-converting booleans, sign-extend */
opcode = nir_op_i2i32;
break;
case nir_op_flt:
opcode = bit_size == 8 ? nir_op_flt8 :
bit_size == 16 ? nir_op_flt16 : nir_op_flt32;
break;
case nir_op_fge:
opcode = bit_size == 8 ? nir_op_fge8 :
bit_size == 16 ? nir_op_fge16 : nir_op_fge32;
break;
case nir_op_feq:
opcode = bit_size == 8 ? nir_op_feq8 :
bit_size == 16 ? nir_op_feq16 : nir_op_feq32;
break;
case nir_op_fneu:
opcode = bit_size == 8 ? nir_op_fneu8 :
bit_size == 16 ? nir_op_fneu16 : nir_op_fneu32;
break;
case nir_op_ilt:
opcode = bit_size == 8 ? nir_op_ilt8 :
bit_size == 16 ? nir_op_ilt16 : nir_op_ilt32;
break;
case nir_op_ige:
opcode = bit_size == 8 ? nir_op_ige8 :
bit_size == 16 ? nir_op_ige16 : nir_op_ige32;
break;
case nir_op_ieq:
opcode = bit_size == 8 ? nir_op_ieq8 :
bit_size == 16 ? nir_op_ieq16 : nir_op_ieq32;
break;
case nir_op_ine:
opcode = bit_size == 8 ? nir_op_ine8 :
bit_size == 16 ? nir_op_ine16 : nir_op_ine32;
break;
case nir_op_ult:
opcode = bit_size == 8 ? nir_op_ult8 :
bit_size == 16 ? nir_op_ult16 : nir_op_ult32;
break;
case nir_op_uge:
opcode = bit_size == 8 ? nir_op_uge8 :
bit_size == 16 ? nir_op_uge16 : nir_op_uge32;
break;
case nir_op_ball_fequal2:
opcode = bit_size == 8 ? nir_op_b8all_fequal2 :
bit_size == 16 ? nir_op_b16all_fequal2 :
nir_op_b32all_fequal2;
break;
case nir_op_ball_fequal3:
opcode = bit_size == 8 ? nir_op_b8all_fequal3 :
bit_size == 16 ? nir_op_b16all_fequal3 :
nir_op_b32all_fequal3;
break;
case nir_op_ball_fequal4:
opcode = bit_size == 8 ? nir_op_b8all_fequal4 :
bit_size == 16 ? nir_op_b16all_fequal4 :
nir_op_b32all_fequal4;
break;
case nir_op_bany_fnequal2:
opcode = bit_size == 8 ? nir_op_b8any_fnequal2 :
bit_size == 16 ? nir_op_b16any_fnequal2 :
nir_op_b32any_fnequal2;
break;
case nir_op_bany_fnequal3:
opcode = bit_size == 8 ? nir_op_b8any_fnequal3 :
bit_size == 16 ? nir_op_b16any_fnequal3 :
nir_op_b32any_fnequal3;
break;
case nir_op_bany_fnequal4:
opcode = bit_size == 8 ? nir_op_b8any_fnequal4 :
bit_size == 16 ? nir_op_b16any_fnequal4 :
nir_op_b32any_fnequal4;
break;
case nir_op_ball_iequal2:
opcode = bit_size == 8 ? nir_op_b8all_iequal2 :
bit_size == 16 ? nir_op_b16all_iequal2 :
nir_op_b32all_iequal2;
break;
case nir_op_ball_iequal3:
opcode = bit_size == 8 ? nir_op_b8all_iequal3 :
bit_size == 16 ? nir_op_b16all_iequal3 :
nir_op_b32all_iequal3;
break;
case nir_op_ball_iequal4:
opcode = bit_size == 8 ? nir_op_b8all_iequal4 :
bit_size == 16 ? nir_op_b16all_iequal4 :
nir_op_b32all_iequal4;
break;
case nir_op_bany_inequal2:
opcode = bit_size == 8 ? nir_op_b8any_inequal2 :
bit_size == 16 ? nir_op_b16any_inequal2 :
nir_op_b32any_inequal2;
break;
case nir_op_bany_inequal3:
opcode = bit_size == 8 ? nir_op_b8any_inequal3 :
bit_size == 16 ? nir_op_b16any_inequal3 :
nir_op_b32any_inequal3;
break;
case nir_op_bany_inequal4:
opcode = bit_size == 8 ? nir_op_b8any_inequal4 :
bit_size == 16 ? nir_op_b16any_inequal4 :
nir_op_b32any_inequal4;
break;
case nir_op_bcsel:
opcode = bit_size == 8 ? nir_op_b8csel :
bit_size == 16 ? nir_op_b16csel : nir_op_b32csel;
/* The destination of the selection may have a different bit-size from
* the bcsel condition.
*/
bit_size = nir_src_bit_size(alu->src[1].src);
break;
default:
assert(alu->dest.dest.ssa.bit_size > 1);
for (unsigned i = 0; i < op_info->num_inputs; i++)
assert(alu->src[i].src.ssa->bit_size > 1);
return false;
}
alu->op = opcode;
if (alu->dest.dest.ssa.bit_size == 1)
alu->dest.dest.ssa.bit_size = bit_size;
return true;
}
static bool
lower_load_const_instr(nir_load_const_instr *load)
{
bool progress = false;
if (load->def.bit_size > 1)
return progress;
/* TODO: It is not clear if there is any case in which we can ever hit
* this path, so for now we just provide a 32-bit default.
*
* TODO2: after some changed on nir_const_value and other on upstream, we
* removed the initialization of a general value like this:
* nir_const_value value = load->value
*
* to initialize per value component. Need to confirm if that is correct,
* but look at the TOO before.
*/
for (unsigned i = 0; i < load->def.num_components; i++) {
load->value[i].u32 = load->value[i].b ? NIR_TRUE : NIR_FALSE;
load->def.bit_size = 32;
progress = true;
}
return progress;
}
static bool
lower_phi_instr(nir_builder *b, nir_phi_instr *phi)
{
if (nir_dest_bit_size(phi->dest) != 1)
return false;
/* Ensure all phi sources have a canonical bit-size. We choose the
* bit-size of the first phi source as the canonical form.
*
* TODO: maybe we can be smarter about how we choose the canonical form.
*/
uint32_t dst_bit_size = 0;
nir_foreach_phi_src(phi_src, phi) {
uint32_t src_bit_size = nir_src_bit_size(phi_src->src);
if (dst_bit_size == 0) {
dst_bit_size = src_bit_size;
} else if (src_bit_size != dst_bit_size) {
assert(phi_src->src.is_ssa);
b->cursor = nir_before_src(&phi_src->src);
nir_op convert_op = get_bool_convert_opcode(dst_bit_size);
nir_ssa_def *new_src =
nir_build_alu(b, convert_op, phi_src->src.ssa, NULL, NULL, NULL);
nir_instr_rewrite_src(&phi->instr, &phi_src->src,
nir_src_for_ssa(new_src));
}
}
phi->dest.ssa.bit_size = dst_bit_size;
return true;
}
static bool
lower_tex_instr(nir_tex_instr *tex)
{
bool progress = false;
rewrite_1bit_ssa_def_to_32bit(&tex->dest.ssa, &progress);
if (tex->dest_type == nir_type_bool1) {
tex->dest_type = nir_type_bool32;
progress = true;
}
return progress;
}
static bool
nir_lower_bool_to_bitsize_instr(nir_builder *b,
nir_instr *instr,
UNUSED void *cb_data)
{
switch (instr->type) {
case nir_instr_type_alu:
return lower_alu_instr(b, nir_instr_as_alu(instr));
case nir_instr_type_load_const:
return lower_load_const_instr(nir_instr_as_load_const(instr));
case nir_instr_type_phi:
return lower_phi_instr(b, nir_instr_as_phi(instr));
case nir_instr_type_ssa_undef:
case nir_instr_type_intrinsic: {
bool progress = false;
nir_foreach_ssa_def(instr, rewrite_1bit_ssa_def_to_32bit, &progress);
return progress;
}
case nir_instr_type_tex:
return lower_tex_instr(nir_instr_as_tex(instr));
default:
nir_foreach_ssa_def(instr, assert_ssa_def_is_not_1bit, NULL);
return false;
}
}
bool
nir_lower_bool_to_bitsize(nir_shader *shader)
{
return nir_shader_instructions_pass(shader, nir_lower_bool_to_bitsize_instr,
nir_metadata_block_index |
nir_metadata_dominance,
NULL);
}
|