1 // Copyright (c) 2021 ZHOU He
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 "gmock/gmock.h"
16 #include "test/opt/assembly_builder.h"
17 #include "test/opt/pass_fixture.h"
18 #include "test/opt/pass_utils.h"
19
20 namespace spvtools {
21 namespace opt {
22 namespace {
23
24 using RemoveUnusedInterfaceVariablesTest = PassTest<::testing::Test>;
25
26 static const std::string expected = R"(OpCapability Shader
27 OpMemoryModel Logical GLSL450
28 OpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET
29 OpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET_0
30 OpExecutionMode %_Z5func1v OriginUpperLeft
31 OpExecutionMode %_Z5func2v OriginUpperLeft
32 OpSource HLSL 630
33 OpName %type_cba "type.cba"
34 OpMemberName %type_cba 0 "color"
35 OpName %cba "cba"
36 OpName %out_var_SV_TARGET "out.var.SV_TARGET"
37 OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET"
38 OpName %_Z5func1v "_Z5func1v"
39 OpName %_Z5func2v "_Z5func2v"
40 OpDecorate %out_var_SV_TARGET Location 0
41 OpDecorate %out_var_SV_TARGET_0 Location 0
42 OpDecorate %cba DescriptorSet 0
43 OpDecorate %cba Binding 0
44 OpMemberDecorate %type_cba 0 Offset 0
45 OpDecorate %type_cba Block
46 %int = OpTypeInt 32 1
47 %int_0 = OpConstant %int 0
48 %float = OpTypeFloat 32
49 %v4float = OpTypeVector %float 4
50 %type_cba = OpTypeStruct %v4float
51 %_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba
52 %_ptr_Output_v4float = OpTypePointer Output %v4float
53 %void = OpTypeVoid
54 %14 = OpTypeFunction %void
55 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
56 %cba = OpVariable %_ptr_Uniform_type_cba Uniform
57 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
58 %out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output
59 %_Z5func1v = OpFunction %void None %14
60 %16 = OpLabel
61 %17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
62 %18 = OpLoad %v4float %17
63 OpStore %out_var_SV_TARGET %18
64 OpReturn
65 OpFunctionEnd
66 %_Z5func2v = OpFunction %void None %14
67 %19 = OpLabel
68 %20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
69 %21 = OpLoad %v4float %20
70 OpStore %out_var_SV_TARGET_0 %21
71 OpReturn
72 OpFunctionEnd
73 )";
74
TEST_F(RemoveUnusedInterfaceVariablesTest,RemoveUnusedVariable)75 TEST_F(RemoveUnusedInterfaceVariablesTest, RemoveUnusedVariable) {
76 const std::string text = R"(OpCapability Shader
77 OpMemoryModel Logical GLSL450
78 OpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET %out_var_SV_TARGET_0
79 OpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET %out_var_SV_TARGET_0
80 OpExecutionMode %_Z5func1v OriginUpperLeft
81 OpExecutionMode %_Z5func2v OriginUpperLeft
82 OpSource HLSL 630
83 OpName %type_cba "type.cba"
84 OpMemberName %type_cba 0 "color"
85 OpName %cba "cba"
86 OpName %out_var_SV_TARGET "out.var.SV_TARGET"
87 OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET"
88 OpName %_Z5func1v "_Z5func1v"
89 OpName %_Z5func2v "_Z5func2v"
90 OpDecorate %out_var_SV_TARGET Location 0
91 OpDecorate %out_var_SV_TARGET_0 Location 0
92 OpDecorate %cba DescriptorSet 0
93 OpDecorate %cba Binding 0
94 OpMemberDecorate %type_cba 0 Offset 0
95 OpDecorate %type_cba Block
96 %int = OpTypeInt 32 1
97 %int_0 = OpConstant %int 0
98 %float = OpTypeFloat 32
99 %v4float = OpTypeVector %float 4
100 %type_cba = OpTypeStruct %v4float
101 %_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba
102 %_ptr_Output_v4float = OpTypePointer Output %v4float
103 %void = OpTypeVoid
104 %14 = OpTypeFunction %void
105 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
106 %cba = OpVariable %_ptr_Uniform_type_cba Uniform
107 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
108 %out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output
109 %_Z5func1v = OpFunction %void None %14
110 %16 = OpLabel
111 %17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
112 %18 = OpLoad %v4float %17
113 OpStore %out_var_SV_TARGET %18
114 OpReturn
115 OpFunctionEnd
116 %_Z5func2v = OpFunction %void None %14
117 %19 = OpLabel
118 %20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
119 %21 = OpLoad %v4float %20
120 OpStore %out_var_SV_TARGET_0 %21
121 OpReturn
122 OpFunctionEnd
123 )";
124
125 SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected,
126 true, true);
127 }
128
TEST_F(RemoveUnusedInterfaceVariablesTest,FixMissingVariable)129 TEST_F(RemoveUnusedInterfaceVariablesTest, FixMissingVariable) {
130 const std::string text = R"(OpCapability Shader
131 OpMemoryModel Logical GLSL450
132 OpEntryPoint Fragment %_Z5func1v "_Z5func1v"
133 OpEntryPoint Fragment %_Z5func2v "_Z5func2v"
134 OpExecutionMode %_Z5func1v OriginUpperLeft
135 OpExecutionMode %_Z5func2v OriginUpperLeft
136 OpSource HLSL 630
137 OpName %type_cba "type.cba"
138 OpMemberName %type_cba 0 "color"
139 OpName %cba "cba"
140 OpName %out_var_SV_TARGET "out.var.SV_TARGET"
141 OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET"
142 OpName %_Z5func1v "_Z5func1v"
143 OpName %_Z5func2v "_Z5func2v"
144 OpDecorate %out_var_SV_TARGET Location 0
145 OpDecorate %out_var_SV_TARGET_0 Location 0
146 OpDecorate %cba DescriptorSet 0
147 OpDecorate %cba Binding 0
148 OpMemberDecorate %type_cba 0 Offset 0
149 OpDecorate %type_cba Block
150 %int = OpTypeInt 32 1
151 %int_0 = OpConstant %int 0
152 %float = OpTypeFloat 32
153 %v4float = OpTypeVector %float 4
154 %type_cba = OpTypeStruct %v4float
155 %_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba
156 %_ptr_Output_v4float = OpTypePointer Output %v4float
157 %void = OpTypeVoid
158 %14 = OpTypeFunction %void
159 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
160 %cba = OpVariable %_ptr_Uniform_type_cba Uniform
161 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
162 %out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output
163 %_Z5func1v = OpFunction %void None %14
164 %16 = OpLabel
165 %17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
166 %18 = OpLoad %v4float %17
167 OpStore %out_var_SV_TARGET %18
168 OpReturn
169 OpFunctionEnd
170 %_Z5func2v = OpFunction %void None %14
171 %19 = OpLabel
172 %20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
173 %21 = OpLoad %v4float %20
174 OpStore %out_var_SV_TARGET_0 %21
175 OpReturn
176 OpFunctionEnd
177 )";
178
179 SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected,
180 true, true);
181 }
182
183 } // namespace
184 } // namespace opt
185 } // namespace spvtools
186