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