File: strip_nonsemantic_info_test.cpp

package info (click to toggle)
spirv-tools 2026.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 28,900 kB
  • sloc: cpp: 477,281; javascript: 5,908; python: 3,326; ansic: 488; sh: 450; ruby: 88; makefile: 18; lisp: 9
file content (360 lines) | stat: -rw-r--r-- 12,289 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright (c) 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <string>

#include "gmock/gmock.h"
#include "spirv-tools/optimizer.hpp"
#include "test/opt/pass_fixture.h"
#include "test/opt/pass_utils.h"

namespace spvtools {
namespace opt {
namespace {

using StripNonSemanticInfoTest = PassTest<::testing::Test>;

// This test acts as an end-to-end code example on how to strip
// reflection info from a SPIR-V module.  Use this code pattern
// when you have compiled HLSL code with Glslang or DXC using
// option -fhlsl_functionality1 to insert reflection information,
// but then want to filter out the extra instructions before sending
// it to a driver that does not implement VK_GOOGLE_hlsl_functionality1.
TEST_F(StripNonSemanticInfoTest, StripReflectEnd2EndExample) {
  // This is a non-sensical example, but exercises the instructions.
  std::string before = R"(OpCapability Shader
OpCapability Linkage
OpExtension "SPV_GOOGLE_decorate_string"
OpExtension "SPV_GOOGLE_hlsl_functionality1"
OpMemoryModel Logical Simple
OpDecorateStringGOOGLE %float HlslSemanticGOOGLE "foobar"
OpDecorateStringGOOGLE %void HlslSemanticGOOGLE "my goodness"
%void = OpTypeVoid
%float = OpTypeFloat 32
)";
  SpirvTools tools(SPV_ENV_UNIVERSAL_1_1);
  std::vector<uint32_t> binary_in;
  tools.Assemble(before, &binary_in);

  // Instantiate the optimizer, and run the strip-nonsemantic-info
  // pass over the |binary_in| module, and place the modified module
  // into |binary_out|.
  spvtools::Optimizer optimizer(SPV_ENV_UNIVERSAL_1_1);
  optimizer.RegisterPass(spvtools::CreateStripNonSemanticInfoPass());
  std::vector<uint32_t> binary_out;
  optimizer.Run(binary_in.data(), binary_in.size(), &binary_out);

  // Check results
  std::string disassembly;
  tools.Disassemble(binary_out.data(), binary_out.size(), &disassembly);
  std::string after = R"(OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical Simple
%void = OpTypeVoid
%float = OpTypeFloat 32
)";
  EXPECT_THAT(disassembly, testing::Eq(after));
}

// This test is functionally the same as the end-to-end test above,
// but uses the test SinglePassRunAndCheck test fixture instead.
TEST_F(StripNonSemanticInfoTest, StripHlslSemantic) {
  // This is a non-sensical example, but exercises the instructions.
  std::string before = R"(OpCapability Shader
OpCapability Linkage
OpExtension "SPV_GOOGLE_decorate_string"
OpExtension "SPV_GOOGLE_hlsl_functionality1"
OpMemoryModel Logical Simple
OpDecorateStringGOOGLE %float HlslSemanticGOOGLE "foobar"
OpDecorateStringGOOGLE %void HlslSemanticGOOGLE "my goodness"
%void = OpTypeVoid
%float = OpTypeFloat 32
)";
  std::string after = R"(OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical Simple
%void = OpTypeVoid
%float = OpTypeFloat 32
)";

  SinglePassRunAndCheck<StripNonSemanticInfoPass>(before, after, false);
}

TEST_F(StripNonSemanticInfoTest, StripHlslCounterBuffer) {
  std::string before = R"(OpCapability Shader
OpCapability Linkage
OpExtension "SPV_GOOGLE_hlsl_functionality1"
OpMemoryModel Logical Simple
OpDecorateId %void HlslCounterBufferGOOGLE %float
%void = OpTypeVoid
%float = OpTypeFloat 32
)";
  std::string after = R"(OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical Simple
%void = OpTypeVoid
%float = OpTypeFloat 32
)";

  SinglePassRunAndCheck<StripNonSemanticInfoPass>(before, after, false);
}

TEST_F(StripNonSemanticInfoTest, StripHlslSemanticOnMember) {
  // This is a non-sensical example, but exercises the instructions.
  std::string before = R"(OpCapability Shader
OpCapability Linkage
OpExtension "SPV_GOOGLE_decorate_string"
OpExtension "SPV_GOOGLE_hlsl_functionality1"
OpMemoryModel Logical Simple
OpMemberDecorateStringGOOGLE %struct 0 HlslSemanticGOOGLE "foobar"
%float = OpTypeFloat 32
%_struct_3 = OpTypeStruct %float
)";
  std::string after = R"(OpCapability Shader
