opcode.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) 2015-2016 The Khronos Group 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. #ifndef SOURCE_OPCODE_H_
  15. #define SOURCE_OPCODE_H_
  16. #include "source/instruction.h"
  17. #include "source/latest_version_spirv_header.h"
  18. #include "source/table.h"
  19. #include "spirv-tools/libspirv.h"
  20. // Returns the name of a registered SPIR-V generator as a null-terminated
  21. // string. If the generator is not known, then returns the string "Unknown".
  22. // The generator parameter should be most significant 16-bits of the generator
  23. // word in the SPIR-V module header.
  24. //
  25. // See the registry at https://www.khronos.org/registry/spir-v/api/spir-v.xml.
  26. const char* spvGeneratorStr(uint32_t generator);
  27. // Combines word_count and opcode enumerant in single word.
  28. uint32_t spvOpcodeMake(uint16_t word_count, spv::Op opcode);
  29. // Splits word into into two constituent parts: word_count and opcode.
  30. void spvOpcodeSplit(const uint32_t word, uint16_t* word_count,
  31. uint16_t* opcode);
  32. // Copies an instruction's word and fixes the endianness to host native. The
  33. // source instruction's stream/opcode/endianness is in the words/opcode/endian
  34. // parameter. The word_count parameter specifies the number of words to copy.
  35. // Writes copied instruction into *inst.
  36. void spvInstructionCopy(const uint32_t* words, const spv::Op opcode,
  37. const uint16_t word_count,
  38. const spv_endianness_t endian, spv_instruction_t* inst);
  39. // Determine if the given opcode is a scalar type. Returns zero if false,
  40. // non-zero otherwise.
  41. int32_t spvOpcodeIsScalarType(const spv::Op opcode);
  42. // Determines if the given opcode is a specialization constant. Returns zero if
  43. // false, non-zero otherwise.
  44. int32_t spvOpcodeIsSpecConstant(const spv::Op opcode);
  45. // Determines if the given opcode is a constant. Returns zero if false, non-zero
  46. // otherwise.
  47. int32_t spvOpcodeIsConstant(const spv::Op opcode);
  48. // Returns true if the given opcode is a constant or undef.
  49. bool spvOpcodeIsConstantOrUndef(const spv::Op opcode);
  50. // Returns true if the given opcode is a scalar specialization constant.
  51. bool spvOpcodeIsScalarSpecConstant(const spv::Op opcode);
  52. // Determines if the given opcode is a composite type. Returns zero if false,
  53. // non-zero otherwise.
  54. int32_t spvOpcodeIsComposite(const spv::Op opcode);
  55. // Determines if the given opcode results in a pointer when using the logical
  56. // addressing model. Returns zero if false, non-zero otherwise.
  57. int32_t spvOpcodeReturnsLogicalPointer(const spv::Op opcode);
  58. // Returns whether the given opcode could result in a pointer or a variable
  59. // pointer when using the logical addressing model.
  60. bool spvOpcodeReturnsLogicalVariablePointer(const spv::Op opcode);
  61. // Determines if the given opcode generates a type. Returns zero if false,
  62. // non-zero otherwise.
  63. int32_t spvOpcodeGeneratesType(spv::Op opcode);
  64. // Returns true if the opcode adds a decoration to an id.
  65. bool spvOpcodeIsDecoration(const spv::Op opcode);
  66. // Returns true if the opcode is a load from memory into a result id. This
  67. // function only considers core instructions.
  68. bool spvOpcodeIsLoad(const spv::Op opcode);
  69. // Returns true if the opcode is an atomic operation that uses the original
  70. // value.
  71. bool spvOpcodeIsAtomicWithLoad(const spv::Op opcode);
  72. // Returns true if the opcode is an atomic operation.
  73. bool spvOpcodeIsAtomicOp(const spv::Op opcode);
  74. // Returns true if the given opcode is a branch instruction.
  75. bool spvOpcodeIsBranch(spv::Op opcode);
  76. // Returns true if the given opcode is a return instruction.
  77. bool spvOpcodeIsReturn(spv::Op opcode);
  78. // Returns true if the given opcode aborts execution. To abort means that after
  79. // executing that instruction, no other instructions will be executed regardless
  80. // of the context in which the instruction appears. Note that `OpUnreachable`
  81. // is considered an abort even if its behaviour is undefined.
  82. bool spvOpcodeIsAbort(spv::Op opcode);
  83. // Returns true if the given opcode is a return instruction or it aborts
  84. // execution.
  85. bool spvOpcodeIsReturnOrAbort(spv::Op opcode);
  86. // Returns true if the given opcode is a basic block terminator.
  87. bool spvOpcodeIsBlockTerminator(spv::Op opcode);
  88. // Returns true if the given opcode always defines an opaque type.
  89. bool spvOpcodeIsBaseOpaqueType(spv::Op opcode);
  90. // Returns true if the given opcode is a non-uniform group operation.
  91. bool spvOpcodeIsNonUniformGroupOperation(spv::Op opcode);
  92. // Returns true if the opcode with vector inputs could be divided into a series
  93. // of independent scalar operations that would give the same result.
  94. bool spvOpcodeIsScalarizable(spv::Op opcode);
  95. // Returns true if the given opcode is a debug instruction.
  96. bool spvOpcodeIsDebug(spv::Op opcode);
  97. // Returns true for opcodes that are binary operators,
  98. // where the order of the operands is irrelevant.
  99. bool spvOpcodeIsCommutativeBinaryOperator(spv::Op opcode);
  100. // Returns true for opcodes that represent linear algebra instructions.
  101. bool spvOpcodeIsLinearAlgebra(spv::Op opcode);
  102. // Returns true for opcodes that represent image sample instructions.
  103. bool spvOpcodeIsImageSample(spv::Op opcode);
  104. // Returns true if the opcode is either OpExtInst or OpExtInstWithForwardRefsKHR
  105. bool spvIsExtendedInstruction(spv::Op opcode);
  106. // Returns a vector containing the indices of the memory semantics <id>
  107. // operands for |opcode|.
  108. std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(spv::Op opcode);
  109. // Returns true for opcodes that represent access chain instructions.
  110. bool spvOpcodeIsAccessChain(spv::Op opcode);
  111. // Returns true for opcodes that represent bit instructions.
  112. bool spvOpcodeIsBit(spv::Op opcode);
  113. // Gets the name of an instruction, without the "Op" prefix.
  114. const char* spvOpcodeString(const spv::Op opcode);
  115. // Returns true for opcodes that generate an untyped pointer result.
  116. bool spvOpcodeGeneratesUntypedPointer(spv::Op opcode);
  117. #endif // SOURCE_OPCODE_H_