1 /* -*- mesa-c++ -*-
2 *
3 * Copyright (c) 2021 Collabora LTD
4 *
5 * Author: Gert Wollny <gert.wollny@collabora.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * on the rights to use, copy, modify, merge, publish, distribute, sub
11 * license, and/or sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "../sfn_alu_defines.h"
28 #include "../sfn_debug.h"
29 #include "../sfn_valuefactory.h"
30 #include "nir_builder.h"
31 #include "util/ralloc.h"
32
33 #include "gtest/gtest.h"
34
35 using namespace r600;
36
37 class ValuefactoryTest : public ::testing::Test {
38
39 public:
40 ValuefactoryTest();
41
42 protected:
43 void SetUp() override;
44 void TearDown() override;
45
46 ValueFactory *factory;
47 nir_builder b;
48 nir_shader_compiler_options options;
49 };
50
TEST_F(ValuefactoryTest,test_create_ssa)51 TEST_F(ValuefactoryTest, test_create_ssa)
52 {
53 auto c1 = nir_imm_float(&b, 2.0);
54 auto c2 = nir_imm_float(&b, 4.0);
55 auto sum = nir_fadd(&b, c1, c2);
56 auto alu = nir_instr_as_alu(sum->parent_instr);
57
58 sfn_log << SfnLog::reg << "Search (test) " << &alu->def << "\n";
59 auto dest_value = factory->dest(alu->def, 0, pin_none);
60 EXPECT_EQ(dest_value->sel(), 1024);
61 EXPECT_EQ(dest_value->chan(), 0);
62 EXPECT_EQ(dest_value->pin(), pin_none);
63
64 nir_src src = nir_src_for_ssa(sum);
65 sfn_log << SfnLog::reg << "Search (test) " << &src << "\n";
66 PVirtualValue value = factory->src(src, 0);
67 EXPECT_EQ(value->sel(), 1024);
68 EXPECT_EQ(value->chan(), 0);
69 EXPECT_EQ(value->pin(), pin_none);
70 }
71
TEST_F(ValuefactoryTest,test_create_ssa_pinned_chan)72 TEST_F(ValuefactoryTest, test_create_ssa_pinned_chan)
73 {
74 auto c1 = nir_imm_float(&b, 2.0);
75 auto c2 = nir_imm_float(&b, 4.0);
76 auto sum = nir_fadd(&b, c1, c2);
77 auto alu = nir_instr_as_alu(sum->parent_instr);
78
79 auto dest_value = factory->dest(alu->def, 0, pin_chan);
80 EXPECT_EQ(dest_value->sel(), 1024);
81 EXPECT_EQ(dest_value->chan(), 0);
82 EXPECT_EQ(dest_value->pin(), pin_chan);
83
84 PVirtualValue value = factory->src(nir_src_for_ssa(sum), 0);
85 EXPECT_EQ(value->sel(), 1024);
86 EXPECT_EQ(value->chan(), 0);
87 EXPECT_EQ(value->pin(), pin_chan);
88 }
89
TEST_F(ValuefactoryTest,test_create_ssa_pinned_chan_and_reg)90 TEST_F(ValuefactoryTest, test_create_ssa_pinned_chan_and_reg)
91 {
92 auto c1 = nir_imm_float(&b, 2.0);
93 auto c2 = nir_imm_float(&b, 4.0);
94 auto sum = nir_fadd(&b, c1, c2);
95 auto alu = nir_instr_as_alu(sum->parent_instr);
96
97 auto dest_value = factory->dest(alu->def, 1, pin_chan);
98 EXPECT_EQ(dest_value->sel(), 1024);
99 EXPECT_EQ(dest_value->chan(), 1);
100 EXPECT_EQ(dest_value->pin(), pin_chan);
101
102 PVirtualValue value = factory->src(nir_src_for_ssa(sum), 1);
103 EXPECT_EQ(value->sel(), 1024);
104 EXPECT_EQ(value->chan(), 1);
105 EXPECT_EQ(value->pin(), pin_chan);
106 }
107
TEST_F(ValuefactoryTest,test_create_const)108 TEST_F(ValuefactoryTest, test_create_const)
109 {
110 auto c1 = nir_imm_int(&b, 2);
111 auto c2 = nir_imm_int(&b, 4);
112 auto sum = nir_iadd(&b, c1, c2);
113
114 auto ci1 = nir_instr_as_load_const(c1->parent_instr);
115 factory->allocate_const(ci1);
116
117 auto ci2 = nir_instr_as_load_const(c2->parent_instr);
118 factory->allocate_const(ci2);
119
120 auto alu = nir_instr_as_alu(sum->parent_instr);
121
122 PVirtualValue value1 = factory->src(alu->src[0], 0);
123 PVirtualValue value2 = factory->src(alu->src[1], 0);
124
125 const auto* cvalue1 = value1->as_literal();
126 const auto* cvalue2 = value2->as_literal();
127
128 ASSERT_TRUE(cvalue1);
129 ASSERT_TRUE(cvalue2);
130
131 EXPECT_EQ(cvalue1->value(), 2);
132 EXPECT_EQ(cvalue2->value(), 4);
133 }
134
TEST_F(ValuefactoryTest,test_create_sysvalue)135 TEST_F(ValuefactoryTest, test_create_sysvalue)
136 {
137 auto ic = factory->inline_const(ALU_SRC_TIME_LO, 0);
138
139 EXPECT_EQ(ic->sel(), ALU_SRC_TIME_LO);
140 EXPECT_EQ(ic->chan(), 0);
141 }
142
143 class GetKCache : public ConstRegisterVisitor {
144 public:
visit(const VirtualValue & value)145 void visit(const VirtualValue& value) { (void)value; }
visit(const Register & value)146 void visit(const Register& value) { (void)value; };
visit(const LocalArray & value)147 void visit(const LocalArray& value) { (void)value; }
visit(const LocalArrayValue & value)148 void visit(const LocalArrayValue& value) { (void)value; }
visit(const UniformValue & value)149 void visit(const UniformValue& value)
150 {
151 (void)value;
152 m_result = value.kcache_bank();
153 }
visit(const LiteralConstant & value)154 void visit(const LiteralConstant& value) { (void)value; }
visit(const InlineConstant & value)155 void visit(const InlineConstant& value) { (void)value; }
156
GetKCache()157 GetKCache():
158 m_result(0)
159 {
160 }
161
162 int m_result;
163 };
164
ValuefactoryTest()165 ValuefactoryTest::ValuefactoryTest()
166 {
167 memset(&options, 0, sizeof(options));
168 init_pool();
169 }
170
171 void
SetUp()172 ValuefactoryTest::SetUp()
173 {
174 glsl_type_singleton_init_or_ref();
175 b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, &options, "test shader");
176 factory = new ValueFactory();
177 }
178
179 void
TearDown()180 ValuefactoryTest::TearDown()
181 {
182 ralloc_free(b.shader);
183 glsl_type_singleton_decref();
184 release_pool();
185 }
186