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