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/intel_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
remap_tess_levels(nir_builder * b,nir_intrinsic_instr * intr,GLenum primitive_mode)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, undef);
79 }
80 nir_instr_remove(&intr->instr);
81 }
82
83 return true;
84 }
85
86 static bool
is_input(nir_intrinsic_instr * intrin)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
is_output(nir_intrinsic_instr * intrin)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
remap_patch_urb_offsets(nir_block * block,nir_builder * b,const struct brw_vue_map * vue_map,GLenum tes_primitive_mode)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 TCS") == 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
brw_nir_lower_vs_inputs(nir_shader * nir,bool edgeflag_is_last,const uint8_t * vs_attrib_wa_flags)162 brw_nir_lower_vs_inputs(nir_shader *nir,
163 bool edgeflag_is_last,
164 const uint8_t *vs_attrib_wa_flags)
165 {
166 /* Start with the location of the variable's base. */
167 nir_foreach_shader_in_variable(var, nir)
168 var->data.driver_location = var->data.location;
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,
175 nir_lower_io_lower_64bit_to_32);
176
177 /* This pass needs actual constants */
178 nir_opt_constant_folding(nir);
179
180 nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
181
182 brw_nir_apply_attribute_workarounds(nir, vs_attrib_wa_flags);
183
184 /* The last step is to remap VERT_ATTRIB_* to actual registers */
185
186 /* Whether or not we have any system generated values. gl_DrawID is not
187 * included here as it lives in its own vec4.
188 */
189 const bool has_sgvs =
190 BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_FIRST_VERTEX) ||
191 BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_BASE_INSTANCE) ||
192 BITSET_TEST(nir->info.system_values_read, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) ||
193 BITSET_TEST(nir->info.system_values_read, 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 &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 uint64_t inputs_read = nir->info.inputs_read;
275 int slot = -1;
276 if (edgeflag_is_last) {
277 inputs_read &= ~BITFIELD64_BIT(VERT_ATTRIB_EDGEFLAG);
278 if (attr == VERT_ATTRIB_EDGEFLAG)
279 slot = num_inputs - 1;
280 }
281 if (slot == -1)
282 slot = util_bitcount64(inputs_read &
283 BITFIELD64_MASK(attr));
284 nir_intrinsic_set_base(intrin, slot);
285 break;
286 }
287
288 default:
289 break; /* Nothing to do */
290 }
291 }
292 }
293 }
294 }
295
296 void
brw_nir_lower_vue_inputs(nir_shader * nir,const struct brw_vue_map * vue_map)297 brw_nir_lower_vue_inputs(nir_shader *nir,
298 const struct brw_vue_map *vue_map)
299 {
300 nir_foreach_shader_in_variable(var, nir)
301 var->data.driver_location = var->data.location;
302
303 /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
304 nir_lower_io(nir, nir_var_shader_in, type_size_vec4,
305 nir_lower_io_lower_64bit_to_32);
306
307 /* This pass needs actual constants */
308 nir_opt_constant_folding(nir);
309
310 nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
311
312 nir_foreach_function(function, nir) {
313 if (!function->impl)
314 continue;
315
316 nir_foreach_block(block, function->impl) {
317 nir_foreach_instr(instr, block) {
318 if (instr->type != nir_instr_type_intrinsic)
319 continue;
320
321 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
322
323 if (intrin->intrinsic == nir_intrinsic_load_input ||
324 intrin->intrinsic == nir_intrinsic_load_per_vertex_input) {
325 /* Offset 0 is the VUE header, which contains
326 * VARYING_SLOT_LAYER [.y], VARYING_SLOT_VIEWPORT [.z], and
327 * VARYING_SLOT_PSIZ [.w].
328 */
329 int varying = nir_intrinsic_base(intrin);
330 int vue_slot;
331 switch (varying) {
332 case VARYING_SLOT_PSIZ:
333 nir_intrinsic_set_base(intrin, 0);
334 nir_intrinsic_set_component(intrin, 3);
335 break;
336
337 default:
338 vue_slot = vue_map->varying_to_slot[varying];
339 assert(vue_slot != -1);
340 nir_intrinsic_set_base(intrin, vue_slot);
341 break;
342 }
343 }
344 }
345 }
346 }
347 }
348
349 void
brw_nir_lower_tes_inputs(nir_shader * nir,const struct brw_vue_map * vue_map)350 brw_nir_lower_tes_inputs(nir_shader *nir, const struct brw_vue_map *vue_map)
351 {
352 nir_foreach_shader_in_variable(var, nir)
353 var->data.driver_location = var->data.location;
354
355 nir_lower_io(nir, nir_var_shader_in, type_size_vec4,
356 nir_lower_io_lower_64bit_to_32);
357
358 /* This pass needs actual constants */
359 nir_opt_constant_folding(nir);
360
361 nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
362
363 nir_foreach_function(function, nir) {
364 if (function->impl) {
365 nir_builder b;
366 nir_builder_init(&b, function->impl);
367 nir_foreach_block(block, function->impl) {
368 remap_patch_urb_offsets(block, &b, vue_map,
369 nir->info.tess.primitive_mode);
370 }
371 }
372 }
373 }
374
375 /**
376 * Convert interpolateAtOffset() offsets from [-0.5, +0.5] floating point
377 * offsets to integer [-8, +7] offsets (in units of 1/16th of a pixel).
378 *
379 * We clamp to +7/16 on the upper end of the range, since +0.5 isn't
380 * representable in a S0.4 value; a naive conversion would give us -8/16,
381 * which is the opposite of what was intended.
382 *
383 * This is allowed by GL_ARB_gpu_shader5's quantization rules:
384 *
385 * "Not all values of <offset> may be supported; x and y offsets may
386 * be rounded to fixed-point values with the number of fraction bits
387 * given by the implementation-dependent constant
388 * FRAGMENT_INTERPOLATION_OFFSET_BITS."
389 */
390 static bool
lower_barycentric_at_offset(nir_builder * b,nir_instr * instr,void * data)391 lower_barycentric_at_offset(nir_builder *b, nir_instr *instr, void *data)
392 {
393 if (instr->type != nir_instr_type_intrinsic)
394 return false;
395
396 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
397
398 if (intrin->intrinsic != nir_intrinsic_load_barycentric_at_offset)
399 return false;
400
401 b->cursor = nir_before_instr(instr);
402
403 assert(intrin->src[0].ssa);
404 nir_ssa_def *offset =
405 nir_imin(b, nir_imm_int(b, 7),
406 nir_f2i32(b, nir_fmul(b, nir_imm_float(b, 16),
407 intrin->src[0].ssa)));
408
409 nir_instr_rewrite_src(instr, &intrin->src[0], nir_src_for_ssa(offset));
410
411 return true;
412 }
413
414 void
brw_nir_lower_fs_inputs(nir_shader * nir,const struct intel_device_info * devinfo,const struct brw_wm_prog_key * key)415 brw_nir_lower_fs_inputs(nir_shader *nir,
416 const struct intel_device_info *devinfo,
417 const struct brw_wm_prog_key *key)
418 {
419 nir_foreach_shader_in_variable(var, nir) {
420 var->data.driver_location = var->data.location;
421
422 /* Apply default interpolation mode.
423 *
424 * Everything defaults to smooth except for the legacy GL color
425 * built-in variables, which might be flat depending on API state.
426 */
427 if (var->data.interpolation == INTERP_MODE_NONE) {
428 const bool flat = key->flat_shade &&
429 (var->data.location == VARYING_SLOT_COL0 ||
430 var->data.location == VARYING_SLOT_COL1);
431
432 var->data.interpolation = flat ? INTERP_MODE_FLAT
433 : INTERP_MODE_SMOOTH;
434 }
435
436 /* On Ironlake and below, there is only one interpolation mode.
437 * Centroid interpolation doesn't mean anything on this hardware --
438 * there is no multisampling.
439 */
440 if (devinfo->ver < 6) {
441 var->data.centroid = false;
442 var->data.sample = false;
443 }
444 }
445
446 nir_lower_io_options lower_io_options = nir_lower_io_lower_64bit_to_32;
447 if (key->persample_interp)
448 lower_io_options |= nir_lower_io_force_sample_interpolation;
449
450 nir_lower_io(nir, nir_var_shader_in, type_size_vec4, lower_io_options);
451 if (devinfo->ver >= 11)
452 nir_lower_interpolation(nir, ~0);
453
454 nir_shader_instructions_pass(nir, lower_barycentric_at_offset,
455 nir_metadata_block_index |
456 nir_metadata_dominance,
457 NULL);
458
459 /* This pass needs actual constants */
460 nir_opt_constant_folding(nir);
461
462 nir_io_add_const_offset_to_base(nir, nir_var_shader_in);
463 }
464
465 void
brw_nir_lower_vue_outputs(nir_shader * nir)466 brw_nir_lower_vue_outputs(nir_shader *nir)
467 {
468 nir_foreach_shader_out_variable(var, nir) {
469 var->data.driver_location = var->data.location;
470 }
471
472 nir_lower_io(nir, nir_var_shader_out, type_size_vec4,
473 nir_lower_io_lower_64bit_to_32);
474 }
475
476 void
brw_nir_lower_tcs_outputs(nir_shader * nir,const struct brw_vue_map * vue_map,GLenum tes_primitive_mode)477 brw_nir_lower_tcs_outputs(nir_shader *nir, const struct brw_vue_map *vue_map,
478 GLenum tes_primitive_mode)
479 {
480 nir_foreach_shader_out_variable(var, nir) {
481 var->data.driver_location = var->data.location;
482 }
483
484 nir_lower_io(nir, nir_var_shader_out, type_size_vec4,
485 nir_lower_io_lower_64bit_to_32);
486
487 /* This pass needs actual constants */
488 nir_opt_constant_folding(nir);
489
490 nir_io_add_const_offset_to_base(nir, nir_var_shader_out);
491
492 nir_foreach_function(function, nir) {
493 if (function->impl) {
494 nir_builder b;
495 nir_builder_init(&b, function->impl);
496 nir_foreach_block(block, function->impl) {
497 remap_patch_urb_offsets(block, &b, vue_map, tes_primitive_mode);
498 }
499 }
500 }
501 }
502
503 void
brw_nir_lower_fs_outputs(nir_shader * nir)504 brw_nir_lower_fs_outputs(nir_shader *nir)
505 {
506 nir_foreach_shader_out_variable(var, nir) {
507 var->data.driver_location =
508 SET_FIELD(var->data.index, BRW_NIR_FRAG_OUTPUT_INDEX) |
509 SET_FIELD(var->data.location, BRW_NIR_FRAG_OUTPUT_LOCATION);
510 }
511
512 nir_lower_io(nir, nir_var_shader_out, type_size_dvec4, 0);
513 }
514
515 #define OPT(pass, ...) ({ \
516 bool this_progress = false; \
517 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
518 if (this_progress) \
519 progress = true; \
520 this_progress; \
521 })
522
523 void
brw_nir_optimize(nir_shader * nir,const struct brw_compiler * compiler,bool is_scalar,bool allow_copies)524 brw_nir_optimize(nir_shader *nir, const struct brw_compiler *compiler,
525 bool is_scalar, bool allow_copies)
526 {
527 bool progress;
528 unsigned lower_flrp =
529 (nir->options->lower_flrp16 ? 16 : 0) |
530 (nir->options->lower_flrp32 ? 32 : 0) |
531 (nir->options->lower_flrp64 ? 64 : 0);
532
533 do {
534 progress = false;
535 OPT(nir_split_array_vars, nir_var_function_temp);
536 OPT(nir_shrink_vec_array_vars, nir_var_function_temp);
537 OPT(nir_opt_deref);
538 OPT(nir_lower_vars_to_ssa);
539 if (allow_copies) {
540 /* Only run this pass in the first call to brw_nir_optimize. Later
541 * calls assume that we've lowered away any copy_deref instructions
542 * and we don't want to introduce any more.
543 */
544 OPT(nir_opt_find_array_copies);
545 }
546 OPT(nir_opt_copy_prop_vars);
547 OPT(nir_opt_dead_write_vars);
548 OPT(nir_opt_combine_stores, nir_var_all);
549
550 if (is_scalar) {
551 OPT(nir_lower_alu_to_scalar, NULL, NULL);
552 } else {
553 OPT(nir_opt_shrink_vectors, true);
554 }
555
556 OPT(nir_copy_prop);
557
558 if (is_scalar) {
559 OPT(nir_lower_phis_to_scalar, false);
560 }
561
562 OPT(nir_copy_prop);
563 OPT(nir_opt_dce);
564 OPT(nir_opt_cse);
565 OPT(nir_opt_combine_stores, nir_var_all);
566
567 /* Passing 0 to the peephole select pass causes it to convert
568 * if-statements that contain only move instructions in the branches
569 * regardless of the count.
570 *
571 * Passing 1 to the peephole select pass causes it to convert
572 * if-statements that contain at most a single ALU instruction (total)
573 * in both branches. Before Gfx6, some math instructions were
574 * prohibitively expensive and the results of compare operations need an
575 * extra resolve step. For these reasons, this pass is more harmful
576 * than good on those platforms.
577 *
578 * For indirect loads of uniforms (push constants), we assume that array
579 * indices will nearly always be in bounds and the cost of the load is
580 * low. Therefore there shouldn't be a performance benefit to avoid it.
581 * However, in vec4 tessellation shaders, these loads operate by
582 * actually pulling from memory.
583 */
584 const bool is_vec4_tessellation = !is_scalar &&
585 (nir->info.stage == MESA_SHADER_TESS_CTRL ||
586 nir->info.stage == MESA_SHADER_TESS_EVAL);
587 OPT(nir_opt_peephole_select, 0, !is_vec4_tessellation, false);
588 OPT(nir_opt_peephole_select, 8, !is_vec4_tessellation,
589 compiler->devinfo->ver >= 6);
590
591 OPT(nir_opt_intrinsics);
592 OPT(nir_opt_idiv_const, 32);
593 OPT(nir_opt_algebraic);
594 OPT(nir_opt_constant_folding);
595
596 if (lower_flrp != 0) {
597 if (OPT(nir_lower_flrp,
598 lower_flrp,
599 false /* always_precise */)) {
600 OPT(nir_opt_constant_folding);
601 }
602
603 /* Nothing should rematerialize any flrps, so we only need to do this
604 * lowering once.
605 */
606 lower_flrp = 0;
607 }
608
609 OPT(nir_opt_dead_cf);
610 if (OPT(nir_opt_trivial_continues)) {
611 /* If nir_opt_trivial_continues makes progress, then we need to clean
612 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
613 * to make progress.
614 */
615 OPT(nir_copy_prop);
616 OPT(nir_opt_dce);
617 }
618 OPT(nir_opt_if, false);
619 OPT(nir_opt_conditional_discard);
620 if (nir->options->max_unroll_iterations != 0) {
621 OPT(nir_opt_loop_unroll);
622 }
623 OPT(nir_opt_remove_phis);
624 OPT(nir_opt_gcm, false);
625 OPT(nir_opt_undef);
626 OPT(nir_lower_pack);
627 } while (progress);
628
629 /* Workaround Gfxbench unused local sampler variable which will trigger an
630 * assert in the opt_large_constants pass.
631 */
632 OPT(nir_remove_dead_variables, nir_var_function_temp, NULL);
633 }
634
635 static unsigned
lower_bit_size_callback(const nir_instr * instr,UNUSED void * data)636 lower_bit_size_callback(const nir_instr *instr, UNUSED void *data)
637 {
638 const struct brw_compiler *compiler = (const struct brw_compiler *) data;
639 const struct intel_device_info *devinfo = compiler->devinfo;
640
641 switch (instr->type) {
642 case nir_instr_type_alu: {
643 nir_alu_instr *alu = nir_instr_as_alu(instr);
644 assert(alu->dest.dest.is_ssa);
645 if (alu->dest.dest.ssa.bit_size >= 32)
646 return 0;
647
648 /* Note: nir_op_iabs and nir_op_ineg are not lowered here because the
649 * 8-bit ABS or NEG instruction should eventually get copy propagated
650 * into the MOV that does the type conversion. This results in far
651 * fewer MOV instructions.
652 */
653 switch (alu->op) {
654 case nir_op_idiv:
655 case nir_op_imod:
656 case nir_op_irem:
657 case nir_op_udiv:
658 case nir_op_umod:
659 case nir_op_fceil:
660 case nir_op_ffloor:
661 case nir_op_ffract:
662 case nir_op_fround_even:
663 case nir_op_ftrunc:
664 return 32;
665 case nir_op_frcp:
666 case nir_op_frsq:
667 case nir_op_fsqrt:
668 case nir_op_fpow:
669 case nir_op_fexp2:
670 case nir_op_flog2:
671 case nir_op_fsin:
672 case nir_op_fcos:
673 return devinfo->ver < 9 ? 32 : 0;
674 case nir_op_isign:
675 assert(!"Should have been lowered by nir_opt_algebraic.");
676 return 0;
677 default:
678 if (nir_op_infos[alu->op].num_inputs >= 2 &&
679 alu->dest.dest.ssa.bit_size == 8)
680 return 16;
681
682 if (nir_alu_instr_is_comparison(alu) &&
683 alu->src[0].src.ssa->bit_size == 8)
684 return 16;
685
686 return 0;
687 }
688 break;
689 }
690
691 case nir_instr_type_intrinsic: {
692 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
693 switch (intrin->intrinsic) {
694 case nir_intrinsic_read_invocation:
695 case nir_intrinsic_read_first_invocation:
696 case nir_intrinsic_vote_feq:
697 case nir_intrinsic_vote_ieq:
698 case nir_intrinsic_shuffle:
699 case nir_intrinsic_shuffle_xor:
700 case nir_intrinsic_shuffle_up:
701 case nir_intrinsic_shuffle_down:
702 case nir_intrinsic_quad_broadcast:
703 case nir_intrinsic_quad_swap_horizontal:
704 case nir_intrinsic_quad_swap_vertical:
705 case nir_intrinsic_quad_swap_diagonal:
706 if (intrin->src[0].ssa->bit_size == 8)
707 return 16;
708 return 0;
709
710 case nir_intrinsic_reduce:
711 case nir_intrinsic_inclusive_scan:
712 case nir_intrinsic_exclusive_scan:
713 /* There are a couple of register region issues that make things
714 * complicated for 8-bit types:
715 *
716 * 1. Only raw moves are allowed to write to a packed 8-bit
717 * destination.
718 * 2. If we use a strided destination, the efficient way to do
719 * scan operations ends up using strides that are too big to
720 * encode in an instruction.
721 *
722 * To get around these issues, we just do all 8-bit scan operations
723 * in 16 bits. It's actually fewer instructions than what we'd have
724 * to do if we were trying to do it in native 8-bit types and the
725 * results are the same once we truncate to 8 bits at the end.
726 */
727 if (intrin->dest.ssa.bit_size == 8)
728 return 16;
729 return 0;
730
731 default:
732 return 0;
733 }
734 break;
735 }
736
737 case nir_instr_type_phi: {
738 nir_phi_instr *phi = nir_instr_as_phi(instr);
739 if (phi->dest.ssa.bit_size == 8)
740 return 16;
741 return 0;
742 }
743
744 default:
745 return 0;
746 }
747 }
748
749 /* Does some simple lowering and runs the standard suite of optimizations
750 *
751 * This is intended to be called more-or-less directly after you get the
752 * shader out of GLSL or some other source. While it is geared towards i965,
753 * it is not at all generator-specific except for the is_scalar flag. Even
754 * there, it is safe to call with is_scalar = false for a shader that is
755 * intended for the FS backend as long as nir_optimize is called again with
756 * is_scalar = true to scalarize everything prior to code gen.
757 */
758 void
brw_preprocess_nir(const struct brw_compiler * compiler,nir_shader * nir,const nir_shader * softfp64)759 brw_preprocess_nir(const struct brw_compiler *compiler, nir_shader *nir,
760 const nir_shader *softfp64)
761 {
762 const struct intel_device_info *devinfo = compiler->devinfo;
763 UNUSED bool progress; /* Written by OPT */
764
765 const bool is_scalar = compiler->scalar_stage[nir->info.stage];
766
767 nir_validate_ssa_dominance(nir, "before brw_preprocess_nir");
768
769 if (is_scalar) {
770 OPT(nir_lower_alu_to_scalar, NULL, NULL);
771 }
772
773 if (nir->info.stage == MESA_SHADER_GEOMETRY)
774 OPT(nir_lower_gs_intrinsics, 0);
775
776 /* See also brw_nir_trig_workarounds.py */
777 if (compiler->precise_trig &&
778 !(devinfo->ver >= 10 || devinfo->is_kabylake))
779 OPT(brw_nir_apply_trig_workarounds);
780
781 if (devinfo->ver >= 12)
782 OPT(brw_nir_clamp_image_1d_2d_array_sizes);
783
784 const nir_lower_tex_options tex_options = {
785 .lower_txp = ~0,
786 .lower_txf_offset = true,
787 .lower_rect_offset = true,
788 .lower_txd_cube_map = true,
789 .lower_txd_3d = devinfo->verx10 >= 125,
790 .lower_txb_shadow_clamp = true,
791 .lower_txd_shadow_clamp = true,
792 .lower_txd_offset_clamp = true,
793 .lower_tg4_offsets = true,
794 .lower_txs_lod = true, /* Wa_14012320009 */
795 };
796
797 OPT(nir_lower_tex, &tex_options);
798 OPT(nir_normalize_cubemap_coords);
799
800 OPT(nir_lower_global_vars_to_local);
801
802 OPT(nir_split_var_copies);
803 OPT(nir_split_struct_vars, nir_var_function_temp);
804
805 brw_nir_optimize(nir, compiler, is_scalar, true);
806
807 OPT(nir_lower_doubles, softfp64, nir->options->lower_doubles_options);
808 OPT(nir_lower_int64);
809
810 OPT(nir_lower_bit_size, lower_bit_size_callback, (void *)compiler);
811
812 if (is_scalar) {
813 OPT(nir_lower_load_const_to_scalar);
814 }
815
816 /* Lower a bunch of stuff */
817 OPT(nir_lower_var_copies);
818
819 /* This needs to be run after the first optimization pass but before we
820 * lower indirect derefs away
821 */
822 if (compiler->supports_shader_constants) {
823 OPT(nir_opt_large_constants, NULL, 32);
824 }
825
826 OPT(nir_lower_system_values);
827 OPT(nir_lower_compute_system_values, NULL);
828
829 const nir_lower_subgroups_options subgroups_options = {
830 .ballot_bit_size = 32,
831 .ballot_components = 1,
832 .lower_to_scalar = true,
833 .lower_vote_trivial = !is_scalar,
834 .lower_shuffle = true,
835 .lower_quad_broadcast_dynamic = true,
836 .lower_elect = true,
837 };
838 OPT(nir_lower_subgroups, &subgroups_options);
839
840 OPT(nir_lower_clip_cull_distance_arrays);
841
842 nir_variable_mode indirect_mask =
843 brw_nir_no_indirect_mask(compiler, nir->info.stage);
844 OPT(nir_lower_indirect_derefs, indirect_mask, UINT32_MAX);
845
846 /* Even in cases where we can handle indirect temporaries via scratch, we
847 * it can still be expensive. Lower indirects on small arrays to
848 * conditional load/stores.
849 *
850 * The threshold of 16 was chosen semi-arbitrarily. The idea is that an
851 * indirect on an array of 16 elements is about 30 instructions at which
852 * point, you may be better off doing a send. With a SIMD8 program, 16
853 * floats is 1/8 of the entire register file. Any array larger than that
854 * is likely to cause pressure issues. Also, this value is sufficiently
855 * high that the benchmarks known to suffer from large temporary array
856 * issues are helped but nothing else in shader-db is hurt except for maybe
857 * that one kerbal space program shader.
858 */
859 if (is_scalar && !(indirect_mask & nir_var_function_temp))
860 OPT(nir_lower_indirect_derefs, nir_var_function_temp, 16);
861
862 /* Lower array derefs of vectors for SSBO and UBO loads. For both UBOs and
863 * SSBOs, our back-end is capable of loading an entire vec4 at a time and
864 * we would like to take advantage of that whenever possible regardless of
865 * whether or not the app gives us full loads. This should allow the
866 * optimizer to combine UBO and SSBO load operations and save us some send
867 * messages.
868 */
869 OPT(nir_lower_array_deref_of_vec,
870 nir_var_mem_ubo | nir_var_mem_ssbo,
871 nir_lower_direct_array_deref_of_vec_load);
872
873 /* Get rid of split copies */
874 brw_nir_optimize(nir, compiler, is_scalar, false);
875 }
876
877 void
brw_nir_link_shaders(const struct brw_compiler * compiler,nir_shader * producer,nir_shader * consumer)878 brw_nir_link_shaders(const struct brw_compiler *compiler,
879 nir_shader *producer, nir_shader *consumer)
880 {
881 nir_lower_io_arrays_to_elements(producer, consumer);
882 nir_validate_shader(producer, "after nir_lower_io_arrays_to_elements");
883 nir_validate_shader(consumer, "after nir_lower_io_arrays_to_elements");
884
885 const bool p_is_scalar = compiler->scalar_stage[producer->info.stage];
886 const bool c_is_scalar = compiler->scalar_stage[consumer->info.stage];
887
888 if (p_is_scalar && c_is_scalar) {
889 NIR_PASS_V(producer, nir_lower_io_to_scalar_early, nir_var_shader_out);
890 NIR_PASS_V(consumer, nir_lower_io_to_scalar_early, nir_var_shader_in);
891 brw_nir_optimize(producer, compiler, p_is_scalar, false);
892 brw_nir_optimize(consumer, compiler, c_is_scalar, false);
893 }
894
895 if (nir_link_opt_varyings(producer, consumer))
896 brw_nir_optimize(consumer, compiler, c_is_scalar, false);
897
898 NIR_PASS_V(producer, nir_remove_dead_variables, nir_var_shader_out, NULL);
899 NIR_PASS_V(consumer, nir_remove_dead_variables, nir_var_shader_in, NULL);
900
901 if (nir_remove_unused_varyings(producer, consumer)) {
902 NIR_PASS_V(producer, nir_lower_global_vars_to_local);
903 NIR_PASS_V(consumer, nir_lower_global_vars_to_local);
904
905 /* The backend might not be able to handle indirects on
906 * temporaries so we need to lower indirects on any of the
907 * varyings we have demoted here.
908 */
909 NIR_PASS_V(producer, nir_lower_indirect_derefs,
910 brw_nir_no_indirect_mask(compiler, producer->info.stage),
911 UINT32_MAX);
912 NIR_PASS_V(consumer, nir_lower_indirect_derefs,
913 brw_nir_no_indirect_mask(compiler, consumer->info.stage),
914 UINT32_MAX);
915
916 brw_nir_optimize(producer, compiler, p_is_scalar, false);
917 brw_nir_optimize(consumer, compiler, c_is_scalar, false);
918 }
919
920 NIR_PASS_V(producer, nir_lower_io_to_vector, nir_var_shader_out);
921 NIR_PASS_V(producer, nir_opt_combine_stores, nir_var_shader_out);
922 NIR_PASS_V(consumer, nir_lower_io_to_vector, nir_var_shader_in);
923
924 if (producer->info.stage != MESA_SHADER_TESS_CTRL) {
925 /* Calling lower_io_to_vector creates output variable writes with
926 * write-masks. On non-TCS outputs, the back-end can't handle it and we
927 * need to call nir_lower_io_to_temporaries to get rid of them. This,
928 * in turn, creates temporary variables and extra copy_deref intrinsics
929 * that we need to clean up.
930 */
931 NIR_PASS_V(producer, nir_lower_io_to_temporaries,
932 nir_shader_get_entrypoint(producer), true, false);
933 NIR_PASS_V(producer, nir_lower_global_vars_to_local);
934 NIR_PASS_V(producer, nir_split_var_copies);
935 NIR_PASS_V(producer, nir_lower_var_copies);
936 }
937 }
938
939 static bool
brw_nir_should_vectorize_mem(unsigned align_mul,unsigned align_offset,unsigned bit_size,unsigned num_components,nir_intrinsic_instr * low,nir_intrinsic_instr * high,void * data)940 brw_nir_should_vectorize_mem(unsigned align_mul, unsigned align_offset,
941 unsigned bit_size,
942 unsigned num_components,
943 nir_intrinsic_instr *low,
944 nir_intrinsic_instr *high,
945 void *data)
946 {
947 /* Don't combine things to generate 64-bit loads/stores. We have to split
948 * those back into 32-bit ones anyway and UBO loads aren't split in NIR so
949 * we don't want to make a mess for the back-end.
950 */
951 if (bit_size > 32)
952 return false;
953
954 /* We can handle at most a vec4 right now. Anything bigger would get
955 * immediately split by brw_nir_lower_mem_access_bit_sizes anyway.
956 */
957 if (num_components > 4)
958 return false;
959
960
961 uint32_t align;
962 if (align_offset)
963 align = 1 << (ffs(align_offset) - 1);
964 else
965 align = align_mul;
966
967 if (align < bit_size / 8)
968 return false;
969
970 return true;
971 }
972
973 static
combine_all_barriers(nir_intrinsic_instr * a,nir_intrinsic_instr * b,void * data)974 bool combine_all_barriers(nir_intrinsic_instr *a,
975 nir_intrinsic_instr *b,
976 void *data)
977 {
978 /* Translation to backend IR will get rid of modes we don't care about, so
979 * no harm in always combining them.
980 *
981 * TODO: While HW has only ACQUIRE|RELEASE fences, we could improve the
982 * scheduling so that it can take advantage of the different semantics.
983 */
984 nir_intrinsic_set_memory_modes(a, nir_intrinsic_memory_modes(a) |
985 nir_intrinsic_memory_modes(b));
986 nir_intrinsic_set_memory_semantics(a, nir_intrinsic_memory_semantics(a) |
987 nir_intrinsic_memory_semantics(b));
988 nir_intrinsic_set_memory_scope(a, MAX2(nir_intrinsic_memory_scope(a),
989 nir_intrinsic_memory_scope(b)));
990 return true;
991 }
992
993 static void
brw_vectorize_lower_mem_access(nir_shader * nir,const struct brw_compiler * compiler,bool is_scalar,bool robust_buffer_access)994 brw_vectorize_lower_mem_access(nir_shader *nir,
995 const struct brw_compiler *compiler,
996 bool is_scalar,
997 bool robust_buffer_access)
998 {
999 const struct intel_device_info *devinfo = compiler->devinfo;
1000 bool progress = false;
1001
1002 if (is_scalar) {
1003 nir_load_store_vectorize_options options = {
1004 .modes = nir_var_mem_ubo | nir_var_mem_ssbo |
1005 nir_var_mem_global | nir_var_mem_shared,
1006 .callback = brw_nir_should_vectorize_mem,
1007 .robust_modes = (nir_variable_mode)0,
1008 };
1009
1010 if (robust_buffer_access) {
1011 options.robust_modes = nir_var_mem_ubo | nir_var_mem_ssbo |
1012 nir_var_mem_global;
1013 }
1014
1015 OPT(nir_opt_load_store_vectorize, &options);
1016 }
1017
1018 OPT(brw_nir_lower_mem_access_bit_sizes, devinfo);
1019
1020 while (progress) {
1021 progress = false;
1022
1023 OPT(nir_lower_pack);
1024 OPT(nir_copy_prop);
1025 OPT(nir_opt_dce);
1026 OPT(nir_opt_cse);
1027 OPT(nir_opt_algebraic);
1028 OPT(nir_opt_constant_folding);
1029 }
1030 }
1031
1032 static bool
nir_shader_has_local_variables(const nir_shader * nir)1033 nir_shader_has_local_variables(const nir_shader *nir)
1034 {
1035 nir_foreach_function(func, nir) {
1036 if (func->impl && !exec_list_is_empty(&func->impl->locals))
1037 return true;
1038 }
1039
1040 return false;
1041 }
1042
1043 /* Prepare the given shader for codegen
1044 *
1045 * This function is intended to be called right before going into the actual
1046 * backend and is highly backend-specific. Also, once this function has been
1047 * called on a shader, it will no longer be in SSA form so most optimizations
1048 * will not work.
1049 */
1050 void
brw_postprocess_nir(nir_shader * nir,const struct brw_compiler * compiler,bool is_scalar,bool debug_enabled,bool robust_buffer_access)1051 brw_postprocess_nir(nir_shader *nir, const struct brw_compiler *compiler,
1052 bool is_scalar, bool debug_enabled,
1053 bool robust_buffer_access)
1054 {
1055 const struct intel_device_info *devinfo = compiler->devinfo;
1056
1057 UNUSED bool progress; /* Written by OPT */
1058
1059 OPT(nir_lower_bit_size, lower_bit_size_callback, (void *)compiler);
1060
1061 OPT(brw_nir_lower_scoped_barriers);
1062 OPT(nir_opt_combine_memory_barriers, combine_all_barriers, NULL);
1063
1064 do {
1065 progress = false;
1066 OPT(nir_opt_algebraic_before_ffma);
1067 } while (progress);
1068
1069 if (devinfo->verx10 >= 125) {
1070 const nir_lower_idiv_options options = {
1071 .imprecise_32bit_lowering = false,
1072 .allow_fp16 = false
1073 };
1074 OPT(nir_lower_idiv, &options);
1075 }
1076
1077 brw_nir_optimize(nir, compiler, is_scalar, false);
1078
1079 if (is_scalar && nir_shader_has_local_variables(nir)) {
1080 OPT(nir_lower_vars_to_explicit_types, nir_var_function_temp,
1081 glsl_get_natural_size_align_bytes);
1082 OPT(nir_lower_explicit_io, nir_var_function_temp,
1083 nir_address_format_32bit_offset);
1084 brw_nir_optimize(nir, compiler, is_scalar, false);
1085 }
1086
1087 brw_vectorize_lower_mem_access(nir, compiler, is_scalar,
1088 robust_buffer_access);
1089
1090 if (OPT(nir_lower_int64))
1091 brw_nir_optimize(nir, compiler, is_scalar, false);
1092
1093 if (devinfo->ver >= 6) {
1094 /* Try and fuse multiply-adds */
1095 OPT(brw_nir_opt_peephole_ffma);
1096 }
1097
1098 if (OPT(nir_opt_comparison_pre)) {
1099 OPT(nir_copy_prop);
1100 OPT(nir_opt_dce);
1101 OPT(nir_opt_cse);
1102
1103 /* Do the select peepehole again. nir_opt_comparison_pre (combined with
1104 * the other optimization passes) will have removed at least one
1105 * instruction from one of the branches of the if-statement, so now it
1106 * might be under the threshold of conversion to bcsel.
1107 *
1108 * See brw_nir_optimize for the explanation of is_vec4_tessellation.
1109 */
1110 const bool is_vec4_tessellation = !is_scalar &&
1111 (nir->info.stage == MESA_SHADER_TESS_CTRL ||
1112 nir->info.stage == MESA_SHADER_TESS_EVAL);
1113 OPT(nir_opt_peephole_select, 0, is_vec4_tessellation, false);
1114 OPT(nir_opt_peephole_select, 1, is_vec4_tessellation,
1115 compiler->devinfo->ver >= 6);
1116 }
1117
1118 do {
1119 progress = false;
1120 if (OPT(nir_opt_algebraic_late)) {
1121 /* At this late stage, anything that makes more constants will wreak
1122 * havok on the vec4 backend. The handling of constants in the vec4
1123 * backend is not good.
1124 */
1125 if (is_scalar)
1126 OPT(nir_opt_constant_folding);
1127
1128 OPT(nir_copy_prop);
1129 OPT(nir_opt_dce);
1130 OPT(nir_opt_cse);
1131 }
1132 } while (progress);
1133
1134
1135 OPT(brw_nir_lower_conversions);
1136
1137 if (is_scalar)
1138 OPT(nir_lower_alu_to_scalar, NULL, NULL);
1139
1140 while (OPT(nir_opt_algebraic_distribute_src_mods)) {
1141 OPT(nir_copy_prop);
1142 OPT(nir_opt_dce);
1143 OPT(nir_opt_cse);
1144 }
1145
1146 OPT(nir_copy_prop);
1147 OPT(nir_opt_dce);
1148 OPT(nir_opt_move, nir_move_comparisons);
1149 OPT(nir_opt_dead_cf);
1150
1151 OPT(nir_lower_bool_to_int32);
1152 OPT(nir_copy_prop);
1153 OPT(nir_opt_dce);
1154
1155 OPT(nir_lower_locals_to_regs);
1156
1157 if (unlikely(debug_enabled)) {
1158 /* Re-index SSA defs so we print more sensible numbers. */
1159 nir_foreach_function(function, nir) {
1160 if (function->impl)
1161 nir_index_ssa_defs(function->impl);
1162 }
1163
1164 fprintf(stderr, "NIR (SSA form) for %s shader:\n",
1165 _mesa_shader_stage_to_string(nir->info.stage));
1166 nir_print_shader(nir, stderr);
1167 }
1168
1169 nir_validate_ssa_dominance(nir, "before nir_convert_from_ssa");
1170
1171 OPT(nir_convert_from_ssa, true);
1172
1173 if (!is_scalar) {
1174 OPT(nir_move_vec_src_uses_to_dest);
1175 OPT(nir_lower_vec_to_movs, NULL, NULL);
1176 }
1177
1178 OPT(nir_opt_dce);
1179
1180 if (OPT(nir_opt_rematerialize_compares))
1181 OPT(nir_opt_dce);
1182
1183 /* This is the last pass we run before we start emitting stuff. It
1184 * determines when we need to insert boolean resolves on Gen <= 5. We
1185 * run it last because it stashes data in instr->pass_flags and we don't
1186 * want that to be squashed by other NIR passes.
1187 */
1188 if (devinfo->ver <= 5)
1189 brw_nir_analyze_boolean_resolves(nir);
1190
1191 nir_sweep(nir);
1192
1193 if (unlikely(debug_enabled)) {
1194 fprintf(stderr, "NIR (final form) for %s shader:\n",
1195 _mesa_shader_stage_to_string(nir->info.stage));
1196 nir_print_shader(nir, stderr);
1197 }
1198 }
1199
1200 static bool
brw_nir_apply_sampler_key(nir_shader * nir,const struct brw_compiler * compiler,const struct brw_sampler_prog_key_data * key_tex)1201 brw_nir_apply_sampler_key(nir_shader *nir,
1202 const struct brw_compiler *compiler,
1203 const struct brw_sampler_prog_key_data *key_tex)
1204 {
1205 const struct intel_device_info *devinfo = compiler->devinfo;
1206 nir_lower_tex_options tex_options = {
1207 .lower_txd_clamp_bindless_sampler = true,
1208 .lower_txd_clamp_if_sampler_index_not_lt_16 = true,
1209 };
1210
1211 /* Iron Lake and prior require lowering of all rectangle textures */
1212 if (devinfo->ver < 6)
1213 tex_options.lower_rect = true;
1214
1215 /* Prior to Broadwell, our hardware can't actually do GL_CLAMP */
1216 if (devinfo->ver < 8) {
1217 tex_options.saturate_s = key_tex->gl_clamp_mask[0];
1218 tex_options.saturate_t = key_tex->gl_clamp_mask[1];
1219 tex_options.saturate_r = key_tex->gl_clamp_mask[2];
1220 }
1221
1222 /* Prior to Haswell, we have to fake texture swizzle */
1223 for (unsigned s = 0; s < MAX_SAMPLERS; s++) {
1224 if (key_tex->swizzles[s] == SWIZZLE_NOOP)
1225 continue;
1226
1227 tex_options.swizzle_result |= BITFIELD_BIT(s);
1228 for (unsigned c = 0; c < 4; c++)
1229 tex_options.swizzles[s][c] = GET_SWZ(key_tex->swizzles[s], c);
1230 }
1231
1232 /* Prior to Haswell, we have to lower gradients on shadow samplers */
1233 tex_options.lower_txd_shadow = devinfo->verx10 <= 70;
1234
1235 tex_options.lower_y_uv_external = key_tex->y_uv_image_mask;
1236 tex_options.lower_y_u_v_external = key_tex->y_u_v_image_mask;
1237 tex_options.lower_yx_xuxv_external = key_tex->yx_xuxv_image_mask;
1238 tex_options.lower_xy_uxvx_external = key_tex->xy_uxvx_image_mask;
1239 tex_options.lower_ayuv_external = key_tex->ayuv_image_mask;
1240 tex_options.lower_xyuv_external = key_tex->xyuv_image_mask;
1241 tex_options.bt709_external = key_tex->bt709_mask;
1242 tex_options.bt2020_external = key_tex->bt2020_mask;
1243
1244 /* Setup array of scaling factors for each texture. */
1245 memcpy(&tex_options.scale_factors, &key_tex->scale_factors,
1246 sizeof(tex_options.scale_factors));
1247
1248 return nir_lower_tex(nir, &tex_options);
1249 }
1250
1251 static unsigned
get_subgroup_size(gl_shader_stage stage,const struct brw_base_prog_key * key,unsigned max_subgroup_size)1252 get_subgroup_size(gl_shader_stage stage,
1253 const struct brw_base_prog_key *key,
1254 unsigned max_subgroup_size)
1255 {
1256 switch (key->subgroup_size_type) {
1257 case BRW_SUBGROUP_SIZE_API_CONSTANT:
1258 /* We have to use the global constant size. */
1259 return BRW_SUBGROUP_SIZE;
1260
1261 case BRW_SUBGROUP_SIZE_UNIFORM:
1262 /* It has to be uniform across all invocations but can vary per stage
1263 * if we want. This gives us a bit more freedom.
1264 *
1265 * For compute, brw_nir_apply_key is called per-dispatch-width so this
1266 * is the actual subgroup size and not a maximum. However, we only
1267 * invoke one size of any given compute shader so it's still guaranteed
1268 * to be uniform across invocations.
1269 */
1270 return max_subgroup_size;
1271
1272 case BRW_SUBGROUP_SIZE_VARYING:
1273 /* The subgroup size is allowed to be fully varying. For geometry
1274 * stages, we know it's always 8 which is max_subgroup_size so we can
1275 * return that. For compute, brw_nir_apply_key is called once per
1276 * dispatch-width so max_subgroup_size is the real subgroup size.
1277 *
1278 * For fragment, we return 0 and let it fall through to the back-end
1279 * compiler. This means we can't optimize based on subgroup size but
1280 * that's a risk the client took when it asked for a varying subgroup
1281 * size.
1282 */
1283 return stage == MESA_SHADER_FRAGMENT ? 0 : max_subgroup_size;
1284
1285 case BRW_SUBGROUP_SIZE_REQUIRE_8:
1286 case BRW_SUBGROUP_SIZE_REQUIRE_16:
1287 case BRW_SUBGROUP_SIZE_REQUIRE_32:
1288 assert(stage == MESA_SHADER_COMPUTE);
1289 /* These enum values are expressly chosen to be equal to the subgroup
1290 * size that they require.
1291 */
1292 return key->subgroup_size_type;
1293 }
1294
1295 unreachable("Invalid subgroup size type");
1296 }
1297
1298 void
brw_nir_apply_key(nir_shader * nir,const struct brw_compiler * compiler,const struct brw_base_prog_key * key,unsigned max_subgroup_size,bool is_scalar)1299 brw_nir_apply_key(nir_shader *nir,
1300 const struct brw_compiler *compiler,
1301 const struct brw_base_prog_key *key,
1302 unsigned max_subgroup_size,
1303 bool is_scalar)
1304 {
1305 bool progress = false;
1306
1307 OPT(brw_nir_apply_sampler_key, compiler, &key->tex);
1308
1309 const nir_lower_subgroups_options subgroups_options = {
1310 .subgroup_size = get_subgroup_size(nir->info.stage, key,
1311 max_subgroup_size),
1312 .ballot_bit_size = 32,
1313 .ballot_components = 1,
1314 .lower_subgroup_masks = true,
1315 };
1316 OPT(nir_lower_subgroups, &subgroups_options);
1317
1318 if (progress)
1319 brw_nir_optimize(nir, compiler, is_scalar, false);
1320 }
1321
1322 enum brw_conditional_mod
brw_cmod_for_nir_comparison(nir_op op)1323 brw_cmod_for_nir_comparison(nir_op op)
1324 {
1325 switch (op) {
1326 case nir_op_flt:
1327 case nir_op_flt32:
1328 case nir_op_ilt:
1329 case nir_op_ilt32:
1330 case nir_op_ult:
1331 case nir_op_ult32:
1332 return BRW_CONDITIONAL_L;
1333
1334 case nir_op_fge:
1335 case nir_op_fge32:
1336 case nir_op_ige:
1337 case nir_op_ige32:
1338 case nir_op_uge:
1339 case nir_op_uge32:
1340 return BRW_CONDITIONAL_GE;
1341
1342 case nir_op_feq:
1343 case nir_op_feq32:
1344 case nir_op_ieq:
1345 case nir_op_ieq32:
1346 case nir_op_b32all_fequal2:
1347 case nir_op_b32all_iequal2:
1348 case nir_op_b32all_fequal3:
1349 case nir_op_b32all_iequal3:
1350 case nir_op_b32all_fequal4:
1351 case nir_op_b32all_iequal4:
1352 return BRW_CONDITIONAL_Z;
1353
1354 case nir_op_fneu:
1355 case nir_op_fneu32:
1356 case nir_op_ine:
1357 case nir_op_ine32:
1358 case nir_op_b32any_fnequal2:
1359 case nir_op_b32any_inequal2:
1360 case nir_op_b32any_fnequal3:
1361 case nir_op_b32any_inequal3:
1362 case nir_op_b32any_fnequal4:
1363 case nir_op_b32any_inequal4:
1364 return BRW_CONDITIONAL_NZ;
1365
1366 default:
1367 unreachable("Unsupported NIR comparison op");
1368 }
1369 }
1370
1371 uint32_t
brw_aop_for_nir_intrinsic(const nir_intrinsic_instr * atomic)1372 brw_aop_for_nir_intrinsic(const nir_intrinsic_instr *atomic)
1373 {
1374 switch (atomic->intrinsic) {
1375 #define AOP_CASE(atom) \
1376 case nir_intrinsic_image_atomic_##atom: \
1377 case nir_intrinsic_bindless_image_atomic_##atom: \
1378 case nir_intrinsic_ssbo_atomic_##atom: \
1379 case nir_intrinsic_shared_atomic_##atom: \
1380 case nir_intrinsic_global_atomic_##atom
1381
1382 AOP_CASE(add): {
1383 unsigned src_idx;
1384 switch (atomic->intrinsic) {
1385 case nir_intrinsic_image_atomic_add:
1386 case nir_intrinsic_bindless_image_atomic_add:
1387 src_idx = 3;
1388 break;
1389 case nir_intrinsic_ssbo_atomic_add:
1390 src_idx = 2;
1391 break;
1392 case nir_intrinsic_shared_atomic_add:
1393 case nir_intrinsic_global_atomic_add:
1394 src_idx = 1;
1395 break;
1396 default:
1397 unreachable("Invalid add atomic opcode");
1398 }
1399
1400 if (nir_src_is_const(atomic->src[src_idx])) {
1401 int64_t add_val = nir_src_as_int(atomic->src[src_idx]);
1402 if (add_val == 1)
1403 return BRW_AOP_INC;
1404 else if (add_val == -1)
1405 return BRW_AOP_DEC;
1406 }
1407 return BRW_AOP_ADD;
1408 }
1409
1410 AOP_CASE(imin): return BRW_AOP_IMIN;
1411 AOP_CASE(umin): return BRW_AOP_UMIN;
1412 AOP_CASE(imax): return BRW_AOP_IMAX;
1413 AOP_CASE(umax): return BRW_AOP_UMAX;
1414 AOP_CASE(and): return BRW_AOP_AND;
1415 AOP_CASE(or): return BRW_AOP_OR;
1416 AOP_CASE(xor): return BRW_AOP_XOR;
1417 AOP_CASE(exchange): return BRW_AOP_MOV;
1418 AOP_CASE(comp_swap): return BRW_AOP_CMPWR;
1419
1420 #undef AOP_CASE
1421 #define AOP_CASE(atom) \
1422 case nir_intrinsic_ssbo_atomic_##atom: \
1423 case nir_intrinsic_shared_atomic_##atom: \
1424 case nir_intrinsic_global_atomic_##atom
1425
1426 AOP_CASE(fmin): return BRW_AOP_FMIN;
1427 AOP_CASE(fmax): return BRW_AOP_FMAX;
1428 AOP_CASE(fcomp_swap): return BRW_AOP_FCMPWR;
1429 AOP_CASE(fadd): return BRW_AOP_FADD;
1430
1431 #undef AOP_CASE
1432
1433 default:
1434 unreachable("Unsupported NIR atomic intrinsic");
1435 }
1436 }
1437
1438 enum brw_reg_type
brw_type_for_nir_type(const struct intel_device_info * devinfo,nir_alu_type type)1439 brw_type_for_nir_type(const struct intel_device_info *devinfo,
1440 nir_alu_type type)
1441 {
1442 switch (type) {
1443 case nir_type_uint:
1444 case nir_type_uint32:
1445 return BRW_REGISTER_TYPE_UD;
1446 case nir_type_bool:
1447 case nir_type_int:
1448 case nir_type_bool32:
1449 case nir_type_int32:
1450 return BRW_REGISTER_TYPE_D;
1451 case nir_type_float:
1452 case nir_type_float32:
1453 return BRW_REGISTER_TYPE_F;
1454 case nir_type_float16:
1455 return BRW_REGISTER_TYPE_HF;
1456 case nir_type_float64:
1457 return BRW_REGISTER_TYPE_DF;
1458 case nir_type_int64:
1459 return devinfo->ver < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_Q;
1460 case nir_type_uint64:
1461 return devinfo->ver < 8 ? BRW_REGISTER_TYPE_DF : BRW_REGISTER_TYPE_UQ;
1462 case nir_type_int16:
1463 return BRW_REGISTER_TYPE_W;
1464 case nir_type_uint16:
1465 return BRW_REGISTER_TYPE_UW;
1466 case nir_type_int8:
1467 return BRW_REGISTER_TYPE_B;
1468 case nir_type_uint8:
1469 return BRW_REGISTER_TYPE_UB;
1470 default:
1471 unreachable("unknown type");
1472 }
1473
1474 return BRW_REGISTER_TYPE_F;
1475 }
1476
1477 nir_shader *
brw_nir_create_passthrough_tcs(void * mem_ctx,const struct brw_compiler * compiler,const nir_shader_compiler_options * options,const struct brw_tcs_prog_key * key)1478 brw_nir_create_passthrough_tcs(void *mem_ctx, const struct brw_compiler *compiler,
1479 const nir_shader_compiler_options *options,
1480 const struct brw_tcs_prog_key *key)
1481 {
1482 nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_TESS_CTRL,
1483 options, "passthrough TCS");
1484 ralloc_steal(mem_ctx, b.shader);
1485 nir_shader *nir = b.shader;
1486 nir_variable *var;
1487 nir_ssa_def *load;
1488 nir_ssa_def *zero = nir_imm_int(&b, 0);
1489 nir_ssa_def *invoc_id = nir_load_invocation_id(&b);
1490
1491 nir->info.inputs_read = key->outputs_written &
1492 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
1493 nir->info.outputs_written = key->outputs_written;
1494 nir->info.tess.tcs_vertices_out = key->input_vertices;
1495 nir->num_uniforms = 8 * sizeof(uint32_t);
1496
1497 var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_0");
1498 var->data.location = 0;
1499 var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_1");
1500 var->data.location = 1;
1501
1502 /* Write the patch URB header. */
1503 for (int i = 0; i <= 1; i++) {
1504 load = nir_load_uniform(&b, 4, 32, zero, .base = i * 4 * sizeof(uint32_t));
1505
1506 nir_store_output(&b, load, zero,
1507 .base = VARYING_SLOT_TESS_LEVEL_INNER - i,
1508 .write_mask = WRITEMASK_XYZW);
1509 }
1510
1511 /* Copy inputs to outputs. */
1512 uint64_t varyings = nir->info.inputs_read;
1513
1514 while (varyings != 0) {
1515 const int varying = ffsll(varyings) - 1;
1516
1517 load = nir_load_per_vertex_input(&b, 4, 32, invoc_id, zero, .base = varying);
1518
1519 nir_store_per_vertex_output(&b, load, invoc_id, zero,
1520 .base = varying,
1521 .write_mask = WRITEMASK_XYZW);
1522
1523 varyings &= ~BITFIELD64_BIT(varying);
1524 }
1525
1526 nir_validate_shader(nir, "in brw_nir_create_passthrough_tcs");
1527
1528 brw_preprocess_nir(compiler, nir, NULL);
1529
1530 return nir;
1531 }
1532