OpCapability Linkage
OpMemoryModel Logical Simple
%float = OpTypeFloat 32
%_struct_3 = OpTypeStruct %float
)";

  SinglePassRunAndCheck<StripNonSemanticInfoPass>(before, after, false);
}

TEST_F(StripNonSemanticInfoTest, StripNonSemanticImport) {
  std::string text = R"(
; CHECK-NOT: OpExtension "SPV_KHR_non_semantic_info"
; CHECK-NOT: OpExtInstImport
OpCapability Shader
OpCapability Linkage
OpExtension "SPV_KHR_non_semantic_info"
%ext = OpExtInstImport "NonSemantic.Test"
OpMemoryModel Logical GLSL450
)";

  SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
}

TEST_F(StripNonSemanticInfoTest, StripNonSemanticGlobal) {
  std::string text = R"(
; CHECK-NOT: OpExtInst
OpCapability Shader
OpCapability Linkage
OpExtension "SPV_KHR_non_semantic_info"
%ext = OpExtInstImport "NonSemantic.Test"
OpMemoryModel Logical GLSL450
%void = OpTypeVoid
%1 = OpExtInst %void %ext 1
)";

  SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
}

TEST_F(StripNonSemanticInfoTest, StripNonSemanticInFunction) {
  std::string text = R"(
; CHECK-NOT: OpExtInst
OpCapability Shader
OpCapability Linkage
OpExtension "SPV_KHR_non_semantic_info"
%ext = OpExtInstImport "NonSemantic.Test"
OpMemoryModel Logical GLSL450
%void = OpTypeVoid
%void_fn = OpTypeFunction %void
%foo = OpFunction %void None %void_fn
%entry = OpLabel
%1 = OpExtInst %void %ext 1 %foo
OpReturn
OpFunctionEnd
)";

  SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
}

TEST_F(StripNonSemanticInfoTest, StripNonSemanticAfterFunction) {
  std::string text = R"(
; CHECK-NOT: OpExtInst
OpCapability Shader
OpCapability Linkage
OpExtension "SPV_KHR_non_semantic_info"
%ext = OpExtInstImport "NonSemantic.Test"
OpMemoryModel Logical GLSL450
%void = OpTypeVoid
%void_fn = OpTypeFunction %void
%foo = OpFunction %void None %void_fn
%entry = OpLabel
OpReturn
OpFunctionEnd
%1 = OpExtInst %void %ext 1 %foo
)";

  SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
}

TEST_F(StripNonSemanticInfoTest, StripNonSemanticBetweenFunctions) {
  std::string text = R"(
; CHECK-NOT: OpExtInst
OpCapability Shader
OpCapability Linkage
OpExtension "SPV_KHR_non_semantic_info"
%ext = OpExtInstImport "NonSemantic.Test"
OpMemoryModel Logical GLSL450
%void = OpTypeVoid
%void_fn = OpTypeFunction %void
%foo = OpFunction %void None %void_fn
%entry = OpLabel
OpReturn
OpFunctionEnd
%1 = OpExtInst %void %ext 1 %foo
%bar = OpFunction %void None %void_fn
%bar_entry = OpLabel
OpReturn
OpFunctionEnd
)";

  SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
}

