intel: Run the optimization loop before and after lowering int64
[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 "dev/gen_debug.h"
27 #include "compiler/glsl_types.h"
28 #include "compiler/nir/nir_builder.h"
29 #include "util/u_math.h"
30
31 static bool
32 remap_tess_levels(nir_builder *b, nir_intrinsic_instr *intr,
33 GLenum primitive_mode)
34 {
35 const int location = nir_intrinsic_base(intr);
36 const unsigned component = nir_intrinsic_component(intr);
37 bool out_of_bounds;
38
39 if (location == VARYING_SLOT_TESS_LEVEL_INNER) {
40 switch (primitive_mode) {
41 case GL_QUADS:
42 /* gl_TessLevelInner[0..1] lives at DWords 3-2 (reversed). */
43 nir_intrinsic_set_base(intr, 0);
44 nir_intrinsic_set_component(intr, 3 - component);
45 out_of_bounds = false;
46 break;
47 case GL_TRIANGLES:
48 /* gl_TessLevelInner[0] lives at DWord 4. */
49 nir_intrinsic_set_base(intr, 1);
50 out_of_bounds = component > 0;
51 break;
52 case GL_ISOLINES:
53 out_of_bounds = true;
54 break;
55 default:
56 unreachable("Bogus tessellation domain");
57 }
58 } else if (location == VARYING_SLOT_TESS_LEVEL_OUTER) {
59 if (primitive_mode == GL_ISOLINES) {
60 /* gl_TessLevelOuter[0..1] lives at DWords 6-7 (in order). */
61 nir_intrinsic_set_base(intr, 1);
62 nir_intrinsic_set_component(intr, 2 + nir_intrinsic_component(intr));
63 out_of_bounds = component > 1;
64 } else {
65 /* Triangles use DWords 7-5 (reversed); Quads use 7-4 (reversed) */
66 nir_intrinsic_set_base(intr, 1);
67 nir_intrinsic_set_component(intr, 3 - nir_intrinsic_component(intr));
68 out_of_bounds = component == 3 && primitive_mode == GL_TRIANGLES;
69 }
70 } else {
71 return false;
72 }
73
74 if (out_of_bounds) {
75 if (nir_intrinsic_infos[intr->intrinsic].has_dest) {
76 b->cursor = nir_before_instr(&intr->instr);
77 nir_ssa_def *undef = nir_ssa_undef(b, 1, 32);
78 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(undef));
79 }
80 nir_instr_remove(&intr->instr);
81 }
82
83 return true;
84 }
85
86 static bool
87 is_input(nir_intrinsic_instr *intrin)
88 {
89 return intrin->intrinsic == nir_intrinsic_load_input ||
90 intrin->intrinsic == nir_intrinsic_load_per_vertex_input ||
91 intrin->intrinsic == nir_intrinsic_load_interpolated_input;
92 }
93
94 static bool
95 is_output(nir_intrinsic_instr *intrin)
96 {
97 return intrin->intrinsic == nir_intrinsic_load_output ||
98 intrin->intrinsic == nir_intrinsic_load_per_vertex_output ||
99 intrin->intrinsic == nir_intrinsic_store_output ||
100 intrin->intrinsic == nir_intrinsic_store_per_vertex_output;
101 }
102
103
104 static bool
105 remap_patch_urb_offsets(nir_block *block, nir_builder *b,
106 const struct brw_vue_map *vue_map,
107 GLenum tes_primitive_mode)
108 {
109 const bool is_passthrough_tcs = b->shader->info.name &&
110 strcmp(b->shader->info.name, "passthrough") == 0;
111
112 nir_foreach_instr_safe(instr, block) {
113 if (instr->type != nir_instr_type_intrinsic)
114 continue;
115
116 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
117
118 gl_shader_stage stage = b->shader->info.stage;
119
120 if ((stage == MESA_SHADER_TESS_CTRL && is_output(intrin)) ||
121 (stage == MESA_SHADER_TESS_EVAL && is_input(intrin))) {
122
123 if (!is_passthrough_tcs &&
124 remap_tess_levels(b, intrin, tes_primitive_mode))
125 continue;
126
127 int vue_slot = vue_map->varying_to_slot[intrin->const_index[0]];
128 assert(vue_slot != -1);
129 intrin->const_index[0] = vue_slot;
130
131 nir_src *vertex = nir_get_io_vertex_index_src(intrin);
132 if (vertex) {
133 if (nir_src_is_const(*vertex)) {
134 intrin->const_index[0] += nir_src_as_uint(*vertex) *
135 vue_map->num_per_vertex_slots;
136 } else {
137 b->cursor = nir_before_instr(&intrin->instr);
138
139 /* Multiply by the number of per-vertex slots. */
140 nir_ssa_def *vertex_offset =
141 nir_imul(b,
142 nir_ssa_for_src(b, *vertex, 1),
143 nir_imm_int(b,
144 vue_map->num_per_vertex_slots));
145
146 /* Add it to the existing offset */
147 nir_src *offset = nir_get_io_offset_src(intrin);
148 nir_ssa_def *total_offset =
149 nir_iadd(b, vertex_offset,
150 nir_ssa_for_src(b, *offset, 1));
151
152 nir_instr_rewrite_src(&intrin->instr, offset,
153 nir_src_for_ssa(total_offset));
154 }
155 }
156 }
157 }
158 return true;
159 }
160
161 void
162 brw_nir_lower_vs_inputs(nir_shader *nir,
163 const uint8_t *vs_attrib_wa_flags)
164 {
165 /* Start with the location of the variable's base. */
166 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
167 var->data.driver_location = var->data.location;
168 }
169
170 /* Now use nir_lower_io to walk dereference chains. Attribute arrays are
171 * loaded as one vec4 or dvec4 per element (or matrix column), depending on
172 * whether it is a double-precision type or not.
173 */
174 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
175
176 /* This pass needs actual constants */
177 nir_opt_constant_folding(nir);
178
179 nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
180
181 brw_nir_apply_attribute_workarounds(nir, vs_attrib_wa_flags);
182
183 /* The last step is to remap VERT_ATTRIB_* to actual registers */
184
185 /* Whether or not we have any system generated values. gl_DrawID is not
186 * included here as it lives in its own vec4.
187 */
188 const bool has_sgvs =
189 nir->info.system_values_read &
190 (BITFIELD64_BIT(SYSTEM_VALUE_FIRST_VERTEX) |
191 BITFIELD64_BIT(SYSTEM_VALUE_BASE_INSTANCE) |
192 BITFIELD64_BIT(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) |
193 BITFIELD64_BIT(SYSTEM_VALUE_INSTANCE_ID));
194
195 const unsigned num_inputs = util_bitcount64(nir->info.inputs_read);
196
197 nir_foreach_function(function, nir) {
198 if (!function->impl)
199 continue;
200
201 nir_builder b;
202 nir_builder_init(&b, function->impl);
203
204 nir_foreach_block(block, function->impl) {
205 nir_foreach_instr_safe(instr, block) {
206 if (instr->type != nir_instr_type_intrinsic)
207 continue;
208
209 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
210
211 switch (intrin->intrinsic) {
212 case nir_intrinsic_load_first_vertex:
213 case nir_intrinsic_load_base_instance:
214 case nir_intrinsic_load_vertex_id_zero_base:
215 case nir_intrinsic_load_instance_id:
216 case nir_intrinsic_load_is_indexed_draw:
217 case nir_intrinsic_load_draw_id: {
218 b.cursor = nir_after_instr(&intrin->instr);
219
220 /* gl_VertexID and friends are stored by the VF as the last
221 * vertex element. We convert them to load_input intrinsics at
222 * the right location.
223 */
224 nir_intrinsic_instr *load =
225 nir_intrinsic_instr_create(nir, nir_intrinsic_load_input);
226 load->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
227
228 nir_intrinsic_set_base(load, num_inputs);
229 switch (intrin->intrinsic) {
230 case nir_intrinsic_load_first_vertex:
231 nir_intrinsic_set_component(load, 0);
232 break;
233 case nir_intrinsic_load_base_instance:
234 nir_intrinsic_set_component(load, 1);
235 break;
236 case nir_intrinsic_load_vertex_id_zero_base:
237 nir_intrinsic_set_component(load, 2);
238 break;
239 case nir_intrinsic_load_instance_id:
240 nir_intrinsic_set_component(load, 3);
241 break;
242 case nir_intrinsic_load_draw_id:
243 case nir_intrinsic_load_is_indexed_draw:
244 /* gl_DrawID and IsIndexedDraw are stored right after
245 * gl_VertexID and friends if any of them exist.
246 */
247 nir_intrinsic_set_base(load, num_inputs + has_sgvs);
248 if (intrin->intrinsic == nir_intrinsic_load_draw_id)
249 nir_intrinsic_set_component(load, 0);
250 else
251 nir_intrinsic_set_component(load, 1);
252 break;
253 default:
254 unreachable("Invalid system value intrinsic");
255 }
256
257 load->num_components = 1;
258 nir_ssa_dest_init(&load->instr, &load->dest, 1, 32, NULL);
259 nir_builder_instr_insert(&b, &load->instr);
260
261 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
262 nir_src_for_ssa(&load->dest.ssa));
263 nir_instr_remove(&intrin->instr);
264 break;
265 }
266
267 case nir_intrinsic_load_input: {
268 /* Attributes come in a contiguous block, ordered by their
269 * gl_vert_attrib value. That means we can compute the slot
270 * number for an attribute by masking out the enabled attributes
271 * before it and counting the bits.
272 */
273 int attr = nir_intrinsic_base(intrin);
274 int slot = util_bitcount64(nir->info.inputs_read &
275 BITFIELD64_MASK(attr));
276 nir_intrinsic_set_base(intrin, slot);
277 break;
278 }
279
280 default:
281 break; /* Nothing to do */
282 }
283 }
284 }
285 }
286 }
287
288 void
289 brw_nir_lower_vue_inputs(nir_shader *nir,
290 const struct brw_vue_map *vue_map)
291 {
292 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
293 var->data.driver_location = var->data.location;
294 }
295
296 /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
297 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, 0);
298
299 /* This pass needs actual constants */
300 nir_opt_constant_folding(nir);
301
302 nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
303
304 nir_foreach_function(function, nir) {
305 if (!function->impl)
306 continue;
307
308 nir_foreach_block(block, function->impl) {
309 nir_foreach_instr(instr, block) {
310 if (instr->type != nir_instr_type_intrinsic)
311 continue;
312
313 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
314
315 if (intrin->intrinsic == nir_intrinsic_load_input ||
316 intrin->intrinsic == nir_intrinsic_load_per_vertex_input) {
317 /* Offset 0 is the VUE header, which contains
318 * VARYING_SLOT_LAYER [.y], VARYING_SLOT_VIEWPORT [.z], and
319 * VARYING_SLOT_PSIZ [.w].
320 */
321 int varying = nir_intrinsic_base(intrin);
322 int vue_slot;
323 switch (varying) {
324 case VARYING_SLOT_PSIZ:
325 nir_intrinsic_set_base(intrin, 0);
326 nir_intrinsic_set_component(intrin, 3);
327 break;
328
329 default:
330 vue_slot = vue_map->varying_to_slot[varying];
331 assert(vue_slot != -1);
332 nir_intrinsic_set_base(intrin, vue_slot);
333 break;
334 }
335 }
336 }
337 }
338 }
339 }
340
341 void
342 brw_nir_lower_tes_inputs(nir_shader *nir, const struct brw_vue_map *vue_map)
343 {
344 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
345 var->data.driver_location = var->data.location;
346 }
347
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 nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
354
355 nir_foreach_function(function, nir) {
356 if (function->impl) {
357 nir_builder b;
358 nir_builder_init(&b, function->impl);
359 nir_foreach_block(block, function->impl) {
360 remap_patch_urb_offsets(block, &b, vue_map,
361 nir->info.tess.primitive_mode);
362 }
363 }
364 }
365 }
366
367 void
368 brw_nir_lower_fs_inputs(nir_shader *nir,
369 const struct gen_device_info *devinfo,
370 const struct brw_wm_prog_key *key)
371 {
372 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
373 var->data.driver_location = var->data.location;
374
375 /* Apply default interpolation mode.
376 *
377 * Everything defaults to smooth except for the legacy GL color
378 * built-in variables, which might be flat depending on API state.
379 */
380 if (var->data.interpolation == INTERP_MODE_NONE) {
381 const bool flat = key->flat_shade &&
382 (var->data.location == VARYING_SLOT_COL0 ||
383 var->data.location == VARYING_SLOT_COL1);
384
385 var->data.interpolation = flat ? INTERP_MODE_FLAT
386 : INTERP_MODE_SMOOTH;
387 }
388
389 /* On Ironlake and below, there is only one interpolation mode.
390 * Centroid interpolation doesn't mean anything on this hardware --
391 * there is no multisampling.
392 */
393 if (devinfo->gen < 6) {
394 var->data.centroid = false;
395 var->data.sample = false;
396 }
397 }
398
399 nir_lower_io_options lower_io_options = 0;
400 if (key->persample_interp)
401 lower_io_options |= nir_lower_io_force_sample_interpolation;
402
403 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, lower_io_options);
404 if (devinfo->gen >= 11)
405 nir_lower_interpolation(nir, ~0);
406
407 /* This pass needs actual constants */
408 nir_opt_constant_folding(nir);
409
410 nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
411 }
412
413 void
414 brw_nir_lower_vue_outputs(nir_shader *nir)
415 {
416 nir_foreach_variable(var, &nir->outputs) {
417 var->data.driver_location = var->data.location;
418 }
419
420 nir_lower_io(nir, nir_var_shader_out, type_size_vec4, 0);
421 }
422
423 void
424 brw_nir_lower_tcs_outputs(nir_shader *nir, const struct brw_vue_map *vue_map,
425 GLenum tes_primitive_mode)
426 {
427 nir_foreach_variable(var, &nir->outputs) {
428 var->data.driver_location = var->data.location;
429 }
430
431 nir_lower_io(nir, nir_var_shader_out, type_size_vec4, 0);
432
433 /* This pass needs actual constants */
434 nir_opt_constant_folding(nir);
435
436 nir_io_add_const_offset_to_base(nir, nir_var_shader_out);
437
438 nir_foreach_function(function, nir) {
439 if (function->impl) {
440 nir_builder b;
441 nir_builder_init(&b, function->impl);
442 nir_foreach_block(block, function->impl) {
443 remap_patch_urb_offsets(block, &b, vue_map, tes_primitive_mode);
444 }
445 }
446 }
447 }
448
449 void
450 brw_nir_lower_fs_outputs(nir_shader *nir)
451 {
452 nir_foreach_variable(var, &nir->outputs) {
453 var->data.driver_location =
454 SET_FIELD(var->data.index, BRW_NIR_FRAG_OUTPUT_INDEX) |
455 SET_FIELD(var->data.location, BRW_NIR_FRAG_OUTPUT_LOCATION);
456 }
457
458 nir_lower_io(nir, nir_var_shader_out, type_size_dvec4, 0);
459 }
460
461 #define OPT(pass, ...) ({ \
462 bool this_progress = false; \
463 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
464 if (this_progress) \
465 progress = true; \
466 this_progress; \
467 })
468
469 static nir_variable_mode
470 brw_nir_no_indirect_mask(const struct brw_compiler *compiler,
471 gl_shader_stage stage)
472 {
473 nir_variable_mode indirect_mask = 0;
474
475 if (compiler->glsl_compiler_options[stage].EmitNoIndirectInput)
476 indirect_mask |= nir_var_shader_in;
477 if (compiler->glsl_compiler_options[stage].EmitNoIndirectOutput)
478 indirect_mask |= nir_var_shader_out;
479 if (compiler->glsl_compiler_options[stage].EmitNoIndirectTemp)
480 indirect_mask |= nir_var_function_temp;
481
482 return indirect_mask;
483 }
484
485 void
486 brw_nir_optimize(nir_shader *nir, const struct brw_compiler *compiler,
487 bool is_scalar, bool allow_copies)
488 {
489 nir_variable_mode indirect_mask =
490 brw_nir_no_indirect_mask(compiler, nir->info.stage);
491
492 bool progress;
493 unsigned lower_flrp =
494 (nir->options->lower_flrp16 ? 16 : 0) |
495 (nir->options->lower_flrp32 ? 32 : 0) |
496 (nir->options->lower_flrp64 ? 64 : 0);
497
498 do {
499 progress = false;
500 OPT(nir_split_array_vars, nir_var_function_temp);
501 OPT(nir_shrink_vec_array_vars, nir_var_function_temp);
502 OPT(nir_opt_deref);
503 OPT(nir_lower_vars_to_ssa);
504 if (allow_copies) {
505 /* Only run this pass in the first call to brw_nir_optimize. Later
506 * calls assume that we've lowered away any copy_deref instructions
507 * and we don't want to introduce any more.
508 */
509 OPT(nir_opt_find_array_copies);
510 }
511 OPT(nir_opt_copy_prop_vars);
512 OPT(nir_opt_dead_write_vars);
513 OPT(nir_opt_combine_stores, nir_var_all);
514
515 if (is_scalar) {
516 OPT(nir_lower_alu_to_scalar, NULL);
517 }
518
519 OPT(nir_copy_prop);
520
521 if (is_scalar) {
522 OPT(nir_lower_phis_to_scalar);
523 }
524
525 OPT(nir_copy_prop);
526 OPT(nir_opt_dce);
527 OPT(nir_opt_cse);
528 OPT(nir_opt_combine_stores, nir_var_all);
529
530 /* Passing 0 to the peephole select pass causes it to convert
531 * if-statements that contain only move instructions in the branches
532 * regardless of the count.
533 *
534 * Passing 1 to the peephole select pass causes it to convert
535 * if-statements that contain at most a single ALU instruction (total)
536 * in both branches. Before Gen6, some math instructions were
537 * prohibitively expensive and the results of compare operations need an
538 * extra resolve step. For these reasons, this pass is more harmful
539 * than good on those platforms.
540 *
541 * For indirect loads of uniforms (push constants), we assume that array
542 * indices will nearly always be in bounds and the cost of the load is
543 * low. Therefore there shouldn't be a performance benefit to avoid it.
544 * However, in vec4 tessellation shaders, these loads operate by
545 * actually pulling from memory.
546 */
547 const bool is_vec4_tessellation = !is_scalar &&
548 (nir->info.stage == MESA_SHADER_TESS_CTRL ||
549 nir->info.stage == MESA_SHADER_TESS_EVAL);
550 OPT(nir_opt_peephole_select, 0, !is_vec4_tessellation, false);
551 OPT(nir_opt_peephole_select, 1, !is_vec4_tessellation,
552 compiler->devinfo->gen >= 6);
553
554 OPT(nir_opt_intrinsics);
555 OPT(nir_opt_idiv_const, 32);
556 OPT(nir_opt_algebraic);
557 OPT(nir_opt_constant_folding);
558
559 if (lower_flrp != 0) {
560 if (OPT(nir_lower_flrp,
561 lower_flrp,
562 false /* always_precise */,
563 compiler->devinfo->gen >= 6)) {
564 OPT(nir_opt_constant_folding);
565 }
566
567 /* Nothing should rematerialize any flrps, so we only need to do this
568 * lowering once.
569 */
570 lower_flrp = 0;
571 }
572
573 OPT(nir_opt_dead_cf);
574 if (OPT(nir_opt_trivial_continues)) {
575 /* If nir_opt_trivial_continues makes progress, then we need to clean
576 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
577 * to make progress.
578 */
579 OPT(nir_copy_prop);
580 OPT(nir_opt_dce);
581 }
582 OPT(nir_opt_if, false);
583 if (nir->options->max_unroll_iterations != 0) {
584 OPT(nir_opt_loop_unroll, indirect_mask);
585 }
586 OPT(nir_opt_remove_phis);
587 OPT(nir_opt_undef);
588 OPT(nir_lower_pack);
589 } while (progress);
590
591 /* Workaround Gfxbench unused local sampler variable which will trigger an
592 * assert in the opt_large_constants pass.
593 */
594 OPT(nir_remove_dead_variables, nir_var_function_temp);
595 }
596
597 static unsigned
598 lower_bit_size_callback(const nir_alu_instr *alu, UNUSED void *data)
599 {
600 assert(alu->dest.dest.is_ssa);
601 if (alu->dest.dest.ssa.bit_size >= 32)
602 return 0;
603
604 const struct brw_compiler *compiler = (const struct brw_compiler *) data;
605
606 switch (alu->op) {
607 case nir_op_idiv:
608 case nir_op_imod:
609 case nir_op_irem:
610 case nir_op_udiv:
611 case nir_op_umod:
612 case nir_op_fceil:
613 case nir_op_ffloor:
614 case nir_op_ffract:
615 case nir_op_fround_even:
616 case nir_op_ftrunc:
617 return 32;
618 case nir_op_frcp:
619 case nir_op_frsq:
620 case nir_op_fsqrt:
621 case nir_op_fpow:
622 case nir_op_fexp2:
623 case nir_op_flog2:
624 case nir_op_fsin:
625 case nir_op_fcos:
626 return compiler->devinfo->gen < 9 ? 32 : 0;
627 default:
628 return 0;
629 }
630 }
631
632 /* Does some simple lowering and runs the standard suite of optimizations
633 *
634 * This is intended to be called more-or-less directly after you get the
635 * shader out of GLSL or some other source. While it is geared towards i965,
636 * it is not at all generator-specific except for the is_scalar flag. Even
637 * there, it is safe to call with is_scalar = false for a shader that is
638 * intended for the FS backend as long as nir_optimize is called again with
639 * is_scalar = true to scalarize everything prior to code gen.
640 */
641 void
642 brw_preprocess_nir(const struct brw_compiler *compiler, nir_shader *nir,
643 const nir_shader *softfp64)
644 {
645 const struct gen_device_info *devinfo = compiler->devinfo;
646 UNUSED bool progress; /* Written by OPT */
647
648 const bool is_scalar = compiler->scalar_stage[nir->info.stage];
649
650 if (is_scalar) {
651 OPT(nir_lower_alu_to_scalar, NULL);
652 }
653
654 if (nir->info.stage == MESA_SHADER_GEOMETRY)
655 OPT(nir_lower_gs_intrinsics);
656
657 /* See also brw_nir_trig_workarounds.py */
658 if (compiler->precise_trig &&
659 !(devinfo->gen >= 10 || devinfo->is_kabylake))
660 OPT(brw_nir_apply_trig_workarounds);
661
662 static const nir_lower_tex_options tex_options = {
663 .lower_txp = ~0,
664 .lower_txf_offset = true,
665 .lower_rect_offset = true,
666 .lower_tex_without_implicit_lod = true,
667 .lower_txd_cube_map = true,
668 .lower_txb_shadow_clamp = true,
669 .lower_txd_shadow_clamp = true,
670 .lower_txd_offset_clamp = true,
671 .lower_tg4_offsets = true,
672 };
673
674 OPT(nir_lower_tex, &tex_options);
675 OPT(nir_normalize_cubemap_coords);
676
677 OPT(nir_lower_global_vars_to_local);
678
679 OPT(nir_split_var_copies);
680 OPT(nir_split_struct_vars, nir_var_function_temp);
681
682 brw_nir_optimize(nir, compiler, is_scalar, true);
683
684 bool lowered_64bit_ops = false;
685 do {
686 progress = false;
687
688 OPT(nir_lower_int64, nir->options->lower_int64_options);
689 OPT(nir_lower_doubles, softfp64, nir->options->lower_doubles_options);
690
691 /* Necessary to lower add -> sub and div -> mul/rcp */
692 OPT(nir_opt_algebraic);
693
694 lowered_64bit_ops |= progress;
695 } while (progress);
696
697 /* This needs to be run after the first optimization pass but before we
698 * lower indirect derefs away
699 */
700 if (compiler->supports_shader_constants) {
701 OPT(nir_opt_large_constants, NULL, 32);
702 }
703
704 OPT(nir_lower_bit_size, lower_bit_size_callback, (void *)compiler);
705
706 if (is_scalar) {
707 OPT(nir_lower_load_const_to_scalar);
708 }
709
710 /* Lower a bunch of stuff */
711 OPT(nir_lower_var_copies);
712
713 OPT(nir_lower_system_values);
714
715 const nir_lower_subgroups_options subgroups_options = {
716 .subgroup_size = BRW_SUBGROUP_SIZE,
717 .ballot_bit_size = 32,
718 .lower_to_scalar = true,
719 .lower_subgroup_masks = true,
720 .lower_vote_trivial = !is_scalar,
721 .lower_shuffle = true,
722 };
723 OPT(nir_lower_subgroups, &subgroups_options);
724
725 OPT(nir_lower_clip_cull_distance_arrays);
726
727 nir_variable_mode indirect_mask =
728 brw_nir_no_indirect_mask(compiler, nir->info.stage);
729 OPT(nir_lower_indirect_derefs, indirect_mask);
730
731 /* Lower array derefs of vectors for SSBO and UBO loads. For both UBOs and
732 * SSBOs, our back-end is capable of loading an entire vec4 at a time and
733 * we would like to take advantage of that whenever possible regardless of
734 * whether or not the app gives us full loads. This should allow the
735 * optimizer to combine UBO and SSBO load operations and save us some send
736 * messages.
737 */
738 OPT(nir_lower_array_deref_of_vec,
739 nir_var_mem_ubo | nir_var_mem_ssbo,
740 nir_lower_direct_array_deref_of_vec_load);
741
742 /* Get rid of split copies */
743 brw_nir_optimize(nir, compiler, is_scalar, false);
744 }
745
746 void
747 brw_nir_link_shaders(const struct brw_compiler *compiler,
748 nir_shader *producer, nir_shader *consumer)
749 {
750 nir_lower_io_arrays_to_elements(producer, consumer);
751 nir_validate_shader(producer, "after nir_lower_io_arrays_to_elements");
752 nir_validate_shader(consumer, "after nir_lower_io_arrays_to_elements");
753
754 const bool p_is_scalar = compiler->scalar_stage[producer->info.stage];
755 const bool c_is_scalar = compiler->scalar_stage[consumer->info.stage];
756
757 if (p_is_scalar && c_is_scalar) {
758 NIR_PASS_V(producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
759 NIR_PASS_V(consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
760 brw_nir_optimize(producer, compiler, p_is_scalar, false);
761 brw_nir_optimize(consumer, compiler, c_is_scalar, false);
762 }
763
764 if (nir_link_opt_varyings(producer, consumer))
765 brw_nir_optimize(consumer, compiler, c_is_scalar, false);
766
767 NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out);
768 NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in);
769
770 if (nir_remove_unused_varyings(producer, consumer)) {
771 NIR_PASS_V(producer, nir_lower_global_vars_to_local);
772 NIR_PASS_V(consumer, nir_lower_global_vars_to_local);
773
774 /* The backend might not be able to handle indirects on
775 * temporaries so we need to lower indirects on any of the
776 * varyings we have demoted here.
777 */
778 NIR_PASS_V(producer, nir_lower_indirect_derefs,
779 brw_nir_no_indirect_mask(compiler, producer->info.stage));
780 NIR_PASS_V(consumer, nir_lower_indirect_derefs,
781 brw_nir_no_indirect_mask(compiler, consumer->info.stage));
782
783 brw_nir_optimize(producer, compiler, p_is_scalar, false);
784 brw_nir_optimize(consumer, compiler, c_is_scalar, false);
785 }
786
787 NIR_PASS_V(producer, nir_lower_io_to_vector, nir_var_shader_out);
788 NIR_PASS_V(producer, nir_opt_combine_stores, nir_var_shader_out);
789 NIR_PASS_V(consumer, nir_lower_io_to_vector, nir_var_shader_in);
790
791 if (producer->info.stage != MESA_SHADER_TESS_CTRL) {
792 /* Calling lower_io_to_vector creates output variable writes with
793 * write-masks. On non-TCS outputs, the back-end can't handle it and we
794 * need to call nir_lower_io_to_temporaries to get rid of them. This,
795 * in turn, creates temporary variables and extra copy_deref intrinsics
796 * that we need to clean up.
797 */
798 NIR_PASS_V(producer, nir_lower_io_to_temporaries,
799 nir_shader_get_entrypoint(producer), true, false);
800 NIR_PASS_V(producer, nir_lower_global_vars_to_local);
801 NIR_PASS_V(producer, nir_split_var_copies);
802 NIR_PASS_V(producer, nir_lower_var_copies);
803 }
804 }
805
806 /* Prepare the given shader for codegen
807 *
808 * This function is intended to be called right before going into the actual
809 * backend and is highly backend-specific. Also, once this function has been
810 * called on a shader, it will no longer be in SSA form so most optimizations
811 * will not work.
812 */
813 void
814 brw_postprocess_nir(nir_shader *nir, const struct brw_compiler *compiler,
815 bool is_scalar)
816 {
817 const struct gen_device_info *devinfo = compiler->devinfo;
818 bool debug_enabled =
819 (INTEL_DEBUG & intel_debug_flag_for_shader_stage(nir->info.stage));
820
821 UNUSED bool progress; /* Written by OPT */
822
823 OPT(brw_nir_lower_mem_access_bit_sizes);
824
825 do {
826 progress = false;
827 OPT(nir_opt_algebraic_before_ffma);
828 } while (progress);
829
830 brw_nir_optimize(nir, compiler, is_scalar, false);
831
832 if (OPT(nir_lower_int64, nir->options->lower_int64_options))
833 brw_nir_optimize(nir, compiler, is_scalar, false);
834
835 if (devinfo->gen >= 6) {
836 /* Try and fuse multiply-adds */
837 OPT(brw_nir_opt_peephole_ffma);
838 }
839
840 if (OPT(nir_opt_comparison_pre)) {
841 OPT(nir_copy_prop);
842 OPT(nir_opt_dce);
843 OPT(nir_opt_cse);
844
845 /* Do the select peepehole again. nir_opt_comparison_pre (combined with
846 * the other optimization passes) will have removed at least one
847 * instruction from one of the branches of the if-statement, so now it
848 * might be under the threshold of conversion to bcsel.
849 *
850 * See brw_nir_optimize for the explanation of is_vec4_tessellation.
851 */
852 const bool is_vec4_tessellation = !is_scalar &&
853 (nir->info.stage == MESA_SHADER_TESS_CTRL ||
854 nir->info.stage == MESA_SHADER_TESS_EVAL);
855 OPT(nir_opt_peephole_select, 0, is_vec4_tessellation, false);
856 OPT(nir_opt_peephole_select, 1, is_vec4_tessellation,
857 compiler->devinfo->gen >= 6);
858 }
859
860 do {
861 progress = false;
862 if (OPT(nir_opt_algebraic_late)) {
863 /* At this late stage, anything that makes more constants will wreak
864 * havok on the vec4 backend. The handling of constants in the vec4
865 * backend is not good.
866 */
867 if (is_scalar) {
868 OPT(nir_opt_constant_folding);
869 OPT(nir_copy_prop);
870 }
871 OPT(nir_opt_dce);
872 OPT(nir_opt_cse);
873 }
874 } while (progress);
875
876
877 OPT(brw_nir_lower_conversions);
878
879 if (is_scalar)
880 OPT(nir_lower_alu_to_scalar, NULL);
881 OPT(nir_lower_to_source_mods, nir_lower_all_source_mods);
882 OPT(nir_copy_prop);
883 OPT(nir_opt_dce);
884 OPT(nir_opt_move_comparisons);
885
886 OPT(nir_lower_bool_to_int32);
887
888 OPT(nir_lower_locals_to_regs);
889
890 if (unlikely(debug_enabled)) {
891 /* Re-index SSA defs so we print more sensible numbers. */
892 nir_foreach_function(function, nir) {
893 if (function->impl)
894 nir_index_ssa_defs(function->impl);
895 }
896
897 fprintf(stderr, "NIR (SSA form) for %s shader:\n",
898 _mesa_shader_stage_to_string(nir->info.stage));
899 nir_print_shader(nir, stderr);
900 }
901
902 OPT(nir_convert_from_ssa, true);
903
904 if (!is_scalar) {
905 OPT(nir_move_vec_src_uses_to_dest);
906 OPT(nir_lower_vec_to_movs);
907 }
908
909 OPT(nir_opt_dce);
910
911 if (OPT(nir_opt_rematerialize_compares))
912 OPT(nir_opt_dce);
913
914 /* This is the last pass we run before we start emitting stuff. It
915 * determines when we need to insert boolean resolves on Gen <= 5. We
916 * run it last because it stashes data in instr->pass_flags and we don't
917 * want that to be squashed by other NIR passes.
918 */
919 if (devinfo->gen <= 5)
920 brw_nir_analyze_boolean_resolves(nir);
921
922 nir_sweep(nir);
923
924 if (unlikely(debug_enabled)) {
925 fprintf(stderr, "NIR (final form) for %s shader:\n",
926 _mesa_shader_stage_to_string(nir->info.stage));
927 nir_print_shader(nir, stderr);
928 }
929 }
930
931 void
932 brw_nir_apply_sampler_key(nir_shader *nir,
933 const struct brw_compiler *compiler,
934 const struct brw_sampler_prog_key_data *key_tex,
935 bool is_scalar)
936 {
937 const struct gen_device_info *devinfo = compiler->devinfo;
938 nir_lower_tex_options tex_options = {
939 .lower_txd_clamp_bindless_sampler = true,
940 .lower_txd_clamp_if_sampler_index_not_lt_16 = true,
941 };
942
943 /* Iron Lake and prior require lowering of all rectangle textures */
944 if (devinfo->gen < 6)
945 tex_options.lower_rect = true;
946
947 /* Prior to Broadwell, our hardware can't actually do GL_CLAMP */
948 if (devinfo->gen < 8) {
949 tex_options.saturate_s = key_tex->gl_clamp_mask[0];
950 tex_options.saturate_t = key_tex->gl_clamp_mask[1];
951 tex_options.saturate_r = key_tex->gl_clamp_mask[2];
952 }
953
954 /* Prior to Haswell, we have to fake texture swizzle */
955 for (unsigned s = 0; s < MAX_SAMPLERS; s++) {
956 if (key_tex->swizzles[s] == SWIZZLE_NOOP)
957 continue;
958
959 tex_options.swizzle_result |= (1 << s);
960 for (unsigned c = 0; c < 4; c++)
961 tex_options.swizzles[s][c] = GET_SWZ(key_tex->swizzles[s], c);
962 }
963
964 /* Prior to Haswell, we have to lower gradients on shadow samplers */
965 tex_options.lower_txd_shadow = devinfo->gen < 8 && !devinfo->is_haswell;
966
967 tex_options.lower_y_uv_external = key_tex->y_uv_image_mask;
968 tex_options.lower_y_u_v_external = key_tex->y_u_v_image_mask;
969 tex_options.lower_yx_xuxv_external = key_tex->yx_xuxv_image_mask;
970 tex_options.lower_xy_uxvx_external = key_tex->xy_uxvx_image_mask;
971 tex_options.lower_ayuv_external = key_tex->ayuv_image_mask;
972 tex_options.lower_xyuv_external = key_tex->xyuv_image_mask;
973
974 /* Setup array of scaling factors for each texture. */
975 memcpy(&tex_options.scale_factors, &key_tex->scale_factors,
976 sizeof(tex_options.scale_factors));
977
978 if (nir_lower_tex(nir, &tex_options)) {
979 nir_validate_shader(nir, "after nir_lower_tex");
980 brw_nir_optimize(nir, compiler, is_scalar, false);
981 }
982 }
983
984 enum brw_reg_type
985 brw_type_for_nir_type(const struct gen_device_info *devinfo, nir_alu_type type)
986 {
987 switch (type) {
988 case nir_type_uint:
989 case nir_type_uint32:
990 return BRW_REGISTER_TYPE_UD;
991 case nir_type_bool:
992 case nir_type_int:
993 case nir_type_bool32:
994 case nir_type_int32:
995 return BRW_REGISTER_TYPE_D;
996 case nir_type_float:
997 case nir_type_float32:
998 return BRW_REGISTER_TYPE_F;
999 case nir_type_float16:
1000 return BRW_REGISTER_TYPE_HF;
1001 case nir_type_float64:
1002 return BRW_REGISTER_TYPE_DF;
1003 case nir_type_int64:
1004 return devinfo->gen < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_Q;
1005 case nir_type_uint64:
1006 return devinfo->gen < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_UQ;
1007 case nir_type_int16:
1008 return BRW_REGISTER_TYPE_W;
1009 case nir_type_uint16:
1010 return BRW_REGISTER_TYPE_UW;
1011 case nir_type_int8:
1012 return BRW_REGISTER_TYPE_B;
1013 case nir_type_uint8:
1014 return BRW_REGISTER_TYPE_UB;
1015 default:
1016 unreachable("unknown type");
1017 }
1018
1019 return BRW_REGISTER_TYPE_F;
1020 }
1021
1022 /* Returns the glsl_base_type corresponding to a nir_alu_type.
1023 * This is used by both brw_vec4_nir and brw_fs_nir.
1024 */
1025 enum glsl_base_type
1026 brw_glsl_base_type_for_nir_type(nir_alu_type type)
1027 {
1028 switch (type) {
1029 case nir_type_float:
1030 case nir_type_float32:
1031 return GLSL_TYPE_FLOAT;
1032
1033 case nir_type_float16:
1034 return GLSL_TYPE_FLOAT16;
1035
1036 case nir_type_float64:
1037 return GLSL_TYPE_DOUBLE;
1038
1039 case nir_type_int:
1040 case nir_type_int32:
1041 return GLSL_TYPE_INT;
1042
1043 case nir_type_uint:
1044 case nir_type_uint32:
1045 return GLSL_TYPE_UINT;
1046
1047 case nir_type_int16:
1048 return GLSL_TYPE_INT16;
1049
1050 case nir_type_uint16:
1051 return GLSL_TYPE_UINT16;
1052
1053 default:
1054 unreachable("bad type");
1055 }
1056 }
1057
1058 nir_shader *
1059 brw_nir_create_passthrough_tcs(void *mem_ctx, const struct brw_compiler *compiler,
1060 const nir_shader_compiler_options *options,
1061 const struct brw_tcs_prog_key *key)
1062 {
1063 nir_builder b;
1064 nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_TESS_CTRL,
1065 options);
1066 nir_shader *nir = b.shader;
1067 nir_variable *var;
1068 nir_intrinsic_instr *load;
1069 nir_intrinsic_instr *store;
1070 nir_ssa_def *zero = nir_imm_int(&b, 0);
1071 nir_ssa_def *invoc_id = nir_load_invocation_id(&b);
1072
1073 nir->info.inputs_read = key->outputs_written &
1074 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
1075 nir->info.outputs_written = key->outputs_written;
1076 nir->info.tess.tcs_vertices_out = key->input_vertices;
1077 nir->info.name = ralloc_strdup(nir, "passthrough");
1078 nir->num_uniforms = 8 * sizeof(uint32_t);
1079
1080 var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_0");
1081 var->data.location = 0;
1082 var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_1");
1083 var->data.location = 1;
1084
1085 /* Write the patch URB header. */
1086 for (int i = 0; i <= 1; i++) {
1087 load = nir_intrinsic_instr_create(nir, nir_intrinsic_load_uniform);
1088 load->num_components = 4;
1089 load->src[0] = nir_src_for_ssa(zero);
1090 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
1091 nir_intrinsic_set_base(load, i * 4 * sizeof(uint32_t));
1092 nir_builder_instr_insert(&b, &load->instr);
1093
1094 store = nir_intrinsic_instr_create(nir, nir_intrinsic_store_output);
1095 store->num_components = 4;
1096 store->src[0] = nir_src_for_ssa(&load->dest.ssa);
1097 store->src[1] = nir_src_for_ssa(zero);
1098 nir_intrinsic_set_base(store, VARYING_SLOT_TESS_LEVEL_INNER - i);
1099 nir_intrinsic_set_write_mask(store, WRITEMASK_XYZW);
1100 nir_builder_instr_insert(&b, &store->instr);
1101 }
1102
1103 /* Copy inputs to outputs. */
1104 uint64_t varyings = nir->info.inputs_read;
1105
1106 while (varyings != 0) {
1107 const int varying = ffsll(varyings) - 1;
1108
1109 load = nir_intrinsic_instr_create(nir,
1110 nir_intrinsic_load_per_vertex_input);
1111 load->num_components = 4;
1112 load->src[0] = nir_src_for_ssa(invoc_id);
1113 load->src[1] = nir_src_for_ssa(zero);
1114 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
1115 nir_intrinsic_set_base(load, varying);
1116 nir_builder_instr_insert(&b, &load->instr);
1117
1118 store = nir_intrinsic_instr_create(nir,
1119 nir_intrinsic_store_per_vertex_output);
1120 store->num_components = 4;
1121 store->src[0] = nir_src_for_ssa(&load->dest.ssa);
1122 store->src[1] = nir_src_for_ssa(invoc_id);
1123 store->src[2] = nir_src_for_ssa(zero);
1124 nir_intrinsic_set_base(store, varying);
1125 nir_intrinsic_set_write_mask(store, WRITEMASK_XYZW);
1126 nir_builder_instr_insert(&b, &store->instr);
1127
1128 varyings &= ~BITFIELD64_BIT(varying);
1129 }
1130
1131 nir_validate_shader(nir, "in brw_nir_create_passthrough_tcs");
1132
1133 brw_preprocess_nir(compiler, nir, NULL);
1134
1135 return nir;
1136 }