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