vk: Add four unit tests for our lock-free data-structures
[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 "glsl/glsl_parser_extras.h"
26 #include "glsl/nir/glsl_to_nir.h"
27 #include "program/prog_to_nir.h"
28
29 static void
30 nir_optimize(nir_shader *nir)
31 {
32 bool progress;
33 do {
34 progress = false;
35 nir_lower_vars_to_ssa(nir);
36 nir_validate_shader(nir);
37 nir_lower_alu_to_scalar(nir);
38 nir_validate_shader(nir);
39 progress |= nir_copy_prop(nir);
40 nir_validate_shader(nir);
41 nir_lower_phis_to_scalar(nir);
42 nir_validate_shader(nir);
43 progress |= nir_copy_prop(nir);
44 nir_validate_shader(nir);
45 progress |= nir_opt_dce(nir);
46 nir_validate_shader(nir);
47 progress |= nir_opt_cse(nir);
48 nir_validate_shader(nir);
49 progress |= nir_opt_peephole_select(nir);
50 nir_validate_shader(nir);
51 progress |= nir_opt_algebraic(nir);
52 nir_validate_shader(nir);
53 progress |= nir_opt_constant_folding(nir);
54 nir_validate_shader(nir);
55 progress |= nir_opt_remove_phis(nir);
56 nir_validate_shader(nir);
57 } while (progress);
58 }
59
60 static bool
61 count_nir_instrs_in_block(nir_block *block, void *state)
62 {
63 int *count = (int *) state;
64 nir_foreach_instr(block, instr) {
65 *count = *count + 1;
66 }
67 return true;
68 }
69
70 static int
71 count_nir_instrs(nir_shader *nir)
72 {
73 int count = 0;
74 nir_foreach_overload(nir, overload) {
75 if (!overload->impl)
76 continue;
77 nir_foreach_block(overload->impl, count_nir_instrs_in_block, &count);
78 }
79 return count;
80 }
81
82 nir_shader *
83 brw_create_nir(struct brw_context *brw,
84 const struct gl_shader_program *shader_prog,
85 const struct gl_program *prog,
86 gl_shader_stage stage)
87 {
88 struct gl_context *ctx = &brw->ctx;
89 const nir_shader_compiler_options *options =
90 ctx->Const.ShaderCompilerOptions[stage].NirOptions;
91 struct gl_shader *shader = shader_prog ? shader_prog->_LinkedShaders[stage] : NULL;
92 nir_shader *nir;
93
94 /* First, lower the GLSL IR or Mesa IR to NIR */
95 if (shader_prog) {
96 nir = glsl_to_nir(shader, options);
97 } else {
98 nir = prog_to_nir(prog, options);
99 nir_convert_to_ssa(nir); /* turn registers into SSA */
100 }
101 nir_validate_shader(nir);
102
103 brw_process_nir(nir, brw->intelScreen->devinfo, shader_prog, stage);
104
105 static GLuint msg_id = 0;
106 _mesa_gl_debug(&brw->ctx, &msg_id,
107 MESA_DEBUG_SOURCE_SHADER_COMPILER,
108 MESA_DEBUG_TYPE_OTHER,
109 MESA_DEBUG_SEVERITY_NOTIFICATION,
110 "%s NIR shader: %d inst\n",
111 _mesa_shader_stage_to_abbrev(stage),
112 count_nir_instrs(nir));
113
114 return nir;
115 }
116
117 void
118 brw_process_nir(nir_shader *nir,
119 const struct brw_device_info *devinfo,
120 const struct gl_shader_program *shader_prog,
121 gl_shader_stage stage)
122 {
123 bool debug_enabled = INTEL_DEBUG & intel_debug_flag_for_shader_stage(stage);
124
125 nir_lower_global_vars_to_local(nir);
126 nir_validate_shader(nir);
127
128 nir_lower_tex_projector(nir);
129 nir_validate_shader(nir);
130
131 nir_normalize_cubemap_coords(nir);
132 nir_validate_shader(nir);
133
134 nir_split_var_copies(nir);
135 nir_validate_shader(nir);
136
137 nir_optimize(nir);
138
139 /* Lower a bunch of stuff */
140 nir_lower_var_copies(nir);
141 nir_validate_shader(nir);
142
143 /* Get rid of split copies */
144 nir_optimize(nir);
145
146 nir_assign_var_locations_scalar_direct_first(nir, &nir->uniforms,
147 &nir->num_direct_uniforms,
148 &nir->num_uniforms);
149 nir_assign_var_locations_scalar(&nir->inputs, &nir->num_inputs);
150 nir_assign_var_locations_scalar(&nir->outputs, &nir->num_outputs);
151
152 nir_lower_io(nir);
153 nir_validate_shader(nir);
154
155 nir_remove_dead_variables(nir);
156 nir_validate_shader(nir);
157
158 if (shader_prog) {
159 nir_lower_samplers(nir, shader_prog, stage);
160 } else {
161 nir_lower_samplers_for_vk(nir);
162 }
163 nir_validate_shader(nir);
164
165 nir_lower_system_values(nir);
166 nir_validate_shader(nir);
167
168 nir_lower_atomics(nir);
169 nir_validate_shader(nir);
170
171 nir_optimize(nir);
172
173 if (devinfo->gen >= 6) {
174 /* Try and fuse multiply-adds */
175 nir_opt_peephole_ffma(nir);
176 nir_validate_shader(nir);
177 }
178
179 nir_opt_algebraic_late(nir);
180 nir_validate_shader(nir);
181
182 nir_lower_locals_to_regs(nir);
183 nir_validate_shader(nir);
184
185 nir_lower_to_source_mods(nir);
186 nir_validate_shader(nir);
187 nir_copy_prop(nir);
188 nir_validate_shader(nir);
189 nir_opt_dce(nir);
190 nir_validate_shader(nir);
191
192 if (unlikely(debug_enabled)) {
193 /* Re-index SSA defs so we print more sensible numbers. */
194 nir_foreach_overload(nir, overload) {
195 if (overload->impl)
196 nir_index_ssa_defs(overload->impl);
197 }
198
199 fprintf(stderr, "NIR (SSA form) for %s shader:\n",
200 _mesa_shader_stage_to_string(stage));
201 nir_print_shader(nir, stderr);
202 }
203
204 nir_convert_from_ssa(nir);
205 nir_validate_shader(nir);
206
207 /* This is the last pass we run before we start emitting stuff. It
208 * determines when we need to insert boolean resolves on Gen <= 5. We
209 * run it last because it stashes data in instr->pass_flags and we don't
210 * want that to be squashed by other NIR passes.
211 */
212 if (devinfo->gen <= 5)
213 brw_nir_analyze_boolean_resolves(nir);
214
215 nir_sweep(nir);
216
217 if (unlikely(debug_enabled)) {
218 fprintf(stderr, "NIR (final form) for %s shader:\n",
219 _mesa_shader_stage_to_string(stage));
220 nir_print_shader(nir, stderr);
221 }
222 }