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