2 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
28 #include "util/debug.h"
31 #include "ir3_compiler.h"
32 #include "ir3_shader.h"
34 static const nir_shader_compiler_options options
= {
46 .native_integers
= true,
47 .vertex_id_zero_based
= true,
48 .lower_extract_byte
= true,
49 .lower_extract_word
= true,
50 .lower_all_io_to_temps
= true,
51 .lower_helper_invocation
= true,
54 /* we don't want to lower vertex_id to _zero_based on newer gpus: */
55 static const nir_shader_compiler_options options_a6xx
= {
67 .native_integers
= true,
68 .vertex_id_zero_based
= false,
69 .lower_extract_byte
= true,
70 .lower_extract_word
= true,
71 .lower_all_io_to_temps
= true,
72 .lower_helper_invocation
= true,
75 const nir_shader_compiler_options
*
76 ir3_get_compiler_options(struct ir3_compiler
*compiler
)
78 if (compiler
->gpu_id
>= 600)
83 /* for given shader key, are any steps handled in nir? */
85 ir3_key_lowers_nir(const struct ir3_shader_key
*key
)
87 return key
->fsaturate_s
| key
->fsaturate_t
| key
->fsaturate_r
|
88 key
->vsaturate_s
| key
->vsaturate_t
| key
->vsaturate_r
|
89 key
->ucp_enables
| key
->color_two_side
|
90 key
->fclamp_color
| key
->vclamp_color
;
93 #define OPT(nir, pass, ...) ({ \
94 bool this_progress = false; \
95 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
99 #define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
102 ir3_optimize_loop(nir_shader
*s
)
108 OPT_V(s
, nir_lower_vars_to_ssa
);
109 progress
|= OPT(s
, nir_opt_copy_prop_vars
);
110 progress
|= OPT(s
, nir_opt_dead_write_vars
);
111 progress
|= OPT(s
, nir_lower_alu_to_scalar
);
112 progress
|= OPT(s
, nir_lower_phis_to_scalar
);
114 progress
|= OPT(s
, nir_copy_prop
);
115 progress
|= OPT(s
, nir_opt_dce
);
116 progress
|= OPT(s
, nir_opt_cse
);
119 gcm
= env_var_as_unsigned("GCM", 0);
121 progress
|= OPT(s
, nir_opt_gcm
, true);
123 progress
|= OPT(s
, nir_opt_gcm
, false);
124 progress
|= OPT(s
, nir_opt_peephole_select
, 16, true, true);
125 progress
|= OPT(s
, nir_opt_intrinsics
);
126 progress
|= OPT(s
, nir_opt_algebraic
);
127 progress
|= OPT(s
, nir_opt_constant_folding
);
128 progress
|= OPT(s
, nir_opt_dead_cf
);
129 if (OPT(s
, nir_opt_trivial_continues
)) {
131 /* If nir_opt_trivial_continues makes progress, then we need to clean
132 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
135 OPT(s
, nir_copy_prop
);
138 progress
|= OPT(s
, nir_opt_if
);
139 progress
|= OPT(s
, nir_opt_remove_phis
);
140 progress
|= OPT(s
, nir_opt_undef
);
146 ir3_optimize_nir(struct ir3_shader
*shader
, nir_shader
*s
,
147 const struct ir3_shader_key
*key
)
149 struct nir_lower_tex_options tex_options
= {
154 switch (shader
->type
) {
155 case MESA_SHADER_FRAGMENT
:
156 tex_options
.saturate_s
= key
->fsaturate_s
;
157 tex_options
.saturate_t
= key
->fsaturate_t
;
158 tex_options
.saturate_r
= key
->fsaturate_r
;
160 case MESA_SHADER_VERTEX
:
161 tex_options
.saturate_s
= key
->vsaturate_s
;
162 tex_options
.saturate_t
= key
->vsaturate_t
;
163 tex_options
.saturate_r
= key
->vsaturate_r
;
171 if (shader
->compiler
->gpu_id
>= 400) {
172 /* a4xx seems to have *no* sam.p */
173 tex_options
.lower_txp
= ~0; /* lower all txp */
175 /* a3xx just needs to avoid sam.p for 3d tex */
176 tex_options
.lower_txp
= (1 << GLSL_SAMPLER_DIM_3D
);
179 if (ir3_shader_debug
& IR3_DBG_DISASM
) {
180 debug_printf("----------------------\n");
181 nir_print_shader(s
, stdout
);
182 debug_printf("----------------------\n");
185 OPT_V(s
, nir_opt_global_to_local
);
186 OPT_V(s
, nir_lower_regs_to_ssa
);
189 if (s
->info
.stage
== MESA_SHADER_VERTEX
) {
190 OPT_V(s
, nir_lower_clip_vs
, key
->ucp_enables
, false);
191 if (key
->vclamp_color
)
192 OPT_V(s
, nir_lower_clamp_color_outputs
);
193 } else if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
194 OPT_V(s
, nir_lower_clip_fs
, key
->ucp_enables
);
195 if (key
->fclamp_color
)
196 OPT_V(s
, nir_lower_clamp_color_outputs
);
198 if (key
->color_two_side
) {
199 OPT_V(s
, nir_lower_two_sided_color
);
202 /* only want to do this the first time (when key is null)
203 * and not again on any potential 2nd variant lowering pass:
205 OPT_V(s
, ir3_nir_apply_trig_workarounds
);
208 OPT_V(s
, nir_lower_tex
, &tex_options
);
209 OPT_V(s
, nir_lower_load_const_to_scalar
);
210 if (shader
->compiler
->gpu_id
< 500)
211 OPT_V(s
, ir3_nir_lower_tg4_to_tex
);
213 ir3_optimize_loop(s
);
215 /* do idiv lowering after first opt loop to give a chance for
216 * divide by immed power-of-two to be caught first:
218 if (OPT(s
, nir_lower_idiv
))
219 ir3_optimize_loop(s
);
221 OPT_V(s
, nir_remove_dead_variables
, nir_var_function_temp
);
223 OPT_V(s
, nir_move_load_const
);
225 if (ir3_shader_debug
& IR3_DBG_DISASM
) {
226 debug_printf("----------------------\n");
227 nir_print_shader(s
, stdout
);
228 debug_printf("----------------------\n");
237 ir3_nir_scan_driver_consts(nir_shader
*shader
,
238 struct ir3_driver_const_layout
*layout
)
240 nir_foreach_function(function
, shader
) {
244 nir_foreach_block(block
, function
->impl
) {
245 nir_foreach_instr(instr
, block
) {
246 if (instr
->type
!= nir_instr_type_intrinsic
)
249 nir_intrinsic_instr
*intr
=
250 nir_instr_as_intrinsic(instr
);
253 switch (intr
->intrinsic
) {
254 case nir_intrinsic_get_buffer_size
:
255 idx
= nir_src_as_const_value(intr
->src
[0])->u32
[0];
256 if (layout
->ssbo_size
.mask
& (1 << idx
))
258 layout
->ssbo_size
.mask
|= (1 << idx
);
259 layout
->ssbo_size
.off
[idx
] =
260 layout
->ssbo_size
.count
;
261 layout
->ssbo_size
.count
+= 1; /* one const per */
263 case nir_intrinsic_image_deref_atomic_add
:
264 case nir_intrinsic_image_deref_atomic_min
:
265 case nir_intrinsic_image_deref_atomic_max
:
266 case nir_intrinsic_image_deref_atomic_and
:
267 case nir_intrinsic_image_deref_atomic_or
:
268 case nir_intrinsic_image_deref_atomic_xor
:
269 case nir_intrinsic_image_deref_atomic_exchange
:
270 case nir_intrinsic_image_deref_atomic_comp_swap
:
271 case nir_intrinsic_image_deref_store
:
272 case nir_intrinsic_image_deref_size
:
273 idx
= nir_intrinsic_get_var(intr
, 0)->data
.driver_location
;
274 if (layout
->image_dims
.mask
& (1 << idx
))
276 layout
->image_dims
.mask
|= (1 << idx
);
277 layout
->image_dims
.off
[idx
] =
278 layout
->image_dims
.count
;
279 layout
->image_dims
.count
+= 3; /* three const per */