1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// Copyright (c) 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "source/name_mapper.h"
#include "test/test_fixture.h"
#include "test/unit_spirv.h"
namespace spvtools {
namespace {
using spvtest::ScopedContext;
using ::testing::Eq;
TEST(TrivialNameTest, Samples) {
auto mapper = GetTrivialNameMapper();
EXPECT_EQ(mapper(1), "1");
EXPECT_EQ(mapper(1999), "1999");
EXPECT_EQ(mapper(1024), "1024");
}
// A test case for the name mappers that actually look at an assembled module.
struct NameIdCase {
std::string assembly; // Input assembly text
uint32_t id;
std::string expected_name;
};
using FriendlyNameTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<NameIdCase>>;
TEST_P(FriendlyNameTest, SingleMapping) {
ScopedContext context(SPV_ENV_UNIVERSAL_1_1);
auto words = CompileSuccessfully(GetParam().assembly, SPV_ENV_UNIVERSAL_1_1);
auto friendly_mapper =
FriendlyNameMapper(context.context, words.data(), words.size());
NameMapper mapper = friendly_mapper.GetNameMapper();
EXPECT_THAT(mapper(GetParam().id), Eq(GetParam().expected_name))
<< GetParam().assembly << std::endl
<< " for id " << GetParam().id;
}
INSTANTIATE_TEST_SUITE_P(ScalarType, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeVoid", 1, "void"},
{"%1 = OpTypeBool", 1, "bool"},
{"%1 = OpTypeInt 8 0", 1, "uchar"},
{"%1 = OpTypeInt 8 1", 1, "char"},
{"%1 = OpTypeInt 16 0", 1, "ushort"},
{"%1 = OpTypeInt 16 1", 1, "short"},
{"%1 = OpTypeInt 32 0", 1, "uint"},
{"%1 = OpTypeInt 32 1", 1, "int"},
{"%1 = OpTypeInt 64 0", 1, "ulong"},
{"%1 = OpTypeInt 64 1", 1, "long"},
{"%1 = OpTypeInt 1 0", 1, "u1"},
{"%1 = OpTypeInt 1 1", 1, "i1"},
{"%1 = OpTypeInt 33 0", 1, "u33"},
{"%1 = OpTypeInt 33 1", 1, "i33"},
{"%1 = OpTypeFloat 16", 1, "half"},
{"%1 = OpTypeFloat 32", 1, "float"},
{"%1 = OpTypeFloat 64", 1, "double"},
{"%1 = OpTypeFloat 10", 1, "fp10"},
{"%1 = OpTypeFloat 55", 1, "fp55"},
}));
INSTANTIATE_TEST_SUITE_P(
VectorType, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeBool %2 = OpTypeVector %1 1", 2, "v1bool"},
{"%1 = OpTypeBool %2 = OpTypeVector %1 2", 2, "v2bool"},
{"%1 = OpTypeBool %2 = OpTypeVector %1 3", 2, "v3bool"},
{"%1 = OpTypeBool %2 = OpTypeVector %1 4", 2, "v4bool"},
{"%1 = OpTypeInt 8 0 %2 = OpTypeVector %1 2", 2, "v2uchar"},
{"%1 = OpTypeInt 16 1 %2 = OpTypeVector %1 3", 2, "v3short"},
{"%1 = OpTypeInt 32 0 %2 = OpTypeVector %1 4", 2, "v4uint"},
{"%1 = OpTypeInt 64 1 %2 = OpTypeVector %1 3", 2, "v3long"},
{"%1 = OpTypeInt 20 0 %2 = OpTypeVector %1 4", 2, "v4u20"},
{"%1 = OpTypeInt 21 1 %2 = OpTypeVector %1 3", 2, "v3i21"},
{"%1 = OpTypeFloat 32 %2 = OpTypeVector %1 2", 2, "v2float"},
// OpName overrides the element name.
{"OpName %1 \"time\" %1 = OpTypeFloat 32 %2 = OpTypeVector %1 2", 2,
"v2time"},
}));
INSTANTIATE_TEST_SUITE_P(
MatrixType, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeBool %2 = OpTypeVector %1 2 %3 = OpTypeMatrix %2 2", 3,
"mat2v2bool"},
{"%1 = OpTypeFloat 32 %2 = OpTypeVector %1 2 %3 = OpTypeMatrix %2 3", 3,
"mat3v2float"},
{"%1 = OpTypeFloat 32 %2 = OpTypeVector %1 2 %3 = OpTypeMatrix %2 4", 3,
"mat4v2float"},
{"OpName %1 \"time\" %1 = OpTypeFloat 32 %2 = OpTypeVector %1 2 %3 = "
"OpTypeMatrix %2 4",
3, "mat4v2time"},
{"OpName %2 \"lat_long\" %1 = OpTypeFloat 32 %2 = OpTypeVector %1 2 %3 "
"= OpTypeMatrix %2 4",
3, "mat4lat_long"},
}));
INSTANTIATE_TEST_SUITE_P(
OpName, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"OpName %1 \"abcdefg\"", 1, "abcdefg"},
{"OpName %1 \"Hello world!\"", 1, "Hello_world_"},
{"OpName %1 \"0123456789\"", 1, "0123456789"},
{"OpName %1 \"_\"", 1, "_"},
// An empty string is not valid for SPIR-V assembly IDs.
{"OpName %1 \"\"", 1, "_"},
// Test uniqueness when presented with things mapping to "_"
{"OpName %1 \"\" OpName %2 \"\"", 1, "_"},
{"OpName %1 \"\" OpName %2 \"\"", 2, "__0"},
{"OpName %1 \"\" OpName %2 \"\" OpName %3 \"_\"", 3, "__1"},
// Test uniqueness of names that are forced to be
// numbers.
{"OpName %1 \"2\" OpName %2 \"2\"", 1, "2"},
{"OpName %1 \"2\" OpName %2 \"2\"", 2, "2_0"},
// Test uniqueness in the face of forward references
// for Ids that don't already have friendly names.
// In particular, the first OpDecorate assigns the name, and
// the second one can't override it.
{"OpDecorate %1 Volatile OpDecorate %1 Restrict", 1, "1"},
// But a forced name can override the name that
// would have been assigned via the OpDecorate
// forward reference.
{"OpName %1 \"mememe\" OpDecorate %1 Volatile OpDecorate %1 Restrict",
1, "mememe"},
// OpName can override other inferences. We assume valid instruction
// ordering, where OpName precedes type definitions.
{"OpName %1 \"myfloat\" %1 = OpTypeFloat 32", 1, "myfloat"},
}));
INSTANTIATE_TEST_SUITE_P(
UniquenessHeuristic, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeVoid %2 = OpTypeVoid %3 = OpTypeVoid", 1, "void"},
{"%1 = OpTypeVoid %2 = OpTypeVoid %3 = OpTypeVoid", 2, "void_0"},
{"%1 = OpTypeVoid %2 = OpTypeVoid %3 = OpTypeVoid", 3, "void_1"},
}));
INSTANTIATE_TEST_SUITE_P(Arrays, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"OpName %2 \"FortyTwo\" %1 = OpTypeFloat 32 "
"%2 = OpConstant %1 42 %3 = OpTypeArray %1 %2",
3, "_arr_float_FortyTwo"},
{"%1 = OpTypeInt 32 0 "
"%2 = OpTypeRuntimeArray %1",
2, "_runtimearr_uint"},
}));
INSTANTIATE_TEST_SUITE_P(Structs, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeBool "
"%2 = OpTypeStruct %1 %1 %1",
2, "_struct_2"},
{"%1 = OpTypeBool "
"%2 = OpTypeStruct %1 %1 %1 "
"%3 = OpTypeStruct %2 %2",
3, "_struct_3"},
}));
INSTANTIATE_TEST_SUITE_P(
Pointer, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeFloat 32 %2 = OpTypePointer Workgroup %1", 2,
"_ptr_Workgroup_float"},
{"%1 = OpTypeBool %2 = OpTypePointer Private %1", 2,
"_ptr_Private_bool"},
// OpTypeForwardPointer doesn't force generation of the name for its
// target type.
{"%1 = OpTypeBool OpTypeForwardPointer %2 Private %2 = OpTypePointer "
"Private %1",
2, "_ptr_Private_bool"},
}));
INSTANTIATE_TEST_SUITE_P(ExoticTypes, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeEvent", 1, "Event"},
{"%1 = OpTypeDeviceEvent", 1, "DeviceEvent"},
{"%1 = OpTypeReserveId", 1, "ReserveId"},
{"%1 = OpTypeQueue", 1, "Queue"},
{"%1 = OpTypeOpaque \"hello world!\"", 1,
"Opaque_hello_world_"},
{"%1 = OpTypePipe ReadOnly", 1, "PipeReadOnly"},
{"%1 = OpTypePipe WriteOnly", 1, "PipeWriteOnly"},
{"%1 = OpTypePipe ReadWrite", 1, "PipeReadWrite"},
{"%1 = OpTypePipeStorage", 1, "PipeStorage"},
{"%1 = OpTypeNamedBarrier", 1, "NamedBarrier"},
}));
// Makes a test case for a BuiltIn variable declaration.
NameIdCase BuiltInCase(std::string assembly_name, std::string expected) {
return NameIdCase{std::string("OpDecorate %1 BuiltIn ") + assembly_name +
" %1 = OpVariable %2 Input",
1, expected};
}
// Makes a test case for a BuiltIn variable declaration. In this overload,
// the expected result is the same as the assembly name.
NameIdCase BuiltInCase(std::string assembly_name) {
return BuiltInCase(assembly_name, assembly_name);
}
// Makes a test case for a BuiltIn variable declaration. In this overload,
// the expected result is the same as the assembly name, but with a "gl_"
// prefix.
NameIdCase BuiltInGLCase(std::string assembly_name) {
return BuiltInCase(assembly_name, std::string("gl_") + assembly_name);
}
INSTANTIATE_TEST_SUITE_P(
BuiltIns, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
BuiltInGLCase("Position"),
BuiltInGLCase("PointSize"),
BuiltInGLCase("ClipDistance"),
BuiltInGLCase("CullDistance"),
BuiltInCase("VertexId", "gl_VertexID"),
BuiltInCase("InstanceId", "gl_InstanceID"),
BuiltInCase("PrimitiveId", "gl_PrimitiveID"),
BuiltInCase("InvocationId", "gl_InvocationID"),
BuiltInGLCase("Layer"),
BuiltInGLCase("ViewportIndex"),
BuiltInGLCase("TessLevelOuter"),
BuiltInGLCase("TessLevelInner"),
BuiltInGLCase("TessCoord"),
BuiltInGLCase("PatchVertices"),
BuiltInGLCase("FragCoord"),
BuiltInGLCase("PointCoord"),
BuiltInGLCase("FrontFacing"),
BuiltInCase("SampleId", "gl_SampleID"),
BuiltInGLCase("SamplePosition"),
BuiltInGLCase("SampleMask"),
BuiltInGLCase("FragDepth"),
BuiltInGLCase("HelperInvocation"),
BuiltInCase("NumWorkgroups", "gl_NumWorkGroups"),
BuiltInCase("WorkgroupSize", "gl_WorkGroupSize"),
BuiltInCase("WorkgroupId", "gl_WorkGroupID"),
BuiltInCase("LocalInvocationId", "gl_LocalInvocationID"),
BuiltInCase("GlobalInvocationId", "gl_GlobalInvocationID"),
BuiltInGLCase("LocalInvocationIndex"),
BuiltInCase("WorkDim"),
BuiltInCase("GlobalSize"),
BuiltInCase("EnqueuedWorkgroupSize"),
BuiltInCase("GlobalOffset"),
BuiltInCase("GlobalLinearId"),
BuiltInCase("SubgroupSize"),
BuiltInCase("SubgroupMaxSize"),
BuiltInCase("NumSubgroups"),
BuiltInCase("NumEnqueuedSubgroups"),
BuiltInCase("SubgroupId"),
BuiltInCase("SubgroupLocalInvocationId"),
BuiltInGLCase("VertexIndex"),
BuiltInGLCase("InstanceIndex"),
BuiltInGLCase("BaseInstance"),
BuiltInCase("SubgroupEqMaskKHR"),
BuiltInCase("SubgroupGeMaskKHR"),
BuiltInCase("SubgroupGtMaskKHR"),
BuiltInCase("SubgroupLeMaskKHR"),
BuiltInCase("SubgroupLtMaskKHR"),
}));
INSTANTIATE_TEST_SUITE_P(DebugNameOverridesBuiltin, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"OpName %1 \"foo\" OpDecorate %1 BuiltIn WorkDim "
"%1 = OpVariable %2 Input",
1, "foo"}}));
INSTANTIATE_TEST_SUITE_P(
SimpleIntegralConstants, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeInt 32 0 %2 = OpConstant %1 0", 2, "uint_0"},
{"%1 = OpTypeInt 32 0 %2 = OpConstant %1 1", 2, "uint_1"},
{"%1 = OpTypeInt 32 0 %2 = OpConstant %1 2", 2, "uint_2"},
{"%1 = OpTypeInt 32 0 %2 = OpConstant %1 9", 2, "uint_9"},
{"%1 = OpTypeInt 32 0 %2 = OpConstant %1 42", 2, "uint_42"},
{"%1 = OpTypeInt 32 1 %2 = OpConstant %1 0", 2, "int_0"},
{"%1 = OpTypeInt 32 1 %2 = OpConstant %1 1", 2, "int_1"},
{"%1 = OpTypeInt 32 1 %2 = OpConstant %1 2", 2, "int_2"},
{"%1 = OpTypeInt 32 1 %2 = OpConstant %1 9", 2, "int_9"},
{"%1 = OpTypeInt 32 1 %2 = OpConstant %1 42", 2, "int_42"},
{"%1 = OpTypeInt 32 1 %2 = OpConstant %1 -42", 2, "int_n42"},
// Exotic bit widths
{"%1 = OpTypeInt 33 0 %2 = OpConstant %1 0", 2, "u33_0"},
{"%1 = OpTypeInt 33 1 %2 = OpConstant %1 10", 2, "i33_10"},
{"%1 = OpTypeInt 33 1 %2 = OpConstant %1 -19", 2, "i33_n19"},
}));
INSTANTIATE_TEST_SUITE_P(
SimpleFloatConstants, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ff4p+16", 2,
"half_0x1_ff4p_16"},
{"%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.d2cp-10", 2,
"half_n0x1_d2cpn10"},
// 32-bit floats
{"%1 = OpTypeFloat 32\n%2 = OpConstant %1 -3.125", 2, "float_n3_125"},
{"%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1.8p+128", 2,
"float_0x1_8p_128"}, // NaN
{"%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.0002p+128", 2,
"float_n0x1_0002p_128"}, // NaN
{"%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1p+128", 2,
"float_0x1p_128"}, // Inf
{"%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1p+128", 2,
"float_n0x1p_128"}, // -Inf
// 64-bit floats
{"%1 = OpTypeFloat 64\n%2 = OpConstant %1 -3.125", 2, "double_n3_125"},
{"%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.ffffffffffffap-1023", 2,
"double_0x1_ffffffffffffapn1023"}, // small normal
{"%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.ffffffffffffap-1023", 2,
"double_n0x1_ffffffffffffapn1023"},
{"%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.8p+1024", 2,
"double_0x1_8p_1024"}, // NaN
{"%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.0002p+1024", 2,
"double_n0x1_0002p_1024"}, // NaN
{"%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1p+1024", 2,
"double_0x1p_1024"}, // Inf
{"%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1p+1024", 2,
"double_n0x1p_1024"}, // -Inf
}));
INSTANTIATE_TEST_SUITE_P(
BooleanConstants, FriendlyNameTest,
::testing::ValuesIn(std::vector<NameIdCase>{
{"%1 = OpTypeBool\n%2 = OpConstantTrue %1", 2, "true"},
{"%1 = OpTypeBool\n%2 = OpConstantFalse %1", 2, "false"},
}));
} // namespace
} // namespace spvtools
|