Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 2 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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 |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 6 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 8 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 14 | |
dan sinclair | 58a6876 | 2018-08-03 08:05:33 -0400 | [diff] [blame] | 15 | #ifndef SOURCE_OPCODE_H_ |
| 16 | #define SOURCE_OPCODE_H_ |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 17 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 18 | #include "source/instruction.h" |
| 19 | #include "source/latest_version_spirv_header.h" |
| 20 | #include "source/table.h" |
David Neto | 5a70335 | 2016-02-17 14:44:00 -0500 | [diff] [blame] | 21 | #include "spirv-tools/libspirv.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 22 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 23 | // Returns the name of a registered SPIR-V generator as a null-terminated |
| 24 | // string. If the generator is not known, then returns the string "Unknown". |
| 25 | // The generator parameter should be most significant 16-bits of the generator |
| 26 | // word in the SPIR-V module header. |
| 27 | // |
| 28 | // See the registry at https://www.khronos.org/registry/spir-v/api/spir-v.xml. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 29 | const char* spvGeneratorStr(uint32_t generator); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 30 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 31 | // Combines word_count and opcode enumerant in single word. |
| 32 | uint32_t spvOpcodeMake(uint16_t word_count, SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 33 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 34 | // Splits word into into two constituent parts: word_count and opcode. |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 35 | void spvOpcodeSplit(const uint32_t word, uint16_t* word_count, |
| 36 | uint16_t* opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 37 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 38 | // Finds the named opcode in the given opcode table. On success, returns |
| 39 | // SPV_SUCCESS and writes a handle of the table entry into *entry. |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 40 | spv_result_t spvOpcodeTableNameLookup(spv_target_env, |
| 41 | const spv_opcode_table table, |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 42 | const char* name, spv_opcode_desc* entry); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 43 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 44 | // Finds the opcode by enumerant in the given opcode table. On success, returns |
| 45 | // SPV_SUCCESS and writes a handle of the table entry into *entry. |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 46 | spv_result_t spvOpcodeTableValueLookup(spv_target_env, |
| 47 | const spv_opcode_table table, |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 48 | const SpvOp opcode, |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 49 | spv_opcode_desc* entry); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 50 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 51 | // Copies an instruction's word and fixes the endianness to host native. The |
| 52 | // source instruction's stream/opcode/endianness is in the words/opcode/endian |
| 53 | // parameter. The word_count parameter specifies the number of words to copy. |
| 54 | // Writes copied instruction into *inst. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 55 | void spvInstructionCopy(const uint32_t* words, const SpvOp opcode, |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 56 | const uint16_t word_count, |
| 57 | const spv_endianness_t endian, spv_instruction_t* inst); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 58 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 59 | // Gets the name of an instruction, without the "Op" prefix. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 60 | const char* spvOpcodeString(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 61 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 62 | // Determine if the given opcode is a scalar type. Returns zero if false, |
| 63 | // non-zero otherwise. |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 64 | int32_t spvOpcodeIsScalarType(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 65 | |
Alan Baker | 0a2ee65 | 2018-03-23 14:18:54 -0400 | [diff] [blame] | 66 | // Determines if the given opcode is a specialization constant. Returns zero if |
| 67 | // false, non-zero otherwise. |
| 68 | int32_t spvOpcodeIsSpecConstant(const SpvOp opcode); |
| 69 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 70 | // Determines if the given opcode is a constant. Returns zero if false, non-zero |
| 71 | // otherwise. |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 72 | int32_t spvOpcodeIsConstant(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 73 | |
David Neto | 1f3fb50 | 2016-09-14 11:57:20 -0400 | [diff] [blame] | 74 | // Returns true if the given opcode is a constant or undef. |
| 75 | bool spvOpcodeIsConstantOrUndef(const SpvOp opcode); |
| 76 | |
Aliya Pazylbekova | edb5264 | 2017-02-24 20:43:28 -0500 | [diff] [blame] | 77 | // Returns true if the given opcode is a scalar specialization constant. |
| 78 | bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode); |
| 79 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 80 | // Determines if the given opcode is a composite type. Returns zero if false, |
| 81 | // non-zero otherwise. |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 82 | int32_t spvOpcodeIsComposite(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 83 | |
Florian Ziesche | 66fcb45 | 2016-03-02 22:17:54 +0100 | [diff] [blame] | 84 | // Determines if the given opcode results in a pointer when using the logical |
| 85 | // addressing model. Returns zero if false, non-zero otherwise. |
| 86 | int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 87 | |
Ehsan Nasiri | 23af06c | 2017-02-01 15:37:39 -0500 | [diff] [blame] | 88 | // Returns whether the given opcode could result in a pointer or a variable |
| 89 | // pointer when using the logical addressing model. |
| 90 | bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode); |
| 91 | |
Lei Zhang | af9906e | 2015-11-16 10:48:43 -0500 | [diff] [blame] | 92 | // Determines if the given opcode generates a type. Returns zero if false, |
| 93 | // non-zero otherwise. |
| 94 | int32_t spvOpcodeGeneratesType(SpvOp opcode); |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 95 | |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 96 | // Returns true if the opcode adds a decoration to an id. |
Steven Perron | eb4653a | 2017-11-13 15:31:43 -0500 | [diff] [blame] | 97 | bool spvOpcodeIsDecoration(const SpvOp opcode); |
| 98 | |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 99 | // Returns true if the opcode is a load from memory into a result id. This |
| 100 | // function only considers core instructions. |
| 101 | bool spvOpcodeIsLoad(const SpvOp opcode); |
| 102 | |
greg-lunarg | e545564 | 2018-10-12 06:46:35 -0600 | [diff] [blame] | 103 | // Returns true if the opcode is an atomic operation that uses the original |
| 104 | // value. |
| 105 | bool spvOpcodeIsAtomicWithLoad(const SpvOp opcode); |
| 106 | |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 107 | // Returns true if the opcode is an atomic operation. |
| 108 | bool spvOpcodeIsAtomicOp(const SpvOp opcode); |
| 109 | |
Diego Novillo | 7432784 | 2017-11-17 08:59:25 -0500 | [diff] [blame] | 110 | // Returns true if the given opcode is a branch instruction. |
| 111 | bool spvOpcodeIsBranch(SpvOp opcode); |
| 112 | |
| 113 | // Returns true if the given opcode is a return instruction. |
| 114 | bool spvOpcodeIsReturn(SpvOp opcode); |
| 115 | |
Diego Novillo | 5f10078 | 2018-01-03 15:25:03 -0500 | [diff] [blame] | 116 | // Returns true if the given opcode is a return instruction or it aborts |
| 117 | // execution. |
| 118 | bool spvOpcodeIsReturnOrAbort(SpvOp opcode); |
| 119 | |
Diego Novillo | 7432784 | 2017-11-17 08:59:25 -0500 | [diff] [blame] | 120 | // Returns true if the given opcode is a basic block terminator. |
| 121 | bool spvOpcodeIsBlockTerminator(SpvOp opcode); |
| 122 | |
Steven Perron | 79a0064 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 123 | // Returns true if the given opcode always defines an opaque type. |
| 124 | bool spvOpcodeIsBaseOpaqueType(SpvOp opcode); |
Alan Baker | 09c206b | 2018-04-17 10:18:59 -0400 | [diff] [blame] | 125 | |
| 126 | // Returns true if the given opcode is a non-uniform group operation. |
| 127 | bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode); |
Steven Perron | 2c0ce87 | 2018-04-23 11:13:07 -0400 | [diff] [blame] | 128 | |
Steven Perron | ee8cd5c | 2018-04-23 11:17:07 -0400 | [diff] [blame] | 129 | // Returns true if the opcode with vector inputs could be divided into a series |
| 130 | // of independent scalar operations that would give the same result. |
Steven Perron | 2c0ce87 | 2018-04-23 11:13:07 -0400 | [diff] [blame] | 131 | bool spvOpcodeIsScalarizable(SpvOp opcode); |
Ryan Harrison | 11c7a9e | 2018-11-20 16:12:28 -0500 | [diff] [blame] | 132 | |
| 133 | // Returns true if the given opcode is a debug instruction. |
| 134 | bool spvOpcodeIsDebug(SpvOp opcode); |
| 135 | |
Ryan Harrison | e545522 | 2019-03-14 13:34:33 -0400 | [diff] [blame^] | 136 | // Returns a vector containing the indices of the memory semantics <id> |
| 137 | // operands for |opcode|. |
| 138 | std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(SpvOp opcode); |
| 139 | |
dan sinclair | 58a6876 | 2018-08-03 08:05:33 -0400 | [diff] [blame] | 140 | #endif // SOURCE_OPCODE_H_ |