glsl: Add tracking for elements of an array-of-arrays that have been accessed
authorIan Romanick <ian.d.romanick@intel.com>
Wed, 14 Dec 2016 04:29:23 +0000 (20:29 -0800)
committerIan Romanick <ian.d.romanick@intel.com>
Mon, 19 Dec 2016 23:55:43 +0000 (15:55 -0800)
If there's a better way to provide access to ir_array_refcount_entry
private members to the test functions, I am very interested to know
about it.

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Cc: Francisco Jerez <currojerez@riseup.net>
Cc: mesa-stable@lists.freedesktop.org
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
src/compiler/Makefile.glsl.am
src/compiler/glsl/ir_array_refcount.cpp
src/compiler/glsl/ir_array_refcount.h
src/compiler/glsl/tests/array_refcount_test.cpp [new file with mode: 0644]

index 5b1d74e28353cdf615f2821447e31072b51212de..5520aca86100e49806946285cc5a92a4022e7703 100644 (file)
@@ -69,6 +69,7 @@ glsl_tests_cache_test_LDADD =                         \
        $(PTHREAD_LIBS)
 
 glsl_tests_general_ir_test_SOURCES =                   \
+       glsl/tests/array_refcount_test.cpp              \
        glsl/tests/builtin_variable_test.cpp            \
        glsl/tests/invalidate_locations_test.cpp        \
        glsl/tests/general_ir_test.cpp                  \
index b997d415d1debb15ec06d6b7e75ad9dda214889a..36b22742fa8a55cf90d263c8bdb307e7a4d76dea 100644 (file)
@@ -56,7 +56,15 @@ ir_array_refcount_visitor::~ir_array_refcount_visitor()
 ir_array_refcount_entry::ir_array_refcount_entry(ir_variable *var)
    : var(var), is_referenced(false)
 {
-   /* empty */
+   num_bits = MAX2(1, var->type->arrays_of_arrays_size());
+   bits = new BITSET_WORD[BITSET_WORDS(num_bits)];
+   memset(bits, 0, BITSET_WORDS(num_bits) * sizeof(bits[0]));
+}
+
+
+ir_array_refcount_entry::~ir_array_refcount_entry()
+{
+   delete [] bits;
 }
 
 
index 9ec46d2f385df14bd3495826dea70be0464e83b8..4296e4b9eebc8ee33a78ed0d0d20e47e1a7f96e7 100644 (file)
 #include "ir.h"
 #include "ir_visitor.h"
 #include "compiler/glsl_types.h"
