Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

radv_shader.c 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  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. #include "util/mesa-sha1.h"
  28. #include "util/u_atomic.h"
  29. #include "radv_debug.h"
  30. #include "radv_private.h"
  31. #include "radv_shader.h"
  32. #include "nir/nir.h"
  33. #include "nir/nir_builder.h"
  34. #include "spirv/nir_spirv.h"
  35. #include <llvm-c/Core.h>
  36. #include <llvm-c/TargetMachine.h>
  37. #include "sid.h"
  38. #include "gfx9d.h"
  39. #include "ac_binary.h"
  40. #include "ac_llvm_util.h"
  41. #include "ac_nir_to_llvm.h"
  42. #include "vk_format.h"
  43. #include "util/debug.h"
  44. #include "ac_exp_param.h"
  45. #include "util/string_buffer.h"
  46. static const struct nir_shader_compiler_options nir_options = {
  47. .vertex_id_zero_based = true,
  48. .lower_scmp = true,
  49. .lower_flrp32 = true,
  50. .lower_flrp64 = true,
  51. .lower_device_index_to_zero = true,
  52. .lower_fsat = true,
  53. .lower_fdiv = true,
  54. .lower_sub = true,
  55. .lower_pack_snorm_2x16 = true,
  56. .lower_pack_snorm_4x8 = true,
  57. .lower_pack_unorm_2x16 = true,
  58. .lower_pack_unorm_4x8 = true,
  59. .lower_unpack_snorm_2x16 = true,
  60. .lower_unpack_snorm_4x8 = true,
  61. .lower_unpack_unorm_2x16 = true,
  62. .lower_unpack_unorm_4x8 = true,
  63. .lower_extract_byte = true,
  64. .lower_extract_word = true,
  65. .lower_ffma = true,
  66. .lower_fpow = true,
  67. .vs_inputs_dual_locations = true,
  68. .max_unroll_iterations = 32
  69. };
  70. VkResult radv_CreateShaderModule(
  71. VkDevice _device,
  72. const VkShaderModuleCreateInfo* pCreateInfo,
  73. const VkAllocationCallbacks* pAllocator,
  74. VkShaderModule* pShaderModule)
  75. {
  76. RADV_FROM_HANDLE(radv_device, device, _device);
  77. struct radv_shader_module *module;
  78. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
  79. assert(pCreateInfo->flags == 0);
  80. module = vk_alloc2(&device->alloc, pAllocator,
  81. sizeof(*module) + pCreateInfo->codeSize, 8,
  82. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  83. if (module == NULL)
  84. return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
  85. module->nir = NULL;
  86. module->size = pCreateInfo->codeSize;
  87. memcpy(module->data, pCreateInfo->pCode, module->size);
  88. _mesa_sha1_compute(module->data, module->size, module->sha1);
  89. *pShaderModule = radv_shader_module_to_handle(module);
  90. return VK_SUCCESS;
  91. }
  92. void radv_DestroyShaderModule(
  93. VkDevice _device,
  94. VkShaderModule _module,
  95. const VkAllocationCallbacks* pAllocator)
  96. {
  97. RADV_FROM_HANDLE(radv_device, device, _device);
  98. RADV_FROM_HANDLE(radv_shader_module, module, _module);
  99. if (!module)
  100. return;
  101. vk_free2(&device->alloc, pAllocator, module);
  102. }
  103. void
  104. radv_optimize_nir(struct nir_shader *shader)
  105. {
  106. bool progress;
  107. do {
  108. progress = false;
  109. NIR_PASS_V(shader, nir_lower_vars_to_ssa);
  110. NIR_PASS_V(shader, nir_lower_64bit_pack);
  111. NIR_PASS_V(shader, nir_lower_alu_to_scalar);
  112. NIR_PASS_V(shader, nir_lower_phis_to_scalar);
  113. NIR_PASS(progress, shader, nir_copy_prop);
  114. NIR_PASS(progress, shader, nir_opt_remove_phis);
  115. NIR_PASS(progress, shader, nir_opt_dce);
  116. if (nir_opt_trivial_continues(shader)) {
  117. progress = true;
  118. NIR_PASS(progress, shader, nir_copy_prop);
  119. NIR_PASS(progress, shader, nir_opt_remove_phis);
  120. NIR_PASS(progress, shader, nir_opt_dce);
  121. }
  122. NIR_PASS(progress, shader, nir_opt_if);
  123. NIR_PASS(progress, shader, nir_opt_dead_cf);
  124. NIR_PASS(progress, shader, nir_opt_cse);
  125. NIR_PASS(progress, shader, nir_opt_peephole_select, 8);
  126. NIR_PASS(progress, shader, nir_opt_algebraic);
  127. NIR_PASS(progress, shader, nir_opt_constant_folding);
  128. NIR_PASS(progress, shader, nir_opt_undef);
  129. NIR_PASS(progress, shader, nir_opt_conditional_discard);
  130. if (shader->options->max_unroll_iterations) {
  131. NIR_PASS(progress, shader, nir_opt_loop_unroll, 0);
  132. }
  133. } while (progress);
  134. NIR_PASS(progress, shader, nir_opt_shrink_load);
  135. NIR_PASS(progress, shader, nir_opt_move_load_ubo);
  136. }
  137. nir_shader *
  138. radv_shader_compile_to_nir(struct radv_device *device,
  139. struct radv_shader_module *module,
  140. const char *entrypoint_name,
  141. gl_shader_stage stage,
  142. const VkSpecializationInfo *spec_info)
  143. {
  144. if (strcmp(entrypoint_name, "main") != 0) {
  145. radv_finishme("Multiple shaders per module not really supported");
  146. }
  147. nir_shader *nir;
  148. nir_function *entry_point;
  149. if (module->nir) {
  150. /* Some things such as our meta clear/blit code will give us a NIR
  151. * shader directly. In that case, we just ignore the SPIR-V entirely
  152. * and just use the NIR shader */
  153. nir = module->nir;
  154. nir->options = &nir_options;
  155. nir_validate_shader(nir);
  156. assert(exec_list_length(&nir->functions) == 1);
  157. struct exec_node *node = exec_list_get_head(&nir->functions);
  158. entry_point = exec_node_data(nir_function, node, node);
  159. } else {
  160. uint32_t *spirv = (uint32_t *) module->data;
  161. assert(module->size % 4 == 0);
  162. if (device->instance->debug_flags & RADV_DEBUG_DUMP_SPIRV)
  163. radv_print_spirv(spirv, module->size, stderr);
  164. uint32_t num_spec_entries = 0;
  165. struct nir_spirv_specialization *spec_entries = NULL;
  166. if (spec_info && spec_info->mapEntryCount > 0) {
  167. num_spec_entries = spec_info->mapEntryCount;
  168. spec_entries = malloc(num_spec_entries * sizeof(*spec_entries));
  169. for (uint32_t i = 0; i < num_spec_entries; i++) {
  170. VkSpecializationMapEntry entry = spec_info->pMapEntries[i];
  171. const void *data = spec_info->pData + entry.offset;
  172. assert(data + entry.size <= spec_info->pData + spec_info->dataSize);
  173. spec_entries[i].id = spec_info->pMapEntries[i].constantID;
  174. if (spec_info->dataSize == 8)
  175. spec_entries[i].data64 = *(const uint64_t *)data;
  176. else
  177. spec_entries[i].data32 = *(const uint32_t *)data;
  178. }
  179. }
  180. const struct spirv_to_nir_options spirv_options = {
  181. .caps = {
  182. .device_group = true,
  183. .draw_parameters = true,
  184. .float64 = true,
  185. .image_read_without_format = true,
  186. .image_write_without_format = true,
  187. .tessellation = true,
  188. .int64 = true,
  189. .multiview = true,
  190. .subgroup_basic = true,
  191. .variable_pointers = true,
  192. .gcn_shader = true,
  193. .trinary_minmax = true,
  194. .shader_viewport_index_layer = true,
  195. },
  196. };
  197. entry_point = spirv_to_nir(spirv, module->size / 4,
  198. spec_entries, num_spec_entries,
  199. stage, entrypoint_name,
  200. &spirv_options, &nir_options);
  201. nir = entry_point->shader;
  202. assert(nir->info.stage == stage);
  203. nir_validate_shader(nir);
  204. free(spec_entries);
  205. /* We have to lower away local constant initializers right before we
  206. * inline functions. That way they get properly initialized at the top
  207. * of the function and not at the top of its caller.
  208. */
  209. NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_local);
  210. NIR_PASS_V(nir, nir_lower_returns);
  211. NIR_PASS_V(nir, nir_inline_functions);
  212. /* Pick off the single entrypoint that we want */
  213. foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
  214. if (func != entry_point)
  215. exec_node_remove(&func->node);
  216. }
  217. assert(exec_list_length(&nir->functions) == 1);
  218. entry_point->name = ralloc_strdup(entry_point, "main");
  219. /* Make sure we lower constant initializers on output variables so that
  220. * nir_remove_dead_variables below sees the corresponding stores
  221. */
  222. NIR_PASS_V(nir, nir_lower_constant_initializers, nir_var_shader_out);
  223. NIR_PASS_V(nir, nir_remove_dead_variables,
  224. nir_var_shader_in | nir_var_shader_out | nir_var_system_value);
  225. /* Now that we've deleted all but the main function, we can go ahead and
  226. * lower the rest of the constant initializers.
  227. */
  228. NIR_PASS_V(nir, nir_lower_constant_initializers, ~0);
  229. NIR_PASS_V(nir, nir_lower_system_values);
  230. NIR_PASS_V(nir, nir_lower_clip_cull_distance_arrays);
  231. }
  232. /* Vulkan uses the separate-shader linking model */
  233. nir->info.separate_shader = true;
  234. nir_shader_gather_info(nir, entry_point->impl);
  235. static const nir_lower_tex_options tex_options = {
  236. .lower_txp = ~0,
  237. };
  238. nir_lower_tex(nir, &tex_options);
  239. nir_lower_vars_to_ssa(nir);
  240. nir_lower_var_copies(nir);
  241. nir_lower_global_vars_to_local(nir);
  242. nir_remove_dead_variables(nir, nir_var_local);
  243. nir_lower_subgroups(nir, &(struct nir_lower_subgroups_options) {
  244. .subgroup_size = 64,
  245. .ballot_bit_size = 64,
  246. .lower_to_scalar = 1,
  247. .lower_subgroup_masks = 1,
  248. .lower_shuffle = 1,
  249. .lower_quad = 1,
  250. });
  251. radv_optimize_nir(nir);
  252. /* Indirect lowering must be called after the radv_optimize_nir() loop
  253. * has been called at least once. Otherwise indirect lowering can
  254. * bloat the instruction count of the loop and cause it to be
  255. * considered too large for unrolling.
  256. */
  257. ac_lower_indirect_derefs(nir, device->physical_device->rad_info.chip_class);
  258. radv_optimize_nir(nir);
  259. return nir;
  260. }
  261. void *
  262. radv_alloc_shader_memory(struct radv_device *device,
  263. struct radv_shader_variant *shader)
  264. {
  265. mtx_lock(&device->shader_slab_mutex);
  266. list_for_each_entry(struct radv_shader_slab, slab, &device->shader_slabs, slabs) {
  267. uint64_t offset = 0;
  268. list_for_each_entry(struct radv_shader_variant, s, &slab->shaders, slab_list) {
  269. if (s->bo_offset - offset >= shader->code_size) {
  270. shader->bo = slab->bo;
  271. shader->bo_offset = offset;
  272. list_addtail(&shader->slab_list, &s->slab_list);
  273. mtx_unlock(&device->shader_slab_mutex);
  274. return slab->ptr + offset;
  275. }
  276. offset = align_u64(s->bo_offset + s->code_size, 256);
  277. }
  278. if (slab->size - offset >= shader->code_size) {
  279. shader->bo = slab->bo;
  280. shader->bo_offset = offset;
  281. list_addtail(&shader->slab_list, &slab->shaders);
  282. mtx_unlock(&device->shader_slab_mutex);
  283. return slab->ptr + offset;
  284. }
  285. }
  286. mtx_unlock(&device->shader_slab_mutex);
  287. struct radv_shader_slab *slab = calloc(1, sizeof(struct radv_shader_slab));
  288. slab->size = 256 * 1024;
  289. slab->bo = device->ws->buffer_create(device->ws, slab->size, 256,
  290. RADEON_DOMAIN_VRAM,
  291. RADEON_FLAG_NO_INTERPROCESS_SHARING |
  292. device->physical_device->cpdma_prefetch_writes_memory ?
  293. 0 : RADEON_FLAG_READ_ONLY);
  294. slab->ptr = (char*)device->ws->buffer_map(slab->bo);
  295. list_inithead(&slab->shaders);
  296. mtx_lock(&device->shader_slab_mutex);
  297. list_add(&slab->slabs, &device->shader_slabs);
  298. shader->bo = slab->bo;
  299. shader->bo_offset = 0;
  300. list_add(&shader->slab_list, &slab->shaders);
  301. mtx_unlock(&device->shader_slab_mutex);
  302. return slab->ptr;
  303. }
  304. void
  305. radv_destroy_shader_slabs(struct radv_device *device)
  306. {
  307. list_for_each_entry_safe(struct radv_shader_slab, slab, &device->shader_slabs, slabs) {
  308. device->ws->buffer_destroy(slab->bo);
  309. free(slab);
  310. }
  311. mtx_destroy(&device->shader_slab_mutex);
  312. }
  313. static void
  314. radv_fill_shader_variant(struct radv_device *device,
  315. struct radv_shader_variant *variant,
  316. struct ac_shader_binary *binary,
  317. gl_shader_stage stage)
  318. {
  319. bool scratch_enabled = variant->config.scratch_bytes_per_wave > 0;
  320. unsigned vgpr_comp_cnt = 0;
  321. if (scratch_enabled && !device->llvm_supports_spill)
  322. radv_finishme("shader scratch support only available with LLVM 4.0");
  323. variant->code_size = binary->code_size;
  324. variant->rsrc2 = S_00B12C_USER_SGPR(variant->info.num_user_sgprs) |
  325. S_00B12C_SCRATCH_EN(scratch_enabled);
  326. variant->rsrc1 = S_00B848_VGPRS((variant->config.num_vgprs - 1) / 4) |
  327. S_00B848_SGPRS((variant->config.num_sgprs - 1) / 8) |
  328. S_00B848_DX10_CLAMP(1) |
  329. S_00B848_FLOAT_MODE(variant->config.float_mode);
  330. switch (stage) {
  331. case MESA_SHADER_TESS_EVAL:
  332. vgpr_comp_cnt = 3;
  333. variant->rsrc2 |= S_00B12C_OC_LDS_EN(1);
  334. break;
  335. case MESA_SHADER_TESS_CTRL:
  336. if (device->physical_device->rad_info.chip_class >= GFX9)
  337. vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
  338. else
  339. variant->rsrc2 |= S_00B12C_OC_LDS_EN(1);
  340. break;
  341. case MESA_SHADER_VERTEX:
  342. case MESA_SHADER_GEOMETRY:
  343. vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
  344. break;
  345. case MESA_SHADER_FRAGMENT:
  346. break;
  347. case MESA_SHADER_COMPUTE: {
  348. struct radv_shader_info *info = &variant->info.info;
  349. variant->rsrc2 |=
  350. S_00B84C_TGID_X_EN(info->cs.uses_block_id[0]) |
  351. S_00B84C_TGID_Y_EN(info->cs.uses_block_id[1]) |
  352. S_00B84C_TGID_Z_EN(info->cs.uses_block_id[2]) |
  353. S_00B84C_TIDIG_COMP_CNT(info->cs.uses_thread_id[2] ? 2 :
  354. info->cs.uses_thread_id[1] ? 1 : 0) |
  355. S_00B84C_TG_SIZE_EN(info->cs.uses_local_invocation_idx) |
  356. S_00B84C_LDS_SIZE(variant->config.lds_size);
  357. break;
  358. }
  359. default:
  360. unreachable("unsupported shader type");
  361. break;
  362. }
  363. if (device->physical_device->rad_info.chip_class >= GFX9 &&
  364. stage == MESA_SHADER_GEOMETRY) {
  365. struct radv_shader_info *info = &variant->info.info;
  366. unsigned es_type = variant->info.gs.es_type;
  367. unsigned gs_vgpr_comp_cnt, es_vgpr_comp_cnt;
  368. if (es_type == MESA_SHADER_VERTEX) {
  369. es_vgpr_comp_cnt = variant->info.vs.vgpr_comp_cnt;
  370. } else if (es_type == MESA_SHADER_TESS_EVAL) {
  371. es_vgpr_comp_cnt = 3;
  372. } else {
  373. unreachable("invalid shader ES type");
  374. }
  375. /* If offsets 4, 5 are used, GS_VGPR_COMP_CNT is ignored and
  376. * VGPR[0:4] are always loaded.
  377. */
  378. if (info->uses_invocation_id)
  379. gs_vgpr_comp_cnt = 3; /* VGPR3 contains InvocationID. */
  380. else if (info->uses_prim_id)
  381. gs_vgpr_comp_cnt = 2; /* VGPR2 contains PrimitiveID. */
  382. else if (variant->info.gs.vertices_in >= 3)
  383. gs_vgpr_comp_cnt = 1; /* VGPR1 contains offsets 2, 3 */
  384. else
  385. gs_vgpr_comp_cnt = 0; /* VGPR0 contains offsets 0, 1 */
  386. variant->rsrc1 |= S_00B228_GS_VGPR_COMP_CNT(gs_vgpr_comp_cnt);
  387. variant->rsrc2 |= S_00B22C_ES_VGPR_COMP_CNT(es_vgpr_comp_cnt) |
  388. S_00B22C_OC_LDS_EN(es_type == MESA_SHADER_TESS_EVAL);
  389. } else if (device->physical_device->rad_info.chip_class >= GFX9 &&
  390. stage == MESA_SHADER_TESS_CTRL)
  391. variant->rsrc1 |= S_00B428_LS_VGPR_COMP_CNT(vgpr_comp_cnt);
  392. else
  393. variant->rsrc1 |= S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt);
  394. void *ptr = radv_alloc_shader_memory(device, variant);
  395. memcpy(ptr, binary->code, binary->code_size);
  396. }
  397. static struct radv_shader_variant *
  398. shader_variant_create(struct radv_device *device,
  399. struct radv_shader_module *module,
  400. struct nir_shader * const *shaders,
  401. int shader_count,
  402. gl_shader_stage stage,
  403. struct radv_nir_compiler_options *options,
  404. bool gs_copy_shader,
  405. void **code_out,
  406. unsigned *code_size_out)
  407. {
  408. enum radeon_family chip_family = device->physical_device->rad_info.family;
  409. enum ac_target_machine_options tm_options = 0;
  410. struct radv_shader_variant *variant;
  411. struct ac_shader_binary binary;
  412. LLVMTargetMachineRef tm;
  413. variant = calloc(1, sizeof(struct radv_shader_variant));
  414. if (!variant)
  415. return NULL;
  416. options->family = chip_family;
  417. options->chip_class = device->physical_device->rad_info.chip_class;
  418. options->dump_shader = radv_can_dump_shader(device, module);
  419. options->dump_preoptir = options->dump_shader &&
  420. device->instance->debug_flags & RADV_DEBUG_PREOPTIR;
  421. options->record_llvm_ir = device->keep_shader_info;
  422. options->tess_offchip_block_dw_size = device->tess_offchip_block_dw_size;
  423. if (options->supports_spill)
  424. tm_options |= AC_TM_SUPPORTS_SPILL;
  425. if (device->instance->perftest_flags & RADV_PERFTEST_SISCHED)
  426. tm_options |= AC_TM_SISCHED;
  427. tm = ac_create_target_machine(chip_family, tm_options);
  428. if (gs_copy_shader) {
  429. assert(shader_count == 1);
  430. radv_compile_gs_copy_shader(tm, *shaders, &binary,
  431. &variant->config, &variant->info,
  432. options);
  433. } else {
  434. radv_compile_nir_shader(tm, &binary, &variant->config,
  435. &variant->info, shaders, shader_count,
  436. options);
  437. }
  438. LLVMDisposeTargetMachine(tm);
  439. radv_fill_shader_variant(device, variant, &binary, stage);
  440. if (code_out) {
  441. *code_out = binary.code;
  442. *code_size_out = binary.code_size;
  443. } else
  444. free(binary.code);
  445. free(binary.config);
  446. free(binary.rodata);
  447. free(binary.global_symbol_offsets);
  448. free(binary.relocs);
  449. variant->ref_count = 1;
  450. if (device->keep_shader_info) {
  451. variant->disasm_string = binary.disasm_string;
  452. variant->llvm_ir_string = binary.llvm_ir_string;
  453. if (!gs_copy_shader && !module->nir) {
  454. variant->nir = *shaders;
  455. variant->spirv = (uint32_t *)module->data;
  456. variant->spirv_size = module->size;
  457. }
  458. } else {
  459. free(binary.disasm_string);
  460. }
  461. return variant;
  462. }
  463. struct radv_shader_variant *
  464. radv_shader_variant_create(struct radv_device *device,
  465. struct radv_shader_module *module,
  466. struct nir_shader *const *shaders,
  467. int shader_count,
  468. struct radv_pipeline_layout *layout,
  469. const struct radv_shader_variant_key *key,
  470. void **code_out,
  471. unsigned *code_size_out)
  472. {
  473. struct radv_nir_compiler_options options = {0};
  474. options.layout = layout;
  475. if (key)
  476. options.key = *key;
  477. options.unsafe_math = !!(device->instance->debug_flags & RADV_DEBUG_UNSAFE_MATH);
  478. options.supports_spill = device->llvm_supports_spill;
  479. return shader_variant_create(device, module, shaders, shader_count, shaders[shader_count - 1]->info.stage,
  480. &options, false, code_out, code_size_out);
  481. }
  482. struct radv_shader_variant *
  483. radv_create_gs_copy_shader(struct radv_device *device,
  484. struct nir_shader *shader,
  485. void **code_out,
  486. unsigned *code_size_out,
  487. bool multiview)
  488. {
  489. struct radv_nir_compiler_options options = {0};
  490. options.key.has_multiview_view_index = multiview;
  491. return shader_variant_create(device, NULL, &shader, 1, MESA_SHADER_VERTEX,
  492. &options, true, code_out, code_size_out);
  493. }
  494. void
  495. radv_shader_variant_destroy(struct radv_device *device,
  496. struct radv_shader_variant *variant)
  497. {
  498. if (!p_atomic_dec_zero(&variant->ref_count))
  499. return;
  500. mtx_lock(&device->shader_slab_mutex);
  501. list_del(&variant->slab_list);
  502. mtx_unlock(&device->shader_slab_mutex);
  503. ralloc_free(variant->nir);
  504. free(variant->disasm_string);
  505. free(variant->llvm_ir_string);
  506. free(variant);
  507. }
  508. const char *
  509. radv_get_shader_name(struct radv_shader_variant *var, gl_shader_stage stage)
  510. {
  511. switch (stage) {
  512. 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";
  513. case MESA_SHADER_GEOMETRY: return "Geometry Shader";
  514. case MESA_SHADER_FRAGMENT: return "Pixel Shader";
  515. case MESA_SHADER_COMPUTE: return "Compute Shader";
  516. case MESA_SHADER_TESS_CTRL: return "Tessellation Control Shader";
  517. case MESA_SHADER_TESS_EVAL: return var->info.tes.as_es ? "Tessellation Evaluation Shader as ES" : "Tessellation Evaluation Shader as VS";
  518. default:
  519. return "Unknown shader";
  520. };
  521. }
  522. static void
  523. generate_shader_stats(struct radv_device *device,
  524. struct radv_shader_variant *variant,
  525. gl_shader_stage stage,
  526. struct _mesa_string_buffer *buf)
  527. {
  528. unsigned lds_increment = device->physical_device->rad_info.chip_class >= CIK ? 512 : 256;
  529. struct ac_shader_config *conf;
  530. unsigned max_simd_waves;
  531. unsigned lds_per_wave = 0;
  532. switch (device->physical_device->rad_info.family) {
  533. /* These always have 8 waves: */
  534. case CHIP_POLARIS10:
  535. case CHIP_POLARIS11:
  536. case CHIP_POLARIS12:
  537. max_simd_waves = 8;
  538. break;
  539. default:
  540. max_simd_waves = 10;
  541. }
  542. conf = &variant->config;
  543. if (stage == MESA_SHADER_FRAGMENT) {
  544. lds_per_wave = conf->lds_size * lds_increment +
  545. align(variant->info.fs.num_interp * 48,
  546. lds_increment);
  547. }
  548. if (conf->num_sgprs)
  549. max_simd_waves =
  550. MIN2(max_simd_waves,
  551. radv_get_num_physical_sgprs(device->physical_device) / conf->num_sgprs);
  552. if (conf->num_vgprs)
  553. max_simd_waves =
  554. MIN2(max_simd_waves,
  555. RADV_NUM_PHYSICAL_VGPRS / conf->num_vgprs);
  556. /* LDS is 64KB per CU (4 SIMDs), divided into 16KB blocks per SIMD
  557. * that PS can use.
  558. */
  559. if (lds_per_wave)
  560. max_simd_waves = MIN2(max_simd_waves, 16384 / lds_per_wave);
  561. if (stage == MESA_SHADER_FRAGMENT) {
  562. _mesa_string_buffer_printf(buf, "*** SHADER CONFIG ***\n"
  563. "SPI_PS_INPUT_ADDR = 0x%04x\n"
  564. "SPI_PS_INPUT_ENA = 0x%04x\n",
  565. conf->spi_ps_input_addr, conf->spi_ps_input_ena);
  566. }
  567. _mesa_string_buffer_printf(buf, "*** SHADER STATS ***\n"
  568. "SGPRS: %d\n"
  569. "VGPRS: %d\n"
  570. "Spilled SGPRs: %d\n"
  571. "Spilled VGPRs: %d\n"
  572. "PrivMem VGPRS: %d\n"
  573. "Code Size: %d bytes\n"
  574. "LDS: %d blocks\n"
  575. "Scratch: %d bytes per wave\n"
  576. "Max Waves: %d\n"
  577. "********************\n\n\n",
  578. conf->num_sgprs, conf->num_vgprs,
  579. conf->spilled_sgprs, conf->spilled_vgprs,
  580. variant->info.private_mem_vgprs, variant->code_size,
  581. conf->lds_size, conf->scratch_bytes_per_wave,
  582. max_simd_waves);
  583. }
  584. void
  585. radv_shader_dump_stats(struct radv_device *device,
  586. struct radv_shader_variant *variant,
  587. gl_shader_stage stage,
  588. FILE *file)
  589. {
  590. struct _mesa_string_buffer *buf = _mesa_string_buffer_create(NULL, 256);
  591. generate_shader_stats(device, variant, stage, buf);
  592. fprintf(file, "\n%s:\n", radv_get_shader_name(variant, stage));
  593. fprintf(file, "%s", buf->buf);
  594. _mesa_string_buffer_destroy(buf);
  595. }
  596. VkResult
  597. radv_GetShaderInfoAMD(VkDevice _device,
  598. VkPipeline _pipeline,
  599. VkShaderStageFlagBits shaderStage,
  600. VkShaderInfoTypeAMD infoType,
  601. size_t* pInfoSize,
  602. void* pInfo)
  603. {
  604. RADV_FROM_HANDLE(radv_device, device, _device);
  605. RADV_FROM_HANDLE(radv_pipeline, pipeline, _pipeline);
  606. gl_shader_stage stage = vk_to_mesa_shader_stage(shaderStage);
  607. struct radv_shader_variant *variant = pipeline->shaders[stage];
  608. struct _mesa_string_buffer *buf;
  609. VkResult result = VK_SUCCESS;
  610. /* Spec doesn't indicate what to do if the stage is invalid, so just
  611. * return no info for this. */
  612. if (!variant)
  613. return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
  614. switch (infoType) {
  615. case VK_SHADER_INFO_TYPE_STATISTICS_AMD:
  616. if (!pInfo) {
  617. *pInfoSize = sizeof(VkShaderStatisticsInfoAMD);
  618. } else {
  619. unsigned lds_multiplier = device->physical_device->rad_info.chip_class >= CIK ? 512 : 256;
  620. struct ac_shader_config *conf = &variant->config;
  621. VkShaderStatisticsInfoAMD statistics = {};
  622. statistics.shaderStageMask = shaderStage;
  623. statistics.numPhysicalVgprs = RADV_NUM_PHYSICAL_VGPRS;
  624. statistics.numPhysicalSgprs = radv_get_num_physical_sgprs(device->physical_device);
  625. statistics.numAvailableSgprs = statistics.numPhysicalSgprs;
  626. if (stage == MESA_SHADER_COMPUTE) {
  627. unsigned *local_size = variant->nir->info.cs.local_size;
  628. unsigned workgroup_size = local_size[0] * local_size[1] * local_size[2];
  629. statistics.numAvailableVgprs = statistics.numPhysicalVgprs /
  630. ceil(workgroup_size / statistics.numPhysicalVgprs);
  631. statistics.computeWorkGroupSize[0] = local_size[0];
  632. statistics.computeWorkGroupSize[1] = local_size[1];
  633. statistics.computeWorkGroupSize[2] = local_size[2];
  634. } else {
  635. statistics.numAvailableVgprs = statistics.numPhysicalVgprs;
  636. }
  637. statistics.resourceUsage.numUsedVgprs = conf->num_vgprs;
  638. statistics.resourceUsage.numUsedSgprs = conf->num_sgprs;
  639. statistics.resourceUsage.ldsSizePerLocalWorkGroup = 32768;
  640. statistics.resourceUsage.ldsUsageSizeInBytes = conf->lds_size * lds_multiplier;
  641. statistics.resourceUsage.scratchMemUsageInBytes = conf->scratch_bytes_per_wave;
  642. size_t size = *pInfoSize;
  643. *pInfoSize = sizeof(statistics);
  644. memcpy(pInfo, &statistics, MIN2(size, *pInfoSize));
  645. if (size < *pInfoSize)
  646. result = VK_INCOMPLETE;
  647. }
  648. break;
  649. case VK_SHADER_INFO_TYPE_DISASSEMBLY_AMD:
  650. buf = _mesa_string_buffer_create(NULL, 1024);
  651. _mesa_string_buffer_printf(buf, "%s:\n", radv_get_shader_name(variant, stage));
  652. _mesa_string_buffer_printf(buf, "%s\n\n", variant->disasm_string);
  653. generate_shader_stats(device, variant, stage, buf);
  654. /* Need to include the null terminator. */
  655. size_t length = buf->length + 1;
  656. if (!pInfo) {
  657. *pInfoSize = length;
  658. } else {
  659. size_t size = *pInfoSize;
  660. *pInfoSize = length;
  661. memcpy(pInfo, buf->buf, MIN2(size, length));
  662. if (size < length)
  663. result = VK_INCOMPLETE;
  664. }
  665. _mesa_string_buffer_destroy(buf);
  666. break;
  667. default:
  668. /* VK_SHADER_INFO_TYPE_BINARY_AMD unimplemented for now. */
  669. result = VK_ERROR_FEATURE_NOT_PRESENT;
  670. break;
  671. }
  672. return result;
  673. }