Debian Codegraph @

third_party/glslang/SPIRV/SpvBuilder.cpp

raw
1 //
2 // Copyright (C) 2014-2015 LunarG, Inc.
3 // Copyright (C) 2015-2018 Google, Inc.
4 // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided
18 // with the distribution.
19 //
20 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21 // contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
36
37 //
38 // Helper for making SPIR-V IR. Generally, this is documented in the header
39 // SpvBuilder.h.
40 //
41
42 #include <cassert>
43 #include <cstdlib>
44
45 #include <unordered_set>
46 #include <algorithm>
47
48 #include "SpvBuilder.h"
49
50 #ifndef GLSLANG_WEB
51 #include "hex_float.h"
52 #endif
53
54 #ifndef _WIN32
55 #include <cstdio>
56 #endif
57
58 namespace spv {
59
60 Builder::Builder(unsigned int spvVersion, unsigned int magicNumber, SpvBuildLogger* buildLogger) :
61 spvVersion(spvVersion),
62 source(SourceLanguageUnknown),
63 sourceVersion(0),
64 sourceFileStringId(NoResult),
65 currentLine(0),
66 currentFile(nullptr),
67 emitOpLines(false),
68 addressModel(AddressingModelLogical),
69 memoryModel(MemoryModelGLSL450),
70 builderNumber(magicNumber),
71 buildPoint(0),
72 uniqueId(0),
73 entryPointFunction(0),
74 generatingOpCodeForSpecConst(false),
75 logger(buildLogger)
76 {
77 clearAccessChain();
78 }
79
80 Builder::~Builder()
81 {
82 }
83
84 Id Builder::import(const char* name)
85 {
86 Instruction* import = new Instruction(getUniqueId(), NoType, OpExtInstImport);
87 import->addStringOperand(name);
88 module.mapInstruction(import);
89
90 imports.push_back(std::unique_ptr<Instruction>(import));
91 return import->getResultId();
92 }
93
94 // Emit instruction for non-filename-based #line directives (ie. no filename
95 // seen yet): emit an OpLine if we've been asked to emit OpLines and the line
96 // number has changed since the last time, and is a valid line number.
97 void Builder::setLine(int lineNum)
98 {
99 if (lineNum != 0 && lineNum != currentLine) {
100 currentLine = lineNum;
101 if (emitOpLines)
102 addLine(sourceFileStringId, currentLine, 0);
103 }
104 }
105
106 // If no filename, do non-filename-based #line emit. Else do filename-based emit.
107 // Emit OpLine if we've been asked to emit OpLines and the line number or filename
108 // has changed since the last time, and line number is valid.
109 void Builder::setLine(int lineNum, const char* filename)
110 {
111 if (filename == nullptr) {
112 setLine(lineNum);
113 return;
114 }
115 if ((lineNum != 0 && lineNum != currentLine) || currentFile == nullptr ||
116 strncmp(filename, currentFile, strlen(currentFile) + 1) != 0) {
117 currentLine = lineNum;
118 currentFile = filename;
119 if (emitOpLines) {
120 spv::Id strId = getStringId(filename);
121 addLine(strId, currentLine, 0);
122 }
123 }
124 }
125
126 void Builder::addLine(Id fileName, int lineNum, int column)
127 {
128 Instruction* line = new Instruction(OpLine);
129 line->addIdOperand(fileName);
130 line->addImmediateOperand(lineNum);
131 line->addImmediateOperand(column);
132 buildPoint->addInstruction(std::unique_ptr<Instruction>(line));
133 }
134
135 // For creating new groupedTypes (will return old type if the requested one was already made).
136 Id Builder::makeVoidType()
137 {
138 Instruction* type;
139 if (groupedTypes[OpTypeVoid].size() == 0) {
140 type = new Instruction(getUniqueId(), NoType, OpTypeVoid);
141 groupedTypes[OpTypeVoid].push_back(type);
142 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
143 module.mapInstruction(type);
144 } else
145 type = groupedTypes[OpTypeVoid].back();
146
147 return type->getResultId();
148 }
149
150 Id Builder::makeBoolType()
151 {
152 Instruction* type;
153 if (groupedTypes[OpTypeBool].size() == 0) {
154 type = new Instruction(getUniqueId(), NoType, OpTypeBool);
155 groupedTypes[OpTypeBool].push_back(type);
156 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
157 module.mapInstruction(type);
158 } else
159 type = groupedTypes[OpTypeBool].back();
160
161 return type->getResultId();
162 }
163
164 Id Builder::makeSamplerType()
165 {
166 Instruction* type;
167 if (groupedTypes[OpTypeSampler].size() == 0) {
168 type = new Instruction(getUniqueId(), NoType, OpTypeSampler);
169 groupedTypes[OpTypeSampler].push_back(type);
170 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
171 module.mapInstruction(type);
172 } else
173 type = groupedTypes[OpTypeSampler].back();
174
175 return type->getResultId();
176 }
177
178 Id Builder::makePointer(StorageClass storageClass, Id pointee)
179 {
180 // try to find it
181 Instruction* type;
182 for (int t = 0; t < (int)groupedTypes[OpTypePointer].size(); ++t) {
183 type = groupedTypes[OpTypePointer][t];
184 if (type->getImmediateOperand(0) == (unsigned)storageClass &&
185 type->getIdOperand(1) == pointee)
186 return type->getResultId();
187 }
188
189 // not found, make it
190 type = new Instruction(getUniqueId(), NoType, OpTypePointer);
191 type->addImmediateOperand(storageClass);
192 type->addIdOperand(pointee);
193 groupedTypes[OpTypePointer].push_back(type);
194 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
195 module.mapInstruction(type);
196
197 return type->getResultId();
198 }
199
200 Id Builder::makeForwardPointer(StorageClass storageClass)
201 {
202 // Caching/uniquifying doesn't work here, because we don't know the
203 // pointee type and there can be multiple forward pointers of the same
204 // storage type. Somebody higher up in the stack must keep track.
205 Instruction* type = new Instruction(getUniqueId(), NoType, OpTypeForwardPointer);
206 type->addImmediateOperand(storageClass);
207 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
208 module.mapInstruction(type);
209
210 return type->getResultId();
211 }
212
213 Id Builder::makePointerFromForwardPointer(StorageClass storageClass, Id forwardPointerType, Id pointee)
214 {
215 // try to find it
216 Instruction* type;
217 for (int t = 0; t < (int)groupedTypes[OpTypePointer].size(); ++t) {
218 type = groupedTypes[OpTypePointer][t];
219 if (type->getImmediateOperand(0) == (unsigned)storageClass &&
220 type->getIdOperand(1) == pointee)
221 return type->getResultId();
222 }
223
224 type = new Instruction(forwardPointerType, NoType, OpTypePointer);
225 type->addImmediateOperand(storageClass);
226 type->addIdOperand(pointee);
227 groupedTypes[OpTypePointer].push_back(type);
228 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
229 module.mapInstruction(type);
230
231 return type->getResultId();
232 }
233
234 Id Builder::makeIntegerType(int width, bool hasSign)
235 {
236 #ifdef GLSLANG_WEB
237 assert(width == 32);
238 width = 32;
239 #endif
240
241 // try to find it
242 Instruction* type;
243 for (int t = 0; t < (int)groupedTypes[OpTypeInt].size(); ++t) {
244 type = groupedTypes[OpTypeInt][t];
245 if (type->getImmediateOperand(0) == (unsigned)width &&
246 type->getImmediateOperand(1) == (hasSign ? 1u : 0u))
247 return type->getResultId();
248 }
249
250 // not found, make it
251 type = new Instruction(getUniqueId(), NoType, OpTypeInt);
252 type->addImmediateOperand(width);
253 type->addImmediateOperand(hasSign ? 1 : 0);
254 groupedTypes[OpTypeInt].push_back(type);
255 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
256 module.mapInstruction(type);
257
258 // deal with capabilities
259 switch (width) {
260 case 8:
261 case 16:
262 // these are currently handled by storage-type declarations and post processing
263 break;
264 case 64:
265 addCapability(CapabilityInt64);
266 break;
267 default:
268 break;
269 }
270
271 return type->getResultId();
272 }
273
274 Id Builder::makeFloatType(int width)
275 {
276 #ifdef GLSLANG_WEB
277 assert(width == 32);
278 width = 32;
279 #endif
280
281 // try to find it
282 Instruction* type;
283 for (int t = 0; t < (int)groupedTypes[OpTypeFloat].size(); ++t) {
284 type = groupedTypes[OpTypeFloat][t];
285 if (type->getImmediateOperand(0) == (unsigned)width)
286 return type->getResultId();
287 }
288
289 // not found, make it
290 type = new Instruction(getUniqueId(), NoType, OpTypeFloat);
291 type->addImmediateOperand(width);
292 groupedTypes[OpTypeFloat].push_back(type);
293 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
294 module.mapInstruction(type);
295
296 // deal with capabilities
297 switch (width) {
298 case 16:
299 // currently handled by storage-type declarations and post processing
300 break;
301 case 64:
302 addCapability(CapabilityFloat64);
303 break;
304 default:
305 break;
306 }
307
308 return type->getResultId();
309 }
310
311 // Make a struct without checking for duplication.
312 // See makeStructResultType() for non-decorated structs
313 // needed as the result of some instructions, which does
314 // check for duplicates.
315 Id Builder::makeStructType(const std::vector<Id>& members, const char* name)
316 {
317 // Don't look for previous one, because in the general case,
318 // structs can be duplicated except for decorations.
319
320 // not found, make it
321 Instruction* type = new Instruction(getUniqueId(), NoType, OpTypeStruct);
322 for (int op = 0; op < (int)members.size(); ++op)
323 type->addIdOperand(members[op]);
324 groupedTypes[OpTypeStruct].push_back(type);
325 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
326 module.mapInstruction(type);
327 addName(type->getResultId(), name);
328
329 return type->getResultId();
330 }
331
332 // Make a struct for the simple results of several instructions,
333 // checking for duplication.
334 Id Builder::makeStructResultType(Id type0, Id type1)
335 {
336 // try to find it
337 Instruction* type;
338 for (int t = 0; t < (int)groupedTypes[OpTypeStruct].size(); ++t) {
339 type = groupedTypes[OpTypeStruct][t];
340 if (type->getNumOperands() != 2)
341 continue;
342 if (type->getIdOperand(0) != type0 ||
343 type->getIdOperand(1) != type1)
344 continue;
345 return type->getResultId();
346 }
347
348 // not found, make it
349 std::vector<spv::Id> members;
350 members.push_back(type0);
351 members.push_back(type1);
352
353 return makeStructType(members, "ResType");
354 }
355
356 Id Builder::makeVectorType(Id component, int size)
357 {
358 // try to find it
359 Instruction* type;
360 for (int t = 0; t < (int)groupedTypes[OpTypeVector].size(); ++t) {
361 type = groupedTypes[OpTypeVector][t];
362 if (type->getIdOperand(0) == component &&
363 type->getImmediateOperand(1) == (unsigned)size)
364 return type->getResultId();
365 }
366
367 // not found, make it
368 type = new Instruction(getUniqueId(), NoType, OpTypeVector);
369 type->addIdOperand(component);
370 type->addImmediateOperand(size);
371 groupedTypes[OpTypeVector].push_back(type);
372 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
373 module.mapInstruction(type);
374
375 return type->getResultId();
376 }
377
378 Id Builder::makeMatrixType(Id component, int cols, int rows)
379 {
380 assert(cols <= maxMatrixSize && rows <= maxMatrixSize);
381
382 Id column = makeVectorType(component, rows);
383
384 // try to find it
385 Instruction* type;
386 for (int t = 0; t < (int)groupedTypes[OpTypeMatrix].size(); ++t) {
387 type = groupedTypes[OpTypeMatrix][t];
388 if (type->getIdOperand(0) == column &&
389 type->getImmediateOperand(1) == (unsigned)cols)
390 return type->getResultId();
391 }
392
393 // not found, make it
394 type = new Instruction(getUniqueId(), NoType, OpTypeMatrix);
395 type->addIdOperand(column);
396 type->addImmediateOperand(cols);
397 groupedTypes[OpTypeMatrix].push_back(type);
398 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
399 module.mapInstruction(type);
400
401 return type->getResultId();
402 }
403
404 Id Builder::makeCooperativeMatrixType(Id component, Id scope, Id rows, Id cols)
405 {
406 // try to find it
407 Instruction* type;
408 for (int t = 0; t < (int)groupedTypes[OpTypeCooperativeMatrixNV].size(); ++t) {
409 type = groupedTypes[OpTypeCooperativeMatrixNV][t];
410 if (type->getIdOperand(0) == component &&
411 type->getIdOperand(1) == scope &&
412 type->getIdOperand(2) == rows &&
413 type->getIdOperand(3) == cols)
414 return type->getResultId();
415 }
416
417 // not found, make it
418 type = new Instruction(getUniqueId(), NoType, OpTypeCooperativeMatrixNV);
419 type->addIdOperand(component);
420 type->addIdOperand(scope);
421 type->addIdOperand(rows);
422 type->addIdOperand(cols);
423 groupedTypes[OpTypeCooperativeMatrixNV].push_back(type);
424 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
425 module.mapInstruction(type);
426
427 return type->getResultId();
428 }
429
430
431 // TODO: performance: track arrays per stride
432 // If a stride is supplied (non-zero) make an array.
433 // If no stride (0), reuse previous array types.
434 // 'size' is an Id of a constant or specialization constant of the array size
435 Id Builder::makeArrayType(Id element, Id sizeId, int stride)
436 {
437 Instruction* type;
438 if (stride == 0) {
439 // try to find existing type
440 for (int t = 0; t < (int)groupedTypes[OpTypeArray].size(); ++t) {
441 type = groupedTypes[OpTypeArray][t];
442 if (type->getIdOperand(0) == element &&
443 type->getIdOperand(1) == sizeId)
444 return type->getResultId();
445 }
446 }
447
448 // not found, make it
449 type = new Instruction(getUniqueId(), NoType, OpTypeArray);
450 type->addIdOperand(element);
451 type->addIdOperand(sizeId);
452 groupedTypes[OpTypeArray].push_back(type);
453 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
454 module.mapInstruction(type);
455
456 return type->getResultId();
457 }
458
459 Id Builder::makeRuntimeArray(Id element)
460 {
461 Instruction* type = new Instruction(getUniqueId(), NoType, OpTypeRuntimeArray);
462 type->addIdOperand(element);
463 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
464 module.mapInstruction(type);
465
466 return type->getResultId();
467 }
468
469 Id Builder::makeFunctionType(Id returnType, const std::vector<Id>& paramTypes)
470 {
471 // try to find it
472 Instruction* type;
473 for (int t = 0; t < (int)groupedTypes[OpTypeFunction].size(); ++t) {
474 type = groupedTypes[OpTypeFunction][t];
475 if (type->getIdOperand(0) != returnType || (int)paramTypes.size() != type->getNumOperands() - 1)
476 continue;
477 bool mismatch = false;
478 for (int p = 0; p < (int)paramTypes.size(); ++p) {
479 if (paramTypes[p] != type->getIdOperand(p + 1)) {
480 mismatch = true;
481 break;
482 }
483 }
484 if (! mismatch)
485 return type->getResultId();
486 }
487
488 // not found, make it
489 type = new Instruction(getUniqueId(), NoType, OpTypeFunction);
490 type->addIdOperand(returnType);
491 for (int p = 0; p < (int)paramTypes.size(); ++p)
492 type->addIdOperand(paramTypes[p]);
493 groupedTypes[OpTypeFunction].push_back(type);
494 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
495 module.mapInstruction(type);
496
497 return type->getResultId();
498 }
499
500 Id Builder::makeImageType(Id sampledType, Dim dim, bool depth, bool arrayed, bool ms, unsigned sampled,
501 ImageFormat format)
502 {
503 assert(sampled == 1 || sampled == 2);
504
505 // try to find it
506 Instruction* type;
507 for (int t = 0; t < (int)groupedTypes[OpTypeImage].size(); ++t) {
508 type = groupedTypes[OpTypeImage][t];
509 if (type->getIdOperand(0) == sampledType &&
510 type->getImmediateOperand(1) == (unsigned int)dim &&
511 type->getImmediateOperand(2) == ( depth ? 1u : 0u) &&
512 type->getImmediateOperand(3) == (arrayed ? 1u : 0u) &&
513 type->getImmediateOperand(4) == ( ms ? 1u : 0u) &&
514 type->getImmediateOperand(5) == sampled &&
515 type->getImmediateOperand(6) == (unsigned int)format)
516 return type->getResultId();
517 }
518
519 // not found, make it
520 type = new Instruction(getUniqueId(), NoType, OpTypeImage);
521 type->addIdOperand(sampledType);
522 type->addImmediateOperand( dim);
523 type->addImmediateOperand( depth ? 1 : 0);
524 type->addImmediateOperand(arrayed ? 1 : 0);
525 type->addImmediateOperand( ms ? 1 : 0);
526 type->addImmediateOperand(sampled);
527 type->addImmediateOperand((unsigned int)format);
528
529 groupedTypes[OpTypeImage].push_back(type);
530 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
531 module.mapInstruction(type);
532
533 #ifndef GLSLANG_WEB
534 // deal with capabilities
535 switch (dim) {
536 case DimBuffer:
537 if (sampled == 1)
538 addCapability(CapabilitySampledBuffer);
539 else
540 addCapability(CapabilityImageBuffer);
541 break;
542 case Dim1D:
543 if (sampled == 1)
544 addCapability(CapabilitySampled1D);
545 else
546 addCapability(CapabilityImage1D);
547 break;
548 case DimCube:
549 if (arrayed) {
550 if (sampled == 1)
551 addCapability(CapabilitySampledCubeArray);
552 else
553 addCapability(CapabilityImageCubeArray);
554 }
555 break;
556 case DimRect:
557 if (sampled == 1)
558 addCapability(CapabilitySampledRect);
559 else
560 addCapability(CapabilityImageRect);
561 break;
562 case DimSubpassData:
563 addCapability(CapabilityInputAttachment);
564 break;
565 default:
566 break;
567 }
568
569 if (ms) {
570 if (sampled == 2) {
571 // Images used with subpass data are not storage
572 // images, so don't require the capability for them.
573 if (dim != Dim::DimSubpassData)
574 addCapability(CapabilityStorageImageMultisample);
575 if (arrayed)
576 addCapability(CapabilityImageMSArray);
577 }
578 }
579 #endif
580
581 return type->getResultId();
582 }
583
584 Id Builder::makeSampledImageType(Id imageType)
585 {
586 // try to find it
587 Instruction* type;
588 for (int t = 0; t < (int)groupedTypes[OpTypeSampledImage].size(); ++t) {
589 type = groupedTypes[OpTypeSampledImage][t];
590 if (type->getIdOperand(0) == imageType)
591 return type->getResultId();
592 }
593
594 // not found, make it
595 type = new Instruction(getUniqueId(), NoType, OpTypeSampledImage);
596 type->addIdOperand(imageType);
597
598 groupedTypes[OpTypeSampledImage].push_back(type);
599 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
600 module.mapInstruction(type);
601
602 return type->getResultId();
603 }
604
605 #ifndef GLSLANG_WEB
606 Id Builder::makeAccelerationStructureType()
607 {
608 Instruction *type;
609 if (groupedTypes[OpTypeAccelerationStructureKHR].size() == 0) {
610 type = new Instruction(getUniqueId(), NoType, OpTypeAccelerationStructureKHR);
611 groupedTypes[OpTypeAccelerationStructureKHR].push_back(type);
612 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
613 module.mapInstruction(type);
614 } else {
615 type = groupedTypes[OpTypeAccelerationStructureKHR].back();
616 }
617
618 return type->getResultId();
619 }
620
621 Id Builder::makeRayQueryType()
622 {
623 Instruction *type;
624 if (groupedTypes[OpTypeRayQueryProvisionalKHR].size() == 0) {
625 type = new Instruction(getUniqueId(), NoType, OpTypeRayQueryProvisionalKHR);
626 groupedTypes[OpTypeRayQueryProvisionalKHR].push_back(type);
627 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
628 module.mapInstruction(type);
629 } else {
630 type = groupedTypes[OpTypeRayQueryProvisionalKHR].back();
631 }
632
633 return type->getResultId();
634 }
635 #endif
636
637 Id Builder::getDerefTypeId(Id resultId) const
638 {
639 Id typeId = getTypeId(resultId);
640 assert(isPointerType(typeId));
641
642 return module.getInstruction(typeId)->getIdOperand(1);
643 }
644
645 Op Builder::getMostBasicTypeClass(Id typeId) const
646 {
647 Instruction* instr = module.getInstruction(typeId);
648
649 Op typeClass = instr->getOpCode();
650 switch (typeClass)
651 {
652 case OpTypeVector:
653 case OpTypeMatrix:
654 case OpTypeArray:
655 case OpTypeRuntimeArray:
656 return getMostBasicTypeClass(instr->getIdOperand(0));
657 case OpTypePointer:
658 return getMostBasicTypeClass(instr->getIdOperand(1));
659 default:
660 return typeClass;
661 }
662 }
663
664 int Builder::getNumTypeConstituents(Id typeId) const
665 {
666 Instruction* instr = module.getInstruction(typeId);
667
668 switch (instr->getOpCode())
669 {
670 case OpTypeBool:
671 case OpTypeInt:
672 case OpTypeFloat:
673 case OpTypePointer:
674 return 1;
675 case OpTypeVector:
676 case OpTypeMatrix:
677 return instr->getImmediateOperand(1);
678 case OpTypeArray:
679 {
680 Id lengthId = instr->getIdOperand(1);
681 return module.getInstruction(lengthId)->getImmediateOperand(0);
682 }
683 case OpTypeStruct:
684 return instr->getNumOperands();
685 case OpTypeCooperativeMatrixNV:
686 // has only one constituent when used with OpCompositeConstruct.
687 return 1;
688 default:
689 assert(0);
690 return 1;
691 }
692 }
693
694 // Return the lowest-level type of scalar that an homogeneous composite is made out of.
695 // Typically, this is just to find out if something is made out of ints or floats.
696 // However, it includes returning a structure, if say, it is an array of structure.
697 Id Builder::getScalarTypeId(Id typeId) const
698 {
699 Instruction* instr = module.getInstruction(typeId);
700
701 Op typeClass = instr->getOpCode();
702 switch (typeClass)
703 {
704 case OpTypeVoid:
705 case OpTypeBool:
706 case OpTypeInt:
707 case OpTypeFloat:
708 case OpTypeStruct:
709 return instr->getResultId();
710 case OpTypeVector:
711 case OpTypeMatrix:
712 case OpTypeArray:
713 case OpTypeRuntimeArray:
714 case OpTypePointer:
715 return getScalarTypeId(getContainedTypeId(typeId));
716 default:
717 assert(0);
718 return NoResult;
719 }
720 }
721
722 // Return the type of 'member' of a composite.
723 Id Builder::getContainedTypeId(Id typeId, int member) const
724 {
725 Instruction* instr = module.getInstruction(typeId);
726
727 Op typeClass = instr->getOpCode();
728 switch (typeClass)
729 {
730 case OpTypeVector:
731 case OpTypeMatrix:
732 case OpTypeArray:
733 case OpTypeRuntimeArray:
734 case OpTypeCooperativeMatrixNV:
735 return instr->getIdOperand(0);
736 case OpTypePointer:
737 return instr->getIdOperand(1);
738 case OpTypeStruct:
739 return instr->getIdOperand(member);
740 default:
741 assert(0);
742 return NoResult;
743 }
744 }
745
746 // Return the immediately contained type of a given composite type.
747 Id Builder::getContainedTypeId(Id typeId) const
748 {
749 return getContainedTypeId(typeId, 0);
750 }
751
752 // Returns true if 'typeId' is or contains a scalar type declared with 'typeOp'
753 // of width 'width'. The 'width' is only consumed for int and float types.
754 // Returns false otherwise.
755 bool Builder::containsType(Id typeId, spv::Op typeOp, unsigned int width) const
756 {
757 const Instruction& instr = *module.getInstruction(typeId);
758
759 Op typeClass = instr.getOpCode();
760 switch (typeClass)
761 {
762 case OpTypeInt:
763 case OpTypeFloat:
764 return typeClass == typeOp && instr.getImmediateOperand(0) == width;
765 case OpTypeStruct:
766 for (int m = 0; m < instr.getNumOperands(); ++m) {
767 if (containsType(instr.getIdOperand(m), typeOp, width))
768 return true;
769 }
770 return false;
771 case OpTypePointer:
772 return false;
773 case OpTypeVector:
774 case OpTypeMatrix:
775 case OpTypeArray:
776 case OpTypeRuntimeArray:
777 return containsType(getContainedTypeId(typeId), typeOp, width);
778 default:
779 return typeClass == typeOp;
780 }
781 }
782
783 // return true if the type is a pointer to PhysicalStorageBufferEXT or an
784 // array of such pointers. These require restrict/aliased decorations.
785 bool Builder::containsPhysicalStorageBufferOrArray(Id typeId) const
786 {
787 const Instruction& instr = *module.getInstruction(typeId);
788
789 Op typeClass = instr.getOpCode();
790 switch (typeClass)
791 {
792 case OpTypePointer:
793 return getTypeStorageClass(typeId) == StorageClassPhysicalStorageBufferEXT;
794 case OpTypeArray:
795 return containsPhysicalStorageBufferOrArray(getContainedTypeId(typeId));
796 default:
797 return false;
798 }
799 }
800
801 // See if a scalar constant of this type has already been created, so it
802 // can be reused rather than duplicated. (Required by the specification).
803 Id Builder::findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned value)
804 {
805 Instruction* constant;
806 for (int i = 0; i < (int)groupedConstants[typeClass].size(); ++i) {
807 constant = groupedConstants[typeClass][i];
808 if (constant->getOpCode() == opcode &&
809 constant->getTypeId() == typeId &&
810 constant->getImmediateOperand(0) == value)
811 return constant->getResultId();
812 }
813
814 return 0;
815 }
816
817 // Version of findScalarConstant (see above) for scalars that take two operands (e.g. a 'double' or 'int64').
818 Id Builder::findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned v1, unsigned v2)
819 {
820 Instruction* constant;
821 for (int i = 0; i < (int)groupedConstants[typeClass].size(); ++i) {
822 constant = groupedConstants[typeClass][i];
823 if (constant->getOpCode() == opcode &&
824 constant->getTypeId() == typeId &&
825 constant->getImmediateOperand(0) == v1 &&
826 constant->getImmediateOperand(1) == v2)
827 return constant->getResultId();
828 }
829
830 return 0;
831 }
832
833 // Return true if consuming 'opcode' means consuming a constant.
834 // "constant" here means after final transform to executable code,
835 // the value consumed will be a constant, so includes specialization.
836 bool Builder::isConstantOpCode(Op opcode) const
837 {
838 switch (opcode) {
839 case OpUndef:
840 case OpConstantTrue:
841 case OpConstantFalse:
842 case OpConstant:
843 case OpConstantComposite:
844 case OpConstantSampler:
845 case OpConstantNull:
846 case OpSpecConstantTrue:
847 case OpSpecConstantFalse:
848 case OpSpecConstant:
849 case OpSpecConstantComposite:
850 case OpSpecConstantOp:
851 return true;
852 default:
853 return false;
854 }
855 }
856
857 // Return true if consuming 'opcode' means consuming a specialization constant.
858 bool Builder::isSpecConstantOpCode(Op opcode) const
859 {
860 switch (opcode) {
861 case OpSpecConstantTrue:
862 case OpSpecConstantFalse:
863 case OpSpecConstant:
864 case OpSpecConstantComposite:
865 case OpSpecConstantOp:
866 return true;
867 default:
868 return false;
869 }
870 }
871
872 Id Builder::makeBoolConstant(bool b, bool specConstant)
873 {
874 Id typeId = makeBoolType();
875 Instruction* constant;
876 Op opcode = specConstant ? (b ? OpSpecConstantTrue : OpSpecConstantFalse) : (b ? OpConstantTrue : OpConstantFalse);
877
878 // See if we already made it. Applies only to regular constants, because specialization constants
879 // must remain distinct for the purpose of applying a SpecId decoration.
880 if (! specConstant) {
881 Id existing = 0;
882 for (int i = 0; i < (int)groupedConstants[OpTypeBool].size(); ++i) {
883 constant = groupedConstants[OpTypeBool][i];
884 if (constant->getTypeId() == typeId && constant->getOpCode() == opcode)
885 existing = constant->getResultId();
886 }
887
888 if (existing)
889 return existing;
890 }
891
892 // Make it
893 Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
894 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
895 groupedConstants[OpTypeBool].push_back(c);
896 module.mapInstruction(c);
897
898 return c->getResultId();
899 }
900
901 Id Builder::makeIntConstant(Id typeId, unsigned value, bool specConstant)
902 {
903 Op opcode = specConstant ? OpSpecConstant : OpConstant;
904
905 // See if we already made it. Applies only to regular constants, because specialization constants
906 // must remain distinct for the purpose of applying a SpecId decoration.
907 if (! specConstant) {
908 Id existing = findScalarConstant(OpTypeInt, opcode, typeId, value);
909 if (existing)
910 return existing;
911 }
912
913 Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
914 c->addImmediateOperand(value);
915 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
916 groupedConstants[OpTypeInt].push_back(c);
917 module.mapInstruction(c);
918
919 return c->getResultId();
920 }
921
922 Id Builder::makeInt64Constant(Id typeId, unsigned long long value, bool specConstant)
923 {
924 Op opcode = specConstant ? OpSpecConstant : OpConstant;
925
926 unsigned op1 = value & 0xFFFFFFFF;
927 unsigned op2 = value >> 32;
928
929 // See if we already made it. Applies only to regular constants, because specialization constants
930 // must remain distinct for the purpose of applying a SpecId decoration.
931 if (! specConstant) {
932 Id existing = findScalarConstant(OpTypeInt, opcode, typeId, op1, op2);
933 if (existing)
934 return existing;
935 }
936
937 Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
938 c->addImmediateOperand(op1);
939 c->addImmediateOperand(op2);
940 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
941 groupedConstants[OpTypeInt].push_back(c);
942 module.mapInstruction(c);
943
944 return c->getResultId();
945 }
946
947 Id Builder::makeFloatConstant(float f, bool specConstant)
948 {
949 Op opcode = specConstant ? OpSpecConstant : OpConstant;
950 Id typeId = makeFloatType(32);
951 union { float fl; unsigned int ui; } u;
952 u.fl = f;
953 unsigned value = u.ui;
954
955 // See if we already made it. Applies only to regular constants, because specialization constants
956 // must remain distinct for the purpose of applying a SpecId decoration.
957 if (! specConstant) {
958 Id existing = findScalarConstant(OpTypeFloat, opcode, typeId, value);
959 if (existing)
960 return existing;
961 }
962
963 Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
964 c->addImmediateOperand(value);
965 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
966 groupedConstants[OpTypeFloat].push_back(c);
967 module.mapInstruction(c);
968
969 return c->getResultId();
970 }
971
972 Id Builder::makeDoubleConstant(double d, bool specConstant)
973 {
974 #ifdef GLSLANG_WEB
975 assert(0);
976 return NoResult;
977 #else
978 Op opcode = specConstant ? OpSpecConstant : OpConstant;
979 Id typeId = makeFloatType(64);
980 union { double db; unsigned long long ull; } u;
981 u.db = d;
982 unsigned long long value = u.ull;
983 unsigned op1 = value & 0xFFFFFFFF;
984 unsigned op2 = value >> 32;
985
986 // See if we already made it. Applies only to regular constants, because specialization constants
987 // must remain distinct for the purpose of applying a SpecId decoration.
988 if (! specConstant) {
989 Id existing = findScalarConstant(OpTypeFloat, opcode, typeId, op1, op2);
990 if (existing)
991 return existing;
992 }
993
994 Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
995 c->addImmediateOperand(op1);
996 c->addImmediateOperand(op2);
997 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
998 groupedConstants[OpTypeFloat].push_back(c);
999 module.mapInstruction(c);
1000
1001 return c->getResultId();
1002 #endif
1003 }
1004
1005 Id Builder::makeFloat16Constant(float f16, bool specConstant)
1006 {
1007 #ifdef GLSLANG_WEB
1008 assert(0);
1009 return NoResult;
1010 #else
1011 Op opcode = specConstant ? OpSpecConstant : OpConstant;
1012 Id typeId = makeFloatType(16);
1013
1014 spvutils::HexFloat<spvutils::FloatProxy<float>> fVal(f16);
1015 spvutils::HexFloat<spvutils::FloatProxy<spvutils::Float16>> f16Val(0);
1016 fVal.castTo(f16Val, spvutils::kRoundToZero);
1017
1018 unsigned value = f16Val.value().getAsFloat().get_value();
1019
1020 // See if we already made it. Applies only to regular constants, because specialization constants
1021 // must remain distinct for the purpose of applying a SpecId decoration.
1022 if (!specConstant) {
1023 Id existing = findScalarConstant(OpTypeFloat, opcode, typeId, value);
1024 if (existing)
1025 return existing;
1026 }
1027
1028 Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
1029 c->addImmediateOperand(value);
1030 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
1031 groupedConstants[OpTypeFloat].push_back(c);
1032 module.mapInstruction(c);
1033
1034 return c->getResultId();
1035 #endif
1036 }
1037
1038 Id Builder::makeFpConstant(Id type, double d, bool specConstant)
1039 {
1040 #ifdef GLSLANG_WEB
1041 const int width = 32;
1042 assert(width == getScalarTypeWidth(type));
1043 #else
1044 const int width = getScalarTypeWidth(type);
1045 #endif
1046
1047 assert(isFloatType(type));
1048
1049 switch (width) {
1050 case 16:
1051 return makeFloat16Constant((float)d, specConstant);
1052 case 32:
1053 return makeFloatConstant((float)d, specConstant);
1054 case 64:
1055 return makeDoubleConstant(d, specConstant);
1056 default:
1057 break;
1058 }
1059
1060 assert(false);
1061 return NoResult;
1062 }
1063
1064 Id Builder::findCompositeConstant(Op typeClass, Id typeId, const std::vector<Id>& comps)
1065 {
1066 Instruction* constant = 0;
1067 bool found = false;
1068 for (int i = 0; i < (int)groupedConstants[typeClass].size(); ++i) {
1069 constant = groupedConstants[typeClass][i];
1070
1071 if (constant->getTypeId() != typeId)
1072 continue;
1073
1074 // same contents?
1075 bool mismatch = false;
1076 for (int op = 0; op < constant->getNumOperands(); ++op) {
1077 if (constant->getIdOperand(op) != comps[op]) {
1078 mismatch = true;
1079 break;
1080 }
1081 }
1082 if (! mismatch) {
1083 found = true;
1084 break;
1085 }
1086 }
1087
1088 return found ? constant->getResultId() : NoResult;
1089 }
1090
1091 Id Builder::findStructConstant(Id typeId, const std::vector<Id>& comps)
1092 {
1093 Instruction* constant = 0;
1094 bool found = false;
1095 for (int i = 0; i < (int)groupedStructConstants[typeId].size(); ++i) {
1096 constant = groupedStructConstants[typeId][i];
1097
1098 // same contents?
1099 bool mismatch = false;
1100 for (int op = 0; op < constant->getNumOperands(); ++op) {
1101 if (constant->getIdOperand(op) != comps[op]) {
1102 mismatch = true;
1103 break;
1104 }
1105 }
1106 if (! mismatch) {
1107 found = true;
1108 break;
1109 }
1110 }
1111
1112 return found ? constant->getResultId() : NoResult;
1113 }
1114
1115 // Comments in header
1116 Id Builder::makeCompositeConstant(Id typeId, const std::vector<Id>& members, bool specConstant)
1117 {
1118 Op opcode = specConstant ? OpSpecConstantComposite : OpConstantComposite;
1119 assert(typeId);
1120 Op typeClass = getTypeClass(typeId);
1121
1122 switch (typeClass) {
1123 case OpTypeVector:
1124 case OpTypeArray:
1125 case OpTypeMatrix:
1126 case OpTypeCooperativeMatrixNV:
1127 if (! specConstant) {
1128 Id existing = findCompositeConstant(typeClass, typeId, members);
1129 if (existing)
1130 return existing;
1131 }
1132 break;
1133 case OpTypeStruct:
1134 if (! specConstant) {
1135 Id existing = findStructConstant(typeId, members);
1136 if (existing)
1137 return existing;
1138 }
1139 break;
1140 default:
1141 assert(0);
1142 return makeFloatConstant(0.0);
1143 }
1144
1145 Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
1146 for (int op = 0; op < (int)members.size(); ++op)
1147 c->addIdOperand(members[op]);
1148 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
1149 if (typeClass == OpTypeStruct)
1150 groupedStructConstants[typeId].push_back(c);
1151 else
1152 groupedConstants[typeClass].push_back(c);
1153 module.mapInstruction(c);
1154
1155 return c->getResultId();
1156 }
1157
1158 Instruction* Builder::addEntryPoint(ExecutionModel model, Function* function, const char* name)
1159 {
1160 Instruction* entryPoint = new Instruction(OpEntryPoint);
1161 entryPoint->addImmediateOperand(model);
1162 entryPoint->addIdOperand(function->getId());
1163 entryPoint->addStringOperand(name);
1164
1165 entryPoints.push_back(std::unique_ptr<Instruction>(entryPoint));
1166
1167 return entryPoint;
1168 }
1169
1170 // Currently relying on the fact that all 'value' of interest are small non-negative values.
1171 void Builder::addExecutionMode(Function* entryPoint, ExecutionMode mode, int value1, int value2, int value3)
1172 {
1173 Instruction* instr = new Instruction(OpExecutionMode);
1174 instr->addIdOperand(entryPoint->getId());
1175 instr->addImmediateOperand(mode);
1176 if (value1 >= 0)
1177 instr->addImmediateOperand(value1);
1178 if (value2 >= 0)
1179 instr->addImmediateOperand(value2);
1180 if (value3 >= 0)
1181 instr->addImmediateOperand(value3);
1182
1183 executionModes.push_back(std::unique_ptr<Instruction>(instr));
1184 }
1185
1186 void Builder::addExecutionMode(Function* entryPoint, ExecutionMode mode, const std::vector<unsigned>& literals)
1187 {
1188 Instruction* instr = new Instruction(OpExecutionMode);
1189 instr->addIdOperand(entryPoint->getId());
1190 instr->addImmediateOperand(mode);
1191 for (auto literal : literals)
1192 instr->addImmediateOperand(literal);
1193
1194 executionModes.push_back(std::unique_ptr<Instruction>(instr));
1195 }
1196
1197 void Builder::addExecutionModeId(Function* entryPoint, ExecutionMode mode, const std::vector<Id>& operandIds)
1198 {
1199 Instruction* instr = new Instruction(OpExecutionModeId);
1200 instr->addIdOperand(entryPoint->getId());
1201 instr->addImmediateOperand(mode);
1202 for (auto operandId : operandIds)
1203 instr->addIdOperand(operandId);
1204
1205 executionModes.push_back(std::unique_ptr<Instruction>(instr));
1206 }
1207
1208 void Builder::addName(Id id, const char* string)
1209 {
1210 Instruction* name = new Instruction(OpName);
1211 name->addIdOperand(id);
1212 name->addStringOperand(string);
1213
1214 names.push_back(std::unique_ptr<Instruction>(name));
1215 }
1216
1217 void Builder::addMemberName(Id id, int memberNumber, const char* string)
1218 {
1219 Instruction* name = new Instruction(OpMemberName);
1220 name->addIdOperand(id);
1221 name->addImmediateOperand(memberNumber);
1222 name->addStringOperand(string);
1223
1224 names.push_back(std::unique_ptr<Instruction>(name));
1225 }
1226
1227 void Builder::addDecoration(Id id, Decoration decoration, int num)
1228 {
1229 if (decoration == spv::DecorationMax)
1230 return;
1231
1232 Instruction* dec = new Instruction(OpDecorate);
1233 dec->addIdOperand(id);
1234 dec->addImmediateOperand(decoration);
1235 if (num >= 0)
1236 dec->addImmediateOperand(num);
1237
1238 decorations.push_back(std::unique_ptr<Instruction>(dec));
1239 }
1240
1241 void Builder::addDecoration(Id id, Decoration decoration, const char* s)
1242 {
1243 if (decoration == spv::DecorationMax)
1244 return;
1245
1246 Instruction* dec = new Instruction(OpDecorateString);
1247 dec->addIdOperand(id);
1248 dec->addImmediateOperand(decoration);
1249 dec->addStringOperand(s);
1250
1251 decorations.push_back(std::unique_ptr<Instruction>(dec));
1252 }
1253
1254 void Builder::addDecoration(Id id, Decoration decoration, const std::vector<unsigned>& literals)
1255 {
1256 if (decoration == spv::DecorationMax)
1257 return;
1258
1259 Instruction* dec = new Instruction(OpDecorate);
1260 dec->addIdOperand(id);
1261 dec->addImmediateOperand(decoration);
1262 for (auto literal : literals)
1263 dec->addImmediateOperand(literal);
1264
1265 decorations.push_back(std::unique_ptr<Instruction>(dec));
1266 }
1267
1268 void Builder::addDecoration(Id id, Decoration decoration, const std::vector<const char*>& strings)
1269 {
1270 if (decoration == spv::DecorationMax)
1271 return;
1272
1273 Instruction* dec = new Instruction(OpDecorateString);
1274 dec->addIdOperand(id);
1275 dec->addImmediateOperand(decoration);
1276 for (auto string : strings)
1277 dec->addStringOperand(string);
1278
1279 decorations.push_back(std::unique_ptr<Instruction>(dec));
1280 }
1281
1282 void Builder::addDecorationId(Id id, Decoration decoration, Id idDecoration)
1283 {
1284 if (decoration == spv::DecorationMax)
1285 return;
1286
1287 Instruction* dec = new Instruction(OpDecorateId);
1288 dec->addIdOperand(id);
1289 dec->addImmediateOperand(decoration);
1290 dec->addIdOperand(idDecoration);
1291
1292 decorations.push_back(std::unique_ptr<Instruction>(dec));
1293 }
1294
1295 void Builder::addDecorationId(Id id, Decoration decoration, const std::vector<Id>& operandIds)
1296 {
1297 if(decoration == spv::DecorationMax)
1298 return;
1299
1300 Instruction* dec = new Instruction(OpDecorateId);
1301 dec->addIdOperand(id);
1302 dec->addImmediateOperand(decoration);
1303
1304 for (auto operandId : operandIds)
1305 dec->addIdOperand(operandId);
1306
1307 decorations.push_back(std::unique_ptr<Instruction>(dec));
1308 }
1309
1310 void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, int num)
1311 {
1312 if (decoration == spv::DecorationMax)
1313 return;
1314
1315 Instruction* dec = new Instruction(OpMemberDecorate);
1316 dec->addIdOperand(id);
1317 dec->addImmediateOperand(member);
1318 dec->addImmediateOperand(decoration);
1319 if (num >= 0)
1320 dec->addImmediateOperand(num);
1321
1322 decorations.push_back(std::unique_ptr<Instruction>(dec));
1323 }
1324
1325 void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, const char *s)
1326 {
1327 if (decoration == spv::DecorationMax)
1328 return;
1329
1330 Instruction* dec = new Instruction(OpMemberDecorateStringGOOGLE);
1331 dec->addIdOperand(id);
1332 dec->addImmediateOperand(member);
1333 dec->addImmediateOperand(decoration);
1334 dec->addStringOperand(s);
1335
1336 decorations.push_back(std::unique_ptr<Instruction>(dec));
1337 }
1338
1339 void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, const std::vector<unsigned>& literals)
1340 {
1341 if (decoration == spv::DecorationMax)
1342 return;
1343
1344 Instruction* dec = new Instruction(OpMemberDecorate);
1345 dec->addIdOperand(id);
1346 dec->addImmediateOperand(member);
1347 dec->addImmediateOperand(decoration);
1348 for (auto literal : literals)
1349 dec->addImmediateOperand(literal);
1350
1351 decorations.push_back(std::unique_ptr<Instruction>(dec));
1352 }
1353
1354 void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, const std::vector<const char*>& strings)
1355 {
1356 if (decoration == spv::DecorationMax)
1357 return;
1358
1359 Instruction* dec = new Instruction(OpMemberDecorateString);
1360 dec->addIdOperand(id);
1361 dec->addImmediateOperand(member);
1362 dec->addImmediateOperand(decoration);
1363 for (auto string : strings)
1364 dec->addStringOperand(string);
1365
1366 decorations.push_back(std::unique_ptr<Instruction>(dec));
1367 }
1368
1369 // Comments in header
1370 Function* Builder::makeEntryPoint(const char* entryPoint)
1371 {
1372 assert(! entryPointFunction);
1373
1374 Block* entry;
1375 std::vector<Id> params;
1376 std::vector<std::vector<Decoration>> decorations;
1377
1378 entryPointFunction = makeFunctionEntry(NoPrecision, makeVoidType(), entryPoint, params, decorations, &entry);
1379
1380 return entryPointFunction;
1381 }
1382
1383 // Comments in header
1384 Function* Builder::makeFunctionEntry(Decoration precision, Id returnType, const char* name,
1385 const std::vector<Id>& paramTypes,
1386 const std::vector<std::vector<Decoration>>& decorations, Block **entry)
1387 {
1388 // Make the function and initial instructions in it
1389 Id typeId = makeFunctionType(returnType, paramTypes);
1390 Id firstParamId = paramTypes.size() == 0 ? 0 : getUniqueIds((int)paramTypes.size());
1391 Function* function = new Function(getUniqueId(), returnType, typeId, firstParamId, module);
1392
1393 // Set up the precisions
1394 setPrecision(function->getId(), precision);
1395 function->setReturnPrecision(precision);
1396 for (unsigned p = 0; p < (unsigned)decorations.size(); ++p) {
1397 for (int d = 0; d < (int)decorations[p].size(); ++d) {
1398 addDecoration(firstParamId + p, decorations[p][d]);
1399 function->addParamPrecision(p, decorations[p][d]);
1400 }
1401 }
1402
1403 // CFG
1404 if (entry) {
1405 *entry = new Block(getUniqueId(), *function);
1406 function->addBlock(*entry);
1407 setBuildPoint(*entry);
1408 }
1409
1410 if (name)
1411 addName(function->getId(), name);
1412
1413 functions.push_back(std::unique_ptr<Function>(function));
1414
1415 return function;
1416 }
1417
1418 // Comments in header
1419 void Builder::makeReturn(bool implicit, Id retVal)
1420 {
1421 if (retVal) {
1422 Instruction* inst = new Instruction(NoResult, NoType, OpReturnValue);
1423 inst->addIdOperand(retVal);
1424 buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));
1425 } else
1426 buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(NoResult, NoType, OpReturn)));
1427
1428 if (! implicit)
1429 createAndSetNoPredecessorBlock("post-return");
1430 }
1431
1432 // Comments in header
1433 void Builder::leaveFunction()
1434 {
1435 Block* block = buildPoint;
1436 Function& function = buildPoint->getParent();
1437 assert(block);
1438
1439 // If our function did not contain a return, add a return void now.
1440 if (! block->isTerminated()) {
1441 if (function.getReturnType() == makeVoidType())
1442 makeReturn(true);
1443 else {
1444 makeReturn(true, createUndefined(function.getReturnType()));
1445 }
1446 }
1447 }
1448
1449 // Comments in header
1450 void Builder::makeDiscard()
1451 {
1452 buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(OpKill)));
1453 createAndSetNoPredecessorBlock("post-discard");
1454 }
1455
1456 // Comments in header
1457 void Builder::makeTerminateInvocation()
1458 {
1459 buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(OpTerminateInvocation)));
1460 createAndSetNoPredecessorBlock("post-terminate-invocation");
1461 }
1462
1463 // Comments in header
1464 Id Builder::createVariable(Decoration precision, StorageClass storageClass, Id type, const char* name, Id initializer)
1465 {
1466 Id pointerType = makePointer(storageClass, type);
1467 Instruction* inst = new Instruction(getUniqueId(), pointerType, OpVariable);
1468 inst->addImmediateOperand(storageClass);
1469 if (initializer != NoResult)
1470 inst->addIdOperand(initializer);
1471
1472 switch (storageClass) {
1473 case StorageClassFunction:
1474 // Validation rules require the declaration in the entry block
1475 buildPoint->getParent().addLocalVariable(std::unique_ptr<Instruction>(inst));
1476 break;
1477
1478 default:
1479 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(inst));
1480 module.mapInstruction(inst);
1481 break;
1482 }
1483
1484 if (name)
1485 addName(inst->getResultId(), name);
1486 setPrecision(inst->getResultId(), precision);
1487
1488 return inst->getResultId();
1489 }
1490
1491 // Comments in header
1492 Id Builder::createUndefined(Id type)
1493 {
1494 Instruction* inst = new Instruction(getUniqueId(), type, OpUndef);
1495 buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));
1496 return inst->getResultId();
1497 }
1498
1499 // av/vis/nonprivate are unnecessary and illegal for some storage classes.
1500 spv::MemoryAccessMask Builder::sanitizeMemoryAccessForStorageClass(spv::MemoryAccessMask memoryAccess, StorageClass sc)
1501 const
1502 {
1503 switch (sc) {
1504 case spv::StorageClassUniform:
1505 case spv::StorageClassWorkgroup:
1506 case spv::StorageClassStorageBuffer:
1507 case spv::StorageClassPhysicalStorageBufferEXT:
1508 break;
1509 default:
1510 memoryAccess = spv::MemoryAccessMask(memoryAccess &
1511 ~(spv::MemoryAccessMakePointerAvailableKHRMask |
1512 spv::MemoryAccessMakePointerVisibleKHRMask |
1513 spv::MemoryAccessNonPrivatePointerKHRMask));
1514 break;
1515 }
1516 return memoryAccess;
1517 }
1518
1519 // Comments in header
1520 void Builder::createStore(Id rValue, Id lValue, spv::MemoryAccessMask memoryAccess, spv::Scope scope,
1521 unsigned int alignment)
1522 {
1523 Instruction* store = new Instruction(OpStore);
1524 store->addIdOperand(lValue);
1525 store->addIdOperand(rValue);
1526
1527 memoryAccess = sanitizeMemoryAccessForStorageClass(memoryAccess, getStorageClass(lValue));
1528
1529 if (memoryAccess != MemoryAccessMaskNone) {
1530 store->addImmediateOperand(memoryAccess);
1531 if (memoryAccess & spv::MemoryAccessAlignedMask) {
1532 store->addImmediateOperand(alignment);
1533 }
1534 if (memoryAccess & spv::MemoryAccessMakePointerAvailableKHRMask) {
1535 store->addIdOperand(makeUintConstant(scope));
1536 }
1537 }
1538
1539 buildPoint->addInstruction(std::unique_ptr<Instruction>(store));
1540 }
1541
1542 // Comments in header
1543 Id Builder::createLoad(Id lValue, spv::Decoration precision, spv::MemoryAccessMask memoryAccess,
1544 spv::Scope scope, unsigned int alignment)
1545 {
1546 Instruction* load = new Instruction(getUniqueId(), getDerefTypeId(lValue), OpLoad);
1547 load->addIdOperand(lValue);
1548
1549 memoryAccess = sanitizeMemoryAccessForStorageClass(memoryAccess, getStorageClass(lValue));
1550
1551 if (memoryAccess != MemoryAccessMaskNone) {
1552 load->addImmediateOperand(memoryAccess);
1553 if (memoryAccess & spv::MemoryAccessAlignedMask) {
1554 load->addImmediateOperand(alignment);
1555 }
1556 if (memoryAccess & spv::MemoryAccessMakePointerVisibleKHRMask) {
1557 load->addIdOperand(makeUintConstant(scope));
1558 }
1559 }
1560
1561 buildPoint->addInstruction(std::unique_ptr<Instruction>(load));
1562 setPrecision(load->getResultId(), precision);
1563
1564 return load->getResultId();
1565 }
1566
1567 // Comments in header
1568 Id Builder::createAccessChain(StorageClass storageClass, Id base, const std::vector<Id>& offsets)
1569 {
1570 // Figure out the final resulting type.
1571 spv::Id typeId = getTypeId(base);
1572 assert(isPointerType(typeId) && offsets.size() > 0);
1573 typeId = getContainedTypeId(typeId);
1574 for (int i = 0; i < (int)offsets.size(); ++i) {
1575 if (isStructType(typeId)) {
1576 assert(isConstantScalar(offsets[i]));
1577 typeId = getContainedTypeId(typeId, getConstantScalar(offsets[i]));
1578 } else
1579 typeId = getContainedTypeId(typeId, offsets[i]);
1580 }
1581 typeId = makePointer(storageClass, typeId);
1582
1583 // Make the instruction
1584 Instruction* chain = new Instruction(getUniqueId(), typeId, OpAccessChain);
1585 chain->addIdOperand(base);
1586 for (int i = 0; i < (int)offsets.size(); ++i)
1587 chain->addIdOperand(offsets[i]);
1588 buildPoint->addInstruction(std::unique_ptr<Instruction>(chain));
1589
1590 return chain->getResultId();
1591 }
1592
1593 Id Builder::createArrayLength(Id base, unsigned int member)
1594 {
1595 spv::Id intType = makeUintType(32);
1596 Instruction* length = new Instruction(getUniqueId(), intType, OpArrayLength);
1597 length->addIdOperand(base);
1598 length->addImmediateOperand(member);
1599 buildPoint->addInstruction(std::unique_ptr<Instruction>(length));
1600
1601 return length->getResultId();
1602 }
1603
1604 Id Builder::createCooperativeMatrixLength(Id type)
1605 {
1606 spv::Id intType = makeUintType(32);
1607
1608 // Generate code for spec constants if in spec constant operation
1609 // generation mode.
1610 if (generatingOpCodeForSpecConst) {
1611 return createSpecConstantOp(OpCooperativeMatrixLengthNV, intType, std::vector<Id>(1, type), std::vector<Id>());
1612 }
1613
1614 Instruction* length = new Instruction(getUniqueId(), intType, OpCooperativeMatrixLengthNV);
1615 length->addIdOperand(type);
1616 buildPoint->addInstruction(std::unique_ptr<Instruction>(length));
1617
1618 return length->getResultId();
1619 }
1620
1621 Id Builder::createCompositeExtract(Id composite, Id typeId, unsigned index)
1622 {
1623 // Generate code for spec constants if in spec constant operation
1624 // generation mode.
1625 if (generatingOpCodeForSpecConst) {
1626 return createSpecConstantOp(OpCompositeExtract, typeId, std::vector<Id>(1, composite),
1627 std::vector<Id>(1, index));
1628 }
1629 Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract);
1630 extract->addIdOperand(composite);
1631 extract->addImmediateOperand(index);
1632 buildPoint->addInstruction(std::unique_ptr<Instruction>(extract));
1633
1634 return extract->getResultId();
1635 }
1636
1637 Id Builder::createCompositeExtract(Id composite, Id typeId, const std::vector<unsigned>& indexes)
1638 {
1639 // Generate code for spec constants if in spec constant operation
1640 // generation mode.
1641 if (generatingOpCodeForSpecConst) {
1642 return createSpecConstantOp(OpCompositeExtract, typeId, std::vector<Id>(1, composite), indexes);
1643 }
1644 Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract);
1645 extract->addIdOperand(composite);
1646 for (int i = 0; i < (int)indexes.size(); ++i)
1647 extract->addImmediateOperand(indexes[i]);
1648 buildPoint->addInstruction(std::unique_ptr<Instruction>(extract));
1649
1650 return extract->getResultId();
1651 }
1652
1653 Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, unsigned index)
1654 {
1655 Instruction* insert = new Instruction(getUniqueId(), typeId, OpCompositeInsert);
1656 insert->addIdOperand(object);
1657 insert->addIdOperand(composite);
1658 insert->addImmediateOperand(index);
1659 buildPoint->addInstruction(std::unique_ptr<Instruction>(insert));
1660
1661 return insert->getResultId();
1662 }
1663
1664 Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, const std::vector<unsigned>& indexes)
1665 {
1666 Instruction* insert = new Instruction(getUniqueId(), typeId, OpCompositeInsert);
1667 insert->addIdOperand(object);
1668 insert->addIdOperand(composite);
1669 for (int i = 0; i < (int)indexes.size(); ++i)
1670 insert->addImmediateOperand(indexes[i]);
1671 buildPoint->addInstruction(std::unique_ptr<Instruction>(insert));
1672
1673 return insert->getResultId();
1674 }
1675
1676 Id Builder::createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex)
1677 {
1678 Instruction* extract = new Instruction(getUniqueId(), typeId, OpVectorExtractDynamic);
1679 extract->addIdOperand(vector);
1680 extract->addIdOperand(componentIndex);
1681 buildPoint->addInstruction(std::unique_ptr<Instruction>(extract));
1682
1683 return extract->getResultId();
1684 }
1685
1686 Id Builder::createVectorInsertDynamic(Id vector, Id typeId, Id component, Id componentIndex)
1687 {
1688 Instruction* insert = new Instruction(getUniqueId(), typeId, OpVectorInsertDynamic);
1689 insert->addIdOperand(vector);
1690 insert->addIdOperand(component);
1691 insert->addIdOperand(componentIndex);
1692 buildPoint->addInstruction(std::unique_ptr<Instruction>(insert));
1693
1694 return insert->getResultId();
1695 }
1696
1697 // An opcode that has no operands, no result id, and no type
1698 void Builder::createNoResultOp(Op opCode)
1699 {
1700 Instruction* op = new Instruction(opCode);
1701 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1702 }
1703
1704 // An opcode that has one id operand, no result id, and no type
1705 void Builder::createNoResultOp(Op opCode, Id operand)
1706 {
1707 Instruction* op = new Instruction(opCode);
1708 op->addIdOperand(operand);
1709 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1710 }
1711
1712 // An opcode that has one or more operands, no result id, and no type
1713 void Builder::createNoResultOp(Op opCode, const std::vector<Id>& operands)
1714 {
1715 Instruction* op = new Instruction(opCode);
1716 for (auto it = operands.cbegin(); it != operands.cend(); ++it) {
1717 op->addIdOperand(*it);
1718 }
1719 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1720 }
1721
1722 // An opcode that has multiple operands, no result id, and no type
1723 void Builder::createNoResultOp(Op opCode, const std::vector<IdImmediate>& operands)
1724 {
1725 Instruction* op = new Instruction(opCode);
1726 for (auto it = operands.cbegin(); it != operands.cend(); ++it) {
1727 if (it->isId)
1728 op->addIdOperand(it->word);
1729 else
1730 op->addImmediateOperand(it->word);
1731 }
1732 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1733 }
1734
1735 void Builder::createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask semantics)
1736 {
1737 Instruction* op = new Instruction(OpControlBarrier);
1738 op->addIdOperand(makeUintConstant(execution));
1739 op->addIdOperand(makeUintConstant(memory));
1740 op->addIdOperand(makeUintConstant(semantics));
1741 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1742 }
1743
1744 void Builder::createMemoryBarrier(unsigned executionScope, unsigned memorySemantics)
1745 {
1746 Instruction* op = new Instruction(OpMemoryBarrier);
1747 op->addIdOperand(makeUintConstant(executionScope));
1748 op->addIdOperand(makeUintConstant(memorySemantics));
1749 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1750 }
1751
1752 // An opcode that has one operands, a result id, and a type
1753 Id Builder::createUnaryOp(Op opCode, Id typeId, Id operand)
1754 {
1755 // Generate code for spec constants if in spec constant operation
1756 // generation mode.
1757 if (generatingOpCodeForSpecConst) {
1758 return createSpecConstantOp(opCode, typeId, std::vector<Id>(1, operand), std::vector<Id>());
1759 }
1760 Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1761 op->addIdOperand(operand);
1762 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1763
1764 return op->getResultId();
1765 }
1766
1767 Id Builder::createBinOp(Op opCode, Id typeId, Id left, Id right)
1768 {
1769 // Generate code for spec constants if in spec constant operation
1770 // generation mode.
1771 if (generatingOpCodeForSpecConst) {
1772 std::vector<Id> operands(2);
1773 operands[0] = left; operands[1] = right;
1774 return createSpecConstantOp(opCode, typeId, operands, std::vector<Id>());
1775 }
1776 Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1777 op->addIdOperand(left);
1778 op->addIdOperand(right);
1779 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1780
1781 return op->getResultId();
1782 }
1783
1784 Id Builder::createTriOp(Op opCode, Id typeId, Id op1, Id op2, Id op3)
1785 {
1786 // Generate code for spec constants if in spec constant operation
1787 // generation mode.
1788 if (generatingOpCodeForSpecConst) {
1789 std::vector<Id> operands(3);
1790 operands[0] = op1;
1791 operands[1] = op2;
1792 operands[2] = op3;
1793 return createSpecConstantOp(
1794 opCode, typeId, operands, std::vector<Id>());
1795 }
1796 Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1797 op->addIdOperand(op1);
1798 op->addIdOperand(op2);
1799 op->addIdOperand(op3);
1800 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1801
1802 return op->getResultId();
1803 }
1804
1805 Id Builder::createOp(Op opCode, Id typeId, const std::vector<Id>& operands)
1806 {
1807 Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1808 for (auto it = operands.cbegin(); it != operands.cend(); ++it)
1809 op->addIdOperand(*it);
1810 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1811
1812 return op->getResultId();
1813 }
1814
1815 Id Builder::createOp(Op opCode, Id typeId, const std::vector<IdImmediate>& operands)
1816 {
1817 Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1818 for (auto it = operands.cbegin(); it != operands.cend(); ++it) {
1819 if (it->isId)
1820 op->addIdOperand(it->word);
1821 else
1822 op->addImmediateOperand(it->word);
1823 }
1824 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1825
1826 return op->getResultId();
1827 }
1828
1829 Id Builder::createSpecConstantOp(Op opCode, Id typeId, const std::vector<Id>& operands,
1830 const std::vector<unsigned>& literals)
1831 {
1832 Instruction* op = new Instruction(getUniqueId(), typeId, OpSpecConstantOp);
1833 op->addImmediateOperand((unsigned) opCode);
1834 for (auto it = operands.cbegin(); it != operands.cend(); ++it)
1835 op->addIdOperand(*it);
1836 for (auto it = literals.cbegin(); it != literals.cend(); ++it)
1837 op->addImmediateOperand(*it);
1838 module.mapInstruction(op);
1839 constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(op));
1840
1841 return op->getResultId();
1842 }
1843
1844 Id Builder::createFunctionCall(spv::Function* function, const std::vector<spv::Id>& args)
1845 {
1846 Instruction* op = new Instruction(getUniqueId(), function->getReturnType(), OpFunctionCall);
1847 op->addIdOperand(function->getId());
1848 for (int a = 0; a < (int)args.size(); ++a)
1849 op->addIdOperand(args[a]);
1850 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1851
1852 return op->getResultId();
1853 }
1854
1855 // Comments in header
1856 Id Builder::createRvalueSwizzle(Decoration precision, Id typeId, Id source, const std::vector<unsigned>& channels)
1857 {
1858 if (channels.size() == 1)
1859 return setPrecision(createCompositeExtract(source, typeId, channels.front()), precision);
1860
1861 if (generatingOpCodeForSpecConst) {
1862 std::vector<Id> operands(2);
1863 operands[0] = operands[1] = source;
1864 return setPrecision(createSpecConstantOp(OpVectorShuffle, typeId, operands, channels), precision);
1865 }
1866 Instruction* swizzle = new Instruction(getUniqueId(), typeId, OpVectorShuffle);
1867 assert(isVector(source));
1868 swizzle->addIdOperand(source);
1869 swizzle->addIdOperand(source);
1870 for (int i = 0; i < (int)channels.size(); ++i)
1871 swizzle->addImmediateOperand(channels[i]);
1872 buildPoint->addInstruction(std::unique_ptr<Instruction>(swizzle));
1873
1874 return setPrecision(swizzle->getResultId(), precision);
1875 }
1876
1877 // Comments in header
1878 Id Builder::createLvalueSwizzle(Id typeId, Id target, Id source, const std::vector<unsigned>& channels)
1879 {
1880 if (channels.size() == 1 && getNumComponents(source) == 1)
1881 return createCompositeInsert(source, target, typeId, channels.front());
1882
1883 Instruction* swizzle = new Instruction(getUniqueId(), typeId, OpVectorShuffle);
1884
1885 assert(isVector(target));
1886 swizzle->addIdOperand(target);
1887
1888 assert(getNumComponents(source) == (int)channels.size());
1889 assert(isVector(source));
1890 swizzle->addIdOperand(source);
1891
1892 // Set up an identity shuffle from the base value to the result value
1893 unsigned int components[4];
1894 int numTargetComponents = getNumComponents(target);
1895 for (int i = 0; i < numTargetComponents; ++i)
1896 components[i] = i;
1897
1898 // Punch in the l-value swizzle
1899 for (int i = 0; i < (int)channels.size(); ++i)
1900 components[channels[i]] = numTargetComponents + i;
1901
1902 // finish the instruction with these components selectors
1903 for (int i = 0; i < numTargetComponents; ++i)
1904 swizzle->addImmediateOperand(components[i]);
1905 buildPoint->addInstruction(std::unique_ptr<Instruction>(swizzle));
1906
1907 return swizzle->getResultId();
1908 }
1909
1910 // Comments in header
1911 void Builder::promoteScalar(Decoration precision, Id& left, Id& right)
1912 {
1913 int direction = getNumComponents(right) - getNumComponents(left);
1914
1915 if (direction > 0)
1916 left = smearScalar(precision, left, makeVectorType(getTypeId(left), getNumComponents(right)));
1917 else if (direction < 0)
1918 right = smearScalar(precision, right, makeVectorType(getTypeId(right), getNumComponents(left)));
1919
1920 return;
1921 }
1922
1923 // Comments in header
1924 Id Builder::smearScalar(Decoration precision, Id scalar, Id vectorType)
1925 {
1926 assert(getNumComponents(scalar) == 1);
1927 assert(getTypeId(scalar) == getScalarTypeId(vectorType));
1928
1929 int numComponents = getNumTypeComponents(vectorType);
1930 if (numComponents == 1)
1931 return scalar;
1932
1933 Instruction* smear = nullptr;
1934 if (generatingOpCodeForSpecConst) {
1935 auto members = std::vector<spv::Id>(numComponents, scalar);
1936 // Sometime even in spec-constant-op mode, the temporary vector created by
1937 // promoting a scalar might not be a spec constant. This should depend on
1938 // the scalar.
1939 // e.g.:
1940 // const vec2 spec_const_result = a_spec_const_vec2 + a_front_end_const_scalar;
1941 // In such cases, the temporary vector created from a_front_end_const_scalar
1942 // is not a spec constant vector, even though the binary operation node is marked
1943 // as 'specConstant' and we are in spec-constant-op mode.
1944 auto result_id = makeCompositeConstant(vectorType, members, isSpecConstant(scalar));
1945 smear = module.getInstruction(result_id);
1946 } else {
1947 smear = new Instruction(getUniqueId(), vectorType, OpCompositeConstruct);
1948 for (int c = 0; c < numComponents; ++c)
1949 smear->addIdOperand(scalar);
1950 buildPoint->addInstruction(std::unique_ptr<Instruction>(smear));
1951 }
1952
1953 return setPrecision(smear->getResultId(), precision);
1954 }
1955
1956 // Comments in header
1957 Id Builder::createBuiltinCall(Id resultType, Id builtins, int entryPoint, const std::vector<Id>& args)
1958 {
1959 Instruction* inst = new Instruction(getUniqueId(), resultType, OpExtInst);
1960 inst->addIdOperand(builtins);
1961 inst->addImmediateOperand(entryPoint);
1962 for (int arg = 0; arg < (int)args.size(); ++arg)
1963 inst->addIdOperand(args[arg]);
1964
1965 buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));
1966
1967 return inst->getResultId();
1968 }
1969
1970 // Accept all parameters needed to create a texture instruction.
1971 // Create the correct instruction based on the inputs, and make the call.
1972 Id Builder::createTextureCall(Decoration precision, Id resultType, bool sparse, bool fetch, bool proj, bool gather,
1973 bool noImplicitLod, const TextureParameters& parameters, ImageOperandsMask signExtensionMask)
1974 {
1975 static const int maxTextureArgs = 10;
1976 Id texArgs[maxTextureArgs] = {};
1977
1978 //
1979 // Set up the fixed arguments
1980 //
1981 int numArgs = 0;
1982 bool explicitLod = false;
1983 texArgs[numArgs++] = parameters.sampler;
1984 texArgs[numArgs++] = parameters.coords;
1985 if (parameters.Dref != NoResult)
1986 texArgs[numArgs++] = parameters.Dref;
1987 if (parameters.component != NoResult)
1988 texArgs[numArgs++] = parameters.component;
1989
1990 #ifndef GLSLANG_WEB
1991 if (parameters.granularity != NoResult)
1992 texArgs[numArgs++] = parameters.granularity;
1993 if (parameters.coarse != NoResult)
1994 texArgs[numArgs++] = parameters.coarse;
1995 #endif
1996
1997 //
1998 // Set up the optional arguments
1999 //
2000 int optArgNum = numArgs; // track which operand, if it exists, is the mask of optional arguments
2001 ++numArgs; // speculatively make room for the mask operand
2002 ImageOperandsMask mask = ImageOperandsMaskNone; // the mask operand
2003 if (parameters.bias) {
2004 mask = (ImageOperandsMask)(mask | ImageOperandsBiasMask);
2005 texArgs[numArgs++] = parameters.bias;
2006 }
2007 if (parameters.lod) {
2008 mask = (ImageOperandsMask)(mask | ImageOperandsLodMask);
2009 texArgs[numArgs++] = parameters.lod;
2010 explicitLod = true;
2011 } else if (parameters.gradX) {
2012 mask = (ImageOperandsMask)(mask | ImageOperandsGradMask);
2013 texArgs[numArgs++] = parameters.gradX;
2014 texArgs[numArgs++] = parameters.gradY;
2015 explicitLod = true;
2016 } else if (noImplicitLod && ! fetch && ! gather) {
2017 // have to explicitly use lod of 0 if not allowed to have them be implicit, and
2018 // we would otherwise be about to issue an implicit instruction
2019 mask = (ImageOperandsMask)(mask | ImageOperandsLodMask);
2020 texArgs[numArgs++] = makeFloatConstant(0.0);
2021 explicitLod = true;
2022 }
2023 if (parameters.offset) {
2024 if (isConstant(parameters.offset))
2025 mask = (ImageOperandsMask)(mask | ImageOperandsConstOffsetMask);
2026 else {
2027 addCapability(CapabilityImageGatherExtended);
2028 mask = (ImageOperandsMask)(mask | ImageOperandsOffsetMask);
2029 }
2030 texArgs[numArgs++] = parameters.offset;
2031 }
2032 if (parameters.offsets) {
2033 addCapability(CapabilityImageGatherExtended);
2034 mask = (ImageOperandsMask)(mask | ImageOperandsConstOffsetsMask);
2035 texArgs[numArgs++] = parameters.offsets;
2036 }
2037 #ifndef GLSLANG_WEB
2038 if (parameters.sample) {
2039 mask = (ImageOperandsMask)(mask | ImageOperandsSampleMask);
2040 texArgs[numArgs++] = parameters.sample;
2041 }
2042 if (parameters.lodClamp) {
2043 // capability if this bit is used
2044 addCapability(CapabilityMinLod);
2045
2046 mask = (ImageOperandsMask)(mask | ImageOperandsMinLodMask);
2047 texArgs[numArgs++] = parameters.lodClamp;
2048 }
2049 if (parameters.nonprivate) {
2050 mask = mask | ImageOperandsNonPrivateTexelKHRMask;
2051 }
2052 if (parameters.volatil) {
2053 mask = mask | ImageOperandsVolatileTexelKHRMask;
2054 }
2055 #endif
2056 mask = mask | signExtensionMask;
2057 if (mask == ImageOperandsMaskNone)
2058 --numArgs; // undo speculative reservation for the mask argument
2059 else
2060 texArgs[optArgNum] = mask;
2061
2062 //
2063 // Set up the instruction
2064 //
2065 Op opCode = OpNop; // All paths below need to set this
2066 if (fetch) {
2067 if (sparse)
2068 opCode = OpImageSparseFetch;
2069 else
2070 opCode = OpImageFetch;
2071 #ifndef GLSLANG_WEB
2072 } else if (parameters.granularity && parameters.coarse) {
2073 opCode = OpImageSampleFootprintNV;
2074 } else if (gather) {
2075 if (parameters.Dref)
2076 if (sparse)
2077 opCode = OpImageSparseDrefGather;
2078 else
2079 opCode = OpImageDrefGather;
2080 else
2081 if (sparse)
2082 opCode = OpImageSparseGather;
2083 else
2084 opCode = OpImageGather;
2085 #endif
2086 } else if (explicitLod) {
2087 if (parameters.Dref) {
2088 if (proj)
2089 if (sparse)
2090 opCode = OpImageSparseSampleProjDrefExplicitLod;
2091 else
2092 opCode = OpImageSampleProjDrefExplicitLod;
2093 else
2094 if (sparse)
2095 opCode = OpImageSparseSampleDrefExplicitLod;
2096 else
2097 opCode = OpImageSampleDrefExplicitLod;
2098 } else {
2099 if (proj)
2100 if (sparse)
2101 opCode = OpImageSparseSampleProjExplicitLod;
2102 else
2103 opCode = OpImageSampleProjExplicitLod;
2104 else
2105 if (sparse)
2106 opCode = OpImageSparseSampleExplicitLod;
2107 else
2108 opCode = OpImageSampleExplicitLod;
2109 }
2110 } else {
2111 if (parameters.Dref) {
2112 if (proj)
2113 if (sparse)
2114 opCode = OpImageSparseSampleProjDrefImplicitLod;
2115 else
2116 opCode = OpImageSampleProjDrefImplicitLod;
2117 else
2118 if (sparse)
2119 opCode = OpImageSparseSampleDrefImplicitLod;
2120 else
2121 opCode = OpImageSampleDrefImplicitLod;
2122 } else {
2123 if (proj)
2124 if (sparse)
2125 opCode = OpImageSparseSampleProjImplicitLod;
2126 else
2127 opCode = OpImageSampleProjImplicitLod;
2128 else
2129 if (sparse)
2130 opCode = OpImageSparseSampleImplicitLod;
2131 else
2132 opCode = OpImageSampleImplicitLod;
2133 }
2134 }
2135
2136 // See if the result type is expecting a smeared result.
2137 // This happens when a legacy shadow*() call is made, which
2138 // gets a vec4 back instead of a float.
2139 Id smearedType = resultType;
2140 if (! isScalarType(resultType)) {
2141 switch (opCode) {
2142 case OpImageSampleDrefImplicitLod:
2143 case OpImageSampleDrefExplicitLod:
2144 case OpImageSampleProjDrefImplicitLod:
2145 case OpImageSampleProjDrefExplicitLod:
2146 resultType = getScalarTypeId(resultType);
2147 break;
2148 default:
2149 break;
2150 }
2151 }
2152
2153 Id typeId0 = 0;
2154 Id typeId1 = 0;
2155
2156 if (sparse) {
2157 typeId0 = resultType;
2158 typeId1 = getDerefTypeId(parameters.texelOut);
2159 resultType = makeStructResultType(typeId0, typeId1);
2160 }
2161
2162 // Build the SPIR-V instruction
2163 Instruction* textureInst = new Instruction(getUniqueId(), resultType, opCode);
2164 for (int op = 0; op < optArgNum; ++op)
2165 textureInst->addIdOperand(texArgs[op]);
2166 if (optArgNum < numArgs)
2167 textureInst->addImmediateOperand(texArgs[optArgNum]);
2168 for (int op = optArgNum + 1; op < numArgs; ++op)
2169 textureInst->addIdOperand(texArgs[op]);
2170 setPrecision(textureInst->getResultId(), precision);
2171 buildPoint->addInstruction(std::unique_ptr<Instruction>(textureInst));
2172
2173 Id resultId = textureInst->getResultId();
2174
2175 if (sparse) {
2176 // set capability
2177 addCapability(CapabilitySparseResidency);
2178
2179 // Decode the return type that was a special structure
2180 createStore(createCompositeExtract(resultId, typeId1, 1), parameters.texelOut);
2181 resultId = createCompositeExtract(resultId, typeId0, 0);
2182 setPrecision(resultId, precision);
2183 } else {
2184 // When a smear is needed, do it, as per what was computed
2185 // above when resultType was changed to a scalar type.
2186 if (resultType != smearedType)
2187 resultId = smearScalar(precision, resultId, smearedType);
2188 }
2189
2190 return resultId;
2191 }
2192
2193 // Comments in header
2194 Id Builder::createTextureQueryCall(Op opCode, const TextureParameters& parameters, bool isUnsignedResult)
2195 {
2196 // Figure out the result type
2197 Id resultType = 0;
2198 switch (opCode) {
2199 case OpImageQuerySize:
2200 case OpImageQuerySizeLod:
2201 {
2202 int numComponents = 0;
2203 switch (getTypeDimensionality(getImageType(parameters.sampler))) {
2204 case Dim1D:
2205 case DimBuffer:
2206 numComponents = 1;
2207 break;
2208 case Dim2D:
2209 case DimCube:
2210 case DimRect:
2211 case DimSubpassData:
2212 numComponents = 2;
2213 break;
2214 case Dim3D:
2215 numComponents = 3;
2216 break;
2217
2218 default:
2219 assert(0);
2220 break;
2221 }
2222 if (isArrayedImageType(getImageType(parameters.sampler)))
2223 ++numComponents;
2224
2225 Id intType = isUnsignedResult ? makeUintType(32) : makeIntType(32);
2226 if (numComponents == 1)
2227 resultType = intType;
2228 else
2229 resultType = makeVectorType(intType, numComponents);
2230
2231 break;
2232 }
2233 case OpImageQueryLod:
2234 resultType = makeVectorType(getScalarTypeId(getTypeId(parameters.coords)), 2);
2235 break;
2236 case OpImageQueryLevels:
2237 case OpImageQuerySamples:
2238 resultType = isUnsignedResult ? makeUintType(32) : makeIntType(32);
2239 break;
2240 default:
2241 assert(0);
2242 break;
2243 }
2244
2245 Instruction* query = new Instruction(getUniqueId(), resultType, opCode);
2246 query->addIdOperand(parameters.sampler);
2247 if (parameters.coords)
2248 query->addIdOperand(parameters.coords);
2249 if (parameters.lod)
2250 query->addIdOperand(parameters.lod);
2251 buildPoint->addInstruction(std::unique_ptr<Instruction>(query));
2252 addCapability(CapabilityImageQuery);
2253
2254 return query->getResultId();
2255 }
2256
2257 // External comments in header.
2258 // Operates recursively to visit the composite's hierarchy.
2259 Id Builder::createCompositeCompare(Decoration precision, Id value1, Id value2, bool equal)
2260 {
2261 Id boolType = makeBoolType();
2262 Id valueType = getTypeId(value1);
2263
2264 Id resultId = NoResult;
2265
2266 int numConstituents = getNumTypeConstituents(valueType);
2267
2268 // Scalars and Vectors
2269
2270 if (isScalarType(valueType) || isVectorType(valueType)) {
2271 assert(valueType == getTypeId(value2));
2272 // These just need a single comparison, just have
2273 // to figure out what it is.
2274 Op op;
2275 switch (getMostBasicTypeClass(valueType)) {
2276 case OpTypeFloat:
2277 op = equal ? OpFOrdEqual : OpFUnordNotEqual;
2278 break;
2279 case OpTypeInt:
2280 default:
2281 op = equal ? OpIEqual : OpINotEqual;
2282 break;
2283 case OpTypeBool:
2284 op = equal ? OpLogicalEqual : OpLogicalNotEqual;
2285 precision = NoPrecision;
2286 break;
2287 }
2288
2289 if (isScalarType(valueType)) {
2290 // scalar
2291 resultId = createBinOp(op, boolType, value1, value2);
2292 } else {
2293 // vector
2294 resultId = createBinOp(op, makeVectorType(boolType, numConstituents), value1, value2);
2295 setPrecision(resultId, precision);
2296 // reduce vector compares...
2297 resultId = createUnaryOp(equal ? OpAll : OpAny, boolType, resultId);
2298 }
2299
2300 return setPrecision(resultId, precision);
2301 }
2302
2303 // Only structs, arrays, and matrices should be left.
2304 // They share in common the reduction operation across their constituents.
2305 assert(isAggregateType(valueType) || isMatrixType(valueType));
2306
2307 // Compare each pair of constituents
2308 for (int constituent = 0; constituent < numConstituents; ++constituent) {
2309 std::vector<unsigned> indexes(1, constituent);
2310 Id constituentType1 = getContainedTypeId(getTypeId(value1), constituent);
2311 Id constituentType2 = getContainedTypeId(getTypeId(value2), constituent);
2312 Id constituent1 = createCompositeExtract(value1, constituentType1, indexes);
2313 Id constituent2 = createCompositeExtract(value2, constituentType2, indexes);
2314
2315 Id subResultId = createCompositeCompare(precision, constituent1, constituent2, equal);
2316
2317 if (constituent == 0)
2318 resultId = subResultId;
2319 else
2320 resultId = setPrecision(createBinOp(equal ? OpLogicalAnd : OpLogicalOr, boolType, resultId, subResultId),
2321 precision);
2322 }
2323
2324 return resultId;
2325 }
2326
2327 // OpCompositeConstruct
2328 Id Builder::createCompositeConstruct(Id typeId, const std::vector<Id>& constituents)
2329 {
2330 assert(isAggregateType(typeId) || (getNumTypeConstituents(typeId) > 1 &&
2331 getNumTypeConstituents(typeId) == (int)constituents.size()));
2332
2333 if (generatingOpCodeForSpecConst) {
2334 // Sometime, even in spec-constant-op mode, the constant composite to be
2335 // constructed may not be a specialization constant.
2336 // e.g.:
2337 // const mat2 m2 = mat2(a_spec_const, a_front_end_const, another_front_end_const, third_front_end_const);
2338 // The first column vector should be a spec constant one, as a_spec_const is a spec constant.
2339 // The second column vector should NOT be spec constant, as it does not contain any spec constants.
2340 // To handle such cases, we check the constituents of the constant vector to determine whether this
2341 // vector should be created as a spec constant.
2342 return makeCompositeConstant(typeId, constituents,
2343 std::any_of(constituents.begin(), constituents.end(),
2344 [&](spv::Id id) { return isSpecConstant(id); }));
2345 }
2346
2347 Instruction* op = new Instruction(getUniqueId(), typeId, OpCompositeConstruct);
2348 for (int c = 0; c < (int)constituents.size(); ++c)
2349 op->addIdOperand(constituents[c]);
2350 buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
2351
2352 return op->getResultId();
2353 }
2354
2355 // Vector or scalar constructor
2356 Id Builder::createConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId)
2357 {
2358 Id result = NoResult;
2359 unsigned int numTargetComponents = getNumTypeComponents(resultTypeId);
2360 unsigned int targetComponent = 0;
2361
2362 // Special case: when calling a vector constructor with a single scalar
2363 // argument, smear the scalar
2364 if (sources.size() == 1 && isScalar(sources[0]) && numTargetComponents > 1)
2365 return smearScalar(precision, sources[0], resultTypeId);
2366
2367 // accumulate the arguments for OpCompositeConstruct
2368 std::vector<Id> constituents;
2369 Id scalarTypeId = getScalarTypeId(resultTypeId);
2370
2371 // lambda to store the result of visiting an argument component
2372 const auto latchResult = [&](Id comp) {
2373 if (numTargetComponents > 1)
2374 constituents.push_back(comp);
2375 else
2376 result = comp;
2377 ++targetComponent;
2378 };
2379
2380 // lambda to visit a vector argument's components
2381 const auto accumulateVectorConstituents = [&](Id sourceArg) {
2382 unsigned int sourceSize = getNumComponents(sourceArg);
2383 unsigned int sourcesToUse = sourceSize;
2384 if (sourcesToUse + targetComponent > numTargetComponents)
2385 sourcesToUse = numTargetComponents - targetComponent;
2386
2387 for (unsigned int s = 0; s < sourcesToUse; ++s) {
2388 std::vector<unsigned> swiz;
2389 swiz.push_back(s);
2390 latchResult(createRvalueSwizzle(precision, scalarTypeId, sourceArg, swiz));
2391 }
2392 };
2393
2394 // lambda to visit a matrix argument's components
2395 const auto accumulateMatrixConstituents = [&](Id sourceArg) {
2396 unsigned int sourceSize = getNumColumns(sourceArg) * getNumRows(sourceArg);
2397 unsigned int sourcesToUse = sourceSize;
2398 if (sourcesToUse + targetComponent > numTargetComponents)
2399 sourcesToUse = numTargetComponents - targetComponent;
2400
2401 int col = 0;
2402 int row = 0;
2403 for (unsigned int s = 0; s < sourcesToUse; ++s) {
2404 if (row >= getNumRows(sourceArg)) {
2405 row = 0;
2406 col++;
2407 }
2408 std::vector<Id> indexes;
2409 indexes.push_back(col);
2410 indexes.push_back(row);
2411 latchResult(createCompositeExtract(sourceArg, scalarTypeId, indexes));
2412 row++;
2413 }
2414 };
2415
2416 // Go through the source arguments, each one could have either
2417 // a single or multiple components to contribute.
2418 for (unsigned int i = 0; i < sources.size(); ++i) {
2419
2420 if (isScalar(sources[i]) || isPointer(sources[i]))
2421 latchResult(sources[i]);
2422 else if (isVector(sources[i]))
2423 accumulateVectorConstituents(sources[i]);
2424 else if (isMatrix(sources[i]))
2425 accumulateMatrixConstituents(sources[i]);
2426 else
2427 assert(0);
2428
2429 if (targetComponent >= numTargetComponents)
2430 break;
2431 }
2432
2433 // If the result is a vector, make it from the gathered constituents.
2434 if (constituents.size() > 0)
2435 result = createCompositeConstruct(resultTypeId, constituents);
2436
2437 return setPrecision(result, precision);
2438 }
2439
2440 // Comments in header
2441 Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId)
2442 {
2443 Id componentTypeId = getScalarTypeId(resultTypeId);
2444 int numCols = getTypeNumColumns(resultTypeId);
2445 int numRows = getTypeNumRows(resultTypeId);
2446
2447 Instruction* instr = module.getInstruction(componentTypeId);
2448 #ifdef GLSLANG_WEB
2449 const unsigned bitCount = 32;
2450 assert(bitCount == instr->getImmediateOperand(0));
2451 #else
2452 const unsigned bitCount = instr->getImmediateOperand(0);
2453 #endif
2454
2455 // Optimize matrix constructed from a bigger matrix
2456 if (isMatrix(sources[0]) && getNumColumns(sources[0]) >= numCols && getNumRows(sources[0]) >= numRows) {
2457 // To truncate the matrix to a smaller number of rows/columns, we need to:
2458 // 1. For each column, extract the column and truncate it to the required size using shuffle
2459 // 2. Assemble the resulting matrix from all columns
2460 Id matrix = sources[0];
2461 Id columnTypeId = getContainedTypeId(resultTypeId);
2462 Id sourceColumnTypeId = getContainedTypeId(getTypeId(matrix));
2463
2464 std::vector<unsigned> channels;
2465 for (int row = 0; row < numRows; ++row)
2466 channels.push_back(row);
2467
2468 std::vector<Id> matrixColumns;
2469 for (int col = 0; col < numCols; ++col) {
2470 std::vector<unsigned> indexes;
2471 indexes.push_back(col);
2472 Id colv = createCompositeExtract(matrix, sourceColumnTypeId, indexes);
2473 setPrecision(colv, precision);
2474
2475 if (numRows != getNumRows(matrix)) {
2476 matrixColumns.push_back(createRvalueSwizzle(precision, columnTypeId, colv, channels));
2477 } else {
2478 matrixColumns.push_back(colv);
2479 }
2480 }
2481
2482 return setPrecision(createCompositeConstruct(resultTypeId, matrixColumns), precision);
2483 }
2484
2485 // Otherwise, will use a two step process
2486 // 1. make a compile-time 2D array of values
2487 // 2. construct a matrix from that array
2488
2489 // Step 1.
2490
2491 // initialize the array to the identity matrix
2492 Id ids[maxMatrixSize][maxMatrixSize];
2493 Id one = (bitCount == 64 ? makeDoubleConstant(1.0) : makeFloatConstant(1.0));
2494 Id zero = (bitCount == 64 ? makeDoubleConstant(0.0) : makeFloatConstant(0.0));
2495 for (int col = 0; col < 4; ++col) {
2496 for (int row = 0; row < 4; ++row) {
2497 if (col == row)
2498 ids[col][row] = one;
2499 else
2500 ids[col][row] = zero;
2501 }
2502 }
2503
2504 // modify components as dictated by the arguments
2505 if (sources.size() == 1 && isScalar(sources[0])) {
2506 // a single scalar; resets the diagonals
2507 for (int col = 0; col < 4; ++col)
2508 ids[col][col] = sources[0];
2509 } else if (isMatrix(sources[0])) {
2510 // constructing from another matrix; copy over the parts that exist in both the argument and constructee
2511 Id matrix = sources[0];
2512 int minCols = std::min(numCols, getNumColumns(matrix));
2513 int minRows = std::min(numRows, getNumRows(matrix));
2514 for (int col = 0; col < minCols; ++col) {
2515 std::vector<unsigned> indexes;
2516 indexes.push_back(col);
2517 for (int row = 0; row < minRows; ++row) {
2518 indexes.push_back(row);
2519 ids[col][row] = createCompositeExtract(matrix, componentTypeId, indexes);
2520 indexes.pop_back();
2521 setPrecision(ids[col][row], precision);
2522 }
2523 }
2524 } else {
2525 // fill in the matrix in column-major order with whatever argument components are available
2526 int row = 0;
2527 int col = 0;
2528
2529 for (int arg = 0; arg < (int)sources.size(); ++arg) {
2530 Id argComp = sources[arg];
2531 for (int comp = 0; comp < getNumComponents(sources[arg]); ++comp) {
2532 if (getNumComponents(sources[arg]) > 1) {
2533 argComp = createCompositeExtract(sources[arg], componentTypeId, comp);
2534 setPrecision(argComp, precision);
2535 }
2536 ids[col][row++] = argComp;
2537 if (row == numRows) {
2538 row = 0;
2539 col++;
2540 }
2541 }
2542 }
2543 }
2544
2545 // Step 2: Construct a matrix from that array.
2546 // First make the column vectors, then make the matrix.
2547
2548 // make the column vectors
2549 Id columnTypeId = getContainedTypeId(resultTypeId);
2550 std::vector<Id> matrixColumns;
2551 for (int col = 0; col < numCols; ++col) {
2552 std::vector<Id> vectorComponents;
2553 for (int row = 0; row < numRows; ++row)
2554 vectorComponents.push_back(ids[col][row]);
2555 Id column = createCompositeConstruct(columnTypeId, vectorComponents);
2556 setPrecision(column, precision);
2557 matrixColumns.push_back(column);
2558 }
2559
2560 // make the matrix
2561 return setPrecision(createCompositeConstruct(resultTypeId, matrixColumns), precision);
2562 }
2563
2564 // Comments in header
2565 Builder::If::If(Id cond, unsigned int ctrl, Builder& gb) :
2566 builder(gb),
2567 condition(cond),
2568 control(ctrl),
2569 elseBlock(0)
2570 {
2571 function = &builder.getBuildPoint()->getParent();
2572
2573 // make the blocks, but only put the then-block into the function,
2574 // the else-block and merge-block will be added later, in order, after
2575 // earlier code is emitted
2576 thenBlock = new Block(builder.getUniqueId(), *function);
2577 mergeBlock = new Block(builder.getUniqueId(), *function);
2578
2579 // Save the current block, so that we can add in the flow control split when
2580 // makeEndIf is called.
2581 headerBlock = builder.getBuildPoint();
2582
2583 function->addBlock(thenBlock);
2584 builder.setBuildPoint(thenBlock);
2585 }
2586
2587 // Comments in header
2588 void Builder::If::makeBeginElse()
2589 {
2590 // Close out the "then" by having it jump to the mergeBlock
2591 builder.createBranch(mergeBlock);
2592
2593 // Make the first else block and add it to the function
2594 elseBlock = new Block(builder.getUniqueId(), *function);
2595 function->addBlock(elseBlock);
2596
2597 // Start building the else block
2598 builder.setBuildPoint(elseBlock);
2599 }
2600
2601 // Comments in header
2602 void Builder::If::makeEndIf()
2603 {
2604 // jump to the merge block
2605 builder.createBranch(mergeBlock);
2606
2607 // Go back to the headerBlock and make the flow control split
2608 builder.setBuildPoint(headerBlock);
2609 builder.createSelectionMerge(mergeBlock, control);
2610 if (elseBlock)
2611 builder.createConditionalBranch(condition, thenBlock, elseBlock);
2612 else
2613 builder.createConditionalBranch(condition, thenBlock, mergeBlock);
2614
2615 // add the merge block to the function
2616 function->addBlock(mergeBlock);
2617 builder.setBuildPoint(mergeBlock);
2618 }
2619
2620 // Comments in header
2621 void Builder::makeSwitch(Id selector, unsigned int control, int numSegments, const std::vector<int>& caseValues,
2622 const std::vector<int>& valueIndexToSegment, int defaultSegment,
2623 std::vector<Block*>& segmentBlocks)
2624 {
2625 Function& function = buildPoint->getParent();
2626
2627 // make all the blocks
2628 for (int s = 0; s < numSegments; ++s)
2629 segmentBlocks.push_back(new Block(getUniqueId(), function));
2630
2631 Block* mergeBlock = new Block(getUniqueId(), function);
2632
2633 // make and insert the switch's selection-merge instruction
2634 createSelectionMerge(mergeBlock, control);
2635
2636 // make the switch instruction
2637 Instruction* switchInst = new Instruction(NoResult, NoType, OpSwitch);
2638 switchInst->addIdOperand(selector);
2639 auto defaultOrMerge = (defaultSegment >= 0) ? segmentBlocks[defaultSegment] : mergeBlock;
2640 switchInst->addIdOperand(defaultOrMerge->getId());
2641 defaultOrMerge->addPredecessor(buildPoint);
2642 for (int i = 0; i < (int)caseValues.size(); ++i) {
2643 switchInst->addImmediateOperand(caseValues[i]);
2644 switchInst->addIdOperand(segmentBlocks[valueIndexToSegment[i]]->getId());
2645 segmentBlocks[valueIndexToSegment[i]]->addPredecessor(buildPoint);
2646 }
2647 buildPoint->addInstruction(std::unique_ptr<Instruction>(switchInst));
2648
2649 // push the merge block
2650 switchMerges.push(mergeBlock);
2651 }
2652
2653 // Comments in header
2654 void Builder::addSwitchBreak()
2655 {
2656 // branch to the top of the merge block stack
2657 createBranch(switchMerges.top());
2658 createAndSetNoPredecessorBlock("post-switch-break");
2659 }
2660
2661 // Comments in header
2662 void Builder::nextSwitchSegment(std::vector<Block*>& segmentBlock, int nextSegment)
2663 {
2664 int lastSegment = nextSegment - 1;
2665 if (lastSegment >= 0) {
2666 // Close out previous segment by jumping, if necessary, to next segment
2667 if (! buildPoint->isTerminated())
2668 createBranch(segmentBlock[nextSegment]);
2669 }
2670 Block* block = segmentBlock[nextSegment];
2671 block->getParent().addBlock(block);
2672 setBuildPoint(block);
2673 }
2674
2675 // Comments in header
2676 void Builder::endSwitch(std::vector<Block*>& /*segmentBlock*/)
2677 {
2678 // Close out previous segment by jumping, if necessary, to next segment
2679 if (! buildPoint->isTerminated())
2680 addSwitchBreak();
2681
2682 switchMerges.top()->getParent().addBlock(switchMerges.top());
2683 setBuildPoint(switchMerges.top());
2684
2685 switchMerges.pop();
2686 }
2687
2688 Block& Builder::makeNewBlock()
2689 {
2690 Function& function = buildPoint->getParent();
2691 auto block = new Block(getUniqueId(), function);
2692 function.addBlock(block);
2693 return *block;
2694 }
2695
2696 Builder::LoopBlocks& Builder::makeNewLoop()
2697 {
2698 // This verbosity is needed to simultaneously get the same behavior
2699 // everywhere (id's in the same order), have a syntax that works
2700 // across lots of versions of C++, have no warnings from pedantic
2701 // compilation modes, and leave the rest of the code alone.
2702 Block& head = makeNewBlock();
2703 Block& body = makeNewBlock();
2704 Block& merge = makeNewBlock();
2705 Block& continue_target = makeNewBlock();
2706 LoopBlocks blocks(head, body, merge, continue_target);
2707 loops.push(blocks);
2708 return loops.top();
2709 }
2710
2711 void Builder::createLoopContinue()
2712 {
2713 createBranch(&loops.top().continue_target);
2714 // Set up a block for dead code.
2715 createAndSetNoPredecessorBlock("post-loop-continue");
2716 }
2717
2718 void Builder::createLoopExit()
2719 {
2720 createBranch(&loops.top().merge);
2721 // Set up a block for dead code.
2722 createAndSetNoPredecessorBlock("post-loop-break");
2723 }
2724
2725 void Builder::closeLoop()
2726 {
2727 loops.pop();
2728 }
2729
2730 void Builder::clearAccessChain()
2731 {
2732 accessChain.base = NoResult;
2733 accessChain.indexChain.clear();
2734 accessChain.instr = NoResult;
2735 accessChain.swizzle.clear();
2736 accessChain.component = NoResult;
2737 accessChain.preSwizzleBaseType = NoType;
2738 accessChain.isRValue = false;
2739 accessChain.coherentFlags.clear();
2740 accessChain.alignment = 0;
2741 }
2742
2743 // Comments in header
2744 void Builder::accessChainPushSwizzle(std::vector<unsigned>& swizzle, Id preSwizzleBaseType,
2745 AccessChain::CoherentFlags coherentFlags, unsigned int alignment)
2746 {
2747 accessChain.coherentFlags |= coherentFlags;
2748 accessChain.alignment |= alignment;
2749
2750 // swizzles can be stacked in GLSL, but simplified to a single
2751 // one here; the base type doesn't change
2752 if (accessChain.preSwizzleBaseType == NoType)
2753 accessChain.preSwizzleBaseType = preSwizzleBaseType;
2754
2755 // if needed, propagate the swizzle for the current access chain
2756 if (accessChain.swizzle.size() > 0) {
2757 std::vector<unsigned> oldSwizzle = accessChain.swizzle;
2758 accessChain.swizzle.resize(0);
2759 for (unsigned int i = 0; i < swizzle.size(); ++i) {
2760 assert(swizzle[i] < oldSwizzle.size());
2761 accessChain.swizzle.push_back(oldSwizzle[swizzle[i]]);
2762 }
2763 } else
2764 accessChain.swizzle = swizzle;
2765
2766 // determine if we need to track this swizzle anymore
2767 simplifyAccessChainSwizzle();
2768 }
2769
2770 // Comments in header
2771 void Builder::accessChainStore(Id rvalue, Decoration nonUniform, spv::MemoryAccessMask memoryAccess, spv::Scope scope, unsigned int alignment)
2772 {
2773 assert(accessChain.isRValue == false);
2774
2775 transferAccessChainSwizzle(true);
2776 Id base = collapseAccessChain();
2777 addDecoration(base, nonUniform);
2778
2779 Id source = rvalue;
2780
2781 // dynamic component should be gone
2782 assert(accessChain.component == NoResult);
2783
2784 // If swizzle still exists, it is out-of-order or not full, we must load the target vector,
2785 // extract and insert elements to perform writeMask and/or swizzle.
2786 if (accessChain.swizzle.size() > 0) {
2787 Id tempBaseId = createLoad(base, spv::NoPrecision);
2788 source = createLvalueSwizzle(getTypeId(tempBaseId), tempBaseId, source, accessChain.swizzle);
2789 }
2790
2791 // take LSB of alignment
2792 alignment = alignment & ~(alignment & (alignment-1));
2793 if (getStorageClass(base) == StorageClassPhysicalStorageBufferEXT) {
2794 memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask);
2795 }
2796
2797 createStore(source, base, memoryAccess, scope, alignment);
2798 }
2799
2800 // Comments in header
2801 Id Builder::accessChainLoad(Decoration precision, Decoration nonUniform, Id resultType,
2802 spv::MemoryAccessMask memoryAccess, spv::Scope scope, unsigned int alignment)
2803 {
2804 Id id;
2805
2806 if (accessChain.isRValue) {
2807 // transfer access chain, but try to stay in registers
2808 transferAccessChainSwizzle(false);
2809 if (accessChain.indexChain.size() > 0) {
2810 Id swizzleBase = accessChain.preSwizzleBaseType != NoType ? accessChain.preSwizzleBaseType : resultType;
2811
2812 // if all the accesses are constants, we can use OpCompositeExtract
2813 std::vector<unsigned> indexes;
2814 bool constant = true;
2815 for (int i = 0; i < (int)accessChain.indexChain.size(); ++i) {
2816 if (isConstantScalar(accessChain.indexChain[i]))
2817 indexes.push_back(getConstantScalar(accessChain.indexChain[i]));
2818 else {
2819 constant = false;
2820 break;
2821 }
2822 }
2823
2824 if (constant) {
2825 id = createCompositeExtract(accessChain.base, swizzleBase, indexes);
2826 setPrecision(id, precision);
2827 } else {
2828 Id lValue = NoResult;
2829 if (spvVersion >= Spv_1_4 && isValidInitializer(accessChain.base)) {
2830 // make a new function variable for this r-value, using an initializer,
2831 // and mark it as NonWritable so that downstream it can be detected as a lookup
2832 // table
2833 lValue = createVariable(NoPrecision, StorageClassFunction, getTypeId(accessChain.base),
2834 "indexable", accessChain.base);
2835 addDecoration(lValue, DecorationNonWritable);
2836 } else {
2837 lValue = createVariable(NoPrecision, StorageClassFunction, getTypeId(accessChain.base),
2838 "indexable");
2839 // store into it
2840 createStore(accessChain.base, lValue);
2841 }
2842 // move base to the new variable
2843 accessChain.base = lValue;
2844 accessChain.isRValue = false;
2845
2846 // load through the access chain
2847 id = createLoad(collapseAccessChain(), precision);
2848 }
2849 } else
2850 id = accessChain.base; // no precision, it was set when this was defined
2851 } else {
2852 transferAccessChainSwizzle(true);
2853
2854 // take LSB of alignment
2855 alignment = alignment & ~(alignment & (alignment-1));
2856 if (getStorageClass(accessChain.base) == StorageClassPhysicalStorageBufferEXT) {
2857 memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask);
2858 }
2859
2860 // load through the access chain
2861 id = collapseAccessChain();
2862 // Apply nonuniform both to the access chain and the loaded value.
2863 // Buffer accesses need the access chain decorated, and this is where
2864 // loaded image types get decorated. TODO: This should maybe move to
2865 // createImageTextureFunctionCall.
2866 addDecoration(id, nonUniform);
2867 id = createLoad(id, precision, memoryAccess, scope, alignment);
2868 addDecoration(id, nonUniform);
2869 }
2870
2871 // Done, unless there are swizzles to do
2872 if (accessChain.swizzle.size() == 0 && accessChain.component == NoResult)
2873 return id;
2874
2875 // Do remaining swizzling
2876
2877 // Do the basic swizzle
2878 if (accessChain.swizzle.size() > 0) {
2879 Id swizzledType = getScalarTypeId(getTypeId(id));
2880 if (accessChain.swizzle.size() > 1)
2881 swizzledType = makeVectorType(swizzledType, (int)accessChain.swizzle.size());
2882 id = createRvalueSwizzle(precision, swizzledType, id, accessChain.swizzle);
2883 }
2884
2885 // Do the dynamic component
2886 if (accessChain.component != NoResult)
2887 id = setPrecision(createVectorExtractDynamic(id, resultType, accessChain.component), precision);
2888
2889 addDecoration(id, nonUniform);
2890 return id;
2891 }
2892
2893 Id Builder::accessChainGetLValue()
2894 {
2895 assert(accessChain.isRValue == false);
2896
2897 transferAccessChainSwizzle(true);
2898 Id lvalue = collapseAccessChain();
2899
2900 // If swizzle exists, it is out-of-order or not full, we must load the target vector,
2901 // extract and insert elements to perform writeMask and/or swizzle. This does not
2902 // go with getting a direct l-value pointer.
2903 assert(accessChain.swizzle.size() == 0);
2904 assert(accessChain.component == NoResult);
2905
2906 return lvalue;
2907 }
2908
2909 // comment in header
2910 Id Builder::accessChainGetInferredType()
2911 {
2912 // anything to operate on?
2913 if (accessChain.base == NoResult)
2914 return NoType;
2915 Id type = getTypeId(accessChain.base);
2916
2917 // do initial dereference
2918 if (! accessChain.isRValue)
2919 type = getContainedTypeId(type);
2920
2921 // dereference each index
2922 for (auto it = accessChain.indexChain.cbegin(); it != accessChain.indexChain.cend(); ++it) {
2923 if (isStructType(type))
2924 type = getContainedTypeId(type, getConstantScalar(*it));
2925 else
2926 type = getContainedTypeId(type);
2927 }
2928
2929 // dereference swizzle
2930 if (accessChain.swizzle.size() == 1)
2931 type = getContainedTypeId(type);
2932 else if (accessChain.swizzle.size() > 1)
2933 type = makeVectorType(getContainedTypeId(type), (int)accessChain.swizzle.size());
2934
2935 // dereference component selection
2936 if (accessChain.component)
2937 type = getContainedTypeId(type);
2938
2939 return type;
2940 }
2941
2942 void Builder::dump(std::vector<unsigned int>& out) const
2943 {
2944 // Header, before first instructions:
2945 out.push_back(MagicNumber);
2946 out.push_back(spvVersion);
2947 out.push_back(builderNumber);
2948 out.push_back(uniqueId + 1);
2949 out.push_back(0);
2950
2951 // Capabilities
2952 for (auto it = capabilities.cbegin(); it != capabilities.cend(); ++it) {
2953 Instruction capInst(0, 0, OpCapability);
2954 capInst.addImmediateOperand(*it);
2955 capInst.dump(out);
2956 }
2957
2958 for (auto it = extensions.cbegin(); it != extensions.cend(); ++it) {
2959 Instruction extInst(0, 0, OpExtension);
2960 extInst.addStringOperand(it->c_str());
2961 extInst.dump(out);
2962 }
2963
2964 dumpInstructions(out, imports);
2965 Instruction memInst(0, 0, OpMemoryModel);
2966 memInst.addImmediateOperand(addressModel);
2967 memInst.addImmediateOperand(memoryModel);
2968 memInst.dump(out);
2969
2970 // Instructions saved up while building:
2971 dumpInstructions(out, entryPoints);
2972 dumpInstructions(out, executionModes);
2973
2974 // Debug instructions
2975 dumpInstructions(out, strings);
2976 dumpSourceInstructions(out);
2977 for (int e = 0; e < (int)sourceExtensions.size(); ++e) {
2978 Instruction sourceExtInst(0, 0, OpSourceExtension);
2979 sourceExtInst.addStringOperand(sourceExtensions[e]);
2980 sourceExtInst.dump(out);
2981 }
2982 dumpInstructions(out, names);
2983 dumpModuleProcesses(out);
2984
2985 // Annotation instructions
2986 dumpInstructions(out, decorations);
2987
2988 dumpInstructions(out, constantsTypesGlobals);
2989 dumpInstructions(out, externals);
2990
2991 // The functions
2992 module.dump(out);
2993 }
2994
2995 //
2996 // Protected methods.
2997 //
2998
2999 // Turn the described access chain in 'accessChain' into an instruction(s)
3000 // computing its address. This *cannot* include complex swizzles, which must
3001 // be handled after this is called.
3002 //
3003 // Can generate code.
3004 Id Builder::collapseAccessChain()
3005 {
3006 assert(accessChain.isRValue == false);
3007
3008 // did we already emit an access chain for this?
3009 if (accessChain.instr != NoResult)
3010 return accessChain.instr;
3011
3012 // If we have a dynamic component, we can still transfer
3013 // that into a final operand to the access chain. We need to remap the
3014 // dynamic component through the swizzle to get a new dynamic component to
3015 // update.
3016 //
3017 // This was not done in transferAccessChainSwizzle() because it might
3018 // generate code.
3019 remapDynamicSwizzle();
3020 if (accessChain.component != NoResult) {
3021 // transfer the dynamic component to the access chain
3022 accessChain.indexChain.push_back(accessChain.component);
3023 accessChain.component = NoResult;
3024 }
3025
3026 // note that non-trivial swizzling is left pending
3027
3028 // do we have an access chain?
3029 if (accessChain.indexChain.size() == 0)
3030 return accessChain.base;
3031
3032 // emit the access chain
3033 StorageClass storageClass = (StorageClass)module.getStorageClass(getTypeId(accessChain.base));
3034 accessChain.instr = createAccessChain(storageClass, accessChain.base, accessChain.indexChain);
3035
3036 return accessChain.instr;
3037 }
3038
3039 // For a dynamic component selection of a swizzle.
3040 //
3041 // Turn the swizzle and dynamic component into just a dynamic component.
3042 //
3043 // Generates code.
3044 void Builder::remapDynamicSwizzle()
3045 {
3046 // do we have a swizzle to remap a dynamic component through?
3047 if (accessChain.component != NoResult && accessChain.swizzle.size() > 1) {
3048 // build a vector of the swizzle for the component to map into
3049 std::vector<Id> components;
3050 for (int c = 0; c < (int)accessChain.swizzle.size(); ++c)
3051 components.push_back(makeUintConstant(accessChain.swizzle[c]));
3052 Id mapType = makeVectorType(makeUintType(32), (int)accessChain.swizzle.size());
3053 Id map = makeCompositeConstant(mapType, components);
3054
3055 // use it
3056 accessChain.component = createVectorExtractDynamic(map, makeUintType(32), accessChain.component);
3057 accessChain.swizzle.clear();
3058 }
3059 }
3060
3061 // clear out swizzle if it is redundant, that is reselecting the same components
3062 // that would be present without the swizzle.
3063 void Builder::simplifyAccessChainSwizzle()
3064 {
3065 // If the swizzle has fewer components than the vector, it is subsetting, and must stay
3066 // to preserve that fact.
3067 if (getNumTypeComponents(accessChain.preSwizzleBaseType) > (int)accessChain.swizzle.size())
3068 return;
3069
3070 // if components are out of order, it is a swizzle
3071 for (unsigned int i = 0; i < accessChain.swizzle.size(); ++i) {
3072 if (i != accessChain.swizzle[i])
3073 return;
3074 }
3075
3076 // otherwise, there is no need to track this swizzle
3077 accessChain.swizzle.clear();
3078 if (accessChain.component == NoResult)
3079 accessChain.preSwizzleBaseType = NoType;
3080 }
3081
3082 // To the extent any swizzling can become part of the chain
3083 // of accesses instead of a post operation, make it so.
3084 // If 'dynamic' is true, include transferring the dynamic component,
3085 // otherwise, leave it pending.
3086 //
3087 // Does not generate code. just updates the access chain.
3088 void Builder::transferAccessChainSwizzle(bool dynamic)
3089 {
3090 // non existent?
3091 if (accessChain.swizzle.size() == 0 && accessChain.component == NoResult)
3092 return;
3093
3094 // too complex?
3095 // (this requires either a swizzle, or generating code for a dynamic component)
3096 if (accessChain.swizzle.size() > 1)
3097 return;
3098
3099 // single component, either in the swizzle and/or dynamic component
3100 if (accessChain.swizzle.size() == 1) {
3101 assert(accessChain.component == NoResult);
3102 // handle static component selection
3103 accessChain.indexChain.push_back(makeUintConstant(accessChain.swizzle.front()));
3104 accessChain.swizzle.clear();
3105 accessChain.preSwizzleBaseType = NoType;
3106 } else if (dynamic && accessChain.component != NoResult) {
3107 assert(accessChain.swizzle.size() == 0);
3108 // handle dynamic component
3109 accessChain.indexChain.push_back(accessChain.component);
3110 accessChain.preSwizzleBaseType = NoType;
3111 accessChain.component = NoResult;
3112 }
3113 }
3114
3115 // Utility method for creating a new block and setting the insert point to
3116 // be in it. This is useful for flow-control operations that need a "dummy"
3117 // block proceeding them (e.g. instructions after a discard, etc).
3118 void Builder::createAndSetNoPredecessorBlock(const char* /*name*/)
3119 {
3120 Block* block = new Block(getUniqueId(), buildPoint->getParent());
3121 block->setUnreachable();
3122 buildPoint->getParent().addBlock(block);
3123 setBuildPoint(block);
3124
3125 // if (name)
3126 // addName(block->getId(), name);
3127 }
3128
3129 // Comments in header
3130 void Builder::createBranch(Block* block)
3131 {
3132 Instruction* branch = new Instruction(OpBranch);
3133 branch->addIdOperand(block->getId());
3134 buildPoint->addInstruction(std::unique_ptr<Instruction>(branch));
3135 block->addPredecessor(buildPoint);
3136 }
3137
3138 void Builder::createSelectionMerge(Block* mergeBlock, unsigned int control)
3139 {
3140 Instruction* merge = new Instruction(OpSelectionMerge);
3141 merge->addIdOperand(mergeBlock->getId());
3142 merge->addImmediateOperand(control);
3143 buildPoint->addInstruction(std::unique_ptr<Instruction>(merge));
3144 }
3145
3146 void Builder::createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control,
3147 const std::vector<unsigned int>& operands)
3148 {
3149 Instruction* merge = new Instruction(OpLoopMerge);
3150 merge->addIdOperand(mergeBlock->getId());
3151 merge->addIdOperand(continueBlock->getId());
3152 merge->addImmediateOperand(control);
3153 for (int op = 0; op < (int)operands.size(); ++op)
3154 merge->addImmediateOperand(operands[op]);
3155 buildPoint->addInstruction(std::unique_ptr<Instruction>(merge));
3156 }
3157
3158 void Builder::createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock)
3159 {
3160 Instruction* branch = new Instruction(OpBranchConditional);
3161 branch->addIdOperand(condition);
3162 branch->addIdOperand(thenBlock->getId());
3163 branch->addIdOperand(elseBlock->getId());
3164 buildPoint->addInstruction(std::unique_ptr<Instruction>(branch));
3165 thenBlock->addPredecessor(buildPoint);
3166 elseBlock->addPredecessor(buildPoint);
3167 }
3168
3169 // OpSource
3170 // [OpSourceContinued]
3171 // ...
3172 void Builder::dumpSourceInstructions(const spv::Id fileId, const std::string& text,
3173 std::vector<unsigned int>& out) const
3174 {
3175 const int maxWordCount = 0xFFFF;
3176 const int opSourceWordCount = 4;
3177 const int nonNullBytesPerInstruction = 4 * (maxWordCount - opSourceWordCount) - 1;
3178
3179 if (source != SourceLanguageUnknown) {
3180 // OpSource Language Version File Source
3181 Instruction sourceInst(NoResult, NoType, OpSource);
3182 sourceInst.addImmediateOperand(source);
3183 sourceInst.addImmediateOperand(sourceVersion);
3184 // File operand
3185 if (fileId != NoResult) {
3186 sourceInst.addIdOperand(fileId);
3187 // Source operand
3188 if (text.size() > 0) {
3189 int nextByte = 0;
3190 std::string subString;
3191 while ((int)text.size() - nextByte > 0) {
3192 subString = text.substr(nextByte, nonNullBytesPerInstruction);
3193 if (nextByte == 0) {
3194 // OpSource
3195 sourceInst.addStringOperand(subString.c_str());
3196 sourceInst.dump(out);
3197 } else {
3198 // OpSourcContinued
3199 Instruction sourceContinuedInst(OpSourceContinued);
3200 sourceContinuedInst.addStringOperand(subString.c_str());
3201 sourceContinuedInst.dump(out);
3202 }
3203 nextByte += nonNullBytesPerInstruction;
3204 }
3205 } else
3206 sourceInst.dump(out);
3207 } else
3208 sourceInst.dump(out);
3209 }
3210 }
3211
3212 // Dump an OpSource[Continued] sequence for the source and every include file
3213 void Builder::dumpSourceInstructions(std::vector<unsigned int>& out) const
3214 {
3215 dumpSourceInstructions(sourceFileStringId, sourceText, out);
3216 for (auto iItr = includeFiles.begin(); iItr != includeFiles.end(); ++iItr)
3217 dumpSourceInstructions(iItr->first, *iItr->second, out);
3218 }
3219
3220 void Builder::dumpInstructions(std::vector<unsigned int>& out,
3221 const std::vector<std::unique_ptr<Instruction> >& instructions) const
3222 {
3223 for (int i = 0; i < (int)instructions.size(); ++i) {
3224 instructions[i]->dump(out);
3225 }
3226 }
3227
3228 void Builder::dumpModuleProcesses(std::vector<unsigned int>& out) const
3229 {
3230 for (int i = 0; i < (int)moduleProcesses.size(); ++i) {
3231 Instruction moduleProcessed(OpModuleProcessed);
3232 moduleProcessed.addStringOperand(moduleProcesses[i]);
3233 moduleProcessed.dump(out);
3234 }
3235 }
3236
3237 }; // end spv namespace