Debian Codegraph @

thirdparty/spirv-cross/spirv_cross_parsed_ir.cpp

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