| 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/enum_string_mapping.h" |
| 22 |
#include "source/extensions.h" |
| 23 |
#include "source/spirv_target_env.h" |
| 24 |
#include "test/test_fixture.h" |
| 25 |
#include "test/unit_spirv.h" |
| 26 |
#include "test/val/val_fixtures.h" |
| 27 |
|
| 28 |
namespace spvtools { |
| 29 |
namespace val { |
| 30 |
namespace { |
| 31 |
|
| 32 |
using ::testing::HasSubstr; |
| 33 |
using ::testing::Not; |
| 34 |
using ::testing::Values; |
| 35 |
using ::testing::ValuesIn; |
| 36 |
|
| 37 |
using ValidateKnownExtensions = spvtest::ValidateBase<std::string>; |
| 38 |
using ValidateUnknownExtensions = spvtest::ValidateBase<std::string>; |
| 39 |
using ValidateExtensionCapabilities = spvtest::ValidateBase<bool>; |
| 40 |
|
| 41 |
// Returns expected error string if |extension| is not recognized. |
| 42 |
std::string GetErrorString(const std::string& extension) { |
| 43 |
return "Found unrecognized extension " + extension; |
| 44 |
} |
| 45 |
|
| 46 |
INSTANTIATE_TEST_SUITE_P( |
| 47 |
ExpectSuccess, ValidateKnownExtensions, |
| 48 |
Values( |
| 49 |
// Match the order as published on the SPIR-V Registry. |
| 50 |
"SPV_AMD_shader_explicit_vertex_parameter", |
| 51 |
"SPV_AMD_shader_trinary_minmax", "SPV_AMD_gcn_shader", |
| 52 |
"SPV_KHR_shader_ballot", "SPV_AMD_shader_ballot", |
| 53 |
"SPV_AMD_gpu_shader_half_float", "SPV_KHR_shader_draw_parameters", |
| 54 |
"SPV_KHR_subgroup_vote", "SPV_KHR_16bit_storage", |
| 55 |
"SPV_KHR_device_group", "SPV_KHR_multiview", |
| 56 |
"SPV_NVX_multiview_per_view_attributes", "SPV_NV_viewport_array2", |
| 57 |
"SPV_NV_stereo_view_rendering", "SPV_NV_sample_mask_override_coverage", |
| 58 |
"SPV_NV_geometry_shader_passthrough", "SPV_AMD_texture_gather_bias_lod", |
| 59 |
"SPV_KHR_storage_buffer_storage_class", "SPV_KHR_variable_pointers", |
| 60 |
"SPV_AMD_gpu_shader_int16", "SPV_KHR_post_depth_coverage", |
| 61 |
"SPV_KHR_shader_atomic_counter_ops", "SPV_EXT_shader_stencil_export", |
| 62 |
"SPV_EXT_shader_viewport_index_layer", |
| 63 |
"SPV_AMD_shader_image_load_store_lod", "SPV_AMD_shader_fragment_mask", |
| 64 |
"SPV_GOOGLE_decorate_string", "SPV_GOOGLE_hlsl_functionality1", |
| 65 |
"SPV_NV_shader_subgroup_partitioned", "SPV_EXT_descriptor_indexing", |
| 66 |
"SPV_KHR_terminate_invocation")); |
| 67 |
|
| 68 |
INSTANTIATE_TEST_SUITE_P(FailSilently, ValidateUnknownExtensions, |
| 69 |
Values("ERROR_unknown_extension", "SPV_KHR_", |
| 70 |
"SPV_KHR_shader_ballot_ERROR")); |
| 71 |
|
| 72 |
TEST_P(ValidateKnownExtensions, ExpectSuccess) { |
| 73 |
const std::string extension = GetParam(); |
| 74 |
const std::string str = |
| 75 |
"OpCapability Shader\nOpCapability Linkage\nOpExtension \"" + extension + |
| 76 |
"\"\nOpMemoryModel Logical GLSL450"; |
| 77 |
CompileSuccessfully(str.c_str()); |
| 78 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 79 |
EXPECT_THAT(getDiagnosticString(), Not(HasSubstr(GetErrorString(extension)))); |
| 80 |
} |
| 81 |
|
| 82 |
TEST_P(ValidateUnknownExtensions, FailSilently) { |
| 83 |
const std::string extension = GetParam(); |
| 84 |
const std::string str = |
| 85 |
"OpCapability Shader\nOpCapability Linkage\nOpExtension \"" + extension + |
| 86 |
"\"\nOpMemoryModel Logical GLSL450"; |
| 87 |
CompileSuccessfully(str.c_str()); |
| 88 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 89 |
EXPECT_THAT(getDiagnosticString(), HasSubstr(GetErrorString(extension))); |
| 90 |
} |
| 91 |
|
| 92 |
TEST_F(ValidateUnknownExtensions, HitMaxNumOfWarnings) { |
| 93 |
const std::string str = |
| 94 |
std::string("OpCapability Shader\n") + "OpCapability Linkage\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 |
"OpExtension \"bad_ext\"\n" + "OpExtension \"bad_ext\"\n" + |
| 108 |
"OpMemoryModel Logical GLSL450"; |
| 109 |
CompileSuccessfully(str.c_str()); |
| 110 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 111 |
EXPECT_THAT(getDiagnosticString(), |
| 112 |
HasSubstr("Other warnings have been suppressed.")); |
| 113 |
} |
| 114 |
|
| 115 |
TEST_F(ValidateExtensionCapabilities, DeclCapabilitySuccess) { |
| 116 |
const std::string str = |
| 117 |
"OpCapability Shader\nOpCapability Linkage\nOpCapability DeviceGroup\n" |
| 118 |
"OpExtension \"SPV_KHR_device_group\"" |
| 119 |
"\nOpMemoryModel Logical GLSL450"; |
| 120 |
CompileSuccessfully(str.c_str()); |
| 121 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 122 |
} |
| 123 |
|
| 124 |
TEST_F(ValidateExtensionCapabilities, DeclCapabilityFailure) { |
| 125 |
const std::string str = |
| 126 |
"OpCapability Shader\nOpCapability Linkage\nOpCapability DeviceGroup\n" |
| 127 |
"\nOpMemoryModel Logical GLSL450"; |
| 128 |
CompileSuccessfully(str.c_str()); |
| 129 |
ASSERT_EQ(SPV_ERROR_MISSING_EXTENSION, ValidateInstructions()); |
| 130 |
EXPECT_THAT(getDiagnosticString(), HasSubstr("1st operand of Capability")); |
| 131 |
EXPECT_THAT(getDiagnosticString(), |
| 132 |
HasSubstr("requires one of these extensions")); |
| 133 |
EXPECT_THAT(getDiagnosticString(), HasSubstr("SPV_KHR_device_group")); |
| 134 |
} |
| 135 |
|
| 136 |
using ValidateAMDShaderBallotCapabilities = spvtest::ValidateBase<std::string>; |
| 137 |
|
| 138 |
// Returns a vector of strings for the prefix of a SPIR-V assembly shader |
| 139 |
// that can use the group instructions introduced by SPV_AMD_shader_ballot. |
| 140 |
std::vector<std::string> ShaderPartsForAMDShaderBallot() { |
| 141 |
return std::vector<std::string>{R"( |
| 142 |
OpCapability Shader |
| 143 |
OpCapability Linkage |
| 144 |
)", |
| 145 |
R"( |
| 146 |
OpMemoryModel Logical GLSL450 |
| 147 |
%float = OpTypeFloat 32 |
| 148 |
%uint = OpTypeInt 32 0 |
| 149 |
%int = OpTypeInt 32 1 |
| 150 |
%scope = OpConstant %uint 3 |
| 151 |
%uint_const = OpConstant %uint 42 |
| 152 |
%int_const = OpConstant %uint 45 |
| 153 |
%float_const = OpConstant %float 3.5 |
| 154 |
|
| 155 |
%void = OpTypeVoid |
| 156 |
%fn_ty = OpTypeFunction %void |
| 157 |
%fn = OpFunction %void None %fn_ty |
| 158 |
%entry = OpLabel |
| 159 |
)"}; |
| 160 |
} |
| 161 |
|
| 162 |
// Returns a list of SPIR-V assembly strings, where each uses only types |
| 163 |
// and IDs that can fit with a shader made from parts from the result |
| 164 |
// of ShaderPartsForAMDShaderBallot. |
| 165 |
std::vector<std::string> AMDShaderBallotGroupInstructions() { |
| 166 |
return std::vector<std::string>{ |
| 167 |
"%iadd_reduce = OpGroupIAddNonUniformAMD %uint %scope Reduce %uint_const", |
| 168 |
"%iadd_iscan = OpGroupIAddNonUniformAMD %uint %scope InclusiveScan " |
| 169 |
"%uint_const", |
| 170 |
"%iadd_escan = OpGroupIAddNonUniformAMD %uint %scope ExclusiveScan " |
| 171 |
"%uint_const", |
| 172 |
|
| 173 |
"%fadd_reduce = OpGroupFAddNonUniformAMD %float %scope Reduce " |
| 174 |
"%float_const", |
| 175 |
"%fadd_iscan = OpGroupFAddNonUniformAMD %float %scope InclusiveScan " |
| 176 |
"%float_const", |
| 177 |
"%fadd_escan = OpGroupFAddNonUniformAMD %float %scope ExclusiveScan " |
| 178 |
"%float_const", |
| 179 |
|
| 180 |
"%fmin_reduce = OpGroupFMinNonUniformAMD %float %scope Reduce " |
| 181 |
"%float_const", |
| 182 |
"%fmin_iscan = OpGroupFMinNonUniformAMD %float %scope InclusiveScan " |
| 183 |
"%float_const", |
| 184 |
"%fmin_escan = OpGroupFMinNonUniformAMD %float %scope ExclusiveScan " |
| 185 |
"%float_const", |
| 186 |
|
| 187 |
"%umin_reduce = OpGroupUMinNonUniformAMD %uint %scope Reduce %uint_const", |
| 188 |
"%umin_iscan = OpGroupUMinNonUniformAMD %uint %scope InclusiveScan " |
| 189 |
"%uint_const", |
| 190 |
"%umin_escan = OpGroupUMinNonUniformAMD %uint %scope ExclusiveScan " |
| 191 |
"%uint_const", |
| 192 |
|
| 193 |
"%smin_reduce = OpGroupUMinNonUniformAMD %int %scope Reduce %int_const", |
| 194 |
"%smin_iscan = OpGroupUMinNonUniformAMD %int %scope InclusiveScan " |
| 195 |
"%int_const", |
| 196 |
"%smin_escan = OpGroupUMinNonUniformAMD %int %scope ExclusiveScan " |
| 197 |
"%int_const", |
| 198 |
|
| 199 |
"%fmax_reduce = OpGroupFMaxNonUniformAMD %float %scope Reduce " |
| 200 |
"%float_const", |
| 201 |
"%fmax_iscan = OpGroupFMaxNonUniformAMD %float %scope InclusiveScan " |
| 202 |
"%float_const", |
| 203 |
"%fmax_escan = OpGroupFMaxNonUniformAMD %float %scope ExclusiveScan " |
| 204 |
"%float_const", |
| 205 |
|
| 206 |
"%umax_reduce = OpGroupUMaxNonUniformAMD %uint %scope Reduce %uint_const", |
| 207 |
"%umax_iscan = OpGroupUMaxNonUniformAMD %uint %scope InclusiveScan " |
| 208 |
"%uint_const", |
| 209 |
"%umax_escan = OpGroupUMaxNonUniformAMD %uint %scope ExclusiveScan " |
| 210 |
"%uint_const", |
| 211 |
|
| 212 |
"%smax_reduce = OpGroupUMaxNonUniformAMD %int %scope Reduce %int_const", |
| 213 |
"%smax_iscan = OpGroupUMaxNonUniformAMD %int %scope InclusiveScan " |
| 214 |
"%int_const", |
| 215 |
"%smax_escan = OpGroupUMaxNonUniformAMD %int %scope ExclusiveScan " |
| 216 |
"%int_const"}; |
| 217 |
} |
| 218 |
|
| 219 |
TEST_P(ValidateAMDShaderBallotCapabilities, ExpectSuccess) { |
| 220 |
// Succeed because the module specifies the SPV_AMD_shader_ballot extension. |
| 221 |
auto parts = ShaderPartsForAMDShaderBallot(); |
| 222 |
|
| 223 |
const std::string assembly = |
| 224 |
parts[0] + "OpExtension \"SPV_AMD_shader_ballot\"\n" + parts[1] + |
| 225 |
GetParam() + "\nOpReturn OpFunctionEnd"; |
| 226 |
|
| 227 |
CompileSuccessfully(assembly.c_str()); |
| 228 |
EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString(); |
| 229 |
} |
| 230 |
|
| 231 |
INSTANTIATE_TEST_SUITE_P(ExpectSuccess, ValidateAMDShaderBallotCapabilities, |
| 232 |
ValuesIn(AMDShaderBallotGroupInstructions())); |
| 233 |
|
| 234 |
TEST_P(ValidateAMDShaderBallotCapabilities, ExpectFailure) { |
| 235 |
// Fail because the module does not specify the SPV_AMD_shader_ballot |
| 236 |
// extension. |
| 237 |
auto parts = ShaderPartsForAMDShaderBallot(); |
| 238 |
|
| 239 |
const std::string assembly = |
| 240 |
parts[0] + parts[1] + GetParam() + "\nOpReturn OpFunctionEnd"; |
| 241 |
|
| 242 |
CompileSuccessfully(assembly.c_str()); |
| 243 |
EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions()); |
| 244 |
|
| 245 |
// Make sure we get an appropriate error message. |
| 246 |
// Find just the opcode name, skipping over the "Op" part. |
| 247 |
auto prefix_with_opcode = GetParam().substr(GetParam().find("Group")); |
| 248 |
auto opcode = prefix_with_opcode.substr(0, prefix_with_opcode.find(' ')); |
| 249 |
EXPECT_THAT( |
| 250 |
getDiagnosticString(), |
| 251 |
HasSubstr(std::string("Opcode " + opcode + |
| 252 |
" requires one of these capabilities: Groups"))); |
| 253 |
} |
| 254 |
|
| 255 |
INSTANTIATE_TEST_SUITE_P(ExpectFailure, ValidateAMDShaderBallotCapabilities, |
| 256 |
ValuesIn(AMDShaderBallotGroupInstructions())); |
| 257 |
|
| 258 |
struct ExtIntoCoreCase { |
| 259 |
const char* ext; |
| 260 |
const char* cap; |
| 261 |
const char* builtin; |
| 262 |
spv_target_env env; |
| 263 |
bool success; |
| 264 |
}; |
| 265 |
|
| 266 |
using ValidateExtIntoCore = spvtest::ValidateBase<ExtIntoCoreCase>; |
| 267 |
|
| 268 |
// Make sure that we don't panic about missing extensions for using |
| 269 |
// functionalities that introduced in extensions but became core SPIR-V later. |
| 270 |
|
| 271 |
TEST_P(ValidateExtIntoCore, DoNotAskForExtensionInLaterVersion) { |
| 272 |
const std::string code = std::string(R"( |
| 273 |
OpCapability Shader |
| 274 |
OpCapability )") + |
| 275 |
GetParam().cap + R"( |
| 276 |
OpMemoryModel Logical GLSL450 |
| 277 |
OpEntryPoint Vertex %main "main" %builtin |
| 278 |
OpDecorate %builtin BuiltIn )" + GetParam().builtin + R"( |
| 279 |
%void = OpTypeVoid |
| 280 |
%3 = OpTypeFunction %void |
| 281 |
%int = OpTypeInt 32 1 |
| 282 |
%_ptr_Input_int = OpTypePointer Input %int |
| 283 |
%builtin = OpVariable %_ptr_Input_int Input |
| 284 |
%main = OpFunction %void None %3 |
| 285 |
%5 = OpLabel |
| 286 |
%18 = OpLoad %int %builtin |
| 287 |
OpReturn |
| 288 |
OpFunctionEnd)"; |
| 289 |
|
| 290 |
CompileSuccessfully(code.c_str(), GetParam().env); |
| 291 |
if (GetParam().success) { |
| 292 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(GetParam().env)) |
| 293 |
<< getDiagnosticString(); |
| 294 |
} else { |
| 295 |
ASSERT_NE(SPV_SUCCESS, ValidateInstructions(GetParam().env)) |
| 296 |
<< " in " << spvTargetEnvDescription(GetParam().env) << ":\n" |
| 297 |
<< code; |
| 298 |
const std::string message = getDiagnosticString(); |
| 299 |
if (spvIsVulkanEnv(GetParam().env)) { |
| 300 |
EXPECT_THAT(message, HasSubstr(std::string(GetParam().cap) + |
| 301 |
" is not allowed by Vulkan")); |
| 302 |
EXPECT_THAT(message, HasSubstr(std::string("or requires extension"))); |
| 303 |
} else { |
| 304 |
EXPECT_THAT(message, |
| 305 |
HasSubstr(std::string("requires one of these extensions: ") + |
| 306 |
GetParam().ext)); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
// clang-format off |
| 312 |
INSTANTIATE_TEST_SUITE_P( |
| 313 |
KHR_extensions, ValidateExtIntoCore, |
| 314 |
ValuesIn(std::vector<ExtIntoCoreCase>{ |
| 315 |
// SPV_KHR_shader_draw_parameters became core SPIR-V 1.3 |
| 316 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_3, true}, |
| 317 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_2, false}, |
| 318 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_1, false}, |
| 319 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_UNIVERSAL_1_0, false}, |
| 320 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_VULKAN_1_1, true}, |
| 321 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseVertex", SPV_ENV_VULKAN_1_0, false}, |
| 322 |
|
| 323 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseInstance", SPV_ENV_UNIVERSAL_1_3, true}, |
| 324 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "BaseInstance", SPV_ENV_VULKAN_1_0, false}, |
| 325 |
|
| 326 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "DrawIndex", SPV_ENV_UNIVERSAL_1_3, true}, |
| 327 |
{"SPV_KHR_shader_draw_parameters", "DrawParameters", "DrawIndex", SPV_ENV_UNIVERSAL_1_1, false}, |
| 328 |
|
| 329 |
// SPV_KHR_multiview became core SPIR-V 1.3 |
| 330 |
{"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_3, true}, |
| 331 |
{"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_2, false}, |
| 332 |
{"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_1, false}, |
| 333 |
{"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_UNIVERSAL_1_0, false}, |
| 334 |
{"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_VULKAN_1_1, true}, |
| 335 |
{"SPV_KHR_multiview", "MultiView", "ViewIndex", SPV_ENV_VULKAN_1_0, false}, |
| 336 |
|
| 337 |
// SPV_KHR_device_group became core SPIR-V 1.3 |
| 338 |
{"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_3, true}, |
| 339 |
{"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_2, false}, |
| 340 |
{"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_1, false}, |
| 341 |
{"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_UNIVERSAL_1_0, false}, |
| 342 |
{"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_VULKAN_1_1, true}, |
| 343 |
{"SPV_KHR_device_group", "DeviceGroup", "DeviceIndex", SPV_ENV_VULKAN_1_0, false}, |
| 344 |
})); |
| 345 |
// clang-format on |
| 346 |
|
| 347 |
} // namespace |
| 348 |
} // namespace val |
| 349 |
} // namespace spvtools |