nir, intel: Move use_scoped_memory_barrier to nir_options
[mesa.git] / src / intel / compiler / brw_compiler.c
1 /*
2 * Copyright © 2015-2016 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_compiler.h"
25 #include "brw_shader.h"
26 #include "brw_eu.h"
27 #include "dev/gen_debug.h"
28 #include "compiler/nir/nir.h"
29 #include "main/errors.h"
30 #include "util/debug.h"
31
32 #define COMMON_OPTIONS \
33 .lower_sub = true, \
34 .lower_fdiv = true, \
35 .lower_scmp = true, \
36 .lower_flrp16 = true, \
37 .lower_fmod = true, \
38 .lower_bitfield_extract = true, \
39 .lower_bitfield_insert = true, \
40 .lower_uadd_carry = true, \
41 .lower_usub_borrow = true, \
42 .lower_fdiv = true, \
43 .lower_flrp64 = true, \
44 .lower_isign = true, \
45 .lower_ldexp = true, \
46 .lower_device_index_to_zero = true, \
47 .vectorize_io = true, \
48 .use_interpolated_input_intrinsics = true, \
49 .vertex_id_zero_based = true, \
50 .lower_base_vertex = true, \
51 .use_scoped_memory_barrier = true
52
53 #define COMMON_SCALAR_OPTIONS \
54 .lower_to_scalar = true, \
55 .lower_pack_half_2x16 = true, \
56 .lower_pack_snorm_2x16 = true, \
57 .lower_pack_snorm_4x8 = true, \
58 .lower_pack_unorm_2x16 = true, \
59 .lower_pack_unorm_4x8 = true, \
60 .lower_unpack_half_2x16 = true, \
61 .lower_unpack_snorm_2x16 = true, \
62 .lower_unpack_snorm_4x8 = true, \
63 .lower_unpack_unorm_2x16 = true, \
64 .lower_unpack_unorm_4x8 = true, \
65 .lower_usub_sat64 = true, \
66 .lower_hadd64 = true, \
67 .max_unroll_iterations = 32
68
69 static const struct nir_shader_compiler_options scalar_nir_options = {
70 COMMON_OPTIONS,
71 COMMON_SCALAR_OPTIONS,
72 };
73
74 static const struct nir_shader_compiler_options vector_nir_options = {
75 COMMON_OPTIONS,
76
77 /* In the vec4 backend, our dpN instruction replicates its result to all the
78 * components of a vec4. We would like NIR to give us replicated fdot
79 * instructions because it can optimize better for us.
80 */
81 .fdot_replicates = true,
82
83 .lower_pack_snorm_2x16 = true,
84 .lower_pack_unorm_2x16 = true,
85 .lower_unpack_snorm_2x16 = true,
86 .lower_unpack_unorm_2x16 = true,
87 .lower_extract_byte = true,
88 .lower_extract_word = true,
89 .intel_vec4 = true,
90 .max_unroll_iterations = 32,
91 };
92
93 struct brw_compiler *
94 brw_compiler_create(void *mem_ctx, const struct gen_device_info *devinfo)
95 {
96 struct brw_compiler *compiler = rzalloc(mem_ctx, struct brw_compiler);
97
98 compiler->devinfo = devinfo;
99
100 brw_fs_alloc_reg_sets(compiler);
101 brw_vec4_alloc_reg_set(compiler);
102 brw_init_compaction_tables(devinfo);
103
104 compiler->precise_trig = env_var_as_boolean("INTEL_PRECISE_TRIG", false);
105
106 compiler->use_tcs_8_patch =
107 devinfo->gen >= 12 ||
108 (devinfo->gen >= 9 && (INTEL_DEBUG & DEBUG_TCS_EIGHT_PATCH));
109
110 if (devinfo->gen >= 10) {
111 /* We don't support vec4 mode on Cannonlake. */
112 for (int i = MESA_SHADER_VERTEX; i < MESA_SHADER_STAGES; i++)
113 compiler->scalar_stage[i] = true;
114 } else {
115 compiler->scalar_stage[MESA_SHADER_VERTEX] =
116 devinfo->gen >= 8 && env_var_as_boolean("INTEL_SCALAR_VS", true);
117 compiler->scalar_stage[MESA_SHADER_TESS_CTRL] =
118 devinfo->gen >= 8 && env_var_as_boolean("INTEL_SCALAR_TCS", true);
119 compiler->scalar_stage[MESA_SHADER_TESS_EVAL] =
120 devinfo->gen >= 8 && env_var_as_boolean("INTEL_SCALAR_TES", true);
121 compiler->scalar_stage[MESA_SHADER_GEOMETRY] =
122 devinfo->gen >= 8 && env_var_as_boolean("INTEL_SCALAR_GS", true);
123 compiler->scalar_stage[MESA_SHADER_FRAGMENT] = true;
124 compiler->scalar_stage[MESA_SHADER_COMPUTE] = true;
125 }
126
127 nir_lower_int64_options int64_options =
128 nir_lower_imul64 |
129 nir_lower_isign64 |
130 nir_lower_divmod64 |
131 nir_lower_imul_high64;
132 nir_lower_doubles_options fp64_options =
133 nir_lower_drcp |
134 nir_lower_dsqrt |
135 nir_lower_drsq |
136 nir_lower_dtrunc |
137 nir_lower_dfloor |
138 nir_lower_dceil |
139 nir_lower_dfract |
140 nir_lower_dround_even |
141 nir_lower_dmod |
142 nir_lower_dsub |
143 nir_lower_ddiv;
144
145 if (!devinfo->has_64bit_float || (INTEL_DEBUG & DEBUG_SOFT64)) {
146 int64_options |= nir_lower_mov64 |
147 nir_lower_icmp64 |
148 nir_lower_iadd64 |
149 nir_lower_iabs64 |
150 nir_lower_ineg64 |
151 nir_lower_logic64 |
152 nir_lower_minmax64 |
153 nir_lower_shift64 |
154 nir_lower_extract64;
155 fp64_options |= nir_lower_fp64_full_software;
156 }
157
158 /* The Bspec's section tittled "Instruction_multiply[DevBDW+]" claims that
159 * destination type can be Quadword and source type Doubleword for Gen8 and
160 * Gen9. So, lower 64 bit multiply instruction on rest of the platforms.
161 */
162 if (devinfo->gen < 8 || devinfo->gen > 9)
163 int64_options |= nir_lower_imul_2x32_64;
164
165 /* We want the GLSL compiler to emit code that uses condition codes */
166 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
167 compiler->glsl_compiler_options[i].MaxUnrollIterations = 0;
168 compiler->glsl_compiler_options[i].MaxIfDepth =
169 devinfo->gen < 6 ? 16 : UINT_MAX;
170
171 compiler->glsl_compiler_options[i].EmitNoIndirectInput = true;
172 compiler->glsl_compiler_options[i].EmitNoIndirectUniform = false;
173
174 bool is_scalar = compiler->scalar_stage[i];
175
176 compiler->glsl_compiler_options[i].EmitNoIndirectOutput = is_scalar;
177 compiler->glsl_compiler_options[i].EmitNoIndirectTemp = is_scalar;
178 compiler->glsl_compiler_options[i].OptimizeForAOS = !is_scalar;
179
180 struct nir_shader_compiler_options *nir_options =
181 rzalloc(compiler, struct nir_shader_compiler_options);
182 if (is_scalar) {
183 *nir_options = scalar_nir_options;
184 } else {
185 *nir_options = vector_nir_options;
186 }
187
188 /* Prior to Gen6, there are no three source operations, and Gen11 loses
189 * LRP.
190 */
191 nir_options->lower_ffma = devinfo->gen < 6;
192 nir_options->lower_flrp32 = devinfo->gen < 6 || devinfo->gen >= 11;
193 nir_options->lower_fpow = devinfo->gen >= 12;
194
195 nir_options->lower_rotate = devinfo->gen < 11;
196 nir_options->lower_bitfield_reverse = devinfo->gen < 7;
197
198 nir_options->lower_int64_options = int64_options;
199 nir_options->lower_doubles_options = fp64_options;
200
201 nir_options->unify_interfaces = i < MESA_SHADER_FRAGMENT;
202
203 compiler->glsl_compiler_options[i].NirOptions = nir_options;
204
205 compiler->glsl_compiler_options[i].ClampBlockIndicesToArrayBounds = true;
206 }
207
208 compiler->glsl_compiler_options[MESA_SHADER_TESS_CTRL].EmitNoIndirectInput = false;
209 compiler->glsl_compiler_options[MESA_SHADER_TESS_EVAL].EmitNoIndirectInput = false;
210 compiler->glsl_compiler_options[MESA_SHADER_TESS_CTRL].EmitNoIndirectOutput = false;
211
212 if (compiler->scalar_stage[MESA_SHADER_GEOMETRY])
213 compiler->glsl_compiler_options[MESA_SHADER_GEOMETRY].EmitNoIndirectInput = false;
214
215 return compiler;
216 }
217
218 static void
219 insert_u64_bit(uint64_t *val, bool add)
220 {
221 *val = (*val << 1) | !!add;
222 }
223
224 uint64_t
225 brw_get_compiler_config_value(const struct brw_compiler *compiler)
226 {
227 uint64_t config = 0;
228 insert_u64_bit(&config, compiler->precise_trig);
229 if (compiler->devinfo->gen >= 8 && compiler->devinfo->gen < 10) {
230 insert_u64_bit(&config, compiler->scalar_stage[MESA_SHADER_VERTEX]);
231 insert_u64_bit(&config, compiler->scalar_stage[MESA_SHADER_TESS_CTRL]);
232 insert_u64_bit(&config, compiler->scalar_stage[MESA_SHADER_TESS_EVAL]);
233 insert_u64_bit(&config, compiler->scalar_stage[MESA_SHADER_GEOMETRY]);
234 }
235 uint64_t debug_bits = INTEL_DEBUG;
236 uint64_t mask = DEBUG_DISK_CACHE_MASK;
237 while (mask != 0) {
238 const uint64_t bit = 1ULL << (ffsll(mask) - 1);
239 insert_u64_bit(&config, (debug_bits & bit) != 0);
240 mask &= ~bit;
241 }
242 return config;
243 }
244
245 unsigned
246 brw_prog_data_size(gl_shader_stage stage)
247 {
248 STATIC_ASSERT(MESA_SHADER_VERTEX == 0);
249 STATIC_ASSERT(MESA_SHADER_TESS_CTRL == 1);
250 STATIC_ASSERT(MESA_SHADER_TESS_EVAL == 2);
251 STATIC_ASSERT(MESA_SHADER_GEOMETRY == 3);
252 STATIC_ASSERT(MESA_SHADER_FRAGMENT == 4);
253 STATIC_ASSERT(MESA_SHADER_COMPUTE == 5);
254 static const size_t stage_sizes[] = {
255 sizeof(struct brw_vs_prog_data),
256 sizeof(struct brw_tcs_prog_data),
257 sizeof(struct brw_tes_prog_data),
258 sizeof(struct brw_gs_prog_data),
259 sizeof(struct brw_wm_prog_data),
260 sizeof(struct brw_cs_prog_data),
261 };
262 assert((int)stage >= 0 && stage < ARRAY_SIZE(stage_sizes));
263 return stage_sizes[stage];
264 }
265
266 unsigned
267 brw_prog_key_size(gl_shader_stage stage)
268 {
269 static const size_t stage_sizes[] = {
270 sizeof(struct brw_vs_prog_key),
271 sizeof(struct brw_tcs_prog_key),
272 sizeof(struct brw_tes_prog_key),
273 sizeof(struct brw_gs_prog_key),
274 sizeof(struct brw_wm_prog_key),
275 sizeof(struct brw_cs_prog_key),
276 };
277 assert((int)stage >= 0 && stage < ARRAY_SIZE(stage_sizes));
278 return stage_sizes[stage];
279 }