radv/aco,aco: implement GS on GFX9+
[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 "vulkan/radv_shader.h"
30 #include "vulkan/radv_descriptor_set.h"
31 #include "vulkan/radv_shader_args.h"
32 #include "sid.h"
33 #include "ac_exp_param.h"
34 #include "ac_shader_util.h"
35
36 #include "util/u_math.h"
37
38 #define MAX_INLINE_PUSH_CONSTS 8
39
40 namespace aco {
41
42 struct ge_output_state {
43 uint8_t mask[VARYING_SLOT_VAR31 + 1];
44 Temp outputs[VARYING_SLOT_VAR31 + 1][4];
45 };
46
47 struct isel_context {
48 const struct radv_nir_compiler_options *options;
49 struct radv_shader_args *args;
50 Program *program;
51 nir_shader *shader;
52 uint32_t constant_data_offset;
53 Block *block;
54 bool *divergent_vals;
55 std::unique_ptr<Temp[]> allocated;
56 std::unordered_map<unsigned, std::array<Temp,NIR_MAX_VEC_COMPONENTS>> allocated_vec;
57 Stage stage; /* Stage */
58 bool has_gfx10_wave64_bpermute = false;
59 struct {
60 bool has_branch;
61 uint16_t loop_nest_depth = 0;
62 struct {
63 unsigned header_idx;
64 Block* exit;
65 bool has_divergent_continue = false;
66 bool has_divergent_branch = false;
67 } parent_loop;
68 struct {
69 bool is_divergent = false;
70 } parent_if;
71 bool exec_potentially_empty = false;
72 std::unique_ptr<unsigned[]> nir_to_aco; /* NIR block index to ACO block index */
73 } cf_info;
74
75 Temp arg_temps[AC_MAX_ARGS];
76
77 /* FS inputs */
78 Temp persp_centroid, linear_centroid;
79
80 /* VS inputs */
81 bool needs_instance_id;
82
83 /* gathered information */
84 uint64_t input_masks[MESA_SHADER_COMPUTE];
85 uint64_t output_masks[MESA_SHADER_COMPUTE];
86
87 /* VS output information */
88 unsigned num_clip_distances;
89 unsigned num_cull_distances;
90
91 /* VS or GS output information */
92 ge_output_state vsgs_output;
93 };
94
95 Temp get_arg(isel_context *ctx, struct ac_arg arg)
96 {
97 assert(arg.used);
98 return ctx->arg_temps[arg.arg_index];
99 }
100
101 unsigned get_interp_input(nir_intrinsic_op intrin, enum glsl_interp_mode interp)
102 {
103 switch (interp) {
104 case INTERP_MODE_SMOOTH:
105 case INTERP_MODE_NONE:
106 if (intrin == nir_intrinsic_load_barycentric_pixel ||
107 intrin == nir_intrinsic_load_barycentric_at_sample ||
108 intrin == nir_intrinsic_load_barycentric_at_offset)
109 return S_0286CC_PERSP_CENTER_ENA(1);
110 else if (intrin == nir_intrinsic_load_barycentric_centroid)
111 return S_0286CC_PERSP_CENTROID_ENA(1);
112 else if (intrin == nir_intrinsic_load_barycentric_sample)
113 return S_0286CC_PERSP_SAMPLE_ENA(1);
114 break;
115 case INTERP_MODE_NOPERSPECTIVE:
116 if (intrin == nir_intrinsic_load_barycentric_pixel)
117 return S_0286CC_LINEAR_CENTER_ENA(1);
118 else if (intrin == nir_intrinsic_load_barycentric_centroid)
119 return S_0286CC_LINEAR_CENTROID_ENA(1);
120 else if (intrin == nir_intrinsic_load_barycentric_sample)
121 return S_0286CC_LINEAR_SAMPLE_ENA(1);
122 break;
123 default:
124 break;
125 }
126 return 0;
127 }
128
129 void init_context(isel_context *ctx, nir_shader *shader)
130 {
131 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
132 unsigned lane_mask_size = ctx->program->lane_mask.size();
133
134 ctx->shader = shader;
135 ctx->divergent_vals = nir_divergence_analysis(shader, nir_divergence_view_index_uniform);
136
137 std::unique_ptr<Temp[]> allocated{new Temp[impl->ssa_alloc]()};
138
139 unsigned spi_ps_inputs = 0;
140
141 std::unique_ptr<unsigned[]> nir_to_aco{new unsigned[impl->num_blocks]()};
142
143 bool done = false;
144 while (!done) {
145 done = true;
146 nir_foreach_block(block, impl) {
147 nir_foreach_instr(instr, block) {
148 switch(instr->type) {
149 case nir_instr_type_alu: {
150 nir_alu_instr *alu_instr = nir_instr_as_alu(instr);
151 unsigned size = alu_instr->dest.dest.ssa.num_components;
152 if (alu_instr->dest.dest.ssa.bit_size == 64)
153 size *= 2;
154 RegType type = RegType::sgpr;
155 switch(alu_instr->op) {
156 case nir_op_fmul:
157 case nir_op_fadd:
158 case nir_op_fsub:
159 case nir_op_fmax:
160 case nir_op_fmin:
161 case nir_op_fmax3:
162 case nir_op_fmin3:
163 case nir_op_fmed3:
164 case nir_op_fneg:
165 case nir_op_fabs:
166 case nir_op_fsat:
167 case nir_op_fsign:
168 case nir_op_frcp:
169 case nir_op_frsq:
170 case nir_op_fsqrt:
171 case nir_op_fexp2:
172 case nir_op_flog2:
173 case nir_op_ffract:
174 case nir_op_ffloor:
175 case nir_op_fceil:
176 case nir_op_ftrunc:
177 case nir_op_fround_even:
178 case nir_op_fsin:
179 case nir_op_fcos:
180 case nir_op_f2f32:
181 case nir_op_f2f64:
182 case nir_op_u2f32:
183 case nir_op_u2f64:
184 case nir_op_i2f32:
185 case nir_op_i2f64:
186 case nir_op_pack_half_2x16:
187 case nir_op_unpack_half_2x16_split_x:
188 case nir_op_unpack_half_2x16_split_y:
189 case nir_op_fddx:
190 case nir_op_fddy:
191 case nir_op_fddx_fine:
192 case nir_op_fddy_fine:
193 case nir_op_fddx_coarse:
194 case nir_op_fddy_coarse:
195 case nir_op_fquantize2f16:
196 case nir_op_ldexp:
197 case nir_op_frexp_sig:
198 case nir_op_frexp_exp:
199 case nir_op_cube_face_index:
200 case nir_op_cube_face_coord:
201 type = RegType::vgpr;
202 break;
203 case nir_op_flt:
204 case nir_op_fge:
205 case nir_op_feq:
206 case nir_op_fne:
207 case nir_op_ilt:
208 case nir_op_ige:
209 case nir_op_ult:
210 case nir_op_uge:
211 case nir_op_ieq:
212 case nir_op_ine:
213 case nir_op_i2b1:
214 size = lane_mask_size;
215 break;
216 case nir_op_f2i64:
217 case nir_op_f2u64:
218 case nir_op_b2i32:
219 case nir_op_b2f32:
220 case nir_op_f2i32:
221 case nir_op_f2u32:
222 type = ctx->divergent_vals[alu_instr->dest.dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
223 break;
224 case nir_op_bcsel:
225 if (alu_instr->dest.dest.ssa.bit_size == 1) {
226 size = lane_mask_size;
227 } else {
228 if (ctx->divergent_vals[alu_instr->dest.dest.ssa.index]) {
229 type = RegType::vgpr;
230 } else {
231 if (allocated[alu_instr->src[1].src.ssa->index].type() == RegType::vgpr ||
232 allocated[alu_instr->src[2].src.ssa->index].type() == RegType::vgpr) {
233 type = RegType::vgpr;
234 }
235 }
236 if (alu_instr->src[1].src.ssa->num_components == 1 && alu_instr->src[2].src.ssa->num_components == 1) {
237 assert(allocated[alu_instr->src[1].src.ssa->index].size() == allocated[alu_instr->src[2].src.ssa->index].size());
238 size = allocated[alu_instr->src[1].src.ssa->index].size();
239 }
240 }
241 break;
242 case nir_op_mov:
243 if (alu_instr->dest.dest.ssa.bit_size == 1) {
244 size = lane_mask_size;
245 } else {
246 type = ctx->divergent_vals[alu_instr->dest.dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
247 }
248 break;
249 default:
250 if (alu_instr->dest.dest.ssa.bit_size == 1) {
251 size = lane_mask_size;
252 } else {
253 for (unsigned i = 0; i < nir_op_infos[alu_instr->op].num_inputs; i++) {
254 if (allocated[alu_instr->src[i].src.ssa->index].type() == RegType::vgpr)
255 type = RegType::vgpr;
256 }
257 }
258 break;
259 }
260 allocated[alu_instr->dest.dest.ssa.index] = Temp(0, RegClass(type, size));
261 break;
262 }
263 case nir_instr_type_load_const: {
264 unsigned size = nir_instr_as_load_const(instr)->def.num_components;
265 if (nir_instr_as_load_const(instr)->def.bit_size == 64)
266 size *= 2;
267 else if (nir_instr_as_load_const(instr)->def.bit_size == 1)
268 size *= lane_mask_size;
269 allocated[nir_instr_as_load_const(instr)->def.index] = Temp(0, RegClass(RegType::sgpr, size));
270 break;
271 }
272 case nir_instr_type_intrinsic: {
273 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
274 if (!nir_intrinsic_infos[intrinsic->intrinsic].has_dest)
275 break;
276 unsigned size = intrinsic->dest.ssa.num_components;
277 if (intrinsic->dest.ssa.bit_size == 64)
278 size *= 2;
279 RegType type = RegType::sgpr;
280 switch(intrinsic->intrinsic) {
281 case nir_intrinsic_load_push_constant:
282 case nir_intrinsic_load_work_group_id:
283 case nir_intrinsic_load_num_work_groups:
284 case nir_intrinsic_load_subgroup_id:
285 case nir_intrinsic_load_num_subgroups:
286 case nir_intrinsic_load_first_vertex:
287 case nir_intrinsic_load_base_instance:
288 case nir_intrinsic_get_buffer_size:
289 case nir_intrinsic_vote_all:
290 case nir_intrinsic_vote_any:
291 case nir_intrinsic_read_first_invocation:
292 case nir_intrinsic_read_invocation:
293 case nir_intrinsic_first_invocation:
294 type = RegType::sgpr;
295 if (intrinsic->dest.ssa.bit_size == 1)
296 size = lane_mask_size;
297 break;
298 case nir_intrinsic_ballot:
299 type = RegType::sgpr;
300 break;
301 case nir_intrinsic_load_sample_id:
302 case nir_intrinsic_load_sample_mask_in:
303 case nir_intrinsic_load_input:
304 case nir_intrinsic_load_per_vertex_input:
305 case nir_intrinsic_load_vertex_id:
306 case nir_intrinsic_load_vertex_id_zero_base:
307 case nir_intrinsic_load_barycentric_sample:
308 case nir_intrinsic_load_barycentric_pixel:
309 case nir_intrinsic_load_barycentric_centroid:
310 case nir_intrinsic_load_barycentric_at_sample:
311 case nir_intrinsic_load_barycentric_at_offset:
312 case nir_intrinsic_load_interpolated_input:
313 case nir_intrinsic_load_frag_coord:
314 case nir_intrinsic_load_sample_pos:
315 case nir_intrinsic_load_layer_id:
316 case nir_intrinsic_load_local_invocation_id:
317 case nir_intrinsic_load_local_invocation_index:
318 case nir_intrinsic_load_subgroup_invocation:
319 case nir_intrinsic_write_invocation_amd:
320 case nir_intrinsic_mbcnt_amd:
321 case nir_intrinsic_load_instance_id:
322 case nir_intrinsic_ssbo_atomic_add:
323 case nir_intrinsic_ssbo_atomic_imin:
324 case nir_intrinsic_ssbo_atomic_umin:
325 case nir_intrinsic_ssbo_atomic_imax:
326 case nir_intrinsic_ssbo_atomic_umax:
327 case nir_intrinsic_ssbo_atomic_and:
328 case nir_intrinsic_ssbo_atomic_or:
329 case nir_intrinsic_ssbo_atomic_xor:
330 case nir_intrinsic_ssbo_atomic_exchange:
331 case nir_intrinsic_ssbo_atomic_comp_swap:
332 case nir_intrinsic_global_atomic_add:
333 case nir_intrinsic_global_atomic_imin:
334 case nir_intrinsic_global_atomic_umin:
335 case nir_intrinsic_global_atomic_imax:
336 case nir_intrinsic_global_atomic_umax:
337 case nir_intrinsic_global_atomic_and:
338 case nir_intrinsic_global_atomic_or:
339 case nir_intrinsic_global_atomic_xor:
340 case nir_intrinsic_global_atomic_exchange:
341 case nir_intrinsic_global_atomic_comp_swap:
342 case nir_intrinsic_image_deref_atomic_add:
343 case nir_intrinsic_image_deref_atomic_umin:
344 case nir_intrinsic_image_deref_atomic_imin:
345 case nir_intrinsic_image_deref_atomic_umax:
346 case nir_intrinsic_image_deref_atomic_imax:
347 case nir_intrinsic_image_deref_atomic_and:
348 case nir_intrinsic_image_deref_atomic_or:
349 case nir_intrinsic_image_deref_atomic_xor:
350 case nir_intrinsic_image_deref_atomic_exchange:
351 case nir_intrinsic_image_deref_atomic_comp_swap:
352 case nir_intrinsic_image_deref_size:
353 case nir_intrinsic_shared_atomic_add:
354 case nir_intrinsic_shared_atomic_imin:
355 case nir_intrinsic_shared_atomic_umin:
356 case nir_intrinsic_shared_atomic_imax:
357 case nir_intrinsic_shared_atomic_umax:
358 case nir_intrinsic_shared_atomic_and:
359 case nir_intrinsic_shared_atomic_or:
360 case nir_intrinsic_shared_atomic_xor:
361 case nir_intrinsic_shared_atomic_exchange:
362 case nir_intrinsic_shared_atomic_comp_swap:
363 case nir_intrinsic_load_scratch:
364 case nir_intrinsic_load_invocation_id:
365 case nir_intrinsic_load_primitive_id:
366 type = RegType::vgpr;
367 break;
368 case nir_intrinsic_shuffle:
369 case nir_intrinsic_quad_broadcast:
370 case nir_intrinsic_quad_swap_horizontal:
371 case nir_intrinsic_quad_swap_vertical:
372 case nir_intrinsic_quad_swap_diagonal:
373 case nir_intrinsic_quad_swizzle_amd:
374 case nir_intrinsic_masked_swizzle_amd:
375 case nir_intrinsic_inclusive_scan:
376 case nir_intrinsic_exclusive_scan:
377 if (intrinsic->dest.ssa.bit_size == 1) {
378 size = lane_mask_size;
379 type = RegType::sgpr;
380 } else if (!ctx->divergent_vals[intrinsic->dest.ssa.index]) {
381 type = RegType::sgpr;
382 } else {
383 type = RegType::vgpr;
384 }
385 break;
386 case nir_intrinsic_load_view_index:
387 type = ctx->stage == fragment_fs ? RegType::vgpr : RegType::sgpr;
388 break;
389 case nir_intrinsic_load_front_face:
390 case nir_intrinsic_load_helper_invocation:
391 case nir_intrinsic_is_helper_invocation:
392 type = RegType::sgpr;
393 size = lane_mask_size;
394 break;
395 case nir_intrinsic_reduce:
396 if (intrinsic->dest.ssa.bit_size == 1) {
397 size = lane_mask_size;
398 type = RegType::sgpr;
399 } else if (!ctx->divergent_vals[intrinsic->dest.ssa.index]) {
400 type = RegType::sgpr;
401 } else {
402 type = RegType::vgpr;
403 }
404 break;
405 case nir_intrinsic_load_ubo:
406 case nir_intrinsic_load_ssbo:
407 case nir_intrinsic_load_global:
408 case nir_intrinsic_vulkan_resource_index:
409 type = ctx->divergent_vals[intrinsic->dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
410 break;
411 /* due to copy propagation, the swizzled imov is removed if num dest components == 1 */
412 case nir_intrinsic_load_shared:
413 if (ctx->divergent_vals[intrinsic->dest.ssa.index])
414 type = RegType::vgpr;
415 else
416 type = RegType::sgpr;
417 break;
418 default:
419 for (unsigned i = 0; i < nir_intrinsic_infos[intrinsic->intrinsic].num_srcs; i++) {
420 if (allocated[intrinsic->src[i].ssa->index].type() == RegType::vgpr)
421 type = RegType::vgpr;
422 }
423 break;
424 }
425 allocated[intrinsic->dest.ssa.index] = Temp(0, RegClass(type, size));
426
427 switch(intrinsic->intrinsic) {
428 case nir_intrinsic_load_barycentric_sample:
429 case nir_intrinsic_load_barycentric_pixel:
430 case nir_intrinsic_load_barycentric_centroid:
431 case nir_intrinsic_load_barycentric_at_sample:
432 case nir_intrinsic_load_barycentric_at_offset: {
433 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(intrinsic);
434 spi_ps_inputs |= get_interp_input(intrinsic->intrinsic, mode);
435 break;
436 }
437 case nir_intrinsic_load_front_face:
438 spi_ps_inputs |= S_0286CC_FRONT_FACE_ENA(1);
439 break;
440 case nir_intrinsic_load_frag_coord:
441 case nir_intrinsic_load_sample_pos: {
442 uint8_t mask = nir_ssa_def_components_read(&intrinsic->dest.ssa);
443 for (unsigned i = 0; i < 4; i++) {
444 if (mask & (1 << i))
445 spi_ps_inputs |= S_0286CC_POS_X_FLOAT_ENA(1) << i;
446
447 }
448 break;
449 }
450 case nir_intrinsic_load_sample_id:
451 spi_ps_inputs |= S_0286CC_ANCILLARY_ENA(1);
452 break;
453 case nir_intrinsic_load_sample_mask_in:
454 spi_ps_inputs |= S_0286CC_ANCILLARY_ENA(1);
455 spi_ps_inputs |= S_0286CC_SAMPLE_COVERAGE_ENA(1);
456 break;
457 default:
458 break;
459 }
460 break;
461 }
462 case nir_instr_type_tex: {
463 nir_tex_instr* tex = nir_instr_as_tex(instr);
464 unsigned size = tex->dest.ssa.num_components;
465
466 if (tex->dest.ssa.bit_size == 64)
467 size *= 2;
468 if (tex->op == nir_texop_texture_samples)
469 assert(!ctx->divergent_vals[tex->dest.ssa.index]);
470 if (ctx->divergent_vals[tex->dest.ssa.index])
471 allocated[tex->dest.ssa.index] = Temp(0, RegClass(RegType::vgpr, size));
472 else
473 allocated[tex->dest.ssa.index] = Temp(0, RegClass(RegType::sgpr, size));
474 break;
475 }
476 case nir_instr_type_parallel_copy: {
477 nir_foreach_parallel_copy_entry(entry, nir_instr_as_parallel_copy(instr)) {
478 allocated[entry->dest.ssa.index] = allocated[entry->src.ssa->index];
479 }
480 break;
481 }
482 case nir_instr_type_ssa_undef: {
483 unsigned size = nir_instr_as_ssa_undef(instr)->def.num_components;
484 if (nir_instr_as_ssa_undef(instr)->def.bit_size == 64)
485 size *= 2;
486 allocated[nir_instr_as_ssa_undef(instr)->def.index] = Temp(0, RegClass(RegType::sgpr, size));
487 break;
488 }
489 case nir_instr_type_phi: {
490 nir_phi_instr* phi = nir_instr_as_phi(instr);
491 RegType type;
492 unsigned size = phi->dest.ssa.num_components;
493
494 if (phi->dest.ssa.bit_size == 1) {
495 assert(size == 1 && "multiple components not yet supported on boolean phis.");
496 type = RegType::sgpr;
497 size *= lane_mask_size;
498 allocated[phi->dest.ssa.index] = Temp(0, RegClass(type, size));
499 break;
500 }
501
502 if (ctx->divergent_vals[phi->dest.ssa.index]) {
503 type = RegType::vgpr;
504 } else {
505 type = RegType::sgpr;
506 nir_foreach_phi_src (src, phi) {
507 if (allocated[src->src.ssa->index].type() == RegType::vgpr)
508 type = RegType::vgpr;
509 if (allocated[src->src.ssa->index].type() == RegType::none)
510 done = false;
511 }
512 }
513
514 size *= phi->dest.ssa.bit_size == 64 ? 2 : 1;
515 RegClass rc = RegClass(type, size);
516 if (rc != allocated[phi->dest.ssa.index].regClass()) {
517 done = false;
518 } else {
519 nir_foreach_phi_src(src, phi)
520 assert(allocated[src->src.ssa->index].size() == rc.size());
521 }
522 allocated[phi->dest.ssa.index] = Temp(0, rc);
523 break;
524 }
525 default:
526 break;
527 }
528 }
529 }
530 }
531
532 if (G_0286CC_POS_W_FLOAT_ENA(spi_ps_inputs)) {
533 /* If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be enabled too */
534 spi_ps_inputs |= S_0286CC_PERSP_CENTER_ENA(1);
535 }
536
537 if (!(spi_ps_inputs & 0x7F)) {
538 /* At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled */
539 spi_ps_inputs |= S_0286CC_PERSP_CENTER_ENA(1);
540 }
541
542 ctx->program->config->spi_ps_input_ena = spi_ps_inputs;
543 ctx->program->config->spi_ps_input_addr = spi_ps_inputs;
544
545 for (unsigned i = 0; i < impl->ssa_alloc; i++)
546 allocated[i] = Temp(ctx->program->allocateId(), allocated[i].regClass());
547
548 ctx->allocated.reset(allocated.release());
549 ctx->cf_info.nir_to_aco.reset(nir_to_aco.release());
550 }
551
552 Pseudo_instruction *add_startpgm(struct isel_context *ctx)
553 {
554 unsigned arg_count = ctx->args->ac.arg_count;
555 if (ctx->stage == fragment_fs) {
556 /* LLVM optimizes away unused FS inputs and computes spi_ps_input_addr
557 * itself and then communicates the results back via the ELF binary.
558 * Mirror what LLVM does by re-mapping the VGPR arguments here.
559 *
560 * TODO: If we made the FS input scanning code into a separate pass that
561 * could run before argument setup, then this wouldn't be necessary
562 * anymore.
563 */
564 struct ac_shader_args *args = &ctx->args->ac;
565 arg_count = 0;
566 for (unsigned i = 0, vgpr_arg = 0, vgpr_reg = 0; i < args->arg_count; i++) {
567 if (args->args[i].file != AC_ARG_VGPR) {
568 arg_count++;
569 continue;
570 }
571
572 if (!(ctx->program->config->spi_ps_input_addr & (1 << vgpr_arg))) {
573 args->args[i].skip = true;
574 } else {
575 args->args[i].offset = vgpr_reg;
576 vgpr_reg += args->args[i].size;
577 arg_count++;
578 }
579 vgpr_arg++;
580 }
581 }
582
583 aco_ptr<Pseudo_instruction> startpgm{create_instruction<Pseudo_instruction>(aco_opcode::p_startpgm, Format::PSEUDO, 0, arg_count + 1)};
584 for (unsigned i = 0, arg = 0; i < ctx->args->ac.arg_count; i++) {
585 if (ctx->args->ac.args[i].skip)
586 continue;
587
588 enum ac_arg_regfile file = ctx->args->ac.args[i].file;
589 unsigned size = ctx->args->ac.args[i].size;
590 unsigned reg = ctx->args->ac.args[i].offset;
591 RegClass type = RegClass(file == AC_ARG_SGPR ? RegType::sgpr : RegType::vgpr, size);
592 Temp dst = Temp{ctx->program->allocateId(), type};
593 ctx->arg_temps[i] = dst;
594 startpgm->definitions[arg] = Definition(dst);
595 startpgm->definitions[arg].setFixed(PhysReg{file == AC_ARG_SGPR ? reg : reg + 256});
596 arg++;
597 }
598 startpgm->definitions[arg_count] = Definition{ctx->program->allocateId(), exec, ctx->program->lane_mask};
599 Pseudo_instruction *instr = startpgm.get();
600 ctx->block->instructions.push_back(std::move(startpgm));
601
602 /* Stash these in the program so that they can be accessed later when
603 * handling spilling.
604 */
605 ctx->program->private_segment_buffer = get_arg(ctx, ctx->args->ring_offsets);
606 ctx->program->scratch_offset = get_arg(ctx, ctx->args->scratch_offset);
607
608 return instr;
609 }
610
611 int
612 type_size(const struct glsl_type *type, bool bindless)
613 {
614 // TODO: don't we need type->std430_base_alignment() here?
615 return glsl_count_attribute_slots(type, false);
616 }
617
618 void
619 shared_var_info(const struct glsl_type *type, unsigned *size, unsigned *align)
620 {
621 assert(glsl_type_is_vector_or_scalar(type));
622
623 uint32_t comp_size = glsl_type_is_boolean(type)
624 ? 4 : glsl_get_bit_size(type) / 8;
625 unsigned length = glsl_get_vector_elements(type);
626 *size = comp_size * length,
627 *align = comp_size;
628 }
629
630 static bool
631 mem_vectorize_callback(unsigned align, unsigned bit_size,
632 unsigned num_components, unsigned high_offset,
633 nir_intrinsic_instr *low, nir_intrinsic_instr *high)
634 {
635 if ((bit_size != 32 && bit_size != 64) || num_components > 4)
636 return false;
637
638 /* >128 bit loads are split except with SMEM */
639 if (bit_size * num_components > 128)
640 return false;
641
642 switch (low->intrinsic) {
643 case nir_intrinsic_load_ubo:
644 case nir_intrinsic_load_ssbo:
645 case nir_intrinsic_store_ssbo:
646 case nir_intrinsic_load_push_constant:
647 return align % 4 == 0;
648 case nir_intrinsic_load_deref:
649 case nir_intrinsic_store_deref:
650 assert(nir_src_as_deref(low->src[0])->mode == nir_var_mem_shared);
651 /* fallthrough */
652 case nir_intrinsic_load_shared:
653 case nir_intrinsic_store_shared:
654 if (bit_size * num_components > 64) /* 96 and 128 bit loads require 128 bit alignment and are split otherwise */
655 return align % 16 == 0;
656 else
657 return align % 4 == 0;
658 default:
659 return false;
660 }
661 return false;
662 }
663
664 void
665 setup_vs_variables(isel_context *ctx, nir_shader *nir)
666 {
667 nir_foreach_variable(variable, &nir->inputs)
668 {
669 variable->data.driver_location = variable->data.location * 4;
670 }
671 nir_foreach_variable(variable, &nir->outputs)
672 {
673 if (ctx->stage == vertex_geometry_gs)
674 variable->data.driver_location = util_bitcount64(ctx->output_masks[nir->info.stage] & ((1ull << variable->data.location) - 1ull)) * 4;
675 else
676 variable->data.driver_location = variable->data.location * 4;
677 }
678
679 if (ctx->stage == vertex_vs) {
680 radv_vs_output_info *outinfo = &ctx->program->info->vs.outinfo;
681
682 memset(outinfo->vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
683 sizeof(outinfo->vs_output_param_offset));
684
685 ctx->needs_instance_id = ctx->program->info->vs.needs_instance_id;
686
687 bool export_clip_dists = ctx->options->key.vs_common_out.export_clip_dists;
688
689 outinfo->param_exports = 0;
690 int pos_written = 0x1;
691 if (outinfo->writes_pointsize || outinfo->writes_viewport_index || outinfo->writes_layer)
692 pos_written |= 1 << 1;
693
694 uint64_t mask = ctx->output_masks[nir->info.stage];
695 while (mask) {
696 int idx = u_bit_scan64(&mask);
697 if (idx >= VARYING_SLOT_VAR0 || idx == VARYING_SLOT_LAYER || idx == VARYING_SLOT_PRIMITIVE_ID ||
698 ((idx == VARYING_SLOT_CLIP_DIST0 || idx == VARYING_SLOT_CLIP_DIST1) && export_clip_dists)) {
699 if (outinfo->vs_output_param_offset[idx] == AC_EXP_PARAM_UNDEFINED)
700 outinfo->vs_output_param_offset[idx] = outinfo->param_exports++;
701 }
702 }
703 if (outinfo->writes_layer &&
704 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] == AC_EXP_PARAM_UNDEFINED) {
705 /* when ctx->options->key.has_multiview_view_index = true, the layer
706 * variable isn't declared in NIR and it's isel's job to get the layer */
707 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] = outinfo->param_exports++;
708 }
709
710 if (outinfo->export_prim_id) {
711 assert(outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] == AC_EXP_PARAM_UNDEFINED);
712 outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = outinfo->param_exports++;
713 }
714
715 ctx->num_clip_distances = util_bitcount(outinfo->clip_dist_mask);
716 ctx->num_cull_distances = util_bitcount(outinfo->cull_dist_mask);
717
718 assert(ctx->num_clip_distances + ctx->num_cull_distances <= 8);
719
720 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
721 pos_written |= 1 << 2;
722 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
723 pos_written |= 1 << 3;
724
725 outinfo->pos_exports = util_bitcount(pos_written);
726 } else if (ctx->stage == vertex_geometry_gs) {
727 /* TODO: radv_nir_shader_info_pass() already sets this but it's larger
728 * than it needs to be in order to set it better, we have to improve
729 * radv_nir_shader_info_pass() because gfx9_get_gs_info() uses
730 * esgs_itemsize and has to be done before compilation
731 */
732 /* radv_es_output_info *outinfo = &ctx->program->info->vs.es_info;
733 outinfo->esgs_itemsize = util_bitcount64(ctx->output_masks[nir->info.stage]) * 16u; */
734 }
735 }
736
737 void
738 setup_variables(isel_context *ctx, nir_shader *nir)
739 {
740 switch (nir->info.stage) {
741 case MESA_SHADER_FRAGMENT: {
742 nir_foreach_variable(variable, &nir->outputs)
743 {
744 int idx = variable->data.location + variable->data.index;
745 variable->data.driver_location = idx * 4;
746 }
747 break;
748 }
749 case MESA_SHADER_COMPUTE: {
750 ctx->program->config->lds_size = (nir->info.cs.shared_size + ctx->program->lds_alloc_granule - 1) /
751 ctx->program->lds_alloc_granule;
752 break;
753 }
754 case MESA_SHADER_VERTEX: {
755 setup_vs_variables(ctx, nir);
756 break;
757 }
758 case MESA_SHADER_GEOMETRY: {
759 assert(ctx->stage == vertex_geometry_gs);
760 nir_foreach_variable(variable, &nir->inputs) {
761 variable->data.driver_location = util_bitcount64(ctx->input_masks[nir->info.stage] & ((1ull << variable->data.location) - 1ull)) * 4;
762 }
763 nir_foreach_variable(variable, &nir->outputs) {
764 variable->data.driver_location = variable->data.location * 4;
765 }
766 ctx->program->info->gs.es_type = MESA_SHADER_VERTEX; /* tesselation shaders are not yet supported */
767 break;
768 }
769 default:
770 unreachable("Unhandled shader stage.");
771 }
772 }
773
774 void
775 get_io_masks(isel_context *ctx, unsigned shader_count, struct nir_shader *const *shaders)
776 {
777 for (unsigned i = 0; i < shader_count; i++) {
778 nir_shader *nir = shaders[i];
779 if (nir->info.stage == MESA_SHADER_COMPUTE)
780 continue;
781
782 uint64_t output_mask = 0;
783 nir_foreach_variable(variable, &nir->outputs) {
784 const glsl_type *type = variable->type;
785 if (nir_is_per_vertex_io(variable, nir->info.stage))
786 type = type->fields.array;
787 unsigned slots = type->count_attribute_slots(false);
788 if (variable->data.compact) {
789 unsigned component_count = variable->data.location_frac + type->length;
790 slots = (component_count + 3) / 4;
791 }
792 output_mask |= ((1ull << slots) - 1) << variable->data.location;
793 }
794
795 uint64_t input_mask = 0;
796 nir_foreach_variable(variable, &nir->inputs) {
797 const glsl_type *type = variable->type;
798 if (nir_is_per_vertex_io(variable, nir->info.stage))
799 type = type->fields.array;
800 unsigned slots = type->count_attribute_slots(false);
801 if (variable->data.compact) {
802 unsigned component_count = variable->data.location_frac + type->length;
803 slots = (component_count + 3) / 4;
804 }
805 input_mask |= ((1ull << slots) - 1) << variable->data.location;
806 }
807
808 ctx->output_masks[nir->info.stage] |= output_mask;
809 if (i + 1 < shader_count)
810 ctx->input_masks[shaders[i + 1]->info.stage] |= output_mask;
811
812 ctx->input_masks[nir->info.stage] |= input_mask;
813 if (i)
814 ctx->output_masks[shaders[i - 1]->info.stage] |= input_mask;
815 }
816 }
817
818 isel_context
819 setup_isel_context(Program* program,
820 unsigned shader_count,
821 struct nir_shader *const *shaders,
822 ac_shader_config* config,
823 struct radv_shader_args *args)
824 {
825 program->stage = 0;
826 for (unsigned i = 0; i < shader_count; i++) {
827 switch (shaders[i]->info.stage) {
828 case MESA_SHADER_VERTEX:
829 program->stage |= sw_vs;
830 break;
831 case MESA_SHADER_TESS_CTRL:
832 program->stage |= sw_tcs;
833 break;
834 case MESA_SHADER_TESS_EVAL:
835 program->stage |= sw_tes;
836 break;
837 case MESA_SHADER_GEOMETRY:
838 program->stage |= sw_gs;
839 break;
840 case MESA_SHADER_FRAGMENT:
841 program->stage |= sw_fs;
842 break;
843 case MESA_SHADER_COMPUTE:
844 program->stage |= sw_cs;
845 break;
846 default:
847 unreachable("Shader stage not implemented");
848 }
849 }
850 bool gfx9_plus = args->options->chip_class >= GFX9;
851 bool ngg = args->shader_info->is_ngg && args->options->chip_class >= GFX10;
852 if (program->stage == sw_vs)
853 program->stage |= hw_vs;
854 else if (program->stage == sw_fs)
855 program->stage |= hw_fs;
856 else if (program->stage == sw_cs)
857 program->stage |= hw_cs;
858 else if (program->stage == (sw_vs | sw_gs) && gfx9_plus && !ngg)
859 program->stage |= hw_gs;
860 else
861 unreachable("Shader stage not implemented");
862
863 program->config = config;
864 program->info = args->shader_info;
865 program->chip_class = args->options->chip_class;
866 program->family = args->options->family;
867 program->wave_size = args->shader_info->wave_size;
868 program->lane_mask = program->wave_size == 32 ? s1 : s2;
869
870 program->lds_alloc_granule = args->options->chip_class >= GFX7 ? 512 : 256;
871 program->lds_limit = args->options->chip_class >= GFX7 ? 65536 : 32768;
872 program->vgpr_limit = 256;
873 program->vgpr_alloc_granule = 3;
874
875 if (args->options->chip_class >= GFX10) {
876 program->physical_sgprs = 2560; /* doesn't matter as long as it's at least 128 * 20 */
877 program->sgpr_alloc_granule = 127;
878 program->sgpr_limit = 106;
879 program->vgpr_alloc_granule = program->wave_size == 32 ? 7 : 3;
880 } else if (program->chip_class >= GFX8) {
881 program->physical_sgprs = 800;
882 program->sgpr_alloc_granule = 15;
883 if (args->options->family == CHIP_TONGA || args->options->family == CHIP_ICELAND)
884 program->sgpr_limit = 94; /* workaround hardware bug */
885 else
886 program->sgpr_limit = 102;
887 } else {
888 program->physical_sgprs = 512;
889 program->sgpr_alloc_granule = 7;
890 program->sgpr_limit = 104;
891 }
892
893 /* TODO: we don't have to allocate VCC if we don't need it */
894 program->needs_vcc = true;
895
896 calc_min_waves(program);
897 program->vgpr_limit = get_addr_vgpr_from_waves(program, program->min_waves);
898 program->sgpr_limit = get_addr_sgpr_from_waves(program, program->min_waves);
899
900 isel_context ctx = {};
901 ctx.program = program;
902 ctx.args = args;
903 ctx.options = args->options;
904 ctx.stage = program->stage;
905
906 get_io_masks(&ctx, shader_count, shaders);
907
908 for (unsigned i = 0; i < shader_count; i++) {
909 nir_shader *nir = shaders[i];
910
911 /* align and copy constant data */
912 while (program->constant_data.size() % 4u)
913 program->constant_data.push_back(0);
914 ctx.constant_data_offset = program->constant_data.size();
915 program->constant_data.insert(program->constant_data.end(),
916 (uint8_t*)nir->constant_data,
917 (uint8_t*)nir->constant_data + nir->constant_data_size);
918
919 /* the variable setup has to be done before lower_io / CSE */
920 setup_variables(&ctx, nir);
921
922 /* optimize and lower memory operations */
923 bool lower_to_scalar = false;
924 bool lower_pack = false;
925 if (nir_opt_load_store_vectorize(nir,
926 (nir_variable_mode)(nir_var_mem_ssbo | nir_var_mem_ubo |
927 nir_var_mem_push_const | nir_var_mem_shared),
928 mem_vectorize_callback)) {
929 lower_to_scalar = true;
930 lower_pack = true;
931 }
932 if (nir->info.stage != MESA_SHADER_COMPUTE)
933 nir_lower_io(nir, (nir_variable_mode)(nir_var_shader_in | nir_var_shader_out), type_size, (nir_lower_io_options)0);
934 nir_lower_explicit_io(nir, nir_var_mem_global, nir_address_format_64bit_global);
935
936 if (lower_to_scalar)
937 nir_lower_alu_to_scalar(nir, NULL, NULL);
938 if (lower_pack)
939 nir_lower_pack(nir);
940
941 /* lower ALU operations */
942 // TODO: implement logic64 in aco, it's more effective for sgprs
943 nir_lower_int64(nir, nir->options->lower_int64_options);
944
945 nir_opt_idiv_const(nir, 32);
946 nir_lower_idiv(nir, nir_lower_idiv_precise);
947
948 /* optimize the lowered ALU operations */
949 bool more_algebraic = true;
950 while (more_algebraic) {
951 more_algebraic = false;
952 NIR_PASS_V(nir, nir_copy_prop);
953 NIR_PASS_V(nir, nir_opt_dce);
954 NIR_PASS_V(nir, nir_opt_constant_folding);
955 NIR_PASS(more_algebraic, nir, nir_opt_algebraic);
956 }
957
958 /* Do late algebraic optimization to turn add(a, neg(b)) back into
959 * subs, then the mandatory cleanup after algebraic. Note that it may
960 * produce fnegs, and if so then we need to keep running to squash
961 * fneg(fneg(a)).
962 */
963 bool more_late_algebraic = true;
964 while (more_late_algebraic) {
965 more_late_algebraic = false;
966 NIR_PASS(more_late_algebraic, nir, nir_opt_algebraic_late);
967 NIR_PASS_V(nir, nir_opt_constant_folding);
968 NIR_PASS_V(nir, nir_copy_prop);
969 NIR_PASS_V(nir, nir_opt_dce);
970 NIR_PASS_V(nir, nir_opt_cse);
971 }
972
973 /* cleanup passes */
974 nir_lower_load_const_to_scalar(nir);
975 nir_opt_shrink_load(nir);
976 nir_move_options move_opts = (nir_move_options)(
977 nir_move_const_undef | nir_move_load_ubo | nir_move_load_input | nir_move_comparisons);
978 nir_opt_sink(nir, move_opts);
979 nir_opt_move(nir, move_opts);
980 nir_convert_to_lcssa(nir, true, false);
981 nir_lower_phis_to_scalar(nir);
982
983 nir_function_impl *func = nir_shader_get_entrypoint(nir);
984 nir_index_ssa_defs(func);
985 nir_metadata_require(func, nir_metadata_block_index);
986
987 if (args->options->dump_preoptir) {
988 fprintf(stderr, "NIR shader before instruction selection:\n");
989 nir_print_shader(nir, stderr);
990 }
991 }
992
993 unsigned scratch_size = 0;
994 for (unsigned i = 0; i < shader_count; i++)
995 scratch_size = std::max(scratch_size, shaders[i]->scratch_size);
996 ctx.program->config->scratch_bytes_per_wave = align(scratch_size * ctx.program->wave_size, 1024);
997
998 ctx.block = ctx.program->create_and_insert_block();
999 ctx.block->loop_nest_depth = 0;
1000 ctx.block->kind = block_kind_top_level;
1001
1002 return ctx;
1003 }
1004
1005 }