aco: Use context variables instead of calculating TCS inputs/outputs.
[mesa.git] / src / amd / compiler / aco_instruction_selection_setup.cpp
1 /*
2 * Copyright © 2018 Valve 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
25 #include <array>
26 #include <unordered_map>
27 #include "aco_ir.h"
28 #include "nir.h"
29 #include "nir_control_flow.h"
30 #include "vulkan/radv_shader.h"
31 #include "vulkan/radv_descriptor_set.h"
32 #include "vulkan/radv_shader_args.h"
33 #include "sid.h"
34 #include "ac_exp_param.h"
35 #include "ac_shader_util.h"
36
37 #include "util/u_math.h"
38
39 #define MAX_INLINE_PUSH_CONSTS 8
40
41 namespace aco {
42
43 struct shader_io_state {
44 uint8_t mask[VARYING_SLOT_MAX];
45 Temp temps[VARYING_SLOT_MAX * 4u];
46
47 shader_io_state() {
48 memset(mask, 0, sizeof(mask));
49 std::fill_n(temps, VARYING_SLOT_MAX * 4u, Temp(0, RegClass::v1));
50 }
51 };
52
53 struct isel_context {
54 const struct radv_nir_compiler_options *options;
55 struct radv_shader_args *args;
56 Program *program;
57 nir_shader *shader;
58 uint32_t constant_data_offset;
59 Block *block;
60 bool *divergent_vals;
61 std::unique_ptr<Temp[]> allocated;
62 std::unordered_map<unsigned, std::array<Temp,NIR_MAX_VEC_COMPONENTS>> allocated_vec;
63 Stage stage; /* Stage */
64 bool has_gfx10_wave64_bpermute = false;
65 struct {
66 bool has_branch;
67 uint16_t loop_nest_depth = 0;
68 struct {
69 unsigned header_idx;
70 Block* exit;
71 bool has_divergent_continue = false;
72 bool has_divergent_branch = false;
73 } parent_loop;
74 struct {
75 bool is_divergent = false;
76 } parent_if;
77 bool exec_potentially_empty_discard = false; /* set to false when loop_nest_depth==0 && parent_if.is_divergent==false */
78 uint16_t exec_potentially_empty_break_depth = UINT16_MAX;
79 /* Set to false when loop_nest_depth==exec_potentially_empty_break_depth
80 * and parent_if.is_divergent==false. Called _break but it's also used for
81 * loop continues. */
82 bool exec_potentially_empty_break = false;
83 std::unique_ptr<unsigned[]> nir_to_aco; /* NIR block index to ACO block index */
84 } cf_info;
85
86 Temp arg_temps[AC_MAX_ARGS];
87
88 /* FS inputs */
89 Temp persp_centroid, linear_centroid;
90
91 /* GS inputs */
92 Temp gs_wave_id;
93
94 /* gathered information */
95 uint64_t input_masks[MESA_SHADER_COMPUTE];
96 uint64_t output_masks[MESA_SHADER_COMPUTE];
97
98 /* VS output information */
99 bool export_clip_dists;
100 unsigned num_clip_distances;
101 unsigned num_cull_distances;
102
103 /* tessellation information */
104 unsigned tcs_tess_lvl_out_loc;
105 unsigned tcs_tess_lvl_in_loc;
106 uint64_t tcs_temp_only_inputs;
107 uint32_t tcs_num_inputs;
108 uint32_t tcs_num_outputs;
109 uint32_t tcs_num_patch_outputs;
110 uint32_t tcs_num_patches;
111 bool tcs_in_out_eq = false;
112
113 /* I/O information */
114 shader_io_state inputs;
115 shader_io_state outputs;
116 };
117
118 Temp get_arg(isel_context *ctx, struct ac_arg arg)
119 {
120 assert(arg.used);
121 return ctx->arg_temps[arg.arg_index];
122 }
123
124 unsigned get_interp_input(nir_intrinsic_op intrin, enum glsl_interp_mode interp)
125 {
126 switch (interp) {
127 case INTERP_MODE_SMOOTH:
128 case INTERP_MODE_NONE:
129 if (intrin == nir_intrinsic_load_barycentric_pixel ||
130 intrin == nir_intrinsic_load_barycentric_at_sample ||
131 intrin == nir_intrinsic_load_barycentric_at_offset)
132 return S_0286CC_PERSP_CENTER_ENA(1);
133 else if (intrin == nir_intrinsic_load_barycentric_centroid)
134 return S_0286CC_PERSP_CENTROID_ENA(1);
135 else if (intrin == nir_intrinsic_load_barycentric_sample)
136 return S_0286CC_PERSP_SAMPLE_ENA(1);
137 break;
138 case INTERP_MODE_NOPERSPECTIVE:
139 if (intrin == nir_intrinsic_load_barycentric_pixel)
140 return S_0286CC_LINEAR_CENTER_ENA(1);
141 else if (intrin == nir_intrinsic_load_barycentric_centroid)
142 return S_0286CC_LINEAR_CENTROID_ENA(1);
143 else if (intrin == nir_intrinsic_load_barycentric_sample)
144 return S_0286CC_LINEAR_SAMPLE_ENA(1);
145 break;
146 default:
147 break;
148 }
149 return 0;
150 }
151
152 /* If one side of a divergent IF ends in a branch and the other doesn't, we
153 * might have to emit the contents of the side without the branch at the merge
154 * block instead. This is so that we can use any SGPR live-out of the side
155 * without the branch without creating a linear phi in the invert or merge block. */
156 bool
157 sanitize_if(nir_function_impl *impl, bool *divergent, nir_if *nif)
158 {
159 //TODO: skip this if the condition is uniform and there are no divergent breaks/continues?
160
161 nir_block *then_block = nir_if_last_then_block(nif);
162 nir_block *else_block = nir_if_last_else_block(nif);
163 bool then_jump = nir_block_ends_in_jump(then_block) || nir_block_is_unreachable(then_block);
164 bool else_jump = nir_block_ends_in_jump(else_block) || nir_block_is_unreachable(else_block);
165 if (then_jump == else_jump)
166 return false;
167
168 /* If the continue from block is empty then return as there is nothing to
169 * move.
170 */
171 if (nir_cf_list_is_empty_block(else_jump ? &nif->then_list : &nif->else_list))
172 return false;
173
174 /* Even though this if statement has a jump on one side, we may still have
175 * phis afterwards. Single-source phis can be produced by loop unrolling
176 * or dead control-flow passes and are perfectly legal. Run a quick phi
177 * removal on the block after the if to clean up any such phis.
178 */
179 nir_opt_remove_phis_block(nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node)));
180
181 /* Finally, move the continue from branch after the if-statement. */
182 nir_block *last_continue_from_blk = else_jump ? then_block : else_block;
183 nir_block *first_continue_from_blk = else_jump ?
184 nir_if_first_then_block(nif) : nir_if_first_else_block(nif);
185
186 nir_cf_list tmp;
187 nir_cf_extract(&tmp, nir_before_block(first_continue_from_blk),
188 nir_after_block(last_continue_from_blk));
189 nir_cf_reinsert(&tmp, nir_after_cf_node(&nif->cf_node));
190
191 /* nir_cf_extract() invalidates dominance metadata, but it should still be
192 * correct because of the specific type of transformation we did. Block
193 * indices are not valid except for block_0's, which is all we care about for
194 * nir_block_is_unreachable(). */
195 impl->valid_metadata =
196 (nir_metadata)(impl->valid_metadata | nir_metadata_dominance | nir_metadata_block_index);
197
198 return true;
199 }
200
201 bool
202 sanitize_cf_list(nir_function_impl *impl, bool *divergent, struct exec_list *cf_list)
203 {
204 bool progress = false;
205 foreach_list_typed(nir_cf_node, cf_node, node, cf_list) {
206 switch (cf_node->type) {
207 case nir_cf_node_block:
208 break;
209 case nir_cf_node_if: {
210 nir_if *nif = nir_cf_node_as_if(cf_node);
211 progress |= sanitize_cf_list(impl, divergent, &nif->then_list);
212 progress |= sanitize_cf_list(impl, divergent, &nif->else_list);
213 progress |= sanitize_if(impl, divergent, nif);
214 break;
215 }
216 case nir_cf_node_loop: {
217 nir_loop *loop = nir_cf_node_as_loop(cf_node);
218 progress |= sanitize_cf_list(impl, divergent, &loop->body);
219 break;
220 }
221 case nir_cf_node_function:
222 unreachable("Invalid cf type");
223 }
224 }
225
226 return progress;
227 }
228
229 RegClass get_reg_class(isel_context *ctx, RegType type, unsigned components, unsigned bitsize)
230 {
231 if (bitsize == 1)
232 return RegClass(RegType::sgpr, ctx->program->lane_mask.size() * components);
233 else
234 return RegClass::get(type, components * bitsize / 8u);
235 }
236
237 void init_context(isel_context *ctx, nir_shader *shader)
238 {
239 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
240 unsigned lane_mask_size = ctx->program->lane_mask.size();
241
242 ctx->shader = shader;
243 ctx->divergent_vals = nir_divergence_analysis(shader, nir_divergence_view_index_uniform);
244
245 /* sanitize control flow */
246 nir_metadata_require(impl, nir_metadata_dominance);
247 sanitize_cf_list(impl, ctx->divergent_vals, &impl->body);
248 nir_metadata_preserve(impl, (nir_metadata)~nir_metadata_block_index);
249
250 /* we'll need this for isel */
251 nir_metadata_require(impl, nir_metadata_block_index);
252
253 if (!(ctx->stage & sw_gs_copy) && ctx->options->dump_preoptir) {
254 fprintf(stderr, "NIR shader before instruction selection:\n");
255 nir_print_shader(shader, stderr);
256 }
257
258 std::unique_ptr<Temp[]> allocated{new Temp[impl->ssa_alloc]()};
259
260 unsigned spi_ps_inputs = 0;
261
262 std::unique_ptr<unsigned[]> nir_to_aco{new unsigned[impl->num_blocks]()};
263
264 bool done = false;
265 while (!done) {
266 done = true;
267 nir_foreach_block(block, impl) {
268 nir_foreach_instr(instr, block) {
269 switch(instr->type) {
270 case nir_instr_type_alu: {
271 nir_alu_instr *alu_instr = nir_instr_as_alu(instr);
272 RegType type = RegType::sgpr;
273 switch(alu_instr->op) {
274 case nir_op_fmul:
275 case nir_op_fadd:
276 case nir_op_fsub:
277 case nir_op_fmax:
278 case nir_op_fmin:
279 case nir_op_fmax3:
280 case nir_op_fmin3:
281 case nir_op_fmed3:
282 case nir_op_fneg:
283 case nir_op_fabs:
284 case nir_op_fsat:
285 case nir_op_fsign:
286 case nir_op_frcp:
287 case nir_op_frsq:
288 case nir_op_fsqrt:
289 case nir_op_fexp2:
290 case nir_op_flog2:
291 case nir_op_ffract:
292 case nir_op_ffloor:
293 case nir_op_fceil:
294 case nir_op_ftrunc:
295 case nir_op_fround_even:
296 case nir_op_fsin:
297 case nir_op_fcos:
298 case nir_op_f2f16:
299 case nir_op_f2f16_rtz:
300 case nir_op_f2f16_rtne:
301 case nir_op_f2f32:
302 case nir_op_f2f64:
303 case nir_op_u2f16:
304 case nir_op_u2f32:
305 case nir_op_u2f64:
306 case nir_op_i2f16:
307 case nir_op_i2f32:
308 case nir_op_i2f64:
309 case nir_op_pack_half_2x16:
310 case nir_op_unpack_half_2x16_split_x:
311 case nir_op_unpack_half_2x16_split_y:
312 case nir_op_fddx:
313 case nir_op_fddy:
314 case nir_op_fddx_fine:
315 case nir_op_fddy_fine:
316 case nir_op_fddx_coarse:
317 case nir_op_fddy_coarse:
318 case nir_op_fquantize2f16:
319 case nir_op_ldexp:
320 case nir_op_frexp_sig:
321 case nir_op_frexp_exp:
322 case nir_op_cube_face_index:
323 case nir_op_cube_face_coord:
324 type = RegType::vgpr;
325 break;
326 case nir_op_f2i16:
327 case nir_op_f2u16:
328 case nir_op_f2i32:
329 case nir_op_f2u32:
330 case nir_op_f2i64:
331 case nir_op_f2u64:
332 case nir_op_b2i32:
333 case nir_op_b2b32:
334 case nir_op_b2f16:
335 case nir_op_b2f32:
336 case nir_op_mov:
337 type = ctx->divergent_vals[alu_instr->dest.dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
338 break;
339 case nir_op_bcsel:
340 type = ctx->divergent_vals[alu_instr->dest.dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
341 /* fallthrough */
342 default:
343 for (unsigned i = 0; i < nir_op_infos[alu_instr->op].num_inputs; i++) {
344 if (allocated[alu_instr->src[i].src.ssa->index].type() == RegType::vgpr)
345 type = RegType::vgpr;
346 }
347 break;
348 }
349
350 RegClass rc = get_reg_class(ctx, type, alu_instr->dest.dest.ssa.num_components, alu_instr->dest.dest.ssa.bit_size);
351 allocated[alu_instr->dest.dest.ssa.index] = Temp(0, rc);
352 break;
353 }
354 case nir_instr_type_load_const: {
355 unsigned num_components = nir_instr_as_load_const(instr)->def.num_components;
356 unsigned bit_size = nir_instr_as_load_const(instr)->def.bit_size;
357 RegClass rc = get_reg_class(ctx, RegType::sgpr, num_components, bit_size);
358 allocated[nir_instr_as_load_const(instr)->def.index] = Temp(0, rc);
359 break;
360 }
361 case nir_instr_type_intrinsic: {
362 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
363 if (!nir_intrinsic_infos[intrinsic->intrinsic].has_dest)
364 break;
365 RegType type = RegType::sgpr;
366 switch(intrinsic->intrinsic) {
367 case nir_intrinsic_load_push_constant:
368 case nir_intrinsic_load_work_group_id:
369 case nir_intrinsic_load_num_work_groups:
370 case nir_intrinsic_load_subgroup_id:
371 case nir_intrinsic_load_num_subgroups:
372 case nir_intrinsic_load_first_vertex:
373 case nir_intrinsic_load_base_instance:
374 case nir_intrinsic_get_buffer_size:
375 case nir_intrinsic_vote_all:
376 case nir_intrinsic_vote_any:
377 case nir_intrinsic_read_first_invocation:
378 case nir_intrinsic_read_invocation:
379 case nir_intrinsic_first_invocation:
380 case nir_intrinsic_ballot:
381 type = RegType::sgpr;
382 break;
383 case nir_intrinsic_load_sample_id:
384 case nir_intrinsic_load_sample_mask_in:
385 case nir_intrinsic_load_input:
386 case nir_intrinsic_load_output:
387 case nir_intrinsic_load_input_vertex:
388 case nir_intrinsic_load_per_vertex_input:
389 case nir_intrinsic_load_per_vertex_output:
390 case nir_intrinsic_load_vertex_id:
391 case nir_intrinsic_load_vertex_id_zero_base:
392 case nir_intrinsic_load_barycentric_sample:
393 case nir_intrinsic_load_barycentric_pixel:
394 case nir_intrinsic_load_barycentric_model:
395 case nir_intrinsic_load_barycentric_centroid:
396 case nir_intrinsic_load_barycentric_at_sample:
397 case nir_intrinsic_load_barycentric_at_offset:
398 case nir_intrinsic_load_interpolated_input:
399 case nir_intrinsic_load_frag_coord:
400 case nir_intrinsic_load_sample_pos:
401 case nir_intrinsic_load_layer_id:
402 case nir_intrinsic_load_local_invocation_id:
403 case nir_intrinsic_load_local_invocation_index:
404 case nir_intrinsic_load_subgroup_invocation:
405 case nir_intrinsic_load_tess_coord:
406 case nir_intrinsic_write_invocation_amd:
407 case nir_intrinsic_mbcnt_amd:
408 case nir_intrinsic_load_instance_id:
409 case nir_intrinsic_ssbo_atomic_add:
410 case nir_intrinsic_ssbo_atomic_imin:
411 case nir_intrinsic_ssbo_atomic_umin:
412 case nir_intrinsic_ssbo_atomic_imax:
413 case nir_intrinsic_ssbo_atomic_umax:
414 case nir_intrinsic_ssbo_atomic_and:
415 case nir_intrinsic_ssbo_atomic_or:
416 case nir_intrinsic_ssbo_atomic_xor:
417 case nir_intrinsic_ssbo_atomic_exchange:
418 case nir_intrinsic_ssbo_atomic_comp_swap:
419 case nir_intrinsic_global_atomic_add:
420 case nir_intrinsic_global_atomic_imin:
421 case nir_intrinsic_global_atomic_umin:
422 case nir_intrinsic_global_atomic_imax:
423 case nir_intrinsic_global_atomic_umax:
424 case nir_intrinsic_global_atomic_and:
425 case nir_intrinsic_global_atomic_or:
426 case nir_intrinsic_global_atomic_xor:
427 case nir_intrinsic_global_atomic_exchange:
428 case nir_intrinsic_global_atomic_comp_swap:
429 case nir_intrinsic_image_deref_atomic_add:
430 case nir_intrinsic_image_deref_atomic_umin:
431 case nir_intrinsic_image_deref_atomic_imin:
432 case nir_intrinsic_image_deref_atomic_umax:
433 case nir_intrinsic_image_deref_atomic_imax:
434 case nir_intrinsic_image_deref_atomic_and:
435 case nir_intrinsic_image_deref_atomic_or:
436 case nir_intrinsic_image_deref_atomic_xor:
437 case nir_intrinsic_image_deref_atomic_exchange:
438 case nir_intrinsic_image_deref_atomic_comp_swap:
439 case nir_intrinsic_image_deref_size:
440 case nir_intrinsic_shared_atomic_add:
441 case nir_intrinsic_shared_atomic_imin:
442 case nir_intrinsic_shared_atomic_umin:
443 case nir_intrinsic_shared_atomic_imax:
444 case nir_intrinsic_shared_atomic_umax:
445 case nir_intrinsic_shared_atomic_and:
446 case nir_intrinsic_shared_atomic_or:
447 case nir_intrinsic_shared_atomic_xor:
448 case nir_intrinsic_shared_atomic_exchange:
449 case nir_intrinsic_shared_atomic_comp_swap:
450 case nir_intrinsic_load_scratch:
451 case nir_intrinsic_load_invocation_id:
452 case nir_intrinsic_load_primitive_id:
453 type = RegType::vgpr;
454 break;
455 case nir_intrinsic_shuffle:
456 case nir_intrinsic_quad_broadcast:
457 case nir_intrinsic_quad_swap_horizontal:
458 case nir_intrinsic_quad_swap_vertical:
459 case nir_intrinsic_quad_swap_diagonal:
460 case nir_intrinsic_quad_swizzle_amd:
461 case nir_intrinsic_masked_swizzle_amd:
462 case nir_intrinsic_inclusive_scan:
463 case nir_intrinsic_exclusive_scan:
464 case nir_intrinsic_reduce:
465 case nir_intrinsic_load_ubo:
466 case nir_intrinsic_load_ssbo:
467 case nir_intrinsic_load_global:
468 case nir_intrinsic_vulkan_resource_index:
469 case nir_intrinsic_load_shared:
470 type = ctx->divergent_vals[intrinsic->dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
471 break;
472 case nir_intrinsic_load_view_index:
473 type = ctx->stage == fragment_fs ? RegType::vgpr : RegType::sgpr;
474 break;
475 default:
476 for (unsigned i = 0; i < nir_intrinsic_infos[intrinsic->intrinsic].num_srcs; i++) {
477 if (allocated[intrinsic->src[i].ssa->index].type() == RegType::vgpr)
478 type = RegType::vgpr;
479 }
480 break;
481 }
482 RegClass rc = get_reg_class(ctx, type, intrinsic->dest.ssa.num_components, intrinsic->dest.ssa.bit_size);
483 allocated[intrinsic->dest.ssa.index] = Temp(0, rc);
484
485 switch(intrinsic->intrinsic) {
486 case nir_intrinsic_load_barycentric_sample:
487 case nir_intrinsic_load_barycentric_pixel:
488 case nir_intrinsic_load_barycentric_centroid:
489 case nir_intrinsic_load_barycentric_at_sample:
490 case nir_intrinsic_load_barycentric_at_offset: {
491 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(intrinsic);
492 spi_ps_inputs |= get_interp_input(intrinsic->intrinsic, mode);
493 break;
494 }
495 case nir_intrinsic_load_barycentric_model:
496 spi_ps_inputs |= S_0286CC_PERSP_PULL_MODEL_ENA(1);
497 break;
498 case nir_intrinsic_load_front_face:
499 spi_ps_inputs |= S_0286CC_FRONT_FACE_ENA(1);
500 break;
501 case nir_intrinsic_load_frag_coord:
502 case nir_intrinsic_load_sample_pos: {
503 uint8_t mask = nir_ssa_def_components_read(&intrinsic->dest.ssa);
504 for (unsigned i = 0; i < 4; i++) {
505 if (mask & (1 << i))
506 spi_ps_inputs |= S_0286CC_POS_X_FLOAT_ENA(1) << i;
507
508 }
509 break;
510 }
511 case nir_intrinsic_load_sample_id:
512 spi_ps_inputs |= S_0286CC_ANCILLARY_ENA(1);
513 break;
514 case nir_intrinsic_load_sample_mask_in:
515 spi_ps_inputs |= S_0286CC_ANCILLARY_ENA(1);
516 spi_ps_inputs |= S_0286CC_SAMPLE_COVERAGE_ENA(1);
517 break;
518 default:
519 break;
520 }
521 break;
522 }
523 case nir_instr_type_tex: {
524 nir_tex_instr* tex = nir_instr_as_tex(instr);
525 unsigned size = tex->dest.ssa.num_components;
526
527 if (tex->dest.ssa.bit_size == 64)
528 size *= 2;
529 if (tex->op == nir_texop_texture_samples)
530 assert(!ctx->divergent_vals[tex->dest.ssa.index]);
531 if (ctx->divergent_vals[tex->dest.ssa.index])
532 allocated[tex->dest.ssa.index] = Temp(0, RegClass(RegType::vgpr, size));
533 else
534 allocated[tex->dest.ssa.index] = Temp(0, RegClass(RegType::sgpr, size));
535 break;
536 }
537 case nir_instr_type_parallel_copy: {
538 nir_foreach_parallel_copy_entry(entry, nir_instr_as_parallel_copy(instr)) {
539 allocated[entry->dest.ssa.index] = allocated[entry->src.ssa->index];
540 }
541 break;
542 }
543 case nir_instr_type_ssa_undef: {
544 unsigned num_components = nir_instr_as_ssa_undef(instr)->def.num_components;
545 unsigned bit_size = nir_instr_as_ssa_undef(instr)->def.bit_size;
546 RegClass rc = get_reg_class(ctx, RegType::sgpr, num_components, bit_size);
547 allocated[nir_instr_as_ssa_undef(instr)->def.index] = Temp(0, rc);
548 break;
549 }
550 case nir_instr_type_phi: {
551 nir_phi_instr* phi = nir_instr_as_phi(instr);
552 RegType type;
553 unsigned size = phi->dest.ssa.num_components;
554
555 if (phi->dest.ssa.bit_size == 1) {
556 assert(size == 1 && "multiple components not yet supported on boolean phis.");
557 type = RegType::sgpr;
558 size *= lane_mask_size;
559 allocated[phi->dest.ssa.index] = Temp(0, RegClass(type, size));
560 break;
561 }
562
563 if (ctx->divergent_vals[phi->dest.ssa.index]) {
564 type = RegType::vgpr;
565 } else {
566 type = RegType::sgpr;
567 nir_foreach_phi_src (src, phi) {
568 if (allocated[src->src.ssa->index].type() == RegType::vgpr)
569 type = RegType::vgpr;
570 if (allocated[src->src.ssa->index].type() == RegType::none)
571 done = false;
572 }
573 }
574
575 RegClass rc = get_reg_class(ctx, type, phi->dest.ssa.num_components, phi->dest.ssa.bit_size);
576 if (rc != allocated[phi->dest.ssa.index].regClass()) {
577 done = false;
578 } else {
579 nir_foreach_phi_src(src, phi)
580 assert(allocated[src->src.ssa->index].size() == rc.size());
581 }
582 allocated[phi->dest.ssa.index] = Temp(0, rc);
583 break;
584 }
585 default:
586 break;
587 }
588 }
589 }
590 }
591
592 if (G_0286CC_POS_W_FLOAT_ENA(spi_ps_inputs)) {
593 /* If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be enabled too */
594 spi_ps_inputs |= S_0286CC_PERSP_CENTER_ENA(1);
595 }
596
597 if (!(spi_ps_inputs & 0x7F)) {
598 /* At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled */
599 spi_ps_inputs |= S_0286CC_PERSP_CENTER_ENA(1);
600 }
601
602 ctx->program->config->spi_ps_input_ena = spi_ps_inputs;
603 ctx->program->config->spi_ps_input_addr = spi_ps_inputs;
604
605 for (unsigned i = 0; i < impl->ssa_alloc; i++)
606 allocated[i] = Temp(ctx->program->allocateId(), allocated[i].regClass());
607
608 ctx->allocated.reset(allocated.release());
609 ctx->cf_info.nir_to_aco.reset(nir_to_aco.release());
610 }
611
612 Pseudo_instruction *add_startpgm(struct isel_context *ctx)
613 {
614 unsigned arg_count = ctx->args->ac.arg_count;
615 if (ctx->stage == fragment_fs) {
616 /* LLVM optimizes away unused FS inputs and computes spi_ps_input_addr
617 * itself and then communicates the results back via the ELF binary.
618 * Mirror what LLVM does by re-mapping the VGPR arguments here.
619 *
620 * TODO: If we made the FS input scanning code into a separate pass that
621 * could run before argument setup, then this wouldn't be necessary
622 * anymore.
623 */
624 struct ac_shader_args *args = &ctx->args->ac;
625 arg_count = 0;
626 for (unsigned i = 0, vgpr_arg = 0, vgpr_reg = 0; i < args->arg_count; i++) {
627 if (args->args[i].file != AC_ARG_VGPR) {
628 arg_count++;
629 continue;
630 }
631
632 if (!(ctx->program->config->spi_ps_input_addr & (1 << vgpr_arg))) {
633 args->args[i].skip = true;
634 } else {
635 args->args[i].offset = vgpr_reg;
636 vgpr_reg += args->args[i].size;
637 arg_count++;
638 }
639 vgpr_arg++;
640 }
641 }
642
643 aco_ptr<Pseudo_instruction> startpgm{create_instruction<Pseudo_instruction>(aco_opcode::p_startpgm, Format::PSEUDO, 0, arg_count + 1)};
644 for (unsigned i = 0, arg = 0; i < ctx->args->ac.arg_count; i++) {
645 if (ctx->args->ac.args[i].skip)
646 continue;
647
648 enum ac_arg_regfile file = ctx->args->ac.args[i].file;
649 unsigned size = ctx->args->ac.args[i].size;
650 unsigned reg = ctx->args->ac.args[i].offset;
651 RegClass type = RegClass(file == AC_ARG_SGPR ? RegType::sgpr : RegType::vgpr, size);
652 Temp dst = Temp{ctx->program->allocateId(), type};
653 ctx->arg_temps[i] = dst;
654 startpgm->definitions[arg] = Definition(dst);
655 startpgm->definitions[arg].setFixed(PhysReg{file == AC_ARG_SGPR ? reg : reg + 256});
656 arg++;
657 }
658 startpgm->definitions[arg_count] = Definition{ctx->program->allocateId(), exec, ctx->program->lane_mask};
659 Pseudo_instruction *instr = startpgm.get();
660 ctx->block->instructions.push_back(std::move(startpgm));
661
662 /* Stash these in the program so that they can be accessed later when
663 * handling spilling.
664 */
665 ctx->program->private_segment_buffer = get_arg(ctx, ctx->args->ring_offsets);
666 ctx->program->scratch_offset = get_arg(ctx, ctx->args->scratch_offset);
667
668 return instr;
669 }
670
671 int
672 type_size(const struct glsl_type *type, bool bindless)
673 {
674 // TODO: don't we need type->std430_base_alignment() here?
675 return glsl_count_attribute_slots(type, false);
676 }
677
678 void
679 shared_var_info(const struct glsl_type *type, unsigned *size, unsigned *align)
680 {
681 assert(glsl_type_is_vector_or_scalar(type));
682
683 uint32_t comp_size = glsl_type_is_boolean(type)
684 ? 4 : glsl_get_bit_size(type) / 8;
685 unsigned length = glsl_get_vector_elements(type);
686 *size = comp_size * length,
687 *align = comp_size;
688 }
689
690 static bool
691 mem_vectorize_callback(unsigned align, unsigned bit_size,
692 unsigned num_components, unsigned high_offset,
693 nir_intrinsic_instr *low, nir_intrinsic_instr *high)
694 {
695 if ((bit_size != 32 && bit_size != 64) || num_components > 4)
696 return false;
697
698 /* >128 bit loads are split except with SMEM */
699 if (bit_size * num_components > 128)
700 return false;
701
702 switch (low->intrinsic) {
703 case nir_intrinsic_load_global:
704 case nir_intrinsic_store_global:
705 return align % 4 == 0;
706 case nir_intrinsic_store_ssbo:
707 if (low->src[0].ssa->bit_size < 32 || high->src[0].ssa->bit_size < 32)
708 return false;
709 return align % 4 == 0;
710 case nir_intrinsic_load_ssbo:
711 if (low->dest.ssa.bit_size < 32 || high->dest.ssa.bit_size < 32)
712 return false;
713 case nir_intrinsic_load_ubo:
714 case nir_intrinsic_load_push_constant:
715 return align % 4 == 0;
716 case nir_intrinsic_load_deref:
717 case nir_intrinsic_store_deref:
718 assert(nir_src_as_deref(low->src[0])->mode == nir_var_mem_shared);
719 /* fallthrough */
720 case nir_intrinsic_load_shared:
721 case nir_intrinsic_store_shared:
722 if (bit_size * num_components > 64) /* 96 and 128 bit loads require 128 bit alignment and are split otherwise */
723 return align % 16 == 0;
724 else
725 return align % 4 == 0;
726 default:
727 return false;
728 }
729 return false;
730 }
731
732 void
733 setup_vs_output_info(isel_context *ctx, nir_shader *nir,
734 bool export_prim_id, bool export_clip_dists,
735 radv_vs_output_info *outinfo)
736 {
737 memset(outinfo->vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
738 sizeof(outinfo->vs_output_param_offset));
739
740 outinfo->param_exports = 0;
741 int pos_written = 0x1;
742 if (outinfo->writes_pointsize || outinfo->writes_viewport_index || outinfo->writes_layer)
743 pos_written |= 1 << 1;
744
745 uint64_t mask = ctx->output_masks[nir->info.stage];
746 while (mask) {
747 int idx = u_bit_scan64(&mask);
748 if (idx >= VARYING_SLOT_VAR0 || idx == VARYING_SLOT_LAYER ||
749 idx == VARYING_SLOT_PRIMITIVE_ID || idx == VARYING_SLOT_VIEWPORT ||
750 ((idx == VARYING_SLOT_CLIP_DIST0 || idx == VARYING_SLOT_CLIP_DIST1) && export_clip_dists)) {
751 if (outinfo->vs_output_param_offset[idx] == AC_EXP_PARAM_UNDEFINED)
752 outinfo->vs_output_param_offset[idx] = outinfo->param_exports++;
753 }
754 }
755 if (outinfo->writes_layer &&
756 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] == AC_EXP_PARAM_UNDEFINED) {
757 /* when ctx->options->key.has_multiview_view_index = true, the layer
758 * variable isn't declared in NIR and it's isel's job to get the layer */
759 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] = outinfo->param_exports++;
760 }
761
762 if (export_prim_id) {
763 assert(outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] == AC_EXP_PARAM_UNDEFINED);
764 outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = outinfo->param_exports++;
765 }
766
767 ctx->export_clip_dists = export_clip_dists;
768 ctx->num_clip_distances = util_bitcount(outinfo->clip_dist_mask);
769 ctx->num_cull_distances = util_bitcount(outinfo->cull_dist_mask);
770
771 assert(ctx->num_clip_distances + ctx->num_cull_distances <= 8);
772
773 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
774 pos_written |= 1 << 2;
775 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
776 pos_written |= 1 << 3;
777
778 outinfo->pos_exports = util_bitcount(pos_written);
779 }
780
781 void
782 setup_vs_variables(isel_context *ctx, nir_shader *nir)
783 {
784 nir_foreach_variable(variable, &nir->inputs)
785 {
786 variable->data.driver_location = variable->data.location * 4;
787 }
788 nir_foreach_variable(variable, &nir->outputs)
789 {
790 if (ctx->stage == vertex_geometry_gs)
791 variable->data.driver_location = util_bitcount64(ctx->output_masks[nir->info.stage] & ((1ull << variable->data.location) - 1ull)) * 4;
792 else if (ctx->stage == vertex_es ||
793 ctx->stage == vertex_ls ||
794 ctx->stage == vertex_tess_control_hs)
795 // TODO: make this more compact
796 variable->data.driver_location = shader_io_get_unique_index((gl_varying_slot) variable->data.location) * 4;
797 else if (ctx->stage == vertex_vs || ctx->stage == ngg_vertex_gs)
798 variable->data.driver_location = variable->data.location * 4;
799 else
800 unreachable("Unsupported VS stage");
801 }
802
803 if (ctx->stage == vertex_vs || ctx->stage == ngg_vertex_gs) {
804 radv_vs_output_info *outinfo = &ctx->program->info->vs.outinfo;
805 setup_vs_output_info(ctx, nir, outinfo->export_prim_id,
806 ctx->options->key.vs_common_out.export_clip_dists, outinfo);
807 } else if (ctx->stage == vertex_geometry_gs || ctx->stage == vertex_es) {
808 /* TODO: radv_nir_shader_info_pass() already sets this but it's larger
809 * than it needs to be in order to set it better, we have to improve
810 * radv_nir_shader_info_pass() because gfx9_get_gs_info() uses
811 * esgs_itemsize and has to be done before compilation
812 */
813 /* radv_es_output_info *outinfo = &ctx->program->info->vs.es_info;
814 outinfo->esgs_itemsize = util_bitcount64(ctx->output_masks[nir->info.stage]) * 16u; */
815 } else if (ctx->stage == vertex_ls) {
816 ctx->tcs_num_inputs = util_last_bit64(ctx->args->shader_info->vs.ls_outputs_written);
817 }
818
819 if (ctx->stage == ngg_vertex_gs && ctx->args->options->key.vs_common_out.export_prim_id) {
820 /* We need to store the primitive IDs in LDS */
821 unsigned lds_size = ctx->program->info->ngg_info.esgs_ring_size;
822 ctx->program->config->lds_size = (lds_size + ctx->program->lds_alloc_granule - 1) /
823 ctx->program->lds_alloc_granule;
824 }
825 }
826
827 void setup_gs_variables(isel_context *ctx, nir_shader *nir)
828 {
829 if (ctx->stage == vertex_geometry_gs || ctx->stage == tess_eval_geometry_gs) {
830 nir_foreach_variable(variable, &nir->inputs) {
831 variable->data.driver_location = util_bitcount64(ctx->input_masks[nir->info.stage] & ((1ull << variable->data.location) - 1ull)) * 4;
832 }
833 } else if (ctx->stage == geometry_gs) {
834 //TODO: make this more compact
835 nir_foreach_variable(variable, &nir->inputs) {
836 variable->data.driver_location = shader_io_get_unique_index((gl_varying_slot)variable->data.location) * 4;
837 }
838 } else {
839 unreachable("Unsupported GS stage.");
840 }
841
842 nir_foreach_variable(variable, &nir->outputs) {
843 variable->data.driver_location = variable->data.location * 4;
844 }
845
846 if (ctx->stage == vertex_geometry_gs)
847 ctx->program->info->gs.es_type = MESA_SHADER_VERTEX;
848 else if (ctx->stage == tess_eval_geometry_gs)
849 ctx->program->info->gs.es_type = MESA_SHADER_TESS_EVAL;
850 }
851
852 void
853 setup_tcs_info(isel_context *ctx, nir_shader *nir)
854 {
855 /* When the number of TCS input and output vertices are the same (typically 3):
856 * - There is an equal amount of LS and HS invocations
857 * - In case of merged LSHS shaders, the LS and HS halves of the shader
858 * always process the exact same vertex. We can use this knowledge to optimize them.
859 */
860 ctx->tcs_in_out_eq =
861 ctx->stage == vertex_tess_control_hs &&
862 ctx->args->options->key.tcs.input_vertices == nir->info.tess.tcs_vertices_out;
863
864 if (ctx->stage == tess_control_hs) {
865 ctx->tcs_num_inputs = ctx->args->options->key.tcs.num_inputs;
866 } else if (ctx->stage == vertex_tess_control_hs) {
867 ctx->tcs_num_inputs = util_last_bit64(ctx->args->shader_info->vs.ls_outputs_written);
868
869 if (ctx->tcs_in_out_eq) {
870 ctx->tcs_temp_only_inputs = ~nir->info.tess.tcs_cross_invocation_inputs_read &
871 ~nir->info.inputs_read_indirectly &
872 nir->info.inputs_read;
873 }
874 } else {
875 unreachable("Unsupported TCS shader stage");
876 }
877
878 ctx->tcs_num_outputs = util_last_bit64(ctx->args->shader_info->tcs.outputs_written);
879 ctx->tcs_num_patch_outputs = util_last_bit64(ctx->args->shader_info->tcs.patch_outputs_written);
880
881 ctx->tcs_num_patches = get_tcs_num_patches(
882 ctx->args->options->key.tcs.input_vertices,
883 nir->info.tess.tcs_vertices_out,
884 ctx->tcs_num_inputs,
885 ctx->tcs_num_outputs,
886 ctx->tcs_num_patch_outputs,
887 ctx->args->options->tess_offchip_block_dw_size,
888 ctx->args->options->chip_class,
889 ctx->args->options->family);
890 unsigned lds_size = calculate_tess_lds_size(
891 ctx->args->options->key.tcs.input_vertices,
892 nir->info.tess.tcs_vertices_out,
893 ctx->tcs_num_inputs,
894 ctx->tcs_num_patches,
895 ctx->tcs_num_outputs,
896 ctx->tcs_num_patch_outputs);
897
898 ctx->args->shader_info->tcs.num_patches = ctx->tcs_num_patches;
899 ctx->args->shader_info->tcs.lds_size = lds_size;
900 ctx->program->config->lds_size = (lds_size + ctx->program->lds_alloc_granule - 1) /
901 ctx->program->lds_alloc_granule;
902 }
903
904 void
905 setup_tcs_variables(isel_context *ctx, nir_shader *nir)
906 {
907 nir_foreach_variable(variable, &nir->inputs) {
908 variable->data.driver_location = shader_io_get_unique_index((gl_varying_slot) variable->data.location) * 4;
909 }
910
911 nir_foreach_variable(variable, &nir->outputs) {
912 variable->data.driver_location = shader_io_get_unique_index((gl_varying_slot) variable->data.location) * 4;
913 }
914
915 ctx->tcs_tess_lvl_out_loc = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER) * 16u;
916 ctx->tcs_tess_lvl_in_loc = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER) * 16u;
917 }
918
919 void
920 setup_tes_variables(isel_context *ctx, nir_shader *nir)
921 {
922 ctx->tcs_num_patches = ctx->args->options->key.tes.num_patches;
923 ctx->tcs_num_outputs = ctx->args->options->key.tes.tcs_num_outputs;
924
925 nir_foreach_variable(variable, &nir->inputs) {
926 variable->data.driver_location = shader_io_get_unique_index((gl_varying_slot) variable->data.location) * 4;
927 }
928
929 nir_foreach_variable(variable, &nir->outputs) {
930 if (ctx->stage == tess_eval_vs || ctx->stage == ngg_tess_eval_gs)
931 variable->data.driver_location = variable->data.location * 4;
932 else if (ctx->stage == tess_eval_es)
933 variable->data.driver_location = shader_io_get_unique_index((gl_varying_slot) variable->data.location) * 4;
934 else if (ctx->stage == tess_eval_geometry_gs)
935 variable->data.driver_location = util_bitcount64(ctx->output_masks[nir->info.stage] & ((1ull << variable->data.location) - 1ull)) * 4;
936 else
937 unreachable("Unsupported TES shader stage");
938 }
939
940 if (ctx->stage == tess_eval_vs || ctx->stage == ngg_tess_eval_gs) {
941 radv_vs_output_info *outinfo = &ctx->program->info->tes.outinfo;
942 setup_vs_output_info(ctx, nir, outinfo->export_prim_id,
943 ctx->options->key.vs_common_out.export_clip_dists, outinfo);
944 }
945 }
946
947 void
948 setup_variables(isel_context *ctx, nir_shader *nir)
949 {
950 switch (nir->info.stage) {
951 case MESA_SHADER_FRAGMENT: {
952 nir_foreach_variable(variable, &nir->outputs)
953 {
954 int idx = variable->data.location + variable->data.index;
955 variable->data.driver_location = idx * 4;
956 }
957 break;
958 }
959 case MESA_SHADER_COMPUTE: {
960 ctx->program->config->lds_size = (nir->info.cs.shared_size + ctx->program->lds_alloc_granule - 1) /
961 ctx->program->lds_alloc_granule;
962 break;
963 }
964 case MESA_SHADER_VERTEX: {
965 setup_vs_variables(ctx, nir);
966 break;
967 }
968 case MESA_SHADER_GEOMETRY: {
969 setup_gs_variables(ctx, nir);
970 break;
971 }
972 case MESA_SHADER_TESS_CTRL: {
973 setup_tcs_variables(ctx, nir);
974 break;
975 }
976 case MESA_SHADER_TESS_EVAL: {
977 setup_tes_variables(ctx, nir);
978 break;
979 }
980 default:
981 unreachable("Unhandled shader stage.");
982 }
983 }
984
985 void
986 get_io_masks(isel_context *ctx, unsigned shader_count, struct nir_shader *const *shaders)
987 {
988 for (unsigned i = 0; i < shader_count; i++) {
989 nir_shader *nir = shaders[i];
990 if (nir->info.stage == MESA_SHADER_COMPUTE)
991 continue;
992
993 uint64_t output_mask = 0;
994 nir_foreach_variable(variable, &nir->outputs) {
995 const glsl_type *type = variable->type;
996 if (nir_is_per_vertex_io(variable, nir->info.stage))
997 type = type->fields.array;
998 unsigned slots = type->count_attribute_slots(false);
999 if (variable->data.compact) {
1000 unsigned component_count = variable->data.location_frac + type->length;
1001 slots = (component_count + 3) / 4;
1002 }
1003 output_mask |= ((1ull << slots) - 1) << variable->data.location;
1004 }
1005
1006 uint64_t input_mask = 0;
1007 nir_foreach_variable(variable, &nir->inputs) {
1008 const glsl_type *type = variable->type;
1009 if (nir_is_per_vertex_io(variable, nir->info.stage))
1010 type = type->fields.array;
1011 unsigned slots = type->count_attribute_slots(false);
1012 if (variable->data.compact) {
1013 unsigned component_count = variable->data.location_frac + type->length;
1014 slots = (component_count + 3) / 4;
1015 }
1016 input_mask |= ((1ull << slots) - 1) << variable->data.location;
1017 }
1018
1019 ctx->output_masks[nir->info.stage] |= output_mask;
1020 if (i + 1 < shader_count)
1021 ctx->input_masks[shaders[i + 1]->info.stage] |= output_mask;
1022
1023 ctx->input_masks[nir->info.stage] |= input_mask;
1024 if (i)
1025 ctx->output_masks[shaders[i - 1]->info.stage] |= input_mask;
1026 }
1027 }
1028
1029 unsigned
1030 lower_bit_size_callback(const nir_alu_instr *alu, void *_)
1031 {
1032 if (nir_op_is_vec(alu->op))
1033 return 0;
1034
1035 unsigned bit_size = alu->dest.dest.ssa.bit_size;
1036 if (nir_alu_instr_is_comparison(alu))
1037 bit_size = nir_src_bit_size(alu->src[0].src);
1038
1039 if (bit_size >= 32 || bit_size == 1)
1040 return 0;
1041
1042 if (alu->op == nir_op_bcsel)
1043 return 0;
1044
1045 const nir_op_info *info = &nir_op_infos[alu->op];
1046
1047 if (info->is_conversion)
1048 return 0;
1049
1050 bool is_integer = info->output_type & (nir_type_uint | nir_type_int);
1051 for (unsigned i = 0; is_integer && (i < info->num_inputs); i++)
1052 is_integer = info->input_types[i] & (nir_type_uint | nir_type_int);
1053
1054 return is_integer ? 32 : 0;
1055 }
1056
1057 void
1058 setup_nir(isel_context *ctx, nir_shader *nir)
1059 {
1060 Program *program = ctx->program;
1061
1062 /* align and copy constant data */
1063 while (program->constant_data.size() % 4u)
1064 program->constant_data.push_back(0);
1065 ctx->constant_data_offset = program->constant_data.size();
1066 program->constant_data.insert(program->constant_data.end(),
1067 (uint8_t*)nir->constant_data,
1068 (uint8_t*)nir->constant_data + nir->constant_data_size);
1069
1070 /* the variable setup has to be done before lower_io / CSE */
1071 setup_variables(ctx, nir);
1072
1073 /* optimize and lower memory operations */
1074 if (nir_lower_explicit_io(nir, nir_var_mem_global, nir_address_format_64bit_global)) {
1075 nir_opt_constant_folding(nir);
1076 nir_opt_cse(nir);
1077 }
1078
1079 bool lower_to_scalar = false;
1080 bool lower_pack = false;
1081 if (nir_opt_load_store_vectorize(nir,
1082 (nir_variable_mode)(nir_var_mem_ssbo | nir_var_mem_ubo |
1083 nir_var_mem_push_const | nir_var_mem_shared |
1084 nir_var_mem_global),
1085 mem_vectorize_callback)) {
1086 lower_to_scalar = true;
1087 lower_pack = true;
1088 }
1089 if (nir->info.stage != MESA_SHADER_COMPUTE)
1090 nir_lower_io(nir, (nir_variable_mode)(nir_var_shader_in | nir_var_shader_out), type_size, (nir_lower_io_options)0);
1091
1092 if (lower_to_scalar)
1093 nir_lower_alu_to_scalar(nir, NULL, NULL);
1094 if (lower_pack)
1095 nir_lower_pack(nir);
1096
1097 /* lower ALU operations */
1098 // TODO: implement logic64 in aco, it's more effective for sgprs
1099 nir_lower_int64(nir, nir->options->lower_int64_options);
1100
1101 if (nir_lower_bit_size(nir, lower_bit_size_callback, NULL))
1102 nir_copy_prop(nir); /* allow nir_opt_idiv_const() to optimize lowered divisions */
1103
1104 nir_opt_idiv_const(nir, 32);
1105 nir_lower_idiv(nir, nir_lower_idiv_precise);
1106
1107 /* optimize the lowered ALU operations */
1108 bool more_algebraic = true;
1109 while (more_algebraic) {
1110 more_algebraic = false;
1111 NIR_PASS_V(nir, nir_copy_prop);
1112 NIR_PASS_V(nir, nir_opt_dce);
1113 NIR_PASS_V(nir, nir_opt_constant_folding);
1114 NIR_PASS(more_algebraic, nir, nir_opt_algebraic);
1115 }
1116
1117 /* Do late algebraic optimization to turn add(a, neg(b)) back into
1118 * subs, then the mandatory cleanup after algebraic. Note that it may
1119 * produce fnegs, and if so then we need to keep running to squash
1120 * fneg(fneg(a)).
1121 */
1122 bool more_late_algebraic = true;
1123 while (more_late_algebraic) {
1124 more_late_algebraic = false;
1125 NIR_PASS(more_late_algebraic, nir, nir_opt_algebraic_late);
1126 NIR_PASS_V(nir, nir_opt_constant_folding);
1127 NIR_PASS_V(nir, nir_copy_prop);
1128 NIR_PASS_V(nir, nir_opt_dce);
1129 NIR_PASS_V(nir, nir_opt_cse);
1130 }
1131
1132 /* cleanup passes */
1133 nir_lower_load_const_to_scalar(nir);
1134 nir_opt_shrink_load(nir);
1135 nir_move_options move_opts = (nir_move_options)(
1136 nir_move_const_undef | nir_move_load_ubo | nir_move_load_input |
1137 nir_move_comparisons | nir_move_copies);
1138 nir_opt_sink(nir, move_opts);
1139 nir_opt_move(nir, move_opts);
1140 nir_convert_to_lcssa(nir, true, false);
1141 nir_lower_phis_to_scalar(nir);
1142
1143 nir_function_impl *func = nir_shader_get_entrypoint(nir);
1144 nir_index_ssa_defs(func);
1145 }
1146
1147 void
1148 setup_xnack(Program *program)
1149 {
1150 switch (program->family) {
1151 /* GFX8 APUs */
1152 case CHIP_CARRIZO:
1153 case CHIP_STONEY:
1154 /* GFX9 APUS */
1155 case CHIP_RAVEN:
1156 case CHIP_RAVEN2:
1157 case CHIP_RENOIR:
1158 program->xnack_enabled = true;
1159 break;
1160 default:
1161 break;
1162 }
1163 }
1164
1165 isel_context
1166 setup_isel_context(Program* program,
1167 unsigned shader_count,
1168 struct nir_shader *const *shaders,
1169 ac_shader_config* config,
1170 struct radv_shader_args *args,
1171 bool is_gs_copy_shader)
1172 {
1173 program->stage = 0;
1174 for (unsigned i = 0; i < shader_count; i++) {
1175 switch (shaders[i]->info.stage) {
1176 case MESA_SHADER_VERTEX:
1177 program->stage |= sw_vs;
1178 break;
1179 case MESA_SHADER_TESS_CTRL:
1180 program->stage |= sw_tcs;
1181 break;
1182 case MESA_SHADER_TESS_EVAL:
1183 program->stage |= sw_tes;
1184 break;
1185 case MESA_SHADER_GEOMETRY:
1186 program->stage |= is_gs_copy_shader ? sw_gs_copy : sw_gs;
1187 break;
1188 case MESA_SHADER_FRAGMENT:
1189 program->stage |= sw_fs;
1190 break;
1191 case MESA_SHADER_COMPUTE:
1192 program->stage |= sw_cs;
1193 break;
1194 default:
1195 unreachable("Shader stage not implemented");
1196 }
1197 }
1198 bool gfx9_plus = args->options->chip_class >= GFX9;
1199 bool ngg = args->shader_info->is_ngg && args->options->chip_class >= GFX10;
1200 if (program->stage == sw_vs && args->shader_info->vs.as_es && !ngg)
1201 program->stage |= hw_es;
1202 else if (program->stage == sw_vs && !args->shader_info->vs.as_ls && !ngg)
1203 program->stage |= hw_vs;
1204 else if (program->stage == sw_vs && ngg)
1205 program->stage |= hw_ngg_gs; /* GFX10/NGG: VS without GS uses the HW GS stage */
1206 else if (program->stage == sw_gs)
1207 program->stage |= hw_gs;
1208 else if (program->stage == sw_fs)
1209 program->stage |= hw_fs;
1210 else if (program->stage == sw_cs)
1211 program->stage |= hw_cs;
1212 else if (program->stage == sw_gs_copy)
1213 program->stage |= hw_vs;
1214 else if (program->stage == (sw_vs | sw_gs) && gfx9_plus && !ngg)
1215 program->stage |= hw_gs;
1216 else if (program->stage == sw_vs && args->shader_info->vs.as_ls)
1217 program->stage |= hw_ls; /* GFX6-8: VS is a Local Shader, when tessellation is used */
1218 else if (program->stage == sw_tcs)
1219 program->stage |= hw_hs; /* GFX6-8: TCS is a Hull Shader */
1220 else if (program->stage == (sw_vs | sw_tcs))
1221 program->stage |= hw_hs; /* GFX9-10: VS+TCS merged into a Hull Shader */
1222 else if (program->stage == sw_tes && !args->shader_info->tes.as_es && !ngg)
1223 program->stage |= hw_vs; /* GFX6-9: TES without GS uses the HW VS stage (and GFX10/legacy) */
1224 else if (program->stage == sw_tes && !args->shader_info->tes.as_es && ngg)
1225 program->stage |= hw_ngg_gs; /* GFX10/NGG: TES without GS uses the HW GS stage */
1226 else if (program->stage == sw_tes && args->shader_info->tes.as_es && !ngg)
1227 program->stage |= hw_es; /* GFX6-8: TES is an Export Shader */
1228 else if (program->stage == (sw_tes | sw_gs) && gfx9_plus && !ngg)
1229 program->stage |= hw_gs; /* GFX9: TES+GS merged into a GS (and GFX10/legacy) */
1230 else
1231 unreachable("Shader stage not implemented");
1232
1233 program->config = config;
1234 program->info = args->shader_info;
1235 program->chip_class = args->options->chip_class;
1236 program->family = args->options->family;
1237 program->wave_size = args->shader_info->wave_size;
1238 program->lane_mask = program->wave_size == 32 ? s1 : s2;
1239
1240 program->lds_alloc_granule = args->options->chip_class >= GFX7 ? 512 : 256;
1241 program->lds_limit = args->options->chip_class >= GFX7 ? 65536 : 32768;
1242 /* apparently gfx702 also has 16-bank LDS but I can't find a family for that */
1243 program->has_16bank_lds = args->options->family == CHIP_KABINI || args->options->family == CHIP_STONEY;
1244
1245 program->vgpr_limit = 256;
1246 program->vgpr_alloc_granule = 3;
1247
1248 if (args->options->chip_class >= GFX10) {
1249 program->physical_sgprs = 2560; /* doesn't matter as long as it's at least 128 * 20 */
1250 program->sgpr_alloc_granule = 127;
1251 program->sgpr_limit = 106;
1252 program->vgpr_alloc_granule = program->wave_size == 32 ? 7 : 3;
1253 } else if (program->chip_class >= GFX8) {
1254 program->physical_sgprs = 800;
1255 program->sgpr_alloc_granule = 15;
1256 if (args->options->family == CHIP_TONGA || args->options->family == CHIP_ICELAND)
1257 program->sgpr_limit = 94; /* workaround hardware bug */
1258 else
1259 program->sgpr_limit = 102;
1260 } else {
1261 program->physical_sgprs = 512;
1262 program->sgpr_alloc_granule = 7;
1263 program->sgpr_limit = 104;
1264 }
1265
1266 isel_context ctx = {};
1267 ctx.program = program;
1268 ctx.args = args;
1269 ctx.options = args->options;
1270 ctx.stage = program->stage;
1271
1272 /* TODO: Check if we need to adjust min_waves for unknown workgroup sizes. */
1273 if (program->stage & (hw_vs | hw_fs)) {
1274 /* PS and legacy VS have separate waves, no workgroups */
1275 program->workgroup_size = program->wave_size;
1276 } else if (program->stage == compute_cs) {
1277 /* CS sets the workgroup size explicitly */
1278 unsigned* bsize = program->info->cs.block_size;
1279 program->workgroup_size = bsize[0] * bsize[1] * bsize[2];
1280 } else if ((program->stage & hw_es) || program->stage == geometry_gs) {
1281 /* Unmerged ESGS operate in workgroups if on-chip GS (LDS rings) are enabled on GFX7-8 (not implemented in Mesa) */
1282 program->workgroup_size = program->wave_size;
1283 } else if (program->stage & hw_gs) {
1284 /* If on-chip GS (LDS rings) are enabled on GFX9 or later, merged GS operates in workgroups */
1285 program->workgroup_size = UINT_MAX; /* TODO: set by VGT_GS_ONCHIP_CNTL, which is not plumbed to ACO */
1286 } else if (program->stage == vertex_ls) {
1287 /* Unmerged LS operates in workgroups */
1288 program->workgroup_size = UINT_MAX; /* TODO: probably tcs_num_patches * tcs_vertices_in, but those are not plumbed to ACO for LS */
1289 } else if (program->stage == tess_control_hs) {
1290 /* Unmerged HS operates in workgroups, size is determined by the output vertices */
1291 setup_tcs_info(&ctx, shaders[0]);
1292 program->workgroup_size = ctx.tcs_num_patches * shaders[0]->info.tess.tcs_vertices_out;
1293 } else if (program->stage == vertex_tess_control_hs) {
1294 /* Merged LSHS operates in workgroups, but can still have a different number of LS and HS invocations */
1295 setup_tcs_info(&ctx, shaders[1]);
1296 program->workgroup_size = ctx.tcs_num_patches * MAX2(shaders[1]->info.tess.tcs_vertices_out, ctx.args->options->key.tcs.input_vertices);
1297 } else if (program->stage & hw_ngg_gs) {
1298 /* TODO: Calculate workgroup size of NGG shaders. */
1299 program->workgroup_size = UINT_MAX;
1300 } else {
1301 unreachable("Unsupported shader stage.");
1302 }
1303
1304 calc_min_waves(program);
1305 program->vgpr_limit = get_addr_vgpr_from_waves(program, program->min_waves);
1306 program->sgpr_limit = get_addr_sgpr_from_waves(program, program->min_waves);
1307
1308 get_io_masks(&ctx, shader_count, shaders);
1309
1310 unsigned scratch_size = 0;
1311 if (program->stage == gs_copy_vs) {
1312 assert(shader_count == 1);
1313 setup_vs_output_info(&ctx, shaders[0], false, true, &args->shader_info->vs.outinfo);
1314 } else {
1315 for (unsigned i = 0; i < shader_count; i++) {
1316 nir_shader *nir = shaders[i];
1317 setup_nir(&ctx, nir);
1318 }
1319
1320 for (unsigned i = 0; i < shader_count; i++)
1321 scratch_size = std::max(scratch_size, shaders[i]->scratch_size);
1322 }
1323
1324 ctx.program->config->scratch_bytes_per_wave = align(scratch_size * ctx.program->wave_size, 1024);
1325
1326 ctx.block = ctx.program->create_and_insert_block();
1327 ctx.block->loop_nest_depth = 0;
1328 ctx.block->kind = block_kind_top_level;
1329
1330 setup_xnack(program);
1331
1332 return ctx;
1333 }
1334
1335 }