opcode.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // Finds the named opcode in the given opcode table. On success, returns
  33. // SPV_SUCCESS and writes a handle of the table entry into *entry.
  34. spv_result_t spvOpcodeTableNameLookup(spv_target_env,
  35. const spv_opcode_table table,
  36. const char* name, spv_opcode_desc* entry);
  37. // Finds the opcode by enumerant in the given opcode table. On success, returns
  38. // SPV_SUCCESS and writes a handle of the table entry into *entry.
  39. spv_result_t spvOpcodeTableValueLookup(spv_target_env,
  40. const spv_opcode_table table,
  41. const spv::Op opcode,
  42. spv_opcode_desc* entry);
  43. // Copies an instruction's word and fixes the endianness to host native. The
  44. // source instruction's stream/opcode/endianness is in the words/opcode/endian
  45. // parameter. The word_count parameter specifies the number of words to copy.
  46. // Writes copied instruction into *inst.
  47. void spvInstructionCopy(const uint32_t* words, const spv::Op opcode,
  48. const uint16_t word_count,
  49. const spv_endianness_t endian, spv_instruction_t* inst);
  50. // Determine if the given opcode is a scalar type. Returns zero if false,
  51. // non-zero otherwise.
  52. int32_t spvOpcodeIsScalarType(const spv::Op opcode);
  53. // Determines if the given opcode is a specialization constant. Returns zero if
  54. // false, non-zero otherwise.
  55. int32_t spvOpcodeIsSpecConstant(const spv::Op opcode);
  56. // Determines if the given opcode is a constant. Returns zero if false, non-zero
  57. // otherwise.
  58. int32_t spvOpcodeIsConstant(const spv::Op opcode);
  59. // Returns true if the given opcode is a constant or undef.
  60. bool spvOpcodeIsConstantOrUndef(const spv::Op opcode);
  61. // Returns true if the given opcode is a scalar specialization constant.
  62. bool spvOpcodeIsScalarSpecConstant(const spv::Op opcode);
  63. // Determines if the given opcode is a composite type. Returns zero if false,
  64. // non-zero otherwise.
  65. int32_t spvOpcodeIsComposite(const spv::Op opcode);
  66. // Determines if the given opcode results in a pointer when using the logical
  67. // addressing model. Returns zero if false, non-zero otherwise.
  68. int32_t spvOpcodeReturnsLogicalPointer(const spv::Op opcode);
  69. // Returns whether the given opcode could result in a pointer or a variable
  70. // pointer when using the logical addressing model.
  71. bool spvOpcodeReturnsLogicalVariablePointer(const spv::Op opcode);
  72. // Determines if the given opcode generates a type. Returns zero if false,
  73. // non-zero otherwise.
  74. int32_t spvOpcodeGeneratesType(spv::Op opcode);
  75. // Returns true if the opcode adds a decoration to an id.
  76. bool spvOpcodeIsDecoration(const spv::Op opcode);
  77. // Returns true if the opcode is a load from memory into a result id. This
  78. // function only considers core instructions.
  79. bool spvOpcodeIsLoad(const spv::Op opcode);
  80. // Returns true if the opcode is an atomic operation that uses the original
  81. // value.
  82. bool spvOpcodeIsAtomicWithLoad(const spv::Op opcode);
  83. // Returns true if the opcode is an atomic operation.
  84. bool spvOpcodeIsAtomicOp(const spv::Op opcode);
  85. // Returns true if the given opcode is a branch instruction.
  86. bool spvOpcodeIsBranch(spv::Op opcode);
  87. // Returns true if the given opcode is a return instruction.
  88. bool spvOpcodeIsReturn(spv::Op opcode);
  89. // Returns true if the given opcode aborts execution. To abort means that after
  90. // executing that instruction, no other instructions will be executed regardless
  91. // of the context in which the instruction appears. Note that `OpUnreachable`
  92. // is considered an abort even if its behaviour is undefined.
  93. bool spvOpcodeIsAbort(spv::Op opcode);
  94. // Returns true if the given opcode is a return instruction or it aborts
  95. // execution.
  96. bool spvOpcodeIsReturnOrAbort(spv::Op opcode);
  97. // Returns true if the given opcode is a basic block terminator.
  98. bool spvOpcodeIsBlockTerminator(spv::Op opcode);
  99. // Returns true if the given opcode always defines an opaque type.
  100. bool spvOpcodeIsBaseOpaqueType(spv::Op opcode);
  101. // Returns true if the given opcode is a non-uniform group operation.
  102. bool spvOpcodeIsNonUniformGroupOperation(spv::Op opcode);
  103. // Returns true if the opcode with vector inputs could be divided into a series
  104. // of independent scalar operations that would give the same result.
  105. bool spvOpcodeIsScalarizable(spv::Op opcode);
  106. // Returns true if the given opcode is a debug instruction.
  107. bool spvOpcodeIsDebug(spv::Op opcode);
  108. // Returns true for opcodes that are binary operators,
  109. // where the order of the operands is irrelevant.
  110. bool spvOpcodeIsCommutativeBinaryOperator(spv::Op opcode);
  111. // Returns true for opcodes that represent linear algebra instructions.
  112. bool spvOpcodeIsLinearAlgebra(spv::Op opcode);
  113. // Returns true for opcodes that represent image sample instructions.
  114. bool spvOpcodeIsImageSample(spv::Op opcode);
  115. // Returns true if the opcode is either OpExtInst or OpExtInstWithForwardRefsKHR
  116. bool spvIsExtendedInstruction(spv::Op opcode);
  117. // Returns a vector containing the indices of the memory semantics <id>
  118. // operands for |opcode|.
  119. std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(spv::Op opcode);
  120. // Returns true for opcodes that represent access chain instructions.
  121. bool spvOpcodeIsAccessChain(spv::Op opcode);
  122. // Returns true for opcodes that represent bit instructions.
  123. bool spvOpcodeIsBit(spv::Op opcode);
  124. // Gets the name of an instruction, without the "Op" prefix.
  125. const char* spvOpcodeString(const spv::Op opcode);
  126. // Returns true for opcodes that generate an untyped pointer result.
  127. bool spvOpcodeGeneratesUntypedPointer(spv::Op opcode);
  128. #endif // SOURCE_OPCODE_H_