blob: c290a5ebb99a9bf74ac5b8aa2a7a9b1ba234b576 [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
186 switch (op)
187 {
lifpan00a765e2018-11-15 09:04:36 +0800188 case OpSourceContinued:
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200189 case OpSourceExtension:
190 case OpNop:
lifpan91610962018-11-13 14:28:38 +0800191 case OpModuleProcessed:
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200192 break;
193
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +0200194 case OpString:
195 {
196 set<SPIRString>(ops[0], extract_string(ir.spirv, instruction.offset + 1));
197 break;
198 }
199
Hans-Kristian Arntzen2cc374a2019-04-24 14:12:50 +0200200 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 Arntzen5bcf02f2018-10-05 11:30:57 +0200205 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);
lifpan876627d2019-04-08 19:45:31 +0800244 if (current_block)
245 current_block->ops.push_back(instruction);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200246 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);
Daniel Thornburgh44c33332022-03-02 23:02:38 +0000262 ir.declared_extensions.push_back(std::move(ext));
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200263 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 Pan5ca87792019-07-04 16:03:06 +0800272 else if (ext == "DebugInfo")
273 set<SPIRExtension>(id, SPIRExtension::SPV_debug_info);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200274 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);
Hans-Kristian Arntzend2a4f982022-04-19 12:07:54 +0200282 else if (ext == "NonSemantic.DebugPrintf")
283 set<SPIRExtension>(id, SPIRExtension::NonSemanticDebugPrintf);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200284 else
285 set<SPIRExtension>(id, SPIRExtension::Unsupported);
286
287 // Other SPIR-V extensions which have ExtInstrs are currently not supported.
288
289 break;
290 }
291
Lifeng Pan5ca87792019-07-04 16:03:06 +0800292 case OpExtInst:
293 {
294 // The SPIR-V debug information extended instructions might come at global scope.
295 if (current_block)
Hans-Kristian Arntzen29cc1892022-02-16 11:49:24 +0100296 {
Lifeng Pan5ca87792019-07-04 16:03:06 +0800297 current_block->ops.push_back(instruction);
Hans-Kristian Arntzen29cc1892022-02-16 11:49:24 +0100298 if (length >= 2)
299 {
300 const auto *type = maybe_get<SPIRType>(ops[0]);
301 if (type)
302 ir.load_type_width.insert({ ops[1], type->width });
303 }
304 }
Lifeng Pan5ca87792019-07-04 16:03:06 +0800305 break;
306 }
307
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200308 case OpEntryPoint:
309 {
310 auto itr =
311 ir.entry_points.insert(make_pair(ops[1], SPIREntryPoint(ops[1], static_cast<ExecutionModel>(ops[0]),
312 extract_string(ir.spirv, instruction.offset + 2))));
313 auto &e = itr.first->second;
314
315 // Strings need nul-terminator and consume the whole word.
316 uint32_t strlen_words = uint32_t((e.name.size() + 1 + 3) >> 2);
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +0200317
318 for (uint32_t i = strlen_words + 2; i < instruction.length; i++)
319 e.interface_variables.push_back(ops[i]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200320
321 // Set the name of the entry point in case OpName is not provided later.
322 ir.set_name(ops[1], e.name);
323
324 // If we don't have an entry, make the first one our "default".
325 if (!ir.default_entry_point)
326 ir.default_entry_point = ops[1];
327 break;
328 }
329
330 case OpExecutionMode:
331 {
332 auto &execution = ir.entry_points[ops[0]];
333 auto mode = static_cast<ExecutionMode>(ops[1]);
334 execution.flags.set(mode);
335
336 switch (mode)
337 {
338 case ExecutionModeInvocations:
339 execution.invocations = ops[2];
340 break;
341
342 case ExecutionModeLocalSize:
343 execution.workgroup_size.x = ops[2];
344 execution.workgroup_size.y = ops[3];
345 execution.workgroup_size.z = ops[4];
346 break;
347
348 case ExecutionModeOutputVertices:
349 execution.output_vertices = ops[2];
350 break;
351
352 default:
353 break;
354 }
355 break;
356 }
357
Hans-Kristian Arntzen7c83fc22022-01-05 15:53:51 +0100358 case OpExecutionModeId:
359 {
360 auto &execution = ir.entry_points[ops[0]];
361 auto mode = static_cast<ExecutionMode>(ops[1]);
362 execution.flags.set(mode);
363
364 if (mode == ExecutionModeLocalSizeId)
365 {
366 execution.workgroup_size.id_x = ops[2];
367 execution.workgroup_size.id_y = ops[3];
368 execution.workgroup_size.id_z = ops[4];
369 }
370
371 break;
372 }
373
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200374 case OpName:
375 {
376 uint32_t id = ops[0];
377 ir.set_name(id, extract_string(ir.spirv, instruction.offset + 1));
378 break;
379 }
380
381 case OpMemberName:
382 {
383 uint32_t id = ops[0];
384 uint32_t member = ops[1];
385 ir.set_member_name(id, member, extract_string(ir.spirv, instruction.offset + 2));
386 break;
387 }
388
Hans-Kristian Arntzenfa42ed32018-11-15 10:51:01 +0100389 case OpDecorationGroup:
390 {
391 // Noop, this simply means an ID should be a collector of decorations.
392 // The meta array is already a flat array of decorations which will contain the relevant decorations.
393 break;
394 }
395
396 case OpGroupDecorate:
397 {
398 uint32_t group_id = ops[0];
399 auto &decorations = ir.meta[group_id].decoration;
400 auto &flags = decorations.decoration_flags;
401
402 // Copies decorations from one ID to another. Only copy decorations which are set in the group,
403 // i.e., we cannot just copy the meta structure directly.
404 for (uint32_t i = 1; i < length; i++)
405 {
406 uint32_t target = ops[i];
407 flags.for_each_bit([&](uint32_t bit) {
408 auto decoration = static_cast<Decoration>(bit);
409
410 if (decoration_is_string(decoration))
411 {
412 ir.set_decoration_string(target, decoration, ir.get_decoration_string(group_id, decoration));
413 }
414 else
415 {
416 ir.meta[target].decoration_word_offset[decoration] =
417 ir.meta[group_id].decoration_word_offset[decoration];
418 ir.set_decoration(target, decoration, ir.get_decoration(group_id, decoration));
419 }
420 });
421 }
422 break;
423 }
424
425 case OpGroupMemberDecorate:
426 {
427 uint32_t group_id = ops[0];
428 auto &flags = ir.meta[group_id].decoration.decoration_flags;
429
430 // Copies decorations from one ID to another. Only copy decorations which are set in the group,
431 // i.e., we cannot just copy the meta structure directly.
432 for (uint32_t i = 1; i + 1 < length; i += 2)
433 {
434 uint32_t target = ops[i + 0];
435 uint32_t index = ops[i + 1];
436 flags.for_each_bit([&](uint32_t bit) {
437 auto decoration = static_cast<Decoration>(bit);
438
439 if (decoration_is_string(decoration))
440 ir.set_member_decoration_string(target, index, decoration,
441 ir.get_decoration_string(group_id, decoration));
442 else
443 ir.set_member_decoration(target, index, decoration, ir.get_decoration(group_id, decoration));
444 });
445 }
446 break;
447 }
448
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200449 case OpDecorate:
450 case OpDecorateId:
451 {
Hans-Kristian Arntzenfa42ed32018-11-15 10:51:01 +0100452 // OpDecorateId technically supports an array of arguments, but our only supported decorations are single uint,
453 // so merge decorate and decorate-id here.
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200454 uint32_t id = ops[0];
455
456 auto decoration = static_cast<Decoration>(ops[1]);
457 if (length >= 3)
458 {
459 ir.meta[id].decoration_word_offset[decoration] = uint32_t(&ops[2] - ir.spirv.data());
460 ir.set_decoration(id, decoration, ops[2]);
461 }
462 else
463 ir.set_decoration(id, decoration);
464
465 break;
466 }
467
468 case OpDecorateStringGOOGLE:
469 {
470 uint32_t id = ops[0];
471 auto decoration = static_cast<Decoration>(ops[1]);
472 ir.set_decoration_string(id, decoration, extract_string(ir.spirv, instruction.offset + 2));
473 break;
474 }
475
476 case OpMemberDecorate:
477 {
478 uint32_t id = ops[0];
479 uint32_t member = ops[1];
480 auto decoration = static_cast<Decoration>(ops[2]);
481 if (length >= 4)
482 ir.set_member_decoration(id, member, decoration, ops[3]);
483 else
484 ir.set_member_decoration(id, member, decoration);
485 break;
486 }
487
488 case OpMemberDecorateStringGOOGLE:
489 {
490 uint32_t id = ops[0];
491 uint32_t member = ops[1];
492 auto decoration = static_cast<Decoration>(ops[2]);
493 ir.set_member_decoration_string(id, member, decoration, extract_string(ir.spirv, instruction.offset + 3));
494 break;
495 }
496
497 // Build up basic types.
498 case OpTypeVoid:
499 {
500 uint32_t id = ops[0];
501 auto &type = set<SPIRType>(id);
502 type.basetype = SPIRType::Void;
503 break;
504 }
505
506 case OpTypeBool:
507 {
508 uint32_t id = ops[0];
509 auto &type = set<SPIRType>(id);
510 type.basetype = SPIRType::Boolean;
511 type.width = 1;
512 break;
513 }
514
515 case OpTypeFloat:
516 {
517 uint32_t id = ops[0];
518 uint32_t width = ops[1];
519 auto &type = set<SPIRType>(id);
520 if (width == 64)
521 type.basetype = SPIRType::Double;
522 else if (width == 32)
523 type.basetype = SPIRType::Float;
524 else if (width == 16)
525 type.basetype = SPIRType::Half;
526 else
527 SPIRV_CROSS_THROW("Unrecognized bit-width of floating point type.");
528 type.width = width;
529 break;
530 }
531
532 case OpTypeInt:
533 {
534 uint32_t id = ops[0];
535 uint32_t width = ops[1];
lifpanb21525b2018-11-28 14:20:24 +0800536 bool signedness = ops[2] != 0;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200537 auto &type = set<SPIRType>(id);
Hans-Kristian Arntzen2ed171e2019-01-30 14:49:55 +0100538 type.basetype = signedness ? to_signed_basetype(width) : to_unsigned_basetype(width);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200539 type.width = width;
540 break;
541 }
542
543 // Build composite types by "inheriting".
544 // NOTE: The self member is also copied! For pointers and array modifiers this is a good thing
545 // since we can refer to decorations on pointee classes which is needed for UBO/SSBO, I/O blocks in geometry/tess etc.
546 case OpTypeVector:
547 {
548 uint32_t id = ops[0];
549 uint32_t vecsize = ops[2];
550
551 auto &base = get<SPIRType>(ops[1]);
552 auto &vecbase = set<SPIRType>(id);
553
554 vecbase = base;
555 vecbase.vecsize = vecsize;
556 vecbase.self = id;
557 vecbase.parent_type = ops[1];
558 break;
559 }
560
561 case OpTypeMatrix:
562 {
563 uint32_t id = ops[0];
564 uint32_t colcount = ops[2];
565
566 auto &base = get<SPIRType>(ops[1]);
567 auto &matrixbase = set<SPIRType>(id);
568
569 matrixbase = base;
570 matrixbase.columns = colcount;
571 matrixbase.self = id;
572 matrixbase.parent_type = ops[1];
573 break;
574 }
575
576 case OpTypeArray:
577 {
578 uint32_t id = ops[0];
579 auto &arraybase = set<SPIRType>(id);
580
581 uint32_t tid = ops[1];
582 auto &base = get<SPIRType>(tid);
583
584 arraybase = base;
585 arraybase.parent_type = tid;
586
587 uint32_t cid = ops[2];
588 ir.mark_used_as_array_length(cid);
589 auto *c = maybe_get<SPIRConstant>(cid);
590 bool literal = c && !c->specialization;
591
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200592 // We're copying type information into Array types, so we'll need a fixup for any physical pointer
593 // references.
594 if (base.forward_pointer)
595 forward_pointer_fixups.push_back({ id, tid });
596
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200597 arraybase.array_size_literal.push_back(literal);
598 arraybase.array.push_back(literal ? c->scalar() : cid);
599 // Do NOT set arraybase.self!
600 break;
601 }
602
603 case OpTypeRuntimeArray:
604 {
605 uint32_t id = ops[0];
606
607 auto &base = get<SPIRType>(ops[1]);
608 auto &arraybase = set<SPIRType>(id);
609
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200610 // We're copying type information into Array types, so we'll need a fixup for any physical pointer
611 // references.
612 if (base.forward_pointer)
613 forward_pointer_fixups.push_back({ id, ops[1] });
614
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200615 arraybase = base;
616 arraybase.array.push_back(0);
617 arraybase.array_size_literal.push_back(true);
618 arraybase.parent_type = ops[1];
619 // Do NOT set arraybase.self!
620 break;
621 }
622
623 case OpTypeImage:
624 {
625 uint32_t id = ops[0];
626 auto &type = set<SPIRType>(id);
627 type.basetype = SPIRType::Image;
628 type.image.type = ops[1];
629 type.image.dim = static_cast<Dim>(ops[2]);
630 type.image.depth = ops[3] == 1;
631 type.image.arrayed = ops[4] != 0;
632 type.image.ms = ops[5] != 0;
633 type.image.sampled = ops[6];
634 type.image.format = static_cast<ImageFormat>(ops[7]);
635 type.image.access = (length >= 9) ? static_cast<AccessQualifier>(ops[8]) : AccessQualifierMax;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200636 break;
637 }
638
639 case OpTypeSampledImage:
640 {
641 uint32_t id = ops[0];
642 uint32_t imagetype = ops[1];
643 auto &type = set<SPIRType>(id);
644 type = get<SPIRType>(imagetype);
645 type.basetype = SPIRType::SampledImage;
646 type.self = id;
647 break;
648 }
649
650 case OpTypeSampler:
651 {
652 uint32_t id = ops[0];
653 auto &type = set<SPIRType>(id);
654 type.basetype = SPIRType::Sampler;
655 break;
656 }
657
658 case OpTypePointer:
659 {
660 uint32_t id = ops[0];
661
Hans-Kristian Arntzen1f018b02020-11-03 10:51:56 +0100662 // Very rarely, we might receive a FunctionPrototype here.
663 // We won't be able to compile it, but we shouldn't crash when parsing.
664 // We should be able to reflect.
665 auto *base = maybe_get<SPIRType>(ops[2]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200666 auto &ptrbase = set<SPIRType>(id);
667
Hans-Kristian Arntzen1f018b02020-11-03 10:51:56 +0100668 if (base)
669 ptrbase = *base;
670
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200671 ptrbase.pointer = true;
Hans-Kristian Arntzend0b93722018-11-26 12:23:28 +0100672 ptrbase.pointer_depth++;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200673 ptrbase.storage = static_cast<StorageClass>(ops[1]);
674
675 if (ptrbase.storage == StorageClassAtomicCounter)
676 ptrbase.basetype = SPIRType::AtomicCounter;
677
Hans-Kristian Arntzen1f018b02020-11-03 10:51:56 +0100678 if (base && base->forward_pointer)
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200679 forward_pointer_fixups.push_back({ id, ops[2] });
680
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200681 ptrbase.parent_type = ops[2];
682
683 // Do NOT set ptrbase.self!
684 break;
685 }
686
Hans-Kristian Arntzen2cc374a2019-04-24 14:12:50 +0200687 case OpTypeForwardPointer:
688 {
689 uint32_t id = ops[0];
690 auto &ptrbase = set<SPIRType>(id);
691 ptrbase.pointer = true;
692 ptrbase.pointer_depth++;
693 ptrbase.storage = static_cast<StorageClass>(ops[1]);
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200694 ptrbase.forward_pointer = true;
Hans-Kristian Arntzen2cc374a2019-04-24 14:12:50 +0200695
696 if (ptrbase.storage == StorageClassAtomicCounter)
697 ptrbase.basetype = SPIRType::AtomicCounter;
698
699 break;
700 }
701
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200702 case OpTypeStruct:
703 {
704 uint32_t id = ops[0];
705 auto &type = set<SPIRType>(id);
706 type.basetype = SPIRType::Struct;
707 for (uint32_t i = 1; i < length; i++)
708 type.member_types.push_back(ops[i]);
709
710 // Check if we have seen this struct type before, with just different
711 // decorations.
712 //
713 // Add workaround for issue #17 as well by looking at OpName for the struct
714 // types, which we shouldn't normally do.
715 // We should not normally have to consider type aliases like this to begin with
716 // however ... glslang issues #304, #307 cover this.
717
718 // For stripped names, never consider struct type aliasing.
719 // We risk declaring the same struct multiple times, but type-punning is not allowed
720 // so this is safe.
721 bool consider_aliasing = !ir.get_name(type.self).empty();
722 if (consider_aliasing)
723 {
724 for (auto &other : global_struct_cache)
725 {
726 if (ir.get_name(type.self) == ir.get_name(other) &&
727 types_are_logically_equivalent(type, get<SPIRType>(other)))
728 {
729 type.type_alias = other;
730 break;
731 }
732 }
733
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +0200734 if (type.type_alias == TypeID(0))
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200735 global_struct_cache.push_back(id);
736 }
737 break;
738 }
739
740 case OpTypeFunction:
741 {
742 uint32_t id = ops[0];
743 uint32_t ret = ops[1];
744
745 auto &func = set<SPIRFunctionPrototype>(id, ret);
746 for (uint32_t i = 2; i < length; i++)
747 func.parameter_types.push_back(ops[i]);
748 break;
749 }
750
Hans-Kristian Arntzen6b0e5582020-04-21 14:25:18 +0200751 case OpTypeAccelerationStructureKHR:
Patrick Moursda39a7b2019-02-26 15:43:03 +0100752 {
753 uint32_t id = ops[0];
754 auto &type = set<SPIRType>(id);
Hans-Kristian Arntzen6b0e5582020-04-21 14:25:18 +0200755 type.basetype = SPIRType::AccelerationStructure;
756 break;
757 }
758
Hans-Kristian Arntzen1a28a042021-01-06 11:32:26 +0100759 case OpTypeRayQueryKHR:
Hans-Kristian Arntzen6b0e5582020-04-21 14:25:18 +0200760 {
761 uint32_t id = ops[0];
762 auto &type = set<SPIRType>(id);
763 type.basetype = SPIRType::RayQuery;
Patrick Moursda39a7b2019-02-26 15:43:03 +0100764 break;
765 }
766
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200767 // Variable declaration
768 // All variables are essentially pointers with a storage qualifier.
769 case OpVariable:
770 {
771 uint32_t type = ops[0];
772 uint32_t id = ops[1];
773 auto storage = static_cast<StorageClass>(ops[2]);
774 uint32_t initializer = length == 4 ? ops[3] : 0;
775
776 if (storage == StorageClassFunction)
777 {
778 if (!current_function)
779 SPIRV_CROSS_THROW("No function currently in scope");
780 current_function->add_local_variable(id);
781 }
782
783 set<SPIRVariable>(id, type, storage, initializer);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200784 break;
785 }
786
787 // OpPhi
788 // OpPhi is a fairly magical opcode.
789 // It selects temporary variables based on which parent block we *came from*.
790 // In high-level languages we can "de-SSA" by creating a function local, and flush out temporaries to this function-local
791 // variable to emulate SSA Phi.
792 case OpPhi:
793 {
794 if (!current_function)
795 SPIRV_CROSS_THROW("No function currently in scope");
796 if (!current_block)
797 SPIRV_CROSS_THROW("No block currently in scope");
798
799 uint32_t result_type = ops[0];
800 uint32_t id = ops[1];
801
802 // Instead of a temporary, create a new function-wide temporary with this ID instead.
803 auto &var = set<SPIRVariable>(id, result_type, spv::StorageClassFunction);
804 var.phi_variable = true;
805
806 current_function->add_local_variable(id);
807
808 for (uint32_t i = 2; i + 2 <= length; i += 2)
809 current_block->phi_variables.push_back({ ops[i], ops[i + 1], id });
810 break;
811 }
812
813 // Constants
814 case OpSpecConstant:
815 case OpConstant:
816 {
817 uint32_t id = ops[1];
818 auto &type = get<SPIRType>(ops[0]);
819
820 if (type.width > 32)
821 set<SPIRConstant>(id, ops[0], ops[2] | (uint64_t(ops[3]) << 32), op == OpSpecConstant);
822 else
823 set<SPIRConstant>(id, ops[0], ops[2], op == OpSpecConstant);
824 break;
825 }
826
827 case OpSpecConstantFalse:
828 case OpConstantFalse:
829 {
830 uint32_t id = ops[1];
831 set<SPIRConstant>(id, ops[0], uint32_t(0), op == OpSpecConstantFalse);
832 break;
833 }
834
835 case OpSpecConstantTrue:
836 case OpConstantTrue:
837 {
838 uint32_t id = ops[1];
839 set<SPIRConstant>(id, ops[0], uint32_t(1), op == OpSpecConstantTrue);
840 break;
841 }
842
843 case OpConstantNull:
844 {
845 uint32_t id = ops[1];
846 uint32_t type = ops[0];
Hans-Kristian Arntzenb8905bb2020-03-26 11:21:23 +0100847 ir.make_constant_null(id, type, true);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200848 break;
849 }
850
851 case OpSpecConstantComposite:
852 case OpConstantComposite:
853 {
854 uint32_t id = ops[1];
855 uint32_t type = ops[0];
856
857 auto &ctype = get<SPIRType>(type);
858
859 // We can have constants which are structs and arrays.
860 // In this case, our SPIRConstant will be a list of other SPIRConstant ids which we
861 // can refer to.
862 if (ctype.basetype == SPIRType::Struct || !ctype.array.empty())
863 {
864 set<SPIRConstant>(id, type, ops + 2, length - 2, op == OpSpecConstantComposite);
865 }
866 else
867 {
868 uint32_t elements = length - 2;
869 if (elements > 4)
870 SPIRV_CROSS_THROW("OpConstantComposite only supports 1, 2, 3 and 4 elements.");
871
872 SPIRConstant remapped_constant_ops[4];
873 const SPIRConstant *c[4];
874 for (uint32_t i = 0; i < elements; i++)
875 {
876 // Specialization constants operations can also be part of this.
877 // We do not know their value, so any attempt to query SPIRConstant later
878 // will fail. We can only propagate the ID of the expression and use to_expression on it.
879 auto *constant_op = maybe_get<SPIRConstantOp>(ops[2 + i]);
Hans-Kristian Arntzendf3e21a2019-03-27 10:51:23 +0100880 auto *undef_op = maybe_get<SPIRUndef>(ops[2 + i]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200881 if (constant_op)
882 {
883 if (op == OpConstantComposite)
884 SPIRV_CROSS_THROW("Specialization constant operation used in OpConstantComposite.");
885
886 remapped_constant_ops[i].make_null(get<SPIRType>(constant_op->basetype));
887 remapped_constant_ops[i].self = constant_op->self;
888 remapped_constant_ops[i].constant_type = constant_op->basetype;
889 remapped_constant_ops[i].specialization = true;
890 c[i] = &remapped_constant_ops[i];
891 }
Hans-Kristian Arntzendf3e21a2019-03-27 10:51:23 +0100892 else if (undef_op)
893 {
894 // Undefined, just pick 0.
895 remapped_constant_ops[i].make_null(get<SPIRType>(undef_op->basetype));
896 remapped_constant_ops[i].constant_type = undef_op->basetype;
897 c[i] = &remapped_constant_ops[i];
898 }
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200899 else
900 c[i] = &get<SPIRConstant>(ops[2 + i]);
901 }
902 set<SPIRConstant>(id, type, c, elements, op == OpSpecConstantComposite);
903 }
904 break;
905 }
906
907 // Functions
908 case OpFunction:
909 {
910 uint32_t res = ops[0];
911 uint32_t id = ops[1];
912 // Control
913 uint32_t type = ops[3];
914
915 if (current_function)
916 SPIRV_CROSS_THROW("Must end a function before starting a new one!");
917
918 current_function = &set<SPIRFunction>(id, res, type);
919 break;
920 }
921
922 case OpFunctionParameter:
923 {
924 uint32_t type = ops[0];
925 uint32_t id = ops[1];
926
927 if (!current_function)
928 SPIRV_CROSS_THROW("Must be in a function!");
929
930 current_function->add_parameter(type, id);
931 set<SPIRVariable>(id, type, StorageClassFunction);
932 break;
933 }
934
935 case OpFunctionEnd:
936 {
937 if (current_block)
938 {
939 // Very specific error message, but seems to come up quite often.
940 SPIRV_CROSS_THROW(
941 "Cannot end a function before ending the current block.\n"
942 "Likely cause: If this SPIR-V was created from glslang HLSL, make sure the entry point is valid.");
943 }
944 current_function = nullptr;
945 break;
946 }
947
948 // Blocks
949 case OpLabel:
950 {
951 // OpLabel always starts a block.
952 if (!current_function)
953 SPIRV_CROSS_THROW("Blocks cannot exist outside functions!");
954
955 uint32_t id = ops[0];
956
957 current_function->blocks.push_back(id);
958 if (!current_function->entry_block)
959 current_function->entry_block = id;
960
961 if (current_block)
962 SPIRV_CROSS_THROW("Cannot start a block before ending the current block.");
963
964 current_block = &set<SPIRBlock>(id);
965 break;
966 }
967
968 // Branch instructions end blocks.
969 case OpBranch:
970 {
971 if (!current_block)
972 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
973
974 uint32_t target = ops[0];
975 current_block->terminator = SPIRBlock::Direct;
976 current_block->next_block = target;
977 current_block = nullptr;
978 break;
979 }
980
981 case OpBranchConditional:
982 {
983 if (!current_block)
984 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
985
986 current_block->condition = ops[0];
987 current_block->true_block = ops[1];
988 current_block->false_block = ops[2];
989
990 current_block->terminator = SPIRBlock::Select;
Hans-Kristian Arntzen2714f542021-11-07 12:37:23 +0100991
992 if (current_block->true_block == current_block->false_block)
993 {
994 // Bogus conditional, translate to a direct branch.
995 // Avoids some ugly edge cases later when analyzing CFGs.
996
997 // There are some super jank cases where the merge block is different from the true/false,
998 // and later branches can "break" out of the selection construct this way.
999 // This is complete nonsense, but CTS hits this case.
1000 // In this scenario, we should see the selection construct as more of a Switch with one default case.
1001 // The problem here is that this breaks any attempt to break out of outer switch statements,
1002 // but it's theoretically solvable if this ever comes up using the ladder breaking system ...
1003
1004 if (current_block->true_block != current_block->next_block &&
1005 current_block->merge == SPIRBlock::MergeSelection)
1006 {
1007 uint32_t ids = ir.increase_bound_by(2);
1008
1009 SPIRType type;
1010 type.basetype = SPIRType::Int;
1011 type.width = 32;
1012 set<SPIRType>(ids, type);
1013 auto &c = set<SPIRConstant>(ids + 1, ids);
1014
1015 current_block->condition = c.self;
1016 current_block->default_block = current_block->true_block;
1017 current_block->terminator = SPIRBlock::MultiSelect;
1018 ir.block_meta[current_block->next_block] &= ~ParsedIR::BLOCK_META_SELECTION_MERGE_BIT;
1019 ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT;
1020 }
1021 else
1022 {
Hans-Kristian Arntzenbe904dc2022-07-22 14:36:31 +02001023 // Collapse loops if we have to.
1024 bool collapsed_loop = current_block->true_block == current_block->merge_block &&
1025 current_block->merge == SPIRBlock::MergeLoop;
1026
1027 if (collapsed_loop)
1028 {
1029 ir.block_meta[current_block->merge_block] &= ~ParsedIR::BLOCK_META_LOOP_MERGE_BIT;
1030 ir.block_meta[current_block->continue_block] &= ~ParsedIR::BLOCK_META_CONTINUE_BIT;
1031 }
1032
Hans-Kristian Arntzen2714f542021-11-07 12:37:23 +01001033 current_block->next_block = current_block->true_block;
1034 current_block->condition = 0;
1035 current_block->true_block = 0;
1036 current_block->false_block = 0;
1037 current_block->merge_block = 0;
1038 current_block->merge = SPIRBlock::MergeNone;
1039 current_block->terminator = SPIRBlock::Direct;
1040 }
1041 }
1042
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001043 current_block = nullptr;
1044 break;
1045 }
1046
1047 case OpSwitch:
1048 {
1049 if (!current_block)
1050 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1051
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001052 current_block->terminator = SPIRBlock::MultiSelect;
1053
1054 current_block->condition = ops[0];
1055 current_block->default_block = ops[1];
1056
Sebastián Aedof099d712021-11-02 17:17:13 -03001057 uint32_t remaining_ops = length - 2;
Sebastián Aedo75e37522021-11-12 10:17:38 -03001058 if ((remaining_ops % 2) == 0)
1059 {
1060 for (uint32_t i = 2; i + 2 <= length; i += 2)
1061 current_block->cases_32bit.push_back({ ops[i], ops[i + 1] });
1062 }
Sebastián Aedof099d712021-11-02 17:17:13 -03001063
Sebastián Aedo48046642021-11-08 15:18:13 -03001064 if ((remaining_ops % 3) == 0)
Sebastián Aedo75e37522021-11-12 10:17:38 -03001065 {
Sebastián Aedo48046642021-11-08 15:18:13 -03001066 for (uint32_t i = 2; i + 3 <= length; i += 3)
1067 {
Sebastián Aedo6d8302e2021-11-18 16:08:59 -03001068 uint64_t value = (static_cast<uint64_t>(ops[i + 1]) << 32) | ops[i];
Sebastián Aedof099d712021-11-02 17:17:13 -03001069 current_block->cases_64bit.push_back({ value, ops[i + 2] });
Sebastián Aedo3eb55322021-10-28 19:57:41 -03001070 }
Sebastián Aedo75e37522021-11-12 10:17:38 -03001071 }
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001072
1073 // If we jump to next block, make it break instead since we're inside a switch case block at that point.
1074 ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT;
1075
1076 current_block = nullptr;
1077 break;
1078 }
1079
1080 case OpKill:
Hans-Kristian Arntzendc62cc72022-03-03 10:36:07 +01001081 case OpTerminateInvocation:
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001082 {
1083 if (!current_block)
1084 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1085 current_block->terminator = SPIRBlock::Kill;
1086 current_block = nullptr;
1087 break;
1088 }
1089
Hans-Kristian Arntzen2097c302021-01-08 11:37:29 +01001090 case OpTerminateRayKHR:
1091 // NV variant is not a terminator.
1092 if (!current_block)
1093 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1094 current_block->terminator = SPIRBlock::TerminateRay;
1095 current_block = nullptr;
1096 break;
1097
1098 case OpIgnoreIntersectionKHR:
1099 // NV variant is not a terminator.
1100 if (!current_block)
1101 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1102 current_block->terminator = SPIRBlock::IgnoreIntersection;
1103 current_block = nullptr;
1104 break;
1105
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001106 case OpReturn:
1107 {
1108 if (!current_block)
1109 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1110 current_block->terminator = SPIRBlock::Return;
1111 current_block = nullptr;
1112 break;
1113 }
1114
1115 case OpReturnValue:
1116 {
1117 if (!current_block)
1118 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1119 current_block->terminator = SPIRBlock::Return;
1120 current_block->return_value = ops[0];
1121 current_block = nullptr;
1122 break;
1123 }
1124
1125 case OpUnreachable:
1126 {
1127 if (!current_block)
1128 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1129 current_block->terminator = SPIRBlock::Unreachable;
1130 current_block = nullptr;
1131 break;
1132 }
1133
1134 case OpSelectionMerge:
1135 {
1136 if (!current_block)
1137 SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
1138
1139 current_block->next_block = ops[0];
1140 current_block->merge = SPIRBlock::MergeSelection;
1141 ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_SELECTION_MERGE_BIT;
1142
1143 if (length >= 2)
1144 {
1145 if (ops[1] & SelectionControlFlattenMask)
1146 current_block->hint = SPIRBlock::HintFlatten;
1147 else if (ops[1] & SelectionControlDontFlattenMask)
1148 current_block->hint = SPIRBlock::HintDontFlatten;
1149 }
1150 break;
1151 }
1152
1153 case OpLoopMerge:
1154 {
1155 if (!current_block)
1156 SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
1157
1158 current_block->merge_block = ops[0];
1159 current_block->continue_block = ops[1];
1160 current_block->merge = SPIRBlock::MergeLoop;
1161
1162 ir.block_meta[current_block->self] |= ParsedIR::BLOCK_META_LOOP_HEADER_BIT;
1163 ir.block_meta[current_block->merge_block] |= ParsedIR::BLOCK_META_LOOP_MERGE_BIT;
1164
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +02001165 ir.continue_block_to_loop_header[current_block->continue_block] = BlockID(current_block->self);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001166
1167 // Don't add loop headers to continue blocks,
1168 // which would make it impossible branch into the loop header since
1169 // they are treated as continues.
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +02001170 if (current_block->continue_block != BlockID(current_block->self))
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001171 ir.block_meta[current_block->continue_block] |= ParsedIR::BLOCK_META_CONTINUE_BIT;
1172
1173 if (length >= 3)
1174 {
1175 if (ops[2] & LoopControlUnrollMask)
1176 current_block->hint = SPIRBlock::HintUnroll;
1177 else if (ops[2] & LoopControlDontUnrollMask)
1178 current_block->hint = SPIRBlock::HintDontUnroll;
1179 }
1180 break;
1181 }
1182
1183 case OpSpecConstantOp:
1184 {
1185 if (length < 3)
1186 SPIRV_CROSS_THROW("OpSpecConstantOp not enough arguments.");
1187
1188 uint32_t result_type = ops[0];
1189 uint32_t id = ops[1];
1190 auto spec_op = static_cast<Op>(ops[2]);
1191
1192 set<SPIRConstantOp>(id, result_type, spec_op, ops + 3, length - 3);
1193 break;
1194 }
1195
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +02001196 case OpLine:
1197 {
1198 // OpLine might come at global scope, but we don't care about those since they will not be declared in any
1199 // meaningful correct order.
Hans-Kristian Arntzen48a7da42019-05-28 15:51:42 +02001200 // Ignore all OpLine directives which live outside a function.
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +02001201 if (current_block)
1202 current_block->ops.push_back(instruction);
1203
Hans-Kristian Arntzen48a7da42019-05-28 15:51:42 +02001204 // Line directives may arrive before first OpLabel.
1205 // Treat this as the line of the function declaration,
1206 // so warnings for arguments can propagate properly.
1207 if (current_function)
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +02001208 {
1209 // Store the first one we find and emit it before creating the function prototype.
1210 if (current_function->entry_line.file_id == 0)
1211 {
1212 current_function->entry_line.file_id = ops[0];
1213 current_function->entry_line.line_literal = ops[1];
1214 }
1215 }
1216 break;
1217 }
1218
Lifeng Pan5ca87792019-07-04 16:03:06 +08001219 case OpNoLine:
1220 {
1221 // OpNoLine might come at global scope.
1222 if (current_block)
1223 current_block->ops.push_back(instruction);
Hans-Kristian Arntzen13378ad2019-07-05 10:25:18 +02001224 break;
Lifeng Pan5ca87792019-07-04 16:03:06 +08001225 }
1226
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001227 // Actual opcodes.
1228 default:
1229 {
Sebastián Aedo75e37522021-11-12 10:17:38 -03001230 if (length >= 2)
Sebastián Aedo48046642021-11-08 15:18:13 -03001231 {
Sebastián Aedof099d712021-11-02 17:17:13 -03001232 const auto *type = maybe_get<SPIRType>(ops[0]);
Sebastián Aedo48046642021-11-08 15:18:13 -03001233 if (type)
Sebastián Aedo250a0292021-11-03 16:12:14 -03001234 ir.load_type_width.insert({ ops[1], type->width });
Sebastián Aedof099d712021-11-02 17:17:13 -03001235 }
Hans-Kristian Arntzen29cc1892022-02-16 11:49:24 +01001236
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001237 if (!current_block)
1238 SPIRV_CROSS_THROW("Currently no block to insert opcode.");
1239
1240 current_block->ops.push_back(instruction);
1241 break;
1242 }
1243 }
1244}
1245
1246bool Parser::types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const
1247{
1248 if (a.basetype != b.basetype)
1249 return false;
1250 if (a.width != b.width)
1251 return false;
1252 if (a.vecsize != b.vecsize)
1253 return false;
1254 if (a.columns != b.columns)
1255 return false;
1256 if (a.array.size() != b.array.size())
1257 return false;
1258
1259 size_t array_count = a.array.size();
1260 if (array_count && memcmp(a.array.data(), b.array.data(), array_count * sizeof(uint32_t)) != 0)
1261 return false;
1262
1263 if (a.basetype == SPIRType::Image || a.basetype == SPIRType::SampledImage)
1264 {
1265 if (memcmp(&a.image, &b.image, sizeof(SPIRType::Image)) != 0)
1266 return false;
1267 }
1268
1269 if (a.member_types.size() != b.member_types.size())
1270 return false;
1271
1272 size_t member_types = a.member_types.size();
1273 for (size_t i = 0; i < member_types; i++)
1274 {
1275 if (!types_are_logically_equivalent(get<SPIRType>(a.member_types[i]), get<SPIRType>(b.member_types[i])))
1276 return false;
1277 }
1278
1279 return true;
1280}
1281
1282bool Parser::variable_storage_is_aliased(const SPIRVariable &v) const
1283{
1284 auto &type = get<SPIRType>(v.basetype);
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +01001285
1286 auto *type_meta = ir.find_meta(type.self);
1287
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001288 bool ssbo = v.storage == StorageClassStorageBuffer ||
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +01001289 (type_meta && type_meta->decoration.decoration_flags.get(DecorationBufferBlock));
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001290 bool image = type.basetype == SPIRType::Image;
1291 bool counter = type.basetype == SPIRType::AtomicCounter;
1292
1293 bool is_restrict;
1294 if (ssbo)
1295 is_restrict = ir.get_buffer_block_flags(v).get(DecorationRestrict);
1296 else
1297 is_restrict = ir.has_decoration(v.self, DecorationRestrict);
1298
1299 return !is_restrict && (ssbo || image || counter);
1300}
Hans-Kristian Arntzena489ba72019-04-02 11:19:03 +02001301} // namespace SPIRV_CROSS_NAMESPACE