| 1 |
// Copyright (c) 2015-2020 The Khronos Group Inc. |
| 2 |
// Modifications Copyright (C) 2020 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 |
#include "source/operand.h" |
| 18 |
|
| 19 |
#include <assert.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
|
| 24 |
#include "DebugInfo.h" |
| 25 |
#include "OpenCLDebugInfo100.h" |
| 26 |
#include "source/macro.h" |
| 27 |
#include "source/spirv_constant.h" |
| 28 |
#include "source/spirv_target_env.h" |
| 29 |
|
| 30 |
// For now, assume unified1 contains up to SPIR-V 1.3 and no later |
| 31 |
// SPIR-V version. |
| 32 |
// TODO(dneto): Make one set of tables, but with version tags on a |
| 33 |
// per-item basis. https://github.com/KhronosGroup/SPIRV-Tools/issues/1195 |
| 34 |
|
| 35 |
#include "operand.kinds-unified1.inc" |
| 36 |
#include "spirv-tools/libspirv.h" |
| 37 |
|
| 38 |
static const spv_operand_table_t kOperandTable = { |
| 39 |
ARRAY_SIZE(pygen_variable_OperandInfoTable), |
| 40 |
pygen_variable_OperandInfoTable}; |
| 41 |
|
| 42 |
spv_result_t spvOperandTableGet(spv_operand_table* pOperandTable, |
| 43 |
spv_target_env) { |
| 44 |
if (!pOperandTable) return SPV_ERROR_INVALID_POINTER; |
| 45 |
|
| 46 |
*pOperandTable = &kOperandTable; |
| 47 |
return SPV_SUCCESS; |
| 48 |
} |
| 49 |
|
| 50 |
spv_result_t spvOperandTableNameLookup(spv_target_env env, |
| 51 |
const spv_operand_table table, |
| 52 |
const spv_operand_type_t type, |
| 53 |
const char* name, |
| 54 |
const size_t nameLength, |
| 55 |
spv_operand_desc* pEntry) { |
| 56 |
if (!table) return SPV_ERROR_INVALID_TABLE; |
| 57 |
if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER; |
| 58 |
|
| 59 |
const auto version = spvVersionForTargetEnv(env); |
| 60 |
for (uint64_t typeIndex = 0; typeIndex < table->count; ++typeIndex) { |
| 61 |
const auto& group = table->types[typeIndex]; |
| 62 |
if (type != group.type) continue; |
| 63 |
for (uint64_t index = 0; index < group.count; ++index) { |
| 64 |
const auto& entry = group.entries[index]; |
| 65 |
// We consider the current operand as available as long as |
| 66 |
// 1. The target environment satisfies the minimal requirement of the |
| 67 |
// operand; or |
| 68 |
// 2. There is at least one extension enabling this operand; or |
| 69 |
// 3. There is at least one capability enabling this operand. |
| 70 |
// |
| 71 |
// Note that the second rule assumes the extension enabling this operand |
| 72 |
// is indeed requested in the SPIR-V code; checking that should be |
| 73 |
// validator's work. |
| 74 |
if (((version >= entry.minVersion && version <= entry.lastVersion) || |
| 75 |
entry.numExtensions > 0u || entry.numCapabilities > 0u) && |
| 76 |
nameLength == strlen(entry.name) && |
| 77 |
!strncmp(entry.name, name, nameLength)) { |
| 78 |
*pEntry = &entry; |
| 79 |
return SPV_SUCCESS; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return SPV_ERROR_INVALID_LOOKUP; |
| 85 |
} |
| 86 |
|
| 87 |
spv_result_t spvOperandTableValueLookup(spv_target_env env, |
| 88 |
const spv_operand_table table, |
| 89 |
const spv_operand_type_t type, |
| 90 |
const uint32_t value, |
| 91 |
spv_operand_desc* pEntry) { |
| 92 |
if (!table) return SPV_ERROR_INVALID_TABLE; |
| 93 |
if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
| 94 |
|
| 95 |
spv_operand_desc_t needle = {"", value, 0, nullptr, 0, nullptr, {}, ~0u, ~0u}; |
| 96 |
|
| 97 |
auto comp = [](const spv_operand_desc_t& lhs, const spv_operand_desc_t& rhs) { |
| 98 |
return lhs.value < rhs.value; |
| 99 |
}; |
| 100 |
|
| 101 |
for (uint64_t typeIndex = 0; typeIndex < table->count; ++typeIndex) { |
| 102 |
const auto& group = table->types[typeIndex]; |
| 103 |
if (type != group.type) continue; |
| 104 |
|
| 105 |
const auto beg = group.entries; |
| 106 |
const auto end = group.entries + group.count; |
| 107 |
|
| 108 |
// We need to loop here because there can exist multiple symbols for the |
| 109 |
// same operand value, and they can be introduced in different target |
| 110 |
// environments, which means they can have different minimal version |
| 111 |
// requirements. For example, SubgroupEqMaskKHR can exist in any SPIR-V |
| 112 |
// version as long as the SPV_KHR_shader_ballot extension is there; but |
| 113 |
// starting from SPIR-V 1.3, SubgroupEqMask, which has the same numeric |
| 114 |
// value as SubgroupEqMaskKHR, is available in core SPIR-V without extension |
| 115 |
// requirements. |
| 116 |
// Assumes the underlying table is already sorted ascendingly according to |
| 117 |
// opcode value. |
| 118 |
const auto version = spvVersionForTargetEnv(env); |
| 119 |
for (auto it = std::lower_bound(beg, end, needle, comp); |
| 120 |
it != end && it->value == value; ++it) { |
| 121 |
// We consider the current operand as available as long as |
| 122 |
// 1. The target environment satisfies the minimal requirement of the |
| 123 |
// operand; or |
| 124 |
// 2. There is at least one extension enabling this operand; or |
| 125 |
// 3. There is at least one capability enabling this operand. |
| 126 |
// |
| 127 |
// Note that the second rule assumes the extension enabling this operand |
| 128 |
// is indeed requested in the SPIR-V code; checking that should be |
| 129 |
// validator's work. |
| 130 |
if ((version >= it->minVersion && version <= it->lastVersion) || |
| 131 |
it->numExtensions > 0u || it->numCapabilities > 0u) { |
| 132 |
*pEntry = it; |
| 133 |
return SPV_SUCCESS; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return SPV_ERROR_INVALID_LOOKUP; |
| 139 |
} |
| 140 |
|
| 141 |
const char* spvOperandTypeStr(spv_operand_type_t type) { |
| 142 |
switch (type) { |
| 143 |
case SPV_OPERAND_TYPE_ID: |
| 144 |
case SPV_OPERAND_TYPE_OPTIONAL_ID: |
| 145 |
return "ID"; |
| 146 |
case SPV_OPERAND_TYPE_TYPE_ID: |
| 147 |
return "type ID"; |
| 148 |
case SPV_OPERAND_TYPE_RESULT_ID: |
| 149 |
return "result ID"; |
| 150 |
case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
| 151 |
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: |
| 152 |
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: |
| 153 |
return "literal number"; |
| 154 |
case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER: |
| 155 |
return "possibly multi-word literal integer"; |
| 156 |
case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: |
| 157 |
return "possibly multi-word literal number"; |
| 158 |
case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: |
| 159 |
return "extension instruction number"; |
| 160 |
case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: |
| 161 |
return "OpSpecConstantOp opcode"; |
| 162 |
case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 163 |
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: |
| 164 |
return "literal string"; |
| 165 |
case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: |
| 166 |
return "source language"; |
| 167 |
case SPV_OPERAND_TYPE_EXECUTION_MODEL: |
| 168 |
return "execution model"; |
| 169 |
case SPV_OPERAND_TYPE_ADDRESSING_MODEL: |
| 170 |
return "addressing model"; |
| 171 |
case SPV_OPERAND_TYPE_MEMORY_MODEL: |
| 172 |
return "memory model"; |
| 173 |
case SPV_OPERAND_TYPE_EXECUTION_MODE: |
| 174 |
return "execution mode"; |
| 175 |
case SPV_OPERAND_TYPE_STORAGE_CLASS: |
| 176 |
return "storage class"; |
| 177 |
case SPV_OPERAND_TYPE_DIMENSIONALITY: |
| 178 |
return "dimensionality"; |
| 179 |
case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE: |
| 180 |
return "sampler addressing mode"; |
| 181 |
case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE: |
| 182 |
return "sampler filter mode"; |
| 183 |
case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT: |
| 184 |
return "image format"; |
| 185 |
case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 186 |
return "floating-point fast math mode"; |
| 187 |
case SPV_OPERAND_TYPE_FP_ROUNDING_MODE: |
| 188 |
return "floating-point rounding mode"; |
| 189 |
case SPV_OPERAND_TYPE_LINKAGE_TYPE: |
| 190 |
return "linkage type"; |
| 191 |
case SPV_OPERAND_TYPE_ACCESS_QUALIFIER: |
| 192 |
case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER: |
| 193 |
return "access qualifier"; |
| 194 |
case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE: |
| 195 |
return "function parameter attribute"; |
| 196 |
case SPV_OPERAND_TYPE_DECORATION: |
| 197 |
return "decoration"; |
| 198 |
case SPV_OPERAND_TYPE_BUILT_IN: |
| 199 |
return "built-in"; |
| 200 |
case SPV_OPERAND_TYPE_SELECTION_CONTROL: |
| 201 |
return "selection control"; |
| 202 |
case SPV_OPERAND_TYPE_LOOP_CONTROL: |
| 203 |
return "loop control"; |
| 204 |
case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 205 |
return "function control"; |
| 206 |
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
| 207 |
return "memory semantics ID"; |
| 208 |
case SPV_OPERAND_TYPE_MEMORY_ACCESS: |
| 209 |
case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
| 210 |
return "memory access"; |
| 211 |
case SPV_OPERAND_TYPE_FRAGMENT_SHADING_RATE: |
| 212 |
return "shading rate"; |
| 213 |
case SPV_OPERAND_TYPE_SCOPE_ID: |
| 214 |
return "scope ID"; |
| 215 |
case SPV_OPERAND_TYPE_GROUP_OPERATION: |
| 216 |
return "group operation"; |
| 217 |
case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS: |
| 218 |
return "kernel enqeue flags"; |
| 219 |
case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: |
| 220 |
return "kernel profiling info"; |
| 221 |
case SPV_OPERAND_TYPE_CAPABILITY: |
| 222 |
return "capability"; |
| 223 |
case SPV_OPERAND_TYPE_RAY_FLAGS: |
| 224 |
return "ray flags"; |
| 225 |
case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION: |
| 226 |
return "ray query intersection"; |
| 227 |
case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE: |
| 228 |
return "ray query committed intersection type"; |
| 229 |
case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE: |
| 230 |
return "ray query candidate intersection type"; |
| 231 |
case SPV_OPERAND_TYPE_IMAGE: |
| 232 |
case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
| 233 |
return "image"; |
| 234 |
case SPV_OPERAND_TYPE_OPTIONAL_CIV: |
| 235 |
return "context-insensitive value"; |
| 236 |
case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: |
| 237 |
return "debug info flags"; |
| 238 |
case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
| 239 |
return "debug base type encoding"; |
| 240 |
case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE: |
| 241 |
return "debug composite type"; |
| 242 |
case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER: |
| 243 |
return "debug type qualifier"; |
| 244 |
case SPV_OPERAND_TYPE_DEBUG_OPERATION: |
| 245 |
return "debug operation"; |
| 246 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS: |
| 247 |
return "OpenCL.DebugInfo.100 debug info flags"; |
| 248 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
| 249 |
return "OpenCL.DebugInfo.100 debug base type encoding"; |
| 250 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE: |
| 251 |
return "OpenCL.DebugInfo.100 debug composite type"; |
| 252 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER: |
| 253 |
return "OpenCL.DebugInfo.100 debug type qualifier"; |
| 254 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION: |
| 255 |
return "OpenCL.DebugInfo.100 debug operation"; |
| 256 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY: |
| 257 |
return "OpenCL.DebugInfo.100 debug imported entity"; |
| 258 |
|
| 259 |
// The next values are for values returned from an instruction, not actually |
| 260 |
// an operand. So the specific strings don't matter. But let's add them |
| 261 |
// for completeness and ease of testing. |
| 262 |
case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER: |
| 263 |
return "image channel order"; |
| 264 |
case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE: |
| 265 |
return "image channel data type"; |
| 266 |
|
| 267 |
case SPV_OPERAND_TYPE_NONE: |
| 268 |
return "NONE"; |
| 269 |
default: |
| 270 |
break; |
| 271 |
} |
| 272 |
return "unknown"; |
| 273 |
} |
| 274 |
|
| 275 |
void spvPushOperandTypes(const spv_operand_type_t* types, |
| 276 |
spv_operand_pattern_t* pattern) { |
| 277 |
const spv_operand_type_t* endTypes; |
| 278 |
for (endTypes = types; *endTypes != SPV_OPERAND_TYPE_NONE; ++endTypes) { |
| 279 |
} |
| 280 |
|
| 281 |
while (endTypes-- != types) { |
| 282 |
pattern->push_back(*endTypes); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void spvPushOperandTypesForMask(spv_target_env env, |
| 287 |
const spv_operand_table operandTable, |
| 288 |
const spv_operand_type_t type, |
| 289 |
const uint32_t mask, |
| 290 |
spv_operand_pattern_t* pattern) { |
| 291 |
// Scan from highest bits to lowest bits because we will append in LIFO |
| 292 |
// fashion, and we need the operands for lower order bits to be consumed first |
| 293 |
for (uint32_t candidate_bit = (1u << 31u); candidate_bit; |
| 294 |
candidate_bit >>= 1) { |
| 295 |
if (candidate_bit & mask) { |
| 296 |
spv_operand_desc entry = nullptr; |
| 297 |
if (SPV_SUCCESS == spvOperandTableValueLookup(env, operandTable, type, |
| 298 |
candidate_bit, &entry)) { |
| 299 |
spvPushOperandTypes(entry->operandTypes, pattern); |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
bool spvOperandIsConcrete(spv_operand_type_t type) { |
| 306 |
if (spvIsIdType(type) || spvOperandIsConcreteMask(type)) { |
| 307 |
return true; |
| 308 |
} |
| 309 |
switch (type) { |
| 310 |
case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
| 311 |
case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: |
| 312 |
case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: |
| 313 |
case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: |
| 314 |
case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 315 |
case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: |
| 316 |
case SPV_OPERAND_TYPE_EXECUTION_MODEL: |
| 317 |
case SPV_OPERAND_TYPE_ADDRESSING_MODEL: |
| 318 |
case SPV_OPERAND_TYPE_MEMORY_MODEL: |
| 319 |
case SPV_OPERAND_TYPE_EXECUTION_MODE: |
| 320 |
case SPV_OPERAND_TYPE_STORAGE_CLASS: |
| 321 |
case SPV_OPERAND_TYPE_DIMENSIONALITY: |
| 322 |
case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE: |
| 323 |
case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE: |
| 324 |
case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT: |
| 325 |
case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER: |
| 326 |
case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE: |
| 327 |
case SPV_OPERAND_TYPE_FP_ROUNDING_MODE: |
| 328 |
case SPV_OPERAND_TYPE_LINKAGE_TYPE: |
| 329 |
case SPV_OPERAND_TYPE_ACCESS_QUALIFIER: |
| 330 |
case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE: |
| 331 |
case SPV_OPERAND_TYPE_DECORATION: |
| 332 |
case SPV_OPERAND_TYPE_BUILT_IN: |
| 333 |
case SPV_OPERAND_TYPE_GROUP_OPERATION: |
| 334 |
case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS: |
| 335 |
case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: |
| 336 |
case SPV_OPERAND_TYPE_CAPABILITY: |
| 337 |
case SPV_OPERAND_TYPE_RAY_FLAGS: |
| 338 |
case SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION: |
| 339 |
case SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE: |
| 340 |
case SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE: |
| 341 |
case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
| 342 |
case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE: |
| 343 |
case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER: |
| 344 |
case SPV_OPERAND_TYPE_DEBUG_OPERATION: |
| 345 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
| 346 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE: |
| 347 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER: |
| 348 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION: |
| 349 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY: |
| 350 |
return true; |
| 351 |
default: |
| 352 |
break; |
| 353 |
} |
| 354 |
return false; |
| 355 |
} |
| 356 |
|
| 357 |
bool spvOperandIsConcreteMask(spv_operand_type_t type) { |
| 358 |
switch (type) { |
| 359 |
case SPV_OPERAND_TYPE_IMAGE: |
| 360 |
case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 361 |
case SPV_OPERAND_TYPE_SELECTION_CONTROL: |
| 362 |
case SPV_OPERAND_TYPE_LOOP_CONTROL: |
| 363 |
case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 364 |
case SPV_OPERAND_TYPE_MEMORY_ACCESS: |
| 365 |
case SPV_OPERAND_TYPE_FRAGMENT_SHADING_RATE: |
| 366 |
case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: |
| 367 |
case SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS: |
| 368 |
return true; |
| 369 |
default: |
| 370 |
break; |
| 371 |
} |
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
bool spvOperandIsOptional(spv_operand_type_t type) { |
| 376 |
switch (type) { |
| 377 |
case SPV_OPERAND_TYPE_OPTIONAL_ID: |
| 378 |
case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
| 379 |
case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
| 380 |
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: |
| 381 |
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: |
| 382 |
case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER: |
| 383 |
case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: |
| 384 |
case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER: |
| 385 |
case SPV_OPERAND_TYPE_OPTIONAL_CIV: |
| 386 |
return true; |
| 387 |
default: |
| 388 |
break; |
| 389 |
} |
| 390 |
// Any variable operand is also optional. |
| 391 |
return spvOperandIsVariable(type); |
| 392 |
} |
| 393 |
|
| 394 |
bool spvOperandIsVariable(spv_operand_type_t type) { |
| 395 |
switch (type) { |
| 396 |
case SPV_OPERAND_TYPE_VARIABLE_ID: |
| 397 |
case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER: |
| 398 |
case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID: |
| 399 |
case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER: |
| 400 |
return true; |
| 401 |
default: |
| 402 |
break; |
| 403 |
} |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
bool spvExpandOperandSequenceOnce(spv_operand_type_t type, |
| 408 |
spv_operand_pattern_t* pattern) { |
| 409 |
switch (type) { |
| 410 |
case SPV_OPERAND_TYPE_VARIABLE_ID: |
| 411 |
pattern->push_back(type); |
| 412 |
pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID); |
| 413 |
return true; |
| 414 |
case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER: |
| 415 |
pattern->push_back(type); |
| 416 |
pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER); |
| 417 |
return true; |
| 418 |
case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID: |
| 419 |
// Represents Zero or more (Literal number, Id) pairs, |
| 420 |
// where the literal number must be a scalar integer. |
| 421 |
pattern->push_back(type); |
| 422 |
pattern->push_back(SPV_OPERAND_TYPE_ID); |
| 423 |
pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER); |
| 424 |
return true; |
| 425 |
case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER: |
| 426 |
// Represents Zero or more (Id, Literal number) pairs. |
| 427 |
pattern->push_back(type); |
| 428 |
pattern->push_back(SPV_OPERAND_TYPE_LITERAL_INTEGER); |
| 429 |
pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID); |
| 430 |
return true; |
| 431 |
default: |
| 432 |
break; |
| 433 |
} |
| 434 |
return false; |
| 435 |
} |
| 436 |
|
| 437 |
spv_operand_type_t spvTakeFirstMatchableOperand( |
| 438 |
spv_operand_pattern_t* pattern) { |
| 439 |
assert(!pattern->empty()); |
| 440 |
spv_operand_type_t result; |
| 441 |
do { |
| 442 |
result = pattern->back(); |
| 443 |
pattern->pop_back(); |
| 444 |
} while (spvExpandOperandSequenceOnce(result, pattern)); |
| 445 |
return result; |
| 446 |
} |
| 447 |
|
| 448 |
spv_operand_pattern_t spvAlternatePatternFollowingImmediate( |
| 449 |
const spv_operand_pattern_t& pattern) { |
| 450 |
auto it = |
| 451 |
std::find(pattern.crbegin(), pattern.crend(), SPV_OPERAND_TYPE_RESULT_ID); |
| 452 |
if (it != pattern.crend()) { |
| 453 |
spv_operand_pattern_t alternatePattern(it - pattern.crbegin() + 2, |
| 454 |
SPV_OPERAND_TYPE_OPTIONAL_CIV); |
| 455 |
alternatePattern[1] = SPV_OPERAND_TYPE_RESULT_ID; |
| 456 |
return alternatePattern; |
| 457 |
} |
| 458 |
|
| 459 |
// No result-id found, so just expect CIVs. |
| 460 |
return {SPV_OPERAND_TYPE_OPTIONAL_CIV}; |
| 461 |
} |
| 462 |
|
| 463 |
bool spvIsIdType(spv_operand_type_t type) { |
| 464 |
switch (type) { |
| 465 |
case SPV_OPERAND_TYPE_ID: |
| 466 |
case SPV_OPERAND_TYPE_TYPE_ID: |
| 467 |
case SPV_OPERAND_TYPE_RESULT_ID: |
| 468 |
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
| 469 |
case SPV_OPERAND_TYPE_SCOPE_ID: |
| 470 |
return true; |
| 471 |
default: |
| 472 |
return false; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
bool spvIsInIdType(spv_operand_type_t type) { |
| 477 |
if (!spvIsIdType(type)) { |
| 478 |
// If it is not an ID it cannot be an input ID. |
| 479 |
return false; |
| 480 |
} |
| 481 |
switch (type) { |
| 482 |
// Deny non-input IDs. |
| 483 |
case SPV_OPERAND_TYPE_TYPE_ID: |
| 484 |
case SPV_OPERAND_TYPE_RESULT_ID: |
| 485 |
return false; |
| 486 |
default: |
| 487 |
return true; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
std::function<bool(unsigned)> spvOperandCanBeForwardDeclaredFunction( |
| 492 |
SpvOp opcode) { |
| 493 |
std::function<bool(unsigned index)> out; |
| 494 |
switch (opcode) { |
| 495 |
case SpvOpExecutionMode: |
| 496 |
case SpvOpExecutionModeId: |
| 497 |
case SpvOpEntryPoint: |
| 498 |
case SpvOpName: |
| 499 |
case SpvOpMemberName: |
| 500 |
case SpvOpSelectionMerge: |
| 501 |
case SpvOpDecorate: |
| 502 |
case SpvOpMemberDecorate: |
| 503 |
case SpvOpDecorateId: |
| 504 |
case SpvOpDecorateStringGOOGLE: |
| 505 |
case SpvOpMemberDecorateStringGOOGLE: |
| 506 |
case SpvOpTypeStruct: |
| 507 |
case SpvOpBranch: |
| 508 |
case SpvOpLoopMerge: |
| 509 |
out = [](unsigned) { return true; }; |
| 510 |
break; |
| 511 |
case SpvOpGroupDecorate: |
| 512 |
case SpvOpGroupMemberDecorate: |
| 513 |
case SpvOpBranchConditional: |
| 514 |
case SpvOpSwitch: |
| 515 |
out = [](unsigned index) { return index != 0; }; |
| 516 |
break; |
| 517 |
|
| 518 |
case SpvOpFunctionCall: |
| 519 |
// The Function parameter. |
| 520 |
out = [](unsigned index) { return index == 2; }; |
| 521 |
break; |
| 522 |
|
| 523 |
case SpvOpPhi: |
| 524 |
out = [](unsigned index) { return index > 1; }; |
| 525 |
break; |
| 526 |
|
| 527 |
case SpvOpEnqueueKernel: |
| 528 |
// The Invoke parameter. |
| 529 |
out = [](unsigned index) { return index == 8; }; |
| 530 |
break; |
| 531 |
|
| 532 |
case SpvOpGetKernelNDrangeSubGroupCount: |
| 533 |
case SpvOpGetKernelNDrangeMaxSubGroupSize: |
| 534 |
// The Invoke parameter. |
| 535 |
out = [](unsigned index) { return index == 3; }; |
| 536 |
break; |
| 537 |
|
| 538 |
case SpvOpGetKernelWorkGroupSize: |
| 539 |
case SpvOpGetKernelPreferredWorkGroupSizeMultiple: |
| 540 |
// The Invoke parameter. |
| 541 |
out = [](unsigned index) { return index == 2; }; |
| 542 |
break; |
| 543 |
case SpvOpTypeForwardPointer: |
| 544 |
out = [](unsigned index) { return index == 0; }; |
| 545 |
break; |
| 546 |
case SpvOpTypeArray: |
| 547 |
out = [](unsigned index) { return index == 1; }; |
| 548 |
break; |
| 549 |
default: |
| 550 |
out = [](unsigned) { return false; }; |
| 551 |
break; |
| 552 |
} |
| 553 |
return out; |
| 554 |
} |
| 555 |
|
| 556 |
std::function<bool(unsigned)> spvDbgInfoExtOperandCanBeForwardDeclaredFunction( |
| 557 |
spv_ext_inst_type_t ext_type, uint32_t key) { |
| 558 |
// TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/532): Forward |
| 559 |
// references for debug info instructions are still in discussion. We must |
| 560 |
// update the following lines of code when we conclude the spec. |
| 561 |
std::function<bool(unsigned index)> out; |
| 562 |
if (ext_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) { |
| 563 |
switch (OpenCLDebugInfo100Instructions(key)) { |
| 564 |
case OpenCLDebugInfo100DebugFunction: |
| 565 |
out = [](unsigned index) { return index == 13; }; |
| 566 |
break; |
| 567 |
case OpenCLDebugInfo100DebugTypeComposite: |
| 568 |
out = [](unsigned index) { return index >= 13; }; |
| 569 |
break; |
| 570 |
default: |
| 571 |
out = [](unsigned) { return false; }; |
| 572 |
break; |
| 573 |
} |
| 574 |
} else { |
| 575 |
switch (DebugInfoInstructions(key)) { |
| 576 |
case DebugInfoDebugFunction: |
| 577 |
out = [](unsigned index) { return index == 13; }; |
| 578 |
break; |
| 579 |
case DebugInfoDebugTypeComposite: |
| 580 |
out = [](unsigned index) { return index >= 12; }; |
| 581 |
break; |
| 582 |
default: |
| 583 |
out = [](unsigned) { return false; }; |
| 584 |
break; |
| 585 |
} |
| 586 |
} |
| 587 |
return out; |
| 588 |
} |