spirv: Change spirv_to_nir() to return a nir_shader
authorCaio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Sun, 19 May 2019 07:22:17 +0000 (00:22 -0700)
committerCaio Marcelo de Oliveira Filho <caio.oliveira@intel.com>
Wed, 29 May 2019 17:34:35 +0000 (10:34 -0700)
spirv_to_nir() returned the nir_function corresponding to the
entrypoint, as a way to identify it.  There's now a bool is_entrypoint
in nir_function and also a helper function to get the entry_point from
a nir_shader.

The return type reflects better what the function name suggests.  It
also helps drivers avoid the mistake of reusing internal shader
references after running NIR_PASS on it.  When using NIR_TEST_CLONE or
NIR_TEST_SERIALIZE, those would be invalidated right in the first pass
executed.

Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
src/amd/vulkan/radv_shader.c
src/compiler/spirv/nir_spirv.h
src/compiler/spirv/spirv2nir.c
src/compiler/spirv/spirv_to_nir.c
src/freedreno/vulkan/tu_shader.c
src/gallium/drivers/freedreno/ir3/ir3_cmdline.c
src/intel/vulkan/anv_pipeline.c
src/mesa/main/glspirv.c

index 05ca14b527a1fe506d2d883ab5a3bf0e8328610b..36dfc47de27b2f2ac11526ca220f56dd6f60eba1 100644 (file)
@@ -287,11 +287,10 @@ radv_shader_compile_to_nir(struct radv_device *device,
                        .push_const_addr_format = nir_address_format_logical,
                        .shared_addr_format = nir_address_format_32bit_offset,
                };
-               nir_function *entry_point = spirv_to_nir(spirv, module->size / 4,
-                                                        spec_entries, num_spec_entries,
-                                                        stage, entrypoint_name,
-                                                        &spirv_options, &nir_options);
-               nir = entry_point->shader;
+               nir = spirv_to_nir(spirv, module->size / 4,
+                                  spec_entries, num_spec_entries,
+                                  stage, entrypoint_name,
+                                  &spirv_options, &nir_options);
                assert(nir->info.stage == stage);
                nir_validate_shader(nir, "after spirv_to_nir");
 
index 1e9e0727b63bb8ff3d570be4a16c8e869ce57df5..c4381bdf62e3e0283e72b2ae11c39277587d91e8 100644 (file)
@@ -94,12 +94,12 @@ bool gl_spirv_validation(const uint32_t *words, size_t word_count,
                          struct nir_spirv_specialization *spec, unsigned num_spec,
                          gl_shader_stage stage, const char *entry_point_name);
 
-nir_function *spirv_to_nir(const uint32_t *words, size_t word_count,
-                           struct nir_spirv_specialization *specializations,
-                           unsigned num_specializations,
-                           gl_shader_stage stage, const char *entry_point_name,
-                           const struct spirv_to_nir_options *options,
-                           const nir_shader_compiler_options *nir_options);
+nir_shader *spirv_to_nir(const uint32_t *words, size_t word_count,
+                         struct nir_spirv_specialization *specializations,
+                         unsigned num_specializations,
+                         gl_shader_stage stage, const char *entry_point_name,
+                         const struct spirv_to_nir_options *options,
+                         const nir_shader_compiler_options *nir_options);
 
 #ifdef __cplusplus
 }
index 5957f064b49c1c9ee79007f46339051c008c47b2..48d2694c96351ddcf5e37e172bd1df0c6be4d6ca 100644 (file)
@@ -74,10 +74,10 @@ int main(int argc, char **argv)
 
    struct spirv_to_nir_options spirv_opts = {};
 
-   nir_function *func = spirv_to_nir(map, word_count, NULL, 0,
-                                     MESA_SHADER_FRAGMENT, "main",
-                                     &spirv_opts, NULL);
-   nir_print_shader(func->shader, stderr);
+   nir_shader *nir = spirv_to_nir(map, word_count, NULL, 0,
+                                  MESA_SHADER_FRAGMENT, "main",
+                                  &spirv_opts, NULL);
+   nir_print_shader(nir, stderr);
 
    return 0;
 }
index d877077fa3bceea965836ec631f7afbcaec30b93..c273e9e9fee732e41ddca36c965ac70c33a9d099 100644 (file)
@@ -4552,7 +4552,7 @@ vtn_emit_kernel_entry_point_wrapper(struct vtn_builder *b,
    return main_entry_point;
 }
 
