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