nir/deref: add helpers to lazily create paths
authorRhys Perry <pendingchaos02@gmail.com>
Tue, 10 Nov 2020 10:13:04 +0000 (10:13 +0000)
committerRhys Perry <pendingchaos02@gmail.com>
Fri, 20 Nov 2020 13:57:34 +0000 (13:57 +0000)
Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7511>

src/compiler/nir/nir_deref.c
src/compiler/nir/nir_deref.h

index a5282ce..4735175 100644 (file)
@@ -641,6 +641,26 @@ nir_compare_derefs(nir_deref_instr *a, nir_deref_instr *b)
    return result;
 }
 
+nir_deref_path *nir_get_deref_path(void *mem_ctx, nir_deref_and_path *deref)
+{
+   if (!deref->_path) {
+      deref->_path = ralloc(mem_ctx, nir_deref_path);
+      nir_deref_path_init(deref->_path, deref->instr, mem_ctx);
+   }
+   return deref->_path;
+}
+
+nir_deref_compare_result nir_compare_derefs_and_paths(void *mem_ctx,
+                                                      nir_deref_and_path *a,
+                                                      nir_deref_and_path *b)
+{
+   if (a->instr == b->instr) /* nir_compare_derefs has a fast path if a == b */
+      return nir_compare_derefs(a->instr, b->instr);
+
+   return nir_compare_deref_paths(nir_get_deref_path(mem_ctx, a),
+                                  nir_get_deref_path(mem_ctx, b));
+}
+
 struct rematerialize_deref_state {
    bool progress;
    nir_builder builder;
index 20d4037..a7ada23 100644 (file)
@@ -44,6 +44,11 @@ typedef struct {
    nir_deref_instr **path;
 } nir_deref_path;
 
+typedef struct {
+   nir_deref_instr *instr;
+   nir_deref_path *_path;
+} nir_deref_and_path;
+
 void nir_deref_path_init(nir_deref_path *path,
                          nir_deref_instr *deref, void *mem_ctx);
 void nir_deref_path_finish(nir_deref_path *path);
@@ -54,6 +59,8 @@ unsigned nir_deref_instr_get_const_offset(nir_deref_instr *deref,
 nir_ssa_def *nir_build_deref_offset(nir_builder *b, nir_deref_instr *deref,
                                     glsl_type_size_align_func size_align);
 
+nir_deref_path *nir_get_deref_path(void *mem_ctx, nir_deref_and_path *deref);
+
 typedef enum {
    nir_derefs_do_not_alias     = 0,
    nir_derefs_equal_bit        = (1 << 0),
@@ -64,6 +71,9 @@ typedef enum {
 
 nir_deref_compare_result nir_compare_deref_paths(nir_deref_path *a_path, nir_deref_path *b_path);
 nir_deref_compare_result nir_compare_derefs(nir_deref_instr *a, nir_deref_instr *b);
+nir_deref_compare_result nir_compare_derefs_and_paths(void *mem_ctx,
+                                                      nir_deref_and_path *a,
+                                                      nir_deref_and_path *b);
 
 #ifdef __cplusplus
 } /* extern "C" */