| 1 |
//===- SPIRVModule.h - Class to represent a SPIR-V module -------*- C++ -*-===// |
| 2 |
// |
| 3 |
// The LLVM/SPIRV Translator |
| 4 |
// |
| 5 |
// This file is distributed under the University of Illinois Open Source |
| 6 |
// License. See LICENSE.TXT for details. |
| 7 |
// |
| 8 |
// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved. |
| 9 |
// |
| 10 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 11 |
// copy of this software and associated documentation files (the "Software"), |
| 12 |
// to deal with the Software without restriction, including without limitation |
| 13 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 15 |
// Software is furnished to do so, subject to the following conditions: |
| 16 |
// |
| 17 |
// Redistributions of source code must retain the above copyright notice, |
| 18 |
// this list of conditions and the following disclaimers. |
| 19 |
// Redistributions in binary form must reproduce the above copyright notice, |
| 20 |
// this list of conditions and the following disclaimers in the documentation |
| 21 |
// and/or other materials provided with the distribution. |
| 22 |
// Neither the names of Advanced Micro Devices, Inc., nor the names of its |
| 23 |
// contributors may be used to endorse or promote products derived from this |
| 24 |
// Software without specific prior written permission. |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 |
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 31 |
// THE SOFTWARE. |
| 32 |
// |
| 33 |
//===----------------------------------------------------------------------===// |
| 34 |
/// \file |
| 35 |
/// |
| 36 |
/// This file defines Module class for SPIR-V. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#ifndef SPIRV_LIBSPIRV_SPIRVMODULE_H |
| 41 |
#define SPIRV_LIBSPIRV_SPIRVMODULE_H |
| 42 |
|
| 43 |
#include "LLVMSPIRVOpts.h" |
| 44 |
#include "SPIRVEntry.h" |
| 45 |
|
| 46 |
#include "llvm/IR/Metadata.h" |
| 47 |
|
| 48 |
#include <iostream> |
| 49 |
#include <set> |
| 50 |
#include <string> |
| 51 |
#include <vector> |
| 52 |
|
| 53 |
namespace llvm { |
| 54 |
class APInt; |
| 55 |
} // namespace llvm |
| 56 |
|
| 57 |
namespace SPIRV { |
| 58 |
|
| 59 |
template <Op> class SPIRVConstantBase; |
| 60 |
using SPIRVConstant = SPIRVConstantBase<OpConstant>; |
| 61 |
|
| 62 |
class SPIRVBasicBlock; |
| 63 |
class SPIRVEntry; |
| 64 |
class SPIRVFunction; |
| 65 |
class SPIRVInstruction; |
| 66 |
class SPIRVType; |
| 67 |
class SPIRVTypeArray; |
| 68 |
class SPIRVTypeBool; |
| 69 |
class SPIRVTypeFloat; |
| 70 |
class SPIRVTypeFunction; |
| 71 |
class SPIRVTypeInt; |
| 72 |
class SPIRVTypeOpaque; |
| 73 |
class SPIRVTypePointer; |
| 74 |
class SPIRVTypeImage; |
| 75 |
class SPIRVTypeSampler; |
| 76 |
class SPIRVTypeSampledImage; |
| 77 |
class SPIRVTypePipeStorage; |
| 78 |
class SPIRVTypeStruct; |
| 79 |
class SPIRVTypeVector; |
| 80 |
class SPIRVTypeVoid; |
| 81 |
class SPIRVTypeDeviceEvent; |
| 82 |
class SPIRVTypeQueue; |
| 83 |
class SPIRVTypePipe; |
| 84 |
class SPIRVTypeVmeImageINTEL; |
| 85 |
class SPIRVValue; |
| 86 |
class SPIRVVariable; |
| 87 |
class SPIRVDecorateGeneric; |
| 88 |
class SPIRVDecorationGroup; |
| 89 |
class SPIRVGroupDecorate; |
| 90 |
class SPIRVGroupMemberDecorate; |
| 91 |
class SPIRVGroupDecorateGeneric; |
| 92 |
class SPIRVInstTemplateBase; |
| 93 |
class SPIRVAsmTargetINTEL; |
| 94 |
class SPIRVAsmINTEL; |
| 95 |
class SPIRVAsmCallINTEL; |
| 96 |
class SPIRVTypeBufferSurfaceINTEL; |
| 97 |
class SPIRVTypeTokenINTEL; |
| 98 |
class SPIRVTypeJointMatrixINTEL; |
| 99 |
class SPIRVTypeCooperativeMatrixKHR; |
| 100 |
class SPIRVConditionalCapabilityINTEL; |
| 101 |
class SPIRVConditionalExtensionINTEL; |
| 102 |
class SPIRVConditionalEntryPointINTEL; |
| 103 |
class SPIRVConditionalCopyObjectINTEL; |
| 104 |
|
| 105 |
typedef SPIRVBasicBlock SPIRVLabel; |
| 106 |
struct SPIRVTypeImageDescriptor; |
| 107 |
|
| 108 |
class SPIRVModule { |
| 109 |
public: |
| 110 |
typedef std::map<SPIRVCapabilityKind, SPIRVCapability *> SPIRVCapMap; |
| 111 |
typedef std::map<std::pair<SPIRVId, SPIRVCapabilityKind>, |
| 112 |
SPIRVConditionalCapabilityINTEL *> |
| 113 |
SPIRVConditionalCapMap; |
| 114 |
typedef std::vector<SPIRVConditionalEntryPointINTEL *> |
| 115 |
SPIRVConditionalEntryPointVec; |
| 116 |
typedef std::set<std::pair<SPIRVId, std::string>> |
| 117 |
SPIRVConditionalExtensionSet; |
| 118 |
|
| 119 |
static SPIRVModule *createSPIRVModule(); |
| 120 |
static SPIRVModule *createSPIRVModule(const SPIRV::TranslatorOpts &); |
| 121 |
SPIRVModule(); |
| 122 |
virtual ~SPIRVModule(); |
| 123 |
|
| 124 |
// Object query functions |
| 125 |
virtual bool exist(SPIRVId) const = 0; |
| 126 |
virtual bool exist(SPIRVId, SPIRVEntry **) const = 0; |
| 127 |
template <class T> T *get(SPIRVId Id) const { |
| 128 |
return static_cast<T *>(getEntry(Id)); |
| 129 |
} |
| 130 |
virtual SPIRVEntry *getEntry(SPIRVId) const = 0; |
| 131 |
virtual bool hasDebugInfo() const = 0; |
| 132 |
|
| 133 |
// Error handling functions |
| 134 |
virtual SPIRVErrorLog &getErrorLog() = 0; |
| 135 |
virtual SPIRVErrorCode getError(std::string &) = 0; |
| 136 |
// Check if extension is allowed, and set ErrCode and DetailedMsg if not. |
| 137 |
// Returns true if no error. |
| 138 |
virtual bool checkExtension(ExtensionID, SPIRVErrorCode, |
| 139 |
const std::string &) = 0; |
| 140 |
void setInvalid() { IsValid = false; } |
| 141 |
bool isModuleValid() { return IsValid; } |
| 142 |
|
| 143 |
// Module query functions |
| 144 |
virtual SPIRVAddressingModelKind getAddressingModel() = 0; |
| 145 |
virtual const SPIRVCapMap &getCapability() const = 0; |
| 146 |
virtual const SPIRVConditionalCapMap &getConditionalCapabilities() const = 0; |
| 147 |
virtual const SPIRVConditionalEntryPointVec & |
| 148 |
getConditionalEntryPoints() const = 0; |
| 149 |
virtual bool hasCapability(SPIRVCapabilityKind) const = 0; |
| 150 |
virtual SPIRVExtInstSetKind getBuiltinSet(SPIRVId) const = 0; |
| 151 |
virtual std::set<std::string> &getExtension() = 0; |
| 152 |
virtual SPIRVConditionalExtensionSet &getConditionalExtensions() = 0; |
| 153 |
virtual SPIRVFunction *getFunction(unsigned) const = 0; |
| 154 |
virtual SPIRVVariable *getVariable(unsigned) const = 0; |
| 155 |
virtual SPIRVValue *getConst(unsigned) const = 0; |
| 156 |
virtual std::vector<SPIRVDecorateGeneric *> *getDecorateVec() = 0; |
| 157 |
virtual std::vector<SPIRVFunction *> *getFuncVec() = 0; |
| 158 |
virtual SPIRVMemoryModelKind getMemoryModel() const = 0; |
| 159 |
virtual unsigned getNumFunctions() const = 0; |
| 160 |
virtual unsigned getNumVariables() const = 0; |
| 161 |
virtual unsigned getNumConsts() const = 0; |
| 162 |
virtual std::vector<SPIRVValue *> getFunctionPointers() const = 0; |
| 163 |
virtual SourceLanguage getSourceLanguage(SPIRVWord *) const = 0; |
| 164 |
virtual std::set<std::string> &getSourceExtension() = 0; |
| 165 |
virtual SPIRVValue *getValue(SPIRVId TheId) const = 0; |
| 166 |
virtual std::vector<SPIRVValue *> |
| 167 |
getValues(const std::vector<SPIRVId> &) const = 0; |
| 168 |
virtual std::vector<SPIRVId> |
| 169 |
getIds(const std::vector<SPIRVEntry *> &) const = 0; |
| 170 |
virtual std::vector<SPIRVId> |
| 171 |
getIds(const std::vector<SPIRVValue *> &) const = 0; |
| 172 |
virtual SPIRVType *getValueType(SPIRVId TheId) const = 0; |
| 173 |
virtual std::vector<SPIRVType *> |
| 174 |
getValueTypes(const std::vector<SPIRVId> &) const = 0; |
| 175 |
virtual SPIRVConstant *getLiteralAsConstant(unsigned Literal) = 0; |
| 176 |
virtual bool isEntryPoint(SPIRVExecutionModelKind, SPIRVId) const = 0; |
| 177 |
virtual unsigned short getGeneratorId() const = 0; |
| 178 |
virtual unsigned short getGeneratorVer() const = 0; |
| 179 |
virtual SPIRVWord getSPIRVVersion() const = 0; |
| 180 |
virtual const std::vector<SPIRVExtInst *> &getDebugInstVec() const = 0; |
| 181 |
virtual const std::vector<SPIRVExtInst *> &getAuxDataInstVec() const = 0; |
| 182 |
|
| 183 |
virtual const std::vector<SPIRVString *> &getStringVec() const = 0; |
| 184 |
|
| 185 |
// Module changing functions |
| 186 |
virtual bool importBuiltinSet(const std::string &, SPIRVId *) = 0; |
| 187 |
virtual bool importBuiltinSetWithId(const std::string &, SPIRVId) = 0; |
| 188 |
virtual void setAddressingModel(SPIRVAddressingModelKind) = 0; |
| 189 |
virtual void setAlignment(SPIRVValue *, SPIRVWord) = 0; |
| 190 |
virtual void setMemoryModel(SPIRVMemoryModelKind) = 0; |
| 191 |
virtual void setName(SPIRVEntry *, const std::string &) = 0; |
| 192 |
virtual void setSourceLanguage(SourceLanguage, SPIRVWord) = 0; |
| 193 |
virtual void setAutoAddCapability(bool E) { AutoAddCapability = E; } |
| 194 |
virtual void setValidateCapability(bool E) { ValidateCapability = E; } |
| 195 |
virtual void setAutoAddExtensions(bool E) { AutoAddExtensions = E; } |
| 196 |
virtual void setGeneratorId(unsigned short) = 0; |
| 197 |
virtual void setGeneratorVer(unsigned short) = 0; |
| 198 |
virtual void resolveUnknownStructFields() = 0; |
| 199 |
virtual void setSPIRVVersion(SPIRVWord) = 0; |
| 200 |
virtual void insertEntryNoId(SPIRVEntry *Entry) = 0; |
| 201 |
virtual bool eraseReferencesOfInst(SPIRVId Id) = 0; |
| 202 |
virtual void eraseCapability(SPIRVCapabilityKind CapKind) = 0; |
| 203 |
|
| 204 |
void setMinSPIRVVersion(VersionNumber Ver) { |
| 205 |
setSPIRVVersion(std::max(static_cast<SPIRVWord>(Ver), getSPIRVVersion())); |
| 206 |
} |
| 207 |
|
| 208 |
// Object creation functions |
| 209 |
template <class T> T *add(T *Entry) { |
| 210 |
addEntry(Entry); |
| 211 |
return Entry; |
| 212 |
} |
| 213 |
virtual SPIRVEntry *addEntry(SPIRVEntry *) = 0; |
| 214 |
virtual SPIRVBasicBlock *addBasicBlock(SPIRVFunction *, |
| 215 |
SPIRVId Id = SPIRVID_INVALID) = 0; |
| 216 |
virtual SPIRVString *getString(const std::string &Str) = 0; |
| 217 |
virtual SPIRVMemberName *addMemberName(SPIRVTypeStruct *ST, |
| 218 |
SPIRVWord MemberNumber, |
| 219 |
const std::string &Name) = 0; |
| 220 |
virtual void addUnknownStructField(SPIRVTypeStruct *, unsigned Idx, |
| 221 |
SPIRVId Id) = 0; |
| 222 |
virtual void addLine(SPIRVEntry *E, SPIRVId FileNameId, SPIRVWord Line, |
| 223 |
SPIRVWord Column) = 0; |
| 224 |
virtual const std::shared_ptr<const SPIRVLine> &getCurrentLine() const = 0; |
| 225 |
virtual void setCurrentLine(const std::shared_ptr<const SPIRVLine> &) = 0; |
| 226 |
virtual void addDebugLine(SPIRVEntry *E, SPIRVType *TheType, |
| 227 |
SPIRVId FileNameId, SPIRVWord LineStart, |
| 228 |
SPIRVWord LineEnd, SPIRVWord ColumnStart, |
| 229 |
SPIRVWord ColumnEnd) = 0; |
| 230 |
virtual const std::shared_ptr<const SPIRVExtInst> & |
| 231 |
getCurrentDebugLine() const = 0; |
| 232 |
virtual void |
| 233 |
setCurrentDebugLine(const std::shared_ptr<const SPIRVExtInst> &) = 0; |
| 234 |
virtual const SPIRVDecorateGeneric *addDecorate(SPIRVDecorateGeneric *) = 0; |
| 235 |
virtual SPIRVDecorationGroup *addDecorationGroup() = 0; |
| 236 |
virtual SPIRVDecorationGroup * |
| 237 |
addDecorationGroup(SPIRVDecorationGroup *Group) = 0; |
| 238 |
virtual SPIRVGroupDecorate * |
| 239 |
addGroupDecorate(SPIRVDecorationGroup *Group, |
| 240 |
const std::vector<SPIRVEntry *> &Targets) = 0; |
| 241 |
virtual SPIRVGroupMemberDecorate * |
| 242 |
addGroupMemberDecorate(SPIRVDecorationGroup *Group, |
| 243 |
const std::vector<SPIRVEntry *> &Targets) = 0; |
| 244 |
virtual SPIRVGroupDecorateGeneric * |
| 245 |
addGroupDecorateGeneric(SPIRVGroupDecorateGeneric *GDec) = 0; |
| 246 |
virtual void addEntryPoint(SPIRVExecutionModelKind, SPIRVId, |
| 247 |
const std::string &, |
| 248 |
const std::vector<SPIRVId> &) = 0; |
| 249 |
virtual void addConditionalEntryPoint(SPIRVId, SPIRVExecutionModelKind, |
| 250 |
SPIRVId, const std::string &, |
| 251 |
const std::vector<SPIRVId> &) = 0; |
| 252 |
virtual void specializeConditionalEntryPoints(SPIRVId, bool) = 0; |
| 253 |
virtual SPIRVForward *addForward(SPIRVType *Ty) = 0; |
| 254 |
virtual SPIRVForward *addForward(SPIRVId, SPIRVType *Ty) = 0; |
| 255 |
virtual SPIRVFunction *addFunction(SPIRVFunction *) = 0; |
| 256 |
virtual SPIRVFunction *addFunction(SPIRVTypeFunction *, |
| 257 |
SPIRVId Id = SPIRVID_INVALID) = 0; |
| 258 |
virtual SPIRVEntry *replaceForward(SPIRVForward *, SPIRVEntry *) = 0; |
| 259 |
virtual void eraseInstruction(SPIRVInstruction *, SPIRVBasicBlock *) = 0; |
| 260 |
virtual bool eraseValue(SPIRVValue *) = 0; |
| 261 |
|
| 262 |
// Type creation functions |
| 263 |
virtual SPIRVTypeArray *addArrayType(SPIRVType *, SPIRVConstant *) = 0; |
| 264 |
virtual SPIRVTypeBool *addBoolType() = 0; |
| 265 |
virtual SPIRVTypeFloat *addFloatType(unsigned, unsigned = FPEncodingMax) = 0; |
| 266 |
virtual SPIRVTypeFunction * |
| 267 |
addFunctionType(SPIRVType *, const std::vector<SPIRVType *> &) = 0; |
| 268 |
virtual SPIRVTypeImage *addImageType(SPIRVType *, |
| 269 |
const SPIRVTypeImageDescriptor &) = 0; |
| 270 |
virtual SPIRVTypeImage *addImageType(SPIRVType *, |
| 271 |
const SPIRVTypeImageDescriptor &, |
| 272 |
SPIRVAccessQualifierKind) = 0; |
| 273 |
virtual SPIRVTypeSampler *addSamplerType() = 0; |
| 274 |
virtual SPIRVTypePipeStorage *addPipeStorageType() = 0; |
| 275 |
virtual SPIRVTypeSampledImage *addSampledImageType(SPIRVTypeImage *T) = 0; |
| 276 |
virtual SPIRVTypeInt *addIntegerType(unsigned) = 0; |
| 277 |
virtual SPIRVTypeOpaque *addOpaqueType(const std::string &) = 0; |
| 278 |
virtual SPIRVTypePointer *addPointerType(SPIRVStorageClassKind, |
| 279 |
SPIRVType *) = 0; |
| 280 |
virtual SPIRVTypeStruct *openStructType(unsigned, const std::string &) = 0; |
| 281 |
virtual SPIRVEntry *addTypeStructContinuedINTEL(unsigned NumMembers) = 0; |
| 282 |
virtual void closeStructType(SPIRVTypeStruct *, bool) = 0; |
| 283 |
virtual SPIRVTypeVector *addVectorType(SPIRVType *, SPIRVWord) = 0; |
| 284 |
virtual SPIRVTypeJointMatrixINTEL * |
| 285 |
addJointMatrixINTELType(SPIRVType *, std::vector<SPIRVValue *>) = 0; |
| 286 |
virtual SPIRVTypeCooperativeMatrixKHR * |
| 287 |
addCooperativeMatrixKHRType(SPIRVType *, std::vector<SPIRVValue *>) = 0; |
| 288 |
virtual SPIRVInstruction * |
| 289 |
addCooperativeMatrixLengthKHRInst(SPIRVType *, SPIRVType *, |
| 290 |
SPIRVBasicBlock *) = 0; |
| 291 |
virtual SPIRVTypeVoid *addVoidType() = 0; |
| 292 |
virtual SPIRVType *addOpaqueGenericType(Op) = 0; |
| 293 |
virtual SPIRVTypeDeviceEvent *addDeviceEventType() = 0; |
| 294 |
virtual SPIRVTypeQueue *addQueueType() = 0; |
| 295 |
virtual SPIRVTypePipe *addPipeType() = 0; |
| 296 |
virtual SPIRVType *addSubgroupAvcINTELType(Op) = 0; |
| 297 |
virtual SPIRVTypeVmeImageINTEL *addVmeImageINTELType(SPIRVTypeImage *) = 0; |
| 298 |
virtual SPIRVTypeBufferSurfaceINTEL * |
| 299 |
addBufferSurfaceINTELType(SPIRVAccessQualifierKind Access) = 0; |
| 300 |
virtual SPIRVTypeTokenINTEL *addTokenTypeINTEL() = 0; |
| 301 |
|
| 302 |
// Constants creation functions |
| 303 |
virtual SPIRVValue * |
| 304 |
addCompositeConstant(SPIRVType *, const std::vector<SPIRVValue *> &) = 0; |
| 305 |
virtual SPIRVEntry * |
| 306 |
addCompositeConstantContinuedINTEL(const std::vector<SPIRVValue *> &) = 0; |
| 307 |
virtual SPIRVValue * |
| 308 |
addSpecConstantComposite(SPIRVType *Ty, |
| 309 |
const std::vector<SPIRVValue *> &Elements) = 0; |
| 310 |
virtual SPIRVEntry * |
| 311 |
addSpecConstantCompositeContinuedINTEL(const std::vector<SPIRVValue *> &) = 0; |
| 312 |
virtual SPIRVValue *addConstantFunctionPointerINTEL(SPIRVType *Ty, |
| 313 |
SPIRVFunction *F) = 0; |
| 314 |
virtual SPIRVValue *addConstant(SPIRVValue *) = 0; |
| 315 |
virtual SPIRVValue *addConstant(SPIRVType *, uint64_t) = 0; |
| 316 |
virtual SPIRVValue *addConstant(SPIRVType *, llvm::APInt) = 0; |
| 317 |
virtual SPIRVValue *addSpecConstant(SPIRVType *, uint64_t) = 0; |
| 318 |
virtual SPIRVValue *addDoubleConstant(SPIRVTypeFloat *, double) = 0; |
| 319 |
virtual SPIRVValue *addFloatConstant(SPIRVTypeFloat *, float) = 0; |
| 320 |
virtual SPIRVValue *addIntegerConstant(SPIRVTypeInt *, uint64_t) = 0; |
| 321 |
virtual SPIRVValue *addNullConstant(SPIRVType *) = 0; |
| 322 |
virtual SPIRVValue *addUndef(SPIRVType *TheType) = 0; |
| 323 |
virtual SPIRVValue *addSamplerConstant(SPIRVType *TheType, SPIRVWord AddrMode, |
| 324 |
SPIRVWord ParametricMode, |
| 325 |
SPIRVWord FilterMode) = 0; |
| 326 |
virtual SPIRVValue *addPipeStorageConstant(SPIRVType *TheType, |
| 327 |
SPIRVWord PacketSize, |
| 328 |
SPIRVWord PacketAlign, |
| 329 |
SPIRVWord Capacity) = 0; |
| 330 |
|
| 331 |
// Instruction creation functions |
| 332 |
virtual SPIRVInstruction *addPtrAccessChainInst(SPIRVType *, SPIRVValue *, |
| 333 |
std::vector<SPIRVValue *>, |
| 334 |
SPIRVBasicBlock *, bool) = 0; |
| 335 |
virtual SPIRVInstruction * |
| 336 |
addAsyncGroupCopy(SPIRVValue *Scope, SPIRVValue *Dest, SPIRVValue *Src, |
| 337 |
SPIRVValue *NumElems, SPIRVValue *Stride, SPIRVValue *Event, |
| 338 |
SPIRVBasicBlock *BB) = 0; |
| 339 |
virtual SPIRVInstruction *addBinaryInst(Op, SPIRVType *, SPIRVValue *, |
| 340 |
SPIRVValue *, SPIRVBasicBlock *) = 0; |
| 341 |
virtual SPIRVInstruction *addBranchConditionalInst(SPIRVValue *, SPIRVLabel *, |
| 342 |
SPIRVLabel *, |
| 343 |
SPIRVBasicBlock *) = 0; |
| 344 |
virtual SPIRVInstruction *addBranchInst(SPIRVLabel *, SPIRVBasicBlock *) = 0; |
| 345 |
virtual SPIRVInstruction *addExtInst(SPIRVType *, SPIRVWord, SPIRVWord, |
| 346 |
const std::vector<SPIRVWord> &, |
| 347 |
SPIRVBasicBlock *, |
| 348 |
SPIRVInstruction * = nullptr) = 0; |
| 349 |
virtual SPIRVInstruction *addExtInst(SPIRVType *, SPIRVWord, SPIRVWord, |
| 350 |
const std::vector<SPIRVValue *> &, |
| 351 |
SPIRVBasicBlock *, |
| 352 |
SPIRVInstruction * = nullptr) = 0; |
| 353 |
virtual SPIRVEntry *createDebugInfo(SPIRVWord, SPIRVType *, |
| 354 |
const std::vector<SPIRVWord> &) = 0; |
| 355 |
virtual SPIRVEntry *addDebugInfo(SPIRVWord, SPIRVType *, |
| 356 |
const std::vector<SPIRVWord> &) = 0; |
| 357 |
virtual SPIRVEntry *addAuxData(SPIRVWord, SPIRVType *, |
| 358 |
const std::vector<SPIRVWord> &) = 0; |
| 359 |
virtual SPIRVEntry *addModuleProcessed(const std::string &) = 0; |
| 360 |
virtual void addCapability(SPIRVCapabilityKind) = 0; |
| 361 |
template <typename T> void addCapabilities(const T &Caps) { |
| 362 |
for (auto I : Caps) |
| 363 |
addCapability(I); |
| 364 |
} |
| 365 |
virtual void addConditionalCapability(SPIRVId, SPIRVCapabilityKind) = 0; |
| 366 |
template <typename T> |
| 367 |
void addConditionalCapabilities(SPIRVId Condition, const T &Caps) { |
| 368 |
for (auto I : Caps) |
| 369 |
addConditionalCapability(Condition, I); |
| 370 |
} |
| 371 |
virtual void eraseConditionalCapability(SPIRVId, SPIRVCapabilityKind) = 0; |
| 372 |
virtual void addExtension(ExtensionID) = 0; |
| 373 |
/// Used by SPIRV entries to add required capability internally. |
| 374 |
/// Should not be used by users directly. |
| 375 |
virtual void addCapabilityInternal(SPIRVCapabilityKind) = 0; |
| 376 |
virtual SPIRVInstruction *addCallInst(SPIRVFunction *, |
| 377 |
const std::vector<SPIRVWord> &, |
| 378 |
SPIRVBasicBlock *) = 0; |
| 379 |
virtual SPIRVInstruction *addIndirectCallInst(SPIRVValue *, SPIRVType *, |
| 380 |
const std::vector<SPIRVWord> &, |
| 381 |
SPIRVBasicBlock *) = 0; |
| 382 |
virtual SPIRVEntry *getOrAddAsmTargetINTEL(const std::string &) = 0; |
| 383 |
virtual SPIRVValue *addAsmINTEL(SPIRVTypeFunction *, SPIRVAsmTargetINTEL *, |
| 384 |
const std::string &, const std::string &) = 0; |
| 385 |
virtual SPIRVInstruction *addAsmCallINTELInst(SPIRVAsmINTEL *, |
| 386 |
const std::vector<SPIRVWord> &, |
| 387 |
SPIRVBasicBlock *) = 0; |
| 388 |
virtual SPIRVInstruction * |
| 389 |
addCompositeConstructInst(SPIRVType *, const std::vector<SPIRVId> &, |
| 390 |
SPIRVBasicBlock *) = 0; |
| 391 |
virtual SPIRVInstruction * |
| 392 |
addCompositeExtractInst(SPIRVType *, SPIRVValue *, |
| 393 |
const std::vector<SPIRVWord> &, |
| 394 |
SPIRVBasicBlock *) = 0; |
| 395 |
virtual SPIRVInstruction * |
| 396 |
addCompositeInsertInst(SPIRVValue *, SPIRVValue *, |
| 397 |
const std::vector<SPIRVWord> &, SPIRVBasicBlock *) = 0; |
| 398 |
virtual SPIRVInstruction *addCopyObjectInst(SPIRVType *, SPIRVValue *, |
| 399 |
SPIRVBasicBlock *) = 0; |
| 400 |
virtual SPIRVInstruction *addCopyMemoryInst(SPIRVValue *, SPIRVValue *, |
| 401 |
const std::vector<SPIRVWord> &, |
| 402 |
SPIRVBasicBlock *) = 0; |
| 403 |
virtual SPIRVInstruction * |
| 404 |
addCopyMemorySizedInst(SPIRVValue *, SPIRVValue *, SPIRVValue *, |
| 405 |
const std::vector<SPIRVWord> &, SPIRVBasicBlock *) = 0; |
| 406 |
virtual SPIRVInstruction *addCmpInst(Op, SPIRVType *, SPIRVValue *, |
| 407 |
SPIRVValue *, SPIRVBasicBlock *) = 0; |
| 408 |
virtual SPIRVInstruction *addControlBarrierInst(SPIRVValue *ExecKind, |
| 409 |
SPIRVValue *MemKind, |
| 410 |
SPIRVValue *MemSema, |
| 411 |
SPIRVBasicBlock *BB) = 0; |
| 412 |
virtual SPIRVInstruction *addGroupInst(Op OpCode, SPIRVType *Type, |
| 413 |
Scope Scope, |
| 414 |
const std::vector<SPIRVValue *> &Ops, |
| 415 |
SPIRVBasicBlock *BB) = 0; |
| 416 |
virtual SPIRVInstTemplateBase *addInstTemplate(Op OC, SPIRVBasicBlock *BB, |
| 417 |
SPIRVType *Ty) = 0; |
| 418 |
virtual SPIRVInstTemplateBase * |
| 419 |
addInstTemplate(Op OC, const std::vector<SPIRVWord> &Ops, SPIRVBasicBlock *BB, |
| 420 |
SPIRVType *Ty) = 0; |
| 421 |
virtual void addInstTemplate(SPIRVInstTemplateBase *Ins, |
| 422 |
const std::vector<SPIRVWord> &Ops, |
| 423 |
SPIRVBasicBlock *BB, SPIRVType *Ty) = 0; |
| 424 |
virtual SPIRVInstruction *addLoadInst(SPIRVValue *, |
| 425 |
const std::vector<SPIRVWord> &, |
| 426 |
SPIRVBasicBlock *) = 0; |
| 427 |
virtual SPIRVInstruction *addLifetimeInst(Op OC, SPIRVValue *Object, |
| 428 |
SPIRVWord Size, |
| 429 |
SPIRVBasicBlock *BB) = 0; |
| 430 |
virtual SPIRVInstruction *addMemoryBarrierInst(Scope ScopeKind, |
| 431 |
SPIRVWord MemFlag, |
| 432 |
SPIRVBasicBlock *BB) = 0; |
| 433 |
virtual SPIRVInstruction *addPhiInst(SPIRVType *, std::vector<SPIRVValue *>, |
| 434 |
SPIRVBasicBlock *) = 0; |
| 435 |
virtual SPIRVInstruction *addUnreachableInst(SPIRVBasicBlock *) = 0; |
| 436 |
virtual SPIRVInstruction *addReturnInst(SPIRVBasicBlock *) = 0; |
| 437 |
virtual SPIRVInstruction *addReturnValueInst(SPIRVValue *, |
| 438 |
SPIRVBasicBlock *) = 0; |
| 439 |
virtual SPIRVInstruction *addSelectInst(SPIRVValue *, SPIRVValue *, |
| 440 |
SPIRVValue *, SPIRVBasicBlock *) = 0; |
| 441 |
virtual SPIRVInstruction *addSelectionMergeInst(SPIRVId MergeBlock, |
| 442 |
SPIRVWord SelectionControl, |
| 443 |
SPIRVBasicBlock *BB) = 0; |
| 444 |
virtual SPIRVInstruction *addLoopMergeInst( |
| 445 |
SPIRVId MergeBlock, SPIRVId ContinueTarget, SPIRVWord LoopControl, |
| 446 |
std::vector<SPIRVWord> LoopControlParameters, SPIRVBasicBlock *BB) = 0; |
| 447 |
virtual SPIRVInstruction * |
| 448 |
addLoopControlINTELInst(SPIRVWord LoopControl, |
| 449 |
std::vector<SPIRVWord> LoopControlParameters, |
| 450 |
SPIRVBasicBlock *BB) = 0; |
| 451 |
virtual SPIRVInstruction * |
| 452 |
addFixedPointIntelInst(Op OC, SPIRVType *ResTy, SPIRVValue *Input, |
| 453 |
const std::vector<SPIRVWord> &Ops, |
| 454 |
SPIRVBasicBlock *BB) = 0; |
| 455 |
virtual SPIRVInstruction * |
| 456 |
addArbFloatPointIntelInst(Op OC, SPIRVType *ResTy, SPIRVValue *InA, |
| 457 |
SPIRVValue *InB, const std::vector<SPIRVWord> &Ops, |
| 458 |
SPIRVBasicBlock *BB) = 0; |
| 459 |
virtual SPIRVInstruction *addStoreInst(SPIRVValue *, SPIRVValue *, |
| 460 |
const std::vector<SPIRVWord> &, |
| 461 |
SPIRVBasicBlock *) = 0; |
| 462 |
virtual SPIRVInstruction *addSwitchInst( |
| 463 |
SPIRVValue *, SPIRVBasicBlock *, |
| 464 |
const std::vector<std::pair<std::vector<SPIRVWord>, SPIRVBasicBlock *>> &, |
| 465 |
SPIRVBasicBlock *) = 0; |
| 466 |
virtual SPIRVInstruction *addVectorTimesScalarInst(SPIRVType *TheType, |
| 467 |
SPIRVId TheVector, |
| 468 |
SPIRVId TheScalar, |
| 469 |
SPIRVBasicBlock *BB) = 0; |
| 470 |
virtual SPIRVInstruction *addVectorTimesMatrixInst(SPIRVType *TheType, |
| 471 |
SPIRVId TheVector, |
| 472 |
SPIRVId TheMatrix, |
| 473 |
SPIRVBasicBlock *BB) = 0; |
| 474 |
virtual SPIRVInstruction *addMatrixTimesScalarInst(SPIRVType *TheType, |
| 475 |
SPIRVId TheMatrix, |
| 476 |
SPIRVId TheScalar, |
| 477 |
SPIRVBasicBlock *BB) = 0; |
| 478 |
virtual SPIRVInstruction *addMatrixTimesVectorInst(SPIRVType *TheType, |
| 479 |
SPIRVId TheMatrix, |
| 480 |
SPIRVId TheVector, |
| 481 |
SPIRVBasicBlock *BB) = 0; |
| 482 |
virtual SPIRVInstruction *addMatrixTimesMatrixInst(SPIRVType *TheType, |
| 483 |
SPIRVId M1, SPIRVId M2, |
| 484 |
SPIRVBasicBlock *BB) = 0; |
| 485 |
virtual SPIRVInstruction *addTransposeInst(SPIRVType *TheType, |
| 486 |
SPIRVId TheMatrix, |
| 487 |
SPIRVBasicBlock *BB) = 0; |
| 488 |
virtual SPIRVInstruction *addUnaryInst(Op, SPIRVType *, SPIRVValue *, |
| 489 |
SPIRVBasicBlock *) = 0; |
| 490 |
virtual SPIRVInstruction *addVariable(SPIRVType *, bool, SPIRVLinkageTypeKind, |
| 491 |
SPIRVValue *, const std::string &, |
| 492 |
SPIRVStorageClassKind, |
| 493 |
SPIRVBasicBlock *) = 0; |
| 494 |
virtual SPIRVValue * |
| 495 |
addVectorShuffleInst(SPIRVType *Type, SPIRVValue *Vec1, SPIRVValue *Vec2, |
| 496 |
const std::vector<SPIRVWord> &Components, |
| 497 |
SPIRVBasicBlock *BB) = 0; |
| 498 |
virtual SPIRVInstruction *addVectorExtractDynamicInst(SPIRVValue *, |
| 499 |
SPIRVValue *, |
| 500 |
SPIRVBasicBlock *) = 0; |
| 501 |
virtual SPIRVInstruction *addVectorInsertDynamicInst(SPIRVValue *, |
| 502 |
SPIRVValue *, |
| 503 |
SPIRVValue *, |
| 504 |
SPIRVBasicBlock *) = 0; |
| 505 |
virtual SPIRVInstruction *addFPGARegINTELInst(SPIRVType *, SPIRVValue *, |
| 506 |
SPIRVBasicBlock *) = 0; |
| 507 |
virtual SPIRVInstruction *addSampledImageInst(SPIRVType *, SPIRVValue *, |
| 508 |
SPIRVValue *, |
| 509 |
SPIRVBasicBlock *) = 0; |
| 510 |
virtual SPIRVEntry *getOrAddAliasDomainDeclINTELInst( |
| 511 |
std::vector<SPIRVId> Args, llvm::MDNode *MD) = 0; |
| 512 |
virtual SPIRVEntry *getOrAddAliasScopeDeclINTELInst( |
| 513 |
std::vector<SPIRVId> Args, llvm::MDNode *MD) = 0; |
| 514 |
virtual SPIRVEntry *getOrAddAliasScopeListDeclINTELInst( |
| 515 |
std::vector<SPIRVId> Args, llvm::MDNode *MD) = 0; |
| 516 |
virtual SPIRVInstruction *addAssumeTrueKHRInst(SPIRVValue *Condition, |
| 517 |
SPIRVBasicBlock *BB) = 0; |
| 518 |
virtual SPIRVInstruction *addExpectKHRInst(SPIRVType *ResultTy, |
| 519 |
SPIRVValue *Value, |
| 520 |
SPIRVValue *ExpectedValue, |
| 521 |
SPIRVBasicBlock *BB) = 0; |
| 522 |
|
| 523 |
virtual SPIRVId getExtInstSetId(SPIRVExtInstSetKind Kind) const = 0; |
| 524 |
|
| 525 |
virtual bool |
| 526 |
isAllowedToUseVersion(SPIRV::VersionNumber RequestedVersion) const final { |
| 527 |
return TranslationOpts.isAllowedToUseVersion(RequestedVersion); |
| 528 |
} |
| 529 |
|
| 530 |
virtual bool isAllowedToUseVersion(SPIRVWord RequestedVersion) const final { |
| 531 |
return TranslationOpts.isAllowedToUseVersion( |
| 532 |
static_cast<SPIRV::VersionNumber>(RequestedVersion)); |
| 533 |
} |
| 534 |
|
| 535 |
virtual SPIRV::VersionNumber getMaximumAllowedSPIRVVersion() const final { |
| 536 |
return TranslationOpts.getMaxVersion(); |
| 537 |
} |
| 538 |
|
| 539 |
virtual bool |
| 540 |
isAllowedToUseExtension(ExtensionID RequestedExtension) const final { |
| 541 |
return TranslationOpts.isAllowedToUseExtension(RequestedExtension); |
| 542 |
} |
| 543 |
|
| 544 |
virtual bool isGenArgNameMDEnabled() const final { |
| 545 |
return TranslationOpts.isGenArgNameMDEnabled(); |
| 546 |
} |
| 547 |
|
| 548 |
virtual std::vector<SPIRVModuleProcessed *> getModuleProcessedVec() = 0; |
| 549 |
|
| 550 |
bool getSpecializationConstant(SPIRVWord SpecId, uint64_t &ConstValue) { |
| 551 |
return TranslationOpts.getSpecializationConstant(SpecId, ConstValue); |
| 552 |
} |
| 553 |
|
| 554 |
void setSpecializationConstant(SPIRVWord SpecId, uint64_t ConstValue) { |
| 555 |
TranslationOpts.setSpecConst(SpecId, ConstValue); |
| 556 |
} |
| 557 |
|
| 558 |
FPContractMode getFPContractMode() const { |
| 559 |
return TranslationOpts.getFPContractMode(); |
| 560 |
} |
| 561 |
|
| 562 |
bool isUnknownIntrinsicAllowed(llvm::IntrinsicInst *II) const noexcept { |
| 563 |
return TranslationOpts.isUnknownIntrinsicAllowed(II); |
| 564 |
} |
| 565 |
|
| 566 |
bool isSPIRVAllowUnknownIntrinsicsEnabled() const noexcept { |
| 567 |
return TranslationOpts.isSPIRVAllowUnknownIntrinsicsEnabled(); |
| 568 |
} |
| 569 |
|
| 570 |
bool allowExtraDIExpressions() const noexcept { |
| 571 |
return TranslationOpts.allowExtraDIExpressions(); |
| 572 |
} |
| 573 |
|
| 574 |
bool shouldReplaceLLVMFmulAddWithOpenCLMad() const noexcept { |
| 575 |
return TranslationOpts.shouldReplaceLLVMFmulAddWithOpenCLMad(); |
| 576 |
} |
| 577 |
|
| 578 |
bool shouldPreserveOCLKernelArgTypeMetadataThroughString() const noexcept { |
| 579 |
return TranslationOpts |
| 580 |
.shouldPreserveOCLKernelArgTypeMetadataThroughString(); |
| 581 |
} |
| 582 |
|
| 583 |
bool shouldEmitFunctionPtrAddrSpace() const noexcept { |
| 584 |
return TranslationOpts.shouldEmitFunctionPtrAddrSpace(); |
| 585 |
} |
| 586 |
|
| 587 |
bool preserveAuxData() const noexcept { |
| 588 |
return TranslationOpts.preserveAuxData(); |
| 589 |
} |
| 590 |
|
| 591 |
BuiltinFormat getBuiltinFormat() const noexcept { |
| 592 |
return TranslationOpts.getBuiltinFormat(); |
| 593 |
} |
| 594 |
|
| 595 |
SPIRVExtInstSetKind getDebugInfoEIS() const { |
| 596 |
switch (TranslationOpts.getDebugInfoEIS()) { |
| 597 |
case DebugInfoEIS::SPIRV_Debug: |
| 598 |
return SPIRVEIS_Debug; |
| 599 |
case DebugInfoEIS::OpenCL_DebugInfo_100: |
| 600 |
return SPIRVEIS_OpenCL_DebugInfo_100; |
| 601 |
case DebugInfoEIS::NonSemantic_Shader_DebugInfo_100: |
| 602 |
return SPIRVEIS_NonSemantic_Shader_DebugInfo_100; |
| 603 |
case DebugInfoEIS::NonSemantic_Shader_DebugInfo_200: |
| 604 |
return SPIRVEIS_NonSemantic_Shader_DebugInfo_200; |
| 605 |
} |
| 606 |
assert(false && "Unexpected debug info EIS!"); |
| 607 |
return SPIRVEIS_Debug; |
| 608 |
} |
| 609 |
|
| 610 |
BIsRepresentation getDesiredBIsRepresentation() const { |
| 611 |
return TranslationOpts.getDesiredBIsRepresentation(); |
| 612 |
} |
| 613 |
|
| 614 |
std::optional<uint32_t> getFnVarCategory() const { |
| 615 |
return TranslationOpts.getFnVarCategory(); |
| 616 |
} |
| 617 |
std::optional<uint32_t> getFnVarFamily() const { |
| 618 |
return TranslationOpts.getFnVarFamily(); |
| 619 |
} |
| 620 |
std::optional<uint32_t> getFnVarArch() const { |
| 621 |
return TranslationOpts.getFnVarArch(); |
| 622 |
} |
| 623 |
std::optional<uint32_t> getFnVarTarget() const { |
| 624 |
return TranslationOpts.getFnVarTarget(); |
| 625 |
} |
| 626 |
std::vector<uint32_t> getFnVarFeatures() const { |
| 627 |
return TranslationOpts.getFnVarFeatures(); |
| 628 |
} |
| 629 |
std::vector<uint32_t> getFnVarCapabilities() const { |
| 630 |
return TranslationOpts.getFnVarCapabilities(); |
| 631 |
} |
| 632 |
|
| 633 |
std::string getFnVarSpvOut() const { |
| 634 |
return TranslationOpts.getFnVarSpvOut(); |
| 635 |
} |
| 636 |
|
| 637 |
// I/O functions |
| 638 |
friend spv_ostream &operator<<(spv_ostream &O, SPIRVModule &M); |
| 639 |
friend std::istream &operator>>(std::istream &I, SPIRVModule &M); |
| 640 |
|
| 641 |
protected: |
| 642 |
bool AutoAddCapability; |
| 643 |
bool ValidateCapability; |
| 644 |
bool AutoAddExtensions = true; |
| 645 |
SPIRV::TranslatorOpts TranslationOpts; |
| 646 |
|
| 647 |
private: |
| 648 |
bool IsValid; |
| 649 |
}; |
| 650 |
|
| 651 |
|
| 652 |
#ifdef _SPIRV_SUPPORT_TEXT_FMT |
| 653 |
|
| 654 |
/// Convert SPIR-V between binary and internel text formats. |
| 655 |
/// This function is not thread safe and should not be used in multi-thread |
| 656 |
/// applications unless guarded by a critical section. |
| 657 |
bool ConvertSPIRV(std::istream &IS, spv_ostream &OS, std::string &ErrMsg, |
| 658 |
bool FromText, bool ToText); |
| 659 |
|
| 660 |
/// Convert SPIR-V between binary and internel text formats. |
| 661 |
/// This function is not thread safe and should not be used in multi-thread |
| 662 |
/// applications unless guarded by a critical section. |
| 663 |
bool ConvertSPIRV(std::string &Input, std::string &Out, std::string &ErrMsg, |
| 664 |
bool ToText); |
| 665 |
#endif |
| 666 |
} // namespace SPIRV |
| 667 |
|
| 668 |
#endif // SPIRV_LIBSPIRV_SPIRVMODULE_H |