| 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/opcode.h" |
| 18 |
|
| 19 |
#include <assert.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <cstdlib> |
| 24 |
|
| 25 |
#include "source/instruction.h" |
| 26 |
#include "source/macro.h" |
| 27 |
#include "source/spirv_constant.h" |
| 28 |
#include "source/spirv_endian.h" |
| 29 |
#include "source/spirv_target_env.h" |
| 30 |
#include "spirv-tools/libspirv.h" |
| 31 |
|
| 32 |
namespace { |
| 33 |
struct OpcodeDescPtrLen { |
| 34 |
const spv_opcode_desc_t* ptr; |
| 35 |
uint32_t len; |
| 36 |
}; |
| 37 |
|
| 38 |
#include "core.insts-unified1.inc" |
| 39 |
|
| 40 |
static const spv_opcode_table_t kOpcodeTable = {ARRAY_SIZE(kOpcodeTableEntries), |
| 41 |
kOpcodeTableEntries}; |
| 42 |
|
| 43 |
// Represents a vendor tool entry in the SPIR-V XML Regsitry. |
| 44 |
struct VendorTool { |
| 45 |
uint32_t value; |
| 46 |
const char* vendor; |
| 47 |
const char* tool; // Might be empty string. |
| 48 |
const char* vendor_tool; // Combiantion of vendor and tool. |
| 49 |
}; |
| 50 |
|
| 51 |
const VendorTool vendor_tools[] = { |
| 52 |
#include "generators.inc" |
| 53 |
}; |
| 54 |
|
| 55 |
} // anonymous namespace |
| 56 |
|
| 57 |
// TODO(dneto): Move this to another file. It doesn't belong with opcode |
| 58 |
// processing. |
| 59 |
const char* spvGeneratorStr(uint32_t generator) { |
| 60 |
auto where = std::find_if( |
| 61 |
std::begin(vendor_tools), std::end(vendor_tools), |
| 62 |
[generator](const VendorTool& vt) { return generator == vt.value; }); |
| 63 |
if (where != std::end(vendor_tools)) return where->vendor_tool; |
| 64 |
return "Unknown"; |
| 65 |
} |
| 66 |
|
| 67 |
uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) { |
| 68 |
return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16); |
| 69 |
} |
| 70 |
|
| 71 |
void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, |
| 72 |
uint16_t* pOpcode) { |
| 73 |
if (pWordCount) { |
| 74 |
*pWordCount = (uint16_t)((0xffff0000 & word) >> 16); |
| 75 |
} |
| 76 |
if (pOpcode) { |
| 77 |
*pOpcode = 0x0000ffff & word; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable, spv_target_env) { |
| 82 |
if (!pInstTable) return SPV_ERROR_INVALID_POINTER; |
| 83 |
|
| 84 |
// Descriptions of each opcode. Each entry describes the format of the |
| 85 |
// instruction that follows a particular opcode. |
| 86 |
|
| 87 |
*pInstTable = &kOpcodeTable; |
| 88 |
return SPV_SUCCESS; |
| 89 |
} |
| 90 |
|
| 91 |
spv_result_t spvOpcodeTableNameLookup(spv_target_env env, |
| 92 |
const spv_opcode_table table, |
| 93 |
const char* name, |
| 94 |
spv_opcode_desc* pEntry) { |
| 95 |
if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER; |
| 96 |
if (!table) return SPV_ERROR_INVALID_TABLE; |
| 97 |
|
| 98 |
// TODO: This lookup of the Opcode table is suboptimal! Binary sort would be |
| 99 |
// preferable but the table requires sorting on the Opcode name, but it's |
| 100 |
// static const initialized and matches the order of the spec. |
| 101 |
const size_t nameLength = strlen(name); |
| 102 |
const auto version = spvVersionForTargetEnv(env); |
| 103 |
for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 104 |
const spv_opcode_desc_t& entry = table->entries[opcodeIndex]; |
| 105 |
// We considers the current opcode as available as long as |
| 106 |
// 1. The target environment satisfies the minimal requirement of the |
| 107 |
// opcode; or |
| 108 |
// 2. There is at least one extension enabling this opcode. |
| 109 |
// |
| 110 |
// Note that the second rule assumes the extension enabling this instruction |
| 111 |
// is indeed requested in the SPIR-V code; checking that should be |
| 112 |
// validator's work. |
| 113 |
if (((version >= entry.minVersion && version <= entry.lastVersion) || |
| 114 |
entry.numExtensions > 0u || entry.numCapabilities > 0u) && |
| 115 |
nameLength == strlen(entry.name) && |
| 116 |
!strncmp(name, entry.name, nameLength)) { |
| 117 |
// NOTE: Found out Opcode! |
| 118 |
*pEntry = &entry; |
| 119 |
return SPV_SUCCESS; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return SPV_ERROR_INVALID_LOOKUP; |
| 124 |
} |
| 125 |
|
| 126 |
spv_result_t spvOpcodeTableValueLookup(spv_target_env env, |
| 127 |
const spv_opcode_table table, |
| 128 |
const SpvOp opcode, |
| 129 |
spv_opcode_desc* pEntry) { |
| 130 |
if (!table) return SPV_ERROR_INVALID_TABLE; |
| 131 |
if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
| 132 |
|
| 133 |
const auto beg = table->entries; |
| 134 |
const auto end = table->entries + table->count; |
| 135 |
|
| 136 |
spv_opcode_desc_t needle = {"", opcode, 0, nullptr, 0, {}, |
| 137 |
false, false, 0, nullptr, ~0u, ~0u}; |
| 138 |
|
| 139 |
auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) { |
| 140 |
return lhs.opcode < rhs.opcode; |
| 141 |
}; |
| 142 |
|
| 143 |
// We need to loop here because there can exist multiple symbols for the same |
| 144 |
// opcode value, and they can be introduced in different target environments, |
| 145 |
// which means they can have different minimal version requirements. |
| 146 |
// Assumes the underlying table is already sorted ascendingly according to |
| 147 |
// opcode value. |
| 148 |
const auto version = spvVersionForTargetEnv(env); |
| 149 |
for (auto it = std::lower_bound(beg, end, needle, comp); |
| 150 |
it != end && it->opcode == opcode; ++it) { |
| 151 |
// We considers the current opcode as available as long as |
| 152 |
// 1. The target environment satisfies the minimal requirement of the |
| 153 |
// opcode; or |
| 154 |
// 2. There is at least one extension enabling this opcode. |
| 155 |
// |
| 156 |
// Note that the second rule assumes the extension enabling this instruction |
| 157 |
// is indeed requested in the SPIR-V code; checking that should be |
| 158 |
// validator's work. |
| 159 |
if ((version >= it->minVersion && version <= it->lastVersion) || |
| 160 |
it->numExtensions > 0u || it->numCapabilities > 0u) { |
| 161 |
*pEntry = it; |
| 162 |
return SPV_SUCCESS; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return SPV_ERROR_INVALID_LOOKUP; |
| 167 |
} |
| 168 |
|
| 169 |
void spvInstructionCopy(const uint32_t* words, const SpvOp opcode, |
| 170 |
const uint16_t wordCount, const spv_endianness_t endian, |
| 171 |
spv_instruction_t* pInst) { |
| 172 |
pInst->opcode = opcode; |
| 173 |
pInst->words.resize(wordCount); |
| 174 |
for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) { |
| 175 |
pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian); |
| 176 |
if (!wordIndex) { |
| 177 |
uint16_t thisWordCount; |
| 178 |
uint16_t thisOpcode; |
| 179 |
spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode); |
| 180 |
assert(opcode == static_cast<SpvOp>(thisOpcode) && |
| 181 |
wordCount == thisWordCount && "Endianness failed!"); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
const char* spvOpcodeString(const uint32_t opcode) { |
| 187 |
const auto beg = kOpcodeTableEntries; |
| 188 |
const auto end = kOpcodeTableEntries + ARRAY_SIZE(kOpcodeTableEntries); |
| 189 |
spv_opcode_desc_t needle = {"", static_cast<SpvOp>(opcode), |
| 190 |
0, nullptr, |
| 191 |
0, {}, |
| 192 |
false, false, |
| 193 |
0, nullptr, |
| 194 |
~0u, ~0u}; |
| 195 |
auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) { |
| 196 |
return lhs.opcode < rhs.opcode; |
| 197 |
}; |
| 198 |
auto it = std::lower_bound(beg, end, needle, comp); |
| 199 |
if (it != end && it->opcode == opcode) { |
| 200 |
return it->name; |
| 201 |
} |
| 202 |
|
| 203 |
assert(0 && "Unreachable!"); |
| 204 |
return "unknown"; |
| 205 |
} |
| 206 |
|
| 207 |
int32_t spvOpcodeIsScalarType(const SpvOp opcode) { |
| 208 |
switch (opcode) { |
| 209 |
case SpvOpTypeInt: |
| 210 |
case SpvOpTypeFloat: |
| 211 |
case SpvOpTypeBool: |
| 212 |
return true; |
| 213 |
default: |
| 214 |
return false; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
int32_t spvOpcodeIsSpecConstant(const SpvOp opcode) { |
| 219 |
switch (opcode) { |
| 220 |
case SpvOpSpecConstantTrue: |
| 221 |
case SpvOpSpecConstantFalse: |
| 222 |
case SpvOpSpecConstant: |
| 223 |
case SpvOpSpecConstantComposite: |
| 224 |
case SpvOpSpecConstantOp: |
| 225 |
return true; |
| 226 |
default: |
| 227 |
return false; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
int32_t spvOpcodeIsConstant(const SpvOp opcode) { |
| 232 |
switch (opcode) { |
| 233 |
case SpvOpConstantTrue: |
| 234 |
case SpvOpConstantFalse: |
| 235 |
case SpvOpConstant: |
| 236 |
case SpvOpConstantComposite: |
| 237 |
case SpvOpConstantSampler: |
| 238 |
case SpvOpConstantNull: |
| 239 |
case SpvOpSpecConstantTrue: |
| 240 |
case SpvOpSpecConstantFalse: |
| 241 |
case SpvOpSpecConstant: |
| 242 |
case SpvOpSpecConstantComposite: |
| 243 |
case SpvOpSpecConstantOp: |
| 244 |
return true; |
| 245 |
default: |
| 246 |
return false; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
bool spvOpcodeIsConstantOrUndef(const SpvOp opcode) { |
| 251 |
return opcode == SpvOpUndef || spvOpcodeIsConstant(opcode); |
| 252 |
} |
| 253 |
|
| 254 |
bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode) { |
| 255 |
switch (opcode) { |
| 256 |
case SpvOpSpecConstantTrue: |
| 257 |
case SpvOpSpecConstantFalse: |
| 258 |
case SpvOpSpecConstant: |
| 259 |
return true; |
| 260 |
default: |
| 261 |
return false; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
int32_t spvOpcodeIsComposite(const SpvOp opcode) { |
| 266 |
switch (opcode) { |
| 267 |
case SpvOpTypeVector: |
| 268 |
case SpvOpTypeMatrix: |
| 269 |
case SpvOpTypeArray: |
| 270 |
case SpvOpTypeStruct: |
| 271 |
case SpvOpTypeCooperativeMatrixNV: |
| 272 |
return true; |
| 273 |
default: |
| 274 |
return false; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode) { |
| 279 |
switch (opcode) { |
| 280 |
case SpvOpVariable: |
| 281 |
case SpvOpAccessChain: |
| 282 |
case SpvOpInBoundsAccessChain: |
| 283 |
case SpvOpFunctionParameter: |
| 284 |
case SpvOpImageTexelPointer: |
| 285 |
case SpvOpCopyObject: |
| 286 |
case SpvOpSelect: |
| 287 |
case SpvOpPhi: |
| 288 |
case SpvOpFunctionCall: |
| 289 |
case SpvOpPtrAccessChain: |
| 290 |
case SpvOpLoad: |
| 291 |
case SpvOpConstantNull: |
| 292 |
return true; |
| 293 |
default: |
| 294 |
return false; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode) { |
| 299 |
switch (opcode) { |
| 300 |
case SpvOpVariable: |
| 301 |
case SpvOpAccessChain: |
| 302 |
case SpvOpInBoundsAccessChain: |
| 303 |
case SpvOpFunctionParameter: |
| 304 |
case SpvOpImageTexelPointer: |
| 305 |
case SpvOpCopyObject: |
| 306 |
return true; |
| 307 |
default: |
| 308 |
return false; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
int32_t spvOpcodeGeneratesType(SpvOp op) { |
| 313 |
switch (op) { |
| 314 |
case SpvOpTypeVoid: |
| 315 |
case SpvOpTypeBool: |
| 316 |
case SpvOpTypeInt: |
| 317 |
case SpvOpTypeFloat: |
| 318 |
case SpvOpTypeVector: |
| 319 |
case SpvOpTypeMatrix: |
| 320 |
case SpvOpTypeImage: |
| 321 |
case SpvOpTypeSampler: |
| 322 |
case SpvOpTypeSampledImage: |
| 323 |
case SpvOpTypeArray: |
| 324 |
case SpvOpTypeRuntimeArray: |
| 325 |
case SpvOpTypeStruct: |
| 326 |
case SpvOpTypeOpaque: |
| 327 |
case SpvOpTypePointer: |
| 328 |
case SpvOpTypeFunction: |
| 329 |
case SpvOpTypeEvent: |
| 330 |
case SpvOpTypeDeviceEvent: |
| 331 |
case SpvOpTypeReserveId: |
| 332 |
case SpvOpTypeQueue: |
| 333 |
case SpvOpTypePipe: |
| 334 |
case SpvOpTypePipeStorage: |
| 335 |
case SpvOpTypeNamedBarrier: |
| 336 |
case SpvOpTypeAccelerationStructureNV: |
| 337 |
case SpvOpTypeCooperativeMatrixNV: |
| 338 |
// case SpvOpTypeAccelerationStructureKHR: covered by |
| 339 |
// SpvOpTypeAccelerationStructureNV |
| 340 |
case SpvOpTypeRayQueryProvisionalKHR: |
| 341 |
return true; |
| 342 |
default: |
| 343 |
// In particular, OpTypeForwardPointer does not generate a type, |
| 344 |
// but declares a storage class for a pointer type generated |
| 345 |
// by a different instruction. |
| 346 |
break; |
| 347 |
} |
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
bool spvOpcodeIsDecoration(const SpvOp opcode) { |
| 352 |
switch (opcode) { |
| 353 |
case SpvOpDecorate: |
| 354 |
case SpvOpDecorateId: |
| 355 |
case SpvOpMemberDecorate: |
| 356 |
case SpvOpGroupDecorate: |
| 357 |
case SpvOpGroupMemberDecorate: |
| 358 |
case SpvOpDecorateStringGOOGLE: |
| 359 |
case SpvOpMemberDecorateStringGOOGLE: |
| 360 |
return true; |
| 361 |
default: |
| 362 |
break; |
| 363 |
} |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
bool spvOpcodeIsLoad(const SpvOp opcode) { |
| 368 |
switch (opcode) { |
| 369 |
case SpvOpLoad: |
| 370 |
case SpvOpImageSampleExplicitLod: |
| 371 |
case SpvOpImageSampleImplicitLod: |
| 372 |
case SpvOpImageSampleDrefImplicitLod: |
| 373 |
case SpvOpImageSampleDrefExplicitLod: |
| 374 |
case SpvOpImageSampleProjImplicitLod: |
| 375 |
case SpvOpImageSampleProjExplicitLod: |
| 376 |
case SpvOpImageSampleProjDrefImplicitLod: |
| 377 |
case SpvOpImageSampleProjDrefExplicitLod: |
| 378 |
case SpvOpImageFetch: |
| 379 |
case SpvOpImageGather: |
| 380 |
case SpvOpImageDrefGather: |
| 381 |
case SpvOpImageRead: |
| 382 |
case SpvOpImageSparseSampleImplicitLod: |
| 383 |
case SpvOpImageSparseSampleExplicitLod: |
| 384 |
case SpvOpImageSparseSampleDrefExplicitLod: |
| 385 |
case SpvOpImageSparseSampleDrefImplicitLod: |
| 386 |
case SpvOpImageSparseFetch: |
| 387 |
case SpvOpImageSparseGather: |
| 388 |
case SpvOpImageSparseDrefGather: |
| 389 |
case SpvOpImageSparseRead: |
| 390 |
return true; |
| 391 |
default: |
| 392 |
return false; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
bool spvOpcodeIsBranch(SpvOp opcode) { |
| 397 |
switch (opcode) { |
| 398 |
case SpvOpBranch: |
| 399 |
case SpvOpBranchConditional: |
| 400 |
case SpvOpSwitch: |
| 401 |
return true; |
| 402 |
default: |
| 403 |
return false; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
bool spvOpcodeIsAtomicWithLoad(const SpvOp opcode) { |
| 408 |
switch (opcode) { |
| 409 |
case SpvOpAtomicLoad: |
| 410 |
case SpvOpAtomicExchange: |
| 411 |
case SpvOpAtomicCompareExchange: |
| 412 |
case SpvOpAtomicCompareExchangeWeak: |
| 413 |
case SpvOpAtomicIIncrement: |
| 414 |
case SpvOpAtomicIDecrement: |
| 415 |
case SpvOpAtomicIAdd: |
| 416 |
case SpvOpAtomicFAddEXT: |
| 417 |
case SpvOpAtomicISub: |
| 418 |
case SpvOpAtomicSMin: |
| 419 |
case SpvOpAtomicUMin: |
| 420 |
case SpvOpAtomicSMax: |
| 421 |
case SpvOpAtomicUMax: |
| 422 |
case SpvOpAtomicAnd: |
| 423 |
case SpvOpAtomicOr: |
| 424 |
case SpvOpAtomicXor: |
| 425 |
case SpvOpAtomicFlagTestAndSet: |
| 426 |
return true; |
| 427 |
default: |
| 428 |
return false; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
bool spvOpcodeIsAtomicOp(const SpvOp opcode) { |
| 433 |
return (spvOpcodeIsAtomicWithLoad(opcode) || opcode == SpvOpAtomicStore || |
| 434 |
opcode == SpvOpAtomicFlagClear); |
| 435 |
} |
| 436 |
|
| 437 |
bool spvOpcodeIsReturn(SpvOp opcode) { |
| 438 |
switch (opcode) { |
| 439 |
case SpvOpReturn: |
| 440 |
case SpvOpReturnValue: |
| 441 |
return true; |
| 442 |
default: |
| 443 |
return false; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
bool spvOpcodeIsReturnOrAbort(SpvOp opcode) { |
| 448 |
return spvOpcodeIsReturn(opcode) || opcode == SpvOpKill || |
| 449 |
opcode == SpvOpUnreachable || opcode == SpvOpTerminateInvocation; |
| 450 |
} |
| 451 |
|
| 452 |
bool spvOpcodeIsBlockTerminator(SpvOp opcode) { |
| 453 |
return spvOpcodeIsBranch(opcode) || spvOpcodeIsReturnOrAbort(opcode); |
| 454 |
} |
| 455 |
|
| 456 |
bool spvOpcodeIsBaseOpaqueType(SpvOp opcode) { |
| 457 |
switch (opcode) { |
| 458 |
case SpvOpTypeImage: |
| 459 |
case SpvOpTypeSampler: |
| 460 |
case SpvOpTypeSampledImage: |
| 461 |
case SpvOpTypeOpaque: |
| 462 |
case SpvOpTypeEvent: |
| 463 |
case SpvOpTypeDeviceEvent: |
| 464 |
case SpvOpTypeReserveId: |
| 465 |
case SpvOpTypeQueue: |
| 466 |
case SpvOpTypePipe: |
| 467 |
case SpvOpTypeForwardPointer: |
| 468 |
case SpvOpTypePipeStorage: |
| 469 |
case SpvOpTypeNamedBarrier: |
| 470 |
return true; |
| 471 |
default: |
| 472 |
return false; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode) { |
| 477 |
switch (opcode) { |
| 478 |
case SpvOpGroupNonUniformElect: |
| 479 |
case SpvOpGroupNonUniformAll: |
| 480 |
case SpvOpGroupNonUniformAny: |
| 481 |
case SpvOpGroupNonUniformAllEqual: |
| 482 |
case SpvOpGroupNonUniformBroadcast: |
| 483 |
case SpvOpGroupNonUniformBroadcastFirst: |
| 484 |
case SpvOpGroupNonUniformBallot: |
| 485 |
case SpvOpGroupNonUniformInverseBallot: |
| 486 |
case SpvOpGroupNonUniformBallotBitExtract: |
| 487 |
case SpvOpGroupNonUniformBallotBitCount: |
| 488 |
case SpvOpGroupNonUniformBallotFindLSB: |
| 489 |
case SpvOpGroupNonUniformBallotFindMSB: |
| 490 |
case SpvOpGroupNonUniformShuffle: |
| 491 |
case SpvOpGroupNonUniformShuffleXor: |
| 492 |
case SpvOpGroupNonUniformShuffleUp: |
| 493 |
case SpvOpGroupNonUniformShuffleDown: |
| 494 |
case SpvOpGroupNonUniformIAdd: |
| 495 |
case SpvOpGroupNonUniformFAdd: |
| 496 |
case SpvOpGroupNonUniformIMul: |
| 497 |
case SpvOpGroupNonUniformFMul: |
| 498 |
case SpvOpGroupNonUniformSMin: |
| 499 |
case SpvOpGroupNonUniformUMin: |
| 500 |
case SpvOpGroupNonUniformFMin: |
| 501 |
case SpvOpGroupNonUniformSMax: |
| 502 |
case SpvOpGroupNonUniformUMax: |
| 503 |
case SpvOpGroupNonUniformFMax: |
| 504 |
case SpvOpGroupNonUniformBitwiseAnd: |
| 505 |
case SpvOpGroupNonUniformBitwiseOr: |
| 506 |
case SpvOpGroupNonUniformBitwiseXor: |
| 507 |
case SpvOpGroupNonUniformLogicalAnd: |
| 508 |
case SpvOpGroupNonUniformLogicalOr: |
| 509 |
case SpvOpGroupNonUniformLogicalXor: |
| 510 |
case SpvOpGroupNonUniformQuadBroadcast: |
| 511 |
case SpvOpGroupNonUniformQuadSwap: |
| 512 |
return true; |
| 513 |
default: |
| 514 |
return false; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
bool spvOpcodeIsScalarizable(SpvOp opcode) { |
| 519 |
switch (opcode) { |
| 520 |
case SpvOpPhi: |
| 521 |
case SpvOpCopyObject: |
| 522 |
case SpvOpConvertFToU: |
| 523 |
case SpvOpConvertFToS: |
| 524 |
case SpvOpConvertSToF: |
| 525 |
case SpvOpConvertUToF: |
| 526 |
case SpvOpUConvert: |
| 527 |
case SpvOpSConvert: |
| 528 |
case SpvOpFConvert: |
| 529 |
case SpvOpQuantizeToF16: |
| 530 |
case SpvOpVectorInsertDynamic: |
| 531 |
case SpvOpSNegate: |
| 532 |
case SpvOpFNegate: |
| 533 |
case SpvOpIAdd: |
| 534 |
case SpvOpFAdd: |
| 535 |
case SpvOpISub: |
| 536 |
case SpvOpFSub: |
| 537 |
case SpvOpIMul: |
| 538 |
case SpvOpFMul: |
| 539 |
case SpvOpUDiv: |
| 540 |
case SpvOpSDiv: |
| 541 |
case SpvOpFDiv: |
| 542 |
case SpvOpUMod: |
| 543 |
case SpvOpSRem: |
| 544 |
case SpvOpSMod: |
| 545 |
case SpvOpFRem: |
| 546 |
case SpvOpFMod: |
| 547 |
case SpvOpVectorTimesScalar: |
| 548 |
case SpvOpIAddCarry: |
| 549 |
case SpvOpISubBorrow: |
| 550 |
case SpvOpUMulExtended: |
| 551 |
case SpvOpSMulExtended: |
| 552 |
case SpvOpShiftRightLogical: |
| 553 |
case SpvOpShiftRightArithmetic: |
| 554 |
case SpvOpShiftLeftLogical: |
| 555 |
case SpvOpBitwiseOr: |
| 556 |
case SpvOpBitwiseAnd: |
| 557 |
case SpvOpNot: |
| 558 |
case SpvOpBitFieldInsert: |
| 559 |
case SpvOpBitFieldSExtract: |
| 560 |
case SpvOpBitFieldUExtract: |
| 561 |
case SpvOpBitReverse: |
| 562 |
case SpvOpBitCount: |
| 563 |
case SpvOpIsNan: |
| 564 |
case SpvOpIsInf: |
| 565 |
case SpvOpIsFinite: |
| 566 |
case SpvOpIsNormal: |
| 567 |
case SpvOpSignBitSet: |
| 568 |
case SpvOpLessOrGreater: |
| 569 |
case SpvOpOrdered: |
| 570 |
case SpvOpUnordered: |
| 571 |
case SpvOpLogicalEqual: |
| 572 |
case SpvOpLogicalNotEqual: |
| 573 |
case SpvOpLogicalOr: |
| 574 |
case SpvOpLogicalAnd: |
| 575 |
case SpvOpLogicalNot: |
| 576 |
case SpvOpSelect: |
| 577 |
case SpvOpIEqual: |
| 578 |
case SpvOpINotEqual: |
| 579 |
case SpvOpUGreaterThan: |
| 580 |
case SpvOpSGreaterThan: |
| 581 |
case SpvOpUGreaterThanEqual: |
| 582 |
case SpvOpSGreaterThanEqual: |
| 583 |
case SpvOpULessThan: |
| 584 |
case SpvOpSLessThan: |
| 585 |
case SpvOpULessThanEqual: |
| 586 |
case SpvOpSLessThanEqual: |
| 587 |
case SpvOpFOrdEqual: |
| 588 |
case SpvOpFUnordEqual: |
| 589 |
case SpvOpFOrdNotEqual: |
| 590 |
case SpvOpFUnordNotEqual: |
| 591 |
case SpvOpFOrdLessThan: |
| 592 |
case SpvOpFUnordLessThan: |
| 593 |
case SpvOpFOrdGreaterThan: |
| 594 |
case SpvOpFUnordGreaterThan: |
| 595 |
case SpvOpFOrdLessThanEqual: |
| 596 |
case SpvOpFUnordLessThanEqual: |
| 597 |
case SpvOpFOrdGreaterThanEqual: |
| 598 |
case SpvOpFUnordGreaterThanEqual: |
| 599 |
return true; |
| 600 |
default: |
| 601 |
return false; |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
bool spvOpcodeIsDebug(SpvOp opcode) { |
| 606 |
switch (opcode) { |
| 607 |
case SpvOpName: |
| 608 |
case SpvOpMemberName: |
| 609 |
case SpvOpSource: |
| 610 |
case SpvOpSourceContinued: |
| 611 |
case SpvOpSourceExtension: |
| 612 |
case SpvOpString: |
| 613 |
case SpvOpLine: |
| 614 |
case SpvOpNoLine: |
| 615 |
return true; |
| 616 |
default: |
| 617 |
return false; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
bool spvOpcodeIsCommutativeBinaryOperator(SpvOp opcode) { |
| 622 |
switch (opcode) { |
| 623 |
case SpvOpPtrEqual: |
| 624 |
case SpvOpPtrNotEqual: |
| 625 |
case SpvOpIAdd: |
| 626 |
case SpvOpFAdd: |
| 627 |
case SpvOpIMul: |
| 628 |
case SpvOpFMul: |
| 629 |
case SpvOpDot: |
| 630 |
case SpvOpIAddCarry: |
| 631 |
case SpvOpUMulExtended: |
| 632 |
case SpvOpSMulExtended: |
| 633 |
case SpvOpBitwiseOr: |
| 634 |
case SpvOpBitwiseXor: |
| 635 |
case SpvOpBitwiseAnd: |
| 636 |
case SpvOpOrdered: |
| 637 |
case SpvOpUnordered: |
| 638 |
case SpvOpLogicalEqual: |
| 639 |
case SpvOpLogicalNotEqual: |
| 640 |
case SpvOpLogicalOr: |
| 641 |
case SpvOpLogicalAnd: |
| 642 |
case SpvOpIEqual: |
| 643 |
case SpvOpINotEqual: |
| 644 |
case SpvOpFOrdEqual: |
| 645 |
case SpvOpFUnordEqual: |
| 646 |
case SpvOpFOrdNotEqual: |
| 647 |
case SpvOpFUnordNotEqual: |
| 648 |
return true; |
| 649 |
default: |
| 650 |
return false; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
bool spvOpcodeIsLinearAlgebra(SpvOp opcode) { |
| 655 |
switch (opcode) { |
| 656 |
case SpvOpTranspose: |
| 657 |
case SpvOpVectorTimesScalar: |
| 658 |
case SpvOpMatrixTimesScalar: |
| 659 |
case SpvOpVectorTimesMatrix: |
| 660 |
case SpvOpMatrixTimesVector: |
| 661 |
case SpvOpMatrixTimesMatrix: |
| 662 |
case SpvOpOuterProduct: |
| 663 |
case SpvOpDot: |
| 664 |
return true; |
| 665 |
default: |
| 666 |
return false; |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
bool spvOpcodeIsImageSample(const SpvOp opcode) { |
| 671 |
switch (opcode) { |
| 672 |
case SpvOpImageSampleImplicitLod: |
| 673 |
case SpvOpImageSampleExplicitLod: |
| 674 |
case SpvOpImageSampleDrefImplicitLod: |
| 675 |
case SpvOpImageSampleDrefExplicitLod: |
| 676 |
case SpvOpImageSampleProjImplicitLod: |
| 677 |
case SpvOpImageSampleProjExplicitLod: |
| 678 |
case SpvOpImageSampleProjDrefImplicitLod: |
| 679 |
case SpvOpImageSampleProjDrefExplicitLod: |
| 680 |
case SpvOpImageSparseSampleImplicitLod: |
| 681 |
case SpvOpImageSparseSampleExplicitLod: |
| 682 |
case SpvOpImageSparseSampleDrefImplicitLod: |
| 683 |
case SpvOpImageSparseSampleDrefExplicitLod: |
| 684 |
return true; |
| 685 |
default: |
| 686 |
return false; |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(SpvOp opcode) { |
| 691 |
switch (opcode) { |
| 692 |
case SpvOpMemoryBarrier: |
| 693 |
return {1}; |
| 694 |
case SpvOpAtomicStore: |
| 695 |
case SpvOpControlBarrier: |
| 696 |
case SpvOpAtomicFlagClear: |
| 697 |
case SpvOpMemoryNamedBarrier: |
| 698 |
return {2}; |
| 699 |
case SpvOpAtomicLoad: |
| 700 |
case SpvOpAtomicExchange: |
| 701 |
case SpvOpAtomicIIncrement: |
| 702 |
case SpvOpAtomicIDecrement: |
| 703 |
case SpvOpAtomicIAdd: |
| 704 |
case SpvOpAtomicFAddEXT: |
| 705 |
case SpvOpAtomicISub: |
| 706 |
case SpvOpAtomicSMin: |
| 707 |
case SpvOpAtomicUMin: |
| 708 |
case SpvOpAtomicSMax: |
| 709 |
case SpvOpAtomicUMax: |
| 710 |
case SpvOpAtomicAnd: |
| 711 |
case SpvOpAtomicOr: |
| 712 |
case SpvOpAtomicXor: |
| 713 |
case SpvOpAtomicFlagTestAndSet: |
| 714 |
return {4}; |
| 715 |
case SpvOpAtomicCompareExchange: |
| 716 |
case SpvOpAtomicCompareExchangeWeak: |
| 717 |
return {4, 5}; |
| 718 |
default: |
| 719 |
return {}; |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
bool spvOpcodeIsAccessChain(SpvOp opcode) { |
| 724 |
switch (opcode) { |
| 725 |
case SpvOpAccessChain: |
| 726 |
case SpvOpInBoundsAccessChain: |
| 727 |
case SpvOpPtrAccessChain: |
| 728 |
case SpvOpInBoundsPtrAccessChain: |
| 729 |
return true; |
| 730 |
default: |
| 731 |
return false; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
bool spvOpcodeIsBit(SpvOp opcode) { |
| 736 |
switch (opcode) { |
| 737 |
case SpvOpShiftRightLogical: |
| 738 |
case SpvOpShiftRightArithmetic: |
| 739 |
case SpvOpShiftLeftLogical: |
| 740 |
case SpvOpBitwiseOr: |
| 741 |
case SpvOpBitwiseXor: |
| 742 |
case SpvOpBitwiseAnd: |
| 743 |
case SpvOpNot: |
| 744 |
case SpvOpBitReverse: |
| 745 |
case SpvOpBitCount: |
| 746 |
return true; |
| 747 |
default: |
| 748 |
return false; |
| 749 |
} |
| 750 |
} |