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 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 15 | #include "opcode.h" |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
David Neto | 5a0b5ca | 2016-12-09 14:01:43 -0500 | [diff] [blame] | 20 | #include <algorithm> |
Lei Zhang | 972788b | 2015-11-12 13:48:30 -0500 | [diff] [blame] | 21 | #include <cstdlib> |
| 22 | |
Lei Zhang | 923f6c1 | 2015-11-11 12:45:23 -0500 | [diff] [blame] | 23 | #include "instruction.h" |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 24 | #include "macro.h" |
David Neto | 5a70335 | 2016-02-17 14:44:00 -0500 | [diff] [blame] | 25 | #include "spirv-tools/libspirv.h" |
Lei Zhang | aa056cd | 2015-11-11 14:24:04 -0500 | [diff] [blame] | 26 | #include "spirv_constant.h" |
David Neto | 4c21571 | 2015-12-22 15:08:41 -0500 | [diff] [blame] | 27 | #include "spirv_endian.h" |
Lei Zhang | 923f6c1 | 2015-11-11 12:45:23 -0500 | [diff] [blame] | 28 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 29 | namespace { |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 30 | struct OpcodeDescPtrLen { |
| 31 | const spv_opcode_desc_t* ptr; |
| 32 | uint32_t len; |
| 33 | }; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 34 | |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 35 | #include "core.insts-1.0.inc" // defines kOpcodeTableEntries_1_0 |
| 36 | #include "core.insts-1.1.inc" // defines kOpcodeTableEntries_1_1 |
| 37 | #include "core.insts-1.2.inc" // defines kOpcodeTableEntries_1_2 |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 38 | |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 39 | static const spv_opcode_table_t kTable_1_0 = { |
| 40 | ARRAY_SIZE(kOpcodeTableEntries_1_0), kOpcodeTableEntries_1_0}; |
| 41 | static const spv_opcode_table_t kTable_1_1 = { |
| 42 | ARRAY_SIZE(kOpcodeTableEntries_1_1), kOpcodeTableEntries_1_1}; |
| 43 | static const spv_opcode_table_t kTable_1_2 = { |
| 44 | ARRAY_SIZE(kOpcodeTableEntries_1_2), kOpcodeTableEntries_1_2}; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 45 | |
David Neto | 5a0b5ca | 2016-12-09 14:01:43 -0500 | [diff] [blame] | 46 | // Represents a vendor tool entry in the SPIR-V XML Regsitry. |
| 47 | struct VendorTool { |
| 48 | uint32_t value; |
| 49 | const char* vendor; |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 50 | const char* tool; // Might be empty string. |
| 51 | const char* vendor_tool; // Combiantion of vendor and tool. |
David Neto | 5a0b5ca | 2016-12-09 14:01:43 -0500 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | const VendorTool vendor_tools[] = { |
| 55 | #include "generators.inc" |
| 56 | }; |
| 57 | |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 58 | } // anonymous namespace |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 59 | |
David Neto | 5a0b5ca | 2016-12-09 14:01:43 -0500 | [diff] [blame] | 60 | // TODO(dneto): Move this to another file. It doesn't belong with opcode |
| 61 | // processing. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 62 | const char* spvGeneratorStr(uint32_t generator) { |
David Neto | 5a0b5ca | 2016-12-09 14:01:43 -0500 | [diff] [blame] | 63 | auto where = std::find_if( |
| 64 | std::begin(vendor_tools), std::end(vendor_tools), |
| 65 | [generator](const VendorTool& vt) { return generator == vt.value; }); |
| 66 | if (where != std::end(vendor_tools)) return where->vendor_tool; |
| 67 | return "Unknown"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 70 | uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 71 | return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16); |
| 72 | } |
| 73 | |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 74 | void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, |
| 75 | uint16_t* pOpcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 76 | if (pWordCount) { |
| 77 | *pWordCount = (uint16_t)((0xffff0000 & word) >> 16); |
| 78 | } |
| 79 | if (pOpcode) { |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 80 | *pOpcode = 0x0000ffff & word; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 84 | spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable, |
| 85 | spv_target_env env) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 86 | if (!pInstTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 87 | |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 88 | // Descriptions of each opcode. Each entry describes the format of the |
| 89 | // instruction that follows a particular opcode. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 90 | |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 91 | switch (env) { |
| 92 | case SPV_ENV_UNIVERSAL_1_0: |
| 93 | case SPV_ENV_VULKAN_1_0: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 94 | case SPV_ENV_OPENCL_1_2: |
| 95 | case SPV_ENV_OPENCL_EMBEDDED_1_2: |
| 96 | case SPV_ENV_OPENCL_2_0: |
| 97 | case SPV_ENV_OPENCL_EMBEDDED_2_0: |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 98 | case SPV_ENV_OPENCL_2_1: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 99 | case SPV_ENV_OPENCL_EMBEDDED_2_1: |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 100 | case SPV_ENV_OPENGL_4_0: |
| 101 | case SPV_ENV_OPENGL_4_1: |
| 102 | case SPV_ENV_OPENGL_4_2: |
| 103 | case SPV_ENV_OPENGL_4_3: |
| 104 | case SPV_ENV_OPENGL_4_5: |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 105 | *pInstTable = &kTable_1_0; |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 106 | return SPV_SUCCESS; |
| 107 | case SPV_ENV_UNIVERSAL_1_1: |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 108 | *pInstTable = &kTable_1_1; |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 109 | return SPV_SUCCESS; |
David Neto | dbc2049 | 2017-03-14 12:43:41 -0400 | [diff] [blame] | 110 | case SPV_ENV_UNIVERSAL_1_2: |
| 111 | case SPV_ENV_OPENCL_2_2: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 112 | case SPV_ENV_OPENCL_EMBEDDED_2_2: |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 113 | *pInstTable = &kTable_1_2; |
David Neto | dbc2049 | 2017-03-14 12:43:41 -0400 | [diff] [blame] | 114 | return SPV_SUCCESS; |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 115 | } |
| 116 | assert(0 && "Unknown spv_target_env in spvOpcodeTableGet()"); |
| 117 | return SPV_ERROR_INVALID_TABLE; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | spv_result_t spvOpcodeTableNameLookup(const spv_opcode_table table, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 121 | const char* name, |
| 122 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 123 | if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER; |
| 124 | if (!table) return SPV_ERROR_INVALID_TABLE; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 125 | |
| 126 | // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be |
| 127 | // preferable but the table requires sorting on the Opcode name, but it's |
| 128 | // static |
| 129 | // const initialized and matches the order of the spec. |
| 130 | const size_t nameLength = strlen(name); |
| 131 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
| 132 | if (nameLength == strlen(table->entries[opcodeIndex].name) && |
| 133 | !strncmp(name, table->entries[opcodeIndex].name, nameLength)) { |
| 134 | // NOTE: Found out Opcode! |
| 135 | *pEntry = &table->entries[opcodeIndex]; |
| 136 | return SPV_SUCCESS; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return SPV_ERROR_INVALID_LOOKUP; |
| 141 | } |
| 142 | |
| 143 | spv_result_t spvOpcodeTableValueLookup(const spv_opcode_table table, |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 144 | const SpvOp opcode, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 145 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 146 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 147 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 148 | |
Jesus Carabano | f063f91 | 2017-10-27 15:28:50 +0300 | [diff] [blame] | 149 | const auto beg = table->entries; |
| 150 | const auto end = table->entries + table->count; |
| 151 | spv_opcode_desc_t value{"", opcode, 0, nullptr, 0, {}, 0, 0}; |
| 152 | auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) { |
| 153 | return lhs.opcode < rhs.opcode; |
| 154 | }; |
| 155 | auto it = std::lower_bound(beg, end, value, comp); |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 156 | if (it != end && it->opcode == opcode) { |
Jesus Carabano | f063f91 | 2017-10-27 15:28:50 +0300 | [diff] [blame] | 157 | *pEntry = it; |
| 158 | return SPV_SUCCESS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return SPV_ERROR_INVALID_LOOKUP; |
| 162 | } |
| 163 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 164 | void spvInstructionCopy(const uint32_t* words, const SpvOp opcode, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 165 | const uint16_t wordCount, const spv_endianness_t endian, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 166 | spv_instruction_t* pInst) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 167 | pInst->opcode = opcode; |
David Neto | b5dc8fc | 2015-10-06 16:22:00 -0400 | [diff] [blame] | 168 | pInst->words.resize(wordCount); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 169 | for (uint16_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) { |
| 170 | pInst->words[wordIndex] = spvFixWord(words[wordIndex], endian); |
| 171 | if (!wordIndex) { |
| 172 | uint16_t thisWordCount; |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 173 | uint16_t thisOpcode; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 174 | spvOpcodeSplit(pInst->words[wordIndex], &thisWordCount, &thisOpcode); |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 175 | assert(opcode == static_cast<SpvOp>(thisOpcode) && |
| 176 | wordCount == thisWordCount && "Endianness failed!"); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 181 | const char* spvOpcodeString(const SpvOp opcode) { |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 182 | // Use the latest SPIR-V version, which should be backward-compatible with all |
| 183 | // previous ones. |
Jesus Carabano | f063f91 | 2017-10-27 15:28:50 +0300 | [diff] [blame] | 184 | |
| 185 | const auto beg = kOpcodeTableEntries_1_2; |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 186 | const auto end = |
| 187 | kOpcodeTableEntries_1_2 + ARRAY_SIZE(kOpcodeTableEntries_1_2); |
Jesus Carabano | f063f91 | 2017-10-27 15:28:50 +0300 | [diff] [blame] | 188 | spv_opcode_desc_t value{"", opcode, 0, nullptr, 0, {}, 0, 0}; |
| 189 | auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) { |
| 190 | return lhs.opcode < rhs.opcode; |
| 191 | }; |
| 192 | auto it = std::lower_bound(beg, end, value, comp); |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 193 | if (it != end && it->opcode == opcode) { |
Jesus Carabano | f063f91 | 2017-10-27 15:28:50 +0300 | [diff] [blame] | 194 | return it->name; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 195 | } |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 196 | |
Lei Zhang | 4f293b7 | 2016-03-21 16:36:14 -0400 | [diff] [blame] | 197 | assert(0 && "Unreachable!"); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 198 | return "unknown"; |
| 199 | } |
| 200 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 201 | int32_t spvOpcodeIsScalarType(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 202 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 203 | case SpvOpTypeInt: |
| 204 | case SpvOpTypeFloat: |
Dejan Mircevski | 276a724 | 2016-01-21 15:55:43 -0500 | [diff] [blame] | 205 | case SpvOpTypeBool: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 206 | return true; |
| 207 | default: |
| 208 | return false; |
| 209 | } |
| 210 | } |
| 211 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 212 | int32_t spvOpcodeIsConstant(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 213 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 214 | case SpvOpConstantTrue: |
| 215 | case SpvOpConstantFalse: |
| 216 | case SpvOpConstant: |
| 217 | case SpvOpConstantComposite: |
| 218 | case SpvOpConstantSampler: |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 219 | case SpvOpConstantNull: |
| 220 | case SpvOpSpecConstantTrue: |
| 221 | case SpvOpSpecConstantFalse: |
| 222 | case SpvOpSpecConstant: |
| 223 | case SpvOpSpecConstantComposite: |
Dejan Mircevski | 3fb2676 | 2016-04-04 15:55:05 -0400 | [diff] [blame] | 224 | case SpvOpSpecConstantOp: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 225 | return true; |
| 226 | default: |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | |
David Neto | 1f3fb50 | 2016-09-14 11:57:20 -0400 | [diff] [blame] | 231 | bool spvOpcodeIsConstantOrUndef(const SpvOp opcode) { |
| 232 | return opcode == SpvOpUndef || spvOpcodeIsConstant(opcode); |
| 233 | } |
| 234 | |
Aliya Pazylbekova | edb5264 | 2017-02-24 20:43:28 -0500 | [diff] [blame] | 235 | bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode) { |
| 236 | switch (opcode) { |
| 237 | case SpvOpSpecConstantTrue: |
| 238 | case SpvOpSpecConstantFalse: |
| 239 | case SpvOpSpecConstant: |
| 240 | return true; |
| 241 | default: |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 246 | int32_t spvOpcodeIsComposite(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 247 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 248 | case SpvOpTypeVector: |
| 249 | case SpvOpTypeMatrix: |
| 250 | case SpvOpTypeArray: |
| 251 | case SpvOpTypeStruct: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 252 | return true; |
| 253 | default: |
| 254 | return false; |
| 255 | } |
| 256 | } |
| 257 | |
Ehsan Nasiri | 23af06c | 2017-02-01 15:37:39 -0500 | [diff] [blame] | 258 | bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode) { |
| 259 | switch (opcode) { |
| 260 | case SpvOpVariable: |
| 261 | case SpvOpAccessChain: |
| 262 | case SpvOpInBoundsAccessChain: |
| 263 | case SpvOpFunctionParameter: |
| 264 | case SpvOpImageTexelPointer: |
| 265 | case SpvOpCopyObject: |
| 266 | case SpvOpSelect: |
| 267 | case SpvOpPhi: |
| 268 | case SpvOpFunctionCall: |
| 269 | case SpvOpPtrAccessChain: |
| 270 | case SpvOpLoad: |
| 271 | case SpvOpConstantNull: |
| 272 | return true; |
| 273 | default: |
| 274 | return false; |
| 275 | } |
| 276 | } |
| 277 | |
Florian Ziesche | 66fcb45 | 2016-03-02 22:17:54 +0100 | [diff] [blame] | 278 | int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 279 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 280 | case SpvOpVariable: |
| 281 | case SpvOpAccessChain: |
| 282 | case SpvOpInBoundsAccessChain: |
| 283 | case SpvOpFunctionParameter: |
Florian Ziesche | 66fcb45 | 2016-03-02 22:17:54 +0100 | [diff] [blame] | 284 | case SpvOpImageTexelPointer: |
| 285 | case SpvOpCopyObject: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 286 | return true; |
| 287 | default: |
| 288 | return false; |
| 289 | } |
| 290 | } |
| 291 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 292 | int32_t spvOpcodeGeneratesType(SpvOp op) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 293 | switch (op) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 294 | case SpvOpTypeVoid: |
| 295 | case SpvOpTypeBool: |
| 296 | case SpvOpTypeInt: |
| 297 | case SpvOpTypeFloat: |
| 298 | case SpvOpTypeVector: |
| 299 | case SpvOpTypeMatrix: |
| 300 | case SpvOpTypeImage: |
| 301 | case SpvOpTypeSampler: |
| 302 | case SpvOpTypeSampledImage: |
| 303 | case SpvOpTypeArray: |
| 304 | case SpvOpTypeRuntimeArray: |
| 305 | case SpvOpTypeStruct: |
| 306 | case SpvOpTypeOpaque: |
| 307 | case SpvOpTypePointer: |
| 308 | case SpvOpTypeFunction: |
| 309 | case SpvOpTypeEvent: |
| 310 | case SpvOpTypeDeviceEvent: |
| 311 | case SpvOpTypeReserveId: |
| 312 | case SpvOpTypeQueue: |
| 313 | case SpvOpTypePipe: |
Andrey Tuganov | 4ef3b3e | 2017-02-28 11:53:47 -0500 | [diff] [blame] | 314 | case SpvOpTypePipeStorage: |
| 315 | case SpvOpTypeNamedBarrier: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 316 | return true; |
David Neto | aef608c | 2015-11-02 14:59:02 -0500 | [diff] [blame] | 317 | default: |
| 318 | // In particular, OpTypeForwardPointer does not generate a type, |
| 319 | // but declares a storage class for a pointer type generated |
| 320 | // by a different instruction. |
| 321 | break; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 322 | } |
| 323 | return 0; |
| 324 | } |
Steven Perron | eb4653a | 2017-11-13 15:31:43 -0500 | [diff] [blame] | 325 | |
| 326 | bool spvOpcodeIsDecoration(const SpvOp opcode) { |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 327 | switch (opcode) { |
Steven Perron | eb4653a | 2017-11-13 15:31:43 -0500 | [diff] [blame] | 328 | case SpvOpDecorate: |
| 329 | case SpvOpDecorateId: |
| 330 | case SpvOpMemberDecorate: |
| 331 | case SpvOpGroupDecorate: |
| 332 | case SpvOpGroupMemberDecorate: |
| 333 | return true; |
| 334 | default: |
| 335 | break; |
| 336 | } |
| 337 | return false; |
| 338 | } |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 339 | |
| 340 | bool spvOpcodeIsLoad(const SpvOp opcode) { |
| 341 | switch (opcode) { |
| 342 | case SpvOpLoad: |
| 343 | case SpvOpImageSampleExplicitLod: |
| 344 | case SpvOpImageSampleImplicitLod: |
| 345 | case SpvOpImageSampleDrefImplicitLod: |
| 346 | case SpvOpImageSampleDrefExplicitLod: |
| 347 | case SpvOpImageSampleProjImplicitLod: |
| 348 | case SpvOpImageSampleProjExplicitLod: |
| 349 | case SpvOpImageSampleProjDrefImplicitLod: |
| 350 | case SpvOpImageSampleProjDrefExplicitLod: |
| 351 | case SpvOpImageFetch: |
| 352 | case SpvOpImageGather: |
| 353 | case SpvOpImageDrefGather: |
| 354 | case SpvOpImageRead: |
| 355 | case SpvOpImageSparseSampleImplicitLod: |
| 356 | case SpvOpImageSparseSampleExplicitLod: |
| 357 | case SpvOpImageSparseSampleDrefExplicitLod: |
| 358 | case SpvOpImageSparseSampleDrefImplicitLod: |
| 359 | case SpvOpImageSparseFetch: |
| 360 | case SpvOpImageSparseGather: |
| 361 | case SpvOpImageSparseDrefGather: |
| 362 | case SpvOpImageSparseRead: |
| 363 | return true; |
| 364 | default: |
| 365 | return false; |
| 366 | } |
| 367 | } |
| 368 | |
Diego Novillo | 7432784 | 2017-11-17 08:59:25 -0500 | [diff] [blame] | 369 | bool spvOpcodeIsBranch(SpvOp opcode) { |
| 370 | switch (opcode) { |
| 371 | case SpvOpBranch: |
| 372 | case SpvOpBranchConditional: |
| 373 | case SpvOpSwitch: |
| 374 | return true; |
| 375 | default: |
| 376 | return false; |
| 377 | } |
| 378 | } |
| 379 | |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 380 | bool spvOpcodeIsAtomicOp(const SpvOp opcode) { |
| 381 | switch (opcode) { |
| 382 | case SpvOpAtomicLoad: |
| 383 | case SpvOpAtomicStore: |
| 384 | case SpvOpAtomicExchange: |
| 385 | case SpvOpAtomicCompareExchange: |
| 386 | case SpvOpAtomicCompareExchangeWeak: |
| 387 | case SpvOpAtomicIIncrement: |
| 388 | case SpvOpAtomicIDecrement: |
| 389 | case SpvOpAtomicIAdd: |
| 390 | case SpvOpAtomicISub: |
| 391 | case SpvOpAtomicSMin: |
| 392 | case SpvOpAtomicUMin: |
| 393 | case SpvOpAtomicSMax: |
| 394 | case SpvOpAtomicUMax: |
| 395 | case SpvOpAtomicAnd: |
| 396 | case SpvOpAtomicOr: |
| 397 | case SpvOpAtomicXor: |
| 398 | case SpvOpAtomicFlagTestAndSet: |
| 399 | case SpvOpAtomicFlagClear: |
| 400 | return true; |
| 401 | default: |
| 402 | return false; |
| 403 | } |
| 404 | } |
Diego Novillo | 7432784 | 2017-11-17 08:59:25 -0500 | [diff] [blame] | 405 | |
| 406 | bool spvOpcodeIsReturn(SpvOp opcode) { |
| 407 | switch (opcode) { |
| 408 | case SpvOpReturn: |
| 409 | case SpvOpReturnValue: |
| 410 | return true; |
| 411 | default: |
| 412 | return false; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | bool spvOpcodeIsBlockTerminator(SpvOp opcode) { |
| 417 | return spvOpcodeIsBranch(opcode) || spvOpcodeIsReturn(opcode) || |
| 418 | opcode == SpvOpKill || opcode == SpvOpUnreachable; |
| 419 | } |