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