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 | |
David Neto | 00fa393 | 2018-02-09 14:29:02 -0500 | [diff] [blame] | 35 | #include "core.insts-unified1.inc" // defines kOpcodeTableEntries_1_3 |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 36 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 37 | static const spv_opcode_table_t kOpcodeTable = {ARRAY_SIZE(kOpcodeTableEntries), |
| 38 | kOpcodeTableEntries}; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 39 | |
David Neto | 5a0b5ca | 2016-12-09 14:01:43 -0500 | [diff] [blame] | 40 | // Represents a vendor tool entry in the SPIR-V XML Regsitry. |
| 41 | struct VendorTool { |
| 42 | uint32_t value; |
| 43 | const char* vendor; |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 44 | const char* tool; // Might be empty string. |
| 45 | const char* vendor_tool; // Combiantion of vendor and tool. |
David Neto | 5a0b5ca | 2016-12-09 14:01:43 -0500 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | const VendorTool vendor_tools[] = { |
| 49 | #include "generators.inc" |
| 50 | }; |
| 51 | |
Lei Zhang | a94701d | 2015-09-14 10:05:37 -0400 | [diff] [blame] | 52 | } // anonymous namespace |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 53 | |
David Neto | 5a0b5ca | 2016-12-09 14:01:43 -0500 | [diff] [blame] | 54 | // TODO(dneto): Move this to another file. It doesn't belong with opcode |
| 55 | // processing. |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 56 | const char* spvGeneratorStr(uint32_t generator) { |
David Neto | 5a0b5ca | 2016-12-09 14:01:43 -0500 | [diff] [blame] | 57 | auto where = std::find_if( |
| 58 | std::begin(vendor_tools), std::end(vendor_tools), |
| 59 | [generator](const VendorTool& vt) { return generator == vt.value; }); |
| 60 | if (where != std::end(vendor_tools)) return where->vendor_tool; |
| 61 | return "Unknown"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 64 | uint32_t spvOpcodeMake(uint16_t wordCount, SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 65 | return ((uint32_t)opcode) | (((uint32_t)wordCount) << 16); |
| 66 | } |
| 67 | |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 68 | void spvOpcodeSplit(const uint32_t word, uint16_t* pWordCount, |
| 69 | uint16_t* pOpcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 70 | if (pWordCount) { |
| 71 | *pWordCount = (uint16_t)((0xffff0000 & word) >> 16); |
| 72 | } |
| 73 | if (pOpcode) { |
Lei Zhang | 6fa3f8a | 2016-03-31 17:26:31 -0400 | [diff] [blame] | 74 | *pOpcode = 0x0000ffff & word; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 78 | spv_result_t spvOpcodeTableGet(spv_opcode_table* pInstTable, spv_target_env) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 79 | if (!pInstTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 80 | |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 81 | // Descriptions of each opcode. Each entry describes the format of the |
| 82 | // instruction that follows a particular opcode. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 83 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 84 | *pInstTable = &kOpcodeTable; |
| 85 | return SPV_SUCCESS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 88 | spv_result_t spvOpcodeTableNameLookup(spv_target_env env, |
| 89 | const spv_opcode_table table, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 90 | const char* name, |
| 91 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 92 | if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER; |
| 93 | if (!table) return SPV_ERROR_INVALID_TABLE; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 94 | |
| 95 | // TODO: This lookup of the Opcode table is suboptimal! Binary sort would be |
| 96 | // preferable but the table requires sorting on the Opcode name, but it's |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 97 | // static const initialized and matches the order of the spec. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 98 | const size_t nameLength = strlen(name); |
| 99 | for (uint64_t opcodeIndex = 0; opcodeIndex < table->count; ++opcodeIndex) { |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 100 | const spv_opcode_desc_t& entry = table->entries[opcodeIndex]; |
| 101 | // We considers the current opcode as available as long as |
| 102 | // 1. The target environment satisfies the minimal requirement of the |
| 103 | // opcode; or |
| 104 | // 2. There is at least one extension enabling this opcode. |
| 105 | // |
| 106 | // Note that the second rule assumes the extension enabling this instruction |
| 107 | // is indeed requested in the SPIR-V code; checking that should be |
| 108 | // validator's work. |
| 109 | if ((static_cast<uint32_t>(env) >= entry.minVersion || |
| 110 | entry.numExtensions > 0u) && |
| 111 | nameLength == strlen(entry.name) && |
| 112 | !strncmp(name, entry.name, nameLength)) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 113 | // NOTE: Found out Opcode! |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 114 | *pEntry = &entry; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 115 | return SPV_SUCCESS; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return SPV_ERROR_INVALID_LOOKUP; |
| 120 | } |
| 121 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 122 | spv_result_t spvOpcodeTableValueLookup(spv_target_env env, |
| 123 | const spv_opcode_table table, |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 124 | const SpvOp opcode, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 125 | spv_opcode_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 126 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 127 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 128 | |
Jesus Carabano | f063f91 | 2017-10-27 15:28:50 +0300 | [diff] [blame] | 129 | const auto beg = table->entries; |
| 130 | const auto end = table->entries + table->count; |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 131 | |
| 132 | spv_opcode_desc_t needle = {"", opcode, 0, nullptr, 0, {}, |
| 133 | false, false, 0, nullptr, ~0u}; |
| 134 | |
Jesus Carabano | f063f91 | 2017-10-27 15:28:50 +0300 | [diff] [blame] | 135 | auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) { |
| 136 | return lhs.opcode < rhs.opcode; |
| 137 | }; |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 138 | |
| 139 | // We need to loop here because there can exist multiple symbols for the same |
| 140 | // opcode value, and they can be introduced in different target environments, |
| 141 | // which means they can have different minimal version requirements. |
| 142 | // Assumes the underlying table is already sorted ascendingly according to |
| 143 | // opcode value. |
| 144 | for (auto it = std::lower_bound(beg, end, needle, comp); |
| 145 | it != end && it->opcode == opcode; ++it) { |
| 146 | // We considers the current opcode as available as long as |
| 147 | // 1. The target environment satisfies the minimal requirement of the |
| 148 | // opcode; or |
| 149 | // 2. There is at least one extension enabling this opcode. |
| 150 | // |
| 151 | // Note that the second rule assumes the extension enabling this instruction |
| 152 | // is indeed requested in the SPIR-V code; checking that should be |
| 153 | // validator's work. |
| 154 | if (static_cast<uint32_t>(env) >= it->minVersion || |
| 155 | it->numExtensions > 0u) { |
| 156 | *pEntry = it; |
| 157 | return SPV_SUCCESS; |
| 158 | } |
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) { |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 182 | const auto beg = kOpcodeTableEntries; |
| 183 | const auto end = kOpcodeTableEntries + ARRAY_SIZE(kOpcodeTableEntries); |
| 184 | spv_opcode_desc_t needle = {"", opcode, 0, nullptr, 0, {}, |
| 185 | false, false, 0, nullptr, ~0u}; |
Jesus Carabano | f063f91 | 2017-10-27 15:28:50 +0300 | [diff] [blame] | 186 | auto comp = [](const spv_opcode_desc_t& lhs, const spv_opcode_desc_t& rhs) { |
| 187 | return lhs.opcode < rhs.opcode; |
| 188 | }; |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame^] | 189 | auto it = std::lower_bound(beg, end, needle, comp); |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 190 | if (it != end && it->opcode == opcode) { |
Jesus Carabano | f063f91 | 2017-10-27 15:28:50 +0300 | [diff] [blame] | 191 | return it->name; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 192 | } |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 193 | |
Lei Zhang | 4f293b7 | 2016-03-21 16:36:14 -0400 | [diff] [blame] | 194 | assert(0 && "Unreachable!"); |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 195 | return "unknown"; |
| 196 | } |
| 197 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 198 | int32_t spvOpcodeIsScalarType(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 199 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 200 | case SpvOpTypeInt: |
| 201 | case SpvOpTypeFloat: |
Dejan Mircevski | 276a724 | 2016-01-21 15:55:43 -0500 | [diff] [blame] | 202 | case SpvOpTypeBool: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 203 | return true; |
| 204 | default: |
| 205 | return false; |
| 206 | } |
| 207 | } |
| 208 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 209 | int32_t spvOpcodeIsConstant(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 210 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 211 | case SpvOpConstantTrue: |
| 212 | case SpvOpConstantFalse: |
| 213 | case SpvOpConstant: |
| 214 | case SpvOpConstantComposite: |
| 215 | case SpvOpConstantSampler: |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 216 | case SpvOpConstantNull: |
| 217 | case SpvOpSpecConstantTrue: |
| 218 | case SpvOpSpecConstantFalse: |
| 219 | case SpvOpSpecConstant: |
| 220 | case SpvOpSpecConstantComposite: |
Dejan Mircevski | 3fb2676 | 2016-04-04 15:55:05 -0400 | [diff] [blame] | 221 | case SpvOpSpecConstantOp: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 222 | return true; |
| 223 | default: |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | |
David Neto | 1f3fb50 | 2016-09-14 11:57:20 -0400 | [diff] [blame] | 228 | bool spvOpcodeIsConstantOrUndef(const SpvOp opcode) { |
| 229 | return opcode == SpvOpUndef || spvOpcodeIsConstant(opcode); |
| 230 | } |
| 231 | |
Aliya Pazylbekova | edb5264 | 2017-02-24 20:43:28 -0500 | [diff] [blame] | 232 | bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode) { |
| 233 | switch (opcode) { |
| 234 | case SpvOpSpecConstantTrue: |
| 235 | case SpvOpSpecConstantFalse: |
| 236 | case SpvOpSpecConstant: |
| 237 | return true; |
| 238 | default: |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 243 | int32_t spvOpcodeIsComposite(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 244 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 245 | case SpvOpTypeVector: |
| 246 | case SpvOpTypeMatrix: |
| 247 | case SpvOpTypeArray: |
| 248 | case SpvOpTypeStruct: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 249 | return true; |
| 250 | default: |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | |
Ehsan Nasiri | 23af06c | 2017-02-01 15:37:39 -0500 | [diff] [blame] | 255 | bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode) { |
| 256 | switch (opcode) { |
| 257 | case SpvOpVariable: |
| 258 | case SpvOpAccessChain: |
| 259 | case SpvOpInBoundsAccessChain: |
| 260 | case SpvOpFunctionParameter: |
| 261 | case SpvOpImageTexelPointer: |
| 262 | case SpvOpCopyObject: |
| 263 | case SpvOpSelect: |
| 264 | case SpvOpPhi: |
| 265 | case SpvOpFunctionCall: |
| 266 | case SpvOpPtrAccessChain: |
| 267 | case SpvOpLoad: |
| 268 | case SpvOpConstantNull: |
| 269 | return true; |
| 270 | default: |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
Florian Ziesche | 66fcb45 | 2016-03-02 22:17:54 +0100 | [diff] [blame] | 275 | int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 276 | switch (opcode) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 277 | case SpvOpVariable: |
| 278 | case SpvOpAccessChain: |
| 279 | case SpvOpInBoundsAccessChain: |
| 280 | case SpvOpFunctionParameter: |
Florian Ziesche | 66fcb45 | 2016-03-02 22:17:54 +0100 | [diff] [blame] | 281 | case SpvOpImageTexelPointer: |
| 282 | case SpvOpCopyObject: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 283 | return true; |
| 284 | default: |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 289 | int32_t spvOpcodeGeneratesType(SpvOp op) { |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 290 | switch (op) { |
Lei Zhang | b36e704 | 2015-10-28 13:40:52 -0400 | [diff] [blame] | 291 | case SpvOpTypeVoid: |
| 292 | case SpvOpTypeBool: |
| 293 | case SpvOpTypeInt: |
| 294 | case SpvOpTypeFloat: |
| 295 | case SpvOpTypeVector: |
| 296 | case SpvOpTypeMatrix: |
| 297 | case SpvOpTypeImage: |
| 298 | case SpvOpTypeSampler: |
| 299 | case SpvOpTypeSampledImage: |
| 300 | case SpvOpTypeArray: |
| 301 | case SpvOpTypeRuntimeArray: |
| 302 | case SpvOpTypeStruct: |
| 303 | case SpvOpTypeOpaque: |
| 304 | case SpvOpTypePointer: |
| 305 | case SpvOpTypeFunction: |
| 306 | case SpvOpTypeEvent: |
| 307 | case SpvOpTypeDeviceEvent: |
| 308 | case SpvOpTypeReserveId: |
| 309 | case SpvOpTypeQueue: |
| 310 | case SpvOpTypePipe: |
Andrey Tuganov | 4ef3b3e | 2017-02-28 11:53:47 -0500 | [diff] [blame] | 311 | case SpvOpTypePipeStorage: |
| 312 | case SpvOpTypeNamedBarrier: |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 313 | return true; |
David Neto | aef608c | 2015-11-02 14:59:02 -0500 | [diff] [blame] | 314 | default: |
| 315 | // In particular, OpTypeForwardPointer does not generate a type, |
| 316 | // but declares a storage class for a pointer type generated |
| 317 | // by a different instruction. |
| 318 | break; |
Andrew Woloszyn | 537e776 | 2015-09-29 11:28:34 -0400 | [diff] [blame] | 319 | } |
| 320 | return 0; |
| 321 | } |
Steven Perron | eb4653a | 2017-11-13 15:31:43 -0500 | [diff] [blame] | 322 | |
| 323 | bool spvOpcodeIsDecoration(const SpvOp opcode) { |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 324 | switch (opcode) { |
Steven Perron | eb4653a | 2017-11-13 15:31:43 -0500 | [diff] [blame] | 325 | case SpvOpDecorate: |
| 326 | case SpvOpDecorateId: |
| 327 | case SpvOpMemberDecorate: |
| 328 | case SpvOpGroupDecorate: |
| 329 | case SpvOpGroupMemberDecorate: |
David Neto | 5f69f75 | 2018-03-05 13:34:13 -0500 | [diff] [blame] | 330 | case SpvOpDecorateStringGOOGLE: |
| 331 | case SpvOpMemberDecorateStringGOOGLE: |
Steven Perron | eb4653a | 2017-11-13 15:31:43 -0500 | [diff] [blame] | 332 | return true; |
| 333 | default: |
| 334 | break; |
| 335 | } |
| 336 | return false; |
| 337 | } |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 338 | |
| 339 | bool spvOpcodeIsLoad(const SpvOp opcode) { |
| 340 | switch (opcode) { |
| 341 | case SpvOpLoad: |
| 342 | case SpvOpImageSampleExplicitLod: |
| 343 | case SpvOpImageSampleImplicitLod: |
| 344 | case SpvOpImageSampleDrefImplicitLod: |
| 345 | case SpvOpImageSampleDrefExplicitLod: |
| 346 | case SpvOpImageSampleProjImplicitLod: |
| 347 | case SpvOpImageSampleProjExplicitLod: |
| 348 | case SpvOpImageSampleProjDrefImplicitLod: |
| 349 | case SpvOpImageSampleProjDrefExplicitLod: |
| 350 | case SpvOpImageFetch: |
| 351 | case SpvOpImageGather: |
| 352 | case SpvOpImageDrefGather: |
| 353 | case SpvOpImageRead: |
| 354 | case SpvOpImageSparseSampleImplicitLod: |
| 355 | case SpvOpImageSparseSampleExplicitLod: |
| 356 | case SpvOpImageSparseSampleDrefExplicitLod: |
| 357 | case SpvOpImageSparseSampleDrefImplicitLod: |
| 358 | case SpvOpImageSparseFetch: |
| 359 | case SpvOpImageSparseGather: |
| 360 | case SpvOpImageSparseDrefGather: |
| 361 | case SpvOpImageSparseRead: |
| 362 | return true; |
| 363 | default: |
| 364 | return false; |
| 365 | } |
| 366 | } |
| 367 | |
Diego Novillo | 7432784 | 2017-11-17 08:59:25 -0500 | [diff] [blame] | 368 | bool spvOpcodeIsBranch(SpvOp opcode) { |
| 369 | switch (opcode) { |
| 370 | case SpvOpBranch: |
| 371 | case SpvOpBranchConditional: |
| 372 | case SpvOpSwitch: |
| 373 | return true; |
| 374 | default: |
| 375 | return false; |
| 376 | } |
| 377 | } |
| 378 | |
Steven Perron | 28c4155 | 2017-11-10 20:26:55 -0500 | [diff] [blame] | 379 | bool spvOpcodeIsAtomicOp(const SpvOp opcode) { |
| 380 | switch (opcode) { |
| 381 | case SpvOpAtomicLoad: |
| 382 | case SpvOpAtomicStore: |
| 383 | case SpvOpAtomicExchange: |
| 384 | case SpvOpAtomicCompareExchange: |
| 385 | case SpvOpAtomicCompareExchangeWeak: |
| 386 | case SpvOpAtomicIIncrement: |
| 387 | case SpvOpAtomicIDecrement: |
| 388 | case SpvOpAtomicIAdd: |
| 389 | case SpvOpAtomicISub: |
| 390 | case SpvOpAtomicSMin: |
| 391 | case SpvOpAtomicUMin: |
| 392 | case SpvOpAtomicSMax: |
| 393 | case SpvOpAtomicUMax: |
| 394 | case SpvOpAtomicAnd: |
| 395 | case SpvOpAtomicOr: |
| 396 | case SpvOpAtomicXor: |
| 397 | case SpvOpAtomicFlagTestAndSet: |
| 398 | case SpvOpAtomicFlagClear: |
| 399 | return true; |
| 400 | default: |
| 401 | return false; |
| 402 | } |
| 403 | } |
Diego Novillo | 7432784 | 2017-11-17 08:59:25 -0500 | [diff] [blame] | 404 | |
| 405 | bool spvOpcodeIsReturn(SpvOp opcode) { |
| 406 | switch (opcode) { |
| 407 | case SpvOpReturn: |
| 408 | case SpvOpReturnValue: |
| 409 | return true; |
| 410 | default: |
| 411 | return false; |
| 412 | } |
| 413 | } |
| 414 | |
Diego Novillo | 5f10078 | 2018-01-03 15:25:03 -0500 | [diff] [blame] | 415 | bool spvOpcodeIsReturnOrAbort(SpvOp opcode) { |
| 416 | return spvOpcodeIsReturn(opcode) || opcode == SpvOpKill || |
| 417 | opcode == SpvOpUnreachable; |
| 418 | } |
| 419 | |
Diego Novillo | 7432784 | 2017-11-17 08:59:25 -0500 | [diff] [blame] | 420 | bool spvOpcodeIsBlockTerminator(SpvOp opcode) { |
Diego Novillo | 5f10078 | 2018-01-03 15:25:03 -0500 | [diff] [blame] | 421 | return spvOpcodeIsBranch(opcode) || spvOpcodeIsReturnOrAbort(opcode); |
Diego Novillo | 7432784 | 2017-11-17 08:59:25 -0500 | [diff] [blame] | 422 | } |
Steven Perron | 79a0064 | 2017-12-11 13:10:24 -0500 | [diff] [blame] | 423 | |
| 424 | bool spvOpcodeIsBaseOpaqueType(SpvOp opcode) { |
| 425 | switch (opcode) { |
| 426 | case SpvOpTypeImage: |
| 427 | case SpvOpTypeSampler: |
| 428 | case SpvOpTypeSampledImage: |
| 429 | case SpvOpTypeOpaque: |
| 430 | case SpvOpTypeEvent: |
| 431 | case SpvOpTypeDeviceEvent: |
| 432 | case SpvOpTypeReserveId: |
| 433 | case SpvOpTypeQueue: |
| 434 | case SpvOpTypePipe: |
| 435 | case SpvOpTypeForwardPointer: |
| 436 | case SpvOpTypePipeStorage: |
| 437 | case SpvOpTypeNamedBarrier: |
| 438 | return true; |
| 439 | default: |
| 440 | return false; |
| 441 | } |
| 442 | } |