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