Debian Codegraph @

test/opt/strip_nonsemantic_info_test.cpp

raw
1 // Copyright (c) 2018 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 <string>
16
17 #include "gmock/gmock.h"
18 #include "spirv-tools/optimizer.hpp"
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 StripNonSemanticInfoTest = PassTest<::testing::Test>;
27
28 // This test acts as an end-to-end code example on how to strip
29 // reflection info from a SPIR-V module. Use this code pattern
30 // when you have compiled HLSL code with Glslang or DXC using
31 // option -fhlsl_functionality1 to insert reflection information,
32 // but then want to filter out the extra instructions before sending
33 // it to a driver that does not implement VK_GOOGLE_hlsl_functionality1.
34 TEST_F(StripNonSemanticInfoTest, StripReflectEnd2EndExample) {
35 // This is a non-sensical example, but exercises the instructions.
36 std::string before = R"(OpCapability Shader
37 OpCapability Linkage
38 OpExtension "SPV_GOOGLE_decorate_string"
39 OpExtension "SPV_GOOGLE_hlsl_functionality1"
40 OpMemoryModel Logical Simple
41 OpDecorateStringGOOGLE %float HlslSemanticGOOGLE "foobar"
42 OpDecorateStringGOOGLE %void HlslSemanticGOOGLE "my goodness"
43 %void = OpTypeVoid
44 %float = OpTypeFloat 32
45 )";
46 SpirvTools tools(SPV_ENV_UNIVERSAL_1_1);
47 std::vector<uint32_t> binary_in;
48 tools.Assemble(before, &binary_in);
49
50 // Instantiate the optimizer, and run the strip-nonsemantic-info
51 // pass over the |binary_in| module, and place the modified module
52 // into |binary_out|.
53 spvtools::Optimizer optimizer(SPV_ENV_UNIVERSAL_1_1);
54 optimizer.RegisterPass(spvtools::CreateStripNonSemanticInfoPass());
55 std::vector<uint32_t> binary_out;
56 optimizer.Run(binary_in.data(), binary_in.size(), &binary_out);
57
58 // Check results
59 std::string disassembly;
60 tools.Disassemble(binary_out.data(), binary_out.size(), &disassembly);
61 std::string after = R"(OpCapability Shader
62 OpCapability Linkage
63 OpMemoryModel Logical Simple
64 %void = OpTypeVoid
65 %float = OpTypeFloat 32
66 )";
67 EXPECT_THAT(disassembly, testing::Eq(after));
68 }
69
70 // This test is functionally the same as the end-to-end test above,
71 // but uses the test SinglePassRunAndCheck test fixture instead.
72 TEST_F(StripNonSemanticInfoTest, StripHlslSemantic) {
73 // This is a non-sensical example, but exercises the instructions.
74 std::string before = R"(OpCapability Shader
75 OpCapability Linkage
76 OpExtension "SPV_GOOGLE_decorate_string"
77 OpExtension "SPV_GOOGLE_hlsl_functionality1"
78 OpMemoryModel Logical Simple
79 OpDecorateStringGOOGLE %float HlslSemanticGOOGLE "foobar"
80 OpDecorateStringGOOGLE %void HlslSemanticGOOGLE "my goodness"
81 %void = OpTypeVoid
82 %float = OpTypeFloat 32
83 )";
84 std::string after = R"(OpCapability Shader
85 OpCapability Linkage
86 OpMemoryModel Logical Simple
87 %void = OpTypeVoid
88 %float = OpTypeFloat 32
89 )";
90
91 SinglePassRunAndCheck<StripNonSemanticInfoPass>(before, after, false);
92 }
93
94 TEST_F(StripNonSemanticInfoTest, StripHlslCounterBuffer) {
95 std::string before = R"(OpCapability Shader
96 OpCapability Linkage
97 OpExtension "SPV_GOOGLE_hlsl_functionality1"
98 OpMemoryModel Logical Simple
99 OpDecorateId %void HlslCounterBufferGOOGLE %float
100 %void = OpTypeVoid
101 %float = OpTypeFloat 32
102 )";
103 std::string after = R"(OpCapability Shader
104 OpCapability Linkage
105 OpMemoryModel Logical Simple
106 %void = OpTypeVoid
107 %float = OpTypeFloat 32
108 )";
109
110 SinglePassRunAndCheck<StripNonSemanticInfoPass>(before, after, false);
111 }
112
113 TEST_F(StripNonSemanticInfoTest, StripHlslSemanticOnMember) {
114 // This is a non-sensical example, but exercises the instructions.
115 std::string before = R"(OpCapability Shader
116 OpCapability Linkage
117 OpExtension "SPV_GOOGLE_decorate_string"
118 OpExtension "SPV_GOOGLE_hlsl_functionality1"
119 OpMemoryModel Logical Simple
120 OpMemberDecorateStringGOOGLE %struct 0 HlslSemanticGOOGLE "foobar"
121 %float = OpTypeFloat 32
122 %_struct_3 = OpTypeStruct %float
123 )";
124 std::string after = R"(OpCapability Shader
125 OpCapability Linkage
126 OpMemoryModel Logical Simple
127 %float = OpTypeFloat 32
128 %_struct_3 = OpTypeStruct %float
129 )";
130
131 SinglePassRunAndCheck<StripNonSemanticInfoPass>(before, after, false);
132 }
133
134 TEST_F(StripNonSemanticInfoTest, StripNonSemanticImport) {
135 std::string text = R"(
136 ; CHECK-NOT: OpExtension "SPV_KHR_non_semantic_info"
137 ; CHECK-NOT: OpExtInstImport
138 OpCapability Shader
139 OpCapability Linkage
140 OpExtension "SPV_KHR_non_semantic_info"
141 %ext = OpExtInstImport "NonSemantic.Test"
142 OpMemoryModel Logical GLSL450
143 )";
144
145 SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
146 }
147
148 TEST_F(StripNonSemanticInfoTest, StripNonSemanticGlobal) {
149 std::string text = R"(
150 ; CHECK-NOT: OpExtInst
151 OpCapability Shader
152 OpCapability Linkage
153 OpExtension "SPV_KHR_non_semantic_info"
154 %ext = OpExtInstImport "NonSemantic.Test"
155 OpMemoryModel Logical GLSL450
156 %void = OpTypeVoid
157 %1 = OpExtInst %void %ext 1
158 )";
159
160 SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
161 }
162
163 TEST_F(StripNonSemanticInfoTest, StripNonSemanticInFunction) {
164 std::string text = R"(
165 ; CHECK-NOT: OpExtInst
166 OpCapability Shader
167 OpCapability Linkage
168 OpExtension "SPV_KHR_non_semantic_info"
169 %ext = OpExtInstImport "NonSemantic.Test"
170 OpMemoryModel Logical GLSL450
171 %void = OpTypeVoid
172 %void_fn = OpTypeFunction %void
173 %foo = OpFunction %void None %void_fn
174 %entry = OpLabel
175 %1 = OpExtInst %void %ext 1 %foo
176 OpReturn
177 OpFunctionEnd
178 )";
179
180 SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
181 }
182
183 TEST_F(StripNonSemanticInfoTest, StripNonSemanticAfterFunction) {
184 std::string text = R"(
185 ; CHECK-NOT: OpExtInst
186 OpCapability Shader
187 OpCapability Linkage
188 OpExtension "SPV_KHR_non_semantic_info"
189 %ext = OpExtInstImport "NonSemantic.Test"
190 OpMemoryModel Logical GLSL450
191 %void = OpTypeVoid
192 %void_fn = OpTypeFunction %void
193 %foo = OpFunction %void None %void_fn
194 %entry = OpLabel
195 OpReturn
196 OpFunctionEnd
197 %1 = OpExtInst %void %ext 1 %foo
198 )";
199
200 SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
201 }
202
203 TEST_F(StripNonSemanticInfoTest, StripNonSemanticBetweenFunctions) {
204 std::string text = R"(
205 ; CHECK-NOT: OpExtInst
206 OpCapability Shader
207 OpCapability Linkage
208 OpExtension "SPV_KHR_non_semantic_info"
209 %ext = OpExtInstImport "NonSemantic.Test"
210 OpMemoryModel Logical GLSL450
211 %void = OpTypeVoid
212 %void_fn = OpTypeFunction %void
213 %foo = OpFunction %void None %void_fn
214 %entry = OpLabel
215 OpReturn
216 OpFunctionEnd
217 %1 = OpExtInst %void %ext 1 %foo
218 %bar = OpFunction %void None %void_fn
219 %bar_entry = OpLabel
220 OpReturn
221 OpFunctionEnd
222 )";
223
224 SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
225 }
226
227 TEST_F(StripNonSemanticInfoTest, StripNonSemanticScopes) {
228 std::string text = R"(
229 ;CHECK-NOT: OpExtension "SPV_KHR_non_semantic_info
230 ;CHECK-NOT: OpExtInstImport "NonSemantic.Shader.DebugInfo.100
231 ;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugInfoNon
232 ;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugSource
233 ;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugCompilationUnit
234 ;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugTypeFunction
235 ;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugFunction
236 ;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugScope
237 ;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugNoScope
238 OpCapability Shader
239 OpExtension "SPV_KHR_non_semantic_info"
240 OpExtension "SPV_KHR_storage_buffer_storage_class"
241 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
242 OpMemoryModel Logical GLSL450
243 OpEntryPoint GLCompute %2 "main" %3
244 OpExecutionMode %2 LocalSize 1 1 1
245 %4 = OpString ""
246 OpDecorate %5 ArrayStride 4
247 OpDecorate %6 ArrayStride 4
248 OpDecorate %7 Block
249 OpMemberDecorate %7 0 Offset 0
250 OpDecorate %3 Binding 0
251 OpDecorate %3 DescriptorSet 0
252 %8 = OpTypeVoid
253 %9 = OpTypeInt 32 0
254 %10 = OpConstant %9 100
255 %11 = OpTypeFunction %8
256 %12 = OpTypeInt 32 1
257 %13 = OpConstant %12 0
258 %14 = OpTypeFloat 32
259 %5 = OpTypePointer StorageBuffer %14
260 %6 = OpTypeRuntimeArray %14
261 %7 = OpTypeStruct %6
262 %15 = OpTypePointer StorageBuffer %7
263 %16 = OpConstant %14 0
264 %3 = OpVariable %15 StorageBuffer
265 %17 = OpExtInst %8 %1 DebugInfoNone
266 %18 = OpExtInst %8 %1 DebugSource %4 %4
267 %19 = OpExtInst %8 %1 DebugCompilationUnit %10 %10 %18 %10
268 %20 = OpExtInst %8 %1 DebugTypeFunction %10 %8
269 %21 = OpExtInst %8 %1 DebugFunction %4 %20 %18 %10 %10 %19 %4 %10 %10
270 %22 = OpExtInst %8 %1 DebugEntryPoint %21 %19 %4 %4
271 %23 = OpExtInst %8 %1 DebugTypeBasic %4 %10 %10 %10
272 %24 = OpExtInst %8 %1 DebugTypeArray %23 %10
273 %25 = OpExtInst %8 %1 DebugTypeMember %4 %24 %18 %10 %10 %10 %10 %10
274 %26 = OpExtInst %8 %1 DebugTypeComposite %4 %10 %18 %10 %10 %19 %4 %10 %10 %25
275 %27 = OpExtInst %8 %1 DebugGlobalVariable %4 %26 %18 %10 %10 %19 %4 %3 %10
276 %2 = OpFunction %8 None %11
277 %28 = OpLabel
278 %29 = OpExtInst %8 %1 DebugFunctionDefinition %21 %2
279 %30 = OpExtInst %8 %1 DebugScope %21
280 %31 = OpExtInst %8 %1 DebugLine %18 %10 %10 %10 %10
281 %32 = OpAccessChain %5 %3 %13 %13
282 OpStore %32 %16
283 OpReturn
284 %33 = OpExtInst %8 %1 DebugNoScope
285 OpFunctionEnd
286 )";
287
288 SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, false);
289 }
290
291 // Make sure that strip reflect does not remove the debug info (OpString and
292 // OpLine).
293 TEST_F(StripNonSemanticInfoTest, DontStripDebug) {
294 std::string text = R"(OpCapability Shader
295 OpMemoryModel Logical Simple
296 OpEntryPoint Fragment %1 "main"
297 OpExecutionMode %1 OriginUpperLeft
298 %2 = OpString "file"
299 %void = OpTypeVoid
300 %4 = OpTypeFunction %void
301 %1 = OpFunction %void None %4
302 %5 = OpLabel
303 OpLine %2 1 1
304 OpReturn
305 OpFunctionEnd
306 )";
307
308 SinglePassRunAndCheck<StripNonSemanticInfoPass>(text, text, false);
309 }
310
311 TEST_F(StripNonSemanticInfoTest, RemovedNonSemanticDebugInfo) {
312 const std::string text = R"(
313 ;CHECK-NOT: OpExtension "SPV_KHR_non_semantic_info
314 ;CHECK-NOT: OpExtInstImport "NonSemantic.Shader.DebugInfo.100
315 ;CHECK-NOT: OpExtInst %void {{%\w+}} DebugSource
316 ;CHECK-NOT: OpExtInst %void {{%\w+}} DebugLine
317 OpCapability Shader
318 OpExtension "SPV_KHR_non_semantic_info"
319 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
320 OpMemoryModel Logical GLSL450
321 OpEntryPoint Fragment %PSMain "PSMain" %in_var_COLOR %out_var_SV_TARGET
322 OpExecutionMode %PSMain OriginUpperLeft
323 %5 = OpString "t.hlsl"
324 %6 = OpString "float"
325 %7 = OpString "color"
326 %8 = OpString "PSInput"
327 %9 = OpString "PSMain"
328 %10 = OpString ""
329 %11 = OpString "input"
330 OpName %in_var_COLOR "in.var.COLOR"
331 OpName %out_var_SV_TARGET "out.var.SV_TARGET"
332 OpName %PSMain "PSMain"
333 OpDecorate %in_var_COLOR Location 0
334 OpDecorate %out_var_SV_TARGET Location 0
335 %uint = OpTypeInt 32 0
336 %float = OpTypeFloat 32
337 %v4float = OpTypeVector %float 4
338 %_ptr_Input_v4float = OpTypePointer Input %v4float
339 %_ptr_Output_v4float = OpTypePointer Output %v4float
340 %void = OpTypeVoid
341 %uint_1 = OpConstant %uint 1
342 %uint_9 = OpConstant %uint 9
343 %21 = OpTypeFunction %void
344 %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
345 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
346 %13 = OpExtInst %void %1 DebugSource %5
347 %PSMain = OpFunction %void None %21
348 %22 = OpLabel
349 %23 = OpLoad %v4float %in_var_COLOR
350 OpStore %out_var_SV_TARGET %23
351 %24 = OpExtInst %void %1 DebugLine %13 %uint_9 %uint_9 %uint_1 %uint_1
352 OpReturn
353 OpFunctionEnd
354 )";
355 SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
356 }
357
358 } // namespace
359 } // namespace opt
360 } // namespace spvtools