2 * Copyright © 2018 Valve Corporation
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:
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
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
26 #include <unordered_map>
29 #include "vulkan/radv_shader.h"
30 #include "vulkan/radv_descriptor_set.h"
31 #include "vulkan/radv_shader_args.h"
33 #include "ac_exp_param.h"
34 #include "ac_shader_util.h"
36 #include "util/u_math.h"
38 #define MAX_INLINE_PUSH_CONSTS 8
42 struct vs_output_state
{
43 uint8_t mask
[VARYING_SLOT_VAR31
+ 1];
44 Temp outputs
[VARYING_SLOT_VAR31
+ 1][4];
48 const struct radv_nir_compiler_options
*options
;
49 struct radv_shader_args
*args
;
52 uint32_t constant_data_offset
;
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;
61 uint16_t loop_nest_depth
= 0;
65 bool has_divergent_continue
= false;
66 bool has_divergent_branch
= false;
69 bool is_divergent
= false;
71 bool exec_potentially_empty
= false;
74 Temp arg_temps
[AC_MAX_ARGS
];
76 /* inputs common for merged stages */
77 Temp merged_wave_info
= Temp(0, s1
);
80 Temp persp_centroid
, linear_centroid
;
83 bool needs_instance_id
;
85 /* VS output information */
86 unsigned num_clip_distances
;
87 unsigned num_cull_distances
;
88 vs_output_state vs_output
;
91 Temp
get_arg(isel_context
*ctx
, struct ac_arg arg
)
94 return ctx
->arg_temps
[arg
.arg_index
];
97 unsigned get_interp_input(nir_intrinsic_op intrin
, enum glsl_interp_mode 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);
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);
125 void init_context(isel_context
*ctx
, nir_shader
*shader
)
127 nir_function_impl
*impl
= nir_shader_get_entrypoint(shader
);
129 ctx
->shader
= shader
;
130 ctx
->divergent_vals
= nir_divergence_analysis(shader
, nir_divergence_view_index_uniform
);
132 std::unique_ptr
<Temp
[]> allocated
{new Temp
[impl
->ssa_alloc
]()};
134 unsigned spi_ps_inputs
= 0;
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)
147 RegType type
= RegType::sgpr
;
148 switch(alu_instr
->op
) {
170 case nir_op_fround_even
:
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
:
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
:
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
;
215 type
= ctx
->divergent_vals
[alu_instr
->dest
.dest
.ssa
.index
] ? RegType::vgpr
: RegType::sgpr
;
218 if (alu_instr
->dest
.dest
.ssa
.bit_size
== 1) {
221 if (ctx
->divergent_vals
[alu_instr
->dest
.dest
.ssa
.index
]) {
222 type
= RegType::vgpr
;
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
;
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();
236 if (alu_instr
->dest
.dest
.ssa
.bit_size
== 1) {
239 type
= ctx
->divergent_vals
[alu_instr
->dest
.dest
.ssa
.index
] ? RegType::vgpr
: RegType::sgpr
;
243 if (alu_instr
->dest
.dest
.ssa
.bit_size
== 1) {
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
;
253 allocated
[alu_instr
->dest
.dest
.ssa
.index
] = Temp(0, RegClass(type
, size
));
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)
260 else if (nir_instr_as_load_const(instr
)->def
.bit_size
== 1)
262 allocated
[nir_instr_as_load_const(instr
)->def
.index
] = Temp(0, RegClass(RegType::sgpr
, size
));
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
)
269 unsigned size
= intrinsic
->dest
.ssa
.num_components
;
270 if (intrinsic
->dest
.ssa
.bit_size
== 64)
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)
291 case nir_intrinsic_ballot
:
292 type
= RegType::sgpr
;
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
;
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) {
370 type
= RegType::sgpr
;
371 } else if (!ctx
->divergent_vals
[intrinsic
->dest
.ssa
.index
]) {
372 type
= RegType::sgpr
;
374 type
= RegType::vgpr
;
377 case nir_intrinsic_load_view_index
:
378 type
= ctx
->stage
== fragment_fs
? RegType::vgpr
: RegType::sgpr
;
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
;
386 case nir_intrinsic_reduce
:
387 if (intrinsic
->dest
.ssa
.bit_size
== 1) {
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
;
394 type
= RegType::vgpr
;
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
;
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
;
408 type
= RegType::sgpr
;
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
;
417 allocated
[intrinsic
->dest
.ssa
.index
] = Temp(0, RegClass(type
, size
));
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
);
429 case nir_intrinsic_load_front_face
:
430 spi_ps_inputs
|= S_0286CC_FRONT_FACE_ENA(1);
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
++) {
437 spi_ps_inputs
|= S_0286CC_POS_X_FLOAT_ENA(1) << i
;
442 case nir_intrinsic_load_sample_id
:
443 spi_ps_inputs
|= S_0286CC_ANCILLARY_ENA(1);
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);
454 case nir_instr_type_tex
: {
455 nir_tex_instr
* tex
= nir_instr_as_tex(instr
);
456 unsigned size
= tex
->dest
.ssa
.num_components
;
458 if (tex
->dest
.ssa
.bit_size
== 64)
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
));
465 allocated
[tex
->dest
.ssa
.index
] = Temp(0, RegClass(RegType::sgpr
, size
));
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
];
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)
478 allocated
[nir_instr_as_ssa_undef(instr
)->def
.index
] = Temp(0, RegClass(RegType::sgpr
, size
));
481 case nir_instr_type_phi
: {
482 nir_phi_instr
* phi
= nir_instr_as_phi(instr
);
484 unsigned size
= phi
->dest
.ssa
.num_components
;
486 if (phi
->dest
.ssa
.bit_size
== 1) {
487 assert(size
== 1 && "multiple components not yet supported on boolean phis.");
488 type
= RegType::sgpr
;
490 allocated
[phi
->dest
.ssa
.index
] = Temp(0, RegClass(type
, size
));
494 if (ctx
->divergent_vals
[phi
->dest
.ssa
.index
]) {
495 type
= RegType::vgpr
;
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
)
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()) {
511 nir_foreach_phi_src(src
, phi
)
512 assert(allocated
[src
->src
.ssa
->index
].size() == rc
.size());
514 allocated
[phi
->dest
.ssa
.index
] = Temp(0, rc
);
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);
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);
534 ctx
->program
->config
->spi_ps_input_ena
= spi_ps_inputs
;
535 ctx
->program
->config
->spi_ps_input_addr
= spi_ps_inputs
;
537 for (unsigned i
= 0; i
< impl
->ssa_alloc
; i
++)
538 allocated
[i
] = Temp(ctx
->program
->allocateId(), allocated
[i
].regClass());
540 ctx
->allocated
.reset(allocated
.release());
543 Pseudo_instruction
*add_startpgm(struct isel_context
*ctx
)
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.
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
555 struct ac_shader_args
*args
= &ctx
->args
->ac
;
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
) {
563 if (!(ctx
->program
->config
->spi_ps_input_addr
& (1 << vgpr_arg
))) {
564 args
->args
[i
].skip
= true;
566 args
->args
[i
].offset
= vgpr_reg
;
567 vgpr_reg
+= args
->args
[i
].size
;
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
)
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});
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
));
593 /* Stash these in the program so that they can be accessed later when
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
);
603 type_size(const struct glsl_type
*type
, bool bindless
)
605 // TODO: don't we need type->std430_base_alignment() here?
606 return glsl_count_attribute_slots(type
, false);
610 shared_var_info(const struct glsl_type
*type
, unsigned *size
, unsigned *align
)
612 assert(glsl_type_is_vector_or_scalar(type
));
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
,
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
)
626 if ((bit_size
!= 32 && bit_size
!= 64) || num_components
> 4)
629 /* >128 bit loads are split except with SMEM */
630 if (bit_size
* num_components
> 128)
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
);
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;
648 return align
% 4 == 0;
656 setup_vs_variables(isel_context
*ctx
, nir_shader
*nir
)
658 nir_foreach_variable(variable
, &nir
->inputs
)
660 variable
->data
.driver_location
= variable
->data
.location
* 4;
662 nir_foreach_variable(variable
, &nir
->outputs
)
664 variable
->data
.driver_location
= variable
->data
.location
* 4;
667 radv_vs_output_info
*outinfo
= &ctx
->program
->info
->vs
.outinfo
;
669 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
670 sizeof(outinfo
->vs_output_param_offset
));
672 ctx
->needs_instance_id
= ctx
->program
->info
->vs
.needs_instance_id
;
674 bool export_clip_dists
= ctx
->options
->key
.vs_common_out
.export_clip_dists
;
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;
681 nir_foreach_variable(variable
, &nir
->outputs
)
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;
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
++;
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
++;
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
++;
710 ctx
->num_clip_distances
= util_bitcount(outinfo
->clip_dist_mask
);
711 ctx
->num_cull_distances
= util_bitcount(outinfo
->cull_dist_mask
);
713 assert(ctx
->num_clip_distances
+ ctx
->num_cull_distances
<= 8);
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;
720 outinfo
->pos_exports
= util_bitcount(pos_written
);
724 setup_variables(isel_context
*ctx
, nir_shader
*nir
)
726 switch (nir
->info
.stage
) {
727 case MESA_SHADER_FRAGMENT
: {
728 nir_foreach_variable(variable
, &nir
->outputs
)
730 int idx
= variable
->data
.location
+ variable
->data
.index
;
731 variable
->data
.driver_location
= idx
* 4;
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
;
740 case MESA_SHADER_VERTEX
: {
741 setup_vs_variables(ctx
, nir
);
745 unreachable("Unhandled shader stage.");
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
)
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
;
762 case MESA_SHADER_TESS_CTRL
:
763 program
->stage
|= sw_tcs
;
765 case MESA_SHADER_TESS_EVAL
:
766 program
->stage
|= sw_tes
;
768 case MESA_SHADER_GEOMETRY
:
769 program
->stage
|= sw_gs
;
771 case MESA_SHADER_FRAGMENT
:
772 program
->stage
|= sw_fs
;
774 case MESA_SHADER_COMPUTE
:
775 program
->stage
|= sw_cs
;
778 unreachable("Shader stage not implemented");
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
;
788 unreachable("Shader stage not implemented");
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
;
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;
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 */
810 program
->sgpr_limit
= 102;
812 program
->physical_sgprs
= 512;
813 program
->sgpr_alloc_granule
= 7;
814 program
->sgpr_limit
= 104;
816 /* TODO: we don't have to allocate VCC if we don't need it */
817 program
->needs_vcc
= true;
819 isel_context ctx
= {};
820 ctx
.program
= program
;
822 ctx
.options
= args
->options
;
823 ctx
.stage
= program
->stage
;
825 for (unsigned i
= 0; i
< shader_count
; i
++) {
826 nir_shader
*nir
= shaders
[i
];
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
);
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
);
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;
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
);
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
);
858 nir_lower_alu_to_scalar(nir
, NULL
, NULL
);
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
);
866 nir_opt_idiv_const(nir
, 32);
867 nir_lower_idiv(nir
, nir_lower_idiv_precise
);
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
);
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
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
);
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
);
904 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
905 nir_index_ssa_defs(func
);
907 if (args
->options
->dump_preoptir
) {
908 fprintf(stderr
, "NIR shader before instruction selection:\n");
909 nir_print_shader(nir
, stderr
);
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);
918 ctx
.block
= ctx
.program
->create_and_insert_block();
919 ctx
.block
->loop_nest_depth
= 0;
920 ctx
.block
->kind
= block_kind_top_level
;