nir: add bit_size info to nir_load_const_instr_create()
[mesa.git] / src / compiler / nir / nir_search.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include <inttypes.h>
29 #include "nir_search.h"
30
31 struct match_state {
32 bool inexact_match;
33 bool has_exact_alu;
34 unsigned variables_seen;
35 nir_alu_src variables[NIR_SEARCH_MAX_VARIABLES];
36 };
37
38 static bool
39 match_expression(const nir_search_expression *expr, nir_alu_instr *instr,
40 unsigned num_components, const uint8_t *swizzle,
41 struct match_state *state);
42
43 static const uint8_t identity_swizzle[] = { 0, 1, 2, 3 };
44
45 static bool alu_instr_is_bool(nir_alu_instr *instr);
46
47 static bool
48 src_is_bool(nir_src src)
49 {
50 if (!src.is_ssa)
51 return false;
52 if (src.ssa->parent_instr->type != nir_instr_type_alu)
53 return false;
54 return alu_instr_is_bool(nir_instr_as_alu(src.ssa->parent_instr));
55 }
56
57 static bool
58 alu_instr_is_bool(nir_alu_instr *instr)
59 {
60 switch (instr->op) {
61 case nir_op_iand:
62 case nir_op_ior:
63 case nir_op_ixor:
64 return src_is_bool(instr->src[0].src) && src_is_bool(instr->src[1].src);
65 case nir_op_inot:
66 return src_is_bool(instr->src[0].src);
67 default:
68 return (nir_alu_type_get_base_type(nir_op_infos[instr->op].output_type)
69 == nir_type_bool);
70 }
71 }
72
73 static bool
74 match_value(const nir_search_value *value, nir_alu_instr *instr, unsigned src,
75 unsigned num_components, const uint8_t *swizzle,
76 struct match_state *state)
77 {
78 uint8_t new_swizzle[4];
79
80 /* If the source is an explicitly sized source, then we need to reset
81 * both the number of components and the swizzle.
82 */
83 if (nir_op_infos[instr->op].input_sizes[src] != 0) {
84 num_components = nir_op_infos[instr->op].input_sizes[src];
85 swizzle = identity_swizzle;
86 }
87
88 for (unsigned i = 0; i < num_components; ++i)
89 new_swizzle[i] = instr->src[src].swizzle[swizzle[i]];
90
91 switch (value->type) {
92 case nir_search_value_expression:
93 if (!instr->src[src].src.is_ssa)
94 return false;
95
96 if (instr->src[src].src.ssa->parent_instr->type != nir_instr_type_alu)
97 return false;
98
99 return match_expression(nir_search_value_as_expression(value),
100 nir_instr_as_alu(instr->src[src].src.ssa->parent_instr),
101 num_components, new_swizzle, state);
102
103 case nir_search_value_variable: {
104 nir_search_variable *var = nir_search_value_as_variable(value);
105 assert(var->variable < NIR_SEARCH_MAX_VARIABLES);
106
107 if (state->variables_seen & (1 << var->variable)) {
108 if (!nir_srcs_equal(state->variables[var->variable].src,
109 instr->src[src].src))
110 return false;
111
112 assert(!instr->src[src].abs && !instr->src[src].negate);
113
114 for (unsigned i = 0; i < num_components; ++i) {
115 if (state->variables[var->variable].swizzle[i] != new_swizzle[i])
116 return false;
117 }
118
119 return true;
120 } else {
121 if (var->is_constant &&
122 instr->src[src].src.ssa->parent_instr->type != nir_instr_type_load_const)
123 return false;
124
125 if (var->type != nir_type_invalid) {
126 if (instr->src[src].src.ssa->parent_instr->type != nir_instr_type_alu)
127 return false;
128
129 nir_alu_instr *src_alu =
130 nir_instr_as_alu(instr->src[src].src.ssa->parent_instr);
131
132 if (nir_alu_type_get_base_type(nir_op_infos[src_alu->op].output_type) !=
133 var->type &&
134 !(nir_alu_type_get_base_type(var->type) == nir_type_bool &&
135 alu_instr_is_bool(src_alu)))
136 return false;
137 }
138
139 state->variables_seen |= (1 << var->variable);
140 state->variables[var->variable].src = instr->src[src].src;
141 state->variables[var->variable].abs = false;
142 state->variables[var->variable].negate = false;
143
144 for (unsigned i = 0; i < 4; ++i) {
145 if (i < num_components)
146 state->variables[var->variable].swizzle[i] = new_swizzle[i];
147 else
148 state->variables[var->variable].swizzle[i] = 0;
149 }
150
151 return true;
152 }
153 }
154
155 case nir_search_value_constant: {
156 nir_search_constant *const_val = nir_search_value_as_constant(value);
157
158 if (!instr->src[src].src.is_ssa)
159 return false;
160
161 if (instr->src[src].src.ssa->parent_instr->type != nir_instr_type_load_const)
162 return false;
163
164 nir_load_const_instr *load =
165 nir_instr_as_load_const(instr->src[src].src.ssa->parent_instr);
166
167 switch (const_val->type) {
168 case nir_type_float:
169 for (unsigned i = 0; i < num_components; ++i) {
170 double val;
171 switch (load->def.bit_size) {
172 case 32:
173 val = load->value.f32[new_swizzle[i]];
174 break;
175 case 64:
176 val = load->value.f64[new_swizzle[i]];
177 break;
178 default:
179 unreachable("unknown bit size");
180 }
181
182 if (val != const_val->data.d)
183 return false;
184 }
185 return true;
186
187 case nir_type_int:
188 for (unsigned i = 0; i < num_components; ++i) {
189 int64_t val;
190 switch (load->def.bit_size) {
191 case 32:
192 val = load->value.i32[new_swizzle[i]];
193 break;
194 case 64:
195 val = load->value.i64[new_swizzle[i]];
196 break;
197 default:
198 unreachable("unknown bit size");
199 }
200
201 if (val != const_val->data.i)
202 return false;
203 }
204 return true;
205
206 case nir_type_uint:
207 case nir_type_bool32:
208 for (unsigned i = 0; i < num_components; ++i) {
209 uint64_t val;
210 switch (load->def.bit_size) {
211 case 32:
212 val = load->value.u32[new_swizzle[i]];
213 break;
214 case 64:
215 val = load->value.u64[new_swizzle[i]];
216 break;
217 default:
218 unreachable("unknown bit size");
219 }
220
221 if (val != const_val->data.u)
222 return false;
223 }
224 return true;
225
226 default:
227 unreachable("Invalid alu source type");
228 }
229 }
230
231 default:
232 unreachable("Invalid search value type");
233 }
234 }
235
236 static bool
237 match_expression(const nir_search_expression *expr, nir_alu_instr *instr,
238 unsigned num_components, const uint8_t *swizzle,
239 struct match_state *state)
240 {
241 if (instr->op != expr->opcode)
242 return false;
243
244 assert(instr->dest.dest.is_ssa);
245
246 state->inexact_match = expr->inexact || state->inexact_match;
247 state->has_exact_alu = instr->exact || state->has_exact_alu;
248 if (state->inexact_match && state->has_exact_alu)
249 return false;
250
251 assert(!instr->dest.saturate);
252 assert(nir_op_infos[instr->op].num_inputs > 0);
253
254 /* If we have an explicitly sized destination, we can only handle the
255 * identity swizzle. While dot(vec3(a, b, c).zxy) is a valid
256 * expression, we don't have the information right now to propagate that
257 * swizzle through. We can only properly propagate swizzles if the
258 * instruction is vectorized.
259 */
260 if (nir_op_infos[instr->op].output_size != 0) {
261 for (unsigned i = 0; i < num_components; i++) {
262 if (swizzle[i] != i)
263 return false;
264 }
265 }
266
267 /* Stash off the current variables_seen bitmask. This way we can
268 * restore it prior to matching in the commutative case below.
269 */
270 unsigned variables_seen_stash = state->variables_seen;
271
272 bool matched = true;
273 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
274 if (!match_value(expr->srcs[i], instr, i, num_components,
275 swizzle, state)) {
276 matched = false;
277 break;
278 }
279 }
280
281 if (matched)
282 return true;
283
284 if (nir_op_infos[instr->op].algebraic_properties & NIR_OP_IS_COMMUTATIVE) {
285 assert(nir_op_infos[instr->op].num_inputs == 2);
286
287 /* Restore the variables_seen bitmask. If we don't do this, then we
288 * could end up with an erroneous failure due to variables found in the
289 * first match attempt above not matching those in the second.
290 */
291 state->variables_seen = variables_seen_stash;
292
293 if (!match_value(expr->srcs[0], instr, 1, num_components,
294 swizzle, state))
295 return false;
296
297 return match_value(expr->srcs[1], instr, 0, num_components,
298 swizzle, state);
299 } else {
300 return false;
301 }
302 }
303
304 typedef struct bitsize_tree {
305 unsigned num_srcs;
306 struct bitsize_tree *srcs[4];
307
308 unsigned common_size;
309 bool is_src_sized[4];
310 bool is_dest_sized;
311
312 unsigned dest_size;
313 unsigned src_size[4];
314 } bitsize_tree;
315
316 static bitsize_tree *
317 build_bitsize_tree(void *mem_ctx, struct match_state *state,
318 const nir_search_value *value)
319 {
320 bitsize_tree *tree = ralloc(mem_ctx, bitsize_tree);
321
322 switch (value->type) {
323 case nir_search_value_expression: {
324 nir_search_expression *expr = nir_search_value_as_expression(value);
325 nir_op_info info = nir_op_infos[expr->opcode];
326 tree->num_srcs = info.num_inputs;
327 tree->common_size = 0;
328 for (unsigned i = 0; i < info.num_inputs; i++) {
329 tree->is_src_sized[i] = !!nir_alu_type_get_type_size(info.input_types[i]);
330 if (tree->is_src_sized[i])
331 tree->src_size[i] = nir_alu_type_get_type_size(info.input_types[i]);
332 tree->srcs[i] = build_bitsize_tree(mem_ctx, state, expr->srcs[i]);
333 }
334 tree->is_dest_sized = !!nir_alu_type_get_type_size(info.output_type);
335 if (tree->is_dest_sized)
336 tree->dest_size = nir_alu_type_get_type_size(info.output_type);
337 break;
338 }
339
340 case nir_search_value_variable: {
341 nir_search_variable *var = nir_search_value_as_variable(value);
342 tree->num_srcs = 0;
343 tree->is_dest_sized = true;
344 tree->dest_size = nir_src_bit_size(state->variables[var->variable].src);
345 break;
346 }
347
348 case nir_search_value_constant: {
349 tree->num_srcs = 0;
350 tree->is_dest_sized = false;
351 tree->common_size = 0;
352 break;
353 }
354 }
355
356 return tree;
357 }
358
359 static unsigned
360 bitsize_tree_filter_up(bitsize_tree *tree)
361 {
362 for (unsigned i = 0; i < tree->num_srcs; i++) {
363 unsigned src_size = bitsize_tree_filter_up(tree->srcs[i]);
364 if (src_size == 0)
365 continue;
366
367 if (tree->is_src_sized[i]) {
368 assert(src_size == tree->src_size[i]);
369 } else if (tree->common_size != 0) {
370 assert(src_size == tree->common_size);
371 tree->src_size[i] = src_size;
372 } else {
373 tree->common_size = src_size;
374 tree->src_size[i] = src_size;
375 }
376 }
377
378 if (tree->num_srcs && tree->common_size) {
379 if (tree->dest_size == 0)
380 tree->dest_size = tree->common_size;
381 else if (!tree->is_dest_sized)
382 assert(tree->dest_size == tree->common_size);
383
384 for (unsigned i = 0; i < tree->num_srcs; i++) {
385 if (!tree->src_size[i])
386 tree->src_size[i] = tree->common_size;
387 }
388 }
389
390 return tree->dest_size;
391 }
392
393 static void
394 bitsize_tree_filter_down(bitsize_tree *tree, unsigned size)
395 {
396 if (tree->dest_size)
397 assert(tree->dest_size == size);
398 else
399 tree->dest_size = size;
400
401 if (!tree->is_dest_sized) {
402 if (tree->common_size)
403 assert(tree->common_size == size);
404 else
405 tree->common_size = size;
406 }
407
408 for (unsigned i = 0; i < tree->num_srcs; i++) {
409 if (!tree->src_size[i]) {
410 assert(tree->common_size);
411 tree->src_size[i] = tree->common_size;
412 }
413 bitsize_tree_filter_down(tree->srcs[i], tree->src_size[i]);
414 }
415 }
416
417 static nir_alu_src
418 construct_value(const nir_search_value *value,
419 unsigned num_components, bitsize_tree *bitsize,
420 struct match_state *state,
421 nir_instr *instr, void *mem_ctx)
422 {
423 switch (value->type) {
424 case nir_search_value_expression: {
425 const nir_search_expression *expr = nir_search_value_as_expression(value);
426
427 if (nir_op_infos[expr->opcode].output_size != 0)
428 num_components = nir_op_infos[expr->opcode].output_size;
429
430 nir_alu_instr *alu = nir_alu_instr_create(mem_ctx, expr->opcode);
431 nir_ssa_dest_init(&alu->instr, &alu->dest.dest, num_components,
432 bitsize->dest_size, NULL);
433 alu->dest.write_mask = (1 << num_components) - 1;
434 alu->dest.saturate = false;
435
436 /* We have no way of knowing what values in a given search expression
437 * map to a particular replacement value. Therefore, if the
438 * expression we are replacing has any exact values, the entire
439 * replacement should be exact.
440 */
441 alu->exact = state->has_exact_alu;
442
443 for (unsigned i = 0; i < nir_op_infos[expr->opcode].num_inputs; i++) {
444 /* If the source is an explicitly sized source, then we need to reset
445 * the number of components to match.
446 */
447 if (nir_op_infos[alu->op].input_sizes[i] != 0)
448 num_components = nir_op_infos[alu->op].input_sizes[i];
449
450 alu->src[i] = construct_value(expr->srcs[i],
451 num_components, bitsize->srcs[i],
452 state, instr, mem_ctx);
453 }
454
455 nir_instr_insert_before(instr, &alu->instr);
456
457 nir_alu_src val;
458 val.src = nir_src_for_ssa(&alu->dest.dest.ssa);
459 val.negate = false;
460 val.abs = false,
461 memcpy(val.swizzle, identity_swizzle, sizeof val.swizzle);
462
463 return val;
464 }
465
466 case nir_search_value_variable: {
467 const nir_search_variable *var = nir_search_value_as_variable(value);
468 assert(state->variables_seen & (1 << var->variable));
469
470 nir_alu_src val = { NIR_SRC_INIT };
471 nir_alu_src_copy(&val, &state->variables[var->variable], mem_ctx);
472
473 assert(!var->is_constant);
474
475 return val;
476 }
477
478 case nir_search_value_constant: {
479 const nir_search_constant *c = nir_search_value_as_constant(value);
480 nir_load_const_instr *load =
481 nir_load_const_instr_create(mem_ctx, 1, bitsize->dest_size);
482
483 switch (c->type) {
484 case nir_type_float:
485 load->def.name = ralloc_asprintf(load, "%f", c->data.d);
486 switch (bitsize->dest_size) {
487 case 32:
488 load->value.f32[0] = c->data.d;
489 break;
490 case 64:
491 load->value.f64[0] = c->data.d;
492 break;
493 default:
494 unreachable("unknown bit size");
495 }
496 break;
497
498 case nir_type_int:
499 load->def.name = ralloc_asprintf(load, "%" PRIi64, c->data.i);
500 switch (bitsize->dest_size) {
501 case 32:
502 load->value.i32[0] = c->data.i;
503 break;
504 case 64:
505 load->value.i64[0] = c->data.i;
506 break;
507 default:
508 unreachable("unknown bit size");
509 }
510 break;
511
512 case nir_type_uint:
513 load->def.name = ralloc_asprintf(load, "%" PRIu64, c->data.u);
514 switch (bitsize->dest_size) {
515 case 32:
516 load->value.u32[0] = c->data.u;
517 break;
518 case 64:
519 load->value.u64[0] = c->data.u;
520 break;
521 default:
522 unreachable("unknown bit size");
523 }
524
525 case nir_type_bool32:
526 load->value.u32[0] = c->data.u;
527 break;
528 default:
529 unreachable("Invalid alu source type");
530 }
531
532 nir_instr_insert_before(instr, &load->instr);
533
534 nir_alu_src val;
535 val.src = nir_src_for_ssa(&load->def);
536 val.negate = false;
537 val.abs = false,
538 memset(val.swizzle, 0, sizeof val.swizzle);
539
540 return val;
541 }
542
543 default:
544 unreachable("Invalid search value type");
545 }
546 }
547
548 nir_alu_instr *
549 nir_replace_instr(nir_alu_instr *instr, const nir_search_expression *search,
550 const nir_search_value *replace, void *mem_ctx)
551 {
552 uint8_t swizzle[4] = { 0, 0, 0, 0 };
553
554 for (unsigned i = 0; i < instr->dest.dest.ssa.num_components; ++i)
555 swizzle[i] = i;
556
557 assert(instr->dest.dest.is_ssa);
558
559 struct match_state state;
560 state.inexact_match = false;
561 state.has_exact_alu = false;
562 state.variables_seen = 0;
563
564 if (!match_expression(search, instr, instr->dest.dest.ssa.num_components,
565 swizzle, &state))
566 return NULL;
567
568 void *bitsize_ctx = ralloc_context(NULL);
569 bitsize_tree *tree = build_bitsize_tree(bitsize_ctx, &state, replace);
570 bitsize_tree_filter_up(tree);
571 bitsize_tree_filter_down(tree, instr->dest.dest.ssa.bit_size);
572
573 /* Inserting a mov may be unnecessary. However, it's much easier to
574 * simply let copy propagation clean this up than to try to go through
575 * and rewrite swizzles ourselves.
576 */
577 nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov);
578 mov->dest.write_mask = instr->dest.write_mask;
579 nir_ssa_dest_init(&mov->instr, &mov->dest.dest,
580 instr->dest.dest.ssa.num_components,
581 instr->dest.dest.ssa.bit_size, NULL);
582
583 mov->src[0] = construct_value(replace,
584 instr->dest.dest.ssa.num_components, tree,
585 &state, &instr->instr, mem_ctx);
586 nir_instr_insert_before(&instr->instr, &mov->instr);
587
588 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa,
589 nir_src_for_ssa(&mov->dest.dest.ssa));
590
591 /* We know this one has no more uses because we just rewrote them all,
592 * so we can remove it. The rest of the matched expression, however, we
593 * don't know so much about. We'll just let dead code clean them up.
594 */
595 nir_instr_remove(&instr->instr);
596
597 ralloc_free(bitsize_ctx);
598
599 return mov;
600 }