glsl_to_nir: remove dead code
[mesa.git] / src / compiler / glsl / 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 "float64_glsl.h"
29 #include "glsl_to_nir.h"
30 #include "ir_visitor.h"
31 #include "ir_hierarchical_visitor.h"
32 #include "ir.h"
33 #include "ir_optimization.h"
34 #include "program.h"
35 #include "compiler/nir/nir_control_flow.h"
36 #include "compiler/nir/nir_builder.h"
37 #include "compiler/nir/nir_builtin_builder.h"
38 #include "compiler/nir/nir_deref.h"
39 #include "main/errors.h"
40 #include "util/imports.h"
41 #include "main/mtypes.h"
42 #include "main/shaderobj.h"
43 #include "util/u_math.h"
44
45 /*
46 * pass to lower GLSL IR to NIR
47 *
48 * This will lower variable dereferences to loads/stores of corresponding
49 * variables in NIR - the variables will be converted to registers in a later
50 * pass.
51 */
52
53 namespace {
54
55 class nir_visitor : public ir_visitor
56 {
57 public:
58 nir_visitor(gl_context *ctx, nir_shader *shader);
59 ~nir_visitor();
60
61 virtual void visit(ir_variable *);
62 virtual void visit(ir_function *);
63 virtual void visit(ir_function_signature *);
64 virtual void visit(ir_loop *);
65 virtual void visit(ir_if *);
66 virtual void visit(ir_discard *);
67 virtual void visit(ir_demote *);
68 virtual void visit(ir_loop_jump *);
69 virtual void visit(ir_return *);
70 virtual void visit(ir_call *);
71 virtual void visit(ir_assignment *);
72 virtual void visit(ir_emit_vertex *);
73 virtual void visit(ir_end_primitive *);
74 virtual void visit(ir_expression *);
75 virtual void visit(ir_swizzle *);
76 virtual void visit(ir_texture *);
77 virtual void visit(ir_constant *);
78 virtual void visit(ir_dereference_variable *);
79 virtual void visit(ir_dereference_record *);
80 virtual void visit(ir_dereference_array *);
81 virtual void visit(ir_barrier *);
82
83 void create_function(ir_function_signature *ir);
84
85 private:
86 void add_instr(nir_instr *instr, unsigned num_components, unsigned bit_size);
87 nir_ssa_def *evaluate_rvalue(ir_rvalue *ir);
88
89 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def **srcs);
90 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1);
91 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
92 nir_ssa_def *src2);
93 nir_alu_instr *emit(nir_op op, unsigned dest_size, nir_ssa_def *src1,
94 nir_ssa_def *src2, nir_ssa_def *src3);
95
96 bool supports_std430;
97
98 nir_shader *shader;
99 nir_function_impl *impl;
100 nir_builder b;
101 nir_ssa_def *result; /* result of the expression tree last visited */
102
103 nir_deref_instr *evaluate_deref(ir_instruction *ir);
104
105 nir_constant *constant_copy(ir_constant *ir, void *mem_ctx);
106
107 /* most recent deref instruction created */
108 nir_deref_instr *deref;
109
110 /* whether the IR we're operating on is per-function or global */
111 bool is_global;
112
113 ir_function_signature *sig;
114
115 /* map of ir_variable -> nir_variable */
116 struct hash_table *var_table;
117
118 /* map of ir_function_signature -> nir_function_overload */
119 struct hash_table *overload_table;
120 };
121
122 /*
123 * This visitor runs before the main visitor, calling create_function() for
124 * each function so that the main visitor can resolve forward references in
125 * calls.
126 */
127
128 class nir_function_visitor : public ir_hierarchical_visitor
129 {
130 public:
131 nir_function_visitor(nir_visitor *v) : visitor(v)
132 {
133 }
134 virtual ir_visitor_status visit_enter(ir_function *);
135
136 private:
137 nir_visitor *visitor;
138 };
139
140 /* glsl_to_nir can only handle converting certain function paramaters
141 * to NIR. This visitor checks for parameters it can't currently handle.
142 */
143 class ir_function_param_visitor : public ir_hierarchical_visitor
144 {
145 public:
146 ir_function_param_visitor()
147 : unsupported(false)
148 {
149 }
150
151 virtual ir_visitor_status visit_enter(ir_function_signature *ir)
152 {
153
154 if (ir->is_intrinsic())
155 return visit_continue;
156
157 foreach_in_list(ir_variable, param, &ir->parameters) {
158 if (!param->type->is_vector() || !param->type->is_scalar()) {
159 unsupported = true;
160 return visit_stop;
161 }
162
163 if (param->data.mode == ir_var_function_inout) {
164 unsupported = true;
165 return visit_stop;
166 }
167 }
168
169 return visit_continue;
170 }
171
172 bool unsupported;
173 };
174
175 } /* end of anonymous namespace */
176
177
178 static bool
179 has_unsupported_function_param(exec_list *ir)
180 {
181 ir_function_param_visitor visitor;
182 visit_list_elements(&visitor, ir);
183 return visitor.unsupported;
184 }
185
186 nir_shader *
187 glsl_to_nir(struct gl_context *ctx,
188 const struct gl_shader_program *shader_prog,
189 gl_shader_stage stage,
190 const nir_shader_compiler_options *options)
191 {
192 struct gl_linked_shader *sh = shader_prog->_LinkedShaders[stage];
193
194 const struct gl_shader_compiler_options *gl_options =
195 &ctx->Const.ShaderCompilerOptions[stage];
196
197 /* glsl_to_nir can only handle converting certain function paramaters
198 * to NIR. If we find something we can't handle then we get the GLSL IR
199 * opts to remove it before we continue on.
200 *
201 * TODO: add missing glsl ir to nir support and remove this loop.
202 */
203 while (has_unsupported_function_param(sh->ir)) {
204 do_common_optimization(sh->ir, true, true, gl_options,
205 ctx->Const.NativeIntegers);
206 }
207
208 nir_shader *shader = nir_shader_create(NULL, stage, options,
209 &sh->Program->info);
210
211 nir_visitor v1(ctx, shader);
212 nir_function_visitor v2(&v1);
213 v2.run(sh->ir);
214 visit_exec_list(sh->ir, &v1);
215
216 nir_validate_shader(shader, "after glsl to nir, before function inline");
217
218 /* We have to lower away local constant initializers right before we
219 * inline functions. That way they get properly initialized at the top
220 * of the function and not at the top of its caller.
221 */
222 nir_lower_variable_initializers(shader, (nir_variable_mode)~0);
223 nir_lower_returns(shader);
224 nir_inline_functions(shader);
225 nir_opt_deref(shader);
226
227 nir_validate_shader(shader, "after function inlining and return lowering");
228
229 /* Now that we have inlined everything remove all of the functions except
230 * main().
231 */
232 foreach_list_typed_safe(nir_function, function, node, &(shader)->functions){
233 if (strcmp("main", function->name) != 0) {
234 exec_node_remove(&function->node);
235 }
236 }
237
238 shader->info.name = ralloc_asprintf(shader, "GLSL%d", shader_prog->Name);
239 if (shader_prog->Label)
240 shader->info.label = ralloc_strdup(shader, shader_prog->Label);
241
242 /* Check for transform feedback varyings specified via the API */
243 shader->info.has_transform_feedback_varyings =
244 shader_prog->TransformFeedback.NumVarying > 0;
245
246 /* Check for transform feedback varyings specified in the Shader */
247 if (shader_prog->last_vert_prog)
248 shader->info.has_transform_feedback_varyings |=
249 shader_prog->last_vert_prog->sh.LinkedTransformFeedback->NumVarying > 0;
250
251 if (shader->info.stage == MESA_SHADER_FRAGMENT) {
252 shader->info.fs.pixel_center_integer = sh->Program->info.fs.pixel_center_integer;
253 shader->info.fs.origin_upper_left = sh->Program->info.fs.origin_upper_left;
254 }
255
256 return shader;
257 }
258
259 nir_visitor::nir_visitor(gl_context *ctx, nir_shader *shader)
260 {
261 this->supports_std430 = ctx->Const.UseSTD430AsDefaultPacking;
262 this->shader = shader;
263 this->is_global = true;
264 this->var_table = _mesa_pointer_hash_table_create(NULL);
265 this->overload_table = _mesa_pointer_hash_table_create(NULL);
266 this->result = NULL;
267 this->impl = NULL;
268 this->deref = NULL;
269 this->sig = NULL;
270 memset(&this->b, 0, sizeof(this->b));
271 }
272
273 nir_visitor::~nir_visitor()
274 {
275 _mesa_hash_table_destroy(this->var_table, NULL);
276 _mesa_hash_table_destroy(this->overload_table, NULL);
277 }
278
279 nir_deref_instr *
280 nir_visitor::evaluate_deref(ir_instruction *ir)
281 {
282 ir->accept(this);
283 return this->deref;
284 }
285
286 nir_constant *
287 nir_visitor::constant_copy(ir_constant *ir, void *mem_ctx)
288 {
289 if (ir == NULL)
290 return NULL;
291
292 nir_constant *ret = rzalloc(mem_ctx, nir_constant);
293
294 const unsigned rows = ir->type->vector_elements;
295 const unsigned cols = ir->type->matrix_columns;
296 unsigned i;
297
298 ret->num_elements = 0;
299 switch (ir->type->base_type) {
300 case GLSL_TYPE_UINT:
301 /* Only float base types can be matrices. */
302 assert(cols == 1);
303
304 for (unsigned r = 0; r < rows; r++)
305 ret->values[r].u32 = ir->value.u[r];
306
307 break;
308
309 case GLSL_TYPE_INT:
310 /* Only float base types can be matrices. */
311 assert(cols == 1);
312
313 for (unsigned r = 0; r < rows; r++)
314 ret->values[r].i32 = ir->value.i[r];
315
316 break;
317
318 case GLSL_TYPE_FLOAT:
319 case GLSL_TYPE_FLOAT16:
320 case GLSL_TYPE_DOUBLE:
321 if (cols > 1) {
322 ret->elements = ralloc_array(mem_ctx, nir_constant *, cols);
323 ret->num_elements = cols;
324 for (unsigned c = 0; c < cols; c++) {
325 nir_constant *col_const = rzalloc(mem_ctx, nir_constant);
326 col_const->num_elements = 0;
327 switch (ir->type->base_type) {
328 case GLSL_TYPE_FLOAT:
329 for (unsigned r = 0; r < rows; r++)
330 col_const->values[r].f32 = ir->value.f[c * rows + r];
331 break;
332
333 case GLSL_TYPE_FLOAT16:
334 for (unsigned r = 0; r < rows; r++)
335 col_const->values[r].u16 = ir->value.f16[c * rows + r];
336 break;
337
338 case GLSL_TYPE_DOUBLE:
339 for (unsigned r = 0; r < rows; r++)
340 col_const->values[r].f64 = ir->value.d[c * rows + r];
341 break;
342
343 default:
344 unreachable("Cannot get here from the first level switch");
345 }
346 ret->elements[c] = col_const;
347 }
348 } else {
349 switch (ir->type->base_type) {
350 case GLSL_TYPE_FLOAT:
351 for (unsigned r = 0; r < rows; r++)
352 ret->values[r].f32 = ir->value.f[r];
353 break;
354
355 case GLSL_TYPE_FLOAT16:
356 for (unsigned r = 0; r < rows; r++)
357 ret->values[r].u16 = ir->value.f16[r];
358 break;
359
360 case GLSL_TYPE_DOUBLE:
361 for (unsigned r = 0; r < rows; r++)
362 ret->values[r].f64 = ir->value.d[r];
363 break;
364
365 default:
366 unreachable("Cannot get here from the first level switch");
367 }
368 }
369 break;
370
371 case GLSL_TYPE_UINT64:
372 /* Only float base types can be matrices. */
373 assert(cols == 1);
374
375 for (unsigned r = 0; r < rows; r++)
376 ret->values[r].u64 = ir->value.u64[r];
377 break;
378
379 case GLSL_TYPE_INT64:
380 /* Only float base types can be matrices. */
381 assert(cols == 1);
382
383 for (unsigned r = 0; r < rows; r++)
384 ret->values[r].i64 = ir->value.i64[r];
385 break;
386
387 case GLSL_TYPE_BOOL:
388 /* Only float base types can be matrices. */
389 assert(cols == 1);
390
391 for (unsigned r = 0; r < rows; r++)
392 ret->values[r].b = ir->value.b[r];
393
394 break;
395
396 case GLSL_TYPE_STRUCT:
397 case GLSL_TYPE_ARRAY:
398 ret->elements = ralloc_array(mem_ctx, nir_constant *,
399 ir->type->length);
400 ret->num_elements = ir->type->length;
401
402 for (i = 0; i < ir->type->length; i++)
403 ret->elements[i] = constant_copy(ir->const_elements[i], mem_ctx);
404 break;
405
406 default:
407 unreachable("not reached");
408 }
409
410 return ret;
411 }
412
413 static const glsl_type *
414 wrap_type_in_array(const glsl_type *elem_type, const glsl_type *array_type)
415 {
416 if (!array_type->is_array())
417 return elem_type;
418
419 elem_type = wrap_type_in_array(elem_type, array_type->fields.array);
420
421 return glsl_type::get_array_instance(elem_type, array_type->length);
422 }
423
424 static unsigned
425 get_nir_how_declared(unsigned how_declared)
426 {
427 if (how_declared == ir_var_hidden)
428 return nir_var_hidden;
429
430 return nir_var_declared_normally;
431 }
432
433 void
434 nir_visitor::visit(ir_variable *ir)
435 {
436 /* TODO: In future we should switch to using the NIR lowering pass but for
437 * now just ignore these variables as GLSL IR should have lowered them.
438 * Anything remaining are just dead vars that weren't cleaned up.
439 */
440 if (ir->data.mode == ir_var_shader_shared)
441 return;
442
443 /* FINISHME: inout parameters */
444 assert(ir->data.mode != ir_var_function_inout);
445
446 if (ir->data.mode == ir_var_function_out)
447 return;
448
449 nir_variable *var = rzalloc(shader, nir_variable);
450 var->type = ir->type;
451 var->name = ralloc_strdup(var, ir->name);
452
453 var->data.always_active_io = ir->data.always_active_io;
454 var->data.read_only = ir->data.read_only;
455 var->data.centroid = ir->data.centroid;
456 var->data.sample = ir->data.sample;
457 var->data.patch = ir->data.patch;
458 var->data.how_declared = get_nir_how_declared(ir->data.how_declared);
459 var->data.invariant = ir->data.invariant;
460 var->data.location = ir->data.location;
461 var->data.stream = ir->data.stream;
462 if (ir->data.stream & (1u << 31))
463 var->data.stream |= NIR_STREAM_PACKED;
464
465 var->data.precision = ir->data.precision;
466 var->data.explicit_location = ir->data.explicit_location;
467 var->data.from_named_ifc_block = ir->data.from_named_ifc_block;
468 var->data.compact = false;
469
470 switch(ir->data.mode) {
471 case ir_var_auto:
472 case ir_var_temporary:
473 if (is_global)
474 var->data.mode = nir_var_shader_temp;
475 else
476 var->data.mode = nir_var_function_temp;
477 break;
478
479 case ir_var_function_in:
480 case ir_var_const_in:
481 var->data.mode = nir_var_function_temp;
482 break;
483
484 case ir_var_shader_in:
485 if (shader->info.stage == MESA_SHADER_GEOMETRY &&
486 ir->data.location == VARYING_SLOT_PRIMITIVE_ID) {
487 /* For whatever reason, GLSL IR makes gl_PrimitiveIDIn an input */
488 var->data.location = SYSTEM_VALUE_PRIMITIVE_ID;
489 var->data.mode = nir_var_system_value;
490 } else {
491 var->data.mode = nir_var_shader_in;
492
493 if (shader->info.stage == MESA_SHADER_TESS_EVAL &&
494 (ir->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
495 ir->data.location == VARYING_SLOT_TESS_LEVEL_OUTER)) {
496 var->data.compact = ir->type->without_array()->is_scalar();
497 }
498
499 if (shader->info.stage > MESA_SHADER_VERTEX &&
500 ir->data.location >= VARYING_SLOT_CLIP_DIST0 &&
501 ir->data.location <= VARYING_SLOT_CULL_DIST1) {
502 var->data.compact = ir->type->without_array()->is_scalar();
503 }
504 }
505 break;
506
507 case ir_var_shader_out:
508 var->data.mode = nir_var_shader_out;
509 if (shader->info.stage == MESA_SHADER_TESS_CTRL &&
510 (ir->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
511 ir->data.location == VARYING_SLOT_TESS_LEVEL_OUTER)) {
512 var->data.compact = ir->type->without_array()->is_scalar();
513 }
514
515 if (shader->info.stage <= MESA_SHADER_GEOMETRY &&
516 ir->data.location >= VARYING_SLOT_CLIP_DIST0 &&
517 ir->data.location <= VARYING_SLOT_CULL_DIST1) {
518 var->data.compact = ir->type->without_array()->is_scalar();
519 }
520 break;
521
522 case ir_var_uniform:
523 if (ir->get_interface_type())
524 var->data.mode = nir_var_mem_ubo;
525 else
526 var->data.mode = nir_var_uniform;
527 break;
528
529 case ir_var_shader_storage:
530 var->data.mode = nir_var_mem_ssbo;
531 break;
532
533 case ir_var_system_value:
534 var->data.mode = nir_var_system_value;
535 break;
536
537 default:
538 unreachable("not reached");
539 }
540
541 unsigned mem_access = 0;
542 if (ir->data.memory_read_only)
543 mem_access |= ACCESS_NON_WRITEABLE;
544 if (ir->data.memory_write_only)
545 mem_access |= ACCESS_NON_READABLE;
546 if (ir->data.memory_coherent)
547 mem_access |= ACCESS_COHERENT;
548 if (ir->data.memory_volatile)
549 mem_access |= ACCESS_VOLATILE;
550 if (ir->data.memory_restrict)
551 mem_access |= ACCESS_RESTRICT;
552
553 var->interface_type = ir->get_interface_type();
554
555 /* For UBO and SSBO variables, we need explicit types */
556 if (var->data.mode & (nir_var_mem_ubo | nir_var_mem_ssbo)) {
557 const glsl_type *explicit_ifc_type =
558 ir->get_interface_type()->get_explicit_interface_type(supports_std430);
559
560 var->interface_type = explicit_ifc_type;
561
562 if (ir->type->without_array()->is_interface()) {
563 /* If the type contains the interface, wrap the explicit type in the
564 * right number of arrays.
565 */
566 var->type = wrap_type_in_array(explicit_ifc_type, ir->type);
567 } else {
568 /* Otherwise, this variable is one entry in the interface */
569 UNUSED bool found = false;
570 for (unsigned i = 0; i < explicit_ifc_type->length; i++) {
571 const glsl_struct_field *field =
572 &explicit_ifc_type->fields.structure[i];
573 if (strcmp(ir->name, field->name) != 0)
574 continue;
575
576 var->type = field->type;
577 if (field->memory_read_only)
578 mem_access |= ACCESS_NON_WRITEABLE;
579 if (field->memory_write_only)
580 mem_access |= ACCESS_NON_READABLE;
581 if (field->memory_coherent)
582 mem_access |= ACCESS_COHERENT;
583 if (field->memory_volatile)
584 mem_access |= ACCESS_VOLATILE;
585 if (field->memory_restrict)
586 mem_access |= ACCESS_RESTRICT;
587
588 found = true;
589 break;
590 }
591 assert(found);
592 }
593 }
594
595 var->data.interpolation = ir->data.interpolation;
596 var->data.location_frac = ir->data.location_frac;
597
598 switch (ir->data.depth_layout) {
599 case ir_depth_layout_none:
600 var->data.depth_layout = nir_depth_layout_none;
601 break;
602 case ir_depth_layout_any:
603 var->data.depth_layout = nir_depth_layout_any;
604 break;
605 case ir_depth_layout_greater:
606 var->data.depth_layout = nir_depth_layout_greater;
607 break;
608 case ir_depth_layout_less:
609 var->data.depth_layout = nir_depth_layout_less;
610 break;
611 case ir_depth_layout_unchanged:
612 var->data.depth_layout = nir_depth_layout_unchanged;
613 break;
614 default:
615 unreachable("not reached");
616 }
617
618 var->data.index = ir->data.index;
619 var->data.descriptor_set = 0;
620 var->data.binding = ir->data.binding;
621 var->data.explicit_binding = ir->data.explicit_binding;
622 var->data.bindless = ir->data.bindless;
623 var->data.offset = ir->data.offset;
624 var->data.access = (gl_access_qualifier)mem_access;
625
626 if (var->type->without_array()->is_image()) {
627 var->data.image.format = ir->data.image_format;
628 } else if (var->data.mode == nir_var_shader_out) {
629 var->data.xfb.buffer = ir->data.xfb_buffer;
630 var->data.xfb.stride = ir->data.xfb_stride;
631 }
632
633 var->data.fb_fetch_output = ir->data.fb_fetch_output;
634 var->data.explicit_xfb_buffer = ir->data.explicit_xfb_buffer;
635 var->data.explicit_xfb_stride = ir->data.explicit_xfb_stride;
636
637 var->num_state_slots = ir->get_num_state_slots();
638 if (var->num_state_slots > 0) {
639 var->state_slots = rzalloc_array(var, nir_state_slot,
640 var->num_state_slots);
641
642 ir_state_slot *state_slots = ir->get_state_slots();
643 for (unsigned i = 0; i < var->num_state_slots; i++) {
644 for (unsigned j = 0; j < 5; j++)
645 var->state_slots[i].tokens[j] = state_slots[i].tokens[j];
646 var->state_slots[i].swizzle = state_slots[i].swizzle;
647 }
648 } else {
649 var->state_slots = NULL;
650 }
651
652 var->constant_initializer = constant_copy(ir->constant_initializer, var);
653
654 if (var->data.mode == nir_var_function_temp)
655 nir_function_impl_add_variable(impl, var);
656 else
657 nir_shader_add_variable(shader, var);
658
659 _mesa_hash_table_insert(var_table, ir, var);
660 }
661
662 ir_visitor_status
663 nir_function_visitor::visit_enter(ir_function *ir)
664 {
665 foreach_in_list(ir_function_signature, sig, &ir->signatures) {
666 visitor->create_function(sig);
667 }
668 return visit_continue_with_parent;
669 }
670
671 void
672 nir_visitor::create_function(ir_function_signature *ir)
673 {
674 if (ir->is_intrinsic())
675 return;
676
677 nir_function *func = nir_function_create(shader, ir->function_name());
678 if (strcmp(ir->function_name(), "main") == 0)
679 func->is_entrypoint = true;
680
681 func->num_params = ir->parameters.length() +
682 (ir->return_type != glsl_type::void_type);
683 func->params = ralloc_array(shader, nir_parameter, func->num_params);
684
685 unsigned np = 0;
686
687 if (ir->return_type != glsl_type::void_type) {
688 /* The return value is a variable deref (basically an out parameter) */
689 func->params[np].num_components = 1;
690 func->params[np].bit_size = 32;
691 np++;
692 }
693
694 foreach_in_list(ir_variable, param, &ir->parameters) {
695 /* FINISHME: pass arrays, structs, etc by reference? */
696 assert(param->type->is_vector() || param->type->is_scalar());
697
698 if (param->data.mode == ir_var_function_in) {
699 func->params[np].num_components = param->type->vector_elements;
700 func->params[np].bit_size = glsl_get_bit_size(param->type);
701 } else {
702 func->params[np].num_components = 1;
703 func->params[np].bit_size = 32;
704 }
705 np++;
706 }
707 assert(np == func->num_params);
708
709 _mesa_hash_table_insert(this->overload_table, ir, func);
710 }
711
712 void
713 nir_visitor::visit(ir_function *ir)
714 {
715 foreach_in_list(ir_function_signature, sig, &ir->signatures)
716 sig->accept(this);
717 }
718
719 void
720 nir_visitor::visit(ir_function_signature *ir)
721 {
722 if (ir->is_intrinsic())
723 return;
724
725 this->sig = ir;
726
727 struct hash_entry *entry =
728 _mesa_hash_table_search(this->overload_table, ir);
729
730 assert(entry);
731 nir_function *func = (nir_function *) entry->data;
732
733 if (ir->is_defined) {
734 nir_function_impl *impl = nir_function_impl_create(func);
735 this->impl = impl;
736
737 this->is_global = false;
738
739 nir_builder_init(&b, impl);
740 b.cursor = nir_after_cf_list(&impl->body);
741
742 unsigned i = (ir->return_type != glsl_type::void_type) ? 1 : 0;
743
744 foreach_in_list(ir_variable, param, &ir->parameters) {
745 nir_variable *var =
746 nir_local_variable_create(impl, param->type, param->name);
747
748 if (param->data.mode == ir_var_function_in) {
749 nir_store_var(&b, var, nir_load_param(&b, i), ~0);
750 }
751
752 _mesa_hash_table_insert(var_table, param, var);
753 i++;
754 }
755
756 visit_exec_list(&ir->body, this);
757
758 this->is_global = true;
759 } else {
760 func->impl = NULL;
761 }
762 }
763
764 void
765 nir_visitor::visit(ir_loop *ir)
766 {
767 nir_push_loop(&b);
768 visit_exec_list(&ir->body_instructions, this);
769 nir_pop_loop(&b, NULL);
770 }
771
772 void
773 nir_visitor::visit(ir_if *ir)
774 {
775 nir_push_if(&b, evaluate_rvalue(ir->condition));
776 visit_exec_list(&ir->then_instructions, this);
777 nir_push_else(&b, NULL);
778 visit_exec_list(&ir->else_instructions, this);
779 nir_pop_if(&b, NULL);
780 }
781
782 void
783 nir_visitor::visit(ir_discard *ir)
784 {
785 /*
786 * discards aren't treated as control flow, because before we lower them
787 * they can appear anywhere in the shader and the stuff after them may still
788 * be executed (yay, crazy GLSL rules!). However, after lowering, all the
789 * discards will be immediately followed by a return.
790 */
791
792 nir_intrinsic_instr *discard;
793 if (ir->condition) {
794 discard = nir_intrinsic_instr_create(this->shader,
795 nir_intrinsic_discard_if);
796 discard->src[0] =
797 nir_src_for_ssa(evaluate_rvalue(ir->condition));
798 } else {
799 discard = nir_intrinsic_instr_create(this->shader, nir_intrinsic_discard);
800 }
801
802 nir_builder_instr_insert(&b, &discard->instr);
803 }
804
805 void
806 nir_visitor::visit(ir_demote *ir)
807 {
808 nir_intrinsic_instr *demote =
809 nir_intrinsic_instr_create(this->shader, nir_intrinsic_demote);
810
811 nir_builder_instr_insert(&b, &demote->instr);
812 }
813
814 void
815 nir_visitor::visit(ir_emit_vertex *ir)
816 {
817 nir_intrinsic_instr *instr =
818 nir_intrinsic_instr_create(this->shader, nir_intrinsic_emit_vertex);
819 nir_intrinsic_set_stream_id(instr, ir->stream_id());
820 nir_builder_instr_insert(&b, &instr->instr);
821 }
822
823 void
824 nir_visitor::visit(ir_end_primitive *ir)
825 {
826 nir_intrinsic_instr *instr =
827 nir_intrinsic_instr_create(this->shader, nir_intrinsic_end_primitive);
828 nir_intrinsic_set_stream_id(instr, ir->stream_id());
829 nir_builder_instr_insert(&b, &instr->instr);
830 }
831
832 void
833 nir_visitor::visit(ir_loop_jump *ir)
834 {
835 nir_jump_type type;
836 switch (ir->mode) {
837 case ir_loop_jump::jump_break:
838 type = nir_jump_break;
839 break;
840 case ir_loop_jump::jump_continue:
841 type = nir_jump_continue;
842 break;
843 default:
844 unreachable("not reached");
845 }
846
847 nir_jump_instr *instr = nir_jump_instr_create(this->shader, type);
848 nir_builder_instr_insert(&b, &instr->instr);
849 }
850
851 void
852 nir_visitor::visit(ir_return *ir)
853 {
854 if (ir->value != NULL) {
855 nir_deref_instr *ret_deref =
856 nir_build_deref_cast(&b, nir_load_param(&b, 0),
857 nir_var_function_temp, ir->value->type, 0);
858
859 nir_ssa_def *val = evaluate_rvalue(ir->value);
860 nir_store_deref(&b, ret_deref, val, ~0);
861 }
862
863 nir_jump_instr *instr = nir_jump_instr_create(this->shader, nir_jump_return);
864 nir_builder_instr_insert(&b, &instr->instr);
865 }
866
867 static void
868 intrinsic_set_std430_align(nir_intrinsic_instr *intrin, const glsl_type *type)
869 {
870 unsigned bit_size = type->is_boolean() ? 32 : glsl_get_bit_size(type);
871 unsigned pow2_components = util_next_power_of_two(type->vector_elements);
872 nir_intrinsic_set_align(intrin, (bit_size / 8) * pow2_components, 0);
873 }
874
875 /* Accumulate any qualifiers along the deref chain to get the actual
876 * load/store qualifier.
877 */
878
879 static enum gl_access_qualifier
880 deref_get_qualifier(nir_deref_instr *deref)
881 {
882 nir_deref_path path;
883 nir_deref_path_init(&path, deref, NULL);
884
885 unsigned qualifiers = path.path[0]->var->data.access;
886
887 const glsl_type *parent_type = path.path[0]->type;
888 for (nir_deref_instr **cur_ptr = &path.path[1]; *cur_ptr; cur_ptr++) {
889 nir_deref_instr *cur = *cur_ptr;
890
891 if (parent_type->is_interface()) {
892 const struct glsl_struct_field *field =
893 &parent_type->fields.structure[cur->strct.index];
894 if (field->memory_read_only)
895 qualifiers |= ACCESS_NON_WRITEABLE;
896 if (field->memory_write_only)
897 qualifiers |= ACCESS_NON_READABLE;
898 if (field->memory_coherent)
899 qualifiers |= ACCESS_COHERENT;
900 if (field->memory_volatile)
901 qualifiers |= ACCESS_VOLATILE;
902 if (field->memory_restrict)
903 qualifiers |= ACCESS_RESTRICT;
904 }
905
906 parent_type = cur->type;
907 }
908
909 nir_deref_path_finish(&path);
910
911 return (gl_access_qualifier) qualifiers;
912 }
913
914 void
915 nir_visitor::visit(ir_call *ir)
916 {
917 if (ir->callee->is_intrinsic()) {
918 nir_intrinsic_op op;
919
920 switch (ir->callee->intrinsic_id) {
921 case ir_intrinsic_generic_atomic_add:
922 op = ir->return_deref->type->is_integer_32_64()
923 ? nir_intrinsic_deref_atomic_add : nir_intrinsic_deref_atomic_fadd;
924 break;
925 case ir_intrinsic_generic_atomic_and:
926 op = nir_intrinsic_deref_atomic_and;
927 break;
928 case ir_intrinsic_generic_atomic_or:
929 op = nir_intrinsic_deref_atomic_or;
930 break;
931 case ir_intrinsic_generic_atomic_xor:
932 op = nir_intrinsic_deref_atomic_xor;
933 break;
934 case ir_intrinsic_generic_atomic_min:
935 assert(ir->return_deref);
936 if (ir->return_deref->type == glsl_type::int_type)
937 op = nir_intrinsic_deref_atomic_imin;
938 else if (ir->return_deref->type == glsl_type::uint_type)
939 op = nir_intrinsic_deref_atomic_umin;
940 else if (ir->return_deref->type == glsl_type::float_type)
941 op = nir_intrinsic_deref_atomic_fmin;
942 else
943 unreachable("Invalid type");
944 break;
945 case ir_intrinsic_generic_atomic_max:
946 assert(ir->return_deref);
947 if (ir->return_deref->type == glsl_type::int_type)
948 op = nir_intrinsic_deref_atomic_imax;
949 else if (ir->return_deref->type == glsl_type::uint_type)
950 op = nir_intrinsic_deref_atomic_umax;
951 else if (ir->return_deref->type == glsl_type::float_type)
952 op = nir_intrinsic_deref_atomic_fmax;
953 else
954 unreachable("Invalid type");
955 break;
956 case ir_intrinsic_generic_atomic_exchange:
957 op = nir_intrinsic_deref_atomic_exchange;
958 break;
959 case ir_intrinsic_generic_atomic_comp_swap:
960 op = ir->return_deref->type->is_integer_32_64()
961 ? nir_intrinsic_deref_atomic_comp_swap
962 : nir_intrinsic_deref_atomic_fcomp_swap;
963 break;
964 case ir_intrinsic_atomic_counter_read:
965 op = nir_intrinsic_atomic_counter_read_deref;
966 break;
967 case ir_intrinsic_atomic_counter_increment:
968 op = nir_intrinsic_atomic_counter_inc_deref;
969 break;
970 case ir_intrinsic_atomic_counter_predecrement:
971 op = nir_intrinsic_atomic_counter_pre_dec_deref;
972 break;
973 case ir_intrinsic_atomic_counter_add:
974 op = nir_intrinsic_atomic_counter_add_deref;
975 break;
976 case ir_intrinsic_atomic_counter_and:
977 op = nir_intrinsic_atomic_counter_and_deref;
978 break;
979 case ir_intrinsic_atomic_counter_or:
980 op = nir_intrinsic_atomic_counter_or_deref;
981 break;
982 case ir_intrinsic_atomic_counter_xor:
983 op = nir_intrinsic_atomic_counter_xor_deref;
984 break;
985 case ir_intrinsic_atomic_counter_min:
986 op = nir_intrinsic_atomic_counter_min_deref;
987 break;
988 case ir_intrinsic_atomic_counter_max:
989 op = nir_intrinsic_atomic_counter_max_deref;
990 break;
991 case ir_intrinsic_atomic_counter_exchange:
992 op = nir_intrinsic_atomic_counter_exchange_deref;
993 break;
994 case ir_intrinsic_atomic_counter_comp_swap:
995 op = nir_intrinsic_atomic_counter_comp_swap_deref;
996 break;
997 case ir_intrinsic_image_load:
998 op = nir_intrinsic_image_deref_load;
999 break;
1000 case ir_intrinsic_image_store:
1001 op = nir_intrinsic_image_deref_store;
1002 break;
1003 case ir_intrinsic_image_atomic_add:
1004 op = ir->return_deref->type->is_integer_32_64()
1005 ? nir_intrinsic_image_deref_atomic_add
1006 : nir_intrinsic_image_deref_atomic_fadd;
1007 break;
1008 case ir_intrinsic_image_atomic_min:
1009 if (ir->return_deref->type == glsl_type::int_type)
1010 op = nir_intrinsic_image_deref_atomic_imin;
1011 else if (ir->return_deref->type == glsl_type::uint_type)
1012 op = nir_intrinsic_image_deref_atomic_umin;
1013 else
1014 unreachable("Invalid type");
1015 break;
1016 case ir_intrinsic_image_atomic_max:
1017 if (ir->return_deref->type == glsl_type::int_type)
1018 op = nir_intrinsic_image_deref_atomic_imax;
1019 else if (ir->return_deref->type == glsl_type::uint_type)
1020 op = nir_intrinsic_image_deref_atomic_umax;
1021 else
1022 unreachable("Invalid type");
1023 break;
1024 case ir_intrinsic_image_atomic_and:
1025 op = nir_intrinsic_image_deref_atomic_and;
1026 break;
1027 case ir_intrinsic_image_atomic_or:
1028 op = nir_intrinsic_image_deref_atomic_or;
1029 break;
1030 case ir_intrinsic_image_atomic_xor:
1031 op = nir_intrinsic_image_deref_atomic_xor;
1032 break;
1033 case ir_intrinsic_image_atomic_exchange:
1034 op = nir_intrinsic_image_deref_atomic_exchange;
1035 break;
1036 case ir_intrinsic_image_atomic_comp_swap:
1037 op = nir_intrinsic_image_deref_atomic_comp_swap;
1038 break;
1039 case ir_intrinsic_image_atomic_inc_wrap:
1040 op = nir_intrinsic_image_deref_atomic_inc_wrap;
1041 break;
1042 case ir_intrinsic_image_atomic_dec_wrap:
1043 op = nir_intrinsic_image_deref_atomic_dec_wrap;
1044 break;
1045 case ir_intrinsic_memory_barrier:
1046 op = nir_intrinsic_memory_barrier;
1047 break;
1048 case ir_intrinsic_image_size:
1049 op = nir_intrinsic_image_deref_size;
1050 break;
1051 case ir_intrinsic_image_samples:
1052 op = nir_intrinsic_image_deref_samples;
1053 break;
1054 case ir_intrinsic_ssbo_store:
1055 case ir_intrinsic_ssbo_load:
1056 case ir_intrinsic_ssbo_atomic_add:
1057 case ir_intrinsic_ssbo_atomic_and:
1058 case ir_intrinsic_ssbo_atomic_or:
1059 case ir_intrinsic_ssbo_atomic_xor:
1060 case ir_intrinsic_ssbo_atomic_min:
1061 case ir_intrinsic_ssbo_atomic_max:
1062 case ir_intrinsic_ssbo_atomic_exchange:
1063 case ir_intrinsic_ssbo_atomic_comp_swap:
1064 /* SSBO store/loads should only have been lowered in GLSL IR for
1065 * non-nir drivers, NIR drivers make use of gl_nir_lower_buffers()
1066 * instead.
1067 */
1068 unreachable("Invalid operation nir doesn't want lowered ssbo "
1069 "store/loads");
1070 case ir_intrinsic_shader_clock:
1071 op = nir_intrinsic_shader_clock;
1072 break;
1073 case ir_intrinsic_begin_invocation_interlock:
1074 op = nir_intrinsic_begin_invocation_interlock;
1075 break;
1076 case ir_intrinsic_end_invocation_interlock:
1077 op = nir_intrinsic_end_invocation_interlock;
1078 break;
1079 case ir_intrinsic_group_memory_barrier:
1080 op = nir_intrinsic_group_memory_barrier;
1081 break;
1082 case ir_intrinsic_memory_barrier_atomic_counter:
1083 op = nir_intrinsic_memory_barrier_atomic_counter;
1084 break;
1085 case ir_intrinsic_memory_barrier_buffer:
1086 op = nir_intrinsic_memory_barrier_buffer;
1087 break;
1088 case ir_intrinsic_memory_barrier_image:
1089 op = nir_intrinsic_memory_barrier_image;
1090 break;
1091 case ir_intrinsic_memory_barrier_shared:
1092 op = nir_intrinsic_memory_barrier_shared;
1093 break;
1094 case ir_intrinsic_shared_load:
1095 op = nir_intrinsic_load_shared;
1096 break;
1097 case ir_intrinsic_shared_store:
1098 op = nir_intrinsic_store_shared;
1099 break;
1100 case ir_intrinsic_shared_atomic_add:
1101 op = ir->return_deref->type->is_integer_32_64()
1102 ? nir_intrinsic_shared_atomic_add
1103 : nir_intrinsic_shared_atomic_fadd;
1104 break;
1105 case ir_intrinsic_shared_atomic_and:
1106 op = nir_intrinsic_shared_atomic_and;
1107 break;
1108 case ir_intrinsic_shared_atomic_or:
1109 op = nir_intrinsic_shared_atomic_or;
1110 break;
1111 case ir_intrinsic_shared_atomic_xor:
1112 op = nir_intrinsic_shared_atomic_xor;
1113 break;
1114 case ir_intrinsic_shared_atomic_min:
1115 assert(ir->return_deref);
1116 if (ir->return_deref->type == glsl_type::int_type)
1117 op = nir_intrinsic_shared_atomic_imin;
1118 else if (ir->return_deref->type == glsl_type::uint_type)
1119 op = nir_intrinsic_shared_atomic_umin;
1120 else if (ir->return_deref->type == glsl_type::float_type)
1121 op = nir_intrinsic_shared_atomic_fmin;
1122 else
1123 unreachable("Invalid type");
1124 break;
1125 case ir_intrinsic_shared_atomic_max:
1126 assert(ir->return_deref);
1127 if (ir->return_deref->type == glsl_type::int_type)
1128 op = nir_intrinsic_shared_atomic_imax;
1129 else if (ir->return_deref->type == glsl_type::uint_type)
1130 op = nir_intrinsic_shared_atomic_umax;
1131 else if (ir->return_deref->type == glsl_type::float_type)
1132 op = nir_intrinsic_shared_atomic_fmax;
1133 else
1134 unreachable("Invalid type");
1135 break;
1136 case ir_intrinsic_shared_atomic_exchange:
1137 op = nir_intrinsic_shared_atomic_exchange;
1138 break;
1139 case ir_intrinsic_shared_atomic_comp_swap:
1140 op = ir->return_deref->type->is_integer_32_64()
1141 ? nir_intrinsic_shared_atomic_comp_swap
1142 : nir_intrinsic_shared_atomic_fcomp_swap;
1143 break;
1144 case ir_intrinsic_vote_any:
1145 op = nir_intrinsic_vote_any;
1146 break;
1147 case ir_intrinsic_vote_all:
1148 op = nir_intrinsic_vote_all;
1149 break;
1150 case ir_intrinsic_vote_eq:
1151 op = nir_intrinsic_vote_ieq;
1152 break;
1153 case ir_intrinsic_ballot:
1154 op = nir_intrinsic_ballot;
1155 break;
1156 case ir_intrinsic_read_invocation:
1157 op = nir_intrinsic_read_invocation;
1158 break;
1159 case ir_intrinsic_read_first_invocation:
1160 op = nir_intrinsic_read_first_invocation;
1161 break;
1162 case ir_intrinsic_helper_invocation:
1163 op = nir_intrinsic_is_helper_invocation;
1164 break;
1165 default:
1166 unreachable("not reached");
1167 }
1168
1169 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(shader, op);
1170 nir_ssa_def *ret = &instr->dest.ssa;
1171
1172 switch (op) {
1173 case nir_intrinsic_deref_atomic_add:
1174 case nir_intrinsic_deref_atomic_imin:
1175 case nir_intrinsic_deref_atomic_umin:
1176 case nir_intrinsic_deref_atomic_imax:
1177 case nir_intrinsic_deref_atomic_umax:
1178 case nir_intrinsic_deref_atomic_and:
1179 case nir_intrinsic_deref_atomic_or:
1180 case nir_intrinsic_deref_atomic_xor:
1181 case nir_intrinsic_deref_atomic_exchange:
1182 case nir_intrinsic_deref_atomic_comp_swap:
1183 case nir_intrinsic_deref_atomic_fadd:
1184 case nir_intrinsic_deref_atomic_fmin:
1185 case nir_intrinsic_deref_atomic_fmax:
1186 case nir_intrinsic_deref_atomic_fcomp_swap: {
1187 int param_count = ir->actual_parameters.length();
1188 assert(param_count == 2 || param_count == 3);
1189
1190 /* Deref */
1191 exec_node *param = ir->actual_parameters.get_head();
1192 ir_rvalue *rvalue = (ir_rvalue *) param;
1193 ir_dereference *deref = rvalue->as_dereference();
1194 ir_swizzle *swizzle = NULL;
1195 if (!deref) {
1196 /* We may have a swizzle to pick off a single vec4 component */
1197 swizzle = rvalue->as_swizzle();
1198 assert(swizzle && swizzle->type->vector_elements == 1);
1199 deref = swizzle->val->as_dereference();
1200 assert(deref);
1201 }
1202 nir_deref_instr *nir_deref = evaluate_deref(deref);
1203 if (swizzle) {
1204 nir_deref = nir_build_deref_array_imm(&b, nir_deref,
1205 swizzle->mask.x);
1206 }
1207 instr->src[0] = nir_src_for_ssa(&nir_deref->dest.ssa);
1208
1209 nir_intrinsic_set_access(instr, deref_get_qualifier(nir_deref));
1210
1211 /* data1 parameter (this is always present) */
1212 param = param->get_next();
1213 ir_instruction *inst = (ir_instruction *) param;
1214 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1215
1216 /* data2 parameter (only with atomic_comp_swap) */
1217 if (param_count == 3) {
1218 assert(op == nir_intrinsic_deref_atomic_comp_swap ||
1219 op == nir_intrinsic_deref_atomic_fcomp_swap);
1220 param = param->get_next();
1221 inst = (ir_instruction *) param;
1222 instr->src[2] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1223 }
1224
1225 /* Atomic result */
1226 assert(ir->return_deref);
1227 nir_ssa_dest_init(&instr->instr, &instr->dest,
1228 ir->return_deref->type->vector_elements, 32, NULL);
1229 nir_builder_instr_insert(&b, &instr->instr);
1230 break;
1231 }
1232 case nir_intrinsic_atomic_counter_read_deref:
1233 case nir_intrinsic_atomic_counter_inc_deref:
1234 case nir_intrinsic_atomic_counter_pre_dec_deref:
1235 case nir_intrinsic_atomic_counter_add_deref:
1236 case nir_intrinsic_atomic_counter_min_deref:
1237 case nir_intrinsic_atomic_counter_max_deref:
1238 case nir_intrinsic_atomic_counter_and_deref:
1239 case nir_intrinsic_atomic_counter_or_deref:
1240 case nir_intrinsic_atomic_counter_xor_deref:
1241 case nir_intrinsic_atomic_counter_exchange_deref:
1242 case nir_intrinsic_atomic_counter_comp_swap_deref: {
1243 /* Set the counter variable dereference. */
1244 exec_node *param = ir->actual_parameters.get_head();
1245 ir_dereference *counter = (ir_dereference *)param;
1246
1247 instr->src[0] = nir_src_for_ssa(&evaluate_deref(counter)->dest.ssa);
1248 param = param->get_next();
1249
1250 /* Set the intrinsic destination. */
1251 if (ir->return_deref) {
1252 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
1253 }
1254
1255 /* Set the intrinsic parameters. */
1256 if (!param->is_tail_sentinel()) {
1257 instr->src[1] =
1258 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1259 param = param->get_next();
1260 }
1261
1262 if (!param->is_tail_sentinel()) {
1263 instr->src[2] =
1264 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1265 param = param->get_next();
1266 }
1267
1268 nir_builder_instr_insert(&b, &instr->instr);
1269 break;
1270 }
1271 case nir_intrinsic_image_deref_load:
1272 case nir_intrinsic_image_deref_store:
1273 case nir_intrinsic_image_deref_atomic_add:
1274 case nir_intrinsic_image_deref_atomic_imin:
1275 case nir_intrinsic_image_deref_atomic_umin:
1276 case nir_intrinsic_image_deref_atomic_imax:
1277 case nir_intrinsic_image_deref_atomic_umax:
1278 case nir_intrinsic_image_deref_atomic_and:
1279 case nir_intrinsic_image_deref_atomic_or:
1280 case nir_intrinsic_image_deref_atomic_xor:
1281 case nir_intrinsic_image_deref_atomic_exchange:
1282 case nir_intrinsic_image_deref_atomic_comp_swap:
1283 case nir_intrinsic_image_deref_atomic_fadd:
1284 case nir_intrinsic_image_deref_samples:
1285 case nir_intrinsic_image_deref_size:
1286 case nir_intrinsic_image_deref_atomic_inc_wrap:
1287 case nir_intrinsic_image_deref_atomic_dec_wrap: {
1288 nir_ssa_undef_instr *instr_undef =
1289 nir_ssa_undef_instr_create(shader, 1, 32);
1290 nir_builder_instr_insert(&b, &instr_undef->instr);
1291
1292 /* Set the image variable dereference. */
1293 exec_node *param = ir->actual_parameters.get_head();
1294 ir_dereference *image = (ir_dereference *)param;
1295 nir_deref_instr *deref = evaluate_deref(image);
1296 const glsl_type *type = deref->type;
1297
1298 nir_intrinsic_set_access(instr, deref_get_qualifier(deref));
1299
1300 instr->src[0] = nir_src_for_ssa(&deref->dest.ssa);
1301 param = param->get_next();
1302
1303 /* Set the intrinsic destination. */
1304 if (ir->return_deref) {
1305 unsigned num_components = ir->return_deref->type->vector_elements;
1306 nir_ssa_dest_init(&instr->instr, &instr->dest,
1307 num_components, 32, NULL);
1308 }
1309
1310 if (op == nir_intrinsic_image_deref_size) {
1311 instr->num_components = instr->dest.ssa.num_components;
1312 } else if (op == nir_intrinsic_image_deref_load ||
1313 op == nir_intrinsic_image_deref_store) {
1314 instr->num_components = 4;
1315 }
1316
1317 if (op == nir_intrinsic_image_deref_size ||
1318 op == nir_intrinsic_image_deref_samples) {
1319 nir_builder_instr_insert(&b, &instr->instr);
1320 break;
1321 }
1322
1323 /* Set the address argument, extending the coordinate vector to four
1324 * components.
1325 */
1326 nir_ssa_def *src_addr =
1327 evaluate_rvalue((ir_dereference *)param);
1328 nir_ssa_def *srcs[4];
1329
1330 for (int i = 0; i < 4; i++) {
1331 if (i < type->coordinate_components())
1332 srcs[i] = nir_channel(&b, src_addr, i);
1333 else
1334 srcs[i] = &instr_undef->def;
1335 }
1336
1337 instr->src[1] = nir_src_for_ssa(nir_vec(&b, srcs, 4));
1338 param = param->get_next();
1339
1340 /* Set the sample argument, which is undefined for single-sample
1341 * images.
1342 */
1343 if (type->sampler_dimensionality == GLSL_SAMPLER_DIM_MS) {
1344 instr->src[2] =
1345 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1346 param = param->get_next();
1347 } else {
1348 instr->src[2] = nir_src_for_ssa(&instr_undef->def);
1349 }
1350
1351 /* Set the intrinsic parameters. */
1352 if (!param->is_tail_sentinel()) {
1353 instr->src[3] =
1354 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1355 param = param->get_next();
1356 } else if (op == nir_intrinsic_image_deref_load) {
1357 instr->src[3] = nir_src_for_ssa(nir_imm_int(&b, 0)); /* LOD */
1358 }
1359
1360 if (!param->is_tail_sentinel()) {
1361 instr->src[4] =
1362 nir_src_for_ssa(evaluate_rvalue((ir_dereference *)param));
1363 param = param->get_next();
1364 } else if (op == nir_intrinsic_image_deref_store) {
1365 instr->src[4] = nir_src_for_ssa(nir_imm_int(&b, 0)); /* LOD */
1366 }
1367
1368 nir_builder_instr_insert(&b, &instr->instr);
1369 break;
1370 }
1371 case nir_intrinsic_memory_barrier:
1372 case nir_intrinsic_group_memory_barrier:
1373 case nir_intrinsic_memory_barrier_atomic_counter:
1374 case nir_intrinsic_memory_barrier_buffer:
1375 case nir_intrinsic_memory_barrier_image:
1376 case nir_intrinsic_memory_barrier_shared:
1377 nir_builder_instr_insert(&b, &instr->instr);
1378 break;
1379 case nir_intrinsic_shader_clock:
1380 nir_ssa_dest_init(&instr->instr, &instr->dest, 2, 32, NULL);
1381 instr->num_components = 2;
1382 nir_builder_instr_insert(&b, &instr->instr);
1383 break;
1384 case nir_intrinsic_begin_invocation_interlock:
1385 nir_builder_instr_insert(&b, &instr->instr);
1386 break;
1387 case nir_intrinsic_end_invocation_interlock:
1388 nir_builder_instr_insert(&b, &instr->instr);
1389 break;
1390 case nir_intrinsic_store_ssbo: {
1391 exec_node *param = ir->actual_parameters.get_head();
1392 ir_rvalue *block = ((ir_instruction *)param)->as_rvalue();
1393
1394 param = param->get_next();
1395 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1396
1397 param = param->get_next();
1398 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
1399
1400 param = param->get_next();
1401 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
1402 assert(write_mask);
1403
1404 nir_ssa_def *nir_val = evaluate_rvalue(val);
1405 if (val->type->is_boolean())
1406 nir_val = nir_b2i32(&b, nir_val);
1407
1408 instr->src[0] = nir_src_for_ssa(nir_val);
1409 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(block));
1410 instr->src[2] = nir_src_for_ssa(evaluate_rvalue(offset));
1411 intrinsic_set_std430_align(instr, val->type);
1412 nir_intrinsic_set_write_mask(instr, write_mask->value.u[0]);
1413 instr->num_components = val->type->vector_elements;
1414
1415 nir_builder_instr_insert(&b, &instr->instr);
1416 break;
1417 }
1418 case nir_intrinsic_load_shared: {
1419 exec_node *param = ir->actual_parameters.get_head();
1420 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1421
1422 nir_intrinsic_set_base(instr, 0);
1423 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(offset));
1424
1425 const glsl_type *type = ir->return_deref->var->type;
1426 instr->num_components = type->vector_elements;
1427 intrinsic_set_std430_align(instr, type);
1428
1429 /* Setup destination register */
1430 unsigned bit_size = type->is_boolean() ? 32 : glsl_get_bit_size(type);
1431 nir_ssa_dest_init(&instr->instr, &instr->dest,
1432 type->vector_elements, bit_size, NULL);
1433
1434 nir_builder_instr_insert(&b, &instr->instr);
1435
1436 /* The value in shared memory is a 32-bit value */
1437 if (type->is_boolean())
1438 ret = nir_b2b1(&b, &instr->dest.ssa);
1439 break;
1440 }
1441 case nir_intrinsic_store_shared: {
1442 exec_node *param = ir->actual_parameters.get_head();
1443 ir_rvalue *offset = ((ir_instruction *)param)->as_rvalue();
1444
1445 param = param->get_next();
1446 ir_rvalue *val = ((ir_instruction *)param)->as_rvalue();
1447
1448 param = param->get_next();
1449 ir_constant *write_mask = ((ir_instruction *)param)->as_constant();
1450 assert(write_mask);
1451
1452 nir_intrinsic_set_base(instr, 0);
1453 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(offset));
1454
1455 nir_intrinsic_set_write_mask(instr, write_mask->value.u[0]);
1456
1457 nir_ssa_def *nir_val = evaluate_rvalue(val);
1458 /* The value in shared memory is a 32-bit value */
1459 if (val->type->is_boolean())
1460 nir_val = nir_b2b32(&b, nir_val);
1461
1462 instr->src[0] = nir_src_for_ssa(nir_val);
1463 instr->num_components = val->type->vector_elements;
1464 intrinsic_set_std430_align(instr, val->type);
1465
1466 nir_builder_instr_insert(&b, &instr->instr);
1467 break;
1468 }
1469 case nir_intrinsic_shared_atomic_add:
1470 case nir_intrinsic_shared_atomic_imin:
1471 case nir_intrinsic_shared_atomic_umin:
1472 case nir_intrinsic_shared_atomic_imax:
1473 case nir_intrinsic_shared_atomic_umax:
1474 case nir_intrinsic_shared_atomic_and:
1475 case nir_intrinsic_shared_atomic_or:
1476 case nir_intrinsic_shared_atomic_xor:
1477 case nir_intrinsic_shared_atomic_exchange:
1478 case nir_intrinsic_shared_atomic_comp_swap:
1479 case nir_intrinsic_shared_atomic_fadd:
1480 case nir_intrinsic_shared_atomic_fmin:
1481 case nir_intrinsic_shared_atomic_fmax:
1482 case nir_intrinsic_shared_atomic_fcomp_swap: {
1483 int param_count = ir->actual_parameters.length();
1484 assert(param_count == 2 || param_count == 3);
1485
1486 /* Offset */
1487 exec_node *param = ir->actual_parameters.get_head();
1488 ir_instruction *inst = (ir_instruction *) param;
1489 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1490
1491 /* data1 parameter (this is always present) */
1492 param = param->get_next();
1493 inst = (ir_instruction *) param;
1494 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1495
1496 /* data2 parameter (only with atomic_comp_swap) */
1497 if (param_count == 3) {
1498 assert(op == nir_intrinsic_shared_atomic_comp_swap ||
1499 op == nir_intrinsic_shared_atomic_fcomp_swap);
1500 param = param->get_next();
1501 inst = (ir_instruction *) param;
1502 instr->src[2] =
1503 nir_src_for_ssa(evaluate_rvalue(inst->as_rvalue()));
1504 }
1505
1506 /* Atomic result */
1507 assert(ir->return_deref);
1508 unsigned bit_size = glsl_get_bit_size(ir->return_deref->type);
1509 nir_ssa_dest_init(&instr->instr, &instr->dest,
1510 ir->return_deref->type->vector_elements,
1511 bit_size, NULL);
1512 nir_builder_instr_insert(&b, &instr->instr);
1513 break;
1514 }
1515 case nir_intrinsic_vote_any:
1516 case nir_intrinsic_vote_all:
1517 case nir_intrinsic_vote_ieq: {
1518 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 1, NULL);
1519 instr->num_components = 1;
1520
1521 ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
1522 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
1523
1524 nir_builder_instr_insert(&b, &instr->instr);
1525 break;
1526 }
1527
1528 case nir_intrinsic_ballot: {
1529 nir_ssa_dest_init(&instr->instr, &instr->dest,
1530 ir->return_deref->type->vector_elements, 64, NULL);
1531 instr->num_components = ir->return_deref->type->vector_elements;
1532
1533 ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
1534 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
1535
1536 nir_builder_instr_insert(&b, &instr->instr);
1537 break;
1538 }
1539 case nir_intrinsic_read_invocation: {
1540 nir_ssa_dest_init(&instr->instr, &instr->dest,
1541 ir->return_deref->type->vector_elements, 32, NULL);
1542 instr->num_components = ir->return_deref->type->vector_elements;
1543
1544 ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
1545 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
1546
1547 ir_rvalue *invocation = (ir_rvalue *) ir->actual_parameters.get_head()->next;
1548 instr->src[1] = nir_src_for_ssa(evaluate_rvalue(invocation));
1549
1550 nir_builder_instr_insert(&b, &instr->instr);
1551 break;
1552 }
1553 case nir_intrinsic_read_first_invocation: {
1554 nir_ssa_dest_init(&instr->instr, &instr->dest,
1555 ir->return_deref->type->vector_elements, 32, NULL);
1556 instr->num_components = ir->return_deref->type->vector_elements;
1557
1558 ir_rvalue *value = (ir_rvalue *) ir->actual_parameters.get_head();
1559 instr->src[0] = nir_src_for_ssa(evaluate_rvalue(value));
1560
1561 nir_builder_instr_insert(&b, &instr->instr);
1562 break;
1563 }
1564 case nir_intrinsic_is_helper_invocation: {
1565 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 1, NULL);
1566 instr->num_components = 1;
1567 nir_builder_instr_insert(&b, &instr->instr);
1568 break;
1569 }
1570 default:
1571 unreachable("not reached");
1572 }
1573
1574 if (ir->return_deref)
1575 nir_store_deref(&b, evaluate_deref(ir->return_deref), ret, ~0);
1576
1577 return;
1578 }
1579
1580 struct hash_entry *entry =
1581 _mesa_hash_table_search(this->overload_table, ir->callee);
1582 assert(entry);
1583 nir_function *callee = (nir_function *) entry->data;
1584
1585 nir_call_instr *call = nir_call_instr_create(this->shader, callee);
1586
1587 unsigned i = 0;
1588 nir_deref_instr *ret_deref = NULL;
1589 if (ir->return_deref) {
1590 nir_variable *ret_tmp =
1591 nir_local_variable_create(this->impl, ir->return_deref->type,
1592 "return_tmp");
1593 ret_deref = nir_build_deref_var(&b, ret_tmp);
1594 call->params[i++] = nir_src_for_ssa(&ret_deref->dest.ssa);
1595 }
1596
1597 foreach_two_lists(formal_node, &ir->callee->parameters,
1598 actual_node, &ir->actual_parameters) {
1599 ir_rvalue *param_rvalue = (ir_rvalue *) actual_node;
1600 ir_variable *sig_param = (ir_variable *) formal_node;
1601
1602 if (sig_param->data.mode == ir_var_function_out) {
1603 nir_deref_instr *out_deref = evaluate_deref(param_rvalue);
1604 call->params[i] = nir_src_for_ssa(&out_deref->dest.ssa);
1605 } else if (sig_param->data.mode == ir_var_function_in) {
1606 nir_ssa_def *val = evaluate_rvalue(param_rvalue);
1607 nir_src src = nir_src_for_ssa(val);
1608
1609 nir_src_copy(&call->params[i], &src, call);
1610 } else if (sig_param->data.mode == ir_var_function_inout) {
1611 unreachable("unimplemented: inout parameters");
1612 }
1613
1614 i++;
1615 }
1616
1617 nir_builder_instr_insert(&b, &call->instr);
1618
1619 if (ir->return_deref)
1620 nir_store_deref(&b, evaluate_deref(ir->return_deref), nir_load_deref(&b, ret_deref), ~0);
1621 }
1622
1623 void
1624 nir_visitor::visit(ir_assignment *ir)
1625 {
1626 unsigned num_components = ir->lhs->type->vector_elements;
1627
1628 b.exact = ir->lhs->variable_referenced()->data.invariant ||
1629 ir->lhs->variable_referenced()->data.precise;
1630
1631 if ((ir->rhs->as_dereference() || ir->rhs->as_constant()) &&
1632 (ir->write_mask == (1 << num_components) - 1 || ir->write_mask == 0)) {
1633 nir_deref_instr *lhs = evaluate_deref(ir->lhs);
1634 nir_deref_instr *rhs = evaluate_deref(ir->rhs);
1635 enum gl_access_qualifier lhs_qualifiers = deref_get_qualifier(lhs);
1636 enum gl_access_qualifier rhs_qualifiers = deref_get_qualifier(rhs);
1637 if (ir->condition) {
1638 nir_push_if(&b, evaluate_rvalue(ir->condition));
1639 nir_copy_deref_with_access(&b, lhs, rhs, lhs_qualifiers,
1640 rhs_qualifiers);
1641 nir_pop_if(&b, NULL);
1642 } else {
1643 nir_copy_deref_with_access(&b, lhs, rhs, lhs_qualifiers,
1644 rhs_qualifiers);
1645 }
1646 return;
1647 }
1648
1649 assert(ir->rhs->type->is_scalar() || ir->rhs->type->is_vector());
1650
1651 ir->lhs->accept(this);
1652 nir_deref_instr *lhs_deref = this->deref;
1653 nir_ssa_def *src = evaluate_rvalue(ir->rhs);
1654
1655 if (ir->write_mask != (1 << num_components) - 1 && ir->write_mask != 0) {
1656 /* GLSL IR will give us the input to the write-masked assignment in a
1657 * single packed vector. So, for example, if the writemask is xzw, then
1658 * we have to swizzle x -> x, y -> z, and z -> w and get the y component
1659 * from the load.
1660 */
1661 unsigned swiz[4];
1662 unsigned component = 0;
1663 for (unsigned i = 0; i < 4; i++) {
1664 swiz[i] = ir->write_mask & (1 << i) ? component++ : 0;
1665 }
1666 src = nir_swizzle(&b, src, swiz, num_components);
1667 }
1668
1669 enum gl_access_qualifier qualifiers = deref_get_qualifier(lhs_deref);
1670 if (ir->condition) {
1671 nir_push_if(&b, evaluate_rvalue(ir->condition));
1672 nir_store_deref_with_access(&b, lhs_deref, src, ir->write_mask,
1673 qualifiers);
1674 nir_pop_if(&b, NULL);
1675 } else {
1676 nir_store_deref_with_access(&b, lhs_deref, src, ir->write_mask,
1677 qualifiers);
1678 }
1679 }
1680
1681 /*
1682 * Given an instruction, returns a pointer to its destination or NULL if there
1683 * is no destination.
1684 *
1685 * Note that this only handles instructions we generate at this level.
1686 */
1687 static nir_dest *
1688 get_instr_dest(nir_instr *instr)
1689 {
1690 nir_alu_instr *alu_instr;
1691 nir_intrinsic_instr *intrinsic_instr;
1692 nir_tex_instr *tex_instr;
1693
1694 switch (instr->type) {
1695 case nir_instr_type_alu:
1696 alu_instr = nir_instr_as_alu(instr);
1697 return &alu_instr->dest.dest;
1698
1699 case nir_instr_type_intrinsic:
1700 intrinsic_instr = nir_instr_as_intrinsic(instr);
1701 if (nir_intrinsic_infos[intrinsic_instr->intrinsic].has_dest)
1702 return &intrinsic_instr->dest;
1703 else
1704 return NULL;
1705
1706 case nir_instr_type_tex:
1707 tex_instr = nir_instr_as_tex(instr);
1708 return &tex_instr->dest;
1709
1710 default:
1711 unreachable("not reached");
1712 }
1713
1714 return NULL;
1715 }
1716
1717 void
1718 nir_visitor::add_instr(nir_instr *instr, unsigned num_components,
1719 unsigned bit_size)
1720 {
1721 nir_dest *dest = get_instr_dest(instr);
1722
1723 if (dest)
1724 nir_ssa_dest_init(instr, dest, num_components, bit_size, NULL);
1725
1726 nir_builder_instr_insert(&b, instr);
1727
1728 if (dest) {
1729 assert(dest->is_ssa);
1730 this->result = &dest->ssa;
1731 }
1732 }
1733
1734 nir_ssa_def *
1735 nir_visitor::evaluate_rvalue(ir_rvalue* ir)
1736 {
1737 ir->accept(this);
1738 if (ir->as_dereference() || ir->as_constant()) {
1739 /*
1740 * A dereference is being used on the right hand side, which means we
1741 * must emit a variable load.
1742 */
1743
1744 enum gl_access_qualifier access = deref_get_qualifier(this->deref);
1745 this->result = nir_load_deref_with_access(&b, this->deref, access);
1746 }
1747
1748 return this->result;
1749 }
1750
1751 static bool
1752 type_is_float(glsl_base_type type)
1753 {
1754 return type == GLSL_TYPE_FLOAT || type == GLSL_TYPE_DOUBLE ||
1755 type == GLSL_TYPE_FLOAT16;
1756 }
1757
1758 static bool
1759 type_is_signed(glsl_base_type type)
1760 {
1761 return type == GLSL_TYPE_INT || type == GLSL_TYPE_INT64 ||
1762 type == GLSL_TYPE_INT16;
1763 }
1764
1765 void
1766 nir_visitor::visit(ir_expression *ir)
1767 {
1768 /* Some special cases */
1769 switch (ir->operation) {
1770 case ir_unop_interpolate_at_centroid:
1771 case ir_binop_interpolate_at_offset:
1772 case ir_binop_interpolate_at_sample: {
1773 ir_dereference *deref = ir->operands[0]->as_dereference();
1774 ir_swizzle *swizzle = NULL;
1775 if (!deref) {
1776 /* the api does not allow a swizzle here, but the varying packing code
1777 * may have pushed one into here.
1778 */
1779 swizzle = ir->operands[0]->as_swizzle();
1780 assert(swizzle);
1781 deref = swizzle->val->as_dereference();
1782 assert(deref);
1783 }
1784
1785 deref->accept(this);
1786
1787 nir_intrinsic_op op;
1788 if (this->deref->mode == nir_var_shader_in) {
1789 switch (ir->operation) {
1790 case ir_unop_interpolate_at_centroid:
1791 op = nir_intrinsic_interp_deref_at_centroid;
1792 break;
1793 case ir_binop_interpolate_at_offset:
1794 op = nir_intrinsic_interp_deref_at_offset;
1795 break;
1796 case ir_binop_interpolate_at_sample:
1797 op = nir_intrinsic_interp_deref_at_sample;
1798 break;
1799 default:
1800 unreachable("Invalid interpolation intrinsic");
1801 }
1802 } else {
1803 /* This case can happen if the vertex shader does not write the
1804 * given varying. In this case, the linker will lower it to a
1805 * global variable. Since interpolating a variable makes no
1806 * sense, we'll just turn it into a load which will probably
1807 * eventually end up as an SSA definition.
1808 */
1809 assert(this->deref->mode == nir_var_shader_temp);
1810 op = nir_intrinsic_load_deref;
1811 }
1812
1813 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(shader, op);
1814 intrin->num_components = deref->type->vector_elements;
1815 intrin->src[0] = nir_src_for_ssa(&this->deref->dest.ssa);
1816
1817 if (intrin->intrinsic == nir_intrinsic_interp_deref_at_offset ||
1818 intrin->intrinsic == nir_intrinsic_interp_deref_at_sample)
1819 intrin->src[1] = nir_src_for_ssa(evaluate_rvalue(ir->operands[1]));
1820
1821 unsigned bit_size = glsl_get_bit_size(deref->type);
1822 add_instr(&intrin->instr, deref->type->vector_elements, bit_size);
1823
1824 if (swizzle) {
1825 unsigned swiz[4] = {
1826 swizzle->mask.x, swizzle->mask.y, swizzle->mask.z, swizzle->mask.w
1827 };
1828
1829 result = nir_swizzle(&b, result, swiz,
1830 swizzle->type->vector_elements);
1831 }
1832
1833 return;
1834 }
1835
1836 case ir_unop_ssbo_unsized_array_length: {
1837 nir_intrinsic_instr *intrin =
1838 nir_intrinsic_instr_create(b.shader,
1839 nir_intrinsic_deref_buffer_array_length);
1840
1841 ir_dereference *deref = ir->operands[0]->as_dereference();
1842 intrin->src[0] = nir_src_for_ssa(&evaluate_deref(deref)->dest.ssa);
1843
1844 add_instr(&intrin->instr, 1, 32);
1845 return;
1846 }
1847
1848 case ir_binop_ubo_load:
1849 /* UBO loads should only have been lowered in GLSL IR for non-nir drivers,
1850 * NIR drivers make use of gl_nir_lower_buffers() instead.
1851 */
1852 unreachable("Invalid operation nir doesn't want lowered ubo loads");
1853 default:
1854 break;
1855 }
1856
1857 nir_ssa_def *srcs[4];
1858 for (unsigned i = 0; i < ir->num_operands; i++)
1859 srcs[i] = evaluate_rvalue(ir->operands[i]);
1860
1861 glsl_base_type types[4];
1862 for (unsigned i = 0; i < ir->num_operands; i++)
1863 types[i] = ir->operands[i]->type->base_type;
1864
1865 glsl_base_type out_type = ir->type->base_type;
1866
1867 switch (ir->operation) {
1868 case ir_unop_bit_not: result = nir_inot(&b, srcs[0]); break;
1869 case ir_unop_logic_not:
1870 result = nir_inot(&b, srcs[0]);
1871 break;
1872 case ir_unop_neg:
1873 result = type_is_float(types[0]) ? nir_fneg(&b, srcs[0])
1874 : nir_ineg(&b, srcs[0]);
1875 break;
1876 case ir_unop_abs:
1877 result = type_is_float(types[0]) ? nir_fabs(&b, srcs[0])
1878 : nir_iabs(&b, srcs[0]);
1879 break;
1880 case ir_unop_clz:
1881 result = nir_uclz(&b, srcs[0]);
1882 break;
1883 case ir_unop_saturate:
1884 assert(type_is_float(types[0]));
1885 result = nir_fsat(&b, srcs[0]);
1886 break;
1887 case ir_unop_sign:
1888 result = type_is_float(types[0]) ? nir_fsign(&b, srcs[0])
1889 : nir_isign(&b, srcs[0]);
1890 break;
1891 case ir_unop_rcp: result = nir_frcp(&b, srcs[0]); break;
1892 case ir_unop_rsq: result = nir_frsq(&b, srcs[0]); break;
1893 case ir_unop_sqrt: result = nir_fsqrt(&b, srcs[0]); break;
1894 case ir_unop_exp: unreachable("ir_unop_exp should have been lowered");
1895 case ir_unop_log: unreachable("ir_unop_log should have been lowered");
1896 case ir_unop_exp2: result = nir_fexp2(&b, srcs[0]); break;
1897 case ir_unop_log2: result = nir_flog2(&b, srcs[0]); break;
1898 case ir_unop_i2f:
1899 case ir_unop_u2f:
1900 case ir_unop_b2f:
1901 case ir_unop_f2i:
1902 case ir_unop_f2u:
1903 case ir_unop_f2b:
1904 case ir_unop_i2b:
1905 case ir_unop_b2i:
1906 case ir_unop_b2i64:
1907 case ir_unop_d2f:
1908 case ir_unop_f2d:
1909 case ir_unop_f162f:
1910 case ir_unop_f2f16:
1911 case ir_unop_f162b:
1912 case ir_unop_b2f16:
1913 case ir_unop_d2i:
1914 case ir_unop_d2u:
1915 case ir_unop_d2b:
1916 case ir_unop_i2d:
1917 case ir_unop_u2d:
1918 case ir_unop_i642i:
1919 case ir_unop_i642u:
1920 case ir_unop_i642f:
1921 case ir_unop_i642b:
1922 case ir_unop_i642d:
1923 case ir_unop_u642i:
1924 case ir_unop_u642u:
1925 case ir_unop_u642f:
1926 case ir_unop_u642d:
1927 case ir_unop_i2i64:
1928 case ir_unop_u2i64:
1929 case ir_unop_f2i64:
1930 case ir_unop_d2i64:
1931 case ir_unop_i2u64:
1932 case ir_unop_u2u64:
1933 case ir_unop_f2u64:
1934 case ir_unop_d2u64:
1935 case ir_unop_i2u:
1936 case ir_unop_u2i:
1937 case ir_unop_i642u64:
1938 case ir_unop_u642i64: {
1939 nir_alu_type src_type = nir_get_nir_type_for_glsl_base_type(types[0]);
1940 nir_alu_type dst_type = nir_get_nir_type_for_glsl_base_type(out_type);
1941 result = nir_build_alu(&b, nir_type_conversion_op(src_type, dst_type,
1942 nir_rounding_mode_undef),
1943 srcs[0], NULL, NULL, NULL);
1944 /* b2i and b2f don't have fixed bit-size versions so the builder will
1945 * just assume 32 and we have to fix it up here.
1946 */
1947 result->bit_size = nir_alu_type_get_type_size(dst_type);
1948 break;
1949 }
1950
1951 case ir_unop_f2fmp: {
1952 result = nir_build_alu(&b, nir_op_f2fmp, srcs[0], NULL, NULL, NULL);
1953 break;
1954 }
1955
1956 case ir_unop_bitcast_i2f:
1957 case ir_unop_bitcast_f2i:
1958 case ir_unop_bitcast_u2f:
1959 case ir_unop_bitcast_f2u:
1960 case ir_unop_bitcast_i642d:
1961 case ir_unop_bitcast_d2i64:
1962 case ir_unop_bitcast_u642d:
1963 case ir_unop_bitcast_d2u64:
1964 case ir_unop_subroutine_to_int:
1965 /* no-op */
1966 result = nir_mov(&b, srcs[0]);
1967 break;
1968 case ir_unop_trunc: result = nir_ftrunc(&b, srcs[0]); break;
1969 case ir_unop_ceil: result = nir_fceil(&b, srcs[0]); break;
1970 case ir_unop_floor: result = nir_ffloor(&b, srcs[0]); break;
1971 case ir_unop_fract: result = nir_ffract(&b, srcs[0]); break;
1972 case ir_unop_frexp_exp: result = nir_frexp_exp(&b, srcs[0]); break;
1973 case ir_unop_frexp_sig: result = nir_frexp_sig(&b, srcs[0]); break;
1974 case ir_unop_round_even: result = nir_fround_even(&b, srcs[0]); break;
1975 case ir_unop_sin: result = nir_fsin(&b, srcs[0]); break;
1976 case ir_unop_cos: result = nir_fcos(&b, srcs[0]); break;
1977 case ir_unop_dFdx: result = nir_fddx(&b, srcs[0]); break;
1978 case ir_unop_dFdy: result = nir_fddy(&b, srcs[0]); break;
1979 case ir_unop_dFdx_fine: result = nir_fddx_fine(&b, srcs[0]); break;
1980 case ir_unop_dFdy_fine: result = nir_fddy_fine(&b, srcs[0]); break;
1981 case ir_unop_dFdx_coarse: result = nir_fddx_coarse(&b, srcs[0]); break;
1982 case ir_unop_dFdy_coarse: result = nir_fddy_coarse(&b, srcs[0]); break;
1983 case ir_unop_pack_snorm_2x16:
1984 result = nir_pack_snorm_2x16(&b, srcs[0]);
1985 break;
1986 case ir_unop_pack_snorm_4x8:
1987 result = nir_pack_snorm_4x8(&b, srcs[0]);
1988 break;
1989 case ir_unop_pack_unorm_2x16:
1990 result = nir_pack_unorm_2x16(&b, srcs[0]);
1991 break;
1992 case ir_unop_pack_unorm_4x8:
1993 result = nir_pack_unorm_4x8(&b, srcs[0]);
1994 break;
1995 case ir_unop_pack_half_2x16:
1996 result = nir_pack_half_2x16(&b, srcs[0]);
1997 break;
1998 case ir_unop_unpack_snorm_2x16:
1999 result = nir_unpack_snorm_2x16(&b, srcs[0]);
2000 break;
2001 case ir_unop_unpack_snorm_4x8:
2002 result = nir_unpack_snorm_4x8(&b, srcs[0]);
2003 break;
2004 case ir_unop_unpack_unorm_2x16:
2005 result = nir_unpack_unorm_2x16(&b, srcs[0]);
2006 break;
2007 case ir_unop_unpack_unorm_4x8:
2008 result = nir_unpack_unorm_4x8(&b, srcs[0]);
2009 break;
2010 case ir_unop_unpack_half_2x16:
2011 result = nir_unpack_half_2x16(&b, srcs[0]);
2012 break;
2013 case ir_unop_pack_sampler_2x32:
2014 case ir_unop_pack_image_2x32:
2015 case ir_unop_pack_double_2x32:
2016 case ir_unop_pack_int_2x32:
2017 case ir_unop_pack_uint_2x32:
2018 result = nir_pack_64_2x32(&b, srcs[0]);
2019 break;
2020 case ir_unop_unpack_sampler_2x32:
2021 case ir_unop_unpack_image_2x32:
2022 case ir_unop_unpack_double_2x32:
2023 case ir_unop_unpack_int_2x32:
2024 case ir_unop_unpack_uint_2x32:
2025 result = nir_unpack_64_2x32(&b, srcs[0]);
2026 break;
2027 case ir_unop_bitfield_reverse:
2028 result = nir_bitfield_reverse(&b, srcs[0]);
2029 break;
2030 case ir_unop_bit_count:
2031 result = nir_bit_count(&b, srcs[0]);
2032 break;
2033 case ir_unop_find_msb:
2034 switch (types[0]) {
2035 case GLSL_TYPE_UINT:
2036 result = nir_ufind_msb(&b, srcs[0]);
2037 break;
2038 case GLSL_TYPE_INT:
2039 result = nir_ifind_msb(&b, srcs[0]);
2040 break;
2041 default:
2042 unreachable("Invalid type for findMSB()");
2043 }
2044 break;
2045 case ir_unop_find_lsb:
2046 result = nir_find_lsb(&b, srcs[0]);
2047 break;
2048
2049 case ir_unop_noise:
2050 switch (ir->type->vector_elements) {
2051 case 1:
2052 switch (ir->operands[0]->type->vector_elements) {
2053 case 1: result = nir_fnoise1_1(&b, srcs[0]); break;
2054 case 2: result = nir_fnoise1_2(&b, srcs[0]); break;
2055 case 3: result = nir_fnoise1_3(&b, srcs[0]); break;
2056 case 4: result = nir_fnoise1_4(&b, srcs[0]); break;
2057 default: unreachable("not reached");
2058 }
2059 break;
2060 case 2:
2061 switch (ir->operands[0]->type->vector_elements) {
2062 case 1: result = nir_fnoise2_1(&b, srcs[0]); break;
2063 case 2: result = nir_fnoise2_2(&b, srcs[0]); break;
2064 case 3: result = nir_fnoise2_3(&b, srcs[0]); break;
2065 case 4: result = nir_fnoise2_4(&b, srcs[0]); break;
2066 default: unreachable("not reached");
2067 }
2068 break;
2069 case 3:
2070 switch (ir->operands[0]->type->vector_elements) {
2071 case 1: result = nir_fnoise3_1(&b, srcs[0]); break;
2072 case 2: result = nir_fnoise3_2(&b, srcs[0]); break;
2073 case 3: result = nir_fnoise3_3(&b, srcs[0]); break;
2074 case 4: result = nir_fnoise3_4(&b, srcs[0]); break;
2075 default: unreachable("not reached");
2076 }
2077 break;
2078 case 4:
2079 switch (ir->operands[0]->type->vector_elements) {
2080 case 1: result = nir_fnoise4_1(&b, srcs[0]); break;
2081 case 2: result = nir_fnoise4_2(&b, srcs[0]); break;
2082 case 3: result = nir_fnoise4_3(&b, srcs[0]); break;
2083 case 4: result = nir_fnoise4_4(&b, srcs[0]); break;
2084 default: unreachable("not reached");
2085 }
2086 break;
2087 default:
2088 unreachable("not reached");
2089 }
2090 break;
2091 case ir_unop_get_buffer_size: {
2092 nir_intrinsic_instr *load = nir_intrinsic_instr_create(
2093 this->shader,
2094 nir_intrinsic_get_buffer_size);
2095 load->num_components = ir->type->vector_elements;
2096 load->src[0] = nir_src_for_ssa(evaluate_rvalue(ir->operands[0]));
2097 unsigned bit_size = glsl_get_bit_size(ir->type);
2098 add_instr(&load->instr, ir->type->vector_elements, bit_size);
2099 return;
2100 }
2101
2102 case ir_unop_atan:
2103 result = nir_atan(&b, srcs[0]);
2104 break;
2105
2106 case ir_binop_add:
2107 result = type_is_float(out_type) ? nir_fadd(&b, srcs[0], srcs[1])
2108 : nir_iadd(&b, srcs[0], srcs[1]);
2109 break;
2110 case ir_binop_add_sat:
2111 result = type_is_signed(out_type) ? nir_iadd_sat(&b, srcs[0], srcs[1])
2112 : nir_uadd_sat(&b, srcs[0], srcs[1]);
2113 break;
2114 case ir_binop_sub:
2115 result = type_is_float(out_type) ? nir_fsub(&b, srcs[0], srcs[1])
2116 : nir_isub(&b, srcs[0], srcs[1]);
2117 break;
2118 case ir_binop_sub_sat:
2119 result = type_is_signed(out_type) ? nir_isub_sat(&b, srcs[0], srcs[1])
2120 : nir_usub_sat(&b, srcs[0], srcs[1]);
2121 break;
2122 case ir_binop_abs_sub:
2123 /* out_type is always unsigned for ir_binop_abs_sub, so we have to key
2124 * on the type of the sources.
2125 */
2126 result = type_is_signed(types[0]) ? nir_uabs_isub(&b, srcs[0], srcs[1])
2127 : nir_uabs_usub(&b, srcs[0], srcs[1]);
2128 break;
2129 case ir_binop_avg:
2130 result = type_is_signed(out_type) ? nir_ihadd(&b, srcs[0], srcs[1])
2131 : nir_uhadd(&b, srcs[0], srcs[1]);
2132 break;
2133 case ir_binop_avg_round:
2134 result = type_is_signed(out_type) ? nir_irhadd(&b, srcs[0], srcs[1])
2135 : nir_urhadd(&b, srcs[0], srcs[1]);
2136 break;
2137 case ir_binop_mul_32x16:
2138 result = type_is_signed(out_type) ? nir_imul_32x16(&b, srcs[0], srcs[1])
2139 : nir_umul_32x16(&b, srcs[0], srcs[1]);
2140 break;
2141 case ir_binop_mul:
2142 if (type_is_float(out_type))
2143 result = nir_fmul(&b, srcs[0], srcs[1]);
2144 else if (out_type == GLSL_TYPE_INT64 &&
2145 (ir->operands[0]->type->base_type == GLSL_TYPE_INT ||
2146 ir->operands[1]->type->base_type == GLSL_TYPE_INT))
2147 result = nir_imul_2x32_64(&b, srcs[0], srcs[1]);
2148 else if (out_type == GLSL_TYPE_UINT64 &&
2149 (ir->operands[0]->type->base_type == GLSL_TYPE_UINT ||
2150 ir->operands[1]->type->base_type == GLSL_TYPE_UINT))
2151 result = nir_umul_2x32_64(&b, srcs[0], srcs[1]);
2152 else
2153 result = nir_imul(&b, srcs[0], srcs[1]);
2154 break;
2155 case ir_binop_div:
2156 if (type_is_float(out_type))
2157 result = nir_fdiv(&b, srcs[0], srcs[1]);
2158 else if (type_is_signed(out_type))
2159 result = nir_idiv(&b, srcs[0], srcs[1]);
2160 else
2161 result = nir_udiv(&b, srcs[0], srcs[1]);
2162 break;
2163 case ir_binop_mod:
2164 result = type_is_float(out_type) ? nir_fmod(&b, srcs[0], srcs[1])
2165 : nir_umod(&b, srcs[0], srcs[1]);
2166 break;
2167 case ir_binop_min:
2168 if (type_is_float(out_type))
2169 result = nir_fmin(&b, srcs[0], srcs[1]);
2170 else if (type_is_signed(out_type))
2171 result = nir_imin(&b, srcs[0], srcs[1]);
2172 else
2173 result = nir_umin(&b, srcs[0], srcs[1]);
2174 break;
2175 case ir_binop_max:
2176 if (type_is_float(out_type))
2177 result = nir_fmax(&b, srcs[0], srcs[1]);
2178 else if (type_is_signed(out_type))
2179 result = nir_imax(&b, srcs[0], srcs[1]);
2180 else
2181 result = nir_umax(&b, srcs[0], srcs[1]);
2182 break;
2183 case ir_binop_pow: result = nir_fpow(&b, srcs[0], srcs[1]); break;
2184 case ir_binop_bit_and: result = nir_iand(&b, srcs[0], srcs[1]); break;
2185 case ir_binop_bit_or: result = nir_ior(&b, srcs[0], srcs[1]); break;
2186 case ir_binop_bit_xor: result = nir_ixor(&b, srcs[0], srcs[1]); break;
2187 case ir_binop_logic_and:
2188 result = nir_iand(&b, srcs[0], srcs[1]);
2189 break;
2190 case ir_binop_logic_or:
2191 result = nir_ior(&b, srcs[0], srcs[1]);
2192 break;
2193 case ir_binop_logic_xor:
2194 result = nir_ixor(&b, srcs[0], srcs[1]);
2195 break;
2196 case ir_binop_lshift: result = nir_ishl(&b, srcs[0], srcs[1]); break;
2197 case ir_binop_rshift:
2198 result = (type_is_signed(out_type)) ? nir_ishr(&b, srcs[0], srcs[1])
2199 : nir_ushr(&b, srcs[0], srcs[1]);
2200 break;
2201 case ir_binop_imul_high:
2202 result = (out_type == GLSL_TYPE_INT) ? nir_imul_high(&b, srcs[0], srcs[1])
2203 : nir_umul_high(&b, srcs[0], srcs[1]);
2204 break;
2205 case ir_binop_carry: result = nir_uadd_carry(&b, srcs[0], srcs[1]); break;
2206 case ir_binop_borrow: result = nir_usub_borrow(&b, srcs[0], srcs[1]); break;
2207 case ir_binop_less:
2208 if (type_is_float(types[0]))
2209 result = nir_flt(&b, srcs[0], srcs[1]);
2210 else if (type_is_signed(types[0]))
2211 result = nir_ilt(&b, srcs[0], srcs[1]);
2212 else
2213 result = nir_ult(&b, srcs[0], srcs[1]);
2214 break;
2215 case ir_binop_gequal:
2216 if (type_is_float(types[0]))
2217 result = nir_fge(&b, srcs[0], srcs[1]);
2218 else if (type_is_signed(types[0]))
2219 result = nir_ige(&b, srcs[0], srcs[1]);
2220 else
2221 result = nir_uge(&b, srcs[0], srcs[1]);
2222 break;
2223 case ir_binop_equal:
2224 if (type_is_float(types[0]))
2225 result = nir_feq(&b, srcs[0], srcs[1]);
2226 else
2227 result = nir_ieq(&b, srcs[0], srcs[1]);
2228 break;
2229 case ir_binop_nequal:
2230 if (type_is_float(types[0]))
2231 result = nir_fne(&b, srcs[0], srcs[1]);
2232 else
2233 result = nir_ine(&b, srcs[0], srcs[1]);
2234 break;
2235 case ir_binop_all_equal:
2236 if (type_is_float(types[0])) {
2237 switch (ir->operands[0]->type->vector_elements) {
2238 case 1: result = nir_feq(&b, srcs[0], srcs[1]); break;
2239 case 2: result = nir_ball_fequal2(&b, srcs[0], srcs[1]); break;
2240 case 3: result = nir_ball_fequal3(&b, srcs[0], srcs[1]); break;
2241 case 4: result = nir_ball_fequal4(&b, srcs[0], srcs[1]); break;
2242 default:
2243 unreachable("not reached");
2244 }
2245 } else {
2246 switch (ir->operands[0]->type->vector_elements) {
2247 case 1: result = nir_ieq(&b, srcs[0], srcs[1]); break;
2248 case 2: result = nir_ball_iequal2(&b, srcs[0], srcs[1]); break;
2249 case 3: result = nir_ball_iequal3(&b, srcs[0], srcs[1]); break;
2250 case 4: result = nir_ball_iequal4(&b, srcs[0], srcs[1]); break;
2251 default:
2252 unreachable("not reached");
2253 }
2254 }
2255 break;
2256 case ir_binop_any_nequal:
2257 if (type_is_float(types[0])) {
2258 switch (ir->operands[0]->type->vector_elements) {
2259 case 1: result = nir_fne(&b, srcs[0], srcs[1]); break;
2260 case 2: result = nir_bany_fnequal2(&b, srcs[0], srcs[1]); break;
2261 case 3: result = nir_bany_fnequal3(&b, srcs[0], srcs[1]); break;
2262 case 4: result = nir_bany_fnequal4(&b, srcs[0], srcs[1]); break;
2263 default:
2264 unreachable("not reached");
2265 }
2266 } else {
2267 switch (ir->operands[0]->type->vector_elements) {
2268 case 1: result = nir_ine(&b, srcs[0], srcs[1]); break;
2269 case 2: result = nir_bany_inequal2(&b, srcs[0], srcs[1]); break;
2270 case 3: result = nir_bany_inequal3(&b, srcs[0], srcs[1]); break;
2271 case 4: result = nir_bany_inequal4(&b, srcs[0], srcs[1]); break;
2272 default:
2273 unreachable("not reached");
2274 }
2275 }
2276 break;
2277 case ir_binop_dot:
2278 switch (ir->operands[0]->type->vector_elements) {
2279 case 2: result = nir_fdot2(&b, srcs[0], srcs[1]); break;
2280 case 3: result = nir_fdot3(&b, srcs[0], srcs[1]); break;
2281 case 4: result = nir_fdot4(&b, srcs[0], srcs[1]); break;
2282 default:
2283 unreachable("not reached");
2284 }
2285 break;
2286 case ir_binop_vector_extract: {
2287 result = nir_channel(&b, srcs[0], 0);
2288 for (unsigned i = 1; i < ir->operands[0]->type->vector_elements; i++) {
2289 nir_ssa_def *swizzled = nir_channel(&b, srcs[0], i);
2290 result = nir_bcsel(&b, nir_ieq(&b, srcs[1], nir_imm_int(&b, i)),
2291 swizzled, result);
2292 }
2293 break;
2294 }
2295
2296 case ir_binop_atan2:
2297 result = nir_atan2(&b, srcs[0], srcs[1]);
2298 break;
2299
2300 case ir_binop_ldexp: result = nir_ldexp(&b, srcs[0], srcs[1]); break;
2301 case ir_triop_fma:
2302 result = nir_ffma(&b, srcs[0], srcs[1], srcs[2]);
2303 break;
2304 case ir_triop_lrp:
2305 result = nir_flrp(&b, srcs[0], srcs[1], srcs[2]);
2306 break;
2307 case ir_triop_csel:
2308 result = nir_bcsel(&b, srcs[0], srcs[1], srcs[2]);
2309 break;
2310 case ir_triop_bitfield_extract:
2311 result = (out_type == GLSL_TYPE_INT) ?
2312 nir_ibitfield_extract(&b, srcs[0], srcs[1], srcs[2]) :
2313 nir_ubitfield_extract(&b, srcs[0], srcs[1], srcs[2]);
2314 break;
2315 case ir_quadop_bitfield_insert:
2316 result = nir_bitfield_insert(&b, srcs[0], srcs[1], srcs[2], srcs[3]);
2317 break;
2318 case ir_quadop_vector:
2319 result = nir_vec(&b, srcs, ir->type->vector_elements);
2320 break;
2321
2322 default:
2323 unreachable("not reached");
2324 }
2325 }
2326
2327 void
2328 nir_visitor::visit(ir_swizzle *ir)
2329 {
2330 unsigned swizzle[4] = { ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w };
2331 result = nir_swizzle(&b, evaluate_rvalue(ir->val), swizzle,
2332 ir->type->vector_elements);
2333 }
2334
2335 void
2336 nir_visitor::visit(ir_texture *ir)
2337 {
2338 unsigned num_srcs;
2339 nir_texop op;
2340 switch (ir->op) {
2341 case ir_tex:
2342 op = nir_texop_tex;
2343 num_srcs = 1; /* coordinate */
2344 break;
2345
2346 case ir_txb:
2347 case ir_txl:
2348 op = (ir->op == ir_txb) ? nir_texop_txb : nir_texop_txl;
2349 num_srcs = 2; /* coordinate, bias/lod */
2350 break;
2351
2352 case ir_txd:
2353 op = nir_texop_txd; /* coordinate, dPdx, dPdy */
2354 num_srcs = 3;
2355 break;
2356
2357 case ir_txf:
2358 op = nir_texop_txf;
2359 if (ir->lod_info.lod != NULL)
2360 num_srcs = 2; /* coordinate, lod */
2361 else
2362 num_srcs = 1; /* coordinate */
2363 break;
2364
2365 case ir_txf_ms:
2366 op = nir_texop_txf_ms;
2367 num_srcs = 2; /* coordinate, sample_index */
2368 break;
2369
2370 case ir_txs:
2371 op = nir_texop_txs;
2372 if (ir->lod_info.lod != NULL)
2373 num_srcs = 1; /* lod */
2374 else
2375 num_srcs = 0;
2376 break;
2377
2378 case ir_lod:
2379 op = nir_texop_lod;
2380 num_srcs = 1; /* coordinate */
2381 break;
2382
2383 case ir_tg4:
2384 op = nir_texop_tg4;
2385 num_srcs = 1; /* coordinate */
2386 break;
2387
2388 case ir_query_levels:
2389 op = nir_texop_query_levels;
2390 num_srcs = 0;
2391 break;
2392
2393 case ir_texture_samples:
2394 op = nir_texop_texture_samples;
2395 num_srcs = 0;
2396 break;
2397
2398 case ir_samples_identical:
2399 op = nir_texop_samples_identical;
2400 num_srcs = 1; /* coordinate */
2401 break;
2402
2403 default:
2404 unreachable("not reached");
2405 }
2406
2407 if (ir->projector != NULL)
2408 num_srcs++;
2409 if (ir->shadow_comparator != NULL)
2410 num_srcs++;
2411 /* offsets are constants we store inside nir_tex_intrs.offsets */
2412 if (ir->offset != NULL && !ir->offset->type->is_array())
2413 num_srcs++;
2414
2415 /* Add one for the texture deref */
2416 num_srcs += 2;
2417
2418 nir_tex_instr *instr = nir_tex_instr_create(this->shader, num_srcs);
2419
2420 instr->op = op;
2421 instr->sampler_dim =
2422 (glsl_sampler_dim) ir->sampler->type->sampler_dimensionality;
2423 instr->is_array = ir->sampler->type->sampler_array;
2424 instr->is_shadow = ir->sampler->type->sampler_shadow;
2425 if (instr->is_shadow)
2426 instr->is_new_style_shadow = (ir->type->vector_elements == 1);
2427 switch (ir->type->base_type) {
2428 case GLSL_TYPE_FLOAT:
2429 instr->dest_type = nir_type_float;
2430 break;
2431 case GLSL_TYPE_FLOAT16:
2432 instr->dest_type = nir_type_float16;
2433 break;
2434 case GLSL_TYPE_INT:
2435 instr->dest_type = nir_type_int;
2436 break;
2437 case GLSL_TYPE_BOOL:
2438 case GLSL_TYPE_UINT:
2439 instr->dest_type = nir_type_uint;
2440 break;
2441 default:
2442 unreachable("not reached");
2443 }
2444
2445 nir_deref_instr *sampler_deref = evaluate_deref(ir->sampler);
2446
2447 /* check for bindless handles */
2448 if (sampler_deref->mode != nir_var_uniform ||
2449 nir_deref_instr_get_variable(sampler_deref)->data.bindless) {
2450 nir_ssa_def *load = nir_load_deref(&b, sampler_deref);
2451 instr->src[0].src = nir_src_for_ssa(load);
2452 instr->src[0].src_type = nir_tex_src_texture_handle;
2453 instr->src[1].src = nir_src_for_ssa(load);
2454 instr->src[1].src_type = nir_tex_src_sampler_handle;
2455 } else {
2456 instr->src[0].src = nir_src_for_ssa(&sampler_deref->dest.ssa);
2457 instr->src[0].src_type = nir_tex_src_texture_deref;
2458 instr->src[1].src = nir_src_for_ssa(&sampler_deref->dest.ssa);
2459 instr->src[1].src_type = nir_tex_src_sampler_deref;
2460 }
2461
2462 unsigned src_number = 2;
2463
2464 if (ir->coordinate != NULL) {
2465 instr->coord_components = ir->coordinate->type->vector_elements;
2466 instr->src[src_number].src =
2467 nir_src_for_ssa(evaluate_rvalue(ir->coordinate));
2468 instr->src[src_number].src_type = nir_tex_src_coord;
2469 src_number++;
2470 }
2471
2472 if (ir->projector != NULL) {
2473 instr->src[src_number].src =
2474 nir_src_for_ssa(evaluate_rvalue(ir->projector));
2475 instr->src[src_number].src_type = nir_tex_src_projector;
2476 src_number++;
2477 }
2478
2479 if (ir->shadow_comparator != NULL) {
2480 instr->src[src_number].src =
2481 nir_src_for_ssa(evaluate_rvalue(ir->shadow_comparator));
2482 instr->src[src_number].src_type = nir_tex_src_comparator;
2483 src_number++;
2484 }
2485
2486 if (ir->offset != NULL) {
2487 if (ir->offset->type->is_array()) {
2488 for (int i = 0; i < ir->offset->type->array_size(); i++) {
2489 const ir_constant *c =
2490 ir->offset->as_constant()->get_array_element(i);
2491
2492 for (unsigned j = 0; j < 2; ++j) {
2493 int val = c->get_int_component(j);
2494 assert(val <= 31 && val >= -32);
2495 instr->tg4_offsets[i][j] = val;
2496 }
2497 }
2498 } else {
2499 assert(ir->offset->type->is_vector() || ir->offset->type->is_scalar());
2500
2501 instr->src[src_number].src =
2502 nir_src_for_ssa(evaluate_rvalue(ir->offset));
2503 instr->src[src_number].src_type = nir_tex_src_offset;
2504 src_number++;
2505 }
2506 }
2507
2508 switch (ir->op) {
2509 case ir_txb:
2510 instr->src[src_number].src =
2511 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.bias));
2512 instr->src[src_number].src_type = nir_tex_src_bias;
2513 src_number++;
2514 break;
2515
2516 case ir_txl:
2517 case ir_txf:
2518 case ir_txs:
2519 if (ir->lod_info.lod != NULL) {
2520 instr->src[src_number].src =
2521 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.lod));
2522 instr->src[src_number].src_type = nir_tex_src_lod;
2523 src_number++;
2524 }
2525 break;
2526
2527 case ir_txd:
2528 instr->src[src_number].src =
2529 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdx));
2530 instr->src[src_number].src_type = nir_tex_src_ddx;
2531 src_number++;
2532 instr->src[src_number].src =
2533 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.grad.dPdy));
2534 instr->src[src_number].src_type = nir_tex_src_ddy;
2535 src_number++;
2536 break;
2537
2538 case ir_txf_ms:
2539 instr->src[src_number].src =
2540 nir_src_for_ssa(evaluate_rvalue(ir->lod_info.sample_index));
2541 instr->src[src_number].src_type = nir_tex_src_ms_index;
2542 src_number++;
2543 break;
2544
2545 case ir_tg4:
2546 instr->component = ir->lod_info.component->as_constant()->value.u[0];
2547 break;
2548
2549 default:
2550 break;
2551 }
2552
2553 assert(src_number == num_srcs);
2554
2555 unsigned bit_size = glsl_get_bit_size(ir->type);
2556 add_instr(&instr->instr, nir_tex_instr_dest_size(instr), bit_size);
2557 }
2558
2559 void
2560 nir_visitor::visit(ir_constant *ir)
2561 {
2562 /*
2563 * We don't know if this variable is an array or struct that gets
2564 * dereferenced, so do the safe thing an make it a variable with a
2565 * constant initializer and return a dereference.
2566 */
2567
2568 nir_variable *var =
2569 nir_local_variable_create(this->impl, ir->type, "const_temp");
2570 var->data.read_only = true;
2571 var->constant_initializer = constant_copy(ir, var);
2572
2573 this->deref = nir_build_deref_var(&b, var);
2574 }
2575
2576 void
2577 nir_visitor::visit(ir_dereference_variable *ir)
2578 {
2579 if (ir->variable_referenced()->data.mode == ir_var_function_out) {
2580 unsigned i = (sig->return_type != glsl_type::void_type) ? 1 : 0;
2581
2582 foreach_in_list(ir_variable, param, &sig->parameters) {
2583 if (param == ir->variable_referenced()) {
2584 break;
2585 }
2586 i++;
2587 }
2588
2589 this->deref = nir_build_deref_cast(&b, nir_load_param(&b, i),
2590 nir_var_function_temp, ir->type, 0);
2591 return;
2592 }
2593
2594 assert(ir->variable_referenced()->data.mode != ir_var_function_inout);
2595
2596 struct hash_entry *entry =
2597 _mesa_hash_table_search(this->var_table, ir->var);
2598 assert(entry);
2599 nir_variable *var = (nir_variable *) entry->data;
2600
2601 this->deref = nir_build_deref_var(&b, var);
2602 }
2603
2604 void
2605 nir_visitor::visit(ir_dereference_record *ir)
2606 {
2607 ir->record->accept(this);
2608
2609 int field_index = ir->field_idx;
2610 assert(field_index >= 0);
2611
2612 this->deref = nir_build_deref_struct(&b, this->deref, field_index);
2613 }
2614
2615 void
2616 nir_visitor::visit(ir_dereference_array *ir)
2617 {
2618 nir_ssa_def *index = evaluate_rvalue(ir->array_index);
2619
2620 ir->array->accept(this);
2621
2622 this->deref = nir_build_deref_array(&b, this->deref, index);
2623 }
2624
2625 void
2626 nir_visitor::visit(ir_barrier *)
2627 {
2628 if (shader->info.stage == MESA_SHADER_COMPUTE) {
2629 nir_intrinsic_instr *shared_barrier =
2630 nir_intrinsic_instr_create(this->shader,
2631 nir_intrinsic_memory_barrier_shared);
2632 nir_builder_instr_insert(&b, &shared_barrier->instr);
2633 } else if (shader->info.stage == MESA_SHADER_TESS_CTRL) {
2634 nir_intrinsic_instr *patch_barrier =
2635 nir_intrinsic_instr_create(this->shader,
2636 nir_intrinsic_memory_barrier_tcs_patch);
2637 nir_builder_instr_insert(&b, &patch_barrier->instr);
2638 }
2639
2640 nir_intrinsic_instr *instr =
2641 nir_intrinsic_instr_create(this->shader, nir_intrinsic_control_barrier);
2642 nir_builder_instr_insert(&b, &instr->instr);
2643 }
2644
2645 nir_shader *
2646 glsl_float64_funcs_to_nir(struct gl_context *ctx,
2647 const nir_shader_compiler_options *options)
2648 {
2649 /* We pretend it's a vertex shader. Ultimately, the stage shouldn't
2650 * matter because we're not optimizing anything here.
2651 */
2652 struct gl_shader *sh = _mesa_new_shader(-1, MESA_SHADER_VERTEX);
2653 sh->Source = float64_source;
2654 sh->CompileStatus = COMPILE_FAILURE;
2655 _mesa_glsl_compile_shader(ctx, sh, false, false, true);
2656
2657 if (!sh->CompileStatus) {
2658 if (sh->InfoLog) {
2659 _mesa_problem(ctx,
2660 "fp64 software impl compile failed:\n%s\nsource:\n%s\n",
2661 sh->InfoLog, float64_source);
2662 }
2663 return NULL;
2664 }
2665
2666 nir_shader *nir = nir_shader_create(NULL, MESA_SHADER_VERTEX, options, NULL);
2667
2668 nir_visitor v1(ctx, nir);
2669 nir_function_visitor v2(&v1);
2670 v2.run(sh->ir);
2671 visit_exec_list(sh->ir, &v1);
2672
2673 /* _mesa_delete_shader will try to free sh->Source but it's static const */
2674 sh->Source = NULL;
2675 _mesa_delete_shader(ctx, sh);
2676
2677 nir_validate_shader(nir, "float64_funcs_to_nir");
2678
2679 NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp);
2680 NIR_PASS_V(nir, nir_lower_returns);
2681 NIR_PASS_V(nir, nir_inline_functions);
2682 NIR_PASS_V(nir, nir_opt_deref);
2683
2684 /* Do some optimizations to clean up the shader now. By optimizing the
2685 * functions in the library, we avoid having to re-do that work every
2686 * time we inline a copy of a function. Reducing basic blocks also helps
2687 * with compile times.
2688 */
2689 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
2690 NIR_PASS_V(nir, nir_copy_prop);
2691 NIR_PASS_V(nir, nir_opt_dce);
2692 NIR_PASS_V(nir, nir_opt_cse);
2693 NIR_PASS_V(nir, nir_opt_gcm, true);
2694 NIR_PASS_V(nir, nir_opt_peephole_select, 1, false, false);
2695 NIR_PASS_V(nir, nir_opt_dce);
2696
2697 return nir;
2698 }