• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2022 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 <iostream>
16 
17 #include "gmock/gmock.h"
18 #include "test/opt/assembly_builder.h"
19 #include "test/opt/pass_fixture.h"
20 #include "test/opt/pass_utils.h"
21 
22 namespace spvtools {
23 namespace opt {
24 namespace {
25 
26 using InterfaceVariableScalarReplacementTest = PassTest<::testing::Test>;
27 
TEST_F(InterfaceVariableScalarReplacementTest,ReplaceInterfaceVarsWithScalars)28 TEST_F(InterfaceVariableScalarReplacementTest,
29        ReplaceInterfaceVarsWithScalars) {
30   const std::string spirv = R"(
31                OpCapability Shader
32                OpCapability Tessellation
33                OpMemoryModel Logical GLSL450
34                OpEntryPoint TessellationControl %func "shader" %x %y %z %w %u %v
35 
36 ; CHECK:     OpName [[x:%\w+]] "x"
37 ; CHECK-NOT: OpName {{%\w+}} "x"
38 ; CHECK:     OpName [[y:%\w+]] "y"
39 ; CHECK-NOT: OpName {{%\w+}} "y"
40 ; CHECK:     OpName [[z0:%\w+]] "z"
41 ; CHECK:     OpName [[z1:%\w+]] "z"
42 ; CHECK:     OpName [[w0:%\w+]] "w"
43 ; CHECK:     OpName [[w1:%\w+]] "w"
44 ; CHECK:     OpName [[u0:%\w+]] "u"
45 ; CHECK:     OpName [[u1:%\w+]] "u"
46 ; CHECK:     OpName [[v0:%\w+]] "v"
47 ; CHECK:     OpName [[v1:%\w+]] "v"
48 ; CHECK:     OpName [[v2:%\w+]] "v"
49 ; CHECK:     OpName [[v3:%\w+]] "v"
50 ; CHECK:     OpName [[v4:%\w+]] "v"
51 ; CHECK:     OpName [[v5:%\w+]] "v"
52                OpName %x "x"
53                OpName %y "y"
54                OpName %z "z"
55                OpName %w "w"
56                OpName %u "u"
57                OpName %v "v"
58 
59 ; CHECK-DAG: OpDecorate [[x]] Location 2
60 ; CHECK-DAG: OpDecorate [[y]] Location 0
61 ; CHECK-DAG: OpDecorate [[z0]] Location 0
62 ; CHECK-DAG: OpDecorate [[z0]] Component 0
63 ; CHECK-DAG: OpDecorate [[z1]] Location 1
64 ; CHECK-DAG: OpDecorate [[z1]] Component 0
65 ; CHECK-DAG: OpDecorate [[z0]] Patch
66 ; CHECK-DAG: OpDecorate [[z1]] Patch
67 ; CHECK-DAG: OpDecorate [[w0]] Location 2
68 ; CHECK-DAG: OpDecorate [[w0]] Component 0
69 ; CHECK-DAG: OpDecorate [[w1]] Location 3
70 ; CHECK-DAG: OpDecorate [[w1]] Component 0
71 ; CHECK-DAG: OpDecorate [[w0]] Patch
72 ; CHECK-DAG: OpDecorate [[w1]] Patch
73 ; CHECK-DAG: OpDecorate [[u0]] Location 3
74 ; CHECK-DAG: OpDecorate [[u0]] Component 2
75 ; CHECK-DAG: OpDecorate [[u1]] Location 4
76 ; CHECK-DAG: OpDecorate [[u1]] Component 2
77 ; CHECK-DAG: OpDecorate [[v0]] Location 3
78 ; CHECK-DAG: OpDecorate [[v0]] Component 3
79 ; CHECK-DAG: OpDecorate [[v1]] Location 4
80 ; CHECK-DAG: OpDecorate [[v1]] Component 3
81 ; CHECK-DAG: OpDecorate [[v2]] Location 5
82 ; CHECK-DAG: OpDecorate [[v2]] Component 3
83 ; CHECK-DAG: OpDecorate [[v3]] Location 6
84 ; CHECK-DAG: OpDecorate [[v3]] Component 3
85 ; CHECK-DAG: OpDecorate [[v4]] Location 7
86 ; CHECK-DAG: OpDecorate [[v4]] Component 3
87 ; CHECK-DAG: OpDecorate [[v5]] Location 8
88 ; CHECK-DAG: OpDecorate [[v5]] Component 3
89                OpDecorate %z Patch
90                OpDecorate %w Patch
91                OpDecorate %z Location 0
92                OpDecorate %x Location 2
93                OpDecorate %v Location 3
94                OpDecorate %v Component 3
95                OpDecorate %y Location 0
96                OpDecorate %w Location 2
97                OpDecorate %u Location 3
98                OpDecorate %u Component 2
99 
100        %uint = OpTypeInt 32 0
101      %uint_1 = OpConstant %uint 1
102      %uint_2 = OpConstant %uint 2
103      %uint_3 = OpConstant %uint 3
104      %uint_4 = OpConstant %uint 4
105 %_arr_uint_uint_2 = OpTypeArray %uint %uint_2
106 %_ptr_Output__arr_uint_uint_2 = OpTypePointer Output %_arr_uint_uint_2
107 %_ptr_Input__arr_uint_uint_2 = OpTypePointer Input %_arr_uint_uint_2
108 %_ptr_Input_uint = OpTypePointer Input %uint
109 %_ptr_Output_uint = OpTypePointer Output %uint
110 %_arr_arr_uint_uint_2_3 = OpTypeArray %_arr_uint_uint_2 %uint_3
111 %_ptr_Input__arr_arr_uint_uint_2_3 = OpTypePointer Input %_arr_arr_uint_uint_2_3
112 %_arr_arr_arr_uint_uint_2_3_4 = OpTypeArray %_arr_arr_uint_uint_2_3 %uint_4
113 %_ptr_Output__arr_arr_arr_uint_uint_2_3_4 = OpTypePointer Output %_arr_arr_arr_uint_uint_2_3_4
114 %_ptr_Output__arr_arr_uint_uint_2_3 = OpTypePointer Output %_arr_arr_uint_uint_2_3
115           %z = OpVariable %_ptr_Output__arr_uint_uint_2 Output
116           %x = OpVariable %_ptr_Output__arr_uint_uint_2 Output
117           %y = OpVariable %_ptr_Input__arr_uint_uint_2 Input
118           %w = OpVariable %_ptr_Input__arr_uint_uint_2 Input
119           %u = OpVariable %_ptr_Input__arr_arr_uint_uint_2_3 Input
120           %v = OpVariable %_ptr_Output__arr_arr_arr_uint_uint_2_3_4 Output
121 
122 ; CHECK-DAG:  [[x]] = OpVariable %_ptr_Output__arr_uint_uint_2 Output
123 ; CHECK-DAG:  [[y]] = OpVariable %_ptr_Input__arr_uint_uint_2 Input
124 ; CHECK-DAG: [[z0]] = OpVariable %_ptr_Output_uint Output
125 ; CHECK-DAG: [[z1]] = OpVariable %_ptr_Output_uint Output
126 ; CHECK-DAG: [[w0]] = OpVariable %_ptr_Input_uint Input
127 ; CHECK-DAG: [[w1]] = OpVariable %_ptr_Input_uint Input
128 ; CHECK-DAG: [[u0]] = OpVariable %_ptr_Input__arr_uint_uint_3 Input
129 ; CHECK-DAG: [[u1]] = OpVariable %_ptr_Input__arr_uint_uint_3 Input
130 ; CHECK-DAG: [[v0]] = OpVariable %_ptr_Output__arr_uint_uint_4 Output
131 ; CHECK-DAG: [[v1]] = OpVariable %_ptr_Output__arr_uint_uint_4 Output
132 ; CHECK-DAG: [[v2]] = OpVariable %_ptr_Output__arr_uint_uint_4 Output
133 ; CHECK-DAG: [[v3]] = OpVariable %_ptr_Output__arr_uint_uint_4 Output
134 ; CHECK-DAG: [[v4]] = OpVariable %_ptr_Output__arr_uint_uint_4 Output
135 ; CHECK-DAG: [[v5]] = OpVariable %_ptr_Output__arr_uint_uint_4 Output
136 
137      %void   = OpTypeVoid
138      %void_f = OpTypeFunction %void
139      %func   = OpFunction %void None %void_f
140      %label  = OpLabel
141 
142 ; CHECK: [[w0_value:%\w+]] = OpLoad %uint [[w0]]
143 ; CHECK: [[w1_value:%\w+]] = OpLoad %uint [[w1]]
144 ; CHECK:  [[w_value:%\w+]] = OpCompositeConstruct %_arr_uint_uint_2 [[w0_value]] [[w1_value]]
145 ; CHECK:       [[w0:%\w+]] = OpCompositeExtract %uint [[w_value]] 0
146 ; CHECK:                     OpStore [[z0]] [[w0]]
147 ; CHECK:       [[w1:%\w+]] = OpCompositeExtract %uint [[w_value]] 1
148 ; CHECK:                     OpStore [[z1]] [[w1]]
149     %w_value = OpLoad %_arr_uint_uint_2 %w
150                OpStore %z %w_value
151 
152 ; CHECK: [[u00_ptr:%\w+]] = OpAccessChain %_ptr_Input_uint [[u0]] %uint_0
153 ; CHECK:     [[u00:%\w+]] = OpLoad %uint [[u00_ptr]]
154 ; CHECK: [[u10_ptr:%\w+]] = OpAccessChain %_ptr_Input_uint [[u1]] %uint_0
155 ; CHECK:     [[u10:%\w+]] = OpLoad %uint [[u10_ptr]]
156 ; CHECK: [[u01_ptr:%\w+]] = OpAccessChain %_ptr_Input_uint [[u0]] %uint_1
157 ; CHECK:     [[u01:%\w+]] = OpLoad %uint [[u01_ptr]]
158 ; CHECK: [[u11_ptr:%\w+]] = OpAccessChain %_ptr_Input_uint [[u1]] %uint_1
159 ; CHECK:     [[u11:%\w+]] = OpLoad %uint [[u11_ptr]]
160 ; CHECK: [[u02_ptr:%\w+]] = OpAccessChain %_ptr_Input_uint [[u0]] %uint_2
161 ; CHECK:     [[u02:%\w+]] = OpLoad %uint [[u02_ptr]]
162 ; CHECK: [[u12_ptr:%\w+]] = OpAccessChain %_ptr_Input_uint [[u1]] %uint_2
163 ; CHECK:     [[u12:%\w+]] = OpLoad %uint [[u12_ptr]]
164 
165 ; CHECK-DAG: [[u0_val:%\w+]] = OpCompositeConstruct %_arr_uint_uint_2 [[u00]] [[u10]]
166 ; CHECK-DAG: [[u1_val:%\w+]] = OpCompositeConstruct %_arr_uint_uint_2 [[u01]] [[u11]]
167 ; CHECK-DAG: [[u2_val:%\w+]] = OpCompositeConstruct %_arr_uint_uint_2 [[u02]] [[u12]]
168 
169 ; CHECK: [[u_val:%\w+]] = OpCompositeConstruct %_arr__arr_uint_uint_2_uint_3 [[u0_val]] [[u1_val]] [[u2_val]]
170 
171 ; CHECK: [[ptr:%\w+]] = OpAccessChain %_ptr_Output_uint [[v0]] %uint_1
172 ; CHECK: [[val:%\w+]] = OpCompositeExtract %uint [[u_val]] 0 0
173 ; CHECK:                OpStore [[ptr]] [[val]]
174 ; CHECK: [[ptr:%\w+]] = OpAccessChain %_ptr_Output_uint [[v1]] %uint_1
175 ; CHECK: [[val:%\w+]] = OpCompositeExtract %uint [[u_val]] 0 1
176 ; CHECK:                OpStore [[ptr]] [[val]]
177 ; CHECK: [[ptr:%\w+]] = OpAccessChain %_ptr_Output_uint [[v2]] %uint_1
178 ; CHECK: [[val:%\w+]] = OpCompositeExtract %uint [[u_val]] 1 0
179 ; CHECK:                OpStore [[ptr]] [[val]]
180 ; CHECK: [[ptr:%\w+]] = OpAccessChain %_ptr_Output_uint [[v3]] %uint_1
181 ; CHECK: [[val:%\w+]] = OpCompositeExtract %uint [[u_val]] 1 1
182 ; CHECK:                OpStore [[ptr]] [[val]]
183 ; CHECK: [[ptr:%\w+]] = OpAccessChain %_ptr_Output_uint [[v4]] %uint_1
184 ; CHECK: [[val:%\w+]] = OpCompositeExtract %uint [[u_val]] 2 0
185 ; CHECK:                OpStore [[ptr]] [[val]]
186 ; CHECK: [[ptr:%\w+]] = OpAccessChain %_ptr_Output_uint [[v5]] %uint_1
187 ; CHECK: [[val:%\w+]] = OpCompositeExtract %uint [[u_val]] 2 1
188 ; CHECK:                OpStore [[ptr]] [[val]]
189      %v_ptr  = OpAccessChain %_ptr_Output__arr_arr_uint_uint_2_3 %v %uint_1
190      %u_val  = OpLoad %_arr_arr_uint_uint_2_3 %u
191                OpStore %v_ptr %u_val
192 
193                OpReturn
194                OpFunctionEnd
195   )";
196 
197   SinglePassRunAndMatch<InterfaceVariableScalarReplacement>(spirv, true);
198 }
199 
TEST_F(InterfaceVariableScalarReplacementTest,CheckPatchDecorationPreservation)200 TEST_F(InterfaceVariableScalarReplacementTest,
201        CheckPatchDecorationPreservation) {
202   // Make sure scalars for the variables with the extra arrayness have the extra
203   // arrayness after running the pass while others do not have it.
204   // Only "y" does not have the extra arrayness in the following SPIR-V.
205   const std::string spirv = R"(
206                OpCapability Shader
207                OpCapability Tessellation
208                OpMemoryModel Logical GLSL450
209                OpEntryPoint TessellationEvaluation %func "shader" %x %y %z %w
210                OpDecorate %z Patch
211                OpDecorate %w Patch
212                OpDecorate %z Location 0
213                OpDecorate %x Location 2
214                OpDecorate %y Location 0
215                OpDecorate %w Location 1
216                OpName %x "x"
217                OpName %y "y"
218                OpName %z "z"
219                OpName %w "w"
220 
221   ; CHECK:     OpName [[y:%\w+]] "y"
222   ; CHECK-NOT: OpName {{%\w+}} "y"
223   ; CHECK-DAG: OpName [[z0:%\w+]] "z"
224   ; CHECK-DAG: OpName [[z1:%\w+]] "z"
225   ; CHECK-DAG: OpName [[w0:%\w+]] "w"
226   ; CHECK-DAG: OpName [[w1:%\w+]] "w"
227   ; CHECK-DAG: OpName [[x0:%\w+]] "x"
228   ; CHECK-DAG: OpName [[x1:%\w+]] "x"
229 
230        %uint = OpTypeInt 32 0
231      %uint_2 = OpConstant %uint 2
232 %_arr_uint_uint_2 = OpTypeArray %uint %uint_2
233 %_ptr_Output__arr_uint_uint_2 = OpTypePointer Output %_arr_uint_uint_2
234 %_ptr_Input__arr_uint_uint_2 = OpTypePointer Input %_arr_uint_uint_2
235           %z = OpVariable %_ptr_Output__arr_uint_uint_2 Output
236           %x = OpVariable %_ptr_Output__arr_uint_uint_2 Output
237           %y = OpVariable %_ptr_Input__arr_uint_uint_2 Input
238           %w = OpVariable %_ptr_Input__arr_uint_uint_2 Input
239 
240   ; CHECK-DAG: [[y]] = OpVariable %_ptr_Input__arr_uint_uint_2 Input
241   ; CHECK-DAG: [[z0]] = OpVariable %_ptr_Output_uint Output
242   ; CHECK-DAG: [[z1]] = OpVariable %_ptr_Output_uint Output
243   ; CHECK-DAG: [[w0]] = OpVariable %_ptr_Input_uint Input
244   ; CHECK-DAG: [[w1]] = OpVariable %_ptr_Input_uint Input
245   ; CHECK-DAG: [[x0]] = OpVariable %_ptr_Output_uint Output
246   ; CHECK-DAG: [[x1]] = OpVariable %_ptr_Output_uint Output
247 
248      %void   = OpTypeVoid
249      %void_f = OpTypeFunction %void
250      %func   = OpFunction %void None %void_f
251      %label  = OpLabel
252                OpReturn
253                OpFunctionEnd
254   )";
255 
256   SinglePassRunAndMatch<InterfaceVariableScalarReplacement>(spirv, true);
257 }
258 
TEST_F(InterfaceVariableScalarReplacementTest,CheckEntryPointInterfaceOperands)259 TEST_F(InterfaceVariableScalarReplacementTest,
260        CheckEntryPointInterfaceOperands) {
261   const std::string spirv = R"(
262                OpCapability Shader
263                OpCapability Tessellation
264                OpMemoryModel Logical GLSL450
265                OpEntryPoint TessellationEvaluation %tess "tess" %x %y
266                OpEntryPoint Vertex %vert "vert" %w
267                OpDecorate %z Location 0
268                OpDecorate %x Location 2
269                OpDecorate %y Location 0
270                OpDecorate %w Location 1
271                OpName %x "x"
272                OpName %y "y"
273                OpName %z "z"
274                OpName %w "w"
275 
276   ; CHECK:     OpName [[y:%\w+]] "y"
277   ; CHECK-NOT: OpName {{%\w+}} "y"
278   ; CHECK-DAG: OpName [[x0:%\w+]] "x"
279   ; CHECK-DAG: OpName [[x1:%\w+]] "x"
280   ; CHECK-DAG: OpName [[w0:%\w+]] "w"
281   ; CHECK-DAG: OpName [[w1:%\w+]] "w"
282   ; CHECK-DAG: OpName [[z:%\w+]] "z"
283   ; CHECK-NOT: OpName {{%\w+}} "z"
284 
285        %uint = OpTypeInt 32 0
286      %uint_2 = OpConstant %uint 2
287 %_arr_uint_uint_2 = OpTypeArray %uint %uint_2
288 %_ptr_Output__arr_uint_uint_2 = OpTypePointer Output %_arr_uint_uint_2
289 %_ptr_Input__arr_uint_uint_2 = OpTypePointer Input %_arr_uint_uint_2
290           %z = OpVariable %_ptr_Output__arr_uint_uint_2 Output
291           %x = OpVariable %_ptr_Output__arr_uint_uint_2 Output
292           %y = OpVariable %_ptr_Input__arr_uint_uint_2 Input
293           %w = OpVariable %_ptr_Input__arr_uint_uint_2 Input
294 
295   ; CHECK-DAG: [[y]] = OpVariable %_ptr_Input__arr_uint_uint_2 Input
296   ; CHECK-DAG: [[z]] = OpVariable %_ptr_Output__arr_uint_uint_2 Output
297   ; CHECK-DAG: [[w0]] = OpVariable %_ptr_Input_uint Input
298   ; CHECK-DAG: [[w1]] = OpVariable %_ptr_Input_uint Input
299   ; CHECK-DAG: [[x0]] = OpVariable %_ptr_Output_uint Output
300   ; CHECK-DAG: [[x1]] = OpVariable %_ptr_Output_uint Output
301 
302      %void   = OpTypeVoid
303      %void_f = OpTypeFunction %void
304      %tess   = OpFunction %void None %void_f
305      %bb0    = OpLabel
306                OpReturn
307                OpFunctionEnd
308      %vert   = OpFunction %void None %void_f
309      %bb1    = OpLabel
310                OpReturn
311                OpFunctionEnd
312   )";
313 
314   SinglePassRunAndMatch<InterfaceVariableScalarReplacement>(spirv, true);
315 }
316 
317 class InterfaceVarSROAErrorTest : public PassTest<::testing::Test> {
318  public:
InterfaceVarSROAErrorTest()319   InterfaceVarSROAErrorTest()
320       : consumer_([this](spv_message_level_t level, const char*,
321                          const spv_position_t& position, const char* message) {
322           if (!error_message_.empty()) error_message_ += "\n";
323           switch (level) {
324             case SPV_MSG_FATAL:
325             case SPV_MSG_INTERNAL_ERROR:
326             case SPV_MSG_ERROR:
327               error_message_ += "ERROR";
328               break;
329             case SPV_MSG_WARNING:
330               error_message_ += "WARNING";
331               break;
332             case SPV_MSG_INFO:
333               error_message_ += "INFO";
334               break;
335             case SPV_MSG_DEBUG:
336               error_message_ += "DEBUG";
337               break;
338           }
339           error_message_ +=
340               ": " + std::to_string(position.index) + ": " + message;
341         }) {}
342 
RunPass(const std::string & text)343   Pass::Status RunPass(const std::string& text) {
344     std::unique_ptr<IRContext> context_ =
345         spvtools::BuildModule(SPV_ENV_UNIVERSAL_1_2, consumer_, text);
346     if (!context_.get()) return Pass::Status::Failure;
347 
348     PassManager manager;
349     manager.SetMessageConsumer(consumer_);
350     manager.AddPass<InterfaceVariableScalarReplacement>();
351 
352     return manager.Run(context_.get());
353   }
354 
GetErrorMessage() const355   std::string GetErrorMessage() const { return error_message_; }
356 
TearDown()357   void TearDown() override { error_message_.clear(); }
358 
359  private:
360   spvtools::MessageConsumer consumer_;
361   std::string error_message_;
362 };
363 
TEST_F(InterfaceVarSROAErrorTest,CheckConflictOfExtraArraynessBetweenEntries)364 TEST_F(InterfaceVarSROAErrorTest, CheckConflictOfExtraArraynessBetweenEntries) {
365   const std::string spirv = R"(
366                OpCapability Shader
367                OpCapability Tessellation
368                OpMemoryModel Logical GLSL450
369                OpEntryPoint TessellationControl %tess "tess" %x %y %z
370                OpEntryPoint Vertex %vert "vert" %z %w
371                OpDecorate %z Location 0
372                OpDecorate %x Location 2
373                OpDecorate %y Location 0
374                OpDecorate %w Location 1
375                OpName %x "x"
376                OpName %y "y"
377                OpName %z "z"
378                OpName %w "w"
379        %uint = OpTypeInt 32 0
380      %uint_2 = OpConstant %uint 2
381 %_arr_uint_uint_2 = OpTypeArray %uint %uint_2
382 %_ptr_Output__arr_uint_uint_2 = OpTypePointer Output %_arr_uint_uint_2
383 %_ptr_Input__arr_uint_uint_2 = OpTypePointer Input %_arr_uint_uint_2
384           %z = OpVariable %_ptr_Output__arr_uint_uint_2 Output
385           %x = OpVariable %_ptr_Output__arr_uint_uint_2 Output
386           %y = OpVariable %_ptr_Input__arr_uint_uint_2 Input
387           %w = OpVariable %_ptr_Input__arr_uint_uint_2 Input
388      %void   = OpTypeVoid
389      %void_f = OpTypeFunction %void
390      %tess   = OpFunction %void None %void_f
391      %bb0    = OpLabel
392                OpReturn
393                OpFunctionEnd
394      %vert   = OpFunction %void None %void_f
395      %bb1    = OpLabel
396                OpReturn
397                OpFunctionEnd
398   )";
399 
400   EXPECT_EQ(RunPass(spirv), Pass::Status::Failure);
401   const char expected_error[] =
402       "ERROR: 0: A variable is arrayed for an entry point but it is not "
403       "arrayed for another entry point\n"
404       "  %z = OpVariable %_ptr_Output__arr_uint_uint_2 Output";
405   EXPECT_STREQ(GetErrorMessage().c_str(), expected_error);
406 }
407 
408 }  // namespace
409 }  // namespace opt
410 }  // namespace spvtools
411