cb5f18eb69bc7de3efa5c360479f434d5ad64656
[mesa.git] / src / intel / compiler / brw_nir.c
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_nir.h"
25 #include "brw_shader.h"
26 #include "common/gen_debug.h"
27 #include "compiler/glsl_types.h"
28 #include "compiler/nir/nir_builder.h"
29
30 static bool
31 is_input(nir_intrinsic_instr *intrin)
32 {
33 return intrin->intrinsic == nir_intrinsic_load_input ||
34 intrin->intrinsic == nir_intrinsic_load_per_vertex_input ||
35 intrin->intrinsic == nir_intrinsic_load_interpolated_input;
36 }
37
38 static bool
39 is_output(nir_intrinsic_instr *intrin)
40 {
41 return intrin->intrinsic == nir_intrinsic_load_output ||
42 intrin->intrinsic == nir_intrinsic_load_per_vertex_output ||
43 intrin->intrinsic == nir_intrinsic_store_output ||
44 intrin->intrinsic == nir_intrinsic_store_per_vertex_output;
45 }
46
47 /**
48 * In many cases, we just add the base and offset together, so there's no
49 * reason to keep them separate. Sometimes, combining them is essential:
50 * if a shader only accesses part of a compound variable (such as a matrix
51 * or array), the variable's base may not actually exist in the VUE map.
52 *
53 * This pass adds constant offsets to instr->const_index[0], and resets
54 * the offset source to 0. Non-constant offsets remain unchanged - since
55 * we don't know what part of a compound variable is accessed, we allocate
56 * storage for the entire thing.
57 */
58
59 static bool
60 add_const_offset_to_base_block(nir_block *block, nir_builder *b,
61 nir_variable_mode mode)
62 {
63 nir_foreach_instr_safe(instr, block) {
64 if (instr->type != nir_instr_type_intrinsic)
65 continue;
66
67 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
68
69 if ((mode == nir_var_shader_in && is_input(intrin)) ||
70 (mode == nir_var_shader_out && is_output(intrin))) {
71 nir_src *offset = nir_get_io_offset_src(intrin);
72 nir_const_value *const_offset = nir_src_as_const_value(*offset);
73
74 if (const_offset) {
75 intrin->const_index[0] += const_offset->u32[0];
76 b->cursor = nir_before_instr(&intrin->instr);
77 nir_instr_rewrite_src(&intrin->instr, offset,
78 nir_src_for_ssa(nir_imm_int(b, 0)));
79 }
80 }
81 }
82 return true;
83 }
84
85 static void
86 add_const_offset_to_base(nir_shader *nir, nir_variable_mode mode)
87 {
88 nir_foreach_function(f, nir) {
89 if (f->impl) {
90 nir_builder b;
91 nir_builder_init(&b, f->impl);
92 nir_foreach_block(block, f->impl) {
93 add_const_offset_to_base_block(block, &b, mode);
94 }
95 }
96 }
97 }
98
99 static bool
100 remap_tess_levels(nir_builder *b, nir_intrinsic_instr *intr,
101 GLenum primitive_mode)
102 {
103 const int location = nir_intrinsic_base(intr);
104 const unsigned component = nir_intrinsic_component(intr);
105 bool out_of_bounds;
106
107 if (location == VARYING_SLOT_TESS_LEVEL_INNER) {
108 switch (primitive_mode) {
109 case GL_QUADS:
110 /* gl_TessLevelInner[0..1] lives at DWords 3-2 (reversed). */
111 nir_intrinsic_set_base(intr, 0);
112 nir_intrinsic_set_component(intr, 3 - component);
113 out_of_bounds = false;
114 break;
115 case GL_TRIANGLES:
116 /* gl_TessLevelInner[0] lives at DWord 4. */
117 nir_intrinsic_set_base(intr, 1);
118 out_of_bounds = component > 0;
119 break;
120 case GL_ISOLINES:
121 out_of_bounds = true;
122 break;
123 default:
124 unreachable("Bogus tessellation domain");
125 }
126 } else if (location == VARYING_SLOT_TESS_LEVEL_OUTER) {
127 if (primitive_mode == GL_ISOLINES) {
128 /* gl_TessLevelOuter[0..1] lives at DWords 6-7 (in order). */
129 nir_intrinsic_set_base(intr, 1);
130 nir_intrinsic_set_component(intr, 2 + nir_intrinsic_component(intr));
131 out_of_bounds = component > 1;
132 } else {
133 /* Triangles use DWords 7-5 (reversed); Quads use 7-4 (reversed) */
134 nir_intrinsic_set_base(intr, 1);
135 nir_intrinsic_set_component(intr, 3 - nir_intrinsic_component(intr));
136 out_of_bounds = component == 3 && primitive_mode == GL_TRIANGLES;
137 }
138 } else {
139 return false;
140 }
141
142 if (out_of_bounds) {
143 if (nir_intrinsic_infos[intr->intrinsic].has_dest) {
144 b->cursor = nir_before_instr(&intr->instr);
145 nir_ssa_def *undef = nir_ssa_undef(b, 1, 32);
146 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(undef));
147 }
148 nir_instr_remove(&intr->instr);
149 }
150
151 return true;
152 }
153
154 static bool
155 remap_patch_urb_offsets(nir_block *block, nir_builder *b,
156 const struct brw_vue_map *vue_map,
157 GLenum tes_primitive_mode)
158 {
159 const bool is_passthrough_tcs = b->shader->info.name &&
160 strcmp(b->shader->info.name, "passthrough") == 0;
161
162 nir_foreach_instr_safe(instr, block) {
163 if (instr->type != nir_instr_type_intrinsic)
164 continue;
165
166 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
167
168 gl_shader_stage stage = b->shader->info.stage;
169
170 if ((stage == MESA_SHADER_TESS_CTRL && is_output(intrin)) ||
171 (stage == MESA_SHADER_TESS_EVAL && is_input(intrin))) {
172
173 if (!is_passthrough_tcs &&
174 remap_tess_levels(b, intrin, tes_primitive_mode))
175 continue;
176
177 int vue_slot = vue_map->varying_to_slot[intrin->const_index[0]];
178 assert(vue_slot != -1);
179 intrin->const_index[0] = vue_slot;
180
181 nir_src *vertex = nir_get_io_vertex_index_src(intrin);
182 if (vertex) {
183 nir_const_value *const_vertex = nir_src_as_const_value(*vertex);
184 if (const_vertex) {
185 intrin->const_index[0] += const_vertex->u32[0] *
186 vue_map->num_per_vertex_slots;
187 } else {
188 b->cursor = nir_before_instr(&intrin->instr);
189
190 /* Multiply by the number of per-vertex slots. */
191 nir_ssa_def *vertex_offset =
192 nir_imul(b,
193 nir_ssa_for_src(b, *vertex, 1),
194 nir_imm_int(b,
195 vue_map->num_per_vertex_slots));
196
197 /* Add it to the existing offset */
198 nir_src *offset = nir_get_io_offset_src(intrin);
199 nir_ssa_def *total_offset =
200 nir_iadd(b, vertex_offset,
201 nir_ssa_for_src(b, *offset, 1));
202
203 nir_instr_rewrite_src(&intrin->instr, offset,
204 nir_src_for_ssa(total_offset));
205 }
206 }
207 }
208 }
209 return true;
210 }
211
212 void
213 brw_nir_lower_vs_inputs(nir_shader *nir,
214 const uint8_t *vs_attrib_wa_flags)
215 {
216 /* Start with the location of the variable's base. */
217 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
218 var->data.driver_location = var->data.location;
219 }
220
221 /* Now use nir_lower_io to walk dereference chains. Attribute arrays are
222 * loaded as one vec4 or dvec4 per element (or matrix column), depending on
223 * whether it is a double-precision type or not.
224 */
225 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
226
227 /* This pass needs actual constants */
228 nir_opt_constant_folding(nir);
229
230 add_const_offset_to_base(nir, nir_var_shader_in);
231
232 brw_nir_apply_attribute_workarounds(nir, vs_attrib_wa_flags);
233
234 /* The last step is to remap VERT_ATTRIB_* to actual registers */
235
236 /* Whether or not we have any system generated values. gl_DrawID is not
237 * included here as it lives in its own vec4.
238 */
239 const bool has_sgvs =
240 nir->info.system_values_read &
241 (BITFIELD64_BIT(SYSTEM_VALUE_FIRST_VERTEX) |
242 BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE) |
243 BITFIELD64_BIT(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) |
244 BITFIELD64_BIT(SYSTEM_VALUE_INSTANCE_ID));
245
246 const unsigned num_inputs = _mesa_bitcount_64(nir->info.inputs_read);
247
248 nir_foreach_function(function, nir) {
249 if (!function->impl)
250 continue;
251
252 nir_builder b;
253 nir_builder_init(&b, function->impl);
254
255 nir_foreach_block(block, function->impl) {
256 nir_foreach_instr_safe(instr, block) {
257 if (instr->type != nir_instr_type_intrinsic)
258 continue;
259
260 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
261
262 switch (intrin->intrinsic) {
263 case nir_intrinsic_load_first_vertex:
264 case nir_intrinsic_load_base_instance:
265 case nir_intrinsic_load_vertex_id_zero_base:
266 case nir_intrinsic_load_instance_id:
267 case nir_intrinsic_load_is_indexed_draw:
268 case nir_intrinsic_load_draw_id: {
269 b.cursor = nir_after_instr(&intrin->instr);
270
271 /* gl_VertexID and friends are stored by the VF as the last
272 * vertex element. We convert them to load_input intrinsics at
273 * the right location.
274 */
275 nir_intrinsic_instr *load =
276 nir_intrinsic_instr_create(nir, nir_intrinsic_load_input);
277 load->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
278
279 nir_intrinsic_set_base(load, num_inputs);
280 switch (intrin->intrinsic) {
281 case nir_intrinsic_load_first_vertex:
282 nir_intrinsic_set_component(load, 0);
283 break;
284 case nir_intrinsic_load_base_instance:
285 nir_intrinsic_set_component(load, 1);
286 break;
287 case nir_intrinsic_load_vertex_id_zero_base:
288 nir_intrinsic_set_component(load, 2);
289 break;
290 case nir_intrinsic_load_instance_id:
291 nir_intrinsic_set_component(load, 3);
292 break;
293 case nir_intrinsic_load_draw_id:
294 case nir_intrinsic_load_is_indexed_draw:
295 /* gl_DrawID and IsIndexedDraw are stored right after
296 * gl_VertexID and friends if any of them exist.
297 */
298 nir_intrinsic_set_base(load, num_inputs + has_sgvs);
299 if (intrin->intrinsic == nir_intrinsic_load_draw_id)
300 nir_intrinsic_set_component(load, 0);
301 else
302 nir_intrinsic_set_component(load, 1);
303 break;
304 default:
305 unreachable("Invalid system value intrinsic");
306 }
307
308 load->num_components = 1;
309 nir_ssa_dest_init(&load->instr, &load->dest, 1, 32, NULL);
310 nir_builder_instr_insert(&b, &load->instr);
311
312 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
313 nir_src_for_ssa(&load->dest.ssa));
314 nir_instr_remove(&intrin->instr);
315 break;
316 }
317
318 case nir_intrinsic_load_input: {
319 /* Attributes come in a contiguous block, ordered by their
320 * gl_vert_attrib value. That means we can compute the slot
321 * number for an attribute by masking out the enabled attributes
322 * before it and counting the bits.
323 */
324 int attr = nir_intrinsic_base(intrin);
325 int slot = _mesa_bitcount_64(nir->info.inputs_read &
326 BITFIELD64_MASK(attr));
327 nir_intrinsic_set_base(intrin, slot);
328 break;
329 }
330
331 default:
332 break; /* Nothing to do */
333 }
334 }
335 }
336 }
337 }
338
339 void
340 brw_nir_lower_vue_inputs(nir_shader *nir,
341 const struct brw_vue_map *vue_map)
342 {
343 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
344 var->data.driver_location = var->data.location;
345 }
346
347 /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
348 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
349
350 /* This pass needs actual constants */
351 nir_opt_constant_folding(nir);
352
353 add_const_offset_to_base(nir, nir_var_shader_in);
354
355 nir_foreach_function(function, nir) {
356 if (!function->impl)
357 continue;
358
359 nir_foreach_block(block, function->impl) {
360 nir_foreach_instr(instr, block) {
361 if (instr->type != nir_instr_type_intrinsic)
362 continue;
363
364 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
365
366 if (intrin->intrinsic == nir_intrinsic_load_input ||
367 intrin->intrinsic == nir_intrinsic_load_per_vertex_input) {
368 /* Offset 0 is the VUE header, which contains
369 * VARYING_SLOT_LAYER [.y], VARYING_SLOT_VIEWPORT [.z], and
370 * VARYING_SLOT_PSIZ [.w].
371 */
372 int varying = nir_intrinsic_base(intrin);
373 int vue_slot;
374 switch (varying) {
375 case VARYING_SLOT_PSIZ:
376 nir_intrinsic_set_base(intrin, 0);
377 nir_intrinsic_set_component(intrin, 3);
378 break;
379
380 default:
381 vue_slot = vue_map->varying_to_slot[varying];
382 assert(vue_slot != -1);
383 nir_intrinsic_set_base(intrin, vue_slot);
384 break;
385 }
386 }
387 }
388 }
389 }
390 }
391
392 void
393 brw_nir_lower_tes_inputs(nir_shader *nir, const struct brw_vue_map *vue_map)
394 {
395 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
396 var->data.driver_location = var->data.location;
397 }
398
399 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
400
401 /* This pass needs actual constants */
402 nir_opt_constant_folding(nir);
403
404 add_const_offset_to_base(nir, nir_var_shader_in);
405
406 nir_foreach_function(function, nir) {
407 if (function->impl) {
408 nir_builder b;
409 nir_builder_init(&b, function->impl);
410 nir_foreach_block(block, function->impl) {
411 remap_patch_urb_offsets(block, &b, vue_map,
412 nir->info.tess.primitive_mode);
413 }
414 }
415 }
416 }
417
418 void
419 brw_nir_lower_fs_inputs(nir_shader *nir,
420 const struct gen_device_info *devinfo,
421 const struct brw_wm_prog_key *key)
422 {
423 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
424 var->data.driver_location = var->data.location;
425
426 /* Apply default interpolation mode.
427 *
428 * Everything defaults to smooth except for the legacy GL color
429 * built-in variables, which might be flat depending on API state.
430 */
431 if (var->data.interpolation == INTERP_MODE_NONE) {
432 const bool flat = key->flat_shade &&
433 (var->data.location == VARYING_SLOT_COL0 ||
434 var->data.location == VARYING_SLOT_COL1);
435
436 var->data.interpolation = flat ? INTERP_MODE_FLAT
437 : INTERP_MODE_SMOOTH;
438 }
439
440 /* On Ironlake and below, there is only one interpolation mode.
441 * Centroid interpolation doesn't mean anything on this hardware --
442 * there is no multisampling.
443 */
444 if (devinfo->gen < 6) {
445 var->data.centroid = false;
446 var->data.sample = false;
447 }
448 }
449
450 nir_lower_io_options lower_io_options = 0;
451 if (key->persample_interp)
452 lower_io_options |= nir_lower_io_force_sample_interpolation;
453
454 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, lower_io_options);
455
456 /* This pass needs actual constants */
457 nir_opt_constant_folding(nir);
458
459 add_const_offset_to_base(nir, nir_var_shader_in);
460 }
461
462 void
463 brw_nir_lower_vue_outputs(nir_shader *nir)
464 {
465 nir_foreach_variable(var, &nir->outputs) {
466 var->data.driver_location = var->data.location;
467 }
468
469 nir_lower_io(nir, nir_var_shader_out, type_size_vec4, 0);
470 }
471
472 void
473 brw_nir_lower_tcs_outputs(nir_shader *nir, const struct brw_vue_map *vue_map,
474 GLenum tes_primitive_mode)
475 {
476 nir_foreach_variable(var, &nir->outputs) {
477 var->data.driver_location = var->data.location;
478 }
479
480 nir_lower_io(nir, nir_var_shader_out, type_size_vec4, 0);
481
482 /* This pass needs actual constants */
483 nir_opt_constant_folding(nir);
484
485 add_const_offset_to_base(nir, nir_var_shader_out);
486
487 nir_foreach_function(function, nir) {
488 if (function->impl) {
489 nir_builder b;
490 nir_builder_init(&b, function->impl);
491 nir_foreach_block(block, function->impl) {
492 remap_patch_urb_offsets(block, &b, vue_map, tes_primitive_mode);
493 }
494 }
495 }
496 }
497
498 void
499 brw_nir_lower_fs_outputs(nir_shader *nir)
500 {
501 nir_foreach_variable(var, &nir->outputs) {
502 var->data.driver_location =
503 SET_FIELD(var->data.index, BRW_NIR_FRAG_OUTPUT_INDEX) |
504 SET_FIELD(var->data.location, BRW_NIR_FRAG_OUTPUT_LOCATION);
505 }
506
507 nir_lower_io(nir, nir_var_shader_out, type_size_dvec4, 0);
508 }
509
510 #define OPT(pass, ...) ({ \
511 bool this_progress = false; \
512 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
513 if (this_progress) \
514 progress = true; \
515 this_progress; \
516 })
517
518 static nir_variable_mode
519 brw_nir_no_indirect_mask(const struct brw_compiler *compiler,
520 gl_shader_stage stage)
521 {
522 nir_variable_mode indirect_mask = 0;
523
524 if (compiler->glsl_compiler_options[stage].EmitNoIndirectInput)
525 indirect_mask |= nir_var_shader_in;
526 if (compiler->glsl_compiler_options[stage].EmitNoIndirectOutput)
527 indirect_mask |= nir_var_shader_out;
528 if (compiler->glsl_compiler_options[stage].EmitNoIndirectTemp)
529 indirect_mask |= nir_var_local;
530
531 return indirect_mask;
532 }
533
534 nir_shader *
535 brw_nir_optimize(nir_shader *nir, const struct brw_compiler *compiler,
536 bool is_scalar)
537 {
538 nir_variable_mode indirect_mask =
539 brw_nir_no_indirect_mask(compiler, nir->info.stage);
540
541 bool progress;
542 do {
543 progress = false;
544 OPT(nir_split_array_vars, nir_var_local);
545 OPT(nir_lower_vars_to_ssa);
546 OPT(nir_opt_copy_prop_vars);
547
548 if (is_scalar) {
549 OPT(nir_lower_alu_to_scalar);
550 }
551
552 OPT(nir_copy_prop);
553
554 if (is_scalar) {
555 OPT(nir_lower_phis_to_scalar);
556 }
557
558 OPT(nir_copy_prop);
559 OPT(nir_opt_dce);
560 OPT(nir_opt_cse);
561 OPT(nir_opt_peephole_select, 0);
562 OPT(nir_opt_intrinsics);
563 OPT(nir_opt_algebraic);
564 OPT(nir_opt_constant_folding);
565 OPT(nir_opt_dead_cf);
566 if (OPT(nir_opt_trivial_continues)) {
567 /* If nir_opt_trivial_continues makes progress, then we need to clean
568 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
569 * to make progress.
570 */
571 OPT(nir_copy_prop);
572 OPT(nir_opt_dce);
573 }
574 OPT(nir_opt_if);
575 if (nir->options->max_unroll_iterations != 0) {
576 OPT(nir_opt_loop_unroll, indirect_mask);
577 }
578 OPT(nir_opt_remove_phis);
579 OPT(nir_opt_undef);
580 OPT(nir_lower_doubles, nir_lower_drcp |
581 nir_lower_dsqrt |
582 nir_lower_drsq |
583 nir_lower_dtrunc |
584 nir_lower_dfloor |
585 nir_lower_dceil |
586 nir_lower_dfract |
587 nir_lower_dround_even |
588 nir_lower_dmod);
589 OPT(nir_lower_pack);
590 } while (progress);
591
592 return nir;
593 }
594
595 static unsigned
596 lower_bit_size_callback(const nir_alu_instr *alu, UNUSED void *data)
597 {
598 assert(alu->dest.dest.is_ssa);
599 if (alu->dest.dest.ssa.bit_size != 16)
600 return 0;
601
602 switch (alu->op) {
603 case nir_op_idiv:
604 case nir_op_imod:
605 case nir_op_irem:
606 case nir_op_udiv:
607 case nir_op_umod:
608 return 32;
609 default:
610 return 0;
611 }
612 }
613
614 /* Does some simple lowering and runs the standard suite of optimizations
615 *
616 * This is intended to be called more-or-less directly after you get the
617 * shader out of GLSL or some other source. While it is geared towards i965,
618 * it is not at all generator-specific except for the is_scalar flag. Even
619 * there, it is safe to call with is_scalar = false for a shader that is
620 * intended for the FS backend as long as nir_optimize is called again with
621 * is_scalar = true to scalarize everything prior to code gen.
622 */
623 nir_shader *
624 brw_preprocess_nir(const struct brw_compiler *compiler, nir_shader *nir)
625 {
626 const struct gen_device_info *devinfo = compiler->devinfo;
627 UNUSED bool progress; /* Written by OPT */
628
629 const bool is_scalar = compiler->scalar_stage[nir->info.stage];
630
631 if (nir->info.stage == MESA_SHADER_GEOMETRY)
632 OPT(nir_lower_gs_intrinsics);
633
634 /* See also brw_nir_trig_workarounds.py */
635 if (compiler->precise_trig &&
636 !(devinfo->gen >= 10 || devinfo->is_kabylake))
637 OPT(brw_nir_apply_trig_workarounds);
638
639 static const nir_lower_tex_options tex_options = {
640 .lower_txp = ~0,
641 .lower_txf_offset = true,
642 .lower_rect_offset = true,
643 .lower_txd_cube_map = true,
644 };
645
646 OPT(nir_lower_tex, &tex_options);
647 OPT(nir_normalize_cubemap_coords);
648
649 OPT(nir_lower_global_vars_to_local);
650
651 OPT(nir_split_var_copies);
652 OPT(nir_split_struct_vars, nir_var_local);
653
654 /* Run opt_algebraic before int64 lowering so we can hopefully get rid
655 * of some int64 instructions.
656 */
657 OPT(nir_opt_algebraic);
658
659 /* Lower int64 instructions before nir_optimize so that loop unrolling
660 * sees their actual cost.
661 */
662 nir_lower_int64(nir, nir_lower_imul64 |
663 nir_lower_isign64 |
664 nir_lower_divmod64);
665
666 nir = brw_nir_optimize(nir, compiler, is_scalar);
667
668 /* This needs to be run after the first optimization pass but before we
669 * lower indirect derefs away
670 */
671 if (compiler->supports_shader_constants) {
672 OPT(nir_opt_large_constants, NULL, 32);
673 }
674
675 nir_lower_bit_size(nir, lower_bit_size_callback, NULL);
676
677 if (is_scalar) {
678 OPT(nir_lower_load_const_to_scalar);
679 }
680
681 /* Lower a bunch of stuff */
682 OPT(nir_lower_var_copies);
683
684 OPT(nir_lower_system_values);
685
686 const nir_lower_subgroups_options subgroups_options = {
687 .subgroup_size = BRW_SUBGROUP_SIZE,
688 .ballot_bit_size = 32,
689 .lower_to_scalar = true,
690 .lower_subgroup_masks = true,
691 .lower_vote_trivial = !is_scalar,
692 .lower_shuffle = true,
693 };
694 OPT(nir_lower_subgroups, &subgroups_options);
695
696 OPT(nir_lower_clip_cull_distance_arrays);
697
698 nir_variable_mode indirect_mask =
699 brw_nir_no_indirect_mask(compiler, nir->info.stage);
700 nir_lower_indirect_derefs(nir, indirect_mask);
701
702 /* Get rid of split copies */
703 nir = brw_nir_optimize(nir, compiler, is_scalar);
704
705 OPT(nir_remove_dead_variables, nir_var_local);
706
707 return nir;
708 }
709
710 void
711 brw_nir_link_shaders(const struct brw_compiler *compiler,
712 nir_shader **producer, nir_shader **consumer)
713 {
714 nir_lower_io_arrays_to_elements(*producer, *consumer);
715 nir_validate_shader(*producer);
716 nir_validate_shader(*consumer);
717
718 NIR_PASS_V(*producer, nir_remove_dead_variables, nir_var_shader_out);
719 NIR_PASS_V(*consumer, nir_remove_dead_variables, nir_var_shader_in);
720
721 if (nir_remove_unused_varyings(*producer, *consumer)) {
722 NIR_PASS_V(*producer, nir_lower_global_vars_to_local);
723 NIR_PASS_V(*consumer, nir_lower_global_vars_to_local);
724
725 /* The backend might not be able to handle indirects on
726 * temporaries so we need to lower indirects on any of the
727 * varyings we have demoted here.
728 */
729 NIR_PASS_V(*producer, nir_lower_indirect_derefs,
730 brw_nir_no_indirect_mask(compiler, (*producer)->info.stage));
731 NIR_PASS_V(*consumer, nir_lower_indirect_derefs,
732 brw_nir_no_indirect_mask(compiler, (*consumer)->info.stage));
733
734 const bool p_is_scalar =
735 compiler->scalar_stage[(*producer)->info.stage];
736 *producer = brw_nir_optimize(*producer, compiler, p_is_scalar);
737
738 const bool c_is_scalar =
739 compiler->scalar_stage[(*consumer)->info.stage];
740 *consumer = brw_nir_optimize(*consumer, compiler, c_is_scalar);
741 }
742 }
743
744 /* Prepare the given shader for codegen
745 *
746 * This function is intended to be called right before going into the actual
747 * backend and is highly backend-specific. Also, once this function has been
748 * called on a shader, it will no longer be in SSA form so most optimizations
749 * will not work.
750 */
751 nir_shader *
752 brw_postprocess_nir(nir_shader *nir, const struct brw_compiler *compiler,
753 bool is_scalar)
754 {
755 const struct gen_device_info *devinfo = compiler->devinfo;
756 bool debug_enabled =
757 (INTEL_DEBUG & intel_debug_flag_for_shader_stage(nir->info.stage));
758
759 UNUSED bool progress; /* Written by OPT */
760
761
762 do {
763 progress = false;
764 OPT(nir_opt_algebraic_before_ffma);
765 } while (progress);
766
767 nir = brw_nir_optimize(nir, compiler, is_scalar);
768
769 if (devinfo->gen >= 6) {
770 /* Try and fuse multiply-adds */
771 OPT(brw_nir_opt_peephole_ffma);
772 }
773
774 OPT(nir_opt_algebraic_late);
775
776 OPT(nir_lower_to_source_mods);
777 OPT(nir_copy_prop);
778 OPT(nir_opt_dce);
779 OPT(nir_opt_move_comparisons);
780
781 OPT(nir_lower_locals_to_regs);
782
783 if (unlikely(debug_enabled)) {
784 /* Re-index SSA defs so we print more sensible numbers. */
785 nir_foreach_function(function, nir) {
786 if (function->impl)
787 nir_index_ssa_defs(function->impl);
788 }
789
790 fprintf(stderr, "NIR (SSA form) for %s shader:\n",
791 _mesa_shader_stage_to_string(nir->info.stage));
792 nir_print_shader(nir, stderr);
793 }
794
795 OPT(nir_convert_from_ssa, true);
796
797 if (!is_scalar) {
798 OPT(nir_move_vec_src_uses_to_dest);
799 OPT(nir_lower_vec_to_movs);
800 }
801
802 OPT(nir_opt_dce);
803
804 /* This is the last pass we run before we start emitting stuff. It
805 * determines when we need to insert boolean resolves on Gen <= 5. We
806 * run it last because it stashes data in instr->pass_flags and we don't
807 * want that to be squashed by other NIR passes.
808 */
809 if (devinfo->gen <= 5)
810 brw_nir_analyze_boolean_resolves(nir);
811
812 nir_sweep(nir);
813
814 if (unlikely(debug_enabled)) {
815 fprintf(stderr, "NIR (final form) for %s shader:\n",
816 _mesa_shader_stage_to_string(nir->info.stage));
817 nir_print_shader(nir, stderr);
818 }
819
820 return nir;
821 }
822
823 nir_shader *
824 brw_nir_apply_sampler_key(nir_shader *nir,
825 const struct brw_compiler *compiler,
826 const struct brw_sampler_prog_key_data *key_tex,
827 bool is_scalar)
828 {
829 const struct gen_device_info *devinfo = compiler->devinfo;
830 nir_lower_tex_options tex_options = { 0 };
831
832 /* Iron Lake and prior require lowering of all rectangle textures */
833 if (devinfo->gen < 6)
834 tex_options.lower_rect = true;
835
836 /* Prior to Broadwell, our hardware can't actually do GL_CLAMP */
837 if (devinfo->gen < 8) {
838 tex_options.saturate_s = key_tex->gl_clamp_mask[0];
839 tex_options.saturate_t = key_tex->gl_clamp_mask[1];
840 tex_options.saturate_r = key_tex->gl_clamp_mask[2];
841 }
842
843 /* Prior to Haswell, we have to fake texture swizzle */
844 for (unsigned s = 0; s < MAX_SAMPLERS; s++) {
845 if (key_tex->swizzles[s] == SWIZZLE_NOOP)
846 continue;
847
848 tex_options.swizzle_result |= (1 << s);
849 for (unsigned c = 0; c < 4; c++)
850 tex_options.swizzles[s][c] = GET_SWZ(key_tex->swizzles[s], c);
851 }
852
853 /* Prior to Haswell, we have to lower gradients on shadow samplers */
854 tex_options.lower_txd_shadow = devinfo->gen < 8 && !devinfo->is_haswell;
855
856 tex_options.lower_y_uv_external = key_tex->y_uv_image_mask;
857 tex_options.lower_y_u_v_external = key_tex->y_u_v_image_mask;
858 tex_options.lower_yx_xuxv_external = key_tex->yx_xuxv_image_mask;
859 tex_options.lower_xy_uxvx_external = key_tex->xy_uxvx_image_mask;
860
861 if (nir_lower_tex(nir, &tex_options)) {
862 nir_validate_shader(nir);
863 nir = brw_nir_optimize(nir, compiler, is_scalar);
864 }
865
866 return nir;
867 }
868
869 enum brw_reg_type
870 brw_type_for_nir_type(const struct gen_device_info *devinfo, nir_alu_type type)
871 {
872 switch (type) {
873 case nir_type_uint:
874 case nir_type_uint32:
875 return BRW_REGISTER_TYPE_UD;
876 case nir_type_bool:
877 case nir_type_int:
878 case nir_type_bool32:
879 case nir_type_int32:
880 return BRW_REGISTER_TYPE_D;
881 case nir_type_float:
882 case nir_type_float32:
883 return BRW_REGISTER_TYPE_F;
884 case nir_type_float16:
885 return BRW_REGISTER_TYPE_HF;
886 case nir_type_float64:
887 return BRW_REGISTER_TYPE_DF;
888 case nir_type_int64:
889 return devinfo->gen < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_Q;
890 case nir_type_uint64:
891 return devinfo->gen < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_UQ;
892 case nir_type_int16:
893 return BRW_REGISTER_TYPE_W;
894 case nir_type_uint16:
895 return BRW_REGISTER_TYPE_UW;
896 case nir_type_int8:
897 return BRW_REGISTER_TYPE_B;
898 case nir_type_uint8:
899 return BRW_REGISTER_TYPE_UB;
900 default:
901 unreachable("unknown type");
902 }
903
904 return BRW_REGISTER_TYPE_F;
905 }
906
907 /* Returns the glsl_base_type corresponding to a nir_alu_type.
908 * This is used by both brw_vec4_nir and brw_fs_nir.
909 */
910 enum glsl_base_type
911 brw_glsl_base_type_for_nir_type(nir_alu_type type)
912 {
913 switch (type) {
914 case nir_type_float:
915 case nir_type_float32:
916 return GLSL_TYPE_FLOAT;
917
918 case nir_type_float16:
919 return GLSL_TYPE_FLOAT16;
920
921 case nir_type_float64:
922 return GLSL_TYPE_DOUBLE;
923
924 case nir_type_int:
925 case nir_type_int32:
926 return GLSL_TYPE_INT;
927
928 case nir_type_uint:
929 case nir_type_uint32:
930 return GLSL_TYPE_UINT;
931
932 case nir_type_int16:
933 return GLSL_TYPE_INT16;
934
935 case nir_type_uint16:
936 return GLSL_TYPE_UINT16;
937
938 default:
939 unreachable("bad type");
940 }
941 }