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 
Builder(unsigned int spvVersion,unsigned int magicNumber,SpvBuildLogger * buildLogger)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 
~Builder()80 Builder::~Builder()
81 {
82 }
83 
import(const char * name)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.
setLine(int lineNum)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.
setLine(int lineNum,const char * filename)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 
addLine(Id fileName,int lineNum,int column)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).
makeVoidType()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 
makeBoolType()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 
makeSamplerType()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 
makePointer(StorageClass storageClass,Id pointee)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 
makeForwardPointer(StorageClass storageClass)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 
makePointerFromForwardPointer(StorageClass storageClass,Id forwardPointerType,Id pointee)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 
makeIntegerType(int width,bool hasSign)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 
makeFloatType(int width)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.
makeStructType(const std::vector<Id> & members,const char * name)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.
makeStructResultType(Id type0,Id type1)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 
makeVectorType(Id component,int size)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 
makeMatrixType(Id component,int cols,int rows)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 
makeCooperativeMatrixType(Id component,Id scope,Id rows,Id cols)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
makeArrayType(Id element,Id sizeId,int stride)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 
makeRuntimeArray(Id element)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 
makeFunctionType(Id returnType,const std::vector<Id> & paramTypes)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 
makeImageType(Id sampledType,Dim dim,bool depth,bool arrayed,bool ms,unsigned sampled,ImageFormat format)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 
makeSampledImageType(Id imageType)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
makeAccelerationStructureType()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 
makeRayQueryType()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 
getDerefTypeId(Id resultId) const637 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 
getMostBasicTypeClass(Id typeId) const645 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 
getNumTypeConstituents(Id typeId) const664 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.
getScalarTypeId(Id typeId) const697 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.
getContainedTypeId(Id typeId,int member) const723 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.
getContainedTypeId(Id typeId) const747 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.
containsType(Id typeId,spv::Op typeOp,unsigned int width) const755 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.
containsPhysicalStorageBufferOrArray(Id typeId) const785 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).
findScalarConstant(Op typeClass,Op opcode,Id typeId,unsigned value)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').
findScalarConstant(Op typeClass,Op opcode,Id typeId,unsigned v1,unsigned v2)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.
isConstantOpCode(Op opcode) const836 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.
isSpecConstantOpCode(Op opcode) const858 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 
makeBoolConstant(bool b,bool specConstant)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 
makeIntConstant(Id typeId,unsigned value,bool specConstant)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 
makeInt64Constant(Id typeId,unsigned long long value,bool specConstant)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 
makeFloatConstant(float f,bool specConstant)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 
makeDoubleConstant(double d,bool specConstant)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 
makeFloat16Constant(float f16,bool specConstant)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 
makeFpConstant(Id type,double d,bool specConstant)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 
findCompositeConstant(Op typeClass,Id typeId,const std::vector<Id> & comps)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 
findStructConstant(Id typeId,const std::vector<Id> & comps)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
makeCompositeConstant(Id typeId,const std::vector<Id> & members,bool specConstant)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 
addEntryPoint(ExecutionModel model,Function * function,const char * name)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.
addExecutionMode(Function * entryPoint,ExecutionMode mode,int value1,int value2,int value3)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 
addName(Id id,const char * string)1186 void Builder::addName(Id id, const char* string)
1187 {
1188     Instruction* name = new Instruction(OpName);
1189     name->addIdOperand(id);
1190     name->addStringOperand(string);
1191 
1192     names.push_back(std::unique_ptr<Instruction>(name));
1193 }
1194 
addMemberName(Id id,int memberNumber,const char * string)1195 void Builder::addMemberName(Id id, int memberNumber, const char* string)
1196 {
1197     Instruction* name = new Instruction(OpMemberName);
1198     name->addIdOperand(id);
1199     name->addImmediateOperand(memberNumber);
1200     name->addStringOperand(string);
1201 
1202     names.push_back(std::unique_ptr<Instruction>(name));
1203 }
1204 
addDecoration(Id id,Decoration decoration,int num)1205 void Builder::addDecoration(Id id, Decoration decoration, int num)
1206 {
1207     if (decoration == spv::DecorationMax)
1208         return;
1209 
1210     Instruction* dec = new Instruction(OpDecorate);
1211     dec->addIdOperand(id);
1212     dec->addImmediateOperand(decoration);
1213     if (num >= 0)
1214         dec->addImmediateOperand(num);
1215 
1216     decorations.push_back(std::unique_ptr<Instruction>(dec));
1217 }
1218 
addDecoration(Id id,Decoration decoration,const char * s)1219 void Builder::addDecoration(Id id, Decoration decoration, const char* s)
1220 {
1221     if (decoration == spv::DecorationMax)
1222         return;
1223 
1224     Instruction* dec = new Instruction(OpDecorateStringGOOGLE);
1225     dec->addIdOperand(id);
1226     dec->addImmediateOperand(decoration);
1227     dec->addStringOperand(s);
1228 
1229     decorations.push_back(std::unique_ptr<Instruction>(dec));
1230 }
1231 
addDecorationId(Id id,Decoration decoration,Id idDecoration)1232 void Builder::addDecorationId(Id id, Decoration decoration, Id idDecoration)
1233 {
1234     if (decoration == spv::DecorationMax)
1235         return;
1236 
1237     Instruction* dec = new Instruction(OpDecorateId);
1238     dec->addIdOperand(id);
1239     dec->addImmediateOperand(decoration);
1240     dec->addIdOperand(idDecoration);
1241 
1242     decorations.push_back(std::unique_ptr<Instruction>(dec));
1243 }
1244 
addMemberDecoration(Id id,unsigned int member,Decoration decoration,int num)1245 void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, int num)
1246 {
1247     if (decoration == spv::DecorationMax)
1248         return;
1249 
1250     Instruction* dec = new Instruction(OpMemberDecorate);
1251     dec->addIdOperand(id);
1252     dec->addImmediateOperand(member);
1253     dec->addImmediateOperand(decoration);
1254     if (num >= 0)
1255         dec->addImmediateOperand(num);
1256 
1257     decorations.push_back(std::unique_ptr<Instruction>(dec));
1258 }
1259 
addMemberDecoration(Id id,unsigned int member,Decoration decoration,const char * s)1260 void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, const char *s)
1261 {
1262     if (decoration == spv::DecorationMax)
1263         return;
1264 
1265     Instruction* dec = new Instruction(OpMemberDecorateStringGOOGLE);
1266     dec->addIdOperand(id);
1267     dec->addImmediateOperand(member);
1268     dec->addImmediateOperand(decoration);
1269     dec->addStringOperand(s);
1270 
1271     decorations.push_back(std::unique_ptr<Instruction>(dec));
1272 }
1273 
1274 // Comments in header
makeEntryPoint(const char * entryPoint)1275 Function* Builder::makeEntryPoint(const char* entryPoint)
1276 {
1277     assert(! entryPointFunction);
1278 
1279     Block* entry;
1280     std::vector<Id> params;
1281     std::vector<std::vector<Decoration>> decorations;
1282 
1283     entryPointFunction = makeFunctionEntry(NoPrecision, makeVoidType(), entryPoint, params, decorations, &entry);
1284 
1285     return entryPointFunction;
1286 }
1287 
1288 // Comments in header
makeFunctionEntry(Decoration precision,Id returnType,const char * name,const std::vector<Id> & paramTypes,const std::vector<std::vector<Decoration>> & decorations,Block ** entry)1289 Function* Builder::makeFunctionEntry(Decoration precision, Id returnType, const char* name,
1290                                      const std::vector<Id>& paramTypes,
1291                                      const std::vector<std::vector<Decoration>>& decorations, Block **entry)
1292 {
1293     // Make the function and initial instructions in it
1294     Id typeId = makeFunctionType(returnType, paramTypes);
1295     Id firstParamId = paramTypes.size() == 0 ? 0 : getUniqueIds((int)paramTypes.size());
1296     Function* function = new Function(getUniqueId(), returnType, typeId, firstParamId, module);
1297 
1298     // Set up the precisions
1299     setPrecision(function->getId(), precision);
1300     for (unsigned p = 0; p < (unsigned)decorations.size(); ++p) {
1301         for (int d = 0; d < (int)decorations[p].size(); ++d)
1302             addDecoration(firstParamId + p, decorations[p][d]);
1303     }
1304 
1305     // CFG
1306     if (entry) {
1307         *entry = new Block(getUniqueId(), *function);
1308         function->addBlock(*entry);
1309         setBuildPoint(*entry);
1310     }
1311 
1312     if (name)
1313         addName(function->getId(), name);
1314 
1315     functions.push_back(std::unique_ptr<Function>(function));
1316 
1317     return function;
1318 }
1319 
1320 // Comments in header
makeReturn(bool implicit,Id retVal)1321 void Builder::makeReturn(bool implicit, Id retVal)
1322 {
1323     if (retVal) {
1324         Instruction* inst = new Instruction(NoResult, NoType, OpReturnValue);
1325         inst->addIdOperand(retVal);
1326         buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));
1327     } else
1328         buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(NoResult, NoType, OpReturn)));
1329 
1330     if (! implicit)
1331         createAndSetNoPredecessorBlock("post-return");
1332 }
1333 
1334 // Comments in header
leaveFunction()1335 void Builder::leaveFunction()
1336 {
1337     Block* block = buildPoint;
1338     Function& function = buildPoint->getParent();
1339     assert(block);
1340 
1341     // If our function did not contain a return, add a return void now.
1342     if (! block->isTerminated()) {
1343         if (function.getReturnType() == makeVoidType())
1344             makeReturn(true);
1345         else {
1346             makeReturn(true, createUndefined(function.getReturnType()));
1347         }
1348     }
1349 }
1350 
1351 // Comments in header
makeDiscard()1352 void Builder::makeDiscard()
1353 {
1354     buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(OpKill)));
1355     createAndSetNoPredecessorBlock("post-discard");
1356 }
1357 
1358 // Comments in header
createVariable(StorageClass storageClass,Id type,const char * name,Id initializer)1359 Id Builder::createVariable(StorageClass storageClass, Id type, const char* name, Id initializer)
1360 {
1361     Id pointerType = makePointer(storageClass, type);
1362     Instruction* inst = new Instruction(getUniqueId(), pointerType, OpVariable);
1363     inst->addImmediateOperand(storageClass);
1364     if (initializer != NoResult)
1365         inst->addIdOperand(initializer);
1366 
1367     switch (storageClass) {
1368     case StorageClassFunction:
1369         // Validation rules require the declaration in the entry block
1370         buildPoint->getParent().addLocalVariable(std::unique_ptr<Instruction>(inst));
1371         break;
1372 
1373     default:
1374         constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(inst));
1375         module.mapInstruction(inst);
1376         break;
1377     }
1378 
1379     if (name)
1380         addName(inst->getResultId(), name);
1381 
1382     return inst->getResultId();
1383 }
1384 
1385 // Comments in header
createUndefined(Id type)1386 Id Builder::createUndefined(Id type)
1387 {
1388   Instruction* inst = new Instruction(getUniqueId(), type, OpUndef);
1389   buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));
1390   return inst->getResultId();
1391 }
1392 
1393 // av/vis/nonprivate are unnecessary and illegal for some storage classes.
sanitizeMemoryAccessForStorageClass(spv::MemoryAccessMask memoryAccess,StorageClass sc) const1394 spv::MemoryAccessMask Builder::sanitizeMemoryAccessForStorageClass(spv::MemoryAccessMask memoryAccess, StorageClass sc)
1395     const
1396 {
1397     switch (sc) {
1398     case spv::StorageClassUniform:
1399     case spv::StorageClassWorkgroup:
1400     case spv::StorageClassStorageBuffer:
1401     case spv::StorageClassPhysicalStorageBufferEXT:
1402         break;
1403     default:
1404         memoryAccess = spv::MemoryAccessMask(memoryAccess &
1405                         ~(spv::MemoryAccessMakePointerAvailableKHRMask |
1406                           spv::MemoryAccessMakePointerVisibleKHRMask |
1407                           spv::MemoryAccessNonPrivatePointerKHRMask));
1408         break;
1409     }
1410     return memoryAccess;
1411 }
1412 
1413 // Comments in header
createStore(Id rValue,Id lValue,spv::MemoryAccessMask memoryAccess,spv::Scope scope,unsigned int alignment)1414 void Builder::createStore(Id rValue, Id lValue, spv::MemoryAccessMask memoryAccess, spv::Scope scope,
1415     unsigned int alignment)
1416 {
1417     Instruction* store = new Instruction(OpStore);
1418     store->addIdOperand(lValue);
1419     store->addIdOperand(rValue);
1420 
1421     memoryAccess = sanitizeMemoryAccessForStorageClass(memoryAccess, getStorageClass(lValue));
1422 
1423     if (memoryAccess != MemoryAccessMaskNone) {
1424         store->addImmediateOperand(memoryAccess);
1425         if (memoryAccess & spv::MemoryAccessAlignedMask) {
1426             store->addImmediateOperand(alignment);
1427         }
1428         if (memoryAccess & spv::MemoryAccessMakePointerAvailableKHRMask) {
1429             store->addIdOperand(makeUintConstant(scope));
1430         }
1431     }
1432 
1433     buildPoint->addInstruction(std::unique_ptr<Instruction>(store));
1434 }
1435 
1436 // Comments in header
createLoad(Id lValue,spv::MemoryAccessMask memoryAccess,spv::Scope scope,unsigned int alignment)1437 Id Builder::createLoad(Id lValue, spv::MemoryAccessMask memoryAccess, spv::Scope scope, unsigned int alignment)
1438 {
1439     Instruction* load = new Instruction(getUniqueId(), getDerefTypeId(lValue), OpLoad);
1440     load->addIdOperand(lValue);
1441 
1442     memoryAccess = sanitizeMemoryAccessForStorageClass(memoryAccess, getStorageClass(lValue));
1443 
1444     if (memoryAccess != MemoryAccessMaskNone) {
1445         load->addImmediateOperand(memoryAccess);
1446         if (memoryAccess & spv::MemoryAccessAlignedMask) {
1447             load->addImmediateOperand(alignment);
1448         }
1449         if (memoryAccess & spv::MemoryAccessMakePointerVisibleKHRMask) {
1450             load->addIdOperand(makeUintConstant(scope));
1451         }
1452     }
1453 
1454     buildPoint->addInstruction(std::unique_ptr<Instruction>(load));
1455 
1456     return load->getResultId();
1457 }
1458 
1459 // Comments in header
createAccessChain(StorageClass storageClass,Id base,const std::vector<Id> & offsets)1460 Id Builder::createAccessChain(StorageClass storageClass, Id base, const std::vector<Id>& offsets)
1461 {
1462     // Figure out the final resulting type.
1463     spv::Id typeId = getTypeId(base);
1464     assert(isPointerType(typeId) && offsets.size() > 0);
1465     typeId = getContainedTypeId(typeId);
1466     for (int i = 0; i < (int)offsets.size(); ++i) {
1467         if (isStructType(typeId)) {
1468             assert(isConstantScalar(offsets[i]));
1469             typeId = getContainedTypeId(typeId, getConstantScalar(offsets[i]));
1470         } else
1471             typeId = getContainedTypeId(typeId, offsets[i]);
1472     }
1473     typeId = makePointer(storageClass, typeId);
1474 
1475     // Make the instruction
1476     Instruction* chain = new Instruction(getUniqueId(), typeId, OpAccessChain);
1477     chain->addIdOperand(base);
1478     for (int i = 0; i < (int)offsets.size(); ++i)
1479         chain->addIdOperand(offsets[i]);
1480     buildPoint->addInstruction(std::unique_ptr<Instruction>(chain));
1481 
1482     return chain->getResultId();
1483 }
1484 
createArrayLength(Id base,unsigned int member)1485 Id Builder::createArrayLength(Id base, unsigned int member)
1486 {
1487     spv::Id intType = makeUintType(32);
1488     Instruction* length = new Instruction(getUniqueId(), intType, OpArrayLength);
1489     length->addIdOperand(base);
1490     length->addImmediateOperand(member);
1491     buildPoint->addInstruction(std::unique_ptr<Instruction>(length));
1492 
1493     return length->getResultId();
1494 }
1495 
createCooperativeMatrixLength(Id type)1496 Id Builder::createCooperativeMatrixLength(Id type)
1497 {
1498     spv::Id intType = makeUintType(32);
1499 
1500     // Generate code for spec constants if in spec constant operation
1501     // generation mode.
1502     if (generatingOpCodeForSpecConst) {
1503         return createSpecConstantOp(OpCooperativeMatrixLengthNV, intType, std::vector<Id>(1, type), std::vector<Id>());
1504     }
1505 
1506     Instruction* length = new Instruction(getUniqueId(), intType, OpCooperativeMatrixLengthNV);
1507     length->addIdOperand(type);
1508     buildPoint->addInstruction(std::unique_ptr<Instruction>(length));
1509 
1510     return length->getResultId();
1511 }
1512 
createCompositeExtract(Id composite,Id typeId,unsigned index)1513 Id Builder::createCompositeExtract(Id composite, Id typeId, unsigned index)
1514 {
1515     // Generate code for spec constants if in spec constant operation
1516     // generation mode.
1517     if (generatingOpCodeForSpecConst) {
1518         return createSpecConstantOp(OpCompositeExtract, typeId, std::vector<Id>(1, composite),
1519             std::vector<Id>(1, index));
1520     }
1521     Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract);
1522     extract->addIdOperand(composite);
1523     extract->addImmediateOperand(index);
1524     buildPoint->addInstruction(std::unique_ptr<Instruction>(extract));
1525 
1526     return extract->getResultId();
1527 }
1528 
createCompositeExtract(Id composite,Id typeId,const std::vector<unsigned> & indexes)1529 Id Builder::createCompositeExtract(Id composite, Id typeId, const std::vector<unsigned>& indexes)
1530 {
1531     // Generate code for spec constants if in spec constant operation
1532     // generation mode.
1533     if (generatingOpCodeForSpecConst) {
1534         return createSpecConstantOp(OpCompositeExtract, typeId, std::vector<Id>(1, composite), indexes);
1535     }
1536     Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract);
1537     extract->addIdOperand(composite);
1538     for (int i = 0; i < (int)indexes.size(); ++i)
1539         extract->addImmediateOperand(indexes[i]);
1540     buildPoint->addInstruction(std::unique_ptr<Instruction>(extract));
1541 
1542     return extract->getResultId();
1543 }
1544 
createCompositeInsert(Id object,Id composite,Id typeId,unsigned index)1545 Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, unsigned index)
1546 {
1547     Instruction* insert = new Instruction(getUniqueId(), typeId, OpCompositeInsert);
1548     insert->addIdOperand(object);
1549     insert->addIdOperand(composite);
1550     insert->addImmediateOperand(index);
1551     buildPoint->addInstruction(std::unique_ptr<Instruction>(insert));
1552 
1553     return insert->getResultId();
1554 }
1555 
createCompositeInsert(Id object,Id composite,Id typeId,const std::vector<unsigned> & indexes)1556 Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, const std::vector<unsigned>& indexes)
1557 {
1558     Instruction* insert = new Instruction(getUniqueId(), typeId, OpCompositeInsert);
1559     insert->addIdOperand(object);
1560     insert->addIdOperand(composite);
1561     for (int i = 0; i < (int)indexes.size(); ++i)
1562         insert->addImmediateOperand(indexes[i]);
1563     buildPoint->addInstruction(std::unique_ptr<Instruction>(insert));
1564 
1565     return insert->getResultId();
1566 }
1567 
createVectorExtractDynamic(Id vector,Id typeId,Id componentIndex)1568 Id Builder::createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex)
1569 {
1570     Instruction* extract = new Instruction(getUniqueId(), typeId, OpVectorExtractDynamic);
1571     extract->addIdOperand(vector);
1572     extract->addIdOperand(componentIndex);
1573     buildPoint->addInstruction(std::unique_ptr<Instruction>(extract));
1574 
1575     return extract->getResultId();
1576 }
1577 
createVectorInsertDynamic(Id vector,Id typeId,Id component,Id componentIndex)1578 Id Builder::createVectorInsertDynamic(Id vector, Id typeId, Id component, Id componentIndex)
1579 {
1580     Instruction* insert = new Instruction(getUniqueId(), typeId, OpVectorInsertDynamic);
1581     insert->addIdOperand(vector);
1582     insert->addIdOperand(component);
1583     insert->addIdOperand(componentIndex);
1584     buildPoint->addInstruction(std::unique_ptr<Instruction>(insert));
1585 
1586     return insert->getResultId();
1587 }
1588 
1589 // An opcode that has no operands, no result id, and no type
createNoResultOp(Op opCode)1590 void Builder::createNoResultOp(Op opCode)
1591 {
1592     Instruction* op = new Instruction(opCode);
1593     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1594 }
1595 
1596 // An opcode that has one id operand, no result id, and no type
createNoResultOp(Op opCode,Id operand)1597 void Builder::createNoResultOp(Op opCode, Id operand)
1598 {
1599     Instruction* op = new Instruction(opCode);
1600     op->addIdOperand(operand);
1601     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1602 }
1603 
1604 // An opcode that has one or more operands, no result id, and no type
createNoResultOp(Op opCode,const std::vector<Id> & operands)1605 void Builder::createNoResultOp(Op opCode, const std::vector<Id>& operands)
1606 {
1607     Instruction* op = new Instruction(opCode);
1608     for (auto it = operands.cbegin(); it != operands.cend(); ++it) {
1609         op->addIdOperand(*it);
1610     }
1611     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1612 }
1613 
1614 // An opcode that has multiple operands, no result id, and no type
createNoResultOp(Op opCode,const std::vector<IdImmediate> & operands)1615 void Builder::createNoResultOp(Op opCode, const std::vector<IdImmediate>& operands)
1616 {
1617     Instruction* op = new Instruction(opCode);
1618     for (auto it = operands.cbegin(); it != operands.cend(); ++it) {
1619         if (it->isId)
1620             op->addIdOperand(it->word);
1621         else
1622             op->addImmediateOperand(it->word);
1623     }
1624     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1625 }
1626 
createControlBarrier(Scope execution,Scope memory,MemorySemanticsMask semantics)1627 void Builder::createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask semantics)
1628 {
1629     Instruction* op = new Instruction(OpControlBarrier);
1630     op->addIdOperand(makeUintConstant(execution));
1631     op->addIdOperand(makeUintConstant(memory));
1632     op->addIdOperand(makeUintConstant(semantics));
1633     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1634 }
1635 
createMemoryBarrier(unsigned executionScope,unsigned memorySemantics)1636 void Builder::createMemoryBarrier(unsigned executionScope, unsigned memorySemantics)
1637 {
1638     Instruction* op = new Instruction(OpMemoryBarrier);
1639     op->addIdOperand(makeUintConstant(executionScope));
1640     op->addIdOperand(makeUintConstant(memorySemantics));
1641     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1642 }
1643 
1644 // An opcode that has one operands, a result id, and a type
createUnaryOp(Op opCode,Id typeId,Id operand)1645 Id Builder::createUnaryOp(Op opCode, Id typeId, Id operand)
1646 {
1647     // Generate code for spec constants if in spec constant operation
1648     // generation mode.
1649     if (generatingOpCodeForSpecConst) {
1650         return createSpecConstantOp(opCode, typeId, std::vector<Id>(1, operand), std::vector<Id>());
1651     }
1652     Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1653     op->addIdOperand(operand);
1654     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1655 
1656     return op->getResultId();
1657 }
1658 
createBinOp(Op opCode,Id typeId,Id left,Id right)1659 Id Builder::createBinOp(Op opCode, Id typeId, Id left, Id right)
1660 {
1661     // Generate code for spec constants if in spec constant operation
1662     // generation mode.
1663     if (generatingOpCodeForSpecConst) {
1664         std::vector<Id> operands(2);
1665         operands[0] = left; operands[1] = right;
1666         return createSpecConstantOp(opCode, typeId, operands, std::vector<Id>());
1667     }
1668     Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1669     op->addIdOperand(left);
1670     op->addIdOperand(right);
1671     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1672 
1673     return op->getResultId();
1674 }
1675 
createTriOp(Op opCode,Id typeId,Id op1,Id op2,Id op3)1676 Id Builder::createTriOp(Op opCode, Id typeId, Id op1, Id op2, Id op3)
1677 {
1678     // Generate code for spec constants if in spec constant operation
1679     // generation mode.
1680     if (generatingOpCodeForSpecConst) {
1681         std::vector<Id> operands(3);
1682         operands[0] = op1;
1683         operands[1] = op2;
1684         operands[2] = op3;
1685         return createSpecConstantOp(
1686             opCode, typeId, operands, std::vector<Id>());
1687     }
1688     Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1689     op->addIdOperand(op1);
1690     op->addIdOperand(op2);
1691     op->addIdOperand(op3);
1692     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1693 
1694     return op->getResultId();
1695 }
1696 
createOp(Op opCode,Id typeId,const std::vector<Id> & operands)1697 Id Builder::createOp(Op opCode, Id typeId, const std::vector<Id>& operands)
1698 {
1699     Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1700     for (auto it = operands.cbegin(); it != operands.cend(); ++it)
1701         op->addIdOperand(*it);
1702     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1703 
1704     return op->getResultId();
1705 }
1706 
createOp(Op opCode,Id typeId,const std::vector<IdImmediate> & operands)1707 Id Builder::createOp(Op opCode, Id typeId, const std::vector<IdImmediate>& operands)
1708 {
1709     Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
1710     for (auto it = operands.cbegin(); it != operands.cend(); ++it) {
1711         if (it->isId)
1712             op->addIdOperand(it->word);
1713         else
1714             op->addImmediateOperand(it->word);
1715     }
1716     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1717 
1718     return op->getResultId();
1719 }
1720 
createSpecConstantOp(Op opCode,Id typeId,const std::vector<Id> & operands,const std::vector<unsigned> & literals)1721 Id Builder::createSpecConstantOp(Op opCode, Id typeId, const std::vector<Id>& operands,
1722     const std::vector<unsigned>& literals)
1723 {
1724     Instruction* op = new Instruction(getUniqueId(), typeId, OpSpecConstantOp);
1725     op->addImmediateOperand((unsigned) opCode);
1726     for (auto it = operands.cbegin(); it != operands.cend(); ++it)
1727         op->addIdOperand(*it);
1728     for (auto it = literals.cbegin(); it != literals.cend(); ++it)
1729         op->addImmediateOperand(*it);
1730     module.mapInstruction(op);
1731     constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(op));
1732 
1733     return op->getResultId();
1734 }
1735 
createFunctionCall(spv::Function * function,const std::vector<spv::Id> & args)1736 Id Builder::createFunctionCall(spv::Function* function, const std::vector<spv::Id>& args)
1737 {
1738     Instruction* op = new Instruction(getUniqueId(), function->getReturnType(), OpFunctionCall);
1739     op->addIdOperand(function->getId());
1740     for (int a = 0; a < (int)args.size(); ++a)
1741         op->addIdOperand(args[a]);
1742     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
1743 
1744     return op->getResultId();
1745 }
1746 
1747 // Comments in header
createRvalueSwizzle(Decoration precision,Id typeId,Id source,const std::vector<unsigned> & channels)1748 Id Builder::createRvalueSwizzle(Decoration precision, Id typeId, Id source, const std::vector<unsigned>& channels)
1749 {
1750     if (channels.size() == 1)
1751         return setPrecision(createCompositeExtract(source, typeId, channels.front()), precision);
1752 
1753     if (generatingOpCodeForSpecConst) {
1754         std::vector<Id> operands(2);
1755         operands[0] = operands[1] = source;
1756         return setPrecision(createSpecConstantOp(OpVectorShuffle, typeId, operands, channels), precision);
1757     }
1758     Instruction* swizzle = new Instruction(getUniqueId(), typeId, OpVectorShuffle);
1759     assert(isVector(source));
1760     swizzle->addIdOperand(source);
1761     swizzle->addIdOperand(source);
1762     for (int i = 0; i < (int)channels.size(); ++i)
1763         swizzle->addImmediateOperand(channels[i]);
1764     buildPoint->addInstruction(std::unique_ptr<Instruction>(swizzle));
1765 
1766     return setPrecision(swizzle->getResultId(), precision);
1767 }
1768 
1769 // Comments in header
createLvalueSwizzle(Id typeId,Id target,Id source,const std::vector<unsigned> & channels)1770 Id Builder::createLvalueSwizzle(Id typeId, Id target, Id source, const std::vector<unsigned>& channels)
1771 {
1772     if (channels.size() == 1 && getNumComponents(source) == 1)
1773         return createCompositeInsert(source, target, typeId, channels.front());
1774 
1775     Instruction* swizzle = new Instruction(getUniqueId(), typeId, OpVectorShuffle);
1776 
1777     assert(isVector(target));
1778     swizzle->addIdOperand(target);
1779 
1780     assert(getNumComponents(source) == (int)channels.size());
1781     assert(isVector(source));
1782     swizzle->addIdOperand(source);
1783 
1784     // Set up an identity shuffle from the base value to the result value
1785     unsigned int components[4];
1786     int numTargetComponents = getNumComponents(target);
1787     for (int i = 0; i < numTargetComponents; ++i)
1788         components[i] = i;
1789 
1790     // Punch in the l-value swizzle
1791     for (int i = 0; i < (int)channels.size(); ++i)
1792         components[channels[i]] = numTargetComponents + i;
1793 
1794     // finish the instruction with these components selectors
1795     for (int i = 0; i < numTargetComponents; ++i)
1796         swizzle->addImmediateOperand(components[i]);
1797     buildPoint->addInstruction(std::unique_ptr<Instruction>(swizzle));
1798 
1799     return swizzle->getResultId();
1800 }
1801 
1802 // Comments in header
promoteScalar(Decoration precision,Id & left,Id & right)1803 void Builder::promoteScalar(Decoration precision, Id& left, Id& right)
1804 {
1805     int direction = getNumComponents(right) - getNumComponents(left);
1806 
1807     if (direction > 0)
1808         left = smearScalar(precision, left, makeVectorType(getTypeId(left), getNumComponents(right)));
1809     else if (direction < 0)
1810         right = smearScalar(precision, right, makeVectorType(getTypeId(right), getNumComponents(left)));
1811 
1812     return;
1813 }
1814 
1815 // Comments in header
smearScalar(Decoration precision,Id scalar,Id vectorType)1816 Id Builder::smearScalar(Decoration precision, Id scalar, Id vectorType)
1817 {
1818     assert(getNumComponents(scalar) == 1);
1819     assert(getTypeId(scalar) == getScalarTypeId(vectorType));
1820 
1821     int numComponents = getNumTypeComponents(vectorType);
1822     if (numComponents == 1)
1823         return scalar;
1824 
1825     Instruction* smear = nullptr;
1826     if (generatingOpCodeForSpecConst) {
1827         auto members = std::vector<spv::Id>(numComponents, scalar);
1828         // Sometime even in spec-constant-op mode, the temporary vector created by
1829         // promoting a scalar might not be a spec constant. This should depend on
1830         // the scalar.
1831         // e.g.:
1832         //  const vec2 spec_const_result = a_spec_const_vec2 + a_front_end_const_scalar;
1833         // In such cases, the temporary vector created from a_front_end_const_scalar
1834         // is not a spec constant vector, even though the binary operation node is marked
1835         // as 'specConstant' and we are in spec-constant-op mode.
1836         auto result_id = makeCompositeConstant(vectorType, members, isSpecConstant(scalar));
1837         smear = module.getInstruction(result_id);
1838     } else {
1839         smear = new Instruction(getUniqueId(), vectorType, OpCompositeConstruct);
1840         for (int c = 0; c < numComponents; ++c)
1841             smear->addIdOperand(scalar);
1842         buildPoint->addInstruction(std::unique_ptr<Instruction>(smear));
1843     }
1844 
1845     return setPrecision(smear->getResultId(), precision);
1846 }
1847 
1848 // Comments in header
createBuiltinCall(Id resultType,Id builtins,int entryPoint,const std::vector<Id> & args)1849 Id Builder::createBuiltinCall(Id resultType, Id builtins, int entryPoint, const std::vector<Id>& args)
1850 {
1851     Instruction* inst = new Instruction(getUniqueId(), resultType, OpExtInst);
1852     inst->addIdOperand(builtins);
1853     inst->addImmediateOperand(entryPoint);
1854     for (int arg = 0; arg < (int)args.size(); ++arg)
1855         inst->addIdOperand(args[arg]);
1856 
1857     buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));
1858 
1859     return inst->getResultId();
1860 }
1861 
1862 // Accept all parameters needed to create a texture instruction.
1863 // Create the correct instruction based on the inputs, and make the call.
createTextureCall(Decoration precision,Id resultType,bool sparse,bool fetch,bool proj,bool gather,bool noImplicitLod,const TextureParameters & parameters,ImageOperandsMask signExtensionMask)1864 Id Builder::createTextureCall(Decoration precision, Id resultType, bool sparse, bool fetch, bool proj, bool gather,
1865     bool noImplicitLod, const TextureParameters& parameters, ImageOperandsMask signExtensionMask)
1866 {
1867     static const int maxTextureArgs = 10;
1868     Id texArgs[maxTextureArgs] = {};
1869 
1870     //
1871     // Set up the fixed arguments
1872     //
1873     int numArgs = 0;
1874     bool explicitLod = false;
1875     texArgs[numArgs++] = parameters.sampler;
1876     texArgs[numArgs++] = parameters.coords;
1877     if (parameters.Dref != NoResult)
1878         texArgs[numArgs++] = parameters.Dref;
1879     if (parameters.component != NoResult)
1880         texArgs[numArgs++] = parameters.component;
1881 
1882 #ifndef GLSLANG_WEB
1883     if (parameters.granularity != NoResult)
1884         texArgs[numArgs++] = parameters.granularity;
1885     if (parameters.coarse != NoResult)
1886         texArgs[numArgs++] = parameters.coarse;
1887 #endif
1888 
1889     //
1890     // Set up the optional arguments
1891     //
1892     int optArgNum = numArgs;    // track which operand, if it exists, is the mask of optional arguments
1893     ++numArgs;                  // speculatively make room for the mask operand
1894     ImageOperandsMask mask = ImageOperandsMaskNone; // the mask operand
1895     if (parameters.bias) {
1896         mask = (ImageOperandsMask)(mask | ImageOperandsBiasMask);
1897         texArgs[numArgs++] = parameters.bias;
1898     }
1899     if (parameters.lod) {
1900         mask = (ImageOperandsMask)(mask | ImageOperandsLodMask);
1901         texArgs[numArgs++] = parameters.lod;
1902         explicitLod = true;
1903     } else if (parameters.gradX) {
1904         mask = (ImageOperandsMask)(mask | ImageOperandsGradMask);
1905         texArgs[numArgs++] = parameters.gradX;
1906         texArgs[numArgs++] = parameters.gradY;
1907         explicitLod = true;
1908     } else if (noImplicitLod && ! fetch && ! gather) {
1909         // have to explicitly use lod of 0 if not allowed to have them be implicit, and
1910         // we would otherwise be about to issue an implicit instruction
1911         mask = (ImageOperandsMask)(mask | ImageOperandsLodMask);
1912         texArgs[numArgs++] = makeFloatConstant(0.0);
1913         explicitLod = true;
1914     }
1915     if (parameters.offset) {
1916         if (isConstant(parameters.offset))
1917             mask = (ImageOperandsMask)(mask | ImageOperandsConstOffsetMask);
1918         else {
1919             addCapability(CapabilityImageGatherExtended);
1920             mask = (ImageOperandsMask)(mask | ImageOperandsOffsetMask);
1921         }
1922         texArgs[numArgs++] = parameters.offset;
1923     }
1924     if (parameters.offsets) {
1925         addCapability(CapabilityImageGatherExtended);
1926         mask = (ImageOperandsMask)(mask | ImageOperandsConstOffsetsMask);
1927         texArgs[numArgs++] = parameters.offsets;
1928     }
1929 #ifndef GLSLANG_WEB
1930     if (parameters.sample) {
1931         mask = (ImageOperandsMask)(mask | ImageOperandsSampleMask);
1932         texArgs[numArgs++] = parameters.sample;
1933     }
1934     if (parameters.lodClamp) {
1935         // capability if this bit is used
1936         addCapability(CapabilityMinLod);
1937 
1938         mask = (ImageOperandsMask)(mask | ImageOperandsMinLodMask);
1939         texArgs[numArgs++] = parameters.lodClamp;
1940     }
1941     if (parameters.nonprivate) {
1942         mask = mask | ImageOperandsNonPrivateTexelKHRMask;
1943     }
1944     if (parameters.volatil) {
1945         mask = mask | ImageOperandsVolatileTexelKHRMask;
1946     }
1947 #endif
1948     mask = mask | signExtensionMask;
1949     if (mask == ImageOperandsMaskNone)
1950         --numArgs;  // undo speculative reservation for the mask argument
1951     else
1952         texArgs[optArgNum] = mask;
1953 
1954     //
1955     // Set up the instruction
1956     //
1957     Op opCode = OpNop;  // All paths below need to set this
1958     if (fetch) {
1959         if (sparse)
1960             opCode = OpImageSparseFetch;
1961         else
1962             opCode = OpImageFetch;
1963 #ifndef GLSLANG_WEB
1964     } else if (parameters.granularity && parameters.coarse) {
1965         opCode = OpImageSampleFootprintNV;
1966     } else if (gather) {
1967         if (parameters.Dref)
1968             if (sparse)
1969                 opCode = OpImageSparseDrefGather;
1970             else
1971                 opCode = OpImageDrefGather;
1972         else
1973             if (sparse)
1974                 opCode = OpImageSparseGather;
1975             else
1976                 opCode = OpImageGather;
1977 #endif
1978     } else if (explicitLod) {
1979         if (parameters.Dref) {
1980             if (proj)
1981                 if (sparse)
1982                     opCode = OpImageSparseSampleProjDrefExplicitLod;
1983                 else
1984                     opCode = OpImageSampleProjDrefExplicitLod;
1985             else
1986                 if (sparse)
1987                     opCode = OpImageSparseSampleDrefExplicitLod;
1988                 else
1989                     opCode = OpImageSampleDrefExplicitLod;
1990         } else {
1991             if (proj)
1992                 if (sparse)
1993                     opCode = OpImageSparseSampleProjExplicitLod;
1994                 else
1995                     opCode = OpImageSampleProjExplicitLod;
1996             else
1997                 if (sparse)
1998                     opCode = OpImageSparseSampleExplicitLod;
1999                 else
2000                     opCode = OpImageSampleExplicitLod;
2001         }
2002     } else {
2003         if (parameters.Dref) {
2004             if (proj)
2005                 if (sparse)
2006                     opCode = OpImageSparseSampleProjDrefImplicitLod;
2007                 else
2008                     opCode = OpImageSampleProjDrefImplicitLod;
2009             else
2010                 if (sparse)
2011                     opCode = OpImageSparseSampleDrefImplicitLod;
2012                 else
2013                     opCode = OpImageSampleDrefImplicitLod;
2014         } else {
2015             if (proj)
2016                 if (sparse)
2017                     opCode = OpImageSparseSampleProjImplicitLod;
2018                 else
2019                     opCode = OpImageSampleProjImplicitLod;
2020             else
2021                 if (sparse)
2022                     opCode = OpImageSparseSampleImplicitLod;
2023                 else
2024                     opCode = OpImageSampleImplicitLod;
2025         }
2026     }
2027 
2028     // See if the result type is expecting a smeared result.
2029     // This happens when a legacy shadow*() call is made, which
2030     // gets a vec4 back instead of a float.
2031     Id smearedType = resultType;
2032     if (! isScalarType(resultType)) {
2033         switch (opCode) {
2034         case OpImageSampleDrefImplicitLod:
2035         case OpImageSampleDrefExplicitLod:
2036         case OpImageSampleProjDrefImplicitLod:
2037         case OpImageSampleProjDrefExplicitLod:
2038             resultType = getScalarTypeId(resultType);
2039             break;
2040         default:
2041             break;
2042         }
2043     }
2044 
2045     Id typeId0 = 0;
2046     Id typeId1 = 0;
2047 
2048     if (sparse) {
2049         typeId0 = resultType;
2050         typeId1 = getDerefTypeId(parameters.texelOut);
2051         resultType = makeStructResultType(typeId0, typeId1);
2052     }
2053 
2054     // Build the SPIR-V instruction
2055     Instruction* textureInst = new Instruction(getUniqueId(), resultType, opCode);
2056     for (int op = 0; op < optArgNum; ++op)
2057         textureInst->addIdOperand(texArgs[op]);
2058     if (optArgNum < numArgs)
2059         textureInst->addImmediateOperand(texArgs[optArgNum]);
2060     for (int op = optArgNum + 1; op < numArgs; ++op)
2061         textureInst->addIdOperand(texArgs[op]);
2062     setPrecision(textureInst->getResultId(), precision);
2063     buildPoint->addInstruction(std::unique_ptr<Instruction>(textureInst));
2064 
2065     Id resultId = textureInst->getResultId();
2066 
2067     if (sparse) {
2068         // set capability
2069         addCapability(CapabilitySparseResidency);
2070 
2071         // Decode the return type that was a special structure
2072         createStore(createCompositeExtract(resultId, typeId1, 1), parameters.texelOut);
2073         resultId = createCompositeExtract(resultId, typeId0, 0);
2074         setPrecision(resultId, precision);
2075     } else {
2076         // When a smear is needed, do it, as per what was computed
2077         // above when resultType was changed to a scalar type.
2078         if (resultType != smearedType)
2079             resultId = smearScalar(precision, resultId, smearedType);
2080     }
2081 
2082     return resultId;
2083 }
2084 
2085 // Comments in header
createTextureQueryCall(Op opCode,const TextureParameters & parameters,bool isUnsignedResult)2086 Id Builder::createTextureQueryCall(Op opCode, const TextureParameters& parameters, bool isUnsignedResult)
2087 {
2088     // Figure out the result type
2089     Id resultType = 0;
2090     switch (opCode) {
2091     case OpImageQuerySize:
2092     case OpImageQuerySizeLod:
2093     {
2094         int numComponents = 0;
2095         switch (getTypeDimensionality(getImageType(parameters.sampler))) {
2096         case Dim1D:
2097         case DimBuffer:
2098             numComponents = 1;
2099             break;
2100         case Dim2D:
2101         case DimCube:
2102         case DimRect:
2103         case DimSubpassData:
2104             numComponents = 2;
2105             break;
2106         case Dim3D:
2107             numComponents = 3;
2108             break;
2109 
2110         default:
2111             assert(0);
2112             break;
2113         }
2114         if (isArrayedImageType(getImageType(parameters.sampler)))
2115             ++numComponents;
2116 
2117         Id intType = isUnsignedResult ? makeUintType(32) : makeIntType(32);
2118         if (numComponents == 1)
2119             resultType = intType;
2120         else
2121             resultType = makeVectorType(intType, numComponents);
2122 
2123         break;
2124     }
2125     case OpImageQueryLod:
2126         resultType = makeVectorType(getScalarTypeId(getTypeId(parameters.coords)), 2);
2127         break;
2128     case OpImageQueryLevels:
2129     case OpImageQuerySamples:
2130         resultType = isUnsignedResult ? makeUintType(32) : makeIntType(32);
2131         break;
2132     default:
2133         assert(0);
2134         break;
2135     }
2136 
2137     Instruction* query = new Instruction(getUniqueId(), resultType, opCode);
2138     query->addIdOperand(parameters.sampler);
2139     if (parameters.coords)
2140         query->addIdOperand(parameters.coords);
2141     if (parameters.lod)
2142         query->addIdOperand(parameters.lod);
2143     buildPoint->addInstruction(std::unique_ptr<Instruction>(query));
2144     addCapability(CapabilityImageQuery);
2145 
2146     return query->getResultId();
2147 }
2148 
2149 // External comments in header.
2150 // Operates recursively to visit the composite's hierarchy.
createCompositeCompare(Decoration precision,Id value1,Id value2,bool equal)2151 Id Builder::createCompositeCompare(Decoration precision, Id value1, Id value2, bool equal)
2152 {
2153     Id boolType = makeBoolType();
2154     Id valueType = getTypeId(value1);
2155 
2156     Id resultId = NoResult;
2157 
2158     int numConstituents = getNumTypeConstituents(valueType);
2159 
2160     // Scalars and Vectors
2161 
2162     if (isScalarType(valueType) || isVectorType(valueType)) {
2163         assert(valueType == getTypeId(value2));
2164         // These just need a single comparison, just have
2165         // to figure out what it is.
2166         Op op;
2167         switch (getMostBasicTypeClass(valueType)) {
2168         case OpTypeFloat:
2169             op = equal ? OpFOrdEqual : OpFOrdNotEqual;
2170             break;
2171         case OpTypeInt:
2172         default:
2173             op = equal ? OpIEqual : OpINotEqual;
2174             break;
2175         case OpTypeBool:
2176             op = equal ? OpLogicalEqual : OpLogicalNotEqual;
2177             precision = NoPrecision;
2178             break;
2179         }
2180 
2181         if (isScalarType(valueType)) {
2182             // scalar
2183             resultId = createBinOp(op, boolType, value1, value2);
2184         } else {
2185             // vector
2186             resultId = createBinOp(op, makeVectorType(boolType, numConstituents), value1, value2);
2187             setPrecision(resultId, precision);
2188             // reduce vector compares...
2189             resultId = createUnaryOp(equal ? OpAll : OpAny, boolType, resultId);
2190         }
2191 
2192         return setPrecision(resultId, precision);
2193     }
2194 
2195     // Only structs, arrays, and matrices should be left.
2196     // They share in common the reduction operation across their constituents.
2197     assert(isAggregateType(valueType) || isMatrixType(valueType));
2198 
2199     // Compare each pair of constituents
2200     for (int constituent = 0; constituent < numConstituents; ++constituent) {
2201         std::vector<unsigned> indexes(1, constituent);
2202         Id constituentType1 = getContainedTypeId(getTypeId(value1), constituent);
2203         Id constituentType2 = getContainedTypeId(getTypeId(value2), constituent);
2204         Id constituent1 = createCompositeExtract(value1, constituentType1, indexes);
2205         Id constituent2 = createCompositeExtract(value2, constituentType2, indexes);
2206 
2207         Id subResultId = createCompositeCompare(precision, constituent1, constituent2, equal);
2208 
2209         if (constituent == 0)
2210             resultId = subResultId;
2211         else
2212             resultId = setPrecision(createBinOp(equal ? OpLogicalAnd : OpLogicalOr, boolType, resultId, subResultId),
2213                                     precision);
2214     }
2215 
2216     return resultId;
2217 }
2218 
2219 // OpCompositeConstruct
createCompositeConstruct(Id typeId,const std::vector<Id> & constituents)2220 Id Builder::createCompositeConstruct(Id typeId, const std::vector<Id>& constituents)
2221 {
2222     assert(isAggregateType(typeId) || (getNumTypeConstituents(typeId) > 1 &&
2223            getNumTypeConstituents(typeId) == (int)constituents.size()));
2224 
2225     if (generatingOpCodeForSpecConst) {
2226         // Sometime, even in spec-constant-op mode, the constant composite to be
2227         // constructed may not be a specialization constant.
2228         // e.g.:
2229         //  const mat2 m2 = mat2(a_spec_const, a_front_end_const, another_front_end_const, third_front_end_const);
2230         // The first column vector should be a spec constant one, as a_spec_const is a spec constant.
2231         // The second column vector should NOT be spec constant, as it does not contain any spec constants.
2232         // To handle such cases, we check the constituents of the constant vector to determine whether this
2233         // vector should be created as a spec constant.
2234         return makeCompositeConstant(typeId, constituents,
2235                                      std::any_of(constituents.begin(), constituents.end(),
2236                                                  [&](spv::Id id) { return isSpecConstant(id); }));
2237     }
2238 
2239     Instruction* op = new Instruction(getUniqueId(), typeId, OpCompositeConstruct);
2240     for (int c = 0; c < (int)constituents.size(); ++c)
2241         op->addIdOperand(constituents[c]);
2242     buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
2243 
2244     return op->getResultId();
2245 }
2246 
2247 // Vector or scalar constructor
createConstructor(Decoration precision,const std::vector<Id> & sources,Id resultTypeId)2248 Id Builder::createConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId)
2249 {
2250     Id result = NoResult;
2251     unsigned int numTargetComponents = getNumTypeComponents(resultTypeId);
2252     unsigned int targetComponent = 0;
2253 
2254     // Special case: when calling a vector constructor with a single scalar
2255     // argument, smear the scalar
2256     if (sources.size() == 1 && isScalar(sources[0]) && numTargetComponents > 1)
2257         return smearScalar(precision, sources[0], resultTypeId);
2258 
2259     // accumulate the arguments for OpCompositeConstruct
2260     std::vector<Id> constituents;
2261     Id scalarTypeId = getScalarTypeId(resultTypeId);
2262 
2263     // lambda to store the result of visiting an argument component
2264     const auto latchResult = [&](Id comp) {
2265         if (numTargetComponents > 1)
2266             constituents.push_back(comp);
2267         else
2268             result = comp;
2269         ++targetComponent;
2270     };
2271 
2272     // lambda to visit a vector argument's components
2273     const auto accumulateVectorConstituents = [&](Id sourceArg) {
2274         unsigned int sourceSize = getNumComponents(sourceArg);
2275         unsigned int sourcesToUse = sourceSize;
2276         if (sourcesToUse + targetComponent > numTargetComponents)
2277             sourcesToUse = numTargetComponents - targetComponent;
2278 
2279         for (unsigned int s = 0; s < sourcesToUse; ++s) {
2280             std::vector<unsigned> swiz;
2281             swiz.push_back(s);
2282             latchResult(createRvalueSwizzle(precision, scalarTypeId, sourceArg, swiz));
2283         }
2284     };
2285 
2286     // lambda to visit a matrix argument's components
2287     const auto accumulateMatrixConstituents = [&](Id sourceArg) {
2288         unsigned int sourceSize = getNumColumns(sourceArg) * getNumRows(sourceArg);
2289         unsigned int sourcesToUse = sourceSize;
2290         if (sourcesToUse + targetComponent > numTargetComponents)
2291             sourcesToUse = numTargetComponents - targetComponent;
2292 
2293         int col = 0;
2294         int row = 0;
2295         for (unsigned int s = 0; s < sourcesToUse; ++s) {
2296             if (row >= getNumRows(sourceArg)) {
2297                 row = 0;
2298                 col++;
2299             }
2300             std::vector<Id> indexes;
2301             indexes.push_back(col);
2302             indexes.push_back(row);
2303             latchResult(createCompositeExtract(sourceArg, scalarTypeId, indexes));
2304             row++;
2305         }
2306     };
2307 
2308     // Go through the source arguments, each one could have either
2309     // a single or multiple components to contribute.
2310     for (unsigned int i = 0; i < sources.size(); ++i) {
2311 
2312         if (isScalar(sources[i]) || isPointer(sources[i]))
2313             latchResult(sources[i]);
2314         else if (isVector(sources[i]))
2315             accumulateVectorConstituents(sources[i]);
2316         else if (isMatrix(sources[i]))
2317             accumulateMatrixConstituents(sources[i]);
2318         else
2319             assert(0);
2320 
2321         if (targetComponent >= numTargetComponents)
2322             break;
2323     }
2324 
2325     // If the result is a vector, make it from the gathered constituents.
2326     if (constituents.size() > 0)
2327         result = createCompositeConstruct(resultTypeId, constituents);
2328 
2329     return setPrecision(result, precision);
2330 }
2331 
2332 // Comments in header
createMatrixConstructor(Decoration precision,const std::vector<Id> & sources,Id resultTypeId)2333 Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId)
2334 {
2335     Id componentTypeId = getScalarTypeId(resultTypeId);
2336     int numCols = getTypeNumColumns(resultTypeId);
2337     int numRows = getTypeNumRows(resultTypeId);
2338 
2339     Instruction* instr = module.getInstruction(componentTypeId);
2340 #ifdef GLSLANG_WEB
2341     const unsigned bitCount = 32;
2342     assert(bitCount == instr->getImmediateOperand(0));
2343 #else
2344     const unsigned bitCount = instr->getImmediateOperand(0);
2345 #endif
2346 
2347     // Optimize matrix constructed from a bigger matrix
2348     if (isMatrix(sources[0]) && getNumColumns(sources[0]) >= numCols && getNumRows(sources[0]) >= numRows) {
2349         // To truncate the matrix to a smaller number of rows/columns, we need to:
2350         // 1. For each column, extract the column and truncate it to the required size using shuffle
2351         // 2. Assemble the resulting matrix from all columns
2352         Id matrix = sources[0];
2353         Id columnTypeId = getContainedTypeId(resultTypeId);
2354         Id sourceColumnTypeId = getContainedTypeId(getTypeId(matrix));
2355 
2356         std::vector<unsigned> channels;
2357         for (int row = 0; row < numRows; ++row)
2358             channels.push_back(row);
2359 
2360         std::vector<Id> matrixColumns;
2361         for (int col = 0; col < numCols; ++col) {
2362             std::vector<unsigned> indexes;
2363             indexes.push_back(col);
2364             Id colv = createCompositeExtract(matrix, sourceColumnTypeId, indexes);
2365             setPrecision(colv, precision);
2366 
2367             if (numRows != getNumRows(matrix)) {
2368                 matrixColumns.push_back(createRvalueSwizzle(precision, columnTypeId, colv, channels));
2369             } else {
2370                 matrixColumns.push_back(colv);
2371             }
2372         }
2373 
2374         return setPrecision(createCompositeConstruct(resultTypeId, matrixColumns), precision);
2375     }
2376 
2377     // Otherwise, will use a two step process
2378     // 1. make a compile-time 2D array of values
2379     // 2. construct a matrix from that array
2380 
2381     // Step 1.
2382 
2383     // initialize the array to the identity matrix
2384     Id ids[maxMatrixSize][maxMatrixSize];
2385     Id  one = (bitCount == 64 ? makeDoubleConstant(1.0) : makeFloatConstant(1.0));
2386     Id zero = (bitCount == 64 ? makeDoubleConstant(0.0) : makeFloatConstant(0.0));
2387     for (int col = 0; col < 4; ++col) {
2388         for (int row = 0; row < 4; ++row) {
2389             if (col == row)
2390                 ids[col][row] = one;
2391             else
2392                 ids[col][row] = zero;
2393         }
2394     }
2395 
2396     // modify components as dictated by the arguments
2397     if (sources.size() == 1 && isScalar(sources[0])) {
2398         // a single scalar; resets the diagonals
2399         for (int col = 0; col < 4; ++col)
2400             ids[col][col] = sources[0];
2401     } else if (isMatrix(sources[0])) {
2402         // constructing from another matrix; copy over the parts that exist in both the argument and constructee
2403         Id matrix = sources[0];
2404         int minCols = std::min(numCols, getNumColumns(matrix));
2405         int minRows = std::min(numRows, getNumRows(matrix));
2406         for (int col = 0; col < minCols; ++col) {
2407             std::vector<unsigned> indexes;
2408             indexes.push_back(col);
2409             for (int row = 0; row < minRows; ++row) {
2410                 indexes.push_back(row);
2411                 ids[col][row] = createCompositeExtract(matrix, componentTypeId, indexes);
2412                 indexes.pop_back();
2413                 setPrecision(ids[col][row], precision);
2414             }
2415         }
2416     } else {
2417         // fill in the matrix in column-major order with whatever argument components are available
2418         int row = 0;
2419         int col = 0;
2420 
2421         for (int arg = 0; arg < (int)sources.size(); ++arg) {
2422             Id argComp = sources[arg];
2423             for (int comp = 0; comp < getNumComponents(sources[arg]); ++comp) {
2424                 if (getNumComponents(sources[arg]) > 1) {
2425                     argComp = createCompositeExtract(sources[arg], componentTypeId, comp);
2426                     setPrecision(argComp, precision);
2427                 }
2428                 ids[col][row++] = argComp;
2429                 if (row == numRows) {
2430                     row = 0;
2431                     col++;
2432                 }
2433             }
2434         }
2435     }
2436 
2437     // Step 2:  Construct a matrix from that array.
2438     // First make the column vectors, then make the matrix.
2439 
2440     // make the column vectors
2441     Id columnTypeId = getContainedTypeId(resultTypeId);
2442     std::vector<Id> matrixColumns;
2443     for (int col = 0; col < numCols; ++col) {
2444         std::vector<Id> vectorComponents;
2445         for (int row = 0; row < numRows; ++row)
2446             vectorComponents.push_back(ids[col][row]);
2447         Id column = createCompositeConstruct(columnTypeId, vectorComponents);
2448         setPrecision(column, precision);
2449         matrixColumns.push_back(column);
2450     }
2451 
2452     // make the matrix
2453     return setPrecision(createCompositeConstruct(resultTypeId, matrixColumns), precision);
2454 }
2455 
2456 // Comments in header
If(Id cond,unsigned int ctrl,Builder & gb)2457 Builder::If::If(Id cond, unsigned int ctrl, Builder& gb) :
2458     builder(gb),
2459     condition(cond),
2460     control(ctrl),
2461     elseBlock(0)
2462 {
2463     function = &builder.getBuildPoint()->getParent();
2464 
2465     // make the blocks, but only put the then-block into the function,
2466     // the else-block and merge-block will be added later, in order, after
2467     // earlier code is emitted
2468     thenBlock = new Block(builder.getUniqueId(), *function);
2469     mergeBlock = new Block(builder.getUniqueId(), *function);
2470 
2471     // Save the current block, so that we can add in the flow control split when
2472     // makeEndIf is called.
2473     headerBlock = builder.getBuildPoint();
2474 
2475     function->addBlock(thenBlock);
2476     builder.setBuildPoint(thenBlock);
2477 }
2478 
2479 // Comments in header
makeBeginElse()2480 void Builder::If::makeBeginElse()
2481 {
2482     // Close out the "then" by having it jump to the mergeBlock
2483     builder.createBranch(mergeBlock);
2484 
2485     // Make the first else block and add it to the function
2486     elseBlock = new Block(builder.getUniqueId(), *function);
2487     function->addBlock(elseBlock);
2488 
2489     // Start building the else block
2490     builder.setBuildPoint(elseBlock);
2491 }
2492 
2493 // Comments in header
makeEndIf()2494 void Builder::If::makeEndIf()
2495 {
2496     // jump to the merge block
2497     builder.createBranch(mergeBlock);
2498 
2499     // Go back to the headerBlock and make the flow control split
2500     builder.setBuildPoint(headerBlock);
2501     builder.createSelectionMerge(mergeBlock, control);
2502     if (elseBlock)
2503         builder.createConditionalBranch(condition, thenBlock, elseBlock);
2504     else
2505         builder.createConditionalBranch(condition, thenBlock, mergeBlock);
2506 
2507     // add the merge block to the function
2508     function->addBlock(mergeBlock);
2509     builder.setBuildPoint(mergeBlock);
2510 }
2511 
2512 // Comments in header
makeSwitch(Id selector,unsigned int control,int numSegments,const std::vector<int> & caseValues,const std::vector<int> & valueIndexToSegment,int defaultSegment,std::vector<Block * > & segmentBlocks)2513 void Builder::makeSwitch(Id selector, unsigned int control, int numSegments, const std::vector<int>& caseValues,
2514                          const std::vector<int>& valueIndexToSegment, int defaultSegment,
2515                          std::vector<Block*>& segmentBlocks)
2516 {
2517     Function& function = buildPoint->getParent();
2518 
2519     // make all the blocks
2520     for (int s = 0; s < numSegments; ++s)
2521         segmentBlocks.push_back(new Block(getUniqueId(), function));
2522 
2523     Block* mergeBlock = new Block(getUniqueId(), function);
2524 
2525     // make and insert the switch's selection-merge instruction
2526     createSelectionMerge(mergeBlock, control);
2527 
2528     // make the switch instruction
2529     Instruction* switchInst = new Instruction(NoResult, NoType, OpSwitch);
2530     switchInst->addIdOperand(selector);
2531     auto defaultOrMerge = (defaultSegment >= 0) ? segmentBlocks[defaultSegment] : mergeBlock;
2532     switchInst->addIdOperand(defaultOrMerge->getId());
2533     defaultOrMerge->addPredecessor(buildPoint);
2534     for (int i = 0; i < (int)caseValues.size(); ++i) {
2535         switchInst->addImmediateOperand(caseValues[i]);
2536         switchInst->addIdOperand(segmentBlocks[valueIndexToSegment[i]]->getId());
2537         segmentBlocks[valueIndexToSegment[i]]->addPredecessor(buildPoint);
2538     }
2539     buildPoint->addInstruction(std::unique_ptr<Instruction>(switchInst));
2540 
2541     // push the merge block
2542     switchMerges.push(mergeBlock);
2543 }
2544 
2545 // Comments in header
addSwitchBreak()2546 void Builder::addSwitchBreak()
2547 {
2548     // branch to the top of the merge block stack
2549     createBranch(switchMerges.top());
2550     createAndSetNoPredecessorBlock("post-switch-break");
2551 }
2552 
2553 // Comments in header
nextSwitchSegment(std::vector<Block * > & segmentBlock,int nextSegment)2554 void Builder::nextSwitchSegment(std::vector<Block*>& segmentBlock, int nextSegment)
2555 {
2556     int lastSegment = nextSegment - 1;
2557     if (lastSegment >= 0) {
2558         // Close out previous segment by jumping, if necessary, to next segment
2559         if (! buildPoint->isTerminated())
2560             createBranch(segmentBlock[nextSegment]);
2561     }
2562     Block* block = segmentBlock[nextSegment];
2563     block->getParent().addBlock(block);
2564     setBuildPoint(block);
2565 }
2566 
2567 // Comments in header
endSwitch(std::vector<Block * > &)2568 void Builder::endSwitch(std::vector<Block*>& /*segmentBlock*/)
2569 {
2570     // Close out previous segment by jumping, if necessary, to next segment
2571     if (! buildPoint->isTerminated())
2572         addSwitchBreak();
2573 
2574     switchMerges.top()->getParent().addBlock(switchMerges.top());
2575     setBuildPoint(switchMerges.top());
2576 
2577     switchMerges.pop();
2578 }
2579 
makeNewBlock()2580 Block& Builder::makeNewBlock()
2581 {
2582     Function& function = buildPoint->getParent();
2583     auto block = new Block(getUniqueId(), function);
2584     function.addBlock(block);
2585     return *block;
2586 }
2587 
makeNewLoop()2588 Builder::LoopBlocks& Builder::makeNewLoop()
2589 {
2590     // This verbosity is needed to simultaneously get the same behavior
2591     // everywhere (id's in the same order), have a syntax that works
2592     // across lots of versions of C++, have no warnings from pedantic
2593     // compilation modes, and leave the rest of the code alone.
2594     Block& head            = makeNewBlock();
2595     Block& body            = makeNewBlock();
2596     Block& merge           = makeNewBlock();
2597     Block& continue_target = makeNewBlock();
2598     LoopBlocks blocks(head, body, merge, continue_target);
2599     loops.push(blocks);
2600     return loops.top();
2601 }
2602 
createLoopContinue()2603 void Builder::createLoopContinue()
2604 {
2605     createBranch(&loops.top().continue_target);
2606     // Set up a block for dead code.
2607     createAndSetNoPredecessorBlock("post-loop-continue");
2608 }
2609 
createLoopExit()2610 void Builder::createLoopExit()
2611 {
2612     createBranch(&loops.top().merge);
2613     // Set up a block for dead code.
2614     createAndSetNoPredecessorBlock("post-loop-break");
2615 }
2616 
closeLoop()2617 void Builder::closeLoop()
2618 {
2619     loops.pop();
2620 }
2621 
clearAccessChain()2622 void Builder::clearAccessChain()
2623 {
2624     accessChain.base = NoResult;
2625     accessChain.indexChain.clear();
2626     accessChain.instr = NoResult;
2627     accessChain.swizzle.clear();
2628     accessChain.component = NoResult;
2629     accessChain.preSwizzleBaseType = NoType;
2630     accessChain.isRValue = false;
2631     accessChain.coherentFlags.clear();
2632     accessChain.alignment = 0;
2633 }
2634 
2635 // Comments in header
accessChainPushSwizzle(std::vector<unsigned> & swizzle,Id preSwizzleBaseType,AccessChain::CoherentFlags coherentFlags,unsigned int alignment)2636 void Builder::accessChainPushSwizzle(std::vector<unsigned>& swizzle, Id preSwizzleBaseType,
2637     AccessChain::CoherentFlags coherentFlags, unsigned int alignment)
2638 {
2639     accessChain.coherentFlags |= coherentFlags;
2640     accessChain.alignment |= alignment;
2641 
2642     // swizzles can be stacked in GLSL, but simplified to a single
2643     // one here; the base type doesn't change
2644     if (accessChain.preSwizzleBaseType == NoType)
2645         accessChain.preSwizzleBaseType = preSwizzleBaseType;
2646 
2647     // if needed, propagate the swizzle for the current access chain
2648     if (accessChain.swizzle.size() > 0) {
2649         std::vector<unsigned> oldSwizzle = accessChain.swizzle;
2650         accessChain.swizzle.resize(0);
2651         for (unsigned int i = 0; i < swizzle.size(); ++i) {
2652             assert(swizzle[i] < oldSwizzle.size());
2653             accessChain.swizzle.push_back(oldSwizzle[swizzle[i]]);
2654         }
2655     } else
2656         accessChain.swizzle = swizzle;
2657 
2658     // determine if we need to track this swizzle anymore
2659     simplifyAccessChainSwizzle();
2660 }
2661 
2662 // Comments in header
accessChainStore(Id rvalue,spv::MemoryAccessMask memoryAccess,spv::Scope scope,unsigned int alignment)2663 void Builder::accessChainStore(Id rvalue, spv::MemoryAccessMask memoryAccess, spv::Scope scope, unsigned int alignment)
2664 {
2665     assert(accessChain.isRValue == false);
2666 
2667     transferAccessChainSwizzle(true);
2668     Id base = collapseAccessChain();
2669     Id source = rvalue;
2670 
2671     // dynamic component should be gone
2672     assert(accessChain.component == NoResult);
2673 
2674     // If swizzle still exists, it is out-of-order or not full, we must load the target vector,
2675     // extract and insert elements to perform writeMask and/or swizzle.
2676     if (accessChain.swizzle.size() > 0) {
2677         Id tempBaseId = createLoad(base);
2678         source = createLvalueSwizzle(getTypeId(tempBaseId), tempBaseId, source, accessChain.swizzle);
2679     }
2680 
2681     // take LSB of alignment
2682     alignment = alignment & ~(alignment & (alignment-1));
2683     if (getStorageClass(base) == StorageClassPhysicalStorageBufferEXT) {
2684         memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask);
2685     }
2686 
2687     createStore(source, base, memoryAccess, scope, alignment);
2688 }
2689 
2690 // Comments in header
accessChainLoad(Decoration precision,Decoration nonUniform,Id resultType,spv::MemoryAccessMask memoryAccess,spv::Scope scope,unsigned int alignment)2691 Id Builder::accessChainLoad(Decoration precision, Decoration nonUniform, Id resultType,
2692     spv::MemoryAccessMask memoryAccess, spv::Scope scope, unsigned int alignment)
2693 {
2694     Id id;
2695 
2696     if (accessChain.isRValue) {
2697         // transfer access chain, but try to stay in registers
2698         transferAccessChainSwizzle(false);
2699         if (accessChain.indexChain.size() > 0) {
2700             Id swizzleBase = accessChain.preSwizzleBaseType != NoType ? accessChain.preSwizzleBaseType : resultType;
2701 
2702             // if all the accesses are constants, we can use OpCompositeExtract
2703             std::vector<unsigned> indexes;
2704             bool constant = true;
2705             for (int i = 0; i < (int)accessChain.indexChain.size(); ++i) {
2706                 if (isConstantScalar(accessChain.indexChain[i]))
2707                     indexes.push_back(getConstantScalar(accessChain.indexChain[i]));
2708                 else {
2709                     constant = false;
2710                     break;
2711                 }
2712             }
2713 
2714             if (constant) {
2715                 id = createCompositeExtract(accessChain.base, swizzleBase, indexes);
2716             } else {
2717                 Id lValue = NoResult;
2718                 if (spvVersion >= Spv_1_4) {
2719                     // make a new function variable for this r-value, using an initializer,
2720                     // and mark it as NonWritable so that downstream it can be detected as a lookup
2721                     // table
2722                     lValue = createVariable(StorageClassFunction, getTypeId(accessChain.base), "indexable",
2723                         accessChain.base);
2724                     addDecoration(lValue, DecorationNonWritable);
2725                 } else {
2726                     lValue = createVariable(StorageClassFunction, getTypeId(accessChain.base), "indexable");
2727                     // store into it
2728                     createStore(accessChain.base, lValue);
2729                 }
2730                 // move base to the new variable
2731                 accessChain.base = lValue;
2732                 accessChain.isRValue = false;
2733 
2734                 // load through the access chain
2735                 id = createLoad(collapseAccessChain());
2736             }
2737             setPrecision(id, precision);
2738         } else
2739             id = accessChain.base;  // no precision, it was set when this was defined
2740     } else {
2741         transferAccessChainSwizzle(true);
2742 
2743         // take LSB of alignment
2744         alignment = alignment & ~(alignment & (alignment-1));
2745         if (getStorageClass(accessChain.base) == StorageClassPhysicalStorageBufferEXT) {
2746             memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask);
2747         }
2748 
2749         // load through the access chain
2750         id = collapseAccessChain();
2751         // Apply nonuniform both to the access chain and the loaded value.
2752         // Buffer accesses need the access chain decorated, and this is where
2753         // loaded image types get decorated. TODO: This should maybe move to
2754         // createImageTextureFunctionCall.
2755         addDecoration(id, nonUniform);
2756         id = createLoad(id, memoryAccess, scope, alignment);
2757         setPrecision(id, precision);
2758         addDecoration(id, nonUniform);
2759     }
2760 
2761     // Done, unless there are swizzles to do
2762     if (accessChain.swizzle.size() == 0 && accessChain.component == NoResult)
2763         return id;
2764 
2765     // Do remaining swizzling
2766 
2767     // Do the basic swizzle
2768     if (accessChain.swizzle.size() > 0) {
2769         Id swizzledType = getScalarTypeId(getTypeId(id));
2770         if (accessChain.swizzle.size() > 1)
2771             swizzledType = makeVectorType(swizzledType, (int)accessChain.swizzle.size());
2772         id = createRvalueSwizzle(precision, swizzledType, id, accessChain.swizzle);
2773     }
2774 
2775     // Do the dynamic component
2776     if (accessChain.component != NoResult)
2777         id = setPrecision(createVectorExtractDynamic(id, resultType, accessChain.component), precision);
2778 
2779     addDecoration(id, nonUniform);
2780     return id;
2781 }
2782 
accessChainGetLValue()2783 Id Builder::accessChainGetLValue()
2784 {
2785     assert(accessChain.isRValue == false);
2786 
2787     transferAccessChainSwizzle(true);
2788     Id lvalue = collapseAccessChain();
2789 
2790     // If swizzle exists, it is out-of-order or not full, we must load the target vector,
2791     // extract and insert elements to perform writeMask and/or swizzle.  This does not
2792     // go with getting a direct l-value pointer.
2793     assert(accessChain.swizzle.size() == 0);
2794     assert(accessChain.component == NoResult);
2795 
2796     return lvalue;
2797 }
2798 
2799 // comment in header
accessChainGetInferredType()2800 Id Builder::accessChainGetInferredType()
2801 {
2802     // anything to operate on?
2803     if (accessChain.base == NoResult)
2804         return NoType;
2805     Id type = getTypeId(accessChain.base);
2806 
2807     // do initial dereference
2808     if (! accessChain.isRValue)
2809         type = getContainedTypeId(type);
2810 
2811     // dereference each index
2812     for (auto it = accessChain.indexChain.cbegin(); it != accessChain.indexChain.cend(); ++it) {
2813         if (isStructType(type))
2814             type = getContainedTypeId(type, getConstantScalar(*it));
2815         else
2816             type = getContainedTypeId(type);
2817     }
2818 
2819     // dereference swizzle
2820     if (accessChain.swizzle.size() == 1)
2821         type = getContainedTypeId(type);
2822     else if (accessChain.swizzle.size() > 1)
2823         type = makeVectorType(getContainedTypeId(type), (int)accessChain.swizzle.size());
2824 
2825     // dereference component selection
2826     if (accessChain.component)
2827         type = getContainedTypeId(type);
2828 
2829     return type;
2830 }
2831 
dump(std::vector<unsigned int> & out) const2832 void Builder::dump(std::vector<unsigned int>& out) const
2833 {
2834     // Header, before first instructions:
2835     out.push_back(MagicNumber);
2836     out.push_back(spvVersion);
2837     out.push_back(builderNumber);
2838     out.push_back(uniqueId + 1);
2839     out.push_back(0);
2840 
2841     // Capabilities
2842     for (auto it = capabilities.cbegin(); it != capabilities.cend(); ++it) {
2843         Instruction capInst(0, 0, OpCapability);
2844         capInst.addImmediateOperand(*it);
2845         capInst.dump(out);
2846     }
2847 
2848     for (auto it = extensions.cbegin(); it != extensions.cend(); ++it) {
2849         Instruction extInst(0, 0, OpExtension);
2850         extInst.addStringOperand(it->c_str());
2851         extInst.dump(out);
2852     }
2853 
2854     dumpInstructions(out, imports);
2855     Instruction memInst(0, 0, OpMemoryModel);
2856     memInst.addImmediateOperand(addressModel);
2857     memInst.addImmediateOperand(memoryModel);
2858     memInst.dump(out);
2859 
2860     // Instructions saved up while building:
2861     dumpInstructions(out, entryPoints);
2862     dumpInstructions(out, executionModes);
2863 
2864     // Debug instructions
2865     dumpInstructions(out, strings);
2866     dumpSourceInstructions(out);
2867     for (int e = 0; e < (int)sourceExtensions.size(); ++e) {
2868         Instruction sourceExtInst(0, 0, OpSourceExtension);
2869         sourceExtInst.addStringOperand(sourceExtensions[e]);
2870         sourceExtInst.dump(out);
2871     }
2872     dumpInstructions(out, names);
2873     dumpModuleProcesses(out);
2874 
2875     // Annotation instructions
2876     dumpInstructions(out, decorations);
2877 
2878     dumpInstructions(out, constantsTypesGlobals);
2879     dumpInstructions(out, externals);
2880 
2881     // The functions
2882     module.dump(out);
2883 }
2884 
2885 //
2886 // Protected methods.
2887 //
2888 
2889 // Turn the described access chain in 'accessChain' into an instruction(s)
2890 // computing its address.  This *cannot* include complex swizzles, which must
2891 // be handled after this is called.
2892 //
2893 // Can generate code.
collapseAccessChain()2894 Id Builder::collapseAccessChain()
2895 {
2896     assert(accessChain.isRValue == false);
2897 
2898     // did we already emit an access chain for this?
2899     if (accessChain.instr != NoResult)
2900         return accessChain.instr;
2901 
2902     // If we have a dynamic component, we can still transfer
2903     // that into a final operand to the access chain.  We need to remap the
2904     // dynamic component through the swizzle to get a new dynamic component to
2905     // update.
2906     //
2907     // This was not done in transferAccessChainSwizzle() because it might
2908     // generate code.
2909     remapDynamicSwizzle();
2910     if (accessChain.component != NoResult) {
2911         // transfer the dynamic component to the access chain
2912         accessChain.indexChain.push_back(accessChain.component);
2913         accessChain.component = NoResult;
2914     }
2915 
2916     // note that non-trivial swizzling is left pending
2917 
2918     // do we have an access chain?
2919     if (accessChain.indexChain.size() == 0)
2920         return accessChain.base;
2921 
2922     // emit the access chain
2923     StorageClass storageClass = (StorageClass)module.getStorageClass(getTypeId(accessChain.base));
2924     accessChain.instr = createAccessChain(storageClass, accessChain.base, accessChain.indexChain);
2925 
2926     return accessChain.instr;
2927 }
2928 
2929 // For a dynamic component selection of a swizzle.
2930 //
2931 // Turn the swizzle and dynamic component into just a dynamic component.
2932 //
2933 // Generates code.
remapDynamicSwizzle()2934 void Builder::remapDynamicSwizzle()
2935 {
2936     // do we have a swizzle to remap a dynamic component through?
2937     if (accessChain.component != NoResult && accessChain.swizzle.size() > 1) {
2938         // build a vector of the swizzle for the component to map into
2939         std::vector<Id> components;
2940         for (int c = 0; c < (int)accessChain.swizzle.size(); ++c)
2941             components.push_back(makeUintConstant(accessChain.swizzle[c]));
2942         Id mapType = makeVectorType(makeUintType(32), (int)accessChain.swizzle.size());
2943         Id map = makeCompositeConstant(mapType, components);
2944 
2945         // use it
2946         accessChain.component = createVectorExtractDynamic(map, makeUintType(32), accessChain.component);
2947         accessChain.swizzle.clear();
2948     }
2949 }
2950 
2951 // clear out swizzle if it is redundant, that is reselecting the same components
2952 // that would be present without the swizzle.
simplifyAccessChainSwizzle()2953 void Builder::simplifyAccessChainSwizzle()
2954 {
2955     // If the swizzle has fewer components than the vector, it is subsetting, and must stay
2956     // to preserve that fact.
2957     if (getNumTypeComponents(accessChain.preSwizzleBaseType) > (int)accessChain.swizzle.size())
2958         return;
2959 
2960     // if components are out of order, it is a swizzle
2961     for (unsigned int i = 0; i < accessChain.swizzle.size(); ++i) {
2962         if (i != accessChain.swizzle[i])
2963             return;
2964     }
2965 
2966     // otherwise, there is no need to track this swizzle
2967     accessChain.swizzle.clear();
2968     if (accessChain.component == NoResult)
2969         accessChain.preSwizzleBaseType = NoType;
2970 }
2971 
2972 // To the extent any swizzling can become part of the chain
2973 // of accesses instead of a post operation, make it so.
2974 // If 'dynamic' is true, include transferring the dynamic component,
2975 // otherwise, leave it pending.
2976 //
2977 // Does not generate code. just updates the access chain.
transferAccessChainSwizzle(bool dynamic)2978 void Builder::transferAccessChainSwizzle(bool dynamic)
2979 {
2980     // non existent?
2981     if (accessChain.swizzle.size() == 0 && accessChain.component == NoResult)
2982         return;
2983 
2984     // too complex?
2985     // (this requires either a swizzle, or generating code for a dynamic component)
2986     if (accessChain.swizzle.size() > 1)
2987         return;
2988 
2989     // single component, either in the swizzle and/or dynamic component
2990     if (accessChain.swizzle.size() == 1) {
2991         assert(accessChain.component == NoResult);
2992         // handle static component selection
2993         accessChain.indexChain.push_back(makeUintConstant(accessChain.swizzle.front()));
2994         accessChain.swizzle.clear();
2995         accessChain.preSwizzleBaseType = NoType;
2996     } else if (dynamic && accessChain.component != NoResult) {
2997         assert(accessChain.swizzle.size() == 0);
2998         // handle dynamic component
2999         accessChain.indexChain.push_back(accessChain.component);
3000         accessChain.preSwizzleBaseType = NoType;
3001         accessChain.component = NoResult;
3002     }
3003 }
3004 
3005 // Utility method for creating a new block and setting the insert point to
3006 // be in it. This is useful for flow-control operations that need a "dummy"
3007 // block proceeding them (e.g. instructions after a discard, etc).
createAndSetNoPredecessorBlock(const char *)3008 void Builder::createAndSetNoPredecessorBlock(const char* /*name*/)
3009 {
3010     Block* block = new Block(getUniqueId(), buildPoint->getParent());
3011     block->setUnreachable();
3012     buildPoint->getParent().addBlock(block);
3013     setBuildPoint(block);
3014 
3015     // if (name)
3016     //    addName(block->getId(), name);
3017 }
3018 
3019 // Comments in header
createBranch(Block * block)3020 void Builder::createBranch(Block* block)
3021 {
3022     Instruction* branch = new Instruction(OpBranch);
3023     branch->addIdOperand(block->getId());
3024     buildPoint->addInstruction(std::unique_ptr<Instruction>(branch));
3025     block->addPredecessor(buildPoint);
3026 }
3027 
createSelectionMerge(Block * mergeBlock,unsigned int control)3028 void Builder::createSelectionMerge(Block* mergeBlock, unsigned int control)
3029 {
3030     Instruction* merge = new Instruction(OpSelectionMerge);
3031     merge->addIdOperand(mergeBlock->getId());
3032     merge->addImmediateOperand(control);
3033     buildPoint->addInstruction(std::unique_ptr<Instruction>(merge));
3034 }
3035 
createLoopMerge(Block * mergeBlock,Block * continueBlock,unsigned int control,const std::vector<unsigned int> & operands)3036 void Builder::createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control,
3037                               const std::vector<unsigned int>& operands)
3038 {
3039     Instruction* merge = new Instruction(OpLoopMerge);
3040     merge->addIdOperand(mergeBlock->getId());
3041     merge->addIdOperand(continueBlock->getId());
3042     merge->addImmediateOperand(control);
3043     for (int op = 0; op < (int)operands.size(); ++op)
3044         merge->addImmediateOperand(operands[op]);
3045     buildPoint->addInstruction(std::unique_ptr<Instruction>(merge));
3046 }
3047 
createConditionalBranch(Id condition,Block * thenBlock,Block * elseBlock)3048 void Builder::createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock)
3049 {
3050     Instruction* branch = new Instruction(OpBranchConditional);
3051     branch->addIdOperand(condition);
3052     branch->addIdOperand(thenBlock->getId());
3053     branch->addIdOperand(elseBlock->getId());
3054     buildPoint->addInstruction(std::unique_ptr<Instruction>(branch));
3055     thenBlock->addPredecessor(buildPoint);
3056     elseBlock->addPredecessor(buildPoint);
3057 }
3058 
3059 // OpSource
3060 // [OpSourceContinued]
3061 // ...
dumpSourceInstructions(const spv::Id fileId,const std::string & text,std::vector<unsigned int> & out) const3062 void Builder::dumpSourceInstructions(const spv::Id fileId, const std::string& text,
3063                                      std::vector<unsigned int>& out) const
3064 {
3065     const int maxWordCount = 0xFFFF;
3066     const int opSourceWordCount = 4;
3067     const int nonNullBytesPerInstruction = 4 * (maxWordCount - opSourceWordCount) - 1;
3068 
3069     if (source != SourceLanguageUnknown) {
3070         // OpSource Language Version File Source
3071         Instruction sourceInst(NoResult, NoType, OpSource);
3072         sourceInst.addImmediateOperand(source);
3073         sourceInst.addImmediateOperand(sourceVersion);
3074         // File operand
3075         if (fileId != NoResult) {
3076             sourceInst.addIdOperand(fileId);
3077             // Source operand
3078             if (text.size() > 0) {
3079                 int nextByte = 0;
3080                 std::string subString;
3081                 while ((int)text.size() - nextByte > 0) {
3082                     subString = text.substr(nextByte, nonNullBytesPerInstruction);
3083                     if (nextByte == 0) {
3084                         // OpSource
3085                         sourceInst.addStringOperand(subString.c_str());
3086                         sourceInst.dump(out);
3087                     } else {
3088                         // OpSourcContinued
3089                         Instruction sourceContinuedInst(OpSourceContinued);
3090                         sourceContinuedInst.addStringOperand(subString.c_str());
3091                         sourceContinuedInst.dump(out);
3092                     }
3093                     nextByte += nonNullBytesPerInstruction;
3094                 }
3095             } else
3096                 sourceInst.dump(out);
3097         } else
3098             sourceInst.dump(out);
3099     }
3100 }
3101 
3102 // Dump an OpSource[Continued] sequence for the source and every include file
dumpSourceInstructions(std::vector<unsigned int> & out) const3103 void Builder::dumpSourceInstructions(std::vector<unsigned int>& out) const
3104 {
3105     dumpSourceInstructions(sourceFileStringId, sourceText, out);
3106     for (auto iItr = includeFiles.begin(); iItr != includeFiles.end(); ++iItr)
3107         dumpSourceInstructions(iItr->first, *iItr->second, out);
3108 }
3109 
dumpInstructions(std::vector<unsigned int> & out,const std::vector<std::unique_ptr<Instruction>> & instructions) const3110 void Builder::dumpInstructions(std::vector<unsigned int>& out,
3111     const std::vector<std::unique_ptr<Instruction> >& instructions) const
3112 {
3113     for (int i = 0; i < (int)instructions.size(); ++i) {
3114         instructions[i]->dump(out);
3115     }
3116 }
3117 
dumpModuleProcesses(std::vector<unsigned int> & out) const3118 void Builder::dumpModuleProcesses(std::vector<unsigned int>& out) const
3119 {
3120     for (int i = 0; i < (int)moduleProcesses.size(); ++i) {
3121         Instruction moduleProcessed(OpModuleProcessed);
3122         moduleProcessed.addStringOperand(moduleProcesses[i]);
3123         moduleProcessed.dump(out);
3124     }
3125 }
3126 
3127 }; // end spv namespace
3128