nvk: Cache NIR shaders
authorFaith Ekstrand <faith.ekstrand@collabora.com>
Wed, 27 Sep 2023 20:10:22 +0000 (15:10 -0500)
committerMarge Bot <emma+marge@anholt.net>
Thu, 28 Sep 2023 03:55:53 +0000 (03:55 +0000)
We can't cache shader binaries just yet but this at least lets us cache
the output of spirv_to_nir and the initial optimize.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25443>

src/nouveau/vulkan/nvk_compute_pipeline.c
src/nouveau/vulkan/nvk_graphics_pipeline.c
src/nouveau/vulkan/nvk_shader.c
src/nouveau/vulkan/nvk_shader.h

index 88e577e..7056ff4 100644 (file)
@@ -183,7 +183,7 @@ nvk_compute_pipeline_create(struct nvk_device *dev,
 
    nir_shader *nir;
    result = nvk_shader_stage_to_nir(dev, &pCreateInfo->stage, &robustness,
-                                    NULL, &nir);
+                                    cache, NULL, &nir);
    if (result != VK_SUCCESS)
       goto fail;
 
index a6026e5..ea3d684 100644 (file)
@@ -310,7 +310,7 @@ nvk_graphics_pipeline_create(struct nvk_device *dev,
                                         pCreateInfo->pNext, sinfo->pNext);
 
       result = nvk_shader_stage_to_nir(dev, sinfo, &robustness[stage],
-                                       NULL, &nir[stage]);
+                                       cache, NULL, &nir[stage]);
       if (result != VK_SUCCESS)
          goto fail;
    }
index cd56eac..d442ac7 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "vk_nir_convert_ycbcr.h"
 #include "vk_pipeline.h"
+#include "vk_pipeline_cache.h"
 #include "vk_pipeline_layout.h"
 #include "vk_shader_module.h"
 #include "vk_ycbcr_conversion.h"
@@ -24,6 +25,8 @@
 
 #include "nv50_ir_driver.h"
 
+#include "util/mesa-sha1.h"
+
 #include "cla097.h"
 #include "clc397.h"
 #include "clc597.h"
@@ -421,16 +424,32 @@ VkResult
 nvk_shader_stage_to_nir(struct nvk_device *dev,
                         const VkPipelineShaderStageCreateInfo *sinfo,
                         const struct vk_pipeline_robustness_state *rstate,
+                        struct vk_pipeline_cache *cache,
                         void *mem_ctx, struct nir_shader **nir_out)
 {
    struct nvk_physical_device *pdev = nvk_device_physical(dev);
    const gl_shader_stage stage = vk_to_mesa_shader_stage(sinfo->stage);
    const nir_shader_compiler_options *nir_options =
       nvk_physical_device_nir_options(pdev, stage);
+
+   unsigned char stage_sha1[SHA1_DIGEST_LENGTH];
+   vk_pipeline_hash_shader_stage(sinfo, rstate, stage_sha1);
+
+   if (cache == NULL)
+      cache = dev->mem_cache;
+
+   nir_shader *nir = vk_pipeline_cache_lookup_nir(cache, stage_sha1,
+                                                  sizeof(stage_sha1),
+                                                  nir_options, NULL,
+                                                  mem_ctx);
+   if (nir != NULL) {
+      *nir_out = nir;
+      return VK_SUCCESS;
+   }
+
    const struct spirv_to_nir_options spirv_options =
       nvk_physical_device_spirv_options(pdev, rstate);
 
-   nir_shader *nir;
    VkResult result = vk_pipeline_shader_stage_to_nir(&dev->vk, sinfo,
                                                      &spirv_options,
                                                      nir_options,
@@ -438,6 +457,8 @@ nvk_shader_stage_to_nir(struct nvk_device *dev,
    if (result != VK_SUCCESS)
       return result;
 
+   vk_pipeline_cache_add_nir(cache, stage_sha1, sizeof(stage_sha1), nir);
+
    *nir_out = nir;
 
    return VK_SUCCESS;
index f2ac9a5..8c4c58d 100644 (file)
@@ -14,6 +14,7 @@
 struct nvk_device;
 struct nvk_physical_device;
 struct nvk_pipeline_compilation_ctx;
+struct vk_pipeline_cache;
 struct vk_pipeline_layout;
 struct vk_pipeline_robustness_state;
 struct vk_shader_module;
@@ -129,6 +130,7 @@ VkResult
 nvk_shader_stage_to_nir(struct nvk_device *dev,
                         const VkPipelineShaderStageCreateInfo *sinfo,
                         const struct vk_pipeline_robustness_state *rstate,
+                        struct vk_pipeline_cache *cache,
                         void *mem_ctx, struct nir_shader **nir_out);
 
 void