Debian Codegraph @

test/val/val_extensions_test.cpp

raw
1 // Copyright (c) 2017 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 // Tests for OpExtension validator rules.
16
17 #include <string>
18 #include <vector>
19
20 #include "gmock/gmock.h"
21 #include "source/extensions.h"
22 #include "source/spirv_target_env.h"
23 #include "test/unit_spirv.h"
24 #include "test/val/val_fixtures.h"
25
26 namespace spvtools {
27 namespace val {
28 namespace {
29
30 using ::testing::HasSubstr;
31 using ::testing::Not;
32 using ::testing::Values;
33 using ::testing::ValuesIn;
34
35 using ValidateKnownExtensions = spvtest::ValidateBase<std::string>;
36 using ValidateUnknownExtensions = spvtest::ValidateBase<std::string>;
37 using ValidateExtensionCapabilities = spvtest::ValidateBase<bool>;
38
39 // Returns expected error string if |extension| is not recognized.
40 std::string GetErrorString(const std::string& extension) {
41 return "Found unrecognized extension " + extension;
42 }
43
44 INSTANTIATE_TEST_SUITE_P(
45 ExpectSuccess, ValidateKnownExtensions,
46 Values(
47 // Match the order as published on the SPIR-V Registry.
48 "SPV_AMD_shader_explicit_vertex_parameter",
49 "SPV_AMD_shader_trinary_minmax", "SPV_AMD_gcn_shader",
50 "SPV_KHR_shader_ballot", "SPV_AMD_shader_ballot",
51 "SPV_AMD_gpu_shader_half_float", "SPV_KHR_shader_draw_parameters",
52 "SPV_KHR_subgroup_vote", "SPV_KHR_16bit_storage",
53 "SPV_KHR_device_group", "SPV_KHR_multiview",
54 "SPV_NVX_multiview_per_view_attributes", "SPV_NV_viewport_array2",
55 "SPV_NV_stereo_view_rendering", "SPV_NV_sample_mask_override_coverage",
56 "SPV_NV_geometry_shader_passthrough", "SPV_AMD_texture_gather_bias_lod",
57 "SPV_KHR_storage_buffer_storage_class", "SPV_KHR_variable_pointers",
58 "SPV_AMD_gpu_shader_int16", "SPV_KHR_post_depth_coverage",
59 "SPV_KHR_shader_atomic_counter_ops", "SPV_EXT_shader_stencil_export",
60 "SPV_EXT_shader_viewport_index_layer",
61 "SPV_AMD_shader_image_load_store_lod", "SPV_AMD_shader_fragment_mask",
62 "SPV_GOOGLE_decorate_string", "SPV_GOOGLE_hlsl_functionality1",
63 "SPV_NV_shader_subgroup_partitioned", "SPV_EXT_descriptor_indexing",
64 "SPV_KHR_terminate_invocation", "SPV_KHR_relaxed_extended_instruction",
65 "SPV_EXT_float8"));
66
67 INSTANTIATE_TEST_SUITE_P(FailSilently, ValidateUnknownExtensions,
68 Values("ERROR_unknown_extension", "SPV_KHR_",
69 "SPV_KHR_shader_ballot_ERROR"));
70
71 TEST_P(ValidateKnownExtensions, ExpectSuccess) {
72 const std::string extension = GetParam();
73 const std::string str =
74 "OpCapability Shader\nOpCapability Linkage\nOpExtension \"" + extension +
75 "\"\nOpMemoryModel Logical GLSL450";
76 CompileSuccessfully(str.c_str());
77 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
78 EXPECT_THAT(getDiagnosticString(), Not(HasSubstr(GetErrorString(extension))));
79 }
80
81 TEST_P(ValidateUnknownExtensions, FailSilently) {
82 const std::string extension = GetParam();
83 const std::string str =
84 "OpCapability Shader\nOpCapability Linkage\nOpExtension \"" + extension +
85 "\"\nOpMemoryModel Logical GLSL450";
86 CompileSuccessfully(str.c_str());
87 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
88 EXPECT_THAT(getDiagnosticString(), HasSubstr(GetErrorString(extension)));
89 }
90
91 TEST_F(ValidateUnknownExtensions, HitMaxNumOfWarnings) {
92 const std::string str =
93 std::string("OpCapability Shader\n") + "OpCapability Linkage\n" +
94 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
95 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
96 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
97 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
98 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
99 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
100 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
101 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
102 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
103 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
104 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
105 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
106 "OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" +
107 "OpMemoryModel Logical GLSL450";
108 CompileSuccessfully(str.c_str());
109 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
110 EXPECT_THAT(getDiagnosticString(),
111 HasSubstr("Other warnings have been suppressed."));
112 }
113
114 TEST_F(ValidateExtensionCapabilities, DeclCapabilitySuccess) {
115 const std::string str =
116 "OpCapability Shader\nOpCapability Linkage\nOpCapability DeviceGroup\n"
117 "OpExtension \"SPV_KHR_device_group\""
118 "\nOpMemoryModel Logical GLSL450";
119 CompileSuccessfully(str.c_str());
120 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
121 }
122
123 TEST_F(ValidateExtensionCapabilities, DeclCapabilityFailure) {
124 const std::string str =
125 "OpCapability Shader\nOpCapability Linkage\nOpCapability DeviceGroup\n"
126 "\nOpMemoryModel Logical GLSL450";
127 CompileSuccessfully(str.c_str());
128 ASSERT_EQ(SPV_ERROR_MISSING_EXTENSION, ValidateInstructions());
129 EXPECT_THAT(getDiagnosticString(), HasSubstr("1st operand of Capability"));
130 EXPECT_THAT(getDiagnosticString(),
131 HasSubstr("requires one of these extensions"));
132 EXPECT_THAT(getDiagnosticString(), HasSubstr("SPV_KHR_device_group"));
133 }
134
135 TEST_F(ValidateExtensionCapabilities, SpirvVersionImageProcessingQCOM) {
136 const std::string str = R"(
137 OpCapability Shader
138 OpCapability TextureBlockMatch2QCOM
139 OpExtension "SPV_QCOM_image_processing"
140 OpExtension "SPV_QCOM_image_processing2"
141 %1 = OpExtInstImport "GLSL.std.450"
142 OpMemoryModel Logical GLSL450
143 OpEntryPoint Fragment %main "main" %v_texcoord %fragColor %target_samp %ref_samp
144 OpExecutionMode %main OriginUpperLeft
145 OpSource GLSL 450
146 OpSourceExtension "GL_QCOM_image_processing"
147 OpSourceExtension "GL_QCOM_image_processing2"
148 OpDecorate %v_texcoord Location 0
149 OpDecorate %fragColor Location 0
150 OpDecorate %target_samp DescriptorSet 0
151 OpDecorate %target_samp Binding 4
152 OpDecorate %ref_samp DescriptorSet 0
153 OpDecorate %ref_samp Binding 5
154 OpDecorate %target_samp BlockMatchTextureQCOM
155 OpDecorate %target_samp BlockMatchSamplerQCOM
156 OpDecorate %ref_samp BlockMatchTextureQCOM
157 OpDecorate %ref_samp BlockMatchSamplerQCOM
158 %void = OpTypeVoid
159 %3 = OpTypeFunction %void
160 %uint = OpTypeInt 32 0
161 %v2uint = OpTypeVector %uint 2
162 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
163 %float = OpTypeFloat 32
164 %v4float = OpTypeVector %float 4
165 %_ptr_Input_v4float = OpTypePointer Input %v4float
166 %v_texcoord = OpVariable %_ptr_Input_v4float Input
167 %uint_0 = OpConstant %uint 0
168 %_ptr_Input_float = OpTypePointer Input %float
169 %_ptr_Function_uint = OpTypePointer Function %uint
170 %uint_1 = OpConstant %uint 1
171 %uint_2 = OpConstant %uint 2
172 %uint_3 = OpConstant %uint 3
173 %uint_4 = OpConstant %uint 4
174 %39 = OpConstantComposite %v2uint %uint_4 %uint_4
175 %_ptr_Output_v4float = OpTypePointer Output %v4float
176 %fragColor = OpVariable %_ptr_Output_v4float Output
177 %42 = OpTypeImage %float 2D 0 0 0 1 Unknown
178 %43 = OpTypeSampledImage %42
179 %_ptr_UniformConstant_43 = OpTypePointer UniformConstant %43
180 %target_samp = OpVariable %_ptr_UniformConstant_43 UniformConstant
181 %ref_samp = OpVariable %_ptr_UniformConstant_43 UniformConstant
182 %main = OpFunction %void None %3
183 %5 = OpLabel
184 %tgt_coords = OpVariable %_ptr_Function_v2uint Function
185 %ref_coords = OpVariable %_ptr_Function_v2uint Function
186 %blockSize = OpVariable %_ptr_Function_v2uint Function
187 %16 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_0
188 %17 = OpLoad %float %16
189 %18 = OpConvertFToU %uint %17
190 %20 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
191 OpStore %20 %18
192 %22 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_1
193 %23 = OpLoad %float %22
194 %24 = OpConvertFToU %uint %23
195 %25 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
196 OpStore %25 %24
197 %28 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_2
198 %29 = OpLoad %float %28
199 %30 = OpConvertFToU %uint %29
200 %31 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_0
201 OpStore %31 %30
202 %33 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_3
203 %34 = OpLoad %float %33
204 %35 = OpConvertFToU %uint %34
205 %36 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_1
206 OpStore %36 %35
207 OpStore %blockSize %39
208 %46 = OpLoad %43 %target_samp
209 %47 = OpLoad %v2uint %tgt_coords
210 %49 = OpLoad %43 %ref_samp
211 %50 = OpLoad %v2uint %ref_coords
212 %51 = OpLoad %v2uint %blockSize
213 %52 = OpImageBlockMatchWindowSADQCOM %v4float %46 %47 %49 %50 %51
214 OpStore %fragColor %52
215 OpReturn
216 OpFunctionEnd
217 )";
218 CompileSuccessfully(str.c_str(), SPV_ENV_UNIVERSAL_1_3);
219 ASSERT_EQ(SPV_ERROR_WRONG_VERSION,
220 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
221 EXPECT_THAT(getDiagnosticString(),
222 HasSubstr("SPV_QCOM_image_processing extension requires SPIR-V "
223 "version 1.4 or later."));
224 }
225
226 TEST_F(ValidateExtensionCapabilities, SpirvVersionCoopMatConversionQCOM) {
227 const std::string body = R"(
228 OpCapability Shader
229 OpCapability Float16
230 OpCapability CooperativeMatrixKHR
231 OpCapability VulkanMemoryModelKHR
232 OpCapability CooperativeMatrixConversionQCOM
233 OpExtension "SPV_KHR_cooperative_matrix"
234 OpExtension "SPV_KHR_vulkan_memory_model"
235 OpExtension "SPV_QCOM_cooperative_matrix_conversion"
236 OpMemoryModel Logical VulkanKHR
237 OpEntryPoint GLCompute %main "main"
238 %void = OpTypeVoid
239 %3 = OpTypeFunction %void
240 %uint = OpTypeInt 32 0
241 %f16 = OpTypeFloat 16
242 %uint_0 = OpConstant %uint 0
243 %uint_8 = OpConstant %uint 8
244 %uint_64 = OpConstant %uint 64
245 %_arr_f16_uint_8 = OpTypeArray %f16 %uint_8
246 %_arr_f16_uint_64 = OpTypeArray %f16 %uint_64
247 %_ptr_Function__arr_f16_uint_64 = OpTypePointer Function %_arr_f16_uint_64
248 %main = OpFunction %void None %3
249 %5 = OpLabel
250 %f16vec64Acc = OpVariable %_ptr_Function__arr_f16_uint_64 Function
251 %83 = OpLoad %_arr_f16_uint_64 %f16vec64Acc
252 %86 = OpExtractSubArrayQCOM %_arr_f16_uint_8 %83 %uint_0
253 OpReturn
254 OpFunctionEnd)";
255
256 CompileSuccessfully(body.c_str());
257 ASSERT_EQ(SPV_ERROR_WRONG_VERSION, ValidateInstructions());
258 EXPECT_THAT(getDiagnosticString(),
259 HasSubstr("SPV_KHR_vulkan_memory_model extension requires SPIR-V "
260 "version 1.3 or later."));
261 }
262
263 TEST_F(ValidateExtensionCapabilities,
264 DeclCapabilityFailureBlockMatchWIndowSAD) {
265 const std::string str = R"(
266 OpCapability Shader
267 OpCapability TextureBlockMatch2QCOM
268 OpExtension "SPV_QCOM_image_processing2"
269 %1 = OpExtInstImport "GLSL.std.450"
270 OpMemoryModel Logical GLSL450
271 OpEntryPoint Fragment %main "main" %v_texcoord %fragColor %target_samp %ref_samp
272 OpExecutionMode %main OriginUpperLeft
273 OpSource GLSL 450
274 OpSourceExtension "GL_QCOM_image_processing"
275 OpSourceExtension "GL_QCOM_image_processing2"
276 OpName %main "main"
277 OpName %tgt_coords "tgt_coords"
278 OpName %v_texcoord "v_texcoord"
279 OpName %ref_coords "ref_coords"
280 OpName %blockSize "blockSize"
281 OpName %fragColor "fragColor"
282 OpName %target_samp "target_samp"
283 OpName %ref_samp "ref_samp"
284 OpDecorate %v_texcoord Location 0
285 OpDecorate %fragColor Location 0
286 OpDecorate %target_samp DescriptorSet 0
287 OpDecorate %target_samp Binding 4
288 OpDecorate %ref_samp DescriptorSet 0
289 OpDecorate %ref_samp Binding 5
290 OpDecorate %target_samp BlockMatchTextureQCOM
291 OpDecorate %target_samp BlockMatchSamplerQCOM
292 OpDecorate %ref_samp BlockMatchTextureQCOM
293 OpDecorate %ref_samp BlockMatchSamplerQCOM
294 %void = OpTypeVoid
295 %3 = OpTypeFunction %void
296 %uint = OpTypeInt 32 0
297 %v2uint = OpTypeVector %uint 2
298 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
299 %float = OpTypeFloat 32
300 %v4float = OpTypeVector %float 4
301 %_ptr_Input_v4float = OpTypePointer Input %v4float
302 %v_texcoord = OpVariable %_ptr_Input_v4float Input
303 %uint_0 = OpConstant %uint 0
304 %_ptr_Input_float = OpTypePointer Input %float
305 %_ptr_Function_uint = OpTypePointer Function %uint
306 %uint_1 = OpConstant %uint 1
307 %uint_2 = OpConstant %uint 2
308 %uint_3 = OpConstant %uint 3
309 %uint_4 = OpConstant %uint 4
310 %39 = OpConstantComposite %v2uint %uint_4 %uint_4
311 %_ptr_Output_v4float = OpTypePointer Output %v4float
312 %fragColor = OpVariable %_ptr_Output_v4float Output
313 %42 = OpTypeImage %float 2D 0 0 0 1 Unknown
314 %43 = OpTypeSampledImage %42
315 %_ptr_UniformConstant_43 = OpTypePointer UniformConstant %43
316 %target_samp = OpVariable %_ptr_UniformConstant_43 UniformConstant
317 %ref_samp = OpVariable %_ptr_UniformConstant_43 UniformConstant
318 %main = OpFunction %void None %3
319 %5 = OpLabel
320 %tgt_coords = OpVariable %_ptr_Function_v2uint Function
321 %ref_coords = OpVariable %_ptr_Function_v2uint Function
322 %blockSize = OpVariable %_ptr_Function_v2uint Function
323 %16 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_0
324 %17 = OpLoad %float %16
325 %18 = OpConvertFToU %uint %17
326 %20 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
327 OpStore %20 %18
328 %22 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_1
329 %23 = OpLoad %float %22
330 %24 = OpConvertFToU %uint %23
331 %25 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
332 OpStore %25 %24
333 %28 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_2
334 %29 = OpLoad %float %28
335 %30 = OpConvertFToU %uint %29
336 %31 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_0
337 OpStore %31 %30
338 %33 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_3
339 %34 = OpLoad %float %33
340 %35 = OpConvertFToU %uint %34
341 %36 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_1
342 OpStore %36 %35
343 OpStore %blockSize %39
344 %46 = OpLoad %43 %target_samp
345 %47 = OpLoad %v2uint %tgt_coords
346 %49 = OpLoad %43 %ref_samp
347 %50 = OpLoad %v2uint %ref_coords
348 %51 = OpLoad %v2uint %blockSize
349 %52 = OpImageBlockMatchWindowSADQCOM %v4float %46 %47 %49 %50 %51
350 OpStore %fragColor %52
351 OpReturn
352 OpFunctionEnd
353 )";
354 CompileSuccessfully(str.c_str(), SPV_ENV_UNIVERSAL_1_4);
355 ASSERT_EQ(SPV_ERROR_MISSING_EXTENSION,
356 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
357 EXPECT_THAT(getDiagnosticString(), HasSubstr("2nd operand of Decorate"));
358 EXPECT_THAT(getDiagnosticString(),
359 HasSubstr("requires one of these extensions"));
360 EXPECT_THAT(getDiagnosticString(), HasSubstr("SPV_QCOM_image_processing"));
361 }
362
363 TEST_F(ValidateExtensionCapabilities,
364 DeclCapabilityFailureBlockMatchWIndowSSD) {
365 const std::string str = R"(
366 OpCapability Shader
367 OpCapability TextureBlockMatch2QCOM
368 OpExtension "SPV_QCOM_image_processing2"
369 %1 = OpExtInstImport "GLSL.std.450"
370 OpMemoryModel Logical GLSL450
371 OpEntryPoint Fragment %main "main" %v_texcoord %fragColor %tex2D_src1 %samp %tex2D_src2
372 OpExecutionMode %main OriginUpperLeft
373 OpSource GLSL 450
374 OpSourceExtension "GL_QCOM_image_processing"
375 OpSourceExtension "GL_QCOM_image_processing2"
376 OpName %main "main"
377 OpName %tgt_coords "tgt_coords"
378 OpName %v_texcoord "v_texcoord"
379 OpName %ref_coords "ref_coords"
380 OpName %blockSize "blockSize"
381 OpName %fragColor "fragColor"
382 OpName %tex2D_src1 "tex2D_src1"
383 OpName %samp "samp"
384 OpName %tex2D_src2 "tex2D_src2"
385 OpDecorate %v_texcoord Location 0
386 OpDecorate %fragColor Location 0
387 OpDecorate %tex2D_src1 DescriptorSet 0
388 OpDecorate %tex2D_src1 Binding 1
389 OpDecorate %samp DescriptorSet 0
390 OpDecorate %samp Binding 3
391 OpDecorate %tex2D_src2 DescriptorSet 0
392 OpDecorate %tex2D_src2 Binding 2
393 OpDecorate %tex2D_src1 BlockMatchTextureQCOM
394 OpDecorate %samp BlockMatchSamplerQCOM
395 OpDecorate %tex2D_src2 BlockMatchTextureQCOM
396 OpDecorate %samp BlockMatchSamplerQCOM
397 %void = OpTypeVoid
398 %3 = OpTypeFunction %void
399 %uint = OpTypeInt 32 0
400 %v2uint = OpTypeVector %uint 2
401 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
402 %float = OpTypeFloat 32
403 %v4float = OpTypeVector %float 4
404 %_ptr_Input_v4float = OpTypePointer Input %v4float
405 %v_texcoord = OpVariable %_ptr_Input_v4float Input
406 %uint_0 = OpConstant %uint 0
407 %_ptr_Input_float = OpTypePointer Input %float
408 %_ptr_Function_uint = OpTypePointer Function %uint
409 %uint_1 = OpConstant %uint 1
410 %uint_2 = OpConstant %uint 2
411 %uint_3 = OpConstant %uint 3
412 %uint_4 = OpConstant %uint 4
413 %39 = OpConstantComposite %v2uint %uint_4 %uint_4
414 %_ptr_Output_v4float = OpTypePointer Output %v4float
415 %fragColor = OpVariable %_ptr_Output_v4float Output
416 %42 = OpTypeImage %float 2D 0 0 0 1 Unknown
417 %_ptr_UniformConstant_42 = OpTypePointer UniformConstant %42
418 %tex2D_src1 = OpVariable %_ptr_UniformConstant_42 UniformConstant
419 %46 = OpTypeSampler
420 %_ptr_UniformConstant_46 = OpTypePointer UniformConstant %46
421 %samp = OpVariable %_ptr_UniformConstant_46 UniformConstant
422 %50 = OpTypeSampledImage %42
423 %tex2D_src2 = OpVariable %_ptr_UniformConstant_42 UniformConstant
424 %main = OpFunction %void None %3
425 %5 = OpLabel
426 %tgt_coords = OpVariable %_ptr_Function_v2uint Function
427 %ref_coords = OpVariable %_ptr_Function_v2uint Function
428 %blockSize = OpVariable %_ptr_Function_v2uint Function
429 %16 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_0
430 %17 = OpLoad %float %16
431 %18 = OpConvertFToU %uint %17
432 %20 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
433 OpStore %20 %18
434 %22 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_1
435 %23 = OpLoad %float %22
436 %24 = OpConvertFToU %uint %23
437 %25 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
438 OpStore %25 %24
439 %28 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_2
440 %29 = OpLoad %float %28
441 %30 = OpConvertFToU %uint %29
442 %31 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_0
443 OpStore %31 %30
444 %33 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_3
445 %34 = OpLoad %float %33
446 %35 = OpConvertFToU %uint %34
447 %36 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_1
448 OpStore %36 %35
449 OpStore %blockSize %39
450 %45 = OpLoad %42 %tex2D_src1
451 %49 = OpLoad %46 %samp
452 %51 = OpSampledImage %50 %45 %49
453 %52 = OpLoad %v2uint %tgt_coords
454 %54 = OpLoad %42 %tex2D_src2
455 %55 = OpLoad %46 %samp
456 %56 = OpSampledImage %50 %54 %55
457 %57 = OpLoad %v2uint %ref_coords
458 %58 = OpLoad %v2uint %blockSize
459 %59 = OpImageBlockMatchWindowSSDQCOM %v4float %51 %52 %56 %57 %58
460 OpStore %fragColor %59
461 OpReturn
462 OpFunctionEnd
463 )";
464 CompileSuccessfully(str.c_str(), SPV_ENV_UNIVERSAL_1_4);
465 ASSERT_EQ(SPV_ERROR_MISSING_EXTENSION,
466 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
467 EXPECT_THAT(getDiagnosticString(), HasSubstr("2nd operand of Decorate"));
468 EXPECT_THAT(getDiagnosticString(),
469 HasSubstr("requires one of these extensions"));
470 EXPECT_THAT(getDiagnosticString(), HasSubstr("SPV_QCOM_image_processing"));
471 }
472
473 using ValidateAMDShaderBallotCapabilities = spvtest::ValidateBase<std::string>;
474
475 // Returns a vector of strings for the prefix of a SPIR-V assembly shader
476 // that can use the group instructions introduced by SPV_AMD_shader_ballot.
477 std::vector<std::string> ShaderPartsForAMDShaderBallot() {
478 return std::vector<std::string>{R"(
479 OpCapability Shader
480 OpCapability Linkage
481 )",
482 R"(
483 OpMemoryModel Logical GLSL450
484 %float = OpTypeFloat 32
485 %uint = OpTypeInt 32 0
486 %int = OpTypeInt 32 1
487 %scope = OpConstant %uint 3
488 %uint_const = OpConstant %uint 42
489 %int_const = OpConstant %uint 45
490 %float_const = OpConstant %float 3.5
491
492 %void = OpTypeVoid
493 %fn_ty = OpTypeFunction %void
494 %fn = OpFunction %void None %fn_ty
495 %entry = OpLabel
496 )"};
497 }
498
499 // Returns a list of SPIR-V assembly strings, where each uses only types
500 // and IDs that can fit with a shader made from parts from the result
501 // of ShaderPartsForAMDShaderBallot.
502 std::vector<std::string> AMDShaderBallotGroupInstructions() {
503 return std::vector<std::string>{
504 "%iadd_reduce = OpGroupIAddNonUniformAMD %uint %scope Reduce %uint_const",
505 "%iadd_iscan = OpGroupIAddNonUniformAMD %uint %scope InclusiveScan "
506 "%uint_const",
507 "%iadd_escan = OpGroupIAddNonUniformAMD %uint %scope ExclusiveScan "
508 "%uint_const",
509
510 "%fadd_reduce = OpGroupFAddNonUniformAMD %float %scope Reduce "
511 "%float_const",
512 "%fadd_iscan = OpGroupFAddNonUniformAMD %float %scope InclusiveScan "
513 "%float_const",
514 "%fadd_escan = OpGroupFAddNonUniformAMD %float %scope ExclusiveScan "
515 "%float_const",
516
517 "%fmin_reduce = OpGroupFMinNonUniformAMD %float %scope Reduce "
518 "%float_const",
519 "%fmin_iscan = OpGroupFMinNonUniformAMD %float %scope InclusiveScan "
520 "%float_const",
521 "%fmin_escan = OpGroupFMinNonUniformAMD %float %scope ExclusiveScan "
522 "%float_const",
523
524 "%umin_reduce = OpGroupUMinNonUniformAMD %uint %scope Reduce %uint_const",
525 "%umin_iscan = OpGroupUMinNonUniformAMD %uint %scope InclusiveScan "
526 "%uint_const",
527 "%umin_escan = OpGroupUMinNonUniformAMD %uint %scope ExclusiveScan "
528 "%uint_const",
529
530 "%smin_reduce = OpGroupUMinNonUniformAMD %int %scope Reduce %int_const",
531 "%smin_iscan = OpGroupUMinNonUniformAMD %int %scope InclusiveScan "
532 "%int_const",
533 "%smin_escan = OpGroupUMinNonUniformAMD %int %scope ExclusiveScan "
534 "%int_const",
535
536 "%fmax_reduce = OpGroupFMaxNonUniformAMD %float %scope Reduce "
537 "%float_const",
538 "%fmax_iscan = OpGroupFMaxNonUniformAMD %float %scope InclusiveScan "
539 "%float_const",
540 "%fmax_escan = OpGroupFMaxNonUniformAMD %float %scope ExclusiveScan "
541 "%float_const",
542
543 "%umax_reduce = OpGroupUMaxNonUniformAMD %uint %scope Reduce %uint_const",
544 "%umax_iscan = OpGroupUMaxNonUniformAMD %uint %scope InclusiveScan "
545 "%uint_const",
546 "%umax_escan = OpGroupUMaxNonUniformAMD %uint %scope ExclusiveScan "
547 "%uint_const",
548
549 "%smax_reduce = OpGroupUMaxNonUniformAMD %int %scope Reduce %int_const",
550 "%smax_iscan = OpGroupUMaxNonUniformAMD %int %scope InclusiveScan "
551 "%int_const",
552 "%smax_escan = OpGroupUMaxNonUniformAMD %int %scope ExclusiveScan "
553 "%int_const"};
554 }
555
556 TEST_P(ValidateAMDShaderBallotCapabilities, ExpectSuccess) {
557 // Succeed because the module specifies the SPV_AMD_shader_ballot extension.
558 auto parts = ShaderPartsForAMDShaderBallot();
559
560 const std::string assembly =
561 parts[0] + "OpExtension \"SPV_AMD_shader_ballot\"\n" + parts[1] +
562 GetParam() + "\nOpReturn OpFunctionEnd";
563
564 CompileSuccessfully(assembly.c_str());
565 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
566 }
567
568 INSTANTIATE_TEST_SUITE_P(ExpectSuccess, ValidateAMDShaderBallotCapabilities,
569 ValuesIn(AMDShaderBallotGroupInstructions()));
570
571 TEST_P(ValidateAMDShaderBallotCapabilities, ExpectFailure) {
572 // Fail because the module does not specify the SPV_AMD_shader_ballot
573 // extension.
574 auto parts = ShaderPartsForAMDShaderBallot();
575
576 const std::string assembly =
577 parts[0] + parts[1] + GetParam() + "\nOpReturn OpFunctionEnd";
578
579 CompileSuccessfully(assembly.c_str());
580 EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
581
582 // Make sure we get an appropriate error message.
583 // Find just the opcode name, skipping over the "Op" part.
584 auto prefix_with_opcode = GetParam().substr(GetParam().find("Group"));
585 auto opcode = prefix_with_opcode.substr(0, prefix_with_opcode.find(' '));
586 EXPECT_THAT(
587 getDiagnosticString(),
588 HasSubstr(std::string("Opcode " + opcode +
589 " requires one of these capabilities: Groups")));
590 }
591
592 INSTANTIATE_TEST_SUITE_P(ExpectFailure, ValidateAMDShaderBallotCapabilities,
593 ValuesIn(AMDShaderBallotGroupInstructions()));
594
595 struct ExtIntoCoreCase {
596 const char* ext;
597 const char* cap;
598 const char* builtin;
599 spv_target_env env;
600 bool success;
601 };
602
603 using ValidateExtIntoCore = spvtest::ValidateBase<ExtIntoCoreCase>;
604
605 // Make sure that we don't panic about missing extensions for using
606 // functionalities that introduced in extensions but became core SPIR-V later.
607
608 TEST_P(ValidateExtIntoCore, DoNotAskForExtensionInLaterVersion) {
609 const std::string code = std::string(R"(
610 OpCapability Shader
611 OpCapability )") +
612 GetParam().cap + R"(
613 OpMemoryModel Logical GLSL450
614 OpEntryPoint Vertex %main "main" %builtin
615 OpDecorate %builtin BuiltIn )" +
616 GetParam().builtin + R"(
617 %void = OpTypeVoid
618 %3 = OpTypeFunction %void
619 %int = OpTypeInt 32 1
620 %_ptr_Input_int = OpTypePointer Input %int
621 %builtin = OpVariable %_ptr_Input_int Input
622 %main = OpFunction %void None %3
623 %5 = OpLabel
624 %18 = OpLoad %int %builtin
625 OpReturn
626 OpFunctionEnd)";
627
628 CompileSuccessfully(code.c_str(), GetParam().env);
629 if (GetParam().success) {
630 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(GetParam().env))
631 << getDiagnosticString();
632 } else {
633 ASSERT_NE(SPV_SUCCESS, ValidateInstructions(GetParam().env))
634 << " in " << spvTargetEnvDescription(GetParam().env) << ":\n"
635 << code;
636 const std::string message = getDiagnosticString();
637 if (spvIsVulkanEnv(GetParam().env)) {
638 EXPECT_THAT(message, HasSubstr(std::string(GetParam().cap) +
639 " is not allowed by Vulkan"));
640 EXPECT_THAT(message, HasSubstr(std::string("or requires extension")));
641 } else {
642 EXPECT_THAT(message,
643 HasSubstr(std::string("requires one of these extensions: ") +
644 GetParam().ext));
645 }
646 }
647 }
648
649 // clang-format off
650 INSTANTIATE_TEST_SUITE_P(
651 KHR_extensions, ValidateExtIntoCore,
652 ValuesIn(std::vector<ExtIntoCoreCase>{
653 // SPV_KHR_shader_draw_parameters became core SPIR-V 1.3
654 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_3, true},
655 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_2, false},
656 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_1, false},
657 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_0, false},
658 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_VULKAN_1_1, true},
659 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_VULKAN_1_0, false},
660
661 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseInstance", SPV_ENV_UNIVERSAL_1_3, true},
662 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseInstance", SPV_ENV_VULKAN_1_0, false},
663
664 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "DrawIndex", SPV_ENV_UNIVERSAL_1_3, true},
665 {"SPV_KHR_shader_draw_parameters", "DrawParameters", "DrawIndex", SPV_ENV_UNIVERSAL_1_1, false},
666
667 // SPV_KHR_multiview became core SPIR-V 1.3
668 {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_3, true},
669 {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_2, false},
670 {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_1, false},
671 {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_0, false},
672 {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_VULKAN_1_1, true},
673 {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_VULKAN_1_0, false},
674
675 // SPV_KHR_device_group became core SPIR-V 1.3
676 {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_3, true},
677 {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_2, false},
678 {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_1, false},
679 {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_0, false},
680 {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_VULKAN_1_1, true},
681 {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_VULKAN_1_0, false},
682 }));
683 // clang-format on
684
685 using ValidateRelaxedExtendedInstructionExt = spvtest::ValidateBase<bool>;
686
687 TEST_F(ValidateRelaxedExtendedInstructionExt, RequiresExtension) {
688 const std::string str = R"(
689 OpCapability Shader
690 OpExtension "SPV_KHR_non_semantic_info"
691 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
692 OpMemoryModel Logical GLSL450
693 OpEntryPoint GLCompute %2 "main"
694 OpExecutionMode %2 LocalSize 1 1 1
695 %3 = OpString "sample"
696 %void = OpTypeVoid
697 %uint = OpTypeInt 32 0
698 %uint_0 = OpConstant %uint 0
699 %7 = OpTypeFunction %void
700 %8 = OpExtInst %void %1 DebugSource %3 %3
701 %9 = OpExtInst %void %1 DebugCompilationUnit %uint_0 %uint_0 %8 %uint_0
702 %10 = OpExtInstWithForwardRefsKHR %void %1 DebugTypeFunction %uint_0 %11
703 %12 = OpExtInstWithForwardRefsKHR %void %1 DebugFunction %3 %10 %8 %uint_0 %uint_0 %11 %3 %uint_0 %uint_0
704 %11 = OpExtInst %void %1 DebugTypeComposite %3 %uint_0 %8 %uint_0 %uint_0 %9 %3 %uint_0 %uint_0 %12
705 %2 = OpFunction %void None %7
706 %13 = OpLabel
707 OpReturn
708 OpFunctionEnd
709 )";
710
711 CompileSuccessfully(str.c_str());
712 EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
713 EXPECT_THAT(
714 getDiagnosticString(),
715 HasSubstr(
716 "ExtInstWithForwardRefsKHR requires one of the following extensions:"
717 " SPV_KHR_relaxed_extended_instruction \n"
718 " %10 = OpExtInstWithForwardRefsKHR %void %1 DebugTypeFunction "
719 "%uint_0 "
720 "%11\n"));
721 }
722
723 } // namespace
724 } // namespace val
725 } // namespace spvtools