1fd4e5da5Sopenharmony_ci// Copyright (c) 2021 ZHOU He
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci#include "test/opt/assembly_builder.h"
16fd4e5da5Sopenharmony_ci#include "test/opt/pass_fixture.h"
17fd4e5da5Sopenharmony_ci#include "test/opt/pass_utils.h"
18fd4e5da5Sopenharmony_ci
19fd4e5da5Sopenharmony_cinamespace spvtools {
20fd4e5da5Sopenharmony_cinamespace opt {
21fd4e5da5Sopenharmony_cinamespace {
22fd4e5da5Sopenharmony_ci
23fd4e5da5Sopenharmony_ciusing RemoveUnusedInterfaceVariablesTest = PassTest<::testing::Test>;
24fd4e5da5Sopenharmony_ci
25fd4e5da5Sopenharmony_cistatic const std::string expected = R"(OpCapability Shader
26fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
27fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET
28fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET_0
29fd4e5da5Sopenharmony_ciOpExecutionMode %_Z5func1v OriginUpperLeft
30fd4e5da5Sopenharmony_ciOpExecutionMode %_Z5func2v OriginUpperLeft
31fd4e5da5Sopenharmony_ciOpSource HLSL 630
32fd4e5da5Sopenharmony_ciOpName %type_cba "type.cba"
33fd4e5da5Sopenharmony_ciOpMemberName %type_cba 0 "color"
34fd4e5da5Sopenharmony_ciOpName %cba "cba"
35fd4e5da5Sopenharmony_ciOpName %out_var_SV_TARGET "out.var.SV_TARGET"
36fd4e5da5Sopenharmony_ciOpName %out_var_SV_TARGET_0 "out.var.SV_TARGET"
37fd4e5da5Sopenharmony_ciOpName %_Z5func1v "_Z5func1v"
38fd4e5da5Sopenharmony_ciOpName %_Z5func2v "_Z5func2v"
39fd4e5da5Sopenharmony_ciOpDecorate %out_var_SV_TARGET Location 0
40fd4e5da5Sopenharmony_ciOpDecorate %out_var_SV_TARGET_0 Location 0
41fd4e5da5Sopenharmony_ciOpDecorate %cba DescriptorSet 0
42fd4e5da5Sopenharmony_ciOpDecorate %cba Binding 0
43fd4e5da5Sopenharmony_ciOpMemberDecorate %type_cba 0 Offset 0
44fd4e5da5Sopenharmony_ciOpDecorate %type_cba Block
45fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
46fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
47fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
48fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
49fd4e5da5Sopenharmony_ci%type_cba = OpTypeStruct %v4float
50fd4e5da5Sopenharmony_ci%_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba
51fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
52fd4e5da5Sopenharmony_ci%void = OpTypeVoid
53fd4e5da5Sopenharmony_ci%14 = OpTypeFunction %void
54fd4e5da5Sopenharmony_ci%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
55fd4e5da5Sopenharmony_ci%cba = OpVariable %_ptr_Uniform_type_cba Uniform
56fd4e5da5Sopenharmony_ci%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
57fd4e5da5Sopenharmony_ci%out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output
58fd4e5da5Sopenharmony_ci%_Z5func1v = OpFunction %void None %14
59fd4e5da5Sopenharmony_ci%16 = OpLabel
60fd4e5da5Sopenharmony_ci%17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
61fd4e5da5Sopenharmony_ci%18 = OpLoad %v4float %17
62fd4e5da5Sopenharmony_ciOpStore %out_var_SV_TARGET %18
63fd4e5da5Sopenharmony_ciOpReturn
64fd4e5da5Sopenharmony_ciOpFunctionEnd
65fd4e5da5Sopenharmony_ci%_Z5func2v = OpFunction %void None %14
66fd4e5da5Sopenharmony_ci%19 = OpLabel
67fd4e5da5Sopenharmony_ci%20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
68fd4e5da5Sopenharmony_ci%21 = OpLoad %v4float %20
69fd4e5da5Sopenharmony_ciOpStore %out_var_SV_TARGET_0 %21
70fd4e5da5Sopenharmony_ciOpReturn
71fd4e5da5Sopenharmony_ciOpFunctionEnd
72fd4e5da5Sopenharmony_ci)";
73fd4e5da5Sopenharmony_ci
74fd4e5da5Sopenharmony_ciTEST_F(RemoveUnusedInterfaceVariablesTest, RemoveUnusedVariable) {
75fd4e5da5Sopenharmony_ci  const std::string text = R"(OpCapability Shader
76fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
77fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET %out_var_SV_TARGET_0
78fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET %out_var_SV_TARGET_0
79fd4e5da5Sopenharmony_ciOpExecutionMode %_Z5func1v OriginUpperLeft
80fd4e5da5Sopenharmony_ciOpExecutionMode %_Z5func2v OriginUpperLeft
81fd4e5da5Sopenharmony_ciOpSource HLSL 630
82fd4e5da5Sopenharmony_ciOpName %type_cba "type.cba"
83fd4e5da5Sopenharmony_ciOpMemberName %type_cba 0 "color"
84fd4e5da5Sopenharmony_ciOpName %cba "cba"
85fd4e5da5Sopenharmony_ciOpName %out_var_SV_TARGET "out.var.SV_TARGET"
86fd4e5da5Sopenharmony_ciOpName %out_var_SV_TARGET_0 "out.var.SV_TARGET"
87fd4e5da5Sopenharmony_ciOpName %_Z5func1v "_Z5func1v"
88fd4e5da5Sopenharmony_ciOpName %_Z5func2v "_Z5func2v"
89fd4e5da5Sopenharmony_ciOpDecorate %out_var_SV_TARGET Location 0
90fd4e5da5Sopenharmony_ciOpDecorate %out_var_SV_TARGET_0 Location 0
91fd4e5da5Sopenharmony_ciOpDecorate %cba DescriptorSet 0
92fd4e5da5Sopenharmony_ciOpDecorate %cba Binding 0
93fd4e5da5Sopenharmony_ciOpMemberDecorate %type_cba 0 Offset 0
94fd4e5da5Sopenharmony_ciOpDecorate %type_cba Block
95fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
96fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
97fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
98fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
99fd4e5da5Sopenharmony_ci%type_cba = OpTypeStruct %v4float
100fd4e5da5Sopenharmony_ci%_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba
101fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
102fd4e5da5Sopenharmony_ci%void = OpTypeVoid
103fd4e5da5Sopenharmony_ci%14 = OpTypeFunction %void
104fd4e5da5Sopenharmony_ci%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
105fd4e5da5Sopenharmony_ci%cba = OpVariable %_ptr_Uniform_type_cba Uniform
106fd4e5da5Sopenharmony_ci%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
107fd4e5da5Sopenharmony_ci%out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output
108fd4e5da5Sopenharmony_ci%_Z5func1v = OpFunction %void None %14
109fd4e5da5Sopenharmony_ci%16 = OpLabel
110fd4e5da5Sopenharmony_ci%17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
111fd4e5da5Sopenharmony_ci%18 = OpLoad %v4float %17
112fd4e5da5Sopenharmony_ciOpStore %out_var_SV_TARGET %18
113fd4e5da5Sopenharmony_ciOpReturn
114fd4e5da5Sopenharmony_ciOpFunctionEnd
115fd4e5da5Sopenharmony_ci%_Z5func2v = OpFunction %void None %14
116fd4e5da5Sopenharmony_ci%19 = OpLabel
117fd4e5da5Sopenharmony_ci%20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
118fd4e5da5Sopenharmony_ci%21 = OpLoad %v4float %20
119fd4e5da5Sopenharmony_ciOpStore %out_var_SV_TARGET_0 %21
120fd4e5da5Sopenharmony_ciOpReturn
121fd4e5da5Sopenharmony_ciOpFunctionEnd
122fd4e5da5Sopenharmony_ci)";
123fd4e5da5Sopenharmony_ci
124fd4e5da5Sopenharmony_ci  SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected,
125fd4e5da5Sopenharmony_ci                                                            true, true);
126fd4e5da5Sopenharmony_ci}
127fd4e5da5Sopenharmony_ci
128fd4e5da5Sopenharmony_ciTEST_F(RemoveUnusedInterfaceVariablesTest, FixMissingVariable) {
129fd4e5da5Sopenharmony_ci  const std::string text = R"(OpCapability Shader
130fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
131fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %_Z5func1v "_Z5func1v"
132fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %_Z5func2v "_Z5func2v"
133fd4e5da5Sopenharmony_ciOpExecutionMode %_Z5func1v OriginUpperLeft
134fd4e5da5Sopenharmony_ciOpExecutionMode %_Z5func2v OriginUpperLeft
135fd4e5da5Sopenharmony_ciOpSource HLSL 630
136fd4e5da5Sopenharmony_ciOpName %type_cba "type.cba"
137fd4e5da5Sopenharmony_ciOpMemberName %type_cba 0 "color"
138fd4e5da5Sopenharmony_ciOpName %cba "cba"
139fd4e5da5Sopenharmony_ciOpName %out_var_SV_TARGET "out.var.SV_TARGET"
140fd4e5da5Sopenharmony_ciOpName %out_var_SV_TARGET_0 "out.var.SV_TARGET"
141fd4e5da5Sopenharmony_ciOpName %_Z5func1v "_Z5func1v"
142fd4e5da5Sopenharmony_ciOpName %_Z5func2v "_Z5func2v"
143fd4e5da5Sopenharmony_ciOpDecorate %out_var_SV_TARGET Location 0
144fd4e5da5Sopenharmony_ciOpDecorate %out_var_SV_TARGET_0 Location 0
145fd4e5da5Sopenharmony_ciOpDecorate %cba DescriptorSet 0
146fd4e5da5Sopenharmony_ciOpDecorate %cba Binding 0
147fd4e5da5Sopenharmony_ciOpMemberDecorate %type_cba 0 Offset 0
148fd4e5da5Sopenharmony_ciOpDecorate %type_cba Block
149fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
150fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
151fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
152fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
153fd4e5da5Sopenharmony_ci%type_cba = OpTypeStruct %v4float
154fd4e5da5Sopenharmony_ci%_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba
155fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
156fd4e5da5Sopenharmony_ci%void = OpTypeVoid
157fd4e5da5Sopenharmony_ci%14 = OpTypeFunction %void
158fd4e5da5Sopenharmony_ci%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
159fd4e5da5Sopenharmony_ci%cba = OpVariable %_ptr_Uniform_type_cba Uniform
160fd4e5da5Sopenharmony_ci%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
161fd4e5da5Sopenharmony_ci%out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output
162fd4e5da5Sopenharmony_ci%_Z5func1v = OpFunction %void None %14
163fd4e5da5Sopenharmony_ci%16 = OpLabel
164fd4e5da5Sopenharmony_ci%17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
165fd4e5da5Sopenharmony_ci%18 = OpLoad %v4float %17
166fd4e5da5Sopenharmony_ciOpStore %out_var_SV_TARGET %18
167fd4e5da5Sopenharmony_ciOpReturn
168fd4e5da5Sopenharmony_ciOpFunctionEnd
169fd4e5da5Sopenharmony_ci%_Z5func2v = OpFunction %void None %14
170fd4e5da5Sopenharmony_ci%19 = OpLabel
171fd4e5da5Sopenharmony_ci%20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
172fd4e5da5Sopenharmony_ci%21 = OpLoad %v4float %20
173fd4e5da5Sopenharmony_ciOpStore %out_var_SV_TARGET_0 %21
174fd4e5da5Sopenharmony_ciOpReturn
175fd4e5da5Sopenharmony_ciOpFunctionEnd
176fd4e5da5Sopenharmony_ci)";
177fd4e5da5Sopenharmony_ci
178fd4e5da5Sopenharmony_ci  SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected,
179fd4e5da5Sopenharmony_ci                                                            true, true);
180fd4e5da5Sopenharmony_ci}
181fd4e5da5Sopenharmony_ci
182fd4e5da5Sopenharmony_ci}  // namespace
183fd4e5da5Sopenharmony_ci}  // namespace opt
184fd4e5da5Sopenharmony_ci}  // namespace spvtools
185