| 1 |
//===- SPIRVModule.cpp - Class to represent 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 implements Module class for SPIR-V. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#include "SPIRVModule.h" |
| 41 |
#include "SPIRVAsm.h" |
| 42 |
#include "SPIRVDebug.h" |
| 43 |
#include "SPIRVEntry.h" |
| 44 |
#include "SPIRVEnum.h" |
| 45 |
#include "SPIRVFnVar.h" |
| 46 |
#include "SPIRVFunction.h" |
| 47 |
#include "SPIRVInstruction.h" |
| 48 |
#include "SPIRVMemAliasingINTEL.h" |
| 49 |
#include "SPIRVNameMapEnum.h" |
| 50 |
#include "SPIRVOpCode.h" |
| 51 |
#include "SPIRVStream.h" |
| 52 |
#include "SPIRVType.h" |
| 53 |
#include "SPIRVValue.h" |
| 54 |
|
| 55 |
#include "llvm/ADT/APInt.h" |
| 56 |
|
| 57 |
#include <set> |
| 58 |
#include <unordered_map> |
| 59 |
#include <unordered_set> |
| 60 |
|
| 61 |
namespace SPIRV { |
| 62 |
|
| 63 |
namespace { |
| 64 |
std::string to_string(uint32_t Version) { |
| 65 |
std::string Res; |
| 66 |
switch (Version) { |
| 67 |
case static_cast<uint32_t>(VersionNumber::SPIRV_1_0): |
| 68 |
Res = "1.0"; |
| 69 |
break; |
| 70 |
case static_cast<uint32_t>(VersionNumber::SPIRV_1_1): |
| 71 |
Res = "1.1"; |
| 72 |
break; |
| 73 |
case static_cast<uint32_t>(VersionNumber::SPIRV_1_2): |
| 74 |
Res = "1.2"; |
| 75 |
break; |
| 76 |
case static_cast<uint32_t>(VersionNumber::SPIRV_1_3): |
| 77 |
Res = "1.3"; |
| 78 |
break; |
| 79 |
case static_cast<uint32_t>(VersionNumber::SPIRV_1_4): |
| 80 |
Res = "1.4"; |
| 81 |
break; |
| 82 |
case static_cast<uint32_t>(VersionNumber::SPIRV_1_5): |
| 83 |
Res = "1.5"; |
| 84 |
break; |
| 85 |
case static_cast<uint32_t>(VersionNumber::SPIRV_1_6): |
| 86 |
Res = "1.6"; |
| 87 |
break; |
| 88 |
default: |
| 89 |
Res = "unknown"; |
| 90 |
} |
| 91 |
|
| 92 |
Res += " (" + std::to_string(Version) + ")"; |
| 93 |
return Res; |
| 94 |
} |
| 95 |
|
| 96 |
std::string to_string(VersionNumber Version) { |
| 97 |
return to_string(static_cast<uint32_t>(Version)); |
| 98 |
} |
| 99 |
} // Anonymous namespace |
| 100 |
|
| 101 |
SPIRVModule::SPIRVModule() |
| 102 |
: AutoAddCapability(true), ValidateCapability(false), IsValid(true) {} |
| 103 |
|
| 104 |
SPIRVModule::~SPIRVModule() {} |
| 105 |
|
| 106 |
class SPIRVModuleImpl : public SPIRVModule { |
| 107 |
public: |
| 108 |
SPIRVModuleImpl() |
| 109 |
: SPIRVModule(), NextId(1), |
| 110 |
SPIRVVersion(static_cast<SPIRVWord>(VersionNumber::SPIRV_1_0)), |
| 111 |
GeneratorId(SPIRVGEN_KhronosLLVMSPIRVTranslator), GeneratorVer(0), |
| 112 |
InstSchema(SPIRVISCH_Default), SrcLang(SourceLanguageOpenCL_C), |
| 113 |
SrcLangVer(102000) { |
| 114 |
AddrModel = sizeof(size_t) == 32 ? AddressingModelPhysical32 |
| 115 |
: AddressingModelPhysical64; |
| 116 |
// OpenCL memory model requires Kernel capability |
| 117 |
setMemoryModel(MemoryModelOpenCL); |
| 118 |
} |
| 119 |
|
| 120 |
SPIRVModuleImpl(const SPIRV::TranslatorOpts &Opts) : SPIRVModuleImpl() { |
| 121 |
TranslationOpts = Opts; |
| 122 |
} |
| 123 |
|
| 124 |
~SPIRVModuleImpl() override; |
| 125 |
|
| 126 |
// Object query functions |
| 127 |
bool exist(SPIRVId) const override; |
| 128 |
bool exist(SPIRVId, SPIRVEntry **) const override; |
| 129 |
SPIRVId getId(SPIRVId Id = SPIRVID_INVALID, unsigned Increment = 1); |
| 130 |
SPIRVEntry *getEntry(SPIRVId Id) const override; |
| 131 |
// If we have at least on OpLine in the module the CurrentLine is non-empty |
| 132 |
bool hasDebugInfo() const override { |
| 133 |
return CurrentLine.get() || !DebugInstVec.empty(); |
| 134 |
} |
| 135 |
|
| 136 |
// Error handling functions |
| 137 |
SPIRVErrorLog &getErrorLog() override { return ErrLog; } |
| 138 |
SPIRVErrorCode getError(std::string &ErrMsg) override { |
| 139 |
return ErrLog.getError(ErrMsg); |
| 140 |
} |
| 141 |
bool checkExtension(ExtensionID Ext, SPIRVErrorCode ErrCode, |
| 142 |
const std::string &Msg) override { |
| 143 |
if (ErrLog.checkError(isAllowedToUseExtension(Ext), ErrCode, Msg)) |
| 144 |
return true; |
| 145 |
setInvalid(); |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
// Module query functions |
| 150 |
SPIRVAddressingModelKind getAddressingModel() override { return AddrModel; } |
| 151 |
SPIRVExtInstSetKind getBuiltinSet(SPIRVId SetId) const override; |
| 152 |
const SPIRVCapMap &getCapability() const override { return CapMap; } |
| 153 |
const SPIRVConditionalCapMap &getConditionalCapabilities() const override { |
| 154 |
return ConditionalCapMap; |
| 155 |
} |
| 156 |
const SPIRVConditionalEntryPointVec & |
| 157 |
getConditionalEntryPoints() const override { |
| 158 |
return ConditionalEntryPointVec; |
| 159 |
} |
| 160 |
bool hasCapability(SPIRVCapabilityKind Cap) const override { |
| 161 |
return CapMap.find(Cap) != CapMap.end(); |
| 162 |
} |
| 163 |
std::set<std::string> &getExtension() override { return SPIRVExt; } |
| 164 |
SPIRVConditionalExtensionSet &getConditionalExtensions() override { |
| 165 |
return SPIRVCondExt; |
| 166 |
} |
| 167 |
SPIRVFunction *getFunction(unsigned I) const override { return FuncVec[I]; } |
| 168 |
SPIRVVariable *getVariable(unsigned I) const override { |
| 169 |
return VariableVec[I]; |
| 170 |
} |
| 171 |
SPIRVValue *getConst(unsigned I) const override { return ConstVec[I]; } |
| 172 |
std::vector<SPIRVDecorateGeneric *> *getDecorateVec() override { |
| 173 |
return &DecorateVec; |
| 174 |
} |
| 175 |
std::vector<SPIRVFunction *> *getFuncVec() override { return &FuncVec; } |
| 176 |
SPIRVValue *getValue(SPIRVId TheId) const override; |
| 177 |
std::vector<SPIRVValue *> |
| 178 |
getValues(const std::vector<SPIRVId> &) const override; |
| 179 |
std::vector<SPIRVId> getIds(const std::vector<SPIRVEntry *> &) const override; |
| 180 |
std::vector<SPIRVId> getIds(const std::vector<SPIRVValue *> &) const override; |
| 181 |
SPIRVType *getValueType(SPIRVId TheId) const override; |
| 182 |
std::vector<SPIRVType *> |
| 183 |
getValueTypes(const std::vector<SPIRVId> &) const override; |
| 184 |
SPIRVMemoryModelKind getMemoryModel() const override { return MemoryModel; } |
| 185 |
SPIRVConstant *getLiteralAsConstant(unsigned Literal) override; |
| 186 |
unsigned getNumFunctions() const override { return FuncVec.size(); } |
| 187 |
unsigned getNumVariables() const override { return VariableVec.size(); } |
| 188 |
unsigned getNumConsts() const override { return ConstVec.size(); } |
| 189 |
std::vector<SPIRVValue *> getFunctionPointers() const override { |
| 190 |
std::vector<SPIRVValue *> Res; |
| 191 |
for (auto *C : ConstVec) |
| 192 |
if (C->getOpCode() == OpConstantFunctionPointerINTEL) |
| 193 |
Res.emplace_back(C); |
| 194 |
return Res; |
| 195 |
} |
| 196 |
SourceLanguage getSourceLanguage(SPIRVWord *Ver = nullptr) const override { |
| 197 |
if (Ver) |
| 198 |
*Ver = SrcLangVer; |
| 199 |
return SrcLang; |
| 200 |
} |
| 201 |
std::set<std::string> &getSourceExtension() override { return SrcExtension; } |
| 202 |
bool isEntryPoint(SPIRVExecutionModelKind, SPIRVId EP) const override; |
| 203 |
unsigned short getGeneratorId() const override { return GeneratorId; } |
| 204 |
unsigned short getGeneratorVer() const override { return GeneratorVer; } |
| 205 |
SPIRVWord getSPIRVVersion() const override { return SPIRVVersion; } |
| 206 |
const std::vector<SPIRVExtInst *> &getDebugInstVec() const override { |
| 207 |
return DebugInstVec; |
| 208 |
} |
| 209 |
const std::vector<SPIRVExtInst *> &getAuxDataInstVec() const override { |
| 210 |
return AuxDataInstVec; |
| 211 |
} |
| 212 |
const std::vector<SPIRVString *> &getStringVec() const override { |
| 213 |
return StringVec; |
| 214 |
} |
| 215 |
// Module changing functions |
| 216 |
bool importBuiltinSet(const std::string &, SPIRVId *) override; |
| 217 |
bool importBuiltinSetWithId(const std::string &, SPIRVId) override; |
| 218 |
void setAddressingModel(SPIRVAddressingModelKind AM) override { |
| 219 |
AddrModel = AM; |
| 220 |
} |
| 221 |
void setAlignment(SPIRVValue *, SPIRVWord) override; |
| 222 |
void setMemoryModel(SPIRVMemoryModelKind MM) override { |
| 223 |
MemoryModel = MM; |
| 224 |
if (MemoryModel == spv::MemoryModelOpenCL) |
| 225 |
addCapability(CapabilityKernel); |
| 226 |
} |
| 227 |
void setName(SPIRVEntry *E, const std::string &Name) override; |
| 228 |
void setSourceLanguage(SourceLanguage Lang, SPIRVWord Ver) override { |
| 229 |
SrcLang = Lang; |
| 230 |
SrcLangVer = Ver; |
| 231 |
} |
| 232 |
void setGeneratorId(unsigned short Id) override { GeneratorId = Id; } |
| 233 |
void setGeneratorVer(unsigned short Ver) override { GeneratorVer = Ver; } |
| 234 |
void resolveUnknownStructFields() override; |
| 235 |
void insertEntryNoId(SPIRVEntry *Entry) override { EntryNoId.insert(Entry); } |
| 236 |
|
| 237 |
void setSPIRVVersion(SPIRVWord Ver) override { |
| 238 |
if (!this->isAllowedToUseVersion(static_cast<VersionNumber>(Ver))) { |
| 239 |
std::stringstream SS; |
| 240 |
SS << "SPIR-V version was restricted to at most " |
| 241 |
<< to_string(getMaximumAllowedSPIRVVersion()) |
| 242 |
<< " but a construct from the input requires SPIR-V version " |
| 243 |
<< to_string(Ver) << " or above\n"; |
| 244 |
getErrorLog().checkError(false, SPIRVEC_RequiresVersion, SS.str()); |
| 245 |
setInvalid(); |
| 246 |
return; |
| 247 |
} |
| 248 |
SPIRVVersion = Ver; |
| 249 |
} |
| 250 |
|
| 251 |
bool eraseReferencesOfInst(SPIRVId Id) override { |
| 252 |
const auto *const Entry = getEntry(Id); |
| 253 |
|
| 254 |
if (!Entry) |
| 255 |
return false; |
| 256 |
|
| 257 |
if (!Entry->hasId()) |
| 258 |
return false; |
| 259 |
|
| 260 |
// Remove all OpNames referencing the Id |
| 261 |
if (NamedId.find(Id) != NamedId.end()) |
| 262 |
NamedId.erase(Id); |
| 263 |
|
| 264 |
// Remove all OpMemberNames referencing the Id |
| 265 |
erase_if(MemberNameVec, [Id](auto M) { return M->getTargetId() == Id; }); |
| 266 |
|
| 267 |
// Remove all decorations of the Id |
| 268 |
erase_if(DecorateVec, [Id](auto D) { return D->getTargetId() == Id; }); |
| 269 |
|
| 270 |
// If Id points to a function, remove its OpEntryPoint, OpExecutionMode |
| 271 |
// and OpExecutionModeId |
| 272 |
if (Entry->getOpCode() == OpFunction) |
| 273 |
erase_if(EntryPointVec, |
| 274 |
[Id](auto EP) { return EP->getTargetId() == Id; }); |
| 275 |
|
| 276 |
return true; |
| 277 |
} |
| 278 |
|
| 279 |
void eraseCapability(SPIRVCapabilityKind CapKind) override { |
| 280 |
CapMap.erase(CapKind); |
| 281 |
} |
| 282 |
|
| 283 |
// Object creation functions |
| 284 |
template <class T> void addTo(std::vector<T *> &V, SPIRVEntry *E); |
| 285 |
SPIRVEntry *addEntry(SPIRVEntry *E) override; |
| 286 |
SPIRVBasicBlock *addBasicBlock(SPIRVFunction *, SPIRVId) override; |
| 287 |
SPIRVString *getString(const std::string &Str) override; |
| 288 |
SPIRVMemberName *addMemberName(SPIRVTypeStruct *ST, SPIRVWord MemberNumber, |
| 289 |
const std::string &Name) override; |
| 290 |
void addUnknownStructField(SPIRVTypeStruct *Struct, unsigned I, |
| 291 |
SPIRVId ID) override; |
| 292 |
void addLine(SPIRVEntry *E, SPIRVId FileNameId, SPIRVWord Line, |
| 293 |
SPIRVWord Column) override; |
| 294 |
const std::shared_ptr<const SPIRVLine> &getCurrentLine() const override; |
| 295 |
void setCurrentLine(const std::shared_ptr<const SPIRVLine> &Line) override; |
| 296 |
void addDebugLine(SPIRVEntry *E, SPIRVType *TheType, SPIRVId FileNameId, |
| 297 |
SPIRVWord LineStart, SPIRVWord LineEnd, |
| 298 |
SPIRVWord ColumnStart, SPIRVWord ColumnEnd) override; |
| 299 |
const std::shared_ptr<const SPIRVExtInst> & |
| 300 |
getCurrentDebugLine() const override; |
| 301 |
void setCurrentDebugLine( |
| 302 |
const std::shared_ptr<const SPIRVExtInst> &DebugLine) override; |
| 303 |
void addCapability(SPIRVCapabilityKind) override; |
| 304 |
void addCapabilityInternal(SPIRVCapabilityKind) override; |
| 305 |
void addConditionalCapability(SPIRVId, SPIRVCapabilityKind) override; |
| 306 |
void eraseConditionalCapability(SPIRVId, SPIRVCapabilityKind) override; |
| 307 |
void addExtension(ExtensionID) override; |
| 308 |
const SPIRVDecorateGeneric *addDecorate(SPIRVDecorateGeneric *) override; |
| 309 |
SPIRVDecorationGroup *addDecorationGroup() override; |
| 310 |
SPIRVDecorationGroup * |
| 311 |
addDecorationGroup(SPIRVDecorationGroup *Group) override; |
| 312 |
SPIRVGroupDecorate * |
| 313 |
addGroupDecorate(SPIRVDecorationGroup *Group, |
| 314 |
const std::vector<SPIRVEntry *> &Targets) override; |
| 315 |
SPIRVGroupDecorateGeneric * |
| 316 |
addGroupDecorateGeneric(SPIRVGroupDecorateGeneric *GDec) override; |
| 317 |
SPIRVGroupMemberDecorate * |
| 318 |
addGroupMemberDecorate(SPIRVDecorationGroup *Group, |
| 319 |
const std::vector<SPIRVEntry *> &Targets) override; |
| 320 |
void addEntryPoint(SPIRVExecutionModelKind ExecModel, SPIRVId EntryPoint, |
| 321 |
const std::string &Name, |
| 322 |
const std::vector<SPIRVId> &Variables) override; |
| 323 |
void addConditionalEntryPoint(SPIRVId, SPIRVExecutionModelKind ExecModel, |
| 324 |
SPIRVId EntryPoint, const std::string &Name, |
| 325 |
const std::vector<SPIRVId> &Variables) override; |
| 326 |
void specializeConditionalEntryPoints(SPIRVId, bool) override; |
| 327 |
SPIRVForward *addForward(SPIRVType *Ty) override; |
| 328 |
SPIRVForward *addForward(SPIRVId, SPIRVType *Ty) override; |
| 329 |
SPIRVFunction *addFunction(SPIRVFunction *) override; |
| 330 |
SPIRVFunction *addFunction(SPIRVTypeFunction *, SPIRVId) override; |
| 331 |
SPIRVEntry *replaceForward(SPIRVForward *, SPIRVEntry *) override; |
| 332 |
void eraseInstruction(SPIRVInstruction *, SPIRVBasicBlock *) override; |
| 333 |
bool eraseValue(SPIRVValue *) override; |
| 334 |
|
| 335 |
// Type creation functions |
| 336 |
template <class T> T *addType(T *Ty); |
| 337 |
SPIRVTypeArray *addArrayType(SPIRVType *, SPIRVConstant *) override; |
| 338 |
SPIRVTypeBool *addBoolType() override; |
| 339 |
SPIRVTypeFloat *addFloatType(unsigned BitWidth, |
| 340 |
unsigned FloatingPointEncoding) override; |
| 341 |
SPIRVTypeFunction *addFunctionType(SPIRVType *, |
| 342 |
const std::vector<SPIRVType *> &) override; |
| 343 |
SPIRVTypeInt *addIntegerType(unsigned BitWidth) override; |
| 344 |
SPIRVTypeOpaque *addOpaqueType(const std::string &) override; |
| 345 |
SPIRVTypePointer *addPointerType(SPIRVStorageClassKind, SPIRVType *) override; |
| 346 |
SPIRVTypeImage *addImageType(SPIRVType *, |
| 347 |
const SPIRVTypeImageDescriptor &) override; |
| 348 |
SPIRVTypeImage *addImageType(SPIRVType *, const SPIRVTypeImageDescriptor &, |
| 349 |
SPIRVAccessQualifierKind) override; |
| 350 |
SPIRVTypeSampler *addSamplerType() override; |
| 351 |
SPIRVTypePipeStorage *addPipeStorageType() override; |
| 352 |
SPIRVTypeSampledImage *addSampledImageType(SPIRVTypeImage *T) override; |
| 353 |
SPIRVTypeStruct *openStructType(unsigned, const std::string &) override; |
| 354 |
SPIRVEntry *addTypeStructContinuedINTEL(unsigned NumMembers) override; |
| 355 |
void closeStructType(SPIRVTypeStruct *T, bool) override; |
| 356 |
SPIRVTypeVector *addVectorType(SPIRVType *, SPIRVWord) override; |
| 357 |
SPIRVTypeJointMatrixINTEL * |
| 358 |
addJointMatrixINTELType(SPIRVType *, std::vector<SPIRVValue *>) override; |
| 359 |
SPIRVTypeCooperativeMatrixKHR * |
| 360 |
addCooperativeMatrixKHRType(SPIRVType *, std::vector<SPIRVValue *>) override; |
| 361 |
SPIRVInstruction * |
| 362 |
addCooperativeMatrixLengthKHRInst(SPIRVType *, SPIRVType *, |
| 363 |
SPIRVBasicBlock *) override; |
| 364 |
SPIRVType *addOpaqueGenericType(Op) override; |
| 365 |
SPIRVTypeDeviceEvent *addDeviceEventType() override; |
| 366 |
SPIRVTypeQueue *addQueueType() override; |
| 367 |
SPIRVTypePipe *addPipeType() override; |
| 368 |
SPIRVTypeVoid *addVoidType() override; |
| 369 |
SPIRVType *addSubgroupAvcINTELType(Op) override; |
| 370 |
SPIRVTypeVmeImageINTEL *addVmeImageINTELType(SPIRVTypeImage *T) override; |
| 371 |
SPIRVTypeBufferSurfaceINTEL * |
| 372 |
addBufferSurfaceINTELType(SPIRVAccessQualifierKind Access) override; |
| 373 |
SPIRVTypeTokenINTEL *addTokenTypeINTEL() override; |
| 374 |
|
| 375 |
// Constant creation functions |
| 376 |
SPIRVInstruction *addBranchInst(SPIRVLabel *, SPIRVBasicBlock *) override; |
| 377 |
SPIRVInstruction *addBranchConditionalInst(SPIRVValue *, SPIRVLabel *, |
| 378 |
SPIRVLabel *, |
| 379 |
SPIRVBasicBlock *) override; |
| 380 |
SPIRVValue *addCompositeConstant(SPIRVType *, |
| 381 |
const std::vector<SPIRVValue *> &) override; |
| 382 |
SPIRVEntry *addCompositeConstantContinuedINTEL( |
| 383 |
const std::vector<SPIRVValue *> &) override; |
| 384 |
SPIRVValue * |
| 385 |
addSpecConstantComposite(SPIRVType *Ty, |
| 386 |
const std::vector<SPIRVValue *> &Elements) override; |
| 387 |
SPIRVEntry *addSpecConstantCompositeContinuedINTEL( |
| 388 |
const std::vector<SPIRVValue *> &) override; |
| 389 |
SPIRVValue *addConstantFunctionPointerINTEL(SPIRVType *Ty, |
| 390 |
SPIRVFunction *F) override; |
| 391 |
SPIRVValue *addConstant(SPIRVValue *) override; |
| 392 |
SPIRVValue *addConstant(SPIRVType *, uint64_t) override; |
| 393 |
SPIRVValue *addConstant(SPIRVType *, llvm::APInt) override; |
| 394 |
SPIRVValue *addSpecConstant(SPIRVType *, uint64_t) override; |
| 395 |
SPIRVValue *addDoubleConstant(SPIRVTypeFloat *, double) override; |
| 396 |
SPIRVValue *addFloatConstant(SPIRVTypeFloat *, float) override; |
| 397 |
SPIRVValue *addIntegerConstant(SPIRVTypeInt *, uint64_t) override; |
| 398 |
SPIRVValue *addNullConstant(SPIRVType *) override; |
| 399 |
SPIRVValue *addUndef(SPIRVType *TheType) override; |
| 400 |
SPIRVValue *addSamplerConstant(SPIRVType *TheType, SPIRVWord AddrMode, |
| 401 |
SPIRVWord ParametricMode, |
| 402 |
SPIRVWord FilterMode) override; |
| 403 |
SPIRVValue *addPipeStorageConstant(SPIRVType *TheType, SPIRVWord PacketSize, |
| 404 |
SPIRVWord PacketAlign, |
| 405 |
SPIRVWord Capacity) override; |
| 406 |
|
| 407 |
// Instruction creation functions |
| 408 |
SPIRVInstruction *addPtrAccessChainInst(SPIRVType *, SPIRVValue *, |
| 409 |
std::vector<SPIRVValue *>, |
| 410 |
SPIRVBasicBlock *, bool) override; |
| 411 |
SPIRVInstruction *addAsyncGroupCopy(SPIRVValue *Scope, SPIRVValue *Dest, |
| 412 |
SPIRVValue *Src, SPIRVValue *NumElems, |
| 413 |
SPIRVValue *Stride, SPIRVValue *Event, |
| 414 |
SPIRVBasicBlock *BB) override; |
| 415 |
SPIRVInstruction *addExtInst(SPIRVType *, SPIRVWord, SPIRVWord, |
| 416 |
const std::vector<SPIRVWord> &, |
| 417 |
SPIRVBasicBlock *, |
| 418 |
SPIRVInstruction * = nullptr) override; |
| 419 |
SPIRVInstruction *addExtInst(SPIRVType *, SPIRVWord, SPIRVWord, |
| 420 |
const std::vector<SPIRVValue *> &, |
| 421 |
SPIRVBasicBlock *, |
| 422 |
SPIRVInstruction * = nullptr) override; |
| 423 |
SPIRVEntry *createDebugInfo(SPIRVWord, SPIRVType *TheType, |
| 424 |
const std::vector<SPIRVWord> &) override; |
| 425 |
SPIRVEntry *addDebugInfo(SPIRVWord, SPIRVType *TheType, |
| 426 |
const std::vector<SPIRVWord> &) override; |
| 427 |
SPIRVEntry *addAuxData(SPIRVWord, SPIRVType *TheType, |
| 428 |
const std::vector<SPIRVWord> &) override; |
| 429 |
SPIRVEntry *addModuleProcessed(const std::string &) override; |
| 430 |
std::vector<SPIRVModuleProcessed *> getModuleProcessedVec() override; |
| 431 |
SPIRVInstruction *addBinaryInst(Op, SPIRVType *, SPIRVValue *, SPIRVValue *, |
| 432 |
SPIRVBasicBlock *) override; |
| 433 |
SPIRVInstruction *addCallInst(SPIRVFunction *, const std::vector<SPIRVWord> &, |
| 434 |
SPIRVBasicBlock *) override; |
| 435 |
SPIRVInstruction *addIndirectCallInst(SPIRVValue *, SPIRVType *, |
| 436 |
const std::vector<SPIRVWord> &, |
| 437 |
SPIRVBasicBlock *) override; |
| 438 |
SPIRVEntry *getOrAddAsmTargetINTEL(const std::string &) override; |
| 439 |
SPIRVValue *addAsmINTEL(SPIRVTypeFunction *, SPIRVAsmTargetINTEL *, |
| 440 |
const std::string &, const std::string &) override; |
| 441 |
SPIRVInstruction *addAsmCallINTELInst(SPIRVAsmINTEL *, |
| 442 |
const std::vector<SPIRVWord> &, |
| 443 |
SPIRVBasicBlock *) override; |
| 444 |
SPIRVInstruction *addCmpInst(Op, SPIRVType *, SPIRVValue *, SPIRVValue *, |
| 445 |
SPIRVBasicBlock *) override; |
| 446 |
SPIRVInstruction *addLoadInst(SPIRVValue *, const std::vector<SPIRVWord> &, |
| 447 |
SPIRVBasicBlock *) override; |
| 448 |
SPIRVInstruction *addPhiInst(SPIRVType *, std::vector<SPIRVValue *>, |
| 449 |
SPIRVBasicBlock *) override; |
| 450 |
SPIRVInstruction *addCompositeConstructInst(SPIRVType *, |
| 451 |
const std::vector<SPIRVId> &, |
| 452 |
SPIRVBasicBlock *) override; |
| 453 |
SPIRVInstruction *addCompositeExtractInst(SPIRVType *, SPIRVValue *, |
| 454 |
const std::vector<SPIRVWord> &, |
| 455 |
SPIRVBasicBlock *) override; |
| 456 |
SPIRVInstruction * |
| 457 |
addCompositeInsertInst(SPIRVValue *Object, SPIRVValue *Composite, |
| 458 |
const std::vector<SPIRVWord> &Indices, |
| 459 |
SPIRVBasicBlock *BB) override; |
| 460 |
SPIRVInstruction *addCopyObjectInst(SPIRVType *TheType, SPIRVValue *Operand, |
| 461 |
SPIRVBasicBlock *BB) override; |
| 462 |
SPIRVInstruction *addCopyMemoryInst(SPIRVValue *, SPIRVValue *, |
| 463 |
const std::vector<SPIRVWord> &, |
| 464 |
SPIRVBasicBlock *) override; |
| 465 |
SPIRVInstruction *addCopyMemorySizedInst(SPIRVValue *, SPIRVValue *, |
| 466 |
SPIRVValue *, |
| 467 |
const std::vector<SPIRVWord> &, |
| 468 |
SPIRVBasicBlock *) override; |
| 469 |
SPIRVInstruction *addControlBarrierInst(SPIRVValue *ExecKind, |
| 470 |
SPIRVValue *MemKind, |
| 471 |
SPIRVValue *MemSema, |
| 472 |
SPIRVBasicBlock *BB) override; |
| 473 |
SPIRVInstruction *addGroupInst(Op OpCode, SPIRVType *Type, Scope Scope, |
| 474 |
const std::vector<SPIRVValue *> &Ops, |
| 475 |
SPIRVBasicBlock *BB) override; |
| 476 |
virtual SPIRVInstruction * |
| 477 |
addInstruction(SPIRVInstruction *Inst, SPIRVBasicBlock *BB, |
| 478 |
SPIRVInstruction *InsertBefore = nullptr); |
| 479 |
SPIRVInstTemplateBase *addInstTemplate(Op OC, SPIRVBasicBlock *BB, |
| 480 |
SPIRVType *Ty) override; |
| 481 |
SPIRVInstTemplateBase *addInstTemplate(Op OC, |
| 482 |
const std::vector<SPIRVWord> &Ops, |
| 483 |
SPIRVBasicBlock *BB, |
| 484 |
SPIRVType *Ty) override; |
| 485 |
void addInstTemplate(SPIRVInstTemplateBase *Ins, |
| 486 |
const std::vector<SPIRVWord> &Ops, SPIRVBasicBlock *BB, |
| 487 |
SPIRVType *Ty) override; |
| 488 |
SPIRVInstruction *addLifetimeInst(Op OC, SPIRVValue *Object, SPIRVWord Size, |
| 489 |
SPIRVBasicBlock *BB) override; |
| 490 |
SPIRVInstruction *addMemoryBarrierInst(Scope ScopeKind, SPIRVWord MemFlag, |
| 491 |
SPIRVBasicBlock *BB) override; |
| 492 |
SPIRVInstruction *addUnreachableInst(SPIRVBasicBlock *) override; |
| 493 |
SPIRVInstruction *addReturnInst(SPIRVBasicBlock *) override; |
| 494 |
SPIRVInstruction *addReturnValueInst(SPIRVValue *, |
| 495 |
SPIRVBasicBlock *) override; |
| 496 |
SPIRVInstruction *addSelectInst(SPIRVValue *, SPIRVValue *, SPIRVValue *, |
| 497 |
SPIRVBasicBlock *) override; |
| 498 |
SPIRVInstruction * |
| 499 |
addLoopMergeInst(SPIRVId MergeBlock, SPIRVId ContinueTarget, |
| 500 |
SPIRVWord LoopControl, |
| 501 |
std::vector<SPIRVWord> LoopControlParameters, |
| 502 |
SPIRVBasicBlock *BB) override; |
| 503 |
SPIRVInstruction * |
| 504 |
addLoopControlINTELInst(SPIRVWord LoopControl, |
| 505 |
std::vector<SPIRVWord> LoopControlParameters, |
| 506 |
SPIRVBasicBlock *BB) override; |
| 507 |
SPIRVInstruction *addFixedPointIntelInst(Op OC, SPIRVType *ResTy, |
| 508 |
SPIRVValue *Input, |
| 509 |
const std::vector<SPIRVWord> &Ops, |
| 510 |
SPIRVBasicBlock *BB) override; |
| 511 |
SPIRVInstruction *addArbFloatPointIntelInst(Op OC, SPIRVType *ResTy, |
| 512 |
SPIRVValue *InA, SPIRVValue *InB, |
| 513 |
const std::vector<SPIRVWord> &Ops, |
| 514 |
SPIRVBasicBlock *BB) override; |
| 515 |
SPIRVInstruction *addSelectionMergeInst(SPIRVId MergeBlock, |
| 516 |
SPIRVWord SelectionControl, |
| 517 |
SPIRVBasicBlock *BB) override; |
| 518 |
SPIRVInstruction *addStoreInst(SPIRVValue *, SPIRVValue *, |
| 519 |
const std::vector<SPIRVWord> &, |
| 520 |
SPIRVBasicBlock *) override; |
| 521 |
SPIRVInstruction *addSwitchInst( |
| 522 |
SPIRVValue *, SPIRVBasicBlock *, |
| 523 |
const std::vector<std::pair<std::vector<SPIRVWord>, SPIRVBasicBlock *>> &, |
| 524 |
SPIRVBasicBlock *) override; |
| 525 |
SPIRVInstruction *addVectorTimesScalarInst(SPIRVType *TheType, |
| 526 |
SPIRVId TheVector, |
| 527 |
SPIRVId TheScalar, |
| 528 |
SPIRVBasicBlock *BB) override; |
| 529 |
SPIRVInstruction *addVectorTimesMatrixInst(SPIRVType *TheType, |
| 530 |
SPIRVId TheVector, |
| 531 |
SPIRVId TheScalar, |
| 532 |
SPIRVBasicBlock *BB) override; |
| 533 |
SPIRVInstruction *addMatrixTimesScalarInst(SPIRVType *TheType, |
| 534 |
SPIRVId TheMatrix, |
| 535 |
SPIRVId TheScalar, |
| 536 |
SPIRVBasicBlock *BB) override; |
| 537 |
SPIRVInstruction *addMatrixTimesVectorInst(SPIRVType *TheType, |
| 538 |
SPIRVId TheMatrix, |
| 539 |
SPIRVId TheVector, |
| 540 |
SPIRVBasicBlock *BB) override; |
| 541 |
SPIRVInstruction *addMatrixTimesMatrixInst(SPIRVType *TheType, SPIRVId M1, |
| 542 |
SPIRVId M2, |
| 543 |
SPIRVBasicBlock *BB) override; |
| 544 |
SPIRVInstruction *addTransposeInst(SPIRVType *TheType, SPIRVId TheMatrix, |
| 545 |
SPIRVBasicBlock *BB) override; |
| 546 |
SPIRVInstruction *addUnaryInst(Op, SPIRVType *, SPIRVValue *, |
| 547 |
SPIRVBasicBlock *) override; |
| 548 |
SPIRVInstruction *addVariable(SPIRVType *, bool, SPIRVLinkageTypeKind, |
| 549 |
SPIRVValue *, const std::string &, |
| 550 |
SPIRVStorageClassKind, |
| 551 |
SPIRVBasicBlock *) override; |
| 552 |
SPIRVValue *addVectorShuffleInst(SPIRVType *Type, SPIRVValue *Vec1, |
| 553 |
SPIRVValue *Vec2, |
| 554 |
const std::vector<SPIRVWord> &Components, |
| 555 |
SPIRVBasicBlock *BB) override; |
| 556 |
SPIRVInstruction *addVectorExtractDynamicInst(SPIRVValue *, SPIRVValue *, |
| 557 |
SPIRVBasicBlock *) override; |
| 558 |
SPIRVInstruction *addVectorInsertDynamicInst(SPIRVValue *, SPIRVValue *, |
| 559 |
SPIRVValue *, |
| 560 |
SPIRVBasicBlock *) override; |
| 561 |
SPIRVInstruction *addFPGARegINTELInst(SPIRVType *, SPIRVValue *, |
| 562 |
SPIRVBasicBlock *) override; |
| 563 |
SPIRVInstruction *addSampledImageInst(SPIRVType *, SPIRVValue *, SPIRVValue *, |
| 564 |
SPIRVBasicBlock *) override; |
| 565 |
template <typename AliasingInstType> |
| 566 |
SPIRVEntry *getOrAddMemAliasingINTELInst(std::vector<SPIRVId> Args, |
| 567 |
llvm::MDNode *MD); |
| 568 |
SPIRVEntry *getOrAddAliasDomainDeclINTELInst(std::vector<SPIRVId> Args, |
| 569 |
llvm::MDNode *MD) override; |
| 570 |
SPIRVEntry *getOrAddAliasScopeDeclINTELInst(std::vector<SPIRVId> Args, |
| 571 |
llvm::MDNode *MD) override; |
| 572 |
SPIRVEntry *getOrAddAliasScopeListDeclINTELInst(std::vector<SPIRVId> Args, |
| 573 |
llvm::MDNode *MD) override; |
| 574 |
SPIRVInstruction *addAssumeTrueKHRInst(SPIRVValue *Condition, |
| 575 |
SPIRVBasicBlock *BB) override; |
| 576 |
SPIRVInstruction *addExpectKHRInst(SPIRVType *ResultTy, SPIRVValue *Value, |
| 577 |
SPIRVValue *ExpectedValue, |
| 578 |
SPIRVBasicBlock *BB) override; |
| 579 |
|
| 580 |
virtual SPIRVId getExtInstSetId(SPIRVExtInstSetKind Kind) const override; |
| 581 |
|
| 582 |
// I/O functions |
| 583 |
friend spv_ostream &operator<<(spv_ostream &O, SPIRVModule &M); |
| 584 |
friend std::istream &operator>>(std::istream &I, SPIRVModule &M); |
| 585 |
|
| 586 |
private: |
| 587 |
SPIRVErrorLog ErrLog; |
| 588 |
SPIRVId NextId; |
| 589 |
SPIRVWord SPIRVVersion; |
| 590 |
unsigned short GeneratorId; |
| 591 |
unsigned short GeneratorVer; |
| 592 |
SPIRVInstructionSchemaKind InstSchema; |
| 593 |
SourceLanguage SrcLang; |
| 594 |
SPIRVWord SrcLangVer; |
| 595 |
std::set<std::string> SrcExtension; |
| 596 |
std::set<std::string> SPIRVExt; |
| 597 |
SPIRVConditionalExtensionSet SPIRVCondExt; |
| 598 |
SPIRVAddressingModelKind AddrModel; |
| 599 |
SPIRVMemoryModelKind MemoryModel; |
| 600 |
|
| 601 |
typedef std::map<SPIRVId, SPIRVEntry *> SPIRVIdToEntryMap; |
| 602 |
typedef std::set<SPIRVEntry *> SPIRVEntrySet; |
| 603 |
typedef std::set<SPIRVId> SPIRVIdSet; |
| 604 |
typedef std::vector<SPIRVId> SPIRVIdVec; |
| 605 |
typedef std::vector<SPIRVFunction *> SPIRVFunctionVector; |
| 606 |
typedef std::vector<SPIRVTypeForwardPointer *> SPIRVForwardPointerVec; |
| 607 |
typedef std::vector<SPIRVType *> SPIRVTypeVec; |
| 608 |
typedef std::vector<SPIRVValue *> SPIRVConstantVector; |
| 609 |
typedef std::vector<SPIRVVariable *> SPIRVVariableVec; |
| 610 |
typedef std::vector<SPIRVString *> SPIRVStringVec; |
| 611 |
typedef std::vector<SPIRVMemberName *> SPIRVMemberNameVec; |
| 612 |
typedef std::vector<SPIRVDecorationGroup *> SPIRVDecGroupVec; |
| 613 |
typedef std::vector<SPIRVGroupDecorateGeneric *> SPIRVGroupDecVec; |
| 614 |
typedef std::vector<SPIRVAsmTargetINTEL *> SPIRVAsmTargetVector; |
| 615 |
typedef std::vector<SPIRVAsmINTEL *> SPIRVAsmVector; |
| 616 |
typedef std::vector<SPIRVEntryPoint *> SPIRVEntryPointVec; |
| 617 |
typedef std::map<SPIRVId, SPIRVExtInstSetKind> SPIRVIdToInstructionSetMap; |
| 618 |
std::map<SPIRVExtInstSetKind, SPIRVId> ExtInstSetIds; |
| 619 |
typedef std::map<SPIRVId, SPIRVExtInstSetKind> SPIRVIdToBuiltinSetMap; |
| 620 |
typedef std::map<SPIRVExecutionModelKind, SPIRVIdSet> SPIRVExecModelIdSetMap; |
| 621 |
typedef std::unordered_map<std::string, SPIRVString *> SPIRVStringMap; |
| 622 |
typedef std::map<SPIRVTypeStruct *, std::vector<std::pair<unsigned, SPIRVId>>> |
| 623 |
SPIRVUnknownStructFieldMap; |
| 624 |
typedef std::vector<SPIRVEntry *> SPIRVAliasInstMDVec; |
| 625 |
typedef std::unordered_map<llvm::MDNode *, SPIRVEntry *> SPIRVAliasInstMDMap; |
| 626 |
|
| 627 |
SPIRVForwardPointerVec ForwardPointerVec; |
| 628 |
SPIRVTypeVec TypeVec; |
| 629 |
SPIRVIdToEntryMap IdEntryMap; |
| 630 |
SPIRVIdToEntryMap IdTypeForwardMap; // Forward declared IDs |
| 631 |
SPIRVFunctionVector FuncVec; |
| 632 |
SPIRVConstantVector ConstVec; |
| 633 |
SPIRVVariableVec VariableVec; |
| 634 |
SPIRVEntrySet EntryNoId; // Entries without id |
| 635 |
SPIRVIdToInstructionSetMap IdToInstSetMap; |
| 636 |
SPIRVIdToBuiltinSetMap IdBuiltinMap; |
| 637 |
SPIRVIdSet NamedId; |
| 638 |
SPIRVStringVec StringVec; |
| 639 |
SPIRVMemberNameVec MemberNameVec; |
| 640 |
std::shared_ptr<const SPIRVLine> CurrentLine; |
| 641 |
std::shared_ptr<const SPIRVExtInst> CurrentDebugLine; |
| 642 |
SPIRVDecorateVec DecorateVec; |
| 643 |
SPIRVDecGroupVec DecGroupVec; |
| 644 |
SPIRVGroupDecVec GroupDecVec; |
| 645 |
SPIRVAsmTargetVector AsmTargetVec; |
| 646 |
SPIRVAsmVector AsmVec; |
| 647 |
SPIRVExecModelIdSetMap EntryPointSet; |
| 648 |
SPIRVEntryPointVec EntryPointVec; |
| 649 |
SPIRVExecModelIdSetMap ConditionalEntryPointSet; |
| 650 |
SPIRVConditionalEntryPointVec ConditionalEntryPointVec; |
| 651 |
SPIRVStringMap StrMap; |
| 652 |
SPIRVCapMap CapMap; |
| 653 |
SPIRVConditionalCapMap ConditionalCapMap; |
| 654 |
SPIRVUnknownStructFieldMap UnknownStructFieldMap; |
| 655 |
std::map<unsigned, SPIRVTypeInt *> IntTypeMap; |
| 656 |
SmallDenseMap<std::pair<unsigned, unsigned>, SPIRVTypeFloat *, 4> |
| 657 |
FloatTypeMap; |
| 658 |
std::map<unsigned, SPIRVConstant *> LiteralMap; |
| 659 |
SmallDenseMap<std::pair<unsigned, SPIRVType *>, SPIRVTypePointer *, 4> |
| 660 |
PointerTypeMap; |
| 661 |
SmallDenseMap<std::pair<SPIRVType *, SPIRVWord>, SPIRVTypeVector *, 4> |
| 662 |
VectorTypeMap; |
| 663 |
std::vector<SPIRVExtInst *> DebugInstVec; |
| 664 |
std::vector<SPIRVExtInst *> AuxDataInstVec; |
| 665 |
std::vector<SPIRVModuleProcessed *> ModuleProcessedVec; |
| 666 |
SPIRVAliasInstMDVec AliasInstMDVec; |
| 667 |
SPIRVAliasInstMDMap AliasInstMDMap; |
| 668 |
|
| 669 |
void layoutEntry(SPIRVEntry *Entry); |
| 670 |
}; |
| 671 |
|
| 672 |
SPIRVModuleImpl::~SPIRVModuleImpl() { |
| 673 |
for (auto I : EntryNoId) |
| 674 |
delete I; |
| 675 |
|
| 676 |
for (auto I : IdEntryMap) |
| 677 |
delete I.second; |
| 678 |
|
| 679 |
for (auto C : CapMap) |
| 680 |
delete C.second; |
| 681 |
|
| 682 |
for (auto C : ConditionalCapMap) |
| 683 |
delete C.second; |
| 684 |
|
| 685 |
for (auto *M : ModuleProcessedVec) |
| 686 |
delete M; |
| 687 |
} |
| 688 |
|
| 689 |
const std::shared_ptr<const SPIRVLine> & |
| 690 |
SPIRVModuleImpl::getCurrentLine() const { |
| 691 |
return CurrentLine; |
| 692 |
} |
| 693 |
|
| 694 |
void SPIRVModuleImpl::setCurrentLine( |
| 695 |
const std::shared_ptr<const SPIRVLine> &Line) { |
| 696 |
CurrentLine = Line; |
| 697 |
} |
| 698 |
|
| 699 |
void SPIRVModuleImpl::addLine(SPIRVEntry *E, SPIRVId FileNameId, SPIRVWord Line, |
| 700 |
SPIRVWord Column) { |
| 701 |
if (!(CurrentLine && CurrentLine->equals(FileNameId, Line, Column))) |
| 702 |
CurrentLine.reset(new SPIRVLine(this, FileNameId, Line, Column)); |
| 703 |
assert(E && "invalid entry"); |
| 704 |
E->setLine(CurrentLine); |
| 705 |
} |
| 706 |
|
| 707 |
const std::shared_ptr<const SPIRVExtInst> & |
| 708 |
SPIRVModuleImpl::getCurrentDebugLine() const { |
| 709 |
return CurrentDebugLine; |
| 710 |
} |
| 711 |
|
| 712 |
void SPIRVModuleImpl::setCurrentDebugLine( |
| 713 |
const std::shared_ptr<const SPIRVExtInst> &DebugLine) { |
| 714 |
CurrentDebugLine = DebugLine; |
| 715 |
} |
| 716 |
|
| 717 |
namespace { |
| 718 |
bool isDebugLineEqual(const SPIRVExtInst &CurrentDebugLine, SPIRVId FileNameId, |
| 719 |
SPIRVId LineStartId, SPIRVId LineEndId, |
| 720 |
SPIRVId ColumnStartId, SPIRVId ColumnEndId) { |
| 721 |
assert(CurrentDebugLine.getExtOp() == SPIRVDebug::DebugLine); |
| 722 |
const std::vector<SPIRVWord> CurrentDebugLineArgs = |
| 723 |
CurrentDebugLine.getArguments(); |
| 724 |
|
| 725 |
using namespace SPIRVDebug::Operand::DebugLine; |
| 726 |
return CurrentDebugLineArgs[SourceIdx] == FileNameId && |
| 727 |
CurrentDebugLineArgs[StartIdx] == LineStartId && |
| 728 |
CurrentDebugLineArgs[EndIdx] == LineEndId && |
| 729 |
CurrentDebugLineArgs[ColumnStartIdx] == ColumnStartId && |
| 730 |
CurrentDebugLineArgs[ColumnEndIdx] == ColumnEndId; |
| 731 |
} |
| 732 |
} // namespace |
| 733 |
|
| 734 |
void SPIRVModuleImpl::addDebugLine(SPIRVEntry *E, SPIRVType *TheType, |
| 735 |
SPIRVId FileNameId, SPIRVWord LineStart, |
| 736 |
SPIRVWord LineEnd, SPIRVWord ColumnStart, |
| 737 |
SPIRVWord ColumnEnd) { |
| 738 |
if (!(CurrentDebugLine && |
| 739 |
isDebugLineEqual(*CurrentDebugLine, FileNameId, |
| 740 |
getLiteralAsConstant(LineStart)->getId(), |
| 741 |
getLiteralAsConstant(LineEnd)->getId(), |
| 742 |
getLiteralAsConstant(ColumnStart)->getId(), |
| 743 |
getLiteralAsConstant(ColumnEnd)->getId()))) { |
| 744 |
using namespace SPIRVDebug::Operand::DebugLine; |
| 745 |
|
| 746 |
std::vector<SPIRVWord> DebugLineOps(OperandCount); |
| 747 |
DebugLineOps[SourceIdx] = FileNameId; |
| 748 |
DebugLineOps[StartIdx] = getLiteralAsConstant(LineStart)->getId(); |
| 749 |
DebugLineOps[EndIdx] = getLiteralAsConstant(LineEnd)->getId(); |
| 750 |
DebugLineOps[ColumnStartIdx] = getLiteralAsConstant(ColumnStart)->getId(); |
| 751 |
DebugLineOps[ColumnEndIdx] = getLiteralAsConstant(ColumnEnd)->getId(); |
| 752 |
|
| 753 |
CurrentDebugLine.reset(static_cast<SPIRVExtInst *>( |
| 754 |
createDebugInfo(SPIRVDebug::DebugLine, TheType, DebugLineOps))); |
| 755 |
} |
| 756 |
|
| 757 |
assert(E && "invalid entry"); |
| 758 |
E->setDebugLine(CurrentDebugLine); |
| 759 |
} |
| 760 |
|
| 761 |
SPIRVValue *SPIRVModuleImpl::addSamplerConstant(SPIRVType *TheType, |
| 762 |
SPIRVWord AddrMode, |
| 763 |
SPIRVWord ParametricMode, |
| 764 |
SPIRVWord FilterMode) { |
| 765 |
return addConstant(new SPIRVConstantSampler(this, TheType, getId(), AddrMode, |
| 766 |
ParametricMode, FilterMode)); |
| 767 |
} |
| 768 |
|
| 769 |
SPIRVValue *SPIRVModuleImpl::addPipeStorageConstant(SPIRVType *TheType, |
| 770 |
SPIRVWord PacketSize, |
| 771 |
SPIRVWord PacketAlign, |
| 772 |
SPIRVWord Capacity) { |
| 773 |
return addConstant(new SPIRVConstantPipeStorage( |
| 774 |
this, TheType, getId(), PacketSize, PacketAlign, Capacity)); |
| 775 |
} |
| 776 |
|
| 777 |
void SPIRVModuleImpl::addExtension(ExtensionID Ext) { |
| 778 |
std::string ExtName; |
| 779 |
SPIRVMap<ExtensionID, std::string>::find(Ext, &ExtName); |
| 780 |
if (!getErrorLog().checkError(isAllowedToUseExtension(Ext), |
| 781 |
SPIRVEC_RequiresExtension, ExtName)) { |
| 782 |
setInvalid(); |
| 783 |
return; |
| 784 |
} |
| 785 |
SPIRVExt.insert(ExtName); |
| 786 |
|
| 787 |
// SPV_EXT_shader_atomic_float16_add extends the |
| 788 |
// SPV_EXT_shader_atomic_float_add extension. |
| 789 |
// The specification requires both extensions to be added to use |
| 790 |
// AtomicFloat16AddEXT capability whereas getRequiredExtension() |
| 791 |
// is able to return a single extensionID. |
| 792 |
if (Ext == ExtensionID::SPV_EXT_shader_atomic_float16_add) { |
| 793 |
SPIRVMap<ExtensionID, std::string>::find( |
| 794 |
ExtensionID::SPV_EXT_shader_atomic_float_add, &ExtName); |
| 795 |
SPIRVExt.insert(ExtName); |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
void SPIRVModuleImpl::addCapability(SPIRVCapabilityKind Cap) { |
| 800 |
addCapabilities(SPIRV::getCapability(Cap)); |
| 801 |
SPIRVDBG(spvdbgs() << "addCapability: " << SPIRVCapabilityNameMap::map(Cap) |
| 802 |
<< '\n'); |
| 803 |
if (hasCapability(Cap)) |
| 804 |
return; |
| 805 |
|
| 806 |
auto *CapObj = new SPIRVCapability(this, Cap); |
| 807 |
if (AutoAddExtensions) { |
| 808 |
// While we are reading existing SPIR-V we need to read it as-is and don't |
| 809 |
// add required extensions for each entry automatically |
| 810 |
auto Ext = CapObj->getRequiredExtension(); |
| 811 |
if (Ext.has_value()) |
| 812 |
addExtension(Ext.value()); |
| 813 |
} |
| 814 |
|
| 815 |
CapMap.insert(std::make_pair(Cap, CapObj)); |
| 816 |
} |
| 817 |
|
| 818 |
void SPIRVModuleImpl::addCapabilityInternal(SPIRVCapabilityKind Cap) { |
| 819 |
if (AutoAddCapability) { |
| 820 |
if (hasCapability(Cap)) |
| 821 |
return; |
| 822 |
|
| 823 |
CapMap.insert(std::make_pair(Cap, new SPIRVCapability(this, Cap))); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
void SPIRVModuleImpl::addConditionalCapability(SPIRVId Condition, |
| 828 |
SPIRVCapabilityKind Cap) { |
| 829 |
SPIRVDBG(spvdbgs() << "addConditionalCapability: " |
| 830 |
<< SPIRVCapabilityNameMap::map(Cap) |
| 831 |
<< ", condition: " << Condition << '\n'); |
| 832 |
if (ConditionalCapMap.find(std::make_pair(Condition, Cap)) != |
| 833 |
ConditionalCapMap.end()) { |
| 834 |
return; |
| 835 |
} |
| 836 |
|
| 837 |
auto *CapObj = new SPIRVConditionalCapabilityINTEL(this, Condition, Cap); |
| 838 |
if (AutoAddExtensions) { |
| 839 |
assert(false && "Auto adding conditional extensions is not supported."); |
| 840 |
} |
| 841 |
|
| 842 |
ConditionalCapMap.insert( |
| 843 |
std::make_pair(std::make_pair(Condition, Cap), CapObj)); |
| 844 |
} |
| 845 |
|
| 846 |
void SPIRVModuleImpl::eraseConditionalCapability(SPIRVId Condition, |
| 847 |
SPIRVCapabilityKind Cap) { |
| 848 |
ConditionalCapMap.erase(std::make_pair(Condition, Cap)); |
| 849 |
} |
| 850 |
|
| 851 |
SPIRVConstant *SPIRVModuleImpl::getLiteralAsConstant(unsigned Literal) { |
| 852 |
auto Loc = LiteralMap.find(Literal); |
| 853 |
if (Loc != LiteralMap.end()) |
| 854 |
return Loc->second; |
| 855 |
auto Ty = addIntegerType(32); |
| 856 |
auto V = new SPIRVConstant(this, Ty, getId(), static_cast<uint64_t>(Literal)); |
| 857 |
LiteralMap[Literal] = V; |
| 858 |
addConstant(V); |
| 859 |
return V; |
| 860 |
} |
| 861 |
|
| 862 |
void SPIRVModuleImpl::layoutEntry(SPIRVEntry *E) { |
| 863 |
auto OC = E->getOpCode(); |
| 864 |
int IntOC = static_cast<int>(OC); |
| 865 |
switch (IntOC) { |
| 866 |
case OpString: |
| 867 |
addTo(StringVec, E); |
| 868 |
break; |
| 869 |
case OpMemberName: |
| 870 |
addTo(MemberNameVec, E); |
| 871 |
break; |
| 872 |
case OpVariable: { |
| 873 |
auto BV = static_cast<SPIRVVariable *>(E); |
| 874 |
if (!BV->getParent()) |
| 875 |
addTo(VariableVec, E); |
| 876 |
} break; |
| 877 |
case OpExtInst: { |
| 878 |
SPIRVExtInst *EI = static_cast<SPIRVExtInst *>(E); |
| 879 |
if ((EI->getExtSetKind() == SPIRVEIS_Debug || |
| 880 |
EI->getExtSetKind() == SPIRVEIS_OpenCL_DebugInfo_100 || |
| 881 |
EI->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_100 || |
| 882 |
EI->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_200) && |
| 883 |
EI->getExtOp() != SPIRVDebug::Declare && |
| 884 |
EI->getExtOp() != SPIRVDebug::Value && |
| 885 |
EI->getExtOp() != SPIRVDebug::Scope && |
| 886 |
EI->getExtOp() != SPIRVDebug::NoScope) { |
| 887 |
DebugInstVec.push_back(EI); |
| 888 |
} |
| 889 |
if (EI->getExtSetKind() == SPIRVEIS_NonSemantic_AuxData) |
| 890 |
AuxDataInstVec.push_back(EI); |
| 891 |
break; |
| 892 |
} |
| 893 |
case OpAsmTargetINTEL: { |
| 894 |
addTo(AsmTargetVec, E); |
| 895 |
break; |
| 896 |
} |
| 897 |
case OpAliasDomainDeclINTEL: |
| 898 |
case OpAliasScopeDeclINTEL: |
| 899 |
case OpAliasScopeListDeclINTEL: { |
| 900 |
addTo(AliasInstMDVec, E); |
| 901 |
break; |
| 902 |
} |
| 903 |
case OpAsmINTEL: { |
| 904 |
addTo(AsmVec, E); |
| 905 |
break; |
| 906 |
} |
| 907 |
default: |
| 908 |
if (isTypeOpCode(OC)) |
| 909 |
TypeVec.push_back(static_cast<SPIRVType *>(E)); |
| 910 |
else if (isConstantOpCode(OC)) |
| 911 |
ConstVec.push_back(static_cast<SPIRVConstant *>(E)); |
| 912 |
break; |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
// Add an entry to the id to entry map. |
| 917 |
// Assert if the id is mapped to a different entry. |
| 918 |
// Certain entries need to be add to specific collectors to maintain |
| 919 |
// logic layout of SPIRV. |
| 920 |
SPIRVEntry *SPIRVModuleImpl::addEntry(SPIRVEntry *Entry) { |
| 921 |
assert(Entry && "Invalid entry"); |
| 922 |
if (Entry->hasId()) { |
| 923 |
SPIRVId Id = Entry->getId(); |
| 924 |
assert(Entry->getId() != SPIRVID_INVALID && "Invalid id"); |
| 925 |
SPIRVEntry *Mapped = nullptr; |
| 926 |
if (exist(Id, &Mapped)) { |
| 927 |
if (Mapped->getOpCode() == internal::OpForward) { |
| 928 |
replaceForward(static_cast<SPIRVForward *>(Mapped), Entry); |
| 929 |
} else { |
| 930 |
assert(Mapped == Entry && "Id used twice"); |
| 931 |
} |
| 932 |
} else |
| 933 |
IdEntryMap[Id] = Entry; |
| 934 |
} else { |
| 935 |
// Collect entries with no ID to de-allocate them at the end. |
| 936 |
// Entry of OpLine will be deleted by std::shared_ptr automatically. |
| 937 |
if (Entry->getOpCode() != OpLine) |
| 938 |
EntryNoId.insert(Entry); |
| 939 |
|
| 940 |
// Store the known ID of pointer type that would be declared later. |
| 941 |
if (Entry->getOpCode() == OpTypeForwardPointer) |
| 942 |
IdTypeForwardMap[static_cast<SPIRVTypeForwardPointer *>(Entry) |
| 943 |
->getPointerId()] = Entry; |
| 944 |
} |
| 945 |
|
| 946 |
Entry->setModule(this); |
| 947 |
|
| 948 |
layoutEntry(Entry); |
| 949 |
if (AutoAddCapability) { |
| 950 |
for (auto &I : Entry->getRequiredCapability()) { |
| 951 |
addCapability(I); |
| 952 |
} |
| 953 |
} |
| 954 |
if (ValidateCapability) { |
| 955 |
assert(none_of( |
| 956 |
Entry->getRequiredCapability().begin(), |
| 957 |
Entry->getRequiredCapability().end(), |
| 958 |
[this](SPIRVCapabilityKind &val) { return !CapMap.count(val); })); |
| 959 |
} |
| 960 |
if (AutoAddExtensions) { |
| 961 |
// While we are reading existing SPIR-V we need to read it as-is and don't |
| 962 |
// add required extensions for each entry automatically |
| 963 |
auto Ext = Entry->getRequiredExtension(); |
| 964 |
if (Ext.has_value()) |
| 965 |
addExtension(Ext.value()); |
| 966 |
} |
| 967 |
|
| 968 |
return Entry; |
| 969 |
} |
| 970 |
|
| 971 |
bool SPIRVModuleImpl::exist(SPIRVId Id) const { return exist(Id, nullptr); } |
| 972 |
|
| 973 |
bool SPIRVModuleImpl::exist(SPIRVId Id, SPIRVEntry **Entry) const { |
| 974 |
assert(Id != SPIRVID_INVALID && "Invalid Id"); |
| 975 |
SPIRVIdToEntryMap::const_iterator Loc = IdEntryMap.find(Id); |
| 976 |
if (Loc == IdEntryMap.end()) |
| 977 |
return false; |
| 978 |
if (Entry) |
| 979 |
*Entry = Loc->second; |
| 980 |
return true; |
| 981 |
} |
| 982 |
|
| 983 |
// If Id is invalid, returns the next available id. |
| 984 |
// Otherwise returns the given id and adjust the next available id by increment. |
| 985 |
SPIRVId SPIRVModuleImpl::getId(SPIRVId Id, unsigned Increment) { |
| 986 |
if (!isValidId(Id)) |
| 987 |
Id = NextId; |
| 988 |
else |
| 989 |
NextId = std::max(Id, NextId); |
| 990 |
NextId += Increment; |
| 991 |
return Id; |
| 992 |
} |
| 993 |
|
| 994 |
SPIRVEntry *SPIRVModuleImpl::getEntry(SPIRVId Id) const { |
| 995 |
assert(Id != SPIRVID_INVALID && "Invalid Id"); |
| 996 |
SPIRVIdToEntryMap::const_iterator Loc = IdEntryMap.find(Id); |
| 997 |
if (Loc != IdEntryMap.end()) { |
| 998 |
return Loc->second; |
| 999 |
} |
| 1000 |
SPIRVIdToEntryMap::const_iterator LocFwd = IdTypeForwardMap.find(Id); |
| 1001 |
if (LocFwd != IdTypeForwardMap.end()) { |
| 1002 |
return LocFwd->second; |
| 1003 |
} |
| 1004 |
assert(false && "Id is not in map"); |
| 1005 |
return nullptr; |
| 1006 |
} |
| 1007 |
|
| 1008 |
SPIRVExtInstSetKind SPIRVModuleImpl::getBuiltinSet(SPIRVId SetId) const { |
| 1009 |
auto Loc = IdToInstSetMap.find(SetId); |
| 1010 |
assert(Loc != IdToInstSetMap.end() && "Invalid builtin set id"); |
| 1011 |
return Loc->second; |
| 1012 |
} |
| 1013 |
|
| 1014 |
bool SPIRVModuleImpl::isEntryPoint(SPIRVExecutionModelKind ExecModel, |
| 1015 |
SPIRVId EP) const { |
| 1016 |
assert(isValid(ExecModel) && "Invalid execution model"); |
| 1017 |
assert(EP != SPIRVID_INVALID && "Invalid function id"); |
| 1018 |
auto Loc = EntryPointSet.find(ExecModel); |
| 1019 |
if (Loc == EntryPointSet.end()) { |
| 1020 |
Loc = ConditionalEntryPointSet.find(ExecModel); |
| 1021 |
if (Loc == ConditionalEntryPointSet.end()) |
| 1022 |
return false; |
| 1023 |
} |
| 1024 |
return Loc->second.count(EP); |
| 1025 |
} |
| 1026 |
|
| 1027 |
// Module change functions |
| 1028 |
bool SPIRVModuleImpl::importBuiltinSet(const std::string &BuiltinSetName, |
| 1029 |
SPIRVId *BuiltinSetId) { |
| 1030 |
SPIRVId TmpBuiltinSetId = getId(); |
| 1031 |
if (!importBuiltinSetWithId(BuiltinSetName, TmpBuiltinSetId)) |
| 1032 |
return false; |
| 1033 |
if (BuiltinSetId) |
| 1034 |
*BuiltinSetId = TmpBuiltinSetId; |
| 1035 |
return true; |
| 1036 |
} |
| 1037 |
|
| 1038 |
bool SPIRVModuleImpl::importBuiltinSetWithId(const std::string &BuiltinSetName, |
| 1039 |
SPIRVId BuiltinSetId) { |
| 1040 |
SPIRVExtInstSetKind BuiltinSet = SPIRVEIS_Count; |
| 1041 |
SPIRVCKRT(SPIRVBuiltinSetNameMap::rfind(BuiltinSetName, &BuiltinSet), |
| 1042 |
InvalidBuiltinSetName, "Actual is " + BuiltinSetName); |
| 1043 |
IdToInstSetMap[BuiltinSetId] = BuiltinSet; |
| 1044 |
ExtInstSetIds[BuiltinSet] = BuiltinSetId; |
| 1045 |
return true; |
| 1046 |
} |
| 1047 |
|
| 1048 |
void SPIRVModuleImpl::setAlignment(SPIRVValue *V, SPIRVWord A) { |
| 1049 |
V->setAlignment(A); |
| 1050 |
} |
| 1051 |
|
| 1052 |
void SPIRVModuleImpl::setName(SPIRVEntry *E, const std::string &Name) { |
| 1053 |
E->setName(Name); |
| 1054 |
if (!E->hasId()) |
| 1055 |
return; |
| 1056 |
if (!Name.empty()) |
| 1057 |
NamedId.insert(E->getId()); |
| 1058 |
else |
| 1059 |
NamedId.erase(E->getId()); |
| 1060 |
} |
| 1061 |
|
| 1062 |
void SPIRVModuleImpl::resolveUnknownStructFields() { |
| 1063 |
for (auto &KV : UnknownStructFieldMap) { |
| 1064 |
auto *Struct = KV.first; |
| 1065 |
for (auto &Indices : KV.second) { |
| 1066 |
unsigned I = Indices.first; |
| 1067 |
SPIRVId ID = Indices.second; |
| 1068 |
|
| 1069 |
auto Ty = static_cast<SPIRVType *>(getEntry(ID)); |
| 1070 |
Struct->setMemberType(I, Ty); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
// Type creation functions |
| 1076 |
template <class T> T *SPIRVModuleImpl::addType(T *Ty) { |
| 1077 |
add(Ty); |
| 1078 |
if (!Ty->getName().empty()) |
| 1079 |
setName(Ty, Ty->getName()); |
| 1080 |
return Ty; |
| 1081 |
} |
| 1082 |
|
| 1083 |
SPIRVTypeVoid *SPIRVModuleImpl::addVoidType() { |
| 1084 |
return addType(new SPIRVTypeVoid(this, getId())); |
| 1085 |
} |
| 1086 |
|
| 1087 |
SPIRVTypeArray *SPIRVModuleImpl::addArrayType(SPIRVType *ElementType, |
| 1088 |
SPIRVConstant *Length) { |
| 1089 |
return addType(new SPIRVTypeArray(this, getId(), ElementType, Length)); |
| 1090 |
} |
| 1091 |
|
| 1092 |
SPIRVTypeBool *SPIRVModuleImpl::addBoolType() { |
| 1093 |
return addType(new SPIRVTypeBool(this, getId())); |
| 1094 |
} |
| 1095 |
|
| 1096 |
SPIRVTypeInt *SPIRVModuleImpl::addIntegerType(unsigned BitWidth) { |
| 1097 |
auto Loc = IntTypeMap.find(BitWidth); |
| 1098 |
if (Loc != IntTypeMap.end()) |
| 1099 |
return Loc->second; |
| 1100 |
auto Ty = new SPIRVTypeInt(this, getId(), BitWidth, false); |
| 1101 |
IntTypeMap[BitWidth] = Ty; |
| 1102 |
return addType(Ty); |
| 1103 |
} |
| 1104 |
|
| 1105 |
SPIRVTypeFloat *SPIRVModuleImpl::addFloatType(unsigned BitWidth, |
| 1106 |
unsigned FloatingPointEncoding) { |
| 1107 |
auto Desc = std::make_pair(BitWidth, FloatingPointEncoding); |
| 1108 |
auto Loc = FloatTypeMap.find(Desc); |
| 1109 |
if (Loc != FloatTypeMap.end()) |
| 1110 |
return Loc->second; |
| 1111 |
auto *Ty = new SPIRVTypeFloat(this, getId(), BitWidth, FloatingPointEncoding); |
| 1112 |
FloatTypeMap[Desc] = Ty; |
| 1113 |
return addType(Ty); |
| 1114 |
} |
| 1115 |
|
| 1116 |
SPIRVTypePointer * |
| 1117 |
SPIRVModuleImpl::addPointerType(SPIRVStorageClassKind StorageClass, |
| 1118 |
SPIRVType *ElementType) { |
| 1119 |
return addType( |
| 1120 |
new SPIRVTypePointer(this, getId(), StorageClass, ElementType)); |
| 1121 |
} |
| 1122 |
|
| 1123 |
SPIRVTypeFunction *SPIRVModuleImpl::addFunctionType( |
| 1124 |
SPIRVType *ReturnType, const std::vector<SPIRVType *> &ParameterTypes) { |
| 1125 |
return addType( |
| 1126 |
new SPIRVTypeFunction(this, getId(), ReturnType, ParameterTypes)); |
| 1127 |
} |
| 1128 |
|
| 1129 |
SPIRVTypeOpaque *SPIRVModuleImpl::addOpaqueType(const std::string &Name) { |
| 1130 |
return addType(new SPIRVTypeOpaque(this, getId(), Name)); |
| 1131 |
} |
| 1132 |
|
| 1133 |
SPIRVTypeStruct *SPIRVModuleImpl::openStructType(unsigned NumMembers, |
| 1134 |
const std::string &Name) { |
| 1135 |
auto T = new SPIRVTypeStruct(this, getId(), NumMembers, Name); |
| 1136 |
return T; |
| 1137 |
} |
| 1138 |
|
| 1139 |
SPIRVEntry *SPIRVModuleImpl::addTypeStructContinuedINTEL(unsigned NumMembers) { |
| 1140 |
return add(new SPIRVTypeStructContinuedINTEL(this, NumMembers)); |
| 1141 |
} |
| 1142 |
|
| 1143 |
void SPIRVModuleImpl::closeStructType(SPIRVTypeStruct *T, bool Packed) { |
| 1144 |
addType(T); |
| 1145 |
T->setPacked(Packed); |
| 1146 |
} |
| 1147 |
|
| 1148 |
SPIRVTypeVector *SPIRVModuleImpl::addVectorType(SPIRVType *CompType, |
| 1149 |
SPIRVWord CompCount) { |
| 1150 |
auto Desc = std::make_pair(CompType, CompCount); |
| 1151 |
auto Loc = VectorTypeMap.find(Desc); |
| 1152 |
if (Loc != VectorTypeMap.end()) |
| 1153 |
return Loc->second; |
| 1154 |
auto *Ty = new SPIRVTypeVector(this, getId(), CompType, CompCount); |
| 1155 |
VectorTypeMap[Desc] = Ty; |
| 1156 |
return addType(Ty); |
| 1157 |
} |
| 1158 |
|
| 1159 |
SPIRVTypeJointMatrixINTEL * |
| 1160 |
SPIRVModuleImpl::addJointMatrixINTELType(SPIRVType *CompType, |
| 1161 |
std::vector<SPIRVValue *> Args) { |
| 1162 |
return addType(new SPIRVTypeJointMatrixINTEL(this, getId(), CompType, Args)); |
| 1163 |
} |
| 1164 |
|
| 1165 |
SPIRVTypeCooperativeMatrixKHR * |
| 1166 |
SPIRVModuleImpl::addCooperativeMatrixKHRType(SPIRVType *CompType, |
| 1167 |
std::vector<SPIRVValue *> Args) { |
| 1168 |
return addType( |
| 1169 |
new SPIRVTypeCooperativeMatrixKHR(this, getId(), CompType, Args)); |
| 1170 |
} |
| 1171 |
|
| 1172 |
SPIRVInstruction *SPIRVModuleImpl::addCooperativeMatrixLengthKHRInst( |
| 1173 |
SPIRVType *RetTy, SPIRVType *MatTy, SPIRVBasicBlock *BB) { |
| 1174 |
return addInstruction( |
| 1175 |
SPIRVInstTemplateBase::create(OpCooperativeMatrixLengthKHR, RetTy, |
| 1176 |
getId(), getVec(MatTy->getId()), BB, this), |
| 1177 |
BB); |
| 1178 |
} |
| 1179 |
|
| 1180 |
SPIRVType *SPIRVModuleImpl::addOpaqueGenericType(Op TheOpCode) { |
| 1181 |
return addType(new SPIRVTypeOpaqueGeneric(TheOpCode, this, getId())); |
| 1182 |
} |
| 1183 |
|
| 1184 |
SPIRVTypeDeviceEvent *SPIRVModuleImpl::addDeviceEventType() { |
| 1185 |
return addType(new SPIRVTypeDeviceEvent(this, getId())); |
| 1186 |
} |
| 1187 |
|
| 1188 |
SPIRVTypeQueue *SPIRVModuleImpl::addQueueType() { |
| 1189 |
return addType(new SPIRVTypeQueue(this, getId())); |
| 1190 |
} |
| 1191 |
|
| 1192 |
SPIRVTypePipe *SPIRVModuleImpl::addPipeType() { |
| 1193 |
return addType(new SPIRVTypePipe(this, getId())); |
| 1194 |
} |
| 1195 |
|
| 1196 |
SPIRVTypeImage * |
| 1197 |
SPIRVModuleImpl::addImageType(SPIRVType *SampledType, |
| 1198 |
const SPIRVTypeImageDescriptor &Desc) { |
| 1199 |
return addType(new SPIRVTypeImage( |
| 1200 |
this, getId(), SampledType ? SampledType->getId() : 0, Desc)); |
| 1201 |
} |
| 1202 |
|
| 1203 |
SPIRVTypeImage * |
| 1204 |
SPIRVModuleImpl::addImageType(SPIRVType *SampledType, |
| 1205 |
const SPIRVTypeImageDescriptor &Desc, |
| 1206 |
SPIRVAccessQualifierKind Acc) { |
| 1207 |
return addType(new SPIRVTypeImage( |
| 1208 |
this, getId(), SampledType ? SampledType->getId() : 0, Desc, Acc)); |
| 1209 |
} |
| 1210 |
|
| 1211 |
SPIRVTypeSampler *SPIRVModuleImpl::addSamplerType() { |
| 1212 |
return addType(new SPIRVTypeSampler(this, getId())); |
| 1213 |
} |
| 1214 |
|
| 1215 |
SPIRVTypePipeStorage *SPIRVModuleImpl::addPipeStorageType() { |
| 1216 |
return addType(new SPIRVTypePipeStorage(this, getId())); |
| 1217 |
} |
| 1218 |
|
| 1219 |
SPIRVTypeSampledImage *SPIRVModuleImpl::addSampledImageType(SPIRVTypeImage *T) { |
| 1220 |
return addType(new SPIRVTypeSampledImage(this, getId(), T)); |
| 1221 |
} |
| 1222 |
|
| 1223 |
SPIRVTypeVmeImageINTEL * |
| 1224 |
SPIRVModuleImpl::addVmeImageINTELType(SPIRVTypeImage *T) { |
| 1225 |
return addType(new SPIRVTypeVmeImageINTEL(this, getId(), T)); |
| 1226 |
} |
| 1227 |
|
| 1228 |
SPIRVTypeBufferSurfaceINTEL * |
| 1229 |
SPIRVModuleImpl::addBufferSurfaceINTELType(SPIRVAccessQualifierKind Access) { |
| 1230 |
return addType(new SPIRVTypeBufferSurfaceINTEL(this, getId(), Access)); |
| 1231 |
} |
| 1232 |
|
| 1233 |
SPIRVType *SPIRVModuleImpl::addSubgroupAvcINTELType(Op TheOpCode) { |
| 1234 |
return addType(new SPIRVTypeSubgroupAvcINTEL(TheOpCode, this, getId())); |
| 1235 |
} |
| 1236 |
|
| 1237 |
SPIRVTypeTokenINTEL *SPIRVModuleImpl::addTokenTypeINTEL() { |
| 1238 |
return addType(new SPIRVTypeTokenINTEL(this, getId())); |
| 1239 |
} |
| 1240 |
|
| 1241 |
SPIRVFunction *SPIRVModuleImpl::addFunction(SPIRVFunction *Func) { |
| 1242 |
FuncVec.push_back(add(Func)); |
| 1243 |
return Func; |
| 1244 |
} |
| 1245 |
|
| 1246 |
SPIRVFunction *SPIRVModuleImpl::addFunction(SPIRVTypeFunction *FuncType, |
| 1247 |
SPIRVId Id) { |
| 1248 |
return addFunction(new SPIRVFunction( |
| 1249 |
this, FuncType, getId(Id, FuncType->getNumParameters() + 1))); |
| 1250 |
} |
| 1251 |
|
| 1252 |
SPIRVBasicBlock *SPIRVModuleImpl::addBasicBlock(SPIRVFunction *Func, |
| 1253 |
SPIRVId Id) { |
| 1254 |
return Func->addBasicBlock(new SPIRVBasicBlock(getId(Id), Func)); |
| 1255 |
} |
| 1256 |
|
| 1257 |
const SPIRVDecorateGeneric * |
| 1258 |
SPIRVModuleImpl::addDecorate(SPIRVDecorateGeneric *Dec) { |
| 1259 |
add(Dec); |
| 1260 |
SPIRVId Id = Dec->getTargetId(); |
| 1261 |
bool Found = exist(Id); |
| 1262 |
(void)Found; |
| 1263 |
assert(Found && "Decorate target does not exist"); |
| 1264 |
if (!Dec->getOwner()) |
| 1265 |
DecorateVec.push_back(Dec); |
| 1266 |
addCapabilities(Dec->getRequiredCapability()); |
| 1267 |
return Dec; |
| 1268 |
} |
| 1269 |
|
| 1270 |
void SPIRVModuleImpl::addEntryPoint(SPIRVExecutionModelKind ExecModel, |
| 1271 |
SPIRVId EntryPoint, const std::string &Name, |
| 1272 |
const std::vector<SPIRVId> &Variables) { |
| 1273 |
assert(isValid(ExecModel) && "Invalid execution model"); |
| 1274 |
assert(EntryPoint != SPIRVID_INVALID && "Invalid entry point"); |
| 1275 |
auto *EP = |
| 1276 |
add(new SPIRVEntryPoint(this, ExecModel, EntryPoint, Name, Variables)); |
| 1277 |
EntryPointVec.push_back(EP); |
| 1278 |
EntryPointSet[ExecModel].insert(EntryPoint); |
| 1279 |
addCapabilities(SPIRV::getCapability(ExecModel)); |
| 1280 |
} |
| 1281 |
|
| 1282 |
void SPIRVModuleImpl::addConditionalEntryPoint( |
| 1283 |
SPIRVId Condition, SPIRVExecutionModelKind ExecModel, SPIRVId EntryPoint, |
| 1284 |
const std::string &Name, const std::vector<SPIRVId> &Variables) { |
| 1285 |
assert(isValid(ExecModel) && "Invalid execution model"); |
| 1286 |
assert(EntryPoint != SPIRVID_INVALID && "Invalid entry point"); |
| 1287 |
auto *EP = add(new SPIRVConditionalEntryPointINTEL( |
| 1288 |
this, Condition, ExecModel, EntryPoint, Name, Variables)); |
| 1289 |
ConditionalEntryPointVec.push_back(EP); |
| 1290 |
ConditionalEntryPointSet[ExecModel].insert(EntryPoint); |
| 1291 |
} |
| 1292 |
|
| 1293 |
void SPIRVModuleImpl::specializeConditionalEntryPoints(SPIRVId Condition, |
| 1294 |
bool ShouldKeep) { |
| 1295 |
std::vector<const SPIRVConditionalEntryPointINTEL *> EPsToRemove; |
| 1296 |
std::vector<SPIRVId> EPIdsToRemove; |
| 1297 |
for (const auto *EP : ConditionalEntryPointVec) { |
| 1298 |
if (EP->getCondition() == Condition) { |
| 1299 |
EPsToRemove.push_back(EP); |
| 1300 |
EPIdsToRemove.push_back(EP->getTargetId()); |
| 1301 |
if (ShouldKeep) { |
| 1302 |
// add the removed conditional entry point as a normal entry point |
| 1303 |
addEntryPoint(EP->getExecModel(), EP->getTargetId(), EP->getName(), |
| 1304 |
EP->getVariables()); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
erase_if(ConditionalEntryPointVec, [&EPsToRemove](const auto *EP) { |
| 1310 |
return std::find(EPsToRemove.begin(), EPsToRemove.end(), EP) != |
| 1311 |
EPsToRemove.end(); |
| 1312 |
}); |
| 1313 |
|
| 1314 |
for (const auto &Id : EPIdsToRemove) { |
| 1315 |
for (auto &[ExecMode, EPSet] : ConditionalEntryPointSet) { |
| 1316 |
EPSet.erase(Id); |
| 1317 |
} |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
SPIRVForward *SPIRVModuleImpl::addForward(SPIRVType *Ty) { |
| 1322 |
return add(new SPIRVForward(this, Ty, getId())); |
| 1323 |
} |
| 1324 |
|
| 1325 |
SPIRVForward *SPIRVModuleImpl::addForward(SPIRVId Id, SPIRVType *Ty) { |
| 1326 |
return add(new SPIRVForward(this, Ty, Id)); |
| 1327 |
} |
| 1328 |
|
| 1329 |
SPIRVEntry *SPIRVModuleImpl::replaceForward(SPIRVForward *Forward, |
| 1330 |
SPIRVEntry *Entry) { |
| 1331 |
SPIRVId Id = Entry->getId(); |
| 1332 |
SPIRVId ForwardId = Forward->getId(); |
| 1333 |
if (ForwardId == Id) { |
| 1334 |
IdEntryMap[Id] = Entry; |
| 1335 |
// Annotations include name, decorations, execution modes |
| 1336 |
Entry->takeAnnotations(Forward); |
| 1337 |
} else { |
| 1338 |
auto Loc = IdEntryMap.find(Id); |
| 1339 |
assert(Loc != IdEntryMap.end()); |
| 1340 |
IdEntryMap.erase(Loc); |
| 1341 |
Entry->setId(ForwardId); |
| 1342 |
IdEntryMap[ForwardId] = Entry; |
| 1343 |
// Replace current Id with ForwardId in decorates. |
| 1344 |
Entry->replaceTargetIdInDecorates(ForwardId); |
| 1345 |
} |
| 1346 |
delete Forward; |
| 1347 |
return Entry; |
| 1348 |
} |
| 1349 |
|
| 1350 |
void SPIRVModuleImpl::eraseInstruction(SPIRVInstruction *I, |
| 1351 |
SPIRVBasicBlock *BB) { |
| 1352 |
SPIRVId Id = I->getId(); |
| 1353 |
BB->eraseInstruction(I); |
| 1354 |
auto Loc = IdEntryMap.find(Id); |
| 1355 |
assert(Loc != IdEntryMap.end()); |
| 1356 |
IdEntryMap.erase(Loc); |
| 1357 |
delete I; |
| 1358 |
} |
| 1359 |
|
| 1360 |
bool SPIRVModuleImpl::eraseValue(SPIRVValue *V) { |
| 1361 |
Op OpCode = V->getOpCode(); |
| 1362 |
SPIRVId Id = V->getId(); |
| 1363 |
|
| 1364 |
if (isTypeOpCode(OpCode)) { |
| 1365 |
erase_if(TypeVec, [Id](auto T) { return T->getId() == Id; }); |
| 1366 |
} else if (OpCode == OpVariable) { |
| 1367 |
erase_if(VariableVec, [Id](auto V) { return V->getId() == Id; }); |
| 1368 |
} else if (isConstantOpCode(OpCode)) { |
| 1369 |
erase_if(ConstVec, [Id](auto C) { return C->getId() == Id; }); |
| 1370 |
} else if (OpCode == Op::OpAsmINTEL) { |
| 1371 |
erase_if(AsmVec, [Id](auto A) { return A->getId() == Id; }); |
| 1372 |
} else if (OpCode == Op::OpAsmTargetINTEL) { |
| 1373 |
erase_if(AsmTargetVec, [Id](auto AT) { return AT->getId() == Id; }); |
| 1374 |
} else { |
| 1375 |
return false; |
| 1376 |
} |
| 1377 |
|
| 1378 |
auto Loc = IdEntryMap.find(Id); |
| 1379 |
assert(Loc != IdEntryMap.end()); |
| 1380 |
IdEntryMap.erase(Loc); |
| 1381 |
delete V; |
| 1382 |
return true; |
| 1383 |
} |
| 1384 |
|
| 1385 |
SPIRVValue *SPIRVModuleImpl::addConstant(SPIRVValue *C) { return add(C); } |
| 1386 |
|
| 1387 |
SPIRVValue *SPIRVModuleImpl::addConstant(SPIRVType *Ty, uint64_t V) { |
| 1388 |
if (Ty->isTypeBool()) { |
| 1389 |
if (V) |
| 1390 |
return addConstant(new SPIRVConstantTrue(this, Ty, getId())); |
| 1391 |
else |
| 1392 |
return addConstant(new SPIRVConstantFalse(this, Ty, getId())); |
| 1393 |
} |
| 1394 |
if (Ty->isTypeInt()) |
| 1395 |
return addIntegerConstant(static_cast<SPIRVTypeInt *>(Ty), V); |
| 1396 |
return addConstant(new SPIRVConstant(this, Ty, getId(), V)); |
| 1397 |
} |
| 1398 |
|
| 1399 |
SPIRVValue *SPIRVModuleImpl::addConstant(SPIRVType *Ty, llvm::APInt V) { |
| 1400 |
return addConstant(new SPIRVConstant(this, Ty, getId(), V)); |
| 1401 |
} |
| 1402 |
|
| 1403 |
SPIRVValue *SPIRVModuleImpl::addIntegerConstant(SPIRVTypeInt *Ty, uint64_t V) { |
| 1404 |
if (Ty->getBitWidth() == 32) { |
| 1405 |
unsigned I32 = static_cast<unsigned>(V); |
| 1406 |
assert(I32 == V && "Integer value truncated"); |
| 1407 |
return getLiteralAsConstant(I32); |
| 1408 |
} |
| 1409 |
return addConstant(new SPIRVConstant(this, Ty, getId(), V)); |
| 1410 |
} |
| 1411 |
|
| 1412 |
SPIRVValue *SPIRVModuleImpl::addFloatConstant(SPIRVTypeFloat *Ty, float V) { |
| 1413 |
return addConstant(new SPIRVConstant(this, Ty, getId(), V)); |
| 1414 |
} |
| 1415 |
|
| 1416 |
SPIRVValue *SPIRVModuleImpl::addDoubleConstant(SPIRVTypeFloat *Ty, double V) { |
| 1417 |
return addConstant(new SPIRVConstant(this, Ty, getId(), V)); |
| 1418 |
} |
| 1419 |
|
| 1420 |
SPIRVValue *SPIRVModuleImpl::addNullConstant(SPIRVType *Ty) { |
| 1421 |
return addConstant(new SPIRVConstantNull(this, Ty, getId())); |
| 1422 |
} |
| 1423 |
|
| 1424 |
SPIRVValue *SPIRVModuleImpl::addCompositeConstant( |
| 1425 |
SPIRVType *Ty, const std::vector<SPIRVValue *> &Elements) { |
| 1426 |
constexpr int MaxNumElements = MaxWordCount - SPIRVConstantComposite::FixedWC; |
| 1427 |
const int NumElements = Elements.size(); |
| 1428 |
|
| 1429 |
// In case number of elements is greater than maximum WordCount and |
| 1430 |
// SPV_INTEL_long_constant_composite is not enabled, the error will be emitted |
| 1431 |
// by validate functionality of SPIRVCompositeConstant class. |
| 1432 |
if (NumElements <= MaxNumElements || |
| 1433 |
!isAllowedToUseExtension(ExtensionID::SPV_INTEL_long_constant_composite)) |
| 1434 |
return addConstant(new SPIRVConstantComposite(this, Ty, getId(), Elements)); |
| 1435 |
|
| 1436 |
auto Start = Elements.begin(); |
| 1437 |
auto End = Start + MaxNumElements; |
| 1438 |
std::vector<SPIRVValue *> Slice(Start, End); |
| 1439 |
auto *Res = |
| 1440 |
static_cast<SPIRVConstantComposite *>(addCompositeConstant(Ty, Slice)); |
| 1441 |
for (; End != Elements.end();) { |
| 1442 |
Start = End; |
| 1443 |
End = ((Elements.end() - End) > MaxNumElements) ? End + MaxNumElements |
| 1444 |
: Elements.end(); |
| 1445 |
Slice.assign(Start, End); |
| 1446 |
auto Continued = static_cast<SPIRVConstantComposite::ContinuedInstType>( |
| 1447 |
addCompositeConstantContinuedINTEL(Slice)); |
| 1448 |
Res->addContinuedInstruction(Continued); |
| 1449 |
} |
| 1450 |
return Res; |
| 1451 |
} |
| 1452 |
|
| 1453 |
SPIRVEntry *SPIRVModuleImpl::addCompositeConstantContinuedINTEL( |
| 1454 |
const std::vector<SPIRVValue *> &Elements) { |
| 1455 |
return add(new SPIRVConstantCompositeContinuedINTEL(this, Elements)); |
| 1456 |
} |
| 1457 |
|
| 1458 |
SPIRVValue *SPIRVModuleImpl::addSpecConstantComposite( |
| 1459 |
SPIRVType *Ty, const std::vector<SPIRVValue *> &Elements) { |
| 1460 |
constexpr int MaxNumElements = |
| 1461 |
MaxWordCount - SPIRVSpecConstantComposite::FixedWC; |
| 1462 |
const int NumElements = Elements.size(); |
| 1463 |
|
| 1464 |
// In case number of elements is greater than maximum WordCount and |
| 1465 |
// SPV_INTEL_long_constant_composite is not enabled, the error will be emitted |
| 1466 |
// by validate functionality of SPIRVSpecConstantComposite class. |
| 1467 |
if (NumElements <= MaxNumElements || |
| 1468 |
!isAllowedToUseExtension(ExtensionID::SPV_INTEL_long_constant_composite)) |
| 1469 |
return addConstant( |
| 1470 |
new SPIRVSpecConstantComposite(this, Ty, getId(), Elements)); |
| 1471 |
|
| 1472 |
auto Start = Elements.begin(); |
| 1473 |
auto End = Start + MaxNumElements; |
| 1474 |
std::vector<SPIRVValue *> Slice(Start, End); |
| 1475 |
auto *Res = static_cast<SPIRVSpecConstantComposite *>( |
| 1476 |
addSpecConstantComposite(Ty, Slice)); |
| 1477 |
for (; End != Elements.end();) { |
| 1478 |
Start = End; |
| 1479 |
End = ((Elements.end() - End) > MaxNumElements) ? End + MaxNumElements |
| 1480 |
: Elements.end(); |
| 1481 |
Slice.assign(Start, End); |
| 1482 |
auto Continued = static_cast<SPIRVSpecConstantComposite::ContinuedInstType>( |
| 1483 |
addSpecConstantCompositeContinuedINTEL(Slice)); |
| 1484 |
Res->addContinuedInstruction(Continued); |
| 1485 |
} |
| 1486 |
return Res; |
| 1487 |
} |
| 1488 |
|
| 1489 |
SPIRVEntry *SPIRVModuleImpl::addSpecConstantCompositeContinuedINTEL( |
| 1490 |
const std::vector<SPIRVValue *> &Elements) { |
| 1491 |
return add(new SPIRVSpecConstantCompositeContinuedINTEL(this, Elements)); |
| 1492 |
} |
| 1493 |
|
| 1494 |
SPIRVValue *SPIRVModuleImpl::addConstantFunctionPointerINTEL(SPIRVType *Ty, |
| 1495 |
SPIRVFunction *F) { |
| 1496 |
return addConstant( |
| 1497 |
new SPIRVConstantFunctionPointerINTEL(getId(), Ty, F, this)); |
| 1498 |
} |
| 1499 |
|
| 1500 |
SPIRVValue *SPIRVModuleImpl::addUndef(SPIRVType *TheType) { |
| 1501 |
return addConstant(new SPIRVUndef(this, TheType, getId())); |
| 1502 |
} |
| 1503 |
|
| 1504 |
SPIRVValue *SPIRVModuleImpl::addSpecConstant(SPIRVType *Ty, uint64_t V) { |
| 1505 |
if (Ty->isTypeBool()) { |
| 1506 |
if (V) |
| 1507 |
return add(new SPIRVSpecConstantTrue(this, Ty, getId())); |
| 1508 |
else |
| 1509 |
return add(new SPIRVSpecConstantFalse(this, Ty, getId())); |
| 1510 |
} |
| 1511 |
return add(new SPIRVSpecConstant(this, Ty, getId(), V)); |
| 1512 |
} |
| 1513 |
|
| 1514 |
// Instruction creation functions |
| 1515 |
|
| 1516 |
SPIRVInstruction * |
| 1517 |
SPIRVModuleImpl::addStoreInst(SPIRVValue *Target, SPIRVValue *Source, |
| 1518 |
const std::vector<SPIRVWord> &TheMemoryAccess, |
| 1519 |
SPIRVBasicBlock *BB) { |
| 1520 |
return BB->addInstruction( |
| 1521 |
new SPIRVStore(Target->getId(), Source->getId(), TheMemoryAccess, BB)); |
| 1522 |
} |
| 1523 |
|
| 1524 |
SPIRVInstruction *SPIRVModuleImpl::addSwitchInst( |
| 1525 |
SPIRVValue *Select, SPIRVBasicBlock *Default, |
| 1526 |
const std::vector<std::pair<std::vector<SPIRVWord>, SPIRVBasicBlock *>> |
| 1527 |
&Pairs, |
| 1528 |
SPIRVBasicBlock *BB) { |
| 1529 |
return BB->addInstruction(new SPIRVSwitch(Select, Default, Pairs, BB)); |
| 1530 |
} |
| 1531 |
|
| 1532 |
SPIRVInstruction * |
| 1533 |
SPIRVModuleImpl::addVectorTimesScalarInst(SPIRVType *TheType, SPIRVId TheVector, |
| 1534 |
SPIRVId TheScalar, |
| 1535 |
SPIRVBasicBlock *BB) { |
| 1536 |
return BB->addInstruction( |
| 1537 |
new SPIRVVectorTimesScalar(TheType, getId(), TheVector, TheScalar, BB)); |
| 1538 |
} |
| 1539 |
|
| 1540 |
SPIRVInstruction * |
| 1541 |
SPIRVModuleImpl::addVectorTimesMatrixInst(SPIRVType *TheType, SPIRVId TheVector, |
| 1542 |
SPIRVId TheMatrix, |
| 1543 |
SPIRVBasicBlock *BB) { |
| 1544 |
return BB->addInstruction( |
| 1545 |
new SPIRVVectorTimesMatrix(TheType, getId(), TheVector, TheMatrix, BB)); |
| 1546 |
} |
| 1547 |
|
| 1548 |
SPIRVInstruction * |
| 1549 |
SPIRVModuleImpl::addMatrixTimesScalarInst(SPIRVType *TheType, SPIRVId TheMatrix, |
| 1550 |
SPIRVId TheScalar, |
| 1551 |
SPIRVBasicBlock *BB) { |
| 1552 |
return BB->addInstruction( |
| 1553 |
new SPIRVMatrixTimesScalar(TheType, getId(), TheMatrix, TheScalar, BB)); |
| 1554 |
} |
| 1555 |
|
| 1556 |
SPIRVInstruction * |
| 1557 |
SPIRVModuleImpl::addMatrixTimesVectorInst(SPIRVType *TheType, SPIRVId TheMatrix, |
| 1558 |
SPIRVId TheVector, |
| 1559 |
SPIRVBasicBlock *BB) { |
| 1560 |
return BB->addInstruction( |
| 1561 |
new SPIRVMatrixTimesVector(TheType, getId(), TheMatrix, TheVector, BB)); |
| 1562 |
} |
| 1563 |
|
| 1564 |
SPIRVInstruction * |
| 1565 |
SPIRVModuleImpl::addMatrixTimesMatrixInst(SPIRVType *TheType, SPIRVId M1, |
| 1566 |
SPIRVId M2, SPIRVBasicBlock *BB) { |
| 1567 |
return BB->addInstruction( |
| 1568 |
new SPIRVMatrixTimesMatrix(TheType, getId(), M1, M2, BB)); |
| 1569 |
} |
| 1570 |
|
| 1571 |
SPIRVInstruction *SPIRVModuleImpl::addTransposeInst(SPIRVType *TheType, |
| 1572 |
SPIRVId TheMatrix, |
| 1573 |
SPIRVBasicBlock *BB) { |
| 1574 |
return BB->addInstruction( |
| 1575 |
new SPIRVTranspose(TheType, getId(), TheMatrix, BB)); |
| 1576 |
} |
| 1577 |
|
| 1578 |
SPIRVInstruction * |
| 1579 |
SPIRVModuleImpl::addGroupInst(Op OpCode, SPIRVType *Type, Scope Scope, |
| 1580 |
const std::vector<SPIRVValue *> &Ops, |
| 1581 |
SPIRVBasicBlock *BB) { |
| 1582 |
assert(!Type || !Type->isTypeVoid()); |
| 1583 |
auto WordOps = getIds(Ops); |
| 1584 |
WordOps.insert(WordOps.begin(), Scope); |
| 1585 |
return addInstTemplate(OpCode, WordOps, BB, Type); |
| 1586 |
} |
| 1587 |
|
| 1588 |
SPIRVInstruction * |
| 1589 |
SPIRVModuleImpl::addInstruction(SPIRVInstruction *Inst, SPIRVBasicBlock *BB, |
| 1590 |
SPIRVInstruction *InsertBefore) { |
| 1591 |
if (BB) |
| 1592 |
return BB->addInstruction(Inst, InsertBefore); |
| 1593 |
if (Inst->getOpCode() != OpSpecConstantOp) { |
| 1594 |
SPIRVInstruction *Res = createSpecConstantOpInst(Inst); |
| 1595 |
delete Inst; |
| 1596 |
Inst = Res; |
| 1597 |
} |
| 1598 |
return static_cast<SPIRVInstruction *>(addConstant(Inst)); |
| 1599 |
} |
| 1600 |
|
| 1601 |
SPIRVInstruction * |
| 1602 |
SPIRVModuleImpl::addLoadInst(SPIRVValue *Source, |
| 1603 |
const std::vector<SPIRVWord> &TheMemoryAccess, |
| 1604 |
SPIRVBasicBlock *BB) { |
| 1605 |
return addInstruction( |
| 1606 |
new SPIRVLoad(getId(), Source->getId(), TheMemoryAccess, BB), BB); |
| 1607 |
} |
| 1608 |
|
| 1609 |
SPIRVInstruction * |
| 1610 |
SPIRVModuleImpl::addPhiInst(SPIRVType *Type, |
| 1611 |
std::vector<SPIRVValue *> IncomingPairs, |
| 1612 |
SPIRVBasicBlock *BB) { |
| 1613 |
return addInstruction(new SPIRVPhi(Type, getId(), IncomingPairs, BB), BB); |
| 1614 |
} |
| 1615 |
|
| 1616 |
SPIRVInstruction *SPIRVModuleImpl::addExtInst( |
| 1617 |
SPIRVType *TheType, SPIRVWord BuiltinSet, SPIRVWord EntryPoint, |
| 1618 |
const std::vector<SPIRVWord> &Args, SPIRVBasicBlock *BB, |
| 1619 |
SPIRVInstruction *InsertBefore) { |
| 1620 |
return addInstruction( |
| 1621 |
new SPIRVExtInst(TheType, getId(), BuiltinSet, EntryPoint, Args, BB), BB, |
| 1622 |
InsertBefore); |
| 1623 |
} |
| 1624 |
|
| 1625 |
SPIRVInstruction *SPIRVModuleImpl::addExtInst( |
| 1626 |
SPIRVType *TheType, SPIRVWord BuiltinSet, SPIRVWord EntryPoint, |
| 1627 |
const std::vector<SPIRVValue *> &Args, SPIRVBasicBlock *BB, |
| 1628 |
SPIRVInstruction *InsertBefore) { |
| 1629 |
return addInstruction( |
| 1630 |
new SPIRVExtInst(TheType, getId(), BuiltinSet, EntryPoint, Args, BB), BB, |
| 1631 |
InsertBefore); |
| 1632 |
} |
| 1633 |
|
| 1634 |
SPIRVEntry * |
| 1635 |
SPIRVModuleImpl::createDebugInfo(SPIRVWord InstId, SPIRVType *TheType, |
| 1636 |
const std::vector<SPIRVWord> &Args) { |
| 1637 |
return new SPIRVExtInst(this, getId(), TheType, SPIRVEIS_OpenCL_DebugInfo_100, |
| 1638 |
ExtInstSetIds[getDebugInfoEIS()], InstId, Args); |
| 1639 |
} |
| 1640 |
|
| 1641 |
SPIRVEntry *SPIRVModuleImpl::addDebugInfo(SPIRVWord InstId, SPIRVType *TheType, |
| 1642 |
const std::vector<SPIRVWord> &Args) { |
| 1643 |
return addEntry(createDebugInfo(InstId, TheType, Args)); |
| 1644 |
} |
| 1645 |
|
| 1646 |
SPIRVEntry *SPIRVModuleImpl::addAuxData(SPIRVWord InstId, SPIRVType *TheType, |
| 1647 |
const std::vector<SPIRVWord> &Args) { |
| 1648 |
return addEntry(new SPIRVExtInst( |
| 1649 |
this, getId(), TheType, SPIRVEIS_NonSemantic_AuxData, |
| 1650 |
getExtInstSetId(SPIRVEIS_NonSemantic_AuxData), InstId, Args)); |
| 1651 |
} |
| 1652 |
|
| 1653 |
SPIRVEntry *SPIRVModuleImpl::addModuleProcessed(const std::string &Process) { |
| 1654 |
ModuleProcessedVec.push_back(new SPIRVModuleProcessed(this, Process)); |
| 1655 |
return ModuleProcessedVec.back(); |
| 1656 |
} |
| 1657 |
|
| 1658 |
std::vector<SPIRVModuleProcessed *> SPIRVModuleImpl::getModuleProcessedVec() { |
| 1659 |
return ModuleProcessedVec; |
| 1660 |
} |
| 1661 |
|
| 1662 |
SPIRVInstruction * |
| 1663 |
SPIRVModuleImpl::addCallInst(SPIRVFunction *TheFunction, |
| 1664 |
const std::vector<SPIRVWord> &TheArguments, |
| 1665 |
SPIRVBasicBlock *BB) { |
| 1666 |
return addInstruction( |
| 1667 |
new SPIRVFunctionCall(getId(), TheFunction, TheArguments, BB), BB); |
| 1668 |
} |
| 1669 |
|
| 1670 |
SPIRVInstruction *SPIRVModuleImpl::addIndirectCallInst( |
| 1671 |
SPIRVValue *TheCalledValue, SPIRVType *TheReturnType, |
| 1672 |
const std::vector<SPIRVWord> &TheArguments, SPIRVBasicBlock *BB) { |
| 1673 |
return addInstruction( |
| 1674 |
new SPIRVFunctionPointerCallINTEL(getId(), TheCalledValue, TheReturnType, |
| 1675 |
TheArguments, BB), |
| 1676 |
BB); |
| 1677 |
} |
| 1678 |
|
| 1679 |
SPIRVEntry * |
| 1680 |
SPIRVModuleImpl::getOrAddAsmTargetINTEL(const std::string &TheTarget) { |
| 1681 |
auto TargetIt = std::find_if(AsmTargetVec.begin(), AsmTargetVec.end(), |
| 1682 |
[&TheTarget](const SPIRVAsmTargetINTEL *Target) { |
| 1683 |
return Target->getTarget() == TheTarget; |
| 1684 |
}); |
| 1685 |
if (TargetIt == AsmTargetVec.end()) |
| 1686 |
return add(new SPIRVAsmTargetINTEL(this, getId(), TheTarget)); |
| 1687 |
return *TargetIt; |
| 1688 |
} |
| 1689 |
|
| 1690 |
SPIRVValue *SPIRVModuleImpl::addAsmINTEL(SPIRVTypeFunction *TheType, |
| 1691 |
SPIRVAsmTargetINTEL *TheTarget, |
| 1692 |
const std::string &TheInstructions, |
| 1693 |
const std::string &TheConstraints) { |
| 1694 |
auto Asm = new SPIRVAsmINTEL(this, TheType, getId(), TheTarget, |
| 1695 |
TheInstructions, TheConstraints); |
| 1696 |
return add(Asm); |
| 1697 |
} |
| 1698 |
|
| 1699 |
SPIRVInstruction * |
| 1700 |
SPIRVModuleImpl::addAsmCallINTELInst(SPIRVAsmINTEL *TheAsm, |
| 1701 |
const std::vector<SPIRVWord> &TheArguments, |
| 1702 |
SPIRVBasicBlock *BB) { |
| 1703 |
return addInstruction( |
| 1704 |
new SPIRVAsmCallINTEL(getId(), TheAsm, TheArguments, BB), BB); |
| 1705 |
} |
| 1706 |
|
| 1707 |
SPIRVInstruction *SPIRVModuleImpl::addBinaryInst(Op TheOpCode, SPIRVType *Type, |
| 1708 |
SPIRVValue *Op1, |
| 1709 |
SPIRVValue *Op2, |
| 1710 |
SPIRVBasicBlock *BB) { |
| 1711 |
if (Type->isTypeFloat(16, FPEncodingBFloat16KHR) && TheOpCode != OpDot) |
| 1712 |
addCapability(internal::CapabilityBFloat16ArithmeticINTEL); |
| 1713 |
return addInstruction(SPIRVInstTemplateBase::create( |
| 1714 |
TheOpCode, Type, getId(), |
| 1715 |
getVec(Op1->getId(), Op2->getId()), BB, this), |
| 1716 |
BB); |
| 1717 |
} |
| 1718 |
|
| 1719 |
SPIRVInstruction *SPIRVModuleImpl::addUnreachableInst(SPIRVBasicBlock *BB) { |
| 1720 |
return addInstruction(new SPIRVUnreachable(BB), BB); |
| 1721 |
} |
| 1722 |
|
| 1723 |
SPIRVInstruction *SPIRVModuleImpl::addReturnInst(SPIRVBasicBlock *BB) { |
| 1724 |
return addInstruction(new SPIRVReturn(BB), BB); |
| 1725 |
} |
| 1726 |
|
| 1727 |
SPIRVInstruction *SPIRVModuleImpl::addReturnValueInst(SPIRVValue *ReturnValue, |
| 1728 |
SPIRVBasicBlock *BB) { |
| 1729 |
return addInstruction(new SPIRVReturnValue(ReturnValue, BB), BB); |
| 1730 |
} |
| 1731 |
|
| 1732 |
SPIRVInstruction *SPIRVModuleImpl::addUnaryInst(Op TheOpCode, |
| 1733 |
SPIRVType *TheType, |
| 1734 |
SPIRVValue *Op, |
| 1735 |
SPIRVBasicBlock *BB) { |
| 1736 |
if (TheType->isTypeFloat(16, FPEncodingBFloat16KHR) && |
| 1737 |
(TheOpCode == OpFNegate || isUnaryPredicateOpCode(TheOpCode))) |
| 1738 |
addCapability(internal::CapabilityBFloat16ArithmeticINTEL); |
| 1739 |
return addInstruction( |
| 1740 |
SPIRVInstTemplateBase::create(TheOpCode, TheType, getId(), |
| 1741 |
getVec(Op->getId()), BB, this), |
| 1742 |
BB); |
| 1743 |
} |
| 1744 |
|
| 1745 |
SPIRVInstruction *SPIRVModuleImpl::addVectorExtractDynamicInst( |
| 1746 |
SPIRVValue *TheVector, SPIRVValue *Index, SPIRVBasicBlock *BB) { |
| 1747 |
return addInstruction( |
| 1748 |
new SPIRVVectorExtractDynamic(getId(), TheVector, Index, BB), BB); |
| 1749 |
} |
| 1750 |
|
| 1751 |
SPIRVInstruction *SPIRVModuleImpl::addVectorInsertDynamicInst( |
| 1752 |
SPIRVValue *TheVector, SPIRVValue *TheComponent, SPIRVValue *Index, |
| 1753 |
SPIRVBasicBlock *BB) { |
| 1754 |
return addInstruction( |
| 1755 |
new SPIRVVectorInsertDynamic(getId(), TheVector, TheComponent, Index, BB), |
| 1756 |
BB); |
| 1757 |
} |
| 1758 |
|
| 1759 |
SPIRVValue *SPIRVModuleImpl::addVectorShuffleInst( |
| 1760 |
SPIRVType *Type, SPIRVValue *Vec1, SPIRVValue *Vec2, |
| 1761 |
const std::vector<SPIRVWord> &Components, SPIRVBasicBlock *BB) { |
| 1762 |
std::vector<SPIRVId> Ops{Vec1->getId(), Vec2->getId()}; |
| 1763 |
Ops.insert(Ops.end(), Components.begin(), Components.end()); |
| 1764 |
|
| 1765 |
return addInstruction(SPIRVInstTemplateBase::create(OpVectorShuffle, Type, |
| 1766 |
getId(), Ops, BB, this), |
| 1767 |
BB); |
| 1768 |
} |
| 1769 |
|
| 1770 |
SPIRVInstruction *SPIRVModuleImpl::addBranchInst(SPIRVLabel *TargetLabel, |
| 1771 |
SPIRVBasicBlock *BB) { |
| 1772 |
return addInstruction(new SPIRVBranch(TargetLabel, BB), BB); |
| 1773 |
} |
| 1774 |
|
| 1775 |
SPIRVInstruction *SPIRVModuleImpl::addBranchConditionalInst( |
| 1776 |
SPIRVValue *Condition, SPIRVLabel *TrueLabel, SPIRVLabel *FalseLabel, |
| 1777 |
SPIRVBasicBlock *BB) { |
| 1778 |
return addInstruction( |
| 1779 |
new SPIRVBranchConditional(Condition, TrueLabel, FalseLabel, BB), BB); |
| 1780 |
} |
| 1781 |
|
| 1782 |
SPIRVInstruction *SPIRVModuleImpl::addCmpInst(Op TheOpCode, SPIRVType *TheType, |
| 1783 |
SPIRVValue *Op1, SPIRVValue *Op2, |
| 1784 |
SPIRVBasicBlock *BB) { |
| 1785 |
return addInstruction(SPIRVInstTemplateBase::create( |
| 1786 |
TheOpCode, TheType, getId(), |
| 1787 |
getVec(Op1->getId(), Op2->getId()), BB, this), |
| 1788 |
BB); |
| 1789 |
} |
| 1790 |
|
| 1791 |
SPIRVInstruction *SPIRVModuleImpl::addControlBarrierInst(SPIRVValue *ExecKind, |
| 1792 |
SPIRVValue *MemKind, |
| 1793 |
SPIRVValue *MemSema, |
| 1794 |
SPIRVBasicBlock *BB) { |
| 1795 |
return addInstruction(new SPIRVControlBarrier(ExecKind, MemKind, MemSema, BB), |
| 1796 |
BB); |
| 1797 |
} |
| 1798 |
|
| 1799 |
SPIRVInstruction *SPIRVModuleImpl::addLifetimeInst(Op OC, SPIRVValue *Object, |
| 1800 |
SPIRVWord Size, |
| 1801 |
SPIRVBasicBlock *BB) { |
| 1802 |
if (OC == OpLifetimeStart) |
| 1803 |
return BB->addInstruction( |
| 1804 |
new SPIRVLifetimeStart(Object->getId(), Size, BB)); |
| 1805 |
else |
| 1806 |
return BB->addInstruction(new SPIRVLifetimeStop(Object->getId(), Size, BB)); |
| 1807 |
} |
| 1808 |
|
| 1809 |
SPIRVInstruction *SPIRVModuleImpl::addMemoryBarrierInst(Scope ScopeKind, |
| 1810 |
SPIRVWord MemFlag, |
| 1811 |
SPIRVBasicBlock *BB) { |
| 1812 |
return addInstruction(SPIRVInstTemplateBase::create( |
| 1813 |
OpMemoryBarrier, nullptr, SPIRVID_INVALID, |
| 1814 |
getVec(static_cast<SPIRVWord>(ScopeKind), MemFlag), |
| 1815 |
BB, this), |
| 1816 |
BB); |
| 1817 |
} |
| 1818 |
|
| 1819 |
SPIRVInstruction *SPIRVModuleImpl::addSelectInst(SPIRVValue *Condition, |
| 1820 |
SPIRVValue *Op1, |
| 1821 |
SPIRVValue *Op2, |
| 1822 |
SPIRVBasicBlock *BB) { |
| 1823 |
return addInstruction( |
| 1824 |
SPIRVInstTemplateBase::create( |
| 1825 |
OpSelect, Op1->getType(), getId(), |
| 1826 |
getVec(Condition->getId(), Op1->getId(), Op2->getId()), BB, this), |
| 1827 |
BB); |
| 1828 |
} |
| 1829 |
|
| 1830 |
SPIRVInstruction *SPIRVModuleImpl::addSelectionMergeInst( |
| 1831 |
SPIRVId MergeBlock, SPIRVWord SelectionControl, SPIRVBasicBlock *BB) { |
| 1832 |
return addInstruction( |
| 1833 |
new SPIRVSelectionMerge(MergeBlock, SelectionControl, BB), BB); |
| 1834 |
} |
| 1835 |
|
| 1836 |
SPIRVInstruction *SPIRVModuleImpl::addLoopMergeInst( |
| 1837 |
SPIRVId MergeBlock, SPIRVId ContinueTarget, SPIRVWord LoopControl, |
| 1838 |
std::vector<SPIRVWord> LoopControlParameters, SPIRVBasicBlock *BB) { |
| 1839 |
return addInstruction( |
| 1840 |
new SPIRVLoopMerge(MergeBlock, ContinueTarget, LoopControl, |
| 1841 |
LoopControlParameters, BB), |
| 1842 |
BB, const_cast<SPIRVInstruction *>(BB->getTerminateInstr())); |
| 1843 |
} |
| 1844 |
|
| 1845 |
SPIRVInstruction *SPIRVModuleImpl::addLoopControlINTELInst( |
| 1846 |
SPIRVWord LoopControl, std::vector<SPIRVWord> LoopControlParameters, |
| 1847 |
SPIRVBasicBlock *BB) { |
| 1848 |
addCapability(CapabilityUnstructuredLoopControlsINTEL); |
| 1849 |
addExtension(ExtensionID::SPV_INTEL_unstructured_loop_controls); |
| 1850 |
return addInstruction( |
| 1851 |
new SPIRVLoopControlINTEL(LoopControl, LoopControlParameters, BB), BB, |
| 1852 |
const_cast<SPIRVInstruction *>(BB->getTerminateInstr())); |
| 1853 |
} |
| 1854 |
|
| 1855 |
SPIRVInstruction *SPIRVModuleImpl::addFixedPointIntelInst( |
| 1856 |
Op OC, SPIRVType *ResTy, SPIRVValue *Input, |
| 1857 |
const std::vector<SPIRVWord> &Ops, SPIRVBasicBlock *BB) { |
| 1858 |
std::vector<SPIRVWord> TheOps = getVec(Input->getId(), Ops); |
| 1859 |
return addInstruction( |
| 1860 |
SPIRVInstTemplateBase::create(OC, ResTy, getId(), TheOps, BB, this), BB); |
| 1861 |
} |
| 1862 |
|
| 1863 |
SPIRVInstruction *SPIRVModuleImpl::addArbFloatPointIntelInst( |
| 1864 |
Op OC, SPIRVType *ResTy, SPIRVValue *InA, SPIRVValue *InB, |
| 1865 |
const std::vector<SPIRVWord> &Ops, SPIRVBasicBlock *BB) { |
| 1866 |
// SPIR-V format: |
| 1867 |
// A<id> [Literal MA] [B<id>] [Literal MB] [Literal Mout] [Literal Sign] |
| 1868 |
// [Literal EnableSubnormals Literal RoundingMode Literal RoundingAccuracy] |
| 1869 |
auto OpsItr = Ops.begin(); |
| 1870 |
std::vector<SPIRVWord> TheOps = getVec(InA->getId(), *OpsItr++); |
| 1871 |
if (InB) |
| 1872 |
TheOps.push_back(InB->getId()); |
| 1873 |
TheOps.insert(TheOps.end(), OpsItr, Ops.end()); |
| 1874 |
|
| 1875 |
return addInstruction( |
| 1876 |
SPIRVInstTemplateBase::create(OC, ResTy, getId(), TheOps, BB, this), BB); |
| 1877 |
} |
| 1878 |
|
| 1879 |
SPIRVInstruction * |
| 1880 |
SPIRVModuleImpl::addPtrAccessChainInst(SPIRVType *Type, SPIRVValue *Base, |
| 1881 |
std::vector<SPIRVValue *> Indices, |
| 1882 |
SPIRVBasicBlock *BB, bool IsInBounds) { |
| 1883 |
return addInstruction( |
| 1884 |
SPIRVInstTemplateBase::create( |
| 1885 |
IsInBounds ? OpInBoundsPtrAccessChain : OpPtrAccessChain, Type, |
| 1886 |
getId(), getVec(Base->getId(), Base->getIds(Indices)), BB, this), |
| 1887 |
BB); |
| 1888 |
} |
| 1889 |
|
| 1890 |
SPIRVInstruction *SPIRVModuleImpl::addAsyncGroupCopy( |
| 1891 |
SPIRVValue *Scope, SPIRVValue *Dest, SPIRVValue *Src, SPIRVValue *NumElems, |
| 1892 |
SPIRVValue *Stride, SPIRVValue *Event, SPIRVBasicBlock *BB) { |
| 1893 |
return addInstruction(new SPIRVGroupAsyncCopy(Scope, getId(), Dest, Src, |
| 1894 |
NumElems, Stride, Event, BB), |
| 1895 |
BB); |
| 1896 |
} |
| 1897 |
|
| 1898 |
SPIRVInstruction *SPIRVModuleImpl::addCompositeConstructInst( |
| 1899 |
SPIRVType *Type, const std::vector<SPIRVId> &Constituents, |
| 1900 |
SPIRVBasicBlock *BB) { |
| 1901 |
return addInstruction( |
| 1902 |
new SPIRVCompositeConstruct(Type, getId(), Constituents, BB), BB); |
| 1903 |
} |
| 1904 |
|
| 1905 |
SPIRVInstruction * |
| 1906 |
SPIRVModuleImpl::addCompositeExtractInst(SPIRVType *Type, SPIRVValue *TheVector, |
| 1907 |
const std::vector<SPIRVWord> &Indices, |
| 1908 |
SPIRVBasicBlock *BB) { |
| 1909 |
return addInstruction(SPIRVInstTemplateBase::create( |
| 1910 |
OpCompositeExtract, Type, getId(), |
| 1911 |
getVec(TheVector->getId(), Indices), BB, this), |
| 1912 |
BB); |
| 1913 |
} |
| 1914 |
|
| 1915 |
SPIRVInstruction *SPIRVModuleImpl::addCompositeInsertInst( |
| 1916 |
SPIRVValue *Object, SPIRVValue *Composite, |
| 1917 |
const std::vector<SPIRVWord> &Indices, SPIRVBasicBlock *BB) { |
| 1918 |
std::vector<SPIRVId> Ops{Object->getId(), Composite->getId()}; |
| 1919 |
Ops.insert(Ops.end(), Indices.begin(), Indices.end()); |
| 1920 |
return addInstruction(SPIRVInstTemplateBase::create(OpCompositeInsert, |
| 1921 |
Composite->getType(), |
| 1922 |
getId(), Ops, BB, this), |
| 1923 |
BB); |
| 1924 |
} |
| 1925 |
|
| 1926 |
SPIRVInstruction *SPIRVModuleImpl::addCopyObjectInst(SPIRVType *TheType, |
| 1927 |
SPIRVValue *Operand, |
| 1928 |
SPIRVBasicBlock *BB) { |
| 1929 |
return addInstruction(new SPIRVCopyObject(TheType, getId(), Operand, BB), BB); |
| 1930 |
} |
| 1931 |
|
| 1932 |
SPIRVInstruction *SPIRVModuleImpl::addCopyMemoryInst( |
| 1933 |
SPIRVValue *TheTarget, SPIRVValue *TheSource, |
| 1934 |
const std::vector<SPIRVWord> &TheMemoryAccess, SPIRVBasicBlock *BB) { |
| 1935 |
return addInstruction( |
| 1936 |
new SPIRVCopyMemory(TheTarget, TheSource, TheMemoryAccess, BB), BB); |
| 1937 |
} |
| 1938 |
|
| 1939 |
SPIRVInstruction *SPIRVModuleImpl::addCopyMemorySizedInst( |
| 1940 |
SPIRVValue *TheTarget, SPIRVValue *TheSource, SPIRVValue *TheSize, |
| 1941 |
const std::vector<SPIRVWord> &TheMemoryAccess, SPIRVBasicBlock *BB) { |
| 1942 |
return addInstruction(new SPIRVCopyMemorySized(TheTarget, TheSource, TheSize, |
| 1943 |
TheMemoryAccess, BB), |
| 1944 |
BB); |
| 1945 |
} |
| 1946 |
|
| 1947 |
SPIRVInstruction *SPIRVModuleImpl::addFPGARegINTELInst(SPIRVType *Type, |
| 1948 |
SPIRVValue *V, |
| 1949 |
SPIRVBasicBlock *BB) { |
| 1950 |
return addInstruction( |
| 1951 |
SPIRVInstTemplateBase::create(OpFPGARegINTEL, Type, getId(), |
| 1952 |
getVec(V->getId()), BB, this), |
| 1953 |
BB); |
| 1954 |
} |
| 1955 |
|
| 1956 |
SPIRVInstruction *SPIRVModuleImpl::addSampledImageInst(SPIRVType *ResultTy, |
| 1957 |
SPIRVValue *Image, |
| 1958 |
SPIRVValue *Sampler, |
| 1959 |
SPIRVBasicBlock *BB) { |
| 1960 |
return addInstruction(SPIRVInstTemplateBase::create( |
| 1961 |
OpSampledImage, ResultTy, getId(), |
| 1962 |
getVec(Image->getId(), Sampler->getId()), BB, this), |
| 1963 |
BB); |
| 1964 |
} |
| 1965 |
|
| 1966 |
SPIRVInstruction *SPIRVModuleImpl::addAssumeTrueKHRInst(SPIRVValue *Condition, |
| 1967 |
SPIRVBasicBlock *BB) { |
| 1968 |
return addInstruction(new SPIRVAssumeTrueKHR(Condition->getId(), BB), BB); |
| 1969 |
} |
| 1970 |
|
| 1971 |
SPIRVInstruction *SPIRVModuleImpl::addExpectKHRInst(SPIRVType *ResultTy, |
| 1972 |
SPIRVValue *Value, |
| 1973 |
SPIRVValue *ExpectedValue, |
| 1974 |
SPIRVBasicBlock *BB) { |
| 1975 |
return addInstruction(SPIRVInstTemplateBase::create( |
| 1976 |
OpExpectKHR, ResultTy, getId(), |
| 1977 |
getVec(Value->getId(), ExpectedValue->getId()), BB, |
| 1978 |
this), |
| 1979 |
BB); |
| 1980 |
} |
| 1981 |
|
| 1982 |
// Create AliasDomainDeclINTEL/AliasScopeDeclINTEL/AliasScopeListDeclINTEL |
| 1983 |
// instructions |
| 1984 |
template <typename AliasingInstType> |
| 1985 |
SPIRVEntry * |
| 1986 |
SPIRVModuleImpl::getOrAddMemAliasingINTELInst(std::vector<SPIRVId> Args, |
| 1987 |
llvm::MDNode *MD) { |
| 1988 |
assert(MD && "noalias/alias.scope metadata can't be null"); |
| 1989 |
// Don't duplicate aliasing instruction. For that use a map with a MDNode key |
| 1990 |
if (AliasInstMDMap.find(MD) != AliasInstMDMap.end()) |
| 1991 |
return AliasInstMDMap[MD]; |
| 1992 |
SPIRVEntry *AliasInst = add(new AliasingInstType(this, getId(), Args)); |
| 1993 |
AliasInstMDMap.emplace(std::make_pair(MD, AliasInst)); |
| 1994 |
return AliasInst; |
| 1995 |
} |
| 1996 |
|
| 1997 |
// Create AliasDomainDeclINTEL instruction |
| 1998 |
SPIRVEntry * |
| 1999 |
SPIRVModuleImpl::getOrAddAliasDomainDeclINTELInst(std::vector<SPIRVId> Args, |
| 2000 |
llvm::MDNode *MD) { |
| 2001 |
return getOrAddMemAliasingINTELInst<SPIRVAliasDomainDeclINTEL>(Args, MD); |
| 2002 |
} |
| 2003 |
|
| 2004 |
// Create AliasScopeDeclINTEL instruction |
| 2005 |
SPIRVEntry * |
| 2006 |
SPIRVModuleImpl::getOrAddAliasScopeDeclINTELInst(std::vector<SPIRVId> Args, |
| 2007 |
llvm::MDNode *MD) { |
| 2008 |
return getOrAddMemAliasingINTELInst<SPIRVAliasScopeDeclINTEL>(Args, MD); |
| 2009 |
} |
| 2010 |
|
| 2011 |
// Create AliasScopeListDeclINTEL instruction |
| 2012 |
SPIRVEntry * |
| 2013 |
SPIRVModuleImpl::getOrAddAliasScopeListDeclINTELInst(std::vector<SPIRVId> Args, |
| 2014 |
llvm::MDNode *MD) { |
| 2015 |
return getOrAddMemAliasingINTELInst<SPIRVAliasScopeListDeclINTEL>(Args, MD); |
| 2016 |
} |
| 2017 |
|
| 2018 |
SPIRVInstruction *SPIRVModuleImpl::addVariable( |
| 2019 |
SPIRVType *Type, bool IsConstant, SPIRVLinkageTypeKind LinkageTy, |
| 2020 |
SPIRVValue *Initializer, const std::string &Name, |
| 2021 |
SPIRVStorageClassKind StorageClass, SPIRVBasicBlock *BB) { |
| 2022 |
SPIRVVariable *Variable = new SPIRVVariable(Type, getId(), Initializer, Name, |
| 2023 |
StorageClass, BB, this); |
| 2024 |
if (BB) |
| 2025 |
return addInstruction(Variable, BB); |
| 2026 |
|
| 2027 |
add(Variable); |
| 2028 |
if (LinkageTy != internal::LinkageTypeInternal) |
| 2029 |
Variable->setLinkageType(LinkageTy); |
| 2030 |
Variable->setIsConstant(IsConstant); |
| 2031 |
return Variable; |
| 2032 |
} |
| 2033 |
|
| 2034 |
template <class T> |
| 2035 |
spv_ostream &operator<<(spv_ostream &O, const std::vector<T *> &V) { |
| 2036 |
for (auto &I : V) |
| 2037 |
O << *I; |
| 2038 |
return O; |
| 2039 |
} |
| 2040 |
|
| 2041 |
template <class T, class B = std::less<T>> |
| 2042 |
spv_ostream &operator<<(spv_ostream &O, const std::unordered_set<T *, B> &V) { |
| 2043 |
for (auto &I : V) |
| 2044 |
O << *I; |
| 2045 |
return O; |
| 2046 |
} |
| 2047 |
|
| 2048 |
// To satisfy SPIR-V spec requirement: |
| 2049 |
// "All operands must be declared before being used", |
| 2050 |
// we do DFS based topological sort |
| 2051 |
// https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search |
| 2052 |
class TopologicalSort { |
| 2053 |
enum DFSState : char { Unvisited, Discovered, Visited }; |
| 2054 |
typedef std::vector<SPIRVType *> SPIRVTypeVec; |
| 2055 |
typedef std::vector<SPIRVValue *> SPIRVConstantVector; |
| 2056 |
typedef std::vector<SPIRVVariable *> SPIRVVariableVec; |
| 2057 |
typedef std::vector<SPIRVEntry *> SPIRVConstAndVarVec; |
| 2058 |
typedef std::vector<SPIRVTypeForwardPointer *> SPIRVForwardPointerVec; |
| 2059 |
typedef std::function<bool(SPIRVEntry *, SPIRVEntry *)> Comp; |
| 2060 |
typedef std::map<SPIRVEntry *, DFSState, Comp> EntryStateMapTy; |
| 2061 |
typedef std::function<bool(const SPIRVTypeForwardPointer *, |
| 2062 |
const SPIRVTypeForwardPointer *)> |
| 2063 |
Equal; |
| 2064 |
typedef std::function<size_t(const SPIRVTypeForwardPointer *)> Hash; |
| 2065 |
// We may create forward pointers as we go through the types. We use |
| 2066 |
// unordered set to avoid duplicates. |
| 2067 |
typedef std::unordered_set<SPIRVTypeForwardPointer *, Hash, Equal> |
| 2068 |
SPIRVForwardPointerSet; |
| 2069 |
|
| 2070 |
SPIRVTypeVec TypeIntVec; |
| 2071 |
SPIRVConstantVector ConstIntVec; |
| 2072 |
SPIRVTypeVec TypeVec; |
| 2073 |
SPIRVConstAndVarVec ConstAndVarVec; |
| 2074 |
SPIRVForwardPointerSet ForwardPointerSet; |
| 2075 |
EntryStateMapTy EntryStateMap; |
| 2076 |
|
| 2077 |
friend spv_ostream &operator<<(spv_ostream &O, const TopologicalSort &S); |
| 2078 |
|
| 2079 |
// This method implements recursive depth-first search among all Entries in |
| 2080 |
// EntryStateMap. Traversing entries and adding them to corresponding |
| 2081 |
// container after visiting all dependent entries(post-order traversal) |
| 2082 |
// guarantees that the entry's operands will appear in the container before |
| 2083 |
// the entry itslef. |
| 2084 |
// Returns true if cyclic dependency detected. |
| 2085 |
bool visit(SPIRVEntry *E) { |
| 2086 |
DFSState &State = EntryStateMap[E]; |
| 2087 |
if (State == Visited) |
| 2088 |
return false; |
| 2089 |
if (State == Discovered) // Cyclic dependency detected |
| 2090 |
return true; |
| 2091 |
State = Discovered; |
| 2092 |
for (SPIRVEntry *Op : E->getNonLiteralOperands()) { |
| 2093 |
if (Op->getOpCode() == OpTypeForwardPointer) { |
| 2094 |
SPIRVEntry *FP = E->getModule()->getEntry( |
| 2095 |
static_cast<SPIRVTypeForwardPointer *>(Op)->getPointerId()); |
| 2096 |
Op = FP; |
| 2097 |
} |
| 2098 |
if (EntryStateMap[Op] == Visited) |
| 2099 |
continue; |
| 2100 |
if (visit(Op)) { |
| 2101 |
// We've found a recursive data type, e.g. a structure having a member |
| 2102 |
// which is a pointer to the same structure. |
| 2103 |
State = Unvisited; // Forget about it |
| 2104 |
if (E->getOpCode() == OpTypePointer) { |
| 2105 |
// If we have a pointer in the recursive chain, we can break the |
| 2106 |
// cyclic dependency by inserting a forward declaration of that |
| 2107 |
// pointer. |
| 2108 |
SPIRVTypePointer *Ptr = static_cast<SPIRVTypePointer *>(E); |
| 2109 |
SPIRVModule *BM = E->getModule(); |
| 2110 |
ForwardPointerSet.insert(BM->add(new SPIRVTypeForwardPointer( |
| 2111 |
BM, Ptr->getId(), Ptr->getPointerStorageClass()))); |
| 2112 |
return false; |
| 2113 |
} |
| 2114 |
return true; |
| 2115 |
} |
| 2116 |
} |
| 2117 |
Op OC = E->getOpCode(); |
| 2118 |
if (OC == OpTypeInt) |
| 2119 |
TypeIntVec.push_back(static_cast<SPIRVType *>(E)); |
| 2120 |
else if (isConstantOpCode(OC)) { |
| 2121 |
SPIRVConstant *C = static_cast<SPIRVConstant *>(E); |
| 2122 |
if (C->getType()->isTypeInt()) |
| 2123 |
ConstIntVec.push_back(C); |
| 2124 |
else |
| 2125 |
ConstAndVarVec.push_back(E); |
| 2126 |
} else if (isTypeOpCode(OC)) |
| 2127 |
TypeVec.push_back(static_cast<SPIRVType *>(E)); |
| 2128 |
else |
| 2129 |
ConstAndVarVec.push_back(E); |
| 2130 |
State = Visited; |
| 2131 |
return false; |
| 2132 |
} |
| 2133 |
|
| 2134 |
public: |
| 2135 |
TopologicalSort(const SPIRVTypeVec &TypeVec, |
| 2136 |
const SPIRVConstantVector &ConstVec, |
| 2137 |
const SPIRVVariableVec &VariableVec, |
| 2138 |
SPIRVForwardPointerVec &ForwardPointerVec) |
| 2139 |
: ForwardPointerSet( |
| 2140 |
16, // bucket count |
| 2141 |
[](const SPIRVTypeForwardPointer *Ptr) { |
| 2142 |
return std::hash<SPIRVId>()(Ptr->getPointerId()); |
| 2143 |
}, |
| 2144 |
[](const SPIRVTypeForwardPointer *Ptr1, |
| 2145 |
const SPIRVTypeForwardPointer *Ptr2) { |
| 2146 |
return Ptr1->getPointerId() == Ptr2->getPointerId(); |
| 2147 |
}), |
| 2148 |
EntryStateMap([](SPIRVEntry *A, SPIRVEntry *B) -> bool { |
| 2149 |
return A->getId() < B->getId(); |
| 2150 |
}) { |
| 2151 |
// Collect entries for sorting |
| 2152 |
for (auto *T : TypeVec) |
| 2153 |
EntryStateMap[T] = DFSState::Unvisited; |
| 2154 |
for (auto *C : ConstVec) |
| 2155 |
EntryStateMap[C] = DFSState::Unvisited; |
| 2156 |
for (auto *V : VariableVec) |
| 2157 |
EntryStateMap[V] = DFSState::Unvisited; |
| 2158 |
// Run topoligical sort |
| 2159 |
for (const auto &ES : EntryStateMap) { |
| 2160 |
if (visit(ES.first)) |
| 2161 |
llvm_unreachable("Cyclic dependency for types detected"); |
| 2162 |
} |
| 2163 |
// Append forward pointers vector |
| 2164 |
ForwardPointerVec.insert(ForwardPointerVec.end(), ForwardPointerSet.begin(), |
| 2165 |
ForwardPointerSet.end()); |
| 2166 |
} |
| 2167 |
}; |
| 2168 |
|
| 2169 |
spv_ostream &operator<<(spv_ostream &O, const TopologicalSort &S) { |
| 2170 |
O << S.TypeIntVec << S.ConstIntVec << S.TypeVec << S.ConstAndVarVec; |
| 2171 |
return O; |
| 2172 |
} |
| 2173 |
|
| 2174 |
spv_ostream &operator<<(spv_ostream &O, SPIRVModule &M) { |
| 2175 |
SPIRVModuleImpl &MI = *static_cast<SPIRVModuleImpl *>(&M); |
| 2176 |
// Start tracking of the current line with no line |
| 2177 |
MI.CurrentLine.reset(); |
| 2178 |
MI.CurrentDebugLine.reset(); |
| 2179 |
|
| 2180 |
SPIRVEncoder Encoder(O); |
| 2181 |
Encoder << MagicNumber << MI.SPIRVVersion |
| 2182 |
<< (((SPIRVWord)MI.GeneratorId << 16) | MI.GeneratorVer) |
| 2183 |
<< MI.NextId /* Bound for Id */ |
| 2184 |
<< MI.InstSchema; |
| 2185 |
O << SPIRVNL(); |
| 2186 |
|
| 2187 |
for (auto &I : MI.CapMap) |
| 2188 |
O << *I.second; |
| 2189 |
|
| 2190 |
for (auto &I : MI.ConditionalCapMap) |
| 2191 |
O << *I.second; |
| 2192 |
|
| 2193 |
for (auto &I : M.getExtension()) { |
| 2194 |
assert(!I.empty() && "Invalid extension"); |
| 2195 |
O << SPIRVExtension(&M, I); |
| 2196 |
} |
| 2197 |
|
| 2198 |
for (auto &I : M.getConditionalExtensions()) { |
| 2199 |
auto Cond = I.first; |
| 2200 |
auto Ext = I.second; |
| 2201 |
assert(!Ext.empty() && "Invalid conditional extension"); |
| 2202 |
O << SPIRVConditionalExtensionINTEL(&M, Cond, Ext); |
| 2203 |
} |
| 2204 |
|
| 2205 |
for (auto &I : MI.IdToInstSetMap) |
| 2206 |
O << SPIRVExtInstImport(&M, I.first, SPIRVBuiltinSetNameMap::map(I.second)); |
| 2207 |
|
| 2208 |
O << SPIRVMemoryModel(&M); |
| 2209 |
|
| 2210 |
O << MI.EntryPointVec; |
| 2211 |
O << MI.ConditionalEntryPointVec; |
| 2212 |
|
| 2213 |
for (auto &I : MI.EntryPointVec) |
| 2214 |
MI.get<SPIRVFunction>(I->getTargetId())->encodeExecutionModes(O); |
| 2215 |
|
| 2216 |
for (auto &I : MI.ConditionalEntryPointVec) |
| 2217 |
MI.get<SPIRVFunction>(I->getTargetId())->encodeExecutionModes(O); |
| 2218 |
|
| 2219 |
O << MI.StringVec; |
| 2220 |
|
| 2221 |
for (auto &I : M.getSourceExtension()) { |
| 2222 |
assert(!I.empty() && "Invalid source extension"); |
| 2223 |
O << SPIRVSourceExtension(&M, I); |
| 2224 |
} |
| 2225 |
|
| 2226 |
O << SPIRVSource(&M); |
| 2227 |
|
| 2228 |
for (auto &I : MI.NamedId) { |
| 2229 |
// Don't output name for entry point since it is redundant |
| 2230 |
bool IsEntryPoint = false; |
| 2231 |
for (auto &EPS : MI.EntryPointSet) |
| 2232 |
if (EPS.second.count(I)) { |
| 2233 |
IsEntryPoint = true; |
| 2234 |
break; |
| 2235 |
} |
| 2236 |
for (auto &EPS : MI.ConditionalEntryPointSet) |
| 2237 |
if (EPS.second.count(I)) { |
| 2238 |
IsEntryPoint = true; |
| 2239 |
break; |
| 2240 |
} |
| 2241 |
if (!IsEntryPoint) |
| 2242 |
M.getEntry(I)->encodeName(O); |
| 2243 |
} |
| 2244 |
|
| 2245 |
if (M.isAllowedToUseExtension( |
| 2246 |
ExtensionID::SPV_INTEL_memory_access_aliasing)) { |
| 2247 |
O << SPIRVNL() << MI.AliasInstMDVec; |
| 2248 |
} |
| 2249 |
|
| 2250 |
TopologicalSort TS(MI.TypeVec, MI.ConstVec, MI.VariableVec, |
| 2251 |
MI.ForwardPointerVec); |
| 2252 |
|
| 2253 |
O << MI.MemberNameVec << MI.ModuleProcessedVec << MI.DecGroupVec |
| 2254 |
<< MI.DecorateVec << MI.GroupDecVec << MI.ForwardPointerVec << TS; |
| 2255 |
|
| 2256 |
if (M.isAllowedToUseExtension(ExtensionID::SPV_INTEL_inline_assembly)) { |
| 2257 |
O << SPIRVNL() << MI.AsmTargetVec << MI.AsmVec; |
| 2258 |
} |
| 2259 |
|
| 2260 |
// At this point we know that FunctionDefinition could have been included both |
| 2261 |
// into DebugInstVec and into basick block of function from FuncVec. |
| 2262 |
// By spec we should only have this instruction to be present inside the |
| 2263 |
// function body, so removing it from the DebugInstVec to avoid duplication. |
| 2264 |
MI.DebugInstVec.erase( |
| 2265 |
std::remove_if(MI.DebugInstVec.begin(), MI.DebugInstVec.end(), |
| 2266 |
[](SPIRVExtInst *I) { |
| 2267 |
return I->getExtOp() == SPIRVDebug::FunctionDefinition; |
| 2268 |
}), |
| 2269 |
MI.DebugInstVec.end()); |
| 2270 |
|
| 2271 |
O << SPIRVNL() << MI.DebugInstVec << MI.AuxDataInstVec << SPIRVNL() |
| 2272 |
<< MI.FuncVec; |
| 2273 |
return O; |
| 2274 |
} |
| 2275 |
|
| 2276 |
template <class T> |
| 2277 |
void SPIRVModuleImpl::addTo(std::vector<T *> &V, SPIRVEntry *E) { |
| 2278 |
V.push_back(static_cast<T *>(E)); |
| 2279 |
} |
| 2280 |
|
| 2281 |
// The first decoration group includes all the previously defined decorates. |
| 2282 |
// The second decoration group includes all the decorates defined between the |
| 2283 |
// first and second decoration group. So long so forth. |
| 2284 |
SPIRVDecorationGroup *SPIRVModuleImpl::addDecorationGroup() { |
| 2285 |
return addDecorationGroup(new SPIRVDecorationGroup(this, getId())); |
| 2286 |
} |
| 2287 |
|
| 2288 |
SPIRVDecorationGroup * |
| 2289 |
SPIRVModuleImpl::addDecorationGroup(SPIRVDecorationGroup *Group) { |
| 2290 |
add(Group); |
| 2291 |
Group->takeDecorates(DecorateVec); |
| 2292 |
DecGroupVec.push_back(Group); |
| 2293 |
SPIRVDBG(spvdbgs() << "[addDecorationGroup] {" << *Group << "}\n"; |
| 2294 |
spvdbgs() << " Remaining DecorateVec: {" << DecorateVec << "}\n"); |
| 2295 |
assert(DecorateVec.empty()); |
| 2296 |
return Group; |
| 2297 |
} |
| 2298 |
|
| 2299 |
SPIRVGroupDecorateGeneric * |
| 2300 |
SPIRVModuleImpl::addGroupDecorateGeneric(SPIRVGroupDecorateGeneric *GDec) { |
| 2301 |
add(GDec); |
| 2302 |
GDec->decorateTargets(); |
| 2303 |
GroupDecVec.push_back(GDec); |
| 2304 |
return GDec; |
| 2305 |
} |
| 2306 |
SPIRVGroupDecorate * |
| 2307 |
SPIRVModuleImpl::addGroupDecorate(SPIRVDecorationGroup *Group, |
| 2308 |
const std::vector<SPIRVEntry *> &Targets) { |
| 2309 |
auto GD = new SPIRVGroupDecorate(Group, getIds(Targets)); |
| 2310 |
addGroupDecorateGeneric(GD); |
| 2311 |
return GD; |
| 2312 |
} |
| 2313 |
|
| 2314 |
SPIRVGroupMemberDecorate *SPIRVModuleImpl::addGroupMemberDecorate( |
| 2315 |
SPIRVDecorationGroup *Group, const std::vector<SPIRVEntry *> &Targets) { |
| 2316 |
auto GMD = new SPIRVGroupMemberDecorate(Group, getIds(Targets)); |
| 2317 |
addGroupDecorateGeneric(GMD); |
| 2318 |
return GMD; |
| 2319 |
} |
| 2320 |
|
| 2321 |
SPIRVString *SPIRVModuleImpl::getString(const std::string &Str) { |
| 2322 |
auto Loc = StrMap.find(Str); |
| 2323 |
if (Loc != StrMap.end()) |
| 2324 |
return Loc->second; |
| 2325 |
auto S = add(new SPIRVString(this, getId(), Str)); |
| 2326 |
StrMap[Str] = S; |
| 2327 |
return S; |
| 2328 |
} |
| 2329 |
|
| 2330 |
SPIRVMemberName *SPIRVModuleImpl::addMemberName(SPIRVTypeStruct *ST, |
| 2331 |
SPIRVWord MemberNumber, |
| 2332 |
const std::string &Name) { |
| 2333 |
return add(new SPIRVMemberName(ST, MemberNumber, Name)); |
| 2334 |
} |
| 2335 |
|
| 2336 |
void SPIRVModuleImpl::addUnknownStructField(SPIRVTypeStruct *Struct, unsigned I, |
| 2337 |
SPIRVId ID) { |
| 2338 |
UnknownStructFieldMap[Struct].push_back(std::make_pair(I, ID)); |
| 2339 |
} |
| 2340 |
|
| 2341 |
std::istream &operator>>(std::istream &I, SPIRVModule &M) { |
| 2342 |
SPIRVDecoder Decoder(I, M); |
| 2343 |
SPIRVModuleImpl &MI = *static_cast<SPIRVModuleImpl *>(&M); |
| 2344 |
// Disable automatic capability filling. |
| 2345 |
MI.setAutoAddCapability(false); |
| 2346 |
MI.setAutoAddExtensions(false); |
| 2347 |
|
| 2348 |
SPIRVWord Magic; |
| 2349 |
Decoder >> Magic; |
| 2350 |
if (!M.getErrorLog().checkError(Magic == MagicNumber, SPIRVEC_InvalidModule, |
| 2351 |
"invalid magic number")) { |
| 2352 |
M.setInvalid(); |
| 2353 |
return I; |
| 2354 |
} |
| 2355 |
|
| 2356 |
Decoder >> MI.SPIRVVersion; |
| 2357 |
bool SPIRVVersionIsKnown = isSPIRVVersionKnown(MI.SPIRVVersion); |
| 2358 |
if (!M.getErrorLog().checkError( |
| 2359 |
SPIRVVersionIsKnown, SPIRVEC_InvalidModule, |
| 2360 |
"unsupported SPIR-V version number '" + to_string(MI.SPIRVVersion) + |
| 2361 |
"'. Range of supported/known SPIR-V " |
| 2362 |
"versions is " + |
| 2363 |
to_string(VersionNumber::MinimumVersion) + " - " + |
| 2364 |
to_string(VersionNumber::MaximumVersion))) { |
| 2365 |
M.setInvalid(); |
| 2366 |
return I; |
| 2367 |
} |
| 2368 |
|
| 2369 |
bool SPIRVVersionIsAllowed = M.isAllowedToUseVersion(MI.SPIRVVersion); |
| 2370 |
if (!M.getErrorLog().checkError( |
| 2371 |
SPIRVVersionIsAllowed, SPIRVEC_InvalidModule, |
| 2372 |
"incorrect SPIR-V version number " + to_string(MI.SPIRVVersion) + |
| 2373 |
" - it conflicts with --spirv-max-version which is set to " + |
| 2374 |
to_string(M.getMaximumAllowedSPIRVVersion()))) { |
| 2375 |
M.setInvalid(); |
| 2376 |
return I; |
| 2377 |
} |
| 2378 |
|
| 2379 |
SPIRVWord Generator = 0; |
| 2380 |
Decoder >> Generator; |
| 2381 |
MI.GeneratorId = Generator >> 16; |
| 2382 |
MI.GeneratorVer = Generator & 0xFFFF; |
| 2383 |
|
| 2384 |
// Bound for Id |
| 2385 |
Decoder >> MI.NextId; |
| 2386 |
|
| 2387 |
Decoder >> MI.InstSchema; |
| 2388 |
if (!M.getErrorLog().checkError(MI.InstSchema == SPIRVISCH_Default, |
| 2389 |
SPIRVEC_InvalidModule, |
| 2390 |
"unsupported instruction schema")) { |
| 2391 |
M.setInvalid(); |
| 2392 |
return I; |
| 2393 |
} |
| 2394 |
|
| 2395 |
while (Decoder.getWordCountAndOpCode() && M.isModuleValid()) { |
| 2396 |
SPIRVEntry *Entry = Decoder.getEntry(); |
| 2397 |
if (Entry != nullptr) |
| 2398 |
M.add(Entry); |
| 2399 |
} |
| 2400 |
|
| 2401 |
MI.resolveUnknownStructFields(); |
| 2402 |
return I; |
| 2403 |
} |
| 2404 |
|
| 2405 |
SPIRVModule *SPIRVModule::createSPIRVModule() { return new SPIRVModuleImpl(); } |
| 2406 |
|
| 2407 |
SPIRVModule *SPIRVModule::createSPIRVModule(const SPIRV::TranslatorOpts &Opts) { |
| 2408 |
return new SPIRVModuleImpl(Opts); |
| 2409 |
} |
| 2410 |
|
| 2411 |
SPIRVValue *SPIRVModuleImpl::getValue(SPIRVId TheId) const { |
| 2412 |
return get<SPIRVValue>(TheId); |
| 2413 |
} |
| 2414 |
|
| 2415 |
SPIRVType *SPIRVModuleImpl::getValueType(SPIRVId TheId) const { |
| 2416 |
return get<SPIRVValue>(TheId)->getType(); |
| 2417 |
} |
| 2418 |
|
| 2419 |
std::vector<SPIRVValue *> |
| 2420 |
SPIRVModuleImpl::getValues(const std::vector<SPIRVId> &IdVec) const { |
| 2421 |
std::vector<SPIRVValue *> ValueVec; |
| 2422 |
for (auto I : IdVec) |
| 2423 |
ValueVec.push_back(getValue(I)); |
| 2424 |
return ValueVec; |
| 2425 |
} |
| 2426 |
|
| 2427 |
std::vector<SPIRVType *> |
| 2428 |
SPIRVModuleImpl::getValueTypes(const std::vector<SPIRVId> &IdVec) const { |
| 2429 |
std::vector<SPIRVType *> TypeVec; |
| 2430 |
for (auto I : IdVec) |
| 2431 |
TypeVec.push_back(getValue(I)->getType()); |
| 2432 |
return TypeVec; |
| 2433 |
} |
| 2434 |
|
| 2435 |
std::vector<SPIRVId> |
| 2436 |
SPIRVModuleImpl::getIds(const std::vector<SPIRVEntry *> &ValueVec) const { |
| 2437 |
std::vector<SPIRVId> IdVec; |
| 2438 |
for (auto I : ValueVec) |
| 2439 |
IdVec.push_back(I->getId()); |
| 2440 |
return IdVec; |
| 2441 |
} |
| 2442 |
|
| 2443 |
std::vector<SPIRVId> |
| 2444 |
SPIRVModuleImpl::getIds(const std::vector<SPIRVValue *> &ValueVec) const { |
| 2445 |
std::vector<SPIRVId> IdVec; |
| 2446 |
for (auto I : ValueVec) |
| 2447 |
IdVec.push_back(I->getId()); |
| 2448 |
return IdVec; |
| 2449 |
} |
| 2450 |
|
| 2451 |
SPIRVInstTemplateBase * |
| 2452 |
SPIRVModuleImpl::addInstTemplate(Op OC, SPIRVBasicBlock *BB, SPIRVType *Ty) { |
| 2453 |
assert(!Ty || !Ty->isTypeVoid()); |
| 2454 |
SPIRVId Id = Ty ? getId() : SPIRVID_INVALID; |
| 2455 |
auto Ins = SPIRVInstTemplateBase::create(OC, Ty, Id, BB, this); |
| 2456 |
BB->addInstruction(Ins); |
| 2457 |
return Ins; |
| 2458 |
} |
| 2459 |
|
| 2460 |
SPIRVInstTemplateBase * |
| 2461 |
SPIRVModuleImpl::addInstTemplate(Op OC, const std::vector<SPIRVWord> &Ops, |
| 2462 |
SPIRVBasicBlock *BB, SPIRVType *Ty) { |
| 2463 |
assert(!Ty || !Ty->isTypeVoid()); |
| 2464 |
SPIRVId Id = Ty ? getId() : SPIRVID_INVALID; |
| 2465 |
auto Ins = SPIRVInstTemplateBase::create(OC, Ty, Id, Ops, BB, this); |
| 2466 |
BB->addInstruction(Ins); |
| 2467 |
return Ins; |
| 2468 |
} |
| 2469 |
|
| 2470 |
void SPIRVModuleImpl::addInstTemplate(SPIRVInstTemplateBase *Ins, |
| 2471 |
const std::vector<SPIRVWord> &Ops, |
| 2472 |
SPIRVBasicBlock *BB, SPIRVType *Ty) { |
| 2473 |
assert(!Ty || !Ty->isTypeVoid()); |
| 2474 |
SPIRVId Id = Ty ? getId() : SPIRVID_INVALID; |
| 2475 |
Ins->init(Ty, Id, BB, this); |
| 2476 |
Ins->setOpWordsAndValidate(Ops); |
| 2477 |
BB->addInstruction(Ins); |
| 2478 |
} |
| 2479 |
|
| 2480 |
SPIRVId SPIRVModuleImpl::getExtInstSetId(SPIRVExtInstSetKind Kind) const { |
| 2481 |
assert(Kind < SPIRVEIS_Count && "Unknown extended instruction set!"); |
| 2482 |
auto Res = ExtInstSetIds.find(Kind); |
| 2483 |
assert(Res != ExtInstSetIds.end() && "extended instruction set not found!"); |
| 2484 |
return Res->second; |
| 2485 |
} |
| 2486 |
|
| 2487 |
bool isSpirvBinary(const std::string &Img) { |
| 2488 |
if (Img.size() < sizeof(unsigned)) |
| 2489 |
return false; |
| 2490 |
auto Magic = reinterpret_cast<const unsigned *>(Img.data()); |
| 2491 |
return *Magic == MagicNumber; |
| 2492 |
} |
| 2493 |
|
| 2494 |
#ifdef _SPIRV_SUPPORT_TEXT_FMT |
| 2495 |
|
| 2496 |
bool convertSpirv(std::istream &IS, std::ostream &OS, std::string &ErrMsg, |
| 2497 |
bool FromText, bool ToText) { |
| 2498 |
auto SaveOpt = SPIRVUseTextFormat; |
| 2499 |
SPIRVUseTextFormat = FromText; |
| 2500 |
// Conversion from/to SPIR-V text representation is a side feature of the |
| 2501 |
// translator which is mostly intended for debug usage. So, this step cannot |
| 2502 |
// be customized to enable/disable particular extensions or restrict/allow |
| 2503 |
// particular SPIR-V versions: all known SPIR-V versions are allowed, all |
| 2504 |
// known SPIR-V extensions are enabled during this conversion |
| 2505 |
SPIRV::TranslatorOpts DefaultOpts; |
| 2506 |
DefaultOpts.enableAllExtensions(); |
| 2507 |
SPIRVModuleImpl M(DefaultOpts); |
| 2508 |
IS >> M; |
| 2509 |
if (M.getError(ErrMsg) != SPIRVEC_Success) { |
| 2510 |
SPIRVUseTextFormat = SaveOpt; |
| 2511 |
return false; |
| 2512 |
} |
| 2513 |
SPIRVUseTextFormat = ToText; |
| 2514 |
OS << M; |
| 2515 |
if (M.getError(ErrMsg) != SPIRVEC_Success) { |
| 2516 |
SPIRVUseTextFormat = SaveOpt; |
| 2517 |
return false; |
| 2518 |
} |
| 2519 |
SPIRVUseTextFormat = SaveOpt; |
| 2520 |
return true; |
| 2521 |
} |
| 2522 |
|
| 2523 |
bool isSpirvText(const std::string &Img) { |
| 2524 |
std::istringstream SS(Img); |
| 2525 |
unsigned Magic = 0; |
| 2526 |
SS >> Magic; |
| 2527 |
if (SS.bad()) |
| 2528 |
return false; |
| 2529 |
return Magic == MagicNumber; |
| 2530 |
} |
| 2531 |
|
| 2532 |
bool convertSpirv(std::string &Input, std::string &Out, std::string &ErrMsg, |
| 2533 |
bool ToText) { |
| 2534 |
auto FromText = isSpirvText(Input); |
| 2535 |
if (ToText == FromText) { |
| 2536 |
Out = Input; |
| 2537 |
return true; |
| 2538 |
} |
| 2539 |
std::istringstream IS(Input); |
| 2540 |
std::ostringstream OS; |
| 2541 |
if (!convertSpirv(IS, OS, ErrMsg, FromText, ToText)) |
| 2542 |
return false; |
| 2543 |
Out = OS.str(); |
| 2544 |
return true; |
| 2545 |
} |
| 2546 |
|
| 2547 |
#endif // _SPIRV_SUPPORT_TEXT_FMT |
| 2548 |
|
| 2549 |
} // namespace SPIRV |