blob: dd7e0e0af6dc4f849c52971901b2b530af5ba57e [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,
Bas Nieuwenhuizen5240fdd2018-01-21 17:13:26 +010056 .lower_device_index_to_zero = true,
Samuel Pitoisetd4d77732017-09-01 11:41:18 +020057 .lower_fsat = true,
58 .lower_fdiv = true,
59 .lower_sub = true,
60 .lower_pack_snorm_2x16 = true,
61 .lower_pack_snorm_4x8 = true,
62 .lower_pack_unorm_2x16 = true,
63 .lower_pack_unorm_4x8 = true,
64 .lower_unpack_snorm_2x16 = true,
65 .lower_unpack_snorm_4x8 = true,
66 .lower_unpack_unorm_2x16 = true,
67 .lower_unpack_unorm_4x8 = true,
68 .lower_extract_byte = true,
69 .lower_extract_word = true,
Dave Airlie2c615942017-10-04 06:33:02 +100070 .lower_ffma = true,
Samuel Pitoiset7aa008d2018-02-02 19:04:57 +010071 .lower_fpow = true,
Timothy Arceri5b8de4b2018-01-08 10:37:27 +110072 .vs_inputs_dual_locations = true,
Samuel Pitoisetd4d77732017-09-01 11:41:18 +020073 .max_unroll_iterations = 32
74};
75
76VkResult radv_CreateShaderModule(
77 VkDevice _device,
78 const VkShaderModuleCreateInfo* pCreateInfo,
79 const VkAllocationCallbacks* pAllocator,
80 VkShaderModule* pShaderModule)
81{
82 RADV_FROM_HANDLE(radv_device, device, _device);
83 struct radv_shader_module *module;
84
85 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
86 assert(pCreateInfo->flags == 0);
87
88 module = vk_alloc2(&device->alloc, pAllocator,
89 sizeof(*module) + pCreateInfo->codeSize, 8,
90 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
91 if (module == NULL)
92 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
93
94 module->nir = NULL;
95 module->size = pCreateInfo->codeSize;
96 memcpy(module->data, pCreateInfo->pCode, module->size);
97
98 _mesa_sha1_compute(module->data, module->size, module->sha1);
99
100 *pShaderModule = radv_shader_module_to_handle(module);
101
102 return VK_SUCCESS;
103}
104
105void radv_DestroyShaderModule(
106 VkDevice _device,
107 VkShaderModule _module,
108 const VkAllocationCallbacks* pAllocator)
109{
110 RADV_FROM_HANDLE(radv_device, device, _device);
111 RADV_FROM_HANDLE(radv_shader_module, module, _module);
112
113 if (!module)
114 return;
115
116 vk_free2(&device->alloc, pAllocator, module);
117}
118
Bas Nieuwenhuizen06f05042017-02-09 00:12:10 +0100119void
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200120radv_optimize_nir(struct nir_shader *shader)
121{
122 bool progress;
123
124 do {
125 progress = false;
126
127 NIR_PASS_V(shader, nir_lower_vars_to_ssa);
128 NIR_PASS_V(shader, nir_lower_64bit_pack);
129 NIR_PASS_V(shader, nir_lower_alu_to_scalar);
130 NIR_PASS_V(shader, nir_lower_phis_to_scalar);
131
132 NIR_PASS(progress, shader, nir_copy_prop);
133 NIR_PASS(progress, shader, nir_opt_remove_phis);
134 NIR_PASS(progress, shader, nir_opt_dce);
135 if (nir_opt_trivial_continues(shader)) {
136 progress = true;
137 NIR_PASS(progress, shader, nir_copy_prop);
Dave Airlie64d9bd12017-09-13 03:49:31 +0100138 NIR_PASS(progress, shader, nir_opt_remove_phis);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200139 NIR_PASS(progress, shader, nir_opt_dce);
140 }
141 NIR_PASS(progress, shader, nir_opt_if);
142 NIR_PASS(progress, shader, nir_opt_dead_cf);
143 NIR_PASS(progress, shader, nir_opt_cse);
144 NIR_PASS(progress, shader, nir_opt_peephole_select, 8);
145 NIR_PASS(progress, shader, nir_opt_algebraic);
146 NIR_PASS(progress, shader, nir_opt_constant_folding);
147 NIR_PASS(progress, shader, nir_opt_undef);
148 NIR_PASS(progress, shader, nir_opt_conditional_discard);
149 if (shader->options->max_unroll_iterations) {
150 NIR_PASS(progress, shader, nir_opt_loop_unroll, 0);
151 }
152 } while (progress);
Samuel Pitoiset3488a3f2018-01-29 17:19:18 +0100153
154 NIR_PASS(progress, shader, nir_opt_shrink_load);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200155}
156
157nir_shader *
158radv_shader_compile_to_nir(struct radv_device *device,
159 struct radv_shader_module *module,
160 const char *entrypoint_name,
161 gl_shader_stage stage,
Samuel Pitoiset47efc522017-09-01 12:09:56 +0200162 const VkSpecializationInfo *spec_info)
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200163{
164 if (strcmp(entrypoint_name, "main") != 0) {
165 radv_finishme("Multiple shaders per module not really supported");
166 }
167
168 nir_shader *nir;
169 nir_function *entry_point;
170 if (module->nir) {
171 /* Some things such as our meta clear/blit code will give us a NIR
172 * shader directly. In that case, we just ignore the SPIR-V entirely
173 * and just use the NIR shader */
174 nir = module->nir;
175 nir->options = &nir_options;
176 nir_validate_shader(nir);
177
178 assert(exec_list_length(&nir->functions) == 1);
179 struct exec_node *node = exec_list_get_head(&nir->functions);
180 entry_point = exec_node_data(nir_function, node, node);
181 } else {
182 uint32_t *spirv = (uint32_t *) module->data;
183 assert(module->size % 4 == 0);
184
Timothy Arceri7664aaf2017-10-11 11:59:20 +1100185 if (device->instance->debug_flags & RADV_DEBUG_DUMP_SPIRV)
Samuel Pitoiset844ae722017-09-22 16:56:40 +0200186 radv_print_spirv(spirv, module->size, stderr);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200187
188 uint32_t num_spec_entries = 0;
189 struct nir_spirv_specialization *spec_entries = NULL;
190 if (spec_info && spec_info->mapEntryCount > 0) {
191 num_spec_entries = spec_info->mapEntryCount;
192 spec_entries = malloc(num_spec_entries * sizeof(*spec_entries));
193 for (uint32_t i = 0; i < num_spec_entries; i++) {
194 VkSpecializationMapEntry entry = spec_info->pMapEntries[i];
195 const void *data = spec_info->pData + entry.offset;
196 assert(data + entry.size <= spec_info->pData + spec_info->dataSize);
197
198 spec_entries[i].id = spec_info->pMapEntries[i].constantID;
199 if (spec_info->dataSize == 8)
200 spec_entries[i].data64 = *(const uint64_t *)data;
201 else
202 spec_entries[i].data32 = *(const uint32_t *)data;
203 }
204 }
Jason Ekstrande19c6232017-10-18 17:28:19 -0700205 const struct spirv_to_nir_options spirv_options = {
206 .caps = {
Bas Nieuwenhuizen5240fdd2018-01-21 17:13:26 +0100207 .device_group = true,
Jason Ekstrande19c6232017-10-18 17:28:19 -0700208 .draw_parameters = true,
209 .float64 = true,
210 .image_read_without_format = true,
211 .image_write_without_format = true,
212 .tessellation = true,
213 .int64 = true,
214 .multiview = true,
Bas Nieuwenhuizen8f9af582018-01-21 15:06:10 +0100215 .subgroup_basic = true,
Jason Ekstrande19c6232017-10-18 17:28:19 -0700216 .variable_pointers = true,
217 },
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200218 };
219 entry_point = spirv_to_nir(spirv, module->size / 4,
220 spec_entries, num_spec_entries,
Jason Ekstrande19c6232017-10-18 17:28:19 -0700221 stage, entrypoint_name,
222 &spirv_options, &nir_options);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200223 nir = entry_point->shader;
Jason Ekstrand59fb59a2017-09-14 19:52:38 -0700224 assert(nir->info.stage == stage);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200225 nir_validate_shader(nir);
226
227 free(spec_entries);
228
229 /* We have to lower away local constant initializers right before we
230 * inline functions. That way they get properly initialized at the top
231 * of the function and not at the top of its caller.
232 */
233 NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_local);
234 NIR_PASS_V(nir, nir_lower_returns);
235 NIR_PASS_V(nir, nir_inline_functions);
236
237 /* Pick off the single entrypoint that we want */
238 foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
239 if (func != entry_point)
240 exec_node_remove(&func->node);
241 }
242 assert(exec_list_length(&nir->functions) == 1);
243 entry_point->name = ralloc_strdup(entry_point, "main");
244
245 NIR_PASS_V(nir, nir_remove_dead_variables,
246 nir_var_shader_in | nir_var_shader_out | nir_var_system_value);
247
248 /* Now that we've deleted all but the main function, we can go ahead and
249 * lower the rest of the constant initializers.
250 */
251 NIR_PASS_V(nir, nir_lower_constant_initializers, ~0);
252 NIR_PASS_V(nir, nir_lower_system_values);
253 NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays);
254 }
255
256 /* Vulkan uses the separate-shader linking model */
257 nir->info.separate_shader = true;
258
259 nir_shader_gather_info(nir, entry_point->impl);
260
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200261 static const nir_lower_tex_options tex_options = {
262 .lower_txp = ~0,
263 };
264
265 nir_lower_tex(nir, &tex_options);
266
267 nir_lower_vars_to_ssa(nir);
268 nir_lower_var_copies(nir);
269 nir_lower_global_vars_to_local(nir);
270 nir_remove_dead_variables(nir, nir_var_local);
Timothy Arceri0f2c7342018-03-05 11:13:11 +1100271 ac_lower_indirect_derefs(nir, device->physical_device->rad_info.chip_class);
Bas Nieuwenhuizen8f9af582018-01-21 15:06:10 +0100272 nir_lower_subgroups(nir, &(struct nir_lower_subgroups_options) {
273 .subgroup_size = 64,
274 .ballot_bit_size = 64,
275 .lower_to_scalar = 1,
276 .lower_subgroup_masks = 1,
277 .lower_shuffle = 1,
278 .lower_quad = 1,
279 });
280
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200281 radv_optimize_nir(nir);
282
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200283 return nir;
284}
285
286void *
287radv_alloc_shader_memory(struct radv_device *device,
288 struct radv_shader_variant *shader)
289{
290 mtx_lock(&device->shader_slab_mutex);
291 list_for_each_entry(struct radv_shader_slab, slab, &device->shader_slabs, slabs) {
292 uint64_t offset = 0;
293 list_for_each_entry(struct radv_shader_variant, s, &slab->shaders, slab_list) {
294 if (s->bo_offset - offset >= shader->code_size) {
295 shader->bo = slab->bo;
296 shader->bo_offset = offset;
297 list_addtail(&shader->slab_list, &s->slab_list);
298 mtx_unlock(&device->shader_slab_mutex);
299 return slab->ptr + offset;
300 }
301 offset = align_u64(s->bo_offset + s->code_size, 256);
302 }
303 if (slab->size - offset >= shader->code_size) {
304 shader->bo = slab->bo;
305 shader->bo_offset = offset;
306 list_addtail(&shader->slab_list, &slab->shaders);
307 mtx_unlock(&device->shader_slab_mutex);
308 return slab->ptr + offset;
309 }
310 }
311
312 mtx_unlock(&device->shader_slab_mutex);
313 struct radv_shader_slab *slab = calloc(1, sizeof(struct radv_shader_slab));
314
315 slab->size = 256 * 1024;
316 slab->bo = device->ws->buffer_create(device->ws, slab->size, 256,
Samuel Pitoiseta3c2a862018-01-04 15:19:47 +0100317 RADEON_DOMAIN_VRAM,
318 RADEON_FLAG_NO_INTERPROCESS_SHARING |
319 device->physical_device->cpdma_prefetch_writes_memory ?
320 0 : RADEON_FLAG_READ_ONLY);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200321 slab->ptr = (char*)device->ws->buffer_map(slab->bo);
322 list_inithead(&slab->shaders);
323
324 mtx_lock(&device->shader_slab_mutex);
325 list_add(&slab->slabs, &device->shader_slabs);
326
327 shader->bo = slab->bo;
328 shader->bo_offset = 0;
329 list_add(&shader->slab_list, &slab->shaders);
330 mtx_unlock(&device->shader_slab_mutex);
331 return slab->ptr;
332}
333
334void
335radv_destroy_shader_slabs(struct radv_device *device)
336{
337 list_for_each_entry_safe(struct radv_shader_slab, slab, &device->shader_slabs, slabs) {
338 device->ws->buffer_destroy(slab->bo);
339 free(slab);
340 }
341 mtx_destroy(&device->shader_slab_mutex);
342}
343
344static void
345radv_fill_shader_variant(struct radv_device *device,
346 struct radv_shader_variant *variant,
347 struct ac_shader_binary *binary,
348 gl_shader_stage stage)
349{
350 bool scratch_enabled = variant->config.scratch_bytes_per_wave > 0;
351 unsigned vgpr_comp_cnt = 0;
352
353 if (scratch_enabled && !device->llvm_supports_spill)
354 radv_finishme("shader scratch support only available with LLVM 4.0");
355
356 variant->code_size = binary->code_size;
357 variant->rsrc2 = S_00B12C_USER_SGPR(variant->info.num_user_sgprs) |
358 S_00B12C_SCRATCH_EN(scratch_enabled);
359
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200360 variant->rsrc1 = S_00B848_VGPRS((variant->config.num_vgprs - 1) / 4) |
361 S_00B848_SGPRS((variant->config.num_sgprs - 1) / 8) |
362 S_00B848_DX10_CLAMP(1) |
363 S_00B848_FLOAT_MODE(variant->config.float_mode);
364
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200365 switch (stage) {
366 case MESA_SHADER_TESS_EVAL:
367 vgpr_comp_cnt = 3;
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200368 variant->rsrc2 |= S_00B12C_OC_LDS_EN(1);
369 break;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200370 case MESA_SHADER_TESS_CTRL:
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200371 if (device->physical_device->rad_info.chip_class >= GFX9)
372 vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
373 else
374 variant->rsrc2 |= S_00B12C_OC_LDS_EN(1);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200375 break;
376 case MESA_SHADER_VERTEX:
377 case MESA_SHADER_GEOMETRY:
378 vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
379 break;
380 case MESA_SHADER_FRAGMENT:
381 break;
Samuel Pitoiset2294d352017-12-14 16:48:03 +0100382 case MESA_SHADER_COMPUTE: {
383 struct ac_shader_info *info = &variant->info.info;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200384 variant->rsrc2 |=
Samuel Pitoiset4237c3d2017-12-18 22:06:38 +0100385 S_00B84C_TGID_X_EN(info->cs.uses_block_id[0]) |
386 S_00B84C_TGID_Y_EN(info->cs.uses_block_id[1]) |
387 S_00B84C_TGID_Z_EN(info->cs.uses_block_id[2]) |
388 S_00B84C_TIDIG_COMP_CNT(info->cs.uses_thread_id[2] ? 2 :
389 info->cs.uses_thread_id[1] ? 1 : 0) |
Samuel Pitoiset90c3bf02017-12-14 17:32:41 +0100390 S_00B84C_TG_SIZE_EN(info->cs.uses_local_invocation_idx) |
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200391 S_00B84C_LDS_SIZE(variant->config.lds_size);
392 break;
Samuel Pitoiset2294d352017-12-14 16:48:03 +0100393 }
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200394 default:
395 unreachable("unsupported shader type");
396 break;
397 }
398
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200399 if (device->physical_device->rad_info.chip_class >= GFX9 &&
Bas Nieuwenhuizen73749ca2017-10-20 02:24:24 +0200400 stage == MESA_SHADER_GEOMETRY) {
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100401 struct ac_shader_info *info = &variant->info.info;
Samuel Pitoiset232c4182018-01-09 16:01:09 +0100402 unsigned es_type = variant->info.gs.es_type;
Samuel Pitoiset4e701cf2018-01-09 16:01:10 +0100403 unsigned gs_vgpr_comp_cnt, es_vgpr_comp_cnt;
404
405 if (es_type == MESA_SHADER_VERTEX) {
406 es_vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
407 } else if (es_type == MESA_SHADER_TESS_EVAL) {
408 es_vgpr_comp_cnt = 3;
409 } else {
Bas Nieuwenhuizen0f89f9b2018-01-17 23:23:02 +0100410 unreachable("invalid shader ES type");
Samuel Pitoiset4e701cf2018-01-09 16:01:10 +0100411 }
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100412
413 /* If offsets 4, 5 are used, GS_VGPR_COMP_CNT is ignored and
414 * VGPR[0:4] are always loaded.
415 */
416 if (info->uses_invocation_id)
417 gs_vgpr_comp_cnt = 3; /* VGPR3 contains InvocationID. */
418 else if (info->uses_prim_id)
419 gs_vgpr_comp_cnt = 2; /* VGPR2 contains PrimitiveID. */
Samuel Pitoisetb462ceb2018-01-05 17:18:52 +0100420 else if (variant->info.gs.vertices_in >= 3)
421 gs_vgpr_comp_cnt = 1; /* VGPR1 contains offsets 2, 3 */
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100422 else
Samuel Pitoisetb462ceb2018-01-05 17:18:52 +0100423 gs_vgpr_comp_cnt = 0; /* VGPR0 contains offsets 0, 1 */
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100424
Samuel Pitoiset2670ebb2017-12-20 20:56:57 +0100425 variant->rsrc1 |= S_00B228_GS_VGPR_COMP_CNT(gs_vgpr_comp_cnt);
Samuel Pitoiset4e701cf2018-01-09 16:01:10 +0100426 variant->rsrc2 |= S_00B22C_ES_VGPR_COMP_CNT(es_vgpr_comp_cnt) |
Samuel Pitoiset232c4182018-01-09 16:01:09 +0100427 S_00B22C_OC_LDS_EN(es_type == MESA_SHADER_TESS_EVAL);
Bas Nieuwenhuizen73749ca2017-10-20 02:24:24 +0200428 } else if (device->physical_device->rad_info.chip_class >= GFX9 &&
Bas Nieuwenhuizen228325f2017-10-18 00:59:16 +0200429 stage == MESA_SHADER_TESS_CTRL)
430 variant->rsrc1 |= S_00B428_LS_VGPR_COMP_CNT(vgpr_comp_cnt);
431 else
432 variant->rsrc1 |= S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200433
434 void *ptr = radv_alloc_shader_memory(device, variant);
435 memcpy(ptr, binary->code, binary->code_size);
436}
437
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200438static struct radv_shader_variant *
439shader_variant_create(struct radv_device *device,
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200440 struct radv_shader_module *module,
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200441 struct nir_shader * const *shaders,
442 int shader_count,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200443 gl_shader_stage stage,
444 struct ac_nir_compiler_options *options,
445 bool gs_copy_shader,
446 void **code_out,
447 unsigned *code_size_out)
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200448{
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200449 enum radeon_family chip_family = device->physical_device->rad_info.family;
Samuel Pitoiset921986b2017-11-30 22:16:09 +0100450 bool dump_shaders = radv_can_dump_shader(device, module);
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200451 enum ac_target_machine_options tm_options = 0;
452 struct radv_shader_variant *variant;
453 struct ac_shader_binary binary;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200454 LLVMTargetMachineRef tm;
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200455
456 variant = calloc(1, sizeof(struct radv_shader_variant));
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200457 if (!variant)
458 return NULL;
459
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200460 options->family = chip_family;
461 options->chip_class = device->physical_device->rad_info.chip_class;
Samuel Pitoiset33e6e5e2018-01-19 12:12:02 +0100462 options->dump_preoptir = radv_can_dump_shader(device, module) &&
463 device->instance->debug_flags & RADV_DEBUG_PREOPTIR;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200464
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200465 if (options->supports_spill)
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200466 tm_options |= AC_TM_SUPPORTS_SPILL;
467 if (device->instance->perftest_flags & RADV_PERFTEST_SISCHED)
468 tm_options |= AC_TM_SISCHED;
469 tm = ac_create_target_machine(chip_family, tm_options);
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200470
471 if (gs_copy_shader) {
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200472 assert(shader_count == 1);
473 ac_create_gs_copy_shader(tm, *shaders, &binary, &variant->config,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200474 &variant->info, options, dump_shaders);
475 } else {
476 ac_compile_nir_shader(tm, &binary, &variant->config,
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200477 &variant->info, shaders, shader_count, options,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200478 dump_shaders);
479 }
480
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200481 LLVMDisposeTargetMachine(tm);
482
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200483 radv_fill_shader_variant(device, variant, &binary, stage);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200484
485 if (code_out) {
486 *code_out = binary.code;
487 *code_size_out = binary.code_size;
488 } else
489 free(binary.code);
490 free(binary.config);
491 free(binary.rodata);
492 free(binary.global_symbol_offsets);
493 free(binary.relocs);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200494 variant->ref_count = 1;
Samuel Pitoiset885d7572017-09-01 13:45:33 +0200495
Alex Smithde889792017-10-27 14:25:05 +0100496 if (device->keep_shader_info) {
Samuel Pitoiset885d7572017-09-01 13:45:33 +0200497 variant->disasm_string = binary.disasm_string;
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200498 if (!gs_copy_shader && !module->nir) {
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200499 variant->nir = *shaders;
Samuel Pitoiset844ae722017-09-22 16:56:40 +0200500 variant->spirv = (uint32_t *)module->data;
501 variant->spirv_size = module->size;
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200502 }
Samuel Pitoiset885d7572017-09-01 13:45:33 +0200503 } else {
504 free(binary.disasm_string);
505 }
506
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200507 return variant;
508}
509
510struct radv_shader_variant *
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200511radv_shader_variant_create(struct radv_device *device,
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200512 struct radv_shader_module *module,
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200513 struct nir_shader *const *shaders,
514 int shader_count,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200515 struct radv_pipeline_layout *layout,
516 const struct ac_shader_variant_key *key,
517 void **code_out,
518 unsigned *code_size_out)
519{
520 struct ac_nir_compiler_options options = {0};
521
522 options.layout = layout;
523 if (key)
524 options.key = *key;
525
Timothy Arceri7664aaf2017-10-11 11:59:20 +1100526 options.unsafe_math = !!(device->instance->debug_flags & RADV_DEBUG_UNSAFE_MATH);
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200527 options.supports_spill = device->llvm_supports_spill;
528
Jason Ekstrand59fb59a2017-09-14 19:52:38 -0700529 return shader_variant_create(device, module, shaders, shader_count, shaders[shader_count - 1]->info.stage,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200530 &options, false, code_out, code_size_out);
531}
532
533struct radv_shader_variant *
534radv_create_gs_copy_shader(struct radv_device *device,
535 struct nir_shader *shader,
536 void **code_out,
537 unsigned *code_size_out,
Samuel Pitoiset47efc522017-09-01 12:09:56 +0200538 bool multiview)
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200539{
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200540 struct ac_nir_compiler_options options = {0};
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200541
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200542 options.key.has_multiview_view_index = multiview;
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200543
Bas Nieuwenhuizence03c112017-10-16 13:18:02 +0200544 return shader_variant_create(device, NULL, &shader, 1, MESA_SHADER_VERTEX,
Samuel Pitoiset92db23f2017-09-01 16:51:12 +0200545 &options, true, code_out, code_size_out);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200546}
547
548void
549radv_shader_variant_destroy(struct radv_device *device,
550 struct radv_shader_variant *variant)
551{
552 if (!p_atomic_dec_zero(&variant->ref_count))
553 return;
554
555 mtx_lock(&device->shader_slab_mutex);
556 list_del(&variant->slab_list);
557 mtx_unlock(&device->shader_slab_mutex);
558
Samuel Pitoiseta2a350a2017-09-22 16:44:08 +0200559 ralloc_free(variant->nir);
Samuel Pitoiset885d7572017-09-01 13:45:33 +0200560 free(variant->disasm_string);
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200561 free(variant);
562}
563
Samuel Pitoisetd4d77732017-09-01 11:41:18 +0200564const char *
565radv_get_shader_name(struct radv_shader_variant *var, gl_shader_stage stage)
566{
567 switch (stage) {
568 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";
569 case MESA_SHADER_GEOMETRY: return "Geometry Shader";
570 case MESA_SHADER_FRAGMENT: return "Pixel Shader";
571 case MESA_SHADER_COMPUTE: return "Compute Shader";
572 case MESA_SHADER_TESS_CTRL: return "Tessellation Control Shader";
573 case MESA_SHADER_TESS_EVAL: return var->info.tes.as_es ? "Tessellation Evaluation Shader as ES" : "Tessellation Evaluation Shader as VS";
574 default:
575 return "Unknown shader";
576 };
577}
578
Alex Smithde889792017-10-27 14:25:05 +0100579static uint32_t
580get_total_sgprs(struct radv_device *device)
581{
582 if (device->physical_device->rad_info.chip_class >= VI)
583 return 800;
584 else
585 return 512;
586}
587
588static void
589generate_shader_stats(struct radv_device *device,
590 struct radv_shader_variant *variant,
591 gl_shader_stage stage,
592 struct _mesa_string_buffer *buf)
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200593{
594 unsigned lds_increment = device->physical_device->rad_info.chip_class >= CIK ? 512 : 256;
595 struct ac_shader_config *conf;
596 unsigned max_simd_waves;
597 unsigned lds_per_wave = 0;
598
599 switch (device->physical_device->rad_info.family) {
600 /* These always have 8 waves: */
601 case CHIP_POLARIS10:
602 case CHIP_POLARIS11:
603 case CHIP_POLARIS12:
604 max_simd_waves = 8;
605 break;
606 default:
607 max_simd_waves = 10;
608 }
609
610 conf = &variant->config;
611
612 if (stage == MESA_SHADER_FRAGMENT) {
613 lds_per_wave = conf->lds_size * lds_increment +
614 align(variant->info.fs.num_interp * 48,
615 lds_increment);
616 }
617
Alex Smithde889792017-10-27 14:25:05 +0100618 if (conf->num_sgprs)
619 max_simd_waves = MIN2(max_simd_waves, get_total_sgprs(device) / conf->num_sgprs);
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200620
621 if (conf->num_vgprs)
622 max_simd_waves = MIN2(max_simd_waves, 256 / conf->num_vgprs);
623
624 /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
625 * that PS can use.
626 */
627 if (lds_per_wave)
628 max_simd_waves = MIN2(max_simd_waves, 16384 / lds_per_wave);
629
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200630 if (stage == MESA_SHADER_FRAGMENT) {
Alex Smithde889792017-10-27 14:25:05 +0100631 _mesa_string_buffer_printf(buf, "*** SHADER CONFIG ***\n"
632 "SPI_PS_INPUT_ADDR = 0x%04x\n"
633 "SPI_PS_INPUT_ENA = 0x%04x\n",
634 conf->spi_ps_input_addr, conf->spi_ps_input_ena);
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200635 }
636
Alex Smithde889792017-10-27 14:25:05 +0100637 _mesa_string_buffer_printf(buf, "*** SHADER STATS ***\n"
638 "SGPRS: %d\n"
639 "VGPRS: %d\n"
640 "Spilled SGPRs: %d\n"
641 "Spilled VGPRs: %d\n"
Samuel Pitoisete96e6f62018-03-01 22:12:56 +0100642 "PrivMem VGPRS: %d\n"
Alex Smithde889792017-10-27 14:25:05 +0100643 "Code Size: %d bytes\n"
644 "LDS: %d blocks\n"
645 "Scratch: %d bytes per wave\n"
646 "Max Waves: %d\n"
647 "********************\n\n\n",
648 conf->num_sgprs, conf->num_vgprs,
Samuel Pitoisete96e6f62018-03-01 22:12:56 +0100649 conf->spilled_sgprs, conf->spilled_vgprs,
650 variant->info.private_mem_vgprs, variant->code_size,
Alex Smithde889792017-10-27 14:25:05 +0100651 conf->lds_size, conf->scratch_bytes_per_wave,
652 max_simd_waves);
653}
654
655void
656radv_shader_dump_stats(struct radv_device *device,
657 struct radv_shader_variant *variant,
658 gl_shader_stage stage,
659 FILE *file)
660{
661 struct _mesa_string_buffer *buf = _mesa_string_buffer_create(NULL, 256);
662
663 generate_shader_stats(device, variant, stage, buf);
664
665 fprintf(file, "\n%s:\n", radv_get_shader_name(variant, stage));
Alex Smith134a40d2017-10-30 08:38:14 +0000666 fprintf(file, "%s", buf->buf);
Alex Smithde889792017-10-27 14:25:05 +0100667
668 _mesa_string_buffer_destroy(buf);
669}
670
671VkResult
672radv_GetShaderInfoAMD(VkDevice _device,
673 VkPipeline _pipeline,
674 VkShaderStageFlagBits shaderStage,
675 VkShaderInfoTypeAMD infoType,
676 size_t* pInfoSize,
677 void* pInfo)
678{
679 RADV_FROM_HANDLE(radv_device, device, _device);
680 RADV_FROM_HANDLE(radv_pipeline, pipeline, _pipeline);
681 gl_shader_stage stage = vk_to_mesa_shader_stage(shaderStage);
682 struct radv_shader_variant *variant = pipeline->shaders[stage];
683 struct _mesa_string_buffer *buf;
684 VkResult result = VK_SUCCESS;
685
686 /* Spec doesn't indicate what to do if the stage is invalid, so just
687 * return no info for this. */
688 if (!variant)
Samuel Pitoisetcd64a4f2017-11-10 09:17:58 +0100689 return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
Alex Smithde889792017-10-27 14:25:05 +0100690
691 switch (infoType) {
692 case VK_SHADER_INFO_TYPE_STATISTICS_AMD:
693 if (!pInfo) {
694 *pInfoSize = sizeof(VkShaderStatisticsInfoAMD);
695 } else {
696 unsigned lds_multiplier = device->physical_device->rad_info.chip_class >= CIK ? 512 : 256;
697 struct ac_shader_config *conf = &variant->config;
698
699 VkShaderStatisticsInfoAMD statistics = {};
700 statistics.shaderStageMask = shaderStage;
701 statistics.numPhysicalVgprs = 256;
702 statistics.numPhysicalSgprs = get_total_sgprs(device);
703 statistics.numAvailableSgprs = statistics.numPhysicalSgprs;
704
705 if (stage == MESA_SHADER_COMPUTE) {
706 unsigned *local_size = variant->nir->info.cs.local_size;
707 unsigned workgroup_size = local_size[0] * local_size[1] * local_size[2];
708
709 statistics.numAvailableVgprs = statistics.numPhysicalVgprs /
710 ceil(workgroup_size / statistics.numPhysicalVgprs);
711
712 statistics.computeWorkGroupSize[0] = local_size[0];
713 statistics.computeWorkGroupSize[1] = local_size[1];
714 statistics.computeWorkGroupSize[2] = local_size[2];
715 } else {
716 statistics.numAvailableVgprs = statistics.numPhysicalVgprs;
717 }
718
719 statistics.resourceUsage.numUsedVgprs = conf->num_vgprs;
720 statistics.resourceUsage.numUsedSgprs = conf->num_sgprs;
721 statistics.resourceUsage.ldsSizePerLocalWorkGroup = 32768;
722 statistics.resourceUsage.ldsUsageSizeInBytes = conf->lds_size * lds_multiplier;
723 statistics.resourceUsage.scratchMemUsageInBytes = conf->scratch_bytes_per_wave;
724
725 size_t size = *pInfoSize;
726 *pInfoSize = sizeof(statistics);
727
728 memcpy(pInfo, &statistics, MIN2(size, *pInfoSize));
729
730 if (size < *pInfoSize)
731 result = VK_INCOMPLETE;
732 }
733
734 break;
735 case VK_SHADER_INFO_TYPE_DISASSEMBLY_AMD:
736 buf = _mesa_string_buffer_create(NULL, 1024);
737
738 _mesa_string_buffer_printf(buf, "%s:\n", radv_get_shader_name(variant, stage));
739 _mesa_string_buffer_printf(buf, "%s\n\n", variant->disasm_string);
740 generate_shader_stats(device, variant, stage, buf);
741
742 /* Need to include the null terminator. */
743 size_t length = buf->length + 1;
744
745 if (!pInfo) {
746 *pInfoSize = length;
747 } else {
748 size_t size = *pInfoSize;
749 *pInfoSize = length;
750
751 memcpy(pInfo, buf->buf, MIN2(size, length));
752
753 if (size < length)
754 result = VK_INCOMPLETE;
755 }
756
757 _mesa_string_buffer_destroy(buf);
758 break;
759 default:
760 /* VK_SHADER_INFO_TYPE_BINARY_AMD unimplemented for now. */
761 result = VK_ERROR_FEATURE_NOT_PRESENT;
762 break;
763 }
764
765 return result;
Samuel Pitoiset80b8d9f2017-09-05 15:34:07 +0200766}