nir: some comment fixups
[mesa.git] / src / glsl / nir / glsl_to_nir.cpp
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 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "glsl_to_nir.h"
29 #include "nir_control_flow.h"
30 #include "ir_visitor.h"
31 #include "ir_hierarchical_visitor.h"
32 #include "ir.h"
33
34 /*
35 * pass to lower GLSL IR to NIR
36 *
37 * This will lower variable dereferences to loads/stores of corresponding
38 * variables in NIR - the variables will be converted to registers in a later
39 * pass.
40 */
41
42 namespace {
43
44 class nir_visitor : public ir_visitor
45 {
46 public:
47 nir_visitor(nir_shader *shader);
48 ~nir_visitor();
49
50 virtual void visit(ir_variable *);
51 virtual void visit(ir_function *);
52 virtual void visit(ir_function_signature *);
53 virtual void visit(ir_loop *);
54 virtual void visit(ir_if *);
55 virtual void visit(ir_discard *);
56 virtual void visit(ir_loop_jump *);
57 virtual void visit(ir_return *);
58 virtual void visit(ir_call *);
59 virtual void visit(ir_assignment *);
60 virtual void visit(ir_emit_vertex *);
61 virtual void visit(ir_end_primitive *);
62 virtual void visit(ir_expression *);
63 virtual void visit(ir_swizzle *);
64 virtual void visit(ir_texture *);
65 virtual void visit(ir_constant *);
66 virtual void visit(ir_dereference_variable *);
67 virtual void visit(ir_dereference_record *);
68 virtual void visit(ir_dereference_array *);
69 virtual void visit(ir_barrier *);
70
71 void create_function(ir_function *ir);
72
73 private:
74 void create_overload(ir_function_signature *ir, nir_function *function);
75 void add_instr(nir_instr *instr, unsigned num_components);
76 nir_src evaluate_rvalue(ir_rvalue *ir);
77
78 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src *srcs);
79 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src src1);
80 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src src1,
81 nir_src src2);
82 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_src src1,
83 nir_src src2, nir_src src3);
84
85 bool supports_ints;
86
87 nir_shader *shader;
88 nir_function_impl *impl;
89 exec_list *cf_node_list;
90 nir_instr *result; /* result of the expression tree last visited */
91
92 nir_deref_var *evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir);
93
94 /* the head of the dereference chain we're creating */
95 nir_deref_var *deref_head;
96 /* the tail of the dereference chain we're creating */
97 nir_deref *deref_tail;
98
99 nir_variable *var; /* variable created by ir_variable visitor */
100
101 /* whether the IR we're operating on is per-function or global */
102 bool is_global;
103
104 /* map of ir_variable -> nir_variable */
105 struct hash_table *var_table;
106
107 /* map of ir_function_signature -> nir_function_overload */
108 struct hash_table *overload_table;
109 };
110
111 /*
112 * This visitor runs before the main visitor, calling create_function() for
113 * each function so that the main visitor can resolve forward references in
114 * calls.
115 */
116
117 class nir_function_visitor : public ir_hierarchical_visitor
118 {
119 public:
120 nir_function_visitor(nir_visitor *v) : visitor(v)
121 {
122 }
123 virtual ir_visitor_status visit_enter(ir_function *);
124
125 private:
126 nir_visitor *visitor;
127 };
128
129 }; /* end of anonymous namespace */
130
131 nir_shader *
132 glsl_to_nir(struct gl_shader *sh, const nir_shader_compiler_options *options)
133 {
134 nir_shader *shader = nir_shader_create(NULL, sh->Stage, options);
135
136 nir_visitor v1(shader);
137 nir_function_visitor v2(&v1);
138 v2.run(sh->ir);
139 visit_exec_list(sh->ir, &v1);
140
141 nir_lower_outputs_to_temporaries(shader);
142
143 shader->gs.vertices_out = sh->Geom.VerticesOut;
144 shader->gs.invocations = sh->Geom.Invocations;
145
146 return shader;
147 }
148
149 nir_visitor::nir_visitor(nir_shader *shader)
150 {
151 this->supports_ints = shader->options->native_integers;
152 this->shader = shader;
153 this->is_global = true;
154 this->var_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
155 _mesa_key_pointer_equal);
156 this->overload_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
157 _mesa_key_pointer_equal);
158 }
159
160 nir_visitor::~nir_visitor()
161 {
162 _mesa_hash_table_destroy(this->var_table, NULL);
163 _mesa_hash_table_destroy(this->overload_table, NULL);
164 }
165
166 nir_deref_var *
167 nir_visitor::evaluate_deref(nir_instr *mem_ctx, ir_instruction *ir)
168 {
169 ir->accept(this);
170 ralloc_steal(mem_ctx, this->deref_head);
171 return this->deref_head;
172 }
173
174 static nir_constant *
175 constant_copy(ir_constant *ir, void *mem_ctx)
176 {
177 if (ir == NULL)
178 return NULL;
179
180 nir_constant *ret = ralloc(mem_ctx, nir_constant);
181
182 unsigned total_elems = ir->type->components();
183 unsigned i;
184 switch (ir->type->base_type) {
185 case GLSL_TYPE_UINT:
186 for (i = 0; i < total_elems; i++)
187 ret->value.u[i] = ir->value.u[i];
188 break;
189
190 case GLSL_TYPE_INT:
191 for (i = 0; i < total_elems; i++)
192 ret->value.i[i] = ir->value.i[i];
193 break;
194
195 case GLSL_TYPE_FLOAT:
196 for (i = 0; i < total_elems; i++)
197 ret->value.f[i] = ir->value.f[i];
198 break;
199
200 case GLSL_TYPE_BOOL:
201 for (i = 0; i < total_elems; i++)
202 ret->value.b[i] = ir->value.b[i];
203 break;
204
205 case GLSL_TYPE_STRUCT:
206 ret->elements = ralloc_array(mem_ctx, nir_constant *,
207 ir->type->length);
208 i = 0;
209 foreach_in_list(ir_constant, field, &ir->components) {
210 ret->elements[i] = constant_copy(field, mem_ctx);
211 i++;
212 }
213 break;
214
215 case GLSL_TYPE_ARRAY:
216 ret->elements = ralloc_array(mem_ctx, nir_constant *,
217 ir->type->length);
218
219 for (i = 0; i < ir->type->length; i++)
220 ret->elements[i] = constant_copy(ir->array_elements[i], mem_ctx);
221 break;
222
223 default:
224 unreachable("not reached");
225 }
226
227 return ret;
228 }
229
230 void
231 nir_visitor::visit(ir_variable *ir)
232 {
233 nir_variable *var = ralloc(shader, nir_variable);
234 var->type = ir->type;
235 var->name = ralloc_strdup(var, ir->name);
236
237 if (ir->is_interface_instance() && ir->get_max_ifc_array_access() != NULL) {
238 unsigned size = ir->get_interface_type()->length;
239 var->max_ifc_array_access = ralloc_array(var, unsigned, size);
240 memcpy(var->max_ifc_array_access, ir->get_max_ifc_array_access(),
241 size * sizeof(unsigned));
242 } else {
243 var->max_ifc_array_access = NULL;
244 }
245
246 var->data.read_only = ir->data.read_only;
247 var->data.centroid = ir->data.centroid;
248 var->data.sample = ir->data.sample;
249 var->data.invariant = ir->data.invariant;
250 var->data.location = ir->data.location;
251
252 switch(ir->data.mode) {
253 case ir_var_auto:
254 case ir_var_temporary:
255 if (is_global)
256 var->data.mode = nir_var_global;
257 else
258 var->data.mode = nir_var_local;
259 break;
260
261 case ir_var_function_in:
262 case ir_var_function_out:
263 case ir_var_function_inout:
264 case ir_var_const_in:
265 var->data.mode = nir_var_local;
266 break;
267
268 case ir_var_shader_in:
269 if (shader->stage == MESA_SHADER_FRAGMENT &&
270 ir->data.location == VARYING_SLOT_FACE) {
271 /* For whatever reason, GLSL IR makes gl_FrontFacing an input */
272 var->data.location = SYSTEM_VALUE_FRONT_FACE;
273 var->data.mode = nir_var_system_value;
274 } else {
275 var->data.mode = nir_var_shader_in;
276 }
277 break;
278
279 case ir_var_shader_out:
280 var->data.mode = nir_var_shader_out;
281 break;
282
283 case ir_var_uniform:
284 var->data.mode = nir_var_uniform;
285 break;
286
287 case ir_var_shader_storage:
288 var->data.mode = nir_var_shader_storage;
289 break;
290
291 case ir_var_system_value:
292 var->data.mode = nir_var_system_value;
293 break;
294
295 default:
296 unreachable("not reached");
297 }
298
299 var->data.interpolation = ir->data.interpolation;
300 var->data.origin_upper_left = ir->data.origin_upper_left;
301 var->data.pixel_center_integer = ir->data.pixel_center_integer;
302 var->data.explicit_location = ir->data.explicit_location;
303 var->data.explicit_index = ir->data.explicit_index;
304 var->data.explicit_binding = ir->data.explicit_binding;
305 var->data.has_initializer = ir->data.has_initializer;
306 var->data.is_unmatched_generic_inout = ir->data.is_unmatched_generic_inout;
307 var->data.location_frac = ir->data.location_frac;
308 var->data.from_named_ifc_block_array = ir->data.from_named_ifc_block_array;
309 var->data.from_named_ifc_block_nonarray = ir->data.from_named_ifc_block_nonarray;
310
311 switch (ir->data.depth_layout) {
312 case ir_depth_layout_none:
313 var->data.depth_layout = nir_depth_layout_none;
314 break;
315 case ir_depth_layout_any:
316 var->data.depth_layout = nir_depth_layout_any;
317 break;
318 case ir_depth_layout_greater:
319 var->data.depth_layout = nir_depth_layout_greater;
320 break;
321 case ir_depth_layout_less:
322 var->data.depth_layout = nir_depth_layout_less;
323 break;
324 case ir_depth_layout_unchanged:
325 var->data.depth_layout = nir_depth_layout_unchanged;
326 break;
327 default:
328 unreachable("not reached");
329 }
330
331 var->data.index = ir->data.index;
332 var->data.binding = ir->data.binding;
333 /* XXX Get rid of buffer_index */
334 var->data.atomic.buffer_index = ir->data.binding;
335 var->data.atomic.offset = ir->data.atomic.offset;
336 var->data.image.read_only = ir->data.image_read_only;
337 var->data.image.write_only = ir->data.image_write_only;
338 var->data.image.coherent = ir->data.image_coherent;
339 var->data.image._volatile = ir->data.image_volatile;
340 var->data.image.restrict_flag = ir->data.image_restrict;
341 var->data.image.format = ir->data.image_format;
342 var->data.max_array_access = ir->data.max_array_access;
343
344 var->num_state_slots = ir->get_num_state_slots();
345 if (var->num_state_slots > 0) {
346 var->state_slots = ralloc_array(var, nir_state_slot,
347 var->num_state_slots);
348
349 ir_state_slot *state_slots = ir->get_state_slots();
350 for (unsigned i = 0; i < var->num_state_slots; i++) {
351 for (unsigned j = 0; j < 5; j++)
352 var->state_slots[i].tokens[j] = state_slots[i].tokens[j];
353 var->state_slots[i].swizzle = state_slots[i].swizzle;
354 }
355 } else {
356 var->state_slots = NULL;
357 }
358
359 var->constant_initializer = constant_copy(ir->constant_initializer, var);
360
361 var->interface_type = ir->get_interface_type();
362
363 switch (var->data.mode) {
364 case nir_var_local:
365 exec_list_push_tail(&impl->locals, &var->node);
366 break;
367
368 case nir_var_global:
369 exec_list_push_tail(&shader->globals, &var->node);
370 break;
371
372 case nir_var_shader_in:
373 exec_list_push_tail(&shader->inputs, &var->node);
374 break;
375
376 case nir_var_shader_out:
377 exec_list_push_tail(&shader->outputs, &var->node);
378 break;
379
380 case nir_var_uniform:
381 case nir_var_shader_storage:
382 exec_list_push_tail(&shader->uniforms, &var->node);
383 break;
384
385 case nir_var_system_value:
386 exec_list_push_tail(&shader->system_values, &var->node);
387 break;
388
389 default:
390 unreachable("not reached");
391 }
392
393 _mesa_hash_table_insert(var_table, ir, var);
394 this->var = var;
395 }
396
397 ir_visitor_status
398 nir_function_visitor::visit_enter(ir_function *ir)
399 {
400 visitor->create_function(ir);
401 return visit_continue_with_parent;
402 }
403
404
405 void
406 nir_visitor::create_function(ir_function *ir)
407 {
408 nir_function *func = nir_function_create(this->shader, ir->name);
409 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
410 create_overload(sig, func);
411 }
412 }
413
414
415
416 void
417 nir_visitor::create_overload(ir_function_signature *ir, nir_function *function)
418 {
419 if (ir->is_intrinsic)
420 return;
421
422 nir_function_overload *overload = nir_function_overload_create(function);
423
424 unsigned num_params = ir->parameters.length();
425 overload->num_params = num_params;
426 overload->params = ralloc_array(shader, nir_parameter, num_params);
427
428 unsigned i = 0;
429 foreach_in_list(ir_variable, param, &ir->parameters) {
430 switch (param->data.mode) {
431 case ir_var_function_in:
432 overload->params[i].param_type = nir_parameter_in;
433 break;
434
435 case ir_var_function_out:
436 overload->params[i].param_type = nir_parameter_out;
437 break;
438
439 case ir_var_function_inout:
440 overload->params[i].param_type = nir_parameter_inout;
441 break;
442
443 default:
444 unreachable("not reached");
445 }
446
447 overload->params[i].type = param->type;
448 i++;
449 }
450
451 overload->return_type = ir->return_type;
452
453 _mesa_hash_table_insert(this->overload_table, ir, overload);
454 }
455
456 void
457 nir_visitor::visit(ir_function *ir)
458 {
459 foreach_in_list(ir_function_signature, sig, &ir->signatures)
460 sig->accept(this);
461 }
462
463 void
464 nir_visitor::visit(ir_function_signature *ir)
465 {
466 if (ir->is_intrinsic)
467 return;
468
469 struct hash_entry *entry =
470 _mesa_hash_table_search(this->overload_table, ir);
471
472 assert(entry);
473 nir_function_overload *overload = (nir_function_overload *) entry->data;
474
475 if (ir->is_defined) {
476 nir_function_impl *impl = nir_function_impl_create(overload);
477 this->impl = impl;
478
479 unsigned num_params = overload->num_params;
480 impl->num_params = num_params;
481 impl->params = ralloc_array(this->shader, nir_variable *, num_params);
482 unsigned i = 0;
483 foreach_in_list(ir_variable, param, &ir->parameters) {
484 param->accept(this);
485 impl->params[i] = this->var;
486 i++;
487 }
488
489 if (overload->return_type == glsl_type::void_type) {
490 impl->return_var = NULL;
491 } else {
492 impl->return_var = ralloc(this->shader, nir_variable);
493 impl->return_var->name = ralloc_strdup(impl->return_var,
494 "return_var");
495 impl->return_var->type = overload->return_type;
496 }
497
498 this->is_global = false;
499
500 this->cf_node_list = &impl->body;
501 visit_exec_list(&ir->body, this);
502
503 this->is_global = true;
504 } else {
505 overload->impl = NULL;
506 }
507 }
508
509 void
510 nir_visitor::visit(ir_loop *ir)
511 {
512 exec_list *old_list = this->cf_node_list;
513
514 nir_loop *loop = nir_loop_create(this->shader);
515 nir_cf_node_insert_end(old_list, &loop->cf_node);
516 this->cf_node_list = &loop->body;
517 visit_exec_list(&ir->body_instructions, this);
518
519 this->cf_node_list = old_list;
520 }
521
522 void
523 nir_visitor::visit(ir_if *ir)
524 {
525 nir_src condition = evaluate_rvalue(ir->condition);
526
527 exec_list *old_list = this->cf_node_list;
528
529 nir_if *if_stmt = nir_if_create(this->shader);
530 if_stmt->condition = condition;
531 nir_cf_node_insert_end(old_list, &if_stmt->cf_node);
532
533 this->cf_node_list = &if_stmt->then_list;
534 visit_exec_list(&ir->then_instructions, this);
535
536 this->cf_node_list = &if_stmt->else_list;
537 visit_exec_list(&ir->else_instructions, this);
538
539 this->cf_node_list = old_list;
540 }
541
542 void
543 nir_visitor::visit(ir_discard *ir)
544 {
545 /*
546 * discards aren't treated as control flow, because before we lower them
547 * they can appear anywhere in the shader and the stuff after them may still
548 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
549 * discards will be immediately followed by a return.
550 */
551
552 nir_intrinsic_instr *discard;
553 if (ir->condition) {
554 discard = nir_intrinsic_instr_create(this->shader,
555 nir_intrinsic_discard_if);
556 discard->src[0] = evaluate_rvalue(ir->condition);
557 } else {
558 discard = nir_intrinsic_instr_create(this->shader, nir_intrinsic_discard);
559 }
560 nir_instr_insert_after_cf_list(this->cf_node_list, &discard->instr);
561 }
562
563 void
564 nir_visitor::visit(ir_emit_vertex *ir)
565 {
566 nir_intrinsic_instr *instr =
567 nir_intrinsic_instr_create(this->shader, nir_intrinsic_emit_vertex);
568 instr->const_index[0] = ir->stream_id();
569 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
570 }
571
572 void
573 nir_visitor::visit(ir_end_primitive *ir)
574 {
575 nir_intrinsic_instr *instr =
576 nir_intrinsic_instr_create(this->shader, nir_intrinsic_end_primitive);
577 instr->const_index[0] = ir->stream_id();
578 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
579 }
580
581 void
582 nir_visitor::visit(ir_loop_jump *ir)
583 {
584 nir_jump_type type;
585 switch (ir->mode) {
586 case ir_loop_jump::jump_break:
587 type = nir_jump_break;
588 break;
589 case ir_loop_jump::jump_continue:
590 type = nir_jump_continue;
591 break;
592 default:
593 unreachable("not reached");
594 }
595
596 nir_jump_instr *instr = nir_jump_instr_create(this->shader, type);
597 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
598 }
599
600 void
601 nir_visitor::visit(ir_return *ir)
602 {
603 if (ir->value != NULL) {
604 nir_intrinsic_instr *copy =
605 nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
606
607 copy->variables[0] = nir_deref_var_create(copy, this->impl->return_var);
608 copy->variables[1] = evaluate_deref(&copy->instr, ir->value);
609 }
610
611 nir_jump_instr *instr = nir_jump_instr_create(this->shader, nir_jump_return);
612 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
613 }
614
615 void
616 nir_visitor::visit(ir_call *ir)
617 {
618 if (ir->callee->is_intrinsic) {
619 nir_intrinsic_op op;
620 if (strcmp(ir->callee_name(), "__intrinsic_atomic_read") == 0) {
621 op = nir_intrinsic_atomic_counter_read_var;
622 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_increment") == 0) {
623 op = nir_intrinsic_atomic_counter_inc_var;
624 } else if (strcmp(ir->callee_name(), "__intrinsic_atomic_predecrement") == 0) {
625 op = nir_intrinsic_atomic_counter_dec_var;
626 } else if (strcmp(ir->callee_name(), "__intrinsic_image_load") == 0) {
627 op = nir_intrinsic_image_load;
628 } else if (strcmp(ir->callee_name(), "__intrinsic_image_store") == 0) {
629 op = nir_intrinsic_image_store;
630 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_add") == 0) {
631 op = nir_intrinsic_image_atomic_add;
632 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_min") == 0) {
633 op = nir_intrinsic_image_atomic_min;
634 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_max") == 0) {
635 op = nir_intrinsic_image_atomic_max;
636 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_and") == 0) {
637 op = nir_intrinsic_image_atomic_and;
638 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_or") == 0) {
639 op = nir_intrinsic_image_atomic_or;
640 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_xor") == 0) {
641 op = nir_intrinsic_image_atomic_xor;
642 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_exchange") == 0) {
643 op = nir_intrinsic_image_atomic_exchange;
644 } else if (strcmp(ir->callee_name(), "__intrinsic_image_atomic_comp_swap") == 0) {
645 op = nir_intrinsic_image_atomic_comp_swap;
646 } else if (strcmp(ir->callee_name(), "__intrinsic_memory_barrier") == 0) {
647 op = nir_intrinsic_memory_barrier;
648 } else if (strcmp(ir->callee_name(), "__intrinsic_image_size") == 0) {
649 op = nir_intrinsic_image_size;
650 } else if (strcmp(ir->callee_name(), "__intrinsic_image_samples") == 0) {
651 op = nir_intrinsic_image_samples;
652 } else {
653 unreachable("not reached");
654 }
655
656 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(shader, op);
657
658 switch (op) {
659 case nir_intrinsic_atomic_counter_read_var:
660 case nir_intrinsic_atomic_counter_inc_var:
661 case nir_intrinsic_atomic_counter_dec_var: {
662 ir_dereference *param =
663 (ir_dereference *) ir->actual_parameters.get_head();
664 instr->variables[0] = evaluate_deref(&instr->instr, param);
665 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, NULL);
666 break;
667 }
668 case nir_intrinsic_image_load:
669 case nir_intrinsic_image_store:
670 case nir_intrinsic_image_atomic_add:
671 case nir_intrinsic_image_atomic_min:
672 case nir_intrinsic_image_atomic_max:
673 case nir_intrinsic_image_atomic_and:
674 case nir_intrinsic_image_atomic_or:
675 case nir_intrinsic_image_atomic_xor:
676 case nir_intrinsic_image_atomic_exchange:
677 case nir_intrinsic_image_atomic_comp_swap:
678 case nir_intrinsic_image_samples:
679 case nir_intrinsic_image_size: {
680 nir_ssa_undef_instr *instr_undef =
681 nir_ssa_undef_instr_create(shader, 1);
682 nir_instr_insert_after_cf_list(this->cf_node_list,
683 &instr_undef->instr);
684
685 /* Set the image variable dereference. */
686 exec_node *param = ir->actual_parameters.get_head();
687 ir_dereference *image = (ir_dereference *)param;
688 const glsl_type *type =
689 image->variable_referenced()->type->without_array();
690
691 instr->variables[0] = evaluate_deref(&instr->instr, image);
692 param = param->get_next();
693
694 /* Set the intrinsic destination. */
695 if (ir->return_deref) {
696 const nir_intrinsic_info *info =
697 &nir_intrinsic_infos[instr->intrinsic];
698 nir_ssa_dest_init(&instr->instr, &instr->dest,
699 info->dest_components, NULL);
700 }
701
702 if (op == nir_intrinsic_image_size ||
703 op == nir_intrinsic_image_samples)
704 break;
705
706 /* Set the address argument, extending the coordinate vector to four
707 * components.
708 */
709 const nir_src src_addr = evaluate_rvalue((ir_dereference *)param);
710 nir_alu_instr *instr_addr = nir_alu_instr_create(shader, nir_op_vec4);
711 nir_ssa_dest_init(&instr_addr->instr, &instr_addr->dest.dest, 4, NULL);
712
713 for (int i = 0; i < 4; i++) {
714 if (i < type->coordinate_components()) {
715 instr_addr->src[i].src = src_addr;
716 instr_addr->src[i].swizzle[0] = i;
717 } else {
718 instr_addr->src[i].src = nir_src_for_ssa(&instr_undef->def);
719 }
720 }
721
722 nir_instr_insert_after_cf_list(cf_node_list, &instr_addr->instr);
723 instr->src[0] = nir_src_for_ssa(&instr_addr->dest.dest.ssa);
724 param = param->get_next();
725
726 /* Set the sample argument, which is undefined for single-sample
727 * images.
728 */
729 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
730 instr->src[1] = evaluate_rvalue((ir_dereference *)param);
731 param = param->get_next();
732 } else {
733 instr->src[1] = nir_src_for_ssa(&instr_undef->def);
734 }
735
736 /* Set the intrinsic parameters. */
737 if (!param->is_tail_sentinel()) {
738 instr->src[2] = evaluate_rvalue((ir_dereference *)param);
739 param = param->get_next();
740 }
741
742 if (!param->is_tail_sentinel()) {
743 instr->src[3] = evaluate_rvalue((ir_dereference *)param);
744 param = param->get_next();
745 }
746 break;
747 }
748 case nir_intrinsic_memory_barrier:
749 break;
750 default:
751 unreachable("not reached");
752 }
753
754 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
755
756 if (ir->return_deref) {
757 nir_intrinsic_instr *store_instr =
758 nir_intrinsic_instr_create(shader, nir_intrinsic_store_var);
759 store_instr->num_components = ir->return_deref->type->vector_elements;
760
761 store_instr->variables[0] =
762 evaluate_deref(&store_instr->instr, ir->return_deref);
763 store_instr->src[0] = nir_src_for_ssa(&instr->dest.ssa);
764
765 nir_instr_insert_after_cf_list(this->cf_node_list,
766 &store_instr->instr);
767 }
768
769 return;
770 }
771
772 struct hash_entry *entry =
773 _mesa_hash_table_search(this->overload_table, ir->callee);
774 assert(entry);
775 nir_function_overload *callee = (nir_function_overload *) entry->data;
776
777 nir_call_instr *instr = nir_call_instr_create(this->shader, callee);
778
779 unsigned i = 0;
780 foreach_in_list(ir_dereference, param, &ir->actual_parameters) {
781 instr->params[i] = evaluate_deref(&instr->instr, param);
782 i++;
783 }
784
785 instr->return_deref = evaluate_deref(&instr->instr, ir->return_deref);
786 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
787 }
788
789 void
790 nir_visitor::visit(ir_assignment *ir)
791 {
792 unsigned num_components = ir->lhs->type->vector_elements;
793
794 if ((ir->rhs->as_dereference() || ir->rhs->as_constant()) &&
795 (ir->write_mask == (1 << num_components) - 1 || ir->write_mask == 0)) {
796 /* We're doing a plain-as-can-be copy, so emit a copy_var */
797 nir_intrinsic_instr *copy =
798 nir_intrinsic_instr_create(this->shader, nir_intrinsic_copy_var);
799
800 copy->variables[0] = evaluate_deref(&copy->instr, ir->lhs);
801 copy->variables[1] = evaluate_deref(&copy->instr, ir->rhs);
802
803 if (ir->condition) {
804 nir_if *if_stmt = nir_if_create(this->shader);
805 if_stmt->condition = evaluate_rvalue(ir->condition);
806 nir_cf_node_insert_end(this->cf_node_list, &if_stmt->cf_node);
807 nir_instr_insert_after_cf_list(&if_stmt->then_list, &copy->instr);
808 } else {
809 nir_instr_insert_after_cf_list(this->cf_node_list, &copy->instr);
810 }
811 return;
812 }
813
814 assert(ir->rhs->type->is_scalar() || ir->rhs->type->is_vector());
815
816 ir->lhs->accept(this);
817 nir_deref_var *lhs_deref = this->deref_head;
818 nir_src src = evaluate_rvalue(ir->rhs);
819
820 if (ir->write_mask != (1 << num_components) - 1 && ir->write_mask != 0) {
821 /*
822 * We have no good way to update only part of a variable, so just load
823 * the LHS and do a vec operation to combine the old with the new, and
824 * then store it
825 * back into the LHS. Copy propagation should get rid of the mess.
826 */
827
828 nir_intrinsic_instr *load =
829 nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
830 load->num_components = ir->lhs->type->vector_elements;
831 nir_ssa_dest_init(&load->instr, &load->dest, num_components, NULL);
832 load->variables[0] = lhs_deref;
833 ralloc_steal(load, load->variables[0]);
834 nir_instr_insert_after_cf_list(this->cf_node_list, &load->instr);
835
836 nir_op vec_op;
837 switch (ir->lhs->type->vector_elements) {
838 case 1: vec_op = nir_op_imov; break;
839 case 2: vec_op = nir_op_vec2; break;
840 case 3: vec_op = nir_op_vec3; break;
841 case 4: vec_op = nir_op_vec4; break;
842 default: unreachable("Invalid number of components"); break;
843 }
844 nir_alu_instr *vec = nir_alu_instr_create(this->shader, vec_op);
845 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components, NULL);
846 vec->dest.write_mask = (1 << num_components) - 1;
847
848 unsigned component = 0;
849 for (unsigned i = 0; i < ir->lhs->type->vector_elements; i++) {
850 if (ir->write_mask & (1 << i)) {
851 vec->src[i].src = src;
852
853 /* GLSL IR will give us the input to the write-masked assignment
854 * in a single packed vector. So, for example, if the
855 * writemask is xzw, then we have to swizzle x -> x, y -> z,
856 * and z -> w and get the y component from the load.
857 */
858 vec->src[i].swizzle[0] = component++;
859 } else {
860 vec->src[i].src.is_ssa = true;
861 vec->src[i].src.ssa = &load->dest.ssa;
862 vec->src[i].swizzle[0] = i;
863 }
864 }
865
866 nir_instr_insert_after_cf_list(this->cf_node_list, &vec->instr);
867
868 src.is_ssa = true;
869 src.ssa = &vec->dest.dest.ssa;
870 }
871
872 nir_intrinsic_instr *store =
873 nir_intrinsic_instr_create(this->shader, nir_intrinsic_store_var);
874 store->num_components = ir->lhs->type->vector_elements;
875 nir_deref *store_deref = nir_copy_deref(store, &lhs_deref->deref);
876 store->variables[0] = nir_deref_as_var(store_deref);
877 store->src[0] = src;
878
879 if (ir->condition) {
880 nir_if *if_stmt = nir_if_create(this->shader);
881 if_stmt->condition = evaluate_rvalue(ir->condition);
882 nir_cf_node_insert_end(this->cf_node_list, &if_stmt->cf_node);
883 nir_instr_insert_after_cf_list(&if_stmt->then_list, &store->instr);
884 } else {
885 nir_instr_insert_after_cf_list(this->cf_node_list, &store->instr);
886 }
887 }
888
889 /*
890 * Given an instruction, returns a pointer to its destination or NULL if there
891 * is no destination.
892 *
893 * Note that this only handles instructions we generate at this level.
894 */
895 static nir_dest *
896 get_instr_dest(nir_instr *instr)
897 {
898 nir_alu_instr *alu_instr;
899 nir_intrinsic_instr *intrinsic_instr;
900 nir_tex_instr *tex_instr;
901
902 switch (instr->type) {
903 case nir_instr_type_alu:
904 alu_instr = nir_instr_as_alu(instr);
905 return &alu_instr->dest.dest;
906
907 case nir_instr_type_intrinsic:
908 intrinsic_instr = nir_instr_as_intrinsic(instr);
909 if (nir_intrinsic_infos[intrinsic_instr->intrinsic].has_dest)
910 return &intrinsic_instr->dest;
911 else
912 return NULL;
913
914 case nir_instr_type_tex:
915 tex_instr = nir_instr_as_tex(instr);
916 return &tex_instr->dest;
917
918 default:
919 unreachable("not reached");
920 }
921
922 return NULL;
923 }
924
925 void
926 nir_visitor::add_instr(nir_instr *instr, unsigned num_components)
927 {
928 nir_dest *dest = get_instr_dest(instr);
929
930 nir_ssa_dest_init(instr, dest, num_components, NULL);
931
932 nir_instr_insert_after_cf_list(this->cf_node_list, instr);
933 this->result = instr;
934 }
935
936 nir_src
937 nir_visitor::evaluate_rvalue(ir_rvalue* ir)
938 {
939 ir->accept(this);
940 if (ir->as_dereference() || ir->as_constant()) {
941 /*
942 * A dereference is being used on the right hand side, which means we
943 * must emit a variable load.
944 */
945
946 nir_intrinsic_instr *load_instr =
947 nir_intrinsic_instr_create(this->shader, nir_intrinsic_load_var);
948 load_instr->num_components = ir->type->vector_elements;
949 load_instr->variables[0] = this->deref_head;
950 ralloc_steal(load_instr, load_instr->variables[0]);
951 add_instr(&load_instr->instr, ir->type->vector_elements);
952 }
953
954 nir_dest *dest = get_instr_dest(this->result);
955 assert(dest->is_ssa);
956
957 return nir_src_for_ssa(&dest->ssa);
958 }
959
960 nir_alu_instr *
961 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src *srcs)
962 {
963 nir_alu_instr *instr = nir_alu_instr_create(this->shader, op);
964 for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++)
965 instr->src[i].src = srcs[i];
966 instr->dest.write_mask = (1 << dest_size) - 1;
967 add_instr(&instr->instr, dest_size);
968 return instr;
969 }
970
971 nir_alu_instr *
972 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1)
973 {
974 assert(nir_op_infos[op].num_inputs == 1);
975 return emit(op, dest_size, &src1);
976 }
977
978 nir_alu_instr *
979 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1,
980 nir_src src2)
981 {
982 assert(nir_op_infos[op].num_inputs == 2);
983 nir_src srcs[] = { src1, src2 };
984 return emit(op, dest_size, srcs);
985 }
986
987 nir_alu_instr *
988 nir_visitor::emit(nir_op op, unsigned dest_size, nir_src src1,
989 nir_src src2, nir_src src3)
990 {
991 assert(nir_op_infos[op].num_inputs == 3);
992 nir_src srcs[] = { src1, src2, src3 };
993 return emit(op, dest_size, srcs);
994 }
995
996 void
997 nir_visitor::visit(ir_expression *ir)
998 {
999 /* Some special cases */
1000 switch (ir->operation) {
1001 case ir_binop_ubo_load: {
1002 ir_constant *const_index = ir->operands[1]->as_constant();
1003
1004 nir_intrinsic_op op;
1005 if (const_index) {
1006 op = nir_intrinsic_load_ubo;
1007 } else {
1008 op = nir_intrinsic_load_ubo_indirect;
1009 }
1010 nir_intrinsic_instr *load = nir_intrinsic_instr_create(this->shader, op);
1011 load->num_components = ir->type->vector_elements;
1012 load->const_index[0] = const_index ? const_index->value.u[0] : 0; /* base offset */
1013 load->src[0] = evaluate_rvalue(ir->operands[0]);
1014 if (!const_index)
1015 load->src[1] = evaluate_rvalue(ir->operands[1]);
1016 add_instr(&load->instr, ir->type->vector_elements);
1017
1018 /*
1019 * In UBO's, a true boolean value is any non-zero value, but we consider
1020 * a true boolean to be ~0. Fix this up with a != 0 comparison.
1021 */
1022
1023 if (ir->type->base_type == GLSL_TYPE_BOOL) {
1024 nir_load_const_instr *const_zero = nir_load_const_instr_create(shader, 1);
1025 const_zero->value.u[0] = 0;
1026 nir_instr_insert_after_cf_list(this->cf_node_list, &const_zero->instr);
1027
1028 nir_alu_instr *compare = nir_alu_instr_create(shader, nir_op_ine);
1029 compare->src[0].src.is_ssa = true;
1030 compare->src[0].src.ssa = &load->dest.ssa;
1031 compare->src[1].src.is_ssa = true;
1032 compare->src[1].src.ssa = &const_zero->def;
1033 for (unsigned i = 0; i < ir->type->vector_elements; i++)
1034 compare->src[1].swizzle[i] = 0;
1035 compare->dest.write_mask = (1 << ir->type->vector_elements) - 1;
1036
1037 add_instr(&compare->instr, ir->type->vector_elements);
1038 }
1039
1040 return;
1041 }
1042
1043 case ir_unop_interpolate_at_centroid:
1044 case ir_binop_interpolate_at_offset:
1045 case ir_binop_interpolate_at_sample: {
1046 ir_dereference *deref = ir->operands[0]->as_dereference();
1047 ir_swizzle *swizzle = NULL;
1048 if (!deref) {
1049 /* the api does not allow a swizzle here, but the varying packing code
1050 * may have pushed one into here.
1051 */
1052 swizzle = ir->operands[0]->as_swizzle();
1053 assert(swizzle);
1054 deref = swizzle->val->as_dereference();
1055 assert(deref);
1056 }
1057
1058 deref->accept(this);
1059
1060 nir_intrinsic_op op;
1061 if (this->deref_head->var->data.mode == nir_var_shader_in) {
1062 switch (ir->operation) {
1063 case ir_unop_interpolate_at_centroid:
1064 op = nir_intrinsic_interp_var_at_centroid;
1065 break;
1066 case ir_binop_interpolate_at_offset:
1067 op = nir_intrinsic_interp_var_at_offset;
1068 break;
1069 case ir_binop_interpolate_at_sample:
1070 op = nir_intrinsic_interp_var_at_sample;
1071 break;
1072 default:
1073 unreachable("Invalid interpolation intrinsic");
1074 }
1075 } else {
1076 /* This case can happen if the vertex shader does not write the
1077 * given varying. In this case, the linker will lower it to a
1078 * global variable. Since interpolating a variable makes no
1079 * sense, we'll just turn it into a load which will probably
1080 * eventually end up as an SSA definition.
1081 */
1082 assert(this->deref_head->var->data.mode == nir_var_global);
1083 op = nir_intrinsic_load_var;
1084 }
1085
1086 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(shader, op);
1087 intrin->num_components = deref->type->vector_elements;
1088 intrin->variables[0] = this->deref_head;
1089 ralloc_steal(intrin, intrin->variables[0]);
1090
1091 if (intrin->intrinsic == nir_intrinsic_interp_var_at_offset ||
1092 intrin->intrinsic == nir_intrinsic_interp_var_at_sample)
1093 intrin->src[0] = evaluate_rvalue(ir->operands[1]);
1094
1095 add_instr(&intrin->instr, deref->type->vector_elements);
1096
1097 if (swizzle) {
1098 nir_alu_instr *mov = nir_alu_instr_create(shader, nir_op_imov);
1099 mov->dest.write_mask = (1 << swizzle->type->vector_elements) - 1;
1100 mov->src[0].src.is_ssa = true;
1101 mov->src[0].src.ssa = &intrin->dest.ssa;
1102
1103 mov->src[0].swizzle[0] = swizzle->mask.x;
1104 mov->src[0].swizzle[1] = swizzle->mask.y;
1105 mov->src[0].swizzle[2] = swizzle->mask.z;
1106 mov->src[0].swizzle[3] = swizzle->mask.w;
1107 for (unsigned i = deref->type->vector_elements; i < 4; i++)
1108 mov->src[0].swizzle[i] = 0;
1109
1110 add_instr(&mov->instr, swizzle->type->vector_elements);
1111 }
1112
1113 return;
1114 }
1115
1116 default:
1117 break;
1118 }
1119
1120 nir_src srcs[4];
1121 for (unsigned i = 0; i < ir->get_num_operands(); i++)
1122 srcs[i] = evaluate_rvalue(ir->operands[i]);
1123
1124 glsl_base_type types[4];
1125 for (unsigned i = 0; i < ir->get_num_operands(); i++)
1126 if (supports_ints)
1127 types[i] = ir->operands[i]->type->base_type;
1128 else
1129 types[i] = GLSL_TYPE_FLOAT;
1130
1131 glsl_base_type out_type;
1132 if (supports_ints)
1133 out_type = ir->type->base_type;
1134 else
1135 out_type = GLSL_TYPE_FLOAT;
1136
1137 unsigned dest_size = ir->type->vector_elements;
1138
1139 nir_alu_instr *instr;
1140 nir_op op;
1141
1142 switch (ir->operation) {
1143 case ir_unop_bit_not: emit(nir_op_inot, dest_size, srcs); break;
1144 case ir_unop_logic_not:
1145 emit(supports_ints ? nir_op_inot : nir_op_fnot, dest_size, srcs);
1146 break;
1147 case ir_unop_neg:
1148 instr = emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fneg : nir_op_ineg,
1149 dest_size, srcs);
1150 break;
1151 case ir_unop_abs:
1152 instr = emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fabs : nir_op_iabs,
1153 dest_size, srcs);
1154 break;
1155 case ir_unop_saturate:
1156 assert(types[0] == GLSL_TYPE_FLOAT);
1157 instr = emit(nir_op_fsat, dest_size, srcs);
1158 break;
1159 case ir_unop_sign:
1160 emit(types[0] == GLSL_TYPE_FLOAT ? nir_op_fsign : nir_op_isign,
1161 dest_size, srcs);
1162 break;
1163 case ir_unop_rcp: emit(nir_op_frcp, dest_size, srcs); break;
1164 case ir_unop_rsq: emit(nir_op_frsq, dest_size, srcs); break;
1165 case ir_unop_sqrt: emit(nir_op_fsqrt, dest_size, srcs); break;
1166 case ir_unop_exp: unreachable("ir_unop_exp should have been lowered");
1167 case ir_unop_log: unreachable("ir_unop_log should have been lowered");
1168 case ir_unop_exp2: emit(nir_op_fexp2, dest_size, srcs); break;
1169 case ir_unop_log2: emit(nir_op_flog2, dest_size, srcs); break;
1170 case ir_unop_i2f:
1171 emit(supports_ints ? nir_op_i2f : nir_op_fmov, dest_size, srcs);
1172 break;
1173 case ir_unop_u2f:
1174 emit(supports_ints ? nir_op_u2f : nir_op_fmov, dest_size, srcs);
1175 break;
1176 case ir_unop_b2f:
1177 emit(supports_ints ? nir_op_b2f : nir_op_fmov, dest_size, srcs);
1178 break;
1179 case ir_unop_f2i: emit(nir_op_f2i, dest_size, srcs); break;
1180 case ir_unop_f2u: emit(nir_op_f2u, dest_size, srcs); break;
1181 case ir_unop_f2b: emit(nir_op_f2b, dest_size, srcs); break;
1182 case ir_unop_i2b: emit(nir_op_i2b, dest_size, srcs); break;
1183 case ir_unop_b2i: emit(nir_op_b2i, dest_size, srcs); break;
1184 case ir_unop_i2u:
1185 case ir_unop_u2i:
1186 case ir_unop_bitcast_i2f:
1187 case ir_unop_bitcast_f2i:
1188 case ir_unop_bitcast_u2f:
1189 case ir_unop_bitcast_f2u:
1190 case ir_unop_subroutine_to_int:
1191 /* no-op */
1192 emit(nir_op_imov, dest_size, srcs);
1193 break;
1194 case ir_unop_any:
1195 switch (ir->operands[0]->type->vector_elements) {
1196 case 2:
1197 emit(supports_ints ? nir_op_bany2 : nir_op_fany2,
1198 dest_size, srcs);
1199 break;
1200 case 3:
1201 emit(supports_ints ? nir_op_bany3 : nir_op_fany3,
1202 dest_size, srcs);
1203 break;
1204 case 4:
1205 emit(supports_ints ? nir_op_bany4 : nir_op_fany4,
1206 dest_size, srcs);
1207 break;
1208 default:
1209 unreachable("not reached");
1210 }
1211 break;
1212 case ir_unop_trunc: emit(nir_op_ftrunc, dest_size, srcs); break;
1213 case ir_unop_ceil: emit(nir_op_fceil, dest_size, srcs); break;
1214 case ir_unop_floor: emit(nir_op_ffloor, dest_size, srcs); break;
1215 case ir_unop_fract: emit(nir_op_ffract, dest_size, srcs); break;
1216 case ir_unop_round_even: emit(nir_op_fround_even, dest_size, srcs); break;
1217 case ir_unop_sin: emit(nir_op_fsin, dest_size, srcs); break;
1218 case ir_unop_cos: emit(nir_op_fcos, dest_size, srcs); break;
1219 case ir_unop_dFdx: emit(nir_op_fddx, dest_size, srcs); break;
1220 case ir_unop_dFdy: emit(nir_op_fddy, dest_size, srcs); break;
1221 case ir_unop_dFdx_fine: emit(nir_op_fddx_fine, dest_size, srcs); break;
1222 case ir_unop_dFdy_fine: emit(nir_op_fddy_fine, dest_size, srcs); break;
1223 case ir_unop_dFdx_coarse: emit(nir_op_fddx_coarse, dest_size, srcs); break;
1224 case ir_unop_dFdy_coarse: emit(nir_op_fddy_coarse, dest_size, srcs); break;
1225 case ir_unop_pack_snorm_2x16:
1226 emit(nir_op_pack_snorm_2x16, dest_size, srcs);
1227 break;
1228 case ir_unop_pack_snorm_4x8:
1229 emit(nir_op_pack_snorm_4x8, dest_size, srcs);
1230 break;
1231 case ir_unop_pack_unorm_2x16:
1232 emit(nir_op_pack_unorm_2x16, dest_size, srcs);
1233 break;
1234 case ir_unop_pack_unorm_4x8:
1235 emit(nir_op_pack_unorm_4x8, dest_size, srcs);
1236 break;
1237 case ir_unop_pack_half_2x16:
1238 emit(nir_op_pack_half_2x16, dest_size, srcs);
1239 break;
1240 case ir_unop_unpack_snorm_2x16:
1241 emit(nir_op_unpack_snorm_2x16, dest_size, srcs);
1242 break;
1243 case ir_unop_unpack_snorm_4x8:
1244 emit(nir_op_unpack_snorm_4x8, dest_size, srcs);
1245 break;
1246 case ir_unop_unpack_unorm_2x16:
1247 emit(nir_op_unpack_unorm_2x16, dest_size, srcs);
1248 break;
1249 case ir_unop_unpack_unorm_4x8:
1250 emit(nir_op_unpack_unorm_4x8, dest_size, srcs);
1251 break;
1252 case ir_unop_unpack_half_2x16:
1253 emit(nir_op_unpack_half_2x16, dest_size, srcs);
1254 break;
1255 case ir_unop_unpack_half_2x16_split_x:
1256 emit(nir_op_unpack_half_2x16_split_x, dest_size, srcs);
1257 break;
1258 case ir_unop_unpack_half_2x16_split_y:
1259 emit(nir_op_unpack_half_2x16_split_y, dest_size, srcs);
1260 break;
1261 case ir_unop_bitfield_reverse:
1262 emit(nir_op_bitfield_reverse, dest_size, srcs);
1263 break;
1264 case ir_unop_bit_count:
1265 emit(nir_op_bit_count, dest_size, srcs);
1266 break;
1267 case ir_unop_find_msb:
1268 switch (types[0]) {
1269 case GLSL_TYPE_UINT:
1270 emit(nir_op_ufind_msb, dest_size, srcs);
1271 break;
1272 case GLSL_TYPE_INT:
1273 emit(nir_op_ifind_msb, dest_size, srcs);
1274 break;
1275 default:
1276 unreachable("Invalid type for findMSB()");
1277 }
1278 break;
1279 case ir_unop_find_lsb:
1280 emit(nir_op_find_lsb, dest_size, srcs);
1281 break;
1282
1283 case ir_unop_noise:
1284 switch (ir->type->vector_elements) {
1285 case 1:
1286 switch (ir->operands[0]->type->vector_elements) {
1287 case 1: emit(nir_op_fnoise1_1, dest_size, srcs); break;
1288 case 2: emit(nir_op_fnoise1_2, dest_size, srcs); break;
1289 case 3: emit(nir_op_fnoise1_3, dest_size, srcs); break;
1290 case 4: emit(nir_op_fnoise1_4, dest_size, srcs); break;
1291 default: unreachable("not reached");
1292 }
1293 break;
1294 case 2:
1295 switch (ir->operands[0]->type->vector_elements) {
1296 case 1: emit(nir_op_fnoise2_1, dest_size, srcs); break;
1297 case 2: emit(nir_op_fnoise2_2, dest_size, srcs); break;
1298 case 3: emit(nir_op_fnoise2_3, dest_size, srcs); break;
1299 case 4: emit(nir_op_fnoise2_4, dest_size, srcs); break;
1300 default: unreachable("not reached");
1301 }
1302 break;
1303 case 3:
1304 switch (ir->operands[0]->type->vector_elements) {
1305 case 1: emit(nir_op_fnoise3_1, dest_size, srcs); break;
1306 case 2: emit(nir_op_fnoise3_2, dest_size, srcs); break;
1307 case 3: emit(nir_op_fnoise3_3, dest_size, srcs); break;
1308 case 4: emit(nir_op_fnoise3_4, dest_size, srcs); break;
1309 default: unreachable("not reached");
1310 }
1311 break;
1312 case 4:
1313 switch (ir->operands[0]->type->vector_elements) {
1314 case 1: emit(nir_op_fnoise4_1, dest_size, srcs); break;
1315 case 2: emit(nir_op_fnoise4_2, dest_size, srcs); break;
1316 case 3: emit(nir_op_fnoise4_3, dest_size, srcs); break;
1317 case 4: emit(nir_op_fnoise4_4, dest_size, srcs); break;
1318 default: unreachable("not reached");
1319 }
1320 break;
1321 default:
1322 unreachable("not reached");
1323 }
1324 break;
1325 case ir_binop_add:
1326 case ir_binop_sub:
1327 case ir_binop_mul:
1328 case ir_binop_div:
1329 case ir_binop_mod:
1330 case ir_binop_min:
1331 case ir_binop_max:
1332 case ir_binop_pow:
1333 case ir_binop_bit_and:
1334 case ir_binop_bit_or:
1335 case ir_binop_bit_xor:
1336 case ir_binop_logic_and:
1337 case ir_binop_logic_or:
1338 case ir_binop_logic_xor:
1339 case ir_binop_lshift:
1340 case ir_binop_rshift:
1341 switch (ir->operation) {
1342 case ir_binop_add:
1343 if (out_type == GLSL_TYPE_FLOAT)
1344 op = nir_op_fadd;
1345 else
1346 op = nir_op_iadd;
1347 break;
1348 case ir_binop_sub:
1349 if (out_type == GLSL_TYPE_FLOAT)
1350 op = nir_op_fsub;
1351 else
1352 op = nir_op_isub;
1353 break;
1354 case ir_binop_mul:
1355 if (out_type == GLSL_TYPE_FLOAT)
1356 op = nir_op_fmul;
1357 else
1358 op = nir_op_imul;
1359 break;
1360 case ir_binop_div:
1361 if (out_type == GLSL_TYPE_FLOAT)
1362 op = nir_op_fdiv;
1363 else if (out_type == GLSL_TYPE_INT)
1364 op = nir_op_idiv;
1365 else
1366 op = nir_op_udiv;
1367 break;
1368 case ir_binop_mod:
1369 if (out_type == GLSL_TYPE_FLOAT)
1370 op = nir_op_fmod;
1371 else
1372 op = nir_op_umod;
1373 break;
1374 case ir_binop_min:
1375 if (out_type == GLSL_TYPE_FLOAT)
1376 op = nir_op_fmin;
1377 else if (out_type == GLSL_TYPE_INT)
1378 op = nir_op_imin;
1379 else
1380 op = nir_op_umin;
1381 break;
1382 case ir_binop_max:
1383 if (out_type == GLSL_TYPE_FLOAT)
1384 op = nir_op_fmax;
1385 else if (out_type == GLSL_TYPE_INT)
1386 op = nir_op_imax;
1387 else
1388 op = nir_op_umax;
1389 break;
1390 case ir_binop_bit_and:
1391 op = nir_op_iand;
1392 break;
1393 case ir_binop_bit_or:
1394 op = nir_op_ior;
1395 break;
1396 case ir_binop_bit_xor:
1397 op = nir_op_ixor;
1398 break;
1399 case ir_binop_logic_and:
1400 if (supports_ints)
1401 op = nir_op_iand;
1402 else
1403 op = nir_op_fand;
1404 break;
1405 case ir_binop_logic_or:
1406 if (supports_ints)
1407 op = nir_op_ior;
1408 else
1409 op = nir_op_for;
1410 break;
1411 case ir_binop_logic_xor:
1412 if (supports_ints)
1413 op = nir_op_ixor;
1414 else
1415 op = nir_op_fxor;
1416 break;
1417 case ir_binop_lshift:
1418 op = nir_op_ishl;
1419 break;
1420 case ir_binop_rshift:
1421 if (out_type == GLSL_TYPE_INT)
1422 op = nir_op_ishr;
1423 else
1424 op = nir_op_ushr;
1425 break;
1426 case ir_binop_pow:
1427 op = nir_op_fpow;
1428 break;
1429
1430 default:
1431 unreachable("not reached");
1432 }
1433
1434 instr = emit(op, dest_size, srcs);
1435
1436 if (ir->operands[0]->type->vector_elements != 1 &&
1437 ir->operands[1]->type->vector_elements == 1) {
1438 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements;
1439 i++) {
1440 instr->src[1].swizzle[i] = 0;
1441 }
1442 }
1443
1444 if (ir->operands[1]->type->vector_elements != 1 &&
1445 ir->operands[0]->type->vector_elements == 1) {
1446 for (unsigned i = 0; i < ir->operands[1]->type->vector_elements;
1447 i++) {
1448 instr->src[0].swizzle[i] = 0;
1449 }
1450 }
1451
1452 break;
1453 case ir_binop_imul_high:
1454 emit(out_type == GLSL_TYPE_UINT ? nir_op_umul_high : nir_op_imul_high,
1455 dest_size, srcs);
1456 break;
1457 case ir_binop_carry: emit(nir_op_uadd_carry, dest_size, srcs); break;
1458 case ir_binop_borrow: emit(nir_op_usub_borrow, dest_size, srcs); break;
1459 case ir_binop_less:
1460 if (supports_ints) {
1461 if (types[0] == GLSL_TYPE_FLOAT)
1462 emit(nir_op_flt, dest_size, srcs);
1463 else if (types[0] == GLSL_TYPE_INT)
1464 emit(nir_op_ilt, dest_size, srcs);
1465 else
1466 emit(nir_op_ult, dest_size, srcs);
1467 } else {
1468 emit(nir_op_slt, dest_size, srcs);
1469 }
1470 break;
1471 case ir_binop_greater:
1472 if (supports_ints) {
1473 if (types[0] == GLSL_TYPE_FLOAT)
1474 emit(nir_op_flt, dest_size, srcs[1], srcs[0]);
1475 else if (types[0] == GLSL_TYPE_INT)
1476 emit(nir_op_ilt, dest_size, srcs[1], srcs[0]);
1477 else
1478 emit(nir_op_ult, dest_size, srcs[1], srcs[0]);
1479 } else {
1480 emit(nir_op_slt, dest_size, srcs[1], srcs[0]);
1481 }
1482 break;
1483 case ir_binop_lequal:
1484 if (supports_ints) {
1485 if (types[0] == GLSL_TYPE_FLOAT)
1486 emit(nir_op_fge, dest_size, srcs[1], srcs[0]);
1487 else if (types[0] == GLSL_TYPE_INT)
1488 emit(nir_op_ige, dest_size, srcs[1], srcs[0]);
1489 else
1490 emit(nir_op_uge, dest_size, srcs[1], srcs[0]);
1491 } else {
1492 emit(nir_op_slt, dest_size, srcs[1], srcs[0]);
1493 }
1494 break;
1495 case ir_binop_gequal:
1496 if (supports_ints) {
1497 if (types[0] == GLSL_TYPE_FLOAT)
1498 emit(nir_op_fge, dest_size, srcs);
1499 else if (types[0] == GLSL_TYPE_INT)
1500 emit(nir_op_ige, dest_size, srcs);
1501 else
1502 emit(nir_op_uge, dest_size, srcs);
1503 } else {
1504 emit(nir_op_slt, dest_size, srcs);
1505 }
1506 break;
1507 case ir_binop_equal:
1508 if (supports_ints) {
1509 if (types[0] == GLSL_TYPE_FLOAT)
1510 emit(nir_op_feq, dest_size, srcs);
1511 else
1512 emit(nir_op_ieq, dest_size, srcs);
1513 } else {
1514 emit(nir_op_seq, dest_size, srcs);
1515 }
1516 break;
1517 case ir_binop_nequal:
1518 if (supports_ints) {
1519 if (types[0] == GLSL_TYPE_FLOAT)
1520 emit(nir_op_fne, dest_size, srcs);
1521 else
1522 emit(nir_op_ine, dest_size, srcs);
1523 } else {
1524 emit(nir_op_sne, dest_size, srcs);
1525 }
1526 break;
1527 case ir_binop_all_equal:
1528 if (supports_ints) {
1529 if (types[0] == GLSL_TYPE_FLOAT) {
1530 switch (ir->operands[0]->type->vector_elements) {
1531 case 1: emit(nir_op_feq, dest_size, srcs); break;
1532 case 2: emit(nir_op_ball_fequal2, dest_size, srcs); break;
1533 case 3: emit(nir_op_ball_fequal3, dest_size, srcs); break;
1534 case 4: emit(nir_op_ball_fequal4, dest_size, srcs); break;
1535 default:
1536 unreachable("not reached");
1537 }
1538 } else {
1539 switch (ir->operands[0]->type->vector_elements) {
1540 case 1: emit(nir_op_ieq, dest_size, srcs); break;
1541 case 2: emit(nir_op_ball_iequal2, dest_size, srcs); break;
1542 case 3: emit(nir_op_ball_iequal3, dest_size, srcs); break;
1543 case 4: emit(nir_op_ball_iequal4, dest_size, srcs); break;
1544 default:
1545 unreachable("not reached");
1546 }
1547 }
1548 } else {
1549 switch (ir->operands[0]->type->vector_elements) {
1550 case 1: emit(nir_op_seq, dest_size, srcs); break;
1551 case 2: emit(nir_op_fall_equal2, dest_size, srcs); break;
1552 case 3: emit(nir_op_fall_equal3, dest_size, srcs); break;
1553 case 4: emit(nir_op_fall_equal4, dest_size, srcs); break;
1554 default:
1555 unreachable("not reached");
1556 }
1557 }
1558 break;
1559 case ir_binop_any_nequal:
1560 if (supports_ints) {
1561 if (types[0] == GLSL_TYPE_FLOAT) {
1562 switch (ir->operands[0]->type->vector_elements) {
1563 case 1: emit(nir_op_fne, dest_size, srcs); break;
1564 case 2: emit(nir_op_bany_fnequal2, dest_size, srcs); break;
1565 case 3: emit(nir_op_bany_fnequal3, dest_size, srcs); break;
1566 case 4: emit(nir_op_bany_fnequal4, dest_size, srcs); break;
1567 default:
1568 unreachable("not reached");
1569 }
1570 } else {
1571 switch (ir->operands[0]->type->vector_elements) {
1572 case 1: emit(nir_op_ine, dest_size, srcs); break;
1573 case 2: emit(nir_op_bany_inequal2, dest_size, srcs); break;
1574 case 3: emit(nir_op_bany_inequal3, dest_size, srcs); break;
1575 case 4: emit(nir_op_bany_inequal4, dest_size, srcs); break;
1576 default:
1577 unreachable("not reached");
1578 }
1579 }
1580 } else {
1581 switch (ir->operands[0]->type->vector_elements) {
1582 case 1: emit(nir_op_sne, dest_size, srcs); break;
1583 case 2: emit(nir_op_fany_nequal2, dest_size, srcs); break;
1584 case 3: emit(nir_op_fany_nequal3, dest_size, srcs); break;
1585 case 4: emit(nir_op_fany_nequal4, dest_size, srcs); break;
1586 default:
1587 unreachable("not reached");
1588 }
1589 }
1590 break;
1591 case ir_binop_dot:
1592 switch (ir->operands[0]->type->vector_elements) {
1593 case 2: emit(nir_op_fdot2, dest_size, srcs); break;
1594 case 3: emit(nir_op_fdot3, dest_size, srcs); break;
1595 case 4: emit(nir_op_fdot4, dest_size, srcs); break;
1596 default:
1597 unreachable("not reached");
1598 }
1599 break;
1600
1601 case ir_binop_pack_half_2x16_split:
1602 emit(nir_op_pack_half_2x16_split, dest_size, srcs);
1603 break;
1604 case ir_binop_bfm: emit(nir_op_bfm, dest_size, srcs); break;
1605 case ir_binop_ldexp: emit(nir_op_ldexp, dest_size, srcs); break;
1606 case ir_triop_fma: emit(nir_op_ffma, dest_size, srcs); break;
1607 case ir_triop_lrp:
1608 instr = emit(nir_op_flrp, dest_size, srcs);
1609 if (ir->operands[0]->type->vector_elements != 1 &&
1610 ir->operands[2]->type->vector_elements == 1) {
1611 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements;
1612 i++) {
1613 instr->src[2].swizzle[i] = 0;
1614 }
1615 }
1616 break;
1617 case ir_triop_csel:
1618 if (supports_ints)
1619 emit(nir_op_bcsel, dest_size, srcs);
1620 else
1621 emit(nir_op_fcsel, dest_size, srcs);
1622 break;
1623 case ir_triop_bfi:
1624 instr = emit(nir_op_bfi, dest_size, srcs);
1625 for (unsigned i = 0; i < ir->operands[1]->type->vector_elements; i++) {
1626 instr->src[0].swizzle[i] = 0;
1627 }
1628 break;
1629 case ir_triop_bitfield_extract:
1630 instr = emit(out_type == GLSL_TYPE_INT ? nir_op_ibitfield_extract :
1631 nir_op_ubitfield_extract, dest_size, srcs);
1632 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements; i++) {
1633 instr->src[1].swizzle[i] = 0;
1634 instr->src[2].swizzle[i] = 0;
1635 }
1636 break;
1637 case ir_quadop_bitfield_insert:
1638 instr = emit(nir_op_bitfield_insert, dest_size, srcs);
1639 for (unsigned i = 0; i < ir->operands[0]->type->vector_elements; i++) {
1640 instr->src[2].swizzle[i] = 0;
1641 instr->src[3].swizzle[i] = 0;
1642 }
1643 break;
1644 case ir_quadop_vector:
1645 switch (ir->type->vector_elements) {
1646 case 2: emit(nir_op_vec2, dest_size, srcs); break;
1647 case 3: emit(nir_op_vec3, dest_size, srcs); break;
1648 case 4: emit(nir_op_vec4, dest_size, srcs); break;
1649 default: unreachable("not reached");
1650 }
1651 break;
1652
1653 default:
1654 unreachable("not reached");
1655 }
1656 }
1657
1658 void
1659 nir_visitor::visit(ir_swizzle *ir)
1660 {
1661 nir_alu_instr *instr = emit(supports_ints ? nir_op_imov : nir_op_fmov,
1662 ir->type->vector_elements,
1663 evaluate_rvalue(ir->val));
1664
1665 unsigned swizzle[4] = { ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w };
1666 for (unsigned i = 0; i < ir->type->vector_elements; i++)
1667 instr->src[0].swizzle[i] = swizzle[i];
1668 }
1669
1670 void
1671 nir_visitor::visit(ir_texture *ir)
1672 {
1673 unsigned num_srcs;
1674 nir_texop op;
1675 switch (ir->op) {
1676 case ir_tex:
1677 op = nir_texop_tex;
1678 num_srcs = 1; /* coordinate */
1679 break;
1680
1681 case ir_txb:
1682 case ir_txl:
1683 op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
1684 num_srcs = 2; /* coordinate, bias/lod */
1685 break;
1686
1687 case ir_txd:
1688 op = nir_texop_txd; /* coordinate, dPdx, dPdy */
1689 num_srcs = 3;
1690 break;
1691
1692 case ir_txf:
1693 op = nir_texop_txf;
1694 if (ir->lod_info.lod != NULL)
1695 num_srcs = 2; /* coordinate, lod */
1696 else
1697 num_srcs = 1; /* coordinate */
1698 break;
1699
1700 case ir_txf_ms:
1701 op = nir_texop_txf_ms;
1702 num_srcs = 2; /* coordinate, sample_index */
1703 break;
1704
1705 case ir_txs:
1706 op = nir_texop_txs;
1707 if (ir->lod_info.lod != NULL)
1708 num_srcs = 1; /* lod */
1709 else
1710 num_srcs = 0;
1711 break;
1712
1713 case ir_lod:
1714 op = nir_texop_lod;
1715 num_srcs = 1; /* coordinate */
1716 break;
1717
1718 case ir_tg4:
1719 op = nir_texop_tg4;
1720 num_srcs = 1; /* coordinate */
1721 break;
1722
1723 case ir_query_levels:
1724 op = nir_texop_query_levels;
1725 num_srcs = 0;
1726 break;
1727
1728 case ir_texture_samples:
1729 op = nir_texop_texture_samples;
1730 num_srcs = 0;
1731 break;
1732
1733 default:
1734 unreachable("not reached");
1735 }
1736
1737 if (ir->projector != NULL)
1738 num_srcs++;
1739 if (ir->shadow_comparitor != NULL)
1740 num_srcs++;
1741 if (ir->offset != NULL && ir->offset->as_constant() == NULL)
1742 num_srcs++;
1743
1744 nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
1745
1746 instr->op = op;
1747 instr->sampler_dim =
1748 (glsl_sampler_dim) ir->sampler->type->sampler_dimensionality;
1749 instr->is_array = ir->sampler->type->sampler_array;
1750 instr->is_shadow = ir->sampler->type->sampler_shadow;
1751 if (instr->is_shadow)
1752 instr->is_new_style_shadow = (ir->type->vector_elements == 1);
1753 switch (ir->type->base_type) {
1754 case GLSL_TYPE_FLOAT:
1755 instr->dest_type = nir_type_float;
1756 break;
1757 case GLSL_TYPE_INT:
1758 instr->dest_type = nir_type_int;
1759 break;
1760 case GLSL_TYPE_UINT:
1761 instr->dest_type = nir_type_unsigned;
1762 break;
1763 default:
1764 unreachable("not reached");
1765 }
1766
1767 instr->sampler = evaluate_deref(&instr->instr, ir->sampler);
1768
1769 unsigned src_number = 0;
1770
1771 if (ir->coordinate != NULL) {
1772 instr->coord_components = ir->coordinate->type->vector_elements;
1773 instr->src[src_number].src = evaluate_rvalue(ir->coordinate);
1774 instr->src[src_number].src_type = nir_tex_src_coord;
1775 src_number++;
1776 }
1777
1778 if (ir->projector != NULL) {
1779 instr->src[src_number].src = evaluate_rvalue(ir->projector);
1780 instr->src[src_number].src_type = nir_tex_src_projector;
1781 src_number++;
1782 }
1783
1784 if (ir->shadow_comparitor != NULL) {
1785 instr->src[src_number].src = evaluate_rvalue(ir->shadow_comparitor);
1786 instr->src[src_number].src_type = nir_tex_src_comparitor;
1787 src_number++;
1788 }
1789
1790 if (ir->offset != NULL) {
1791 /* we don't support multiple offsets yet */
1792 assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
1793
1794 ir_constant *const_offset = ir->offset->as_constant();
1795 if (const_offset != NULL) {
1796 for (unsigned i = 0; i < const_offset->type->vector_elements; i++)
1797 instr->const_offset[i] = const_offset->value.i[i];
1798 } else {
1799 instr->src[src_number].src = evaluate_rvalue(ir->offset);
1800 instr->src[src_number].src_type = nir_tex_src_offset;
1801 src_number++;
1802 }
1803 }
1804
1805 switch (ir->op) {
1806 case ir_txb:
1807 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.bias);
1808 instr->src[src_number].src_type = nir_tex_src_bias;
1809 src_number++;
1810 break;
1811
1812 case ir_txl:
1813 case ir_txf:
1814 case ir_txs:
1815 if (ir->lod_info.lod != NULL) {
1816 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.lod);
1817 instr->src[src_number].src_type = nir_tex_src_lod;
1818 src_number++;
1819 }
1820 break;
1821
1822 case ir_txd:
1823 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdx);
1824 instr->src[src_number].src_type = nir_tex_src_ddx;
1825 src_number++;
1826 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.grad.dPdy);
1827 instr->src[src_number].src_type = nir_tex_src_ddy;
1828 src_number++;
1829 break;
1830
1831 case ir_txf_ms:
1832 instr->src[src_number].src = evaluate_rvalue(ir->lod_info.sample_index);
1833 instr->src[src_number].src_type = nir_tex_src_ms_index;
1834 src_number++;
1835 break;
1836
1837 case ir_tg4:
1838 instr->component = ir->lod_info.component->as_constant()->value.u[0];
1839 break;
1840
1841 default:
1842 break;
1843 }
1844
1845 assert(src_number == num_srcs);
1846
1847 add_instr(&instr->instr, nir_tex_instr_dest_size(instr));
1848 }
1849
1850 void
1851 nir_visitor::visit(ir_constant *ir)
1852 {
1853 /*
1854 * We don't know if this variable is an an array or struct that gets
1855 * dereferenced, so do the safe thing an make it a variable with a
1856 * constant initializer and return a dereference.
1857 */
1858
1859 nir_variable *var = ralloc(this->shader, nir_variable);
1860 var->name = ralloc_strdup(var, "const_temp");
1861 var->type = ir->type;
1862 var->data.mode = nir_var_local;
1863 var->data.read_only = true;
1864 var->constant_initializer = constant_copy(ir, var);
1865 exec_list_push_tail(&this->impl->locals, &var->node);
1866
1867 this->deref_head = nir_deref_var_create(this->shader, var);
1868 this->deref_tail = &this->deref_head->deref;
1869 }
1870
1871 void
1872 nir_visitor::visit(ir_dereference_variable *ir)
1873 {
1874 struct hash_entry *entry =
1875 _mesa_hash_table_search(this->var_table, ir->var);
1876 assert(entry);
1877 nir_variable *var = (nir_variable *) entry->data;
1878
1879 nir_deref_var *deref = nir_deref_var_create(this->shader, var);
1880 this->deref_head = deref;
1881 this->deref_tail = &deref->deref;
1882 }
1883
1884 void
1885 nir_visitor::visit(ir_dereference_record *ir)
1886 {
1887 ir->record->accept(this);
1888
1889 int field_index = this->deref_tail->type->field_index(ir->field);
1890 assert(field_index >= 0);
1891
1892 nir_deref_struct *deref = nir_deref_struct_create(this->deref_tail, field_index);
1893 deref->deref.type = ir->type;
1894 this->deref_tail->child = &deref->deref;
1895 this->deref_tail = &deref->deref;
1896 }
1897
1898 void
1899 nir_visitor::visit(ir_dereference_array *ir)
1900 {
1901 nir_deref_array *deref = nir_deref_array_create(this->shader);
1902 deref->deref.type = ir->type;
1903
1904 ir_constant *const_index = ir->array_index->as_constant();
1905 if (const_index != NULL) {
1906 deref->deref_array_type = nir_deref_array_type_direct;
1907 deref->base_offset = const_index->value.u[0];
1908 } else {
1909 deref->deref_array_type = nir_deref_array_type_indirect;
1910 deref->indirect = evaluate_rvalue(ir->array_index);
1911 }
1912
1913 ir->array->accept(this);
1914
1915 this->deref_tail->child = &deref->deref;
1916 ralloc_steal(this->deref_tail, deref);
1917 this->deref_tail = &deref->deref;
1918 }
1919
1920 void
1921 nir_visitor::visit(ir_barrier *ir)
1922 {
1923 nir_intrinsic_instr *instr =
1924 nir_intrinsic_instr_create(this->shader, nir_intrinsic_barrier);
1925 nir_instr_insert_after_cf_list(this->cf_node_list, &instr->instr);
1926 }