Module: Mesa
Branch: main
Commit: 5b29463746d559c926cb84b35c749625123dec82
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=5b29463746d559c926cb84b35c749625123dec82

Author: Yonggang Luo <[email protected]>
Date:   Fri Jun 23 11:57:47 2023 +0800

nir: Add function nir_function_set_impl

This function is added for create strong relationship between
nir_function_impl and nir_function.

So that nir_function->impl->function == nir_function is always true when
(nir_function->impl != NULL && nir_function->impl != 
NIR_SERIALIZE_FUNC_HAS_IMPL)

And indeed this invariant is already done in functions validate_function and 
validate_function_impl
of nir_validate

Signed-off-by: Yonggang Luo <[email protected]>
Reviewed-by: Alyssa Rosenzweig <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23820>

---

 src/compiler/clc/nir_load_libclc.c     |  3 +--
 src/compiler/nir/nir.c                 |  5 +----
 src/compiler/nir/nir.h                 | 10 ++++++++++
 src/compiler/nir/nir_clone.c           |  3 +--
 src/compiler/nir/nir_serialize.c       |  5 ++---
 src/microsoft/compiler/dxil_nir_tess.c |  3 +--
 6 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/src/compiler/clc/nir_load_libclc.c 
b/src/compiler/clc/nir_load_libclc.c
index 4472f744754..d9d1ad2d6a3 100644
--- a/src/compiler/clc/nir_load_libclc.c
+++ b/src/compiler/clc/nir_load_libclc.c
@@ -276,8 +276,7 @@ libclc_add_generic_variants(nir_shader *shader)
       for (unsigned i = 0; i < gfunc->num_params; i++)
          gfunc->params[i] = func->params[i];
 
-      gfunc->impl = nir_function_impl_clone(shader, func->impl);
-      gfunc->impl->function = gfunc;
+      nir_function_set_impl(gfunc, nir_function_impl_clone(shader, 
func->impl));
 
       /* Rewrite any global pointers to generic */
       nir_foreach_block(block, gfunc->impl) {
diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c
index 510bc8ab3a4..1210d80bd39 100644
--- a/src/compiler/nir/nir.c
+++ b/src/compiler/nir/nir.c
@@ -667,10 +667,7 @@ nir_function_impl_create(nir_function *function)
    assert(function->impl == NULL);
 
    nir_function_impl *impl = nir_function_impl_create_bare(function->shader);
-
-   function->impl = impl;
-   impl->function = function;
-
+   nir_function_set_impl(function, impl);
    return impl;
 }
 
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 93c25f15379..57b8446593c 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -3402,6 +3402,9 @@ typedef struct nir_function {
    /** The implementation of this function.
     *
     * If the function is only declared and not implemented, this is NULL.
+    *
+    * Unless setting to NULL or NIR_SERIALIZE_FUNC_HAS_IMPL, set with
+    * nir_function_set_impl to maintain IR invariants.
     */
    nir_function_impl *impl;
 
@@ -4090,6 +4093,13 @@ void nir_sort_variables_with_modes(nir_shader *shader,
 /** creates a function and adds it to the shader's list of functions */
 nir_function *nir_function_create(nir_shader *shader, const char *name);
 
+static inline void
+nir_function_set_impl(nir_function *func, nir_function_impl *impl)
+{
+   func->impl = impl;
+   impl->function = func;
+}
+
 nir_function_impl *nir_function_impl_create(nir_function *func);
 /** creates a function_impl that isn't tied to any particular function */
 nir_function_impl *nir_function_impl_create_bare(nir_shader *shader);
diff --git a/src/compiler/nir/nir_clone.c b/src/compiler/nir/nir_clone.c
index 5774cde21f1..2820a1655c5 100644
--- a/src/compiler/nir/nir_clone.c
+++ b/src/compiler/nir/nir_clone.c
@@ -771,8 +771,7 @@ nir_shader_clone(void *mem_ctx, const nir_shader *s)
     */
    nir_foreach_function(fxn, s) {
       nir_function *nfxn = remap_global(&state, fxn);
-      nfxn->impl = clone_function_impl(&state, fxn->impl);
-      nfxn->impl->function = nfxn;
+      nir_function_set_impl(nfxn, clone_function_impl(&state, fxn->impl));
    }
 
    ns->info = s->info;
diff --git a/src/compiler/nir/nir_serialize.c b/src/compiler/nir/nir_serialize.c
index 3004f1abb48..ffaa71ca163 100644
--- a/src/compiler/nir/nir_serialize.c
+++ b/src/compiler/nir/nir_serialize.c
@@ -2001,10 +2001,9 @@ write_function_impl(write_ctx *ctx, const 
nir_function_impl *fi)
 }
 
 static nir_function_impl *
-read_function_impl(read_ctx *ctx, nir_function *fxn)
+read_function_impl(read_ctx *ctx)
 {
    nir_function_impl *fi = nir_function_impl_create_bare(ctx->nir);
-   fi->function = fxn;
 
    fi->structured = blob_read_uint8(ctx->blob);
    bool preamble = blob_read_uint8(ctx->blob);
@@ -2223,7 +2222,7 @@ nir_deserialize(void *mem_ctx,
 
    nir_foreach_function(fxn, ctx.nir) {
       if (fxn->impl == NIR_SERIALIZE_FUNC_HAS_IMPL)
-         fxn->impl = read_function_impl(&ctx, fxn);
+         nir_function_set_impl(fxn, read_function_impl(&ctx));
    }
 
    ctx.nir->constant_data_size = blob_read_uint32(blob);
diff --git a/src/microsoft/compiler/dxil_nir_tess.c 
b/src/microsoft/compiler/dxil_nir_tess.c
index 635710ff201..e9a6d2033a5 100644
--- a/src/microsoft/compiler/dxil_nir_tess.c
+++ b/src/microsoft/compiler/dxil_nir_tess.c
@@ -194,8 +194,7 @@ dxil_nir_split_tess_ctrl(nir_shader *nir, nir_function 
**patch_const_func)
 
    *patch_const_func = nir_function_create(nir, "PatchConstantFunc");
    nir_function_impl *patch_const_func_impl = nir_function_impl_clone(nir, 
entrypoint);
-   (*patch_const_func)->impl = patch_const_func_impl;
-   patch_const_func_impl->function = *patch_const_func;
+   nir_function_set_impl(*patch_const_func, patch_const_func_impl);
 
    remove_hs_intrinsics(entrypoint);
    prune_patch_function_to_intrinsic_and_srcs(patch_const_func_impl);

Reply via email to