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