• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
GetErrorString(const std::string & extension)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",
65         "SPV_KHR_relaxed_extended_instruction"));
66 
67 INSTANTIATE_TEST_SUITE_P(FailSilently, ValidateUnknownExtensions,
68                          Values("ERROR_unknown_extension", "SPV_KHR_",
69                                 "SPV_KHR_shader_ballot_ERROR"));
70 
TEST_P(ValidateKnownExtensions,ExpectSuccess)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 
TEST_P(ValidateUnknownExtensions,FailSilently)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 
TEST_F(ValidateUnknownExtensions,HitMaxNumOfWarnings)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 
TEST_F(ValidateExtensionCapabilities,DeclCapabilitySuccess)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 
TEST_F(ValidateExtensionCapabilities,DeclCapabilityFailure)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 
TEST_F(ValidateExtensionCapabilities,DeclCapabilityFailureBlockMatchWIndowSAD)135 TEST_F(ValidateExtensionCapabilities,
136        DeclCapabilityFailureBlockMatchWIndowSAD) {
137   const std::string str = R"(
138                OpCapability Shader
139                OpCapability TextureBlockMatch2QCOM
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                OpName %main "main"
149                OpName %tgt_coords "tgt_coords"
150                OpName %v_texcoord "v_texcoord"
151                OpName %ref_coords "ref_coords"
152                OpName %blockSize "blockSize"
153                OpName %fragColor "fragColor"
154                OpName %target_samp "target_samp"
155                OpName %ref_samp "ref_samp"
156                OpDecorate %v_texcoord Location 0
157                OpDecorate %fragColor Location 0
158                OpDecorate %target_samp DescriptorSet 0
159                OpDecorate %target_samp Binding 4
160                OpDecorate %ref_samp DescriptorSet 0
161                OpDecorate %ref_samp Binding 5
162                OpDecorate %target_samp BlockMatchTextureQCOM
163                OpDecorate %target_samp BlockMatchSamplerQCOM
164                OpDecorate %ref_samp BlockMatchTextureQCOM
165                OpDecorate %ref_samp BlockMatchSamplerQCOM
166        %void = OpTypeVoid
167           %3 = OpTypeFunction %void
168        %uint = OpTypeInt 32 0
169      %v2uint = OpTypeVector %uint 2
170 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
171       %float = OpTypeFloat 32
172     %v4float = OpTypeVector %float 4
173 %_ptr_Input_v4float = OpTypePointer Input %v4float
174  %v_texcoord = OpVariable %_ptr_Input_v4float Input
175      %uint_0 = OpConstant %uint 0
176 %_ptr_Input_float = OpTypePointer Input %float
177 %_ptr_Function_uint = OpTypePointer Function %uint
178      %uint_1 = OpConstant %uint 1
179      %uint_2 = OpConstant %uint 2
180      %uint_3 = OpConstant %uint 3
181      %uint_4 = OpConstant %uint 4
182          %39 = OpConstantComposite %v2uint %uint_4 %uint_4
183 %_ptr_Output_v4float = OpTypePointer Output %v4float
184   %fragColor = OpVariable %_ptr_Output_v4float Output
185          %42 = OpTypeImage %float 2D 0 0 0 1 Unknown
186          %43 = OpTypeSampledImage %42
187 %_ptr_UniformConstant_43 = OpTypePointer UniformConstant %43
188 %target_samp = OpVariable %_ptr_UniformConstant_43 UniformConstant
189    %ref_samp = OpVariable %_ptr_UniformConstant_43 UniformConstant
190        %main = OpFunction %void None %3
191           %5 = OpLabel
192  %tgt_coords = OpVariable %_ptr_Function_v2uint Function
193  %ref_coords = OpVariable %_ptr_Function_v2uint Function
194   %blockSize = OpVariable %_ptr_Function_v2uint Function
195          %16 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_0
196          %17 = OpLoad %float %16
197          %18 = OpConvertFToU %uint %17
198          %20 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
199                OpStore %20 %18
200          %22 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_1
201          %23 = OpLoad %float %22
202          %24 = OpConvertFToU %uint %23
203          %25 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
204                OpStore %25 %24
205          %28 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_2
206          %29 = OpLoad %float %28
207          %30 = OpConvertFToU %uint %29
208          %31 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_0
209                OpStore %31 %30
210          %33 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_3
211          %34 = OpLoad %float %33
212          %35 = OpConvertFToU %uint %34
213          %36 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_1
214                OpStore %36 %35
215                OpStore %blockSize %39
216          %46 = OpLoad %43 %target_samp
217          %47 = OpLoad %v2uint %tgt_coords
218          %49 = OpLoad %43 %ref_samp
219          %50 = OpLoad %v2uint %ref_coords
220          %51 = OpLoad %v2uint %blockSize
221          %52 = OpImageBlockMatchWindowSADQCOM %v4float %46 %47 %49 %50 %51
222                OpStore %fragColor %52
223                OpReturn
224                OpFunctionEnd
225 )";
226   CompileSuccessfully(str.c_str());
227   ASSERT_EQ(SPV_ERROR_MISSING_EXTENSION, ValidateInstructions());
228   EXPECT_THAT(getDiagnosticString(), HasSubstr("2nd operand of Decorate"));
229   EXPECT_THAT(getDiagnosticString(),
230               HasSubstr("requires one of these extensions"));
231   EXPECT_THAT(getDiagnosticString(), HasSubstr("SPV_QCOM_image_processing"));
232 }
233 
TEST_F(ValidateExtensionCapabilities,DeclCapabilityFailureBlockMatchWIndowSSD)234 TEST_F(ValidateExtensionCapabilities,
235        DeclCapabilityFailureBlockMatchWIndowSSD) {
236   const std::string str = R"(
237                OpCapability Shader
238                OpCapability TextureBlockMatch2QCOM
239                OpExtension "SPV_QCOM_image_processing2"
240           %1 = OpExtInstImport "GLSL.std.450"
241                OpMemoryModel Logical GLSL450
242                OpEntryPoint Fragment %main "main" %v_texcoord %fragColor %tex2D_src1 %samp %tex2D_src2
243                OpExecutionMode %main OriginUpperLeft
244                OpSource GLSL 450
245                OpSourceExtension "GL_QCOM_image_processing"
246                OpSourceExtension "GL_QCOM_image_processing2"
247                OpName %main "main"
248                OpName %tgt_coords "tgt_coords"
249                OpName %v_texcoord "v_texcoord"
250                OpName %ref_coords "ref_coords"
251                OpName %blockSize "blockSize"
252                OpName %fragColor "fragColor"
253                OpName %tex2D_src1 "tex2D_src1"
254                OpName %samp "samp"
255                OpName %tex2D_src2 "tex2D_src2"
256                OpDecorate %v_texcoord Location 0
257                OpDecorate %fragColor Location 0
258                OpDecorate %tex2D_src1 DescriptorSet 0
259                OpDecorate %tex2D_src1 Binding 1
260                OpDecorate %samp DescriptorSet 0
261                OpDecorate %samp Binding 3
262                OpDecorate %tex2D_src2 DescriptorSet 0
263                OpDecorate %tex2D_src2 Binding 2
264                OpDecorate %tex2D_src1 BlockMatchTextureQCOM
265                OpDecorate %samp BlockMatchSamplerQCOM
266                OpDecorate %tex2D_src2 BlockMatchTextureQCOM
267                OpDecorate %samp BlockMatchSamplerQCOM
268        %void = OpTypeVoid
269           %3 = OpTypeFunction %void
270        %uint = OpTypeInt 32 0
271      %v2uint = OpTypeVector %uint 2
272 %_ptr_Function_v2uint = OpTypePointer Function %v2uint
273       %float = OpTypeFloat 32
274     %v4float = OpTypeVector %float 4
275 %_ptr_Input_v4float = OpTypePointer Input %v4float
276  %v_texcoord = OpVariable %_ptr_Input_v4float Input
277      %uint_0 = OpConstant %uint 0
278 %_ptr_Input_float = OpTypePointer Input %float
279 %_ptr_Function_uint = OpTypePointer Function %uint
280      %uint_1 = OpConstant %uint 1
281      %uint_2 = OpConstant %uint 2
282      %uint_3 = OpConstant %uint 3
283      %uint_4 = OpConstant %uint 4
284          %39 = OpConstantComposite %v2uint %uint_4 %uint_4
285 %_ptr_Output_v4float = OpTypePointer Output %v4float
286   %fragColor = OpVariable %_ptr_Output_v4float Output
287          %42 = OpTypeImage %float 2D 0 0 0 1 Unknown
288 %_ptr_UniformConstant_42 = OpTypePointer UniformConstant %42
289  %tex2D_src1 = OpVariable %_ptr_UniformConstant_42 UniformConstant
290          %46 = OpTypeSampler
291 %_ptr_UniformConstant_46 = OpTypePointer UniformConstant %46
292        %samp = OpVariable %_ptr_UniformConstant_46 UniformConstant
293          %50 = OpTypeSampledImage %42
294  %tex2D_src2 = OpVariable %_ptr_UniformConstant_42 UniformConstant
295        %main = OpFunction %void None %3
296           %5 = OpLabel
297  %tgt_coords = OpVariable %_ptr_Function_v2uint Function
298  %ref_coords = OpVariable %_ptr_Function_v2uint Function
299   %blockSize = OpVariable %_ptr_Function_v2uint Function
300          %16 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_0
301          %17 = OpLoad %float %16
302          %18 = OpConvertFToU %uint %17
303          %20 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
304                OpStore %20 %18
305          %22 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_1
306          %23 = OpLoad %float %22
307          %24 = OpConvertFToU %uint %23
308          %25 = OpAccessChain %_ptr_Function_uint %tgt_coords %uint_0
309                OpStore %25 %24
310          %28 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_2
311          %29 = OpLoad %float %28
312          %30 = OpConvertFToU %uint %29
313          %31 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_0
314                OpStore %31 %30
315          %33 = OpAccessChain %_ptr_Input_float %v_texcoord %uint_3
316          %34 = OpLoad %float %33
317          %35 = OpConvertFToU %uint %34
318          %36 = OpAccessChain %_ptr_Function_uint %ref_coords %uint_1
319                OpStore %36 %35
320                OpStore %blockSize %39
321          %45 = OpLoad %42 %tex2D_src1
322          %49 = OpLoad %46 %samp
323          %51 = OpSampledImage %50 %45 %49
324          %52 = OpLoad %v2uint %tgt_coords
325          %54 = OpLoad %42 %tex2D_src2
326          %55 = OpLoad %46 %samp
327          %56 = OpSampledImage %50 %54 %55
328          %57 = OpLoad %v2uint %ref_coords
329          %58 = OpLoad %v2uint %blockSize
330          %59 = OpImageBlockMatchWindowSSDQCOM %v4float %51 %52 %56 %57 %58
331                OpStore %fragColor %59
332                OpReturn
333                OpFunctionEnd
334 )";
335   CompileSuccessfully(str.c_str());
336   ASSERT_EQ(SPV_ERROR_MISSING_EXTENSION, ValidateInstructions());
337   EXPECT_THAT(getDiagnosticString(), HasSubstr("2nd operand of Decorate"));
338   EXPECT_THAT(getDiagnosticString(),
339               HasSubstr("requires one of these extensions"));
340   EXPECT_THAT(getDiagnosticString(), HasSubstr("SPV_QCOM_image_processing"));
341 }
342 
343 using ValidateAMDShaderBallotCapabilities = spvtest::ValidateBase<std::string>;
344 
345 // Returns a vector of strings for the prefix of a SPIR-V assembly shader
346 // that can use the group instructions introduced by SPV_AMD_shader_ballot.
ShaderPartsForAMDShaderBallot()347 std::vector<std::string> ShaderPartsForAMDShaderBallot() {
348   return std::vector<std::string>{R"(
349   OpCapability Shader
350   OpCapability Linkage
351   )",
352                                   R"(
353   OpMemoryModel Logical GLSL450
354   %float = OpTypeFloat 32
355   %uint = OpTypeInt 32 0
356   %int = OpTypeInt 32 1
357   %scope = OpConstant %uint 3
358   %uint_const = OpConstant %uint 42
359   %int_const = OpConstant %uint 45
360   %float_const = OpConstant %float 3.5
361 
362   %void = OpTypeVoid
363   %fn_ty = OpTypeFunction %void
364   %fn = OpFunction %void None %fn_ty
365   %entry = OpLabel
366   )"};
367 }
368 
369 // Returns a list of SPIR-V assembly strings, where each uses only types
370 // and IDs that can fit with a shader made from parts from the result
371 // of ShaderPartsForAMDShaderBallot.
AMDShaderBallotGroupInstructions()372 std::vector<std::string> AMDShaderBallotGroupInstructions() {
373   return std::vector<std::string>{
374       "%iadd_reduce = OpGroupIAddNonUniformAMD %uint %scope Reduce %uint_const",
375       "%iadd_iscan = OpGroupIAddNonUniformAMD %uint %scope InclusiveScan "
376       "%uint_const",
377       "%iadd_escan = OpGroupIAddNonUniformAMD %uint %scope ExclusiveScan "
378       "%uint_const",
379 
380       "%fadd_reduce = OpGroupFAddNonUniformAMD %float %scope Reduce "
381       "%float_const",
382       "%fadd_iscan = OpGroupFAddNonUniformAMD %float %scope InclusiveScan "
383       "%float_const",
384       "%fadd_escan = OpGroupFAddNonUniformAMD %float %scope ExclusiveScan "
385       "%float_const",
386 
387       "%fmin_reduce = OpGroupFMinNonUniformAMD %float %scope Reduce "
388       "%float_const",
389       "%fmin_iscan = OpGroupFMinNonUniformAMD %float %scope InclusiveScan "
390       "%float_const",
391       "%fmin_escan = OpGroupFMinNonUniformAMD %float %scope ExclusiveScan "
392       "%float_const",
393 
394       "%umin_reduce = OpGroupUMinNonUniformAMD %uint %scope Reduce %uint_const",
395       "%umin_iscan = OpGroupUMinNonUniformAMD %uint %scope InclusiveScan "
396       "%uint_const",
397       "%umin_escan = OpGroupUMinNonUniformAMD %uint %scope ExclusiveScan "
398       "%uint_const",
399 
400       "%smin_reduce = OpGroupUMinNonUniformAMD %int %scope Reduce %int_const",
401       "%smin_iscan = OpGroupUMinNonUniformAMD %int %scope InclusiveScan "
402       "%int_const",
403       "%smin_escan = OpGroupUMinNonUniformAMD %int %scope ExclusiveScan "
404       "%int_const",
405 
406       "%fmax_reduce = OpGroupFMaxNonUniformAMD %float %scope Reduce "
407       "%float_const",
408       "%fmax_iscan = OpGroupFMaxNonUniformAMD %float %scope InclusiveScan "
409       "%float_const",
410       "%fmax_escan = OpGroupFMaxNonUniformAMD %float %scope ExclusiveScan "
411       "%float_const",
412 
413       "%umax_reduce = OpGroupUMaxNonUniformAMD %uint %scope Reduce %uint_const",
414       "%umax_iscan = OpGroupUMaxNonUniformAMD %uint %scope InclusiveScan "
415       "%uint_const",
416       "%umax_escan = OpGroupUMaxNonUniformAMD %uint %scope ExclusiveScan "
417       "%uint_const",
418 
419       "%smax_reduce = OpGroupUMaxNonUniformAMD %int %scope Reduce %int_const",
420       "%smax_iscan = OpGroupUMaxNonUniformAMD %int %scope InclusiveScan "
421       "%int_const",
422       "%smax_escan = OpGroupUMaxNonUniformAMD %int %scope ExclusiveScan "
423       "%int_const"};
424 }
425 
TEST_P(ValidateAMDShaderBallotCapabilities,ExpectSuccess)426 TEST_P(ValidateAMDShaderBallotCapabilities, ExpectSuccess) {
427   // Succeed because the module specifies the SPV_AMD_shader_ballot extension.
428   auto parts = ShaderPartsForAMDShaderBallot();
429 
430   const std::string assembly =
431       parts[0] + "OpExtension \"SPV_AMD_shader_ballot\"\n" + parts[1] +
432       GetParam() + "\nOpReturn OpFunctionEnd";
433 
434   CompileSuccessfully(assembly.c_str());
435   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
436 }
437 
438 INSTANTIATE_TEST_SUITE_P(ExpectSuccess, ValidateAMDShaderBallotCapabilities,
439                          ValuesIn(AMDShaderBallotGroupInstructions()));
440 
TEST_P(ValidateAMDShaderBallotCapabilities,ExpectFailure)441 TEST_P(ValidateAMDShaderBallotCapabilities, ExpectFailure) {
442   // Fail because the module does not specify the SPV_AMD_shader_ballot
443   // extension.
444   auto parts = ShaderPartsForAMDShaderBallot();
445 
446   const std::string assembly =
447       parts[0] + parts[1] + GetParam() + "\nOpReturn OpFunctionEnd";
448 
449   CompileSuccessfully(assembly.c_str());
450   EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
451 
452   // Make sure we get an appropriate error message.
453   // Find just the opcode name, skipping over the "Op" part.
454   auto prefix_with_opcode = GetParam().substr(GetParam().find("Group"));
455   auto opcode = prefix_with_opcode.substr(0, prefix_with_opcode.find(' '));
456   EXPECT_THAT(
457       getDiagnosticString(),
458       HasSubstr(std::string("Opcode " + opcode +
459                             " requires one of these capabilities: Groups")));
460 }
461 
462 INSTANTIATE_TEST_SUITE_P(ExpectFailure, ValidateAMDShaderBallotCapabilities,
463                          ValuesIn(AMDShaderBallotGroupInstructions()));
464 
465 struct ExtIntoCoreCase {
466   const char* ext;
467   const char* cap;
468   const char* builtin;
469   spv_target_env env;
470   bool success;
471 };
472 
473 using ValidateExtIntoCore = spvtest::ValidateBase<ExtIntoCoreCase>;
474 
475 // Make sure that we don't panic about missing extensions for using
476 // functionalities that introduced in extensions but became core SPIR-V later.
477 
TEST_P(ValidateExtIntoCore,DoNotAskForExtensionInLaterVersion)478 TEST_P(ValidateExtIntoCore, DoNotAskForExtensionInLaterVersion) {
479   const std::string code = std::string(R"(
480                OpCapability Shader
481                OpCapability )") +
482                            GetParam().cap + R"(
483                OpMemoryModel Logical GLSL450
484                OpEntryPoint Vertex %main "main" %builtin
485                OpDecorate %builtin BuiltIn )" + GetParam().builtin + R"(
486        %void = OpTypeVoid
487           %3 = OpTypeFunction %void
488         %int = OpTypeInt 32 1
489 %_ptr_Input_int = OpTypePointer Input %int
490     %builtin = OpVariable %_ptr_Input_int Input
491        %main = OpFunction %void None %3
492           %5 = OpLabel
493          %18 = OpLoad %int %builtin
494                OpReturn
495                OpFunctionEnd)";
496 
497   CompileSuccessfully(code.c_str(), GetParam().env);
498   if (GetParam().success) {
499     ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(GetParam().env))
500         << getDiagnosticString();
501   } else {
502     ASSERT_NE(SPV_SUCCESS, ValidateInstructions(GetParam().env))
503         << " in " << spvTargetEnvDescription(GetParam().env) << ":\n"
504         << code;
505     const std::string message = getDiagnosticString();
506     if (spvIsVulkanEnv(GetParam().env)) {
507       EXPECT_THAT(message, HasSubstr(std::string(GetParam().cap) +
508                                      " is not allowed by Vulkan"));
509       EXPECT_THAT(message, HasSubstr(std::string("or requires extension")));
510     } else {
511       EXPECT_THAT(message,
512                   HasSubstr(std::string("requires one of these extensions: ") +
513                             GetParam().ext));
514     }
515   }
516 }
517 
518 // clang-format off
519 INSTANTIATE_TEST_SUITE_P(
520     KHR_extensions, ValidateExtIntoCore,
521     ValuesIn(std::vector<ExtIntoCoreCase>{
522         // SPV_KHR_shader_draw_parameters became core SPIR-V 1.3
523         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_3, true},
524         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_2, false},
525         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_1, false},
526         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_0, false},
527         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_VULKAN_1_1, true},
528         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_VULKAN_1_0, false},
529 
530         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseInstance", SPV_ENV_UNIVERSAL_1_3, true},
531         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseInstance", SPV_ENV_VULKAN_1_0, false},
532 
533         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "DrawIndex", SPV_ENV_UNIVERSAL_1_3, true},
534         {"SPV_KHR_shader_draw_parameters", "DrawParameters", "DrawIndex", SPV_ENV_UNIVERSAL_1_1, false},
535 
536         // SPV_KHR_multiview became core SPIR-V 1.3
537         {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_3, true},
538         {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_2, false},
539         {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_1, false},
540         {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_0, false},
541         {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_VULKAN_1_1, true},
542         {"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_VULKAN_1_0, false},
543 
544         // SPV_KHR_device_group became core SPIR-V 1.3
545         {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_3, true},
546         {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_2, false},
547         {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_1, false},
548         {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_0, false},
549         {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_VULKAN_1_1, true},
550         {"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_VULKAN_1_0, false},
551     }));
552 // clang-format on
553 
554 using ValidateRelaxedExtendedInstructionExt = spvtest::ValidateBase<bool>;
555 
TEST_F(ValidateRelaxedExtendedInstructionExt,RequiresExtension)556 TEST_F(ValidateRelaxedExtendedInstructionExt, RequiresExtension) {
557   const std::string str = R"(
558              OpCapability Shader
559              OpExtension "SPV_KHR_non_semantic_info"
560         %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
561              OpMemoryModel Logical GLSL450
562              OpEntryPoint GLCompute %2 "main"
563              OpExecutionMode %2 LocalSize 1 1 1
564         %3 = OpString "sample"
565      %void = OpTypeVoid
566      %uint = OpTypeInt 32 0
567    %uint_0 = OpConstant %uint 0
568         %7 = OpTypeFunction %void
569         %8 = OpExtInst %void %1 DebugSource %3 %3
570         %9 = OpExtInst %void %1 DebugCompilationUnit %uint_0 %uint_0 %8 %uint_0
571        %10 = OpExtInstWithForwardRefsKHR %void %1 DebugTypeFunction %uint_0 %11
572        %12 = OpExtInstWithForwardRefsKHR %void %1 DebugFunction %3 %10 %8 %uint_0 %uint_0 %11 %3 %uint_0 %uint_0
573        %11 = OpExtInst %void %1 DebugTypeComposite %3 %uint_0 %8 %uint_0 %uint_0 %9 %3 %uint_0 %uint_0 %12
574         %2 = OpFunction %void None %7
575        %13 = OpLabel
576              OpReturn
577              OpFunctionEnd
578 )";
579 
580   CompileSuccessfully(str.c_str());
581   EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
582   EXPECT_THAT(
583       getDiagnosticString(),
584       HasSubstr(
585           "ExtInstWithForwardRefsKHR requires one of the following extensions:"
586           " SPV_KHR_relaxed_extended_instruction \n"
587           "  %10 = OpExtInstWithForwardRefsKHR %void %1 DebugTypeFunction "
588           "%uint_0 "
589           "%11\n"));
590 }
591 
592 }  // namespace
593 }  // namespace val
594 }  // namespace spvtools
595