brw_nir.c revision ca941799ce76eac8afe2503fbacffee057e949d3
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 "glsl/glsl_parser_extras.h"
27#include "glsl/nir/glsl_to_nir.h"
28#include "program/prog_to_nir.h"
29
30static void
31brw_nir_lower_inputs(nir_shader *nir, bool is_scalar)
32{
33   nir_assign_var_locations(&nir->inputs, &nir->num_inputs,
34                            is_scalar ? type_size_scalar : type_size_vec4);
35}
36
37static void
38brw_nir_lower_outputs(nir_shader *nir, bool is_scalar)
39{
40   if (is_scalar) {
41      nir_assign_var_locations(&nir->outputs, &nir->num_outputs, type_size_scalar);
42   } else {
43      foreach_list_typed(nir_variable, var, node, &nir->outputs)
44         var->data.driver_location = var->data.location;
45   }
46}
47
48static void
49nir_optimize(nir_shader *nir, bool is_scalar)
50{
51   bool progress;
52   do {
53      progress = false;
54      nir_lower_vars_to_ssa(nir);
55      nir_validate_shader(nir);
56
57      if (is_scalar) {
58         nir_lower_alu_to_scalar(nir);
59         nir_validate_shader(nir);
60      }
61
62      progress |= nir_copy_prop(nir);
63      nir_validate_shader(nir);
64
65      if (is_scalar) {
66         nir_lower_phis_to_scalar(nir);
67         nir_validate_shader(nir);
68      }
69
70      progress |= nir_copy_prop(nir);
71      nir_validate_shader(nir);
72      progress |= nir_opt_dce(nir);
73      nir_validate_shader(nir);
74      progress |= nir_opt_cse(nir);
75      nir_validate_shader(nir);
76      progress |= nir_opt_peephole_select(nir);
77      nir_validate_shader(nir);
78      progress |= nir_opt_algebraic(nir);
79      nir_validate_shader(nir);
80      progress |= nir_opt_constant_folding(nir);
81      nir_validate_shader(nir);
82      progress |= nir_opt_dead_cf(nir);
83      nir_validate_shader(nir);
84      progress |= nir_opt_remove_phis(nir);
85      nir_validate_shader(nir);
86      progress |= nir_opt_undef(nir);
87      nir_validate_shader(nir);
88   } while (progress);
89}
90
91nir_shader *
92brw_create_nir(struct brw_context *brw,
93               const struct gl_shader_program *shader_prog,
94               const struct gl_program *prog,
95               gl_shader_stage stage,
96               bool is_scalar)
97{
98   struct gl_context *ctx = &brw->ctx;
99   const nir_shader_compiler_options *options =
100      ctx->Const.ShaderCompilerOptions[stage].NirOptions;
101   static const nir_lower_tex_options tex_options = {
102      .lower_txp = ~0,
103   };
104   bool debug_enabled = INTEL_DEBUG & intel_debug_flag_for_shader_stage(stage);
105   nir_shader *nir;
106
107   /* First, lower the GLSL IR or Mesa IR to NIR */
108   if (shader_prog) {
109      nir = glsl_to_nir(shader_prog, stage, options);
110   } else {
111      nir = prog_to_nir(prog, options);
112      nir_convert_to_ssa(nir); /* turn registers into SSA */
113   }
114   nir_validate_shader(nir);
115
116   if (stage == MESA_SHADER_GEOMETRY) {
117      nir_lower_gs_intrinsics(nir);
118      nir_validate_shader(nir);
119   }
120
121   nir_lower_global_vars_to_local(nir);
122   nir_validate_shader(nir);
123
124   nir_lower_tex(nir, &tex_options);
125   nir_validate_shader(nir);
126
127   nir_normalize_cubemap_coords(nir);
128   nir_validate_shader(nir);
129
130   nir_split_var_copies(nir);
131   nir_validate_shader(nir);
132
133   nir_optimize(nir, is_scalar);
134
135   /* Lower a bunch of stuff */
136   nir_lower_var_copies(nir);
137   nir_validate_shader(nir);
138
139   /* Get rid of split copies */
140   nir_optimize(nir, is_scalar);
141
142   brw_nir_lower_inputs(nir, is_scalar);
143   brw_nir_lower_outputs(nir, is_scalar);
144   nir_assign_var_locations(&nir->uniforms,
145                            &nir->num_uniforms,
146                            is_scalar ? type_size_scalar : type_size_vec4);
147   nir_lower_io(nir, -1, is_scalar ? type_size_scalar : type_size_vec4);
148   nir_validate_shader(nir);
149
150   nir_remove_dead_variables(nir);
151   nir_validate_shader(nir);
152
153   if (shader_prog) {
154      nir_lower_samplers(nir, shader_prog);
155      nir_validate_shader(nir);
156   }
157
158   nir_lower_system_values(nir);
159   nir_validate_shader(nir);
160
161   nir_lower_atomics(nir);
162   nir_validate_shader(nir);
163
164   nir_optimize(nir, is_scalar);
165
166   if (brw->gen >= 6) {
167      /* Try and fuse multiply-adds */
168      nir_opt_peephole_ffma(nir);
169      nir_validate_shader(nir);
170   }
171
172   nir_opt_algebraic_late(nir);
173   nir_validate_shader(nir);
174
175   nir_lower_locals_to_regs(nir);
176   nir_validate_shader(nir);
177
178   nir_lower_to_source_mods(nir);
179   nir_validate_shader(nir);
180   nir_copy_prop(nir);
181   nir_validate_shader(nir);
182   nir_opt_dce(nir);
183   nir_validate_shader(nir);
184
185   if (unlikely(debug_enabled)) {
186      /* Re-index SSA defs so we print more sensible numbers. */
187      nir_foreach_overload(nir, overload) {
188         if (overload->impl)
189            nir_index_ssa_defs(overload->impl);
190      }
191
192      fprintf(stderr, "NIR (SSA form) for %s shader:\n",
193              _mesa_shader_stage_to_string(stage));
194      nir_print_shader(nir, stderr);
195   }
196
197   nir_convert_from_ssa(nir, true);
198   nir_validate_shader(nir);
199
200   if (!is_scalar) {
201      nir_move_vec_src_uses_to_dest(nir);
202      nir_validate_shader(nir);
203
204      nir_lower_vec_to_movs(nir);
205      nir_validate_shader(nir);
206   }
207
208   /* This is the last pass we run before we start emitting stuff.  It
209    * determines when we need to insert boolean resolves on Gen <= 5.  We
210    * run it last because it stashes data in instr->pass_flags and we don't
211    * want that to be squashed by other NIR passes.
212    */
213   if (brw->gen <= 5)
214      brw_nir_analyze_boolean_resolves(nir);
215
216   nir_sweep(nir);
217
218   if (unlikely(debug_enabled)) {
219      fprintf(stderr, "NIR (final form) for %s shader:\n",
220              _mesa_shader_stage_to_string(stage));
221      nir_print_shader(nir, stderr);
222   }
223
224   return nir;
225}
226
227enum brw_reg_type
228brw_type_for_nir_type(nir_alu_type type)
229{
230   switch (type) {
231   case nir_type_unsigned:
232      return BRW_REGISTER_TYPE_UD;
233   case nir_type_bool:
234   case nir_type_int:
235      return BRW_REGISTER_TYPE_D;
236   case nir_type_float:
237      return BRW_REGISTER_TYPE_F;
238   default:
239      unreachable("unknown type");
240   }
241
242   return BRW_REGISTER_TYPE_F;
243}
244
245/* Returns the glsl_base_type corresponding to a nir_alu_type.
246 * This is used by both brw_vec4_nir and brw_fs_nir.
247 */
248enum glsl_base_type
249brw_glsl_base_type_for_nir_type(nir_alu_type type)
250{
251   switch (type) {
252   case nir_type_float:
253      return GLSL_TYPE_FLOAT;
254
255   case nir_type_int:
256      return GLSL_TYPE_INT;
257
258   case nir_type_unsigned:
259      return GLSL_TYPE_UINT;
260
261   default:
262      unreachable("bad type");
263   }
264}
265