i965/vec4: Add a test for copy propagation behavior.
authorEric Anholt <eric@anholt.net>
Mon, 7 Apr 2014 18:40:39 +0000 (11:40 -0700)
committerEric Anholt <eric@anholt.net>
Tue, 8 Apr 2014 07:59:48 +0000 (00:59 -0700)
I thought I was seeing a bug in the code while reviewing, but it's not
there.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
src/mesa/drivers/dri/i965/.gitignore
src/mesa/drivers/dri/i965/Makefile.am
src/mesa/drivers/dri/i965/test_vec4_copy_propagation.cpp [new file with mode: 0644]

index 777e8d9214b3918cfe7d838f91d8a41d34d85e7f..3cf4ba03dbf0664acd62a42112196331248629b2 100644 (file)
@@ -1,4 +1,5 @@
 i965_symbols_test
 test_eu_compact
+test_vec4_copy_propagation
 test_vec4_register_coalesce
 test_blorp_blit_eu_gen
index 54a38bdbd43435ea4550add367f62f488210740b..7ab376cc12f679ed19c09b9f08cc1751ab83c78c 100644 (file)
@@ -55,6 +55,7 @@ TEST_LIBS = \
 
 TESTS = \
         test_eu_compact \
+        test_vec4_copy_propagation \
         test_vec4_register_coalesce \
         test_blorp_blit_eu_gen
 
@@ -66,6 +67,12 @@ test_vec4_register_coalesce_LDADD = \
         $(TEST_LIBS) \
         $(top_builddir)/src/gtest/libgtest.la
 
+test_vec4_copy_propagation_SOURCES = \
+       test_vec4_copy_propagation.cpp
+test_vec4_copy_propagation_LDADD = \
+        $(TEST_LIBS) \
+        $(top_builddir)/src/gtest/libgtest.la
+
 test_eu_compact_SOURCES = \
        test_eu_compact.c
 nodist_EXTRA_test_eu_compact_SOURCES = dummy.cpp
diff --git a/src/mesa/drivers/dri/i965/test_vec4_copy_propagation.cpp b/src/mesa/drivers/dri/i965/test_vec4_copy_propagation.cpp
new file mode 100644 (file)
index 0000000..cb70096
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * Copyright © 2014 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 "brw_vec4.h"
+#include "brw_vs.h"
+
+using namespace brw;
+
+int ret = 0;
+
+class copy_propagation_test : public ::testing::Test {
+   virtual void SetUp();
+
+public:
+   struct brw_context *brw;
+   struct gl_context *ctx;
+   struct gl_shader_program *shader_prog;
+   struct brw_vertex_program *vp;
+   vec4_visitor *v;
+};
+
+class copy_propagation_vec4_visitor : public vec4_visitor
+{
+public:
+   copy_propagation_vec4_visitor(struct brw_context *brw,
+                                  struct gl_shader_program *shader_prog)
+      : vec4_visitor(brw, NULL, NULL, NULL, NULL, shader_prog,
+                     MESA_SHADER_VERTEX, NULL,
+                     false, false /* no_spills */,
+                     ST_NONE, ST_NONE, ST_NONE)
+   {
+   }
+
+protected:
+   virtual dst_reg *make_reg_for_system_value(ir_variable *ir)
+   {
+      assert(!"Not reached");
+      return NULL;
+   }
+
+   virtual void setup_payload()
+   {
+      assert(!"Not reached");
+   }
+
+   virtual void emit_prolog()
+   {
+      assert(!"Not reached");
+   }
+
+   virtual void emit_program_code()
+   {
+      assert(!"Not reached");
+   }
+
+   virtual void emit_thread_end()
+   {
+      assert(!"Not reached");
+   }
+
+   virtual void emit_urb_write_header(int mrf)
+   {
+      assert(!"Not reached");
+   }
+
+   virtual vec4_instruction *emit_urb_write_opcode(bool complete)
+   {
+      assert(!"Not reached");
+      unreachable();
+   }
+};
+
+
+void copy_propagation_test::SetUp()
+{
+   brw = (struct brw_context *)calloc(1, sizeof(*brw));
+   ctx = &brw->ctx;
+
+   vp = ralloc(NULL, struct brw_vertex_program);
+
+   shader_prog = ralloc(NULL, struct gl_shader_program);
+
+   v = new copy_propagation_vec4_visitor(brw, shader_prog);
+
+   _mesa_init_vertex_program(ctx, &vp->program, GL_VERTEX_SHADER, 0);
+
+   brw->gen = 4;
+}
+
+static void
+copy_propagation(vec4_visitor *v)
+{
+   bool print = false;
+
+   if (print) {
+      fprintf(stderr, "instructions before:\n");
+      v->dump_instructions();
+   }
+
+   v->opt_copy_propagation();
+
+   if (print) {
+      fprintf(stderr, "instructions after:\n");
+      v->dump_instructions();
+   }
+}
+
+TEST_F(copy_propagation_test, test_swizzle_swizzle)
+{
+   dst_reg a = dst_reg(v, glsl_type::vec4_type);
+   dst_reg b = dst_reg(v, glsl_type::vec4_type);
+   dst_reg c = dst_reg(v, glsl_type::vec4_type);
+
+   v->emit(v->ADD(a, src_reg(a), src_reg(a)));
+
+   v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_Y,
+                                                      SWIZZLE_Z,
+                                                      SWIZZLE_W,
+                                                      SWIZZLE_X))));
+
+   vec4_instruction *test_mov =
+      v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_Y,
+                                                 SWIZZLE_Z,
+                                                 SWIZZLE_W,
+                                                 SWIZZLE_X)));
+   v->emit(test_mov);
+
+   copy_propagation(v);
+
+   EXPECT_EQ(test_mov->src[0].reg, a.reg);
+   EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_Z,
+                                                    SWIZZLE_W,
+                                                    SWIZZLE_X,
+                                                    SWIZZLE_Y));
+}