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