2 * Copyright © 2014 Intel Corporation
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:
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
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
25 #include "glsl/glsl_parser_extras.h"
26 #include "glsl/nir/glsl_to_nir.h"
27 #include "program/prog_to_nir.h"
30 nir_optimize(nir_shader
*nir
)
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
);
61 count_nir_instrs_in_block(nir_block
*block
, void *state
)
63 int *count
= (int *) state
;
64 nir_foreach_instr(block
, instr
) {
71 count_nir_instrs(nir_shader
*nir
)
74 nir_foreach_overload(nir
, overload
) {
77 nir_foreach_block(overload
->impl
, count_nir_instrs_in_block
, &count
);
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
)
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 bool debug_enabled
= INTEL_DEBUG
& intel_debug_flag_for_shader_stage(stage
);
95 /* First, lower the GLSL IR or Mesa IR to NIR */
97 nir
= glsl_to_nir(shader
, options
);
99 nir
= prog_to_nir(prog
, options
);
100 nir_convert_to_ssa(nir
); /* turn registers into SSA */
102 nir_validate_shader(nir
);
104 nir_lower_global_vars_to_local(nir
);
105 nir_validate_shader(nir
);
107 nir_lower_tex_projector(nir
);
108 nir_validate_shader(nir
);
110 nir_normalize_cubemap_coords(nir
);
111 nir_validate_shader(nir
);
113 nir_split_var_copies(nir
);
114 nir_validate_shader(nir
);
118 /* Lower a bunch of stuff */
119 nir_lower_var_copies(nir
);
120 nir_validate_shader(nir
);
122 /* Get rid of split copies */
126 nir_assign_var_locations_scalar_direct_first(nir
, &nir
->uniforms
,
127 &nir
->num_direct_uniforms
,
130 /* ARB programs generally create a giant array of "uniform" data, and allow
131 * indirect addressing without any boundaries. In the absence of bounds
132 * analysis, it's all or nothing. num_direct_uniforms is only useful when
133 * we have some direct and some indirect access; it doesn't matter here.
135 nir
->num_direct_uniforms
= 0;
137 nir_assign_var_locations_scalar(&nir
->inputs
, &nir
->num_inputs
);
138 nir_assign_var_locations_scalar(&nir
->outputs
, &nir
->num_outputs
);
141 nir_validate_shader(nir
);
143 nir_remove_dead_variables(nir
);
144 nir_validate_shader(nir
);
147 nir_lower_samplers_for_vk(nir
);
148 nir_validate_shader(nir
);
151 nir_lower_system_values(nir
);
152 nir_validate_shader(nir
);
154 nir_lower_atomics(nir
);
155 nir_validate_shader(nir
);
160 /* Try and fuse multiply-adds */
161 nir_opt_peephole_ffma(nir
);
162 nir_validate_shader(nir
);
165 nir_opt_algebraic_late(nir
);
166 nir_validate_shader(nir
);
168 nir_lower_locals_to_regs(nir
);
169 nir_validate_shader(nir
);
171 nir_lower_to_source_mods(nir
);
172 nir_validate_shader(nir
);
174 nir_validate_shader(nir
);
176 nir_validate_shader(nir
);
178 if (unlikely(debug_enabled
)) {
179 fprintf(stderr
, "NIR (SSA form) for %s shader:\n",
180 _mesa_shader_stage_to_string(stage
));
181 nir_print_shader(nir
, stderr
);
184 static GLuint msg_id
= 0;
185 _mesa_gl_debug(&brw
->ctx
, &msg_id
,
186 MESA_DEBUG_SOURCE_SHADER_COMPILER
,
187 MESA_DEBUG_TYPE_OTHER
,
188 MESA_DEBUG_SEVERITY_NOTIFICATION
,
189 "%s NIR shader: %d inst\n",
190 _mesa_shader_stage_to_abbrev(stage
),
191 count_nir_instrs(nir
));
193 nir_convert_from_ssa(nir
);
194 nir_validate_shader(nir
);
196 /* This is the last pass we run before we start emitting stuff. It
197 * determines when we need to insert boolean resolves on Gen <= 5. We
198 * run it last because it stashes data in instr->pass_flags and we don't
199 * want that to be squashed by other NIR passes.
202 brw_nir_analyze_boolean_resolves(nir
);
206 if (unlikely(debug_enabled
)) {
207 fprintf(stderr
, "NIR (final form) for %s shader:\n",
208 _mesa_shader_stage_to_string(stage
));
209 nir_print_shader(nir
, stderr
);