2015-06-26 22:58:36 +00:00
|
|
|
//
|
2017-01-06 19:34:14 +00:00
|
|
|
// Copyright (C) 2014-2015 LunarG, Inc.
|
|
|
|
// Copyright (C) 2015-2016 Google, Inc.
|
2015-06-26 22:58:36 +00:00
|
|
|
//
|
2017-01-06 19:34:14 +00:00
|
|
|
// All rights reserved.
|
2015-06-26 22:58:36 +00:00
|
|
|
//
|
2017-01-06 19:34:14 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions
|
|
|
|
// are met:
|
2015-06-26 22:58:36 +00:00
|
|
|
//
|
|
|
|
// Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
//
|
|
|
|
// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
2017-01-06 19:34:14 +00:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// "Builder" is an interface to fully build SPIR-V IR. Allocate one of
|
|
|
|
// these to build (a thread safe) internal SPIR-V representation (IR),
|
|
|
|
// and then dump it as a binary stream according to the SPIR-V specification.
|
|
|
|
//
|
|
|
|
// A Builder has a 1:1 relationship with a SPIR-V module.
|
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#ifndef SpvBuilder_H
|
|
|
|
#define SpvBuilder_H
|
|
|
|
|
2016-05-04 19:55:59 +00:00
|
|
|
#include "Logger.h"
|
2015-08-07 04:53:06 +00:00
|
|
|
#include "spirv.hpp"
|
2015-06-26 22:58:36 +00:00
|
|
|
#include "spvIR.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2016-05-02 22:11:54 +00:00
|
|
|
#include <memory>
|
2016-02-01 20:45:25 +00:00
|
|
|
#include <set>
|
2016-05-02 22:11:54 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <stack>
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
namespace spv {
|
|
|
|
|
|
|
|
class Builder {
|
|
|
|
public:
|
2016-05-04 19:55:59 +00:00
|
|
|
Builder(unsigned int userNumber, SpvBuildLogger* logger);
|
2015-06-26 22:58:36 +00:00
|
|
|
virtual ~Builder();
|
|
|
|
|
|
|
|
static const int maxMatrixSize = 4;
|
|
|
|
|
|
|
|
void setSource(spv::SourceLanguage lang, int version)
|
|
|
|
{
|
|
|
|
source = lang;
|
|
|
|
sourceVersion = version;
|
|
|
|
}
|
2016-05-05 04:30:44 +00:00
|
|
|
void addSourceExtension(const char* ext) { sourceExtensions.push_back(ext); }
|
2016-09-21 10:56:12 +00:00
|
|
|
void addExtension(const char* ext) { extensions.insert(ext); }
|
2015-06-26 22:58:36 +00:00
|
|
|
Id import(const char*);
|
|
|
|
void setMemoryModel(spv::AddressingModel addr, spv::MemoryModel mem)
|
|
|
|
{
|
|
|
|
addressModel = addr;
|
|
|
|
memoryModel = mem;
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:45:25 +00:00
|
|
|
void addCapability(spv::Capability cap) { capabilities.insert(cap); }
|
2015-08-07 04:53:06 +00:00
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
// To get a new <id> for anything needing a new one.
|
|
|
|
Id getUniqueId() { return ++uniqueId; }
|
|
|
|
|
|
|
|
// To get a set of new <id>s, e.g., for a set of function parameters
|
|
|
|
Id getUniqueIds(int numIds)
|
|
|
|
{
|
|
|
|
Id id = uniqueId + 1;
|
|
|
|
uniqueId += numIds;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For creating new types (will return old type if the requested one was already made).
|
|
|
|
Id makeVoidType();
|
|
|
|
Id makeBoolType();
|
|
|
|
Id makePointer(StorageClass, Id type);
|
|
|
|
Id makeIntegerType(int width, bool hasSign); // generic
|
|
|
|
Id makeIntType(int width) { return makeIntegerType(width, true); }
|
|
|
|
Id makeUintType(int width) { return makeIntegerType(width, false); }
|
|
|
|
Id makeFloatType(int width);
|
2016-02-02 19:37:46 +00:00
|
|
|
Id makeStructType(const std::vector<Id>& members, const char*);
|
2015-11-16 04:33:39 +00:00
|
|
|
Id makeStructResultType(Id type0, Id type1);
|
2015-06-26 22:58:36 +00:00
|
|
|
Id makeVectorType(Id component, int size);
|
|
|
|
Id makeMatrixType(Id component, int cols, int rows);
|
2016-02-16 03:58:50 +00:00
|
|
|
Id makeArrayType(Id element, Id sizeId, int stride); // 0 stride means no stride decoration
|
2015-09-12 18:17:44 +00:00
|
|
|
Id makeRuntimeArray(Id element);
|
2016-02-02 19:37:46 +00:00
|
|
|
Id makeFunctionType(Id returnType, const std::vector<Id>& paramTypes);
|
2015-08-07 04:53:06 +00:00
|
|
|
Id makeImageType(Id sampledType, Dim, bool depth, bool arrayed, bool ms, unsigned sampled, ImageFormat format);
|
2015-11-16 04:33:39 +00:00
|
|
|
Id makeSamplerType();
|
2015-08-07 04:53:06 +00:00
|
|
|
Id makeSampledImageType(Id imageType);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// For querying about types.
|
|
|
|
Id getTypeId(Id resultId) const { return module.getTypeId(resultId); }
|
|
|
|
Id getDerefTypeId(Id resultId) const;
|
|
|
|
Op getOpCode(Id id) const { return module.getInstruction(id)->getOpCode(); }
|
|
|
|
Op getTypeClass(Id typeId) const { return getOpCode(typeId); }
|
|
|
|
Op getMostBasicTypeClass(Id typeId) const;
|
|
|
|
int getNumComponents(Id resultId) const { return getNumTypeComponents(getTypeId(resultId)); }
|
2015-12-22 03:54:09 +00:00
|
|
|
int getNumTypeConstituents(Id typeId) const;
|
|
|
|
int getNumTypeComponents(Id typeId) const { return getNumTypeConstituents(typeId); }
|
2015-06-26 22:58:36 +00:00
|
|
|
Id getScalarTypeId(Id typeId) const;
|
|
|
|
Id getContainedTypeId(Id typeId) const;
|
|
|
|
Id getContainedTypeId(Id typeId, int) const;
|
2015-11-16 04:33:39 +00:00
|
|
|
StorageClass getTypeStorageClass(Id typeId) const { return module.getStorageClass(typeId); }
|
2016-02-15 18:57:00 +00:00
|
|
|
ImageFormat getImageTypeFormat(Id typeId) const { return (ImageFormat)module.getInstruction(typeId)->getImmediateOperand(6); }
|
2015-06-26 22:58:36 +00:00
|
|
|
|
2015-12-09 02:32:47 +00:00
|
|
|
bool isPointer(Id resultId) const { return isPointerType(getTypeId(resultId)); }
|
|
|
|
bool isScalar(Id resultId) const { return isScalarType(getTypeId(resultId)); }
|
|
|
|
bool isVector(Id resultId) const { return isVectorType(getTypeId(resultId)); }
|
|
|
|
bool isMatrix(Id resultId) const { return isMatrixType(getTypeId(resultId)); }
|
|
|
|
bool isAggregate(Id resultId) const { return isAggregateType(getTypeId(resultId)); }
|
|
|
|
bool isSampledImage(Id resultId) const { return isSampledImageType(getTypeId(resultId)); }
|
|
|
|
|
|
|
|
bool isBoolType(Id typeId) const { return groupedTypes[OpTypeBool].size() > 0 && typeId == groupedTypes[OpTypeBool].back()->getResultId(); }
|
|
|
|
bool isPointerType(Id typeId) const { return getTypeClass(typeId) == OpTypePointer; }
|
|
|
|
bool isScalarType(Id typeId) const { return getTypeClass(typeId) == OpTypeFloat || getTypeClass(typeId) == OpTypeInt || getTypeClass(typeId) == OpTypeBool; }
|
|
|
|
bool isVectorType(Id typeId) const { return getTypeClass(typeId) == OpTypeVector; }
|
|
|
|
bool isMatrixType(Id typeId) const { return getTypeClass(typeId) == OpTypeMatrix; }
|
|
|
|
bool isStructType(Id typeId) const { return getTypeClass(typeId) == OpTypeStruct; }
|
|
|
|
bool isArrayType(Id typeId) const { return getTypeClass(typeId) == OpTypeArray; }
|
|
|
|
bool isAggregateType(Id typeId) const { return isArrayType(typeId) || isStructType(typeId); }
|
|
|
|
bool isImageType(Id typeId) const { return getTypeClass(typeId) == OpTypeImage; }
|
|
|
|
bool isSamplerType(Id typeId) const { return getTypeClass(typeId) == OpTypeSampler; }
|
|
|
|
bool isSampledImageType(Id typeId) const { return getTypeClass(typeId) == OpTypeSampledImage; }
|
2015-06-26 22:58:36 +00:00
|
|
|
|
2015-10-13 16:39:19 +00:00
|
|
|
bool isConstantOpCode(Op opcode) const;
|
2016-04-14 20:40:20 +00:00
|
|
|
bool isSpecConstantOpCode(Op opcode) const;
|
2015-10-13 16:39:19 +00:00
|
|
|
bool isConstant(Id resultId) const { return isConstantOpCode(getOpCode(resultId)); }
|
2015-06-26 22:58:36 +00:00
|
|
|
bool isConstantScalar(Id resultId) const { return getOpCode(resultId) == OpConstant; }
|
2016-04-14 20:40:20 +00:00
|
|
|
bool isSpecConstant(Id resultId) const { return isSpecConstantOpCode(getOpCode(resultId)); }
|
2015-06-26 22:58:36 +00:00
|
|
|
unsigned int getConstantScalar(Id resultId) const { return module.getInstruction(resultId)->getImmediateOperand(0); }
|
2015-11-16 04:33:39 +00:00
|
|
|
StorageClass getStorageClass(Id resultId) const { return getTypeStorageClass(getTypeId(resultId)); }
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
int getTypeNumColumns(Id typeId) const
|
|
|
|
{
|
|
|
|
assert(isMatrixType(typeId));
|
2015-12-22 03:54:09 +00:00
|
|
|
return getNumTypeConstituents(typeId);
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
|
|
|
int getNumColumns(Id resultId) const { return getTypeNumColumns(getTypeId(resultId)); }
|
|
|
|
int getTypeNumRows(Id typeId) const
|
|
|
|
{
|
|
|
|
assert(isMatrixType(typeId));
|
|
|
|
return getNumTypeComponents(getContainedTypeId(typeId));
|
|
|
|
}
|
|
|
|
int getNumRows(Id resultId) const { return getTypeNumRows(getTypeId(resultId)); }
|
|
|
|
|
2015-08-07 04:53:06 +00:00
|
|
|
Dim getTypeDimensionality(Id typeId) const
|
|
|
|
{
|
|
|
|
assert(isImageType(typeId));
|
|
|
|
return (Dim)module.getInstruction(typeId)->getImmediateOperand(1);
|
|
|
|
}
|
|
|
|
Id getImageType(Id resultId) const
|
2015-06-26 22:58:36 +00:00
|
|
|
{
|
2015-09-16 09:48:22 +00:00
|
|
|
Id typeId = getTypeId(resultId);
|
|
|
|
assert(isImageType(typeId) || isSampledImageType(typeId));
|
|
|
|
return isSampledImageType(typeId) ? module.getInstruction(typeId)->getIdOperand(0) : typeId;
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
2015-08-07 04:53:06 +00:00
|
|
|
bool isArrayedImageType(Id typeId) const
|
2015-06-26 22:58:36 +00:00
|
|
|
{
|
2015-08-07 04:53:06 +00:00
|
|
|
assert(isImageType(typeId));
|
|
|
|
return module.getInstruction(typeId)->getImmediateOperand(3) != 0;
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For making new constants (will return old constant if the requested one was already made).
|
2015-11-16 04:33:39 +00:00
|
|
|
Id makeBoolConstant(bool b, bool specConstant = false);
|
|
|
|
Id makeIntConstant(int i, bool specConstant = false) { return makeIntConstant(makeIntType(32), (unsigned)i, specConstant); }
|
|
|
|
Id makeUintConstant(unsigned u, bool specConstant = false) { return makeIntConstant(makeUintType(32), u, specConstant); }
|
2016-04-22 08:51:45 +00:00
|
|
|
Id makeInt64Constant(long long i, bool specConstant = false) { return makeInt64Constant(makeIntType(64), (unsigned long long)i, specConstant); }
|
|
|
|
Id makeUint64Constant(unsigned long long u, bool specConstant = false) { return makeInt64Constant(makeUintType(64), u, specConstant); }
|
2015-11-16 04:33:39 +00:00
|
|
|
Id makeFloatConstant(float f, bool specConstant = false);
|
|
|
|
Id makeDoubleConstant(double d, bool specConstant = false);
|
Parser: Implement extension GL_AMD_gpu_shader_half_float.
- Add built-in types: float16_t, f16vec, f16mat.
- Add support of half float constant: hf, HF.
- Extend built-in floating-point operators: +, -, *, /, ++, --, +=, -=,
*=, /=, ==, !=, >=, <=, >, <.
- Add support of type conversions: float16_t -> XXX, XXX -> float16_t.
- Add new built-in functions.
2016-07-29 08:00:05 +00:00
|
|
|
#ifdef AMD_EXTENSIONS
|
|
|
|
Id makeFloat16Constant(float f16, bool specConstant = false);
|
|
|
|
#endif
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Turn the array of constants into a proper spv constant of the requested type.
|
2016-02-16 03:58:50 +00:00
|
|
|
Id makeCompositeConstant(Id type, std::vector<Id>& comps, bool specConst = false);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Methods for adding information outside the CFG.
|
2015-11-16 04:33:39 +00:00
|
|
|
Instruction* addEntryPoint(ExecutionModel, Function*, const char* name);
|
2015-09-16 22:04:05 +00:00
|
|
|
void addExecutionMode(Function*, ExecutionMode mode, int value1 = -1, int value2 = -1, int value3 = -1);
|
2015-06-26 22:58:36 +00:00
|
|
|
void addName(Id, const char* name);
|
|
|
|
void addMemberName(Id, int member, const char* name);
|
|
|
|
void addLine(Id target, Id fileName, int line, int column);
|
|
|
|
void addDecoration(Id, Decoration, int num = -1);
|
|
|
|
void addMemberDecoration(Id, unsigned int member, Decoration, int num = -1);
|
|
|
|
|
|
|
|
// At the end of what block do the next create*() instructions go?
|
|
|
|
void setBuildPoint(Block* bp) { buildPoint = bp; }
|
|
|
|
Block* getBuildPoint() const { return buildPoint; }
|
|
|
|
|
2016-03-13 01:17:47 +00:00
|
|
|
// Make the entry-point function. The returned pointer is only valid
|
2016-01-18 14:23:56 +00:00
|
|
|
// for the lifetime of this builder.
|
2016-09-19 22:01:41 +00:00
|
|
|
Function* makeEntryPoint(const char*);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Make a shader-style function, and create its entry block if entry is non-zero.
|
|
|
|
// Return the function, pass back the entry.
|
2016-01-18 14:23:56 +00:00
|
|
|
// The returned pointer is only valid for the lifetime of this builder.
|
2016-02-02 19:37:46 +00:00
|
|
|
Function* makeFunctionEntry(Decoration precision, Id returnType, const char* name, const std::vector<Id>& paramTypes,
|
|
|
|
const std::vector<Decoration>& precisions, Block **entry = 0);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
2015-09-15 02:58:02 +00:00
|
|
|
// Create a return. An 'implicit' return is one not appearing in the source
|
|
|
|
// code. In the case of an implicit return, no post-return block is inserted.
|
|
|
|
void makeReturn(bool implicit, Id retVal = 0);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Generate all the code needed to finish up a function.
|
2015-09-15 02:58:02 +00:00
|
|
|
void leaveFunction();
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Create a discard.
|
|
|
|
void makeDiscard();
|
|
|
|
|
|
|
|
// Create a global or function local or IO variable.
|
|
|
|
Id createVariable(StorageClass, Id type, const char* name = 0);
|
|
|
|
|
2016-02-02 19:37:46 +00:00
|
|
|
// Create an intermediate with an undefined value.
|
2015-08-11 00:45:24 +00:00
|
|
|
Id createUndefined(Id type);
|
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
// Store into an Id and return the l-value
|
|
|
|
void createStore(Id rValue, Id lValue);
|
|
|
|
|
|
|
|
// Load from an Id and return it
|
|
|
|
Id createLoad(Id lValue);
|
|
|
|
|
|
|
|
// Create an OpAccessChain instruction
|
|
|
|
Id createAccessChain(StorageClass, Id base, std::vector<Id>& offsets);
|
|
|
|
|
2015-09-22 03:50:29 +00:00
|
|
|
// Create an OpArrayLength instruction
|
|
|
|
Id createArrayLength(Id base, unsigned int member);
|
|
|
|
|
2015-06-26 22:58:36 +00:00
|
|
|
// Create an OpCompositeExtract instruction
|
|
|
|
Id createCompositeExtract(Id composite, Id typeId, unsigned index);
|
|
|
|
Id createCompositeExtract(Id composite, Id typeId, std::vector<unsigned>& indexes);
|
|
|
|
Id createCompositeInsert(Id object, Id composite, Id typeId, unsigned index);
|
|
|
|
Id createCompositeInsert(Id object, Id composite, Id typeId, std::vector<unsigned>& indexes);
|
|
|
|
|
|
|
|
Id createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex);
|
|
|
|
Id createVectorInsertDynamic(Id vector, Id typeId, Id component, Id componentIndex);
|
|
|
|
|
|
|
|
void createNoResultOp(Op);
|
|
|
|
void createNoResultOp(Op, Id operand);
|
2015-09-09 08:42:49 +00:00
|
|
|
void createNoResultOp(Op, const std::vector<Id>& operands);
|
2015-08-07 04:53:06 +00:00
|
|
|
void createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask);
|
2015-06-26 22:58:36 +00:00
|
|
|
void createMemoryBarrier(unsigned executionScope, unsigned memorySemantics);
|
|
|
|
Id createUnaryOp(Op, Id typeId, Id operand);
|
|
|
|
Id createBinOp(Op, Id typeId, Id operand1, Id operand2);
|
|
|
|
Id createTriOp(Op, Id typeId, Id operand1, Id operand2, Id operand3);
|
2015-08-07 04:53:06 +00:00
|
|
|
Id createOp(Op, Id typeId, const std::vector<Id>& operands);
|
2015-06-26 22:58:36 +00:00
|
|
|
Id createFunctionCall(spv::Function*, std::vector<spv::Id>&);
|
Spec Constant Operations
Approach:
Add a flag in `Builder` to indicate 'spec constant mode' and 'normal
mode'. When the builder is in 'normal mode', nothing changed. When the
builder is in 'spec constant mode', binary, unary and other instruction
creation rountines will be redirected to `createSpecConstantOp()` to
create instrution at module level with `OpSpecConstantOp <original
opcode> <operands>`.
'spec constant mode' should be enabled if and only if we are creating
spec constants. So a flager setter/recover guard is added when handling
binary/unary nodes in `createSpvConstantsFromConstSubTree()`.
Note when handling spec constants which are represented as ConstantUnion
Node, we should not use `OpSpecConstantOp` to initialize the composite
constant, so builder is set to 'normal mode'.
Tests:
Tests are added in Test/spv.specConstantOperations.vert, including:
1) Arithmetic, shift opeations for both scalar and composite type spec constants.
2) Size conversion from/to float and double for both scalar and vector.
3) Bitwise and/or/xor for both scalar and vector.
4) Unary negate/not for both scalar and vector.
5) Vector swizzles.
6) Comparisons for scalars.
7) == and != for composite type spec constants
Issues:
1) To implement == and != for composite type spec constants, the Spec needs
to allow OpAll, OpAny, OpFOrdEqual, OpFUnordEqual, OpOrdNotEqual,
OpFUnordNotEqual. Currently none of them are allowed in the Spec.
2016-03-21 13:51:37 +00:00
|
|
|
Id createSpecConstantOp(Op, Id typeId, const std::vector<spv::Id>& operands, const std::vector<unsigned>& literals);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Take an rvalue (source) and a set of channels to extract from it to
|
|
|
|
// make a new rvalue, which is returned.
|
2016-02-02 19:37:46 +00:00
|
|
|
Id createRvalueSwizzle(Decoration precision, Id typeId, Id source, std::vector<unsigned>& channels);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Take a copy of an lvalue (target) and a source of components, and set the
|
|
|
|
// source components into the lvalue where the 'channels' say to put them.
|
|
|
|
// An updated version of the target is returned.
|
|
|
|
// (No true lvalue or stores are used.)
|
|
|
|
Id createLvalueSwizzle(Id typeId, Id target, Id source, std::vector<unsigned>& channels);
|
|
|
|
|
2016-02-02 19:37:46 +00:00
|
|
|
// If both the id and precision are valid, the id
|
|
|
|
// gets tagged with the requested precision.
|
|
|
|
// The passed in id is always the returned id, to simplify use patterns.
|
|
|
|
Id setPrecision(Id id, Decoration precision)
|
2015-06-26 22:58:36 +00:00
|
|
|
{
|
2016-02-02 19:37:46 +00:00
|
|
|
if (precision != NoPrecision && id != NoResult)
|
|
|
|
addDecoration(id, precision);
|
|
|
|
|
|
|
|
return id;
|
2015-06-26 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Can smear a scalar to a vector for the following forms:
|
|
|
|
// - promoteScalar(scalar, vector) // smear scalar to width of vector
|
|
|
|
// - promoteScalar(vector, scalar) // smear scalar to width of vector
|
|
|
|
// - promoteScalar(pointer, scalar) // smear scalar to width of what pointer points to
|
|
|
|
// - promoteScalar(scalar, scalar) // do nothing
|
|
|
|
// Other forms are not allowed.
|
|
|
|
//
|
2015-12-10 02:08:42 +00:00
|
|
|
// Generally, the type of 'scalar' does not need to be the same type as the components in 'vector'.
|
|
|
|
// The type of the created vector is a vector of components of the same type as the scalar.
|
|
|
|
//
|
2015-06-26 22:58:36 +00:00
|
|
|
// Note: One of the arguments will change, with the result coming back that way rather than
|
|
|
|
// through the return value.
|
|
|
|
void promoteScalar(Decoration precision, Id& left, Id& right);
|
|
|
|
|
2015-12-10 02:08:42 +00:00
|
|
|
// Make a value by smearing the scalar to fill the type.
|
|
|
|
// vectorType should be the correct type for making a vector of scalarVal.
|
|
|
|
// (No conversions are done.)
|
|
|
|
Id smearScalar(Decoration precision, Id scalarVal, Id vectorType);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Create a call to a built-in function.
|
2016-02-02 19:37:46 +00:00
|
|
|
Id createBuiltinCall(Id resultType, Id builtins, int entryPoint, std::vector<Id>& args);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// List of parameters used to create a texture operation
|
|
|
|
struct TextureParameters {
|
|
|
|
Id sampler;
|
|
|
|
Id coords;
|
|
|
|
Id bias;
|
|
|
|
Id lod;
|
|
|
|
Id Dref;
|
|
|
|
Id offset;
|
2015-08-07 04:53:06 +00:00
|
|
|
Id offsets;
|
2015-06-26 22:58:36 +00:00
|
|
|
Id gradX;
|
|
|
|
Id gradY;
|
2015-08-07 04:53:06 +00:00
|
|
|
Id sample;
|
2016-06-16 18:43:23 +00:00
|
|
|
Id component;
|
2015-12-31 08:11:41 +00:00
|
|
|
Id texelOut;
|
|
|
|
Id lodClamp;
|
2015-06-26 22:58:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Select the correct texture operation based on all inputs, and emit the correct instruction
|
2016-02-15 22:40:42 +00:00
|
|
|
Id createTextureCall(Decoration precision, Id resultType, bool sparse, bool fetch, bool proj, bool gather, bool noImplicit, const TextureParameters&);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// Emit the OpTextureQuery* instruction that was passed in.
|
|
|
|
// Figure out the right return value and type, and return it.
|
|
|
|
Id createTextureQueryCall(Op, const TextureParameters&);
|
|
|
|
|
|
|
|
Id createSamplePositionCall(Decoration precision, Id, Id);
|
|
|
|
|
|
|
|
Id createBitFieldExtractCall(Decoration precision, Id, Id, Id, bool isSigned);
|
|
|
|
Id createBitFieldInsertCall(Decoration precision, Id, Id, Id, Id);
|
|
|
|
|
2016-02-02 19:37:46 +00:00
|
|
|
// Reduction comparison for composites: For equal and not-equal resulting in a scalar.
|
2015-12-22 03:54:09 +00:00
|
|
|
Id createCompositeCompare(Decoration precision, Id, Id, bool /* true if for equal, false if for not-equal */);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// OpCompositeConstruct
|
|
|
|
Id createCompositeConstruct(Id typeId, std::vector<Id>& constituents);
|
|
|
|
|
|
|
|
// vector or scalar constructor
|
|
|
|
Id createConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId);
|
|
|
|
|
|
|
|
// matrix constructor
|
|
|
|
Id createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id constructee);
|
|
|
|
|
|
|
|
// Helper to use for building nested control flow with if-then-else.
|
|
|
|
class If {
|
|
|
|
public:
|
|
|
|
If(Id condition, Builder& builder);
|
|
|
|
~If() {}
|
|
|
|
|
|
|
|
void makeBeginElse();
|
|
|
|
void makeEndIf();
|
|
|
|
|
|
|
|
private:
|
|
|
|
If(const If&);
|
|
|
|
If& operator=(If&);
|
|
|
|
|
|
|
|
Builder& builder;
|
|
|
|
Id condition;
|
|
|
|
Function* function;
|
|
|
|
Block* headerBlock;
|
|
|
|
Block* thenBlock;
|
|
|
|
Block* elseBlock;
|
|
|
|
Block* mergeBlock;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make a switch statement. A switch has 'numSegments' of pieces of code, not containing
|
|
|
|
// any case/default labels, all separated by one or more case/default labels. Each possible
|
|
|
|
// case value v is a jump to the caseValues[v] segment. The defaultSegment is also in this
|
|
|
|
// number space. How to compute the value is given by 'condition', as in switch(condition).
|
|
|
|
//
|
|
|
|
// The SPIR-V Builder will maintain the stack of post-switch merge blocks for nested switches.
|
|
|
|
//
|
|
|
|
// Use a defaultSegment < 0 if there is no default segment (to branch to post switch).
|
|
|
|
//
|
|
|
|
// Returns the right set of basic blocks to start each code segment with, so that the caller's
|
|
|
|
// recursion stack can hold the memory for it.
|
|
|
|
//
|
|
|
|
void makeSwitch(Id condition, int numSegments, std::vector<int>& caseValues, std::vector<int>& valueToSegment, int defaultSegment,
|
|
|
|
std::vector<Block*>& segmentBB); // return argument
|
|
|
|
|
|
|
|
// Add a branch to the innermost switch's merge block.
|
|
|
|
void addSwitchBreak();
|
|
|
|
|
|
|
|
// Move to the next code segment, passing in the return argument in makeSwitch()
|
|
|
|
void nextSwitchSegment(std::vector<Block*>& segmentBB, int segment);
|
|
|
|
|
|
|
|
// Finish off the innermost switch.
|
|
|
|
void endSwitch(std::vector<Block*>& segmentBB);
|
|
|
|
|
2016-01-10 17:15:13 +00:00
|
|
|
struct LoopBlocks {
|
2016-07-09 04:09:10 +00:00
|
|
|
LoopBlocks(Block& head, Block& body, Block& merge, Block& continue_target) :
|
|
|
|
head(head), body(body), merge(merge), continue_target(continue_target) { }
|
2016-01-11 20:57:11 +00:00
|
|
|
Block &head, &body, &merge, &continue_target;
|
2016-07-09 04:09:10 +00:00
|
|
|
private:
|
|
|
|
LoopBlocks();
|
|
|
|
LoopBlocks& operator=(const LoopBlocks&);
|
2016-01-10 17:15:13 +00:00
|
|
|
};
|
|
|
|
|
2016-01-11 14:35:22 +00:00
|
|
|
// Start a new loop and prepare the builder to generate code for it. Until
|
|
|
|
// closeLoop() is called for this loop, createLoopContinue() and
|
|
|
|
// createLoopExit() will target its corresponding blocks.
|
|
|
|
LoopBlocks& makeNewLoop();
|
2015-06-26 22:58:36 +00:00
|
|
|
|
2016-01-11 14:35:22 +00:00
|
|
|
// Create a new block in the function containing the build point. Memory is
|
|
|
|
// owned by the function object.
|
|
|
|
Block& makeNewBlock();
|
2015-06-26 22:58:36 +00:00
|
|
|
|
2016-01-11 14:35:22 +00:00
|
|
|
// Add a branch to the continue_target of the current (innermost) loop.
|
2015-06-26 22:58:36 +00:00
|
|
|
void createLoopContinue();
|
|
|
|
|
2016-01-11 14:35:22 +00:00
|
|
|
// Add an exit (e.g. "break") from the innermost loop that we're currently
|
|
|
|
// in.
|
2015-06-26 22:58:36 +00:00
|
|
|
void createLoopExit();
|
|
|
|
|
|
|
|
// Close the innermost loop that you're in
|
|
|
|
void closeLoop();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Access chain design for an R-Value vs. L-Value:
|
|
|
|
//
|
|
|
|
// There is a single access chain the builder is building at
|
|
|
|
// any particular time. Such a chain can be used to either to a load or
|
|
|
|
// a store, when desired.
|
|
|
|
//
|
|
|
|
// Expressions can be r-values, l-values, or both, or only r-values:
|
|
|
|
// a[b.c].d = .... // l-value
|
|
|
|
// ... = a[b.c].d; // r-value, that also looks like an l-value
|
|
|
|
// ++a[b.c].d; // r-value and l-value
|
|
|
|
// (x + y)[2]; // r-value only, can't possibly be l-value
|
|
|
|
//
|
|
|
|
// Computing an r-value means generating code. Hence,
|
|
|
|
// r-values should only be computed when they are needed, not speculatively.
|
|
|
|
//
|
|
|
|
// Computing an l-value means saving away information for later use in the compiler,
|
|
|
|
// no code is generated until the l-value is later dereferenced. It is okay
|
|
|
|
// to speculatively generate an l-value, just not okay to speculatively dereference it.
|
|
|
|
//
|
|
|
|
// The base of the access chain (the left-most variable or expression
|
|
|
|
// from which everything is based) can be set either as an l-value
|
|
|
|
// or as an r-value. Most efficient would be to set an l-value if one
|
|
|
|
// is available. If an expression was evaluated, the resulting r-value
|
|
|
|
// can be set as the chain base.
|
|
|
|
//
|
|
|
|
// The users of this single access chain can save and restore if they
|
|
|
|
// want to nest or manage multiple chains.
|
|
|
|
//
|
|
|
|
|
|
|
|
struct AccessChain {
|
2015-11-16 04:33:39 +00:00
|
|
|
Id base; // for l-values, pointer to the base object, for r-values, the base object
|
2015-06-26 22:58:36 +00:00
|
|
|
std::vector<Id> indexChain;
|
2015-11-16 04:33:39 +00:00
|
|
|
Id instr; // cache the instruction that generates this access chain
|
|
|
|
std::vector<unsigned> swizzle; // each std::vector element selects the next GLSL component number
|
|
|
|
Id component; // a dynamic component index, can coexist with a swizzle, done after the swizzle, NoResult if not present
|
|
|
|
Id preSwizzleBaseType; // dereferenced type, before swizzle or component is applied; NoType unless a swizzle or component is present
|
|
|
|
bool isRValue; // true if 'base' is an r-value, otherwise, base is an l-value
|
2015-06-26 22:58:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// the SPIR-V builder maintains a single active chain that
|
2016-09-01 23:05:23 +00:00
|
|
|
// the following methods operate on
|
2015-06-26 22:58:36 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
// for external save and restore
|
|
|
|
AccessChain getAccessChain() { return accessChain; }
|
|
|
|
void setAccessChain(AccessChain newChain) { accessChain = newChain; }
|
|
|
|
|
|
|
|
// clear accessChain
|
|
|
|
void clearAccessChain();
|
|
|
|
|
|
|
|
// set new base as an l-value base
|
|
|
|
void setAccessChainLValue(Id lValue)
|
|
|
|
{
|
|
|
|
assert(isPointer(lValue));
|
|
|
|
accessChain.base = lValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set new base value as an r-value
|
|
|
|
void setAccessChainRValue(Id rValue)
|
|
|
|
{
|
|
|
|
accessChain.isRValue = true;
|
|
|
|
accessChain.base = rValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// push offset onto the end of the chain
|
2015-09-13 20:46:30 +00:00
|
|
|
void accessChainPush(Id offset)
|
2015-06-26 22:58:36 +00:00
|
|
|
{
|
|
|
|
accessChain.indexChain.push_back(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// push new swizzle onto the end of any existing swizzle, merging into a single swizzle
|
2015-09-13 20:46:30 +00:00
|
|
|
void accessChainPushSwizzle(std::vector<unsigned>& swizzle, Id preSwizzleBaseType);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// push a variable component selection onto the access chain; supporting only one, so unsided
|
2015-09-13 20:46:30 +00:00
|
|
|
void accessChainPushComponent(Id component, Id preSwizzleBaseType)
|
|
|
|
{
|
|
|
|
accessChain.component = component;
|
|
|
|
if (accessChain.preSwizzleBaseType == NoType)
|
|
|
|
accessChain.preSwizzleBaseType = preSwizzleBaseType;
|
|
|
|
}
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// use accessChain and swizzle to store value
|
|
|
|
void accessChainStore(Id rvalue);
|
|
|
|
|
|
|
|
// use accessChain and swizzle to load an r-value
|
2016-02-02 19:37:46 +00:00
|
|
|
Id accessChainLoad(Decoration precision, Id ResultType);
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// get the direct pointer for an l-value
|
|
|
|
Id accessChainGetLValue();
|
|
|
|
|
2016-02-09 04:38:15 +00:00
|
|
|
// Get the inferred SPIR-V type of the result of the current access chain,
|
|
|
|
// based on the type of the base and the chain of dereferences.
|
|
|
|
Id accessChainGetInferredType();
|
|
|
|
|
2016-03-10 00:54:03 +00:00
|
|
|
// Remove OpDecorate instructions whose operands are defined in unreachable
|
|
|
|
// blocks.
|
|
|
|
void eliminateDeadDecorations();
|
2015-06-26 22:58:36 +00:00
|
|
|
void dump(std::vector<unsigned int>&) const;
|
|
|
|
|
2016-01-10 17:15:13 +00:00
|
|
|
void createBranch(Block* block);
|
|
|
|
void createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock);
|
|
|
|
void createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control);
|
|
|
|
|
Spec Constant Operations
Approach:
Add a flag in `Builder` to indicate 'spec constant mode' and 'normal
mode'. When the builder is in 'normal mode', nothing changed. When the
builder is in 'spec constant mode', binary, unary and other instruction
creation rountines will be redirected to `createSpecConstantOp()` to
create instrution at module level with `OpSpecConstantOp <original
opcode> <operands>`.
'spec constant mode' should be enabled if and only if we are creating
spec constants. So a flager setter/recover guard is added when handling
binary/unary nodes in `createSpvConstantsFromConstSubTree()`.
Note when handling spec constants which are represented as ConstantUnion
Node, we should not use `OpSpecConstantOp` to initialize the composite
constant, so builder is set to 'normal mode'.
Tests:
Tests are added in Test/spv.specConstantOperations.vert, including:
1) Arithmetic, shift opeations for both scalar and composite type spec constants.
2) Size conversion from/to float and double for both scalar and vector.
3) Bitwise and/or/xor for both scalar and vector.
4) Unary negate/not for both scalar and vector.
5) Vector swizzles.
6) Comparisons for scalars.
7) == and != for composite type spec constants
Issues:
1) To implement == and != for composite type spec constants, the Spec needs
to allow OpAll, OpAny, OpFOrdEqual, OpFUnordEqual, OpOrdNotEqual,
OpFUnordNotEqual. Currently none of them are allowed in the Spec.
2016-03-21 13:51:37 +00:00
|
|
|
// Sets to generate opcode for specialization constants.
|
|
|
|
void setToSpecConstCodeGenMode() { generatingOpCodeForSpecConst = true; }
|
|
|
|
// Sets to generate opcode for non-specialization constants (normal mode).
|
|
|
|
void setToNormalCodeGenMode() { generatingOpCodeForSpecConst = false; }
|
|
|
|
// Check if the builder is generating code for spec constants.
|
|
|
|
bool isInSpecConstCodeGenMode() { return generatingOpCodeForSpecConst; }
|
|
|
|
|
2016-01-10 17:15:13 +00:00
|
|
|
protected:
|
2015-11-16 04:33:39 +00:00
|
|
|
Id makeIntConstant(Id typeId, unsigned value, bool specConstant);
|
2016-04-22 08:51:45 +00:00
|
|
|
Id makeInt64Constant(Id typeId, unsigned long long value, bool specConstant);
|
2015-11-16 04:33:39 +00:00
|
|
|
Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned value) const;
|
|
|
|
Id findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned v1, unsigned v2) const;
|
2015-06-26 22:58:36 +00:00
|
|
|
Id findCompositeConstant(Op typeClass, std::vector<Id>& comps) const;
|
|
|
|
Id collapseAccessChain();
|
2015-11-16 04:33:39 +00:00
|
|
|
void transferAccessChainSwizzle(bool dynamic);
|
2015-06-26 22:58:36 +00:00
|
|
|
void simplifyAccessChainSwizzle();
|
|
|
|
void createAndSetNoPredecessorBlock(const char*);
|
2015-11-16 04:33:39 +00:00
|
|
|
void createSelectionMerge(Block* mergeBlock, unsigned int control);
|
2016-01-18 14:23:56 +00:00
|
|
|
void dumpInstructions(std::vector<unsigned int>&, const std::vector<std::unique_ptr<Instruction> >&) const;
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
SourceLanguage source;
|
|
|
|
int sourceVersion;
|
2016-09-21 10:56:12 +00:00
|
|
|
std::set<const char*> extensions;
|
2016-05-05 04:30:44 +00:00
|
|
|
std::vector<const char*> sourceExtensions;
|
2015-06-26 22:58:36 +00:00
|
|
|
AddressingModel addressModel;
|
|
|
|
MemoryModel memoryModel;
|
2016-02-01 20:45:25 +00:00
|
|
|
std::set<spv::Capability> capabilities;
|
2015-06-26 22:58:36 +00:00
|
|
|
int builderNumber;
|
|
|
|
Module module;
|
|
|
|
Block* buildPoint;
|
|
|
|
Id uniqueId;
|
2016-11-26 20:31:47 +00:00
|
|
|
Function* entryPointFunction;
|
Spec Constant Operations
Approach:
Add a flag in `Builder` to indicate 'spec constant mode' and 'normal
mode'. When the builder is in 'normal mode', nothing changed. When the
builder is in 'spec constant mode', binary, unary and other instruction
creation rountines will be redirected to `createSpecConstantOp()` to
create instrution at module level with `OpSpecConstantOp <original
opcode> <operands>`.
'spec constant mode' should be enabled if and only if we are creating
spec constants. So a flager setter/recover guard is added when handling
binary/unary nodes in `createSpvConstantsFromConstSubTree()`.
Note when handling spec constants which are represented as ConstantUnion
Node, we should not use `OpSpecConstantOp` to initialize the composite
constant, so builder is set to 'normal mode'.
Tests:
Tests are added in Test/spv.specConstantOperations.vert, including:
1) Arithmetic, shift opeations for both scalar and composite type spec constants.
2) Size conversion from/to float and double for both scalar and vector.
3) Bitwise and/or/xor for both scalar and vector.
4) Unary negate/not for both scalar and vector.
5) Vector swizzles.
6) Comparisons for scalars.
7) == and != for composite type spec constants
Issues:
1) To implement == and != for composite type spec constants, the Spec needs
to allow OpAll, OpAny, OpFOrdEqual, OpFUnordEqual, OpOrdNotEqual,
OpFUnordNotEqual. Currently none of them are allowed in the Spec.
2016-03-21 13:51:37 +00:00
|
|
|
bool generatingOpCodeForSpecConst;
|
2015-06-26 22:58:36 +00:00
|
|
|
AccessChain accessChain;
|
|
|
|
|
|
|
|
// special blocks of instructions for output
|
2016-01-18 14:23:56 +00:00
|
|
|
std::vector<std::unique_ptr<Instruction> > imports;
|
|
|
|
std::vector<std::unique_ptr<Instruction> > entryPoints;
|
|
|
|
std::vector<std::unique_ptr<Instruction> > executionModes;
|
|
|
|
std::vector<std::unique_ptr<Instruction> > names;
|
|
|
|
std::vector<std::unique_ptr<Instruction> > lines;
|
|
|
|
std::vector<std::unique_ptr<Instruction> > decorations;
|
|
|
|
std::vector<std::unique_ptr<Instruction> > constantsTypesGlobals;
|
|
|
|
std::vector<std::unique_ptr<Instruction> > externals;
|
|
|
|
std::vector<std::unique_ptr<Function> > functions;
|
2015-06-26 22:58:36 +00:00
|
|
|
|
|
|
|
// not output, internally used for quick & dirty canonical (unique) creation
|
|
|
|
std::vector<Instruction*> groupedConstants[OpConstant]; // all types appear before OpConstant
|
|
|
|
std::vector<Instruction*> groupedTypes[OpConstant];
|
|
|
|
|
|
|
|
// stack of switches
|
|
|
|
std::stack<Block*> switchMerges;
|
|
|
|
|
|
|
|
// Our loop stack.
|
2016-01-11 14:35:22 +00:00
|
|
|
std::stack<LoopBlocks> loops;
|
2016-05-02 22:11:54 +00:00
|
|
|
|
|
|
|
// The stream for outputing warnings and errors.
|
2016-05-04 19:55:59 +00:00
|
|
|
SpvBuildLogger* logger;
|
2015-06-26 22:58:36 +00:00
|
|
|
}; // end Builder class
|
|
|
|
|
|
|
|
}; // end spv namespace
|
|
|
|
|
|
|
|
#endif // SpvBuilder_H
|