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