Debian Codegraph @

test/opt/trim_capabilities_pass_test.cpp

raw
1 // Copyright (c) 2023 Google Inc.
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 <gtest/gtest-param-test.h>
16 #include <gtest/gtest.h>
17
18 #include <tuple>
19
20 #include "spirv-tools/optimizer.hpp"
21 #include "test/opt/pass_fixture.h"
22 #include "test/opt/pass_utils.h"
23
24 namespace spvtools {
25 namespace opt {
26 namespace {
27
28 using TrimCapabilitiesPassTest = PassTest<::testing::Test>;
29
30 TEST_F(TrimCapabilitiesPassTest, CheckKnownAliasTransformations) {
31 // Those are expected changes caused by the test process:
32 // - SPV is assembled. -> capability goes from text to number.
33 // - SPV is optimized.
34 // - SPV is disassembled -> capability goes from number to text.
35 // - CHECK rule compares both text versions.
36 // Because some capabilities share the same number (aliases), the text
37 // compared with the CHECK rules depends on which alias is the first on the
38 // SPIRV-Headers enum. This could change, and we want to easily distinguish
39 // real failure from alias order change. This test is only here to list known
40 // alias transformations. If this test breaks, it's not a bug in the
41 // optimization pass, but just the SPIRV-Headers enum order that has changed.
42 // If that happens, tests needs to be updated to the correct alias is used in
43 // the CHECK rule.
44 const std::string kTest = R"(
45 OpCapability Linkage
46 OpCapability StorageUniform16
47 OpCapability StorageUniformBufferBlock16
48 OpCapability ShaderViewportIndexLayerNV
49 OpCapability FragmentBarycentricNV
50 OpCapability ShadingRateNV
51 OpCapability ShaderNonUniformEXT
52 OpCapability RuntimeDescriptorArrayEXT
53 OpCapability InputAttachmentArrayDynamicIndexingEXT
54 OpCapability UniformTexelBufferArrayDynamicIndexingEXT
55 OpCapability StorageTexelBufferArrayDynamicIndexingEXT
56 OpCapability UniformBufferArrayNonUniformIndexingEXT
57 OpCapability SampledImageArrayNonUniformIndexingEXT
58 OpCapability StorageBufferArrayNonUniformIndexingEXT
59 OpCapability StorageImageArrayNonUniformIndexingEXT
60 OpCapability InputAttachmentArrayNonUniformIndexingEXT
61 OpCapability UniformTexelBufferArrayNonUniformIndexingEXT
62 OpCapability StorageTexelBufferArrayNonUniformIndexingEXT
63 OpCapability VulkanMemoryModelKHR
64 OpCapability VulkanMemoryModelDeviceScopeKHR
65 OpCapability PhysicalStorageBufferAddressesEXT
66 OpCapability DemoteToHelperInvocationEXT
67 OpCapability DotProductInputAllKHR
68 OpCapability DotProductInput4x8BitKHR
69 OpCapability DotProductInput4x8BitPackedKHR
70 OpCapability DotProductKHR
71 OpCapability ComputeDerivativeGroupQuadsKHR
72 OpCapability ComputeDerivativeGroupLinearKHR
73 ; CHECK: OpCapability Linkage
74 ; CHECK-NOT: OpCapability StorageUniform16
75 ; CHECK-NOT: OpCapability StorageUniformBufferBlock16
76 ; CHECK-NOT: OpCapability ShaderViewportIndexLayerNV
77 ; CHECK-NOT: OpCapability FragmentBarycentricNV
78 ; CHECK-NOT: OpCapability ShadingRateNV
79 ; CHECK-NOT: OpCapability ShaderNonUniformEXT
80 ; CHECK-NOT: OpCapability RuntimeDescriptorArrayEXT
81 ; CHECK-NOT: OpCapability InputAttachmentArrayDynamicIndexingEXT
82 ; CHECK-NOT: OpCapability UniformTexelBufferArrayDynamicIndexingEXT
83 ; CHECK-NOT: OpCapability StorageTexelBufferArrayDynamicIndexingEXT
84 ; CHECK-NOT: OpCapability UniformBufferArrayNonUniformIndexingEXT
85 ; CHECK-NOT: OpCapability SampledImageArrayNonUniformIndexingEXT
86 ; CHECK-NOT: OpCapability StorageBufferArrayNonUniformIndexingEXT
87 ; CHECK-NOT: OpCapability StorageImageArrayNonUniformIndexingEXT
88 ; CHECK-NOT: OpCapability InputAttachmentArrayNonUniformIndexingEXT
89 ; CHECK-NOT: OpCapability UniformTexelBufferArrayNonUniformIndexingEXT
90 ; CHECK-NOT: OpCapability StorageTexelBufferArrayNonUniformIndexingEXT
91 ; CHECK-NOT: OpCapability VulkanMemoryModelKHR
92 ; CHECK-NOT: OpCapability VulkanMemoryModelDeviceScopeKHR
93 ; CHECK-NOT: OpCapability PhysicalStorageBufferAddressesEXT
94 ; CHECK-NOT: OpCapability DemoteToHelperInvocationEXT
95 ; CHECK-NOT: OpCapability DotProductInputAllKHR
96 ; CHECK-NOT: OpCapability DotProductInput4x8BitKHR
97 ; CHECK-NOT: OpCapability DotProductInput4x8BitPackedKHR
98 ; CHECK-NOT: OpCapability DotProductKHR
99 ; CHECK-NOT: OpCapability ComputeDerivativeGroupQuadsKHR
100 ; CHECK-NOT: OpCapability ComputeDerivativeGroupLinearKHR
101 ; CHECK: OpCapability UniformAndStorageBuffer16BitAccess
102 ; CHECK: OpCapability StorageBuffer16BitAccess
103 ; CHECK: OpCapability ShaderViewportIndexLayerEXT
104 ; CHECK: OpCapability FragmentBarycentricKHR
105 ; CHECK: OpCapability FragmentDensityEXT
106 ; CHECK: OpCapability ShaderNonUniform
107 ; CHECK: OpCapability RuntimeDescriptorArray
108 ; CHECK: OpCapability InputAttachmentArrayDynamicIndexing
109 ; CHECK: OpCapability UniformTexelBufferArrayDynamicIndexing
110 ; CHECK: OpCapability StorageTexelBufferArrayDynamicIndexing
111 ; CHECK: OpCapability UniformBufferArrayNonUniformIndexing
112 ; CHECK: OpCapability SampledImageArrayNonUniformIndexing
113 ; CHECK: OpCapability StorageBufferArrayNonUniformIndexing
114 ; CHECK: OpCapability StorageImageArrayNonUniformIndexing
115 ; CHECK: OpCapability InputAttachmentArrayNonUniformIndexing
116 ; CHECK: OpCapability UniformTexelBufferArrayNonUniformIndexing
117 ; CHECK: OpCapability StorageTexelBufferArrayNonUniformIndexing
118 ; CHECK: OpCapability VulkanMemoryModel
119 ; CHECK: OpCapability VulkanMemoryModelDeviceScope
120 ; CHECK: OpCapability PhysicalStorageBufferAddresses
121 ; CHECK: OpCapability DemoteToHelperInvocation
122 ; CHECK: OpCapability DotProductInputAll
123 ; CHECK: OpCapability DotProductInput4x8Bit
124 ; CHECK: OpCapability DotProductInput4x8BitPacked
125 ; CHECK: OpCapability DotProduct
126 OpMemoryModel Logical Vulkan
127 OpEntryPoint GLCompute %1 "main"
128 %void = OpTypeVoid
129 %3 = OpTypeFunction %void
130 %1 = OpFunction %void None %3
131 %6 = OpLabel
132 OpReturn
133 OpFunctionEnd;
134 )";
135 SetTargetEnv(SPV_ENV_VULKAN_1_3);
136 const auto result =
137 SinglePassRunAndMatch<EmptyPass>(kTest, /* skip_nop= */ false);
138 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
139 }
140
141 TEST_F(TrimCapabilitiesPassTest, LinkagePreventsChanges) {
142 const std::string kTest = R"(
143 OpCapability Linkage
144 OpCapability ClipDistance
145 OpCapability CullDistance
146 OpCapability DemoteToHelperInvocation
147 OpCapability DeviceGroup
148 OpCapability DrawParameters
149 OpCapability Float16
150 OpCapability Float64
151 OpCapability FragmentBarycentricKHR
152 OpCapability FragmentFullyCoveredEXT
153 OpCapability FragmentShadingRateKHR
154 OpCapability GroupNonUniform
155 OpCapability GroupNonUniformArithmetic
156 OpCapability GroupNonUniformBallot
157 OpCapability GroupNonUniformQuad
158 OpCapability GroupNonUniformShuffle
159 OpCapability Image1D
160 OpCapability ImageBuffer
161 OpCapability ImageGatherExtended
162 OpCapability ImageMSArray
163 OpCapability ImageQuery
164 OpCapability InputAttachment
165 OpCapability InputAttachmentArrayNonUniformIndexing
166 OpCapability Int16
167 OpCapability Int64
168 OpCapability Int64Atomics
169 OpCapability Int64ImageEXT
170 OpCapability MeshShadingNV
171 OpCapability MinLod
172 OpCapability MultiView
173 OpCapability MultiViewport
174 OpCapability PhysicalStorageBufferAddresses
175 OpCapability RayQueryKHR
176 OpCapability RayTracingKHR
177 OpCapability RayTracingNV
178 OpCapability RayTraversalPrimitiveCullingKHR
179 OpCapability RuntimeDescriptorArray
180 OpCapability SampleMaskPostDepthCoverage
181 OpCapability SampleRateShading
182 OpCapability Sampled1D
183 OpCapability SampledBuffer
184 OpCapability SampledImageArrayNonUniformIndexing
185 OpCapability Shader
186 OpCapability ShaderClockKHR
187 OpCapability ShaderLayer
188 OpCapability ShaderNonUniform
189 OpCapability ShaderViewportIndex
190 OpCapability ShaderViewportIndexLayerEXT
191 OpCapability SparseResidency
192 OpCapability StencilExportEXT
193 OpCapability StorageImageArrayNonUniformIndexingEXT
194 OpCapability StorageImageExtendedFormats
195 OpCapability StorageImageReadWithoutFormat
196 OpCapability StorageImageWriteWithoutFormat
197 OpCapability StorageInputOutput16
198 OpCapability StoragePushConstant16
199 OpCapability StorageTexelBufferArrayNonUniformIndexing
200 OpCapability StorageUniform16
201 OpCapability StorageUniformBufferBlock16
202 OpCapability Tessellation
203 OpCapability UniformTexelBufferArrayNonUniformIndexing
204 OpCapability VulkanMemoryModel
205 OpExtension "SPV_EXT_fragment_fully_covered"
206 OpExtension "SPV_EXT_shader_image_int64"
207 OpExtension "SPV_EXT_shader_stencil_export"
208 OpExtension "SPV_EXT_shader_viewport_index_layer"
209 OpExtension "SPV_KHR_fragment_shader_barycentric"
210 OpExtension "SPV_KHR_fragment_shading_rate"
211 OpExtension "SPV_KHR_post_depth_coverage"
212 OpExtension "SPV_KHR_ray_query"
213 OpExtension "SPV_KHR_ray_tracing"
214 OpExtension "SPV_KHR_shader_clock"
215 OpExtension "SPV_NV_mesh_shader"
216 OpExtension "SPV_NV_ray_tracing"
217 OpExtension "SPV_NV_viewport_array2"
218 ; CHECK: OpCapability Linkage
219 ; CHECK: OpCapability ClipDistance
220 ; CHECK: OpCapability CullDistance
221 ; CHECK: OpCapability DemoteToHelperInvocation
222 ; CHECK: OpCapability DeviceGroup
223 ; CHECK: OpCapability DrawParameters
224 ; CHECK: OpCapability Float16
225 ; CHECK: OpCapability Float64
226 ; CHECK: OpCapability FragmentBarycentricKHR
227 ; CHECK: OpCapability FragmentFullyCoveredEXT
228 ; CHECK: OpCapability FragmentShadingRateKHR
229 ; CHECK: OpCapability GroupNonUniform
230 ; CHECK: OpCapability GroupNonUniformArithmetic
231 ; CHECK: OpCapability GroupNonUniformBallot
232 ; CHECK: OpCapability GroupNonUniformQuad
233 ; CHECK: OpCapability GroupNonUniformShuffle
234 ; CHECK: OpCapability Image1D
235 ; CHECK: OpCapability ImageBuffer
236 ; CHECK: OpCapability ImageGatherExtended
237 ; CHECK: OpCapability ImageMSArray
238 ; CHECK: OpCapability ImageQuery
239 ; CHECK: OpCapability InputAttachment
240 ; CHECK: OpCapability InputAttachmentArrayNonUniformIndexing
241 ; CHECK: OpCapability Int16
242 ; CHECK: OpCapability Int64
243 ; CHECK: OpCapability Int64Atomics
244 ; CHECK: OpCapability Int64ImageEXT
245 ; CHECK: OpCapability MeshShadingNV
246 ; CHECK: OpCapability MinLod
247 ; CHECK: OpCapability MultiView
248 ; CHECK: OpCapability MultiViewport
249 ; CHECK: OpCapability PhysicalStorageBufferAddresses
250 ; CHECK: OpCapability RayQueryKHR
251 ; CHECK: OpCapability RayTracingKHR
252 ; CHECK: OpCapability RayTracingNV
253 ; CHECK: OpCapability RayTraversalPrimitiveCullingKHR
254 ; CHECK: OpCapability RuntimeDescriptorArray
255 ; CHECK: OpCapability SampleMaskPostDepthCoverage
256 ; CHECK: OpCapability SampleRateShading
257 ; CHECK: OpCapability Sampled1D
258 ; CHECK: OpCapability SampledBuffer
259 ; CHECK: OpCapability SampledImageArrayNonUniformIndexing
260 ; CHECK: OpCapability Shader
261 ; CHECK: OpCapability ShaderClockKHR
262 ; CHECK: OpCapability ShaderLayer
263 ; CHECK: OpCapability ShaderNonUniform
264 ; CHECK: OpCapability ShaderViewportIndex
265 ; CHECK: OpCapability ShaderViewportIndexLayerEXT
266 ; CHECK: OpCapability SparseResidency
267 ; CHECK: OpCapability StencilExportEXT
268 ; CHECK: OpCapability StorageImageArrayNonUniformIndexing
269 ; CHECK: OpCapability StorageImageExtendedFormats
270 ; CHECK: OpCapability StorageImageReadWithoutFormat
271 ; CHECK: OpCapability StorageImageWriteWithoutFormat
272 ; CHECK: OpCapability StorageInputOutput16
273 ; CHECK: OpCapability StoragePushConstant16
274 ; CHECK: OpCapability StorageTexelBufferArrayNonUniformIndexing
275 ; CHECK: OpCapability Tessellation
276 ; CHECK: OpCapability UniformTexelBufferArrayNonUniformIndex
277 ; CHECK: OpCapability VulkanMemoryModel
278 ; CHECK: OpExtension "SPV_EXT_fragment_fully_covered"
279 ; CHECK: OpExtension "SPV_EXT_shader_image_int64"
280 ; CHECK: OpExtension "SPV_EXT_shader_stencil_export"
281 ; CHECK: OpExtension "SPV_EXT_shader_viewport_index_layer"
282 ; CHECK: OpExtension "SPV_KHR_fragment_shader_barycentric"
283 ; CHECK: OpExtension "SPV_KHR_fragment_shading_rate"
284 ; CHECK: OpExtension "SPV_KHR_post_depth_coverage"
285 ; CHECK: OpExtension "SPV_KHR_ray_query"
286 ; CHECK: OpExtension "SPV_KHR_ray_tracing"
287 ; CHECK: OpExtension "SPV_KHR_shader_clock"
288 ; CHECK: OpExtension "SPV_NV_mesh_shader"
289 ; CHECK: OpExtension "SPV_NV_ray_tracing"
290 ; CHECK: OpExtension "SPV_NV_viewport_array2"
291 OpMemoryModel Logical Vulkan
292 %void = OpTypeVoid
293 %3 = OpTypeFunction %void
294 %1 = OpFunction %void None %3
295 %6 = OpLabel
296 OpReturn
297 OpFunctionEnd;
298 )";
299 SetTargetEnv(SPV_ENV_VULKAN_1_3);
300 const auto result =
301 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
302 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
303 }
304
305 TEST_F(TrimCapabilitiesPassTest, KeepShader) {
306 const std::string kTest = R"(
307 OpCapability Shader
308 ; CHECK: OpCapability Shader
309 OpMemoryModel Logical GLSL450
310 OpEntryPoint GLCompute %1 "main"
311 %void = OpTypeVoid
312 %3 = OpTypeFunction %void
313 %1 = OpFunction %void None %3
314 %6 = OpLabel
315 OpReturn
316 OpFunctionEnd;
317 )";
318 const auto result =
319 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
320 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
321 }
322
323 TEST_F(TrimCapabilitiesPassTest, KeepShaderClockWhenInUse) {
324 const std::string kTest = R"(
325 OpCapability Shader
326 OpCapability Int64
327 OpCapability ShaderClockKHR
328 OpExtension "SPV_KHR_shader_clock"
329 ; CHECK: OpCapability ShaderClockKHR
330 ; CHECK: OpExtension "SPV_KHR_shader_clock"
331 OpMemoryModel Logical GLSL450
332 OpEntryPoint GLCompute %1 "main"
333 %void = OpTypeVoid
334 %uint = OpTypeInt 32 0
335 %ulong = OpTypeInt 64 0
336 %scope = OpConstant %uint 1
337 %3 = OpTypeFunction %void
338 %1 = OpFunction %void None %3
339 %6 = OpLabel
340 %7 = OpReadClockKHR %ulong %scope
341 OpReturn
342 OpFunctionEnd;
343 )";
344 const auto result =
345 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
346 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
347 }
348
349 TEST_F(TrimCapabilitiesPassTest, TrimShaderClockWhenUnused) {
350 const std::string kTest = R"(
351 OpCapability Shader
352 OpCapability Int64
353 OpCapability ShaderClockKHR
354 OpExtension "SPV_KHR_shader_clock"
355 ; CHECK-NOT: OpCapability ShaderClockKHR
356 ; CHECK-NOT: OpExtension "SPV_KHR_shader_clock"
357 OpMemoryModel Logical GLSL450
358 OpEntryPoint GLCompute %1 "main"
359 %void = OpTypeVoid
360 %3 = OpTypeFunction %void
361 %1 = OpFunction %void None %3
362 %6 = OpLabel
363 OpReturn
364 OpFunctionEnd;
365 )";
366 const auto result =
367 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
368 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
369 }
370
371 TEST_F(TrimCapabilitiesPassTest, AMDShaderBallotExtensionRemains) {
372 const std::string kTest = R"(
373 OpCapability Shader
374 OpCapability Groups
375 OpExtension "SPV_AMD_shader_ballot"
376 ; CHECK: OpCapability Groups
377 ; CHECK: OpExtension "SPV_AMD_shader_ballot"
378 OpMemoryModel Logical GLSL450
379 OpEntryPoint GLCompute %1 "main"
380 %void = OpTypeVoid
381 %uint = OpTypeInt 32 0
382 %1 = OpTypeFunction %void
383 %uint_0 = OpConstant %uint 0
384 %2 = OpFunction %void None %1
385 %3 = OpLabel
386 %4 = OpGroupIAddNonUniformAMD %uint %uint_0 ExclusiveScan %uint_0
387 OpReturn
388 OpFunctionEnd;
389 )";
390 const auto result =
391 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
392 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
393 }
394
395 TEST_F(TrimCapabilitiesPassTest, AMDShaderBallotExtensionRemoved) {
396 const std::string kTest = R"(
397 OpCapability Shader
398 OpCapability Groups
399 OpExtension "SPV_AMD_shader_ballot"
400 ; CHECK-NOT: OpCapability Groups
401 ; CHECK-NOT: OpExtension "SPV_AMD_shader_ballot"
402 OpMemoryModel Logical GLSL450
403 OpEntryPoint GLCompute %1 "main"
404 %void = OpTypeVoid
405 %1 = OpTypeFunction %void
406 %2 = OpFunction %void None %1
407 %3 = OpLabel
408 OpReturn
409 OpFunctionEnd;
410 )";
411 const auto result =
412 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
413 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
414 }
415
416 TEST_F(TrimCapabilitiesPassTest, MinLod_RemovedIfNotUsed) {
417 const std::string kTest = R"(
418 OpCapability Shader
419 OpCapability Sampled1D
420 OpCapability MinLod
421 ; CHECK-NOT: OpCapability MinLod
422 OpMemoryModel Logical GLSL450
423 OpEntryPoint GLCompute %1 "main"
424 %void = OpTypeVoid
425 %float = OpTypeFloat 32
426 %v3float = OpTypeVector %float 3
427 %v4float = OpTypeVector %float 4
428 %type_image = OpTypeImage %float Cube 2 0 0 1 Rgba32f
429 %ptr_type_image = OpTypePointer UniformConstant %type_image
430 %type_sampler = OpTypeSampler
431 %ptr_type_sampler = OpTypePointer UniformConstant %type_sampler
432 %float_0 = OpConstant %float 0
433 %float_000 = OpConstantComposite %v3float %float_0 %float_0 %float_0
434 %image = OpVariable %ptr_type_image UniformConstant
435 %sampler = OpVariable %ptr_type_sampler UniformConstant
436 %1 = OpTypeFunction %void
437 %2 = OpFunction %void None %1
438 %3 = OpLabel
439 %21 = OpLoad %type_image %image
440 %22 = OpLoad %type_sampler %sampler
441 %24 = OpSampledImage %type_sampled_image %21 %22
442 %25 = OpImageSampleImplicitLod %v4float %24 %float_000
443 OpReturn
444 OpFunctionEnd;
445 )";
446 const auto result =
447 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
448 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
449 }
450
451 TEST_F(TrimCapabilitiesPassTest, MinLod_RemainsWithOpImageSampleImplicitLod) {
452 const std::string kTest = R"(
453 OpCapability Shader
454 OpCapability Sampled1D
455 OpCapability MinLod
456 ; CHECK: OpCapability MinLod
457 OpMemoryModel Logical GLSL450
458 OpEntryPoint GLCompute %1 "main"
459 %void = OpTypeVoid
460 %float = OpTypeFloat 32
461 %v3float = OpTypeVector %float 3
462 %v4float = OpTypeVector %float 4
463 %type_image = OpTypeImage %float Cube 2 0 0 1 Rgba32f
464 %ptr_type_image = OpTypePointer UniformConstant %type_image
465 %type_sampler = OpTypeSampler
466 %ptr_type_sampler = OpTypePointer UniformConstant %type_sampler
467 %float_0 = OpConstant %float 0
468 %float_000 = OpConstantComposite %v3float %float_0 %float_0 %float_0
469 %image = OpVariable %ptr_type_image UniformConstant
470 %sampler = OpVariable %ptr_type_sampler UniformConstant
471 %1 = OpTypeFunction %void
472 %2 = OpFunction %void None %1
473 %3 = OpLabel
474 %21 = OpLoad %type_image %image
475 %22 = OpLoad %type_sampler %sampler
476 %24 = OpSampledImage %type_sampled_image %21 %22
477 %25 = OpImageSampleImplicitLod %v4float %24 %float_000 MinLod %float_0
478 OpReturn
479 OpFunctionEnd;
480 )";
481 const auto result =
482 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
483 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
484 }
485
486 TEST_F(TrimCapabilitiesPassTest,
487 MinLod_RemainsWithOpImageSparseSampleImplicitLod) {
488 const std::string kTest = R"(
489 OpCapability Shader
490 OpCapability SparseResidency
491 OpCapability ImageGatherExtended
492 OpCapability MinLod
493 ; CHECK: OpCapability MinLod
494 OpMemoryModel Logical GLSL450
495 OpEntryPoint Fragment %2 "main"
496 OpExecutionMode %2 OriginUpperLeft
497 %void = OpTypeVoid
498 %uint = OpTypeInt 32 0
499 %float = OpTypeFloat 32
500 %v2float = OpTypeVector %float 2
501 %v3float = OpTypeVector %float 3
502 %v4float = OpTypeVector %float 4
503 %type_image = OpTypeImage %float 2D 2 0 0 1 Unknown
504 %ptr_type_image = OpTypePointer UniformConstant %type_image
505 %type_sampler = OpTypeSampler
506 %ptr_type_sampler = OpTypePointer UniformConstant %type_sampler
507 %type_sampled_image = OpTypeSampledImage %type_image
508 %sparse_struct = OpTypeStruct %uint %v4float
509 %float_0 = OpConstant %float 0
510 %float_00 = OpConstantComposite %v2float %float_0 %float_0
511 %float_000 = OpConstantComposite %v3float %float_0 %float_0 %float_0
512 %image = OpVariable %ptr_type_image UniformConstant
513 %sampler = OpVariable %ptr_type_sampler UniformConstant
514 %1 = OpTypeFunction %void
515 %2 = OpFunction %void None %1
516 %3 = OpLabel
517 %21 = OpLoad %type_image %image
518 %22 = OpLoad %type_sampler %sampler
519 %24 = OpSampledImage %type_sampled_image %21 %22
520 %25 = OpImageSparseSampleImplicitLod %sparse_struct %24 %float_00 MinLod %float_0
521 OpReturn
522 OpFunctionEnd;
523 )";
524 const auto result =
525 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
526 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
527 }
528
529 TEST_F(TrimCapabilitiesPassTest, MinLod_DetectsMinLodWithBitmaskImageOperand) {
530 const std::string kTest = R"(
531 OpCapability MinLod
532 ; CHECK: OpCapability MinLod
533 OpCapability Shader
534 OpCapability SparseResidency
535 OpCapability ImageGatherExtended
536 OpMemoryModel Logical GLSL450
537 OpEntryPoint Fragment %1 "main"
538 OpExecutionMode %1 OriginUpperLeft
539 %type_sampler = OpTypeSampler
540 %int = OpTypeInt 32 1
541 %float = OpTypeFloat 32
542 %v2int = OpTypeVector %int 2
543 %v2float = OpTypeVector %float 2
544 %v4float = OpTypeVector %float 4
545 %ptr_sampler = OpTypePointer UniformConstant %type_sampler
546 %type_image = OpTypeImage %float 2D 2 0 0 1 Unknown
547 %ptr_image = OpTypePointer UniformConstant %type_image
548 %void = OpTypeVoid
549 %uint = OpTypeInt 32 0
550 %type_sampled_image = OpTypeSampledImage %type_image
551 %type_struct = OpTypeStruct %uint %v4float
552
553 %int_1 = OpConstant %int 1
554 %float_0 = OpConstant %float 0
555 %float_1 = OpConstant %float 1
556 %8 = OpConstantComposite %v2float %float_0 %float_0
557 %12 = OpConstantComposite %v2int %int_1 %int_1
558
559 %2 = OpVariable %ptr_sampler UniformConstant
560 %3 = OpVariable %ptr_image UniformConstant
561 %27 = OpTypeFunction %void
562 %1 = OpFunction %void None %27
563 %28 = OpLabel
564 %29 = OpLoad %type_image %3
565 %30 = OpLoad %type_sampler %2
566 %31 = OpSampledImage %type_sampled_image %29 %30
567 %32 = OpImageSparseSampleImplicitLod %type_struct %31 %8 ConstOffset|MinLod %12 %float_0
568 OpReturn
569 OpFunctionEnd
570 )";
571 const auto result =
572 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
573 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
574 }
575
576 TEST_F(TrimCapabilitiesPassTest,
577 StorageInputOutput16_RemainsWithInputPointer_Vulkan1_0) {
578 const std::string kTest = R"(
579 OpCapability Shader
580 OpCapability Float16
581 OpCapability StorageInputOutput16
582 OpExtension "SPV_KHR_16bit_storage"
583 ; CHECK: OpCapability StorageInputOutput16
584 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
585 OpMemoryModel Logical GLSL450
586 OpEntryPoint GLCompute %2 "main"
587 %void = OpTypeVoid
588 %half = OpTypeFloat 16
589 %ptr = OpTypePointer Input %half
590 %1 = OpTypeFunction %void
591 %2 = OpFunction %void None %1
592 %3 = OpLabel
593 OpReturn
594 OpFunctionEnd
595 )";
596 SetTargetEnv(SPV_ENV_VULKAN_1_0);
597 const auto result =
598 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
599 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
600 }
601
602 TEST_F(TrimCapabilitiesPassTest,
603 StorageInputOutput16_RemainsWithInputPointer_Vulkan1_1) {
604 const std::string kTest = R"(
605 OpCapability Shader
606 OpCapability Float16
607 OpCapability StorageInputOutput16
608 OpExtension "SPV_KHR_16bit_storage"
609 ; CHECK: OpCapability StorageInputOutput16
610 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
611 OpMemoryModel Logical GLSL450
612 OpEntryPoint GLCompute %2 "main"
613 %void = OpTypeVoid
614 %half = OpTypeFloat 16
615 %ptr = OpTypePointer Input %half
616 %1 = OpTypeFunction %void
617 %2 = OpFunction %void None %1
618 %3 = OpLabel
619 OpReturn
620 OpFunctionEnd
621 )";
622 SetTargetEnv(SPV_ENV_VULKAN_1_1);
623 const auto result =
624 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
625 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
626 }
627
628 TEST_F(TrimCapabilitiesPassTest,
629 StorageInputOutput16_RemainsWithInputPointerArray_Vulkan1_0) {
630 const std::string kTest = R"(
631 OpCapability Shader
632 OpCapability Float16
633 OpCapability StorageInputOutput16
634 OpExtension "SPV_KHR_16bit_storage"
635 ; CHECK: OpCapability StorageInputOutput16
636 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
637 OpMemoryModel Logical GLSL450
638 OpEntryPoint GLCompute %2 "main"
639 %void = OpTypeVoid
640 %half = OpTypeFloat 16
641 %uint = OpTypeInt 32 0
642 %uint_1 = OpConstant %uint 1
643 %array = OpTypeArray %half %uint_1
644 %ptr = OpTypePointer Input %array
645 %1 = OpTypeFunction %void
646 %2 = OpFunction %void None %1
647 %3 = OpLabel
648 OpReturn
649 OpFunctionEnd
650 )";
651 SetTargetEnv(SPV_ENV_VULKAN_1_0);
652 const auto result =
653 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
654 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
655 }
656
657 TEST_F(TrimCapabilitiesPassTest,
658 StorageInputOutput16_RemainsWithInputPointerArray_Vulkan1_1) {
659 const std::string kTest = R"(
660 OpCapability Shader
661 OpCapability Float16
662 OpCapability StorageInputOutput16
663 OpExtension "SPV_KHR_16bit_storage"
664 ; CHECK: OpCapability StorageInputOutput16
665 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
666 OpMemoryModel Logical GLSL450
667 OpEntryPoint GLCompute %2 "main"
668 %void = OpTypeVoid
669 %half = OpTypeFloat 16
670 %uint = OpTypeInt 32 0
671 %uint_1 = OpConstant %uint 1
672 %array = OpTypeArray %half %uint_1
673 %ptr = OpTypePointer Input %array
674 %1 = OpTypeFunction %void
675 %2 = OpFunction %void None %1
676 %3 = OpLabel
677 OpReturn
678 OpFunctionEnd
679 )";
680 SetTargetEnv(SPV_ENV_VULKAN_1_1);
681 const auto result =
682 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
683 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
684 }
685
686 TEST_F(TrimCapabilitiesPassTest,
687 StorageInputOutput16_RemainsWithInputPointerStruct_Vulkan1_0) {
688 const std::string kTest = R"(
689 OpCapability Shader
690 OpCapability Float16
691 OpCapability StorageInputOutput16
692 OpExtension "SPV_KHR_16bit_storage"
693 ; CHECK: OpCapability StorageInputOutput16
694 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
695 OpMemoryModel Logical GLSL450
696 OpEntryPoint GLCompute %2 "main"
697 %void = OpTypeVoid
698 %half = OpTypeFloat 16
699 %struct = OpTypeStruct %half
700 %ptr = OpTypePointer Input %struct
701 %1 = OpTypeFunction %void
702 %2 = OpFunction %void None %1
703 %3 = OpLabel
704 OpReturn
705 OpFunctionEnd
706 )";
707 SetTargetEnv(SPV_ENV_VULKAN_1_0);
708 const auto result =
709 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
710 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
711 }
712
713 TEST_F(TrimCapabilitiesPassTest,
714 StorageInputOutput16_RemainsWithInputPointerStruct_Vulkan1_1) {
715 const std::string kTest = R"(
716 OpCapability Shader
717 OpCapability Float16
718 OpCapability StorageInputOutput16
719 OpExtension "SPV_KHR_16bit_storage"
720 ; CHECK: OpCapability StorageInputOutput16
721 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
722 OpMemoryModel Logical GLSL450
723 OpEntryPoint GLCompute %2 "main"
724 %void = OpTypeVoid
725 %half = OpTypeFloat 16
726 %struct = OpTypeStruct %half
727 %ptr = OpTypePointer Input %struct
728 %1 = OpTypeFunction %void
729 %2 = OpFunction %void None %1
730 %3 = OpLabel
731 OpReturn
732 OpFunctionEnd
733 )";
734 SetTargetEnv(SPV_ENV_VULKAN_1_1);
735 const auto result =
736 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
737 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
738 }
739
740 TEST_F(TrimCapabilitiesPassTest,
741 StorageInputOutput16_RemainsWithInputPointerStructOfStruct_Vulkan1_0) {
742 const std::string kTest = R"(
743 OpCapability Shader
744 OpCapability Float16
745 OpCapability StorageInputOutput16
746 OpExtension "SPV_KHR_16bit_storage"
747 ; CHECK: OpCapability StorageInputOutput16
748 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
749 OpMemoryModel Logical GLSL450
750 OpEntryPoint GLCompute %2 "main"
751 %void = OpTypeVoid
752 %half = OpTypeFloat 16
753 %float = OpTypeFloat 32
754 %struct = OpTypeStruct %float %half
755 %parent = OpTypeStruct %float %struct
756 %ptr = OpTypePointer Input %parent
757 %1 = OpTypeFunction %void
758 %2 = OpFunction %void None %1
759 %3 = OpLabel
760 OpReturn
761 OpFunctionEnd
762 )";
763 SetTargetEnv(SPV_ENV_VULKAN_1_0);
764 const auto result =
765 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
766 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
767 }
768
769 TEST_F(TrimCapabilitiesPassTest,
770 StorageInputOutput16_RemainsWithInputPointerStructOfStruct_Vulkan1_1) {
771 const std::string kTest = R"(
772 OpCapability Shader
773 OpCapability Float16
774 OpCapability StorageInputOutput16
775 OpExtension "SPV_KHR_16bit_storage"
776 ; CHECK: OpCapability StorageInputOutput16
777 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
778 OpMemoryModel Logical GLSL450
779 OpEntryPoint GLCompute %2 "main"
780 %void = OpTypeVoid
781 %half = OpTypeFloat 16
782 %float = OpTypeFloat 32
783 %struct = OpTypeStruct %float %half
784 %parent = OpTypeStruct %float %struct
785 %ptr = OpTypePointer Input %parent
786 %1 = OpTypeFunction %void
787 %2 = OpFunction %void None %1
788 %3 = OpLabel
789 OpReturn
790 OpFunctionEnd
791 )";
792 SetTargetEnv(SPV_ENV_VULKAN_1_1);
793 const auto result =
794 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
795 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
796 }
797
798 TEST_F(TrimCapabilitiesPassTest,
799 StorageInputOutput16_RemainsWithInputPointerArrayOfStruct_Vulkan1_0) {
800 const std::string kTest = R"(
801 OpCapability Shader
802 OpCapability Float16
803 OpCapability StorageInputOutput16
804 OpExtension "SPV_KHR_16bit_storage"
805 ; CHECK: OpCapability StorageInputOutput16
806 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
807 OpMemoryModel Logical GLSL450
808 OpEntryPoint GLCompute %2 "main"
809 %void = OpTypeVoid
810 %half = OpTypeFloat 16
811 %struct = OpTypeStruct %half
812 %uint = OpTypeInt 32 0
813 %uint_1 = OpConstant %uint 1
814 %array = OpTypeArray %struct %uint_1
815 %ptr = OpTypePointer Input %array
816 %1 = OpTypeFunction %void
817 %2 = OpFunction %void None %1
818 %3 = OpLabel
819 OpReturn
820 OpFunctionEnd
821 )";
822 SetTargetEnv(SPV_ENV_VULKAN_1_0);
823 const auto result =
824 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
825 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
826 }
827
828 TEST_F(TrimCapabilitiesPassTest,
829 StorageInputOutput16_RemainsWithInputPointerArrayOfStruct_Vulkan1_1) {
830 const std::string kTest = R"(
831 OpCapability Shader
832 OpCapability Float16
833 OpCapability StorageInputOutput16
834 OpExtension "SPV_KHR_16bit_storage"
835 ; CHECK: OpCapability StorageInputOutput16
836 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
837 OpMemoryModel Logical GLSL450
838 OpEntryPoint GLCompute %2 "main"
839 %void = OpTypeVoid
840 %half = OpTypeFloat 16
841 %struct = OpTypeStruct %half
842 %uint = OpTypeInt 32 0
843 %uint_1 = OpConstant %uint 1
844 %array = OpTypeArray %struct %uint_1
845 %ptr = OpTypePointer Input %array
846 %1 = OpTypeFunction %void
847 %2 = OpFunction %void None %1
848 %3 = OpLabel
849 OpReturn
850 OpFunctionEnd
851 )";
852 SetTargetEnv(SPV_ENV_VULKAN_1_1);
853 const auto result =
854 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
855 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
856 }
857
858 TEST_F(TrimCapabilitiesPassTest,
859 StorageInputOutput16_RemainsWithInputPointerVector_Vulkan1_0) {
860 const std::string kTest = R"(
861 OpCapability Shader
862 OpCapability Float16
863 OpCapability StorageInputOutput16
864 OpExtension "SPV_KHR_16bit_storage"
865 ; CHECK: OpCapability StorageInputOutput16
866 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
867 OpMemoryModel Logical GLSL450
868 OpEntryPoint GLCompute %2 "main"
869 %void = OpTypeVoid
870 %half = OpTypeFloat 16
871 %vector = OpTypeVector %half 4
872 %ptr = OpTypePointer Input %vector
873 %1 = OpTypeFunction %void
874 %2 = OpFunction %void None %1
875 %3 = OpLabel
876 OpReturn
877 OpFunctionEnd
878 )";
879 SetTargetEnv(SPV_ENV_VULKAN_1_0);
880 const auto result =
881 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
882 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
883 }
884
885 TEST_F(TrimCapabilitiesPassTest,
886 StorageInputOutput16_RemainsWithInputPointerVector_Vulkan1_1) {
887 const std::string kTest = R"(
888 OpCapability Shader
889 OpCapability Float16
890 OpCapability StorageInputOutput16
891 OpExtension "SPV_KHR_16bit_storage"
892 ; CHECK: OpCapability StorageInputOutput16
893 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
894 OpMemoryModel Logical GLSL450
895 OpEntryPoint GLCompute %2 "main"
896 %void = OpTypeVoid
897 %half = OpTypeFloat 16
898 %vector = OpTypeVector %half 4
899 %ptr = OpTypePointer Input %vector
900 %1 = OpTypeFunction %void
901 %2 = OpFunction %void None %1
902 %3 = OpLabel
903 OpReturn
904 OpFunctionEnd
905 )";
906 SetTargetEnv(SPV_ENV_VULKAN_1_1);
907 const auto result =
908 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
909 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
910 }
911
912 TEST_F(TrimCapabilitiesPassTest,
913 StorageInputOutput16_RemainsWithInputPointerMatrix_Vulkan1_0) {
914 const std::string kTest = R"(
915 OpCapability Shader
916 OpCapability Float16
917 OpCapability StorageInputOutput16
918 OpExtension "SPV_KHR_16bit_storage"
919 ; CHECK: OpCapability StorageInputOutput16
920 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
921 OpMemoryModel Logical GLSL450
922 OpEntryPoint GLCompute %2 "main"
923 %void = OpTypeVoid
924 %half = OpTypeFloat 16
925 %vector = OpTypeVector %half 4
926 %matrix = OpTypeMatrix %vector 4
927 %ptr = OpTypePointer Input %matrix
928 %1 = OpTypeFunction %void
929 %2 = OpFunction %void None %1
930 %3 = OpLabel
931 OpReturn
932 OpFunctionEnd
933 )";
934 SetTargetEnv(SPV_ENV_VULKAN_1_0);
935 const auto result =
936 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
937 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
938 }
939
940 TEST_F(TrimCapabilitiesPassTest,
941 StorageInputOutput16_RemainsWithInputPointerMatrix_Vulkan1_1) {
942 const std::string kTest = R"(
943 OpCapability Shader
944 OpCapability Float16
945 OpCapability StorageInputOutput16
946 OpExtension "SPV_KHR_16bit_storage"
947 ; CHECK: OpCapability StorageInputOutput16
948 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
949 OpMemoryModel Logical GLSL450
950 OpEntryPoint GLCompute %2 "main"
951 %void = OpTypeVoid
952 %half = OpTypeFloat 16
953 %vector = OpTypeVector %half 4
954 %matrix = OpTypeMatrix %vector 4
955 %ptr = OpTypePointer Input %matrix
956 %1 = OpTypeFunction %void
957 %2 = OpFunction %void None %1
958 %3 = OpLabel
959 OpReturn
960 OpFunctionEnd
961 )";
962 SetTargetEnv(SPV_ENV_VULKAN_1_1);
963 const auto result =
964 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
965 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
966 }
967
968 TEST_F(TrimCapabilitiesPassTest,
969 StorageInputOutput16_IsRemovedWithoutInputPointer) {
970 const std::string kTest = R"(
971 OpCapability Shader
972 OpCapability Float16
973 OpCapability StorageInputOutput16
974 OpExtension "SPV_KHR_16bit_storage"
975 ; CHECK-NOT: OpCapability StorageInputOutput16
976 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
977 OpMemoryModel Logical GLSL450
978 OpEntryPoint GLCompute %2 "main"
979 %void = OpTypeVoid
980 %1 = OpTypeFunction %void
981 %2 = OpFunction %void None %1
982 %3 = OpLabel
983 OpReturn
984 OpFunctionEnd
985 )";
986 SetTargetEnv(SPV_ENV_VULKAN_1_0);
987 const auto result =
988 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
989 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
990 }
991
992 TEST_F(TrimCapabilitiesPassTest,
993 StorageInputOutput16_RemainsWithOutputPointer_Vulkan1_0) {
994 const std::string kTest = R"(
995 OpCapability Shader
996 OpCapability Float16
997 OpCapability StorageInputOutput16
998 OpExtension "SPV_KHR_16bit_storage"
999 ; CHECK: OpCapability StorageInputOutput16
1000 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
1001 OpMemoryModel Logical GLSL450
1002 OpEntryPoint GLCompute %2 "main"
1003 %void = OpTypeVoid
1004 %half = OpTypeFloat 16
1005 %ptr = OpTypePointer Output %half
1006 %1 = OpTypeFunction %void
1007 %2 = OpFunction %void None %1
1008 %3 = OpLabel
1009 OpReturn
1010 OpFunctionEnd
1011 )";
1012 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1013 const auto result =
1014 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1015 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1016 }
1017
1018 TEST_F(TrimCapabilitiesPassTest,
1019 StorageInputOutput16_RemainsWithOutputPointer_Vulkan1_1) {
1020 const std::string kTest = R"(
1021 OpCapability Shader
1022 OpCapability Float16
1023 OpCapability StorageInputOutput16
1024 OpExtension "SPV_KHR_16bit_storage"
1025 ; CHECK: OpCapability StorageInputOutput16
1026 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1027 OpMemoryModel Logical GLSL450
1028 OpEntryPoint GLCompute %2 "main"
1029 %void = OpTypeVoid
1030 %half = OpTypeFloat 16
1031 %ptr = OpTypePointer Output %half
1032 %1 = OpTypeFunction %void
1033 %2 = OpFunction %void None %1
1034 %3 = OpLabel
1035 OpReturn
1036 OpFunctionEnd
1037 )";
1038 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1039 const auto result =
1040 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1041 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1042 }
1043
1044 TEST_F(TrimCapabilitiesPassTest,
1045 StorageInputOutput16_RemovedWithoutOutputPointer) {
1046 const std::string kTest = R"(
1047 OpCapability Shader
1048 OpCapability Float16
1049 OpCapability StorageInputOutput16
1050 OpExtension "SPV_KHR_16bit_storage"
1051 ; CHECK-NOT: OpCapability StorageInputOutput16
1052 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1053 OpMemoryModel Logical GLSL450
1054 OpEntryPoint GLCompute %2 "main"
1055 %void = OpTypeVoid
1056 %1 = OpTypeFunction %void
1057 %2 = OpFunction %void None %1
1058 %3 = OpLabel
1059 OpReturn
1060 OpFunctionEnd
1061 )";
1062 const auto result =
1063 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1064 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1065 }
1066
1067 TEST_F(TrimCapabilitiesPassTest,
1068 StoragePushConstant16_RemainsSimplePointer_Vulkan1_0) {
1069 const std::string kTest = R"(
1070 OpCapability Shader
1071 OpCapability Float16
1072 OpCapability StoragePushConstant16
1073 OpExtension "SPV_KHR_16bit_storage"
1074 ; CHECK: OpCapability StoragePushConstant16
1075 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
1076 OpMemoryModel Logical GLSL450
1077 OpEntryPoint GLCompute %2 "main"
1078 %void = OpTypeVoid
1079 %half = OpTypeFloat 16
1080 %ptr = OpTypePointer PushConstant %half
1081 %1 = OpTypeFunction %void
1082 %2 = OpFunction %void None %1
1083 %3 = OpLabel
1084 OpReturn
1085 OpFunctionEnd
1086 )";
1087 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1088 const auto result =
1089 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1090 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1091 }
1092
1093 TEST_F(TrimCapabilitiesPassTest,
1094 StoragePushConstant16_RemainsSimplePointer_Vulkan1_1) {
1095 const std::string kTest = R"(
1096 OpCapability Shader
1097 OpCapability Float16
1098 OpCapability StoragePushConstant16
1099 OpExtension "SPV_KHR_16bit_storage"
1100 ; CHECK: OpCapability StoragePushConstant16
1101 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1102 OpMemoryModel Logical GLSL450
1103 OpEntryPoint GLCompute %2 "main"
1104 %void = OpTypeVoid
1105 %half = OpTypeFloat 16
1106 %ptr = OpTypePointer PushConstant %half
1107 %1 = OpTypeFunction %void
1108 %2 = OpFunction %void None %1
1109 %3 = OpLabel
1110 OpReturn
1111 OpFunctionEnd
1112 )";
1113 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1114 const auto result =
1115 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1116 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1117 }
1118
1119 TEST_F(TrimCapabilitiesPassTest, StoragePushConstant16_RemovedSimplePointer) {
1120 const std::string kTest = R"(
1121 OpCapability Shader
1122 OpCapability Float16
1123 OpCapability StoragePushConstant16
1124 OpExtension "SPV_KHR_16bit_storage"
1125 ; CHECK-NOT: OpCapability StoragePushConstant16
1126 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1127 OpMemoryModel Logical GLSL450
1128 OpEntryPoint GLCompute %2 "main"
1129 %void = OpTypeVoid
1130 %half = OpTypeFloat 16
1131 %ptr = OpTypePointer Function %half
1132 %1 = OpTypeFunction %void
1133 %2 = OpFunction %void None %1
1134 %3 = OpLabel
1135 OpReturn
1136 OpFunctionEnd
1137 )";
1138 const auto result =
1139 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1140 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1141 }
1142
1143 TEST_F(TrimCapabilitiesPassTest,
1144 StorageUniformBufferBlock16_RemainsSimplePointer_Vulkan1_0) {
1145 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1146 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1147 spv::Capability::StorageBuffer16BitAccess);
1148
1149 const std::string kTest = R"(
1150 OpCapability Shader
1151 OpCapability Float16
1152 OpCapability StorageBuffer16BitAccess
1153 OpExtension "SPV_KHR_16bit_storage"
1154
1155 ; CHECK: OpCapability StorageBuffer16BitAccess
1156 ; `-> StorageUniformBufferBlock16
1157 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
1158
1159 OpMemoryModel Logical GLSL450
1160 OpEntryPoint GLCompute %2 "main"
1161 OpDecorate %struct BufferBlock
1162 %void = OpTypeVoid
1163 %half = OpTypeFloat 16
1164 %struct = OpTypeStruct %half
1165 %ptr = OpTypePointer Uniform %struct
1166 %1 = OpTypeFunction %void
1167 %2 = OpFunction %void None %1
1168 %3 = OpLabel
1169 OpReturn
1170 OpFunctionEnd
1171 )";
1172 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1173 const auto result =
1174 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1175 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1176 }
1177
1178 TEST_F(TrimCapabilitiesPassTest,
1179 StorageUniformBufferBlock16_RemainsSimplePointer_Vulkan1_1) {
1180 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1181 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1182 spv::Capability::StorageBuffer16BitAccess);
1183
1184 const std::string kTest = R"(
1185 OpCapability Shader
1186 OpCapability Float16
1187 OpCapability StorageBuffer16BitAccess
1188 OpExtension "SPV_KHR_16bit_storage"
1189
1190 ; CHECK: OpCapability StorageBuffer16BitAccess
1191 ; `-> StorageUniformBufferBlock16
1192 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1193
1194 OpMemoryModel Logical GLSL450
1195 OpEntryPoint GLCompute %2 "main"
1196 OpDecorate %struct BufferBlock
1197 %void = OpTypeVoid
1198 %half = OpTypeFloat 16
1199 %struct = OpTypeStruct %half
1200 %ptr = OpTypePointer Uniform %struct
1201 %1 = OpTypeFunction %void
1202 %2 = OpFunction %void None %1
1203 %3 = OpLabel
1204 OpReturn
1205 OpFunctionEnd
1206 )";
1207 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1208 const auto result =
1209 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1210 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1211 }
1212
1213 TEST_F(TrimCapabilitiesPassTest,
1214 StorageUniformBufferBlock16_RemovedSimplePointer) {
1215 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1216 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1217 spv::Capability::StorageBuffer16BitAccess);
1218
1219 const std::string kTest = R"(
1220 OpCapability Shader
1221 OpCapability Float16
1222 OpCapability StorageBuffer16BitAccess
1223 OpExtension "SPV_KHR_16bit_storage"
1224
1225 ; CHECK-NOT: OpCapability StorageBuffer16BitAccess
1226 ; `-> StorageUniformBufferBlock16
1227 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1228
1229 OpMemoryModel Logical GLSL450
1230 OpEntryPoint GLCompute %2 "main"
1231 %void = OpTypeVoid
1232 %half = OpTypeFloat 16
1233 %struct = OpTypeStruct %half
1234 %ptr = OpTypePointer Function %struct
1235 %1 = OpTypeFunction %void
1236 %2 = OpFunction %void None %1
1237 %3 = OpLabel
1238 OpReturn
1239 OpFunctionEnd
1240 )";
1241 const auto result =
1242 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1243 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1244 }
1245
1246 TEST_F(TrimCapabilitiesPassTest,
1247 StorageUniform16_RemovedWithBufferBlockPointer_Vulkan1_0) {
1248 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1249 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1250 spv::Capability::StorageBuffer16BitAccess);
1251 static_assert(spv::Capability::StorageUniform16 ==
1252 spv::Capability::UniformAndStorageBuffer16BitAccess);
1253
1254 const std::string kTest = R"(
1255 OpCapability Shader
1256 OpCapability Float16
1257 OpCapability StorageBuffer16BitAccess
1258 OpCapability UniformAndStorageBuffer16BitAccess
1259 OpExtension "SPV_KHR_16bit_storage"
1260
1261 ; CHECK: OpCapability StorageBuffer16BitAccess
1262 ; `-> StorageUniformBufferBlock16
1263 ; CHECK-NOT: OpCapability UniformAndStorageBuffer16BitAccess
1264 ; `-> StorageUniform16
1265 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
1266
1267 OpMemoryModel Logical GLSL450
1268 OpEntryPoint GLCompute %2 "main"
1269 OpDecorate %struct BufferBlock
1270 %void = OpTypeVoid
1271 %half = OpTypeFloat 16
1272 %struct = OpTypeStruct %half
1273 %ptr = OpTypePointer Uniform %struct
1274 %1 = OpTypeFunction %void
1275 %2 = OpFunction %void None %1
1276 %3 = OpLabel
1277 OpReturn
1278 OpFunctionEnd
1279 )";
1280 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1281 const auto result =
1282 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1283 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1284 }
1285
1286 TEST_F(TrimCapabilitiesPassTest,
1287 StorageUniform16_RemovedWithBufferBlockPointer_Vulkan1_1) {
1288 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1289 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1290 spv::Capability::StorageBuffer16BitAccess);
1291 static_assert(spv::Capability::StorageUniform16 ==
1292 spv::Capability::UniformAndStorageBuffer16BitAccess);
1293
1294 const std::string kTest = R"(
1295 OpCapability Shader
1296 OpCapability Float16
1297 OpCapability StorageBuffer16BitAccess
1298 OpCapability UniformAndStorageBuffer16BitAccess
1299 OpExtension "SPV_KHR_16bit_storage"
1300
1301 ; CHECK: OpCapability StorageBuffer16BitAccess
1302 ; `-> StorageUniformBufferBlock16
1303 ; CHECK-NOT: OpCapability UniformAndStorageBuffer16BitAccess
1304 ; `-> StorageUniform16
1305 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1306
1307 OpMemoryModel Logical GLSL450
1308 OpEntryPoint GLCompute %2 "main"
1309 OpDecorate %struct BufferBlock
1310 %void = OpTypeVoid
1311 %half = OpTypeFloat 16
1312 %struct = OpTypeStruct %half
1313 %ptr = OpTypePointer Uniform %struct
1314 %1 = OpTypeFunction %void
1315 %2 = OpFunction %void None %1
1316 %3 = OpLabel
1317 OpReturn
1318 OpFunctionEnd
1319 )";
1320 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1321 const auto result =
1322 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1323 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1324 }
1325
1326 TEST_F(TrimCapabilitiesPassTest,
1327 StorageUniform16_RemovedWithNonBlockUniformPointer_Vulkan1_0) {
1328 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1329 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1330 spv::Capability::StorageBuffer16BitAccess);
1331 static_assert(spv::Capability::StorageUniform16 ==
1332 spv::Capability::UniformAndStorageBuffer16BitAccess);
1333
1334 const std::string kTest = R"(
1335 OpCapability Shader
1336 OpCapability Float16
1337 OpCapability StorageBuffer16BitAccess
1338 OpCapability UniformAndStorageBuffer16BitAccess
1339 OpExtension "SPV_KHR_16bit_storage"
1340
1341 ; CHECK-NOT: OpCapability StorageBuffer16BitAccess
1342 ; `-> StorageUniformBufferBlock16
1343 ; CHECK: OpCapability UniformAndStorageBuffer16BitAccess
1344 ; `-> StorageUniform16
1345 ; CHECK: OpExtension "SPV_KHR_16bit_storage"
1346
1347 OpMemoryModel Logical GLSL450
1348 OpEntryPoint GLCompute %2 "main"
1349 %void = OpTypeVoid
1350 %half = OpTypeFloat 16
1351 %struct = OpTypeStruct %half
1352 %ptr = OpTypePointer Uniform %struct
1353 %1 = OpTypeFunction %void
1354 %2 = OpFunction %void None %1
1355 %3 = OpLabel
1356 OpReturn
1357 OpFunctionEnd
1358 )";
1359 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1360 const auto result =
1361 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1362 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1363 }
1364
1365 TEST_F(TrimCapabilitiesPassTest,
1366 StorageUniform16_RemovedWithNonBlockUniformPointer_Vulkan1_1) {
1367 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1368 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1369 spv::Capability::StorageBuffer16BitAccess);
1370 static_assert(spv::Capability::StorageUniform16 ==
1371 spv::Capability::UniformAndStorageBuffer16BitAccess);
1372
1373 const std::string kTest = R"(
1374 OpCapability Shader
1375 OpCapability Float16
1376 OpCapability StorageBuffer16BitAccess
1377 OpCapability UniformAndStorageBuffer16BitAccess
1378 OpExtension "SPV_KHR_16bit_storage"
1379
1380 ; CHECK-NOT: OpCapability StorageBuffer16BitAccess
1381 ; `-> StorageUniformBufferBlock16
1382 ; CHECK: OpCapability UniformAndStorageBuffer16BitAccess
1383 ; `-> StorageUniform16
1384 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1385
1386 OpMemoryModel Logical GLSL450
1387 OpEntryPoint GLCompute %2 "main"
1388 %void = OpTypeVoid
1389 %half = OpTypeFloat 16
1390 %struct = OpTypeStruct %half
1391 %ptr = OpTypePointer Uniform %struct
1392 %1 = OpTypeFunction %void
1393 %2 = OpFunction %void None %1
1394 %3 = OpLabel
1395 OpReturn
1396 OpFunctionEnd
1397 )";
1398 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1399 const auto result =
1400 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1401 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1402 }
1403
1404 TEST_F(TrimCapabilitiesPassTest,
1405 StorageBuffer16BitAccess_RemainsSimplePointer_Vulkan1_0) {
1406 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1407 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1408 spv::Capability::StorageBuffer16BitAccess);
1409
1410 const std::string kTest = R"(
1411 OpCapability Shader
1412 OpCapability Float16
1413 OpCapability StorageBuffer16BitAccess
1414
1415 ; CHECK: OpCapability StorageBuffer16BitAccess
1416 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1417
1418 OpMemoryModel Logical GLSL450
1419 OpEntryPoint GLCompute %2 "main"
1420 OpDecorate %struct Block
1421 %void = OpTypeVoid
1422 %half = OpTypeFloat 16
1423 %struct = OpTypeStruct %half
1424 %ptr = OpTypePointer StorageBuffer %struct
1425 %1 = OpTypeFunction %void
1426 %2 = OpFunction %void None %1
1427 %3 = OpLabel
1428 OpReturn
1429 OpFunctionEnd
1430 )";
1431 SetTargetEnv(SPV_ENV_VULKAN_1_0);
1432 const auto result =
1433 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1434 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1435 }
1436
1437 TEST_F(TrimCapabilitiesPassTest,
1438 StorageBuffer16BitAccess_RemainsSimplePointer_Vulkan1_1) {
1439 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1440 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1441 spv::Capability::StorageBuffer16BitAccess);
1442
1443 const std::string kTest = R"(
1444 OpCapability Shader
1445 OpCapability Float16
1446 OpCapability StorageBuffer16BitAccess
1447
1448 ; CHECK: OpCapability StorageBuffer16BitAccess
1449 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1450
1451 OpMemoryModel Logical GLSL450
1452 OpEntryPoint GLCompute %2 "main"
1453 OpDecorate %struct Block
1454 %void = OpTypeVoid
1455 %half = OpTypeFloat 16
1456 %struct = OpTypeStruct %half
1457 %ptr = OpTypePointer StorageBuffer %struct
1458 %1 = OpTypeFunction %void
1459 %2 = OpFunction %void None %1
1460 %3 = OpLabel
1461 OpReturn
1462 OpFunctionEnd
1463 )";
1464 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1465 const auto result =
1466 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1467 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1468 }
1469
1470 TEST_F(
1471 TrimCapabilitiesPassTest,
1472 StorageBuffer16BitAccess_RemainsSimplePointerUshortPhysicalStorage_Vulkan1_1) {
1473 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1474 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1475 spv::Capability::StorageBuffer16BitAccess);
1476
1477 const std::string kTest = R"(
1478 OpCapability Shader
1479 OpCapability StorageBuffer16BitAccess
1480 OpCapability PhysicalStorageBufferAddresses
1481
1482 ; CHECK: OpCapability StorageBuffer16BitAccess
1483 ; CHECK: OpCapability PhysicalStorageBufferAddresses
1484 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1485 ; CHECK-NOT: OpCapability Int16
1486
1487 OpMemoryModel Logical GLSL450
1488 OpEntryPoint GLCompute %2 "main"
1489 OpDecorate %struct Block
1490 %void = OpTypeVoid
1491 %ushort = OpTypeInt 16 0
1492 %struct = OpTypeStruct %ushort
1493 %ptr = OpTypePointer PhysicalStorageBuffer %struct
1494 %1 = OpTypeFunction %void
1495 %2 = OpFunction %void None %1
1496 %3 = OpLabel
1497 OpReturn
1498 OpFunctionEnd
1499 )";
1500 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1501 const auto result =
1502 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1503 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1504 }
1505
1506 TEST_F(
1507 TrimCapabilitiesPassTest,
1508 StorageBuffer16BitAccess_RemainsSimplePointerUshortStorageBuffer_Vulkan1_1) {
1509 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1510 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1511 spv::Capability::StorageBuffer16BitAccess);
1512
1513 const std::string kTest = R"(
1514 OpCapability Shader
1515 OpCapability StorageBuffer16BitAccess
1516
1517 ; CHECK: OpCapability StorageBuffer16BitAccess
1518 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1519 ; CHECK-NOT: OpCapability Int16
1520
1521 OpMemoryModel Logical GLSL450
1522 OpEntryPoint GLCompute %2 "main"
1523 OpDecorate %struct Block
1524 %void = OpTypeVoid
1525 %ushort = OpTypeInt 16 0
1526 %struct = OpTypeStruct %ushort
1527 %ptr = OpTypePointer StorageBuffer %struct
1528 %1 = OpTypeFunction %void
1529 %2 = OpFunction %void None %1
1530 %3 = OpLabel
1531 OpReturn
1532 OpFunctionEnd
1533 )";
1534 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1535 const auto result =
1536 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1537 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1538 }
1539
1540 TEST_F(
1541 TrimCapabilitiesPassTest,
1542 StorageBuffer16BitAccess_RemainsSimplePointerUshortRecordBuffer_Vulkan1_1) {
1543 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1544 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1545 spv::Capability::StorageBuffer16BitAccess);
1546
1547 const std::string kTest = R"(
1548 OpCapability Shader
1549 OpCapability RayTracingKHR
1550 OpCapability StorageBuffer16BitAccess
1551 OpExtension "SPV_KHR_ray_tracing"
1552
1553 ; CHECK-NOT: OpCapability Int16
1554 ; CHECK: OpCapability RayTracingKHR
1555 ; CHECK: OpCapability StorageBuffer16BitAccess
1556 ; CHECK: OpExtension "SPV_KHR_ray_tracing"
1557 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1558
1559 OpMemoryModel Logical GLSL450
1560 OpEntryPoint GLCompute %2 "main"
1561 OpDecorate %struct Block
1562 %void = OpTypeVoid
1563 %ushort = OpTypeInt 16 0
1564 %struct = OpTypeStruct %ushort
1565 %ptr = OpTypePointer ShaderRecordBufferKHR %struct
1566 %1 = OpTypeFunction %void
1567 %2 = OpFunction %void None %1
1568 %3 = OpLabel
1569 OpReturn
1570 OpFunctionEnd
1571 )";
1572 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1573 const auto result =
1574 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1575 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1576 }
1577
1578 TEST_F(TrimCapabilitiesPassTest,
1579 StorageBuffer16BitAccess_TrimRecordBuffer_Vulkan1_1) {
1580 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1581 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1582 spv::Capability::StorageBuffer16BitAccess);
1583
1584 const std::string kTest = R"(
1585 OpCapability Shader
1586 OpCapability RayTracingKHR
1587 OpCapability StorageBuffer16BitAccess
1588 OpExtension "SPV_KHR_ray_tracing"
1589
1590 ; CHECK-NOT: OpCapability RayTracingKHR
1591 ; CHECK-NOT: OpCapability StorageBuffer16BitAccess
1592 ; CHECK-NOT: OpExtension "SPV_KHR_ray_tracing"
1593
1594 OpMemoryModel Logical GLSL450
1595 OpEntryPoint GLCompute %2 "main"
1596 OpDecorate %struct Block
1597 %void = OpTypeVoid
1598 %1 = OpTypeFunction %void
1599 %2 = OpFunction %void None %1
1600 %3 = OpLabel
1601 OpReturn
1602 OpFunctionEnd
1603 )";
1604 SetTargetEnv(SPV_ENV_VULKAN_1_1);
1605 const auto result =
1606 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1607 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1608 }
1609
1610 TEST_F(TrimCapabilitiesPassTest,
1611 StorageBuffer16BitAccess_RemovedSimplePointer) {
1612 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/5354
1613 static_assert(spv::Capability::StorageUniformBufferBlock16 ==
1614 spv::Capability::StorageBuffer16BitAccess);
1615
1616 const std::string kTest = R"(
1617 OpCapability Shader
1618 OpCapability Float16
1619 OpCapability StorageBuffer16BitAccess
1620 OpExtension "SPV_KHR_16bit_storage"
1621
1622 ; CHECK-NOT: OpCapability StorageBuffer16BitAccess
1623 ; CHECK-NOT: OpExtension "SPV_KHR_16bit_storage"
1624
1625 OpMemoryModel Logical GLSL450
1626 OpEntryPoint GLCompute %2 "main"
1627 %void = OpTypeVoid
1628 %half = OpTypeFloat 16
1629 %struct = OpTypeStruct %half
1630 %ptr = OpTypePointer Function %struct
1631 %1 = OpTypeFunction %void
1632 %2 = OpFunction %void None %1
1633 %3 = OpLabel
1634 OpReturn
1635 OpFunctionEnd
1636 )";
1637 const auto result =
1638 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1639 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1640 }
1641
1642 TEST_F(TrimCapabilitiesPassTest, FragmentShaderInterlock_RemovedIfNotUsed) {
1643 const std::string kTest = R"(
1644 OpCapability Shader
1645 OpCapability FragmentShaderPixelInterlockEXT
1646 OpCapability FragmentShaderSampleInterlockEXT
1647 OpCapability FragmentShaderShadingRateInterlockEXT
1648 OpExtension "SPV_EXT_fragment_shader_interlock"
1649 ; CHECK-NOT: OpCapability FragmentShaderPixelInterlockEXT
1650 ; CHECK-NOT: OpCapability FragmentShaderSampleInterlockEXT
1651 ; CHECK-NOT: OpCapability FragmentShaderShadingRateInterlockEXT
1652 ; CHECK-NOT: OpExtension "SPV_EXT_fragment_shader_interlock"
1653 OpMemoryModel Logical GLSL450
1654 OpEntryPoint GLCompute %1 "main"
1655 %void = OpTypeVoid
1656 %1 = OpTypeFunction %void
1657 %2 = OpFunction %void None %1
1658 %3 = OpLabel
1659 OpReturn
1660 OpFunctionEnd;
1661 )";
1662 const auto result =
1663 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1664 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1665 }
1666
1667 TEST_F(TrimCapabilitiesPassTest,
1668 FragmentShaderPixelInterlock_RemainsWhenOrderedIsUsed) {
1669 const std::string kTest = R"(
1670 OpCapability Shader
1671 OpCapability FragmentShaderPixelInterlockEXT
1672 OpCapability FragmentShaderSampleInterlockEXT
1673 OpCapability FragmentShaderShadingRateInterlockEXT
1674 OpExtension "SPV_EXT_fragment_shader_interlock"
1675 ; CHECK: OpCapability FragmentShaderPixelInterlockEXT
1676 ; CHECK-NOT: OpCapability FragmentShaderSampleInterlockEXT
1677 ; CHECK-NOT: OpCapability FragmentShaderShadingRateInterlockEXT
1678 ; CHECK: OpExtension "SPV_EXT_fragment_shader_interlock"
1679 OpMemoryModel Logical GLSL450
1680 OpEntryPoint GLCompute %1 "main"
1681 OpExecutionMode %main PixelInterlockOrderedEXT
1682 %void = OpTypeVoid
1683 %1 = OpTypeFunction %void
1684 %2 = OpFunction %void None %1
1685 %3 = OpLabel
1686 OpBeginInvocationInterlockEXT
1687 OpEndInvocationInterlockEXT
1688 OpReturn
1689 OpFunctionEnd;
1690 )";
1691 const auto result =
1692 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1693 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1694 }
1695
1696 TEST_F(TrimCapabilitiesPassTest,
1697 FragmentShaderPixelInterlock_RemainsWhenUnorderedIsUsed) {
1698 const std::string kTest = R"(
1699 OpCapability Shader
1700 OpCapability FragmentShaderPixelInterlockEXT
1701 OpCapability FragmentShaderSampleInterlockEXT
1702 OpCapability FragmentShaderShadingRateInterlockEXT
1703 OpExtension "SPV_EXT_fragment_shader_interlock"
1704 ; CHECK: OpCapability FragmentShaderPixelInterlockEXT
1705 ; CHECK-NOT: OpCapability FragmentShaderSampleInterlockEXT
1706 ; CHECK-NOT: OpCapability FragmentShaderShadingRateInterlockEXT
1707 ; CHECK: OpExtension "SPV_EXT_fragment_shader_interlock"
1708 OpMemoryModel Logical GLSL450
1709 OpEntryPoint GLCompute %1 "main"
1710 OpExecutionMode %main PixelInterlockUnorderedEXT
1711 %void = OpTypeVoid
1712 %1 = OpTypeFunction %void
1713 %2 = OpFunction %void None %1
1714 %3 = OpLabel
1715 OpBeginInvocationInterlockEXT
1716 OpEndInvocationInterlockEXT
1717 OpReturn
1718 OpFunctionEnd;
1719 )";
1720 const auto result =
1721 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1722 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1723 }
1724
1725 TEST_F(TrimCapabilitiesPassTest,
1726 FragmentShaderSampleInterlock_RemainsWhenOrderedIsUsed) {
1727 const std::string kTest = R"(
1728 OpCapability Shader
1729 OpCapability FragmentShaderPixelInterlockEXT
1730 OpCapability FragmentShaderSampleInterlockEXT
1731 OpCapability FragmentShaderShadingRateInterlockEXT
1732 OpExtension "SPV_EXT_fragment_shader_interlock"
1733 ; CHECK-NOT: OpCapability FragmentShaderPixelInterlockEXT
1734 ; CHECK: OpCapability FragmentShaderSampleInterlockEXT
1735 ; CHECK-NOT: OpCapability FragmentShaderShadingRateInterlockEXT
1736 ; CHECK: OpExtension "SPV_EXT_fragment_shader_interlock"
1737 OpMemoryModel Logical GLSL450
1738 OpEntryPoint GLCompute %1 "main"
1739 OpExecutionMode %main SampleInterlockOrderedEXT
1740 %void = OpTypeVoid
1741 %1 = OpTypeFunction %void
1742 %2 = OpFunction %void None %1
1743 %3 = OpLabel
1744 OpBeginInvocationInterlockEXT
1745 OpEndInvocationInterlockEXT
1746 OpReturn
1747 OpFunctionEnd;
1748 )";
1749 const auto result =
1750 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1751 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1752 }
1753
1754 TEST_F(TrimCapabilitiesPassTest,
1755 FragmentShaderSampleInterlock_RemainsWhenUnorderedIsUsed) {
1756 const std::string kTest = R"(
1757 OpCapability Shader
1758 OpCapability FragmentShaderPixelInterlockEXT
1759 OpCapability FragmentShaderSampleInterlockEXT
1760 OpCapability FragmentShaderShadingRateInterlockEXT
1761 OpExtension "SPV_EXT_fragment_shader_interlock"
1762 ; CHECK-NOT: OpCapability FragmentShaderPixelInterlockEXT
1763 ; CHECK: OpCapability FragmentShaderSampleInterlockEXT
1764 ; CHECK-NOT: OpCapability FragmentShaderShadingRateInterlockEXT
1765 ; CHECK: OpExtension "SPV_EXT_fragment_shader_interlock"
1766 OpMemoryModel Logical GLSL450
1767 OpEntryPoint GLCompute %1 "main"
1768 OpExecutionMode %main SampleInterlockUnorderedEXT
1769 %void = OpTypeVoid
1770 %1 = OpTypeFunction %void
1771 %2 = OpFunction %void None %1
1772 %3 = OpLabel
1773 OpBeginInvocationInterlockEXT
1774 OpEndInvocationInterlockEXT
1775 OpReturn
1776 OpFunctionEnd;
1777 )";
1778 const auto result =
1779 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1780 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1781 }
1782
1783 TEST_F(TrimCapabilitiesPassTest,
1784 FragmentShaderShadingRateInterlock_RemainsWhenOrderedIsUsed) {
1785 const std::string kTest = R"(
1786 OpCapability Shader
1787 OpCapability FragmentShaderPixelInterlockEXT
1788 OpCapability FragmentShaderSampleInterlockEXT
1789 OpCapability FragmentShaderShadingRateInterlockEXT
1790 OpExtension "SPV_EXT_fragment_shader_interlock"
1791 ; CHECK-NOT: OpCapability FragmentShaderPixelInterlockEXT
1792 ; CHECK-NOT: OpCapability FragmentShaderSampleInterlockEXT
1793 ; CHECK: OpCapability FragmentShaderShadingRateInterlockEXT
1794 ; CHECK: OpExtension "SPV_EXT_fragment_shader_interlock"
1795 OpMemoryModel Logical GLSL450
1796 OpEntryPoint GLCompute %1 "main"
1797 OpExecutionMode %main ShadingRateInterlockOrderedEXT
1798 %void = OpTypeVoid
1799 %1 = OpTypeFunction %void
1800 %2 = OpFunction %void None %1
1801 %3 = OpLabel
1802 OpBeginInvocationInterlockEXT
1803 OpEndInvocationInterlockEXT
1804 OpReturn
1805 OpFunctionEnd;
1806 )";
1807 const auto result =
1808 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1809 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1810 }
1811
1812 TEST_F(TrimCapabilitiesPassTest,
1813 FragmentShaderShadingRateInterlock_RemainsWhenUnorderedIsUsed) {
1814 const std::string kTest = R"(
1815 OpCapability Shader
1816 OpCapability FragmentShaderPixelInterlockEXT
1817 OpCapability FragmentShaderSampleInterlockEXT
1818 OpCapability FragmentShaderShadingRateInterlockEXT
1819 OpExtension "SPV_EXT_fragment_shader_interlock"
1820 ; CHECK-NOT: OpCapability FragmentShaderPixelInterlockEXT
1821 ; CHECK-NOT: OpCapability FragmentShaderSampleInterlockEXT
1822 ; CHECK: OpCapability FragmentShaderShadingRateInterlockEXT
1823 ; CHECK: OpExtension "SPV_EXT_fragment_shader_interlock"
1824 OpMemoryModel Logical GLSL450
1825 OpEntryPoint GLCompute %1 "main"
1826 OpExecutionMode %main ShadingRateInterlockUnorderedEXT
1827 %void = OpTypeVoid
1828 %1 = OpTypeFunction %void
1829 %2 = OpFunction %void None %1
1830 %3 = OpLabel
1831 OpBeginInvocationInterlockEXT
1832 OpEndInvocationInterlockEXT
1833 OpReturn
1834 OpFunctionEnd;
1835 )";
1836 const auto result =
1837 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1838 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1839 }
1840
1841 TEST_F(TrimCapabilitiesPassTest, Int64_RemovedWhenUnused) {
1842 const std::string kTest = R"(
1843 OpCapability Int64
1844 ; CHECK-NOT: OpCapability Int64
1845 OpCapability Shader
1846 OpMemoryModel Logical GLSL450
1847 OpEntryPoint GLCompute %1 "main"
1848 %void = OpTypeVoid
1849 %3 = OpTypeFunction %void
1850 %1 = OpFunction %void None %3
1851 %6 = OpLabel
1852 OpReturn
1853 OpFunctionEnd;
1854 )";
1855 const auto result =
1856 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1857 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1858 }
1859
1860 TEST_F(TrimCapabilitiesPassTest, Int64_RemainsWhenUsed) {
1861 const std::string kTest = R"(
1862 OpCapability Int64
1863 ; CHECK: OpCapability Int64
1864 OpCapability Shader
1865 OpMemoryModel Logical GLSL450
1866 OpEntryPoint GLCompute %1 "main"
1867 %void = OpTypeVoid
1868 %int = OpTypeInt 64 0
1869 %3 = OpTypeFunction %void
1870 %1 = OpFunction %void None %3
1871 %6 = OpLabel
1872 OpReturn
1873 OpFunctionEnd;
1874 )";
1875 const auto result =
1876 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1877 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1878 }
1879
1880 TEST_F(TrimCapabilitiesPassTest, RayQueryKHR_RemovedWhenUnused) {
1881 const std::string kTest = R"(
1882 OpCapability Shader
1883 OpCapability RayQueryKHR
1884 OpExtension "SPV_KHR_ray_query"
1885 ; CHECK-NOT: OpCapability RayQueryKHR
1886 ; CHECK-NOT: OpExtension "SPV_KHR_ray_query"
1887 OpMemoryModel Logical GLSL450
1888 OpEntryPoint Vertex %main "main" %out_var_TEXCOORD1
1889 OpSource HLSL 660
1890 OpName %out_var_TEXCOORD1 "out.var.TEXCOORD1"
1891 OpName %main "main"
1892 OpDecorate %out_var_TEXCOORD1 Flat
1893 OpDecorate %out_var_TEXCOORD1 Location 0
1894 %uint = OpTypeInt 32 0
1895 %uint_1234 = OpConstant %uint 1234
1896 %_ptr_Output_uint = OpTypePointer Output %uint
1897 %void = OpTypeVoid
1898 %7 = OpTypeFunction %void
1899 %out_var_TEXCOORD1 = OpVariable %_ptr_Output_uint Output
1900 %main = OpFunction %void None %7
1901 %8 = OpLabel
1902 OpStore %out_var_TEXCOORD1 %uint_1234
1903 OpReturn
1904 OpFunctionEnd
1905 )";
1906 const auto result =
1907 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1908 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
1909 }
1910
1911 TEST_F(TrimCapabilitiesPassTest,
1912 RayQueryKHR_RemainsWhenAccelerationStructureIsPresent) {
1913 const std::string kTest = R"(
1914 OpCapability Shader
1915 OpCapability RayQueryKHR
1916 OpExtension "SPV_KHR_ray_query"
1917 ; CHECK: OpCapability RayQueryKHR
1918 ; CHECK: OpExtension "SPV_KHR_ray_query"
1919 OpMemoryModel Logical GLSL450
1920 OpEntryPoint GLCompute %main "main"
1921 OpExecutionMode %main LocalSize 1 2 4
1922 OpDecorate %var_bvh DescriptorSet 0
1923 OpDecorate %var_bvh Binding 0
1924 %bvh = OpTypeAccelerationStructureKHR
1925 %ptr_bvh = OpTypePointer UniformConstant %bvh
1926 %void = OpTypeVoid
1927 %20 = OpTypeFunction %void
1928 %var_bvh = OpVariable %ptr_bvh UniformConstant
1929 %main = OpFunction %void None %20
1930 %30 = OpLabel
1931 OpReturn
1932 OpFunctionEnd
1933 )";
1934 const auto result =
1935 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1936 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1937 }
1938
1939 TEST_F(TrimCapabilitiesPassTest, RayQueryKHR_RemainsWhenRayQueryTypeIsPresent) {
1940 const std::string kTest = R"(
1941 OpCapability Shader
1942 OpCapability RayQueryKHR
1943 OpExtension "SPV_KHR_ray_query"
1944 ; CHECK: OpCapability RayQueryKHR
1945 ; CHECK: OpExtension "SPV_KHR_ray_query"
1946 OpMemoryModel Logical GLSL450
1947 OpEntryPoint GLCompute %main "main"
1948 OpExecutionMode %main LocalSize 1 2 4
1949 %query = OpTypeRayQueryKHR
1950 %void = OpTypeVoid
1951 %20 = OpTypeFunction %void
1952 %ptr_query = OpTypePointer Function %query
1953 %main = OpFunction %void None %20
1954 %30 = OpLabel
1955 %var_query = OpVariable %ptr_query Function
1956 OpReturn
1957 OpFunctionEnd
1958 )";
1959 const auto result =
1960 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
1961 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
1962 }
1963
1964 TEST_F(TrimCapabilitiesPassTest, RayQueryKHR_RemainsWhenUsed) {
1965 const std::string kTest = R"(
1966 OpCapability Shader
1967 OpCapability RayQueryKHR
1968 OpExtension "SPV_KHR_ray_query"
1969 ; CHECK: OpCapability RayQueryKHR
1970 ; CHECK: OpExtension "SPV_KHR_ray_query"
1971 OpMemoryModel Logical GLSL450
1972 OpEntryPoint GLCompute %main "main"
1973 OpExecutionMode %main LocalSize 1 2 4
1974 OpDecorate %bvh DescriptorSet 0
1975 OpDecorate %bvh Binding 0
1976 OpDecorate %output DescriptorSet 0
1977 OpDecorate %output Binding 1
1978 OpDecorate %_runtimearr_float ArrayStride 4
1979 OpMemberDecorate %type_RWStructuredBuffer_float 0 Offset 0
1980 OpDecorate %type_RWStructuredBuffer_float BufferBlock
1981 %float = OpTypeFloat 32
1982 %float_0 = OpConstant %float 0
1983 %int = OpTypeInt 32 1
1984 %v3float = OpTypeVector %float 3
1985 %12 = OpConstantComposite %v3float %float_0 %float_0 %float_0
1986 %int_0 = OpConstant %int 0
1987 %uint = OpTypeInt 32 0
1988 %uint_0 = OpConstant %uint 0
1989 %uint_1 = OpConstant %uint 1
1990 %accelerationStructureKHR = OpTypeAccelerationStructureKHR
1991 %_ptr_UniformConstant_accelerationStructureKHR = OpTypePointer UniformConstant %accelerationStructureKHR
1992 %_runtimearr_float = OpTypeRuntimeArray %float
1993 %type_RWStructuredBuffer_float = OpTypeStruct %_runtimearr_float
1994 %_ptr_Uniform_type_RWStructuredBuffer_float = OpTypePointer Uniform %type_RWStructuredBuffer_float
1995 %void = OpTypeVoid
1996 %20 = OpTypeFunction %void
1997 %rayQueryKHR = OpTypeRayQueryKHR
1998 %_ptr_Function_rayQueryKHR = OpTypePointer Function %rayQueryKHR
1999 %bool = OpTypeBool
2000 %_ptr_Uniform_float = OpTypePointer Uniform %float
2001 %bvh = OpVariable %_ptr_UniformConstant_accelerationStructureKHR UniformConstant
2002 %output = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_float Uniform
2003 %main = OpFunction %void None %20
2004 %24 = OpLabel
2005 %25 = OpVariable %_ptr_Function_rayQueryKHR Function
2006 %26 = OpLoad %accelerationStructureKHR %bvh
2007 OpRayQueryInitializeKHR %25 %26 %uint_0 %uint_0 %12 %float_0 %12 %float_0
2008 %27 = OpRayQueryProceedKHR %bool %25
2009 %28 = OpRayQueryGetIntersectionTypeKHR %uint %25 %uint_1
2010 %29 = OpIEqual %bool %28 %uint_1
2011 OpSelectionMerge %30 None
2012 OpBranchConditional %29 %31 %30
2013 %31 = OpLabel
2014 %32 = OpAccessChain %_ptr_Uniform_float %output %int_0 %uint_0
2015 OpStore %32 %float_0
2016 OpBranch %30
2017 %30 = OpLabel
2018 OpReturn
2019 OpFunctionEnd
2020 )";
2021 const auto result =
2022 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2023 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2024 }
2025
2026 TEST_F(TrimCapabilitiesPassTest,
2027 RayTracingKHR_RemainsWithIntersectionExecutionMode) {
2028 const std::string kTest = R"(
2029 OpCapability RayTracingKHR
2030 OpExtension "SPV_KHR_ray_tracing"
2031 ; CHECK: OpCapability RayTracingKHR
2032 ; CHECK: OpExtension "SPV_KHR_ray_tracing"
2033 OpMemoryModel Logical GLSL450
2034 OpEntryPoint IntersectionKHR %main "main"
2035 OpSource HLSL 660
2036 OpName %main "main"
2037 %void = OpTypeVoid
2038 %3 = OpTypeFunction %void
2039 %main = OpFunction %void None %3
2040 %4 = OpLabel
2041 OpReturn
2042 OpFunctionEnd
2043 )";
2044 const auto result =
2045 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2046 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2047 }
2048
2049 TEST_F(TrimCapabilitiesPassTest,
2050 RayTracingKHR_RemainsWithClosestHitExecutionMode) {
2051 const std::string kTest = R"(
2052 OpCapability RayTracingKHR
2053 OpExtension "SPV_KHR_ray_tracing"
2054 ; CHECK: OpCapability RayTracingKHR
2055 ; CHECK: OpExtension "SPV_KHR_ray_tracing"
2056 OpMemoryModel Logical GLSL450
2057 OpEntryPoint ClosestHitKHR %main "main" %a
2058 OpSource HLSL 630
2059 OpName %Payload "Payload"
2060 OpMemberName %Payload 0 "color"
2061 OpName %a "a"
2062 OpName %main "main"
2063 %float = OpTypeFloat 32
2064 %v4float = OpTypeVector %float 4
2065 %Payload = OpTypeStruct %v4float
2066 %ptr_payload = OpTypePointer IncomingRayPayloadKHR %Payload
2067 %void = OpTypeVoid
2068 %8 = OpTypeFunction %void
2069 %a = OpVariable %ptr_payload IncomingRayPayloadKHR
2070 %main = OpFunction %void None %8
2071 %9 = OpLabel
2072 %10 = OpLoad %Payload %a
2073 OpStore %a %10
2074 OpReturn
2075 OpFunctionEnd
2076
2077 )";
2078 const auto result =
2079 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2080 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2081 }
2082
2083 TEST_F(TrimCapabilitiesPassTest, RayTracingKHR_RemainsWithAnyHitExecutionMode) {
2084 const std::string kTest = R"(
2085 OpCapability RayTracingKHR
2086 OpExtension "SPV_KHR_ray_tracing"
2087 ; CHECK: OpCapability RayTracingKHR
2088 ; CHECK: OpExtension "SPV_KHR_ray_tracing"
2089 OpMemoryModel Logical GLSL450
2090 OpEntryPoint AnyHitKHR %main "main" %a
2091 OpSource HLSL 630
2092 OpName %Payload "Payload"
2093 OpMemberName %Payload 0 "color"
2094 OpName %a "a"
2095 OpName %main "main"
2096 %float = OpTypeFloat 32
2097 %v4float = OpTypeVector %float 4
2098 %Payload = OpTypeStruct %v4float
2099 %ptr_payload = OpTypePointer IncomingRayPayloadKHR %Payload
2100 %void = OpTypeVoid
2101 %8 = OpTypeFunction %void
2102 %a = OpVariable %ptr_payload IncomingRayPayloadKHR
2103 %main = OpFunction %void None %8
2104 %9 = OpLabel
2105 %10 = OpLoad %Payload %a
2106 OpStore %a %10
2107 OpReturn
2108 OpFunctionEnd
2109 )";
2110 const auto result =
2111 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2112 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2113 }
2114
2115 TEST_F(TrimCapabilitiesPassTest, RayTracingKHR_RemainsWithMissExecutionMode) {
2116 const std::string kTest = R"(
2117 OpCapability RayTracingKHR
2118 OpExtension "SPV_KHR_ray_tracing"
2119 ; CHECK: OpCapability RayTracingKHR
2120 ; CHECK: OpExtension "SPV_KHR_ray_tracing"
2121 OpMemoryModel Logical GLSL450
2122 OpEntryPoint MissKHR %main "main" %a
2123 OpSource HLSL 630
2124 OpName %Payload "Payload"
2125 OpMemberName %Payload 0 "color"
2126 OpName %a "a"
2127 OpName %main "main"
2128 %float = OpTypeFloat 32
2129 %v4float = OpTypeVector %float 4
2130 %Payload = OpTypeStruct %v4float
2131 %ptr_payload = OpTypePointer IncomingRayPayloadKHR %Payload
2132 %void = OpTypeVoid
2133 %8 = OpTypeFunction %void
2134 %a = OpVariable %ptr_payload IncomingRayPayloadKHR
2135 %main = OpFunction %void None %8
2136 %9 = OpLabel
2137 %10 = OpLoad %Payload %a
2138 OpStore %a %10
2139 OpReturn
2140 OpFunctionEnd
2141 )";
2142 const auto result =
2143 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2144 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2145 }
2146
2147 TEST_F(TrimCapabilitiesPassTest,
2148 RayTracingKHR_RemainsWithRayGenerationExecutionMode) {
2149 const std::string kTest = R"(
2150 OpCapability RayTracingKHR
2151 OpExtension "SPV_KHR_ray_tracing"
2152 ; CHECK: OpCapability RayTracingKHR
2153 ; CHECK: OpExtension "SPV_KHR_ray_tracing"
2154 OpMemoryModel Logical GLSL450
2155 OpEntryPoint RayGenerationKHR %main "main"
2156 OpSource HLSL 630
2157 OpName %main "main"
2158 %void = OpTypeVoid
2159 %3 = OpTypeFunction %void
2160 %main = OpFunction %void None %3
2161 %4 = OpLabel
2162 OpReturn
2163 OpFunctionEnd
2164 )";
2165 const auto result =
2166 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2167 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2168 }
2169
2170 TEST_F(TrimCapabilitiesPassTest,
2171 RayTracingKHR_RemainsWithCallableExecutionMode) {
2172 const std::string kTest = R"(
2173 ; CHECK: OpCapability RayTracingKHR
2174 ; CHECK: OpExtension "SPV_KHR_ray_tracing"
2175 OpCapability RayTracingKHR
2176 OpExtension "SPV_KHR_ray_tracing"
2177 OpMemoryModel Logical GLSL450
2178 OpEntryPoint CallableKHR %main "main" %a
2179 OpSource HLSL 660
2180 OpName %Payload "Payload"
2181 OpMemberName %Payload 0 "data"
2182 OpName %a "a"
2183 OpName %main "main"
2184 %float = OpTypeFloat 32
2185 %v4float = OpTypeVector %float 4
2186 %Payload = OpTypeStruct %v4float
2187 %ptr_payload = OpTypePointer IncomingCallableDataKHR %Payload
2188 %void = OpTypeVoid
2189 %8 = OpTypeFunction %void
2190 %a = OpVariable %ptr_payload IncomingCallableDataKHR
2191 %main = OpFunction %void None %8
2192 %9 = OpLabel
2193 %10 = OpLoad %Payload %a
2194 OpStore %a %10
2195 OpReturn
2196 OpFunctionEnd
2197 )";
2198 const auto result =
2199 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2200 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2201 }
2202
2203 TEST_F(TrimCapabilitiesPassTest,
2204 ImageMSArray_RemainsIfSampledIs2AndArrayedIs1) {
2205 const std::string kTest = R"(
2206 OpCapability ImageMSArray
2207 ; CHECK: OpCapability ImageMSArray
2208 OpCapability Shader
2209 OpCapability StorageImageMultisample
2210 OpCapability StorageImageReadWithoutFormat
2211 OpMemoryModel Logical GLSL450
2212 OpEntryPoint Fragment %main "main"
2213 OpExecutionMode %main OriginUpperLeft
2214 OpDecorate %var_image DescriptorSet 0
2215 OpDecorate %var_image Binding 1
2216 %void = OpTypeVoid
2217 %func = OpTypeFunction %void
2218 %f32 = OpTypeFloat 32
2219 %u32 = OpTypeInt 32 0
2220 %uint_2 = OpConstant %u32 2
2221 %uint_1 = OpConstant %u32 1
2222 %v2uint = OpTypeVector %u32 2
2223 %v4float = OpTypeVector %f32 4
2224 %image = OpTypeImage %f32 2D 2 1 1 2 Unknown
2225 %ptr_image = OpTypePointer UniformConstant %image
2226 %10 = OpConstantComposite %v2uint %uint_1 %uint_2
2227 %var_image = OpVariable %ptr_image UniformConstant
2228 %main = OpFunction %void None %func
2229 %main_lab = OpLabel
2230 %18 = OpLoad %image %var_image
2231 %19 = OpImageRead %v4float %18 %10 Sample %uint_2
2232 OpReturn
2233 OpFunctionEnd
2234 )";
2235 const auto result =
2236 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2237 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2238 }
2239
2240 TEST_F(TrimCapabilitiesPassTest, ImageMSArray_RemovedIfNotUsed) {
2241 const std::string kTest = R"(
2242 OpCapability Shader
2243 OpCapability ImageMSArray
2244 ; CHECK-NOT: OpCapability ImageMSArray
2245 OpMemoryModel Logical GLSL450
2246 OpEntryPoint Fragment %main "main" %out_var_SV_Target
2247 OpExecutionMode %main OriginUpperLeft
2248 OpSource HLSL 660
2249 OpName %out_var_SV_Target "out.var.SV_Target"
2250 OpName %main "main"
2251 OpDecorate %out_var_SV_Target Location 0
2252 %float = OpTypeFloat 32
2253 %v4float = OpTypeVector %float 4
2254 %_ptr_Output_v4float = OpTypePointer Output %v4float
2255 %void = OpTypeVoid
2256 %7 = OpTypeFunction %void
2257 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
2258 %main = OpFunction %void None %7
2259 %8 = OpLabel
2260 OpReturn
2261 OpFunctionEnd
2262 )";
2263 const auto result =
2264 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2265 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2266 }
2267
2268 TEST_F(TrimCapabilitiesPassTest, ImageMSArray_RemovedIfArrayedIsNot1) {
2269 const std::string kTest = R"(
2270 OpCapability ImageMSArray
2271 ; CHECK-NOT: OpCapability ImageMSArray
2272 OpCapability Shader
2273 OpCapability StorageImageMultisample
2274 OpCapability StorageImageReadWithoutFormat
2275 OpMemoryModel Logical GLSL450
2276 OpEntryPoint Fragment %main "main"
2277 OpExecutionMode %main OriginUpperLeft
2278 OpDecorate %var_image DescriptorSet 0
2279 OpDecorate %var_image Binding 1
2280 %void = OpTypeVoid
2281 %func = OpTypeFunction %void
2282 %f32 = OpTypeFloat 32
2283 %u32 = OpTypeInt 32 0
2284 %uint_2 = OpConstant %u32 2
2285 %uint_1 = OpConstant %u32 1
2286 %v2uint = OpTypeVector %u32 2
2287 %v4float = OpTypeVector %f32 4
2288 %image = OpTypeImage %f32 2D 2 0 1 2 Unknown
2289 %ptr_image = OpTypePointer UniformConstant %image
2290 %10 = OpConstantComposite %v2uint %uint_1 %uint_2
2291 %var_image = OpVariable %ptr_image UniformConstant
2292 %main = OpFunction %void None %func
2293 %main_lab = OpLabel
2294 %18 = OpLoad %image %var_image
2295 %19 = OpImageRead %v4float %18 %10 Sample %uint_2
2296 OpReturn
2297 OpFunctionEnd
2298 )";
2299 const auto result =
2300 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2301 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2302 }
2303
2304 TEST_F(TrimCapabilitiesPassTest, ImageMSArray_RemovedIfSampledNot2) {
2305 const std::string kTest = R"(
2306 OpCapability ImageMSArray
2307 ; CHECK-NOT: OpCapability ImageMSArray
2308 OpCapability Shader
2309 OpCapability StorageImageReadWithoutFormat
2310 OpMemoryModel Logical GLSL450
2311 OpEntryPoint Fragment %main "main"
2312 OpExecutionMode %main OriginUpperLeft
2313 OpDecorate %var_image DescriptorSet 0
2314 OpDecorate %var_image Binding 1
2315 %void = OpTypeVoid
2316 %func = OpTypeFunction %void
2317 %f32 = OpTypeFloat 32
2318 %u32 = OpTypeInt 32 0
2319 %uint_3 = OpConstant %u32 3
2320 %uint_2 = OpConstant %u32 2
2321 %uint_1 = OpConstant %u32 1
2322 %v3uint = OpTypeVector %u32 3
2323 %v4float = OpTypeVector %f32 4
2324 %image = OpTypeImage %f32 2D 2 1 0 2 Unknown
2325 %ptr_image = OpTypePointer UniformConstant %image
2326 %10 = OpConstantComposite %v3uint %uint_1 %uint_2 %uint_3
2327 %var_image = OpVariable %ptr_image UniformConstant
2328 %main = OpFunction %void None %func
2329 %main_lab = OpLabel
2330 %18 = OpLoad %image %var_image
2331 %19 = OpImageRead %v4float %18 %10
2332 OpReturn
2333 OpFunctionEnd
2334 )";
2335 const auto result =
2336 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2337 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2338 }
2339
2340 TEST_F(TrimCapabilitiesPassTest, Float64_RemovedWhenUnused) {
2341 const std::string kTest = R"(
2342 OpCapability Float64
2343 ; CHECK-NOT: OpCapability Float64
2344 OpCapability Shader
2345 OpMemoryModel Logical GLSL450
2346 OpEntryPoint GLCompute %1 "main"
2347 %void = OpTypeVoid
2348 %3 = OpTypeFunction %void
2349 %1 = OpFunction %void None %3
2350 %6 = OpLabel
2351 OpReturn
2352 OpFunctionEnd;
2353 )";
2354 const auto result =
2355 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2356 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2357 }
2358
2359 TEST_F(TrimCapabilitiesPassTest, Float64_RemainsWhenUsed) {
2360 const std::string kTest = R"(
2361 OpCapability Float64
2362 ; CHECK: OpCapability Float64
2363 OpCapability Shader
2364 OpMemoryModel Logical GLSL450
2365 OpEntryPoint GLCompute %1 "main"
2366 %void = OpTypeVoid
2367 %float = OpTypeFloat 64
2368 %3 = OpTypeFunction %void
2369 %1 = OpFunction %void None %3
2370 %6 = OpLabel
2371 OpReturn
2372 OpFunctionEnd;
2373 )";
2374 const auto result =
2375 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2376 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2377 }
2378
2379 TEST_F(TrimCapabilitiesPassTest,
2380 ComputeDerivativeGroupQuads_ReamainsWithExecMode) {
2381 const std::string kTest = R"(
2382 OpCapability ComputeDerivativeGroupQuadsKHR
2383 OpCapability ComputeDerivativeGroupLinearKHR
2384 ; CHECK-NOT: OpCapability ComputeDerivativeGroupLinearKHR
2385 ; CHECK: OpCapability ComputeDerivativeGroupQuadsKHR
2386 ; CHECK-NOT: OpCapability ComputeDerivativeGroupLinearKHR
2387 OpCapability Shader
2388 ; CHECK: OpExtension "SPV_NV_compute_shader_derivatives"
2389 OpExtension "SPV_NV_compute_shader_derivatives"
2390 OpMemoryModel Logical GLSL450
2391 OpEntryPoint GLCompute %1 "main"
2392 OpExecutionMode %1 DerivativeGroupQuadsNV
2393 %void = OpTypeVoid
2394 %3 = OpTypeFunction %void
2395 %1 = OpFunction %void None %3
2396 %6 = OpLabel
2397 OpReturn
2398 OpFunctionEnd
2399 )";
2400 const auto result =
2401 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2402 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2403 }
2404
2405 TEST_F(TrimCapabilitiesPassTest,
2406 ComputeDerivativeGroupLinear_ReamainsWithExecMode) {
2407 const std::string kTest = R"(
2408 OpCapability ComputeDerivativeGroupLinearKHR
2409 OpCapability ComputeDerivativeGroupQuadsKHR
2410 ; CHECK-NOT: OpCapability ComputeDerivativeGroupQuadsKHR
2411 ; CHECK: OpCapability ComputeDerivativeGroupLinearKHR
2412 ; CHECK-NOT: OpCapability ComputeDerivativeGroupQuadsKHR
2413 OpCapability Shader
2414 ; CHECK: OpExtension "SPV_NV_compute_shader_derivatives"
2415 OpExtension "SPV_NV_compute_shader_derivatives"
2416 OpMemoryModel Logical GLSL450
2417 OpEntryPoint GLCompute %1 "main"
2418 OpExecutionMode %1 DerivativeGroupLinearNV
2419 %void = OpTypeVoid
2420 %float = OpTypeFloat 64
2421 %3 = OpTypeFunction %void
2422 %1 = OpFunction %void None %3
2423 %6 = OpLabel
2424 OpReturn
2425 OpFunctionEnd
2426 )";
2427 const auto result =
2428 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2429 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2430 }
2431
2432 TEST_F(TrimCapabilitiesPassTest,
2433 StorageImageReadWithoutFormat_RemovedIfUnused) {
2434 const std::string kTest = R"(
2435 OpCapability StorageImageReadWithoutFormat
2436 ; CHECK-NOT: OpCapability StorageImageReadWithoutFormat
2437 OpCapability Shader
2438 OpMemoryModel Logical GLSL450
2439 OpEntryPoint Fragment %PSMain "PSMain" %out_var
2440 OpExecutionMode %PSMain OriginUpperLeft
2441 OpDecorate %out_var Location 0
2442 %float = OpTypeFloat 32
2443 %float4 = OpTypeVector %float 4
2444 %float_0 = OpConstant %float 0
2445 %float4_0000 = OpConstantComposite %float4 %float_0 %float_0 %float_0 %float_0
2446 %ptr_float4 = OpTypePointer Output %float4
2447 %void = OpTypeVoid
2448 %9 = OpTypeFunction %void
2449 %out_var = OpVariable %ptr_float4 Output
2450 %PSMain = OpFunction %void None %9
2451 %10 = OpLabel
2452 OpStore %out_var %float4_0000
2453 OpReturn
2454 OpFunctionEnd
2455 )";
2456 const auto result =
2457 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2458 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2459 }
2460
2461 TEST_F(TrimCapabilitiesPassTest,
2462 StorageImageReadWithoutFormat_RemovedIfUnusedOpImageFetch) {
2463 const std::string kTest = R"(
2464 OpCapability StorageImageReadWithoutFormat
2465 ; CHECK-NOT: OpCapability StorageImageReadWithoutFormat
2466 OpCapability Shader
2467 OpMemoryModel Logical GLSL450
2468 OpEntryPoint Fragment %PSMain "PSMain" %out_var
2469 OpExecutionMode %PSMain OriginUpperLeft
2470 OpDecorate %out_var Location 0
2471 OpDecorate %texture DescriptorSet 0
2472 OpDecorate %texture Binding 1
2473 %float = OpTypeFloat 32
2474 %float4 = OpTypeVector %float 4
2475 %int = OpTypeInt 32 1
2476 %int2 = OpTypeVector %int 2
2477 %type_image = OpTypeImage %float 2D 2 0 0 1 Unknown
2478 %ptr_image = OpTypePointer UniformConstant %type_image
2479 %int_0 = OpConstant %int 0
2480 %int2_00 = OpConstantComposite %int2 %int_0 %int_0
2481 %ptr_float4 = OpTypePointer Output %float4
2482 %void = OpTypeVoid
2483 %9 = OpTypeFunction %void
2484 %texture = OpVariable %ptr_image UniformConstant
2485 %out_var = OpVariable %ptr_float4 Output
2486 %PSMain = OpFunction %void None %9
2487 %10 = OpLabel
2488 %11 = OpLoad %type_image %texture
2489 %12 = OpImageFetch %float4 %11 %int2_00 Lod %int_0
2490 OpStore %out_var %12
2491 OpReturn
2492 OpFunctionEnd
2493 )";
2494 const auto result =
2495 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2496 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2497 }
2498
2499 TEST_F(TrimCapabilitiesPassTest,
2500 StorageImageReadWithoutFormat_RemainsWhenRequiredWithRead) {
2501 const std::string kTest = R"(
2502 OpCapability StorageImageReadWithoutFormat
2503 ; CHECK: OpCapability StorageImageReadWithoutFormat
2504 OpCapability Shader
2505 OpMemoryModel Logical GLSL450
2506 OpEntryPoint Fragment %PSMain "PSMain" %out_var
2507 OpExecutionMode %PSMain OriginUpperLeft
2508 OpDecorate %out_var Location 0
2509 OpDecorate %texture DescriptorSet 0
2510 OpDecorate %texture Binding 1
2511 %float = OpTypeFloat 32
2512 %float4 = OpTypeVector %float 4
2513 %int = OpTypeInt 32 1
2514 %int2 = OpTypeVector %int 2
2515 %type_image = OpTypeImage %float 2D 2 0 0 1 Unknown
2516 %ptr_image = OpTypePointer UniformConstant %type_image
2517 %int_0 = OpConstant %int 0
2518 %int2_00 = OpConstantComposite %int2 %int_0 %int_0
2519 %ptr_float4 = OpTypePointer Output %float4
2520 %void = OpTypeVoid
2521 %9 = OpTypeFunction %void
2522 %texture = OpVariable %ptr_image UniformConstant
2523 %out_var = OpVariable %ptr_float4 Output
2524 %PSMain = OpFunction %void None %9
2525 %10 = OpLabel
2526 %11 = OpLoad %type_image %texture
2527 %12 = OpImageRead %float4 %11 %int2_00 Lod %int_0
2528 OpStore %out_var %12
2529 OpReturn
2530 OpFunctionEnd
2531 )";
2532 const auto result =
2533 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2534 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2535 }
2536
2537 TEST_F(TrimCapabilitiesPassTest,
2538 StorageImageReadWithoutFormat_RemainsWhenRequiredWithSparseRead) {
2539 const std::string kTest = R"(
2540 OpCapability StorageImageReadWithoutFormat
2541 ; CHECK: OpCapability StorageImageReadWithoutFormat
2542 OpCapability SparseResidency
2543 OpCapability Shader
2544 OpMemoryModel Logical GLSL450
2545 OpEntryPoint Fragment %PSMain "PSMain"
2546 OpExecutionMode %PSMain OriginUpperLeft
2547 OpDecorate %texture DescriptorSet 0
2548 OpDecorate %texture Binding 1
2549 %float = OpTypeFloat 32
2550 %float4 = OpTypeVector %float 4
2551 %int = OpTypeInt 32 1
2552 %int2 = OpTypeVector %int 2
2553 %type_image = OpTypeImage %float 2D 2 0 0 2 Unknown
2554 %struct = OpTypeStruct %int %float4
2555 %ptr_image = OpTypePointer UniformConstant %type_image
2556 %int_0 = OpConstant %int 0
2557 %int2_00 = OpConstantComposite %int2 %int_0 %int_0
2558 %void = OpTypeVoid
2559 %9 = OpTypeFunction %void
2560 %texture = OpVariable %ptr_image UniformConstant
2561 %PSMain = OpFunction %void None %9
2562 %10 = OpLabel
2563 %11 = OpLoad %type_image %texture
2564 %12 = OpImageSparseRead %struct %11 %int2_00
2565 OpReturn
2566 OpFunctionEnd
2567 )";
2568 const auto result =
2569 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2570 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2571 }
2572
2573 TEST_F(TrimCapabilitiesPassTest,
2574 StorageImageReadWithoutFormat_RemovedWithReadOnSubpassData) {
2575 const std::string kTest = R"(
2576 OpCapability StorageImageReadWithoutFormat
2577 ; CHECK-NOT: OpCapability StorageImageReadWithoutFormat
2578 OpCapability InputAttachment
2579 OpCapability Shader
2580 OpMemoryModel Logical GLSL450
2581 OpEntryPoint Fragment %PSMain "PSMain" %out_var
2582 OpExecutionMode %PSMain OriginUpperLeft
2583 OpDecorate %out_var Location 0
2584 OpDecorate %texture DescriptorSet 0
2585 OpDecorate %texture Binding 1
2586 %float = OpTypeFloat 32
2587 %float4 = OpTypeVector %float 4
2588 %int = OpTypeInt 32 1
2589 %int2 = OpTypeVector %int 2
2590 %type_image = OpTypeImage %float SubpassData 2 0 0 2 Unknown
2591 %ptr_image = OpTypePointer UniformConstant %type_image
2592 %int_0 = OpConstant %int 0
2593 %int2_00 = OpConstantComposite %int2 %int_0 %int_0
2594 %ptr_float4 = OpTypePointer Output %float4
2595 %void = OpTypeVoid
2596 %9 = OpTypeFunction %void
2597 %texture = OpVariable %ptr_image UniformConstant
2598 %out_var = OpVariable %ptr_float4 Output
2599 %PSMain = OpFunction %void None %9
2600 %10 = OpLabel
2601 %11 = OpLoad %type_image %texture
2602 %12 = OpImageRead %float4 %11 %int2_00
2603 OpStore %out_var %12
2604 OpReturn
2605 OpFunctionEnd
2606 )";
2607 const auto result =
2608 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2609 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2610 }
2611
2612 TEST_F(TrimCapabilitiesPassTest,
2613 StorageImageWriteWithoutFormat_RemainsWhenRequiredWithWrite) {
2614 const std::string kTest = R"(
2615 OpCapability StorageImageWriteWithoutFormat
2616 ; CHECK: OpCapability StorageImageWriteWithoutFormat
2617 OpCapability Shader
2618 OpCapability StorageImageExtendedFormats
2619 OpMemoryModel Logical GLSL450
2620 OpEntryPoint GLCompute %main "main" %id %img
2621 OpExecutionMode %main LocalSize 8 8 8
2622 OpSource HLSL 670
2623 OpName %type_image "type.3d.image"
2624 OpName %img "img"
2625 OpName %main "main"
2626 OpDecorate %id BuiltIn GlobalInvocationId
2627 OpDecorate %img DescriptorSet 0
2628 OpDecorate %img Binding 0
2629 %float = OpTypeFloat 32
2630 %float_4 = OpConstant %float 4
2631 %float_5 = OpConstant %float 5
2632 %v2float = OpTypeVector %float 2
2633 %9 = OpConstantComposite %v2float %float_4 %float_5
2634 %type_image = OpTypeImage %float 3D 2 0 0 2 Unknown
2635 %ptr_img = OpTypePointer UniformConstant %type_image
2636 %uint = OpTypeInt 32 0
2637 %v3uint = OpTypeVector %uint 3
2638 %ptr_input = OpTypePointer Input %v3uint
2639 %void = OpTypeVoid
2640 %15 = OpTypeFunction %void
2641 %img = OpVariable %ptr_img UniformConstant
2642 %id = OpVariable %ptr_input Input
2643 %main = OpFunction %void None %15
2644 %16 = OpLabel
2645 %17 = OpLoad %v3uint %id
2646 %18 = OpLoad %type_image %img
2647 OpImageWrite %18 %17 %9 None
2648 OpReturn
2649 OpFunctionEnd
2650 )";
2651 const auto result =
2652 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2653 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2654 }
2655
2656 TEST_F(TrimCapabilitiesPassTest,
2657 StorageImageWriteWithoutFormat_RemovedWithWriteOnKnownFormat) {
2658 const std::string kTest = R"(
2659 OpCapability StorageImageWriteWithoutFormat
2660 ; CHECK-NOT: OpCapability StorageImageWriteWithoutFormat
2661 OpCapability Shader
2662 OpCapability StorageImageExtendedFormats
2663 OpMemoryModel Logical GLSL450
2664 OpEntryPoint GLCompute %main "main" %id %img
2665 OpExecutionMode %main LocalSize 8 8 8
2666 OpSource HLSL 670
2667 OpName %type_image "type.3d.image"
2668 OpName %img "img"
2669 OpName %main "main"
2670 OpDecorate %id BuiltIn GlobalInvocationId
2671 OpDecorate %img DescriptorSet 0
2672 OpDecorate %img Binding 0
2673 %float = OpTypeFloat 32
2674 %float_4 = OpConstant %float 4
2675 %float_5 = OpConstant %float 5
2676 %v2float = OpTypeVector %float 2
2677 %9 = OpConstantComposite %v2float %float_4 %float_5
2678 %type_image = OpTypeImage %float 3D 2 0 0 2 Rg32f
2679 %ptr_img = OpTypePointer UniformConstant %type_image
2680 %uint = OpTypeInt 32 0
2681 %v3uint = OpTypeVector %uint 3
2682 %ptr_input = OpTypePointer Input %v3uint
2683 %void = OpTypeVoid
2684 %15 = OpTypeFunction %void
2685 %img = OpVariable %ptr_img UniformConstant
2686 %id = OpVariable %ptr_input Input
2687 %main = OpFunction %void None %15
2688 %16 = OpLabel
2689 %17 = OpLoad %v3uint %id
2690 %18 = OpLoad %type_image %img
2691 OpImageWrite %18 %17 %9 None
2692 OpReturn
2693 OpFunctionEnd
2694 )";
2695 const auto result =
2696 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2697 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2698 }
2699
2700 TEST_F(TrimCapabilitiesPassTest, PhysicalStorageBuffer_RemovedWhenUnused) {
2701 const std::string kTest = R"(
2702 OpCapability PhysicalStorageBufferAddresses
2703 ; CHECK-NOT: OpCapability PhysicalStorageBufferAddresses
2704 OpCapability Shader
2705 OpMemoryModel Logical GLSL450
2706 OpEntryPoint GLCompute %1 "main"
2707 %void = OpTypeVoid
2708 %3 = OpTypeFunction %void
2709 %1 = OpFunction %void None %3
2710 %6 = OpLabel
2711 OpReturn
2712 OpFunctionEnd;
2713 )";
2714 const auto result =
2715 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2716 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2717 }
2718
2719 TEST_F(TrimCapabilitiesPassTest,
2720 PhysicalStorageBuffer_RemainsWithOpTypeForwardPointer) {
2721 const std::string kTest = R"(
2722 OpCapability PhysicalStorageBufferAddresses
2723 ; CHECK: OpCapability PhysicalStorageBufferAddresses
2724 OpCapability Shader
2725 OpMemoryModel Logical GLSL450
2726 OpEntryPoint GLCompute %main "main"
2727 OpExecutionMode %main LocalSize 1 2 4
2728 %void = OpTypeVoid
2729 %int = OpTypeInt 32 0
2730 %struct = OpTypeStruct %int
2731 OpTypeForwardPointer %ptr PhysicalStorageBuffer
2732 %ptr = OpTypePointer PhysicalStorageBuffer %struct
2733 %3 = OpTypeFunction %void
2734 %main = OpFunction %void None %3
2735 %6 = OpLabel
2736 OpReturn
2737 OpFunctionEnd
2738 )";
2739 const auto result =
2740 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2741 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2742 }
2743
2744 TEST_F(TrimCapabilitiesPassTest,
2745 PhysicalStorageBuffer_RemainsWithPhysicalStorageBufferStorage) {
2746 const std::string kTest = R"(
2747 OpCapability PhysicalStorageBufferAddresses
2748 ; CHECK: OpCapability PhysicalStorageBufferAddresses
2749 OpCapability Shader
2750 OpMemoryModel Logical GLSL450
2751 OpEntryPoint GLCompute %main "main"
2752 OpExecutionMode %main LocalSize 1 2 4
2753 %void = OpTypeVoid
2754 %int = OpTypeInt 32 0
2755 %struct = OpTypeStruct %int
2756 %ptr = OpTypePointer PhysicalStorageBuffer %struct
2757 %3 = OpTypeFunction %void
2758 %main = OpFunction %void None %3
2759 %6 = OpLabel
2760 OpReturn
2761 OpFunctionEnd;
2762 )";
2763 const auto result =
2764 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2765 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2766 }
2767
2768 TEST_F(TrimCapabilitiesPassTest,
2769 PhysicalStorageBuffer_RemainsWithRestrictDecoration) {
2770 const std::string kTest = R"(
2771 OpCapability PhysicalStorageBufferAddresses
2772 ; CHECK: OpCapability PhysicalStorageBufferAddresses
2773 OpCapability Shader
2774 OpMemoryModel Logical GLSL450
2775 OpEntryPoint GLCompute %main "main"
2776 OpExecutionMode %main LocalSize 1 2 4
2777 OpDecorate %var RestrictPointer
2778 %void = OpTypeVoid
2779 %int = OpTypeInt 32 0
2780 %struct = OpTypeStruct %int
2781 %ptr = OpTypePointer Function %struct
2782 %3 = OpTypeFunction %void
2783 %main = OpFunction %void None %3
2784 %6 = OpLabel
2785 %var = OpVariable %ptr Function
2786 OpReturn
2787 OpFunctionEnd;
2788 )";
2789 const auto result =
2790 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2791 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2792 }
2793
2794 TEST_F(TrimCapabilitiesPassTest,
2795 PhysicalStorageBuffer_RemainsWithAliasedDecoration) {
2796 const std::string kTest = R"(
2797 OpCapability PhysicalStorageBufferAddresses
2798 ; CHECK: OpCapability PhysicalStorageBufferAddresses
2799 OpCapability Shader
2800 OpMemoryModel Logical GLSL450
2801 OpEntryPoint GLCompute %main "main"
2802 OpExecutionMode %main LocalSize 1 2 4
2803 OpDecorate %var AliasedPointer
2804 %void = OpTypeVoid
2805 %int = OpTypeInt 32 0
2806 %struct = OpTypeStruct %int
2807 %ptr = OpTypePointer Function %struct
2808 %3 = OpTypeFunction %void
2809 %main = OpFunction %void None %3
2810 %6 = OpLabel
2811 %var = OpVariable %ptr Function
2812 OpReturn
2813 OpFunctionEnd;
2814 )";
2815 const auto result =
2816 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2817 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2818 }
2819
2820 TEST_F(TrimCapabilitiesPassTest, Float16_RemovedWhenUnused) {
2821 const std::string kTest = R"(
2822 OpCapability Float16
2823 ; CHECK-NOT: OpCapability Float16
2824 OpCapability Shader
2825 OpMemoryModel Logical GLSL450
2826 OpEntryPoint GLCompute %1 "main"
2827 %void = OpTypeVoid
2828 %3 = OpTypeFunction %void
2829 %1 = OpFunction %void None %3
2830 %6 = OpLabel
2831 OpReturn
2832 OpFunctionEnd;
2833 )";
2834 const auto result =
2835 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2836 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2837 }
2838
2839 TEST_F(TrimCapabilitiesPassTest, Float16_RemainsWhenUsed) {
2840 const std::string kTest = R"(
2841 OpCapability Float16
2842 ; CHECK: OpCapability Float16
2843 OpCapability Shader
2844 OpMemoryModel Logical GLSL450
2845 OpEntryPoint GLCompute %1 "main"
2846 %void = OpTypeVoid
2847 %float = OpTypeFloat 16
2848 %3 = OpTypeFunction %void
2849 %1 = OpFunction %void None %3
2850 %6 = OpLabel
2851 OpReturn
2852 OpFunctionEnd;
2853 )";
2854 const auto result =
2855 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2856 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2857 }
2858
2859 TEST_F(TrimCapabilitiesPassTest, Int16_RemovedWhenUnused) {
2860 const std::string kTest = R"(
2861 OpCapability Int16
2862 ; CHECK-NOT: OpCapability Int16
2863 OpCapability Shader
2864 OpMemoryModel Logical GLSL450
2865 OpEntryPoint GLCompute %1 "main"
2866 %void = OpTypeVoid
2867 %3 = OpTypeFunction %void
2868 %1 = OpFunction %void None %3
2869 %6 = OpLabel
2870 OpReturn
2871 OpFunctionEnd;
2872 )";
2873 const auto result =
2874 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2875 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2876 }
2877
2878 TEST_F(TrimCapabilitiesPassTest, Int16_RemainsWhenUsed) {
2879 const std::string kTest = R"(
2880 OpCapability Int16
2881 ; CHECK: OpCapability Int16
2882 OpCapability Shader
2883 OpMemoryModel Logical GLSL450
2884 OpEntryPoint GLCompute %1 "main"
2885 %void = OpTypeVoid
2886 %int = OpTypeInt 16 1
2887 %3 = OpTypeFunction %void
2888 %1 = OpFunction %void None %3
2889 %6 = OpLabel
2890 OpReturn
2891 OpFunctionEnd;
2892 )";
2893 const auto result =
2894 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2895 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2896 }
2897
2898 TEST_F(TrimCapabilitiesPassTest, UInt16_RemainsWhenUsed) {
2899 const std::string kTest = R"(
2900 OpCapability Int16
2901 ; CHECK: OpCapability Int16
2902 OpCapability Shader
2903 OpMemoryModel Logical GLSL450
2904 OpEntryPoint GLCompute %1 "main"
2905 %void = OpTypeVoid
2906 %uint = OpTypeInt 16 0
2907 %3 = OpTypeFunction %void
2908 %1 = OpFunction %void None %3
2909 %6 = OpLabel
2910 OpReturn
2911 OpFunctionEnd;
2912 )";
2913 const auto result =
2914 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2915 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2916 }
2917
2918 TEST_F(TrimCapabilitiesPassTest,
2919 VulkanMemoryModelDeviceScope_RemovedWhenUnused) {
2920 const std::string kTest = R"(
2921 OpCapability VulkanMemoryModelDeviceScope
2922 ; CHECK-NOT: OpCapability VulkanMemoryModelDeviceScope
2923 OpCapability Shader
2924 OpMemoryModel Logical GLSL450
2925 OpEntryPoint GLCompute %1 "main"
2926 %void = OpTypeVoid
2927 %3 = OpTypeFunction %void
2928 %1 = OpFunction %void None %3
2929 %6 = OpLabel
2930 OpReturn
2931 OpFunctionEnd;
2932 )";
2933 const auto result =
2934 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2935 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2936 }
2937
2938 TEST_F(TrimCapabilitiesPassTest,
2939 VulkanMemoryModelDeviceScope_RemovedWhenUsedWithGLSL450) {
2940 const std::string kTest = R"(
2941 OpCapability VulkanMemoryModelDeviceScope
2942 ; CHECK-NOT: OpCapability VulkanMemoryModelDeviceScope
2943 OpCapability Shader
2944 OpCapability ShaderClockKHR
2945 OpCapability Int64
2946 OpExtension "SPV_KHR_shader_clock"
2947 OpMemoryModel Logical GLSL450
2948 OpEntryPoint GLCompute %main "main"
2949 OpExecutionMode %main LocalSize 1 2 4
2950 %void = OpTypeVoid
2951 %uint = OpTypeInt 32 0
2952 %ulong = OpTypeInt 64 0
2953 %uint_1 = OpConstant %uint 1
2954 %3 = OpTypeFunction %void
2955 %main = OpFunction %void None %3
2956 %6 = OpLabel
2957 %22 = OpReadClockKHR %ulong %uint_1 ; Device Scope
2958 OpReturn
2959 OpFunctionEnd
2960 )";
2961 const auto result =
2962 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2963 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2964 }
2965
2966 TEST_F(TrimCapabilitiesPassTest,
2967 VulkanMemoryModelDeviceScope_RemainsWhenUsedWithVulkan) {
2968 const std::string kTest = R"(
2969 OpCapability VulkanMemoryModelDeviceScope
2970 ; CHECK: OpCapability VulkanMemoryModelDeviceScope
2971 OpCapability Shader
2972 OpCapability ShaderClockKHR
2973 OpCapability Int64
2974 OpExtension "SPV_KHR_shader_clock"
2975 OpMemoryModel Logical Vulkan
2976 OpEntryPoint GLCompute %main "main"
2977 OpExecutionMode %main LocalSize 1 2 4
2978 %void = OpTypeVoid
2979 %uint = OpTypeInt 32 0
2980 %ulong = OpTypeInt 64 0
2981 %uint_1 = OpConstant %uint 1
2982 %3 = OpTypeFunction %void
2983 %main = OpFunction %void None %3
2984 %6 = OpLabel
2985 %22 = OpReadClockKHR %ulong %uint_1 ; Device Scope
2986 OpReturn
2987 OpFunctionEnd
2988 )";
2989 const auto result =
2990 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2991 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2992 }
2993
2994 TEST_F(TrimCapabilitiesPassTest, GroupNonUniform_RemovedWhenUnused) {
2995 const std::string kTest = R"(
2996 OpCapability Shader
2997 OpCapability GroupNonUniformVote
2998 ; CHECK-NOT: OpCapability GroupNonUniformVote
2999 OpCapability GroupNonUniformArithmetic
3000 ; CHECK-NOT: OpCapability GroupNonUniformArithmetic
3001 OpCapability GroupNonUniformClustered
3002 ; CHECK-NOT: OpCapability GroupNonUniformClustered
3003 OpCapability GroupNonUniformPartitionedEXT
3004 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3005 OpCapability GroupNonUniform
3006 ; CHECK-NOT: OpCapability GroupNonUniform
3007 OpExtension "SPV_NV_shader_subgroup_partitioned"
3008 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3009 OpMemoryModel Logical GLSL450
3010 OpEntryPoint GLCompute %main "main"
3011 OpExecutionMode %main LocalSize 1 2 4
3012 %void = OpTypeVoid
3013 %3 = OpTypeFunction %void
3014 %main = OpFunction %void None %3
3015 %6 = OpLabel
3016 OpReturn
3017 OpFunctionEnd;
3018 )";
3019 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3020 kTest, /* do_validation= */ false);
3021 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3022 }
3023
3024 TEST_F(TrimCapabilitiesPassTest,
3025 GroupNonUniform_RemainsGroupNonUniformWhenInUse) {
3026 const std::string kTest = R"(
3027 OpCapability GroupNonUniformVote
3028 ; CHECK-NOT: OpCapability GroupNonUniformVote
3029 OpCapability GroupNonUniformArithmetic
3030 ; CHECK-NOT: OpCapability GroupNonUniformArithmetic
3031 OpCapability GroupNonUniformClustered
3032 ; CHECK-NOT: OpCapability GroupNonUniformClustered
3033 OpCapability GroupNonUniformPartitionedEXT
3034 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3035 OpCapability GroupNonUniform
3036 ; CHECK: OpCapability GroupNonUniform
3037 OpCapability Shader
3038 OpExtension "SPV_NV_shader_subgroup_partitioned"
3039 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3040 OpMemoryModel Logical GLSL450
3041 OpEntryPoint GLCompute %main "main"
3042 OpExecutionMode %main LocalSize 1 2 4
3043 %void = OpTypeVoid
3044 %bool = OpTypeBool
3045 %uint = OpTypeInt 32 0
3046 %scope_subgroup = OpConstant %uint 3
3047 %3 = OpTypeFunction %void
3048 %main = OpFunction %void None %3
3049 %6 = OpLabel
3050 %7 = OpGroupNonUniformElect %bool %scope_subgroup
3051 OpReturn
3052 OpFunctionEnd;
3053 )";
3054 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3055 kTest, /* do_validation= */ false);
3056 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3057 }
3058
3059 TEST_F(TrimCapabilitiesPassTest,
3060 GroupNonUniformVote_Remains_OpGroupNonUniformAll) {
3061 const std::string kTest = R"(
3062 OpCapability Shader
3063 OpCapability GroupNonUniformVote
3064 ; CHECK: OpCapability GroupNonUniformVote
3065 OpCapability GroupNonUniformArithmetic
3066 ; CHECK-NOT: OpCapability GroupNonUniformArithmetic
3067 OpCapability GroupNonUniformClustered
3068 ; CHECK-NOT: OpCapability GroupNonUniformClustered
3069 OpCapability GroupNonUniformPartitionedEXT
3070 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3071 OpCapability GroupNonUniform
3072 ; CHECK-NOT: OpCapability GroupNonUniform
3073 OpExtension "SPV_NV_shader_subgroup_partitioned"
3074 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3075 OpMemoryModel Logical GLSL450
3076 OpEntryPoint GLCompute %main "main"
3077 OpExecutionMode %main LocalSize 1 2 4
3078 %void = OpTypeVoid
3079 %bool = OpTypeBool
3080 %uint = OpTypeInt 32 0
3081 %scope_subgroup = OpConstant %uint 3
3082 %true = OpConstantTrue %bool
3083 %3 = OpTypeFunction %void
3084 %main = OpFunction %void None %3
3085 %6 = OpLabel
3086 %7 = OpGroupNonUniformAll %bool %scope_subgroup %true
3087 OpReturn
3088 OpFunctionEnd;
3089 )";
3090 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3091 kTest, /* do_validation= */ false);
3092 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3093 }
3094
3095 TEST_F(TrimCapabilitiesPassTest,
3096 GroupNonUniformVote_Remains_OpGroupNonUniformAny) {
3097 const std::string kTest = R"(
3098 OpCapability Shader
3099 OpCapability GroupNonUniformVote
3100 ; CHECK: OpCapability GroupNonUniformVote
3101 OpCapability GroupNonUniformArithmetic
3102 ; CHECK-NOT: OpCapability GroupNonUniformArithmetic
3103 OpCapability GroupNonUniformClustered
3104 ; CHECK-NOT: OpCapability GroupNonUniformClustered
3105 OpCapability GroupNonUniformPartitionedEXT
3106 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3107 OpCapability GroupNonUniform
3108 ; CHECK-NOT: OpCapability GroupNonUniform
3109 OpExtension "SPV_NV_shader_subgroup_partitioned"
3110 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3111 OpMemoryModel Logical GLSL450
3112 OpEntryPoint GLCompute %main "main"
3113 OpExecutionMode %main LocalSize 1 2 4
3114 %void = OpTypeVoid
3115 %bool = OpTypeBool
3116 %uint = OpTypeInt 32 0
3117 %scope_subgroup = OpConstant %uint 3
3118 %true = OpConstantTrue %bool
3119 %3 = OpTypeFunction %void
3120 %main = OpFunction %void None %3
3121 %6 = OpLabel
3122 %7 = OpGroupNonUniformAny %bool %scope_subgroup %true
3123 OpReturn
3124 OpFunctionEnd;
3125 )";
3126 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3127 kTest, /* do_validation= */ false);
3128 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3129 }
3130
3131 TEST_F(TrimCapabilitiesPassTest,
3132 GroupNonUniformArithmetic_Remains_OpGroupNonUniformIAdd_Reduce) {
3133 const std::string kTest = R"(
3134 OpCapability Shader
3135 OpCapability GroupNonUniformVote
3136 ; CHECK-NOT: OpCapability GroupNonUniformVote
3137 OpCapability GroupNonUniformArithmetic
3138 ; CHECK: OpCapability GroupNonUniformArithmetic
3139 OpCapability GroupNonUniformClustered
3140 ; CHECK-NOT: OpCapability GroupNonUniformClustered
3141 OpCapability GroupNonUniformPartitionedEXT
3142 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3143 OpCapability GroupNonUniform
3144 ; CHECK-NOT: OpCapability GroupNonUniform
3145 OpExtension "SPV_NV_shader_subgroup_partitioned"
3146 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3147 OpMemoryModel Logical GLSL450
3148 OpEntryPoint GLCompute %main "main"
3149 OpExecutionMode %main LocalSize 1 2 4
3150 %void = OpTypeVoid
3151 %bool = OpTypeBool
3152 %uint = OpTypeInt 32 0
3153 %scope_subgroup = OpConstant %uint 3
3154 %uint_1 = OpConstant %uint 1
3155 %true = OpConstantTrue %bool
3156 %3 = OpTypeFunction %void
3157 %main = OpFunction %void None %3
3158 %6 = OpLabel
3159 %7 = OpGroupNonUniformIAdd %uint %scope_subgroup Reduce %uint_1
3160 OpReturn
3161 OpFunctionEnd;
3162 )";
3163 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3164 kTest, /* do_validation= */ true);
3165 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3166 }
3167
3168 TEST_F(TrimCapabilitiesPassTest,
3169 GroupNonUniformArithmetic_Remains_OpGroupNonUniformIAdd_InclusiveScan) {
3170 const std::string kTest = R"(
3171 OpCapability Shader
3172 OpCapability GroupNonUniformVote
3173 ; CHECK-NOT: OpCapability GroupNonUniformVote
3174 OpCapability GroupNonUniformArithmetic
3175 ; CHECK: OpCapability GroupNonUniformArithmetic
3176 OpCapability GroupNonUniformClustered
3177 ; CHECK-NOT: OpCapability GroupNonUniformClustered
3178 OpCapability GroupNonUniformPartitionedEXT
3179 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3180 OpCapability GroupNonUniform
3181 ; CHECK-NOT: OpCapability GroupNonUniform
3182 OpExtension "SPV_NV_shader_subgroup_partitioned"
3183 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3184 OpMemoryModel Logical GLSL450
3185 OpEntryPoint GLCompute %main "main"
3186 OpExecutionMode %main LocalSize 1 2 4
3187 %void = OpTypeVoid
3188 %bool = OpTypeBool
3189 %uint = OpTypeInt 32 0
3190 %scope_subgroup = OpConstant %uint 3
3191 %uint_1 = OpConstant %uint 1
3192 %true = OpConstantTrue %bool
3193 %3 = OpTypeFunction %void
3194 %main = OpFunction %void None %3
3195 %6 = OpLabel
3196 %7 = OpGroupNonUniformIAdd %uint %scope_subgroup InclusiveScan %uint_1
3197 OpReturn
3198 OpFunctionEnd;
3199 )";
3200 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3201 kTest, /* do_validation= */ true);
3202 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3203 }
3204
3205 TEST_F(TrimCapabilitiesPassTest,
3206 GroupNonUniformArithmetic_Remains_OpGroupNonUniformIAdd_ExclusiveScan) {
3207 const std::string kTest = R"(
3208 OpCapability Shader
3209 OpCapability GroupNonUniformVote
3210 ; CHECK-NOT: OpCapability GroupNonUniformVote
3211 OpCapability GroupNonUniformArithmetic
3212 ; CHECK: OpCapability GroupNonUniformArithmetic
3213 OpCapability GroupNonUniformClustered
3214 ; CHECK-NOT: OpCapability GroupNonUniformClustered
3215 OpCapability GroupNonUniformPartitionedEXT
3216 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3217 OpCapability GroupNonUniform
3218 ; CHECK-NOT: OpCapability GroupNonUniform
3219 OpExtension "SPV_NV_shader_subgroup_partitioned"
3220 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3221 OpMemoryModel Logical GLSL450
3222 OpEntryPoint GLCompute %main "main"
3223 OpExecutionMode %main LocalSize 1 2 4
3224 %void = OpTypeVoid
3225 %bool = OpTypeBool
3226 %uint = OpTypeInt 32 0
3227 %scope_subgroup = OpConstant %uint 3
3228 %uint_1 = OpConstant %uint 1
3229 %true = OpConstantTrue %bool
3230 %3 = OpTypeFunction %void
3231 %main = OpFunction %void None %3
3232 %6 = OpLabel
3233 %7 = OpGroupNonUniformIAdd %uint %scope_subgroup ExclusiveScan %uint_1
3234 OpReturn
3235 OpFunctionEnd;
3236 )";
3237 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3238 kTest, /* do_validation= */ true);
3239 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3240 }
3241
3242 TEST_F(TrimCapabilitiesPassTest,
3243 GroupNonUniformClustered_Remains_OpGroupNonUniformIAdd_ClusteredReduce) {
3244 const std::string kTest = R"(
3245 OpCapability Shader
3246 OpCapability GroupNonUniformVote
3247 ; CHECK-NOT: OpCapability GroupNonUniformVote
3248 OpCapability GroupNonUniformArithmetic
3249 ; CHECK-NOT: OpCapability GroupNonUniformArithmetic
3250 OpCapability GroupNonUniformClustered
3251 ; CHECK: OpCapability GroupNonUniformClustered
3252 OpCapability GroupNonUniformPartitionedEXT
3253 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3254 OpCapability GroupNonUniform
3255 ; CHECK-NOT: OpCapability GroupNonUniform
3256 OpExtension "SPV_NV_shader_subgroup_partitioned"
3257 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3258 OpMemoryModel Logical GLSL450
3259 OpEntryPoint GLCompute %main "main"
3260 OpExecutionMode %main LocalSize 1 2 4
3261 %void = OpTypeVoid
3262 %bool = OpTypeBool
3263 %uint = OpTypeInt 32 0
3264 %scope_subgroup = OpConstant %uint 3
3265 %uint_1 = OpConstant %uint 1
3266 %true = OpConstantTrue %bool
3267 %3 = OpTypeFunction %void
3268 %main = OpFunction %void None %3
3269 %6 = OpLabel
3270 %7 = OpGroupNonUniformIAdd %uint %scope_subgroup ClusteredReduce %uint_1 %uint_1
3271 OpReturn
3272 OpFunctionEnd;
3273 )";
3274 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3275 kTest, /* do_validation= */ true);
3276 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3277 }
3278
3279 struct SubgroupTestCase {
3280 // The result type of the subgroup instruction.
3281 std::string resultType;
3282 // The opcode of the subgroup instruction.
3283 std::string opcode;
3284 // The actual operand of the subgroup instruction.
3285 std::string operand;
3286 };
3287
3288 static const std::vector<SubgroupTestCase> kSubgroupTestCases{
3289 // clang-format off
3290 { "uint", "OpGroupNonUniformIAdd", "uint_1" },
3291 { "float", "OpGroupNonUniformFAdd", "float_1" },
3292 { "uint", "OpGroupNonUniformIMul", "uint_1" },
3293 { "float", "OpGroupNonUniformFMul", "float_1" },
3294 { "int", "OpGroupNonUniformSMin", "int_1" },
3295 { "uint", "OpGroupNonUniformUMin", "uint_1" },
3296 { "float", "OpGroupNonUniformFMin", "float_1" },
3297 { "int", "OpGroupNonUniformSMax", "int_1" },
3298 { "uint", "OpGroupNonUniformUMax", "uint_1" },
3299 { "float", "OpGroupNonUniformFMax", "float_1" },
3300 { "uint", "OpGroupNonUniformBitwiseAnd", "uint_1" },
3301 { "uint", "OpGroupNonUniformBitwiseOr", "uint_1" },
3302 { "uint", "OpGroupNonUniformBitwiseXor", "uint_1" },
3303 { "bool", "OpGroupNonUniformLogicalAnd", "true" },
3304 { "bool", "OpGroupNonUniformLogicalOr", "true" },
3305 { "bool", "OpGroupNonUniformLogicalXor", "true" }
3306 // clang-format on
3307 };
3308
3309 using TrimCapabilitiesPassTestSubgroupEXT_Unsigned = PassTest<
3310 ::testing::TestWithParam<std::tuple<SubgroupTestCase, std::string>>>;
3311 TEST_P(TrimCapabilitiesPassTestSubgroupEXT_Unsigned,
3312 GroupNonUniformPartitionedEXT_Remains) {
3313 SubgroupTestCase test_case = std::get<0>(GetParam());
3314 const std::string operation = std::get<1>(GetParam());
3315
3316 const std::string kTest = R"(
3317 OpCapability Shader
3318 OpCapability GroupNonUniformVote
3319 ; CHECK-NOT: OpCapability GroupNonUniformVote
3320 OpCapability GroupNonUniformArithmetic
3321 ; CHECK-NOT: OpCapability GroupNonUniformArithmetic
3322 OpCapability GroupNonUniformClustered
3323 ; CHECK-NOT: OpCapability GroupNonUniformClustered
3324 OpCapability GroupNonUniformPartitionedEXT
3325 ; CHECK: OpCapability GroupNonUniformPartitionedEXT
3326 OpCapability GroupNonUniform
3327 ; CHECK-NOT: OpCapability GroupNonUniform
3328 OpExtension "SPV_NV_shader_subgroup_partitioned"
3329 ; CHECK: OpExtension "SPV_NV_shader_subgroup_partitioned"
3330 OpMemoryModel Logical GLSL450
3331 OpEntryPoint GLCompute %main "main"
3332 OpExecutionMode %main LocalSize 1 2 4
3333 %void = OpTypeVoid
3334 %bool = OpTypeBool
3335 %uint = OpTypeInt 32 0
3336 %int = OpTypeInt 32 1
3337 %float = OpTypeFloat 32
3338 %v4uint = OpTypeVector %uint 4
3339 %scope_subgroup = OpConstant %uint 3
3340 %uint_1 = OpConstant %uint 1
3341 %int_1 = OpConstant %int 1
3342 %float_1 = OpConstant %float 1
3343 %uint4_1111 = OpConstantComposite %v4uint %uint_1 %uint_1 %uint_1 %uint_1
3344 %true = OpConstantTrue %bool
3345 %3 = OpTypeFunction %void
3346 %main = OpFunction %void None %3
3347 %6 = OpLabel
3348 %7 = )" + test_case.opcode +
3349 " %" + test_case.resultType + " %scope_subgroup " +
3350 operation + " %" + test_case.operand +
3351 R"( %uint4_1111
3352 OpReturn
3353 OpFunctionEnd;
3354 )";
3355 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3356 kTest, /* do_validation= */ true);
3357 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3358 }
3359
3360 INSTANTIATE_TEST_SUITE_P(
3361 TrimCapabilitiesPassTestSubgroupEXT_Unsigned_I,
3362 TrimCapabilitiesPassTestSubgroupEXT_Unsigned,
3363 ::testing::Combine(::testing::ValuesIn(kSubgroupTestCases),
3364 ::testing::Values("PartitionedReduceEXT",
3365 "PartitionedInclusiveScanEXT",
3366 "PartitionedExclusiveScanEXT")),
3367 [](const ::testing::TestParamInfo<
3368 TrimCapabilitiesPassTestSubgroupEXT_Unsigned::ParamType>& info) {
3369 return std::get<0>(info.param).opcode + "_" + std::get<1>(info.param);
3370 });
3371
3372 using TrimCapabilitiesPassTestSubgroupArithmetic_Unsigned = PassTest<
3373 ::testing::TestWithParam<std::tuple<SubgroupTestCase, std::string>>>;
3374 TEST_P(TrimCapabilitiesPassTestSubgroupArithmetic_Unsigned,
3375 GroupNonUniformPartitionedArithmetic_Remains) {
3376 SubgroupTestCase test_case = std::get<0>(GetParam());
3377 const std::string operation = std::get<1>(GetParam());
3378
3379 const std::string kTest = R"(
3380 OpCapability Shader
3381 OpCapability GroupNonUniformVote
3382 ; CHECK-NOT: OpCapability GroupNonUniformVote
3383 OpCapability GroupNonUniformArithmetic
3384 ; CHECK: OpCapability GroupNonUniformArithmetic
3385 OpCapability GroupNonUniformClustered
3386 ; CHECK-NOT: OpCapability GroupNonUniformClustered
3387 OpCapability GroupNonUniformPartitionedEXT
3388 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3389 OpCapability GroupNonUniform
3390 ; CHECK-NOT: OpCapability GroupNonUniform
3391 OpExtension "SPV_NV_shader_subgroup_partitioned"
3392 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3393 OpMemoryModel Logical GLSL450
3394 OpEntryPoint GLCompute %main "main"
3395 OpExecutionMode %main LocalSize 1 2 4
3396 %void = OpTypeVoid
3397 %bool = OpTypeBool
3398 %uint = OpTypeInt 32 0
3399 %int = OpTypeInt 32 1
3400 %float = OpTypeFloat 32
3401 %v4uint = OpTypeVector %uint 4
3402 %scope_subgroup = OpConstant %uint 3
3403 %uint_1 = OpConstant %uint 1
3404 %int_1 = OpConstant %int 1
3405 %float_1 = OpConstant %float 1
3406 %uint4_1111 = OpConstantComposite %v4uint %uint_1 %uint_1 %uint_1 %uint_1
3407 %true = OpConstantTrue %bool
3408 %3 = OpTypeFunction %void
3409 %main = OpFunction %void None %3
3410 %6 = OpLabel
3411 %7 = )" + test_case.opcode +
3412 " %" + test_case.resultType + " %scope_subgroup " +
3413 operation + " %" + test_case.operand + R"( %uint_1
3414 OpReturn
3415 OpFunctionEnd;
3416 )";
3417 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3418 kTest, /* do_validation= */ true);
3419 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3420 }
3421
3422 INSTANTIATE_TEST_SUITE_P(
3423 TrimCapabilitiesPassTestSubgroupArithmetic_Unsigned_I,
3424 TrimCapabilitiesPassTestSubgroupArithmetic_Unsigned,
3425 ::testing::Combine(::testing::ValuesIn(kSubgroupTestCases),
3426 ::testing::Values("Reduce", "InclusiveScan",
3427 "ExclusiveScan")),
3428 [](const ::testing::TestParamInfo<
3429 TrimCapabilitiesPassTestSubgroupArithmetic_Unsigned::ParamType>& info) {
3430 return std::get<0>(info.param).opcode + "_" + std::get<1>(info.param);
3431 });
3432
3433 using TrimCapabilitiesPassTestSubgroupClustered_Unsigned = PassTest<
3434 ::testing::TestWithParam<std::tuple<SubgroupTestCase, std::string>>>;
3435 TEST_P(TrimCapabilitiesPassTestSubgroupClustered_Unsigned,
3436 GroupNonUniformPartitionedClustered_Remains) {
3437 SubgroupTestCase test_case = std::get<0>(GetParam());
3438 const std::string operation = std::get<1>(GetParam());
3439
3440 const std::string kTest = R"(
3441 OpCapability Shader
3442 OpCapability GroupNonUniformVote
3443 ; CHECK-NOT: OpCapability GroupNonUniformVote
3444 OpCapability GroupNonUniformArithmetic
3445 ; CHECK-NOT: OpCapability GroupNonUniformArithmetic
3446 OpCapability GroupNonUniformClustered
3447 ; CHECK: OpCapability GroupNonUniformClustered
3448 OpCapability GroupNonUniformPartitionedEXT
3449 ; CHECK-NOT: OpCapability GroupNonUniformPartitionedEXT
3450 OpCapability GroupNonUniform
3451 ; CHECK-NOT: OpCapability GroupNonUniform
3452 OpExtension "SPV_NV_shader_subgroup_partitioned"
3453 ; CHECK-NOT: OpExtension "SPV_NV_shader_subgroup_partitioned"
3454 OpMemoryModel Logical GLSL450
3455 OpEntryPoint GLCompute %main "main"
3456 OpExecutionMode %main LocalSize 1 2 4
3457 %void = OpTypeVoid
3458 %bool = OpTypeBool
3459 %uint = OpTypeInt 32 0
3460 %int = OpTypeInt 32 1
3461 %float = OpTypeFloat 32
3462 %v4uint = OpTypeVector %uint 4
3463 %scope_subgroup = OpConstant %uint 3
3464 %uint_1 = OpConstant %uint 1
3465 %int_1 = OpConstant %int 1
3466 %float_1 = OpConstant %float 1
3467 %uint4_1111 = OpConstantComposite %v4uint %uint_1 %uint_1 %uint_1 %uint_1
3468 %true = OpConstantTrue %bool
3469 %3 = OpTypeFunction %void
3470 %main = OpFunction %void None %3
3471 %6 = OpLabel
3472 %7 = )" + test_case.opcode +
3473 " %" + test_case.resultType + " %scope_subgroup " +
3474 operation + " %" + test_case.operand + R"( %uint_1
3475 OpReturn
3476 OpFunctionEnd;
3477 )";
3478 const auto result = SinglePassRunAndMatch<TrimCapabilitiesPass>(
3479 kTest, /* do_validation= */ true);
3480 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3481 }
3482
3483 TEST_F(TrimCapabilitiesPassTest, InterpolationFunction_RemovedIfNotUsed) {
3484 const std::string kTest = R"(
3485 OpCapability Shader
3486 OpCapability InterpolationFunction
3487 ; CHECK-NOT: OpCapability InterpolationFunction
3488 OpMemoryModel Logical GLSL450
3489 OpEntryPoint Fragment %main "main" %out_var_SV_Target
3490 OpExecutionMode %main OriginUpperLeft
3491 OpSource HLSL 660
3492 OpName %out_var_SV_Target "out.var.SV_Target"
3493 OpName %main "main"
3494 OpDecorate %out_var_SV_Target Location 0
3495 %float = OpTypeFloat 32
3496 %v4float = OpTypeVector %float 4
3497 %_ptr_Output_v4float = OpTypePointer Output %v4float
3498 %void = OpTypeVoid
3499 %7 = OpTypeFunction %void
3500 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
3501 %main = OpFunction %void None %7
3502 %8 = OpLabel
3503 OpReturn
3504 OpFunctionEnd
3505 )";
3506 const auto result =
3507 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3508 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3509 }
3510
3511 TEST_F(TrimCapabilitiesPassTest,
3512 InterpolationFunction_RemainsWithInterpolateAtCentroid) {
3513 const std::string kTest = R"(
3514 OpCapability Shader
3515 OpCapability InterpolationFunction
3516 ; CHECK: OpCapability InterpolationFunction
3517 %std450 = OpExtInstImport "GLSL.std.450"
3518 OpMemoryModel Logical GLSL450
3519 OpEntryPoint Fragment %main "main" %out_var_SV_Target %gl_PointCoord
3520 OpExecutionMode %main OriginUpperLeft
3521 OpSource HLSL 660
3522 OpName %out_var_SV_Target "out.var.SV_Target"
3523 OpName %main "main"
3524 OpDecorate %out_var_SV_Target Location 0
3525 OpDecorate %gl_PointCoord BuiltIn PointCoord
3526 %float = OpTypeFloat 32
3527 %v2float = OpTypeVector %float 2
3528 %v4float = OpTypeVector %float 4
3529 %_ptr_Output_v4float = OpTypePointer Output %v4float
3530 %_ptr_Input_v2float = OpTypePointer Input %v2float
3531 %void = OpTypeVoid
3532 %7 = OpTypeFunction %void
3533 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
3534 %gl_PointCoord = OpVariable %_ptr_Input_v2float Input
3535 %main = OpFunction %void None %7
3536 %8 = OpLabel
3537 %9 = OpExtInst %v4float %std450 InterpolateAtCentroid %gl_PointCoord
3538 OpReturn
3539 OpFunctionEnd
3540 )";
3541 const auto result =
3542 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3543 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
3544 }
3545
3546 TEST_F(TrimCapabilitiesPassTest, QuadControlKHR_RemoveIfNotUsed) {
3547 const std::string kTest = R"(
3548 OpCapability Shader
3549 OpCapability QuadControlKHR
3550 ; CHECK-NOT: OpCapability QuadControlKHR
3551 OpExtension "SPV_KHR_quad_control"
3552 ; CHECK-NOT: OpExtension "SPV_KHR_quad_control"
3553 OpMemoryModel Logical GLSL450
3554 OpEntryPoint Fragment %main "main" %out_var_SV_Target
3555 OpExecutionMode %main OriginUpperLeft
3556 OpSource HLSL 660
3557 OpName %out_var_SV_Target "out.var.SV_Target"
3558 OpName %main "main"
3559 OpDecorate %out_var_SV_Target Location 0
3560 %float = OpTypeFloat 32
3561 %v4float = OpTypeVector %float 4
3562 %_ptr_Output_v4float = OpTypePointer Output %v4float
3563 %void = OpTypeVoid
3564 %7 = OpTypeFunction %void
3565 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
3566 %main = OpFunction %void None %7
3567 %8 = OpLabel
3568 OpReturn
3569 OpFunctionEnd
3570 )";
3571 const auto result =
3572 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3573 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3574 }
3575
3576 TEST_F(TrimCapabilitiesPassTest, QuadControlKHR_RemainsWithQuadAny) {
3577 const std::string kTest = R"(
3578 OpCapability Shader
3579 OpCapability QuadControlKHR
3580 ; CHECK: OpCapability QuadControlKHR
3581 OpExtension "SPV_KHR_quad_control"
3582 ; CHECK: OpExtension "SPV_KHR_quad_control"
3583 OpMemoryModel Logical GLSL450
3584 OpEntryPoint Fragment %main "main" %out_var_SV_Target
3585 OpExecutionMode %main OriginUpperLeft
3586 OpSource HLSL 660
3587 OpName %out_var_SV_Target "out.var.SV_Target"
3588 OpName %main "main"
3589 OpDecorate %out_var_SV_Target Location 0
3590 %bool = OpTypeBool
3591 %float = OpTypeFloat 32
3592 %v4float = OpTypeVector %float 4
3593 %_ptr_Output_v4float = OpTypePointer Output %v4float
3594 %void = OpTypeVoid
3595 %7 = OpTypeFunction %void
3596 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
3597 %main = OpFunction %void None %7
3598 %8 = OpLabel
3599 %true = OpConstantTrue %bool
3600 %10 = OpGroupNonUniformQuadAnyKHR %bool %true
3601 OpReturn
3602 OpFunctionEnd
3603 )";
3604 const auto result =
3605 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3606 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
3607 }
3608
3609 TEST_F(TrimCapabilitiesPassTest, PhysicalStorageBuffer_RecursiveTypes) {
3610 const std::string kTest = R"(
3611 OpCapability Shader
3612 OpCapability PhysicalStorageBufferAddresses
3613 ; CHECK: OpCapability PhysicalStorageBufferAddresses
3614 OpExtension "SPV_KHR_physical_storage_buffer"
3615 ; CHECK: OpExtension "SPV_KHR_physical_storage_buffer"
3616 OpMemoryModel PhysicalStorageBuffer64 GLSL450
3617 OpEntryPoint Fragment %1 "main"
3618 OpExecutionMode %1 OriginUpperLeft
3619 OpSource HLSL 600
3620 OpMemberDecorate %_struct_2 0 Offset 0
3621 OpMemberDecorate %_struct_2 1 Offset 16
3622 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer__struct_2 PhysicalStorageBuffer
3623 %int = OpTypeInt 32 1
3624 %_struct_2 = OpTypeStruct %int %_ptr_PhysicalStorageBuffer__struct_2
3625 %_ptr_PhysicalStorageBuffer__struct_2 = OpTypePointer PhysicalStorageBuffer %_struct_2
3626 %void = OpTypeVoid
3627 %6 = OpTypeFunction %void
3628 %1 = OpFunction %void None %6
3629 %7 = OpLabel
3630 OpReturn
3631 OpFunctionEnd
3632 )";
3633 const auto result =
3634 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3635 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
3636 }
3637
3638 TEST_F(TrimCapabilitiesPassTest, Geometry_Remains) {
3639 const std::string kTest = R"(
3640 OpCapability Geometry
3641 ; CHECK: OpCapability Geometry
3642 OpMemoryModel Logical GLSL450
3643 OpEntryPoint Geometry %gs_main "gs_main" %gl_Position
3644 OpExecutionMode %gs_main OutputVertices 3
3645 OpExecutionMode %gs_main Invocations 1
3646 OpExecutionMode %gs_main Triangles
3647 OpExecutionMode %gs_main OutputTriangleStrip
3648 OpSource HLSL 660
3649 OpName %gs_main "gs_main"
3650 OpDecorate %gl_Position BuiltIn Position
3651 %float = OpTypeFloat 32
3652 %v4float = OpTypeVector %float 4
3653 %_ptr_Output_v4float = OpTypePointer Output %v4float
3654 %void = OpTypeVoid
3655 %7 = OpTypeFunction %void
3656 %gl_Position = OpVariable %_ptr_Output_v4float Output
3657 %gs_main = OpFunction %void None %7
3658 %8 = OpLabel
3659 OpEmitVertex
3660 OpReturn
3661 OpFunctionEnd
3662 )";
3663 const auto result =
3664 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3665 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
3666 }
3667
3668 // FIXME(6277): enable once spirv-opt supports SPV_INTEL_function_variants
3669 #if 0
3670 TEST_F(TrimCapabilitiesPassTest, Geometry_RemainsIntel) {
3671 const std::string kTest = R"(
3672 OpCapability Geometry
3673 ; CHECK: OpCapability Geometry
3674 OpCapability SpecConditionalINTEL
3675 OpExtension "SPV_INTEL_function_variants"
3676 OpMemoryModel Logical GLSL450
3677 OpConditionalEntryPointINTEL %false Geometry %gs_main "gs_main"
3678 OpExecutionMode %gs_main OutputVertices 3
3679 OpExecutionMode %gs_main Invocations 1
3680 OpExecutionMode %gs_main Triangles
3681 OpExecutionMode %gs_main OutputTriangleStrip
3682 OpSource HLSL 660
3683 OpName %gs_main "gs_main"
3684 OpDecorate %gl_Position BuiltIn Position
3685 %bool = OpTypeBool
3686 %false = OpSpecConstantFalse %bool
3687 %float = OpTypeFloat 32
3688 %v4float = OpTypeVector %float 4
3689 %_ptr_Output_v4float = OpTypePointer Output %v4float
3690 %void = OpTypeVoid
3691 %7 = OpTypeFunction %void
3692 %gl_Position = OpVariable %_ptr_Output_v4float Output
3693 %gs_main = OpFunction %void None %7
3694 %8 = OpLabel
3695 OpEmitVertex
3696 OpReturn
3697 OpFunctionEnd
3698 )";
3699 const auto result =
3700 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3701 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
3702 }
3703 #endif
3704
3705 TEST_F(TrimCapabilitiesPassTest, Geometry_Removed) {
3706 const std::string kTest = R"(
3707 OpCapability Shader
3708 OpCapability Geometry
3709 ; CHECK-NOT: OpCapability Geometry
3710 OpMemoryModel Logical GLSL450
3711 OpEntryPoint Fragment %ps_main "ps_main" %in_var_POSITION %out_var_SV_Target
3712 OpExecutionMode %ps_main OriginUpperLeft
3713 OpSource HLSL 660
3714 OpName %in_var_POSITION "in.var.POSITION"
3715 OpName %out_var_SV_Target "out.var.SV_Target"
3716 OpName %ps_main "ps_main"
3717 OpDecorate %in_var_POSITION Location 0
3718 OpDecorate %out_var_SV_Target Location 0
3719 %float = OpTypeFloat 32
3720 %v4float = OpTypeVector %float 4
3721 %_ptr_Input_v4float = OpTypePointer Input %v4float
3722 %_ptr_Output_v4float = OpTypePointer Output %v4float
3723 %void = OpTypeVoid
3724 %9 = OpTypeFunction %void
3725 %in_var_POSITION = OpVariable %_ptr_Input_v4float Input
3726 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
3727 %ps_main = OpFunction %void None %9
3728 %10 = OpLabel
3729 %11 = OpLoad %v4float %in_var_POSITION
3730 OpStore %out_var_SV_Target %11
3731 OpReturn
3732 OpFunctionEnd
3733 )";
3734 const auto result =
3735 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3736 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3737 }
3738
3739 // FIXME(6277): enable once spirv-opt supports SPV_INTEL_function_variants
3740 #if 0
3741 TEST_F(TrimCapabilitiesPassTest, Geometry_RemovedIntel) {
3742 const std::string kTest = R"(
3743 OpCapability Shader
3744 OpCapability Geometry
3745 ; CHECK-NOT: OpCapability Geometry
3746 OpCapability SpecConditionalINTEL
3747 OpExtension "SPV_INTEL_function_variants"
3748 OpMemoryModel Logical GLSL450
3749 OpConditionalEntryPointINTEL %false Fragment %ps_main "ps_main" %in_var_POSITION %out_var_SV_Target
3750 OpExecutionMode %ps_main OriginUpperLeft
3751 OpSource HLSL 660
3752 OpName %in_var_POSITION "in.var.POSITION"
3753 OpName %out_var_SV_Target "out.var.SV_Target"
3754 OpName %ps_main "ps_main"
3755 OpDecorate %in_var_POSITION Location 0
3756 OpDecorate %out_var_SV_Target Location 0
3757 %bool = OpTypeBool
3758 %false = OpSpecConstantFalse %bool
3759 %float = OpTypeFloat 32
3760 %v4float = OpTypeVector %float 4
3761 %_ptr_Input_v4float = OpTypePointer Input %v4float
3762 %_ptr_Output_v4float = OpTypePointer Output %v4float
3763 %void = OpTypeVoid
3764 %9 = OpTypeFunction %void
3765 %in_var_POSITION = OpVariable %_ptr_Input_v4float Input
3766 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
3767 %ps_main = OpFunction %void None %9
3768 %10 = OpLabel
3769 %11 = OpLoad %v4float %in_var_POSITION
3770 OpStore %out_var_SV_Target %11
3771 OpReturn
3772 OpFunctionEnd
3773 )";
3774 const auto result =
3775 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3776 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
3777 }
3778 #endif
3779
3780 TEST_F(TrimCapabilitiesPassTest, CheckNop) {
3781 const std::string kTest = R"(
3782 OpCapability Shader
3783 ; CHECK: OpCapability Shader
3784 OpMemoryModel Logical GLSL450
3785 OpEntryPoint GLCompute %1 "main"
3786 %void = OpTypeVoid
3787 %3 = OpTypeFunction %void
3788 %1 = OpFunction %void None %3
3789 %6 = OpLabel
3790 OpNop
3791 OpReturn
3792 OpFunctionEnd;
3793 )";
3794 const auto result =
3795 SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
3796 EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
3797 }
3798
3799 INSTANTIATE_TEST_SUITE_P(
3800 TrimCapabilitiesPassTestSubgroupClustered_Unsigned_I,
3801 TrimCapabilitiesPassTestSubgroupClustered_Unsigned,
3802 ::testing::Combine(::testing::ValuesIn(kSubgroupTestCases),
3803 ::testing::Values("ClusteredReduce")),
3804 [](const ::testing::TestParamInfo<
3805 TrimCapabilitiesPassTestSubgroupClustered_Unsigned::ParamType>& info) {
3806 return std::get<0>(info.param).opcode + "_" + std::get<1>(info.param);
3807 });
3808
3809 } // namespace
3810 } // namespace opt
3811 } // namespace spvtools