Debian Codegraph @

third_party/spirv-cross/spirv_cross_parsed_ir.cpp

raw
1 /*
2 * Copyright 2018-2020 Arm Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "spirv_cross_parsed_ir.hpp"
18 #include <algorithm>
19 #include <assert.h>
20
21 using namespace std;
22 using namespace spv;
23
24 namespace SPIRV_CROSS_NAMESPACE
25 {
26 ParsedIR::ParsedIR()
27 {
28 // If we move ParsedIR, we need to make sure the pointer stays fixed since the child Variant objects consume a pointer to this group,
29 // so need an extra pointer here.
30 pool_group.reset(new ObjectPoolGroup);
31
32 pool_group->pools[TypeType].reset(new ObjectPool<SPIRType>);
33 pool_group->pools[TypeVariable].reset(new ObjectPool<SPIRVariable>);
34 pool_group->pools[TypeConstant].reset(new ObjectPool<SPIRConstant>);
35 pool_group->pools[TypeFunction].reset(new ObjectPool<SPIRFunction>);
36 pool_group->pools[TypeFunctionPrototype].reset(new ObjectPool<SPIRFunctionPrototype>);
37 pool_group->pools[TypeBlock].reset(new ObjectPool<SPIRBlock>);
38 pool_group->pools[TypeExtension].reset(new ObjectPool<SPIRExtension>);
39 pool_group->pools[TypeExpression].reset(new ObjectPool<SPIRExpression>);
40 pool_group->pools[TypeConstantOp].reset(new ObjectPool<SPIRConstantOp>);
41 pool_group->pools[TypeCombinedImageSampler].reset(new ObjectPool<SPIRCombinedImageSampler>);
42 pool_group->pools[TypeAccessChain].reset(new ObjectPool<SPIRAccessChain>);
43 pool_group->pools[TypeUndef].reset(new ObjectPool<SPIRUndef>);
44 pool_group->pools[TypeString].reset(new ObjectPool<SPIRString>);
45 }
46
47 // Should have been default-implemented, but need this on MSVC 2013.
48 ParsedIR::ParsedIR(ParsedIR &&other) SPIRV_CROSS_NOEXCEPT
49 {
50 *this = move(other);
51 }
52
53 ParsedIR &ParsedIR::operator=(ParsedIR &&other) SPIRV_CROSS_NOEXCEPT
54 {
55 if (this != &other)
56 {
57 pool_group = move(other.pool_group);
58 spirv = move(other.spirv);
59 meta = move(other.meta);
60 for (int i = 0; i < TypeCount; i++)
61 ids_for_type[i] = move(other.ids_for_type[i]);
62 ids_for_constant_or_type = move(other.ids_for_constant_or_type);
63 ids_for_constant_or_variable = move(other.ids_for_constant_or_variable);
64 declared_capabilities = move(other.declared_capabilities);
65 declared_extensions = move(other.declared_extensions);
66 block_meta = move(other.block_meta);
67 continue_block_to_loop_header = move(other.continue_block_to_loop_header);
68 entry_points = move(other.entry_points);
69 ids = move(other.ids);
70 addressing_model = other.addressing_model;
71 memory_model = other.memory_model;
72
73 default_entry_point = other.default_entry_point;
74 source = other.source;
75 loop_iteration_depth_hard = other.loop_iteration_depth_hard;
76 loop_iteration_depth_soft = other.loop_iteration_depth_soft;
77
78 meta_needing_name_fixup = std::move(other.meta_needing_name_fixup);
79 }
80 return *this;
81 }
82
83 ParsedIR::ParsedIR(const ParsedIR &other)
84 : ParsedIR()
85 {
86 *this = other;
87 }
88
89 ParsedIR &ParsedIR::operator=(const ParsedIR &other)
90 {
91 if (this != &other)
92 {
93 spirv = other.spirv;
94 meta = other.meta;
95 for (int i = 0; i < TypeCount; i++)
96 ids_for_type[i] = other.ids_for_type[i];
97 ids_for_constant_or_type = other.ids_for_constant_or_type;
98 ids_for_constant_or_variable = other.ids_for_constant_or_variable;
99 declared_capabilities = other.declared_capabilities;
100 declared_extensions = other.declared_extensions;
101 block_meta = other.block_meta;
102 continue_block_to_loop_header = other.continue_block_to_loop_header;
103 entry_points = other.entry_points;
104 default_entry_point = other.default_entry_point;
105 source = other.source;
106 loop_iteration_depth_hard = other.loop_iteration_depth_hard;
107 loop_iteration_depth_soft = other.loop_iteration_depth_soft;
108 addressing_model = other.addressing_model;
109 memory_model = other.memory_model;
110
111 meta_needing_name_fixup = other.meta_needing_name_fixup;
112
113 // Very deliberate copying of IDs. There is no default copy constructor, nor a simple default constructor.
114 // Construct object first so we have the correct allocator set-up, then we can copy object into our new pool group.
115 ids.clear();
116 ids.reserve(other.ids.size());
117 for (size_t i = 0; i < other.ids.size(); i++)
118 {
119 ids.emplace_back(pool_group.get());
120 ids.back() = other.ids[i];
121 }
122 }
123 return *this;
124 }
125
126 void ParsedIR::set_id_bounds(uint32_t bounds)
127 {
128 ids.reserve(bounds);
129 while (ids.size() < bounds)
130 ids.emplace_back(pool_group.get());
131
132 block_meta.resize(bounds);
133 }
134
135 // Roll our own versions of these functions to avoid potential locale shenanigans.
136 static bool is_alpha(char c)
137 {
138 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
139 }
140
141 static bool is_numeric(char c)
142 {
143 return c >= '0' && c <= '9';
144 }
145
146 static bool is_alphanumeric(char c)
147 {
148 return is_alpha(c) || is_numeric(c);
149 }
150
151 static bool is_valid_identifier(const string &name)
152 {
153 if (name.empty())
154 return true;
155
156 if (is_numeric(name[0]))
157 return false;
158
159 for (auto c : name)
160 if (!is_alphanumeric(c) && c != '_')
161 return false;
162
163 bool saw_underscore = false;
164 // Two underscores in a row is not a valid identifier either.
165 // Technically reserved, but it's easier to treat it as invalid.
166 for (auto c : name)
167 {
168 bool is_underscore = c == '_';
169 if (is_underscore && saw_underscore)
170 return false;
171 saw_underscore = is_underscore;
172 }
173
174 return true;
175 }
176
177 static bool is_reserved_prefix(const string &name)
178 {
179 // Generic reserved identifiers used by the implementation.
180 return name.compare(0, 3, "gl_", 3) == 0 ||
181 // Ignore this case for now, might rewrite internal code to always use spv prefix.
182 //name.compare(0, 11, "SPIRV_Cross", 11) == 0 ||
183 name.compare(0, 3, "spv", 3) == 0;
184 }
185
186 static bool is_reserved_identifier(const string &name, bool member, bool allow_reserved_prefixes)
187 {
188 if (!allow_reserved_prefixes && is_reserved_prefix(name))
189 return true;
190
191 if (member)
192 {
193 // Reserved member identifiers come in one form:
194 // _m[0-9]+$.
195 if (name.size() < 3)
196 return false;
197
198 if (name.compare(0, 2, "_m", 2) != 0)
199 return false;
200
201 size_t index = 2;
202 while (index < name.size() && is_numeric(name[index]))
203 index++;
204
205 return index == name.size();
206 }
207 else
208 {
209 // Reserved non-member identifiers come in two forms:
210 // _[0-9]+$, used for temporaries which map directly to a SPIR-V ID.
211 // _[0-9]+_, used for auxillary temporaries which derived from a SPIR-V ID.
212 if (name.size() < 2)
213 return false;
214
215 if (name[0] != '_' || !is_numeric(name[1]))
216 return false;
217
218 size_t index = 2;
219 while (index < name.size() && is_numeric(name[index]))
220 index++;
221
222 return index == name.size() || (index < name.size() && name[index] == '_');
223 }
224 }
225
226 bool ParsedIR::is_globally_reserved_identifier(std::string &str, bool allow_reserved_prefixes)
227 {
228 return is_reserved_identifier(str, false, allow_reserved_prefixes);
229 }
230
231 static string make_unreserved_identifier(const string &name)
232 {
233 if (is_reserved_prefix(name))
234 return "_RESERVED_IDENTIFIER_FIXUP_" + name;
235 else
236 return "_RESERVED_IDENTIFIER_FIXUP" + name;
237 }
238
239 void ParsedIR::sanitize_underscores(std::string &str)
240 {
241 // Compact adjacent underscores to make it valid.
242 auto dst = str.begin();
243 auto src = dst;
244 bool saw_underscore = false;
245 while (src != str.end())
246 {
247 bool is_underscore = *src == '_';
248 if (saw_underscore && is_underscore)
249 {
250 src++;
251 }
252 else
253 {
254 if (dst != src)
255 *dst = *src;
256 dst++;
257 src++;
258 saw_underscore = is_underscore;
259 }
260 }
261 str.erase(dst, str.end());
262 }
263
264 static string ensure_valid_identifier(const string &name)
265 {
266 // Functions in glslangValidator are mangled with name(<mangled> stuff.
267 // Normally, we would never see '(' in any legal identifiers, so just strip them out.
268 auto str = name.substr(0, name.find('('));
269
270 if (str.empty())
271 return str;
272
273 if (is_numeric(str[0]))
274 str[0] = '_';
275
276 for (auto &c : str)
277 if (!is_alphanumeric(c) && c != '_')
278 c = '_';
279
280 ParsedIR::sanitize_underscores(str);
281 return str;
282 }
283
284 const string &ParsedIR::get_name(ID id) const
285 {
286 auto *m = find_meta(id);
287 if (m)
288 return m->decoration.alias;
289 else
290 return empty_string;
291 }
292
293 const string &ParsedIR::get_member_name(TypeID id, uint32_t index) const
294 {
295 auto *m = find_meta(id);
296 if (m)
297 {
298 if (index >= m->members.size())
299 return empty_string;
300 return m->members[index].alias;
301 }
302 else
303 return empty_string;
304 }
305
306 void ParsedIR::sanitize_identifier(std::string &name, bool member, bool allow_reserved_prefixes)
307 {
308 if (!is_valid_identifier(name))
309 name = ensure_valid_identifier(name);
310 if (is_reserved_identifier(name, member, allow_reserved_prefixes))
311 name = make_unreserved_identifier(name);
312 }
313
314 void ParsedIR::fixup_reserved_names()
315 {
316 for (uint32_t id : meta_needing_name_fixup)
317 {
318 auto &m = meta[id];
319 sanitize_identifier(m.decoration.alias, false, false);
320 for (auto &memb : m.members)
321 sanitize_identifier(memb.alias, true, false);
322 }
323 meta_needing_name_fixup.clear();
324 }
325
326 void ParsedIR::set_name(ID id, const string &name)
327 {
328 auto &m = meta[id];
329 m.decoration.alias = name;
330 if (!is_valid_identifier(name) || is_reserved_identifier(name, false, false))
331 meta_needing_name_fixup.insert(id);
332 }
333
334 void ParsedIR::set_member_name(TypeID id, uint32_t index, const string &name)
335 {
336 auto &m = meta[id];
337 m.members.resize(max(meta[id].members.size(), size_t(index) + 1));
338 m.members[index].alias = name;
339 if (!is_valid_identifier(name) || is_reserved_identifier(name, true, false))
340 meta_needing_name_fixup.insert(id);
341 }
342
343 void ParsedIR::set_decoration_string(ID id, Decoration decoration, const string &argument)
344 {
345 auto &dec = meta[id].decoration;
346 dec.decoration_flags.set(decoration);
347
348 switch (decoration)
349 {
350 case DecorationHlslSemanticGOOGLE:
351 dec.hlsl_semantic = argument;
352 break;
353
354 default:
355 break;
356 }
357 }
358
359 void ParsedIR::set_decoration(ID id, Decoration decoration, uint32_t argument)
360 {
361 auto &dec = meta[id].decoration;
362 dec.decoration_flags.set(decoration);
363
364 switch (decoration)
365 {
366 case DecorationBuiltIn:
367 dec.builtin = true;
368 dec.builtin_type = static_cast<BuiltIn>(argument);
369 break;
370
371 case DecorationLocation:
372 dec.location = argument;
373 break;
374
375 case DecorationComponent:
376 dec.component = argument;
377 break;
378
379 case DecorationOffset:
380 dec.offset = argument;
381 break;
382
383 case DecorationXfbBuffer:
384 dec.xfb_buffer = argument;
385 break;
386
387 case DecorationXfbStride:
388 dec.xfb_stride = argument;
389 break;
390
391 case DecorationStream:
392 dec.stream = argument;
393 break;
394
395 case DecorationArrayStride:
396 dec.array_stride = argument;
397 break;
398
399 case DecorationMatrixStride:
400 dec.matrix_stride = argument;
401 break;
402
403 case DecorationBinding:
404 dec.binding = argument;
405 break;
406
407 case DecorationDescriptorSet:
408 dec.set = argument;
409 break;
410
411 case DecorationInputAttachmentIndex:
412 dec.input_attachment = argument;
413 break;
414
415 case DecorationSpecId:
416 dec.spec_id = argument;
417 break;
418
419 case DecorationIndex:
420 dec.index = argument;
421 break;
422
423 case DecorationHlslCounterBufferGOOGLE:
424 meta[id].hlsl_magic_counter_buffer = argument;
425 meta[argument].hlsl_is_magic_counter_buffer = true;
426 break;
427
428 case DecorationFPRoundingMode:
429 dec.fp_rounding_mode = static_cast<FPRoundingMode>(argument);
430 break;
431
432 default:
433 break;
434 }
435 }
436
437 void ParsedIR::set_member_decoration(TypeID id, uint32_t index, Decoration decoration, uint32_t argument)
438 {
439 meta[id].members.resize(max(meta[id].members.size(), size_t(index) + 1));
440 auto &dec = meta[id].members[index];
441 dec.decoration_flags.set(decoration);
442
443 switch (decoration)
444 {
445 case DecorationBuiltIn:
446 dec.builtin = true;
447 dec.builtin_type = static_cast<BuiltIn>(argument);
448 break;
449
450 case DecorationLocation:
451 dec.location = argument;
452 break;
453
454 case DecorationComponent:
455 dec.component = argument;
456 break;
457
458 case DecorationBinding:
459 dec.binding = argument;
460 break;
461
462 case DecorationOffset:
463 dec.offset = argument;
464 break;
465
466 case DecorationXfbBuffer:
467 dec.xfb_buffer = argument;
468 break;
469
470 case DecorationXfbStride:
471 dec.xfb_stride = argument;
472 break;
473
474 case DecorationStream:
475 dec.stream = argument;
476 break;
477
478 case DecorationSpecId:
479 dec.spec_id = argument;
480 break;
481
482 case DecorationMatrixStride:
483 dec.matrix_stride = argument;
484 break;
485
486 case DecorationIndex:
487 dec.index = argument;
488 break;
489
490 default:
491 break;
492 }
493 }
494
495 // Recursively marks any constants referenced by the specified constant instruction as being used
496 // as an array length. The id must be a constant instruction (SPIRConstant or SPIRConstantOp).
497 void ParsedIR::mark_used_as_array_length(ID id)
498 {
499 switch (ids[id].get_type())
500 {
501 case TypeConstant:
502 get<SPIRConstant>(id).is_used_as_array_length = true;
503 break;
504
505 case TypeConstantOp:
506 {
507 auto &cop = get<SPIRConstantOp>(id);
508 if (cop.opcode == OpCompositeExtract)
509 mark_used_as_array_length(cop.arguments[0]);
510 else if (cop.opcode == OpCompositeInsert)
511 {
512 mark_used_as_array_length(cop.arguments[0]);
513 mark_used_as_array_length(cop.arguments[1]);
514 }
515 else
516 for (uint32_t arg_id : cop.arguments)
517 mark_used_as_array_length(arg_id);
518 break;
519 }
520
521 case TypeUndef:
522 break;
523
524 default:
525 assert(0);
526 }
527 }
528
529 Bitset ParsedIR::get_buffer_block_flags(const SPIRVariable &var) const
530 {
531 auto &type = get<SPIRType>(var.basetype);
532 assert(type.basetype == SPIRType::Struct);
533
534 // Some flags like non-writable, non-readable are actually found
535 // as member decorations. If all members have a decoration set, propagate
536 // the decoration up as a regular variable decoration.
537 Bitset base_flags;
538 auto *m = find_meta(var.self);
539 if (m)
540 base_flags = m->decoration.decoration_flags;
541
542 if (type.member_types.empty())
543 return base_flags;
544
545 Bitset all_members_flags = get_member_decoration_bitset(type.self, 0);
546 for (uint32_t i = 1; i < uint32_t(type.member_types.size()); i++)
547 all_members_flags.merge_and(get_member_decoration_bitset(type.self, i));
548
549 base_flags.merge_or(all_members_flags);
550 return base_flags;
551 }
552
553 const Bitset &ParsedIR::get_member_decoration_bitset(TypeID id, uint32_t index) const
554 {
555 auto *m = find_meta(id);
556 if (m)
557 {
558 if (index >= m->members.size())
559 return cleared_bitset;
560 return m->members[index].decoration_flags;
561 }
562 else
563 return cleared_bitset;
564 }
565
566 bool ParsedIR::has_decoration(ID id, Decoration decoration) const
567 {
568 return get_decoration_bitset(id).get(decoration);
569 }
570
571 uint32_t ParsedIR::get_decoration(ID id, Decoration decoration) const
572 {
573 auto *m = find_meta(id);
574 if (!m)
575 return 0;
576
577 auto &dec = m->decoration;
578 if (!dec.decoration_flags.get(decoration))
579 return 0;
580
581 switch (decoration)
582 {
583 case DecorationBuiltIn:
584 return dec.builtin_type;
585 case DecorationLocation:
586 return dec.location;
587 case DecorationComponent:
588 return dec.component;
589 case DecorationOffset:
590 return dec.offset;
591 case DecorationXfbBuffer:
592 return dec.xfb_buffer;
593 case DecorationXfbStride:
594 return dec.xfb_stride;
595 case DecorationStream:
596 return dec.stream;
597 case DecorationBinding:
598 return dec.binding;
599 case DecorationDescriptorSet:
600 return dec.set;
601 case DecorationInputAttachmentIndex:
602 return dec.input_attachment;
603 case DecorationSpecId:
604 return dec.spec_id;
605 case DecorationArrayStride:
606 return dec.array_stride;
607 case DecorationMatrixStride:
608 return dec.matrix_stride;
609 case DecorationIndex:
610 return dec.index;
611 case DecorationFPRoundingMode:
612 return dec.fp_rounding_mode;
613 default:
614 return 1;
615 }
616 }
617
618 const string &ParsedIR::get_decoration_string(ID id, Decoration decoration) const
619 {
620 auto *m = find_meta(id);
621 if (!m)
622 return empty_string;
623
624 auto &dec = m->decoration;
625
626 if (!dec.decoration_flags.get(decoration))
627 return empty_string;
628
629 switch (decoration)
630 {
631 case DecorationHlslSemanticGOOGLE:
632 return dec.hlsl_semantic;
633
634 default:
635 return empty_string;
636 }
637 }
638
639 void ParsedIR::unset_decoration(ID id, Decoration decoration)
640 {
641 auto &dec = meta[id].decoration;
642 dec.decoration_flags.clear(decoration);
643 switch (decoration)
644 {
645 case DecorationBuiltIn:
646 dec.builtin = false;
647 break;
648
649 case DecorationLocation:
650 dec.location = 0;
651 break;
652
653 case DecorationComponent:
654 dec.component = 0;
655 break;
656
657 case DecorationOffset:
658 dec.offset = 0;
659 break;
660
661 case DecorationXfbBuffer:
662 dec.xfb_buffer = 0;
663 break;
664
665 case DecorationXfbStride:
666 dec.xfb_stride = 0;
667 break;
668
669 case DecorationStream:
670 dec.stream = 0;
671 break;
672
673 case DecorationBinding:
674 dec.binding = 0;
675 break;
676
677 case DecorationDescriptorSet:
678 dec.set = 0;
679 break;
680
681 case DecorationInputAttachmentIndex:
682 dec.input_attachment = 0;
683 break;
684
685 case DecorationSpecId:
686 dec.spec_id = 0;
687 break;
688
689 case DecorationHlslSemanticGOOGLE:
690 dec.hlsl_semantic.clear();
691 break;
692
693 case DecorationFPRoundingMode:
694 dec.fp_rounding_mode = FPRoundingModeMax;
695 break;
696
697 case DecorationHlslCounterBufferGOOGLE:
698 {
699 auto &counter = meta[id].hlsl_magic_counter_buffer;
700 if (counter)
701 {
702 meta[counter].hlsl_is_magic_counter_buffer = false;
703 counter = 0;
704 }
705 break;
706 }
707
708 default:
709 break;
710 }
711 }
712
713 bool ParsedIR::has_member_decoration(TypeID id, uint32_t index, Decoration decoration) const
714 {
715 return get_member_decoration_bitset(id, index).get(decoration);
716 }
717
718 uint32_t ParsedIR::get_member_decoration(TypeID id, uint32_t index, Decoration decoration) const
719 {
720 auto *m = find_meta(id);
721 if (!m)
722 return 0;
723
724 if (index >= m->members.size())
725 return 0;
726
727 auto &dec = m->members[index];
728 if (!dec.decoration_flags.get(decoration))
729 return 0;
730
731 switch (decoration)
732 {
733 case DecorationBuiltIn:
734 return dec.builtin_type;
735 case DecorationLocation:
736 return dec.location;
737 case DecorationComponent:
738 return dec.component;
739 case DecorationBinding:
740 return dec.binding;
741 case DecorationOffset:
742 return dec.offset;
743 case DecorationXfbBuffer:
744 return dec.xfb_buffer;
745 case DecorationXfbStride:
746 return dec.xfb_stride;
747 case DecorationStream:
748 return dec.stream;
749 case DecorationSpecId:
750 return dec.spec_id;
751 case DecorationIndex:
752 return dec.index;
753 default:
754 return 1;
755 }
756 }
757
758 const Bitset &ParsedIR::get_decoration_bitset(ID id) const
759 {
760 auto *m = find_meta(id);
761 if (m)
762 {
763 auto &dec = m->decoration;
764 return dec.decoration_flags;
765 }
766 else
767 return cleared_bitset;
768 }
769
770 void ParsedIR::set_member_decoration_string(TypeID id, uint32_t index, Decoration decoration, const string &argument)
771 {
772 meta[id].members.resize(max(meta[id].members.size(), size_t(index) + 1));
773 auto &dec = meta[id].members[index];
774 dec.decoration_flags.set(decoration);
775
776 switch (decoration)
777 {
778 case DecorationHlslSemanticGOOGLE:
779 dec.hlsl_semantic = argument;
780 break;
781
782 default:
783 break;
784 }
785 }
786
787 const string &ParsedIR::get_member_decoration_string(TypeID id, uint32_t index, Decoration decoration) const
788 {
789 auto *m = find_meta(id);
790 if (m)
791 {
792 if (!has_member_decoration(id, index, decoration))
793 return empty_string;
794
795 auto &dec = m->members[index];
796
797 switch (decoration)
798 {
799 case DecorationHlslSemanticGOOGLE:
800 return dec.hlsl_semantic;
801
802 default:
803 return empty_string;
804 }
805 }
806 else
807 return empty_string;
808 }
809
810 void ParsedIR::unset_member_decoration(TypeID id, uint32_t index, Decoration decoration)
811 {
812 auto &m = meta[id];
813 if (index >= m.members.size())
814 return;
815
816 auto &dec = m.members[index];
817
818 dec.decoration_flags.clear(decoration);
819 switch (decoration)
820 {
821 case DecorationBuiltIn:
822 dec.builtin = false;
823 break;
824
825 case DecorationLocation:
826 dec.location = 0;
827 break;
828
829 case DecorationComponent:
830 dec.component = 0;
831 break;
832
833 case DecorationOffset:
834 dec.offset = 0;
835 break;
836
837 case DecorationXfbBuffer:
838 dec.xfb_buffer = 0;
839 break;
840
841 case DecorationXfbStride:
842 dec.xfb_stride = 0;
843 break;
844
845 case DecorationStream:
846 dec.stream = 0;
847 break;
848
849 case DecorationSpecId:
850 dec.spec_id = 0;
851 break;
852
853 case DecorationHlslSemanticGOOGLE:
854 dec.hlsl_semantic.clear();
855 break;
856
857 default:
858 break;
859 }
860 }
861
862 uint32_t ParsedIR::increase_bound_by(uint32_t incr_amount)
863 {
864 auto curr_bound = ids.size();
865 auto new_bound = curr_bound + incr_amount;
866
867 ids.reserve(ids.size() + incr_amount);
868 for (uint32_t i = 0; i < incr_amount; i++)
869 ids.emplace_back(pool_group.get());
870
871 block_meta.resize(new_bound);
872 return uint32_t(curr_bound);
873 }
874
875 void ParsedIR::remove_typed_id(Types type, ID id)
876 {
877 auto &type_ids = ids_for_type[type];
878 type_ids.erase(remove(begin(type_ids), end(type_ids), id), end(type_ids));
879 }
880
881 void ParsedIR::reset_all_of_type(Types type)
882 {
883 for (auto &id : ids_for_type[type])
884 if (ids[id].get_type() == type)
885 ids[id].reset();
886
887 ids_for_type[type].clear();
888 }
889
890 void ParsedIR::add_typed_id(Types type, ID id)
891 {
892 if (loop_iteration_depth_hard != 0)
893 SPIRV_CROSS_THROW("Cannot add typed ID while looping over it.");
894
895 if (loop_iteration_depth_soft != 0)
896 {
897 if (!ids[id].empty())
898 SPIRV_CROSS_THROW("Cannot override IDs when loop is soft locked.");
899 return;
900 }
901
902 if (ids[id].empty() || ids[id].get_type() != type)
903 {
904 switch (type)
905 {
906 case TypeConstant:
907 ids_for_constant_or_variable.push_back(id);
908 ids_for_constant_or_type.push_back(id);
909 break;
910
911 case TypeVariable:
912 ids_for_constant_or_variable.push_back(id);
913 break;
914
915 case TypeType:
916 case TypeConstantOp:
917 ids_for_constant_or_type.push_back(id);
918 break;
919
920 default:
921 break;
922 }
923 }
924
925 if (ids[id].empty())
926 {
927 ids_for_type[type].push_back(id);
928 }
929 else if (ids[id].get_type() != type)
930 {
931 remove_typed_id(ids[id].get_type(), id);
932 ids_for_type[type].push_back(id);
933 }
934 }
935
936 const Meta *ParsedIR::find_meta(ID id) const
937 {
938 auto itr = meta.find(id);
939 if (itr != end(meta))
940 return &itr->second;
941 else
942 return nullptr;
943 }
944
945 Meta *ParsedIR::find_meta(ID id)
946 {
947 auto itr = meta.find(id);
948 if (itr != end(meta))
949 return &itr->second;
950 else
951 return nullptr;
952 }
953
954 ParsedIR::LoopLock ParsedIR::create_loop_hard_lock() const
955 {
956 return ParsedIR::LoopLock(&loop_iteration_depth_hard);
957 }
958
959 ParsedIR::LoopLock ParsedIR::create_loop_soft_lock() const
960 {
961 return ParsedIR::LoopLock(&loop_iteration_depth_soft);
962 }
963
964 ParsedIR::LoopLock::~LoopLock()
965 {
966 if (lock)
967 (*lock)--;
968 }
969
970 ParsedIR::LoopLock::LoopLock(uint32_t *lock_)
971 : lock(lock_)
972 {
973 if (lock)
974 (*lock)++;
975 }
976
977 ParsedIR::LoopLock::LoopLock(LoopLock &&other) SPIRV_CROSS_NOEXCEPT
978 {
979 *this = move(other);
980 }
981
982 ParsedIR::LoopLock &ParsedIR::LoopLock::operator=(LoopLock &&other) SPIRV_CROSS_NOEXCEPT
983 {
984 if (lock)
985 (*lock)--;
986 lock = other.lock;
987 other.lock = nullptr;
988 return *this;
989 }
990
991 void ParsedIR::make_constant_null(uint32_t id, uint32_t type, bool add_to_typed_id_set)
992 {
993 auto &constant_type = get<SPIRType>(type);
994
995 if (constant_type.pointer)
996 {
997 if (add_to_typed_id_set)
998 add_typed_id(TypeConstant, id);
999 auto &constant = variant_set<SPIRConstant>(ids[id], type);
1000 constant.self = id;
1001 constant.make_null(constant_type);
1002 }
1003 else if (!constant_type.array.empty())
1004 {
1005 assert(constant_type.parent_type);
1006 uint32_t parent_id = increase_bound_by(1);
1007 make_constant_null(parent_id, constant_type.parent_type, add_to_typed_id_set);
1008
1009 if (!constant_type.array_size_literal.back())
1010 SPIRV_CROSS_THROW("Array size of OpConstantNull must be a literal.");
1011
1012 SmallVector<uint32_t> elements(constant_type.array.back());
1013 for (uint32_t i = 0; i < constant_type.array.back(); i++)
1014 elements[i] = parent_id;
1015
1016 if (add_to_typed_id_set)
1017 add_typed_id(TypeConstant, id);
1018 variant_set<SPIRConstant>(ids[id], type, elements.data(), uint32_t(elements.size()), false).self = id;
1019 }
1020 else if (!constant_type.member_types.empty())
1021 {
1022 uint32_t member_ids = increase_bound_by(uint32_t(constant_type.member_types.size()));
1023 SmallVector<uint32_t> elements(constant_type.member_types.size());
1024 for (uint32_t i = 0; i < constant_type.member_types.size(); i++)
1025 {
1026 make_constant_null(member_ids + i, constant_type.member_types[i], add_to_typed_id_set);
1027 elements[i] = member_ids + i;
1028 }
1029
1030 if (add_to_typed_id_set)
1031 add_typed_id(TypeConstant, id);
1032 variant_set<SPIRConstant>(ids[id], type, elements.data(), uint32_t(elements.size()), false).self = id;
1033 }
1034 else
1035 {
1036 if (add_to_typed_id_set)
1037 add_typed_id(TypeConstant, id);
1038 auto &constant = variant_set<SPIRConstant>(ids[id], type);
1039 constant.self = id;
1040 constant.make_null(constant_type);
1041 }
1042 }
1043
1044 } // namespace SPIRV_CROSS_NAMESPACE