aco: Use common argument handling
[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_image_deref_atomic_add:
326 case nir_intrinsic_image_deref_atomic_umin:
327 case nir_intrinsic_image_deref_atomic_imin:
328 case nir_intrinsic_image_deref_atomic_umax:
329 case nir_intrinsic_image_deref_atomic_imax:
330 case nir_intrinsic_image_deref_atomic_and:
331 case nir_intrinsic_image_deref_atomic_or:
332 case nir_intrinsic_image_deref_atomic_xor:
333 case nir_intrinsic_image_deref_atomic_exchange:
334 case nir_intrinsic_image_deref_atomic_comp_swap:
335 case nir_intrinsic_image_deref_size:
336 case nir_intrinsic_shared_atomic_add:
337 case nir_intrinsic_shared_atomic_imin:
338 case nir_intrinsic_shared_atomic_umin:
339 case nir_intrinsic_shared_atomic_imax:
340 case nir_intrinsic_shared_atomic_umax:
341 case nir_intrinsic_shared_atomic_and:
342 case nir_intrinsic_shared_atomic_or:
343 case nir_intrinsic_shared_atomic_xor:
344 case nir_intrinsic_shared_atomic_exchange:
345 case nir_intrinsic_shared_atomic_comp_swap:
346 case nir_intrinsic_load_scratch:
347 type = RegType::vgpr;
348 break;
349 case nir_intrinsic_shuffle:
350 case nir_intrinsic_quad_broadcast:
351 case nir_intrinsic_quad_swap_horizontal:
352 case nir_intrinsic_quad_swap_vertical:
353 case nir_intrinsic_quad_swap_diagonal:
354 case nir_intrinsic_quad_swizzle_amd:
355 case nir_intrinsic_masked_swizzle_amd:
356 case nir_intrinsic_inclusive_scan:
357 case nir_intrinsic_exclusive_scan:
358 if (intrinsic->dest.ssa.bit_size == 1) {
359 size = 2;
360 type = RegType::sgpr;
361 } else if (!ctx->divergent_vals[intrinsic->dest.ssa.index]) {
362 type = RegType::sgpr;
363 } else {
364 type = RegType::vgpr;
365 }
366 break;
367 case nir_intrinsic_load_view_index:
368 type = ctx->stage == fragment_fs ? RegType::vgpr : RegType::sgpr;
369 break;
370 case nir_intrinsic_load_front_face:
371 case nir_intrinsic_load_helper_invocation:
372 case nir_intrinsic_is_helper_invocation:
373 type = RegType::sgpr;
374 size = 2;
375 break;
376 case nir_intrinsic_reduce:
377 if (intrinsic->dest.ssa.bit_size == 1) {
378 size = 2;
379 type = RegType::sgpr;
380 } else if (nir_intrinsic_cluster_size(intrinsic) == 0 ||
381 !ctx->divergent_vals[intrinsic->dest.ssa.index]) {
382 type = RegType::sgpr;
383 } else {
384 type = RegType::vgpr;
385 }
386 break;
387 case nir_intrinsic_load_ubo:
388 case nir_intrinsic_load_ssbo:
389 case nir_intrinsic_load_global:
390 case nir_intrinsic_vulkan_resource_index:
391 type = ctx->divergent_vals[intrinsic->dest.ssa.index] ? RegType::vgpr : RegType::sgpr;
392 break;
393 /* due to copy propagation, the swizzled imov is removed if num dest components == 1 */
394 case nir_intrinsic_load_shared:
395 if (ctx->divergent_vals[intrinsic->dest.ssa.index])
396 type = RegType::vgpr;
397 else
398 type = RegType::sgpr;
399 break;
400 default:
401 for (unsigned i = 0; i < nir_intrinsic_infos[intrinsic->intrinsic].num_srcs; i++) {
402 if (allocated[intrinsic->src[i].ssa->index].type() == RegType::vgpr)
403 type = RegType::vgpr;
404 }
405 break;
406 }
407 allocated[intrinsic->dest.ssa.index] = Temp(0, RegClass(type, size));
408
409 switch(intrinsic->intrinsic) {
410 case nir_intrinsic_load_barycentric_sample:
411 case nir_intrinsic_load_barycentric_pixel:
412 case nir_intrinsic_load_barycentric_centroid:
413 case nir_intrinsic_load_barycentric_at_sample:
414 case nir_intrinsic_load_barycentric_at_offset: {
415 glsl_interp_mode mode = (glsl_interp_mode)nir_intrinsic_interp_mode(intrinsic);
416 spi_ps_inputs |= get_interp_input(intrinsic->intrinsic, mode);
417 break;
418 }
419 case nir_intrinsic_load_front_face:
420 spi_ps_inputs |= S_0286CC_FRONT_FACE_ENA(1);
421 break;
422 case nir_intrinsic_load_frag_coord:
423 case nir_intrinsic_load_sample_pos: {
424 uint8_t mask = nir_ssa_def_components_read(&intrinsic->dest.ssa);
425 for (unsigned i = 0; i < 4; i++) {
426 if (mask & (1 << i))
427 spi_ps_inputs |= S_0286CC_POS_X_FLOAT_ENA(1) << i;
428
429 }
430 break;
431 }
432 case nir_intrinsic_load_sample_id:
433 spi_ps_inputs |= S_0286CC_ANCILLARY_ENA(1);
434 break;
435 case nir_intrinsic_load_sample_mask_in:
436 spi_ps_inputs |= S_0286CC_ANCILLARY_ENA(1);
437 spi_ps_inputs |= S_0286CC_SAMPLE_COVERAGE_ENA(1);
438 break;
439 default:
440 break;
441 }
442 break;
443 }
444 case nir_instr_type_tex: {
445 nir_tex_instr* tex = nir_instr_as_tex(instr);
446 unsigned size = tex->dest.ssa.num_components;
447
448 if (tex->dest.ssa.bit_size == 64)
449 size *= 2;
450 if (tex->op == nir_texop_texture_samples)
451 assert(!ctx->divergent_vals[tex->dest.ssa.index]);
452 if (ctx->divergent_vals[tex->dest.ssa.index])
453 allocated[tex->dest.ssa.index] = Temp(0, RegClass(RegType::vgpr, size));
454 else
455 allocated[tex->dest.ssa.index] = Temp(0, RegClass(RegType::sgpr, size));
456 break;
457 }
458 case nir_instr_type_parallel_copy: {
459 nir_foreach_parallel_copy_entry(entry, nir_instr_as_parallel_copy(instr)) {
460 allocated[entry->dest.ssa.index] = allocated[entry->src.ssa->index];
461 }
462 break;
463 }
464 case nir_instr_type_ssa_undef: {
465 unsigned size = nir_instr_as_ssa_undef(instr)->def.num_components;
466 if (nir_instr_as_ssa_undef(instr)->def.bit_size == 64)
467 size *= 2;
468 allocated[nir_instr_as_ssa_undef(instr)->def.index] = Temp(0, RegClass(RegType::sgpr, size));
469 break;
470 }
471 case nir_instr_type_phi: {
472 nir_phi_instr* phi = nir_instr_as_phi(instr);
473 RegType type;
474 unsigned size = phi->dest.ssa.num_components;
475
476 if (phi->dest.ssa.bit_size == 1) {
477 assert(size == 1 && "multiple components not yet supported on boolean phis.");
478 type = RegType::sgpr;
479 size *= 2;
480 allocated[phi->dest.ssa.index] = Temp(0, RegClass(type, size));
481 break;
482 }
483
484 if (ctx->divergent_vals[phi->dest.ssa.index]) {
485 type = RegType::vgpr;
486 } else {
487 type = RegType::sgpr;
488 nir_foreach_phi_src (src, phi) {
489 if (allocated[src->src.ssa->index].type() == RegType::vgpr)
490 type = RegType::vgpr;
491 if (allocated[src->src.ssa->index].type() == RegType::none)
492 done = false;
493 }
494 }
495
496 size *= phi->dest.ssa.bit_size == 64 ? 2 : 1;
497 RegClass rc = RegClass(type, size);
498 if (rc != allocated[phi->dest.ssa.index].regClass()) {
499 done = false;
500 } else {
501 nir_foreach_phi_src(src, phi)
502 assert(allocated[src->src.ssa->index].size() == rc.size());
503 }
504 allocated[phi->dest.ssa.index] = Temp(0, rc);
505 break;
506 }
507 default:
508 break;
509 }
510 }
511 }
512 }
513
514 if (G_0286CC_POS_W_FLOAT_ENA(spi_ps_inputs)) {
515 /* If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be enabled too */
516 spi_ps_inputs |= S_0286CC_PERSP_CENTER_ENA(1);
517 }
518
519 if (!(spi_ps_inputs & 0x7F)) {
520 /* At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled */
521 spi_ps_inputs |= S_0286CC_PERSP_CENTER_ENA(1);
522 }
523
524 ctx->program->config->spi_ps_input_ena = spi_ps_inputs;
525 ctx->program->config->spi_ps_input_addr = spi_ps_inputs;
526
527 for (unsigned i = 0; i < impl->ssa_alloc; i++)
528 allocated[i] = Temp(ctx->program->allocateId(), allocated[i].regClass());
529
530 ctx->allocated.reset(allocated.release());
531 }
532
533 Pseudo_instruction *add_startpgm(struct isel_context *ctx)
534 {
535 unsigned arg_count = ctx->args->ac.arg_count;
536 if (ctx->stage == fragment_fs) {
537 /* LLVM optimizes away unused FS inputs and computes spi_ps_input_addr
538 * itself and then communicates the results back via the ELF binary.
539 * Mirror what LLVM does by re-mapping the VGPR arguments here.
540 *
541 * TODO: If we made the FS input scanning code into a separate pass that
542 * could run before argument setup, then this wouldn't be necessary
543 * anymore.
544 */
545 struct ac_shader_args *args = &ctx->args->ac;
546 arg_count = 0;
547 for (unsigned i = 0, vgpr_arg = 0, vgpr_reg = 0; i < args->arg_count; i++) {
548 if (args->args[i].file != AC_ARG_VGPR) {
549 arg_count++;
550 continue;
551 }
552
553 if (!(ctx->program->config->spi_ps_input_addr & (1 << vgpr_arg))) {
554 args->args[i].skip = true;
555 } else {
556 args->args[i].offset = vgpr_reg;
557 vgpr_reg += args->args[i].size;
558 arg_count++;
559 }
560 vgpr_arg++;
561 }
562 }
563
564 aco_ptr<Pseudo_instruction> startpgm{create_instruction<Pseudo_instruction>(aco_opcode::p_startpgm, Format::PSEUDO, 0, arg_count + 1)};
565 for (unsigned i = 0, arg = 0; i < ctx->args->ac.arg_count; i++) {
566 if (ctx->args->ac.args[i].skip)
567 continue;
568
569 enum ac_arg_regfile file = ctx->args->ac.args[i].file;
570 unsigned size = ctx->args->ac.args[i].size;
571 unsigned reg = ctx->args->ac.args[i].offset;
572 RegClass type = RegClass(file == AC_ARG_SGPR ? RegType::sgpr : RegType::vgpr, size);
573 Temp dst = Temp{ctx->program->allocateId(), type};
574 ctx->arg_temps[i] = dst;
575 startpgm->definitions[arg] = Definition(dst);
576 startpgm->definitions[arg].setFixed(PhysReg{file == AC_ARG_SGPR ? reg : reg + 256});
577 arg++;
578 }
579 startpgm->definitions[arg_count] = Definition{ctx->program->allocateId(), exec, s2};
580 Pseudo_instruction *instr = startpgm.get();
581 ctx->block->instructions.push_back(std::move(startpgm));
582
583 /* Stash these in the program so that they can be accessed later when
584 * handling spilling.
585 */
586 ctx->program->private_segment_buffer = get_arg(ctx, ctx->args->ring_offsets);
587 ctx->program->scratch_offset = get_arg(ctx, ctx->args->scratch_offset);
588
589 return instr;
590 }
591
592 int
593 type_size(const struct glsl_type *type, bool bindless)
594 {
595 // TODO: don't we need type->std430_base_alignment() here?
596 return glsl_count_attribute_slots(type, false);
597 }
598
599 void
600 shared_var_info(const struct glsl_type *type, unsigned *size, unsigned *align)
601 {
602 assert(glsl_type_is_vector_or_scalar(type));
603
604 uint32_t comp_size = glsl_type_is_boolean(type)
605 ? 4 : glsl_get_bit_size(type) / 8;
606 unsigned length = glsl_get_vector_elements(type);
607 *size = comp_size * length,
608 *align = comp_size;
609 }
610
611 int
612 get_align(nir_variable_mode mode, bool is_store, unsigned bit_size, unsigned num_components)
613 {
614 /* TODO: ACO doesn't have good support for non-32-bit reads/writes yet */
615 if (bit_size != 32)
616 return -1;
617
618 switch (mode) {
619 case nir_var_mem_ubo:
620 case nir_var_mem_ssbo:
621 //case nir_var_mem_push_const: enable with 1240!
622 case nir_var_mem_shared:
623 /* TODO: what are the alignment requirements for LDS? */
624 return num_components <= 4 ? 4 : -1;
625 default:
626 return -1;
627 }
628 }
629
630 void
631 setup_vs_variables(isel_context *ctx, nir_shader *nir)
632 {
633 nir_foreach_variable(variable, &nir->inputs)
634 {
635 variable->data.driver_location = variable->data.location * 4;
636 }
637 nir_foreach_variable(variable, &nir->outputs)
638 {
639 variable->data.driver_location = variable->data.location * 4;
640 }
641
642 radv_vs_output_info *outinfo = &ctx->program->info->vs.outinfo;
643
644 memset(outinfo->vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
645 sizeof(outinfo->vs_output_param_offset));
646
647 ctx->needs_instance_id = ctx->program->info->vs.needs_instance_id;
648
649 bool export_clip_dists = ctx->options->key.vs_common_out.export_clip_dists;
650
651 outinfo->param_exports = 0;
652 int pos_written = 0x1;
653 if (outinfo->writes_pointsize || outinfo->writes_viewport_index || outinfo->writes_layer)
654 pos_written |= 1 << 1;
655
656 nir_foreach_variable(variable, &nir->outputs)
657 {
658 int idx = variable->data.location;
659 unsigned slots = variable->type->count_attribute_slots(false);
660 if (variable->data.compact) {
661 unsigned component_count = variable->data.location_frac + variable->type->length;
662 slots = (component_count + 3) / 4;
663 }
664
665 if (idx >= VARYING_SLOT_VAR0 || idx == VARYING_SLOT_LAYER || idx == VARYING_SLOT_PRIMITIVE_ID ||
666 ((idx == VARYING_SLOT_CLIP_DIST0 || idx == VARYING_SLOT_CLIP_DIST1) && export_clip_dists)) {
667 for (unsigned i = 0; i < slots; i++) {
668 if (outinfo->vs_output_param_offset[idx + i] == AC_EXP_PARAM_UNDEFINED)
669 outinfo->vs_output_param_offset[idx + i] = outinfo->param_exports++;
670 }
671 }
672 }
673 if (outinfo->writes_layer &&
674 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] == AC_EXP_PARAM_UNDEFINED) {
675 /* when ctx->options->key.has_multiview_view_index = true, the layer
676 * variable isn't declared in NIR and it's isel's job to get the layer */
677 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] = outinfo->param_exports++;
678 }
679
680 if (outinfo->export_prim_id) {
681 assert(outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] == AC_EXP_PARAM_UNDEFINED);
682 outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = outinfo->param_exports++;
683 }
684
685 ctx->num_clip_distances = util_bitcount(outinfo->clip_dist_mask);
686 ctx->num_cull_distances = util_bitcount(outinfo->cull_dist_mask);
687
688 assert(ctx->num_clip_distances + ctx->num_cull_distances <= 8);
689
690 if (ctx->num_clip_distances + ctx->num_cull_distances > 0)
691 pos_written |= 1 << 2;
692 if (ctx->num_clip_distances + ctx->num_cull_distances > 4)
693 pos_written |= 1 << 3;
694
695 outinfo->pos_exports = util_bitcount(pos_written);
696 }
697
698 void
699 setup_variables(isel_context *ctx, nir_shader *nir)
700 {
701 switch (nir->info.stage) {
702 case MESA_SHADER_FRAGMENT: {
703 nir_foreach_variable(variable, &nir->outputs)
704 {
705 int idx = variable->data.location + variable->data.index;
706 variable->data.driver_location = idx * 4;
707 }
708 break;
709 }
710 case MESA_SHADER_COMPUTE: {
711 ctx->program->config->lds_size = (nir->info.cs.shared_size + ctx->program->lds_alloc_granule - 1) /
712 ctx->program->lds_alloc_granule;
713 break;
714 }
715 case MESA_SHADER_VERTEX: {
716 setup_vs_variables(ctx, nir);
717 break;
718 }
719 default:
720 unreachable("Unhandled shader stage.");
721 }
722 }
723
724 isel_context
725 setup_isel_context(Program* program,
726 unsigned shader_count,
727 struct nir_shader *const *shaders,
728 ac_shader_config* config,
729 struct radv_shader_args *args)
730 {
731 program->stage = 0;
732 for (unsigned i = 0; i < shader_count; i++) {
733 switch (shaders[i]->info.stage) {
734 case MESA_SHADER_VERTEX:
735 program->stage |= sw_vs;
736 break;
737 case MESA_SHADER_TESS_CTRL:
738 program->stage |= sw_tcs;
739 break;
740 case MESA_SHADER_TESS_EVAL:
741 program->stage |= sw_tes;
742 break;
743 case MESA_SHADER_GEOMETRY:
744 program->stage |= sw_gs;
745 break;
746 case MESA_SHADER_FRAGMENT:
747 program->stage |= sw_fs;
748 break;
749 case MESA_SHADER_COMPUTE:
750 program->stage |= sw_cs;
751 break;
752 default:
753 unreachable("Shader stage not implemented");
754 }
755 }
756 if (program->stage == sw_vs)
757 program->stage |= hw_vs;
758 else if (program->stage == sw_fs)
759 program->stage |= hw_fs;
760 else if (program->stage == sw_cs)
761 program->stage |= hw_cs;
762 else
763 unreachable("Shader stage not implemented");
764
765 program->config = config;
766 program->info = args->shader_info;
767 program->chip_class = args->options->chip_class;
768 program->family = args->options->family;
769 program->wave_size = args->shader_info->wave_size;
770
771 program->lds_alloc_granule = args->options->chip_class >= GFX7 ? 512 : 256;
772 program->lds_limit = args->options->chip_class >= GFX7 ? 65536 : 32768;
773 program->vgpr_limit = 256;
774
775 if (args->options->chip_class >= GFX10) {
776 program->physical_sgprs = 2560; /* doesn't matter as long as it's at least 128 * 20 */
777 program->sgpr_alloc_granule = 127;
778 program->sgpr_limit = 106;
779 } else if (program->chip_class >= GFX8) {
780 program->physical_sgprs = 800;
781 program->sgpr_alloc_granule = 15;
782 if (args->options->family == CHIP_TONGA || args->options->family == CHIP_ICELAND)
783 program->sgpr_limit = 94; /* workaround hardware bug */
784 else
785 program->sgpr_limit = 102;
786 } else {
787 program->physical_sgprs = 512;
788 program->sgpr_alloc_granule = 7;
789 program->sgpr_limit = 104;
790 }
791 /* TODO: we don't have to allocate VCC if we don't need it */
792 program->needs_vcc = true;
793
794 isel_context ctx = {};
795 ctx.program = program;
796 ctx.args = args;
797 ctx.options = args->options;
798 ctx.stage = program->stage;
799
800 for (unsigned i = 0; i < shader_count; i++) {
801 nir_shader *nir = shaders[i];
802
803 /* align and copy constant data */
804 while (program->constant_data.size() % 4u)
805 program->constant_data.push_back(0);
806 ctx.constant_data_offset = program->constant_data.size();
807 program->constant_data.insert(program->constant_data.end(),
808 (uint8_t*)nir->constant_data,
809 (uint8_t*)nir->constant_data + nir->constant_data_size);
810
811 /* the variable setup has to be done before lower_io / CSE */
812 if (nir->info.stage == MESA_SHADER_COMPUTE)
813 nir_lower_vars_to_explicit_types(nir, nir_var_mem_shared, shared_var_info);
814 setup_variables(&ctx, nir);
815
816 /* optimize and lower memory operations */
817 bool lower_to_scalar = false;
818 bool lower_pack = false;
819 // TODO: uncomment this once !1240 is merged
820 /*if (nir_opt_load_store_vectorize(nir,
821 (nir_variable_mode)(nir_var_mem_ssbo | nir_var_mem_ubo |
822 nir_var_mem_push_const | nir_var_mem_shared),
823 get_align)) {
824 lower_to_scalar = true;
825 lower_pack = true;
826 }*/
827 if (nir->info.stage == MESA_SHADER_COMPUTE)
828 lower_to_scalar |= nir_lower_explicit_io(nir, nir_var_mem_shared, nir_address_format_32bit_offset);
829 else
830 nir_lower_io(nir, (nir_variable_mode)(nir_var_shader_in | nir_var_shader_out), type_size, (nir_lower_io_options)0);
831 nir_lower_explicit_io(nir, nir_var_mem_global, nir_address_format_64bit_global);
832
833 if (lower_to_scalar)
834 nir_lower_alu_to_scalar(nir, NULL, NULL);
835 if (lower_pack)
836 nir_lower_pack(nir);
837
838 /* lower ALU operations */
839 // TODO: implement logic64 in aco, it's more effective for sgprs
840 nir_lower_int64(nir, nir->options->lower_int64_options);
841
842 nir_opt_idiv_const(nir, 32);
843 nir_lower_idiv(nir, nir_lower_idiv_precise);
844
845 /* optimize the lowered ALU operations */
846 bool more_algebraic = true;
847 while (more_algebraic) {
848 more_algebraic = false;
849 NIR_PASS_V(nir, nir_copy_prop);
850 NIR_PASS_V(nir, nir_opt_dce);
851 NIR_PASS_V(nir, nir_opt_constant_folding);
852 NIR_PASS(more_algebraic, nir, nir_opt_algebraic);
853 }
854
855 /* Do late algebraic optimization to turn add(a, neg(b)) back into
856 * subs, then the mandatory cleanup after algebraic. Note that it may
857 * produce fnegs, and if so then we need to keep running to squash
858 * fneg(fneg(a)).
859 */
860 bool more_late_algebraic = true;
861 while (more_late_algebraic) {
862 more_late_algebraic = false;
863 NIR_PASS(more_late_algebraic, nir, nir_opt_algebraic_late);
864 NIR_PASS_V(nir, nir_opt_constant_folding);
865 NIR_PASS_V(nir, nir_copy_prop);
866 NIR_PASS_V(nir, nir_opt_dce);
867 NIR_PASS_V(nir, nir_opt_cse);
868 }
869
870 /* cleanup passes */
871 nir_lower_load_const_to_scalar(nir);
872 nir_opt_shrink_load(nir);
873 nir_move_options move_opts = (nir_move_options)(
874 nir_move_const_undef | nir_move_load_ubo | nir_move_load_input | nir_move_comparisons);
875 nir_opt_sink(nir, move_opts);
876 nir_opt_move(nir, move_opts);
877 nir_convert_to_lcssa(nir, true, false);
878 nir_lower_phis_to_scalar(nir);
879
880 nir_function_impl *func = nir_shader_get_entrypoint(nir);
881 nir_index_ssa_defs(func);
882
883 if (args->options->dump_preoptir) {
884 fprintf(stderr, "NIR shader before instruction selection:\n");
885 nir_print_shader(nir, stderr);
886 }
887 }
888
889 unsigned scratch_size = 0;
890 for (unsigned i = 0; i < shader_count; i++)
891 scratch_size = std::max(scratch_size, shaders[i]->scratch_size);
892 ctx.program->config->scratch_bytes_per_wave = align(scratch_size * ctx.program->wave_size, 1024);
893
894 ctx.block = ctx.program->create_and_insert_block();
895 ctx.block->loop_nest_depth = 0;
896 ctx.block->kind = block_kind_top_level;
897
898 return ctx;
899 }
900
901 }