-nir_function *
+nir_shader *
 spirv_to_nir(const uint32_t *words, size_t word_count,
              struct nir_spirv_specialization *spec, unsigned num_spec,
              gl_shader_stage stage, const char *entry_point_name,
@@ -4669,7 +4669,8 @@ spirv_to_nir(const uint32_t *words, size_t word_count,
    /* Unparent the shader from the vtn_builder before we delete the builder */
    ralloc_steal(NULL, b->shader);
 
+   nir_shader *shader = b->shader;
    ralloc_free(b);
 
-   return entry_point;
+   return shader;
 }
index 5d5e65247833d04197ffd2fc8e0ca3545a9eb9de..d87aa1dbf7129f7d5e7d75ce5450624ee6690857 100644 (file)
@@ -68,16 +68,16 @@ tu_spirv_to_nir(struct ir3_compiler *compiler,
       num_spec = spec_info->mapEntryCount;
    }
 
-   nir_function *entry_point =
+   nir_shader *nir =
       spirv_to_nir(words, word_count, spec, num_spec, stage, entry_point_name,
                    &spirv_options, nir_options);
 
    free(spec);
 
-   assert(entry_point->shader->info.stage == stage);
-   nir_validate_shader(entry_point->shader, "after spirv_to_nir");
+   assert(nir->info.stage == stage);
+   nir_validate_shader(nir, "after spirv_to_nir");
 
-   return entry_point->shader;
+   return nir;
 }
 
 static void
index d45f5afec8be6c481027d0de51c14b31a0efa2b6..34b39aa65f087526e19f8605cdbc1b16c7ec958d 100644 (file)
@@ -242,21 +242,21 @@ load_spirv(const char *filename, const char *entry, gl_shader_stage stage)
                        .func = debug_func,
                }
        };
-       nir_function *entry_point;
+       nir_shader *nir;
        void *buf;
        size_t size;
 
        read_file(filename, &buf, &size);
 
-       entry_point = spirv_to_nir(buf, size / 4,
+       nir = spirv_to_nir(buf, size / 4,
                        NULL, 0, /* spec_entries */
                        stage, entry,
                        &spirv_options,
                        ir3_get_compiler_options(compiler));
 
-       nir_print_shader(entry_point->shader, stdout);
+       nir_print_shader(nir, stdout);
 
-       return entry_point->shader;
+       return nir;
 }
 
 static void print_usage(void)
index df9a968f854fd08812b43093874cac6cd7fb8413..f244b0c991b09ad7da9e873855bd4b254a9bcf3c 100644 (file)
@@ -184,11 +184,10 @@ anv_shader_compile_to_nir(struct anv_device *device,
    };
 
 
-   nir_function *entry_point =
+   nir_shader *nir =
       spirv_to_nir(spirv, module->size / 4,
                    spec_entries, num_spec_entries,
                    stage, entrypoint_name, &spirv_options, nir_options);
-   nir_shader *nir = entry_point->shader;
    assert(nir->info.stage == stage);
    nir_validate_shader(nir, "after spirv_to_nir");
    ralloc_steal(mem_ctx, nir);
index 8d1ac9b5072c52377b06fb6b8e31ba9f05ad11fb..bb5a3f7452b004ce0f497917d45508484ad2ecac 100644 (file)
@@ -186,8 +186,6 @@ _mesa_spirv_to_nir(struct gl_context *ctx,
                    gl_shader_stage stage,
                    const nir_shader_compiler_options *options)
 {
-   nir_shader *nir = NULL;
-
    struct gl_linked_shader *linked_shader = prog->_LinkedShaders[stage];
    assert (linked_shader);
 
@@ -217,7 +215,7 @@ _mesa_spirv_to_nir(struct gl_context *ctx,
       .caps = ctx->Const.SpirVCapabilities
    };
 
-   nir_function *entry_point =
+   nir_shader *nir =
       spirv_to_nir((const uint32_t *) &spirv_module->Binary[0],
                    spirv_module->Length / 4,
                    spec_entries, spirv_data->NumSpecializationConstants,
@@ -226,8 +224,7 @@ _mesa_spirv_to_nir(struct gl_context *ctx,
                    options);
    free(spec_entries);
 
-   assert (entry_point);
-   nir = entry_point->shader;
+   assert(nir);
    assert(nir->info.stage == stage);
 
    nir->options = options;