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