Mesa (main): ra: Add a unit test.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Jun 4 19:32:16 UTC 2021


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

Author: Eric Anholt <eric at anholt.net>
Date:   Thu Mar  4 12:22:11 2021 -0800

ra: Add a unit test.

This is mostly checking that we agree with a bit of the table from the
paper.  It proved quite useful as I was refactoring.

Acked-by: Jason Ekstrand <jason at jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9437>

---

 src/util/meson.build                  |  34 +++----
 src/util/register_allocate.c          | 123 +------------------------
 src/util/register_allocate_internal.h | 169 ++++++++++++++++++++++++++++++++++
 src/util/register_allocate_test.cpp   |  96 +++++++++++++++++++
 4 files changed, 278 insertions(+), 144 deletions(-)

diff --git a/src/util/meson.build b/src/util/meson.build
index b187a69cdfd..daed5b784dc 100644
--- a/src/util/meson.build
+++ b/src/util/meson.build
@@ -342,28 +342,18 @@ if with_tests
     )
   endif
 
-  test(
-    'bitset',
-    executable(
-       'bitset_test',
-       files('bitset_test.cpp'),
-       include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux],
-       dependencies : [idep_mesautil, idep_gtest],
-     ),
-     suite : ['util'],
-  )
-
-  test(
-    'u_debug_stack',
-    executable(
-      'u_debug_stack_test',
-      files('u_debug_stack_test.cpp'),
-      include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux],
-      dependencies : [idep_mesautil, idep_gtest],
-      c_args : [c_msvc_compat_args],
-    ),
-     suite : ['util'],
-  )
+  foreach t: ['bitset', 'register_allocate', 'u_debug_stack']
+    test(
+      t,
+      executable(
+        t + '_test',
+        files(t + '_test.cpp'),
+        include_directories : [inc_include, inc_src, inc_mapi, inc_mesa, inc_gallium, inc_gallium_aux],
+        dependencies : [idep_mesautil, idep_gtest],
+      ),
+      suite : ['util'],
+    )
+  endforeach
 
   process_test_exe = executable(
     'process_test',
diff --git a/src/util/register_allocate.c b/src/util/register_allocate.c
index 1d9a074174f..802b20a3704 100644
--- a/src/util/register_allocate.c
+++ b/src/util/register_allocate.c
@@ -80,128 +80,7 @@
 #include "util/u_dynarray.h"
 #include "u_math.h"
 #include "register_allocate.h"
-
-struct ra_reg {
-   BITSET_WORD *conflicts;
-   struct util_dynarray conflict_list;
-};
-
-struct ra_regs {
-   struct ra_reg *regs;
-   unsigned int count;
-
-   struct ra_class **classes;
-   unsigned int class_count;
-
-   bool round_robin;
-};
-
-struct ra_class {
-   /**
-    * Bitset indicating which registers belong to this class.
-    *
-    * (If bit N is set, then register N belongs to this class.)
-    */
-   BITSET_WORD *regs;
-
-   /**
-    * p(B) in Runeson/Nyström paper.
-    *
-    * This is "how many regs are in the set."
-    */
-   unsigned int p;
-
-   /**
-    * q(B,C) (indexed by C, B is this register class) in
-    * Runeson/Nyström paper.  This is "how many registers of B could
-    * the worst choice register from C conflict with".
-    */
-   unsigned int *q;
-};
-
-struct ra_node {
-   /** @{
-    *
-    * List of which nodes this node interferes with.  This should be
-    * symmetric with the other node.
-    */
-   BITSET_WORD *adjacency;
-
-   struct util_dynarray adjacency_list;
-   /** @} */
-
-   unsigned int class;
-
-   /* Client-assigned register, if assigned, or NO_REG. */
-   unsigned int forced_reg;
-
-   /* Register, if assigned, or NO_REG. */
-   unsigned int reg;
-
-   /**
-    * The q total, as defined in the Runeson/Nyström paper, for all the
-    * interfering nodes not in the stack.
-    */
-   unsigned int q_total;
-
-   /* For an implementation that needs register spilling, this is the
-    * approximate cost of spilling this node.
-    */
-   float spill_cost;
-
-   /* Temporary data for the algorithm to scratch around in */
-   struct {
-      /**
-       * Temporary version of q_total which we decrement as things are placed
-       * into the stack.
-       */
-      unsigned int q_total;
-   } tmp;
-};
-
-struct ra_graph {
-   struct ra_regs *regs;
-   /**
-    * the variables that need register allocation.
-    */
-   struct ra_node *nodes;
-   unsigned int count; /**< count of nodes. */
-
-   unsigned int alloc; /**< count of nodes allocated. */
-
-   ra_select_reg_callback select_reg_callback;
-   void *select_reg_callback_data;
-
-   /* Temporary data for the algorithm to scratch around in */
-   struct {
-      unsigned int *stack;
-      unsigned int stack_count;
-
-      /** Bit-set indicating, for each register, if it's in the stack */
-      BITSET_WORD *in_stack;
-
-      /** Bit-set indicating, for each register, if it pre-assigned */
-      BITSET_WORD *reg_assigned;
-
-      /** Bit-set indicating, for each register, the value of the pq test */
-      BITSET_WORD *pq_test;
-
-      /** For each BITSET_WORD, the minimum q value or ~0 if unknown */
-      unsigned int *min_q_total;
-
-      /*
-       * * For each BITSET_WORD, the node with the minimum q_total if
-       * min_q_total[i] != ~0.
-       */
-      unsigned int *min_q_node;
-
-      /**
-       * Tracks the start of the set of optimistically-colored registers in the
-       * stack.
-       */
-      unsigned int stack_optimistic_start;
-   } tmp;
-};
+#include "register_allocate_internal.h"
 
 /**
  * Creates a set of registers for the allocator.
diff --git a/src/util/register_allocate_internal.h b/src/util/register_allocate_internal.h
new file mode 100644
index 00000000000..fd53bff9afd
--- /dev/null
+++ b/src/util/register_allocate_internal.h
@@ -0,0 +1,169 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Eric Anholt <eric at anholt.net>
+ *
+ */
+
+#ifndef REGISTER_ALLOCATE_INTERNAL_H
+#define REGISTER_ALLOCATE_INTERNAL_H
+
+#include <stdbool.h>
+#include "util/bitset.h"
+#include "util/u_dynarray.h"
+
+#ifdef __cplusplus
+extern "C" {
+
+#define class klass
+#endif
+
+struct ra_reg {
+   BITSET_WORD *conflicts;
+   struct util_dynarray conflict_list;
+};
+
+struct ra_regs {
+   struct ra_reg *regs;
+   unsigned int count;
+
+   struct ra_class **classes;
+   unsigned int class_count;
+
+   bool round_robin;
+};
+
+struct ra_class {
+   /**
+    * Bitset indicating which registers belong to this class.
+    *
+    * (If bit N is set, then register N belongs to this class.)
+    */
+   BITSET_WORD *regs;
+
+   /**
+    * p(B) in Runeson/Nyström paper.
+    *
+    * This is "how many regs are in the set."
+    */
+   unsigned int p;
+
+   /**
+    * q(B,C) (indexed by C, B is this register class) in
+    * Runeson/Nyström paper.  This is "how many registers of B could
+    * the worst choice register from C conflict with".
+    */
+   unsigned int *q;
+};
+
+struct ra_node {
+   /** @{
+    *
+    * List of which nodes this node interferes with.  This should be
+    * symmetric with the other node.
+    */
+   BITSET_WORD *adjacency;
+
+   struct util_dynarray adjacency_list;
+   /** @} */
+
+   unsigned int class;
+
+   /* Client-assigned register, if assigned, or NO_REG. */
+   unsigned int forced_reg;
+
+   /* Register, if assigned, or NO_REG. */
+   unsigned int reg;
+
+   /**
+    * The q total, as defined in the Runeson/Nyström paper, for all the
+    * interfering nodes not in the stack.
+    */
+   unsigned int q_total;
+
+   /* For an implementation that needs register spilling, this is the
+    * approximate cost of spilling this node.
+    */
+   float spill_cost;
+
+   /* Temporary data for the algorithm to scratch around in */
+   struct {
+      /**
+       * Temporary version of q_total which we decrement as things are placed
+       * into the stack.
+       */
+      unsigned int q_total;
+   } tmp;
+};
+
+struct ra_graph {
+   struct ra_regs *regs;
+   /**
+    * the variables that need register allocation.
+    */
+   struct ra_node *nodes;
+   unsigned int count; /**< count of nodes. */
+
+   unsigned int alloc; /**< count of nodes allocated. */
+
+   ra_select_reg_callback select_reg_callback;
+   void *select_reg_callback_data;
+
+   /* Temporary data for the algorithm to scratch around in */
+   struct {
+      unsigned int *stack;
+      unsigned int stack_count;
+
+      /** Bit-set indicating, for each register, if it's in the stack */
+      BITSET_WORD *in_stack;
+
+      /** Bit-set indicating, for each register, if it pre-assigned */
+      BITSET_WORD *reg_assigned;
+
+      /** Bit-set indicating, for each register, the value of the pq test */
+      BITSET_WORD *pq_test;
+
+      /** For each BITSET_WORD, the minimum q value or ~0 if unknown */
+      unsigned int *min_q_total;
+
+      /*
+       * * For each BITSET_WORD, the node with the minimum q_total if
+       * min_q_total[i] != ~0.
+       */
+      unsigned int *min_q_node;
+
+      /**
+       * Tracks the start of the set of optimistically-colored registers in the
+       * stack.
+       */
+      unsigned int stack_optimistic_start;
+   } tmp;
+};
+
+#ifdef __cplusplus
+} /* extern "C" */
+
+#undef class
+#endif
+
+#endif /* REGISTER_ALLOCATE_INTERNAL_H  */
diff --git a/src/util/register_allocate_test.cpp b/src/util/register_allocate_test.cpp
new file mode 100644
index 00000000000..84b20617604
--- /dev/null
+++ b/src/util/register_allocate_test.cpp
@@ -0,0 +1,96 @@
+/*
+ * Copyright © 2021 Google LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <gtest/gtest.h>
+#include "ralloc.h"
+#include "register_allocate.h"
+#include "register_allocate_internal.h"
+
+class ra_test : public ::testing::Test {
+public:
+   void *mem_ctx;
+
+protected:
+   ra_test();
+   ~ra_test();
+};
+
+ra_test::ra_test()
+{
+   mem_ctx = ralloc_context(NULL);
+}
+
+ra_test::~ra_test()
+{
+   ralloc_free(mem_ctx);
+}
+
+TEST_F(ra_test, thumb)
+{
+   struct ra_regs *regs = ra_alloc_reg_set(mem_ctx, 100, true);
+
+   /* r0..15 are the real HW registers. */
+   int next_vreg = 16;
+
+   /* reg32low is any of the low 8 registers. */
+   int reg32low = ra_alloc_reg_class(regs);
+   for (int i = 0; i < 8; i++) {
+      int vreg = next_vreg++;
+      ra_class_add_reg(regs, reg32low, vreg);
+      ra_add_transitive_reg_conflict(regs, i, vreg);
+   }
+
+   /* reg64low is pairs of the low 8 registers (with wraparound!) */
+   int reg64low = ra_alloc_reg_class(regs);
+   for (int i = 0; i < 8; i++) {
+      int vreg = next_vreg++;
+      ra_class_add_reg(regs, reg64low, vreg);
+      ra_add_transitive_reg_conflict(regs, i, vreg);
+      ra_add_transitive_reg_conflict(regs, (i + 1) % 8, vreg);
+   }
+
+   /* reg96 is one of either r[0..2] or r[1..3] */
+   int reg96 = ra_alloc_reg_class(regs);
+   for (int i = 0; i < 2; i++) {
+      int vreg = next_vreg++;
+      ra_class_add_reg(regs, reg96, vreg);
+      for (int j = 0; j < 3; j++)
+         ra_add_transitive_reg_conflict(regs, i + j, vreg);
+   }
+
+   ra_set_finalize(regs, NULL);
+
+   /* Table 4.1 */
+   ASSERT_EQ(regs->classes[reg32low]->p, 8);
+   ASSERT_EQ(regs->classes[reg32low]->q[reg32low], 1);
+   ASSERT_EQ(regs->classes[reg32low]->q[reg64low], 2);
+   ASSERT_EQ(regs->classes[reg32low]->q[reg96], 3);
+   ASSERT_EQ(regs->classes[reg64low]->p, 8);
+   ASSERT_EQ(regs->classes[reg64low]->q[reg32low], 2);
+   ASSERT_EQ(regs->classes[reg64low]->q[reg64low], 3);
+   ASSERT_EQ(regs->classes[reg64low]->q[reg96], 4);
+   ASSERT_EQ(regs->classes[reg96]->p, 2);
+   ASSERT_EQ(regs->classes[reg96]->q[reg96], 2);
+   ASSERT_EQ(regs->classes[reg96]->q[reg64low], 2);
+   ASSERT_EQ(regs->classes[reg96]->q[reg96], 2);
+}



More information about the mesa-commit mailing list