blob: 49eb1933c1c1ca05cdf285012752e195b65f0910 [file] [log] [blame]
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001/*
Hans-Kristian Arntzen47044822021-01-14 16:07:49 +01002 * Copyright 2018-2021 Arm Limited
Jon Leechf2a65542021-05-08 01:47:48 -07003 * SPDX-License-Identifier: Apache-2.0 OR MIT
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02004 *
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 Arntzencf1e9e02020-11-25 15:22:08 +010018/*
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 Arntzencf1e9e02020-11-25 15:22:08 +010022 */
23
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020024#include "spirv_parser.hpp"
25#include <assert.h>
26
27using namespace std;
28using namespace spv;
29
Hans-Kristian Arntzen9b92e682019-03-29 10:29:44 +010030namespace SPIRV_CROSS_NAMESPACE
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020031{
Hans-Kristian Arntzen3fe57d32019-04-09 12:46:23 +020032Parser::Parser(vector<uint32_t> spirv)
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020033{
Daniel Thornburgh44c33332022-03-02 23:02:38 +000034 ir.spirv = std::move(spirv);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020035}
36
37Parser::Parser(const uint32_t *spirv_data, size_t word_count)
38{
Hans-Kristian Arntzen3fe57d32019-04-09 12:46:23 +020039 ir.spirv = vector<uint32_t>(spirv_data, spirv_data + word_count);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020040}
41
Hans-Kristian Arntzenfa42ed32018-11-15 10:51:01 +010042static 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 Arntzen5bcf02f2018-10-05 11:30:57 +020054static 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
59static 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 Arntzenab1fa902019-05-07 09:53:40 +020069 case 0x10400: // SPIR-V 1.4
Hans-Kristian Arntzen02c34fe2019-09-19 10:26:04 +020070 case 0x10500: // SPIR-V 1.5
Hans-Kristian Arntzen7c122282022-01-06 14:16:28 +010071 case 0x10600: // SPIR-V 1.6
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +020072 return true;
73
74 default:
75 return false;
76 }
77}
78
79void 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 Arntzen92a42942020-02-14 12:57:01 +010097
98 const uint32_t MaximumNumberOfIDs = 0x3fffff;
99 if (bound > MaximumNumberOfIDs)
100 SPIRV_CROSS_THROW("ID bound exceeds limit of 0x3fffff.\n");
101
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200102 ir.set_id_bounds(bound);
103
104 uint32_t offset = 5;
105
Hans-Kristian Arntzena489ba72019-04-02 11:19:03 +0200106 SmallVector<Instruction> instructions;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200107 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 Arntzen58dad822020-05-25 11:05:42 +0200130 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 Arntzen5bcf02f2018-10-05 11:30:57 +0200140 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 Arntzenbc4cb1b2021-03-08 10:39:59 +0100144 if (ir.default_entry_point == 0)
145 SPIRV_CROSS_THROW("There is no entry point in the SPIR-V module.");
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200146}
147
148const 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 Arntzen3fe57d32019-04-09 12:46:23 +0200161static string extract_string(const vector<uint32_t> &spirv, uint32_t offset)
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200162{
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
180void 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
Hans-Kristian Arntzen4c345162022-09-05 12:31:22 +0200186 // HACK for glslang that might emit OpEmitMeshTasksEXT followed by return / branch.
187 // Instead of failing hard, just ignore it.
188 if (ignore_trailing_block_opcodes)
189 {
190 ignore_trailing_block_opcodes = false;
191 if (op == OpReturn || op == OpBranch || op == OpUnreachable)
192 return;
193 }
194
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200195 switch (op)
196 {
lifpan00a765e2018-11-15 09:04:36 +0800197 case OpSourceContinued:
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200198 case OpSourceExtension:
199 case OpNop:
lifpan91610962018-11-13 14:28:38 +0800200 case OpModuleProcessed:
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200201 break;
202
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +0200203 case OpString:
204 {
205 set<SPIRString>(ops[0], extract_string(ir.spirv, instruction.offset + 1));
206 break;
207 }
208
Hans-Kristian Arntzen2cc374a2019-04-24 14:12:50 +0200209 case OpMemoryModel:
210 ir.addressing_model = static_cast<AddressingModel>(ops[0]);
211 ir.memory_model = static_cast<MemoryModel>(ops[1]);
212 break;
213
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200214 case OpSource:
215 {
216 auto lang = static_cast<SourceLanguage>(ops[0]);
217 switch (lang)
218 {
219 case SourceLanguageESSL:
220 ir.source.es = true;
221 ir.source.version = ops[1];
222 ir.source.known = true;
223 ir.source.hlsl = false;
224 break;
225
226 case SourceLanguageGLSL:
227 ir.source.es = false;
228 ir.source.version = ops[1];
229 ir.source.known = true;
230 ir.source.hlsl = false;
231 break;
232
233 case SourceLanguageHLSL:
234 // For purposes of cross-compiling, this is GLSL 450.
235 ir.source.es = false;
236 ir.source.version = 450;
237 ir.source.known = true;
238 ir.source.hlsl = true;
239 break;
240
241 default:
242 ir.source.known = false;
243 break;
244 }
245 break;
246 }
247
248 case OpUndef:
249 {
250 uint32_t result_type = ops[0];
251 uint32_t id = ops[1];
252 set<SPIRUndef>(id, result_type);
lifpan876627d2019-04-08 19:45:31 +0800253 if (current_block)
254 current_block->ops.push_back(instruction);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200255 break;
256 }
257
258 case OpCapability:
259 {
260 uint32_t cap = ops[0];
261 if (cap == CapabilityKernel)
262 SPIRV_CROSS_THROW("Kernel capability not supported.");
263
264 ir.declared_capabilities.push_back(static_cast<Capability>(ops[0]));
265 break;
266 }
267
268 case OpExtension:
269 {
270 auto ext = extract_string(ir.spirv, instruction.offset);
Daniel Thornburgh44c33332022-03-02 23:02:38 +0000271 ir.declared_extensions.push_back(std::move(ext));
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200272 break;
273 }
274
275 case OpExtInstImport:
276 {
277 uint32_t id = ops[0];
278 auto ext = extract_string(ir.spirv, instruction.offset + 1);
279 if (ext == "GLSL.std.450")
280 set<SPIRExtension>(id, SPIRExtension::GLSL);
Lifeng Pan5ca87792019-07-04 16:03:06 +0800281 else if (ext == "DebugInfo")
282 set<SPIRExtension>(id, SPIRExtension::SPV_debug_info);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200283 else if (ext == "SPV_AMD_shader_ballot")
284 set<SPIRExtension>(id, SPIRExtension::SPV_AMD_shader_ballot);
285 else if (ext == "SPV_AMD_shader_explicit_vertex_parameter")
286 set<SPIRExtension>(id, SPIRExtension::SPV_AMD_shader_explicit_vertex_parameter);
287 else if (ext == "SPV_AMD_shader_trinary_minmax")
288 set<SPIRExtension>(id, SPIRExtension::SPV_AMD_shader_trinary_minmax);
289 else if (ext == "SPV_AMD_gcn_shader")
290 set<SPIRExtension>(id, SPIRExtension::SPV_AMD_gcn_shader);
Hans-Kristian Arntzend2a4f982022-04-19 12:07:54 +0200291 else if (ext == "NonSemantic.DebugPrintf")
292 set<SPIRExtension>(id, SPIRExtension::NonSemanticDebugPrintf);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200293 else
294 set<SPIRExtension>(id, SPIRExtension::Unsupported);
295
296 // Other SPIR-V extensions which have ExtInstrs are currently not supported.
297
298 break;
299 }
300
Lifeng Pan5ca87792019-07-04 16:03:06 +0800301 case OpExtInst:
302 {
303 // The SPIR-V debug information extended instructions might come at global scope.
304 if (current_block)
Hans-Kristian Arntzen29cc1892022-02-16 11:49:24 +0100305 {
Lifeng Pan5ca87792019-07-04 16:03:06 +0800306 current_block->ops.push_back(instruction);
Hans-Kristian Arntzen29cc1892022-02-16 11:49:24 +0100307 if (length >= 2)
308 {
309 const auto *type = maybe_get<SPIRType>(ops[0]);
310 if (type)
311 ir.load_type_width.insert({ ops[1], type->width });
312 }
313 }
Lifeng Pan5ca87792019-07-04 16:03:06 +0800314 break;
315 }
316
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200317 case OpEntryPoint:
318 {
319 auto itr =
320 ir.entry_points.insert(make_pair(ops[1], SPIREntryPoint(ops[1], static_cast<ExecutionModel>(ops[0]),
321 extract_string(ir.spirv, instruction.offset + 2))));
322 auto &e = itr.first->second;
323
324 // Strings need nul-terminator and consume the whole word.
325 uint32_t strlen_words = uint32_t((e.name.size() + 1 + 3) >> 2);
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +0200326
327 for (uint32_t i = strlen_words + 2; i < instruction.length; i++)
328 e.interface_variables.push_back(ops[i]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200329
330 // Set the name of the entry point in case OpName is not provided later.
331 ir.set_name(ops[1], e.name);
332
333 // If we don't have an entry, make the first one our "default".
334 if (!ir.default_entry_point)
335 ir.default_entry_point = ops[1];
336 break;
337 }
338
339 case OpExecutionMode:
340 {
341 auto &execution = ir.entry_points[ops[0]];
342 auto mode = static_cast<ExecutionMode>(ops[1]);
343 execution.flags.set(mode);
344
345 switch (mode)
346 {
347 case ExecutionModeInvocations:
348 execution.invocations = ops[2];
349 break;
350
351 case ExecutionModeLocalSize:
352 execution.workgroup_size.x = ops[2];
353 execution.workgroup_size.y = ops[3];
354 execution.workgroup_size.z = ops[4];
355 break;
356
357 case ExecutionModeOutputVertices:
358 execution.output_vertices = ops[2];
359 break;
360
Hans-Kristian Arntzen57626172022-09-02 16:31:04 +0200361 case ExecutionModeOutputPrimitivesEXT:
362 execution.output_primitives = ops[2];
363 break;
364
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200365 default:
366 break;
367 }
368 break;
369 }
370
Hans-Kristian Arntzen7c83fc22022-01-05 15:53:51 +0100371 case OpExecutionModeId:
372 {
373 auto &execution = ir.entry_points[ops[0]];
374 auto mode = static_cast<ExecutionMode>(ops[1]);
375 execution.flags.set(mode);
376
377 if (mode == ExecutionModeLocalSizeId)
378 {
379 execution.workgroup_size.id_x = ops[2];
380 execution.workgroup_size.id_y = ops[3];
381 execution.workgroup_size.id_z = ops[4];
382 }
383
384 break;
385 }
386
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200387 case OpName:
388 {
389 uint32_t id = ops[0];
390 ir.set_name(id, extract_string(ir.spirv, instruction.offset + 1));
391 break;
392 }
393
394 case OpMemberName:
395 {
396 uint32_t id = ops[0];
397 uint32_t member = ops[1];
398 ir.set_member_name(id, member, extract_string(ir.spirv, instruction.offset + 2));
399 break;
400 }
401
Hans-Kristian Arntzenfa42ed32018-11-15 10:51:01 +0100402 case OpDecorationGroup:
403 {
404 // Noop, this simply means an ID should be a collector of decorations.
405 // The meta array is already a flat array of decorations which will contain the relevant decorations.
406 break;
407 }
408
409 case OpGroupDecorate:
410 {
411 uint32_t group_id = ops[0];
412 auto &decorations = ir.meta[group_id].decoration;
413 auto &flags = decorations.decoration_flags;
414
415 // Copies decorations from one ID to another. Only copy decorations which are set in the group,
416 // i.e., we cannot just copy the meta structure directly.
417 for (uint32_t i = 1; i < length; i++)
418 {
419 uint32_t target = ops[i];
420 flags.for_each_bit([&](uint32_t bit) {
421 auto decoration = static_cast<Decoration>(bit);
422
423 if (decoration_is_string(decoration))
424 {
425 ir.set_decoration_string(target, decoration, ir.get_decoration_string(group_id, decoration));
426 }
427 else
428 {
429 ir.meta[target].decoration_word_offset[decoration] =
430 ir.meta[group_id].decoration_word_offset[decoration];
431 ir.set_decoration(target, decoration, ir.get_decoration(group_id, decoration));
432 }
433 });
434 }
435 break;
436 }
437
438 case OpGroupMemberDecorate:
439 {
440 uint32_t group_id = ops[0];
441 auto &flags = ir.meta[group_id].decoration.decoration_flags;
442
443 // Copies decorations from one ID to another. Only copy decorations which are set in the group,
444 // i.e., we cannot just copy the meta structure directly.
445 for (uint32_t i = 1; i + 1 < length; i += 2)
446 {
447 uint32_t target = ops[i + 0];
448 uint32_t index = ops[i + 1];
449 flags.for_each_bit([&](uint32_t bit) {
450 auto decoration = static_cast<Decoration>(bit);
451
452 if (decoration_is_string(decoration))
453 ir.set_member_decoration_string(target, index, decoration,
454 ir.get_decoration_string(group_id, decoration));
455 else
456 ir.set_member_decoration(target, index, decoration, ir.get_decoration(group_id, decoration));
457 });
458 }
459 break;
460 }
461
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200462 case OpDecorate:
463 case OpDecorateId:
464 {
Hans-Kristian Arntzenfa42ed32018-11-15 10:51:01 +0100465 // OpDecorateId technically supports an array of arguments, but our only supported decorations are single uint,
466 // so merge decorate and decorate-id here.
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200467 uint32_t id = ops[0];
468
469 auto decoration = static_cast<Decoration>(ops[1]);
470 if (length >= 3)
471 {
472 ir.meta[id].decoration_word_offset[decoration] = uint32_t(&ops[2] - ir.spirv.data());
473 ir.set_decoration(id, decoration, ops[2]);
474 }
475 else
476 ir.set_decoration(id, decoration);
477
478 break;
479 }
480
481 case OpDecorateStringGOOGLE:
482 {
483 uint32_t id = ops[0];
484 auto decoration = static_cast<Decoration>(ops[1]);
485 ir.set_decoration_string(id, decoration, extract_string(ir.spirv, instruction.offset + 2));
486 break;
487 }
488
489 case OpMemberDecorate:
490 {
491 uint32_t id = ops[0];
492 uint32_t member = ops[1];
493 auto decoration = static_cast<Decoration>(ops[2]);
494 if (length >= 4)
495 ir.set_member_decoration(id, member, decoration, ops[3]);
496 else
497 ir.set_member_decoration(id, member, decoration);
498 break;
499 }
500
501 case OpMemberDecorateStringGOOGLE:
502 {
503 uint32_t id = ops[0];
504 uint32_t member = ops[1];
505 auto decoration = static_cast<Decoration>(ops[2]);
506 ir.set_member_decoration_string(id, member, decoration, extract_string(ir.spirv, instruction.offset + 3));
507 break;
508 }
509
510 // Build up basic types.
511 case OpTypeVoid:
512 {
513 uint32_t id = ops[0];
514 auto &type = set<SPIRType>(id);
515 type.basetype = SPIRType::Void;
516 break;
517 }
518
519 case OpTypeBool:
520 {
521 uint32_t id = ops[0];
522 auto &type = set<SPIRType>(id);
523 type.basetype = SPIRType::Boolean;
524 type.width = 1;
525 break;
526 }
527
528 case OpTypeFloat:
529 {
530 uint32_t id = ops[0];
531 uint32_t width = ops[1];
532 auto &type = set<SPIRType>(id);
533 if (width == 64)
534 type.basetype = SPIRType::Double;
535 else if (width == 32)
536 type.basetype = SPIRType::Float;
537 else if (width == 16)
538 type.basetype = SPIRType::Half;
539 else
540 SPIRV_CROSS_THROW("Unrecognized bit-width of floating point type.");
541 type.width = width;
542 break;
543 }
544
545 case OpTypeInt:
546 {
547 uint32_t id = ops[0];
548 uint32_t width = ops[1];
lifpanb21525b2018-11-28 14:20:24 +0800549 bool signedness = ops[2] != 0;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200550 auto &type = set<SPIRType>(id);
Hans-Kristian Arntzen2ed171e2019-01-30 14:49:55 +0100551 type.basetype = signedness ? to_signed_basetype(width) : to_unsigned_basetype(width);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200552 type.width = width;
553 break;
554 }
555
556 // Build composite types by "inheriting".
557 // NOTE: The self member is also copied! For pointers and array modifiers this is a good thing
558 // since we can refer to decorations on pointee classes which is needed for UBO/SSBO, I/O blocks in geometry/tess etc.
559 case OpTypeVector:
560 {
561 uint32_t id = ops[0];
562 uint32_t vecsize = ops[2];
563
564 auto &base = get<SPIRType>(ops[1]);
565 auto &vecbase = set<SPIRType>(id);
566
567 vecbase = base;
568 vecbase.vecsize = vecsize;
569 vecbase.self = id;
570 vecbase.parent_type = ops[1];
571 break;
572 }
573
574 case OpTypeMatrix:
575 {
576 uint32_t id = ops[0];
577 uint32_t colcount = ops[2];
578
579 auto &base = get<SPIRType>(ops[1]);
580 auto &matrixbase = set<SPIRType>(id);
581
582 matrixbase = base;
583 matrixbase.columns = colcount;
584 matrixbase.self = id;
585 matrixbase.parent_type = ops[1];
586 break;
587 }
588
589 case OpTypeArray:
590 {
591 uint32_t id = ops[0];
592 auto &arraybase = set<SPIRType>(id);
593
594 uint32_t tid = ops[1];
595 auto &base = get<SPIRType>(tid);
596
597 arraybase = base;
598 arraybase.parent_type = tid;
599
600 uint32_t cid = ops[2];
601 ir.mark_used_as_array_length(cid);
602 auto *c = maybe_get<SPIRConstant>(cid);
603 bool literal = c && !c->specialization;
604
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200605 // We're copying type information into Array types, so we'll need a fixup for any physical pointer
606 // references.
607 if (base.forward_pointer)
608 forward_pointer_fixups.push_back({ id, tid });
609
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200610 arraybase.array_size_literal.push_back(literal);
611 arraybase.array.push_back(literal ? c->scalar() : cid);
612 // Do NOT set arraybase.self!
613 break;
614 }
615
616 case OpTypeRuntimeArray:
617 {
618 uint32_t id = ops[0];
619
620 auto &base = get<SPIRType>(ops[1]);
621 auto &arraybase = set<SPIRType>(id);
622
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200623 // We're copying type information into Array types, so we'll need a fixup for any physical pointer
624 // references.
625 if (base.forward_pointer)
626 forward_pointer_fixups.push_back({ id, ops[1] });
627
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200628 arraybase = base;
629 arraybase.array.push_back(0);
630 arraybase.array_size_literal.push_back(true);
631 arraybase.parent_type = ops[1];
632 // Do NOT set arraybase.self!
633 break;
634 }
635
636 case OpTypeImage:
637 {
638 uint32_t id = ops[0];
639 auto &type = set<SPIRType>(id);
640 type.basetype = SPIRType::Image;
641 type.image.type = ops[1];
642 type.image.dim = static_cast<Dim>(ops[2]);
643 type.image.depth = ops[3] == 1;
644 type.image.arrayed = ops[4] != 0;
645 type.image.ms = ops[5] != 0;
646 type.image.sampled = ops[6];
647 type.image.format = static_cast<ImageFormat>(ops[7]);
648 type.image.access = (length >= 9) ? static_cast<AccessQualifier>(ops[8]) : AccessQualifierMax;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200649 break;
650 }
651
652 case OpTypeSampledImage:
653 {
654 uint32_t id = ops[0];
655 uint32_t imagetype = ops[1];
656 auto &type = set<SPIRType>(id);
657 type = get<SPIRType>(imagetype);
658 type.basetype = SPIRType::SampledImage;
659 type.self = id;
660 break;
661 }
662
663 case OpTypeSampler:
664 {
665 uint32_t id = ops[0];
666 auto &type = set<SPIRType>(id);
667 type.basetype = SPIRType::Sampler;
668 break;
669 }
670
671 case OpTypePointer:
672 {
673 uint32_t id = ops[0];
674
Hans-Kristian Arntzen1f018b02020-11-03 10:51:56 +0100675 // Very rarely, we might receive a FunctionPrototype here.
676 // We won't be able to compile it, but we shouldn't crash when parsing.
677 // We should be able to reflect.
678 auto *base = maybe_get<SPIRType>(ops[2]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200679 auto &ptrbase = set<SPIRType>(id);
680
Hans-Kristian Arntzen1f018b02020-11-03 10:51:56 +0100681 if (base)
682 ptrbase = *base;
683
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200684 ptrbase.pointer = true;
Hans-Kristian Arntzend0b93722018-11-26 12:23:28 +0100685 ptrbase.pointer_depth++;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200686 ptrbase.storage = static_cast<StorageClass>(ops[1]);
687
688 if (ptrbase.storage == StorageClassAtomicCounter)
689 ptrbase.basetype = SPIRType::AtomicCounter;
690
Hans-Kristian Arntzen1f018b02020-11-03 10:51:56 +0100691 if (base && base->forward_pointer)
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200692 forward_pointer_fixups.push_back({ id, ops[2] });
693
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200694 ptrbase.parent_type = ops[2];
695
696 // Do NOT set ptrbase.self!
697 break;
698 }
699
Hans-Kristian Arntzen2cc374a2019-04-24 14:12:50 +0200700 case OpTypeForwardPointer:
701 {
702 uint32_t id = ops[0];
703 auto &ptrbase = set<SPIRType>(id);
704 ptrbase.pointer = true;
705 ptrbase.pointer_depth++;
706 ptrbase.storage = static_cast<StorageClass>(ops[1]);
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200707 ptrbase.forward_pointer = true;
Hans-Kristian Arntzen2cc374a2019-04-24 14:12:50 +0200708
709 if (ptrbase.storage == StorageClassAtomicCounter)
710 ptrbase.basetype = SPIRType::AtomicCounter;
711
712 break;
713 }
714
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200715 case OpTypeStruct:
716 {
717 uint32_t id = ops[0];
718 auto &type = set<SPIRType>(id);
719 type.basetype = SPIRType::Struct;
720 for (uint32_t i = 1; i < length; i++)
721 type.member_types.push_back(ops[i]);
722
723 // Check if we have seen this struct type before, with just different
724 // decorations.
725 //
726 // Add workaround for issue #17 as well by looking at OpName for the struct
727 // types, which we shouldn't normally do.
728 // We should not normally have to consider type aliases like this to begin with
729 // however ... glslang issues #304, #307 cover this.
730
731 // For stripped names, never consider struct type aliasing.
732 // We risk declaring the same struct multiple times, but type-punning is not allowed
733 // so this is safe.
734 bool consider_aliasing = !ir.get_name(type.self).empty();
735 if (consider_aliasing)
736 {
737 for (auto &other : global_struct_cache)
738 {
739 if (ir.get_name(type.self) == ir.get_name(other) &&
740 types_are_logically_equivalent(type, get<SPIRType>(other)))
741 {
742 type.type_alias = other;
743 break;
744 }
745 }
746
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +0200747 if (type.type_alias == TypeID(0))
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200748 global_struct_cache.push_back(id);
749 }
750 break;
751 }
752
753 case OpTypeFunction:
754 {
755 uint32_t id = ops[0];
756 uint32_t ret = ops[1];
757
758 auto &func = set<SPIRFunctionPrototype>(id, ret);
759 for (uint32_t i = 2; i < length; i++)
760 func.parameter_types.push_back(ops[i]);
761 break;
762 }
763
Hans-Kristian Arntzen6b0e5582020-04-21 14:25:18 +0200764 case OpTypeAccelerationStructureKHR:
Patrick Moursda39a7b2019-02-26 15:43:03 +0100765 {
766 uint32_t id = ops[0];
767 auto &type = set<SPIRType>(id);
Hans-Kristian Arntzen6b0e5582020-04-21 14:25:18 +0200768 type.basetype = SPIRType::AccelerationStructure;
769 break;
770 }
771
Hans-Kristian Arntzen1a28a042021-01-06 11:32:26 +0100772 case OpTypeRayQueryKHR:
Hans-Kristian Arntzen6b0e5582020-04-21 14:25:18 +0200773 {
774 uint32_t id = ops[0];
775 auto &type = set<SPIRType>(id);
776 type.basetype = SPIRType::RayQuery;
Patrick Moursda39a7b2019-02-26 15:43:03 +0100777 break;
778 }
779
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200780 // Variable declaration
781 // All variables are essentially pointers with a storage qualifier.
782 case OpVariable:
783 {
784 uint32_t type = ops[0];
785 uint32_t id = ops[1];
786 auto storage = static_cast<StorageClass>(ops[2]);
787 uint32_t initializer = length == 4 ? ops[3] : 0;
788
789 if (storage == StorageClassFunction)
790 {
791 if (!current_function)
792 SPIRV_CROSS_THROW("No function currently in scope");
793 current_function->add_local_variable(id);
794 }
795
796 set<SPIRVariable>(id, type, storage, initializer);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200797 break;
798 }
799
800 // OpPhi
801 // OpPhi is a fairly magical opcode.
802 // It selects temporary variables based on which parent block we *came from*.
803 // In high-level languages we can "de-SSA" by creating a function local, and flush out temporaries to this function-local
804 // variable to emulate SSA Phi.
805 case OpPhi:
806 {
807 if (!current_function)
808 SPIRV_CROSS_THROW("No function currently in scope");
809 if (!current_block)
810 SPIRV_CROSS_THROW("No block currently in scope");
811
812 uint32_t result_type = ops[0];
813 uint32_t id = ops[1];
814
815 // Instead of a temporary, create a new function-wide temporary with this ID instead.
816 auto &var = set<SPIRVariable>(id, result_type, spv::StorageClassFunction);
817 var.phi_variable = true;
818
819 current_function->add_local_variable(id);
820
821 for (uint32_t i = 2; i + 2 <= length; i += 2)
822 current_block->phi_variables.push_back({ ops[i], ops[i + 1], id });
823 break;
824 }
825
826 // Constants
827 case OpSpecConstant:
828 case OpConstant:
829 {
830 uint32_t id = ops[1];
831 auto &type = get<SPIRType>(ops[0]);
832
833 if (type.width > 32)
834 set<SPIRConstant>(id, ops[0], ops[2] | (uint64_t(ops[3]) << 32), op == OpSpecConstant);
835 else
836 set<SPIRConstant>(id, ops[0], ops[2], op == OpSpecConstant);
837 break;
838 }
839
840 case OpSpecConstantFalse:
841 case OpConstantFalse:
842 {
843 uint32_t id = ops[1];
844 set<SPIRConstant>(id, ops[0], uint32_t(0), op == OpSpecConstantFalse);
845 break;
846 }
847
848 case OpSpecConstantTrue:
849 case OpConstantTrue:
850 {
851 uint32_t id = ops[1];
852 set<SPIRConstant>(id, ops[0], uint32_t(1), op == OpSpecConstantTrue);
853 break;
854 }
855
856 case OpConstantNull:
857 {
858 uint32_t id = ops[1];
859 uint32_t type = ops[0];
Hans-Kristian Arntzenb8905bb2020-03-26 11:21:23 +0100860 ir.make_constant_null(id, type, true);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200861 break;
862 }
863
864 case OpSpecConstantComposite:
865 case OpConstantComposite:
866 {
867 uint32_t id = ops[1];
868 uint32_t type = ops[0];
869
870 auto &ctype = get<SPIRType>(type);
871
872 // We can have constants which are structs and arrays.
873 // In this case, our SPIRConstant will be a list of other SPIRConstant ids which we
874 // can refer to.
875 if (ctype.basetype == SPIRType::Struct || !ctype.array.empty())
876 {
877 set<SPIRConstant>(id, type, ops + 2, length - 2, op == OpSpecConstantComposite);
878 }
879 else
880 {
881 uint32_t elements = length - 2;
882 if (elements > 4)
883 SPIRV_CROSS_THROW("OpConstantComposite only supports 1, 2, 3 and 4 elements.");
884
885 SPIRConstant remapped_constant_ops[4];
886 const SPIRConstant *c[4];
887 for (uint32_t i = 0; i < elements; i++)
888 {
889 // Specialization constants operations can also be part of this.
890 // We do not know their value, so any attempt to query SPIRConstant later
891 // will fail. We can only propagate the ID of the expression and use to_expression on it.
892 auto *constant_op = maybe_get<SPIRConstantOp>(ops[2 + i]);
Hans-Kristian Arntzendf3e21a2019-03-27 10:51:23 +0100893 auto *undef_op = maybe_get<SPIRUndef>(ops[2 + i]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200894 if (constant_op)
895 {
896 if (op == OpConstantComposite)
897 SPIRV_CROSS_THROW("Specialization constant operation used in OpConstantComposite.");
898
899 remapped_constant_ops[i].make_null(get<SPIRType>(constant_op->basetype));
900 remapped_constant_ops[i].self = constant_op->self;
901 remapped_constant_ops[i].constant_type = constant_op->basetype;
902 remapped_constant_ops[i].specialization = true;
903 c[i] = &remapped_constant_ops[i];
904 }
Hans-Kristian Arntzendf3e21a2019-03-27 10:51:23 +0100905 else if (undef_op)
906 {
907 // Undefined, just pick 0.
908 remapped_constant_ops[i].make_null(get<SPIRType>(undef_op->basetype));
909 remapped_constant_ops[i].constant_type = undef_op->basetype;
910 c[i] = &remapped_constant_ops[i];
911 }
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200912 else
913 c[i] = &get<SPIRConstant>(ops[2 + i]);
914 }
915 set<SPIRConstant>(id, type, c, elements, op == OpSpecConstantComposite);
916 }
917 break;
918 }
919
920 // Functions
921 case OpFunction:
922 {
923 uint32_t res = ops[0];
924 uint32_t id = ops[1];
925 // Control
926 uint32_t type = ops[3];
927
928 if (current_function)
929 SPIRV_CROSS_THROW("Must end a function before starting a new one!");
930
931 current_function = &set<SPIRFunction>(id, res, type);
932 break;
933 }
934
935 case OpFunctionParameter:
936 {
937 uint32_t type = ops[0];
938 uint32_t id = ops[1];
939
940 if (!current_function)
941 SPIRV_CROSS_THROW("Must be in a function!");
942
943 current_function->add_parameter(type, id);
944 set<SPIRVariable>(id, type, StorageClassFunction);
945 break;
946 }
947
948 case OpFunctionEnd:
949 {
950 if (current_block)
951 {
952 // Very specific error message, but seems to come up quite often.
953 SPIRV_CROSS_THROW(
954 "Cannot end a function before ending the current block.\n"
955 "Likely cause: If this SPIR-V was created from glslang HLSL, make sure the entry point is valid.");
956 }
957 current_function = nullptr;
958 break;
959 }
960
961 // Blocks
962 case OpLabel:
963 {
964 // OpLabel always starts a block.
965 if (!current_function)
966 SPIRV_CROSS_THROW("Blocks cannot exist outside functions!");
967
968 uint32_t id = ops[0];
969
970 current_function->blocks.push_back(id);
971 if (!current_function->entry_block)
972 current_function->entry_block = id;
973
974 if (current_block)
975 SPIRV_CROSS_THROW("Cannot start a block before ending the current block.");
976
977 current_block = &set<SPIRBlock>(id);
978 break;
979 }
980
981 // Branch instructions end blocks.
982 case OpBranch:
983 {
984 if (!current_block)
985 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
986
987 uint32_t target = ops[0];
988 current_block->terminator = SPIRBlock::Direct;
989 current_block->next_block = target;
990 current_block = nullptr;
991 break;
992 }
993
994 case OpBranchConditional:
995 {
996 if (!current_block)
997 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
998
999 current_block->condition = ops[0];
1000 current_block->true_block = ops[1];
1001 current_block->false_block = ops[2];
1002
1003 current_block->terminator = SPIRBlock::Select;
Hans-Kristian Arntzen2714f542021-11-07 12:37:23 +01001004
1005 if (current_block->true_block == current_block->false_block)
1006 {
1007 // Bogus conditional, translate to a direct branch.
1008 // Avoids some ugly edge cases later when analyzing CFGs.
1009
1010 // There are some super jank cases where the merge block is different from the true/false,
1011 // and later branches can "break" out of the selection construct this way.
1012 // This is complete nonsense, but CTS hits this case.
1013 // In this scenario, we should see the selection construct as more of a Switch with one default case.
1014 // The problem here is that this breaks any attempt to break out of outer switch statements,
1015 // but it's theoretically solvable if this ever comes up using the ladder breaking system ...
1016
1017 if (current_block->true_block != current_block->next_block &&
1018 current_block->merge == SPIRBlock::MergeSelection)
1019 {
1020 uint32_t ids = ir.increase_bound_by(2);
1021
1022 SPIRType type;
1023 type.basetype = SPIRType::Int;
1024 type.width = 32;
1025 set<SPIRType>(ids, type);
1026 auto &c = set<SPIRConstant>(ids + 1, ids);
1027
1028 current_block->condition = c.self;
1029 current_block->default_block = current_block->true_block;
1030 current_block->terminator = SPIRBlock::MultiSelect;
1031 ir.block_meta[current_block->next_block] &= ~ParsedIR::BLOCK_META_SELECTION_MERGE_BIT;
1032 ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT;
1033 }
1034 else
1035 {
Hans-Kristian Arntzenbe904dc2022-07-22 14:36:31 +02001036 // Collapse loops if we have to.
1037 bool collapsed_loop = current_block->true_block == current_block->merge_block &&
1038 current_block->merge == SPIRBlock::MergeLoop;
1039
1040 if (collapsed_loop)
1041 {
1042 ir.block_meta[current_block->merge_block] &= ~ParsedIR::BLOCK_META_LOOP_MERGE_BIT;
1043 ir.block_meta[current_block->continue_block] &= ~ParsedIR::BLOCK_META_CONTINUE_BIT;
1044 }
1045
Hans-Kristian Arntzen2714f542021-11-07 12:37:23 +01001046 current_block->next_block = current_block->true_block;
1047 current_block->condition = 0;
1048 current_block->true_block = 0;
1049 current_block->false_block = 0;
1050 current_block->merge_block = 0;
1051 current_block->merge = SPIRBlock::MergeNone;
1052 current_block->terminator = SPIRBlock::Direct;
1053 }
1054 }
1055
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001056 current_block = nullptr;
1057 break;
1058 }
1059
1060 case OpSwitch:
1061 {
1062 if (!current_block)
1063 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1064
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001065 current_block->terminator = SPIRBlock::MultiSelect;
1066
1067 current_block->condition = ops[0];
1068 current_block->default_block = ops[1];
1069
Sebastián Aedof099d712021-11-02 17:17:13 -03001070 uint32_t remaining_ops = length - 2;
Sebastián Aedo75e37522021-11-12 10:17:38 -03001071 if ((remaining_ops % 2) == 0)
1072 {
1073 for (uint32_t i = 2; i + 2 <= length; i += 2)
1074 current_block->cases_32bit.push_back({ ops[i], ops[i + 1] });
1075 }
Sebastián Aedof099d712021-11-02 17:17:13 -03001076
Sebastián Aedo48046642021-11-08 15:18:13 -03001077 if ((remaining_ops % 3) == 0)
Sebastián Aedo75e37522021-11-12 10:17:38 -03001078 {
Sebastián Aedo48046642021-11-08 15:18:13 -03001079 for (uint32_t i = 2; i + 3 <= length; i += 3)
1080 {
Sebastián Aedo6d8302e2021-11-18 16:08:59 -03001081 uint64_t value = (static_cast<uint64_t>(ops[i + 1]) << 32) | ops[i];
Sebastián Aedof099d712021-11-02 17:17:13 -03001082 current_block->cases_64bit.push_back({ value, ops[i + 2] });
Sebastián Aedo3eb55322021-10-28 19:57:41 -03001083 }
Sebastián Aedo75e37522021-11-12 10:17:38 -03001084 }
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001085
1086 // If we jump to next block, make it break instead since we're inside a switch case block at that point.
1087 ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT;
1088
1089 current_block = nullptr;
1090 break;
1091 }
1092
1093 case OpKill:
Hans-Kristian Arntzendc62cc72022-03-03 10:36:07 +01001094 case OpTerminateInvocation:
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001095 {
1096 if (!current_block)
1097 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1098 current_block->terminator = SPIRBlock::Kill;
1099 current_block = nullptr;
1100 break;
1101 }
1102
Hans-Kristian Arntzen2097c302021-01-08 11:37:29 +01001103 case OpTerminateRayKHR:
1104 // NV variant is not a terminator.
1105 if (!current_block)
1106 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1107 current_block->terminator = SPIRBlock::TerminateRay;
1108 current_block = nullptr;
1109 break;
1110
1111 case OpIgnoreIntersectionKHR:
1112 // NV variant is not a terminator.
1113 if (!current_block)
1114 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1115 current_block->terminator = SPIRBlock::IgnoreIntersection;
1116 current_block = nullptr;
1117 break;
1118
Hans-Kristian Arntzen4c345162022-09-05 12:31:22 +02001119 case OpEmitMeshTasksEXT:
1120 if (!current_block)
1121 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1122 current_block->terminator = SPIRBlock::EmitMeshTasks;
1123 for (uint32_t i = 0; i < 3; i++)
1124 current_block->mesh.groups[i] = ops[i];
1125 current_block->mesh.payload = length >= 4 ? ops[3] : 0;
1126 current_block = nullptr;
1127 // Currently glslang is bugged and does not treat EmitMeshTasksEXT as a terminator.
1128 ignore_trailing_block_opcodes = true;
1129 break;
1130
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001131 case OpReturn:
1132 {
1133 if (!current_block)
1134 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1135 current_block->terminator = SPIRBlock::Return;
1136 current_block = nullptr;
1137 break;
1138 }
1139
1140 case OpReturnValue:
1141 {
1142 if (!current_block)
1143 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1144 current_block->terminator = SPIRBlock::Return;
1145 current_block->return_value = ops[0];
1146 current_block = nullptr;
1147 break;
1148 }
1149
1150 case OpUnreachable:
1151 {
1152 if (!current_block)
1153 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1154 current_block->terminator = SPIRBlock::Unreachable;
1155 current_block = nullptr;
1156 break;
1157 }
1158
1159 case OpSelectionMerge:
1160 {
1161 if (!current_block)
1162 SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
1163
1164 current_block->next_block = ops[0];
1165 current_block->merge = SPIRBlock::MergeSelection;
1166 ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_SELECTION_MERGE_BIT;
1167
1168 if (length >= 2)
1169 {
1170 if (ops[1] & SelectionControlFlattenMask)
1171 current_block->hint = SPIRBlock::HintFlatten;
1172 else if (ops[1] & SelectionControlDontFlattenMask)
1173 current_block->hint = SPIRBlock::HintDontFlatten;
1174 }
1175 break;
1176 }
1177
1178 case OpLoopMerge:
1179 {
1180 if (!current_block)
1181 SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
1182
1183 current_block->merge_block = ops[0];
1184 current_block->continue_block = ops[1];
1185 current_block->merge = SPIRBlock::MergeLoop;
1186
1187 ir.block_meta[current_block->self] |= ParsedIR::BLOCK_META_LOOP_HEADER_BIT;
1188 ir.block_meta[current_block->merge_block] |= ParsedIR::BLOCK_META_LOOP_MERGE_BIT;
1189
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +02001190 ir.continue_block_to_loop_header[current_block->continue_block] = BlockID(current_block->self);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001191
1192 // Don't add loop headers to continue blocks,
1193 // which would make it impossible branch into the loop header since
1194 // they are treated as continues.
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +02001195 if (current_block->continue_block != BlockID(current_block->self))
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001196 ir.block_meta[current_block->continue_block] |= ParsedIR::BLOCK_META_CONTINUE_BIT;
1197
1198 if (length >= 3)
1199 {
1200 if (ops[2] & LoopControlUnrollMask)
1201 current_block->hint = SPIRBlock::HintUnroll;
1202 else if (ops[2] & LoopControlDontUnrollMask)
1203 current_block->hint = SPIRBlock::HintDontUnroll;
1204 }
1205 break;
1206 }
1207
1208 case OpSpecConstantOp:
1209 {
1210 if (length < 3)
1211 SPIRV_CROSS_THROW("OpSpecConstantOp not enough arguments.");
1212
1213 uint32_t result_type = ops[0];
1214 uint32_t id = ops[1];
1215 auto spec_op = static_cast<Op>(ops[2]);
1216
1217 set<SPIRConstantOp>(id, result_type, spec_op, ops + 3, length - 3);
1218 break;
1219 }
1220
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +02001221 case OpLine:
1222 {
1223 // OpLine might come at global scope, but we don't care about those since they will not be declared in any
1224 // meaningful correct order.
Hans-Kristian Arntzen48a7da42019-05-28 15:51:42 +02001225 // Ignore all OpLine directives which live outside a function.
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +02001226 if (current_block)
1227 current_block->ops.push_back(instruction);
1228
Hans-Kristian Arntzen48a7da42019-05-28 15:51:42 +02001229 // Line directives may arrive before first OpLabel.
1230 // Treat this as the line of the function declaration,
1231 // so warnings for arguments can propagate properly.
1232 if (current_function)
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +02001233 {
1234 // Store the first one we find and emit it before creating the function prototype.
1235 if (current_function->entry_line.file_id == 0)
1236 {
1237 current_function->entry_line.file_id = ops[0];
1238 current_function->entry_line.line_literal = ops[1];
1239 }
1240 }
1241 break;
1242 }
1243
Lifeng Pan5ca87792019-07-04 16:03:06 +08001244 case OpNoLine:
1245 {
1246 // OpNoLine might come at global scope.
1247 if (current_block)
1248 current_block->ops.push_back(instruction);
Hans-Kristian Arntzen13378ad2019-07-05 10:25:18 +02001249 break;
Lifeng Pan5ca87792019-07-04 16:03:06 +08001250 }
1251
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001252 // Actual opcodes.
1253 default:
1254 {
Sebastián Aedo75e37522021-11-12 10:17:38 -03001255 if (length >= 2)
Sebastián Aedo48046642021-11-08 15:18:13 -03001256 {
Sebastián Aedof099d712021-11-02 17:17:13 -03001257 const auto *type = maybe_get<SPIRType>(ops[0]);
Sebastián Aedo48046642021-11-08 15:18:13 -03001258 if (type)
Sebastián Aedo250a0292021-11-03 16:12:14 -03001259 ir.load_type_width.insert({ ops[1], type->width });
Sebastián Aedof099d712021-11-02 17:17:13 -03001260 }
Hans-Kristian Arntzen29cc1892022-02-16 11:49:24 +01001261
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001262 if (!current_block)
1263 SPIRV_CROSS_THROW("Currently no block to insert opcode.");
1264
1265 current_block->ops.push_back(instruction);
1266 break;
1267 }
1268 }
1269}
1270
1271bool Parser::types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const
1272{
1273 if (a.basetype != b.basetype)
1274 return false;
1275 if (a.width != b.width)
1276 return false;
1277 if (a.vecsize != b.vecsize)
1278 return false;
1279 if (a.columns != b.columns)
1280 return false;
1281 if (a.array.size() != b.array.size())
1282 return false;
1283
1284 size_t array_count = a.array.size();
1285 if (array_count && memcmp(a.array.data(), b.array.data(), array_count * sizeof(uint32_t)) != 0)
1286 return false;
1287
1288 if (a.basetype == SPIRType::Image || a.basetype == SPIRType::SampledImage)
1289 {
1290 if (memcmp(&a.image, &b.image, sizeof(SPIRType::Image)) != 0)
1291 return false;
1292 }
1293
1294 if (a.member_types.size() != b.member_types.size())
1295 return false;
1296
1297 size_t member_types = a.member_types.size();
1298 for (size_t i = 0; i < member_types; i++)
1299 {
1300 if (!types_are_logically_equivalent(get<SPIRType>(a.member_types[i]), get<SPIRType>(b.member_types[i])))
1301 return false;
1302 }
1303
1304 return true;
1305}
1306
1307bool Parser::variable_storage_is_aliased(const SPIRVariable &v) const
1308{
1309 auto &type = get<SPIRType>(v.basetype);
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +01001310
1311 auto *type_meta = ir.find_meta(type.self);
1312
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001313 bool ssbo = v.storage == StorageClassStorageBuffer ||
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +01001314 (type_meta && type_meta->decoration.decoration_flags.get(DecorationBufferBlock));
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001315 bool image = type.basetype == SPIRType::Image;
1316 bool counter = type.basetype == SPIRType::AtomicCounter;
1317
1318 bool is_restrict;
1319 if (ssbo)
1320 is_restrict = ir.get_buffer_block_flags(v).get(DecorationRestrict);
1321 else
1322 is_restrict = ir.has_decoration(v.self, DecorationRestrict);
1323
1324 return !is_restrict && (ssbo || image || counter);
1325}
Hans-Kristian Arntzena489ba72019-04-02 11:19:03 +02001326} // namespace SPIRV_CROSS_NAMESPACE