aco: increase accuracy of SGPR limits
[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 "sid.h"
32 #include "ac_exp_param.h"
33 #include "ac_shader_util.h"
34
35 #include "util/u_math.h"
36
37 #define MAX_INLINE_PUSH_CONSTS 8
38
39 namespace aco {
40
41 enum fs_input {
42 persp_sample_p1,
43 persp_sample_p2,
44 persp_center_p1,
45 persp_center_p2,
46 persp_centroid_p1,
47 persp_centroid_p2,
48 persp_pull_model,
49 linear_sample_p1,
50 linear_sample_p2,
51 linear_center_p1,
52 linear_center_p2,
53 linear_centroid_p1,
54 linear_centroid_p2,
55 line_stipple,
56 frag_pos_0,
57 frag_pos_1,
58 frag_pos_2,
59 frag_pos_3,
60 front_face,
61 ancillary,
62 sample_coverage,
63 fixed_pt,
64 max_inputs,
65 };
66
67 struct vs_output_state {
68 uint8_t mask[VARYING_SLOT_VAR31 + 1];
69 Temp outputs[VARYING_SLOT_VAR31 + 1][4];
70 };
71
72 struct isel_context {
73 struct radv_nir_compiler_options *options;
74 Program *program;
75 nir_shader *shader;
76 uint32_t constant_data_offset;
77 Block *block;
78 bool *divergent_vals;
79 std::unique_ptr<Temp[]> allocated;
80 std::unordered_map<unsigned, std::array<Temp,4>> allocated_vec;
81 Stage stage; /* Stage */
82 struct {
83 bool has_branch;
84 uint16_t loop_nest_depth = 0;
85 struct {
86 unsigned header_idx;
87 Block* exit;
88 bool has_divergent_continue = false;
89 bool has_divergent_branch = false;
90 } parent_loop;
91 struct {
92 bool is_divergent = false;
93 } parent_if;
94 bool exec_potentially_empty = false;
95 } cf_info;
96
97 /* scratch */
98 bool scratch_enabled = false;
99 Temp private_segment_buffer = Temp(0, s2); /* also the part of the scratch descriptor on compute */
100 Temp scratch_offset = Temp(0, s1);
101
102 /* inputs common for merged stages */
103 Temp merged_wave_info = Temp(0, s1);
104
105 /* FS inputs */
106 bool fs_vgpr_args[fs_input::max_inputs];
107 Temp fs_inputs[fs_input::max_inputs];
108 Temp prim_mask = Temp(0, s1);
109 Temp descriptor_sets[MAX_SETS];
110 Temp push_constants = Temp(0, s1);
111 Temp inline_push_consts[MAX_INLINE_PUSH_CONSTS];
112 unsigned num_inline_push_consts = 0;
113 unsigned base_inline_push_consts = 0;
114
115 /* VS inputs */
116 Temp vertex_buffers = Temp(0, s1);
117 Temp base_vertex = Temp(0, s1);
118 Temp start_instance = Temp(0, s1);
119 Temp draw_id = Temp(0, s1);
120 Temp view_index = Temp(0, s1);
121 Temp es2gs_offset = Temp(0, s1);
122 Temp vertex_id = Temp(0, v1);
123 Temp rel_auto_id = Temp(0, v1);
124 Temp instance_id = Temp(0, v1);
125 Temp vs_prim_id = Temp(0, v1);
126 bool needs_instance_id;
127
128 /* CS inputs */
129 Temp num_workgroups[3] = {Temp(0, s1), Temp(0, s1), Temp(0, s1)};
130 Temp workgroup_ids[3] = {Temp(0, s1), Temp(0, s1), Temp(0, s1)};
131 Temp tg_size = Temp(0, s1);
132 Temp local_invocation_ids[3] = {Temp(0, v1), Temp(0, v1), Temp(0, v1)};
133
134 /* VS output information */
135 unsigned num_clip_distances;
136 unsigned num_cull_distances;
137 vs_output_state vs_output;
138
139 /* Streamout */
140 Temp streamout_buffers = Temp(0, s1);
141 Temp streamout_write_idx = Temp(0, s1);
142 Temp streamout_config = Temp(0, s1);
143 Temp streamout_offset[4] = {Temp(0, s1), Temp(0, s1), Temp(0, s1), Temp(0, s1)};
144 };
145
146 fs_input get_interp_input(nir_intrinsic_op intrin, enum glsl_interp_mode interp)
147 {
148 switch (interp) {
149 case INTERP_MODE_SMOOTH:
150 case INTERP_MODE_NONE:
151 if (intrin == nir_intrinsic_load_barycentric_pixel ||
152 intrin == nir_intrinsic_load_barycentric_at_sample ||
153 intrin == nir_intrinsic_load_barycentric_at_offset)
154 return fs_input::persp_center_p1;
155 else if (intrin == nir_intrinsic_load_barycentric_centroid)
156 return fs_input::persp_centroid_p1;
157 else if (intrin == nir_intrinsic_load_barycentric_sample)
158 return fs_input::persp_sample_p1;
159 break;
160 case INTERP_MODE_NOPERSPECTIVE:
161 if (intrin == nir_intrinsic_load_barycentric_pixel)
162 return fs_input::linear_center_p1;
163 else if (intrin == nir_intrinsic_load_barycentric_centroid)
164 return fs_input::linear_centroid_p1;
165 else if (intrin == nir_intrinsic_load_barycentric_sample)
166 return fs_input::linear_sample_p1;
167 break;
168 default:
169 break;
170 }
171 return fs_input::max_inputs;
172 }
173
174 void init_context(isel_context *ctx, nir_shader *shader)
175 {
176 nir_function_impl *impl = nir_shader_get_entrypoint(shader);
177
178 ctx->shader = shader;
179 ctx->divergent_vals = nir_divergence_analysis(shader, nir_divergence_view_index_uniform);
180
181 std::unique_ptr<Temp[]> allocated{new Temp[impl->ssa_alloc]()};
182 memset(&ctx->fs_vgpr_args, false, sizeof(ctx->fs_vgpr_args));
183
184 bool done = false;
185 while (!done) {
186 done = true;
187 nir_foreach_block(block, impl) {
188 nir_foreach_instr(instr, block) {
189 switch(instr->type) {
190 case nir_instr_type_alu: {
191 nir_alu_instr *alu_instr = nir_instr_as_alu(instr);
192 unsigned size = alu_instr->dest.dest.ssa.num_components;
193 if (alu_instr->dest.dest.ssa.bit_size == 64)
194 size *= 2;
195 RegType type = RegType::sgpr;
196 switch(alu_instr->op) {
197 case nir_op_fmul:
198 case nir_op_fadd:
199 case nir_op_fsub:
200 case nir_op_fmax:
201 case nir_op_fmin:
202 case nir_op_fmax3:
203 case nir_op_fmin3:
204 case nir_op_fmed3:
205 case nir_op_fneg:
206 case nir_op_fabs:
207 case nir_op_fsat:
208 case nir_op_fsign:
209 case nir_op_frcp:
210 case nir_op_frsq:
211 case nir_op_fsqrt:
212 case nir_op_fexp2:
213 case nir_op_flog2:
214 case nir_op_ffract:
215 case nir_op_ffloor:
216 case nir_op_fceil:
217 case nir_op_ftrunc:
218 case nir_op_fround_even:
219 case nir_op_fsin:
220 case nir_op_fcos:
221 case nir_op_f2f32:
222 case nir_op_f2f64:
223 case nir_op_u2f32:
224 case nir_op_u2f64:
225 case nir_op_i2f32:
226 case nir_op_i2f64:
227 case nir_op_pack_half_2x16:
228 case nir_op_unpack_half_2x16_split_x:
229 case nir_op_unpack_half_2x16_split_y:
230 case nir_op_fddx:
231 case nir_op_fddy:
232 case nir_op_fddx_fine:
233 case nir_op_fddy_fine:
234 case nir_op_fddx_coarse:
235 case nir_op_fddy_coarse:
236 case nir_op_fquantize2f16:
237 case nir_op_ldexp:
238 case nir_op_frexp_sig:
239 case nir_op_frexp_exp:
240 case nir_op_cube_face_index:
241 case nir_op_cube_face_coord:
242 type = RegType::vgpr;
243 break;
244 case nir_op_flt:
245 case nir_op_fge:
246 case nir_op_feq:
247 case nir_op_fne:
248 size = 2;
249 break;
250 case nir_op_ilt:
251 case nir_op_ige:
252 case nir_op_ult:
253 case nir_op_uge:
254 size = alu_instr->src[0].src.ssa->bit_size == 64 ? 2 : 1;
255 /* fallthrough */
256 case nir_op_ieq:
257 case nir_op_ine:
258 case nir_op_i2b1:
259 if (ctx->divergent_vals[alu_instr->dest.dest.ssa.index]) {
260 size = 2;
261 } else {
262 for (unsigned i = 0; i < nir_op_infos[alu_instr->op].num_inputs; i++) {
263 if (allocated[alu_instr->src[i].src.ssa->index].type() == RegType::vgpr)
264 size = 2;
265 }
266 }
267 break;
268 case nir_op_f2i64:
269 case nir_op_f2u64:
270 case nir_op_b2i32:
271 case nir_op_b2f32:
272 case nir_op_f2i32:
273 case nir_op_f2u32:
274 type = ctx->divergent_vals[alu_instr->dest.dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
275 break;
276 case nir_op_bcsel:
277 if (alu_instr->dest.dest.ssa.bit_size == 1) {
278 if (ctx->divergent_vals[alu_instr->dest.dest.ssa.index])
279 size = 2;
280 else if (allocated[alu_instr->src[1].src.ssa->index].regClass() == s2 &&
281 allocated[alu_instr->src[2].src.ssa->index].regClass() == s2)
282 size = 2;
283 else
284 size = 1;
285 } else {
286 if (ctx->divergent_vals[alu_instr->dest.dest.ssa.index]) {
287 type = RegType::vgpr;
288 } else {
289 if (allocated[alu_instr->src[1].src.ssa->index].type() == RegType::vgpr ||
290 allocated[alu_instr->src[2].src.ssa->index].type() == RegType::vgpr) {
291 type = RegType::vgpr;
292 }
293 }
294 if (alu_instr->src[1].src.ssa->num_components == 1 && alu_instr->src[2].src.ssa->num_components == 1) {
295 assert(allocated[alu_instr->src[1].src.ssa->index].size() == allocated[alu_instr->src[2].src.ssa->index].size());
296 size = allocated[alu_instr->src[1].src.ssa->index].size();
297 }
298 }
299 break;
300 case nir_op_mov:
301 if (alu_instr->dest.dest.ssa.bit_size == 1) {
302 size = allocated[alu_instr->src[0].src.ssa->index].size();
303 } else {
304 type = ctx->divergent_vals[alu_instr->dest.dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
305 }
306 break;
307 case nir_op_inot:
308 case nir_op_ixor:
309 if (alu_instr->dest.dest.ssa.bit_size == 1) {
310 size = ctx->divergent_vals[alu_instr->dest.dest.ssa.index] ? 2 : 1;
311 break;
312 } else {
313 /* fallthrough */
314 }
315 default:
316 if (alu_instr->dest.dest.ssa.bit_size == 1) {
317 if (ctx->divergent_vals[alu_instr->dest.dest.ssa.index]) {
318 size = 2;
319 } else {
320 size = 2;
321 for (unsigned i = 0; i < nir_op_infos[alu_instr->op].num_inputs; i++) {
322 if (allocated[alu_instr->src[i].src.ssa->index].regClass() == s1) {
323 size = 1;
324 break;
325 }
326 }
327 }
328 } else {
329 for (unsigned i = 0; i < nir_op_infos[alu_instr->op].num_inputs; i++) {
330 if (allocated[alu_instr->src[i].src.ssa->index].type() == RegType::vgpr)
331 type = RegType::vgpr;
332 }
333 }
334 break;
335 }
336 allocated[alu_instr->dest.dest.ssa.index] = Temp(0, RegClass(type, size));
337 break;
338 }
339 case nir_instr_type_load_const: {
340 unsigned size = nir_instr_as_load_const(instr)->def.num_components;
341 if (nir_instr_as_load_const(instr)->def.bit_size == 64)
342 size *= 2;
343 allocated[nir_instr_as_load_const(instr)->def.index] = Temp(0, RegClass(RegType::sgpr, size));
344 break;
345 }
346 case nir_instr_type_intrinsic: {
347 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
348 if (!nir_intrinsic_infos[intrinsic->intrinsic].has_dest)
349 break;
350 unsigned size = intrinsic->dest.ssa.num_components;
351 if (intrinsic->dest.ssa.bit_size == 64)
352 size *= 2;
353 RegType type = RegType::sgpr;
354 switch(intrinsic->intrinsic) {
355 case nir_intrinsic_load_push_constant:
356 case nir_intrinsic_load_work_group_id:
357 case nir_intrinsic_load_num_work_groups:
358 case nir_intrinsic_load_subgroup_id:
359 case nir_intrinsic_load_num_subgroups:
360 case nir_intrinsic_load_first_vertex:
361 case nir_intrinsic_load_base_instance:
362 case nir_intrinsic_get_buffer_size:
363 case nir_intrinsic_vote_all:
364 case nir_intrinsic_vote_any:
365 case nir_intrinsic_read_first_invocation:
366 case nir_intrinsic_read_invocation:
367 case nir_intrinsic_first_invocation:
368 type = RegType::sgpr;
369 break;
370 case nir_intrinsic_ballot:
371 type = RegType::sgpr;
372 size = 2;
373 break;
374 case nir_intrinsic_load_sample_id:
375 case nir_intrinsic_load_sample_mask_in:
376 case nir_intrinsic_load_input:
377 case nir_intrinsic_load_vertex_id:
378 case nir_intrinsic_load_vertex_id_zero_base:
379 case nir_intrinsic_load_barycentric_sample:
380 case nir_intrinsic_load_barycentric_pixel:
381 case nir_intrinsic_load_barycentric_centroid:
382 case nir_intrinsic_load_barycentric_at_sample:
383 case nir_intrinsic_load_barycentric_at_offset:
384 case nir_intrinsic_load_interpolated_input:
385 case nir_intrinsic_load_frag_coord:
386 case nir_intrinsic_load_sample_pos:
387 case nir_intrinsic_load_layer_id:
388 case nir_intrinsic_load_local_invocation_id:
389 case nir_intrinsic_load_local_invocation_index:
390 case nir_intrinsic_load_subgroup_invocation:
391 case nir_intrinsic_write_invocation_amd:
392 case nir_intrinsic_mbcnt_amd:
393 case nir_intrinsic_load_instance_id:
394 case nir_intrinsic_ssbo_atomic_add:
395 case nir_intrinsic_ssbo_atomic_imin:
396 case nir_intrinsic_ssbo_atomic_umin:
397 case nir_intrinsic_ssbo_atomic_imax:
398 case nir_intrinsic_ssbo_atomic_umax:
399 case nir_intrinsic_ssbo_atomic_and:
400 case nir_intrinsic_ssbo_atomic_or:
401 case nir_intrinsic_ssbo_atomic_xor:
402 case nir_intrinsic_ssbo_atomic_exchange:
403 case nir_intrinsic_ssbo_atomic_comp_swap:
404 case nir_intrinsic_image_deref_atomic_add:
405 case nir_intrinsic_image_deref_atomic_umin:
406 case nir_intrinsic_image_deref_atomic_imin:
407 case nir_intrinsic_image_deref_atomic_umax:
408 case nir_intrinsic_image_deref_atomic_imax:
409 case nir_intrinsic_image_deref_atomic_and:
410 case nir_intrinsic_image_deref_atomic_or:
411 case nir_intrinsic_image_deref_atomic_xor:
412 case nir_intrinsic_image_deref_atomic_exchange:
413 case nir_intrinsic_image_deref_atomic_comp_swap:
414 case nir_intrinsic_image_deref_size:
415 case nir_intrinsic_shared_atomic_add:
416 case nir_intrinsic_shared_atomic_imin:
417 case nir_intrinsic_shared_atomic_umin:
418 case nir_intrinsic_shared_atomic_imax:
419 case nir_intrinsic_shared_atomic_umax:
420 case nir_intrinsic_shared_atomic_and:
421 case nir_intrinsic_shared_atomic_or:
422 case nir_intrinsic_shared_atomic_xor:
423 case nir_intrinsic_shared_atomic_exchange:
424 case nir_intrinsic_shared_atomic_comp_swap:
425 case nir_intrinsic_load_scratch:
426 type = RegType::vgpr;
427 break;
428 case nir_intrinsic_shuffle:
429 case nir_intrinsic_quad_broadcast:
430 case nir_intrinsic_quad_swap_horizontal:
431 case nir_intrinsic_quad_swap_vertical:
432 case nir_intrinsic_quad_swap_diagonal:
433 case nir_intrinsic_quad_swizzle_amd:
434 case nir_intrinsic_masked_swizzle_amd:
435 case nir_intrinsic_inclusive_scan:
436 case nir_intrinsic_exclusive_scan:
437 if (!ctx->divergent_vals[intrinsic->dest.ssa.index]) {
438 type = RegType::sgpr;
439 } else if (intrinsic->src[0].ssa->bit_size == 1) {
440 type = RegType::sgpr;
441 size = 2;
442 } else {
443 type = RegType::vgpr;
444 }
445 break;
446 case nir_intrinsic_load_view_index:
447 type = ctx->stage == fragment_fs ? RegType::vgpr : RegType::sgpr;
448 break;
449 case nir_intrinsic_load_front_face:
450 case nir_intrinsic_load_helper_invocation:
451 case nir_intrinsic_is_helper_invocation:
452 type = RegType::sgpr;
453 size = 2;
454 break;
455 case nir_intrinsic_reduce:
456 if (nir_intrinsic_cluster_size(intrinsic) == 0 ||
457 !ctx->divergent_vals[intrinsic->dest.ssa.index]) {
458 type = RegType::sgpr;
459 } else if (intrinsic->src[0].ssa->bit_size == 1) {
460 type = RegType::sgpr;
461 size = 2;
462 } else {
463 type = RegType::vgpr;
464 }
465 break;
466 case nir_intrinsic_load_ubo:
467 case nir_intrinsic_load_ssbo:
468 case nir_intrinsic_load_global:
469 case nir_intrinsic_vulkan_resource_index:
470 type = ctx->divergent_vals[intrinsic->dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
471 break;
472 /* due to copy propagation, the swizzled imov is removed if num dest components == 1 */
473 case nir_intrinsic_load_shared:
474 if (ctx->divergent_vals[intrinsic->dest.ssa.index])
475 type = RegType::vgpr;
476 else
477 type = RegType::sgpr;
478 break;
479 default:
480 for (unsigned i = 0; i < nir_intrinsic_infos[intrinsic->intrinsic].num_srcs; i++) {
481 if (allocated[intrinsic->src[i].ssa->index].type() == RegType::vgpr)
482 type = RegType::vgpr;
483 }
484 break;
485 }
486 allocated[intrinsic->dest.ssa.index] = Temp(0, RegClass(type, size));
487
488 switch(intrinsic->intrinsic) {
489 case nir_intrinsic_load_barycentric_sample:
490 case nir_intrinsic_load_barycentric_pixel:
491 case nir_intrinsic_load_barycentric_centroid:
492 case nir_intrinsic_load_barycentric_at_sample:
493 case nir_intrinsic_load_barycentric_at_offset: {
494 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(intrinsic);
495 ctx->fs_vgpr_args[get_interp_input(intrinsic->intrinsic, mode)] = true;
496 break;
497 }
498 case nir_intrinsic_load_front_face:
499 ctx->fs_vgpr_args[fs_input::front_face] = true;
500 break;
501 case nir_intrinsic_load_frag_coord:
502 case nir_intrinsic_load_sample_pos: {
503 uint8_t mask = nir_ssa_def_components_read(&intrinsic->dest.ssa);
504 for (unsigned i = 0; i < 4; i++) {
505 if (mask & (1 << i))
506 ctx->fs_vgpr_args[fs_input::frag_pos_0 + i] = true;
507
508 }
509 break;
510 }
511 case nir_intrinsic_load_sample_id:
512 ctx->fs_vgpr_args[fs_input::ancillary] = true;
513 break;
514 case nir_intrinsic_load_sample_mask_in:
515 ctx->fs_vgpr_args[fs_input::ancillary] = true;
516 ctx->fs_vgpr_args[fs_input::sample_coverage] = true;
517 break;
518 default:
519 break;
520 }
521 break;
522 }
523 case nir_instr_type_tex: {
524 nir_tex_instr* tex = nir_instr_as_tex(instr);
525 unsigned size = tex->dest.ssa.num_components;
526
527 if (tex->dest.ssa.bit_size == 64)
528 size *= 2;
529 if (tex->op == nir_texop_texture_samples)
530 assert(!ctx->divergent_vals[tex->dest.ssa.index]);
531 if (ctx->divergent_vals[tex->dest.ssa.index])
532 allocated[tex->dest.ssa.index] = Temp(0, RegClass(RegType::vgpr, size));
533 else
534 allocated[tex->dest.ssa.index] = Temp(0, RegClass(RegType::sgpr, size));
535 break;
536 }
537 case nir_instr_type_parallel_copy: {
538 nir_foreach_parallel_copy_entry(entry, nir_instr_as_parallel_copy(instr)) {
539 allocated[entry->dest.ssa.index] = allocated[entry->src.ssa->index];
540 }
541 break;
542 }
543 case nir_instr_type_ssa_undef: {
544 unsigned size = nir_instr_as_ssa_undef(instr)->def.num_components;
545 if (nir_instr_as_ssa_undef(instr)->def.bit_size == 64)
546 size *= 2;
547 allocated[nir_instr_as_ssa_undef(instr)->def.index] = Temp(0, RegClass(RegType::sgpr, size));
548 break;
549 }
550 case nir_instr_type_phi: {
551 nir_phi_instr* phi = nir_instr_as_phi(instr);
552 RegType type;
553 unsigned size = phi->dest.ssa.num_components;
554
555 if (phi->dest.ssa.bit_size == 1) {
556 assert(size == 1 && "multiple components not yet supported on boolean phis.");
557 type = RegType::sgpr;
558 size *= ctx->divergent_vals[phi->dest.ssa.index] ? 2 : 1;
559 allocated[phi->dest.ssa.index] = Temp(0, RegClass(type, size));
560 break;
561 }
562
563 if (ctx->divergent_vals[phi->dest.ssa.index]) {
564 type = RegType::vgpr;
565 } else {
566 type = RegType::sgpr;
567 nir_foreach_phi_src (src, phi) {
568 if (allocated[src->src.ssa->index].type() == RegType::vgpr)
569 type = RegType::vgpr;
570 if (allocated[src->src.ssa->index].type() == RegType::none)
571 done = false;
572 }
573 }
574
575 size *= phi->dest.ssa.bit_size == 64 ? 2 : 1;
576 RegClass rc = RegClass(type, size);
577 if (rc != allocated[phi->dest.ssa.index].regClass()) {
578 done = false;
579 } else {
580 nir_foreach_phi_src(src, phi)
581 assert(allocated[src->src.ssa->index].size() == rc.size());
582 }
583 allocated[phi->dest.ssa.index] = Temp(0, rc);
584 break;
585 }
586 default:
587 break;
588 }
589 }
590 }
591 }
592
593 for (unsigned i = 0; i < impl->ssa_alloc; i++)
594 allocated[i] = Temp(ctx->program->allocateId(), allocated[i].regClass());
595
596 ctx->allocated.reset(allocated.release());
597 }
598
599 struct user_sgpr_info {
600 uint8_t num_sgpr;
601 uint8_t remaining_sgprs;
602 uint8_t user_sgpr_idx;
603 bool need_ring_offsets;
604 bool indirect_all_descriptor_sets;
605 };
606
607 static void allocate_inline_push_consts(isel_context *ctx,
608 user_sgpr_info& user_sgpr_info)
609 {
610 uint8_t remaining_sgprs = user_sgpr_info.remaining_sgprs;
611
612 /* Only supported if shaders use push constants. */
613 if (ctx->program->info->min_push_constant_used == UINT8_MAX)
614 return;
615
616 /* Only supported if shaders don't have indirect push constants. */
617 if (ctx->program->info->has_indirect_push_constants)
618 return;
619
620 /* Only supported for 32-bit push constants. */
621 //TODO: it's possible that some day, the load/store vectorization could make this inaccurate
622 if (!ctx->program->info->has_only_32bit_push_constants)
623 return;
624
625 uint8_t num_push_consts =
626 (ctx->program->info->max_push_constant_used -
627 ctx->program->info->min_push_constant_used) / 4;
628
629 /* Check if the number of user SGPRs is large enough. */
630 if (num_push_consts < remaining_sgprs) {
631 ctx->program->info->num_inline_push_consts = num_push_consts;
632 } else {
633 ctx->program->info->num_inline_push_consts = remaining_sgprs;
634 }
635
636 /* Clamp to the maximum number of allowed inlined push constants. */
637 if (ctx->program->info->num_inline_push_consts > MAX_INLINE_PUSH_CONSTS)
638 ctx->program->info->num_inline_push_consts = MAX_INLINE_PUSH_CONSTS;
639
640 if (ctx->program->info->num_inline_push_consts == num_push_consts &&
641 !ctx->program->info->loads_dynamic_offsets) {
642 /* Disable the default push constants path if all constants are
643 * inlined and if shaders don't use dynamic descriptors.
644 */
645 ctx->program->info->loads_push_constants = false;
646 user_sgpr_info.num_sgpr--;
647 user_sgpr_info.remaining_sgprs++;
648 }
649
650 ctx->program->info->base_inline_push_consts =
651 ctx->program->info->min_push_constant_used / 4;
652
653 user_sgpr_info.num_sgpr += ctx->program->info->num_inline_push_consts;
654 user_sgpr_info.remaining_sgprs -= ctx->program->info->num_inline_push_consts;
655 }
656
657 static void allocate_user_sgprs(isel_context *ctx,
658 bool needs_view_index, user_sgpr_info& user_sgpr_info)
659 {
660 memset(&user_sgpr_info, 0, sizeof(struct user_sgpr_info));
661 uint32_t user_sgpr_count = 0;
662
663 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
664 if (ctx->stage != fragment_fs &&
665 ctx->stage != compute_cs
666 /*|| ctx->is_gs_copy_shader */)
667 user_sgpr_info.need_ring_offsets = true;
668
669 if (ctx->stage == fragment_fs &&
670 ctx->program->info->ps.needs_sample_positions)
671 user_sgpr_info.need_ring_offsets = true;
672
673 /* 2 user sgprs will nearly always be allocated for scratch/rings */
674 if (ctx->options->supports_spill || user_sgpr_info.need_ring_offsets || ctx->scratch_enabled)
675 user_sgpr_count += 2;
676
677 switch (ctx->stage) {
678 case vertex_vs:
679 /* if (!ctx->is_gs_copy_shader) */ {
680 if (ctx->program->info->vs.has_vertex_buffers)
681 user_sgpr_count++;
682 user_sgpr_count += ctx->program->info->vs.needs_draw_id ? 3 : 2;
683 }
684 break;
685 case fragment_fs:
686 //user_sgpr_count += ctx->program->info->ps.needs_sample_positions;
687 break;
688 case compute_cs:
689 if (ctx->program->info->cs.uses_grid_size)
690 user_sgpr_count += 3;
691 break;
692 default:
693 unreachable("Shader stage not implemented");
694 }
695
696 if (needs_view_index)
697 user_sgpr_count++;
698
699 if (ctx->program->info->loads_push_constants)
700 user_sgpr_count += 1; /* we use 32bit pointers */
701
702 if (ctx->program->info->so.num_outputs)
703 user_sgpr_count += 1; /* we use 32bit pointers */
704
705 uint32_t available_sgprs = ctx->options->chip_class >= GFX9 && !(ctx->stage & hw_cs) ? 32 : 16;
706 uint32_t remaining_sgprs = available_sgprs - user_sgpr_count;
707 uint32_t num_desc_set = util_bitcount(ctx->program->info->desc_set_used_mask);
708
709 if (available_sgprs < user_sgpr_count + num_desc_set) {
710 user_sgpr_info.indirect_all_descriptor_sets = true;
711 user_sgpr_info.num_sgpr = user_sgpr_count + 1;
712 user_sgpr_info.remaining_sgprs = remaining_sgprs - 1;
713 } else {
714 user_sgpr_info.num_sgpr = user_sgpr_count + num_desc_set;
715 user_sgpr_info.remaining_sgprs = remaining_sgprs - num_desc_set;
716 }
717
718 allocate_inline_push_consts(ctx, user_sgpr_info);
719 }
720
721 #define MAX_ARGS 64
722 struct arg_info {
723 RegClass types[MAX_ARGS];
724 Temp *assign[MAX_ARGS];
725 PhysReg reg[MAX_ARGS];
726 unsigned array_params_mask;
727 uint8_t count;
728 uint8_t sgpr_count;
729 uint8_t num_sgprs_used;
730 uint8_t num_vgprs_used;
731 };
732
733 static void
734 add_arg(arg_info *info, RegClass rc, Temp *param_ptr, unsigned reg)
735 {
736 assert(info->count < MAX_ARGS);
737
738 info->assign[info->count] = param_ptr;
739 info->types[info->count] = rc;
740
741 if (rc.type() == RegType::sgpr) {
742 info->num_sgprs_used += rc.size();
743 info->sgpr_count++;
744 info->reg[info->count] = PhysReg{reg};
745 } else {
746 assert(rc.type() == RegType::vgpr);
747 info->num_vgprs_used += rc.size();
748 info->reg[info->count] = PhysReg{reg + 256};
749 }
750 info->count++;
751 }
752
753 static void
754 set_loc(struct radv_userdata_info *ud_info, uint8_t *sgpr_idx, uint8_t num_sgprs)
755 {
756 ud_info->sgpr_idx = *sgpr_idx;
757 ud_info->num_sgprs = num_sgprs;
758 *sgpr_idx += num_sgprs;
759 }
760
761 static void
762 set_loc_shader(isel_context *ctx, int idx, uint8_t *sgpr_idx,
763 uint8_t num_sgprs)
764 {
765 struct radv_userdata_info *ud_info = &ctx->program->info->user_sgprs_locs.shader_data[idx];
766 assert(ud_info);
767
768 set_loc(ud_info, sgpr_idx, num_sgprs);
769 }
770
771 static void
772 set_loc_shader_ptr(isel_context *ctx, int idx, uint8_t *sgpr_idx)
773 {
774 bool use_32bit_pointers = idx != AC_UD_SCRATCH_RING_OFFSETS;
775
776 set_loc_shader(ctx, idx, sgpr_idx, use_32bit_pointers ? 1 : 2);
777 }
778
779 static void
780 set_loc_desc(isel_context *ctx, int idx, uint8_t *sgpr_idx)
781 {
782 struct radv_userdata_locations *locs = &ctx->program->info->user_sgprs_locs;
783 struct radv_userdata_info *ud_info = &locs->descriptor_sets[idx];
784 assert(ud_info);
785
786 set_loc(ud_info, sgpr_idx, 1);
787 locs->descriptor_sets_enabled |= 1 << idx;
788 }
789
790 static void
791 declare_global_input_sgprs(isel_context *ctx,
792 /* bool has_previous_stage, gl_shader_stage previous_stage, */
793 user_sgpr_info *user_sgpr_info,
794 struct arg_info *args,
795 Temp *desc_sets)
796 {
797 /* 1 for each descriptor set */
798 if (!user_sgpr_info->indirect_all_descriptor_sets) {
799 uint32_t mask = ctx->program->info->desc_set_used_mask;
800 while (mask) {
801 int i = u_bit_scan(&mask);
802 add_arg(args, s1, &desc_sets[i], user_sgpr_info->user_sgpr_idx);
803 set_loc_desc(ctx, i, &user_sgpr_info->user_sgpr_idx);
804 }
805 /* NIR->LLVM might have set this to true if RADV_DEBUG=compiletime */
806 ctx->program->info->need_indirect_descriptor_sets = false;
807 } else {
808 add_arg(args, s1, desc_sets, user_sgpr_info->user_sgpr_idx);
809 set_loc_shader_ptr(ctx, AC_UD_INDIRECT_DESCRIPTOR_SETS, &user_sgpr_info->user_sgpr_idx);
810 ctx->program->info->need_indirect_descriptor_sets = true;
811 }
812
813 if (ctx->program->info->loads_push_constants) {
814 /* 1 for push constants and dynamic descriptors */
815 add_arg(args, s1, &ctx->push_constants, user_sgpr_info->user_sgpr_idx);
816 set_loc_shader_ptr(ctx, AC_UD_PUSH_CONSTANTS, &user_sgpr_info->user_sgpr_idx);
817 }
818
819 if (ctx->program->info->num_inline_push_consts) {
820 unsigned count = ctx->program->info->num_inline_push_consts;
821 for (unsigned i = 0; i < count; i++)
822 add_arg(args, s1, &ctx->inline_push_consts[i], user_sgpr_info->user_sgpr_idx + i);
823 set_loc_shader(ctx, AC_UD_INLINE_PUSH_CONSTANTS, &user_sgpr_info->user_sgpr_idx, count);
824
825 ctx->num_inline_push_consts = ctx->program->info->num_inline_push_consts;
826 ctx->base_inline_push_consts = ctx->program->info->base_inline_push_consts;
827 }
828
829 if (ctx->program->info->so.num_outputs) {
830 add_arg(args, s1, &ctx->streamout_buffers, user_sgpr_info->user_sgpr_idx);
831 set_loc_shader_ptr(ctx, AC_UD_STREAMOUT_BUFFERS, &user_sgpr_info->user_sgpr_idx);
832 }
833 }
834
835 static void
836 declare_vs_input_vgprs(isel_context *ctx, struct arg_info *args)
837 {
838 unsigned vgpr_idx = 0;
839 add_arg(args, v1, &ctx->vertex_id, vgpr_idx++);
840 if (ctx->options->chip_class >= GFX10) {
841 add_arg(args, v1, NULL, vgpr_idx++); /* unused */
842 add_arg(args, v1, &ctx->vs_prim_id, vgpr_idx++);
843 add_arg(args, v1, &ctx->instance_id, vgpr_idx++);
844 } else {
845 if (ctx->options->key.vs.out.as_ls) {
846 add_arg(args, v1, &ctx->rel_auto_id, vgpr_idx++);
847 add_arg(args, v1, &ctx->instance_id, vgpr_idx++);
848 } else {
849 add_arg(args, v1, &ctx->instance_id, vgpr_idx++);
850 add_arg(args, v1, &ctx->vs_prim_id, vgpr_idx++);
851 }
852 add_arg(args, v1, NULL, vgpr_idx); /* unused */
853 }
854 }
855
856 static void
857 declare_streamout_sgprs(isel_context *ctx, struct arg_info *args, unsigned *idx)
858 {
859 /* Streamout SGPRs. */
860 if (ctx->program->info->so.num_outputs) {
861 assert(ctx->stage & hw_vs);
862
863 if (ctx->stage != tess_eval_vs) {
864 add_arg(args, s1, &ctx->streamout_config, (*idx)++);
865 } else {
866 args->assign[args->count - 1] = &ctx->streamout_config;
867 args->types[args->count - 1] = s1;
868 }
869
870 add_arg(args, s1, &ctx->streamout_write_idx, (*idx)++);
871 }
872
873 /* A streamout buffer offset is loaded if the stride is non-zero. */
874 for (unsigned i = 0; i < 4; i++) {
875 if (!ctx->program->info->so.strides[i])
876 continue;
877
878 add_arg(args, s1, &ctx->streamout_offset[i], (*idx)++);
879 }
880 }
881
882 static bool needs_view_index_sgpr(isel_context *ctx)
883 {
884 switch (ctx->stage) {
885 case vertex_vs:
886 return ctx->program->info->needs_multiview_view_index || ctx->options->key.has_multiview_view_index;
887 case tess_eval_vs:
888 return ctx->program->info->needs_multiview_view_index && ctx->options->key.has_multiview_view_index;
889 case vertex_ls:
890 case vertex_es:
891 case vertex_tess_control_hs:
892 case vertex_geometry_gs:
893 case tess_control_hs:
894 case tess_eval_es:
895 case tess_eval_geometry_gs:
896 case geometry_gs:
897 return ctx->program->info->needs_multiview_view_index;
898 default:
899 return false;
900 }
901 }
902
903 static inline bool
904 add_fs_arg(isel_context *ctx, arg_info *args, unsigned &vgpr_idx, fs_input input, unsigned value, bool enable_next = false, RegClass rc = v1)
905 {
906 if (!ctx->fs_vgpr_args[input])
907 return false;
908
909 add_arg(args, rc, &ctx->fs_inputs[input], vgpr_idx);
910 vgpr_idx += rc.size();
911
912 if (enable_next) {
913 add_arg(args, rc, &ctx->fs_inputs[input + 1], vgpr_idx);
914 vgpr_idx += rc.size();
915 }
916
917 ctx->program->config->spi_ps_input_addr |= value;
918 ctx->program->config->spi_ps_input_ena |= value;
919 return true;
920 }
921
922 void add_startpgm(struct isel_context *ctx)
923 {
924 user_sgpr_info user_sgpr_info;
925 bool needs_view_index = needs_view_index_sgpr(ctx);
926 allocate_user_sgprs(ctx, needs_view_index, user_sgpr_info);
927 arg_info args = {};
928
929 /* this needs to be in sgprs 0 and 1 */
930 if (ctx->options->supports_spill || user_sgpr_info.need_ring_offsets || ctx->scratch_enabled) {
931 add_arg(&args, s2, &ctx->private_segment_buffer, 0);
932 set_loc_shader_ptr(ctx, AC_UD_SCRATCH_RING_OFFSETS, &user_sgpr_info.user_sgpr_idx);
933 }
934
935 unsigned vgpr_idx = 0;
936 switch (ctx->stage) {
937 case vertex_vs: {
938 declare_global_input_sgprs(ctx, &user_sgpr_info, &args, ctx->descriptor_sets);
939 if (ctx->program->info->vs.has_vertex_buffers) {
940 add_arg(&args, s1, &ctx->vertex_buffers, user_sgpr_info.user_sgpr_idx);
941 set_loc_shader_ptr(ctx, AC_UD_VS_VERTEX_BUFFERS, &user_sgpr_info.user_sgpr_idx);
942 }
943 add_arg(&args, s1, &ctx->base_vertex, user_sgpr_info.user_sgpr_idx);
944 add_arg(&args, s1, &ctx->start_instance, user_sgpr_info.user_sgpr_idx + 1);
945 if (ctx->program->info->vs.needs_draw_id) {
946 add_arg(&args, s1, &ctx->draw_id, user_sgpr_info.user_sgpr_idx + 2);
947 set_loc_shader(ctx, AC_UD_VS_BASE_VERTEX_START_INSTANCE, &user_sgpr_info.user_sgpr_idx, 3);
948 } else
949 set_loc_shader(ctx, AC_UD_VS_BASE_VERTEX_START_INSTANCE, &user_sgpr_info.user_sgpr_idx, 2);
950
951 if (needs_view_index) {
952 add_arg(&args, s1, &ctx->view_index, user_sgpr_info.user_sgpr_idx);
953 set_loc_shader(ctx, AC_UD_VIEW_INDEX, &user_sgpr_info.user_sgpr_idx, 1);
954 }
955
956 assert(user_sgpr_info.user_sgpr_idx == user_sgpr_info.num_sgpr);
957 unsigned idx = user_sgpr_info.user_sgpr_idx;
958 if (ctx->options->key.vs.out.as_es)
959 add_arg(&args, s1, &ctx->es2gs_offset, idx++);
960 else
961 declare_streamout_sgprs(ctx, &args, &idx);
962
963 if (ctx->scratch_enabled)
964 add_arg(&args, s1, &ctx->scratch_offset, idx++);
965
966 declare_vs_input_vgprs(ctx, &args);
967 break;
968 }
969 case fragment_fs: {
970 declare_global_input_sgprs(ctx, &user_sgpr_info, &args, ctx->descriptor_sets);
971
972 assert(user_sgpr_info.user_sgpr_idx == user_sgpr_info.num_sgpr);
973 add_arg(&args, s1, &ctx->prim_mask, user_sgpr_info.user_sgpr_idx);
974
975 if (ctx->scratch_enabled)
976 add_arg(&args, s1, &ctx->scratch_offset, user_sgpr_info.user_sgpr_idx + 1);
977
978 ctx->program->config->spi_ps_input_addr = 0;
979 ctx->program->config->spi_ps_input_ena = 0;
980
981 bool has_interp_mode = false;
982
983 has_interp_mode |= add_fs_arg(ctx, &args, vgpr_idx, fs_input::persp_sample_p1, S_0286CC_PERSP_SAMPLE_ENA(1), true);
984 has_interp_mode |= add_fs_arg(ctx, &args, vgpr_idx, fs_input::persp_center_p1, S_0286CC_PERSP_CENTER_ENA(1), true);
985 has_interp_mode |= add_fs_arg(ctx, &args, vgpr_idx, fs_input::persp_centroid_p1, S_0286CC_PERSP_CENTROID_ENA(1), true);
986 has_interp_mode |= add_fs_arg(ctx, &args, vgpr_idx, fs_input::persp_pull_model, S_0286CC_PERSP_PULL_MODEL_ENA(1), false, v3);
987
988 if (!has_interp_mode && ctx->fs_vgpr_args[fs_input::frag_pos_3]) {
989 /* If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be enabled too */
990 ctx->fs_vgpr_args[fs_input::persp_center_p1] = true;
991 has_interp_mode = add_fs_arg(ctx, &args, vgpr_idx, fs_input::persp_center_p1, S_0286CC_PERSP_CENTER_ENA(1), true);
992 }
993
994 has_interp_mode |= add_fs_arg(ctx, &args, vgpr_idx, fs_input::linear_sample_p1, S_0286CC_LINEAR_SAMPLE_ENA(1), true);
995 has_interp_mode |= add_fs_arg(ctx, &args, vgpr_idx, fs_input::linear_center_p1, S_0286CC_LINEAR_CENTER_ENA(1), true);
996 has_interp_mode |= add_fs_arg(ctx, &args, vgpr_idx, fs_input::linear_centroid_p1, S_0286CC_LINEAR_CENTROID_ENA(1), true);
997 has_interp_mode |= add_fs_arg(ctx, &args, vgpr_idx, fs_input::line_stipple, S_0286CC_LINE_STIPPLE_TEX_ENA(1));
998
999 if (!has_interp_mode) {
1000 /* At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled */
1001 ctx->fs_vgpr_args[fs_input::persp_center_p1] = true;
1002 has_interp_mode = add_fs_arg(ctx, &args, vgpr_idx, fs_input::persp_center_p1, S_0286CC_PERSP_CENTER_ENA(1), true);
1003 }
1004
1005 add_fs_arg(ctx, &args, vgpr_idx, fs_input::frag_pos_0, S_0286CC_POS_X_FLOAT_ENA(1));
1006 add_fs_arg(ctx, &args, vgpr_idx, fs_input::frag_pos_1, S_0286CC_POS_Y_FLOAT_ENA(1));
1007 add_fs_arg(ctx, &args, vgpr_idx, fs_input::frag_pos_2, S_0286CC_POS_Z_FLOAT_ENA(1));
1008 add_fs_arg(ctx, &args, vgpr_idx, fs_input::frag_pos_3, S_0286CC_POS_W_FLOAT_ENA(1));
1009
1010 add_fs_arg(ctx, &args, vgpr_idx, fs_input::front_face, S_0286CC_FRONT_FACE_ENA(1));
1011 add_fs_arg(ctx, &args, vgpr_idx, fs_input::ancillary, S_0286CC_ANCILLARY_ENA(1));
1012 add_fs_arg(ctx, &args, vgpr_idx, fs_input::sample_coverage, S_0286CC_SAMPLE_COVERAGE_ENA(1));
1013 add_fs_arg(ctx, &args, vgpr_idx, fs_input::fixed_pt, S_0286CC_POS_FIXED_PT_ENA(1));
1014
1015 ASSERTED bool unset_interp_mode = !(ctx->program->config->spi_ps_input_addr & 0x7F) ||
1016 (G_0286CC_POS_W_FLOAT_ENA(ctx->program->config->spi_ps_input_addr)
1017 && !(ctx->program->config->spi_ps_input_addr & 0xF));
1018
1019 assert(has_interp_mode);
1020 assert(!unset_interp_mode);
1021 break;
1022 }
1023 case compute_cs: {
1024 declare_global_input_sgprs(ctx, &user_sgpr_info, &args, ctx->descriptor_sets);
1025
1026 if (ctx->program->info->cs.uses_grid_size) {
1027 add_arg(&args, s1, &ctx->num_workgroups[0], user_sgpr_info.user_sgpr_idx);
1028 add_arg(&args, s1, &ctx->num_workgroups[1], user_sgpr_info.user_sgpr_idx + 1);
1029 add_arg(&args, s1, &ctx->num_workgroups[2], user_sgpr_info.user_sgpr_idx + 2);
1030 set_loc_shader(ctx, AC_UD_CS_GRID_SIZE, &user_sgpr_info.user_sgpr_idx, 3);
1031 }
1032 assert(user_sgpr_info.user_sgpr_idx == user_sgpr_info.num_sgpr);
1033 unsigned idx = user_sgpr_info.user_sgpr_idx;
1034 for (unsigned i = 0; i < 3; i++) {
1035 if (ctx->program->info->cs.uses_block_id[i])
1036 add_arg(&args, s1, &ctx->workgroup_ids[i], idx++);
1037 }
1038
1039 if (ctx->program->info->cs.uses_local_invocation_idx)
1040 add_arg(&args, s1, &ctx->tg_size, idx++);
1041 if (ctx->scratch_enabled)
1042 add_arg(&args, s1, &ctx->scratch_offset, idx++);
1043
1044 add_arg(&args, v1, &ctx->local_invocation_ids[0], vgpr_idx++);
1045 add_arg(&args, v1, &ctx->local_invocation_ids[1], vgpr_idx++);
1046 add_arg(&args, v1, &ctx->local_invocation_ids[2], vgpr_idx++);
1047 break;
1048 }
1049 default:
1050 unreachable("Shader stage not implemented");
1051 }
1052
1053 ctx->program->info->num_input_vgprs = 0;
1054 ctx->program->info->num_input_sgprs = args.num_sgprs_used;
1055 ctx->program->info->num_user_sgprs = user_sgpr_info.num_sgpr;
1056 ctx->program->info->num_input_vgprs = args.num_vgprs_used;
1057
1058 if (ctx->stage == fragment_fs) {
1059 /* Verify that we have a correct assumption about input VGPR count */
1060 ASSERTED unsigned input_vgpr_cnt = ac_get_fs_input_vgpr_cnt(ctx->program->config, nullptr, nullptr);
1061 assert(input_vgpr_cnt == ctx->program->info->num_input_vgprs);
1062 }
1063
1064 aco_ptr<Pseudo_instruction> startpgm{create_instruction<Pseudo_instruction>(aco_opcode::p_startpgm, Format::PSEUDO, 0, args.count + 1)};
1065 for (unsigned i = 0; i < args.count; i++) {
1066 if (args.assign[i]) {
1067 *args.assign[i] = Temp{ctx->program->allocateId(), args.types[i]};
1068 startpgm->definitions[i] = Definition(*args.assign[i]);
1069 startpgm->definitions[i].setFixed(args.reg[i]);
1070 }
1071 }
1072 startpgm->definitions[args.count] = Definition{ctx->program->allocateId(), exec, s2};
1073 ctx->block->instructions.push_back(std::move(startpgm));
1074 }
1075
1076 int
1077 type_size(const struct glsl_type *type, bool bindless)
1078 {
1079 // TODO: don't we need type->std430_base_alignment() here?
1080 return glsl_count_attribute_slots(type, false);
1081 }
1082
1083 void
1084 shared_var_info(const struct glsl_type *type, unsigned *size, unsigned *align)
1085 {
1086 assert(glsl_type_is_vector_or_scalar(type));
1087
1088 uint32_t comp_size = glsl_type_is_boolean(type)
1089 ? 4 : glsl_get_bit_size(type) / 8;
1090 unsigned length = glsl_get_vector_elements(type);
1091 *size = comp_size * length,
1092 *align = comp_size;
1093 }
1094
1095 int
1096 get_align(nir_variable_mode mode, bool is_store, unsigned bit_size, unsigned num_components)
1097 {
1098 /* TODO: ACO doesn't have good support for non-32-bit reads/writes yet */
1099 if (bit_size != 32)
1100 return -1;
1101
1102 switch (mode) {
1103 case nir_var_mem_ubo:
1104 case nir_var_mem_ssbo:
1105 //case nir_var_mem_push_const: enable with 1240!
1106 case nir_var_mem_shared:
1107 /* TODO: what are the alignment requirements for LDS? */
1108 return num_components <= 4 ? 4 : -1;
1109 default:
1110 return -1;
1111 }
1112 }
1113
1114 void
1115 setup_vs_variables(isel_context *ctx, nir_shader *nir)
1116 {
1117 nir_foreach_variable(variable, &nir->inputs)
1118 {
1119 variable->data.driver_location = variable->data.location * 4;
1120 }
1121 nir_foreach_variable(variable, &nir->outputs)
1122 {
1123 variable->data.driver_location = variable->data.location * 4;
1124 }
1125
1126 radv_vs_output_info *outinfo = &ctx->program->info->vs.outinfo;
1127
1128 memset(outinfo->vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
1129 sizeof(outinfo->vs_output_param_offset));
1130
1131 ctx->needs_instance_id = ctx->program->info->vs.needs_instance_id;
1132
1133 bool export_clip_dists = ctx->options->key.vs_common_out.export_clip_dists;
1134
1135 outinfo->param_exports = 0;
1136 int pos_written = 0x1;
1137 if (outinfo->writes_pointsize || outinfo->writes_viewport_index || outinfo->writes_layer)
1138 pos_written |= 1 << 1;
1139
1140 nir_foreach_variable(variable, &nir->outputs)
1141 {
1142 int idx = variable->data.location;
1143 unsigned slots = variable->type->count_attribute_slots(false);
1144 if (variable->data.compact) {
1145 unsigned component_count = variable->data.location_frac + variable->type->length;
1146 slots = (component_count + 3) / 4;
1147 }
1148
1149 if (idx >= VARYING_SLOT_VAR0 || idx == VARYING_SLOT_LAYER || idx == VARYING_SLOT_PRIMITIVE_ID ||
1150 ((idx == VARYING_SLOT_CLIP_DIST0 || idx == VARYING_SLOT_CLIP_DIST1) && export_clip_dists)) {
1151 for (unsigned i = 0; i < slots; i++) {
1152 if (outinfo->vs_output_param_offset[idx + i] == AC_EXP_PARAM_UNDEFINED)
1153 outinfo->vs_output_param_offset[idx + i] = outinfo->param_exports++;
1154 }
1155 }
1156 }
1157 if (outinfo->writes_layer &&
1158 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] == AC_EXP_PARAM_UNDEFINED) {
1159 /* when ctx->options->key.has_multiview_view_index = true, the layer
1160 * variable isn't declared in NIR and it's isel's job to get the layer */
1161 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] = outinfo->param_exports++;
1162 }
1163
1164 if (outinfo->export_prim_id) {
1165 assert(outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] == AC_EXP_PARAM_UNDEFINED);
1166 outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = outinfo->param_exports++;
1167 }
1168
1169 ctx->num_clip_distances = util_bitcount(outinfo->clip_dist_mask);
1170 ctx->num_cull_distances = util_bitcount(outinfo->cull_dist_mask);
1171
1172 assert(ctx->num_clip_distances + ctx->num_cull_distances <= 8);
1173
1174 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
1175 pos_written |= 1 << 2;
1176 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
1177 pos_written |= 1 << 3;
1178
1179 outinfo->pos_exports = util_bitcount(pos_written);
1180 }
1181
1182 void
1183 setup_variables(isel_context *ctx, nir_shader *nir)
1184 {
1185 switch (nir->info.stage) {
1186 case MESA_SHADER_FRAGMENT: {
1187 nir_foreach_variable(variable, &nir->outputs)
1188 {
1189 int idx = variable->data.location + variable->data.index;
1190 variable->data.driver_location = idx * 4;
1191 }
1192 break;
1193 }
1194 case MESA_SHADER_COMPUTE: {
1195 unsigned lds_allocation_size_unit = 4 * 64;
1196 if (ctx->program->chip_class >= GFX7)
1197 lds_allocation_size_unit = 4 * 128;
1198 ctx->program->config->lds_size = (nir->info.cs.shared_size + lds_allocation_size_unit - 1) / lds_allocation_size_unit;
1199 break;
1200 }
1201 case MESA_SHADER_VERTEX: {
1202 setup_vs_variables(ctx, nir);
1203 break;
1204 }
1205 default:
1206 unreachable("Unhandled shader stage.");
1207 }
1208 }
1209
1210 isel_context
1211 setup_isel_context(Program* program,
1212 unsigned shader_count,
1213 struct nir_shader *const *shaders,
1214 ac_shader_config* config,
1215 radv_shader_info *info,
1216 radv_nir_compiler_options *options)
1217 {
1218 program->stage = 0;
1219 for (unsigned i = 0; i < shader_count; i++) {
1220 switch (shaders[i]->info.stage) {
1221 case MESA_SHADER_VERTEX:
1222 program->stage |= sw_vs;
1223 break;
1224 case MESA_SHADER_TESS_CTRL:
1225 program->stage |= sw_tcs;
1226 break;
1227 case MESA_SHADER_TESS_EVAL:
1228 program->stage |= sw_tes;
1229 break;
1230 case MESA_SHADER_GEOMETRY:
1231 program->stage |= sw_gs;
1232 break;
1233 case MESA_SHADER_FRAGMENT:
1234 program->stage |= sw_fs;
1235 break;
1236 case MESA_SHADER_COMPUTE:
1237 program->stage |= sw_cs;
1238 break;
1239 default:
1240 unreachable("Shader stage not implemented");
1241 }
1242 }
1243 if (program->stage == sw_vs)
1244 program->stage |= hw_vs;
1245 else if (program->stage == sw_fs)
1246 program->stage |= hw_fs;
1247 else if (program->stage == sw_cs)
1248 program->stage |= hw_cs;
1249 else
1250 unreachable("Shader stage not implemented");
1251
1252 program->config = config;
1253 program->info = info;
1254 program->chip_class = options->chip_class;
1255 program->family = options->family;
1256 program->wave_size = options->wave_size;
1257
1258 if (options->chip_class >= GFX10) {
1259 program->physical_sgprs = 2560; /* doesn't matter as long as it's at least 128 * 20 */
1260 program->sgpr_alloc_granule = 127;
1261 program->sgpr_limit = 106;
1262 } else if (program->chip_class >= GFX8) {
1263 program->physical_sgprs = 800;
1264 program->sgpr_alloc_granule = 15;
1265 program->sgpr_limit = 102;
1266 } else {
1267 program->physical_sgprs = 512;
1268 program->sgpr_alloc_granule = 7;
1269 if (options->family == CHIP_TONGA || options->family == CHIP_ICELAND)
1270 program->sgpr_limit = 94; /* workaround hardware bug */
1271 else
1272 program->sgpr_limit = 104;
1273 }
1274 /* TODO: we don't have to allocate VCC if we don't need it */
1275 program->needs_vcc = true;
1276
1277 for (unsigned i = 0; i < MAX_SETS; ++i)
1278 program->info->user_sgprs_locs.descriptor_sets[i].sgpr_idx = -1;
1279 for (unsigned i = 0; i < AC_UD_MAX_UD; ++i)
1280 program->info->user_sgprs_locs.shader_data[i].sgpr_idx = -1;
1281
1282 isel_context ctx = {};
1283 ctx.program = program;
1284 ctx.options = options;
1285 ctx.stage = program->stage;
1286
1287 for (unsigned i = 0; i < fs_input::max_inputs; ++i)
1288 ctx.fs_inputs[i] = Temp(0, v1);
1289 ctx.fs_inputs[fs_input::persp_pull_model] = Temp(0, v3);
1290 for (unsigned i = 0; i < MAX_SETS; ++i)
1291 ctx.descriptor_sets[i] = Temp(0, s1);
1292 for (unsigned i = 0; i < MAX_INLINE_PUSH_CONSTS; ++i)
1293 ctx.inline_push_consts[i] = Temp(0, s1);
1294 for (unsigned i = 0; i <= VARYING_SLOT_VAR31; ++i) {
1295 for (unsigned j = 0; j < 4; ++j)
1296 ctx.vs_output.outputs[i][j] = Temp(0, v1);
1297 }
1298
1299 for (unsigned i = 0; i < shader_count; i++) {
1300 nir_shader *nir = shaders[i];
1301
1302 /* align and copy constant data */
1303 while (program->constant_data.size() % 4u)
1304 program->constant_data.push_back(0);
1305 ctx.constant_data_offset = program->constant_data.size();
1306 program->constant_data.insert(program->constant_data.end(),
1307 (uint8_t*)nir->constant_data,
1308 (uint8_t*)nir->constant_data + nir->constant_data_size);
1309
1310 /* the variable setup has to be done before lower_io / CSE */
1311 if (nir->info.stage == MESA_SHADER_COMPUTE)
1312 nir_lower_vars_to_explicit_types(nir, nir_var_mem_shared, shared_var_info);
1313 setup_variables(&ctx, nir);
1314
1315 /* optimize and lower memory operations */
1316 bool lower_to_scalar = false;
1317 bool lower_pack = false;
1318 // TODO: uncomment this once !1240 is merged
1319 /*if (nir_opt_load_store_vectorize(nir,
1320 (nir_variable_mode)(nir_var_mem_ssbo | nir_var_mem_ubo |
1321 nir_var_mem_push_const | nir_var_mem_shared),
1322 get_align)) {
1323 lower_to_scalar = true;
1324 lower_pack = true;
1325 }*/
1326 if (nir->info.stage == MESA_SHADER_COMPUTE)
1327 lower_to_scalar |= nir_lower_explicit_io(nir, nir_var_mem_shared, nir_address_format_32bit_offset);
1328 else
1329 nir_lower_io(nir, (nir_variable_mode)(nir_var_shader_in | nir_var_shader_out), type_size, (nir_lower_io_options)0);
1330 nir_lower_explicit_io(nir, nir_var_mem_global, nir_address_format_64bit_global);
1331
1332 if (lower_to_scalar)
1333 nir_lower_alu_to_scalar(nir, NULL, NULL);
1334 if (lower_pack)
1335 nir_lower_pack(nir);
1336
1337 /* lower ALU operations */
1338 // TODO: implement logic64 in aco, it's more effective for sgprs
1339 nir_lower_int64(nir, (nir_lower_int64_options) (nir_lower_imul64 |
1340 nir_lower_imul_high64 |
1341 nir_lower_imul_2x32_64 |
1342 nir_lower_divmod64 |
1343 nir_lower_logic64 |
1344 nir_lower_minmax64 |
1345 nir_lower_iabs64));
1346
1347 nir_opt_idiv_const(nir, 32);
1348 nir_lower_idiv(nir, nir_lower_idiv_precise);
1349
1350 /* optimize the lowered ALU operations */
1351 bool more_algebraic = true;
1352 while (more_algebraic) {
1353 more_algebraic = false;
1354 NIR_PASS_V(nir, nir_copy_prop);
1355 NIR_PASS_V(nir, nir_opt_dce);
1356 NIR_PASS_V(nir, nir_opt_constant_folding);
1357 NIR_PASS(more_algebraic, nir, nir_opt_algebraic);
1358 }
1359
1360 /* Do late algebraic optimization to turn add(a, neg(b)) back into
1361 * subs, then the mandatory cleanup after algebraic. Note that it may
1362 * produce fnegs, and if so then we need to keep running to squash
1363 * fneg(fneg(a)).
1364 */
1365 bool more_late_algebraic = true;
1366 while (more_late_algebraic) {
1367 more_late_algebraic = false;
1368 NIR_PASS(more_late_algebraic, nir, nir_opt_algebraic_late);
1369 NIR_PASS_V(nir, nir_opt_constant_folding);
1370 NIR_PASS_V(nir, nir_copy_prop);
1371 NIR_PASS_V(nir, nir_opt_dce);
1372 NIR_PASS_V(nir, nir_opt_cse);
1373 }
1374
1375 /* cleanup passes */
1376 nir_lower_load_const_to_scalar(nir);
1377 nir_opt_shrink_load(nir);
1378 nir_move_options move_opts = (nir_move_options)(
1379 nir_move_const_undef | nir_move_load_ubo | nir_move_load_input | nir_move_comparisons);
1380 nir_opt_sink(nir, move_opts);
1381 nir_opt_move(nir, move_opts);
1382 nir_convert_to_lcssa(nir, true, false);
1383 nir_lower_phis_to_scalar(nir);
1384
1385 nir_function_impl *func = nir_shader_get_entrypoint(nir);
1386 nir_index_ssa_defs(func);
1387
1388 if (options->dump_preoptir) {
1389 fprintf(stderr, "NIR shader before instruction selection:\n");
1390 nir_print_shader(nir, stderr);
1391 }
1392 }
1393
1394 unsigned scratch_size = 0;
1395 for (unsigned i = 0; i < shader_count; i++)
1396 scratch_size = std::max(scratch_size, shaders[i]->scratch_size);
1397 ctx.scratch_enabled = scratch_size > 0;
1398 ctx.program->config->scratch_bytes_per_wave = align(scratch_size * ctx.options->wave_size, 1024);
1399 ctx.program->config->float_mode = V_00B028_FP_64_DENORMS;
1400 ctx.program->info->wave_size = ctx.options->wave_size;
1401
1402 ctx.block = ctx.program->create_and_insert_block();
1403 ctx.block->loop_nest_depth = 0;
1404 ctx.block->kind = block_kind_top_level;
1405
1406 return ctx;
1407 }
1408
1409 }