1 // Copyright (c) 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "reduce_test_util.h"
16
17 #include "source/opt/build_module.h"
18 #include "source/reduce/reduction_opportunity.h"
19 #include "source/reduce/remove_opname_instruction_reduction_pass.h"
20
21 namespace spvtools {
22 namespace reduce {
23 namespace {
24
TEST(RemoveOpnameInstructionReductionPassTest,NothingToRemove)25 TEST(RemoveOpnameInstructionReductionPassTest, NothingToRemove) {
26 const std::string source = R"(
27 OpCapability Shader
28 %1 = OpExtInstImport "GLSL.std.450"
29 OpMemoryModel Logical GLSL450
30 OpEntryPoint Fragment %4 "main"
31 OpExecutionMode %4 OriginUpperLeft
32 OpSource ESSL 310
33 %2 = OpTypeVoid
34 %3 = OpTypeFunction %2
35 %4 = OpFunction %2 None %3
36 %5 = OpLabel
37 OpReturn
38 OpFunctionEnd
39 )";
40
41 const auto env = SPV_ENV_UNIVERSAL_1_3;
42 const auto consumer = nullptr;
43 const auto context =
44 BuildModule(env, consumer, source, kReduceAssembleOption);
45 const auto pass = TestSubclass<RemoveOpNameInstructionReductionPass>(env);
46 const auto ops = pass.WrapGetAvailableOpportunities(context.get());
47 ASSERT_EQ(0, ops.size());
48 }
49
TEST(RemoveOpnameInstructionReductionPassTest,RemoveSingleOpName)50 TEST(RemoveOpnameInstructionReductionPassTest, RemoveSingleOpName) {
51 const std::string prologue = R"(
52 OpCapability Shader
53 %1 = OpExtInstImport "GLSL.std.450"
54 OpMemoryModel Logical GLSL450
55 OpEntryPoint Fragment %4 "main"
56 OpExecutionMode %4 OriginUpperLeft
57 OpSource ESSL 310
58 )";
59
60 const std::string epilogue = R"(
61 %2 = OpTypeVoid
62 %3 = OpTypeFunction %2
63 %4 = OpFunction %2 None %3
64 %5 = OpLabel
65 OpReturn
66 OpFunctionEnd
67 )";
68
69 const std::string original = prologue + R"(
70 OpName %4 "main"
71 )" + epilogue;
72
73 const std::string expected = prologue + epilogue;
74
75 const auto env = SPV_ENV_UNIVERSAL_1_3;
76 const auto consumer = nullptr;
77 const auto context =
78 BuildModule(env, consumer, original, kReduceAssembleOption);
79 const auto pass = TestSubclass<RemoveOpNameInstructionReductionPass>(env);
80 const auto ops = pass.WrapGetAvailableOpportunities(context.get());
81 ASSERT_EQ(1, ops.size());
82 ASSERT_TRUE(ops[0]->PreconditionHolds());
83 ops[0]->TryToApply();
84
85 CheckEqual(env, expected, context.get());
86 }
87
TEST(RemoveOpnameInstructionReductionPassTest,TryApplyRemovesAllOpName)88 TEST(RemoveOpnameInstructionReductionPassTest, TryApplyRemovesAllOpName) {
89 const std::string prologue = R"(
90 OpCapability Shader
91 %1 = OpExtInstImport "GLSL.std.450"
92 OpMemoryModel Logical GLSL450
93 OpEntryPoint Fragment %4 "main"
94 OpExecutionMode %4 OriginUpperLeft
95 OpSource ESSL 310
96 )";
97
98 const std::string epilogue = R"(
99 %2 = OpTypeVoid
100 %3 = OpTypeFunction %2
101 %6 = OpTypeFloat 32
102 %7 = OpTypePointer Function %6
103 %9 = OpConstant %6 1
104 %4 = OpFunction %2 None %3
105 %5 = OpLabel
106 %8 = OpVariable %7 Function
107 %10 = OpVariable %7 Function
108 %11 = OpVariable %7 Function
109 %12 = OpVariable %7 Function
110 OpStore %8 %9
111 OpStore %10 %9
112 OpStore %11 %9
113 OpStore %12 %9
114 OpReturn
115 OpFunctionEnd
116 )";
117
118 const std::string original = prologue + R"(
119 OpName %4 "main"
120 OpName %8 "a"
121 OpName %10 "b"
122 OpName %11 "c"
123 OpName %12 "d"
124 )" + epilogue;
125
126 const std::string expected = prologue + epilogue;
127
128 const auto env = SPV_ENV_UNIVERSAL_1_3;
129 auto pass = TestSubclass<RemoveOpNameInstructionReductionPass>(env);
130
131 {
132 // Check the right number of opportunities is detected
133 const auto consumer = nullptr;
134 const auto context =
135 BuildModule(env, consumer, original, kReduceAssembleOption);
136 const auto ops = pass.WrapGetAvailableOpportunities(context.get());
137 ASSERT_EQ(5, ops.size());
138 }
139
140 {
141 // The reduction should remove all OpName
142 std::vector<uint32_t> binary;
143 SpirvTools t(env);
144 ASSERT_TRUE(t.Assemble(original, &binary, kReduceAssembleOption));
145 auto reduced_binary = pass.TryApplyReduction(binary);
146 CheckEqual(env, expected, reduced_binary);
147 }
148 }
149
TEST(RemoveOpnameInstructionReductionPassTest,TryApplyRemovesAllOpNameAndOpMemberName)150 TEST(RemoveOpnameInstructionReductionPassTest,
151 TryApplyRemovesAllOpNameAndOpMemberName) {
152 const std::string prologue = R"(
153 OpCapability Shader
154 %1 = OpExtInstImport "GLSL.std.450"
155 OpMemoryModel Logical GLSL450
156 OpEntryPoint Fragment %4 "main"
157 OpExecutionMode %4 OriginUpperLeft
158 OpSource ESSL 310
159 )";
160
161 const std::string epilogue = R"(
162 %2 = OpTypeVoid
163 %3 = OpTypeFunction %2
164 %6 = OpTypeFloat 32
165 %7 = OpTypeInt 32 1
166 %8 = OpTypeVector %6 3
167 %9 = OpTypeStruct %6 %7 %8
168 %10 = OpTypePointer Function %9
169 %12 = OpConstant %7 0
170 %13 = OpConstant %6 1
171 %14 = OpTypePointer Function %6
172 %4 = OpFunction %2 None %3
173 %5 = OpLabel
174 %11 = OpVariable %10 Function
175 %15 = OpAccessChain %14 %11 %12
176 OpStore %15 %13
177 OpReturn
178 OpFunctionEnd
179 )";
180
181 const std::string original = prologue + R"(
182 OpName %4 "main"
183 OpName %9 "S"
184 OpMemberName %9 0 "f"
185 OpMemberName %9 1 "i"
186 OpMemberName %9 2 "v"
187 OpName %11 "s"
188 )" + epilogue;
189
190 const std::string expected = prologue + epilogue;
191
192 const auto env = SPV_ENV_UNIVERSAL_1_3;
193 auto pass = TestSubclass<RemoveOpNameInstructionReductionPass>(env);
194
195 {
196 // Check the right number of opportunities is detected
197 const auto consumer = nullptr;
198 const auto context =
199 BuildModule(env, consumer, original, kReduceAssembleOption);
200 const auto ops = pass.WrapGetAvailableOpportunities(context.get());
201 ASSERT_EQ(6, ops.size());
202 }
203
204 {
205 // The reduction should remove all OpName
206 std::vector<uint32_t> binary;
207 SpirvTools t(env);
208 ASSERT_TRUE(t.Assemble(original, &binary, kReduceAssembleOption));
209 auto reduced_binary = pass.TryApplyReduction(binary);
210 CheckEqual(env, expected, reduced_binary);
211 }
212 }
213
214 } // namespace
215 } // namespace reduce
216 } // namespace spvtools
217