TEST_F(StripNonSemanticInfoTest, StripNonSemanticScopes) {
  std::string text = R"(
;CHECK-NOT: OpExtension "SPV_KHR_non_semantic_info
;CHECK-NOT: OpExtInstImport "NonSemantic.Shader.DebugInfo.100
;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugInfoNon
;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugSource
;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugCompilationUnit
;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugTypeFunction
;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugFunction
;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugScope
;CHECK-NOT: OpExtInst %8 {{%\w+}} DebugNoScope
               OpCapability Shader
               OpExtension "SPV_KHR_non_semantic_info"
               OpExtension "SPV_KHR_storage_buffer_storage_class"
          %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
               OpMemoryModel Logical GLSL450
               OpEntryPoint GLCompute %2 "main" %3
               OpExecutionMode %2 LocalSize 1 1 1
          %4 = OpString ""
               OpDecorate %5 ArrayStride 4
               OpDecorate %6 ArrayStride 4
               OpDecorate %7 Block
               OpMemberDecorate %7 0 Offset 0
               OpDecorate %3 Binding 0
               OpDecorate %3 DescriptorSet 0
          %8 = OpTypeVoid
          %9 = OpTypeInt 32 0
         %10 = OpConstant %9 100
         %11 = OpTypeFunction %8
         %12 = OpTypeInt 32 1
         %13 = OpConstant %12 0
         %14 = OpTypeFloat 32
          %5 = OpTypePointer StorageBuffer %14
          %6 = OpTypeRuntimeArray %14
          %7 = OpTypeStruct %6
         %15 = OpTypePointer StorageBuffer %7
         %16 = OpConstant %14 0
          %3 = OpVariable %15 StorageBuffer
         %17 = OpExtInst %8 %1 DebugInfoNone
         %18 = OpExtInst %8 %1 DebugSource %4 %4
         %19 = OpExtInst %8 %1 DebugCompilationUnit %10 %10 %18 %10
         %20 = OpExtInst %8 %1 DebugTypeFunction %10 %8
         %21 = OpExtInst %8 %1 DebugFunction %4 %20 %18 %10 %10 %19 %4 %10 %10
         %22 = OpExtInst %8 %1 DebugEntryPoint %21 %19 %4 %4
         %23 = OpExtInst %8 %1 DebugTypeBasic %4 %10 %10 %10
         %24 = OpExtInst %8 %1 DebugTypeArray %23 %10
         %25 = OpExtInst %8 %1 DebugTypeMember %4 %24 %18 %10 %10 %10 %10 %10
         %26 = OpExtInst %8 %1 DebugTypeComposite %4 %10 %18 %10 %10 %19 %4 %10 %10 %25
         %27 = OpExtInst %8 %1 DebugGlobalVariable %4 %26 %18 %10 %10 %19 %4 %3 %10
          %2 = OpFunction %8 None %11
         %28 = OpLabel
         %29 = OpExtInst %8 %1 DebugFunctionDefinition %21 %2
         %30 = OpExtInst %8 %1 DebugScope %21
         %31 = OpExtInst %8 %1 DebugLine %18 %10 %10 %10 %10
         %32 = OpAccessChain %5 %3 %13 %13
               OpStore %32 %16
               OpReturn
         %33 = OpExtInst %8 %1 DebugNoScope
               OpFunctionEnd
)";

  SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, false);
}

// Make sure that strip reflect does not remove the debug info (OpString and
// OpLine).
TEST_F(StripNonSemanticInfoTest, DontStripDebug) {
  std::string text = R"(OpCapability Shader
OpMemoryModel Logical Simple
OpEntryPoint Fragment %1 "main"
OpExecutionMode %1 OriginUpperLeft
%2 = OpString "file"
%void = OpTypeVoid
%4 = OpTypeFunction %void
%1 = OpFunction %void None %4
%5 = OpLabel
OpLine %2 1 1
OpReturn
OpFunctionEnd
)";

  SinglePassRunAndCheck<StripNonSemanticInfoPass>(text, text, false);
}

TEST_F(StripNonSemanticInfoTest, RemovedNonSemanticDebugInfo) {
  const std::string text = R"(
;CHECK-NOT: OpExtension "SPV_KHR_non_semantic_info
;CHECK-NOT: OpExtInstImport "NonSemantic.Shader.DebugInfo.100
;CHECK-NOT: OpExtInst %void {{%\w+}} DebugSource
;CHECK-NOT: OpExtInst %void {{%\w+}} DebugLine
               OpCapability Shader
               OpExtension "SPV_KHR_non_semantic_info"
          %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Fragment %PSMain "PSMain" %in_var_COLOR %out_var_SV_TARGET
               OpExecutionMode %PSMain OriginUpperLeft
          %5 = OpString "t.hlsl"
          %6 = OpString "float"
          %7 = OpString "color"
          %8 = OpString "PSInput"
          %9 = OpString "PSMain"
         %10 = OpString ""
         %11 = OpString "input"
               OpName %in_var_COLOR "in.var.COLOR"
               OpName %out_var_SV_TARGET "out.var.SV_TARGET"
               OpName %PSMain "PSMain"
               OpDecorate %in_var_COLOR Location 0
               OpDecorate %out_var_SV_TARGET Location 0
       %uint = OpTypeInt 32 0
      %float = OpTypeFloat 32
    %v4float = OpTypeVector %float 4
%_ptr_Input_v4float = OpTypePointer Input %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
       %void = OpTypeVoid
     %uint_1 = OpConstant %uint 1
     %uint_9 = OpConstant %uint 9
         %21 = OpTypeFunction %void
%in_var_COLOR = OpVariable %_ptr_Input_v4float Input
%out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
         %13 = OpExtInst %void %1 DebugSource %5
     %PSMain = OpFunction %void None %21
         %22 = OpLabel
         %23 = OpLoad %v4float %in_var_COLOR
               OpStore %out_var_SV_TARGET %23
         %24 = OpExtInst %void %1 DebugLine %13 %uint_9 %uint_9 %uint_1 %uint_1
               OpReturn
               OpFunctionEnd
)";
  SinglePassRunAndMatch<StripNonSemanticInfoPass>(text, true);
}

}  // namespace
}  // namespace opt
}  // namespace spvtools