2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include "util/mesa-sha1.h"
29 #include "util/u_atomic.h"
30 #include "radv_debug.h"
31 #include "radv_private.h"
32 #include "radv_shader.h"
33 #include "radv_shader_helper.h"
34 #include "radv_shader_args.h"
36 #include "nir/nir_builder.h"
37 #include "spirv/nir_spirv.h"
40 #include "ac_binary.h"
41 #include "ac_llvm_util.h"
42 #include "ac_nir_to_llvm.h"
44 #include "vk_format.h"
45 #include "util/debug.h"
46 #include "ac_exp_param.h"
48 #include "aco_interface.h"
50 #include "util/string_buffer.h"
52 static const struct nir_shader_compiler_options nir_options_llvm
= {
53 .vertex_id_zero_based
= true,
58 .lower_device_index_to_zero
= true,
62 .lower_bitfield_insert_to_bitfield_select
= true,
63 .lower_bitfield_extract
= true,
65 .lower_pack_snorm_2x16
= true,
66 .lower_pack_snorm_4x8
= true,
67 .lower_pack_unorm_2x16
= true,
68 .lower_pack_unorm_4x8
= true,
69 .lower_unpack_snorm_2x16
= true,
70 .lower_unpack_snorm_4x8
= true,
71 .lower_unpack_unorm_2x16
= true,
72 .lower_unpack_unorm_4x8
= true,
73 .lower_extract_byte
= true,
74 .lower_extract_word
= true,
77 .lower_mul_2x32_64
= true,
79 .max_unroll_iterations
= 32,
80 .use_interpolated_input_intrinsics
= true,
81 /* nir_lower_int64() isn't actually called for the LLVM backend, but
82 * this helps the loop unrolling heuristics. */
83 .lower_int64_options
= nir_lower_imul64
|
84 nir_lower_imul_high64
|
85 nir_lower_imul_2x32_64
|
91 static const struct nir_shader_compiler_options nir_options_aco
= {
92 .vertex_id_zero_based
= true,
97 .lower_device_index_to_zero
= true,
100 .lower_bitfield_insert_to_bitfield_select
= true,
101 .lower_bitfield_extract
= true,
102 .lower_pack_snorm_2x16
= true,
103 .lower_pack_snorm_4x8
= true,
104 .lower_pack_unorm_2x16
= true,
105 .lower_pack_unorm_4x8
= true,
106 .lower_unpack_snorm_2x16
= true,
107 .lower_unpack_snorm_4x8
= true,
108 .lower_unpack_unorm_2x16
= true,
109 .lower_unpack_unorm_4x8
= true,
110 .lower_unpack_half_2x16
= true,
111 .lower_extract_byte
= true,
112 .lower_extract_word
= true,
115 .lower_mul_2x32_64
= true,
116 .lower_rotate
= true,
117 .max_unroll_iterations
= 32,
118 .use_interpolated_input_intrinsics
= true,
119 .lower_int64_options
= nir_lower_imul64
|
120 nir_lower_imul_high64
|
121 nir_lower_imul_2x32_64
|
129 radv_can_dump_shader(struct radv_device
*device
,
130 struct radv_shader_module
*module
,
131 bool is_gs_copy_shader
)
133 if (!(device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADERS
))
136 return !module
->nir
||
137 (device
->instance
->debug_flags
& RADV_DEBUG_DUMP_META_SHADERS
);
139 return is_gs_copy_shader
;
143 radv_can_dump_shader_stats(struct radv_device
*device
,
144 struct radv_shader_module
*module
)
146 /* Only dump non-meta shader stats. */
147 return device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADER_STATS
&&
148 module
&& !module
->nir
;
151 VkResult
radv_CreateShaderModule(
153 const VkShaderModuleCreateInfo
* pCreateInfo
,
154 const VkAllocationCallbacks
* pAllocator
,
155 VkShaderModule
* pShaderModule
)
157 RADV_FROM_HANDLE(radv_device
, device
, _device
);
158 struct radv_shader_module
*module
;
160 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO
);
161 assert(pCreateInfo
->flags
== 0);
163 module
= vk_alloc2(&device
->alloc
, pAllocator
,
164 sizeof(*module
) + pCreateInfo
->codeSize
, 8,
165 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
167 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
170 module
->size
= pCreateInfo
->codeSize
;
171 memcpy(module
->data
, pCreateInfo
->pCode
, module
->size
);
173 _mesa_sha1_compute(module
->data
, module
->size
, module
->sha1
);
175 *pShaderModule
= radv_shader_module_to_handle(module
);
180 void radv_DestroyShaderModule(
182 VkShaderModule _module
,
183 const VkAllocationCallbacks
* pAllocator
)
185 RADV_FROM_HANDLE(radv_device
, device
, _device
);
186 RADV_FROM_HANDLE(radv_shader_module
, module
, _module
);
191 vk_free2(&device
->alloc
, pAllocator
, module
);
195 radv_optimize_nir(struct nir_shader
*shader
, bool optimize_conservatively
,
199 unsigned lower_flrp
=
200 (shader
->options
->lower_flrp16
? 16 : 0) |
201 (shader
->options
->lower_flrp32
? 32 : 0) |
202 (shader
->options
->lower_flrp64
? 64 : 0);
207 NIR_PASS(progress
, shader
, nir_split_array_vars
, nir_var_function_temp
);
208 NIR_PASS(progress
, shader
, nir_shrink_vec_array_vars
, nir_var_function_temp
);
210 NIR_PASS_V(shader
, nir_lower_vars_to_ssa
);
211 NIR_PASS_V(shader
, nir_lower_pack
);
214 /* Only run this pass in the first call to
215 * radv_optimize_nir. Later calls assume that we've
216 * lowered away any copy_deref instructions and we
217 * don't want to introduce any more.
219 NIR_PASS(progress
, shader
, nir_opt_find_array_copies
);
222 NIR_PASS(progress
, shader
, nir_opt_copy_prop_vars
);
223 NIR_PASS(progress
, shader
, nir_opt_dead_write_vars
);
224 NIR_PASS(progress
, shader
, nir_remove_dead_variables
,
225 nir_var_function_temp
| nir_var_shader_in
| nir_var_shader_out
);
227 NIR_PASS_V(shader
, nir_lower_alu_to_scalar
, NULL
, NULL
);
228 NIR_PASS_V(shader
, nir_lower_phis_to_scalar
);
230 NIR_PASS(progress
, shader
, nir_copy_prop
);
231 NIR_PASS(progress
, shader
, nir_opt_remove_phis
);
232 NIR_PASS(progress
, shader
, nir_opt_dce
);
233 if (nir_opt_trivial_continues(shader
)) {
235 NIR_PASS(progress
, shader
, nir_copy_prop
);
236 NIR_PASS(progress
, shader
, nir_opt_remove_phis
);
237 NIR_PASS(progress
, shader
, nir_opt_dce
);
239 NIR_PASS(progress
, shader
, nir_opt_if
, true);
240 NIR_PASS(progress
, shader
, nir_opt_dead_cf
);
241 NIR_PASS(progress
, shader
, nir_opt_cse
);
242 NIR_PASS(progress
, shader
, nir_opt_peephole_select
, 8, true, true);
243 NIR_PASS(progress
, shader
, nir_opt_constant_folding
);
244 NIR_PASS(progress
, shader
, nir_opt_algebraic
);
246 if (lower_flrp
!= 0) {
247 bool lower_flrp_progress
= false;
248 NIR_PASS(lower_flrp_progress
,
252 false /* always_precise */,
253 shader
->options
->lower_ffma
);
254 if (lower_flrp_progress
) {
255 NIR_PASS(progress
, shader
,
256 nir_opt_constant_folding
);
260 /* Nothing should rematerialize any flrps, so we only
261 * need to do this lowering once.
266 NIR_PASS(progress
, shader
, nir_opt_undef
);
267 if (shader
->options
->max_unroll_iterations
) {
268 NIR_PASS(progress
, shader
, nir_opt_loop_unroll
, 0);
270 } while (progress
&& !optimize_conservatively
);
272 NIR_PASS(progress
, shader
, nir_opt_conditional_discard
);
273 NIR_PASS(progress
, shader
, nir_opt_shrink_load
);
274 NIR_PASS(progress
, shader
, nir_opt_move
, nir_move_load_ubo
);
278 shared_var_info(const struct glsl_type
*type
, unsigned *size
, unsigned *align
)
280 assert(glsl_type_is_vector_or_scalar(type
));
282 uint32_t comp_size
= glsl_type_is_boolean(type
) ? 4 : glsl_get_bit_size(type
) / 8;
283 unsigned length
= glsl_get_vector_elements(type
);
284 *size
= comp_size
* length
,
289 radv_shader_compile_to_nir(struct radv_device
*device
,
290 struct radv_shader_module
*module
,
291 const char *entrypoint_name
,
292 gl_shader_stage stage
,
293 const VkSpecializationInfo
*spec_info
,
294 const VkPipelineCreateFlags flags
,
295 const struct radv_pipeline_layout
*layout
,
296 unsigned subgroup_size
, unsigned ballot_bit_size
)
299 const nir_shader_compiler_options
*nir_options
=
300 device
->physical_device
->use_aco
? &nir_options_aco
:
304 /* Some things such as our meta clear/blit code will give us a NIR
305 * shader directly. In that case, we just ignore the SPIR-V entirely
306 * and just use the NIR shader */
308 nir
->options
= nir_options
;
309 nir_validate_shader(nir
, "in internal shader");
311 assert(exec_list_length(&nir
->functions
) == 1);
313 uint32_t *spirv
= (uint32_t *) module
->data
;
314 assert(module
->size
% 4 == 0);
316 if (device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SPIRV
)
317 radv_print_spirv(module
->data
, module
->size
, stderr
);
319 uint32_t num_spec_entries
= 0;
320 struct nir_spirv_specialization
*spec_entries
= NULL
;
321 if (spec_info
&& spec_info
->mapEntryCount
> 0) {
322 num_spec_entries
= spec_info
->mapEntryCount
;
323 spec_entries
= malloc(num_spec_entries
* sizeof(*spec_entries
));
324 for (uint32_t i
= 0; i
< num_spec_entries
; i
++) {
325 VkSpecializationMapEntry entry
= spec_info
->pMapEntries
[i
];
326 const void *data
= spec_info
->pData
+ entry
.offset
;
327 assert(data
+ entry
.size
<= spec_info
->pData
+ spec_info
->dataSize
);
329 spec_entries
[i
].id
= spec_info
->pMapEntries
[i
].constantID
;
330 if (spec_info
->dataSize
== 8)
331 spec_entries
[i
].data64
= *(const uint64_t *)data
;
333 spec_entries
[i
].data32
= *(const uint32_t *)data
;
336 const struct spirv_to_nir_options spirv_options
= {
337 .lower_ubo_ssbo_access_to_offsets
= true,
339 .amd_fragment_mask
= true,
340 .amd_gcn_shader
= true,
341 .amd_image_read_write_lod
= true,
342 .amd_shader_ballot
= device
->physical_device
->use_shader_ballot
,
343 .amd_shader_explicit_vertex_parameter
= true,
344 .amd_trinary_minmax
= true,
345 .demote_to_helper_invocation
= device
->physical_device
->use_aco
,
346 .derivative_group
= true,
347 .descriptor_array_dynamic_indexing
= true,
348 .descriptor_array_non_uniform_indexing
= true,
349 .descriptor_indexing
= true,
350 .device_group
= true,
351 .draw_parameters
= true,
352 .float_controls
= true,
353 .float16
= !device
->physical_device
->use_aco
,
355 .geometry_streams
= true,
356 .image_ms_array
= true,
357 .image_read_without_format
= true,
358 .image_write_without_format
= true,
359 .int8
= !device
->physical_device
->use_aco
,
360 .int16
= !device
->physical_device
->use_aco
,
362 .int64_atomics
= true,
364 .physical_storage_buffer_address
= true,
365 .post_depth_coverage
= true,
366 .runtime_descriptor_array
= true,
367 .shader_clock
= true,
368 .shader_viewport_index_layer
= true,
369 .stencil_export
= true,
370 .storage_8bit
= !device
->physical_device
->use_aco
,
371 .storage_16bit
= !device
->physical_device
->use_aco
,
372 .storage_image_ms
= true,
373 .subgroup_arithmetic
= true,
374 .subgroup_ballot
= true,
375 .subgroup_basic
= true,
376 .subgroup_quad
= true,
377 .subgroup_shuffle
= true,
378 .subgroup_vote
= true,
379 .tessellation
= true,
380 .transform_feedback
= true,
381 .variable_pointers
= true,
383 .ubo_addr_format
= nir_address_format_32bit_index_offset
,
384 .ssbo_addr_format
= nir_address_format_32bit_index_offset
,
385 .phys_ssbo_addr_format
= nir_address_format_64bit_global
,
386 .push_const_addr_format
= nir_address_format_logical
,
387 .shared_addr_format
= nir_address_format_32bit_offset
,
388 .frag_coord_is_sysval
= true,
390 nir
= spirv_to_nir(spirv
, module
->size
/ 4,
391 spec_entries
, num_spec_entries
,
392 stage
, entrypoint_name
,
393 &spirv_options
, nir_options
);
394 assert(nir
->info
.stage
== stage
);
395 nir_validate_shader(nir
, "after spirv_to_nir");
399 /* We have to lower away local constant initializers right before we
400 * inline functions. That way they get properly initialized at the top
401 * of the function and not at the top of its caller.
403 NIR_PASS_V(nir
, nir_lower_variable_initializers
, nir_var_function_temp
);
404 NIR_PASS_V(nir
, nir_lower_returns
);
405 NIR_PASS_V(nir
, nir_inline_functions
);
406 NIR_PASS_V(nir
, nir_opt_deref
);
408 /* Pick off the single entrypoint that we want */
409 foreach_list_typed_safe(nir_function
, func
, node
, &nir
->functions
) {
410 if (func
->is_entrypoint
)
411 func
->name
= ralloc_strdup(func
, "main");
413 exec_node_remove(&func
->node
);
415 assert(exec_list_length(&nir
->functions
) == 1);
417 /* Make sure we lower constant initializers on output variables so that
418 * nir_remove_dead_variables below sees the corresponding stores
420 NIR_PASS_V(nir
, nir_lower_variable_initializers
, nir_var_shader_out
);
422 /* Now that we've deleted all but the main function, we can go ahead and
423 * lower the rest of the constant initializers.
425 NIR_PASS_V(nir
, nir_lower_variable_initializers
, ~0);
427 /* Split member structs. We do this before lower_io_to_temporaries so that
428 * it doesn't lower system values to temporaries by accident.
430 NIR_PASS_V(nir
, nir_split_var_copies
);
431 NIR_PASS_V(nir
, nir_split_per_member_structs
);
433 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
&&
434 device
->physical_device
->use_aco
)
435 NIR_PASS_V(nir
, nir_lower_io_to_vector
, nir_var_shader_out
);
436 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
)
437 NIR_PASS_V(nir
, nir_lower_input_attachments
, true);
439 NIR_PASS_V(nir
, nir_remove_dead_variables
,
440 nir_var_shader_in
| nir_var_shader_out
| nir_var_system_value
| nir_var_mem_shared
);
442 NIR_PASS_V(nir
, nir_propagate_invariant
);
444 NIR_PASS_V(nir
, nir_lower_system_values
);
445 NIR_PASS_V(nir
, nir_lower_clip_cull_distance_arrays
);
446 NIR_PASS_V(nir
, radv_nir_lower_ycbcr_textures
, layout
);
447 if (device
->instance
->debug_flags
& RADV_DEBUG_DISCARD_TO_DEMOTE
)
448 NIR_PASS_V(nir
, nir_lower_discard_to_demote
);
451 /* Vulkan uses the separate-shader linking model */
452 nir
->info
.separate_shader
= true;
454 nir_shader_gather_info(nir
, nir_shader_get_entrypoint(nir
));
456 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
)
457 nir_lower_gs_intrinsics(nir
, true);
459 static const nir_lower_tex_options tex_options
= {
461 .lower_tg4_offsets
= true,
464 nir_lower_tex(nir
, &tex_options
);
466 nir_lower_vars_to_ssa(nir
);
468 if (nir
->info
.stage
== MESA_SHADER_VERTEX
||
469 nir
->info
.stage
== MESA_SHADER_GEOMETRY
||
470 nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
471 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
472 nir_shader_get_entrypoint(nir
), true, true);
473 } else if (nir
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
474 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
475 nir_shader_get_entrypoint(nir
), true, false);
478 nir_split_var_copies(nir
);
480 nir_lower_global_vars_to_local(nir
);
481 nir_remove_dead_variables(nir
, nir_var_function_temp
);
482 bool gfx7minus
= device
->physical_device
->rad_info
.chip_class
<= GFX7
;
483 nir_lower_subgroups(nir
, &(struct nir_lower_subgroups_options
) {
484 .subgroup_size
= subgroup_size
,
485 .ballot_bit_size
= ballot_bit_size
,
486 .lower_to_scalar
= 1,
487 .lower_subgroup_masks
= 1,
489 .lower_shuffle_to_32bit
= 1,
490 .lower_vote_eq_to_ballot
= 1,
491 .lower_quad_broadcast_dynamic
= 1,
492 .lower_quad_broadcast_dynamic_to_const
= gfx7minus
,
495 nir_lower_load_const_to_scalar(nir
);
497 if (!(flags
& VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT
))
498 radv_optimize_nir(nir
, false, true);
500 /* We call nir_lower_var_copies() after the first radv_optimize_nir()
501 * to remove any copies introduced by nir_opt_find_array_copies().
503 nir_lower_var_copies(nir
);
505 /* Lower deref operations for compute shared memory. */
506 if (nir
->info
.stage
== MESA_SHADER_COMPUTE
) {
507 NIR_PASS_V(nir
, nir_lower_vars_to_explicit_types
,
508 nir_var_mem_shared
, shared_var_info
);
509 NIR_PASS_V(nir
, nir_lower_explicit_io
,
510 nir_var_mem_shared
, nir_address_format_32bit_offset
);
513 /* Lower large variables that are always constant with load_constant
514 * intrinsics, which get turned into PC-relative loads from a data
515 * section next to the shader.
517 NIR_PASS_V(nir
, nir_opt_large_constants
,
518 glsl_get_natural_size_align_bytes
, 16);
520 /* Indirect lowering must be called after the radv_optimize_nir() loop
521 * has been called at least once. Otherwise indirect lowering can
522 * bloat the instruction count of the loop and cause it to be
523 * considered too large for unrolling.
525 ac_lower_indirect_derefs(nir
, device
->physical_device
->rad_info
.chip_class
);
526 radv_optimize_nir(nir
, flags
& VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT
, false);
532 type_size_vec4(const struct glsl_type
*type
, bool bindless
)
534 return glsl_count_attribute_slots(type
, false);
537 static nir_variable
*
538 find_layer_in_var(nir_shader
*nir
)
540 nir_foreach_variable(var
, &nir
->inputs
) {
541 if (var
->data
.location
== VARYING_SLOT_LAYER
) {
547 nir_variable_create(nir
, nir_var_shader_in
, glsl_int_type(), "layer id");
548 var
->data
.location
= VARYING_SLOT_LAYER
;
549 var
->data
.interpolation
= INTERP_MODE_FLAT
;
553 /* We use layered rendering to implement multiview, which means we need to map
554 * view_index to gl_Layer. The attachment lowering also uses needs to know the
555 * layer so that it can sample from the correct layer. The code generates a
556 * load from the layer_id sysval, but since we don't have a way to get at this
557 * information from the fragment shader, we also need to lower this to the
558 * gl_Layer varying. This pass lowers both to a varying load from the LAYER
559 * slot, before lowering io, so that nir_assign_var_locations() will give the
560 * LAYER varying the correct driver_location.
564 lower_view_index(nir_shader
*nir
)
566 bool progress
= false;
567 nir_function_impl
*entry
= nir_shader_get_entrypoint(nir
);
569 nir_builder_init(&b
, entry
);
571 nir_variable
*layer
= NULL
;
572 nir_foreach_block(block
, entry
) {
573 nir_foreach_instr_safe(instr
, block
) {
574 if (instr
->type
!= nir_instr_type_intrinsic
)
577 nir_intrinsic_instr
*load
= nir_instr_as_intrinsic(instr
);
578 if (load
->intrinsic
!= nir_intrinsic_load_view_index
&&
579 load
->intrinsic
!= nir_intrinsic_load_layer_id
)
583 layer
= find_layer_in_var(nir
);
585 b
.cursor
= nir_before_instr(instr
);
586 nir_ssa_def
*def
= nir_load_var(&b
, layer
);
587 nir_ssa_def_rewrite_uses(&load
->dest
.ssa
,
588 nir_src_for_ssa(def
));
590 nir_instr_remove(instr
);
599 radv_lower_fs_io(nir_shader
*nir
)
601 NIR_PASS_V(nir
, lower_view_index
);
602 nir_assign_io_var_locations(&nir
->inputs
, &nir
->num_inputs
,
603 MESA_SHADER_FRAGMENT
);
605 NIR_PASS_V(nir
, nir_lower_io
, nir_var_shader_in
, type_size_vec4
, 0);
607 /* This pass needs actual constants */
608 nir_opt_constant_folding(nir
);
610 NIR_PASS_V(nir
, nir_io_add_const_offset_to_base
, nir_var_shader_in
);
615 radv_alloc_shader_memory(struct radv_device
*device
,
616 struct radv_shader_variant
*shader
)
618 mtx_lock(&device
->shader_slab_mutex
);
619 list_for_each_entry(struct radv_shader_slab
, slab
, &device
->shader_slabs
, slabs
) {
621 list_for_each_entry(struct radv_shader_variant
, s
, &slab
->shaders
, slab_list
) {
622 if (s
->bo_offset
- offset
>= shader
->code_size
) {
623 shader
->bo
= slab
->bo
;
624 shader
->bo_offset
= offset
;
625 list_addtail(&shader
->slab_list
, &s
->slab_list
);
626 mtx_unlock(&device
->shader_slab_mutex
);
627 return slab
->ptr
+ offset
;
629 offset
= align_u64(s
->bo_offset
+ s
->code_size
, 256);
631 if (slab
->size
- offset
>= shader
->code_size
) {
632 shader
->bo
= slab
->bo
;
633 shader
->bo_offset
= offset
;
634 list_addtail(&shader
->slab_list
, &slab
->shaders
);
635 mtx_unlock(&device
->shader_slab_mutex
);
636 return slab
->ptr
+ offset
;
640 mtx_unlock(&device
->shader_slab_mutex
);
641 struct radv_shader_slab
*slab
= calloc(1, sizeof(struct radv_shader_slab
));
643 slab
->size
= 256 * 1024;
644 slab
->bo
= device
->ws
->buffer_create(device
->ws
, slab
->size
, 256,
646 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
647 (device
->physical_device
->rad_info
.cpdma_prefetch_writes_memory
?
648 0 : RADEON_FLAG_READ_ONLY
),
649 RADV_BO_PRIORITY_SHADER
);
650 slab
->ptr
= (char*)device
->ws
->buffer_map(slab
->bo
);
651 list_inithead(&slab
->shaders
);
653 mtx_lock(&device
->shader_slab_mutex
);
654 list_add(&slab
->slabs
, &device
->shader_slabs
);
656 shader
->bo
= slab
->bo
;
657 shader
->bo_offset
= 0;
658 list_add(&shader
->slab_list
, &slab
->shaders
);
659 mtx_unlock(&device
->shader_slab_mutex
);
664 radv_destroy_shader_slabs(struct radv_device
*device
)
666 list_for_each_entry_safe(struct radv_shader_slab
, slab
, &device
->shader_slabs
, slabs
) {
667 device
->ws
->buffer_destroy(slab
->bo
);
670 mtx_destroy(&device
->shader_slab_mutex
);
673 /* For the UMR disassembler. */
674 #define DEBUGGER_END_OF_CODE_MARKER 0xbf9f0000 /* invalid instruction */
675 #define DEBUGGER_NUM_MARKERS 5
678 radv_get_shader_binary_size(size_t code_size
)
680 return code_size
+ DEBUGGER_NUM_MARKERS
* 4;
683 static void radv_postprocess_config(const struct radv_physical_device
*pdevice
,
684 const struct ac_shader_config
*config_in
,
685 const struct radv_shader_info
*info
,
686 gl_shader_stage stage
,
687 struct ac_shader_config
*config_out
)
689 bool scratch_enabled
= config_in
->scratch_bytes_per_wave
> 0;
690 unsigned vgpr_comp_cnt
= 0;
691 unsigned num_input_vgprs
= info
->num_input_vgprs
;
693 if (stage
== MESA_SHADER_FRAGMENT
) {
694 num_input_vgprs
= ac_get_fs_input_vgpr_cnt(config_in
, NULL
, NULL
);
697 unsigned num_vgprs
= MAX2(config_in
->num_vgprs
, num_input_vgprs
);
698 /* +3 for scratch wave offset and VCC */
699 unsigned num_sgprs
= MAX2(config_in
->num_sgprs
, info
->num_input_sgprs
+ 3);
700 unsigned num_shared_vgprs
= config_in
->num_shared_vgprs
;
701 /* shared VGPRs are introduced in Navi and are allocated in blocks of 8 (RDNA ref 3.6.5) */
702 assert((pdevice
->rad_info
.chip_class
>= GFX10
&& num_shared_vgprs
% 8 == 0)
703 || (pdevice
->rad_info
.chip_class
< GFX10
&& num_shared_vgprs
== 0));
704 unsigned num_shared_vgpr_blocks
= num_shared_vgprs
/ 8;
706 *config_out
= *config_in
;
707 config_out
->num_vgprs
= num_vgprs
;
708 config_out
->num_sgprs
= num_sgprs
;
709 config_out
->num_shared_vgprs
= num_shared_vgprs
;
711 config_out
->rsrc2
= S_00B12C_USER_SGPR(info
->num_user_sgprs
) |
712 S_00B12C_SCRATCH_EN(scratch_enabled
);
714 if (!pdevice
->use_ngg_streamout
) {
715 config_out
->rsrc2
|= S_00B12C_SO_BASE0_EN(!!info
->so
.strides
[0]) |
716 S_00B12C_SO_BASE1_EN(!!info
->so
.strides
[1]) |
717 S_00B12C_SO_BASE2_EN(!!info
->so
.strides
[2]) |
718 S_00B12C_SO_BASE3_EN(!!info
->so
.strides
[3]) |
719 S_00B12C_SO_EN(!!info
->so
.num_outputs
);
722 config_out
->rsrc1
= S_00B848_VGPRS((num_vgprs
- 1) /
723 (info
->wave_size
== 32 ? 8 : 4)) |
724 S_00B848_DX10_CLAMP(1) |
725 S_00B848_FLOAT_MODE(config_out
->float_mode
);
727 if (pdevice
->rad_info
.chip_class
>= GFX10
) {
728 config_out
->rsrc2
|= S_00B22C_USER_SGPR_MSB_GFX10(info
->num_user_sgprs
>> 5);
730 config_out
->rsrc1
|= S_00B228_SGPRS((num_sgprs
- 1) / 8);
731 config_out
->rsrc2
|= S_00B22C_USER_SGPR_MSB_GFX9(info
->num_user_sgprs
>> 5);
735 case MESA_SHADER_TESS_EVAL
:
737 config_out
->rsrc1
|= S_00B228_MEM_ORDERED(pdevice
->rad_info
.chip_class
>= GFX10
);
738 config_out
->rsrc2
|= S_00B22C_OC_LDS_EN(1);
739 } else if (info
->tes
.as_es
) {
740 assert(pdevice
->rad_info
.chip_class
<= GFX8
);
741 vgpr_comp_cnt
= info
->uses_prim_id
? 3 : 2;
743 config_out
->rsrc2
|= S_00B12C_OC_LDS_EN(1);
745 bool enable_prim_id
= info
->tes
.export_prim_id
|| info
->uses_prim_id
;
746 vgpr_comp_cnt
= enable_prim_id
? 3 : 2;
748 config_out
->rsrc1
|= S_00B128_MEM_ORDERED(pdevice
->rad_info
.chip_class
>= GFX10
);
749 config_out
->rsrc2
|= S_00B12C_OC_LDS_EN(1);
751 config_out
->rsrc2
|= S_00B22C_SHARED_VGPR_CNT(num_shared_vgpr_blocks
);
753 case MESA_SHADER_TESS_CTRL
:
754 if (pdevice
->rad_info
.chip_class
>= GFX9
) {
755 /* We need at least 2 components for LS.
756 * VGPR0-3: (VertexID, RelAutoindex, InstanceID / StepRate0, InstanceID).
757 * StepRate0 is set to 1. so that VGPR3 doesn't have to be loaded.
759 if (pdevice
->rad_info
.chip_class
>= GFX10
) {
760 vgpr_comp_cnt
= info
->vs
.needs_instance_id
? 3 : 1;
762 vgpr_comp_cnt
= info
->vs
.needs_instance_id
? 2 : 1;
765 config_out
->rsrc2
|= S_00B12C_OC_LDS_EN(1);
767 config_out
->rsrc1
|= S_00B428_MEM_ORDERED(pdevice
->rad_info
.chip_class
>= GFX10
) |
768 S_00B848_WGP_MODE(pdevice
->rad_info
.chip_class
>= GFX10
);
769 config_out
->rsrc2
|= S_00B42C_SHARED_VGPR_CNT(num_shared_vgpr_blocks
);
771 case MESA_SHADER_VERTEX
:
773 config_out
->rsrc1
|= S_00B228_MEM_ORDERED(pdevice
->rad_info
.chip_class
>= GFX10
);
774 } else if (info
->vs
.as_ls
) {
775 assert(pdevice
->rad_info
.chip_class
<= GFX8
);
776 /* We need at least 2 components for LS.
777 * VGPR0-3: (VertexID, RelAutoindex, InstanceID / StepRate0, InstanceID).
778 * StepRate0 is set to 1. so that VGPR3 doesn't have to be loaded.
780 vgpr_comp_cnt
= info
->vs
.needs_instance_id
? 2 : 1;
781 } else if (info
->vs
.as_es
) {
782 assert(pdevice
->rad_info
.chip_class
<= GFX8
);
783 /* VGPR0-3: (VertexID, InstanceID / StepRate0, ...) */
784 vgpr_comp_cnt
= info
->vs
.needs_instance_id
? 1 : 0;
786 /* VGPR0-3: (VertexID, InstanceID / StepRate0, PrimID, InstanceID)
787 * If PrimID is disabled. InstanceID / StepRate1 is loaded instead.
788 * StepRate0 is set to 1. so that VGPR3 doesn't have to be loaded.
790 if (info
->vs
.needs_instance_id
&& pdevice
->rad_info
.chip_class
>= GFX10
) {
792 } else if (info
->vs
.export_prim_id
) {
794 } else if (info
->vs
.needs_instance_id
) {
800 config_out
->rsrc1
|= S_00B128_MEM_ORDERED(pdevice
->rad_info
.chip_class
>= GFX10
);
801 config_out
->rsrc2
|= S_00B12C_SHARED_VGPR_CNT(num_shared_vgpr_blocks
);
804 case MESA_SHADER_FRAGMENT
:
805 config_out
->rsrc1
|= S_00B028_MEM_ORDERED(pdevice
->rad_info
.chip_class
>= GFX10
);
806 config_out
->rsrc2
|= S_00B02C_SHARED_VGPR_CNT(num_shared_vgpr_blocks
);
808 case MESA_SHADER_GEOMETRY
:
809 config_out
->rsrc1
|= S_00B228_MEM_ORDERED(pdevice
->rad_info
.chip_class
>= GFX10
) |
810 S_00B848_WGP_MODE(pdevice
->rad_info
.chip_class
>= GFX10
);
811 config_out
->rsrc2
|= S_00B22C_SHARED_VGPR_CNT(num_shared_vgpr_blocks
);
813 case MESA_SHADER_COMPUTE
:
814 config_out
->rsrc1
|= S_00B848_MEM_ORDERED(pdevice
->rad_info
.chip_class
>= GFX10
) |
815 S_00B848_WGP_MODE(pdevice
->rad_info
.chip_class
>= GFX10
);
817 S_00B84C_TGID_X_EN(info
->cs
.uses_block_id
[0]) |
818 S_00B84C_TGID_Y_EN(info
->cs
.uses_block_id
[1]) |
819 S_00B84C_TGID_Z_EN(info
->cs
.uses_block_id
[2]) |
820 S_00B84C_TIDIG_COMP_CNT(info
->cs
.uses_thread_id
[2] ? 2 :
821 info
->cs
.uses_thread_id
[1] ? 1 : 0) |
822 S_00B84C_TG_SIZE_EN(info
->cs
.uses_local_invocation_idx
) |
823 S_00B84C_LDS_SIZE(config_in
->lds_size
);
824 config_out
->rsrc3
|= S_00B8A0_SHARED_VGPR_CNT(num_shared_vgpr_blocks
);
828 unreachable("unsupported shader type");
832 if (pdevice
->rad_info
.chip_class
>= GFX10
&& info
->is_ngg
&&
833 (stage
== MESA_SHADER_VERTEX
|| stage
== MESA_SHADER_TESS_EVAL
|| stage
== MESA_SHADER_GEOMETRY
)) {
834 unsigned gs_vgpr_comp_cnt
, es_vgpr_comp_cnt
;
835 gl_shader_stage es_stage
= stage
;
836 if (stage
== MESA_SHADER_GEOMETRY
)
837 es_stage
= info
->gs
.es_type
;
839 /* VGPR5-8: (VertexID, UserVGPR0, UserVGPR1, UserVGPR2 / InstanceID) */
840 if (es_stage
== MESA_SHADER_VERTEX
) {
841 es_vgpr_comp_cnt
= info
->vs
.needs_instance_id
? 3 : 0;
842 } else if (es_stage
== MESA_SHADER_TESS_EVAL
) {
843 bool enable_prim_id
= info
->tes
.export_prim_id
|| info
->uses_prim_id
;
844 es_vgpr_comp_cnt
= enable_prim_id
? 3 : 2;
846 unreachable("Unexpected ES shader stage");
848 bool tes_triangles
= stage
== MESA_SHADER_TESS_EVAL
&&
849 info
->tes
.primitive_mode
>= 4; /* GL_TRIANGLES */
850 if (info
->uses_invocation_id
|| stage
== MESA_SHADER_VERTEX
) {
851 gs_vgpr_comp_cnt
= 3; /* VGPR3 contains InvocationID. */
852 } else if (info
->uses_prim_id
) {
853 gs_vgpr_comp_cnt
= 2; /* VGPR2 contains PrimitiveID. */
854 } else if (info
->gs
.vertices_in
>= 3 || tes_triangles
) {
855 gs_vgpr_comp_cnt
= 1; /* VGPR1 contains offsets 2, 3 */
857 gs_vgpr_comp_cnt
= 0; /* VGPR0 contains offsets 0, 1 */
860 config_out
->rsrc1
|= S_00B228_GS_VGPR_COMP_CNT(gs_vgpr_comp_cnt
) |
861 S_00B228_WGP_MODE(1);
862 config_out
->rsrc2
|= S_00B22C_ES_VGPR_COMP_CNT(es_vgpr_comp_cnt
) |
863 S_00B22C_LDS_SIZE(config_in
->lds_size
) |
864 S_00B22C_OC_LDS_EN(es_stage
== MESA_SHADER_TESS_EVAL
);
865 } else if (pdevice
->rad_info
.chip_class
>= GFX9
&&
866 stage
== MESA_SHADER_GEOMETRY
) {
867 unsigned es_type
= info
->gs
.es_type
;
868 unsigned gs_vgpr_comp_cnt
, es_vgpr_comp_cnt
;
870 if (es_type
== MESA_SHADER_VERTEX
) {
871 /* VGPR0-3: (VertexID, InstanceID / StepRate0, ...) */
872 if (info
->vs
.needs_instance_id
) {
873 es_vgpr_comp_cnt
= pdevice
->rad_info
.chip_class
>= GFX10
? 3 : 1;
875 es_vgpr_comp_cnt
= 0;
877 } else if (es_type
== MESA_SHADER_TESS_EVAL
) {
878 es_vgpr_comp_cnt
= info
->uses_prim_id
? 3 : 2;
880 unreachable("invalid shader ES type");
883 /* If offsets 4, 5 are used, GS_VGPR_COMP_CNT is ignored and
884 * VGPR[0:4] are always loaded.
886 if (info
->uses_invocation_id
) {
887 gs_vgpr_comp_cnt
= 3; /* VGPR3 contains InvocationID. */
888 } else if (info
->uses_prim_id
) {
889 gs_vgpr_comp_cnt
= 2; /* VGPR2 contains PrimitiveID. */
890 } else if (info
->gs
.vertices_in
>= 3) {
891 gs_vgpr_comp_cnt
= 1; /* VGPR1 contains offsets 2, 3 */
893 gs_vgpr_comp_cnt
= 0; /* VGPR0 contains offsets 0, 1 */
896 config_out
->rsrc1
|= S_00B228_GS_VGPR_COMP_CNT(gs_vgpr_comp_cnt
);
897 config_out
->rsrc2
|= S_00B22C_ES_VGPR_COMP_CNT(es_vgpr_comp_cnt
) |
898 S_00B22C_OC_LDS_EN(es_type
== MESA_SHADER_TESS_EVAL
);
899 } else if (pdevice
->rad_info
.chip_class
>= GFX9
&&
900 stage
== MESA_SHADER_TESS_CTRL
) {
901 config_out
->rsrc1
|= S_00B428_LS_VGPR_COMP_CNT(vgpr_comp_cnt
);
903 config_out
->rsrc1
|= S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt
);
907 struct radv_shader_variant
*
908 radv_shader_variant_create(struct radv_device
*device
,
909 const struct radv_shader_binary
*binary
,
910 bool keep_shader_info
)
912 struct ac_shader_config config
= {0};
913 struct ac_rtld_binary rtld_binary
= {0};
914 struct radv_shader_variant
*variant
= calloc(1, sizeof(struct radv_shader_variant
));
918 variant
->ref_count
= 1;
920 if (binary
->type
== RADV_BINARY_TYPE_RTLD
) {
921 struct ac_rtld_symbol lds_symbols
[2];
922 unsigned num_lds_symbols
= 0;
923 const char *elf_data
= (const char *)((struct radv_shader_binary_rtld
*)binary
)->data
;
924 size_t elf_size
= ((struct radv_shader_binary_rtld
*)binary
)->elf_size
;
926 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
927 (binary
->stage
== MESA_SHADER_GEOMETRY
|| binary
->info
.is_ngg
) &&
928 !binary
->is_gs_copy_shader
) {
929 /* We add this symbol even on LLVM <= 8 to ensure that
930 * shader->config.lds_size is set correctly below.
932 struct ac_rtld_symbol
*sym
= &lds_symbols
[num_lds_symbols
++];
933 sym
->name
= "esgs_ring";
934 sym
->size
= binary
->info
.ngg_info
.esgs_ring_size
;
935 sym
->align
= 64 * 1024;
938 if (binary
->info
.is_ngg
&&
939 binary
->stage
== MESA_SHADER_GEOMETRY
) {
940 struct ac_rtld_symbol
*sym
= &lds_symbols
[num_lds_symbols
++];
941 sym
->name
= "ngg_emit";
942 sym
->size
= binary
->info
.ngg_info
.ngg_emit_size
* 4;
946 struct ac_rtld_open_info open_info
= {
947 .info
= &device
->physical_device
->rad_info
,
948 .shader_type
= binary
->stage
,
949 .wave_size
= binary
->info
.wave_size
,
951 .elf_ptrs
= &elf_data
,
952 .elf_sizes
= &elf_size
,
953 .num_shared_lds_symbols
= num_lds_symbols
,
954 .shared_lds_symbols
= lds_symbols
,
957 if (!ac_rtld_open(&rtld_binary
, open_info
)) {
962 if (!ac_rtld_read_config(&rtld_binary
, &config
)) {
963 ac_rtld_close(&rtld_binary
);
968 if (rtld_binary
.lds_size
> 0) {
969 unsigned alloc_granularity
= device
->physical_device
->rad_info
.chip_class
>= GFX7
? 512 : 256;
970 config
.lds_size
= align(rtld_binary
.lds_size
, alloc_granularity
) / alloc_granularity
;
973 variant
->code_size
= rtld_binary
.rx_size
;
974 variant
->exec_size
= rtld_binary
.exec_size
;
976 assert(binary
->type
== RADV_BINARY_TYPE_LEGACY
);
977 config
= ((struct radv_shader_binary_legacy
*)binary
)->config
;
978 variant
->code_size
= radv_get_shader_binary_size(((struct radv_shader_binary_legacy
*)binary
)->code_size
);
979 variant
->exec_size
= ((struct radv_shader_binary_legacy
*)binary
)->exec_size
;
982 variant
->info
= binary
->info
;
983 radv_postprocess_config(device
->physical_device
, &config
, &binary
->info
,
984 binary
->stage
, &variant
->config
);
986 if (radv_device_use_secure_compile(device
->instance
)) {
987 if (binary
->type
== RADV_BINARY_TYPE_RTLD
)
988 ac_rtld_close(&rtld_binary
);
993 void *dest_ptr
= radv_alloc_shader_memory(device
, variant
);
995 if (binary
->type
== RADV_BINARY_TYPE_RTLD
) {
996 struct radv_shader_binary_rtld
* bin
= (struct radv_shader_binary_rtld
*)binary
;
997 struct ac_rtld_upload_info info
= {
998 .binary
= &rtld_binary
,
999 .rx_va
= radv_buffer_get_va(variant
->bo
) + variant
->bo_offset
,
1003 if (!ac_rtld_upload(&info
)) {
1004 radv_shader_variant_destroy(device
, variant
);
1005 ac_rtld_close(&rtld_binary
);
1009 if (keep_shader_info
||
1010 (device
->instance
->debug_flags
& RADV_DEBUG_DUMP_SHADERS
)) {
1011 const char *disasm_data
;
1013 if (!ac_rtld_get_section_by_name(&rtld_binary
, ".AMDGPU.disasm", &disasm_data
, &disasm_size
)) {
1014 radv_shader_variant_destroy(device
, variant
);
1015 ac_rtld_close(&rtld_binary
);
1019 variant
->ir_string
= bin
->llvm_ir_size
? strdup((const char*)(bin
->data
+ bin
->elf_size
)) : NULL
;
1020 variant
->disasm_string
= malloc(disasm_size
+ 1);
1021 memcpy(variant
->disasm_string
, disasm_data
, disasm_size
);
1022 variant
->disasm_string
[disasm_size
] = 0;
1025 ac_rtld_close(&rtld_binary
);
1027 struct radv_shader_binary_legacy
* bin
= (struct radv_shader_binary_legacy
*)binary
;
1028 memcpy(dest_ptr
, bin
->data
+ bin
->stats_size
, bin
->code_size
);
1030 /* Add end-of-code markers for the UMR disassembler. */
1031 uint32_t *ptr32
= (uint32_t *)dest_ptr
+ bin
->code_size
/ 4;
1032 for (unsigned i
= 0; i
< DEBUGGER_NUM_MARKERS
; i
++)
1033 ptr32
[i
] = DEBUGGER_END_OF_CODE_MARKER
;
1035 variant
->ir_string
= bin
->ir_size
? strdup((const char*)(bin
->data
+ bin
->stats_size
+ bin
->code_size
)) : NULL
;
1036 variant
->disasm_string
= bin
->disasm_size
? strdup((const char*)(bin
->data
+ bin
->stats_size
+ bin
->code_size
+ bin
->ir_size
)) : NULL
;
1038 if (bin
->stats_size
) {
1039 variant
->statistics
= calloc(bin
->stats_size
, 1);
1040 memcpy(variant
->statistics
, bin
->data
, bin
->stats_size
);
1047 radv_dump_nir_shaders(struct nir_shader
* const *shaders
,
1053 FILE *f
= open_memstream(&data
, &size
);
1055 for (int i
= 0; i
< shader_count
; ++i
)
1056 nir_print_shader(shaders
[i
], f
);
1060 ret
= malloc(size
+ 1);
1062 memcpy(ret
, data
, size
);
1069 static struct radv_shader_variant
*
1070 shader_variant_compile(struct radv_device
*device
,
1071 struct radv_shader_module
*module
,
1072 struct nir_shader
* const *shaders
,
1074 gl_shader_stage stage
,
1075 struct radv_shader_info
*info
,
1076 struct radv_nir_compiler_options
*options
,
1077 bool gs_copy_shader
,
1078 bool keep_shader_info
,
1079 bool keep_statistic_info
,
1080 struct radv_shader_binary
**binary_out
)
1082 enum radeon_family chip_family
= device
->physical_device
->rad_info
.family
;
1083 struct radv_shader_binary
*binary
= NULL
;
1085 options
->family
= chip_family
;
1086 options
->chip_class
= device
->physical_device
->rad_info
.chip_class
;
1087 options
->dump_shader
= radv_can_dump_shader(device
, module
, gs_copy_shader
);
1088 options
->dump_preoptir
= options
->dump_shader
&&
1089 device
->instance
->debug_flags
& RADV_DEBUG_PREOPTIR
;
1090 options
->record_ir
= keep_shader_info
;
1091 options
->record_stats
= keep_statistic_info
;
1092 options
->check_ir
= device
->instance
->debug_flags
& RADV_DEBUG_CHECKIR
;
1093 options
->tess_offchip_block_dw_size
= device
->tess_offchip_block_dw_size
;
1094 options
->address32_hi
= device
->physical_device
->rad_info
.address32_hi
;
1095 options
->has_ls_vgpr_init_bug
= device
->physical_device
->rad_info
.has_ls_vgpr_init_bug
;
1096 options
->use_ngg_streamout
= device
->physical_device
->use_ngg_streamout
;
1098 struct radv_shader_args args
= {};
1099 args
.options
= options
;
1100 args
.shader_info
= info
;
1101 args
.is_gs_copy_shader
= gs_copy_shader
;
1102 radv_declare_shader_args(&args
,
1103 gs_copy_shader
? MESA_SHADER_VERTEX
1104 : shaders
[shader_count
- 1]->info
.stage
,
1106 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
1107 : MESA_SHADER_VERTEX
);
1109 if (!device
->physical_device
->use_aco
||
1110 options
->dump_shader
|| options
->record_ir
)
1111 ac_init_llvm_once();
1113 if (device
->physical_device
->use_aco
) {
1114 aco_compile_shader(shader_count
, shaders
, &binary
, &args
);
1116 llvm_compile_shader(device
, shader_count
, shaders
, &binary
, &args
);
1119 binary
->info
= *info
;
1121 struct radv_shader_variant
*variant
= radv_shader_variant_create(device
, binary
,
1128 if (options
->dump_shader
) {
1129 fprintf(stderr
, "%s", radv_get_shader_name(info
, shaders
[0]->info
.stage
));
1130 for (int i
= 1; i
< shader_count
; ++i
)
1131 fprintf(stderr
, " + %s", radv_get_shader_name(info
, shaders
[i
]->info
.stage
));
1133 fprintf(stderr
, "\ndisasm:\n%s\n", variant
->disasm_string
);
1137 if (keep_shader_info
) {
1138 variant
->nir_string
= radv_dump_nir_shaders(shaders
, shader_count
);
1139 if (!gs_copy_shader
&& !module
->nir
) {
1140 variant
->spirv
= malloc(module
->size
);
1141 if (!variant
->spirv
) {
1147 memcpy(variant
->spirv
, module
->data
, module
->size
);
1148 variant
->spirv_size
= module
->size
;
1153 *binary_out
= binary
;
1160 struct radv_shader_variant
*
1161 radv_shader_variant_compile(struct radv_device
*device
,
1162 struct radv_shader_module
*module
,
1163 struct nir_shader
*const *shaders
,
1165 struct radv_pipeline_layout
*layout
,
1166 const struct radv_shader_variant_key
*key
,
1167 struct radv_shader_info
*info
,
1168 bool keep_shader_info
, bool keep_statistic_info
,
1169 struct radv_shader_binary
**binary_out
)
1171 struct radv_nir_compiler_options options
= {0};
1173 options
.layout
= layout
;
1177 options
.explicit_scratch_args
= device
->physical_device
->use_aco
;
1178 options
.robust_buffer_access
= device
->robust_buffer_access
;
1180 return shader_variant_compile(device
, module
, shaders
, shader_count
, shaders
[shader_count
- 1]->info
.stage
, info
,
1181 &options
, false, keep_shader_info
, keep_statistic_info
, binary_out
);
1184 struct radv_shader_variant
*
1185 radv_create_gs_copy_shader(struct radv_device
*device
,
1186 struct nir_shader
*shader
,
1187 struct radv_shader_info
*info
,
1188 struct radv_shader_binary
**binary_out
,
1189 bool keep_shader_info
, bool keep_statistic_info
,
1192 struct radv_nir_compiler_options options
= {0};
1194 options
.explicit_scratch_args
= device
->physical_device
->use_aco
;
1195 options
.key
.has_multiview_view_index
= multiview
;
1197 return shader_variant_compile(device
, NULL
, &shader
, 1, MESA_SHADER_VERTEX
,
1198 info
, &options
, true, keep_shader_info
, keep_statistic_info
, binary_out
);
1202 radv_shader_variant_destroy(struct radv_device
*device
,
1203 struct radv_shader_variant
*variant
)
1205 if (!p_atomic_dec_zero(&variant
->ref_count
))
1208 mtx_lock(&device
->shader_slab_mutex
);
1209 list_del(&variant
->slab_list
);
1210 mtx_unlock(&device
->shader_slab_mutex
);
1212 free(variant
->spirv
);
1213 free(variant
->nir_string
);
1214 free(variant
->disasm_string
);
1215 free(variant
->ir_string
);
1216 free(variant
->statistics
);
1221 radv_get_shader_name(struct radv_shader_info
*info
,
1222 gl_shader_stage stage
)
1225 case MESA_SHADER_VERTEX
:
1227 return "Vertex Shader as LS";
1228 else if (info
->vs
.as_es
)
1229 return "Vertex Shader as ES";
1230 else if (info
->is_ngg
)
1231 return "Vertex Shader as ESGS";
1233 return "Vertex Shader as VS";
1234 case MESA_SHADER_TESS_CTRL
:
1235 return "Tessellation Control Shader";
1236 case MESA_SHADER_TESS_EVAL
:
1237 if (info
->tes
.as_es
)
1238 return "Tessellation Evaluation Shader as ES";
1239 else if (info
->is_ngg
)
1240 return "Tessellation Evaluation Shader as ESGS";
1242 return "Tessellation Evaluation Shader as VS";
1243 case MESA_SHADER_GEOMETRY
:
1244 return "Geometry Shader";
1245 case MESA_SHADER_FRAGMENT
:
1246 return "Pixel Shader";
1247 case MESA_SHADER_COMPUTE
:
1248 return "Compute Shader";
1250 return "Unknown shader";
1255 radv_get_max_workgroup_size(enum chip_class chip_class
,
1256 gl_shader_stage stage
,
1257 const unsigned *sizes
)
1260 case MESA_SHADER_TESS_CTRL
:
1261 return chip_class
>= GFX7
? 128 : 64;
1262 case MESA_SHADER_GEOMETRY
:
1263 return chip_class
>= GFX9
? 128 : 64;
1264 case MESA_SHADER_COMPUTE
:
1270 unsigned max_workgroup_size
= sizes
[0] * sizes
[1] * sizes
[2];
1271 return max_workgroup_size
;
1275 radv_get_max_waves(struct radv_device
*device
,
1276 struct radv_shader_variant
*variant
,
1277 gl_shader_stage stage
)
1279 enum chip_class chip_class
= device
->physical_device
->rad_info
.chip_class
;
1280 unsigned lds_increment
= chip_class
>= GFX7
? 512 : 256;
1281 uint8_t wave_size
= variant
->info
.wave_size
;
1282 struct ac_shader_config
*conf
= &variant
->config
;
1283 unsigned max_simd_waves
;
1284 unsigned lds_per_wave
= 0;
1286 max_simd_waves
= device
->physical_device
->rad_info
.max_wave64_per_simd
;
1288 if (stage
== MESA_SHADER_FRAGMENT
) {
1289 lds_per_wave
= conf
->lds_size
* lds_increment
+
1290 align(variant
->info
.ps
.num_interp
* 48,
1292 } else if (stage
== MESA_SHADER_COMPUTE
) {
1293 unsigned max_workgroup_size
=
1294 radv_get_max_workgroup_size(chip_class
, stage
, variant
->info
.cs
.block_size
);
1295 lds_per_wave
= (conf
->lds_size
* lds_increment
) /
1296 DIV_ROUND_UP(max_workgroup_size
, wave_size
);
1299 if (conf
->num_sgprs
) {
1300 unsigned sgprs
= align(conf
->num_sgprs
, chip_class
>= GFX8
? 16 : 8);
1302 MIN2(max_simd_waves
,
1303 device
->physical_device
->rad_info
.num_physical_sgprs_per_simd
/
1307 if (conf
->num_vgprs
) {
1308 unsigned vgprs
= align(conf
->num_vgprs
, wave_size
== 32 ? 8 : 4);
1310 MIN2(max_simd_waves
,
1311 device
->physical_device
->rad_info
.num_physical_wave64_vgprs_per_simd
/ vgprs
);
1314 unsigned max_lds_per_simd
= device
->physical_device
->rad_info
.lds_size_per_workgroup
/ device
->physical_device
->rad_info
.num_simd_per_compute_unit
;
1316 max_simd_waves
= MIN2(max_simd_waves
, max_lds_per_simd
/ lds_per_wave
);
1318 return max_simd_waves
;
1322 generate_shader_stats(struct radv_device
*device
,
1323 struct radv_shader_variant
*variant
,
1324 gl_shader_stage stage
,
1325 struct _mesa_string_buffer
*buf
)
1327 struct ac_shader_config
*conf
= &variant
->config
;
1328 unsigned max_simd_waves
= radv_get_max_waves(device
, variant
, stage
);
1330 if (stage
== MESA_SHADER_FRAGMENT
) {
1331 _mesa_string_buffer_printf(buf
, "*** SHADER CONFIG ***\n"
1332 "SPI_PS_INPUT_ADDR = 0x%04x\n"
1333 "SPI_PS_INPUT_ENA = 0x%04x\n",
1334 conf
->spi_ps_input_addr
, conf
->spi_ps_input_ena
);
1337 _mesa_string_buffer_printf(buf
, "*** SHADER STATS ***\n"
1340 "Spilled SGPRs: %d\n"
1341 "Spilled VGPRs: %d\n"
1342 "PrivMem VGPRS: %d\n"
1343 "Code Size: %d bytes\n"
1345 "Scratch: %d bytes per wave\n"
1347 conf
->num_sgprs
, conf
->num_vgprs
,
1348 conf
->spilled_sgprs
, conf
->spilled_vgprs
,
1349 variant
->info
.private_mem_vgprs
, variant
->exec_size
,
1350 conf
->lds_size
, conf
->scratch_bytes_per_wave
,
1353 if (variant
->statistics
) {
1354 _mesa_string_buffer_printf(buf
, "*** COMPILER STATS ***\n");
1355 for (unsigned i
= 0; i
< variant
->statistics
->count
; i
++) {
1356 struct radv_compiler_statistic_info
*info
= &variant
->statistics
->infos
[i
];
1357 uint32_t value
= variant
->statistics
->values
[i
];
1358 _mesa_string_buffer_printf(buf
, "%s: %lu\n", info
->name
, value
);
1362 _mesa_string_buffer_printf(buf
, "********************\n\n\n");
1366 radv_shader_dump_stats(struct radv_device
*device
,
1367 struct radv_shader_variant
*variant
,
1368 gl_shader_stage stage
,
1371 struct _mesa_string_buffer
*buf
= _mesa_string_buffer_create(NULL
, 256);
1373 generate_shader_stats(device
, variant
, stage
, buf
);
1375 fprintf(file
, "\n%s:\n", radv_get_shader_name(&variant
->info
, stage
));
1376 fprintf(file
, "%s", buf
->buf
);
1378 _mesa_string_buffer_destroy(buf
);
1382 radv_GetShaderInfoAMD(VkDevice _device
,
1383 VkPipeline _pipeline
,
1384 VkShaderStageFlagBits shaderStage
,
1385 VkShaderInfoTypeAMD infoType
,
1389 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1390 RADV_FROM_HANDLE(radv_pipeline
, pipeline
, _pipeline
);
1391 gl_shader_stage stage
= vk_to_mesa_shader_stage(shaderStage
);
1392 struct radv_shader_variant
*variant
= pipeline
->shaders
[stage
];
1393 struct _mesa_string_buffer
*buf
;
1394 VkResult result
= VK_SUCCESS
;
1396 /* Spec doesn't indicate what to do if the stage is invalid, so just
1397 * return no info for this. */
1399 return vk_error(device
->instance
, VK_ERROR_FEATURE_NOT_PRESENT
);
1402 case VK_SHADER_INFO_TYPE_STATISTICS_AMD
:
1404 *pInfoSize
= sizeof(VkShaderStatisticsInfoAMD
);
1406 unsigned lds_multiplier
= device
->physical_device
->rad_info
.chip_class
>= GFX7
? 512 : 256;
1407 struct ac_shader_config
*conf
= &variant
->config
;
1409 VkShaderStatisticsInfoAMD statistics
= {};
1410 statistics
.shaderStageMask
= shaderStage
;
1411 statistics
.numPhysicalVgprs
= device
->physical_device
->rad_info
.num_physical_wave64_vgprs_per_simd
;
1412 statistics
.numPhysicalSgprs
= device
->physical_device
->rad_info
.num_physical_sgprs_per_simd
;
1413 statistics
.numAvailableSgprs
= statistics
.numPhysicalSgprs
;
1415 if (stage
== MESA_SHADER_COMPUTE
) {
1416 unsigned *local_size
= variant
->info
.cs
.block_size
;
1417 unsigned workgroup_size
= local_size
[0] * local_size
[1] * local_size
[2];
1419 statistics
.numAvailableVgprs
= statistics
.numPhysicalVgprs
/
1420 ceil((double)workgroup_size
/ statistics
.numPhysicalVgprs
);
1422 statistics
.computeWorkGroupSize
[0] = local_size
[0];
1423 statistics
.computeWorkGroupSize
[1] = local_size
[1];
1424 statistics
.computeWorkGroupSize
[2] = local_size
[2];
1426 statistics
.numAvailableVgprs
= statistics
.numPhysicalVgprs
;
1429 statistics
.resourceUsage
.numUsedVgprs
= conf
->num_vgprs
;
1430 statistics
.resourceUsage
.numUsedSgprs
= conf
->num_sgprs
;
1431 statistics
.resourceUsage
.ldsSizePerLocalWorkGroup
= 32768;
1432 statistics
.resourceUsage
.ldsUsageSizeInBytes
= conf
->lds_size
* lds_multiplier
;
1433 statistics
.resourceUsage
.scratchMemUsageInBytes
= conf
->scratch_bytes_per_wave
;
1435 size_t size
= *pInfoSize
;
1436 *pInfoSize
= sizeof(statistics
);
1438 memcpy(pInfo
, &statistics
, MIN2(size
, *pInfoSize
));
1440 if (size
< *pInfoSize
)
1441 result
= VK_INCOMPLETE
;
1445 case VK_SHADER_INFO_TYPE_DISASSEMBLY_AMD
:
1446 buf
= _mesa_string_buffer_create(NULL
, 1024);
1448 _mesa_string_buffer_printf(buf
, "%s:\n", radv_get_shader_name(&variant
->info
, stage
));
1449 _mesa_string_buffer_printf(buf
, "%s\n\n", variant
->ir_string
);
1450 _mesa_string_buffer_printf(buf
, "%s\n\n", variant
->disasm_string
);
1451 generate_shader_stats(device
, variant
, stage
, buf
);
1453 /* Need to include the null terminator. */
1454 size_t length
= buf
->length
+ 1;
1457 *pInfoSize
= length
;
1459 size_t size
= *pInfoSize
;
1460 *pInfoSize
= length
;
1462 memcpy(pInfo
, buf
->buf
, MIN2(size
, length
));
1465 result
= VK_INCOMPLETE
;
1468 _mesa_string_buffer_destroy(buf
);
1471 /* VK_SHADER_INFO_TYPE_BINARY_AMD unimplemented for now. */
1472 result
= VK_ERROR_FEATURE_NOT_PRESENT
;