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

Author: Alyssa Rosenzweig <[email protected]>
Date:   Tue Apr 19 12:46:43 2022 -0400

nir: Use u_worklist to back nir_block_worklist

u_worklist is nir_block_worklist, suitably generalized. All we need to do is
define the macros to translate between the APIs.

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

---

 src/compiler/nir/nir_worklist.c | 102 ----------------------------------------
 src/compiler/nir/nir_worklist.h |  57 ++++++++--------------
 2 files changed, 19 insertions(+), 140 deletions(-)

diff --git a/src/compiler/nir/nir_worklist.c b/src/compiler/nir/nir_worklist.c
index 0c20d6ddce2..155e22f7277 100644
--- a/src/compiler/nir/nir_worklist.c
+++ b/src/compiler/nir/nir_worklist.c
@@ -27,26 +27,6 @@
 
 #include "nir_worklist.h"
 
-void
-nir_block_worklist_init(nir_block_worklist *w, unsigned num_blocks,
-                        void *mem_ctx)
-{
-   w->size = num_blocks;
-   w->count = 0;
-   w->start = 0;
-
-   w->blocks_present = rzalloc_array(mem_ctx, BITSET_WORD,
-                                     BITSET_WORDS(num_blocks));
-   w->blocks = rzalloc_array(mem_ctx, nir_block *, num_blocks);
-}
-
-void
-nir_block_worklist_fini(nir_block_worklist *w)
-{
-   ralloc_free(w->blocks_present);
-   ralloc_free(w->blocks);
-}
-
 void
 nir_block_worklist_add_all(nir_block_worklist *w, nir_function_impl *impl)
 {
@@ -55,88 +35,6 @@ nir_block_worklist_add_all(nir_block_worklist *w, 
nir_function_impl *impl)
    }
 }
 
-void
-nir_block_worklist_push_head(nir_block_worklist *w, nir_block *block)
-{
-   /* Pushing a block we already have is a no-op */
-   if (BITSET_TEST(w->blocks_present, block->index))
-      return;
-
-   assert(w->count < w->size);
-
-   if (w->start == 0)
-      w->start = w->size - 1;
-   else
-      w->start--;
-
-   w->count++;
-
-   w->blocks[w->start] = block;
-   BITSET_SET(w->blocks_present, block->index);
-}
-
-nir_block *
-nir_block_worklist_peek_head(const nir_block_worklist *w)
-{
-   assert(w->count > 0);
-
-   return w->blocks[w->start];
-}
-
-nir_block *
-nir_block_worklist_pop_head(nir_block_worklist *w)
-{
-   assert(w->count > 0);
-
-   unsigned head = w->start;
-
-   w->start = (w->start + 1) % w->size;
-   w->count--;
-
-   BITSET_CLEAR(w->blocks_present, w->blocks[head]->index);
-   return w->blocks[head];
-}
-
-void
-nir_block_worklist_push_tail(nir_block_worklist *w, nir_block *block)
-{
-   /* Pushing a block we already have is a no-op */
-   if (BITSET_TEST(w->blocks_present, block->index))
-      return;
-
-   assert(w->count < w->size);
-
-   w->count++;
-
-   unsigned tail = (w->start + w->count - 1) % w->size;
-
-   w->blocks[tail] = block;
-   BITSET_SET(w->blocks_present, block->index);
-}
-
-nir_block *
-nir_block_worklist_peek_tail(const nir_block_worklist *w)
-{
-   assert(w->count > 0);
-
-   unsigned tail = (w->start + w->count - 1) % w->size;
-
-   return w->blocks[tail];
-}
-
-nir_block *
-nir_block_worklist_pop_tail(nir_block_worklist *w)
-{
-   assert(w->count > 0);
-
-   unsigned tail = (w->start + w->count - 1) % w->size;
-
-   w->count--;
-
-   BITSET_CLEAR(w->blocks_present, w->blocks[tail]->index);
-   return w->blocks[tail];
-}
-
 static bool
 nir_instr_worklist_add_srcs_cb(nir_src *src, void *state)
 {
diff --git a/src/compiler/nir/nir_worklist.h b/src/compiler/nir/nir_worklist.h
index ad45b064d5c..9dd389bc3c2 100644
--- a/src/compiler/nir/nir_worklist.h
+++ b/src/compiler/nir/nir_worklist.h
@@ -32,59 +32,40 @@
 #include "nir.h"
 #include "util/set.h"
 #include "util/u_vector.h"
+#include "util/u_worklist.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-/** Represents a double-ended queue of unique blocks
- *
- * The worklist datastructure guarantees that eacy block is in the queue at
- * most once.  Pushing a block onto either end of the queue is a no-op if
- * the block is already in the queue.  In order for this to work, the
- * caller must ensure that the blocks are properly indexed.
- */
-typedef struct {
-   /* The total size of the worklist */
-   unsigned size;
-
-   /* The number of blocks currently in the worklist */
-   unsigned count;
-
-   /* The offset in the array of blocks at which the list starts */
-   unsigned start;
-
-   /* A bitset of all of the blocks currently present in the worklist */
-   BITSET_WORD *blocks_present;
+typedef u_worklist nir_block_worklist;
 
-   /* The actual worklist */
-   nir_block **blocks;
-} nir_block_worklist;
+#define nir_block_worklist_init(w, num_blocks, mem_ctx) \
+        u_worklist_init(w, num_blocks, mem_ctx)
 
-void nir_block_worklist_init(nir_block_worklist *w, unsigned num_blocks,
-                             void *mem_ctx);
-void nir_block_worklist_fini(nir_block_worklist *w);
+#define nir_block_worklist_fini(w) u_worklist_fini(w)
 
-void nir_block_worklist_add_all(nir_block_worklist *w, nir_function_impl 
*impl);
-
-static inline bool
-nir_block_worklist_is_empty(const nir_block_worklist *w)
-{
-   return w->count == 0;
-}
+#define nir_block_worklist_is_empty(w) u_worklist_is_empty(w)
 
-void nir_block_worklist_push_head(nir_block_worklist *w, nir_block *block);
+#define nir_block_worklist_push_head(w, block) \
+        u_worklist_push_head(w, block, index)
 
-nir_block *nir_block_worklist_peek_head(const nir_block_worklist *w);
+#define nir_block_worklist_peek_head(w) \
+        u_worklist_peek_head(w, nir_block, index)
 
-nir_block *nir_block_worklist_pop_head(nir_block_worklist *w);
+#define nir_block_worklist_pop_head(w) \
+        u_worklist_pop_head(w, nir_block, index)
 
-void nir_block_worklist_push_tail(nir_block_worklist *w, nir_block *block);
+#define nir_block_worklist_push_tail(w, block) \
+        u_worklist_push_tail(w, block, index)
 
-nir_block *nir_block_worklist_peek_tail(const nir_block_worklist *w);
+#define nir_block_worklist_peek_tail(w) \
+        u_worklist_peek_tail(w, nir_block, index)
 
-nir_block *nir_block_worklist_pop_tail(nir_block_worklist *w);
+#define nir_block_worklist_pop_tail(w) \
+        u_worklist_pop_tail(w, nir_block, index)
 
+void nir_block_worklist_add_all(nir_block_worklist *w, nir_function_impl 
*impl);
 
 /*
  * This worklist implementation, in contrast to the block worklist, does not

Reply via email to