<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, Jul 25, 2018 at 5:19 PM Timothy Arceri <<a href="mailto:tarceri@itsqueeze.com">tarceri@itsqueeze.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 12/07/18 07:18, Jason Ekstrand wrote:<br>
> This pulls the SPIR-V to NIR step out into common code.<br>
> ---<br>
>   src/intel/vulkan/anv_pipeline.c | 278 +++++++++++++-------------------<br>
>   1 file changed, 116 insertions(+), 162 deletions(-)<br>
> <br>
> diff --git a/src/intel/vulkan/anv_pipeline.c b/src/intel/vulkan/anv_pipeline.c<br>
> index bc268b87e55..50d6ab358d2 100644<br>
> --- a/src/intel/vulkan/anv_pipeline.c<br>
> +++ b/src/intel/vulkan/anv_pipeline.c<br>
> @@ -404,6 +404,14 @@ struct anv_pipeline_stage {<br>
>         gl_shader_stage stage;<br>
>         unsigned char sha1[20];<br>
>      } cache_key;<br>
> +<br>
> +   nir_shader *nir;<br>
> +<br>
> +   struct anv_pipeline_binding surface_to_descriptor[256];<br>
> +   struct anv_pipeline_binding sampler_to_descriptor[256];<br>
> +   struct anv_pipeline_bind_map bind_map;<br>
> +<br>
> +   union brw_any_prog_data prog_data;<br>
>   };<br>
>   <br>
>   static void<br>
> @@ -547,58 +555,40 @@ anv_fill_binding_table(struct brw_stage_prog_data *prog_data, unsigned bias)<br>
>   static VkResult<br>
>   anv_pipeline_compile_vs(struct anv_pipeline *pipeline,<br>
>                           struct anv_pipeline_cache *cache,<br>
> -                        const VkGraphicsPipelineCreateInfo *info,<br>
>                           struct anv_pipeline_stage *stage)<br>
>   {<br>
>      const struct brw_compiler *compiler =<br>
>         pipeline->device->instance->physicalDevice.compiler;<br>
>      struct anv_shader_bin *bin = NULL;<br>
>   <br>
> -   ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);<br>
> -<br>
>      if (bin == NULL) {<br>
> -      struct brw_vs_prog_data prog_data = {};<br>
> -      struct anv_pipeline_binding surface_to_descriptor[256];<br>
> -      struct anv_pipeline_binding sampler_to_descriptor[256];<br>
> -<br>
> -      struct anv_pipeline_bind_map map = {<br>
> -         .surface_to_descriptor = surface_to_descriptor,<br>
> -         .sampler_to_descriptor = sampler_to_descriptor<br>
> -      };<br>
> -<br>
>         void *mem_ctx = ralloc_context(NULL);<br>
>   <br>
> -      nir_shader *nir = anv_pipeline_compile(pipeline, mem_ctx, layout, stage,<br>
> -                                             &prog_data.base.base, &map);<br>
> -      if (nir == NULL) {<br>
> -         ralloc_free(mem_ctx);<br>
> -         return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
> -      }<br>
> -<br>
> -      anv_fill_binding_table(&prog_data.base.base, 0);<br>
> +      anv_fill_binding_table(&stage->prog_data.vs.base.base, 0);<br>
>   <br>
>         brw_compute_vue_map(&pipeline->device->info,<br>
> -                          &prog_data.base.vue_map,<br>
> -                          nir->info.outputs_written,<br>
> -                          nir->info.separate_shader);<br>
> +                          &stage->prog_data.vs.base.vue_map,<br>
> +                          stage->nir->info.outputs_written,<br>
> +                          stage->nir->info.separate_shader);<br>
>   <br>
>         const unsigned *shader_code =<br>
>            brw_compile_vs(compiler, NULL, mem_ctx, &stage->key.vs,<br>
> -                        &prog_data, nir, -1, NULL);<br>
> +                        &stage->prog_data.vs, stage->nir, -1, NULL);<br>
>         if (shader_code == NULL) {<br>
>            ralloc_free(mem_ctx);<br>
>            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
>         }<br>
>   <br>
> -      unsigned code_size = prog_data.base.base.program_size;<br>
> +      unsigned code_size = stage->prog_data.vs.base.base.program_size;<br>
>         bin = anv_device_upload_kernel(pipeline->device, cache,<br>
>                                        &stage->cache_key,<br>
>                                        sizeof(stage->cache_key),<br>
>                                        shader_code, code_size,<br>
> -                                     nir->constant_data,<br>
> -                                     nir->constant_data_size,<br>
> -                                     &prog_data.base.base, sizeof(prog_data),<br>
> -                                     &map);<br>
> +                                     stage->nir->constant_data,<br>
> +                                     stage->nir->constant_data_size,<br>
> +                                     &stage->prog_data.base,<br>
> +                                     sizeof(stage->prog_data.vs),<br>
> +                                     &stage->bind_map);<br>
>         if (!bin) {<br>
>            ralloc_free(mem_ctx);<br>
>            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
> @@ -654,7 +644,6 @@ merge_tess_info(struct shader_info *tes_info,<br>
>   static VkResult<br>
>   anv_pipeline_compile_tcs_tes(struct anv_pipeline *pipeline,<br>
>                                struct anv_pipeline_cache *cache,<br>
> -                             const VkGraphicsPipelineCreateInfo *info,<br>
>                                struct anv_pipeline_stage *tcs_stage,<br>
>                                struct anv_pipeline_stage *tes_stage)<br>
>   {<br>
> @@ -664,85 +653,60 @@ anv_pipeline_compile_tcs_tes(struct anv_pipeline *pipeline,<br>
>      struct anv_shader_bin *tcs_bin = NULL;<br>
>      struct anv_shader_bin *tes_bin = NULL;<br>
>   <br>
> -   ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);<br>
> -<br>
>      if (tcs_bin == NULL || tes_bin == NULL) {<br>
> -      struct brw_tcs_prog_data tcs_prog_data = {};<br>
> -      struct brw_tes_prog_data tes_prog_data = {};<br>
> -      struct anv_pipeline_binding tcs_surface_to_descriptor[256];<br>
> -      struct anv_pipeline_binding tcs_sampler_to_descriptor[256];<br>
> -      struct anv_pipeline_binding tes_surface_to_descriptor[256];<br>
> -      struct anv_pipeline_binding tes_sampler_to_descriptor[256];<br>
> -<br>
> -      struct anv_pipeline_bind_map tcs_map = {<br>
> -         .surface_to_descriptor = tcs_surface_to_descriptor,<br>
> -         .sampler_to_descriptor = tcs_sampler_to_descriptor<br>
> -      };<br>
> -      struct anv_pipeline_bind_map tes_map = {<br>
> -         .surface_to_descriptor = tes_surface_to_descriptor,<br>
> -         .sampler_to_descriptor = tes_sampler_to_descriptor<br>
> -      };<br>
> -<br>
>         void *mem_ctx = ralloc_context(NULL);<br>
>   <br>
> -      nir_shader *tcs_nir =<br>
> -         anv_pipeline_compile(pipeline, mem_ctx, layout, tcs_stage,<br>
> -                              &tcs_prog_data.base.base, &tcs_map);<br>
> -      nir_shader *tes_nir =<br>
> -         anv_pipeline_compile(pipeline, mem_ctx, layout, tes_stage,<br>
> -                              &tes_prog_data.base.base, &tes_map);<br>
> -      if (tcs_nir == NULL || tes_nir == NULL) {<br>
> -         ralloc_free(mem_ctx);<br>
> -         return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
> -      }<br>
> -<br>
> -      nir_lower_tes_patch_vertices(tes_nir,<br>
> -                                   tcs_nir->info.tess.tcs_vertices_out);<br>
> +      nir_lower_tes_patch_vertices(tes_stage->nir,<br>
> +                                   tcs_stage->nir->info.tess.tcs_vertices_out);<br>
>   <br>
>         /* Copy TCS info into the TES info */<br>
> -      merge_tess_info(&tes_nir->info, &tcs_nir->info);<br>
> +      merge_tess_info(&tes_stage->nir->info, &tcs_stage->nir->info);<br>
>   <br>
> -      anv_fill_binding_table(&tcs_prog_data.base.base, 0);<br>
> -      anv_fill_binding_table(&tes_prog_data.base.base, 0);<br>
> +      anv_fill_binding_table(&tcs_stage->prog_data.tcs.base.base, 0);<br>
> +      anv_fill_binding_table(&tes_stage->prog_data.tes.base.base, 0);<br>
>   <br>
>         /* Whacking the key after cache lookup is a bit sketchy, but all of<br>
>          * this comes from the SPIR-V, which is part of the hash used for the<br>
>          * pipeline cache.  So it should be safe.<br>
>          */<br>
> -      tcs_stage->key.tcs.tes_primitive_mode = tes_nir->info.tess.primitive_mode;<br>
> -      tcs_stage->key.tcs.outputs_written = tcs_nir->info.outputs_written;<br>
> +      tcs_stage->key.tcs.tes_primitive_mode =<br>
> +         tes_stage->nir->info.tess.primitive_mode;<br>
> +      tcs_stage->key.tcs.outputs_written =<br>
> +         tcs_stage->nir->info.outputs_written;<br>
>         tcs_stage->key.tcs.patch_outputs_written =<br>
> -         tcs_nir->info.patch_outputs_written;<br>
> +         tcs_stage->nir->info.patch_outputs_written;<br>
>         tcs_stage->key.tcs.quads_workaround =<br>
>            devinfo->gen < 9 &&<br>
> -         tes_nir->info.tess.primitive_mode == 7 /* GL_QUADS */ &&<br>
> -         tes_nir->info.tess.spacing == TESS_SPACING_EQUAL;<br>
> +         tes_stage->nir->info.tess.primitive_mode == 7 /* GL_QUADS */ &&<br>
> +         tes_stage->nir->info.tess.spacing == TESS_SPACING_EQUAL;<br>
>   <br>
> -      tes_stage->key.tes.inputs_read = tcs_nir->info.outputs_written;<br>
> +      tes_stage->key.tes.inputs_read =<br>
> +         tcs_stage->nir->info.outputs_written;<br>
>         tes_stage->key.tes.patch_inputs_read =<br>
> -         tcs_nir->info.patch_outputs_written;<br>
> +         tcs_stage->nir->info.patch_outputs_written;<br>
>   <br>
>         const int shader_time_index = -1;<br>
>         const unsigned *shader_code;<br>
>   <br>
>         shader_code =<br>
>            brw_compile_tcs(compiler, NULL, mem_ctx, &tcs_stage->key.tcs,<br>
> -                         &tcs_prog_data, tcs_nir, shader_time_index, NULL);<br>
> +                         &tcs_stage->prog_data.tcs, tcs_stage->nir,<br>
> +                         shader_time_index, NULL);<br>
>         if (shader_code == NULL) {<br>
>            ralloc_free(mem_ctx);<br>
>            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
>         }<br>
>   <br>
> -      unsigned code_size = tcs_prog_data.base.base.program_size;<br>
> +      unsigned code_size = tcs_stage->prog_data.base.program_size;<br>
>         tcs_bin = anv_device_upload_kernel(pipeline->device, cache,<br>
>                                            &tcs_stage->cache_key,<br>
>                                            sizeof(tcs_stage->cache_key),<br>
>                                            shader_code, code_size,<br>
> -                                         tcs_nir->constant_data,<br>
> -                                         tcs_nir->constant_data_size,<br>
> -                                         &tcs_prog_data.base.base,<br>
> -                                         sizeof(tcs_prog_data),<br>
> -                                         &tcs_map);<br>
> +                                         tcs_stage->nir->constant_data,<br>
> +                                         tcs_stage->nir->constant_data_size,<br>
> +                                         &tcs_stage->prog_data.base,<br>
> +                                         sizeof(tcs_stage->prog_data.tcs),<br>
> +                                         &tcs_stage->bind_map);<br>
>         if (!tcs_bin) {<br>
>            ralloc_free(mem_ctx);<br>
>            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
> @@ -750,23 +714,24 @@ anv_pipeline_compile_tcs_tes(struct anv_pipeline *pipeline,<br>
>   <br>
>         shader_code =<br>
>            brw_compile_tes(compiler, NULL, mem_ctx, &tes_stage->key.tes,<br>
> -                         &tcs_prog_data.base.vue_map, &tes_prog_data, tes_nir,<br>
> +                         &tcs_stage->prog_data.tcs.base.vue_map,<br>
> +                         &tes_stage->prog_data.tes, tes_stage->nir,<br>
>                            NULL, shader_time_index, NULL);<br>
>         if (shader_code == NULL) {<br>
>            ralloc_free(mem_ctx);<br>
>            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
>         }<br>
>   <br>
> -      code_size = tes_prog_data.base.base.program_size;<br>
> +      code_size = tes_stage->prog_data.base.program_size;<br>
>         tes_bin = anv_device_upload_kernel(pipeline->device, cache,<br>
>                                            &tes_stage->cache_key,<br>
>                                            sizeof(tes_stage->cache_key),<br>
>                                            shader_code, code_size,<br>
> -                                         tes_nir->constant_data,<br>
> -                                         tes_nir->constant_data_size,<br>
> -                                         &tes_prog_data.base.base,<br>
> -                                         sizeof(tes_prog_data),<br>
> -                                         &tes_map);<br>
> +                                         tes_stage->nir->constant_data,<br>
> +                                         tes_stage->nir->constant_data_size,<br>
> +                                         &tes_stage->prog_data.base,<br>
> +                                         sizeof(tes_stage->prog_data.tes),<br>
> +                                         &tes_stage->bind_map);<br>
>         if (!tes_bin) {<br>
>            ralloc_free(mem_ctx);<br>
>            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
> @@ -784,59 +749,42 @@ anv_pipeline_compile_tcs_tes(struct anv_pipeline *pipeline,<br>
>   static VkResult<br>
>   anv_pipeline_compile_gs(struct anv_pipeline *pipeline,<br>
>                           struct anv_pipeline_cache *cache,<br>
> -                        const VkGraphicsPipelineCreateInfo *info,<br>
>                           struct anv_pipeline_stage *stage)<br>
>   {<br>
>      const struct brw_compiler *compiler =<br>
>         pipeline->device->instance->physicalDevice.compiler;<br>
>      struct anv_shader_bin *bin = NULL;<br>
>   <br>
> -   ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);<br>
> -<br>
>      if (bin == NULL) {<br>
> -      struct brw_gs_prog_data prog_data = {};<br>
> -      struct anv_pipeline_binding surface_to_descriptor[256];<br>
> -      struct anv_pipeline_binding sampler_to_descriptor[256];<br>
> -<br>
> -      struct anv_pipeline_bind_map map = {<br>
> -         .surface_to_descriptor = surface_to_descriptor,<br>
> -         .sampler_to_descriptor = sampler_to_descriptor<br>
> -      };<br>
> -<br>
>         void *mem_ctx = ralloc_context(NULL);<br>
>   <br>
> -      nir_shader *nir = anv_pipeline_compile(pipeline, mem_ctx, layout, stage,<br>
> -                                             &prog_data.base.base, &map);<br>
> -      if (nir == NULL) {<br>
> -         ralloc_free(mem_ctx);<br>
> -         return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
> -      }<br>
> -<br>
> -      anv_fill_binding_table(&prog_data.base.base, 0);<br>
> +      anv_fill_binding_table(&stage->prog_data.gs.base.base, 0);<br>
>   <br>
>         brw_compute_vue_map(&pipeline->device->info,<br>
> -                          &prog_data.base.vue_map,<br>
> -                          nir->info.outputs_written,<br>
> -                          nir->info.separate_shader);<br>
> +                          &stage->prog_data.gs.base.vue_map,<br>
> +                          stage->nir->info.outputs_written,<br>
> +                          stage->nir->info.separate_shader);<br>
>   <br>
>         const unsigned *shader_code =<br>
>            brw_compile_gs(compiler, NULL, mem_ctx, &stage-><a href="http://key.gs" rel="noreferrer" target="_blank">key.gs</a>,<br>
> -                        &prog_data, nir, NULL, -1, NULL);<br>
> +                        &stage-><a href="http://prog_data.gs" rel="noreferrer" target="_blank">prog_data.gs</a>, stage->nir,<br>
> +                        NULL, -1, NULL);<br>
>         if (shader_code == NULL) {<br>
>            ralloc_free(mem_ctx);<br>
>            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
>         }<br>
>   <br>
>         /* TODO: SIMD8 GS */<br>
> -      const unsigned code_size = prog_data.base.base.program_size;<br>
> +      const unsigned code_size = stage->prog_data.base.program_size;<br>
>         bin = anv_device_upload_kernel(pipeline->device, cache,<br>
>                                        &stage->cache_key,<br>
>                                        sizeof(stage->cache_key),<br>
>                                        shader_code, code_size,<br>
> -                                     nir->constant_data,<br>
> -                                     nir->constant_data_size,<br>
> -                                     &prog_data.base.base, sizeof(prog_data),<br>
> -                                     &map);<br>
> +                                     stage->nir->constant_data,<br>
> +                                     stage->nir->constant_data_size,<br>
> +                                     &stage->prog_data.base,<br>
> +                                     sizeof(stage-><a href="http://prog_data.gs" rel="noreferrer" target="_blank">prog_data.gs</a>),<br>
> +                                     &stage->bind_map);<br>
>         if (!bin) {<br>
>            ralloc_free(mem_ctx);<br>
>            return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
> @@ -853,7 +801,6 @@ anv_pipeline_compile_gs(struct anv_pipeline *pipeline,<br>
>   static VkResult<br>
>   anv_pipeline_compile_fs(struct anv_pipeline *pipeline,<br>
>                           struct anv_pipeline_cache *cache,<br>
> -                        const VkGraphicsPipelineCreateInfo *info,<br>
>                           struct anv_pipeline_stage *stage)<br>
>   {<br>
>      const struct brw_compiler *compiler =<br>
> @@ -867,38 +814,20 @@ anv_pipeline_compile_fs(struct anv_pipeline *pipeline,<br>
>         &anv_pipeline_get_last_vue_prog_data(pipeline)->vue_map;<br>
>      stage->key.wm.input_slots_valid = vue_map->slots_valid;<br>
>   <br>
> -   ANV_FROM_HANDLE(anv_pipeline_layout, layout, info->layout);<br>
> -<br>
>      if (bin == NULL) {<br>
> -      struct brw_wm_prog_data prog_data = {};<br>
> -      struct anv_pipeline_binding surface_to_descriptor[256];<br>
> -      struct anv_pipeline_binding sampler_to_descriptor[256];<br>
> -<br>
> -      struct anv_pipeline_bind_map map = {<br>
> -         .surface_to_descriptor = surface_to_descriptor + 8,<br>
> -         .sampler_to_descriptor = sampler_to_descriptor<br>
> -      };<br></blockquote><div><br></div><div>This is relevant ^^<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> -<br>
>         void *mem_ctx = ralloc_context(NULL);<br>
>   <br>
> -      nir_shader *nir = anv_pipeline_compile(pipeline, mem_ctx, layout, stage,<br>
> -                                             &prog_data.base, &map);<br>
> -      if (nir == NULL) {<br>
> -         ralloc_free(mem_ctx);<br>
> -         return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);<br>
> -      }<br>
> -<br>
>         unsigned num_rts = 0;<br>
>         const int max_rt = FRAG_RESULT_DATA7 - FRAG_RESULT_DATA0 + 1;<br>
>         struct anv_pipeline_binding rt_bindings[max_rt];<br>
> -      nir_function_impl *impl = nir_shader_get_entrypoint(nir);<br>
> +      nir_function_impl *impl = nir_shader_get_entrypoint(stage->nir);<br>
>         int rt_to_bindings[max_rt];<br>
>         memset(rt_to_bindings, -1, sizeof(rt_to_bindings));<br>
>         bool rt_used[max_rt];<br>
>         memset(rt_used, 0, sizeof(rt_used));<br>
>   <br>
>         /* Flag used render targets */<br>
> -      nir_foreach_variable_safe(var, &nir->outputs) {<br>
> +      nir_foreach_variable_safe(var, &stage->nir->outputs) {<br>
>            if (var->data.location < FRAG_RESULT_DATA0)<br>
>               continue;<br>
>   <br>
> @@ -930,7 +859,7 @@ anv_pipeline_compile_fs(struct anv_pipeline *pipeline,<br>
>         }<br>
>   <br>
>         bool deleted_output = false;<br>
> -      nir_foreach_variable_safe(var, &nir->outputs) {<br>
> +      nir_foreach_variable_safe(var, &stage->nir->outputs) {<br>
>            if (var->data.location < FRAG_RESULT_DATA0)<br>
>               continue;<br>
>   <br>
> @@ -951,7 +880,7 @@ anv_pipeline_compile_fs(struct anv_pipeline *pipeline,<br>
>         }<br>
>   <br>
>         if (deleted_output)<br>
> -         nir_fixup_deref_modes(nir);<br>
> +         nir_fixup_deref_modes(stage->nir);<br>
>   <br>
>         if (num_rts == 0) {<br>
>            /* If we have no render targets, we need a null render target */<br>
> @@ -970,31 +899,36 @@ anv_pipeline_compile_fs(struct anv_pipeline *pipeline,<br>
>         stage->key.wm.color_outputs_valid = (1 << num_rts) - 1;<br>
>   <br>
>         assert(num_rts <= max_rt);<br>
> -      map.surface_to_descriptor -= num_rts;<br>
> -      map.surface_count += num_rts;<br>
> -      assert(map.surface_count <= 256);<br>
> -      memcpy(map.surface_to_descriptor, rt_bindings,<br>
> -             num_rts * sizeof(*rt_bindings));<br>
> +      assert(stage->bind_map.surface_count + num_rts <= 256);<br>
> +      memmove(stage->bind_map.surface_to_descriptor + num_rts,<br>
> +              stage->bind_map.surface_to_descriptor,<br>
> +              stage->bind_map.surface_count *<br>
> +              sizeof(*stage->bind_map.surface_to_descriptor));<br>
> +      typed_memcpy(stage->bind_map.surface_to_descriptor,<br>
> +                   rt_bindings, num_rts);<br>
> +      stage->bind_map.surface_count += num_rts;<br>
>   <br>
<br>
I'm not following the above change. Can you provide a little more <br>
context as to whats going on here?<br></blockquote><div><br></div><div>Yeah, this one's tricky.  The old code initialized map.surface_to_descriptor at surface_to_descriptor + 8 prior to anv_nir_apply_pipeline_layout being called.  In this code, it decrements map.surface_to_descriptor by num_rts (guaranteed to be <= 8) and copies the render target bindings in.  In the new code, we don't get the luxury of juggling the pointers like that and map.surface_to_descriptor points to the start of whatever array we are given.  Instead of simply decrementing the pointer and trusting it doesn't underrun, we have to memmove the bindings up to make room for the render targets and then we copy the render target bindings in.</div><div><br></div><div>Does that make more sense?</div><div><br></div><div>--Jason<br></div></div></div>