90c4f6687673e214d5310be1cb9050bc8831a2fe
[mesa.git] / src / mesa / drivers / dri / i965 / 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 "compiler/nir/glsl_to_nir.h"
27 #include "compiler/nir/nir_builder.h"
28 #include "program/prog_to_nir.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 }
36
37 static bool
38 is_output(nir_intrinsic_instr *intrin)
39 {
40 return intrin->intrinsic == nir_intrinsic_load_output ||
41 intrin->intrinsic == nir_intrinsic_load_per_vertex_output ||
42 intrin->intrinsic == nir_intrinsic_store_output ||
43 intrin->intrinsic == nir_intrinsic_store_per_vertex_output;
44 }
45
46 /**
47 * In many cases, we just add the base and offset together, so there's no
48 * reason to keep them separate. Sometimes, combining them is essential:
49 * if a shader only accesses part of a compound variable (such as a matrix
50 * or array), the variable's base may not actually exist in the VUE map.
51 *
52 * This pass adds constant offsets to instr->const_index[0], and resets
53 * the offset source to 0. Non-constant offsets remain unchanged - since
54 * we don't know what part of a compound variable is accessed, we allocate
55 * storage for the entire thing.
56 */
57 struct add_const_offset_to_base_params {
58 nir_builder b;
59 nir_variable_mode mode;
60 };
61
62 static bool
63 add_const_offset_to_base_block(nir_block *block, void *closure)
64 {
65 struct add_const_offset_to_base_params *params = closure;
66 nir_builder *b = &params->b;
67
68 nir_foreach_instr_safe(block, instr) {
69 if (instr->type != nir_instr_type_intrinsic)
70 continue;
71
72 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
73
74 if ((params->mode == nir_var_shader_in && is_input(intrin)) ||
75 (params->mode == nir_var_shader_out && is_output(intrin))) {
76 nir_src *offset = nir_get_io_offset_src(intrin);
77 nir_const_value *const_offset = nir_src_as_const_value(*offset);
78
79 if (const_offset) {
80 intrin->const_index[0] += const_offset->u[0];
81 b->cursor = nir_before_instr(&intrin->instr);
82 nir_instr_rewrite_src(&intrin->instr, offset,
83 nir_src_for_ssa(nir_imm_int(b, 0)));
84 }
85 }
86 }
87 return true;
88 }
89
90 static void
91 add_const_offset_to_base(nir_shader *nir, nir_variable_mode mode)
92 {
93 struct add_const_offset_to_base_params params = { .mode = mode };
94
95 nir_foreach_function(nir, f) {
96 if (f->impl) {
97 nir_builder_init(&params.b, f->impl);
98 nir_foreach_block(f->impl, add_const_offset_to_base_block, &params);
99 }
100 }
101 }
102
103 static bool
104 remap_vs_attrs(nir_block *block, void *closure)
105 {
106 GLbitfield64 inputs_read = *((GLbitfield64 *) closure);
107
108 nir_foreach_instr(block, instr) {
109 if (instr->type != nir_instr_type_intrinsic)
110 continue;
111
112 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
113
114 if (intrin->intrinsic == nir_intrinsic_load_input) {
115 /* Attributes come in a contiguous block, ordered by their
116 * gl_vert_attrib value. That means we can compute the slot
117 * number for an attribute by masking out the enabled attributes
118 * before it and counting the bits.
119 */
120 int attr = intrin->const_index[0];
121 int slot = _mesa_bitcount_64(inputs_read & BITFIELD64_MASK(attr));
122
123 intrin->const_index[0] = 4 * slot;
124 }
125 }
126 return true;
127 }
128
129 static bool
130 remap_inputs_with_vue_map(nir_block *block, void *closure)
131 {
132 const struct brw_vue_map *vue_map = closure;
133
134 nir_foreach_instr(block, instr) {
135 if (instr->type != nir_instr_type_intrinsic)
136 continue;
137
138 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
139
140 if (intrin->intrinsic == nir_intrinsic_load_input ||
141 intrin->intrinsic == nir_intrinsic_load_per_vertex_input) {
142 int vue_slot = vue_map->varying_to_slot[intrin->const_index[0]];
143 assert(vue_slot != -1);
144 intrin->const_index[0] = vue_slot;
145 }
146 }
147 return true;
148 }
149
150 struct remap_patch_urb_offsets_state {
151 nir_builder b;
152 const struct brw_vue_map *vue_map;
153 };
154
155 static bool
156 remap_patch_urb_offsets(nir_block *block, void *closure)
157 {
158 struct remap_patch_urb_offsets_state *state = closure;
159
160 nir_foreach_instr_safe(block, instr) {
161 if (instr->type != nir_instr_type_intrinsic)
162 continue;
163
164 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
165
166 gl_shader_stage stage = state->b.shader->stage;
167
168 if ((stage == MESA_SHADER_TESS_CTRL && is_output(intrin)) ||
169 (stage == MESA_SHADER_TESS_EVAL && is_input(intrin))) {
170 int vue_slot = state->vue_map->varying_to_slot[intrin->const_index[0]];
171 assert(vue_slot != -1);
172 intrin->const_index[0] = vue_slot;
173
174 nir_src *vertex = nir_get_io_vertex_index_src(intrin);
175 if (vertex) {
176 nir_const_value *const_vertex = nir_src_as_const_value(*vertex);
177 if (const_vertex) {
178 intrin->const_index[0] += const_vertex->u[0] *
179 state->vue_map->num_per_vertex_slots;
180 } else {
181 state->b.cursor = nir_before_instr(&intrin->instr);
182
183 /* Multiply by the number of per-vertex slots. */
184 nir_ssa_def *vertex_offset =
185 nir_imul(&state->b,
186 nir_ssa_for_src(&state->b, *vertex, 1),
187 nir_imm_int(&state->b,
188 state->vue_map->num_per_vertex_slots));
189
190 /* Add it to the existing offset */
191 nir_src *offset = nir_get_io_offset_src(intrin);
192 nir_ssa_def *total_offset =
193 nir_iadd(&state->b, vertex_offset,
194 nir_ssa_for_src(&state->b, *offset, 1));
195
196 nir_instr_rewrite_src(&intrin->instr, offset,
197 nir_src_for_ssa(total_offset));
198 }
199 }
200 }
201 }
202 return true;
203 }
204
205 void
206 brw_nir_lower_vs_inputs(nir_shader *nir,
207 const struct brw_device_info *devinfo,
208 bool is_scalar,
209 bool use_legacy_snorm_formula,
210 const uint8_t *vs_attrib_wa_flags)
211 {
212 /* Start with the location of the variable's base. */
213 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
214 var->data.driver_location = var->data.location;
215 }
216
217 /* Now use nir_lower_io to walk dereference chains. Attribute arrays
218 * are loaded as one vec4 per element (or matrix column), so we use
219 * type_size_vec4 here.
220 */
221 nir_lower_io(nir, nir_var_shader_in, type_size_vec4);
222
223 /* This pass needs actual constants */
224 nir_opt_constant_folding(nir);
225
226 add_const_offset_to_base(nir, nir_var_shader_in);
227
228 brw_nir_apply_attribute_workarounds(nir, use_legacy_snorm_formula,
229 vs_attrib_wa_flags);
230
231 if (is_scalar) {
232 /* Finally, translate VERT_ATTRIB_* values into the actual registers.
233 *
234 * Note that we can use nir->info.inputs_read instead of
235 * key->inputs_read since the two are identical aside from Gen4-5
236 * edge flag differences.
237 */
238 GLbitfield64 inputs_read = nir->info.inputs_read;
239
240 nir_foreach_function(nir, function) {
241 if (function->impl) {
242 nir_foreach_block(function->impl, remap_vs_attrs, &inputs_read);
243 }
244 }
245 }
246 }
247
248 void
249 brw_nir_lower_vue_inputs(nir_shader *nir,
250 const struct brw_device_info *devinfo,
251 bool is_scalar)
252 {
253 if (!is_scalar && nir->stage == MESA_SHADER_GEOMETRY) {
254 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
255 var->data.driver_location = var->data.location;
256 }
257 nir_lower_io(nir, nir_var_shader_in, type_size_vec4);
258 } else {
259 /* The GLSL linker will have already matched up GS inputs and
260 * the outputs of prior stages. The driver does extend VS outputs
261 * in some cases, but only for legacy OpenGL or Gen4-5 hardware,
262 * neither of which offer geometry shader support. So we can
263 * safely ignore that.
264 *
265 * For SSO pipelines, we use a fixed VUE map layout based on variable
266 * locations, so we can rely on rendezvous-by-location to make this
267 * work.
268 *
269 * However, we need to ignore VARYING_SLOT_PRIMITIVE_ID, as it's not
270 * written by previous stages and shows up via payload magic.
271 */
272 struct brw_vue_map input_vue_map;
273 GLbitfield64 inputs_read =
274 nir->info.inputs_read & ~VARYING_BIT_PRIMITIVE_ID;
275 brw_compute_vue_map(devinfo, &input_vue_map, inputs_read,
276 nir->info.separate_shader ||
277 nir->stage == MESA_SHADER_TESS_CTRL);
278
279 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
280 var->data.driver_location = var->data.location;
281 }
282
283 /* Inputs are stored in vec4 slots, so use type_size_vec4(). */
284 nir_lower_io(nir, nir_var_shader_in, type_size_vec4);
285
286 /* This pass needs actual constants */
287 nir_opt_constant_folding(nir);
288
289 add_const_offset_to_base(nir, nir_var_shader_in);
290
291 nir_foreach_function(nir, function) {
292 if (function->impl) {
293 nir_foreach_block(function->impl, remap_inputs_with_vue_map,
294 &input_vue_map);
295 }
296 }
297 }
298 }
299
300 void
301 brw_nir_lower_tes_inputs(nir_shader *nir, const struct brw_vue_map *vue_map)
302 {
303 struct remap_patch_urb_offsets_state state;
304 state.vue_map = vue_map;
305
306 foreach_list_typed(nir_variable, var, node, &nir->inputs) {
307 var->data.driver_location = var->data.location;
308 }
309
310 nir_lower_io(nir, nir_var_shader_in, type_size_vec4);
311
312 /* This pass needs actual constants */
313 nir_opt_constant_folding(nir);
314
315 add_const_offset_to_base(nir, nir_var_shader_in);
316
317 nir_foreach_function(nir, function) {
318 if (function->impl) {
319 nir_builder_init(&state.b, function->impl);
320 nir_foreach_block(function->impl, remap_patch_urb_offsets, &state);
321 }
322 }
323 }
324
325 void
326 brw_nir_lower_fs_inputs(nir_shader *nir)
327 {
328 nir_assign_var_locations(&nir->inputs, &nir->num_inputs, type_size_scalar);
329 nir_lower_io(nir, nir_var_shader_in, type_size_scalar);
330 }
331
332 void
333 brw_nir_lower_vue_outputs(nir_shader *nir,
334 bool is_scalar)
335 {
336 if (is_scalar) {
337 nir_assign_var_locations(&nir->outputs, &nir->num_outputs,
338 type_size_vec4_times_4);
339 nir_lower_io(nir, nir_var_shader_out, type_size_vec4_times_4);
340 } else {
341 nir_foreach_variable(var, &nir->outputs)
342 var->data.driver_location = var->data.location;
343 nir_lower_io(nir, nir_var_shader_out, type_size_vec4);
344 }
345 }
346
347 void
348 brw_nir_lower_tcs_outputs(nir_shader *nir, const struct brw_vue_map *vue_map)
349 {
350 struct remap_patch_urb_offsets_state state;
351 state.vue_map = vue_map;
352
353 nir_foreach_variable(var, &nir->outputs) {
354 var->data.driver_location = var->data.location;
355 }
356
357 nir_lower_io(nir, nir_var_shader_out, type_size_vec4);
358
359 /* This pass needs actual constants */
360 nir_opt_constant_folding(nir);
361
362 add_const_offset_to_base(nir, nir_var_shader_out);
363
364 nir_foreach_function(nir, function) {
365 if (function->impl) {
366 nir_builder_init(&state.b, function->impl);
367 nir_foreach_block(function->impl, remap_patch_urb_offsets, &state);
368 }
369 }
370 }
371
372 void
373 brw_nir_lower_fs_outputs(nir_shader *nir)
374 {
375 nir_assign_var_locations(&nir->outputs, &nir->num_outputs,
376 type_size_scalar);
377 nir_lower_io(nir, nir_var_shader_out, type_size_scalar);
378 }
379
380 static int
381 type_size_scalar_bytes(const struct glsl_type *type)
382 {
383 return type_size_scalar(type) * 4;
384 }
385
386 static int
387 type_size_vec4_bytes(const struct glsl_type *type)
388 {
389 return type_size_vec4(type) * 16;
390 }
391
392 static void
393 brw_nir_lower_uniforms(nir_shader *nir, bool is_scalar)
394 {
395 if (is_scalar) {
396 nir_assign_var_locations(&nir->uniforms, &nir->num_uniforms,
397 type_size_scalar_bytes);
398 nir_lower_io(nir, nir_var_uniform, type_size_scalar_bytes);
399 } else {
400 nir_assign_var_locations(&nir->uniforms, &nir->num_uniforms,
401 type_size_vec4_bytes);
402 nir_lower_io(nir, nir_var_uniform, type_size_vec4_bytes);
403 }
404 }
405
406 #define OPT(pass, ...) ({ \
407 bool this_progress = false; \
408 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
409 if (this_progress) \
410 progress = true; \
411 this_progress; \
412 })
413
414 #define OPT_V(pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
415
416 static nir_shader *
417 nir_optimize(nir_shader *nir, bool is_scalar)
418 {
419 bool progress;
420 do {
421 progress = false;
422 OPT_V(nir_lower_vars_to_ssa);
423
424 if (is_scalar) {
425 OPT_V(nir_lower_alu_to_scalar);
426 }
427
428 OPT(nir_copy_prop);
429
430 if (is_scalar) {
431 OPT_V(nir_lower_phis_to_scalar);
432 }
433
434 OPT(nir_copy_prop);
435 OPT(nir_opt_dce);
436 OPT(nir_opt_cse);
437 OPT(nir_opt_peephole_select);
438 OPT(nir_opt_algebraic);
439 OPT(nir_opt_constant_folding);
440 OPT(nir_opt_dead_cf);
441 OPT(nir_opt_remove_phis);
442 OPT(nir_opt_undef);
443 } while (progress);
444
445 return nir;
446 }
447
448 /* Does some simple lowering and runs the standard suite of optimizations
449 *
450 * This is intended to be called more-or-less directly after you get the
451 * shader out of GLSL or some other source. While it is geared towards i965,
452 * it is not at all generator-specific except for the is_scalar flag. Even
453 * there, it is safe to call with is_scalar = false for a shader that is
454 * intended for the FS backend as long as nir_optimize is called again with
455 * is_scalar = true to scalarize everything prior to code gen.
456 */
457 nir_shader *
458 brw_preprocess_nir(nir_shader *nir, bool is_scalar)
459 {
460 bool progress; /* Written by OPT and OPT_V */
461 (void)progress;
462
463 if (nir->stage == MESA_SHADER_GEOMETRY)
464 OPT(nir_lower_gs_intrinsics);
465
466 static const nir_lower_tex_options tex_options = {
467 .lower_txp = ~0,
468 };
469
470 OPT(nir_lower_tex, &tex_options);
471 OPT(nir_normalize_cubemap_coords);
472
473 OPT(nir_lower_global_vars_to_local);
474
475 OPT(nir_split_var_copies);
476
477 nir = nir_optimize(nir, is_scalar);
478
479 if (is_scalar) {
480 OPT_V(nir_lower_load_const_to_scalar);
481 }
482
483 /* Lower a bunch of stuff */
484 OPT_V(nir_lower_var_copies);
485
486 /* Get rid of split copies */
487 nir = nir_optimize(nir, is_scalar);
488
489 OPT(nir_remove_dead_variables);
490
491 return nir;
492 }
493
494 /* Prepare the given shader for codegen
495 *
496 * This function is intended to be called right before going into the actual
497 * backend and is highly backend-specific. Also, once this function has been
498 * called on a shader, it will no longer be in SSA form so most optimizations
499 * will not work.
500 */
501 nir_shader *
502 brw_postprocess_nir(nir_shader *nir,
503 const struct brw_device_info *devinfo,
504 bool is_scalar)
505 {
506 bool debug_enabled =
507 (INTEL_DEBUG & intel_debug_flag_for_shader_stage(nir->stage));
508
509 bool progress; /* Written by OPT and OPT_V */
510 (void)progress;
511
512 nir = nir_optimize(nir, is_scalar);
513
514 if (devinfo->gen >= 6) {
515 /* Try and fuse multiply-adds */
516 OPT(brw_nir_opt_peephole_ffma);
517 }
518
519 OPT(nir_opt_algebraic_late);
520
521 OPT(nir_lower_locals_to_regs);
522
523 OPT_V(nir_lower_to_source_mods);
524 OPT(nir_copy_prop);
525 OPT(nir_opt_dce);
526
527 if (unlikely(debug_enabled)) {
528 /* Re-index SSA defs so we print more sensible numbers. */
529 nir_foreach_function(nir, function) {
530 if (function->impl)
531 nir_index_ssa_defs(function->impl);
532 }
533
534 fprintf(stderr, "NIR (SSA form) for %s shader:\n",
535 _mesa_shader_stage_to_string(nir->stage));
536 nir_print_shader(nir, stderr);
537 }
538
539 OPT_V(nir_convert_from_ssa, true);
540
541 if (!is_scalar) {
542 OPT_V(nir_move_vec_src_uses_to_dest);
543 OPT(nir_lower_vec_to_movs);
544 }
545
546 /* This is the last pass we run before we start emitting stuff. It
547 * determines when we need to insert boolean resolves on Gen <= 5. We
548 * run it last because it stashes data in instr->pass_flags and we don't
549 * want that to be squashed by other NIR passes.
550 */
551 if (devinfo->gen <= 5)
552 brw_nir_analyze_boolean_resolves(nir);
553
554 nir_sweep(nir);
555
556 if (unlikely(debug_enabled)) {
557 fprintf(stderr, "NIR (final form) for %s shader:\n",
558 _mesa_shader_stage_to_string(nir->stage));
559 nir_print_shader(nir, stderr);
560 }
561
562 return nir;
563 }
564
565 nir_shader *
566 brw_create_nir(struct brw_context *brw,
567 const struct gl_shader_program *shader_prog,
568 const struct gl_program *prog,
569 gl_shader_stage stage,
570 bool is_scalar)
571 {
572 struct gl_context *ctx = &brw->ctx;
573 const nir_shader_compiler_options *options =
574 ctx->Const.ShaderCompilerOptions[stage].NirOptions;
575 bool progress;
576 nir_shader *nir;
577
578 /* First, lower the GLSL IR or Mesa IR to NIR */
579 if (shader_prog) {
580 nir = glsl_to_nir(shader_prog, stage, options);
581 } else {
582 nir = prog_to_nir(prog, options);
583 OPT_V(nir_convert_to_ssa); /* turn registers into SSA */
584 }
585 nir_validate_shader(nir);
586
587 (void)progress;
588
589 nir = brw_preprocess_nir(nir, is_scalar);
590
591 OPT(nir_lower_system_values);
592 OPT_V(brw_nir_lower_uniforms, is_scalar);
593
594 if (shader_prog) {
595 OPT_V(nir_lower_samplers, shader_prog);
596 OPT_V(nir_lower_atomics, shader_prog);
597 }
598
599 return nir;
600 }
601
602 nir_shader *
603 brw_nir_apply_sampler_key(nir_shader *nir,
604 const struct brw_device_info *devinfo,
605 const struct brw_sampler_prog_key_data *key_tex,
606 bool is_scalar)
607 {
608 nir_lower_tex_options tex_options = { 0 };
609
610 /* Iron Lake and prior require lowering of all rectangle textures */
611 if (devinfo->gen < 6)
612 tex_options.lower_rect = true;
613
614 /* Prior to Broadwell, our hardware can't actually do GL_CLAMP */
615 if (devinfo->gen < 8) {
616 tex_options.saturate_s = key_tex->gl_clamp_mask[0];
617 tex_options.saturate_t = key_tex->gl_clamp_mask[1];
618 tex_options.saturate_r = key_tex->gl_clamp_mask[2];
619 }
620
621 /* Prior to Haswell, we have to fake texture swizzle */
622 for (unsigned s = 0; s < MAX_SAMPLERS; s++) {
623 if (key_tex->swizzles[s] == SWIZZLE_NOOP)
624 continue;
625
626 tex_options.swizzle_result |= (1 << s);
627 for (unsigned c = 0; c < 4; c++)
628 tex_options.swizzles[s][c] = GET_SWZ(key_tex->swizzles[s], c);
629 }
630
631 if (nir_lower_tex(nir, &tex_options)) {
632 nir_validate_shader(nir);
633 nir = nir_optimize(nir, is_scalar);
634 }
635
636 return nir;
637 }
638
639 enum brw_reg_type
640 brw_type_for_nir_type(nir_alu_type type)
641 {
642 switch (type) {
643 case nir_type_uint:
644 return BRW_REGISTER_TYPE_UD;
645 case nir_type_bool:
646 case nir_type_int:
647 return BRW_REGISTER_TYPE_D;
648 case nir_type_float:
649 return BRW_REGISTER_TYPE_F;
650 default:
651 unreachable("unknown type");
652 }
653
654 return BRW_REGISTER_TYPE_F;
655 }
656
657 /* Returns the glsl_base_type corresponding to a nir_alu_type.
658 * This is used by both brw_vec4_nir and brw_fs_nir.
659 */
660 enum glsl_base_type
661 brw_glsl_base_type_for_nir_type(nir_alu_type type)
662 {
663 switch (type) {
664 case nir_type_float:
665 return GLSL_TYPE_FLOAT;
666
667 case nir_type_int:
668 return GLSL_TYPE_INT;
669
670 case nir_type_uint:
671 return GLSL_TYPE_UINT;
672
673 default:
674 unreachable("bad type");
675 }
676 }