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