821c38a8669dd0195c75d5d0bc2ef14a6f936b17
[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 bool use_legacy_snorm_formula,
215 const uint8_t *vs_attrib_wa_flags)
216 {
217 /* Start with the location of the variable's base. */
218 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
219 var->data.driver_location = var->data.location;
220 }
221
222 /* Now use nir_lower_io to walk dereference chains. Attribute arrays are
223 * loaded as one vec4 or dvec4 per element (or matrix column), depending on
224 * whether it is a double-precision type or not.
225 */
226 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
227
228 /* This pass needs actual constants */
229 nir_opt_constant_folding(nir);
230
231 add_const_offset_to_base(nir, nir_var_shader_in);
232
233 brw_nir_apply_attribute_workarounds(nir, use_legacy_snorm_formula,
234 vs_attrib_wa_flags);
235
236 /* The last step is to remap VERT_ATTRIB_* to actual registers */
237
238 /* Whether or not we have any system generated values. gl_DrawID is not
239 * included here as it lives in its own vec4.
240 */
241 const bool has_sgvs =
242 nir->info.system_values_read &
243 (BITFIELD64_BIT(SYSTEM_VALUE_BASE_VERTEX) |
244 BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE) |
245 BITFIELD64_BIT(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) |
246 BITFIELD64_BIT(SYSTEM_VALUE_INSTANCE_ID));
247
248 const unsigned num_inputs = _mesa_bitcount_64(nir->info.inputs_read);
249
250 nir_foreach_function(function, nir) {
251 if (!function->impl)
252 continue;
253
254 nir_builder b;
255 nir_builder_init(&b, function->impl);
256
257 nir_foreach_block(block, function->impl) {
258 nir_foreach_instr_safe(instr, block) {
259 if (instr->type != nir_instr_type_intrinsic)
260 continue;
261
262 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
263
264 switch (intrin->intrinsic) {
265 case nir_intrinsic_load_base_vertex:
266 case nir_intrinsic_load_base_instance:
267 case nir_intrinsic_load_vertex_id_zero_base:
268 case nir_intrinsic_load_instance_id:
269 case nir_intrinsic_load_draw_id: {
270 b.cursor = nir_after_instr(&intrin->instr);
271
272 /* gl_VertexID and friends are stored by the VF as the last
273 * vertex element. We convert them to load_input intrinsics at
274 * the right location.
275 */
276 nir_intrinsic_instr *load =
277 nir_intrinsic_instr_create(nir, nir_intrinsic_load_input);
278 load->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
279
280 nir_intrinsic_set_base(load, num_inputs);
281 switch (intrin->intrinsic) {
282 case nir_intrinsic_load_base_vertex:
283 nir_intrinsic_set_component(load, 0);
284 break;
285 case nir_intrinsic_load_base_instance:
286 nir_intrinsic_set_component(load, 1);
287 break;
288 case nir_intrinsic_load_vertex_id_zero_base:
289 nir_intrinsic_set_component(load, 2);
290 break;
291 case nir_intrinsic_load_instance_id:
292 nir_intrinsic_set_component(load, 3);
293 break;
294 case nir_intrinsic_load_draw_id:
295 /* gl_DrawID is stored right after gl_VertexID and friends
296 * if any of them exist.
297 */
298 nir_intrinsic_set_base(load, num_inputs + has_sgvs);
299 nir_intrinsic_set_component(load, 0);
300 break;
301 default:
302 unreachable("Invalid system value intrinsic");
303 }
304
305 load->num_components = 1;
306 nir_ssa_dest_init(&load->instr, &load->dest, 1, 32, NULL);
307 nir_builder_instr_insert(&b, &load->instr);
308
309 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
310 nir_src_for_ssa(&load->dest.ssa));
311 nir_instr_remove(&intrin->instr);
312 break;
313 }
314
315 case nir_intrinsic_load_input: {
316 /* Attributes come in a contiguous block, ordered by their
317 * gl_vert_attrib value. That means we can compute the slot
318 * number for an attribute by masking out the enabled attributes
319 * before it and counting the bits.
320 */
321 int attr = nir_intrinsic_base(intrin);
322 int slot = _mesa_bitcount_64(nir->info.inputs_read &
323 BITFIELD64_MASK(attr));
324 nir_intrinsic_set_base(intrin, slot);
325 break;
326 }
327
328 default:
329 break; /* Nothing to do */
330 }
331 }
332 }
333 }
334 }
335
336 void
337 brw_nir_lower_vue_inputs(nir_shader *nir,
338 const struct brw_vue_map *vue_map)
339 {
340 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
341 var->data.driver_location = var->data.location;
342 }
343
344 /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
345 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
346
347 /* This pass needs actual constants */
348 nir_opt_constant_folding(nir);
349
350 add_const_offset_to_base(nir, nir_var_shader_in);
351
352 nir_foreach_function(function, nir) {
353 if (!function->impl)
354 continue;
355
356 nir_foreach_block(block, function->impl) {
357 nir_foreach_instr(instr, block) {
358 if (instr->type != nir_instr_type_intrinsic)
359 continue;
360
361 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
362
363 if (intrin->intrinsic == nir_intrinsic_load_input ||
364 intrin->intrinsic == nir_intrinsic_load_per_vertex_input) {
365 /* Offset 0 is the VUE header, which contains
366 * VARYING_SLOT_LAYER [.y], VARYING_SLOT_VIEWPORT [.z], and
367 * VARYING_SLOT_PSIZ [.w].
368 */
369 int varying = nir_intrinsic_base(intrin);
370 int vue_slot;
371 switch (varying) {
372 case VARYING_SLOT_PSIZ:
373 nir_intrinsic_set_base(intrin, 0);
374 nir_intrinsic_set_component(intrin, 3);
375 break;
376
377 default:
378 vue_slot = vue_map->varying_to_slot[varying];
379 assert(vue_slot != -1);
380 nir_intrinsic_set_base(intrin, vue_slot);
381 break;
382 }
383 }
384 }
385 }
386 }
387 }
388
389 void
390 brw_nir_lower_tes_inputs(nir_shader *nir, const struct brw_vue_map *vue_map)
391 {
392 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
393 var->data.driver_location = var->data.location;
394 }
395
396 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
397
398 /* This pass needs actual constants */
399 nir_opt_constant_folding(nir);
400
401 add_const_offset_to_base(nir, nir_var_shader_in);
402
403 nir_foreach_function(function, nir) {
404 if (function->impl) {
405 nir_builder b;
406 nir_builder_init(&b, function->impl);
407 nir_foreach_block(block, function->impl) {
408 remap_patch_urb_offsets(block, &b, vue_map,
409 nir->info.tess.primitive_mode);
410 }
411 }
412 }
413 }
414
415 void
416 brw_nir_lower_fs_inputs(nir_shader *nir,
417 const struct gen_device_info *devinfo,
418 const struct brw_wm_prog_key *key)
419 {
420 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
421 var->data.driver_location = var->data.location;
422
423 /* Apply default interpolation mode.
424 *
425 * Everything defaults to smooth except for the legacy GL color
426 * built-in variables, which might be flat depending on API state.
427 */
428 if (var->data.interpolation == INTERP_MODE_NONE) {
429 const bool flat = key->flat_shade &&
430 (var->data.location == VARYING_SLOT_COL0 ||
431 var->data.location == VARYING_SLOT_COL1);
432
433 var->data.interpolation = flat ? INTERP_MODE_FLAT
434 : INTERP_MODE_SMOOTH;
435 }
436
437 /* On Ironlake and below, there is only one interpolation mode.
438 * Centroid interpolation doesn't mean anything on this hardware --
439 * there is no multisampling.
440 */
441 if (devinfo->gen < 6) {
442 var->data.centroid = false;
443 var->data.sample = false;
444 }
445 }
446
447 nir_lower_io_options lower_io_options = 0;
448 if (key->persample_interp)
449 lower_io_options |= nir_lower_io_force_sample_interpolation;
450
451 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, lower_io_options);
452
453 /* This pass needs actual constants */
454 nir_opt_constant_folding(nir);
455
456 add_const_offset_to_base(nir, nir_var_shader_in);
457 }
458
459 void
460 brw_nir_lower_vue_outputs(nir_shader *nir,
461 bool is_scalar)
462 {
463 nir_foreach_variable(var, &nir->outputs) {
464 var->data.driver_location = var->data.location;
465 }
466
467 nir_lower_io(nir, nir_var_shader_out, type_size_vec4, 0);
468 }
469
470 void
471 brw_nir_lower_tcs_outputs(nir_shader *nir, const struct brw_vue_map *vue_map,
472 GLenum tes_primitive_mode)
473 {
474 nir_foreach_variable(var, &nir->outputs) {
475 var->data.driver_location = var->data.location;
476 }
477
478 nir_lower_io(nir, nir_var_shader_out, type_size_vec4, 0);
479
480 /* This pass needs actual constants */
481 nir_opt_constant_folding(nir);
482
483 add_const_offset_to_base(nir, nir_var_shader_out);
484
485 nir_foreach_function(function, nir) {
486 if (function->impl) {
487 nir_builder b;
488 nir_builder_init(&b, function->impl);
489 nir_foreach_block(block, function->impl) {
490 remap_patch_urb_offsets(block, &b, vue_map, tes_primitive_mode);
491 }
492 }
493 }
494 }
495
496 void
497 brw_nir_lower_fs_outputs(nir_shader *nir)
498 {
499 nir_foreach_variable(var, &nir->outputs) {
500 var->data.driver_location =
501 SET_FIELD(var->data.index, BRW_NIR_FRAG_OUTPUT_INDEX) |
502 SET_FIELD(var->data.location, BRW_NIR_FRAG_OUTPUT_LOCATION);
503 }
504
505 nir_lower_io(nir, nir_var_shader_out, type_size_dvec4, 0);
506 }
507
508 void
509 brw_nir_lower_cs_shared(nir_shader *nir)
510 {
511 nir_assign_var_locations(&nir->shared, &nir->num_shared,
512 type_size_scalar_bytes);
513 nir_lower_io(nir, nir_var_shared, type_size_scalar_bytes, 0);
514 }
515
516 #define OPT(pass, ...) ({ \
517 bool this_progress = false; \
518 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
519 if (this_progress) \
520 progress = true; \
521 this_progress; \
522 })
523
524 static nir_variable_mode
525 brw_nir_no_indirect_mask(const struct brw_compiler *compiler,
526 gl_shader_stage stage)
527 {
528 nir_variable_mode indirect_mask = 0;
529
530 if (compiler->glsl_compiler_options[stage].EmitNoIndirectInput)
531 indirect_mask |= nir_var_shader_in;
532 if (compiler->glsl_compiler_options[stage].EmitNoIndirectOutput)
533 indirect_mask |= nir_var_shader_out;
534 if (compiler->glsl_compiler_options[stage].EmitNoIndirectTemp)
535 indirect_mask |= nir_var_local;
536
537 return indirect_mask;
538 }
539
540 nir_shader *
541 brw_nir_optimize(nir_shader *nir, const struct brw_compiler *compiler,
542 bool is_scalar)
543 {
544 nir_variable_mode indirect_mask =
545 brw_nir_no_indirect_mask(compiler, nir->info.stage);
546
547 bool progress;
548 do {
549 progress = false;
550 OPT(nir_lower_vars_to_ssa);
551 OPT(nir_opt_copy_prop_vars);
552
553 if (is_scalar) {
554 OPT(nir_lower_alu_to_scalar);
555 }
556
557 OPT(nir_copy_prop);
558
559 if (is_scalar) {
560 OPT(nir_lower_phis_to_scalar);
561 }
562
563 OPT(nir_copy_prop);
564 OPT(nir_opt_dce);
565 OPT(nir_opt_cse);
566 OPT(nir_opt_peephole_select, 0);
567 OPT(nir_opt_intrinsics);
568 OPT(nir_opt_algebraic);
569 OPT(nir_opt_constant_folding);
570 OPT(nir_opt_dead_cf);
571 if (OPT(nir_opt_trivial_continues)) {
572 /* If nir_opt_trivial_continues makes progress, then we need to clean
573 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
574 * to make progress.
575 */
576 OPT(nir_copy_prop);
577 OPT(nir_opt_dce);
578 }
579 OPT(nir_opt_if);
580 if (nir->options->max_unroll_iterations != 0) {
581 OPT(nir_opt_loop_unroll, indirect_mask);
582 }
583 OPT(nir_opt_remove_phis);
584 OPT(nir_opt_undef);
585 OPT(nir_lower_doubles, nir_lower_drcp |
586 nir_lower_dsqrt |
587 nir_lower_drsq |
588 nir_lower_dtrunc |
589 nir_lower_dfloor |
590 nir_lower_dceil |
591 nir_lower_dfract |
592 nir_lower_dround_even |
593 nir_lower_dmod);
594 OPT(nir_lower_64bit_pack);
595 } while (progress);
596
597 return nir;
598 }
599
600 /* Does some simple lowering and runs the standard suite of optimizations
601 *
602 * This is intended to be called more-or-less directly after you get the
603 * shader out of GLSL or some other source. While it is geared towards i965,
604 * it is not at all generator-specific except for the is_scalar flag. Even
605 * there, it is safe to call with is_scalar = false for a shader that is
606 * intended for the FS backend as long as nir_optimize is called again with
607 * is_scalar = true to scalarize everything prior to code gen.
608 */
609 nir_shader *
610 brw_preprocess_nir(const struct brw_compiler *compiler, nir_shader *nir)
611 {
612 const struct gen_device_info *devinfo = compiler->devinfo;
613 UNUSED bool progress; /* Written by OPT */
614
615 const bool is_scalar = compiler->scalar_stage[nir->info.stage];
616
617 if (nir->info.stage == MESA_SHADER_GEOMETRY)
618 OPT(nir_lower_gs_intrinsics);
619
620 /* See also brw_nir_trig_workarounds.py */
621 if (compiler->precise_trig &&
622 !(devinfo->gen >= 10 || devinfo->is_kabylake))
623 OPT(brw_nir_apply_trig_workarounds);
624
625 static const nir_lower_tex_options tex_options = {
626 .lower_txp = ~0,
627 .lower_txf_offset = true,
628 .lower_rect_offset = true,
629 .lower_txd_cube_map = true,
630 };
631
632 OPT(nir_lower_tex, &tex_options);
633 OPT(nir_normalize_cubemap_coords);
634
635 OPT(nir_lower_global_vars_to_local);
636
637 OPT(nir_split_var_copies);
638
639 nir = brw_nir_optimize(nir, compiler, is_scalar);
640
641 if (is_scalar) {
642 OPT(nir_lower_load_const_to_scalar);
643 }
644
645 /* Lower a bunch of stuff */
646 OPT(nir_lower_var_copies);
647
648 OPT(nir_lower_system_values);
649
650 const nir_lower_subgroups_options subgroups_options = {
651 .subgroup_size = nir->info.stage == MESA_SHADER_COMPUTE ? 32 :
652 nir->info.stage == MESA_SHADER_FRAGMENT ? 16 : 8,
653 .ballot_bit_size = 32,
654 .lower_to_scalar = true,
655 .lower_subgroup_masks = true,
656 .lower_vote_trivial = !is_scalar,
657 };
658 OPT(nir_lower_subgroups, &subgroups_options);
659
660 OPT(nir_lower_clip_cull_distance_arrays);
661
662 nir_variable_mode indirect_mask =
663 brw_nir_no_indirect_mask(compiler, nir->info.stage);
664 nir_lower_indirect_derefs(nir, indirect_mask);
665
666 nir_lower_int64(nir, nir_lower_imul64 |
667 nir_lower_isign64 |
668 nir_lower_divmod64);
669
670 /* Get rid of split copies */
671 nir = brw_nir_optimize(nir, compiler, is_scalar);
672
673 OPT(nir_remove_dead_variables, nir_var_local);
674
675 return nir;
676 }
677
678 /* Prepare the given shader for codegen
679 *
680 * This function is intended to be called right before going into the actual
681 * backend and is highly backend-specific. Also, once this function has been
682 * called on a shader, it will no longer be in SSA form so most optimizations
683 * will not work.
684 */
685 nir_shader *
686 brw_postprocess_nir(nir_shader *nir, const struct brw_compiler *compiler,
687 bool is_scalar)
688 {
689 const struct gen_device_info *devinfo = compiler->devinfo;
690 bool debug_enabled =
691 (INTEL_DEBUG & intel_debug_flag_for_shader_stage(nir->info.stage));
692
693 UNUSED bool progress; /* Written by OPT */
694
695
696 do {
697 progress = false;
698 OPT(nir_opt_algebraic_before_ffma);
699 } while (progress);
700
701 nir = brw_nir_optimize(nir, compiler, is_scalar);
702
703 if (devinfo->gen >= 6) {
704 /* Try and fuse multiply-adds */
705 OPT(brw_nir_opt_peephole_ffma);
706 }
707
708 OPT(nir_opt_algebraic_late);
709
710 OPT(nir_lower_to_source_mods);
711 OPT(nir_copy_prop);
712 OPT(nir_opt_dce);
713 OPT(nir_opt_move_comparisons);
714
715 OPT(nir_lower_locals_to_regs);
716
717 if (unlikely(debug_enabled)) {
718 /* Re-index SSA defs so we print more sensible numbers. */
719 nir_foreach_function(function, nir) {
720 if (function->impl)
721 nir_index_ssa_defs(function->impl);
722 }
723
724 fprintf(stderr, "NIR (SSA form) for %s shader:\n",
725 _mesa_shader_stage_to_string(nir->info.stage));
726 nir_print_shader(nir, stderr);
727 }
728
729 OPT(nir_convert_from_ssa, true);
730
731 if (!is_scalar) {
732 OPT(nir_move_vec_src_uses_to_dest);
733 OPT(nir_lower_vec_to_movs);
734 }
735
736 /* This is the last pass we run before we start emitting stuff. It
737 * determines when we need to insert boolean resolves on Gen <= 5. We
738 * run it last because it stashes data in instr->pass_flags and we don't
739 * want that to be squashed by other NIR passes.
740 */
741 if (devinfo->gen <= 5)
742 brw_nir_analyze_boolean_resolves(nir);
743
744 nir_sweep(nir);
745
746 if (unlikely(debug_enabled)) {
747 fprintf(stderr, "NIR (final form) for %s shader:\n",
748 _mesa_shader_stage_to_string(nir->info.stage));
749 nir_print_shader(nir, stderr);
750 }
751
752 return nir;
753 }
754
755 nir_shader *
756 brw_nir_apply_sampler_key(nir_shader *nir,
757 const struct brw_compiler *compiler,
758 const struct brw_sampler_prog_key_data *key_tex,
759 bool is_scalar)
760 {
761 const struct gen_device_info *devinfo = compiler->devinfo;
762 nir_lower_tex_options tex_options = { 0 };
763
764 /* Iron Lake and prior require lowering of all rectangle textures */
765 if (devinfo->gen < 6)
766 tex_options.lower_rect = true;
767
768 /* Prior to Broadwell, our hardware can't actually do GL_CLAMP */
769 if (devinfo->gen < 8) {
770 tex_options.saturate_s = key_tex->gl_clamp_mask[0];
771 tex_options.saturate_t = key_tex->gl_clamp_mask[1];
772 tex_options.saturate_r = key_tex->gl_clamp_mask[2];
773 }
774
775 /* Prior to Haswell, we have to fake texture swizzle */
776 for (unsigned s = 0; s < MAX_SAMPLERS; s++) {
777 if (key_tex->swizzles[s] == SWIZZLE_NOOP)
778 continue;
779
780 tex_options.swizzle_result |= (1 << s);
781 for (unsigned c = 0; c < 4; c++)
782 tex_options.swizzles[s][c] = GET_SWZ(key_tex->swizzles[s], c);
783 }
784
785 /* Prior to Haswell, we have to lower gradients on shadow samplers */
786 tex_options.lower_txd_shadow = devinfo->gen < 8 && !devinfo->is_haswell;
787
788 tex_options.lower_y_uv_external = key_tex->y_uv_image_mask;
789 tex_options.lower_y_u_v_external = key_tex->y_u_v_image_mask;
790 tex_options.lower_yx_xuxv_external = key_tex->yx_xuxv_image_mask;
791 tex_options.lower_xy_uxvx_external = key_tex->xy_uxvx_image_mask;
792
793 if (nir_lower_tex(nir, &tex_options)) {
794 nir_validate_shader(nir);
795 nir = brw_nir_optimize(nir, compiler, is_scalar);
796 }
797
798 return nir;
799 }
800
801 enum brw_reg_type
802 brw_type_for_nir_type(const struct gen_device_info *devinfo, nir_alu_type type)
803 {
804 switch (type) {
805 case nir_type_uint:
806 case nir_type_uint32:
807 return BRW_REGISTER_TYPE_UD;
808 case nir_type_bool:
809 case nir_type_int:
810 case nir_type_bool32:
811 case nir_type_int32:
812 return BRW_REGISTER_TYPE_D;
813 case nir_type_float:
814 case nir_type_float32:
815 return BRW_REGISTER_TYPE_F;
816 case nir_type_float64:
817 return BRW_REGISTER_TYPE_DF;
818 case nir_type_int64:
819 return devinfo->gen < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_Q;
820 case nir_type_uint64:
821 return devinfo->gen < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_UQ;
822 default:
823 unreachable("unknown type");
824 }
825
826 return BRW_REGISTER_TYPE_F;
827 }
828
829 /* Returns the glsl_base_type corresponding to a nir_alu_type.
830 * This is used by both brw_vec4_nir and brw_fs_nir.
831 */
832 enum glsl_base_type
833 brw_glsl_base_type_for_nir_type(nir_alu_type type)
834 {
835 switch (type) {
836 case nir_type_float:
837 case nir_type_float32:
838 return GLSL_TYPE_FLOAT;
839
840 case nir_type_float64:
841 return GLSL_TYPE_DOUBLE;
842
843 case nir_type_int:
844 case nir_type_int32:
845 return GLSL_TYPE_INT;
846
847 case nir_type_uint:
848 case nir_type_uint32:
849 return GLSL_TYPE_UINT;
850
851 default:
852 unreachable("bad type");
853 }
854 }