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