From 3b90d3997a14bd8bea633fa4b7bbb5accff5e559 Mon Sep 17 00:00:00 2001 From: Emma Anholt Date: Thu, 10 Mar 2022 11:15:16 -0800 Subject: [PATCH] turnip: use vk_shader_module_to_nir(). Reviewed-by: Jason Ekstrand Part-of: --- src/freedreno/vulkan/tu_pipeline.c | 11 ++++-- src/freedreno/vulkan/tu_private.h | 1 + src/freedreno/vulkan/tu_shader.c | 56 +++++++----------------------- 3 files changed, 23 insertions(+), 45 deletions(-) diff --git a/src/freedreno/vulkan/tu_pipeline.c b/src/freedreno/vulkan/tu_pipeline.c index 4e9131523e8..a8787758512 100644 --- a/src/freedreno/vulkan/tu_pipeline.c +++ b/src/freedreno/vulkan/tu_pipeline.c @@ -248,6 +248,7 @@ tu6_emit_load_state(struct tu_pipeline *pipeline, bool compute) struct tu_pipeline_builder { struct tu_device *device; + void *mem_ctx; struct tu_pipeline_cache *cache; struct tu_pipeline_layout *layout; const VkAllocationCallbacks *alloc; @@ -2461,7 +2462,7 @@ tu_pipeline_builder_compile_shaders(struct tu_pipeline_builder *builder, if (!stage_info) continue; - nir[stage] = tu_spirv_to_nir(builder->device, stage_info, stage); + nir[stage] = tu_spirv_to_nir(builder->device, builder->mem_ctx, stage_info, stage); if (!nir[stage]) return VK_ERROR_OUT_OF_HOST_MEMORY; } @@ -3360,6 +3361,7 @@ tu_pipeline_builder_finish(struct tu_pipeline_builder *builder) continue; tu_shader_destroy(builder->device, builder->shaders[i], builder->alloc); } + ralloc_free(builder->mem_ctx); } static void @@ -3374,6 +3376,7 @@ tu_pipeline_builder_init_graphics( *builder = (struct tu_pipeline_builder) { .device = dev, + .mem_ctx = ralloc_context(NULL), .cache = cache, .create_info = create_info, .alloc = alloc, @@ -3520,7 +3523,8 @@ tu_compute_pipeline_create(VkDevice device, struct ir3_shader_key key = {}; - nir_shader *nir = tu_spirv_to_nir(dev, stage_info, MESA_SHADER_COMPUTE); + void *pipeline_mem_ctx = ralloc_context(NULL); + nir_shader *nir = tu_spirv_to_nir(dev, pipeline_mem_ctx, stage_info, MESA_SHADER_COMPUTE); const bool executable_info = pCreateInfo->flags & VK_PIPELINE_CREATE_CAPTURE_INTERNAL_REPRESENTATIONS_BIT_KHR; @@ -3573,6 +3577,7 @@ tu_compute_pipeline_create(VkDevice device, tu_append_executable(pipeline, v, nir_initial_disasm); tu_shader_destroy(dev, shader, pAllocator); + ralloc_free(pipeline_mem_ctx); *pPipeline = tu_pipeline_to_handle(pipeline); @@ -3582,6 +3587,8 @@ fail: if (shader) tu_shader_destroy(dev, shader, pAllocator); + ralloc_free(pipeline_mem_ctx); + vk_object_free(&dev->vk, pAllocator, pipeline); return result; diff --git a/src/freedreno/vulkan/tu_private.h b/src/freedreno/vulkan/tu_private.h index 7d1441475b9..50f6a648a4d 100644 --- a/src/freedreno/vulkan/tu_private.h +++ b/src/freedreno/vulkan/tu_private.h @@ -1263,6 +1263,7 @@ tu_nir_lower_multiview(nir_shader *nir, uint32_t mask, bool *multi_pos_output, nir_shader * tu_spirv_to_nir(struct tu_device *dev, + void *mem_ctx, const VkPipelineShaderStageCreateInfo *stage_info, gl_shader_stage stage); diff --git a/src/freedreno/vulkan/tu_shader.c b/src/freedreno/vulkan/tu_shader.c index ef6689c868c..2b11c78fa4d 100644 --- a/src/freedreno/vulkan/tu_shader.c +++ b/src/freedreno/vulkan/tu_shader.c @@ -33,6 +33,7 @@ nir_shader * tu_spirv_to_nir(struct tu_device *dev, + void *mem_ctx, const VkPipelineShaderStageCreateInfo *stage_info, gl_shader_stage stage) { @@ -91,59 +92,28 @@ tu_spirv_to_nir(struct tu_device *dev, const nir_shader_compiler_options *nir_options = ir3_get_compiler_options(dev->compiler); - /* convert VkSpecializationInfo */ - const VkSpecializationInfo *spec_info = stage_info->pSpecializationInfo; - uint32_t num_spec = 0; - struct nir_spirv_specialization *spec = - vk_spec_info_to_nir_spirv(spec_info, &num_spec); - struct vk_shader_module *module = vk_shader_module_from_handle(stage_info->module); assert(module->size % 4 == 0); - nir_shader *nir = - spirv_to_nir((void*)module->data, module->size / 4, - spec, num_spec, stage, stage_info->pName, - &spirv_options, nir_options); - free(spec); - - assert(nir->info.stage == stage); - nir_validate_shader(nir, "after spirv_to_nir"); - - const struct nir_lower_sysvals_to_varyings_options sysvals_to_varyings = { - .point_coord = true, - }; - NIR_PASS_V(nir, nir_lower_sysvals_to_varyings, &sysvals_to_varyings); + nir_shader *nir; + VkResult result = vk_shader_module_to_nir(&dev->vk, module, + stage, stage_info->pName, + stage_info->pSpecializationInfo, + &spirv_options, nir_options, + mem_ctx, &nir); + if (result != VK_SUCCESS) + return NULL; if (unlikely(dev->physical_device->instance->debug_flags & TU_DEBUG_NIR)) { fprintf(stderr, "translated nir:\n"); nir_print_shader(nir, stderr); } - /* multi step inlining procedure */ - NIR_PASS_V(nir, nir_lower_variable_initializers, nir_var_function_temp); - NIR_PASS_V(nir, nir_lower_returns); - NIR_PASS_V(nir, nir_inline_functions); - NIR_PASS_V(nir, nir_copy_prop); - NIR_PASS_V(nir, nir_opt_deref); - foreach_list_typed_safe(nir_function, func, node, &nir->functions) { - if (!func->is_entrypoint) - exec_node_remove(&func->node); - } - assert(exec_list_length(&nir->functions) == 1); - NIR_PASS_V(nir, nir_lower_variable_initializers, ~nir_var_function_temp); - - /* Split member structs. We do this before lower_io_to_temporaries so that - * it doesn't lower system values to temporaries by accident. - */ - NIR_PASS_V(nir, nir_split_var_copies); - NIR_PASS_V(nir, nir_split_per_member_structs); - - NIR_PASS_V(nir, nir_remove_dead_variables, - nir_var_shader_in | nir_var_shader_out | nir_var_system_value | nir_var_mem_shared, - NULL); - - NIR_PASS_V(nir, nir_propagate_invariant, false); + const struct nir_lower_sysvals_to_varyings_options sysvals_to_varyings = { + .point_coord = true, + }; + NIR_PASS_V(nir, nir_lower_sysvals_to_varyings, &sysvals_to_varyings); NIR_PASS_V(nir, nir_lower_global_vars_to_local); NIR_PASS_V(nir, nir_split_var_copies);