Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1 | /* |
Hans-Kristian Arntzen | 4704482 | 2021-01-14 16:07:49 +0100 | [diff] [blame] | 2 | * Copyright 2018-2021 Arm Limited |
Jon Leech | f2a6554 | 2021-05-08 01:47:48 -0700 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 OR MIT |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Hans-Kristian Arntzen | cf1e9e0 | 2020-11-25 15:22:08 +0100 | [diff] [blame] | 18 | /* |
| 19 | * At your option, you may choose to accept this material under either: |
| 20 | * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or |
| 21 | * 2. The MIT License, found at <http://opensource.org/licenses/MIT>. |
Hans-Kristian Arntzen | cf1e9e0 | 2020-11-25 15:22:08 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 24 | #include "spirv_parser.hpp" |
| 25 | #include <assert.h> |
| 26 | |
| 27 | using namespace std; |
| 28 | using namespace spv; |
| 29 | |
Hans-Kristian Arntzen | 9b92e68 | 2019-03-29 10:29:44 +0100 | [diff] [blame] | 30 | namespace SPIRV_CROSS_NAMESPACE |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 31 | { |
Hans-Kristian Arntzen | 3fe57d3 | 2019-04-09 12:46:23 +0200 | [diff] [blame] | 32 | Parser::Parser(vector<uint32_t> spirv) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 33 | { |
| 34 | ir.spirv = move(spirv); |
| 35 | } |
| 36 | |
| 37 | Parser::Parser(const uint32_t *spirv_data, size_t word_count) |
| 38 | { |
Hans-Kristian Arntzen | 3fe57d3 | 2019-04-09 12:46:23 +0200 | [diff] [blame] | 39 | ir.spirv = vector<uint32_t>(spirv_data, spirv_data + word_count); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Hans-Kristian Arntzen | fa42ed3 | 2018-11-15 10:51:01 +0100 | [diff] [blame] | 42 | static bool decoration_is_string(Decoration decoration) |
| 43 | { |
| 44 | switch (decoration) |
| 45 | { |
| 46 | case DecorationHlslSemanticGOOGLE: |
| 47 | return true; |
| 48 | |
| 49 | default: |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 54 | static inline uint32_t swap_endian(uint32_t v) |
| 55 | { |
| 56 | return ((v >> 24) & 0x000000ffu) | ((v >> 8) & 0x0000ff00u) | ((v << 8) & 0x00ff0000u) | ((v << 24) & 0xff000000u); |
| 57 | } |
| 58 | |
| 59 | static bool is_valid_spirv_version(uint32_t version) |
| 60 | { |
| 61 | switch (version) |
| 62 | { |
| 63 | // Allow v99 since it tends to just work. |
| 64 | case 99: |
| 65 | case 0x10000: // SPIR-V 1.0 |
| 66 | case 0x10100: // SPIR-V 1.1 |
| 67 | case 0x10200: // SPIR-V 1.2 |
| 68 | case 0x10300: // SPIR-V 1.3 |
Hans-Kristian Arntzen | ab1fa90 | 2019-05-07 09:53:40 +0200 | [diff] [blame] | 69 | case 0x10400: // SPIR-V 1.4 |
Hans-Kristian Arntzen | 02c34fe | 2019-09-19 10:26:04 +0200 | [diff] [blame] | 70 | case 0x10500: // SPIR-V 1.5 |
Hans-Kristian Arntzen | 7c12228 | 2022-01-06 14:16:28 +0100 | [diff] [blame] | 71 | case 0x10600: // SPIR-V 1.6 |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 72 | return true; |
| 73 | |
| 74 | default: |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void Parser::parse() |
| 80 | { |
| 81 | auto &spirv = ir.spirv; |
| 82 | |
| 83 | auto len = spirv.size(); |
| 84 | if (len < 5) |
| 85 | SPIRV_CROSS_THROW("SPIRV file too small."); |
| 86 | |
| 87 | auto s = spirv.data(); |
| 88 | |
| 89 | // Endian-swap if we need to. |
| 90 | if (s[0] == swap_endian(MagicNumber)) |
| 91 | transform(begin(spirv), end(spirv), begin(spirv), [](uint32_t c) { return swap_endian(c); }); |
| 92 | |
| 93 | if (s[0] != MagicNumber || !is_valid_spirv_version(s[1])) |
| 94 | SPIRV_CROSS_THROW("Invalid SPIRV format."); |
| 95 | |
| 96 | uint32_t bound = s[3]; |
Hans-Kristian Arntzen | 92a4294 | 2020-02-14 12:57:01 +0100 | [diff] [blame] | 97 | |
| 98 | const uint32_t MaximumNumberOfIDs = 0x3fffff; |
| 99 | if (bound > MaximumNumberOfIDs) |
| 100 | SPIRV_CROSS_THROW("ID bound exceeds limit of 0x3fffff.\n"); |
| 101 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 102 | ir.set_id_bounds(bound); |
| 103 | |
| 104 | uint32_t offset = 5; |
| 105 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 106 | SmallVector<Instruction> instructions; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 107 | while (offset < len) |
| 108 | { |
| 109 | Instruction instr = {}; |
| 110 | instr.op = spirv[offset] & 0xffff; |
| 111 | instr.count = (spirv[offset] >> 16) & 0xffff; |
| 112 | |
| 113 | if (instr.count == 0) |
| 114 | SPIRV_CROSS_THROW("SPIR-V instructions cannot consume 0 words. Invalid SPIR-V file."); |
| 115 | |
| 116 | instr.offset = offset + 1; |
| 117 | instr.length = instr.count - 1; |
| 118 | |
| 119 | offset += instr.count; |
| 120 | |
| 121 | if (offset > spirv.size()) |
| 122 | SPIRV_CROSS_THROW("SPIR-V instruction goes out of bounds."); |
| 123 | |
| 124 | instructions.push_back(instr); |
| 125 | } |
| 126 | |
| 127 | for (auto &i : instructions) |
| 128 | parse(i); |
| 129 | |
Hans-Kristian Arntzen | 58dad82 | 2020-05-25 11:05:42 +0200 | [diff] [blame] | 130 | for (auto &fixup : forward_pointer_fixups) |
| 131 | { |
| 132 | auto &target = get<SPIRType>(fixup.first); |
| 133 | auto &source = get<SPIRType>(fixup.second); |
| 134 | target.member_types = source.member_types; |
| 135 | target.basetype = source.basetype; |
| 136 | target.self = source.self; |
| 137 | } |
| 138 | forward_pointer_fixups.clear(); |
| 139 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 140 | if (current_function) |
| 141 | SPIRV_CROSS_THROW("Function was not terminated."); |
| 142 | if (current_block) |
| 143 | SPIRV_CROSS_THROW("Block was not terminated."); |
Hans-Kristian Arntzen | bc4cb1b | 2021-03-08 10:39:59 +0100 | [diff] [blame] | 144 | if (ir.default_entry_point == 0) |
| 145 | SPIRV_CROSS_THROW("There is no entry point in the SPIR-V module."); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | const uint32_t *Parser::stream(const Instruction &instr) const |
| 149 | { |
| 150 | // If we're not going to use any arguments, just return nullptr. |
| 151 | // We want to avoid case where we return an out of range pointer |
| 152 | // that trips debug assertions on some platforms. |
| 153 | if (!instr.length) |
| 154 | return nullptr; |
| 155 | |
| 156 | if (instr.offset + instr.length > ir.spirv.size()) |
| 157 | SPIRV_CROSS_THROW("Compiler::stream() out of range."); |
| 158 | return &ir.spirv[instr.offset]; |
| 159 | } |
| 160 | |
Hans-Kristian Arntzen | 3fe57d3 | 2019-04-09 12:46:23 +0200 | [diff] [blame] | 161 | static string extract_string(const vector<uint32_t> &spirv, uint32_t offset) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 162 | { |
| 163 | string ret; |
| 164 | for (uint32_t i = offset; i < spirv.size(); i++) |
| 165 | { |
| 166 | uint32_t w = spirv[i]; |
| 167 | |
| 168 | for (uint32_t j = 0; j < 4; j++, w >>= 8) |
| 169 | { |
| 170 | char c = w & 0xff; |
| 171 | if (c == '\0') |
| 172 | return ret; |
| 173 | ret += c; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | SPIRV_CROSS_THROW("String was not terminated before EOF"); |
| 178 | } |
| 179 | |
| 180 | void Parser::parse(const Instruction &instruction) |
| 181 | { |
| 182 | auto *ops = stream(instruction); |
| 183 | auto op = static_cast<Op>(instruction.op); |
| 184 | uint32_t length = instruction.length; |
| 185 | |
| 186 | switch (op) |
| 187 | { |
lifpan | 00a765e | 2018-11-15 09:04:36 +0800 | [diff] [blame] | 188 | case OpSourceContinued: |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 189 | case OpSourceExtension: |
| 190 | case OpNop: |
lifpan | 9161096 | 2018-11-13 14:28:38 +0800 | [diff] [blame] | 191 | case OpModuleProcessed: |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 192 | break; |
| 193 | |
Hans-Kristian Arntzen | 65af09d | 2019-05-28 13:41:46 +0200 | [diff] [blame] | 194 | case OpString: |
| 195 | { |
| 196 | set<SPIRString>(ops[0], extract_string(ir.spirv, instruction.offset + 1)); |
| 197 | break; |
| 198 | } |
| 199 | |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 200 | case OpMemoryModel: |
| 201 | ir.addressing_model = static_cast<AddressingModel>(ops[0]); |
| 202 | ir.memory_model = static_cast<MemoryModel>(ops[1]); |
| 203 | break; |
| 204 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 205 | case OpSource: |
| 206 | { |
| 207 | auto lang = static_cast<SourceLanguage>(ops[0]); |
| 208 | switch (lang) |
| 209 | { |
| 210 | case SourceLanguageESSL: |
| 211 | ir.source.es = true; |
| 212 | ir.source.version = ops[1]; |
| 213 | ir.source.known = true; |
| 214 | ir.source.hlsl = false; |
| 215 | break; |
| 216 | |
| 217 | case SourceLanguageGLSL: |
| 218 | ir.source.es = false; |
| 219 | ir.source.version = ops[1]; |
| 220 | ir.source.known = true; |
| 221 | ir.source.hlsl = false; |
| 222 | break; |
| 223 | |
| 224 | case SourceLanguageHLSL: |
| 225 | // For purposes of cross-compiling, this is GLSL 450. |
| 226 | ir.source.es = false; |
| 227 | ir.source.version = 450; |
| 228 | ir.source.known = true; |
| 229 | ir.source.hlsl = true; |
| 230 | break; |
| 231 | |
| 232 | default: |
| 233 | ir.source.known = false; |
| 234 | break; |
| 235 | } |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | case OpUndef: |
| 240 | { |
| 241 | uint32_t result_type = ops[0]; |
| 242 | uint32_t id = ops[1]; |
| 243 | set<SPIRUndef>(id, result_type); |
lifpan | 876627d | 2019-04-08 19:45:31 +0800 | [diff] [blame] | 244 | if (current_block) |
| 245 | current_block->ops.push_back(instruction); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | |
| 249 | case OpCapability: |
| 250 | { |
| 251 | uint32_t cap = ops[0]; |
| 252 | if (cap == CapabilityKernel) |
| 253 | SPIRV_CROSS_THROW("Kernel capability not supported."); |
| 254 | |
| 255 | ir.declared_capabilities.push_back(static_cast<Capability>(ops[0])); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | case OpExtension: |
| 260 | { |
| 261 | auto ext = extract_string(ir.spirv, instruction.offset); |
| 262 | ir.declared_extensions.push_back(move(ext)); |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | case OpExtInstImport: |
| 267 | { |
| 268 | uint32_t id = ops[0]; |
| 269 | auto ext = extract_string(ir.spirv, instruction.offset + 1); |
| 270 | if (ext == "GLSL.std.450") |
| 271 | set<SPIRExtension>(id, SPIRExtension::GLSL); |
Lifeng Pan | 5ca8779 | 2019-07-04 16:03:06 +0800 | [diff] [blame] | 272 | else if (ext == "DebugInfo") |
| 273 | set<SPIRExtension>(id, SPIRExtension::SPV_debug_info); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 274 | else if (ext == "SPV_AMD_shader_ballot") |
| 275 | set<SPIRExtension>(id, SPIRExtension::SPV_AMD_shader_ballot); |
| 276 | else if (ext == "SPV_AMD_shader_explicit_vertex_parameter") |
| 277 | set<SPIRExtension>(id, SPIRExtension::SPV_AMD_shader_explicit_vertex_parameter); |
| 278 | else if (ext == "SPV_AMD_shader_trinary_minmax") |
| 279 | set<SPIRExtension>(id, SPIRExtension::SPV_AMD_shader_trinary_minmax); |
| 280 | else if (ext == "SPV_AMD_gcn_shader") |
| 281 | set<SPIRExtension>(id, SPIRExtension::SPV_AMD_gcn_shader); |
| 282 | else |
| 283 | set<SPIRExtension>(id, SPIRExtension::Unsupported); |
| 284 | |
| 285 | // Other SPIR-V extensions which have ExtInstrs are currently not supported. |
| 286 | |
| 287 | break; |
| 288 | } |
| 289 | |
Lifeng Pan | 5ca8779 | 2019-07-04 16:03:06 +0800 | [diff] [blame] | 290 | case OpExtInst: |
| 291 | { |
| 292 | // The SPIR-V debug information extended instructions might come at global scope. |
| 293 | if (current_block) |
Hans-Kristian Arntzen | 29cc189 | 2022-02-16 11:49:24 +0100 | [diff] [blame^] | 294 | { |
Lifeng Pan | 5ca8779 | 2019-07-04 16:03:06 +0800 | [diff] [blame] | 295 | current_block->ops.push_back(instruction); |
Hans-Kristian Arntzen | 29cc189 | 2022-02-16 11:49:24 +0100 | [diff] [blame^] | 296 | if (length >= 2) |
| 297 | { |
| 298 | const auto *type = maybe_get<SPIRType>(ops[0]); |
| 299 | if (type) |
| 300 | ir.load_type_width.insert({ ops[1], type->width }); |
| 301 | } |
| 302 | } |
Lifeng Pan | 5ca8779 | 2019-07-04 16:03:06 +0800 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 306 | case OpEntryPoint: |
| 307 | { |
| 308 | auto itr = |
| 309 | ir.entry_points.insert(make_pair(ops[1], SPIREntryPoint(ops[1], static_cast<ExecutionModel>(ops[0]), |
| 310 | extract_string(ir.spirv, instruction.offset + 2)))); |
| 311 | auto &e = itr.first->second; |
| 312 | |
| 313 | // Strings need nul-terminator and consume the whole word. |
| 314 | uint32_t strlen_words = uint32_t((e.name.size() + 1 + 3) >> 2); |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 315 | |
| 316 | for (uint32_t i = strlen_words + 2; i < instruction.length; i++) |
| 317 | e.interface_variables.push_back(ops[i]); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 318 | |
| 319 | // Set the name of the entry point in case OpName is not provided later. |
| 320 | ir.set_name(ops[1], e.name); |
| 321 | |
| 322 | // If we don't have an entry, make the first one our "default". |
| 323 | if (!ir.default_entry_point) |
| 324 | ir.default_entry_point = ops[1]; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | case OpExecutionMode: |
| 329 | { |
| 330 | auto &execution = ir.entry_points[ops[0]]; |
| 331 | auto mode = static_cast<ExecutionMode>(ops[1]); |
| 332 | execution.flags.set(mode); |
| 333 | |
| 334 | switch (mode) |
| 335 | { |
| 336 | case ExecutionModeInvocations: |
| 337 | execution.invocations = ops[2]; |
| 338 | break; |
| 339 | |
| 340 | case ExecutionModeLocalSize: |
| 341 | execution.workgroup_size.x = ops[2]; |
| 342 | execution.workgroup_size.y = ops[3]; |
| 343 | execution.workgroup_size.z = ops[4]; |
| 344 | break; |
| 345 | |
| 346 | case ExecutionModeOutputVertices: |
| 347 | execution.output_vertices = ops[2]; |
| 348 | break; |
| 349 | |
| 350 | default: |
| 351 | break; |
| 352 | } |
| 353 | break; |
| 354 | } |
| 355 | |
Hans-Kristian Arntzen | 7c83fc2 | 2022-01-05 15:53:51 +0100 | [diff] [blame] | 356 | case OpExecutionModeId: |
| 357 | { |
| 358 | auto &execution = ir.entry_points[ops[0]]; |
| 359 | auto mode = static_cast<ExecutionMode>(ops[1]); |
| 360 | execution.flags.set(mode); |
| 361 | |
| 362 | if (mode == ExecutionModeLocalSizeId) |
| 363 | { |
| 364 | execution.workgroup_size.id_x = ops[2]; |
| 365 | execution.workgroup_size.id_y = ops[3]; |
| 366 | execution.workgroup_size.id_z = ops[4]; |
| 367 | } |
| 368 | |
| 369 | break; |
| 370 | } |
| 371 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 372 | case OpName: |
| 373 | { |
| 374 | uint32_t id = ops[0]; |
| 375 | ir.set_name(id, extract_string(ir.spirv, instruction.offset + 1)); |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | case OpMemberName: |
| 380 | { |
| 381 | uint32_t id = ops[0]; |
| 382 | uint32_t member = ops[1]; |
| 383 | ir.set_member_name(id, member, extract_string(ir.spirv, instruction.offset + 2)); |
| 384 | break; |
| 385 | } |
| 386 | |
Hans-Kristian Arntzen | fa42ed3 | 2018-11-15 10:51:01 +0100 | [diff] [blame] | 387 | case OpDecorationGroup: |
| 388 | { |
| 389 | // Noop, this simply means an ID should be a collector of decorations. |
| 390 | // The meta array is already a flat array of decorations which will contain the relevant decorations. |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | case OpGroupDecorate: |
| 395 | { |
| 396 | uint32_t group_id = ops[0]; |
| 397 | auto &decorations = ir.meta[group_id].decoration; |
| 398 | auto &flags = decorations.decoration_flags; |
| 399 | |
| 400 | // Copies decorations from one ID to another. Only copy decorations which are set in the group, |
| 401 | // i.e., we cannot just copy the meta structure directly. |
| 402 | for (uint32_t i = 1; i < length; i++) |
| 403 | { |
| 404 | uint32_t target = ops[i]; |
| 405 | flags.for_each_bit([&](uint32_t bit) { |
| 406 | auto decoration = static_cast<Decoration>(bit); |
| 407 | |
| 408 | if (decoration_is_string(decoration)) |
| 409 | { |
| 410 | ir.set_decoration_string(target, decoration, ir.get_decoration_string(group_id, decoration)); |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | ir.meta[target].decoration_word_offset[decoration] = |
| 415 | ir.meta[group_id].decoration_word_offset[decoration]; |
| 416 | ir.set_decoration(target, decoration, ir.get_decoration(group_id, decoration)); |
| 417 | } |
| 418 | }); |
| 419 | } |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | case OpGroupMemberDecorate: |
| 424 | { |
| 425 | uint32_t group_id = ops[0]; |
| 426 | auto &flags = ir.meta[group_id].decoration.decoration_flags; |
| 427 | |
| 428 | // Copies decorations from one ID to another. Only copy decorations which are set in the group, |
| 429 | // i.e., we cannot just copy the meta structure directly. |
| 430 | for (uint32_t i = 1; i + 1 < length; i += 2) |
| 431 | { |
| 432 | uint32_t target = ops[i + 0]; |
| 433 | uint32_t index = ops[i + 1]; |
| 434 | flags.for_each_bit([&](uint32_t bit) { |
| 435 | auto decoration = static_cast<Decoration>(bit); |
| 436 | |
| 437 | if (decoration_is_string(decoration)) |
| 438 | ir.set_member_decoration_string(target, index, decoration, |
| 439 | ir.get_decoration_string(group_id, decoration)); |
| 440 | else |
| 441 | ir.set_member_decoration(target, index, decoration, ir.get_decoration(group_id, decoration)); |
| 442 | }); |
| 443 | } |
| 444 | break; |
| 445 | } |
| 446 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 447 | case OpDecorate: |
| 448 | case OpDecorateId: |
| 449 | { |
Hans-Kristian Arntzen | fa42ed3 | 2018-11-15 10:51:01 +0100 | [diff] [blame] | 450 | // OpDecorateId technically supports an array of arguments, but our only supported decorations are single uint, |
| 451 | // so merge decorate and decorate-id here. |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 452 | uint32_t id = ops[0]; |
| 453 | |
| 454 | auto decoration = static_cast<Decoration>(ops[1]); |
| 455 | if (length >= 3) |
| 456 | { |
| 457 | ir.meta[id].decoration_word_offset[decoration] = uint32_t(&ops[2] - ir.spirv.data()); |
| 458 | ir.set_decoration(id, decoration, ops[2]); |
| 459 | } |
| 460 | else |
| 461 | ir.set_decoration(id, decoration); |
| 462 | |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | case OpDecorateStringGOOGLE: |
| 467 | { |
| 468 | uint32_t id = ops[0]; |
| 469 | auto decoration = static_cast<Decoration>(ops[1]); |
| 470 | ir.set_decoration_string(id, decoration, extract_string(ir.spirv, instruction.offset + 2)); |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | case OpMemberDecorate: |
| 475 | { |
| 476 | uint32_t id = ops[0]; |
| 477 | uint32_t member = ops[1]; |
| 478 | auto decoration = static_cast<Decoration>(ops[2]); |
| 479 | if (length >= 4) |
| 480 | ir.set_member_decoration(id, member, decoration, ops[3]); |
| 481 | else |
| 482 | ir.set_member_decoration(id, member, decoration); |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | case OpMemberDecorateStringGOOGLE: |
| 487 | { |
| 488 | uint32_t id = ops[0]; |
| 489 | uint32_t member = ops[1]; |
| 490 | auto decoration = static_cast<Decoration>(ops[2]); |
| 491 | ir.set_member_decoration_string(id, member, decoration, extract_string(ir.spirv, instruction.offset + 3)); |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | // Build up basic types. |
| 496 | case OpTypeVoid: |
| 497 | { |
| 498 | uint32_t id = ops[0]; |
| 499 | auto &type = set<SPIRType>(id); |
| 500 | type.basetype = SPIRType::Void; |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | case OpTypeBool: |
| 505 | { |
| 506 | uint32_t id = ops[0]; |
| 507 | auto &type = set<SPIRType>(id); |
| 508 | type.basetype = SPIRType::Boolean; |
| 509 | type.width = 1; |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | case OpTypeFloat: |
| 514 | { |
| 515 | uint32_t id = ops[0]; |
| 516 | uint32_t width = ops[1]; |
| 517 | auto &type = set<SPIRType>(id); |
| 518 | if (width == 64) |
| 519 | type.basetype = SPIRType::Double; |
| 520 | else if (width == 32) |
| 521 | type.basetype = SPIRType::Float; |
| 522 | else if (width == 16) |
| 523 | type.basetype = SPIRType::Half; |
| 524 | else |
| 525 | SPIRV_CROSS_THROW("Unrecognized bit-width of floating point type."); |
| 526 | type.width = width; |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | case OpTypeInt: |
| 531 | { |
| 532 | uint32_t id = ops[0]; |
| 533 | uint32_t width = ops[1]; |
lifpan | b21525b | 2018-11-28 14:20:24 +0800 | [diff] [blame] | 534 | bool signedness = ops[2] != 0; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 535 | auto &type = set<SPIRType>(id); |
Hans-Kristian Arntzen | 2ed171e | 2019-01-30 14:49:55 +0100 | [diff] [blame] | 536 | type.basetype = signedness ? to_signed_basetype(width) : to_unsigned_basetype(width); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 537 | type.width = width; |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | // Build composite types by "inheriting". |
| 542 | // NOTE: The self member is also copied! For pointers and array modifiers this is a good thing |
| 543 | // since we can refer to decorations on pointee classes which is needed for UBO/SSBO, I/O blocks in geometry/tess etc. |
| 544 | case OpTypeVector: |
| 545 | { |
| 546 | uint32_t id = ops[0]; |
| 547 | uint32_t vecsize = ops[2]; |
| 548 | |
| 549 | auto &base = get<SPIRType>(ops[1]); |
| 550 | auto &vecbase = set<SPIRType>(id); |
| 551 | |
| 552 | vecbase = base; |
| 553 | vecbase.vecsize = vecsize; |
| 554 | vecbase.self = id; |
| 555 | vecbase.parent_type = ops[1]; |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | case OpTypeMatrix: |
| 560 | { |
| 561 | uint32_t id = ops[0]; |
| 562 | uint32_t colcount = ops[2]; |
| 563 | |
| 564 | auto &base = get<SPIRType>(ops[1]); |
| 565 | auto &matrixbase = set<SPIRType>(id); |
| 566 | |
| 567 | matrixbase = base; |
| 568 | matrixbase.columns = colcount; |
| 569 | matrixbase.self = id; |
| 570 | matrixbase.parent_type = ops[1]; |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | case OpTypeArray: |
| 575 | { |
| 576 | uint32_t id = ops[0]; |
| 577 | auto &arraybase = set<SPIRType>(id); |
| 578 | |
| 579 | uint32_t tid = ops[1]; |
| 580 | auto &base = get<SPIRType>(tid); |
| 581 | |
| 582 | arraybase = base; |
| 583 | arraybase.parent_type = tid; |
| 584 | |
| 585 | uint32_t cid = ops[2]; |
| 586 | ir.mark_used_as_array_length(cid); |
| 587 | auto *c = maybe_get<SPIRConstant>(cid); |
| 588 | bool literal = c && !c->specialization; |
| 589 | |
Hans-Kristian Arntzen | 58dad82 | 2020-05-25 11:05:42 +0200 | [diff] [blame] | 590 | // We're copying type information into Array types, so we'll need a fixup for any physical pointer |
| 591 | // references. |
| 592 | if (base.forward_pointer) |
| 593 | forward_pointer_fixups.push_back({ id, tid }); |
| 594 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 595 | arraybase.array_size_literal.push_back(literal); |
| 596 | arraybase.array.push_back(literal ? c->scalar() : cid); |
| 597 | // Do NOT set arraybase.self! |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | case OpTypeRuntimeArray: |
| 602 | { |
| 603 | uint32_t id = ops[0]; |
| 604 | |
| 605 | auto &base = get<SPIRType>(ops[1]); |
| 606 | auto &arraybase = set<SPIRType>(id); |
| 607 | |
Hans-Kristian Arntzen | 58dad82 | 2020-05-25 11:05:42 +0200 | [diff] [blame] | 608 | // We're copying type information into Array types, so we'll need a fixup for any physical pointer |
| 609 | // references. |
| 610 | if (base.forward_pointer) |
| 611 | forward_pointer_fixups.push_back({ id, ops[1] }); |
| 612 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 613 | arraybase = base; |
| 614 | arraybase.array.push_back(0); |
| 615 | arraybase.array_size_literal.push_back(true); |
| 616 | arraybase.parent_type = ops[1]; |
| 617 | // Do NOT set arraybase.self! |
| 618 | break; |
| 619 | } |
| 620 | |
| 621 | case OpTypeImage: |
| 622 | { |
| 623 | uint32_t id = ops[0]; |
| 624 | auto &type = set<SPIRType>(id); |
| 625 | type.basetype = SPIRType::Image; |
| 626 | type.image.type = ops[1]; |
| 627 | type.image.dim = static_cast<Dim>(ops[2]); |
| 628 | type.image.depth = ops[3] == 1; |
| 629 | type.image.arrayed = ops[4] != 0; |
| 630 | type.image.ms = ops[5] != 0; |
| 631 | type.image.sampled = ops[6]; |
| 632 | type.image.format = static_cast<ImageFormat>(ops[7]); |
| 633 | type.image.access = (length >= 9) ? static_cast<AccessQualifier>(ops[8]) : AccessQualifierMax; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | |
| 637 | case OpTypeSampledImage: |
| 638 | { |
| 639 | uint32_t id = ops[0]; |
| 640 | uint32_t imagetype = ops[1]; |
| 641 | auto &type = set<SPIRType>(id); |
| 642 | type = get<SPIRType>(imagetype); |
| 643 | type.basetype = SPIRType::SampledImage; |
| 644 | type.self = id; |
| 645 | break; |
| 646 | } |
| 647 | |
| 648 | case OpTypeSampler: |
| 649 | { |
| 650 | uint32_t id = ops[0]; |
| 651 | auto &type = set<SPIRType>(id); |
| 652 | type.basetype = SPIRType::Sampler; |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | case OpTypePointer: |
| 657 | { |
| 658 | uint32_t id = ops[0]; |
| 659 | |
Hans-Kristian Arntzen | 1f018b0 | 2020-11-03 10:51:56 +0100 | [diff] [blame] | 660 | // Very rarely, we might receive a FunctionPrototype here. |
| 661 | // We won't be able to compile it, but we shouldn't crash when parsing. |
| 662 | // We should be able to reflect. |
| 663 | auto *base = maybe_get<SPIRType>(ops[2]); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 664 | auto &ptrbase = set<SPIRType>(id); |
| 665 | |
Hans-Kristian Arntzen | 1f018b0 | 2020-11-03 10:51:56 +0100 | [diff] [blame] | 666 | if (base) |
| 667 | ptrbase = *base; |
| 668 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 669 | ptrbase.pointer = true; |
Hans-Kristian Arntzen | d0b9372 | 2018-11-26 12:23:28 +0100 | [diff] [blame] | 670 | ptrbase.pointer_depth++; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 671 | ptrbase.storage = static_cast<StorageClass>(ops[1]); |
| 672 | |
| 673 | if (ptrbase.storage == StorageClassAtomicCounter) |
| 674 | ptrbase.basetype = SPIRType::AtomicCounter; |
| 675 | |
Hans-Kristian Arntzen | 1f018b0 | 2020-11-03 10:51:56 +0100 | [diff] [blame] | 676 | if (base && base->forward_pointer) |
Hans-Kristian Arntzen | 58dad82 | 2020-05-25 11:05:42 +0200 | [diff] [blame] | 677 | forward_pointer_fixups.push_back({ id, ops[2] }); |
| 678 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 679 | ptrbase.parent_type = ops[2]; |
| 680 | |
| 681 | // Do NOT set ptrbase.self! |
| 682 | break; |
| 683 | } |
| 684 | |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 685 | case OpTypeForwardPointer: |
| 686 | { |
| 687 | uint32_t id = ops[0]; |
| 688 | auto &ptrbase = set<SPIRType>(id); |
| 689 | ptrbase.pointer = true; |
| 690 | ptrbase.pointer_depth++; |
| 691 | ptrbase.storage = static_cast<StorageClass>(ops[1]); |
Hans-Kristian Arntzen | 58dad82 | 2020-05-25 11:05:42 +0200 | [diff] [blame] | 692 | ptrbase.forward_pointer = true; |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 693 | |
| 694 | if (ptrbase.storage == StorageClassAtomicCounter) |
| 695 | ptrbase.basetype = SPIRType::AtomicCounter; |
| 696 | |
| 697 | break; |
| 698 | } |
| 699 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 700 | case OpTypeStruct: |
| 701 | { |
| 702 | uint32_t id = ops[0]; |
| 703 | auto &type = set<SPIRType>(id); |
| 704 | type.basetype = SPIRType::Struct; |
| 705 | for (uint32_t i = 1; i < length; i++) |
| 706 | type.member_types.push_back(ops[i]); |
| 707 | |
| 708 | // Check if we have seen this struct type before, with just different |
| 709 | // decorations. |
| 710 | // |
| 711 | // Add workaround for issue #17 as well by looking at OpName for the struct |
| 712 | // types, which we shouldn't normally do. |
| 713 | // We should not normally have to consider type aliases like this to begin with |
| 714 | // however ... glslang issues #304, #307 cover this. |
| 715 | |
| 716 | // For stripped names, never consider struct type aliasing. |
| 717 | // We risk declaring the same struct multiple times, but type-punning is not allowed |
| 718 | // so this is safe. |
| 719 | bool consider_aliasing = !ir.get_name(type.self).empty(); |
| 720 | if (consider_aliasing) |
| 721 | { |
| 722 | for (auto &other : global_struct_cache) |
| 723 | { |
| 724 | if (ir.get_name(type.self) == ir.get_name(other) && |
| 725 | types_are_logically_equivalent(type, get<SPIRType>(other))) |
| 726 | { |
| 727 | type.type_alias = other; |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 732 | if (type.type_alias == TypeID(0)) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 733 | global_struct_cache.push_back(id); |
| 734 | } |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | case OpTypeFunction: |
| 739 | { |
| 740 | uint32_t id = ops[0]; |
| 741 | uint32_t ret = ops[1]; |
| 742 | |
| 743 | auto &func = set<SPIRFunctionPrototype>(id, ret); |
| 744 | for (uint32_t i = 2; i < length; i++) |
| 745 | func.parameter_types.push_back(ops[i]); |
| 746 | break; |
| 747 | } |
| 748 | |
Hans-Kristian Arntzen | 6b0e558 | 2020-04-21 14:25:18 +0200 | [diff] [blame] | 749 | case OpTypeAccelerationStructureKHR: |
Patrick Mours | da39a7b | 2019-02-26 15:43:03 +0100 | [diff] [blame] | 750 | { |
| 751 | uint32_t id = ops[0]; |
| 752 | auto &type = set<SPIRType>(id); |
Hans-Kristian Arntzen | 6b0e558 | 2020-04-21 14:25:18 +0200 | [diff] [blame] | 753 | type.basetype = SPIRType::AccelerationStructure; |
| 754 | break; |
| 755 | } |
| 756 | |
Hans-Kristian Arntzen | 1a28a04 | 2021-01-06 11:32:26 +0100 | [diff] [blame] | 757 | case OpTypeRayQueryKHR: |
Hans-Kristian Arntzen | 6b0e558 | 2020-04-21 14:25:18 +0200 | [diff] [blame] | 758 | { |
| 759 | uint32_t id = ops[0]; |
| 760 | auto &type = set<SPIRType>(id); |
| 761 | type.basetype = SPIRType::RayQuery; |
Patrick Mours | da39a7b | 2019-02-26 15:43:03 +0100 | [diff] [blame] | 762 | break; |
| 763 | } |
| 764 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 765 | // Variable declaration |
| 766 | // All variables are essentially pointers with a storage qualifier. |
| 767 | case OpVariable: |
| 768 | { |
| 769 | uint32_t type = ops[0]; |
| 770 | uint32_t id = ops[1]; |
| 771 | auto storage = static_cast<StorageClass>(ops[2]); |
| 772 | uint32_t initializer = length == 4 ? ops[3] : 0; |
| 773 | |
| 774 | if (storage == StorageClassFunction) |
| 775 | { |
| 776 | if (!current_function) |
| 777 | SPIRV_CROSS_THROW("No function currently in scope"); |
| 778 | current_function->add_local_variable(id); |
| 779 | } |
| 780 | |
| 781 | set<SPIRVariable>(id, type, storage, initializer); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 782 | break; |
| 783 | } |
| 784 | |
| 785 | // OpPhi |
| 786 | // OpPhi is a fairly magical opcode. |
| 787 | // It selects temporary variables based on which parent block we *came from*. |
| 788 | // In high-level languages we can "de-SSA" by creating a function local, and flush out temporaries to this function-local |
| 789 | // variable to emulate SSA Phi. |
| 790 | case OpPhi: |
| 791 | { |
| 792 | if (!current_function) |
| 793 | SPIRV_CROSS_THROW("No function currently in scope"); |
| 794 | if (!current_block) |
| 795 | SPIRV_CROSS_THROW("No block currently in scope"); |
| 796 | |
| 797 | uint32_t result_type = ops[0]; |
| 798 | uint32_t id = ops[1]; |
| 799 | |
| 800 | // Instead of a temporary, create a new function-wide temporary with this ID instead. |
| 801 | auto &var = set<SPIRVariable>(id, result_type, spv::StorageClassFunction); |
| 802 | var.phi_variable = true; |
| 803 | |
| 804 | current_function->add_local_variable(id); |
| 805 | |
| 806 | for (uint32_t i = 2; i + 2 <= length; i += 2) |
| 807 | current_block->phi_variables.push_back({ ops[i], ops[i + 1], id }); |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | // Constants |
| 812 | case OpSpecConstant: |
| 813 | case OpConstant: |
| 814 | { |
| 815 | uint32_t id = ops[1]; |
| 816 | auto &type = get<SPIRType>(ops[0]); |
| 817 | |
| 818 | if (type.width > 32) |
| 819 | set<SPIRConstant>(id, ops[0], ops[2] | (uint64_t(ops[3]) << 32), op == OpSpecConstant); |
| 820 | else |
| 821 | set<SPIRConstant>(id, ops[0], ops[2], op == OpSpecConstant); |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | case OpSpecConstantFalse: |
| 826 | case OpConstantFalse: |
| 827 | { |
| 828 | uint32_t id = ops[1]; |
| 829 | set<SPIRConstant>(id, ops[0], uint32_t(0), op == OpSpecConstantFalse); |
| 830 | break; |
| 831 | } |
| 832 | |
| 833 | case OpSpecConstantTrue: |
| 834 | case OpConstantTrue: |
| 835 | { |
| 836 | uint32_t id = ops[1]; |
| 837 | set<SPIRConstant>(id, ops[0], uint32_t(1), op == OpSpecConstantTrue); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | case OpConstantNull: |
| 842 | { |
| 843 | uint32_t id = ops[1]; |
| 844 | uint32_t type = ops[0]; |
Hans-Kristian Arntzen | b8905bb | 2020-03-26 11:21:23 +0100 | [diff] [blame] | 845 | ir.make_constant_null(id, type, true); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 846 | break; |
| 847 | } |
| 848 | |
| 849 | case OpSpecConstantComposite: |
| 850 | case OpConstantComposite: |
| 851 | { |
| 852 | uint32_t id = ops[1]; |
| 853 | uint32_t type = ops[0]; |
| 854 | |
| 855 | auto &ctype = get<SPIRType>(type); |
| 856 | |
| 857 | // We can have constants which are structs and arrays. |
| 858 | // In this case, our SPIRConstant will be a list of other SPIRConstant ids which we |
| 859 | // can refer to. |
| 860 | if (ctype.basetype == SPIRType::Struct || !ctype.array.empty()) |
| 861 | { |
| 862 | set<SPIRConstant>(id, type, ops + 2, length - 2, op == OpSpecConstantComposite); |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | uint32_t elements = length - 2; |
| 867 | if (elements > 4) |
| 868 | SPIRV_CROSS_THROW("OpConstantComposite only supports 1, 2, 3 and 4 elements."); |
| 869 | |
| 870 | SPIRConstant remapped_constant_ops[4]; |
| 871 | const SPIRConstant *c[4]; |
| 872 | for (uint32_t i = 0; i < elements; i++) |
| 873 | { |
| 874 | // Specialization constants operations can also be part of this. |
| 875 | // We do not know their value, so any attempt to query SPIRConstant later |
| 876 | // will fail. We can only propagate the ID of the expression and use to_expression on it. |
| 877 | auto *constant_op = maybe_get<SPIRConstantOp>(ops[2 + i]); |
Hans-Kristian Arntzen | df3e21a | 2019-03-27 10:51:23 +0100 | [diff] [blame] | 878 | auto *undef_op = maybe_get<SPIRUndef>(ops[2 + i]); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 879 | if (constant_op) |
| 880 | { |
| 881 | if (op == OpConstantComposite) |
| 882 | SPIRV_CROSS_THROW("Specialization constant operation used in OpConstantComposite."); |
| 883 | |
| 884 | remapped_constant_ops[i].make_null(get<SPIRType>(constant_op->basetype)); |
| 885 | remapped_constant_ops[i].self = constant_op->self; |
| 886 | remapped_constant_ops[i].constant_type = constant_op->basetype; |
| 887 | remapped_constant_ops[i].specialization = true; |
| 888 | c[i] = &remapped_constant_ops[i]; |
| 889 | } |
Hans-Kristian Arntzen | df3e21a | 2019-03-27 10:51:23 +0100 | [diff] [blame] | 890 | else if (undef_op) |
| 891 | { |
| 892 | // Undefined, just pick 0. |
| 893 | remapped_constant_ops[i].make_null(get<SPIRType>(undef_op->basetype)); |
| 894 | remapped_constant_ops[i].constant_type = undef_op->basetype; |
| 895 | c[i] = &remapped_constant_ops[i]; |
| 896 | } |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 897 | else |
| 898 | c[i] = &get<SPIRConstant>(ops[2 + i]); |
| 899 | } |
| 900 | set<SPIRConstant>(id, type, c, elements, op == OpSpecConstantComposite); |
| 901 | } |
| 902 | break; |
| 903 | } |
| 904 | |
| 905 | // Functions |
| 906 | case OpFunction: |
| 907 | { |
| 908 | uint32_t res = ops[0]; |
| 909 | uint32_t id = ops[1]; |
| 910 | // Control |
| 911 | uint32_t type = ops[3]; |
| 912 | |
| 913 | if (current_function) |
| 914 | SPIRV_CROSS_THROW("Must end a function before starting a new one!"); |
| 915 | |
| 916 | current_function = &set<SPIRFunction>(id, res, type); |
| 917 | break; |
| 918 | } |
| 919 | |
| 920 | case OpFunctionParameter: |
| 921 | { |
| 922 | uint32_t type = ops[0]; |
| 923 | uint32_t id = ops[1]; |
| 924 | |
| 925 | if (!current_function) |
| 926 | SPIRV_CROSS_THROW("Must be in a function!"); |
| 927 | |
| 928 | current_function->add_parameter(type, id); |
| 929 | set<SPIRVariable>(id, type, StorageClassFunction); |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | case OpFunctionEnd: |
| 934 | { |
| 935 | if (current_block) |
| 936 | { |
| 937 | // Very specific error message, but seems to come up quite often. |
| 938 | SPIRV_CROSS_THROW( |
| 939 | "Cannot end a function before ending the current block.\n" |
| 940 | "Likely cause: If this SPIR-V was created from glslang HLSL, make sure the entry point is valid."); |
| 941 | } |
| 942 | current_function = nullptr; |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | // Blocks |
| 947 | case OpLabel: |
| 948 | { |
| 949 | // OpLabel always starts a block. |
| 950 | if (!current_function) |
| 951 | SPIRV_CROSS_THROW("Blocks cannot exist outside functions!"); |
| 952 | |
| 953 | uint32_t id = ops[0]; |
| 954 | |
| 955 | current_function->blocks.push_back(id); |
| 956 | if (!current_function->entry_block) |
| 957 | current_function->entry_block = id; |
| 958 | |
| 959 | if (current_block) |
| 960 | SPIRV_CROSS_THROW("Cannot start a block before ending the current block."); |
| 961 | |
| 962 | current_block = &set<SPIRBlock>(id); |
| 963 | break; |
| 964 | } |
| 965 | |
| 966 | // Branch instructions end blocks. |
| 967 | case OpBranch: |
| 968 | { |
| 969 | if (!current_block) |
| 970 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 971 | |
| 972 | uint32_t target = ops[0]; |
| 973 | current_block->terminator = SPIRBlock::Direct; |
| 974 | current_block->next_block = target; |
| 975 | current_block = nullptr; |
| 976 | break; |
| 977 | } |
| 978 | |
| 979 | case OpBranchConditional: |
| 980 | { |
| 981 | if (!current_block) |
| 982 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 983 | |
| 984 | current_block->condition = ops[0]; |
| 985 | current_block->true_block = ops[1]; |
| 986 | current_block->false_block = ops[2]; |
| 987 | |
| 988 | current_block->terminator = SPIRBlock::Select; |
Hans-Kristian Arntzen | 2714f54 | 2021-11-07 12:37:23 +0100 | [diff] [blame] | 989 | |
| 990 | if (current_block->true_block == current_block->false_block) |
| 991 | { |
| 992 | // Bogus conditional, translate to a direct branch. |
| 993 | // Avoids some ugly edge cases later when analyzing CFGs. |
| 994 | |
| 995 | // There are some super jank cases where the merge block is different from the true/false, |
| 996 | // and later branches can "break" out of the selection construct this way. |
| 997 | // This is complete nonsense, but CTS hits this case. |
| 998 | // In this scenario, we should see the selection construct as more of a Switch with one default case. |
| 999 | // The problem here is that this breaks any attempt to break out of outer switch statements, |
| 1000 | // but it's theoretically solvable if this ever comes up using the ladder breaking system ... |
| 1001 | |
| 1002 | if (current_block->true_block != current_block->next_block && |
| 1003 | current_block->merge == SPIRBlock::MergeSelection) |
| 1004 | { |
| 1005 | uint32_t ids = ir.increase_bound_by(2); |
| 1006 | |
| 1007 | SPIRType type; |
| 1008 | type.basetype = SPIRType::Int; |
| 1009 | type.width = 32; |
| 1010 | set<SPIRType>(ids, type); |
| 1011 | auto &c = set<SPIRConstant>(ids + 1, ids); |
| 1012 | |
| 1013 | current_block->condition = c.self; |
| 1014 | current_block->default_block = current_block->true_block; |
| 1015 | current_block->terminator = SPIRBlock::MultiSelect; |
| 1016 | ir.block_meta[current_block->next_block] &= ~ParsedIR::BLOCK_META_SELECTION_MERGE_BIT; |
| 1017 | ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT; |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | ir.block_meta[current_block->next_block] &= ~ParsedIR::BLOCK_META_SELECTION_MERGE_BIT; |
| 1022 | current_block->next_block = current_block->true_block; |
| 1023 | current_block->condition = 0; |
| 1024 | current_block->true_block = 0; |
| 1025 | current_block->false_block = 0; |
| 1026 | current_block->merge_block = 0; |
| 1027 | current_block->merge = SPIRBlock::MergeNone; |
| 1028 | current_block->terminator = SPIRBlock::Direct; |
| 1029 | } |
| 1030 | } |
| 1031 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1032 | current_block = nullptr; |
| 1033 | break; |
| 1034 | } |
| 1035 | |
| 1036 | case OpSwitch: |
| 1037 | { |
| 1038 | if (!current_block) |
| 1039 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 1040 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1041 | current_block->terminator = SPIRBlock::MultiSelect; |
| 1042 | |
| 1043 | current_block->condition = ops[0]; |
| 1044 | current_block->default_block = ops[1]; |
| 1045 | |
Sebastián Aedo | f099d71 | 2021-11-02 17:17:13 -0300 | [diff] [blame] | 1046 | uint32_t remaining_ops = length - 2; |
Sebastián Aedo | 75e3752 | 2021-11-12 10:17:38 -0300 | [diff] [blame] | 1047 | if ((remaining_ops % 2) == 0) |
| 1048 | { |
| 1049 | for (uint32_t i = 2; i + 2 <= length; i += 2) |
| 1050 | current_block->cases_32bit.push_back({ ops[i], ops[i + 1] }); |
| 1051 | } |
Sebastián Aedo | f099d71 | 2021-11-02 17:17:13 -0300 | [diff] [blame] | 1052 | |
Sebastián Aedo | 4804664 | 2021-11-08 15:18:13 -0300 | [diff] [blame] | 1053 | if ((remaining_ops % 3) == 0) |
Sebastián Aedo | 75e3752 | 2021-11-12 10:17:38 -0300 | [diff] [blame] | 1054 | { |
Sebastián Aedo | 4804664 | 2021-11-08 15:18:13 -0300 | [diff] [blame] | 1055 | for (uint32_t i = 2; i + 3 <= length; i += 3) |
| 1056 | { |
Sebastián Aedo | 6d8302e | 2021-11-18 16:08:59 -0300 | [diff] [blame] | 1057 | uint64_t value = (static_cast<uint64_t>(ops[i + 1]) << 32) | ops[i]; |
Sebastián Aedo | f099d71 | 2021-11-02 17:17:13 -0300 | [diff] [blame] | 1058 | current_block->cases_64bit.push_back({ value, ops[i + 2] }); |
Sebastián Aedo | 3eb5532 | 2021-10-28 19:57:41 -0300 | [diff] [blame] | 1059 | } |
Sebastián Aedo | 75e3752 | 2021-11-12 10:17:38 -0300 | [diff] [blame] | 1060 | } |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1061 | |
| 1062 | // If we jump to next block, make it break instead since we're inside a switch case block at that point. |
| 1063 | ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT; |
| 1064 | |
| 1065 | current_block = nullptr; |
| 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | case OpKill: |
| 1070 | { |
| 1071 | if (!current_block) |
| 1072 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 1073 | current_block->terminator = SPIRBlock::Kill; |
| 1074 | current_block = nullptr; |
| 1075 | break; |
| 1076 | } |
| 1077 | |
Hans-Kristian Arntzen | 2097c30 | 2021-01-08 11:37:29 +0100 | [diff] [blame] | 1078 | case OpTerminateRayKHR: |
| 1079 | // NV variant is not a terminator. |
| 1080 | if (!current_block) |
| 1081 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 1082 | current_block->terminator = SPIRBlock::TerminateRay; |
| 1083 | current_block = nullptr; |
| 1084 | break; |
| 1085 | |
| 1086 | case OpIgnoreIntersectionKHR: |
| 1087 | // NV variant is not a terminator. |
| 1088 | if (!current_block) |
| 1089 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 1090 | current_block->terminator = SPIRBlock::IgnoreIntersection; |
| 1091 | current_block = nullptr; |
| 1092 | break; |
| 1093 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1094 | case OpReturn: |
| 1095 | { |
| 1096 | if (!current_block) |
| 1097 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 1098 | current_block->terminator = SPIRBlock::Return; |
| 1099 | current_block = nullptr; |
| 1100 | break; |
| 1101 | } |
| 1102 | |
| 1103 | case OpReturnValue: |
| 1104 | { |
| 1105 | if (!current_block) |
| 1106 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 1107 | current_block->terminator = SPIRBlock::Return; |
| 1108 | current_block->return_value = ops[0]; |
| 1109 | current_block = nullptr; |
| 1110 | break; |
| 1111 | } |
| 1112 | |
| 1113 | case OpUnreachable: |
| 1114 | { |
| 1115 | if (!current_block) |
| 1116 | SPIRV_CROSS_THROW("Trying to end a non-existing block."); |
| 1117 | current_block->terminator = SPIRBlock::Unreachable; |
| 1118 | current_block = nullptr; |
| 1119 | break; |
| 1120 | } |
| 1121 | |
| 1122 | case OpSelectionMerge: |
| 1123 | { |
| 1124 | if (!current_block) |
| 1125 | SPIRV_CROSS_THROW("Trying to modify a non-existing block."); |
| 1126 | |
| 1127 | current_block->next_block = ops[0]; |
| 1128 | current_block->merge = SPIRBlock::MergeSelection; |
| 1129 | ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_SELECTION_MERGE_BIT; |
| 1130 | |
| 1131 | if (length >= 2) |
| 1132 | { |
| 1133 | if (ops[1] & SelectionControlFlattenMask) |
| 1134 | current_block->hint = SPIRBlock::HintFlatten; |
| 1135 | else if (ops[1] & SelectionControlDontFlattenMask) |
| 1136 | current_block->hint = SPIRBlock::HintDontFlatten; |
| 1137 | } |
| 1138 | break; |
| 1139 | } |
| 1140 | |
| 1141 | case OpLoopMerge: |
| 1142 | { |
| 1143 | if (!current_block) |
| 1144 | SPIRV_CROSS_THROW("Trying to modify a non-existing block."); |
| 1145 | |
| 1146 | current_block->merge_block = ops[0]; |
| 1147 | current_block->continue_block = ops[1]; |
| 1148 | current_block->merge = SPIRBlock::MergeLoop; |
| 1149 | |
| 1150 | ir.block_meta[current_block->self] |= ParsedIR::BLOCK_META_LOOP_HEADER_BIT; |
| 1151 | ir.block_meta[current_block->merge_block] |= ParsedIR::BLOCK_META_LOOP_MERGE_BIT; |
| 1152 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1153 | ir.continue_block_to_loop_header[current_block->continue_block] = BlockID(current_block->self); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1154 | |
| 1155 | // Don't add loop headers to continue blocks, |
| 1156 | // which would make it impossible branch into the loop header since |
| 1157 | // they are treated as continues. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1158 | if (current_block->continue_block != BlockID(current_block->self)) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1159 | ir.block_meta[current_block->continue_block] |= ParsedIR::BLOCK_META_CONTINUE_BIT; |
| 1160 | |
| 1161 | if (length >= 3) |
| 1162 | { |
| 1163 | if (ops[2] & LoopControlUnrollMask) |
| 1164 | current_block->hint = SPIRBlock::HintUnroll; |
| 1165 | else if (ops[2] & LoopControlDontUnrollMask) |
| 1166 | current_block->hint = SPIRBlock::HintDontUnroll; |
| 1167 | } |
| 1168 | break; |
| 1169 | } |
| 1170 | |
| 1171 | case OpSpecConstantOp: |
| 1172 | { |
| 1173 | if (length < 3) |
| 1174 | SPIRV_CROSS_THROW("OpSpecConstantOp not enough arguments."); |
| 1175 | |
| 1176 | uint32_t result_type = ops[0]; |
| 1177 | uint32_t id = ops[1]; |
| 1178 | auto spec_op = static_cast<Op>(ops[2]); |
| 1179 | |
| 1180 | set<SPIRConstantOp>(id, result_type, spec_op, ops + 3, length - 3); |
| 1181 | break; |
| 1182 | } |
| 1183 | |
Hans-Kristian Arntzen | 65af09d | 2019-05-28 13:41:46 +0200 | [diff] [blame] | 1184 | case OpLine: |
| 1185 | { |
| 1186 | // OpLine might come at global scope, but we don't care about those since they will not be declared in any |
| 1187 | // meaningful correct order. |
Hans-Kristian Arntzen | 48a7da4 | 2019-05-28 15:51:42 +0200 | [diff] [blame] | 1188 | // Ignore all OpLine directives which live outside a function. |
Hans-Kristian Arntzen | 65af09d | 2019-05-28 13:41:46 +0200 | [diff] [blame] | 1189 | if (current_block) |
| 1190 | current_block->ops.push_back(instruction); |
| 1191 | |
Hans-Kristian Arntzen | 48a7da4 | 2019-05-28 15:51:42 +0200 | [diff] [blame] | 1192 | // Line directives may arrive before first OpLabel. |
| 1193 | // Treat this as the line of the function declaration, |
| 1194 | // so warnings for arguments can propagate properly. |
| 1195 | if (current_function) |
Hans-Kristian Arntzen | 65af09d | 2019-05-28 13:41:46 +0200 | [diff] [blame] | 1196 | { |
| 1197 | // Store the first one we find and emit it before creating the function prototype. |
| 1198 | if (current_function->entry_line.file_id == 0) |
| 1199 | { |
| 1200 | current_function->entry_line.file_id = ops[0]; |
| 1201 | current_function->entry_line.line_literal = ops[1]; |
| 1202 | } |
| 1203 | } |
| 1204 | break; |
| 1205 | } |
| 1206 | |
Lifeng Pan | 5ca8779 | 2019-07-04 16:03:06 +0800 | [diff] [blame] | 1207 | case OpNoLine: |
| 1208 | { |
| 1209 | // OpNoLine might come at global scope. |
| 1210 | if (current_block) |
| 1211 | current_block->ops.push_back(instruction); |
Hans-Kristian Arntzen | 13378ad | 2019-07-05 10:25:18 +0200 | [diff] [blame] | 1212 | break; |
Lifeng Pan | 5ca8779 | 2019-07-04 16:03:06 +0800 | [diff] [blame] | 1213 | } |
| 1214 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1215 | // Actual opcodes. |
| 1216 | default: |
| 1217 | { |
Sebastián Aedo | 75e3752 | 2021-11-12 10:17:38 -0300 | [diff] [blame] | 1218 | if (length >= 2) |
Sebastián Aedo | 4804664 | 2021-11-08 15:18:13 -0300 | [diff] [blame] | 1219 | { |
Sebastián Aedo | f099d71 | 2021-11-02 17:17:13 -0300 | [diff] [blame] | 1220 | const auto *type = maybe_get<SPIRType>(ops[0]); |
Sebastián Aedo | 4804664 | 2021-11-08 15:18:13 -0300 | [diff] [blame] | 1221 | if (type) |
Sebastián Aedo | 250a029 | 2021-11-03 16:12:14 -0300 | [diff] [blame] | 1222 | ir.load_type_width.insert({ ops[1], type->width }); |
Sebastián Aedo | f099d71 | 2021-11-02 17:17:13 -0300 | [diff] [blame] | 1223 | } |
Hans-Kristian Arntzen | 29cc189 | 2022-02-16 11:49:24 +0100 | [diff] [blame^] | 1224 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1225 | if (!current_block) |
| 1226 | SPIRV_CROSS_THROW("Currently no block to insert opcode."); |
| 1227 | |
| 1228 | current_block->ops.push_back(instruction); |
| 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | bool Parser::types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const |
| 1235 | { |
| 1236 | if (a.basetype != b.basetype) |
| 1237 | return false; |
| 1238 | if (a.width != b.width) |
| 1239 | return false; |
| 1240 | if (a.vecsize != b.vecsize) |
| 1241 | return false; |
| 1242 | if (a.columns != b.columns) |
| 1243 | return false; |
| 1244 | if (a.array.size() != b.array.size()) |
| 1245 | return false; |
| 1246 | |
| 1247 | size_t array_count = a.array.size(); |
| 1248 | if (array_count && memcmp(a.array.data(), b.array.data(), array_count * sizeof(uint32_t)) != 0) |
| 1249 | return false; |
| 1250 | |
| 1251 | if (a.basetype == SPIRType::Image || a.basetype == SPIRType::SampledImage) |
| 1252 | { |
| 1253 | if (memcmp(&a.image, &b.image, sizeof(SPIRType::Image)) != 0) |
| 1254 | return false; |
| 1255 | } |
| 1256 | |
| 1257 | if (a.member_types.size() != b.member_types.size()) |
| 1258 | return false; |
| 1259 | |
| 1260 | size_t member_types = a.member_types.size(); |
| 1261 | for (size_t i = 0; i < member_types; i++) |
| 1262 | { |
| 1263 | if (!types_are_logically_equivalent(get<SPIRType>(a.member_types[i]), get<SPIRType>(b.member_types[i]))) |
| 1264 | return false; |
| 1265 | } |
| 1266 | |
| 1267 | return true; |
| 1268 | } |
| 1269 | |
| 1270 | bool Parser::variable_storage_is_aliased(const SPIRVariable &v) const |
| 1271 | { |
| 1272 | auto &type = get<SPIRType>(v.basetype); |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 1273 | |
| 1274 | auto *type_meta = ir.find_meta(type.self); |
| 1275 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1276 | bool ssbo = v.storage == StorageClassStorageBuffer || |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 1277 | (type_meta && type_meta->decoration.decoration_flags.get(DecorationBufferBlock)); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1278 | bool image = type.basetype == SPIRType::Image; |
| 1279 | bool counter = type.basetype == SPIRType::AtomicCounter; |
| 1280 | |
| 1281 | bool is_restrict; |
| 1282 | if (ssbo) |
| 1283 | is_restrict = ir.get_buffer_block_flags(v).get(DecorationRestrict); |
| 1284 | else |
| 1285 | is_restrict = ir.has_decoration(v.self, DecorationRestrict); |
| 1286 | |
| 1287 | return !is_restrict && (ssbo || image || counter); |
| 1288 | } |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1289 | } // namespace SPIRV_CROSS_NAMESPACE |