blob: 1e20aa6f9adf1a10f00435530fc16b0f301a8f4e [file] [log] [blame]
Samuel Pitoisetd4d77732017-09-01 11:41:18 +02001/*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
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:
14 *
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
17 * Software.
18 *
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
25 * IN THE SOFTWARE.
26 */
27
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 "nir/nir.h"
34#include "nir/nir_builder.h"
35#include "spirv/nir_spirv.h"
36
37#include <llvm-c/Core.h>
38#include <llvm-c/TargetMachine.h>
39
40#include "sid.h"
41#include "gfx9d.h"
Samuel Pitoisetd4d77732017-09-01 11:41:18 +020042#include "ac_binary.h"
43#include "ac_llvm_util.h"
44#include "ac_nir_to_llvm.h"
45#include "vk_format.h"
46#include "util/debug.h"
47#include "ac_exp_param.h"
48
Alex Smithde889792017-10-27 14:25:05 +010049#include "util/string_buffer.h"
50
Samuel Pitoisetd4d77732017-09-01 11:41:18 +020051static const struct nir_shader_compiler_options nir_options = {
52 .vertex_id_zero_based = true,
53 .lower_scmp = true,
54 .lower_flrp32 = true,
Timothy Arcerif0d74ec2018-01-12 11:12:09 +110055 .lower_flrp64 = true,
Samuel Pitoisetd4d77732017-09-01 11:41:18 +020056 .lower_fsat = true,
57 .lower_fdiv = true,
58 .lower_sub = true,
59 .lower_pack_snorm_2x16 = true,
60 .lower_pack_snorm_4x8 = true,
61 .lower_pack_unorm_2x16 = true,
62 .lower_pack_unorm_4x8 = true,
63 .lower_unpack_snorm_2x16 = true,
64 .lower_unpack_snorm_4x8 = true,
65 .lower_unpack_unorm_2x16 = true,
66 .lower_unpack_unorm_4x8 = true,
67 .lower_extract_byte = true,
68 .lower_extract_word = true,
Dave Airlie2c615942017-10-04 06:33:02 +100069 .lower_ffma = true,
Samuel Pitoiset7aa008d2018-02-02 19:04:57 +010070 .lower_fpow = true,
Timothy Arceri5b8de4b2018-01-08 10:37:27 +110071 .vs_inputs_dual_locations = true,
Samuel Pitoisetd4d77732017-09-01 11:41:18 +020072 .max_unroll_iterations = 32
73};
74
75VkResult radv_CreateShaderModule(
76 VkDevice _device,
77 const VkShaderModuleCreateInfo* pCreateInfo,
78 const VkAllocationCallbacks* pAllocator,
79 VkShaderModule* pShaderModule)
80{
81 RADV_FROM_HANDLE(radv_device, device, _device);
82 struct radv_shader_module *module;
83
84 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
85 assert(pCreateInfo->flags == 0);
86
87 module = vk_alloc2(&device->alloc, pAllocator,
88 sizeof(*module) + pCreateInfo->codeSize, 8,
89 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
90 if (module == NULL)
91 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
92
93 module->nir = NULL;
94 module->size = pCreateInfo->codeSize;
95 memcpy(module->data, pCreateInfo->pCode, module->size);
96
97 _mesa_sha1_compute(module->data, module->size, module->sha1);
98
99 *pShaderModule = radv_shader_module_to_handle(module);
100
101 return VK_SUCCESS;
102}
103
104void radv_DestroyShaderModule(
105 VkDevice _device,
106 VkShaderModule _module,
107 const VkAllocationCallbacks* pAllocator)
108{
109 RADV_FROM_HANDLE(radv_device, device, _device);
110 RADV_FROM_HANDLE(radv_shader_module, module, _module);
111
112 if (!module)
113 return;
114
115 vk_free2(&device->alloc, pAllocator, module);
116}
117
Bas Nieuwenhuizen06f05042017-02-09 00:12:10 +0100118void
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200119radv_optimize_nir(struct nir_shader *shader)
120{
121 bool progress;
122
123 do {
124 progress = false;
125
126 NIR_PASS_V(shader, nir_lower_vars_to_ssa);
127 NIR_PASS_V(shader, nir_lower_64bit_pack);
128 NIR_PASS_V(shader, nir_lower_alu_to_scalar);
129 NIR_PASS_V(shader, nir_lower_phis_to_scalar);
130
131 NIR_PASS(progress, shader, nir_copy_prop);
132 NIR_PASS(progress, shader, nir_opt_remove_phis);
133 NIR_PASS(progress, shader, nir_opt_dce);
134 if (nir_opt_trivial_continues(shader)) {
135 progress = true;
136 NIR_PASS(progress, shader, nir_copy_prop);
Dave Airlie64d9bd12017-09-13 03:49:31 +0100137 NIR_PASS(progress, shader, nir_opt_remove_phis);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200138 NIR_PASS(progress, shader, nir_opt_dce);
139 }
140 NIR_PASS(progress, shader, nir_opt_if);
141 NIR_PASS(progress, shader, nir_opt_dead_cf);
142 NIR_PASS(progress, shader, nir_opt_cse);
143 NIR_PASS(progress, shader, nir_opt_peephole_select, 8);
144 NIR_PASS(progress, shader, nir_opt_algebraic);
145 NIR_PASS(progress, shader, nir_opt_constant_folding);
146 NIR_PASS(progress, shader, nir_opt_undef);
147 NIR_PASS(progress, shader, nir_opt_conditional_discard);
148 if (shader->options->max_unroll_iterations) {
149 NIR_PASS(progress, shader, nir_opt_loop_unroll, 0);
150 }
151 } while (progress);
Samuel Pitoiset3488a3f2018-01-29 17:19:18 +0100152
153 NIR_PASS(progress, shader, nir_opt_shrink_load);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200154}
155
156nir_shader *
157radv_shader_compile_to_nir(struct radv_device *device,
158 struct radv_shader_module *module,
159 const char *entrypoint_name,
160 gl_shader_stage stage,
Samuel Pitoiset47efc522017-09-01 12:09:56 +0200161 const VkSpecializationInfo *spec_info)
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200162{
163 if (strcmp(entrypoint_name, "main") != 0) {
164 radv_finishme("Multiple shaders per module not really supported");
165 }
166
167 nir_shader *nir;
168 nir_function *entry_point;
169 if (module->nir) {
170 /* Some things such as our meta clear/blit code will give us a NIR
171 * shader directly. In that case, we just ignore the SPIR-V entirely
172 * and just use the NIR shader */
173 nir = module->nir;
174 nir->options = &nir_options;
175 nir_validate_shader(nir);
176
177 assert(exec_list_length(&nir->functions) == 1);
178 struct exec_node *node = exec_list_get_head(&nir->functions);
179 entry_point = exec_node_data(nir_function, node, node);
180 } else {
181 uint32_t *spirv = (uint32_t *) module->data;
182 assert(module->size % 4 == 0);
183
Timothy Arceri7664aaf2017-10-11 11:59:20 +1100184 if (device->instance->debug_flags & RADV_DEBUG_DUMP_SPIRV)
Samuel Pitoiset844ae722017-09-22 16:56:40 +0200185 radv_print_spirv(spirv, module->size, stderr);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200186
187 uint32_t num_spec_entries = 0;
188 struct nir_spirv_specialization *spec_entries = NULL;
189 if (spec_info && spec_info->mapEntryCount > 0) {
190 num_spec_entries = spec_info->mapEntryCount;
191 spec_entries = malloc(num_spec_entries * sizeof(*spec_entries));
192 for (uint32_t i = 0; i < num_spec_entries; i++) {
193 VkSpecializationMapEntry entry = spec_info->pMapEntries[i];
194 const void *data = spec_info->pData + entry.offset;
195 assert(data + entry.size <= spec_info->pData + spec_info->dataSize);
196
197 spec_entries[i].id = spec_info->pMapEntries[i].constantID;
198 if (spec_info->dataSize == 8)
199 spec_entries[i].data64 = *(const uint64_t *)data;
200 else
201 spec_entries[i].data32 = *(const uint32_t *)data;
202 }
203 }
Jason Ekstrande19c6232017-10-18 17:28:19 -0700204 const struct spirv_to_nir_options spirv_options = {
205 .caps = {
206 .draw_parameters = true,
207 .float64 = true,
208 .image_read_without_format = true,
209 .image_write_without_format = true,
210 .tessellation = true,
211 .int64 = true,
212 .multiview = true,
Bas Nieuwenhuizen8f9af582018-01-21 15:06:10 +0100213 .subgroup_basic = true,
Jason Ekstrande19c6232017-10-18 17:28:19 -0700214 .variable_pointers = true,
215 },
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200216 };
217 entry_point = spirv_to_nir(spirv, module->size / 4,
218 spec_entries, num_spec_entries,
Jason Ekstrande19c6232017-10-18 17:28:19 -0700219 stage, entrypoint_name,
220 &spirv_options, &nir_options);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200221 nir = entry_point->shader;
Jason Ekstrand59fb59a2017-09-14 19:52:38 -0700222 assert(nir->info.stage == stage);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200223 nir_validate_shader(nir);
224
225 free(spec_entries);
226
227 /* We have to lower away local constant initializers right before we
228 * inline functions. That way they get properly initialized at the top
229 * of the function and not at the top of its caller.
230 */
231 NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_local);
232 NIR_PASS_V(nir, nir_lower_returns);
233 NIR_PASS_V(nir, nir_inline_functions);
234
235 /* Pick off the single entrypoint that we want */
236 foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
237 if (func != entry_point)
238 exec_node_remove(&func->node);
239 }
240 assert(exec_list_length(&nir->functions) == 1);
241 entry_point->name = ralloc_strdup(entry_point, "main");
242
243 NIR_PASS_V(nir, nir_remove_dead_variables,
244 nir_var_shader_in | nir_var_shader_out | nir_var_system_value);
245
246 /* Now that we've deleted all but the main function, we can go ahead and
247 * lower the rest of the constant initializers.
248 */
249 NIR_PASS_V(nir, nir_lower_constant_initializers, ~0);
250 NIR_PASS_V(nir, nir_lower_system_values);
251 NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays);
252 }
253
254 /* Vulkan uses the separate-shader linking model */
255 nir->info.separate_shader = true;
256
257 nir_shader_gather_info(nir, entry_point->impl);
258
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200259 static const nir_lower_tex_options tex_options = {
260 .lower_txp = ~0,
261 };
262
263 nir_lower_tex(nir, &tex_options);
264
265 nir_lower_vars_to_ssa(nir);
266 nir_lower_var_copies(nir);
267 nir_lower_global_vars_to_local(nir);
268 nir_remove_dead_variables(nir, nir_var_local);
Timothy Arceri0f2c7342018-03-05 11:13:11 +1100269 ac_lower_indirect_derefs(nir, device->physical_device->rad_info.chip_class);
Bas Nieuwenhuizen8f9af582018-01-21 15:06:10 +0100270 nir_lower_subgroups(nir, &(struct nir_lower_subgroups_options) {
271 .subgroup_size = 64,
272 .ballot_bit_size = 64,
273 .lower_to_scalar = 1,
274 .lower_subgroup_masks = 1,
275 .lower_shuffle = 1,
276 .lower_quad = 1,
277 });
278
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200279 radv_optimize_nir(nir);
280
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200281 return nir;
282}
283
284void *
285radv_alloc_shader_memory(struct radv_device *device,
286 struct radv_shader_variant *shader)
287{
288 mtx_lock(&device->shader_slab_mutex);
289 list_for_each_entry(struct radv_shader_slab, slab, &device->shader_slabs, slabs) {
290 uint64_t offset = 0;
291 list_for_each_entry(struct radv_shader_variant, s, &slab->shaders, slab_list) {
292 if (s->bo_offset - offset >= shader->code_size) {
293 shader->bo = slab->bo;
294 shader->bo_offset = offset;
295 list_addtail(&shader->slab_list, &s->slab_list);
296 mtx_unlock(&device->shader_slab_mutex);
297 return slab->ptr + offset;
298 }
299 offset = align_u64(s->bo_offset + s->code_size, 256);
300 }
301 if (slab->size - offset >= shader->code_size) {
302 shader->bo = slab->bo;
303 shader->bo_offset = offset;
304 list_addtail(&shader->slab_list, &slab->shaders);
305 mtx_unlock(&device->shader_slab_mutex);
306 return slab->ptr + offset;
307 }
308 }
309
310 mtx_unlock(&device->shader_slab_mutex);
311 struct radv_shader_slab *slab = calloc(1, sizeof(struct radv_shader_slab));
312
313 slab->size = 256 * 1024;
314 slab->bo = device->ws->buffer_create(device->ws, slab->size, 256,
Samuel Pitoiseta3c2a862018-01-04 15:19:47 +0100315 RADEON_DOMAIN_VRAM,
316 RADEON_FLAG_NO_INTERPROCESS_SHARING |
317 device->physical_device->cpdma_prefetch_writes_memory ?
318 0 : RADEON_FLAG_READ_ONLY);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200319 slab->ptr = (char*)device->ws->buffer_map(slab->bo);
320 list_inithead(&slab->shaders);
321
322 mtx_lock(&device->shader_slab_mutex);
323 list_add(&slab->slabs, &device->shader_slabs);
324
325 shader->bo = slab->bo;
326 shader->bo_offset = 0;
327 list_add(&shader->slab_list, &slab->shaders);
328 mtx_unlock(&device->shader_slab_mutex);
329 return slab->ptr;
330}
331
332void
333radv_destroy_shader_slabs(struct radv_device *device)
334{
335 list_for_each_entry_safe(struct radv_shader_slab, slab, &device->shader_slabs, slabs) {
336 device->ws->buffer_destroy(slab->bo);
337 free(slab);
338 }
339 mtx_destroy(&device->shader_slab_mutex);
340}
341
342static void
343radv_fill_shader_variant(struct radv_device *device,
344 struct radv_shader_variant *variant,
345 struct ac_shader_binary *binary,
346 gl_shader_stage stage)
347{
348 bool scratch_enabled = variant->config.scratch_bytes_per_wave > 0;
349 unsigned vgpr_comp_cnt = 0;
350
351 if (scratch_enabled && !device->llvm_supports_spill)
352 radv_finishme("shader scratch support only available with LLVM 4.0");
353
354 variant->code_size = binary->code_size;
355 variant->rsrc2 = S_00B12C_USER_SGPR(variant->info.num_user_sgprs) |
356 S_00B12C_SCRATCH_EN(scratch_enabled);
357
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200358 variant->rsrc1 = S_00B848_VGPRS((variant->config.num_vgprs - 1) / 4) |
359 S_00B848_SGPRS((variant->config.num_sgprs - 1) / 8) |
360 S_00B848_DX10_CLAMP(1) |
361 S_00B848_FLOAT_MODE(variant->config.float_mode);
362
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200363 switch (stage) {
364 case MESA_SHADER_TESS_EVAL:
365 vgpr_comp_cnt = 3;
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200366 variant->rsrc2 |= S_00B12C_OC_LDS_EN(1);
367 break;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200368 case MESA_SHADER_TESS_CTRL:
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200369 if (device->physical_device->rad_info.chip_class >= GFX9)
370 vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
371 else
372 variant->rsrc2 |= S_00B12C_OC_LDS_EN(1);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200373 break;
374 case MESA_SHADER_VERTEX:
375 case MESA_SHADER_GEOMETRY:
376 vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
377 break;
378 case MESA_SHADER_FRAGMENT:
379 break;
Samuel Pitoiset2294d352017-12-14 16:48:03 +0100380 case MESA_SHADER_COMPUTE: {
381 struct ac_shader_info *info = &variant->info.info;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200382 variant->rsrc2 |=
Samuel Pitoiset4237c3d2017-12-18 22:06:38 +0100383 S_00B84C_TGID_X_EN(info->cs.uses_block_id[0]) |
384 S_00B84C_TGID_Y_EN(info->cs.uses_block_id[1]) |
385 S_00B84C_TGID_Z_EN(info->cs.uses_block_id[2]) |
386 S_00B84C_TIDIG_COMP_CNT(info->cs.uses_thread_id[2] ? 2 :
387 info->cs.uses_thread_id[1] ? 1 : 0) |
Samuel Pitoiset90c3bf02017-12-14 17:32:41 +0100388 S_00B84C_TG_SIZE_EN(info->cs.uses_local_invocation_idx) |
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200389 S_00B84C_LDS_SIZE(variant->config.lds_size);
390 break;
Samuel Pitoiset2294d352017-12-14 16:48:03 +0100391 }
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200392 default:
393 unreachable("unsupported shader type");
394 break;
395 }
396
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200397 if (device->physical_device->rad_info.chip_class >= GFX9 &&
Bas Nieuwenhuizen73749ca2017-10-20 02:24:24 +0200398 stage == MESA_SHADER_GEOMETRY) {
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100399 struct ac_shader_info *info = &variant->info.info;
Samuel Pitoiset232c4182018-01-09 16:01:09 +0100400 unsigned es_type = variant->info.gs.es_type;
Samuel Pitoiset4e701cf2018-01-09 16:01:10 +0100401 unsigned gs_vgpr_comp_cnt, es_vgpr_comp_cnt;
402
403 if (es_type == MESA_SHADER_VERTEX) {
404 es_vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
405 } else if (es_type == MESA_SHADER_TESS_EVAL) {
406 es_vgpr_comp_cnt = 3;
407 } else {
Bas Nieuwenhuizen0f89f9b2018-01-17 23:23:02 +0100408 unreachable("invalid shader ES type");
Samuel Pitoiset4e701cf2018-01-09 16:01:10 +0100409 }
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100410
411 /* If offsets 4, 5 are used, GS_VGPR_COMP_CNT is ignored and
412 * VGPR[0:4] are always loaded.
413 */
414 if (info->uses_invocation_id)
415 gs_vgpr_comp_cnt = 3; /* VGPR3 contains InvocationID. */
416 else if (info->uses_prim_id)
417 gs_vgpr_comp_cnt = 2; /* VGPR2 contains PrimitiveID. */
Samuel Pitoisetb462ceb2018-01-05 17:18:52 +0100418 else if (variant->info.gs.vertices_in >= 3)
419 gs_vgpr_comp_cnt = 1; /* VGPR1 contains offsets 2, 3 */
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100420 else
Samuel Pitoisetb462ceb2018-01-05 17:18:52 +0100421 gs_vgpr_comp_cnt = 0; /* VGPR0 contains offsets 0, 1 */
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100422
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100423 variant->rsrc1 |= S_00B228_GS_VGPR_COMP_CNT(gs_vgpr_comp_cnt);
Samuel Pitoiset4e701cf2018-01-09 16:01:10 +0100424 variant->rsrc2 |= S_00B22C_ES_VGPR_COMP_CNT(es_vgpr_comp_cnt) |
Samuel Pitoiset232c4182018-01-09 16:01:09 +0100425 S_00B22C_OC_LDS_EN(es_type == MESA_SHADER_TESS_EVAL);
Bas Nieuwenhuizen73749ca2017-10-20 02:24:24 +0200426 } else if (device->physical_device->rad_info.chip_class >= GFX9 &&
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200427 stage == MESA_SHADER_TESS_CTRL)
428 variant->rsrc1 |= S_00B428_LS_VGPR_COMP_CNT(vgpr_comp_cnt);
429 else
430 variant->rsrc1 |= S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200431
432 void *ptr = radv_alloc_shader_memory(device, variant);
433 memcpy(ptr, binary->code, binary->code_size);
434}
435
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200436static struct radv_shader_variant *
437shader_variant_create(struct radv_device *device,
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200438 struct radv_shader_module *module,
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200439 struct nir_shader * const *shaders,
440 int shader_count,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200441 gl_shader_stage stage,
442 struct ac_nir_compiler_options *options,
443 bool gs_copy_shader,
444 void **code_out,
445 unsigned *code_size_out)
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200446{
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200447 enum radeon_family chip_family = device->physical_device->rad_info.family;
Samuel Pitoiset921986b2017-11-30 22:16:09 +0100448 bool dump_shaders = radv_can_dump_shader(device, module);
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200449 enum ac_target_machine_options tm_options = 0;
450 struct radv_shader_variant *variant;
451 struct ac_shader_binary binary;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200452 LLVMTargetMachineRef tm;
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200453
454 variant = calloc(1, sizeof(struct radv_shader_variant));
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200455 if (!variant)
456 return NULL;
457
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200458 options->family = chip_family;
459 options->chip_class = device->physical_device->rad_info.chip_class;
Samuel Pitoiset33e6e5e2018-01-19 12:12:02 +0100460 options->dump_preoptir = radv_can_dump_shader(device, module) &&
461 device->instance->debug_flags & RADV_DEBUG_PREOPTIR;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200462
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200463 if (options->supports_spill)
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200464 tm_options |= AC_TM_SUPPORTS_SPILL;
465 if (device->instance->perftest_flags & RADV_PERFTEST_SISCHED)
466 tm_options |= AC_TM_SISCHED;
467 tm = ac_create_target_machine(chip_family, tm_options);
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200468
469 if (gs_copy_shader) {
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200470 assert(shader_count == 1);
471 ac_create_gs_copy_shader(tm, *shaders, &binary, &variant->config,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200472 &variant->info, options, dump_shaders);
473 } else {
474 ac_compile_nir_shader(tm, &binary, &variant->config,
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200475 &variant->info, shaders, shader_count, options,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200476 dump_shaders);
477 }
478
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200479 LLVMDisposeTargetMachine(tm);
480
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200481 radv_fill_shader_variant(device, variant, &binary, stage);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200482
483 if (code_out) {
484 *code_out = binary.code;
485 *code_size_out = binary.code_size;
486 } else
487 free(binary.code);
488 free(binary.config);
489 free(binary.rodata);
490 free(binary.global_symbol_offsets);
491 free(binary.relocs);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200492 variant->ref_count = 1;
Samuel Pitoiset885d7572017-09-01 13:45:33 +0200493
Alex Smithde889792017-10-27 14:25:05 +0100494 if (device->keep_shader_info) {
Samuel Pitoiset885d7572017-09-01 13:45:33 +0200495 variant->disasm_string = binary.disasm_string;
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200496 if (!gs_copy_shader && !module->nir) {
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200497 variant->nir = *shaders;
Samuel Pitoiset844ae722017-09-22 16:56:40 +0200498 variant->spirv = (uint32_t *)module->data;
499 variant->spirv_size = module->size;
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200500 }
Samuel Pitoiset885d7572017-09-01 13:45:33 +0200501 } else {
502 free(binary.disasm_string);
503 }
504
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200505 return variant;
506}
507
508struct radv_shader_variant *
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200509radv_shader_variant_create(struct radv_device *device,
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200510 struct radv_shader_module *module,
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200511 struct nir_shader *const *shaders,
512 int shader_count,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200513 struct radv_pipeline_layout *layout,
514 const struct ac_shader_variant_key *key,
515 void **code_out,
516 unsigned *code_size_out)
517{
518 struct ac_nir_compiler_options options = {0};
519
520 options.layout = layout;
521 if (key)
522 options.key = *key;
523
Timothy Arceri7664aaf2017-10-11 11:59:20 +1100524 options.unsafe_math = !!(device->instance->debug_flags & RADV_DEBUG_UNSAFE_MATH);
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200525 options.supports_spill = device->llvm_supports_spill;
526
Jason Ekstrand59fb59a2017-09-14 19:52:38 -0700527 return shader_variant_create(device, module, shaders, shader_count, shaders[shader_count - 1]->info.stage,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200528 &options, false, code_out, code_size_out);
529}
530
531struct radv_shader_variant *
532radv_create_gs_copy_shader(struct radv_device *device,
533 struct nir_shader *shader,
534 void **code_out,
535 unsigned *code_size_out,
Samuel Pitoiset47efc522017-09-01 12:09:56 +0200536 bool multiview)
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200537{
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200538 struct ac_nir_compiler_options options = {0};
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200539
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200540 options.key.has_multiview_view_index = multiview;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200541
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200542 return shader_variant_create(device, NULL, &shader, 1, MESA_SHADER_VERTEX,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200543 &options, true, code_out, code_size_out);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200544}
545
546void
547radv_shader_variant_destroy(struct radv_device *device,
548 struct radv_shader_variant *variant)
549{
550 if (!p_atomic_dec_zero(&variant->ref_count))
551 return;
552
553 mtx_lock(&device->shader_slab_mutex);
554 list_del(&variant->slab_list);
555 mtx_unlock(&device->shader_slab_mutex);
556
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200557 ralloc_free(variant->nir);
Samuel Pitoiset885d7572017-09-01 13:45:33 +0200558 free(variant->disasm_string);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200559 free(variant);
560}
561
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200562const char *
563radv_get_shader_name(struct radv_shader_variant *var, gl_shader_stage stage)
564{
565 switch (stage) {
566 case MESA_SHADER_VERTEX: return var->info.vs.as_ls ? "Vertex Shader as LS" : var->info.vs.as_es ? "Vertex Shader as ES" : "Vertex Shader as VS";
567 case MESA_SHADER_GEOMETRY: return "Geometry Shader";
568 case MESA_SHADER_FRAGMENT: return "Pixel Shader";
569 case MESA_SHADER_COMPUTE: return "Compute Shader";
570 case MESA_SHADER_TESS_CTRL: return "Tessellation Control Shader";
571 case MESA_SHADER_TESS_EVAL: return var->info.tes.as_es ? "Tessellation Evaluation Shader as ES" : "Tessellation Evaluation Shader as VS";
572 default:
573 return "Unknown shader";
574 };
575}
576
Alex Smithde889792017-10-27 14:25:05 +0100577static uint32_t
578get_total_sgprs(struct radv_device *device)
579{
580 if (device->physical_device->rad_info.chip_class >= VI)
581 return 800;
582 else
583 return 512;
584}
585
586static void
587generate_shader_stats(struct radv_device *device,
588 struct radv_shader_variant *variant,
589 gl_shader_stage stage,
590 struct _mesa_string_buffer *buf)
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200591{
592 unsigned lds_increment = device->physical_device->rad_info.chip_class >= CIK ? 512 : 256;
593 struct ac_shader_config *conf;
594 unsigned max_simd_waves;
595 unsigned lds_per_wave = 0;
596
597 switch (device->physical_device->rad_info.family) {
598 /* These always have 8 waves: */
599 case CHIP_POLARIS10:
600 case CHIP_POLARIS11:
601 case CHIP_POLARIS12:
602 max_simd_waves = 8;
603 break;
604 default:
605 max_simd_waves = 10;
606 }
607
608 conf = &variant->config;
609
610 if (stage == MESA_SHADER_FRAGMENT) {
611 lds_per_wave = conf->lds_size * lds_increment +
612 align(variant->info.fs.num_interp * 48,
613 lds_increment);
614 }
615
Alex Smithde889792017-10-27 14:25:05 +0100616 if (conf->num_sgprs)
617 max_simd_waves = MIN2(max_simd_waves, get_total_sgprs(device) / conf->num_sgprs);
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200618
619 if (conf->num_vgprs)
620 max_simd_waves = MIN2(max_simd_waves, 256 / conf->num_vgprs);
621
622 /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
623 * that PS can use.
624 */
625 if (lds_per_wave)
626 max_simd_waves = MIN2(max_simd_waves, 16384 / lds_per_wave);
627
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200628 if (stage == MESA_SHADER_FRAGMENT) {
Alex Smithde889792017-10-27 14:25:05 +0100629 _mesa_string_buffer_printf(buf, "*** SHADER CONFIG ***\n"
630 "SPI_PS_INPUT_ADDR = 0x%04x\n"
631 "SPI_PS_INPUT_ENA = 0x%04x\n",
632 conf->spi_ps_input_addr, conf->spi_ps_input_ena);
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200633 }
634
Alex Smithde889792017-10-27 14:25:05 +0100635 _mesa_string_buffer_printf(buf, "*** SHADER STATS ***\n"
636 "SGPRS: %d\n"
637 "VGPRS: %d\n"
638 "Spilled SGPRs: %d\n"
639 "Spilled VGPRs: %d\n"
Samuel Pitoisete96e6f62018-03-01 22:12:56 +0100640 "PrivMem VGPRS: %d\n"
Alex Smithde889792017-10-27 14:25:05 +0100641 "Code Size: %d bytes\n"
642 "LDS: %d blocks\n"
643 "Scratch: %d bytes per wave\n"
644 "Max Waves: %d\n"
645 "********************\n\n\n",
646 conf->num_sgprs, conf->num_vgprs,
Samuel Pitoisete96e6f62018-03-01 22:12:56 +0100647 conf->spilled_sgprs, conf->spilled_vgprs,
648 variant->info.private_mem_vgprs, variant->code_size,
Alex Smithde889792017-10-27 14:25:05 +0100649 conf->lds_size, conf->scratch_bytes_per_wave,
650 max_simd_waves);
651}
652
653void
654radv_shader_dump_stats(struct radv_device *device,
655 struct radv_shader_variant *variant,
656 gl_shader_stage stage,
657 FILE *file)
658{
659 struct _mesa_string_buffer *buf = _mesa_string_buffer_create(NULL, 256);
660
661 generate_shader_stats(device, variant, stage, buf);
662
663 fprintf(file, "\n%s:\n", radv_get_shader_name(variant, stage));
Alex Smith134a40d2017-10-30 08:38:14 +0000664 fprintf(file, "%s", buf->buf);
Alex Smithde889792017-10-27 14:25:05 +0100665
666 _mesa_string_buffer_destroy(buf);
667}
668
669VkResult
670radv_GetShaderInfoAMD(VkDevice _device,
671 VkPipeline _pipeline,
672 VkShaderStageFlagBits shaderStage,
673 VkShaderInfoTypeAMD infoType,
674 size_t* pInfoSize,
675 void* pInfo)
676{
677 RADV_FROM_HANDLE(radv_device, device, _device);
678 RADV_FROM_HANDLE(radv_pipeline, pipeline, _pipeline);
679 gl_shader_stage stage = vk_to_mesa_shader_stage(shaderStage);
680 struct radv_shader_variant *variant = pipeline->shaders[stage];
681 struct _mesa_string_buffer *buf;
682 VkResult result = VK_SUCCESS;
683
684 /* Spec doesn't indicate what to do if the stage is invalid, so just
685 * return no info for this. */
686 if (!variant)
Samuel Pitoisetcd64a4f2017-11-10 09:17:58 +0100687 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
Alex Smithde889792017-10-27 14:25:05 +0100688
689 switch (infoType) {
690 case VK_SHADER_INFO_TYPE_STATISTICS_AMD:
691 if (!pInfo) {
692 *pInfoSize = sizeof(VkShaderStatisticsInfoAMD);
693 } else {
694 unsigned lds_multiplier = device->physical_device->rad_info.chip_class >= CIK ? 512 : 256;
695 struct ac_shader_config *conf = &variant->config;
696
697 VkShaderStatisticsInfoAMD statistics = {};
698 statistics.shaderStageMask = shaderStage;
699 statistics.numPhysicalVgprs = 256;
700 statistics.numPhysicalSgprs = get_total_sgprs(device);
701 statistics.numAvailableSgprs = statistics.numPhysicalSgprs;
702
703 if (stage == MESA_SHADER_COMPUTE) {
704 unsigned *local_size = variant->nir->info.cs.local_size;
705 unsigned workgroup_size = local_size[0] * local_size[1] * local_size[2];
706
707 statistics.numAvailableVgprs = statistics.numPhysicalVgprs /
708 ceil(workgroup_size / statistics.numPhysicalVgprs);
709
710 statistics.computeWorkGroupSize[0] = local_size[0];
711 statistics.computeWorkGroupSize[1] = local_size[1];
712 statistics.computeWorkGroupSize[2] = local_size[2];
713 } else {
714 statistics.numAvailableVgprs = statistics.numPhysicalVgprs;
715 }
716
717 statistics.resourceUsage.numUsedVgprs = conf->num_vgprs;
718 statistics.resourceUsage.numUsedSgprs = conf->num_sgprs;
719 statistics.resourceUsage.ldsSizePerLocalWorkGroup = 32768;
720 statistics.resourceUsage.ldsUsageSizeInBytes = conf->lds_size * lds_multiplier;
721 statistics.resourceUsage.scratchMemUsageInBytes = conf->scratch_bytes_per_wave;
722
723 size_t size = *pInfoSize;
724 *pInfoSize = sizeof(statistics);
725
726 memcpy(pInfo, &statistics, MIN2(size, *pInfoSize));
727
728 if (size < *pInfoSize)
729 result = VK_INCOMPLETE;
730 }
731
732 break;
733 case VK_SHADER_INFO_TYPE_DISASSEMBLY_AMD:
734 buf = _mesa_string_buffer_create(NULL, 1024);
735
736 _mesa_string_buffer_printf(buf, "%s:\n", radv_get_shader_name(variant, stage));
737 _mesa_string_buffer_printf(buf, "%s\n\n", variant->disasm_string);
738 generate_shader_stats(device, variant, stage, buf);
739
740 /* Need to include the null terminator. */
741 size_t length = buf->length + 1;
742
743 if (!pInfo) {
744 *pInfoSize = length;
745 } else {
746 size_t size = *pInfoSize;
747 *pInfoSize = length;
748
749 memcpy(pInfo, buf->buf, MIN2(size, length));
750
751 if (size < length)
752 result = VK_INCOMPLETE;
753 }
754
755 _mesa_string_buffer_destroy(buf);
756 break;
757 default:
758 /* VK_SHADER_INFO_TYPE_BINARY_AMD unimplemented for now. */
759 result = VK_ERROR_FEATURE_NOT_PRESENT;
760 break;
761 }
762
763 return result;
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200764}