blob: 4ae28356beaea3774cf2a4e3c5dbc7a5d5726bf1 [file] [log] [blame]
John Kessenich140f3df2015-06-26 16:58:36 -06001//
John Kessenich6c292d32016-02-15 20:58:50 -07002//Copyright (C) 2014-2015 LunarG, Inc.
3//Copyright (C) 2015-2016 Google, Inc.
John Kessenich140f3df2015-06-26 16:58:36 -06004//
5//All rights reserved.
6//
7//Redistribution and use in source and binary forms, with or without
8//modification, are permitted provided that the following conditions
9//are met:
10//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
23//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34//POSSIBILITY OF SUCH DAMAGE.
35
36//
John Kessenich140f3df2015-06-26 16:58:36 -060037// "Builder" is an interface to fully build SPIR-V IR. Allocate one of
38// these to build (a thread safe) internal SPIR-V representation (IR),
39// and then dump it as a binary stream according to the SPIR-V specification.
40//
41// A Builder has a 1:1 relationship with a SPIR-V module.
42//
43
44#pragma once
45#ifndef SpvBuilder_H
46#define SpvBuilder_H
47
Lei Zhang17535f72016-05-04 15:55:59 -040048#include "Logger.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "spirv.hpp"
John Kessenich140f3df2015-06-26 16:58:36 -060050#include "spvIR.h"
51
52#include <algorithm>
John Kessenich140f3df2015-06-26 16:58:36 -060053#include <map>
Lei Zhang09caf122016-05-02 18:11:54 -040054#include <memory>
John Kessenich92187592016-02-01 13:45:25 -070055#include <set>
Lei Zhang09caf122016-05-02 18:11:54 -040056#include <sstream>
57#include <stack>
John Kessenich140f3df2015-06-26 16:58:36 -060058
59namespace spv {
60
61class Builder {
62public:
Lei Zhang17535f72016-05-04 15:55:59 -040063 Builder(unsigned int userNumber, SpvBuildLogger* logger);
John Kessenich140f3df2015-06-26 16:58:36 -060064 virtual ~Builder();
65
66 static const int maxMatrixSize = 4;
67
68 void setSource(spv::SourceLanguage lang, int version)
69 {
70 source = lang;
71 sourceVersion = version;
72 }
73 void addSourceExtension(const char* ext) { extensions.push_back(ext); }
74 Id import(const char*);
75 void setMemoryModel(spv::AddressingModel addr, spv::MemoryModel mem)
76 {
77 addressModel = addr;
78 memoryModel = mem;
79 }
80
John Kessenich92187592016-02-01 13:45:25 -070081 void addCapability(spv::Capability cap) { capabilities.insert(cap); }
John Kessenich5e4b1242015-08-06 22:53:06 -060082
John Kessenich140f3df2015-06-26 16:58:36 -060083 // To get a new <id> for anything needing a new one.
84 Id getUniqueId() { return ++uniqueId; }
85
86 // To get a set of new <id>s, e.g., for a set of function parameters
87 Id getUniqueIds(int numIds)
88 {
89 Id id = uniqueId + 1;
90 uniqueId += numIds;
91 return id;
92 }
93
94 // For creating new types (will return old type if the requested one was already made).
95 Id makeVoidType();
96 Id makeBoolType();
97 Id makePointer(StorageClass, Id type);
98 Id makeIntegerType(int width, bool hasSign); // generic
99 Id makeIntType(int width) { return makeIntegerType(width, true); }
100 Id makeUintType(int width) { return makeIntegerType(width, false); }
101 Id makeFloatType(int width);
John Kessenich32cfd492016-02-02 12:37:46 -0700102 Id makeStructType(const std::vector<Id>& members, const char*);
John Kessenich55e7d112015-11-15 21:33:39 -0700103 Id makeStructResultType(Id type0, Id type1);
John Kessenich140f3df2015-06-26 16:58:36 -0600104 Id makeVectorType(Id component, int size);
105 Id makeMatrixType(Id component, int cols, int rows);
John Kessenich6c292d32016-02-15 20:58:50 -0700106 Id makeArrayType(Id element, Id sizeId, int stride); // 0 stride means no stride decoration
John Kessenichc9a80832015-09-12 12:17:44 -0600107 Id makeRuntimeArray(Id element);
John Kessenich32cfd492016-02-02 12:37:46 -0700108 Id makeFunctionType(Id returnType, const std::vector<Id>& paramTypes);
John Kessenich5e4b1242015-08-06 22:53:06 -0600109 Id makeImageType(Id sampledType, Dim, bool depth, bool arrayed, bool ms, unsigned sampled, ImageFormat format);
John Kessenich55e7d112015-11-15 21:33:39 -0700110 Id makeSamplerType();
John Kessenich5e4b1242015-08-06 22:53:06 -0600111 Id makeSampledImageType(Id imageType);
John Kessenich140f3df2015-06-26 16:58:36 -0600112
113 // For querying about types.
114 Id getTypeId(Id resultId) const { return module.getTypeId(resultId); }
115 Id getDerefTypeId(Id resultId) const;
116 Op getOpCode(Id id) const { return module.getInstruction(id)->getOpCode(); }
117 Op getTypeClass(Id typeId) const { return getOpCode(typeId); }
118 Op getMostBasicTypeClass(Id typeId) const;
119 int getNumComponents(Id resultId) const { return getNumTypeComponents(getTypeId(resultId)); }
John Kessenich22118352015-12-21 20:54:09 -0700120 int getNumTypeConstituents(Id typeId) const;
121 int getNumTypeComponents(Id typeId) const { return getNumTypeConstituents(typeId); }
John Kessenich140f3df2015-06-26 16:58:36 -0600122 Id getScalarTypeId(Id typeId) const;
123 Id getContainedTypeId(Id typeId) const;
124 Id getContainedTypeId(Id typeId, int) const;
John Kessenich55e7d112015-11-15 21:33:39 -0700125 StorageClass getTypeStorageClass(Id typeId) const { return module.getStorageClass(typeId); }
John Kessenich5d0fa972016-02-15 11:57:00 -0700126 ImageFormat getImageTypeFormat(Id typeId) const { return (ImageFormat)module.getInstruction(typeId)->getImmediateOperand(6); }
John Kessenich140f3df2015-06-26 16:58:36 -0600127
John Kessenich33661452015-12-08 19:32:47 -0700128 bool isPointer(Id resultId) const { return isPointerType(getTypeId(resultId)); }
129 bool isScalar(Id resultId) const { return isScalarType(getTypeId(resultId)); }
130 bool isVector(Id resultId) const { return isVectorType(getTypeId(resultId)); }
131 bool isMatrix(Id resultId) const { return isMatrixType(getTypeId(resultId)); }
132 bool isAggregate(Id resultId) const { return isAggregateType(getTypeId(resultId)); }
133 bool isSampledImage(Id resultId) const { return isSampledImageType(getTypeId(resultId)); }
John Kessenich140f3df2015-06-26 16:58:36 -0600134
John Kessenich33661452015-12-08 19:32:47 -0700135 bool isBoolType(Id typeId) const { return groupedTypes[OpTypeBool].size() > 0 && typeId == groupedTypes[OpTypeBool].back()->getResultId(); }
136 bool isPointerType(Id typeId) const { return getTypeClass(typeId) == OpTypePointer; }
137 bool isScalarType(Id typeId) const { return getTypeClass(typeId) == OpTypeFloat || getTypeClass(typeId) == OpTypeInt || getTypeClass(typeId) == OpTypeBool; }
138 bool isVectorType(Id typeId) const { return getTypeClass(typeId) == OpTypeVector; }
139 bool isMatrixType(Id typeId) const { return getTypeClass(typeId) == OpTypeMatrix; }
140 bool isStructType(Id typeId) const { return getTypeClass(typeId) == OpTypeStruct; }
141 bool isArrayType(Id typeId) const { return getTypeClass(typeId) == OpTypeArray; }
142 bool isAggregateType(Id typeId) const { return isArrayType(typeId) || isStructType(typeId); }
143 bool isImageType(Id typeId) const { return getTypeClass(typeId) == OpTypeImage; }
144 bool isSamplerType(Id typeId) const { return getTypeClass(typeId) == OpTypeSampler; }
145 bool isSampledImageType(Id typeId) const { return getTypeClass(typeId) == OpTypeSampledImage; }
John Kessenich140f3df2015-06-26 16:58:36 -0600146
John Kessenich71631272015-10-13 10:39:19 -0600147 bool isConstantOpCode(Op opcode) const;
qining27e04a02016-04-14 16:40:20 -0400148 bool isSpecConstantOpCode(Op opcode) const;
John Kessenich71631272015-10-13 10:39:19 -0600149 bool isConstant(Id resultId) const { return isConstantOpCode(getOpCode(resultId)); }
John Kessenich140f3df2015-06-26 16:58:36 -0600150 bool isConstantScalar(Id resultId) const { return getOpCode(resultId) == OpConstant; }
qining27e04a02016-04-14 16:40:20 -0400151 bool isSpecConstant(Id resultId) const { return isSpecConstantOpCode(getOpCode(resultId)); }
John Kessenich140f3df2015-06-26 16:58:36 -0600152 unsigned int getConstantScalar(Id resultId) const { return module.getInstruction(resultId)->getImmediateOperand(0); }
John Kessenich55e7d112015-11-15 21:33:39 -0700153 StorageClass getStorageClass(Id resultId) const { return getTypeStorageClass(getTypeId(resultId)); }
John Kessenich140f3df2015-06-26 16:58:36 -0600154
155 int getTypeNumColumns(Id typeId) const
156 {
157 assert(isMatrixType(typeId));
John Kessenich22118352015-12-21 20:54:09 -0700158 return getNumTypeConstituents(typeId);
John Kessenich140f3df2015-06-26 16:58:36 -0600159 }
160 int getNumColumns(Id resultId) const { return getTypeNumColumns(getTypeId(resultId)); }
161 int getTypeNumRows(Id typeId) const
162 {
163 assert(isMatrixType(typeId));
164 return getNumTypeComponents(getContainedTypeId(typeId));
165 }
166 int getNumRows(Id resultId) const { return getTypeNumRows(getTypeId(resultId)); }
167
John Kessenich5e4b1242015-08-06 22:53:06 -0600168 Dim getTypeDimensionality(Id typeId) const
John Kessenich140f3df2015-06-26 16:58:36 -0600169 {
John Kessenich5e4b1242015-08-06 22:53:06 -0600170 assert(isImageType(typeId));
171 return (Dim)module.getInstruction(typeId)->getImmediateOperand(1);
John Kessenich140f3df2015-06-26 16:58:36 -0600172 }
John Kessenich5e4b1242015-08-06 22:53:06 -0600173 Id getImageType(Id resultId) const
John Kessenich140f3df2015-06-26 16:58:36 -0600174 {
Rex Xu6b86d492015-09-16 17:48:22 +0800175 Id typeId = getTypeId(resultId);
176 assert(isImageType(typeId) || isSampledImageType(typeId));
177 return isSampledImageType(typeId) ? module.getInstruction(typeId)->getIdOperand(0) : typeId;
John Kessenich5e4b1242015-08-06 22:53:06 -0600178 }
179 bool isArrayedImageType(Id typeId) const
180 {
181 assert(isImageType(typeId));
182 return module.getInstruction(typeId)->getImmediateOperand(3) != 0;
John Kessenich140f3df2015-06-26 16:58:36 -0600183 }
184
185 // For making new constants (will return old constant if the requested one was already made).
John Kessenich55e7d112015-11-15 21:33:39 -0700186 Id makeBoolConstant(bool b, bool specConstant = false);
187 Id makeIntConstant(int i, bool specConstant = false) { return makeIntConstant(makeIntType(32), (unsigned)i, specConstant); }
188 Id makeUintConstant(unsigned u, bool specConstant = false) { return makeIntConstant(makeUintType(32), u, specConstant); }
Rex Xu8ff43de2016-04-22 16:51:45 +0800189 Id makeInt64Constant(long long i, bool specConstant = false) { return makeInt64Constant(makeIntType(64), (unsigned long long)i, specConstant); }
190 Id makeUint64Constant(unsigned long long u, bool specConstant = false) { return makeInt64Constant(makeUintType(64), u, specConstant); }
John Kessenich55e7d112015-11-15 21:33:39 -0700191 Id makeFloatConstant(float f, bool specConstant = false);
192 Id makeDoubleConstant(double d, bool specConstant = false);
John Kessenich140f3df2015-06-26 16:58:36 -0600193
194 // Turn the array of constants into a proper spv constant of the requested type.
John Kessenich6c292d32016-02-15 20:58:50 -0700195 Id makeCompositeConstant(Id type, std::vector<Id>& comps, bool specConst = false);
John Kessenich140f3df2015-06-26 16:58:36 -0600196
197 // Methods for adding information outside the CFG.
John Kessenich55e7d112015-11-15 21:33:39 -0700198 Instruction* addEntryPoint(ExecutionModel, Function*, const char* name);
John Kessenichb56a26a2015-09-16 16:04:05 -0600199 void addExecutionMode(Function*, ExecutionMode mode, int value1 = -1, int value2 = -1, int value3 = -1);
John Kessenich140f3df2015-06-26 16:58:36 -0600200 void addName(Id, const char* name);
201 void addMemberName(Id, int member, const char* name);
202 void addLine(Id target, Id fileName, int line, int column);
203 void addDecoration(Id, Decoration, int num = -1);
204 void addMemberDecoration(Id, unsigned int member, Decoration, int num = -1);
205
206 // At the end of what block do the next create*() instructions go?
207 void setBuildPoint(Block* bp) { buildPoint = bp; }
208 Block* getBuildPoint() const { return buildPoint; }
209
John Kessenich4d65ee32016-03-12 18:17:47 -0700210 // Make the entry-point function. The returned pointer is only valid
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500211 // for the lifetime of this builder.
John Kessenich4d65ee32016-03-12 18:17:47 -0700212 Function* makeEntrypoint(const char*);
John Kessenich140f3df2015-06-26 16:58:36 -0600213
John Kessenich140f3df2015-06-26 16:58:36 -0600214 // Make a shader-style function, and create its entry block if entry is non-zero.
215 // Return the function, pass back the entry.
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500216 // The returned pointer is only valid for the lifetime of this builder.
John Kessenich32cfd492016-02-02 12:37:46 -0700217 Function* makeFunctionEntry(Decoration precision, Id returnType, const char* name, const std::vector<Id>& paramTypes,
218 const std::vector<Decoration>& precisions, Block **entry = 0);
John Kessenich140f3df2015-06-26 16:58:36 -0600219
John Kesseniche770b3e2015-09-14 20:58:02 -0600220 // Create a return. An 'implicit' return is one not appearing in the source
221 // code. In the case of an implicit return, no post-return block is inserted.
222 void makeReturn(bool implicit, Id retVal = 0);
John Kessenich140f3df2015-06-26 16:58:36 -0600223
224 // Generate all the code needed to finish up a function.
John Kesseniche770b3e2015-09-14 20:58:02 -0600225 void leaveFunction();
John Kessenich140f3df2015-06-26 16:58:36 -0600226
227 // Create a discard.
228 void makeDiscard();
229
230 // Create a global or function local or IO variable.
231 Id createVariable(StorageClass, Id type, const char* name = 0);
232
John Kessenich32cfd492016-02-02 12:37:46 -0700233 // Create an intermediate with an undefined value.
Miro Knejp28f9b1c2015-08-11 02:45:24 +0200234 Id createUndefined(Id type);
235
John Kessenich140f3df2015-06-26 16:58:36 -0600236 // Store into an Id and return the l-value
237 void createStore(Id rValue, Id lValue);
238
239 // Load from an Id and return it
240 Id createLoad(Id lValue);
241
242 // Create an OpAccessChain instruction
243 Id createAccessChain(StorageClass, Id base, std::vector<Id>& offsets);
244
John Kessenichee21fc92015-09-21 21:50:29 -0600245 // Create an OpArrayLength instruction
246 Id createArrayLength(Id base, unsigned int member);
247
John Kessenich140f3df2015-06-26 16:58:36 -0600248 // Create an OpCompositeExtract instruction
249 Id createCompositeExtract(Id composite, Id typeId, unsigned index);
250 Id createCompositeExtract(Id composite, Id typeId, std::vector<unsigned>& indexes);
251 Id createCompositeInsert(Id object, Id composite, Id typeId, unsigned index);
252 Id createCompositeInsert(Id object, Id composite, Id typeId, std::vector<unsigned>& indexes);
253
254 Id createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex);
255 Id createVectorInsertDynamic(Id vector, Id typeId, Id component, Id componentIndex);
256
257 void createNoResultOp(Op);
258 void createNoResultOp(Op, Id operand);
Rex Xufc618912015-09-09 16:42:49 +0800259 void createNoResultOp(Op, const std::vector<Id>& operands);
John Kessenich5e4b1242015-08-06 22:53:06 -0600260 void createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask);
John Kessenich140f3df2015-06-26 16:58:36 -0600261 void createMemoryBarrier(unsigned executionScope, unsigned memorySemantics);
262 Id createUnaryOp(Op, Id typeId, Id operand);
263 Id createBinOp(Op, Id typeId, Id operand1, Id operand2);
264 Id createTriOp(Op, Id typeId, Id operand1, Id operand2, Id operand3);
John Kessenich5e4b1242015-08-06 22:53:06 -0600265 Id createOp(Op, Id typeId, const std::vector<Id>& operands);
John Kessenich140f3df2015-06-26 16:58:36 -0600266 Id createFunctionCall(spv::Function*, std::vector<spv::Id>&);
qining13545202016-03-21 09:51:37 -0400267 Id createSpecConstantOp(Op, Id typeId, const std::vector<spv::Id>& operands, const std::vector<unsigned>& literals);
John Kessenich140f3df2015-06-26 16:58:36 -0600268
269 // Take an rvalue (source) and a set of channels to extract from it to
270 // make a new rvalue, which is returned.
John Kessenich32cfd492016-02-02 12:37:46 -0700271 Id createRvalueSwizzle(Decoration precision, Id typeId, Id source, std::vector<unsigned>& channels);
John Kessenich140f3df2015-06-26 16:58:36 -0600272
273 // Take a copy of an lvalue (target) and a source of components, and set the
274 // source components into the lvalue where the 'channels' say to put them.
275 // An updated version of the target is returned.
276 // (No true lvalue or stores are used.)
277 Id createLvalueSwizzle(Id typeId, Id target, Id source, std::vector<unsigned>& channels);
278
John Kessenich32cfd492016-02-02 12:37:46 -0700279 // If both the id and precision are valid, the id
280 // gets tagged with the requested precision.
281 // The passed in id is always the returned id, to simplify use patterns.
282 Id setPrecision(Id id, Decoration precision)
John Kessenich140f3df2015-06-26 16:58:36 -0600283 {
John Kessenich32cfd492016-02-02 12:37:46 -0700284 if (precision != NoPrecision && id != NoResult)
285 addDecoration(id, precision);
286
287 return id;
John Kessenich140f3df2015-06-26 16:58:36 -0600288 }
289
290 // Can smear a scalar to a vector for the following forms:
291 // - promoteScalar(scalar, vector) // smear scalar to width of vector
292 // - promoteScalar(vector, scalar) // smear scalar to width of vector
293 // - promoteScalar(pointer, scalar) // smear scalar to width of what pointer points to
294 // - promoteScalar(scalar, scalar) // do nothing
295 // Other forms are not allowed.
296 //
John Kessenich76f71392015-12-09 19:08:42 -0700297 // Generally, the type of 'scalar' does not need to be the same type as the components in 'vector'.
298 // The type of the created vector is a vector of components of the same type as the scalar.
299 //
John Kessenich140f3df2015-06-26 16:58:36 -0600300 // Note: One of the arguments will change, with the result coming back that way rather than
301 // through the return value.
302 void promoteScalar(Decoration precision, Id& left, Id& right);
303
John Kessenich76f71392015-12-09 19:08:42 -0700304 // Make a value by smearing the scalar to fill the type.
305 // vectorType should be the correct type for making a vector of scalarVal.
306 // (No conversions are done.)
307 Id smearScalar(Decoration precision, Id scalarVal, Id vectorType);
John Kessenich140f3df2015-06-26 16:58:36 -0600308
309 // Create a call to a built-in function.
John Kessenich32cfd492016-02-02 12:37:46 -0700310 Id createBuiltinCall(Id resultType, Id builtins, int entryPoint, std::vector<Id>& args);
John Kessenich140f3df2015-06-26 16:58:36 -0600311
312 // List of parameters used to create a texture operation
313 struct TextureParameters {
314 Id sampler;
315 Id coords;
316 Id bias;
317 Id lod;
318 Id Dref;
319 Id offset;
John Kessenich5e4b1242015-08-06 22:53:06 -0600320 Id offsets;
John Kessenich140f3df2015-06-26 16:58:36 -0600321 Id gradX;
322 Id gradY;
John Kessenich5e4b1242015-08-06 22:53:06 -0600323 Id sample;
John Kessenich76d4dfc2016-06-16 12:43:23 -0600324 Id component;
Rex Xu48edadf2015-12-31 16:11:41 +0800325 Id texelOut;
326 Id lodClamp;
John Kessenich140f3df2015-06-26 16:58:36 -0600327 };
328
329 // Select the correct texture operation based on all inputs, and emit the correct instruction
John Kessenich019f08f2016-02-15 15:40:42 -0700330 Id createTextureCall(Decoration precision, Id resultType, bool sparse, bool fetch, bool proj, bool gather, bool noImplicit, const TextureParameters&);
John Kessenich140f3df2015-06-26 16:58:36 -0600331
332 // Emit the OpTextureQuery* instruction that was passed in.
333 // Figure out the right return value and type, and return it.
334 Id createTextureQueryCall(Op, const TextureParameters&);
335
336 Id createSamplePositionCall(Decoration precision, Id, Id);
337
338 Id createBitFieldExtractCall(Decoration precision, Id, Id, Id, bool isSigned);
339 Id createBitFieldInsertCall(Decoration precision, Id, Id, Id, Id);
340
John Kessenich32cfd492016-02-02 12:37:46 -0700341 // Reduction comparison for composites: For equal and not-equal resulting in a scalar.
John Kessenich22118352015-12-21 20:54:09 -0700342 Id createCompositeCompare(Decoration precision, Id, Id, bool /* true if for equal, false if for not-equal */);
John Kessenich140f3df2015-06-26 16:58:36 -0600343
344 // OpCompositeConstruct
345 Id createCompositeConstruct(Id typeId, std::vector<Id>& constituents);
346
347 // vector or scalar constructor
348 Id createConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId);
349
350 // matrix constructor
351 Id createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id constructee);
352
353 // Helper to use for building nested control flow with if-then-else.
354 class If {
355 public:
356 If(Id condition, Builder& builder);
357 ~If() {}
358
359 void makeBeginElse();
360 void makeEndIf();
361
362 private:
363 If(const If&);
364 If& operator=(If&);
365
366 Builder& builder;
367 Id condition;
368 Function* function;
369 Block* headerBlock;
370 Block* thenBlock;
371 Block* elseBlock;
372 Block* mergeBlock;
373 };
374
375 // Make a switch statement. A switch has 'numSegments' of pieces of code, not containing
376 // any case/default labels, all separated by one or more case/default labels. Each possible
377 // case value v is a jump to the caseValues[v] segment. The defaultSegment is also in this
378 // number space. How to compute the value is given by 'condition', as in switch(condition).
379 //
380 // The SPIR-V Builder will maintain the stack of post-switch merge blocks for nested switches.
381 //
382 // Use a defaultSegment < 0 if there is no default segment (to branch to post switch).
383 //
384 // Returns the right set of basic blocks to start each code segment with, so that the caller's
385 // recursion stack can hold the memory for it.
386 //
387 void makeSwitch(Id condition, int numSegments, std::vector<int>& caseValues, std::vector<int>& valueToSegment, int defaultSegment,
388 std::vector<Block*>& segmentBB); // return argument
389
390 // Add a branch to the innermost switch's merge block.
391 void addSwitchBreak();
392
393 // Move to the next code segment, passing in the return argument in makeSwitch()
394 void nextSwitchSegment(std::vector<Block*>& segmentBB, int segment);
395
396 // Finish off the innermost switch.
397 void endSwitch(std::vector<Block*>& segmentBB);
398
Dejan Mircevski9c6734c2016-01-10 12:15:13 -0500399 struct LoopBlocks {
Dejan Mircevski832c65c2016-01-11 15:57:11 -0500400 Block &head, &body, &merge, &continue_target;
Dejan Mircevski9c6734c2016-01-10 12:15:13 -0500401 };
Dejan Mircevski9c6734c2016-01-10 12:15:13 -0500402
Dejan Mircevski7819bee2016-01-11 09:35:22 -0500403 // Start a new loop and prepare the builder to generate code for it. Until
404 // closeLoop() is called for this loop, createLoopContinue() and
405 // createLoopExit() will target its corresponding blocks.
406 LoopBlocks& makeNewLoop();
407
408 // Create a new block in the function containing the build point. Memory is
409 // owned by the function object.
Dejan Mircevski9c6734c2016-01-10 12:15:13 -0500410 Block& makeNewBlock();
John Kessenich140f3df2015-06-26 16:58:36 -0600411
Dejan Mircevski7819bee2016-01-11 09:35:22 -0500412 // Add a branch to the continue_target of the current (innermost) loop.
John Kessenich140f3df2015-06-26 16:58:36 -0600413 void createLoopContinue();
414
Dejan Mircevski7819bee2016-01-11 09:35:22 -0500415 // Add an exit (e.g. "break") from the innermost loop that we're currently
416 // in.
John Kessenich140f3df2015-06-26 16:58:36 -0600417 void createLoopExit();
418
419 // Close the innermost loop that you're in
420 void closeLoop();
421
422 //
423 // Access chain design for an R-Value vs. L-Value:
424 //
425 // There is a single access chain the builder is building at
426 // any particular time. Such a chain can be used to either to a load or
427 // a store, when desired.
428 //
429 // Expressions can be r-values, l-values, or both, or only r-values:
430 // a[b.c].d = .... // l-value
431 // ... = a[b.c].d; // r-value, that also looks like an l-value
432 // ++a[b.c].d; // r-value and l-value
433 // (x + y)[2]; // r-value only, can't possibly be l-value
434 //
435 // Computing an r-value means generating code. Hence,
436 // r-values should only be computed when they are needed, not speculatively.
437 //
438 // Computing an l-value means saving away information for later use in the compiler,
439 // no code is generated until the l-value is later dereferenced. It is okay
440 // to speculatively generate an l-value, just not okay to speculatively dereference it.
441 //
442 // The base of the access chain (the left-most variable or expression
443 // from which everything is based) can be set either as an l-value
444 // or as an r-value. Most efficient would be to set an l-value if one
445 // is available. If an expression was evaluated, the resulting r-value
446 // can be set as the chain base.
447 //
448 // The users of this single access chain can save and restore if they
449 // want to nest or manage multiple chains.
450 //
451
452 struct AccessChain {
John Kessenich55e7d112015-11-15 21:33:39 -0700453 Id base; // for l-values, pointer to the base object, for r-values, the base object
John Kessenich140f3df2015-06-26 16:58:36 -0600454 std::vector<Id> indexChain;
John Kessenich55e7d112015-11-15 21:33:39 -0700455 Id instr; // cache the instruction that generates this access chain
456 std::vector<unsigned> swizzle; // each std::vector element selects the next GLSL component number
457 Id component; // a dynamic component index, can coexist with a swizzle, done after the swizzle, NoResult if not present
458 Id preSwizzleBaseType; // dereferenced type, before swizzle or component is applied; NoType unless a swizzle or component is present
459 bool isRValue; // true if 'base' is an r-value, otherwise, base is an l-value
John Kessenich140f3df2015-06-26 16:58:36 -0600460 };
461
462 //
463 // the SPIR-V builder maintains a single active chain that
464 // the following methods operated on
465 //
466
467 // for external save and restore
468 AccessChain getAccessChain() { return accessChain; }
469 void setAccessChain(AccessChain newChain) { accessChain = newChain; }
470
471 // clear accessChain
472 void clearAccessChain();
473
474 // set new base as an l-value base
475 void setAccessChainLValue(Id lValue)
476 {
477 assert(isPointer(lValue));
478 accessChain.base = lValue;
John Kessenich140f3df2015-06-26 16:58:36 -0600479 }
480
481 // set new base value as an r-value
482 void setAccessChainRValue(Id rValue)
483 {
484 accessChain.isRValue = true;
485 accessChain.base = rValue;
John Kessenich140f3df2015-06-26 16:58:36 -0600486 }
487
488 // push offset onto the end of the chain
John Kessenichfa668da2015-09-13 14:46:30 -0600489 void accessChainPush(Id offset)
John Kessenich140f3df2015-06-26 16:58:36 -0600490 {
491 accessChain.indexChain.push_back(offset);
John Kessenich140f3df2015-06-26 16:58:36 -0600492 }
493
494 // push new swizzle onto the end of any existing swizzle, merging into a single swizzle
John Kessenichfa668da2015-09-13 14:46:30 -0600495 void accessChainPushSwizzle(std::vector<unsigned>& swizzle, Id preSwizzleBaseType);
John Kessenich140f3df2015-06-26 16:58:36 -0600496
497 // push a variable component selection onto the access chain; supporting only one, so unsided
John Kessenichfa668da2015-09-13 14:46:30 -0600498 void accessChainPushComponent(Id component, Id preSwizzleBaseType)
499 {
500 accessChain.component = component;
501 if (accessChain.preSwizzleBaseType == NoType)
502 accessChain.preSwizzleBaseType = preSwizzleBaseType;
503 }
John Kessenich140f3df2015-06-26 16:58:36 -0600504
505 // use accessChain and swizzle to store value
506 void accessChainStore(Id rvalue);
507
508 // use accessChain and swizzle to load an r-value
John Kessenich32cfd492016-02-02 12:37:46 -0700509 Id accessChainLoad(Decoration precision, Id ResultType);
John Kessenich140f3df2015-06-26 16:58:36 -0600510
511 // get the direct pointer for an l-value
512 Id accessChainGetLValue();
513
John Kessenich103bef92016-02-08 21:38:15 -0700514 // Get the inferred SPIR-V type of the result of the current access chain,
515 // based on the type of the base and the chain of dereferences.
516 Id accessChainGetInferredType();
517
qiningda397332016-03-09 19:54:03 -0500518 // Remove OpDecorate instructions whose operands are defined in unreachable
519 // blocks.
520 void eliminateDeadDecorations();
John Kessenich140f3df2015-06-26 16:58:36 -0600521 void dump(std::vector<unsigned int>&) const;
522
Dejan Mircevski9c6734c2016-01-10 12:15:13 -0500523 void createBranch(Block* block);
524 void createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock);
525 void createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control);
526
qining13545202016-03-21 09:51:37 -0400527 // Sets to generate opcode for specialization constants.
528 void setToSpecConstCodeGenMode() { generatingOpCodeForSpecConst = true; }
529 // Sets to generate opcode for non-specialization constants (normal mode).
530 void setToNormalCodeGenMode() { generatingOpCodeForSpecConst = false; }
531 // Check if the builder is generating code for spec constants.
532 bool isInSpecConstCodeGenMode() { return generatingOpCodeForSpecConst; }
533
Dejan Mircevski9c6734c2016-01-10 12:15:13 -0500534 protected:
John Kessenich55e7d112015-11-15 21:33:39 -0700535 Id makeIntConstant(Id typeId, unsigned value, bool specConstant);
Rex Xu8ff43de2016-04-22 16:51:45 +0800536 Id makeInt64Constant(Id typeId, unsigned long long value, bool specConstant);
John Kessenich55e7d112015-11-15 21:33:39 -0700537 Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned value) const;
538 Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned v1, unsigned v2) const;
John Kessenich140f3df2015-06-26 16:58:36 -0600539 Id findCompositeConstant(Op typeClass, std::vector<Id>& comps) const;
540 Id collapseAccessChain();
John Kessenich55e7d112015-11-15 21:33:39 -0700541 void transferAccessChainSwizzle(bool dynamic);
John Kessenich140f3df2015-06-26 16:58:36 -0600542 void simplifyAccessChainSwizzle();
John Kessenich140f3df2015-06-26 16:58:36 -0600543 void createAndSetNoPredecessorBlock(const char*);
John Kessenich55e7d112015-11-15 21:33:39 -0700544 void createSelectionMerge(Block* mergeBlock, unsigned int control);
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500545 void dumpInstructions(std::vector<unsigned int>&, const std::vector<std::unique_ptr<Instruction> >&) const;
John Kessenich140f3df2015-06-26 16:58:36 -0600546
547 SourceLanguage source;
548 int sourceVersion;
549 std::vector<const char*> extensions;
550 AddressingModel addressModel;
551 MemoryModel memoryModel;
John Kessenich92187592016-02-01 13:45:25 -0700552 std::set<spv::Capability> capabilities;
John Kessenich140f3df2015-06-26 16:58:36 -0600553 int builderNumber;
554 Module module;
555 Block* buildPoint;
556 Id uniqueId;
557 Function* mainFunction;
qining13545202016-03-21 09:51:37 -0400558 bool generatingOpCodeForSpecConst;
John Kessenich140f3df2015-06-26 16:58:36 -0600559 AccessChain accessChain;
560
561 // special blocks of instructions for output
Andrew Woloszynb7946d12016-01-18 09:23:56 -0500562 std::vector<std::unique_ptr<Instruction> > imports;
563 std::vector<std::unique_ptr<Instruction> > entryPoints;
564 std::vector<std::unique_ptr<Instruction> > executionModes;
565 std::vector<std::unique_ptr<Instruction> > names;
566 std::vector<std::unique_ptr<Instruction> > lines;
567 std::vector<std::unique_ptr<Instruction> > decorations;
568 std::vector<std::unique_ptr<Instruction> > constantsTypesGlobals;
569 std::vector<std::unique_ptr<Instruction> > externals;
570 std::vector<std::unique_ptr<Function> > functions;
John Kessenich140f3df2015-06-26 16:58:36 -0600571
572 // not output, internally used for quick & dirty canonical (unique) creation
573 std::vector<Instruction*> groupedConstants[OpConstant]; // all types appear before OpConstant
574 std::vector<Instruction*> groupedTypes[OpConstant];
575
576 // stack of switches
577 std::stack<Block*> switchMerges;
578
John Kessenich140f3df2015-06-26 16:58:36 -0600579 // Our loop stack.
Dejan Mircevski7819bee2016-01-11 09:35:22 -0500580 std::stack<LoopBlocks> loops;
Lei Zhang09caf122016-05-02 18:11:54 -0400581
582 // The stream for outputing warnings and errors.
Lei Zhang17535f72016-05-04 15:55:59 -0400583 SpvBuildLogger* logger;
John Kessenich140f3df2015-06-26 16:58:36 -0600584}; // end Builder class
585
John Kessenich140f3df2015-06-26 16:58:36 -0600586}; // end spv namespace
587
588#endif // SpvBuilder_H