| 1 |
// Copyright (c) 2017 Google Inc. |
| 2 |
// Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights |
| 3 |
// reserved. |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the License for the specific language governing permissions and |
| 15 |
// limitations under the License. |
| 16 |
|
| 17 |
// Tests for unique type declaration rules validator. |
| 18 |
|
| 19 |
#include <string> |
| 20 |
|
| 21 |
#include "gmock/gmock.h" |
| 22 |
#include "test/unit_spirv.h" |
| 23 |
#include "test/val/val_fixtures.h" |
| 24 |
|
| 25 |
namespace spvtools { |
| 26 |
namespace val { |
| 27 |
namespace { |
| 28 |
|
| 29 |
using ::testing::HasSubstr; |
| 30 |
using ::testing::Not; |
| 31 |
|
| 32 |
using ValidateTypeUnique = spvtest::ValidateBase<bool>; |
| 33 |
|
| 34 |
const spv_result_t kDuplicateTypeError = SPV_ERROR_INVALID_DATA; |
| 35 |
|
| 36 |
const std::string& GetHeader() { |
| 37 |
static const std::string header = R"( |
| 38 |
OpCapability Shader |
| 39 |
OpCapability Linkage |
| 40 |
OpMemoryModel Logical GLSL450 |
| 41 |
%floatt = OpTypeFloat 32 |
| 42 |
%vec2t = OpTypeVector %floatt 2 |
| 43 |
%vec3t = OpTypeVector %floatt 3 |
| 44 |
%vec4t = OpTypeVector %floatt 4 |
| 45 |
%mat22t = OpTypeMatrix %vec2t 2 |
| 46 |
%mat33t = OpTypeMatrix %vec3t 3 |
| 47 |
%mat44t = OpTypeMatrix %vec4t 4 |
| 48 |
%intt = OpTypeInt 32 1 |
| 49 |
%uintt = OpTypeInt 32 0 |
| 50 |
%num3 = OpConstant %uintt 3 |
| 51 |
%const3 = OpConstant %uintt 3 |
| 52 |
%val3 = OpConstant %uintt 3 |
| 53 |
%array = OpTypeArray %vec3t %num3 |
| 54 |
%struct = OpTypeStruct %floatt %floatt %vec3t |
| 55 |
%boolt = OpTypeBool |
| 56 |
%array2 = OpTypeArray %vec3t %num3 |
| 57 |
%voidt = OpTypeVoid |
| 58 |
%vfunct = OpTypeFunction %voidt |
| 59 |
%struct2 = OpTypeStruct %floatt %floatt %vec3t |
| 60 |
%false = OpConstantFalse %boolt |
| 61 |
%true = OpConstantTrue %boolt |
| 62 |
%runtime_arrayt = OpTypeRuntimeArray %floatt |
| 63 |
%runtime_arrayt2 = OpTypeRuntimeArray %floatt |
| 64 |
)"; |
| 65 |
|
| 66 |
return header; |
| 67 |
} |
| 68 |
|
| 69 |
const std::string& GetBody() { |
| 70 |
static const std::string body = R"( |
| 71 |
%main = OpFunction %voidt None %vfunct |
| 72 |
%mainl = OpLabel |
| 73 |
%a = OpIAdd %uintt %const3 %val3 |
| 74 |
%b = OpIAdd %uintt %const3 %val3 |
| 75 |
OpSelectionMerge %endl None |
| 76 |
OpBranchConditional %true %truel %falsel |
| 77 |
%truel = OpLabel |
| 78 |
%add1 = OpIAdd %uintt %a %b |
| 79 |
%add2 = OpIAdd %uintt %a %b |
| 80 |
OpBranch %endl |
| 81 |
%falsel = OpLabel |
| 82 |
%sub1 = OpISub %uintt %a %b |
| 83 |
%sub2 = OpISub %uintt %a %b |
| 84 |
OpBranch %endl |
| 85 |
%endl = OpLabel |
| 86 |
OpReturn |
| 87 |
OpFunctionEnd |
| 88 |
)"; |
| 89 |
|
| 90 |
return body; |
| 91 |
} |
| 92 |
|
| 93 |
// Returns expected error string if |opcode| produces a duplicate type |
| 94 |
// declaration. |
| 95 |
std::string GetErrorString(spv::Op opcode) { |
| 96 |
return "Duplicate non-aggregate type declarations are not allowed. Opcode: " + |
| 97 |
std::string(spvOpcodeString(opcode)); |
| 98 |
} |
| 99 |
|
| 100 |
TEST_F(ValidateTypeUnique, success) { |
| 101 |
std::string str = GetHeader() + GetBody(); |
| 102 |
CompileSuccessfully(str.c_str()); |
| 103 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 104 |
} |
| 105 |
|
| 106 |
TEST_F(ValidateTypeUnique, duplicate_void) { |
| 107 |
std::string str = GetHeader() + R"( |
| 108 |
%boolt2 = OpTypeVoid |
| 109 |
)" + GetBody(); |
| 110 |
CompileSuccessfully(str.c_str()); |
| 111 |
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions()); |
| 112 |
EXPECT_THAT(getDiagnosticString(), |
| 113 |
HasSubstr(GetErrorString(spv::Op::OpTypeVoid))); |
| 114 |
} |
| 115 |
|
| 116 |
TEST_F(ValidateTypeUnique, duplicate_bool) { |
| 117 |
std::string str = GetHeader() + R"( |
| 118 |
%boolt2 = OpTypeBool |
| 119 |
)" + GetBody(); |
| 120 |
CompileSuccessfully(str.c_str()); |
| 121 |
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions()); |
| 122 |
EXPECT_THAT(getDiagnosticString(), |
| 123 |
HasSubstr(GetErrorString(spv::Op::OpTypeBool))); |
| 124 |
} |
| 125 |
|
| 126 |
TEST_F(ValidateTypeUnique, duplicate_int) { |
| 127 |
std::string str = GetHeader() + R"( |
| 128 |
%uintt2 = OpTypeInt 32 0 |
| 129 |
)" + GetBody(); |
| 130 |
CompileSuccessfully(str.c_str()); |
| 131 |
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions()); |
| 132 |
EXPECT_THAT(getDiagnosticString(), |
| 133 |
HasSubstr(GetErrorString(spv::Op::OpTypeInt))); |
| 134 |
} |
| 135 |
|
| 136 |
TEST_F(ValidateTypeUnique, duplicate_float) { |
| 137 |
std::string str = GetHeader() + R"( |
| 138 |
%floatt2 = OpTypeFloat 32 |
| 139 |
)" + GetBody(); |
| 140 |
CompileSuccessfully(str.c_str()); |
| 141 |
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions()); |
| 142 |
EXPECT_THAT(getDiagnosticString(), |
| 143 |
HasSubstr(GetErrorString(spv::Op::OpTypeFloat))); |
| 144 |
} |
| 145 |
|
| 146 |
TEST_F(ValidateTypeUnique, duplicate_vec3) { |
| 147 |
std::string str = GetHeader() + R"( |
| 148 |
%vec3t2 = OpTypeVector %floatt 3 |
| 149 |
)" + GetBody(); |
| 150 |
CompileSuccessfully(str.c_str()); |
| 151 |
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions()); |
| 152 |
EXPECT_THAT(getDiagnosticString(), |
| 153 |
HasSubstr(GetErrorString(spv::Op::OpTypeVector))); |
| 154 |
} |
| 155 |
|
| 156 |
TEST_F(ValidateTypeUnique, duplicate_mat33) { |
| 157 |
std::string str = GetHeader() + R"( |
| 158 |
%mat33t2 = OpTypeMatrix %vec3t 3 |
| 159 |
)" + GetBody(); |
| 160 |
CompileSuccessfully(str.c_str()); |
| 161 |
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions()); |
| 162 |
EXPECT_THAT(getDiagnosticString(), |
| 163 |
HasSubstr(GetErrorString(spv::Op::OpTypeMatrix))); |
| 164 |
} |
| 165 |
|
| 166 |
TEST_F(ValidateTypeUnique, duplicate_vfunc) { |
| 167 |
std::string str = GetHeader() + R"( |
| 168 |
%vfunct2 = OpTypeFunction %voidt |
| 169 |
)" + GetBody(); |
| 170 |
CompileSuccessfully(str.c_str()); |
| 171 |
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions()); |
| 172 |
EXPECT_THAT(getDiagnosticString(), |
| 173 |
HasSubstr(GetErrorString(spv::Op::OpTypeFunction))); |
| 174 |
} |
| 175 |
|
| 176 |
TEST_F(ValidateTypeUnique, duplicate_pipe_storage) { |
| 177 |
std::string str = R"( |
| 178 |
OpCapability Addresses |
| 179 |
OpCapability Kernel |
| 180 |
OpCapability Linkage |
| 181 |
OpCapability Pipes |
| 182 |
OpCapability PipeStorage |
| 183 |
OpMemoryModel Physical32 OpenCL |
| 184 |
%ps = OpTypePipeStorage |
| 185 |
%ps2 = OpTypePipeStorage |
| 186 |
)"; |
| 187 |
CompileSuccessfully(str.c_str(), SPV_ENV_UNIVERSAL_1_1); |
| 188 |
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions(SPV_ENV_UNIVERSAL_1_1)); |
| 189 |
EXPECT_THAT(getDiagnosticString(), |
| 190 |
HasSubstr(GetErrorString(spv::Op::OpTypePipeStorage))); |
| 191 |
} |
| 192 |
|
| 193 |
TEST_F(ValidateTypeUnique, duplicate_named_barrier) { |
| 194 |
std::string str = R"( |
| 195 |
OpCapability Addresses |
| 196 |
OpCapability Kernel |
| 197 |
OpCapability Linkage |
| 198 |
OpCapability NamedBarrier |
| 199 |
OpMemoryModel Physical32 OpenCL |
| 200 |
%nb = OpTypeNamedBarrier |
| 201 |
%nb2 = OpTypeNamedBarrier |
| 202 |
)"; |
| 203 |
CompileSuccessfully(str.c_str(), SPV_ENV_UNIVERSAL_1_1); |
| 204 |
ASSERT_EQ(kDuplicateTypeError, ValidateInstructions(SPV_ENV_UNIVERSAL_1_1)); |
| 205 |
EXPECT_THAT(getDiagnosticString(), |
| 206 |
HasSubstr(GetErrorString(spv::Op::OpTypeNamedBarrier))); |
| 207 |
} |
| 208 |
|
| 209 |
TEST_F(ValidateTypeUnique, duplicate_forward_pointer) { |
| 210 |
std::string str = R"( |
| 211 |
OpCapability Addresses |
| 212 |
OpCapability Kernel |
| 213 |
OpCapability GenericPointer |
| 214 |
OpCapability Linkage |
| 215 |
OpMemoryModel Physical32 OpenCL |
| 216 |
OpTypeForwardPointer %ptr Generic |
| 217 |
OpTypeForwardPointer %ptr2 Generic |
| 218 |
%intt = OpTypeInt 32 0 |
| 219 |
%int_struct = OpTypeStruct %intt |
| 220 |
%floatt = OpTypeFloat 32 |
| 221 |
%ptr = OpTypePointer Generic %int_struct |
| 222 |
%float_struct = OpTypeStruct %floatt |
| 223 |
%ptr2 = OpTypePointer Generic %float_struct |
| 224 |
)"; |
| 225 |
CompileSuccessfully(str.c_str()); |
| 226 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 227 |
} |
| 228 |
|
| 229 |
TEST_F(ValidateTypeUnique, duplicate_void_with_extension) { |
| 230 |
std::string str = R"( |
| 231 |
OpCapability Addresses |
| 232 |
OpCapability Kernel |
| 233 |
OpCapability Linkage |
| 234 |
OpCapability Pipes |
| 235 |
OpExtension "SPV_VALIDATOR_ignore_type_decl_unique" |
| 236 |
OpMemoryModel Physical32 OpenCL |
| 237 |
%voidt = OpTypeVoid |
| 238 |
%voidt2 = OpTypeVoid |
| 239 |
)"; |
| 240 |
CompileSuccessfully(str.c_str()); |
| 241 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 242 |
EXPECT_THAT(getDiagnosticString(), |
| 243 |
Not(HasSubstr(GetErrorString(spv::Op::OpTypeVoid)))); |
| 244 |
} |
| 245 |
|
| 246 |
TEST_F(ValidateTypeUnique, DuplicatePointerTypesNoExtension) { |
| 247 |
std::string str = R"( |
| 248 |
OpCapability Shader |
| 249 |
OpCapability Linkage |
| 250 |
OpMemoryModel Logical GLSL450 |
| 251 |
%u32 = OpTypeInt 32 0 |
| 252 |
%ptr1 = OpTypePointer Input %u32 |
| 253 |
%ptr2 = OpTypePointer Input %u32 |
| 254 |
)"; |
| 255 |
CompileSuccessfully(str.c_str()); |
| 256 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 257 |
} |
| 258 |
|
| 259 |
TEST_F(ValidateTypeUnique, DuplicatePointerTypesWithExtension) { |
| 260 |
std::string str = R"( |
| 261 |
OpCapability Shader |
| 262 |
OpCapability Linkage |
| 263 |
OpExtension "SPV_KHR_variable_pointers" |
| 264 |
OpMemoryModel Logical GLSL450 |
| 265 |
%u32 = OpTypeInt 32 0 |
| 266 |
%ptr1 = OpTypePointer Input %u32 |
| 267 |
%ptr2 = OpTypePointer Input %u32 |
| 268 |
)"; |
| 269 |
CompileSuccessfully(str.c_str()); |
| 270 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()); |
| 271 |
EXPECT_THAT(getDiagnosticString(), |
| 272 |
Not(HasSubstr(GetErrorString(spv::Op::OpTypePointer)))); |
| 273 |
} |
| 274 |
|
| 275 |
TEST_F(ValidateTypeUnique, DuplicateUntypedPointer) { |
| 276 |
std::string str = R"( |
| 277 |
OpCapability Shader |
| 278 |
OpCapability Linkage |
| 279 |
OpCapability UntypedPointersKHR |
| 280 |
OpCapability WorkgroupMemoryExplicitLayoutKHR |
| 281 |
OpExtension "SPV_KHR_workgroup_memory_explicit_layout" |
| 282 |
OpExtension "SPV_KHR_untyped_pointers" |
| 283 |
OpMemoryModel Logical GLSL450 |
| 284 |
%u32 = OpTypeInt 32 0 |
| 285 |
%ptr1 = OpTypeUntypedPointerKHR Workgroup |
| 286 |
%ptr2 = OpTypeUntypedPointerKHR Workgroup |
| 287 |
)"; |
| 288 |
|
| 289 |
CompileSuccessfully(str.c_str(), SPV_ENV_UNIVERSAL_1_4); |
| 290 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4)); |
| 291 |
} |
| 292 |
|
| 293 |
TEST_F(ValidateTypeUnique, DuplicateNodePayloadArrayType) { |
| 294 |
std::string str = R"( |
| 295 |
OpCapability Shader |
| 296 |
OpCapability ShaderEnqueueAMDX |
| 297 |
OpCapability Linkage |
| 298 |
OpExtension "SPV_AMDX_shader_enqueue" |
| 299 |
OpMemoryModel Logical GLSL450 |
| 300 |
%floatt = OpTypeFloat 32 |
| 301 |
%struct = OpTypeStruct %floatt |
| 302 |
%npat1 = OpTypeNodePayloadArrayAMDX %struct |
| 303 |
%npat2 = OpTypeNodePayloadArrayAMDX %struct |
| 304 |
%void = OpTypeVoid |
| 305 |
%void_fn = OpTypeFunction %void |
| 306 |
%main = OpFunction %void None %void_fn |
| 307 |
%entry = OpLabel |
| 308 |
OpReturn |
| 309 |
OpFunctionEnd |
| 310 |
)"; |
| 311 |
CompileSuccessfully(str.c_str(), SPV_ENV_UNIVERSAL_1_4); |
| 312 |
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4)); |
| 313 |
} |
| 314 |
|
| 315 |
} // namespace |
| 316 |
} // namespace val |
| 317 |
} // namespace spvtools |