+#include "util/bitset.h"
 
 class ir_array_refcount_entry
 {
 public:
    ir_array_refcount_entry(ir_variable *var);
+   ~ir_array_refcount_entry();
 
    ir_variable *var; /* The key: the variable's pointer. */
 
    /** Has the variable been referenced? */
    bool is_referenced;
+
+   /** Has a linearized array index been referenced? */
+   bool is_linearized_index_referenced(unsigned linearized_index) const
+   {
+      assert(bits != 0);
+      assert(linearized_index <= num_bits);
+
+      return BITSET_TEST(bits, linearized_index);
+   }
+
+private:
+   /** Set of bit-flags to note which array elements have been accessed. */
+   BITSET_WORD *bits;
+
+   /**
+    * Total number of bits referenced by \c bits.
+    *
+    * Also the total number of array(s-of-arrays) elements of \c var.
+    */
+   unsigned num_bits;
+
+   friend class array_refcount_test;
 };
 
 class ir_array_refcount_visitor : public ir_hierarchical_visitor {
diff --git a/src/compiler/glsl/tests/array_refcount_test.cpp b/src/compiler/glsl/tests/array_refcount_test.cpp
new file mode 100644 (file)
index 0000000..d80ea81
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * Copyright © 2016 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.
+ */
+#include <gtest/gtest.h>
+#include "ir.h"
+#include "ir_array_refcount.h"
+
+class array_refcount_test : public ::testing::Test {
+public:
+   virtual void SetUp();
+   virtual void TearDown();
+
+   void *mem_ctx;
+
+   /**
+    * glsl_type for a vec4[3][4][5].
+    *
+    * The exceptionally verbose name is picked because it matches the syntax
+    * of http://cdecl.org/.
+    */
+   const glsl_type *array_3_of_array_4_of_array_5_of_vec4;
+
+   /**
+    * Wrapper to access private member "bits" of ir_array_refcount_entry
+    *
+    * The test class is a friend to ir_array_refcount_entry, but the
+    * individual tests are not part of the class.  Since the friendliness of
+    * the test class does not extend to the tests, provide a wrapper.
+    */
+   const BITSET_WORD *get_bits(const ir_array_refcount_entry &entry)
+   {
+      return entry.bits;
+   }
+
+   /**
+    * Wrapper to access private member "num_bits" of ir_array_refcount_entry
+    *
+    * The test class is a friend to ir_array_refcount_entry, but the
+    * individual tests are not part of the class.  Since the friendliness of
+    * the test class does not extend to the tests, provide a wrapper.
+    */
+   unsigned get_num_bits(const ir_array_refcount_entry &entry)
+   {
+      return entry.num_bits;
+   }
+};
+
+void
+array_refcount_test::SetUp()
+{
+   mem_ctx = ralloc_context(NULL);
+
+   /* The type of vec4 x[3][4][5]; */
+   const glsl_type *const array_5_of_vec4 =
+      glsl_type::get_array_instance(glsl_type::vec4_type, 5);
+   const glsl_type *const array_4_of_array_5_of_vec4 =
+      glsl_type::get_array_instance(array_5_of_vec4, 4);
+   array_3_of_array_4_of_array_5_of_vec4 =
+      glsl_type::get_array_instance(array_4_of_array_5_of_vec4, 3);
+}
+
+void
+array_refcount_test::TearDown()
+{
+   ralloc_free(mem_ctx);
+   mem_ctx = NULL;
+}
+
+TEST_F(array_refcount_test, ir_array_refcount_entry_initial_state_for_scalar)
+{
+   ir_variable *const var =
+      new(mem_ctx) ir_variable(glsl_type::int_type, "a", ir_var_auto);
+
+   ir_array_refcount_entry entry(var);
+
+   ASSERT_NE((void *)0, get_bits(entry));
+   EXPECT_FALSE(entry.is_referenced);
+   EXPECT_EQ(1, get_num_bits(entry));
+   EXPECT_FALSE(entry.is_linearized_index_referenced(0));
+}
+
+TEST_F(array_refcount_test, ir_array_refcount_entry_initial_state_for_vector)
+{
+   ir_variable *const var =
+      new(mem_ctx) ir_variable(glsl_type::vec4_type, "a", ir_var_auto);
+
+   ir_array_refcount_entry entry(var);
+
+   ASSERT_NE((void *)0, get_bits(entry));
+   EXPECT_FALSE(entry.is_referenced);
+   EXPECT_EQ(1, get_num_bits(entry));
+   EXPECT_FALSE(entry.is_linearized_index_referenced(0));
+}
+
+TEST_F(array_refcount_test, ir_array_refcount_entry_initial_state_for_matrix)
+{
+   ir_variable *const var =
+      new(mem_ctx) ir_variable(glsl_type::mat4_type, "a", ir_var_auto);
+
+   ir_array_refcount_entry entry(var);
+
+   ASSERT_NE((void *)0, get_bits(entry));
+   EXPECT_FALSE(entry.is_referenced);
+   EXPECT_EQ(1, get_num_bits(entry));
+   EXPECT_FALSE(entry.is_linearized_index_referenced(0));
+}
+
+TEST_F(array_refcount_test, ir_array_refcount_entry_initial_state_for_array)
+{
+   ir_variable *const var =
+      new(mem_ctx) ir_variable(array_3_of_array_4_of_array_5_of_vec4,
+                               "a",
+                               ir_var_auto);
+   const unsigned total_elements = var->type->arrays_of_arrays_size();
+
+   ir_array_refcount_entry entry(var);
+
+   ASSERT_NE((void *)0, get_bits(entry));
+   EXPECT_FALSE(entry.is_referenced);
+   EXPECT_EQ(total_elements, get_num_bits(entry));
+
+   for (unsigned i = 0; i < total_elements; i++)
+      EXPECT_FALSE(entry.is_linearized_index_referenced(i)) << "index = " << i;
+}