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 brw_create_nir(struct brw_context
*brw
,
62 const struct gl_shader_program
*shader_prog
,
63 const struct gl_program
*prog
,
64 gl_shader_stage stage
)
66 struct gl_context
*ctx
= &brw
->ctx
;
67 const nir_shader_compiler_options
*options
=
68 ctx
->Const
.ShaderCompilerOptions
[stage
].NirOptions
;
69 struct gl_shader
*shader
= shader_prog
? shader_prog
->_LinkedShaders
[stage
] : NULL
;
70 bool debug_enabled
= INTEL_DEBUG
& intel_debug_flag_for_shader_stage(stage
);
73 /* First, lower the GLSL IR or Mesa IR to NIR */
75 nir
= glsl_to_nir(shader
, options
);
77 nir
= prog_to_nir(prog
, options
);
78 nir_convert_to_ssa(nir
); /* turn registers into SSA */
80 nir_validate_shader(nir
);
82 nir_lower_global_vars_to_local(nir
);
83 nir_validate_shader(nir
);
85 nir_lower_tex_projector(nir
);
86 nir_validate_shader(nir
);
88 nir_normalize_cubemap_coords(nir
);
89 nir_validate_shader(nir
);
91 nir_split_var_copies(nir
);
92 nir_validate_shader(nir
);
96 /* Lower a bunch of stuff */
97 nir_lower_var_copies(nir
);
98 nir_validate_shader(nir
);
100 /* Get rid of split copies */
103 nir_assign_var_locations_scalar_direct_first(nir
, &nir
->uniforms
,
104 &nir
->num_direct_uniforms
,
106 nir_assign_var_locations_scalar(&nir
->inputs
, &nir
->num_inputs
);
107 nir_assign_var_locations_scalar(&nir
->outputs
, &nir
->num_outputs
);
110 nir_validate_shader(nir
);
112 nir_remove_dead_variables(nir
);
113 nir_validate_shader(nir
);
116 nir_lower_samplers(nir
, shader_prog
, stage
);
117 nir_validate_shader(nir
);
120 nir_lower_system_values(nir
);
121 nir_validate_shader(nir
);
123 nir_lower_atomics(nir
);
124 nir_validate_shader(nir
);
129 /* Try and fuse multiply-adds */
130 nir_opt_peephole_ffma(nir
);
131 nir_validate_shader(nir
);
134 nir_opt_algebraic_late(nir
);
135 nir_validate_shader(nir
);
137 nir_lower_locals_to_regs(nir
);
138 nir_validate_shader(nir
);
140 nir_lower_to_source_mods(nir
);
141 nir_validate_shader(nir
);
143 nir_validate_shader(nir
);
145 nir_validate_shader(nir
);
147 if (unlikely(debug_enabled
)) {
148 /* Re-index SSA defs so we print more sensible numbers. */
149 nir_foreach_overload(nir
, overload
) {
151 nir_index_ssa_defs(overload
->impl
);
154 fprintf(stderr
, "NIR (SSA form) for %s shader:\n",
155 _mesa_shader_stage_to_string(stage
));
156 nir_print_shader(nir
, stderr
);
159 nir_convert_from_ssa(nir
, true);
160 nir_validate_shader(nir
);
162 /* This is the last pass we run before we start emitting stuff. It
163 * determines when we need to insert boolean resolves on Gen <= 5. We
164 * run it last because it stashes data in instr->pass_flags and we don't
165 * want that to be squashed by other NIR passes.
168 brw_nir_analyze_boolean_resolves(nir
);
172 if (unlikely(debug_enabled
)) {
173 fprintf(stderr
, "NIR (final form) for %s shader:\n",
174 _mesa_shader_stage_to_string(stage
));
175 nir_print_shader(nir
, stderr
);