2015-06-26 22:58:36 +00:00
//
2017-01-06 19:34:14 +00:00
// Copyright (C) 2014-2015 LunarG, Inc.
2018-12-14 17:47:35 +00:00
// Copyright (C) 2015-2018 Google, Inc.
2018-03-06 23:12:04 +00:00
// Copyright (C) 2017 ARM Limited.
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>
2018-02-27 02:20:05 +00:00
# include <unordered_map>
2018-12-06 18:13:15 +00:00
# include <map>
2015-06-26 22:58:36 +00:00
namespace spv {
2019-02-07 15:04:12 +00:00
typedef enum {
Spv_1_0 = ( 1 < < 16 ) ,
Spv_1_1 = ( 1 < < 16 ) | ( 1 < < 8 ) ,
Spv_1_2 = ( 1 < < 16 ) | ( 2 < < 8 ) ,
Spv_1_3 = ( 1 < < 16 ) | ( 3 < < 8 ) ,
Spv_1_4 = ( 1 < < 16 ) | ( 4 < < 8 ) ,
2019-08-19 05:58:08 +00:00
Spv_1_5 = ( 1 < < 16 ) | ( 5 < < 8 ) ,
2019-02-07 15:04:12 +00:00
} SpvVersion ;
2015-06-26 22:58:36 +00:00
class Builder {
public :
2018-02-01 01:35:56 +00:00
Builder ( unsigned int spvVersion , unsigned int userNumber , SpvBuildLogger * logger ) ;
2015-06-26 22:58:36 +00:00
virtual ~ Builder ( ) ;
static const int maxMatrixSize = 4 ;
2018-03-06 23:12:04 +00:00
unsigned int getSpvVersion ( ) const { return spvVersion ; }
2015-06-26 22:58:36 +00:00
void setSource ( spv : : SourceLanguage lang , int version )
{
source = lang ;
sourceVersion = version ;
}
2018-12-06 18:13:15 +00:00
spv : : Id getStringId ( const std : : string & str )
2017-05-31 23:11:16 +00:00
{
2018-12-06 18:13:15 +00:00
auto sItr = stringIds . find ( str ) ;
if ( sItr ! = stringIds . end ( ) )
return sItr - > second ;
spv : : Id strId = getUniqueId ( ) ;
Instruction * fileString = new Instruction ( strId , NoType , OpString ) ;
const char * file_c_str = str . c_str ( ) ;
2018-12-08 00:36:33 +00:00
fileString - > addStringOperand ( file_c_str ) ;
2017-05-31 23:11:16 +00:00
strings . push_back ( std : : unique_ptr < Instruction > ( fileString ) ) ;
2018-12-06 18:13:15 +00:00
stringIds [ file_c_str ] = strId ;
return strId ;
}
void setSourceFile ( const std : : string & file )
{
sourceFileStringId = getStringId ( file ) ;
2017-05-31 23:11:16 +00:00
}
void setSourceText ( const std : : string & text ) { sourceText = text ; }
2016-05-05 04:30:44 +00:00
void addSourceExtension ( const char * ext ) { sourceExtensions . push_back ( ext ) ; }
2017-07-21 02:00:36 +00:00
void addModuleProcessed ( const std : : string & p ) { moduleProcesses . push_back ( p . c_str ( ) ) ; }
2017-06-01 00:50:53 +00:00
void setEmitOpLines ( ) { emitOpLines = true ; }
2016-09-21 10:56:12 +00:00
void addExtension ( const char * ext ) { extensions . insert ( ext ) ; }
2019-10-11 02:54:21 +00:00
void removeExtension ( const char * ext )
{
extensions . erase ( ext ) ;
}
2019-08-19 05:58:08 +00:00
void addIncorporatedExtension ( const char * ext , SpvVersion incorporatedVersion )
{
if ( getSpvVersion ( ) < static_cast < unsigned > ( incorporatedVersion ) )
addExtension ( ext ) ;
}
2019-10-11 02:54:21 +00:00
void promoteIncorporatedExtension ( const char * baseExt , const char * promoExt , SpvVersion incorporatedVersion )
{
removeExtension ( baseExt ) ;
addIncorporatedExtension ( promoExt , incorporatedVersion ) ;
}
2018-12-06 18:13:15 +00:00
void addInclude ( const std : : string & name , const std : : string & text )
{
spv : : Id incId = getStringId ( name ) ;
includeFiles [ incId ] = & text ;
}
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 ;
}
2018-12-08 00:36:33 +00:00
// Generate OpLine for non-filename-based #line directives (ie no filename
// seen yet): Log the current line, and if different than the last one,
// issue a new OpLine using the new line and current source file name.
2017-06-01 00:50:53 +00:00
void setLine ( int line ) ;
2018-12-08 00:36:33 +00:00
// If filename null, generate OpLine for non-filename-based line directives,
// else do filename-based: Log the current line and file, and if different
// than the last one, issue a new OpLine using the new line and file
// name.
void setLine ( int line , const char * filename ) ;
2017-06-01 00:50:53 +00:00
// Low-level OpLine. See setLine() for a layered helper.
void addLine ( Id fileName , int line , int column ) ;
2015-06-26 22:58:36 +00:00
// For creating new types (will return old type if the requested one was already made).
Id makeVoidType ( ) ;
Id makeBoolType ( ) ;
2019-01-06 23:58:04 +00:00
Id makePointer ( StorageClass , Id pointee ) ;
Id makeForwardPointer ( StorageClass ) ;
Id makePointerFromForwardPointer ( StorageClass , Id forwardPointerType , Id pointee ) ;
2015-06-26 22:58:36 +00:00
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 ) ;
2019-02-19 19:10:32 +00:00
Id makeCooperativeMatrixType ( Id component , Id scope , Id rows , Id cols ) ;
2015-06-26 22:58:36 +00:00
2018-09-19 18:42:24 +00:00
// accelerationStructureNV type
Id makeAccelerationStructureNVType ( ) ;
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 ) ) ; }
2019-02-19 19:10:32 +00:00
bool isCooperativeMatrix ( Id resultId ) const { return isCooperativeMatrixType ( getTypeId ( resultId ) ) ; }
2015-12-09 02:32:47 +00:00
bool isAggregate ( Id resultId ) const { return isAggregateType ( getTypeId ( resultId ) ) ; }
bool isSampledImage ( Id resultId ) const { return isSampledImageType ( getTypeId ( resultId ) ) ; }
2018-02-27 02:20:05 +00:00
bool isBoolType ( Id typeId ) { return groupedTypes [ OpTypeBool ] . size ( ) > 0 & & typeId = = groupedTypes [ OpTypeBool ] . back ( ) - > getResultId ( ) ; }
2017-03-29 09:12:40 +00:00
bool isIntType ( Id typeId ) const { return getTypeClass ( typeId ) = = OpTypeInt & & module . getInstruction ( typeId ) - > getImmediateOperand ( 1 ) ! = 0 ; }
bool isUintType ( Id typeId ) const { return getTypeClass ( typeId ) = = OpTypeInt & & module . getInstruction ( typeId ) - > getImmediateOperand ( 1 ) = = 0 ; }
bool isFloatType ( Id typeId ) const { return getTypeClass ( typeId ) = = OpTypeFloat ; }
2015-12-09 02:32:47 +00:00
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 ; }
2019-08-11 13:41:45 +00:00
# ifdef GLSLANG_WEB
bool isCooperativeMatrixType ( Id typeId ) const { return false ; }
# else
2019-02-19 19:10:32 +00:00
bool isCooperativeMatrixType ( Id typeId ) const { return getTypeClass ( typeId ) = = OpTypeCooperativeMatrixNV ; }
2019-08-11 13:41:45 +00:00
# endif
2019-02-19 19:10:32 +00:00
bool isAggregateType ( Id typeId ) const { return isArrayType ( typeId ) | | isStructType ( typeId ) | | isCooperativeMatrixType ( typeId ) ; }
2015-12-09 02:32:47 +00:00
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 ; }
2018-09-25 20:32:06 +00:00
bool containsType ( Id typeId , Op typeOp , unsigned int width ) const ;
2019-01-06 23:58:04 +00:00
bool containsPhysicalStorageBufferOrArray ( Id typeId ) const ;
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
2017-03-29 09:12:40 +00:00
int getScalarTypeWidth ( Id typeId ) const
{
Id scalarTypeId = getScalarTypeId ( typeId ) ;
assert ( getTypeClass ( scalarTypeId ) = = OpTypeInt | | getTypeClass ( scalarTypeId ) = = OpTypeFloat ) ;
return module . getInstruction ( scalarTypeId ) - > getImmediateOperand ( 0 ) ;
}
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 ) ;
2018-03-06 23:12:04 +00:00
Id makeInt8Constant ( int i , bool specConstant = false ) { return makeIntConstant ( makeIntType ( 8 ) , ( unsigned ) i , specConstant ) ; }
Id makeUint8Constant ( unsigned u , bool specConstant = false ) { return makeIntConstant ( makeUintType ( 8 ) , u , specConstant ) ; }
Id makeInt16Constant ( int i , bool specConstant = false ) { return makeIntConstant ( makeIntType ( 16 ) , ( unsigned ) i , specConstant ) ; }
Id makeUint16Constant ( unsigned u , bool specConstant = false ) { return makeIntConstant ( makeUintType ( 16 ) , u , specConstant ) ; }
2015-11-16 04:33:39 +00:00
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
Id makeFloat16Constant ( float f16 , bool specConstant = false ) ;
2018-03-13 09:57:59 +00:00
Id makeFpConstant ( Id type , double d , bool specConstant = false ) ;
2015-06-26 22:58:36 +00:00
// Turn the array of constants into a proper spv constant of the requested type.
2017-01-26 17:46:02 +00:00
Id makeCompositeConstant ( Id type , const 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 addDecoration ( Id , Decoration , int num = - 1 ) ;
2018-03-08 01:05:55 +00:00
void addDecoration ( Id , Decoration , const char * ) ;
void addDecorationId ( Id id , Decoration , Id idDecoration ) ;
2015-06-26 22:58:36 +00:00
void addMemberDecoration ( Id , unsigned int member , Decoration , int num = - 1 ) ;
2018-03-08 01:05:55 +00:00
void addMemberDecoration ( Id , unsigned int member , Decoration , const char * ) ;
2015-06-26 22:58:36 +00:00
// 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 ,
2017-07-18 08:35:46 +00:00
const std : : vector < 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.
2019-02-07 15:04:12 +00:00
Id createVariable ( StorageClass , Id type , const char * name = 0 , Id initializer = NoResult ) ;
2015-06-26 22:58:36 +00:00
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
2019-01-06 23:58:04 +00:00
void createStore ( Id rValue , Id lValue , spv : : MemoryAccessMask memoryAccess = spv : : MemoryAccessMaskNone , spv : : Scope scope = spv : : ScopeMax , unsigned int alignment = 0 ) ;
2015-06-26 22:58:36 +00:00
// Load from an Id and return it
2019-01-06 23:58:04 +00:00
Id createLoad ( Id lValue , spv : : MemoryAccessMask memoryAccess = spv : : MemoryAccessMaskNone , spv : : Scope scope = spv : : ScopeMax , unsigned int alignment = 0 ) ;
2015-06-26 22:58:36 +00:00
// Create an OpAccessChain instruction
2017-01-26 17:46:02 +00:00
Id createAccessChain ( StorageClass , Id base , const std : : vector < Id > & offsets ) ;
2015-06-26 22:58:36 +00:00
2015-09-22 03:50:29 +00:00
// Create an OpArrayLength instruction
Id createArrayLength ( Id base , unsigned int member ) ;
2019-02-19 19:10:32 +00:00
// Create an OpCooperativeMatrixLengthNV instruction
Id createCooperativeMatrixLength ( Id type ) ;
2015-06-26 22:58:36 +00:00
// Create an OpCompositeExtract instruction
Id createCompositeExtract ( Id composite , Id typeId , unsigned index ) ;
2017-01-26 17:46:02 +00:00
Id createCompositeExtract ( Id composite , Id typeId , const std : : vector < unsigned > & indexes ) ;
2015-06-26 22:58:36 +00:00
Id createCompositeInsert ( Id object , Id composite , Id typeId , unsigned index ) ;
2017-01-26 17:46:02 +00:00
Id createCompositeInsert ( Id object , Id composite , Id typeId , const std : : vector < unsigned > & indexes ) ;
2015-06-26 22:58:36 +00:00
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 ) ;
2018-09-05 15:11:41 +00:00
void createNoResultOp ( Op , const std : : vector < Id > & operands ) ;
2018-08-14 19:31:43 +00:00
void createNoResultOp ( Op , const std : : vector < IdImmediate > & 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 ) ;
2018-08-14 19:31:43 +00:00
Id createOp ( Op , Id typeId , const std : : vector < IdImmediate > & operands ) ;
2017-01-26 17:46:02 +00:00
Id createFunctionCall ( spv : : Function * , const 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.
2017-01-26 17:46:02 +00:00
Id createRvalueSwizzle ( Decoration precision , Id typeId , Id source , const 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.)
2017-01-26 17:46:02 +00:00
Id createLvalueSwizzle ( Id typeId , Id target , Id source , const std : : vector < unsigned > & channels ) ;
2015-06-26 22:58:36 +00:00
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.
//
2018-03-06 23:12:04 +00:00
// Note: One of the arguments will change, with the result coming back that way rather than
2015-06-26 22:58:36 +00:00
// 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.
2017-01-26 17:46:02 +00:00
Id createBuiltinCall ( Id resultType , Id builtins , int entryPoint , const 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 ;
2018-09-19 18:41:27 +00:00
Id granularity ;
Id coarse ;
2018-09-05 15:11:41 +00:00
bool nonprivate ;
bool volatil ;
2015-06-26 22:58:36 +00:00
} ;
// Select the correct texture operation based on all inputs, and emit the correct instruction
2019-03-31 16:51:57 +00:00
Id createTextureCall ( Decoration precision , Id resultType , bool sparse , bool fetch , bool proj , bool gather ,
bool noImplicit , const TextureParameters & , ImageOperandsMask ) ;
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.
2017-03-10 19:45:50 +00:00
Id createTextureQueryCall ( Op , const TextureParameters & , bool isUnsignedResult ) ;
2015-06-26 22:58:36 +00:00
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
2017-01-26 17:46:02 +00:00
Id createCompositeConstruct ( Id typeId , const std : : vector < Id > & constituents ) ;
2015-06-26 22:58:36 +00:00
// 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 :
2017-07-04 15:23:40 +00:00
If ( Id condition , unsigned int ctrl , Builder & builder ) ;
2015-06-26 22:58:36 +00:00
~ If ( ) { }
void makeBeginElse ( ) ;
void makeEndIf ( ) ;
private :
If ( const If & ) ;
If & operator = ( If & ) ;
Builder & builder ;
Id condition ;
2017-07-04 15:23:40 +00:00
unsigned int control ;
2015-06-26 22:58:36 +00:00
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.
//
2017-07-04 15:23:40 +00:00
void makeSwitch ( Id condition , unsigned int control , int numSegments , const std : : vector < int > & caseValues ,
2017-01-26 17:46:02 +00:00
const std : : vector < int > & valueToSegment , int defaultSegment , std : : vector < Block * > & segmentBB ) ; // return argument
2015-06-26 22:58:36 +00:00
// 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
2019-01-06 23:58:04 +00:00
unsigned int alignment ; // bitwise OR of alignment values passed in. Accumulates worst alignment. Only tracks base and (optional) component selection alignment.
2018-09-05 15:11:41 +00:00
// Accumulate whether anything in the chain of structures has coherent decorations.
struct CoherentFlags {
2019-08-11 13:41:45 +00:00
CoherentFlags ( ) { clear ( ) ; }
# ifdef GLSLANG_WEB
void clear ( ) { }
bool isVolatile ( ) const { return false ; }
CoherentFlags operator | = ( const CoherentFlags & other ) { return * this ; }
# else
bool isVolatile ( ) const { return volatil ; }
2018-09-05 15:11:41 +00:00
unsigned coherent : 1 ;
unsigned devicecoherent : 1 ;
unsigned queuefamilycoherent : 1 ;
unsigned workgroupcoherent : 1 ;
unsigned subgroupcoherent : 1 ;
unsigned nonprivate : 1 ;
unsigned volatil : 1 ;
unsigned isImage : 1 ;
void clear ( ) {
coherent = 0 ;
devicecoherent = 0 ;
queuefamilycoherent = 0 ;
workgroupcoherent = 0 ;
subgroupcoherent = 0 ;
nonprivate = 0 ;
volatil = 0 ;
isImage = 0 ;
}
CoherentFlags operator | = ( const CoherentFlags & other ) {
coherent | = other . coherent ;
devicecoherent | = other . devicecoherent ;
queuefamilycoherent | = other . queuefamilycoherent ;
workgroupcoherent | = other . workgroupcoherent ;
subgroupcoherent | = other . subgroupcoherent ;
nonprivate | = other . nonprivate ;
volatil | = other . volatil ;
isImage | = other . isImage ;
return * this ;
}
2019-08-11 13:41:45 +00:00
# endif
2018-09-05 15:11:41 +00:00
} ;
CoherentFlags coherentFlags ;
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
2019-01-06 23:58:04 +00:00
void accessChainPush ( Id offset , AccessChain : : CoherentFlags coherentFlags , unsigned int alignment )
2015-06-26 22:58:36 +00:00
{
accessChain . indexChain . push_back ( offset ) ;
2018-09-05 15:11:41 +00:00
accessChain . coherentFlags | = coherentFlags ;
2019-01-06 23:58:04 +00:00
accessChain . alignment | = alignment ;
2015-06-26 22:58:36 +00:00
}
// push new swizzle onto the end of any existing swizzle, merging into a single swizzle
2019-01-06 23:58:04 +00:00
void accessChainPushSwizzle ( std : : vector < unsigned > & swizzle , Id preSwizzleBaseType , AccessChain : : CoherentFlags coherentFlags , unsigned int alignment ) ;
2015-06-26 22:58:36 +00:00
2018-02-05 21:44:14 +00:00
// push a dynamic component selection onto the access chain, only applicable with a
// non-trivial swizzle or no swizzle
2019-01-06 23:58:04 +00:00
void accessChainPushComponent ( Id component , Id preSwizzleBaseType , AccessChain : : CoherentFlags coherentFlags , unsigned int alignment )
2015-09-13 20:46:30 +00:00
{
2018-02-05 21:44:14 +00:00
if ( accessChain . swizzle . size ( ) ! = 1 ) {
accessChain . component = component ;
if ( accessChain . preSwizzleBaseType = = NoType )
accessChain . preSwizzleBaseType = preSwizzleBaseType ;
}
2019-01-06 23:58:04 +00:00
accessChain . coherentFlags | = coherentFlags ;
accessChain . alignment | = alignment ;
2015-09-13 20:46:30 +00:00
}
2015-06-26 22:58:36 +00:00
// use accessChain and swizzle to store value
2019-01-06 23:58:04 +00:00
void accessChainStore ( Id rvalue , spv : : MemoryAccessMask memoryAccess = spv : : MemoryAccessMaskNone , spv : : Scope scope = spv : : ScopeMax , unsigned int alignment = 0 ) ;
2015-06-26 22:58:36 +00:00
// use accessChain and swizzle to load an r-value
2019-01-06 23:58:04 +00:00
Id accessChainLoad ( Decoration precision , Decoration nonUniform , Id ResultType , spv : : MemoryAccessMask memoryAccess = spv : : MemoryAccessMaskNone , spv : : Scope scope = spv : : ScopeMax , unsigned int alignment = 0 ) ;
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 ( ) ;
2018-08-03 21:56:12 +00:00
// Add capabilities, extensions, remove unneeded decorations, etc.,
// based on the resulting SPIR-V.
void postProcess ( ) ;
// Hook to visit each instruction in a block in a function
2019-01-06 23:58:04 +00:00
void postProcess ( Instruction & ) ;
2018-08-03 21:56:12 +00:00
// Hook to visit each instruction in a reachable block in a function.
2018-08-15 19:54:09 +00:00
void postProcessReachable ( const Instruction & ) ;
// Hook to visit each non-32-bit sized float/int operation in a block.
void postProcessType ( const Instruction & , spv : : Id typeId ) ;
2018-08-03 21:56:12 +00:00
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 ) ;
2019-01-12 10:31:41 +00:00
void createLoopMerge ( Block * mergeBlock , Block * continueBlock , unsigned int control , const std : : vector < unsigned int > & operands ) ;
2016-01-10 17:15:13 +00:00
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 ) ;
2018-02-27 02:20:05 +00:00
Id findScalarConstant ( Op typeClass , Op opcode , Id typeId , unsigned value ) ;
Id findScalarConstant ( Op typeClass , Op opcode , Id typeId , unsigned v1 , unsigned v2 ) ;
2019-02-19 19:10:32 +00:00
Id findCompositeConstant ( Op typeClass , Id typeId , const std : : vector < Id > & comps ) ;
2018-02-27 02:20:05 +00:00
Id findStructConstant ( Id typeId , const std : : vector < Id > & comps ) ;
2015-06-26 22:58:36 +00:00
Id collapseAccessChain ( ) ;
2018-02-05 21:44:14 +00:00
void remapDynamicSwizzle ( ) ;
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 ) ;
2017-05-31 23:11:16 +00:00
void dumpSourceInstructions ( std : : vector < unsigned int > & ) const ;
2018-12-06 18:13:15 +00:00
void dumpSourceInstructions ( const spv : : Id fileId , const std : : string & text , std : : vector < unsigned int > & ) const ;
2016-01-18 14:23:56 +00:00
void dumpInstructions ( std : : vector < unsigned int > & , const std : : vector < std : : unique_ptr < Instruction > > & ) const ;
2017-07-21 02:00:36 +00:00
void dumpModuleProcesses ( std : : vector < unsigned int > & ) const ;
2019-01-06 23:58:04 +00:00
spv : : MemoryAccessMask sanitizeMemoryAccessForStorageClass ( spv : : MemoryAccessMask memoryAccess , StorageClass sc ) const ;
2015-06-26 22:58:36 +00:00
2018-02-01 01:35:56 +00:00
unsigned int spvVersion ; // the version of SPIR-V to emit in the header
2015-06-26 22:58:36 +00:00
SourceLanguage source ;
int sourceVersion ;
2017-05-31 23:11:16 +00:00
spv : : Id sourceFileStringId ;
std : : string sourceText ;
2017-06-01 00:50:53 +00:00
int currentLine ;
2018-12-08 00:36:33 +00:00
const char * currentFile ;
2017-06-01 00:50:53 +00:00
bool emitOpLines ;
2016-10-14 09:22:23 +00:00
std : : set < std : : string > extensions ;
2016-05-05 04:30:44 +00:00
std : : vector < const char * > sourceExtensions ;
2017-07-21 02:00:36 +00:00
std : : vector < const char * > moduleProcesses ;
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
2017-05-31 23:11:16 +00:00
std : : vector < std : : unique_ptr < Instruction > > strings ;
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 > > 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
2018-02-27 02:20:05 +00:00
std : : unordered_map < unsigned int , std : : vector < Instruction * > > groupedConstants ; // map type opcodes to constant inst.
std : : unordered_map < unsigned int , std : : vector < Instruction * > > groupedStructConstants ; // map struct-id to constant instructions
std : : unordered_map < unsigned int , std : : vector < Instruction * > > groupedTypes ; // map type opcodes to type instructions
2015-06-26 22:58:36 +00:00
// 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
2018-12-08 00:36:33 +00:00
// map from strings to their string ids
std : : unordered_map < std : : string , spv : : Id > stringIds ;
2018-12-06 18:13:15 +00:00
// map from include file name ids to their contents
std : : map < spv : : Id , const std : : string * > includeFiles ;
2017-05-31 23:11:16 +00:00
// The stream for outputting 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