blob: 526c92cb59e35cd74bbc8fb4baf8485a5b393110 [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
Hans-Kristian Arntzen57626172022-09-02 16:31:04 +0200352 case ExecutionModeOutputPrimitivesEXT:
353 execution.output_primitives = ops[2];
354 break;
355
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200356 default:
357 break;
358 }
359 break;
360 }
361
Hans-Kristian Arntzen7c83fc22022-01-05 15:53:51 +0100362 case OpExecutionModeId:
363 {
364 auto &execution = ir.entry_points[ops[0]];
365 auto mode = static_cast<ExecutionMode>(ops[1]);
366 execution.flags.set(mode);
367
368 if (mode == ExecutionModeLocalSizeId)
369 {
370 execution.workgroup_size.id_x = ops[2];
371 execution.workgroup_size.id_y = ops[3];
372 execution.workgroup_size.id_z = ops[4];
373 }
374
375 break;
376 }
377
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200378 case OpName:
379 {
380 uint32_t id = ops[0];
381 ir.set_name(id, extract_string(ir.spirv, instruction.offset + 1));
382 break;
383 }
384
385 case OpMemberName:
386 {
387 uint32_t id = ops[0];
388 uint32_t member = ops[1];
389 ir.set_member_name(id, member, extract_string(ir.spirv, instruction.offset + 2));
390 break;
391 }
392
Hans-Kristian Arntzenfa42ed32018-11-15 10:51:01 +0100393 case OpDecorationGroup:
394 {
395 // Noop, this simply means an ID should be a collector of decorations.
396 // The meta array is already a flat array of decorations which will contain the relevant decorations.
397 break;
398 }
399
400 case OpGroupDecorate:
401 {
402 uint32_t group_id = ops[0];
403 auto &decorations = ir.meta[group_id].decoration;
404 auto &flags = decorations.decoration_flags;
405
406 // Copies decorations from one ID to another. Only copy decorations which are set in the group,
407 // i.e., we cannot just copy the meta structure directly.
408 for (uint32_t i = 1; i < length; i++)
409 {
410 uint32_t target = ops[i];
411 flags.for_each_bit([&](uint32_t bit) {
412 auto decoration = static_cast<Decoration>(bit);
413
414 if (decoration_is_string(decoration))
415 {
416 ir.set_decoration_string(target, decoration, ir.get_decoration_string(group_id, decoration));
417 }
418 else
419 {
420 ir.meta[target].decoration_word_offset[decoration] =
421 ir.meta[group_id].decoration_word_offset[decoration];
422 ir.set_decoration(target, decoration, ir.get_decoration(group_id, decoration));
423 }
424 });
425 }
426 break;
427 }
428
429 case OpGroupMemberDecorate:
430 {
431 uint32_t group_id = ops[0];
432 auto &flags = ir.meta[group_id].decoration.decoration_flags;
433
434 // Copies decorations from one ID to another. Only copy decorations which are set in the group,
435 // i.e., we cannot just copy the meta structure directly.
436 for (uint32_t i = 1; i + 1 < length; i += 2)
437 {
438 uint32_t target = ops[i + 0];
439 uint32_t index = ops[i + 1];
440 flags.for_each_bit([&](uint32_t bit) {
441 auto decoration = static_cast<Decoration>(bit);
442
443 if (decoration_is_string(decoration))
444 ir.set_member_decoration_string(target, index, decoration,
445 ir.get_decoration_string(group_id, decoration));
446 else
447 ir.set_member_decoration(target, index, decoration, ir.get_decoration(group_id, decoration));
448 });
449 }
450 break;
451 }
452
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200453 case OpDecorate:
454 case OpDecorateId:
455 {
Hans-Kristian Arntzenfa42ed32018-11-15 10:51:01 +0100456 // OpDecorateId technically supports an array of arguments, but our only supported decorations are single uint,
457 // so merge decorate and decorate-id here.
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200458 uint32_t id = ops[0];
459
460 auto decoration = static_cast<Decoration>(ops[1]);
461 if (length >= 3)
462 {
463 ir.meta[id].decoration_word_offset[decoration] = uint32_t(&ops[2] - ir.spirv.data());
464 ir.set_decoration(id, decoration, ops[2]);
465 }
466 else
467 ir.set_decoration(id, decoration);
468
469 break;
470 }
471
472 case OpDecorateStringGOOGLE:
473 {
474 uint32_t id = ops[0];
475 auto decoration = static_cast<Decoration>(ops[1]);
476 ir.set_decoration_string(id, decoration, extract_string(ir.spirv, instruction.offset + 2));
477 break;
478 }
479
480 case OpMemberDecorate:
481 {
482 uint32_t id = ops[0];
483 uint32_t member = ops[1];
484 auto decoration = static_cast<Decoration>(ops[2]);
485 if (length >= 4)
486 ir.set_member_decoration(id, member, decoration, ops[3]);
487 else
488 ir.set_member_decoration(id, member, decoration);
489 break;
490 }
491
492 case OpMemberDecorateStringGOOGLE:
493 {
494 uint32_t id = ops[0];
495 uint32_t member = ops[1];
496 auto decoration = static_cast<Decoration>(ops[2]);
497 ir.set_member_decoration_string(id, member, decoration, extract_string(ir.spirv, instruction.offset + 3));
498 break;
499 }
500
501 // Build up basic types.
502 case OpTypeVoid:
503 {
504 uint32_t id = ops[0];
505 auto &type = set<SPIRType>(id);
506 type.basetype = SPIRType::Void;
507 break;
508 }
509
510 case OpTypeBool:
511 {
512 uint32_t id = ops[0];
513 auto &type = set<SPIRType>(id);
514 type.basetype = SPIRType::Boolean;
515 type.width = 1;
516 break;
517 }
518
519 case OpTypeFloat:
520 {
521 uint32_t id = ops[0];
522 uint32_t width = ops[1];
523 auto &type = set<SPIRType>(id);
524 if (width == 64)
525 type.basetype = SPIRType::Double;
526 else if (width == 32)
527 type.basetype = SPIRType::Float;
528 else if (width == 16)
529 type.basetype = SPIRType::Half;
530 else
531 SPIRV_CROSS_THROW("Unrecognized bit-width of floating point type.");
532 type.width = width;
533 break;
534 }
535
536 case OpTypeInt:
537 {
538 uint32_t id = ops[0];
539 uint32_t width = ops[1];
lifpanb21525b2018-11-28 14:20:24 +0800540 bool signedness = ops[2] != 0;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200541 auto &type = set<SPIRType>(id);
Hans-Kristian Arntzen2ed171e2019-01-30 14:49:55 +0100542 type.basetype = signedness ? to_signed_basetype(width) : to_unsigned_basetype(width);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200543 type.width = width;
544 break;
545 }
546
547 // Build composite types by "inheriting".
548 // NOTE: The self member is also copied! For pointers and array modifiers this is a good thing
549 // since we can refer to decorations on pointee classes which is needed for UBO/SSBO, I/O blocks in geometry/tess etc.
550 case OpTypeVector:
551 {
552 uint32_t id = ops[0];
553 uint32_t vecsize = ops[2];
554
555 auto &base = get<SPIRType>(ops[1]);
556 auto &vecbase = set<SPIRType>(id);
557
558 vecbase = base;
559 vecbase.vecsize = vecsize;
560 vecbase.self = id;
561 vecbase.parent_type = ops[1];
562 break;
563 }
564
565 case OpTypeMatrix:
566 {
567 uint32_t id = ops[0];
568 uint32_t colcount = ops[2];
569
570 auto &base = get<SPIRType>(ops[1]);
571 auto &matrixbase = set<SPIRType>(id);
572
573 matrixbase = base;
574 matrixbase.columns = colcount;
575 matrixbase.self = id;
576 matrixbase.parent_type = ops[1];
577 break;
578 }
579
580 case OpTypeArray:
581 {
582 uint32_t id = ops[0];
583 auto &arraybase = set<SPIRType>(id);
584
585 uint32_t tid = ops[1];
586 auto &base = get<SPIRType>(tid);
587
588 arraybase = base;
589 arraybase.parent_type = tid;
590
591 uint32_t cid = ops[2];
592 ir.mark_used_as_array_length(cid);
593 auto *c = maybe_get<SPIRConstant>(cid);
594 bool literal = c && !c->specialization;
595
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200596 // We're copying type information into Array types, so we'll need a fixup for any physical pointer
597 // references.
598 if (base.forward_pointer)
599 forward_pointer_fixups.push_back({ id, tid });
600
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200601 arraybase.array_size_literal.push_back(literal);
602 arraybase.array.push_back(literal ? c->scalar() : cid);
603 // Do NOT set arraybase.self!
604 break;
605 }
606
607 case OpTypeRuntimeArray:
608 {
609 uint32_t id = ops[0];
610
611 auto &base = get<SPIRType>(ops[1]);
612 auto &arraybase = set<SPIRType>(id);
613
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200614 // We're copying type information into Array types, so we'll need a fixup for any physical pointer
615 // references.
616 if (base.forward_pointer)
617 forward_pointer_fixups.push_back({ id, ops[1] });
618
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200619 arraybase = base;
620 arraybase.array.push_back(0);
621 arraybase.array_size_literal.push_back(true);
622 arraybase.parent_type = ops[1];
623 // Do NOT set arraybase.self!
624 break;
625 }
626
627 case OpTypeImage:
628 {
629 uint32_t id = ops[0];
630 auto &type = set<SPIRType>(id);
631 type.basetype = SPIRType::Image;
632 type.image.type = ops[1];
633 type.image.dim = static_cast<Dim>(ops[2]);
634 type.image.depth = ops[3] == 1;
635 type.image.arrayed = ops[4] != 0;
636 type.image.ms = ops[5] != 0;
637 type.image.sampled = ops[6];
638 type.image.format = static_cast<ImageFormat>(ops[7]);
639 type.image.access = (length >= 9) ? static_cast<AccessQualifier>(ops[8]) : AccessQualifierMax;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200640 break;
641 }
642
643 case OpTypeSampledImage:
644 {
645 uint32_t id = ops[0];
646 uint32_t imagetype = ops[1];
647 auto &type = set<SPIRType>(id);
648 type = get<SPIRType>(imagetype);
649 type.basetype = SPIRType::SampledImage;
650 type.self = id;
651 break;
652 }
653
654 case OpTypeSampler:
655 {
656 uint32_t id = ops[0];
657 auto &type = set<SPIRType>(id);
658 type.basetype = SPIRType::Sampler;
659 break;
660 }
661
662 case OpTypePointer:
663 {
664 uint32_t id = ops[0];
665
Hans-Kristian Arntzen1f018b02020-11-03 10:51:56 +0100666 // Very rarely, we might receive a FunctionPrototype here.
667 // We won't be able to compile it, but we shouldn't crash when parsing.
668 // We should be able to reflect.
669 auto *base = maybe_get<SPIRType>(ops[2]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200670 auto &ptrbase = set<SPIRType>(id);
671
Hans-Kristian Arntzen1f018b02020-11-03 10:51:56 +0100672 if (base)
673 ptrbase = *base;
674
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200675 ptrbase.pointer = true;
Hans-Kristian Arntzend0b93722018-11-26 12:23:28 +0100676 ptrbase.pointer_depth++;
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200677 ptrbase.storage = static_cast<StorageClass>(ops[1]);
678
679 if (ptrbase.storage == StorageClassAtomicCounter)
680 ptrbase.basetype = SPIRType::AtomicCounter;
681
Hans-Kristian Arntzen1f018b02020-11-03 10:51:56 +0100682 if (base && base->forward_pointer)
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200683 forward_pointer_fixups.push_back({ id, ops[2] });
684
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200685 ptrbase.parent_type = ops[2];
686
687 // Do NOT set ptrbase.self!
688 break;
689 }
690
Hans-Kristian Arntzen2cc374a2019-04-24 14:12:50 +0200691 case OpTypeForwardPointer:
692 {
693 uint32_t id = ops[0];
694 auto &ptrbase = set<SPIRType>(id);
695 ptrbase.pointer = true;
696 ptrbase.pointer_depth++;
697 ptrbase.storage = static_cast<StorageClass>(ops[1]);
Hans-Kristian Arntzen58dad822020-05-25 11:05:42 +0200698 ptrbase.forward_pointer = true;
Hans-Kristian Arntzen2cc374a2019-04-24 14:12:50 +0200699
700 if (ptrbase.storage == StorageClassAtomicCounter)
701 ptrbase.basetype = SPIRType::AtomicCounter;
702
703 break;
704 }
705
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200706 case OpTypeStruct:
707 {
708 uint32_t id = ops[0];
709 auto &type = set<SPIRType>(id);
710 type.basetype = SPIRType::Struct;
711 for (uint32_t i = 1; i < length; i++)
712 type.member_types.push_back(ops[i]);
713
714 // Check if we have seen this struct type before, with just different
715 // decorations.
716 //
717 // Add workaround for issue #17 as well by looking at OpName for the struct
718 // types, which we shouldn't normally do.
719 // We should not normally have to consider type aliases like this to begin with
720 // however ... glslang issues #304, #307 cover this.
721
722 // For stripped names, never consider struct type aliasing.
723 // We risk declaring the same struct multiple times, but type-punning is not allowed
724 // so this is safe.
725 bool consider_aliasing = !ir.get_name(type.self).empty();
726 if (consider_aliasing)
727 {
728 for (auto &other : global_struct_cache)
729 {
730 if (ir.get_name(type.self) == ir.get_name(other) &&
731 types_are_logically_equivalent(type, get<SPIRType>(other)))
732 {
733 type.type_alias = other;
734 break;
735 }
736 }
737
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +0200738 if (type.type_alias == TypeID(0))
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200739 global_struct_cache.push_back(id);
740 }
741 break;
742 }
743
744 case OpTypeFunction:
745 {
746 uint32_t id = ops[0];
747 uint32_t ret = ops[1];
748
749 auto &func = set<SPIRFunctionPrototype>(id, ret);
750 for (uint32_t i = 2; i < length; i++)
751 func.parameter_types.push_back(ops[i]);
752 break;
753 }
754
Hans-Kristian Arntzen6b0e5582020-04-21 14:25:18 +0200755 case OpTypeAccelerationStructureKHR:
Patrick Moursda39a7b2019-02-26 15:43:03 +0100756 {
757 uint32_t id = ops[0];
758 auto &type = set<SPIRType>(id);
Hans-Kristian Arntzen6b0e5582020-04-21 14:25:18 +0200759 type.basetype = SPIRType::AccelerationStructure;
760 break;
761 }
762
Hans-Kristian Arntzen1a28a042021-01-06 11:32:26 +0100763 case OpTypeRayQueryKHR:
Hans-Kristian Arntzen6b0e5582020-04-21 14:25:18 +0200764 {
765 uint32_t id = ops[0];
766 auto &type = set<SPIRType>(id);
767 type.basetype = SPIRType::RayQuery;
Patrick Moursda39a7b2019-02-26 15:43:03 +0100768 break;
769 }
770
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200771 // Variable declaration
772 // All variables are essentially pointers with a storage qualifier.
773 case OpVariable:
774 {
775 uint32_t type = ops[0];
776 uint32_t id = ops[1];
777 auto storage = static_cast<StorageClass>(ops[2]);
778 uint32_t initializer = length == 4 ? ops[3] : 0;
779
780 if (storage == StorageClassFunction)
781 {
782 if (!current_function)
783 SPIRV_CROSS_THROW("No function currently in scope");
784 current_function->add_local_variable(id);
785 }
786
787 set<SPIRVariable>(id, type, storage, initializer);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200788 break;
789 }
790
791 // OpPhi
792 // OpPhi is a fairly magical opcode.
793 // It selects temporary variables based on which parent block we *came from*.
794 // In high-level languages we can "de-SSA" by creating a function local, and flush out temporaries to this function-local
795 // variable to emulate SSA Phi.
796 case OpPhi:
797 {
798 if (!current_function)
799 SPIRV_CROSS_THROW("No function currently in scope");
800 if (!current_block)
801 SPIRV_CROSS_THROW("No block currently in scope");
802
803 uint32_t result_type = ops[0];
804 uint32_t id = ops[1];
805
806 // Instead of a temporary, create a new function-wide temporary with this ID instead.
807 auto &var = set<SPIRVariable>(id, result_type, spv::StorageClassFunction);
808 var.phi_variable = true;
809
810 current_function->add_local_variable(id);
811
812 for (uint32_t i = 2; i + 2 <= length; i += 2)
813 current_block->phi_variables.push_back({ ops[i], ops[i + 1], id });
814 break;
815 }
816
817 // Constants
818 case OpSpecConstant:
819 case OpConstant:
820 {
821 uint32_t id = ops[1];
822 auto &type = get<SPIRType>(ops[0]);
823
824 if (type.width > 32)
825 set<SPIRConstant>(id, ops[0], ops[2] | (uint64_t(ops[3]) << 32), op == OpSpecConstant);
826 else
827 set<SPIRConstant>(id, ops[0], ops[2], op == OpSpecConstant);
828 break;
829 }
830
831 case OpSpecConstantFalse:
832 case OpConstantFalse:
833 {
834 uint32_t id = ops[1];
835 set<SPIRConstant>(id, ops[0], uint32_t(0), op == OpSpecConstantFalse);
836 break;
837 }
838
839 case OpSpecConstantTrue:
840 case OpConstantTrue:
841 {
842 uint32_t id = ops[1];
843 set<SPIRConstant>(id, ops[0], uint32_t(1), op == OpSpecConstantTrue);
844 break;
845 }
846
847 case OpConstantNull:
848 {
849 uint32_t id = ops[1];
850 uint32_t type = ops[0];
Hans-Kristian Arntzenb8905bb2020-03-26 11:21:23 +0100851 ir.make_constant_null(id, type, true);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200852 break;
853 }
854
855 case OpSpecConstantComposite:
856 case OpConstantComposite:
857 {
858 uint32_t id = ops[1];
859 uint32_t type = ops[0];
860
861 auto &ctype = get<SPIRType>(type);
862
863 // We can have constants which are structs and arrays.
864 // In this case, our SPIRConstant will be a list of other SPIRConstant ids which we
865 // can refer to.
866 if (ctype.basetype == SPIRType::Struct || !ctype.array.empty())
867 {
868 set<SPIRConstant>(id, type, ops + 2, length - 2, op == OpSpecConstantComposite);
869 }
870 else
871 {
872 uint32_t elements = length - 2;
873 if (elements > 4)
874 SPIRV_CROSS_THROW("OpConstantComposite only supports 1, 2, 3 and 4 elements.");
875
876 SPIRConstant remapped_constant_ops[4];
877 const SPIRConstant *c[4];
878 for (uint32_t i = 0; i < elements; i++)
879 {
880 // Specialization constants operations can also be part of this.
881 // We do not know their value, so any attempt to query SPIRConstant later
882 // will fail. We can only propagate the ID of the expression and use to_expression on it.
883 auto *constant_op = maybe_get<SPIRConstantOp>(ops[2 + i]);
Hans-Kristian Arntzendf3e21a2019-03-27 10:51:23 +0100884 auto *undef_op = maybe_get<SPIRUndef>(ops[2 + i]);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200885 if (constant_op)
886 {
887 if (op == OpConstantComposite)
888 SPIRV_CROSS_THROW("Specialization constant operation used in OpConstantComposite.");
889
890 remapped_constant_ops[i].make_null(get<SPIRType>(constant_op->basetype));
891 remapped_constant_ops[i].self = constant_op->self;
892 remapped_constant_ops[i].constant_type = constant_op->basetype;
893 remapped_constant_ops[i].specialization = true;
894 c[i] = &remapped_constant_ops[i];
895 }
Hans-Kristian Arntzendf3e21a2019-03-27 10:51:23 +0100896 else if (undef_op)
897 {
898 // Undefined, just pick 0.
899 remapped_constant_ops[i].make_null(get<SPIRType>(undef_op->basetype));
900 remapped_constant_ops[i].constant_type = undef_op->basetype;
901 c[i] = &remapped_constant_ops[i];
902 }
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +0200903 else
904 c[i] = &get<SPIRConstant>(ops[2 + i]);
905 }
906 set<SPIRConstant>(id, type, c, elements, op == OpSpecConstantComposite);
907 }
908 break;
909 }
910
911 // Functions
912 case OpFunction:
913 {
914 uint32_t res = ops[0];
915 uint32_t id = ops[1];
916 // Control
917 uint32_t type = ops[3];
918
919 if (current_function)
920 SPIRV_CROSS_THROW("Must end a function before starting a new one!");
921
922 current_function = &set<SPIRFunction>(id, res, type);
923 break;
924 }
925
926 case OpFunctionParameter:
927 {
928 uint32_t type = ops[0];
929 uint32_t id = ops[1];
930
931 if (!current_function)
932 SPIRV_CROSS_THROW("Must be in a function!");
933
934 current_function->add_parameter(type, id);
935 set<SPIRVariable>(id, type, StorageClassFunction);
936 break;
937 }
938
939 case OpFunctionEnd:
940 {
941 if (current_block)
942 {
943 // Very specific error message, but seems to come up quite often.
944 SPIRV_CROSS_THROW(
945 "Cannot end a function before ending the current block.\n"
946 "Likely cause: If this SPIR-V was created from glslang HLSL, make sure the entry point is valid.");
947 }
948 current_function = nullptr;
949 break;
950 }
951
952 // Blocks
953 case OpLabel:
954 {
955 // OpLabel always starts a block.
956 if (!current_function)
957 SPIRV_CROSS_THROW("Blocks cannot exist outside functions!");
958
959 uint32_t id = ops[0];
960
961 current_function->blocks.push_back(id);
962 if (!current_function->entry_block)
963 current_function->entry_block = id;
964
965 if (current_block)
966 SPIRV_CROSS_THROW("Cannot start a block before ending the current block.");
967
968 current_block = &set<SPIRBlock>(id);
969 break;
970 }
971
972 // Branch instructions end blocks.
973 case OpBranch:
974 {
975 if (!current_block)
976 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
977
978 uint32_t target = ops[0];
979 current_block->terminator = SPIRBlock::Direct;
980 current_block->next_block = target;
981 current_block = nullptr;
982 break;
983 }
984
985 case OpBranchConditional:
986 {
987 if (!current_block)
988 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
989
990 current_block->condition = ops[0];
991 current_block->true_block = ops[1];
992 current_block->false_block = ops[2];
993
994 current_block->terminator = SPIRBlock::Select;
Hans-Kristian Arntzen2714f542021-11-07 12:37:23 +0100995
996 if (current_block->true_block == current_block->false_block)
997 {
998 // Bogus conditional, translate to a direct branch.
999 // Avoids some ugly edge cases later when analyzing CFGs.
1000
1001 // There are some super jank cases where the merge block is different from the true/false,
1002 // and later branches can "break" out of the selection construct this way.
1003 // This is complete nonsense, but CTS hits this case.
1004 // In this scenario, we should see the selection construct as more of a Switch with one default case.
1005 // The problem here is that this breaks any attempt to break out of outer switch statements,
1006 // but it's theoretically solvable if this ever comes up using the ladder breaking system ...
1007
1008 if (current_block->true_block != current_block->next_block &&
1009 current_block->merge == SPIRBlock::MergeSelection)
1010 {
1011 uint32_t ids = ir.increase_bound_by(2);
1012
1013 SPIRType type;
1014 type.basetype = SPIRType::Int;
1015 type.width = 32;
1016 set<SPIRType>(ids, type);
1017 auto &c = set<SPIRConstant>(ids + 1, ids);
1018
1019 current_block->condition = c.self;
1020 current_block->default_block = current_block->true_block;
1021 current_block->terminator = SPIRBlock::MultiSelect;
1022 ir.block_meta[current_block->next_block] &= ~ParsedIR::BLOCK_META_SELECTION_MERGE_BIT;
1023 ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT;
1024 }
1025 else
1026 {
Hans-Kristian Arntzenbe904dc2022-07-22 14:36:31 +02001027 // Collapse loops if we have to.
1028 bool collapsed_loop = current_block->true_block == current_block->merge_block &&
1029 current_block->merge == SPIRBlock::MergeLoop;
1030
1031 if (collapsed_loop)
1032 {
1033 ir.block_meta[current_block->merge_block] &= ~ParsedIR::BLOCK_META_LOOP_MERGE_BIT;
1034 ir.block_meta[current_block->continue_block] &= ~ParsedIR::BLOCK_META_CONTINUE_BIT;
1035 }
1036
Hans-Kristian Arntzen2714f542021-11-07 12:37:23 +01001037 current_block->next_block = current_block->true_block;
1038 current_block->condition = 0;
1039 current_block->true_block = 0;
1040 current_block->false_block = 0;
1041 current_block->merge_block = 0;
1042 current_block->merge = SPIRBlock::MergeNone;
1043 current_block->terminator = SPIRBlock::Direct;
1044 }
1045 }
1046
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001047 current_block = nullptr;
1048 break;
1049 }
1050
1051 case OpSwitch:
1052 {
1053 if (!current_block)
1054 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1055
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001056 current_block->terminator = SPIRBlock::MultiSelect;
1057
1058 current_block->condition = ops[0];
1059 current_block->default_block = ops[1];
1060
Sebastián Aedof099d712021-11-02 17:17:13 -03001061 uint32_t remaining_ops = length - 2;
Sebastián Aedo75e37522021-11-12 10:17:38 -03001062 if ((remaining_ops % 2) == 0)
1063 {
1064 for (uint32_t i = 2; i + 2 <= length; i += 2)
1065 current_block->cases_32bit.push_back({ ops[i], ops[i + 1] });
1066 }
Sebastián Aedof099d712021-11-02 17:17:13 -03001067
Sebastián Aedo48046642021-11-08 15:18:13 -03001068 if ((remaining_ops % 3) == 0)
Sebastián Aedo75e37522021-11-12 10:17:38 -03001069 {
Sebastián Aedo48046642021-11-08 15:18:13 -03001070 for (uint32_t i = 2; i + 3 <= length; i += 3)
1071 {
Sebastián Aedo6d8302e2021-11-18 16:08:59 -03001072 uint64_t value = (static_cast<uint64_t>(ops[i + 1]) << 32) | ops[i];
Sebastián Aedof099d712021-11-02 17:17:13 -03001073 current_block->cases_64bit.push_back({ value, ops[i + 2] });
Sebastián Aedo3eb55322021-10-28 19:57:41 -03001074 }
Sebastián Aedo75e37522021-11-12 10:17:38 -03001075 }
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001076
1077 // If we jump to next block, make it break instead since we're inside a switch case block at that point.
1078 ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT;
1079
1080 current_block = nullptr;
1081 break;
1082 }
1083
1084 case OpKill:
Hans-Kristian Arntzendc62cc72022-03-03 10:36:07 +01001085 case OpTerminateInvocation:
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001086 {
1087 if (!current_block)
1088 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1089 current_block->terminator = SPIRBlock::Kill;
1090 current_block = nullptr;
1091 break;
1092 }
1093
Hans-Kristian Arntzen2097c302021-01-08 11:37:29 +01001094 case OpTerminateRayKHR:
1095 // NV variant is not a terminator.
1096 if (!current_block)
1097 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1098 current_block->terminator = SPIRBlock::TerminateRay;
1099 current_block = nullptr;
1100 break;
1101
1102 case OpIgnoreIntersectionKHR:
1103 // NV variant is not a terminator.
1104 if (!current_block)
1105 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1106 current_block->terminator = SPIRBlock::IgnoreIntersection;
1107 current_block = nullptr;
1108 break;
1109
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001110 case OpReturn:
1111 {
1112 if (!current_block)
1113 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1114 current_block->terminator = SPIRBlock::Return;
1115 current_block = nullptr;
1116 break;
1117 }
1118
1119 case OpReturnValue:
1120 {
1121 if (!current_block)
1122 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1123 current_block->terminator = SPIRBlock::Return;
1124 current_block->return_value = ops[0];
1125 current_block = nullptr;
1126 break;
1127 }
1128
1129 case OpUnreachable:
1130 {
1131 if (!current_block)
1132 SPIRV_CROSS_THROW("Trying to end a non-existing block.");
1133 current_block->terminator = SPIRBlock::Unreachable;
1134 current_block = nullptr;
1135 break;
1136 }
1137
1138 case OpSelectionMerge:
1139 {
1140 if (!current_block)
1141 SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
1142
1143 current_block->next_block = ops[0];
1144 current_block->merge = SPIRBlock::MergeSelection;
1145 ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_SELECTION_MERGE_BIT;
1146
1147 if (length >= 2)
1148 {
1149 if (ops[1] & SelectionControlFlattenMask)
1150 current_block->hint = SPIRBlock::HintFlatten;
1151 else if (ops[1] & SelectionControlDontFlattenMask)
1152 current_block->hint = SPIRBlock::HintDontFlatten;
1153 }
1154 break;
1155 }
1156
1157 case OpLoopMerge:
1158 {
1159 if (!current_block)
1160 SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
1161
1162 current_block->merge_block = ops[0];
1163 current_block->continue_block = ops[1];
1164 current_block->merge = SPIRBlock::MergeLoop;
1165
1166 ir.block_meta[current_block->self] |= ParsedIR::BLOCK_META_LOOP_HEADER_BIT;
1167 ir.block_meta[current_block->merge_block] |= ParsedIR::BLOCK_META_LOOP_MERGE_BIT;
1168
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +02001169 ir.continue_block_to_loop_header[current_block->continue_block] = BlockID(current_block->self);
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001170
1171 // Don't add loop headers to continue blocks,
1172 // which would make it impossible branch into the loop header since
1173 // they are treated as continues.
Hans-Kristian Arntzen333980a2019-09-05 12:43:40 +02001174 if (current_block->continue_block != BlockID(current_block->self))
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001175 ir.block_meta[current_block->continue_block] |= ParsedIR::BLOCK_META_CONTINUE_BIT;
1176
1177 if (length >= 3)
1178 {
1179 if (ops[2] & LoopControlUnrollMask)
1180 current_block->hint = SPIRBlock::HintUnroll;
1181 else if (ops[2] & LoopControlDontUnrollMask)
1182 current_block->hint = SPIRBlock::HintDontUnroll;
1183 }
1184 break;
1185 }
1186
1187 case OpSpecConstantOp:
1188 {
1189 if (length < 3)
1190 SPIRV_CROSS_THROW("OpSpecConstantOp not enough arguments.");
1191
1192 uint32_t result_type = ops[0];
1193 uint32_t id = ops[1];
1194 auto spec_op = static_cast<Op>(ops[2]);
1195
1196 set<SPIRConstantOp>(id, result_type, spec_op, ops + 3, length - 3);
1197 break;
1198 }
1199
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +02001200 case OpLine:
1201 {
1202 // OpLine might come at global scope, but we don't care about those since they will not be declared in any
1203 // meaningful correct order.
Hans-Kristian Arntzen48a7da42019-05-28 15:51:42 +02001204 // Ignore all OpLine directives which live outside a function.
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +02001205 if (current_block)
1206 current_block->ops.push_back(instruction);
1207
Hans-Kristian Arntzen48a7da42019-05-28 15:51:42 +02001208 // Line directives may arrive before first OpLabel.
1209 // Treat this as the line of the function declaration,
1210 // so warnings for arguments can propagate properly.
1211 if (current_function)
Hans-Kristian Arntzen65af09d2019-05-28 13:41:46 +02001212 {
1213 // Store the first one we find and emit it before creating the function prototype.
1214 if (current_function->entry_line.file_id == 0)
1215 {
1216 current_function->entry_line.file_id = ops[0];
1217 current_function->entry_line.line_literal = ops[1];
1218 }
1219 }
1220 break;
1221 }
1222
Lifeng Pan5ca87792019-07-04 16:03:06 +08001223 case OpNoLine:
1224 {
1225 // OpNoLine might come at global scope.
1226 if (current_block)
1227 current_block->ops.push_back(instruction);
Hans-Kristian Arntzen13378ad2019-07-05 10:25:18 +02001228 break;
Lifeng Pan5ca87792019-07-04 16:03:06 +08001229 }
1230
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001231 // Actual opcodes.
1232 default:
1233 {
Sebastián Aedo75e37522021-11-12 10:17:38 -03001234 if (length >= 2)
Sebastián Aedo48046642021-11-08 15:18:13 -03001235 {
Sebastián Aedof099d712021-11-02 17:17:13 -03001236 const auto *type = maybe_get<SPIRType>(ops[0]);
Sebastián Aedo48046642021-11-08 15:18:13 -03001237 if (type)
Sebastián Aedo250a0292021-11-03 16:12:14 -03001238 ir.load_type_width.insert({ ops[1], type->width });
Sebastián Aedof099d712021-11-02 17:17:13 -03001239 }
Hans-Kristian Arntzen29cc1892022-02-16 11:49:24 +01001240
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001241 if (!current_block)
1242 SPIRV_CROSS_THROW("Currently no block to insert opcode.");
1243
1244 current_block->ops.push_back(instruction);
1245 break;
1246 }
1247 }
1248}
1249
1250bool Parser::types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const
1251{
1252 if (a.basetype != b.basetype)
1253 return false;
1254 if (a.width != b.width)
1255 return false;
1256 if (a.vecsize != b.vecsize)
1257 return false;
1258 if (a.columns != b.columns)
1259 return false;
1260 if (a.array.size() != b.array.size())
1261 return false;
1262
1263 size_t array_count = a.array.size();
1264 if (array_count && memcmp(a.array.data(), b.array.data(), array_count * sizeof(uint32_t)) != 0)
1265 return false;
1266
1267 if (a.basetype == SPIRType::Image || a.basetype == SPIRType::SampledImage)
1268 {
1269 if (memcmp(&a.image, &b.image, sizeof(SPIRType::Image)) != 0)
1270 return false;
1271 }
1272
1273 if (a.member_types.size() != b.member_types.size())
1274 return false;
1275
1276 size_t member_types = a.member_types.size();
1277 for (size_t i = 0; i < member_types; i++)
1278 {
1279 if (!types_are_logically_equivalent(get<SPIRType>(a.member_types[i]), get<SPIRType>(b.member_types[i])))
1280 return false;
1281 }
1282
1283 return true;
1284}
1285
1286bool Parser::variable_storage_is_aliased(const SPIRVariable &v) const
1287{
1288 auto &type = get<SPIRType>(v.basetype);
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +01001289
1290 auto *type_meta = ir.find_meta(type.self);
1291
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001292 bool ssbo = v.storage == StorageClassStorageBuffer ||
Hans-Kristian Arntzenb6298782019-01-10 14:04:01 +01001293 (type_meta && type_meta->decoration.decoration_flags.get(DecorationBufferBlock));
Hans-Kristian Arntzen5bcf02f2018-10-05 11:30:57 +02001294 bool image = type.basetype == SPIRType::Image;
1295 bool counter = type.basetype == SPIRType::AtomicCounter;
1296
1297 bool is_restrict;
1298 if (ssbo)
1299 is_restrict = ir.get_buffer_block_flags(v).get(DecorationRestrict);
1300 else
1301 is_restrict = ir.has_decoration(v.self, DecorationRestrict);
1302
1303 return !is_restrict && (ssbo || image || counter);
1304}
Hans-Kristian Arntzena489ba72019-04-02 11:19:03 +02001305} // namespace SPIRV_CROSS_NAMESPACE