[Mesa-dev] [PATCH 02/11] nir: Add a pass-running infastructure

Jason Ekstrand jason at jlekstrand.net
Wed Oct 28 14:32:02 PDT 2015


---
 src/glsl/nir/nir.h      | 19 +++++++++++++++
 src/glsl/nir/nir_pass.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
index e3777f9..069c7c1 100644
--- a/src/glsl/nir/nir.h
+++ b/src/glsl/nir/nir.h
@@ -1582,6 +1582,25 @@ typedef struct nir_shader {
       foreach_list_typed(nir_function_overload, overload, node, \
                          &(func)->overload_list)
 
+typedef struct nir_pass {
+   bool (*shader_pass_func)(nir_shader *shader, void *data);
+   bool (*impl_pass_func)(nir_function_impl *impl, void *data);
+   nir_metadata metadata_preserved;
+   void *data;
+} nir_pass;
+
+static inline nir_pass
+nir_pass_with_data(nir_pass pass, void *data)
+{
+   pass.data = data;
+   return pass;
+}
+
+bool nir_shader_run_pass(nir_shader *shader, const nir_pass *pass);
+bool nir_function_impl_run_pass(nir_function_impl *impl, const nir_pass *pass);
+bool nir_shader_optimize(nir_shader *shader,
+                         const nir_pass *passes, unsigned num_passes);
+
 nir_shader *nir_shader_create(void *mem_ctx,
                               gl_shader_stage stage,
                               const nir_shader_compiler_options *options);
diff --git a/src/glsl/nir/nir_pass.c b/src/glsl/nir/nir_pass.c
index a03e124..059d016 100644
--- a/src/glsl/nir/nir_pass.c
+++ b/src/glsl/nir/nir_pass.c
@@ -27,7 +27,7 @@
 #include "nir.h"
 
 /*
- * Handles management of the metadata.
+ * Handles management of NIR passes and metadata.
  */
 
 void
@@ -52,3 +52,65 @@ nir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved)
 {
    impl->valid_metadata &= preserved;
 }
+
+static bool
+_function_impl_run_pass(nir_function_impl *impl, const nir_pass *pass)
+{
+   bool progress = pass->impl_pass_func(impl, pass->data);
+   if (progress)
+      nir_metadata_preserve(impl, pass->metadata_preserved);
+
+   return progress;
+}
+
+bool
+nir_function_impl_run_pass(nir_function_impl *impl, const nir_pass *pass)
+{
+   bool progress = _function_impl_run_pass(impl, pass);
+
+   /* TODO: Add a way to validate a single function_impl */
+   nir_validate_shader(impl->overload->function->shader);
+
+   return progress;
+}
+
+bool
+nir_shader_run_pass(nir_shader *shader, const nir_pass *pass)
+{
+   bool progress;
+   if (pass->shader_pass_func) {
+      progress = pass->shader_pass_func(shader, pass->data);
+
+      if (progress) {
+         nir_foreach_overload(shader, overload) {
+            if (overload->impl)
+               nir_metadata_preserve(overload->impl, pass->metadata_preserved);
+         }
+      }
+   } else {
+      progress = false;
+      nir_foreach_overload(shader, overload) {
+         if (overload->impl)
+            progress |= _function_impl_run_pass(overload->impl, pass);
+      }
+   }
+
+   return progress;
+}
+
+bool
+nir_shader_optimize(nir_shader *shader,
+                    const nir_pass *passes, unsigned num_passes)
+{
+   bool progress, total_progress = false;
+
+   do {
+      progress = false;
+      for (unsigned p = 0; p < num_passes; p++)
+         progress |= nir_shader_run_pass(shader, &passes[p]);
+
+      total_progress |= progress;
+   } while (progress);
+
+   return total_progress;
+}
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list