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 | |
| 15 | #include "operand.h" |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 20 | |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 21 | #include "macro.h" |
David Neto | ba73a7c | 2016-01-06 13:08:39 -0500 | [diff] [blame] | 22 | |
David Neto | 00fa393 | 2018-02-09 14:29:02 -0500 | [diff] [blame] | 23 | // For now, assume unified1 contains up to SPIR-V 1.3 and no later |
| 24 | // SPIR-V version. |
| 25 | // TODO(dneto): Make one set of tables, but with version tags on a |
| 26 | // per-item basis. https://github.com/KhronosGroup/SPIRV-Tools/issues/1195 |
| 27 | |
David Neto | 00fa393 | 2018-02-09 14:29:02 -0500 | [diff] [blame] | 28 | #include "operand.kinds-unified1.inc" |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 29 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 30 | static const spv_operand_table_t kOperandTable = { |
| 31 | ARRAY_SIZE(pygen_variable_OperandInfoTable), |
| 32 | pygen_variable_OperandInfoTable}; |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 33 | |
| 34 | spv_result_t spvOperandTableGet(spv_operand_table* pOperandTable, |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 35 | spv_target_env) { |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 36 | if (!pOperandTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 37 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 38 | *pOperandTable = &kOperandTable; |
| 39 | return SPV_SUCCESS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 42 | spv_result_t spvOperandTableNameLookup(spv_target_env env, |
| 43 | const spv_operand_table table, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 44 | const spv_operand_type_t type, |
David Neto | 388c40d | 2015-09-16 16:42:56 -0400 | [diff] [blame] | 45 | const char* name, |
| 46 | const size_t nameLength, |
| 47 | spv_operand_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 48 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 49 | if (!name || !pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 50 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 51 | for (uint64_t typeIndex = 0; typeIndex < table->count; ++typeIndex) { |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 52 | const auto& group = table->types[typeIndex]; |
| 53 | if (type != group.type) continue; |
| 54 | for (uint64_t index = 0; index < group.count; ++index) { |
| 55 | const auto& entry = group.entries[index]; |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 56 | // We considers the current operand as available as long as |
| 57 | // 1. The target environment satisfies the minimal requirement of the |
| 58 | // operand; or |
| 59 | // 2. There is at least one extension enabling this operand. |
| 60 | // |
| 61 | // Note that the second rule assumes the extension enabling this operand |
| 62 | // is indeed requested in the SPIR-V code; checking that should be |
| 63 | // validator's work. |
| 64 | if ((static_cast<uint32_t>(env) >= entry.minVersion || |
| 65 | entry.numExtensions > 0u) && |
| 66 | nameLength == strlen(entry.name) && |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 67 | !strncmp(entry.name, name, nameLength)) { |
| 68 | *pEntry = &entry; |
| 69 | return SPV_SUCCESS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return SPV_ERROR_INVALID_LOOKUP; |
| 75 | } |
| 76 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 77 | spv_result_t spvOperandTableValueLookup(spv_target_env env, |
| 78 | const spv_operand_table table, |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 79 | const spv_operand_type_t type, |
| 80 | const uint32_t value, |
Dejan Mircevski | 50babb2 | 2015-09-29 10:56:32 -0400 | [diff] [blame] | 81 | spv_operand_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 82 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 83 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 84 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 85 | spv_operand_desc_t needle = {"", value, 0, nullptr, 0, nullptr, {}, ~0u}; |
| 86 | |
| 87 | auto comp = [](const spv_operand_desc_t& lhs, const spv_operand_desc_t& rhs) { |
| 88 | return lhs.value < rhs.value; |
| 89 | }; |
| 90 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 91 | for (uint64_t typeIndex = 0; typeIndex < table->count; ++typeIndex) { |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 92 | const auto& group = table->types[typeIndex]; |
| 93 | if (type != group.type) continue; |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 94 | |
| 95 | const auto beg = group.entries; |
| 96 | const auto end = group.entries + group.count; |
| 97 | |
| 98 | // We need to loop here because there can exist multiple symbols for the |
| 99 | // same operand value, and they can be introduced in different target |
| 100 | // environments, which means they can have different minimal version |
| 101 | // requirements. For example, SubgroupEqMaskKHR can exist in any SPIR-V |
| 102 | // version as long as the SPV_KHR_shader_ballot extension is there; but |
| 103 | // starting from SPIR-V 1.3, SubgroupEqMask, which has the same numeric |
| 104 | // value as SubgroupEqMaskKHR, is available in core SPIR-V without extension |
| 105 | // requirements. |
| 106 | // Assumes the underlying table is already sorted ascendingly according to |
| 107 | // opcode value. |
| 108 | for (auto it = std::lower_bound(beg, end, needle, comp); |
| 109 | it != end && it->value == value; ++it) { |
| 110 | // We considers the current operand as available as long as |
| 111 | // 1. The target environment satisfies the minimal requirement of the |
| 112 | // operand; or |
| 113 | // 2. There is at least one extension enabling this operand. |
| 114 | // |
| 115 | // Note that the second rule assumes the extension enabling this operand |
| 116 | // is indeed requested in the SPIR-V code; checking that should be |
| 117 | // validator's work. |
| 118 | if (static_cast<uint32_t>(env) >= it->minVersion || |
| 119 | it->numExtensions > 0u) { |
| 120 | *pEntry = it; |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 121 | return SPV_SUCCESS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return SPV_ERROR_INVALID_LOOKUP; |
| 127 | } |
| 128 | |
Dejan Mircevski | 50babb2 | 2015-09-29 10:56:32 -0400 | [diff] [blame] | 129 | const char* spvOperandTypeStr(spv_operand_type_t type) { |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 130 | switch (type) { |
| 131 | case SPV_OPERAND_TYPE_ID: |
David Neto | fadbf62 | 2015-09-14 17:07:11 -0400 | [diff] [blame] | 132 | case SPV_OPERAND_TYPE_OPTIONAL_ID: |
David Neto | fadbf62 | 2015-09-14 17:07:11 -0400 | [diff] [blame] | 133 | return "ID"; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 134 | case SPV_OPERAND_TYPE_TYPE_ID: |
| 135 | return "type ID"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 136 | case SPV_OPERAND_TYPE_RESULT_ID: |
| 137 | return "result ID"; |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 138 | case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 139 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER: |
| 140 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 141 | return "literal number"; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 142 | case SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER: |
| 143 | return "possibly multi-word literal integer"; |
| 144 | case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: |
| 145 | return "possibly multi-word literal number"; |
| 146 | case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: |
| 147 | return "extension instruction number"; |
David Neto | 0f166be | 2015-11-11 01:56:49 -0500 | [diff] [blame] | 148 | case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: |
| 149 | return "OpSpecConstantOp opcode"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 150 | case SPV_OPERAND_TYPE_LITERAL_STRING: |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 151 | case SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 152 | return "literal string"; |
| 153 | case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: |
Dejan Mircevski | d2c81cf | 2015-10-09 11:06:10 -0400 | [diff] [blame] | 154 | return "source language"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 155 | case SPV_OPERAND_TYPE_EXECUTION_MODEL: |
| 156 | return "execution model"; |
| 157 | case SPV_OPERAND_TYPE_ADDRESSING_MODEL: |
| 158 | return "addressing model"; |
| 159 | case SPV_OPERAND_TYPE_MEMORY_MODEL: |
| 160 | return "memory model"; |
| 161 | case SPV_OPERAND_TYPE_EXECUTION_MODE: |
| 162 | return "execution mode"; |
| 163 | case SPV_OPERAND_TYPE_STORAGE_CLASS: |
| 164 | return "storage class"; |
| 165 | case SPV_OPERAND_TYPE_DIMENSIONALITY: |
| 166 | return "dimensionality"; |
| 167 | case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE: |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 168 | return "sampler addressing mode"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 169 | case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE: |
| 170 | return "sampler filter mode"; |
David Neto | b30a0c5 | 2015-09-16 15:56:43 -0400 | [diff] [blame] | 171 | case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT: |
Dejan Mircevski | 971b344 | 2015-10-13 12:54:47 -0400 | [diff] [blame] | 172 | return "image format"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 173 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
Dejan Mircevski | 355cc0c | 2015-10-13 15:02:03 -0400 | [diff] [blame] | 174 | return "floating-point fast math mode"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 175 | case SPV_OPERAND_TYPE_FP_ROUNDING_MODE: |
Dejan Mircevski | 355cc0c | 2015-10-13 15:02:03 -0400 | [diff] [blame] | 176 | return "floating-point rounding mode"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 177 | case SPV_OPERAND_TYPE_LINKAGE_TYPE: |
| 178 | return "linkage type"; |
| 179 | case SPV_OPERAND_TYPE_ACCESS_QUALIFIER: |
David Neto | 2889a0c | 2016-02-15 13:50:00 -0500 | [diff] [blame] | 180 | case SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 181 | return "access qualifier"; |
| 182 | case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE: |
| 183 | return "function parameter attribute"; |
| 184 | case SPV_OPERAND_TYPE_DECORATION: |
| 185 | return "decoration"; |
| 186 | case SPV_OPERAND_TYPE_BUILT_IN: |
Dejan Mircevski | d7b0f83 | 2015-10-13 15:39:38 -0400 | [diff] [blame] | 187 | return "built-in"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 188 | case SPV_OPERAND_TYPE_SELECTION_CONTROL: |
| 189 | return "selection control"; |
| 190 | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
| 191 | return "loop control"; |
| 192 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 193 | return "function control"; |
David Neto | 64a9be9 | 2015-11-18 15:48:32 -0500 | [diff] [blame] | 194 | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 195 | return "memory semantics ID"; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 196 | case SPV_OPERAND_TYPE_MEMORY_ACCESS: |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 197 | case SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 198 | return "memory access"; |
David Neto | 64a9be9 | 2015-11-18 15:48:32 -0500 | [diff] [blame] | 199 | case SPV_OPERAND_TYPE_SCOPE_ID: |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 200 | return "scope ID"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 201 | case SPV_OPERAND_TYPE_GROUP_OPERATION: |
| 202 | return "group operation"; |
| 203 | case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS: |
| 204 | return "kernel enqeue flags"; |
David Neto | 4799482 | 2015-08-27 13:11:01 -0400 | [diff] [blame] | 205 | case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 206 | return "kernel profiling info"; |
| 207 | case SPV_OPERAND_TYPE_CAPABILITY: |
| 208 | return "capability"; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 209 | case SPV_OPERAND_TYPE_IMAGE: |
David Neto | ee1b3bb | 2015-09-18 11:19:18 -0400 | [diff] [blame] | 210 | case SPV_OPERAND_TYPE_OPTIONAL_IMAGE: |
David Neto | d9ad050 | 2015-11-24 18:37:24 -0500 | [diff] [blame] | 211 | return "image"; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 212 | case SPV_OPERAND_TYPE_OPTIONAL_CIV: |
| 213 | return "context-insensitive value"; |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 214 | case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: |
| 215 | return "debug info flags"; |
| 216 | case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
| 217 | return "debug base type encoding"; |
| 218 | case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE: |
| 219 | return "debug composite type"; |
| 220 | case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER: |
| 221 | return "debug type qualifier"; |
| 222 | case SPV_OPERAND_TYPE_DEBUG_OPERATION: |
| 223 | return "debug operation"; |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 224 | |
| 225 | // The next values are for values returned from an instruction, not actually |
| 226 | // an operand. So the specific strings don't matter. But let's add them |
| 227 | // for completeness and ease of testing. |
| 228 | case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER: |
| 229 | return "image channel order"; |
| 230 | case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE: |
| 231 | return "image channel data type"; |
| 232 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 233 | case SPV_OPERAND_TYPE_NONE: |
| 234 | return "NONE"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 235 | default: |
| 236 | assert(0 && "Unhandled operand type!"); |
| 237 | break; |
| 238 | } |
| 239 | return "unknown"; |
| 240 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 241 | |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 242 | void spvPushOperandTypes(const spv_operand_type_t* types, |
| 243 | spv_operand_pattern_t* pattern) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 244 | const spv_operand_type_t* endTypes; |
Dejan Mircevski | 50babb2 | 2015-09-29 10:56:32 -0400 | [diff] [blame] | 245 | for (endTypes = types; *endTypes != SPV_OPERAND_TYPE_NONE; ++endTypes) |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 246 | ; |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 247 | while (endTypes-- != types) { |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 248 | pattern->push_back(*endTypes); |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 249 | } |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 252 | void spvPushOperandTypesForMask(spv_target_env env, |
| 253 | const spv_operand_table operandTable, |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 254 | const spv_operand_type_t type, |
| 255 | const uint32_t mask, |
| 256 | spv_operand_pattern_t* pattern) { |
| 257 | // Scan from highest bits to lowest bits because we will append in LIFO |
| 258 | // fashion, and we need the operands for lower order bits to be consumed first |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 259 | for (uint32_t candidate_bit = (1u << 31u); candidate_bit; |
| 260 | candidate_bit >>= 1) { |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 261 | if (candidate_bit & mask) { |
| 262 | spv_operand_desc entry = nullptr; |
Lei Zhang | 1ef6b19 | 2018-03-14 13:06:18 -0400 | [diff] [blame] | 263 | if (SPV_SUCCESS == spvOperandTableValueLookup(env, operandTable, type, |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 264 | candidate_bit, &entry)) { |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 265 | spvPushOperandTypes(entry->operandTypes, pattern); |
David Neto | 5bf88fc | 2015-09-17 17:06:10 -0400 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
David Neto | 0dbe184 | 2017-12-03 14:26:16 -0500 | [diff] [blame] | 271 | bool spvOperandIsConcrete(spv_operand_type_t type) { |
| 272 | if (spvIsIdType(type) || spvOperandIsConcreteMask(type)) { |
| 273 | return true; |
| 274 | } |
| 275 | switch (type) { |
| 276 | case SPV_OPERAND_TYPE_LITERAL_INTEGER: |
| 277 | case SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER: |
| 278 | case SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER: |
| 279 | case SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER: |
| 280 | case SPV_OPERAND_TYPE_LITERAL_STRING: |
| 281 | case SPV_OPERAND_TYPE_SOURCE_LANGUAGE: |
| 282 | case SPV_OPERAND_TYPE_EXECUTION_MODEL: |
| 283 | case SPV_OPERAND_TYPE_ADDRESSING_MODEL: |
| 284 | case SPV_OPERAND_TYPE_MEMORY_MODEL: |
| 285 | case SPV_OPERAND_TYPE_EXECUTION_MODE: |
| 286 | case SPV_OPERAND_TYPE_STORAGE_CLASS: |
| 287 | case SPV_OPERAND_TYPE_DIMENSIONALITY: |
| 288 | case SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE: |
| 289 | case SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE: |
| 290 | case SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT: |
| 291 | case SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER: |
| 292 | case SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE: |
| 293 | case SPV_OPERAND_TYPE_FP_ROUNDING_MODE: |
| 294 | case SPV_OPERAND_TYPE_LINKAGE_TYPE: |
| 295 | case SPV_OPERAND_TYPE_ACCESS_QUALIFIER: |
| 296 | case SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE: |
| 297 | case SPV_OPERAND_TYPE_DECORATION: |
| 298 | case SPV_OPERAND_TYPE_BUILT_IN: |
| 299 | case SPV_OPERAND_TYPE_GROUP_OPERATION: |
| 300 | case SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS: |
| 301 | case SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO: |
| 302 | case SPV_OPERAND_TYPE_CAPABILITY: |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 303 | case SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING: |
| 304 | case SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE: |
| 305 | case SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER: |
| 306 | case SPV_OPERAND_TYPE_DEBUG_OPERATION: |
David Neto | 0dbe184 | 2017-12-03 14:26:16 -0500 | [diff] [blame] | 307 | return true; |
| 308 | default: |
| 309 | break; |
| 310 | } |
| 311 | return false; |
| 312 | } |
| 313 | |
David Neto | b526756 | 2016-02-02 12:05:34 -0500 | [diff] [blame] | 314 | bool spvOperandIsConcreteMask(spv_operand_type_t type) { |
David Neto | 0dbe184 | 2017-12-03 14:26:16 -0500 | [diff] [blame] | 315 | switch (type) { |
| 316 | case SPV_OPERAND_TYPE_IMAGE: |
| 317 | case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: |
| 318 | case SPV_OPERAND_TYPE_SELECTION_CONTROL: |
| 319 | case SPV_OPERAND_TYPE_LOOP_CONTROL: |
| 320 | case SPV_OPERAND_TYPE_FUNCTION_CONTROL: |
| 321 | case SPV_OPERAND_TYPE_MEMORY_ACCESS: |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 322 | case SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS: |
David Neto | 0dbe184 | 2017-12-03 14:26:16 -0500 | [diff] [blame] | 323 | return true; |
| 324 | default: |
| 325 | break; |
| 326 | } |
| 327 | return false; |
David Neto | b526756 | 2016-02-02 12:05:34 -0500 | [diff] [blame] | 328 | } |
| 329 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 330 | bool spvOperandIsOptional(spv_operand_type_t type) { |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 331 | return SPV_OPERAND_TYPE_FIRST_OPTIONAL_TYPE <= type && |
| 332 | type <= SPV_OPERAND_TYPE_LAST_OPTIONAL_TYPE; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | bool spvOperandIsVariable(spv_operand_type_t type) { |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 336 | return SPV_OPERAND_TYPE_FIRST_VARIABLE_TYPE <= type && |
| 337 | type <= SPV_OPERAND_TYPE_LAST_VARIABLE_TYPE; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 338 | } |
| 339 | |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 340 | bool spvExpandOperandSequenceOnce(spv_operand_type_t type, |
| 341 | spv_operand_pattern_t* pattern) { |
| 342 | switch (type) { |
| 343 | case SPV_OPERAND_TYPE_VARIABLE_ID: |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 344 | pattern->push_back(type); |
| 345 | pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 346 | return true; |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 347 | case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER: |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 348 | pattern->push_back(type); |
| 349 | pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 350 | return true; |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 351 | case SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID: |
David Neto | 201caf7 | 2015-11-04 17:38:17 -0500 | [diff] [blame] | 352 | // Represents Zero or more (Literal number, Id) pairs, |
| 353 | // where the literal number must be a scalar integer. |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 354 | pattern->push_back(type); |
| 355 | pattern->push_back(SPV_OPERAND_TYPE_ID); |
| 356 | pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 357 | return true; |
Lei Zhang | 6483bd7 | 2015-10-14 17:02:39 -0400 | [diff] [blame] | 358 | case SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER: |
David Neto | 561dc4e | 2015-09-25 14:23:29 -0400 | [diff] [blame] | 359 | // Represents Zero or more (Id, Literal number) pairs. |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 360 | pattern->push_back(type); |
| 361 | pattern->push_back(SPV_OPERAND_TYPE_LITERAL_INTEGER); |
| 362 | pattern->push_back(SPV_OPERAND_TYPE_OPTIONAL_ID); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 363 | return true; |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 364 | default: |
| 365 | break; |
| 366 | } |
| 367 | return false; |
| 368 | } |
| 369 | |
Dejan Mircevski | 50babb2 | 2015-09-29 10:56:32 -0400 | [diff] [blame] | 370 | spv_operand_type_t spvTakeFirstMatchableOperand( |
| 371 | spv_operand_pattern_t* pattern) { |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 372 | assert(!pattern->empty()); |
| 373 | spv_operand_type_t result; |
| 374 | do { |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 375 | result = pattern->back(); |
| 376 | pattern->pop_back(); |
Dejan Mircevski | 50babb2 | 2015-09-29 10:56:32 -0400 | [diff] [blame] | 377 | } while (spvExpandOperandSequenceOnce(result, pattern)); |
David Neto | 78c3b43 | 2015-08-27 13:03:52 -0400 | [diff] [blame] | 378 | return result; |
| 379 | } |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 380 | |
Dejan Mircevski | 897bff9 | 2015-09-29 10:38:18 -0400 | [diff] [blame] | 381 | spv_operand_pattern_t spvAlternatePatternFollowingImmediate( |
| 382 | const spv_operand_pattern_t& pattern) { |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 383 | auto it = |
| 384 | std::find(pattern.crbegin(), pattern.crend(), SPV_OPERAND_TYPE_RESULT_ID); |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 385 | if (it != pattern.crend()) { |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 386 | spv_operand_pattern_t alternatePattern(it - pattern.crbegin() + 2, |
| 387 | SPV_OPERAND_TYPE_OPTIONAL_CIV); |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 388 | alternatePattern[1] = SPV_OPERAND_TYPE_RESULT_ID; |
| 389 | return alternatePattern; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 390 | } |
Chris Forbes | 78338d5 | 2017-06-27 16:28:22 -0700 | [diff] [blame] | 391 | |
Dejan Mircevski | 897bff9 | 2015-09-29 10:38:18 -0400 | [diff] [blame] | 392 | // No result-id found, so just expect CIVs. |
Lei Zhang | 16981f8 | 2017-09-21 17:24:57 -0400 | [diff] [blame] | 393 | return {SPV_OPERAND_TYPE_OPTIONAL_CIV}; |
Dejan Mircevski | 903f9d6 | 2015-09-28 17:04:39 -0400 | [diff] [blame] | 394 | } |
Dejan Mircevski | 961f5dc | 2016-01-15 11:25:11 -0500 | [diff] [blame] | 395 | |
| 396 | bool spvIsIdType(spv_operand_type_t type) { |
| 397 | switch (type) { |
| 398 | case SPV_OPERAND_TYPE_ID: |
| 399 | case SPV_OPERAND_TYPE_TYPE_ID: |
| 400 | case SPV_OPERAND_TYPE_RESULT_ID: |
| 401 | case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID: |
| 402 | case SPV_OPERAND_TYPE_SCOPE_ID: |
| 403 | return true; |
| 404 | default: |
| 405 | return false; |
| 406 | } |
Dejan Mircevski | 961f5dc | 2016-01-15 11:25:11 -0500 | [diff] [blame] | 407 | } |
Andrey Tuganov | b36acbe | 2017-08-09 14:01:12 -0400 | [diff] [blame] | 408 | |
| 409 | std::function<bool(unsigned)> spvOperandCanBeForwardDeclaredFunction( |
| 410 | SpvOp opcode) { |
| 411 | std::function<bool(unsigned index)> out; |
| 412 | switch (opcode) { |
| 413 | case SpvOpExecutionMode: |
| 414 | case SpvOpEntryPoint: |
| 415 | case SpvOpName: |
| 416 | case SpvOpMemberName: |
| 417 | case SpvOpSelectionMerge: |
| 418 | case SpvOpDecorate: |
| 419 | case SpvOpMemberDecorate: |
David Neto | 5f69f75 | 2018-03-05 13:34:13 -0500 | [diff] [blame] | 420 | case SpvOpDecorateId: |
| 421 | case SpvOpDecorateStringGOOGLE: |
| 422 | case SpvOpMemberDecorateStringGOOGLE: |
Andrey Tuganov | b36acbe | 2017-08-09 14:01:12 -0400 | [diff] [blame] | 423 | case SpvOpTypeStruct: |
| 424 | case SpvOpBranch: |
| 425 | case SpvOpLoopMerge: |
| 426 | out = [](unsigned) { return true; }; |
| 427 | break; |
| 428 | case SpvOpGroupDecorate: |
| 429 | case SpvOpGroupMemberDecorate: |
| 430 | case SpvOpBranchConditional: |
| 431 | case SpvOpSwitch: |
| 432 | out = [](unsigned index) { return index != 0; }; |
| 433 | break; |
| 434 | |
| 435 | case SpvOpFunctionCall: |
| 436 | // The Function parameter. |
| 437 | out = [](unsigned index) { return index == 2; }; |
| 438 | break; |
| 439 | |
| 440 | case SpvOpPhi: |
| 441 | out = [](unsigned index) { return index > 1; }; |
| 442 | break; |
| 443 | |
| 444 | case SpvOpEnqueueKernel: |
| 445 | // The Invoke parameter. |
| 446 | out = [](unsigned index) { return index == 8; }; |
| 447 | break; |
| 448 | |
| 449 | case SpvOpGetKernelNDrangeSubGroupCount: |
| 450 | case SpvOpGetKernelNDrangeMaxSubGroupSize: |
| 451 | // The Invoke parameter. |
| 452 | out = [](unsigned index) { return index == 3; }; |
| 453 | break; |
| 454 | |
| 455 | case SpvOpGetKernelWorkGroupSize: |
| 456 | case SpvOpGetKernelPreferredWorkGroupSizeMultiple: |
| 457 | // The Invoke parameter. |
| 458 | out = [](unsigned index) { return index == 2; }; |
| 459 | break; |
| 460 | case SpvOpTypeForwardPointer: |
| 461 | out = [](unsigned index) { return index == 0; }; |
| 462 | break; |
| 463 | default: |
| 464 | out = [](unsigned) { return false; }; |
| 465 | break; |
| 466 | } |
| 467 | return out; |
| 468 | } |