| 1 |
//===- SPIRVEntry.cpp - Base Class for SPIR-V Entities ----------*- 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 base class for SPIR-V entities. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#include "SPIRVEntry.h" |
| 41 |
#include "SPIRVAsm.h" |
| 42 |
#include "SPIRVBasicBlock.h" |
| 43 |
#include "SPIRVDebug.h" |
| 44 |
#include "SPIRVDecorate.h" |
| 45 |
#include "SPIRVFnVar.h" |
| 46 |
#include "SPIRVFunction.h" |
| 47 |
#include "SPIRVInstruction.h" |
| 48 |
#include "SPIRVMemAliasingINTEL.h" |
| 49 |
#include "SPIRVStream.h" |
| 50 |
#include "SPIRVType.h" |
| 51 |
|
| 52 |
#include <algorithm> |
| 53 |
#include <map> |
| 54 |
#include <set> |
| 55 |
#include <sstream> |
| 56 |
#include <string> |
| 57 |
#include <utility> |
| 58 |
|
| 59 |
using namespace SPIRV; |
| 60 |
|
| 61 |
namespace SPIRV { |
| 62 |
|
| 63 |
template <typename T> SPIRVEntry *create() { return new T(); } |
| 64 |
|
| 65 |
SPIRVEntry *SPIRVEntry::create(Op OpCode) { |
| 66 |
typedef SPIRVEntry *(*SPIRVFactoryTy)(); |
| 67 |
struct TableEntry { |
| 68 |
Op Opn; |
| 69 |
SPIRVFactoryTy Factory; |
| 70 |
operator std::pair<const Op, SPIRVFactoryTy>() { |
| 71 |
return std::make_pair(Opn, Factory); |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
static TableEntry Table[] = { |
| 76 |
#define _SPIRV_OP(x, ...) {Op##x, &SPIRV::create<SPIRV##x>}, |
| 77 |
#define _SPIRV_OP_INTERNAL(x, ...) {internal::Op##x, &SPIRV::create<SPIRV##x>}, |
| 78 |
#include "SPIRVOpCodeEnum.h" |
| 79 |
#include "SPIRVOpCodeEnumInternal.h" |
| 80 |
#undef _SPIRV_OP_INTERNAL |
| 81 |
#undef _SPIRV_OP |
| 82 |
}; |
| 83 |
|
| 84 |
typedef std::map<Op, SPIRVFactoryTy> OpToFactoryMapTy; |
| 85 |
static const OpToFactoryMapTy OpToFactoryMap(std::begin(Table), |
| 86 |
std::end(Table)); |
| 87 |
|
| 88 |
OpToFactoryMapTy::const_iterator Loc = OpToFactoryMap.find(OpCode); |
| 89 |
if (Loc != OpToFactoryMap.end()) |
| 90 |
return Loc->second(); |
| 91 |
|
| 92 |
SPIRVDBG(spvdbgs() << "No factory for OpCode " << (unsigned)OpCode << '\n';) |
| 93 |
assert(0 && "Not implemented"); |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
std::unique_ptr<SPIRV::SPIRVEntry> SPIRVEntry::createUnique(Op OC) { |
| 98 |
return std::unique_ptr<SPIRVEntry>(create(OC)); |
| 99 |
} |
| 100 |
|
| 101 |
std::unique_ptr<SPIRV::SPIRVExtInst> |
| 102 |
SPIRVEntry::createUnique(SPIRVExtInstSetKind Set, unsigned ExtOp) { |
| 103 |
return std::unique_ptr<SPIRVExtInst>(new SPIRVExtInst(Set, ExtOp)); |
| 104 |
} |
| 105 |
|
| 106 |
SPIRVErrorLog &SPIRVEntry::getErrorLog() const { return Module->getErrorLog(); } |
| 107 |
|
| 108 |
bool SPIRVEntry::exist(SPIRVId TheId) const { return Module->exist(TheId); } |
| 109 |
|
| 110 |
SPIRVEntry *SPIRVEntry::getOrCreate(SPIRVId TheId) const { |
| 111 |
SPIRVEntry *Entry = nullptr; |
| 112 |
bool Found = Module->exist(TheId, &Entry); |
| 113 |
if (!Found) |
| 114 |
return Module->addForward(TheId, nullptr); |
| 115 |
return Entry; |
| 116 |
} |
| 117 |
|
| 118 |
SPIRVValue *SPIRVEntry::getValue(SPIRVId TheId) const { |
| 119 |
return get<SPIRVValue>(TheId); |
| 120 |
} |
| 121 |
|
| 122 |
SPIRVType *SPIRVEntry::getValueType(SPIRVId TheId) const { |
| 123 |
return get<SPIRVValue>(TheId)->getType(); |
| 124 |
} |
| 125 |
|
| 126 |
SPIRVEncoder SPIRVEntry::getEncoder(spv_ostream &O) const { |
| 127 |
return SPIRVEncoder(O); |
| 128 |
} |
| 129 |
|
| 130 |
SPIRVDecoder SPIRVEntry::getDecoder(std::istream &I) { |
| 131 |
return SPIRVDecoder(I, *Module); |
| 132 |
} |
| 133 |
|
| 134 |
void SPIRVEntry::setWordCount(SPIRVWord TheWordCount) { |
| 135 |
WordCount = TheWordCount; |
| 136 |
} |
| 137 |
|
| 138 |
void SPIRVEntry::setName(const std::string &TheName) { |
| 139 |
Name = TheName; |
| 140 |
SPIRVDBG(spvdbgs() << "Set name for obj " << Id << " " << Name << '\n'); |
| 141 |
} |
| 142 |
|
| 143 |
void SPIRVEntry::setModule(SPIRVModule *TheModule) { |
| 144 |
assert(TheModule && "Invalid module"); |
| 145 |
if (TheModule == Module) |
| 146 |
return; |
| 147 |
assert(Module == NULL && "Cannot change owner of entry"); |
| 148 |
Module = TheModule; |
| 149 |
} |
| 150 |
|
| 151 |
void SPIRVEntry::encode(spv_ostream &O) const { |
| 152 |
assert(0 && "Not implemented"); |
| 153 |
} |
| 154 |
|
| 155 |
void SPIRVEntry::encodeName(spv_ostream &O) const { |
| 156 |
if (!Name.empty()) |
| 157 |
O << SPIRVName(this, Name); |
| 158 |
} |
| 159 |
|
| 160 |
bool SPIRVEntry::isEndOfBlock() const { |
| 161 |
switch (OpCode) { |
| 162 |
case OpBranch: |
| 163 |
case OpBranchConditional: |
| 164 |
case OpSwitch: |
| 165 |
case OpKill: |
| 166 |
case OpReturn: |
| 167 |
case OpReturnValue: |
| 168 |
case OpUnreachable: |
| 169 |
return true; |
| 170 |
default: |
| 171 |
return false; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
void SPIRVEntry::encodeLine(spv_ostream &O) const { |
| 176 |
if (!Module) |
| 177 |
return; |
| 178 |
const std::shared_ptr<const SPIRVLine> &CurrLine = Module->getCurrentLine(); |
| 179 |
if (Line && (!CurrLine || *Line != *CurrLine)) { |
| 180 |
O << *Line; |
| 181 |
Module->setCurrentLine(Line); |
| 182 |
} |
| 183 |
if (isEndOfBlock() || OpCode == OpNoLine) |
| 184 |
Module->setCurrentLine(nullptr); |
| 185 |
} |
| 186 |
|
| 187 |
namespace { |
| 188 |
bool isDebugLineEqual(const SPIRVExtInst &DL1, const SPIRVExtInst &DL2) { |
| 189 |
std::vector<SPIRVWord> DL1Args = DL1.getArguments(); |
| 190 |
std::vector<SPIRVWord> DL2Args = DL2.getArguments(); |
| 191 |
|
| 192 |
using namespace SPIRVDebug::Operand::DebugLine; |
| 193 |
assert(DL1Args.size() == OperandCount && DL2Args.size() == OperandCount && |
| 194 |
"Invalid number of operands"); |
| 195 |
return DL1Args[SourceIdx] == DL2Args[SourceIdx] && |
| 196 |
DL1Args[StartIdx] == DL2Args[StartIdx] && |
| 197 |
DL1Args[EndIdx] == DL2Args[EndIdx] && |
| 198 |
DL1Args[ColumnStartIdx] == DL2Args[ColumnStartIdx] && |
| 199 |
DL1Args[ColumnEndIdx] == DL2Args[ColumnEndIdx]; |
| 200 |
} |
| 201 |
} // namespace |
| 202 |
|
| 203 |
void SPIRVEntry::encodeDebugLine(spv_ostream &O) const { |
| 204 |
if (!Module) |
| 205 |
return; |
| 206 |
const std::shared_ptr<const SPIRVExtInst> &CurrDebugLine = |
| 207 |
Module->getCurrentDebugLine(); |
| 208 |
if (DebugLine && |
| 209 |
(!CurrDebugLine || !isDebugLineEqual(*DebugLine, *CurrDebugLine))) { |
| 210 |
O << *DebugLine; |
| 211 |
Module->setCurrentDebugLine(DebugLine); |
| 212 |
} |
| 213 |
if (isEndOfBlock() || |
| 214 |
isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100, |
| 215 |
SPIRVDebug::DebugNoLine) || |
| 216 |
isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200, |
| 217 |
SPIRVDebug::DebugNoLine)) |
| 218 |
Module->setCurrentDebugLine(nullptr); |
| 219 |
} |
| 220 |
|
| 221 |
void SPIRVEntry::encodeAll(spv_ostream &O) const { |
| 222 |
encodeLine(O); |
| 223 |
encodeDebugLine(O); |
| 224 |
encodeWordCountOpCode(O); |
| 225 |
encode(O); |
| 226 |
encodeChildren(O); |
| 227 |
} |
| 228 |
|
| 229 |
void SPIRVEntry::encodeChildren(spv_ostream &O) const {} |
| 230 |
|
| 231 |
void SPIRVEntry::encodeWordCountOpCode(spv_ostream &O) const { |
| 232 |
#ifdef _SPIRV_SUPPORT_TEXT_FMT |
| 233 |
if (SPIRVUseTextFormat) { |
| 234 |
getEncoder(O) << WordCount << OpCode; |
| 235 |
return; |
| 236 |
} |
| 237 |
#endif |
| 238 |
assert(WordCount < 65536 && "WordCount must fit into 16-bit value"); |
| 239 |
SPIRVWord WordCountOpCode = (WordCount << WordCountShift) | OpCode; |
| 240 |
getEncoder(O) << WordCountOpCode; |
| 241 |
} |
| 242 |
// Read words from SPIRV binary and create members for SPIRVEntry. |
| 243 |
// The word count and op code has already been read before calling this |
| 244 |
// function for creating the SPIRVEntry. Therefore the input stream only |
| 245 |
// contains the remaining part of the words for the SPIRVEntry. |
| 246 |
void SPIRVEntry::decode(std::istream &I) { assert(0 && "Not implemented"); } |
| 247 |
|
| 248 |
std::vector<SPIRVValue *> |
| 249 |
SPIRVEntry::getValues(const std::vector<SPIRVId> &IdVec) const { |
| 250 |
std::vector<SPIRVValue *> ValueVec; |
| 251 |
for (auto I : IdVec) |
| 252 |
ValueVec.push_back(getValue(I)); |
| 253 |
return ValueVec; |
| 254 |
} |
| 255 |
|
| 256 |
std::vector<SPIRVType *> |
| 257 |
SPIRVEntry::getValueTypes(const std::vector<SPIRVId> &IdVec) const { |
| 258 |
std::vector<SPIRVType *> TypeVec; |
| 259 |
for (auto I : IdVec) |
| 260 |
TypeVec.push_back(getValue(I)->getType()); |
| 261 |
return TypeVec; |
| 262 |
} |
| 263 |
|
| 264 |
std::vector<SPIRVId> |
| 265 |
SPIRVEntry::getIds(const std::vector<SPIRVValue *> ValueVec) const { |
| 266 |
std::vector<SPIRVId> IdVec; |
| 267 |
for (auto I : ValueVec) |
| 268 |
IdVec.push_back(I->getId()); |
| 269 |
return IdVec; |
| 270 |
} |
| 271 |
|
| 272 |
SPIRVEntry *SPIRVEntry::getEntry(SPIRVId TheId) const { |
| 273 |
return Module->getEntry(TheId); |
| 274 |
} |
| 275 |
|
| 276 |
void SPIRVEntry::validateFunctionControlMask(SPIRVWord TheFCtlMask) const { |
| 277 |
SPIRVCK(isValidFunctionControlMask(TheFCtlMask), InvalidFunctionControlMask, |
| 278 |
""); |
| 279 |
} |
| 280 |
|
| 281 |
void SPIRVEntry::validateValues(const std::vector<SPIRVId> &Ids) const { |
| 282 |
for (auto I : Ids) |
| 283 |
getValue(I)->validate(); |
| 284 |
} |
| 285 |
|
| 286 |
void SPIRVEntry::validateBuiltin(SPIRVWord TheSet, SPIRVWord Index) const { |
| 287 |
assert(TheSet != SPIRVWORD_MAX && Index != SPIRVWORD_MAX && |
| 288 |
"Invalid builtin"); |
| 289 |
} |
| 290 |
|
| 291 |
void SPIRVEntry::addDecorate(SPIRVDecorate *Dec) { |
| 292 |
auto Kind = Dec->getDecorateKind(); |
| 293 |
Decorates.insert(std::make_pair(Kind, Dec)); |
| 294 |
Module->addDecorate(Dec); |
| 295 |
if (Kind == spv::DecorationLinkageAttributes) { |
| 296 |
auto *LinkageAttr = static_cast<const SPIRVDecorateLinkageAttr *>(Dec); |
| 297 |
setName(LinkageAttr->getLinkageName()); |
| 298 |
} |
| 299 |
SPIRVDBG(spvdbgs() << "[addDecorate] " << *Dec << '\n';) |
| 300 |
} |
| 301 |
|
| 302 |
void SPIRVEntry::addDecorate(SPIRVDecorateId *Dec) { |
| 303 |
DecorateIds.insert(std::make_pair(Dec->getDecorateKind(), Dec)); |
| 304 |
Module->addDecorate(Dec); |
| 305 |
SPIRVDBG(spvdbgs() << "[addDecorateId] " << *Dec << '\n';) |
| 306 |
} |
| 307 |
|
| 308 |
void SPIRVEntry::addDecorate(Decoration Kind) { |
| 309 |
addDecorate(new SPIRVDecorate(Kind, this)); |
| 310 |
} |
| 311 |
|
| 312 |
void SPIRVEntry::addDecorate(Decoration Kind, SPIRVWord Literal) { |
| 313 |
switch (static_cast<int>(Kind)) { |
| 314 |
case DecorationAliasScopeINTEL: |
| 315 |
case DecorationNoAliasINTEL: |
| 316 |
addDecorate(new SPIRVDecorateId(Kind, this, Literal)); |
| 317 |
return; |
| 318 |
default: |
| 319 |
addDecorate(new SPIRVDecorate(Kind, this, Literal)); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
void SPIRVEntry::eraseDecorate(Decoration Dec) { Decorates.erase(Dec); } |
| 324 |
|
| 325 |
void SPIRVEntry::takeDecorates(SPIRVEntry *E) { |
| 326 |
Decorates = std::move(E->Decorates); |
| 327 |
SPIRVDBG(spvdbgs() << "[takeDecorates] " << Id << '\n';) |
| 328 |
} |
| 329 |
|
| 330 |
void SPIRVEntry::takeDecorateIds(SPIRVEntry *E) { |
| 331 |
DecorateIds = std::move(E->DecorateIds); |
| 332 |
SPIRVDBG(spvdbgs() << "[takeDecorateIds] " << Id << '\n';) |
| 333 |
} |
| 334 |
|
| 335 |
void SPIRVEntry::setLine(const std::shared_ptr<const SPIRVLine> &L) { |
| 336 |
Line = L; |
| 337 |
SPIRVDBG(if (L) spvdbgs() << "[setLine] " << *L << '\n';) |
| 338 |
} |
| 339 |
|
| 340 |
void SPIRVEntry::setDebugLine(const std::shared_ptr<const SPIRVExtInst> &DL) { |
| 341 |
DebugLine = DL; |
| 342 |
SPIRVDBG(if (DL) spvdbgs() << "[setDebugLine] " << *DL << '\n';) |
| 343 |
} |
| 344 |
|
| 345 |
void SPIRVEntry::addMemberDecorate(SPIRVMemberDecorate *Dec) { |
| 346 |
assert(canHaveMemberDecorates()); |
| 347 |
MemberDecorates.insert(std::make_pair( |
| 348 |
std::make_pair(Dec->getMemberNumber(), Dec->getDecorateKind()), Dec)); |
| 349 |
Module->addDecorate(Dec); |
| 350 |
SPIRVDBG(spvdbgs() << "[addMemberDecorate] " << *Dec << '\n';) |
| 351 |
} |
| 352 |
|
| 353 |
void SPIRVEntry::addMemberDecorate(SPIRVWord MemberNumber, Decoration Kind) { |
| 354 |
addMemberDecorate(new SPIRVMemberDecorate(Kind, MemberNumber, this)); |
| 355 |
} |
| 356 |
|
| 357 |
void SPIRVEntry::addMemberDecorate(SPIRVWord MemberNumber, Decoration Kind, |
| 358 |
SPIRVWord Literal) { |
| 359 |
addMemberDecorate(new SPIRVMemberDecorate(Kind, MemberNumber, this, Literal)); |
| 360 |
} |
| 361 |
|
| 362 |
void SPIRVEntry::eraseMemberDecorate(SPIRVWord MemberNumber, Decoration Dec) { |
| 363 |
MemberDecorates.erase(std::make_pair(MemberNumber, Dec)); |
| 364 |
} |
| 365 |
|
| 366 |
void SPIRVEntry::takeMemberDecorates(SPIRVEntry *E) { |
| 367 |
MemberDecorates = std::move(E->MemberDecorates); |
| 368 |
SPIRVDBG(spvdbgs() << "[takeMemberDecorates] " << Id << '\n';) |
| 369 |
} |
| 370 |
|
| 371 |
void SPIRVEntry::takeAnnotations(SPIRVForward *E) { |
| 372 |
Module->setName(this, E->getName()); |
| 373 |
takeDecorates(E); |
| 374 |
takeDecorateIds(E); |
| 375 |
takeMemberDecorates(E); |
| 376 |
if (OpCode == OpFunction) |
| 377 |
static_cast<SPIRVFunction *>(this)->takeExecutionModes(E); |
| 378 |
} |
| 379 |
|
| 380 |
void SPIRVEntry::replaceTargetIdInDecorates(SPIRVId Id) { |
| 381 |
for (auto It = Decorates.begin(), E = Decorates.end(); It != E; ++It) |
| 382 |
const_cast<SPIRVDecorate *>(It->second)->setTargetId(Id); |
| 383 |
for (auto It = DecorateIds.begin(), E = DecorateIds.end(); It != E; ++It) |
| 384 |
const_cast<SPIRVDecorateId *>(It->second)->setTargetId(Id); |
| 385 |
for (auto It = MemberDecorates.begin(), E = MemberDecorates.end(); It != E; |
| 386 |
++It) |
| 387 |
const_cast<SPIRVMemberDecorate *>(It->second)->setTargetId(Id); |
| 388 |
} |
| 389 |
|
| 390 |
// Check if an entry has Kind of decoration and get the literal of the |
| 391 |
// first decoration of such kind at Index. |
| 392 |
bool SPIRVEntry::hasDecorate(Decoration Kind, size_t Index, |
| 393 |
SPIRVWord *Result) const { |
| 394 |
auto Loc = Decorates.find(Kind); |
| 395 |
if (Loc == Decorates.end()) |
| 396 |
return false; |
| 397 |
if (Result) |
| 398 |
*Result = Loc->second->getLiteral(Index); |
| 399 |
return true; |
| 400 |
} |
| 401 |
|
| 402 |
bool SPIRVEntry::hasDecorateId(Decoration Kind, size_t Index, |
| 403 |
SPIRVId *Result) const { |
| 404 |
auto Loc = DecorateIds.find(Kind); |
| 405 |
if (Loc == DecorateIds.end()) |
| 406 |
return false; |
| 407 |
if (Result) |
| 408 |
*Result = Loc->second->getLiteral(Index); |
| 409 |
return true; |
| 410 |
} |
| 411 |
|
| 412 |
// Check if an entry member has Kind of decoration and get the literal of the |
| 413 |
// first decoration of such kind at Index. |
| 414 |
bool SPIRVEntry::hasMemberDecorate(Decoration Kind, size_t Index, |
| 415 |
SPIRVWord MemberNumber, |
| 416 |
SPIRVWord *Result) const { |
| 417 |
auto Loc = MemberDecorates.find({MemberNumber, Kind}); |
| 418 |
if (Loc == MemberDecorates.end()) |
| 419 |
return false; |
| 420 |
if (Result) |
| 421 |
*Result = Loc->second->getLiteral(Index); |
| 422 |
return true; |
| 423 |
} |
| 424 |
|
| 425 |
std::vector<std::string> |
| 426 |
SPIRVEntry::getDecorationStringLiteral(Decoration Kind) const { |
| 427 |
auto Loc = Decorates.find(Kind); |
| 428 |
if (Loc == Decorates.end()) |
| 429 |
return {}; |
| 430 |
|
| 431 |
return getVecString(Loc->second->getVecLiteral()); |
| 432 |
} |
| 433 |
|
| 434 |
std::vector<std::string> |
| 435 |
SPIRVEntry::getMemberDecorationStringLiteral(Decoration Kind, |
| 436 |
SPIRVWord MemberNumber) const { |
| 437 |
auto Loc = MemberDecorates.find({MemberNumber, Kind}); |
| 438 |
if (Loc == MemberDecorates.end()) |
| 439 |
return {}; |
| 440 |
|
| 441 |
return getVecString(Loc->second->getVecLiteral()); |
| 442 |
} |
| 443 |
|
| 444 |
std::vector<std::vector<std::string>> |
| 445 |
SPIRVEntry::getAllDecorationStringLiterals(Decoration Kind) const { |
| 446 |
auto Loc = Decorates.find(Kind); |
| 447 |
if (Loc == Decorates.end()) |
| 448 |
return {}; |
| 449 |
|
| 450 |
std::vector<std::vector<std::string>> Literals; |
| 451 |
auto It = Decorates.equal_range(Kind); |
| 452 |
for (auto Itr = It.first; Itr != It.second; ++Itr) |
| 453 |
Literals.push_back(getVecString(Itr->second->getVecLiteral())); |
| 454 |
return Literals; |
| 455 |
} |
| 456 |
|
| 457 |
std::vector<std::vector<std::string>> |
| 458 |
SPIRVEntry::getAllMemberDecorationStringLiterals(Decoration Kind, |
| 459 |
SPIRVWord MemberNumber) const { |
| 460 |
auto Loc = MemberDecorates.find({MemberNumber, Kind}); |
| 461 |
if (Loc == MemberDecorates.end()) |
| 462 |
return {}; |
| 463 |
|
| 464 |
std::vector<std::vector<std::string>> Literals; |
| 465 |
auto It = MemberDecorates.equal_range({MemberNumber, Kind}); |
| 466 |
for (auto Itr = It.first; Itr != It.second; ++Itr) |
| 467 |
Literals.push_back(getVecString(Itr->second->getVecLiteral())); |
| 468 |
return Literals; |
| 469 |
} |
| 470 |
|
| 471 |
std::vector<SPIRVWord> |
| 472 |
SPIRVEntry::getDecorationLiterals(Decoration Kind) const { |
| 473 |
auto Loc = Decorates.find(Kind); |
| 474 |
if (Loc == Decorates.end()) |
| 475 |
return {}; |
| 476 |
|
| 477 |
return (Loc->second->getVecLiteral()); |
| 478 |
} |
| 479 |
|
| 480 |
std::vector<SPIRVId> |
| 481 |
SPIRVEntry::getDecorationIdLiterals(Decoration Kind) const { |
| 482 |
auto Loc = DecorateIds.find(Kind); |
| 483 |
if (Loc == DecorateIds.end()) |
| 484 |
return {}; |
| 485 |
|
| 486 |
return (Loc->second->getVecLiteral()); |
| 487 |
} |
| 488 |
|
| 489 |
std::vector<SPIRVWord> |
| 490 |
SPIRVEntry::getMemberDecorationLiterals(Decoration Kind, |
| 491 |
SPIRVWord MemberNumber) const { |
| 492 |
auto Loc = MemberDecorates.find({MemberNumber, Kind}); |
| 493 |
if (Loc == MemberDecorates.end()) |
| 494 |
return {}; |
| 495 |
|
| 496 |
return (Loc->second->getVecLiteral()); |
| 497 |
} |
| 498 |
|
| 499 |
// Get literals of all decorations of Kind at Index. |
| 500 |
std::set<SPIRVWord> SPIRVEntry::getDecorate(Decoration Kind, |
| 501 |
size_t Index) const { |
| 502 |
auto Range = Decorates.equal_range(Kind); |
| 503 |
std::set<SPIRVWord> Value; |
| 504 |
for (auto I = Range.first, E = Range.second; I != E; ++I) { |
| 505 |
assert(Index < I->second->getLiteralCount() && "Invalid index"); |
| 506 |
Value.insert(I->second->getLiteral(Index)); |
| 507 |
} |
| 508 |
return Value; |
| 509 |
} |
| 510 |
|
| 511 |
std::vector<SPIRVDecorate const *> |
| 512 |
SPIRVEntry::getDecorations(Decoration Kind) const { |
| 513 |
auto Range = Decorates.equal_range(Kind); |
| 514 |
std::vector<SPIRVDecorate const *> Decors; |
| 515 |
Decors.reserve(Decorates.count(Kind)); |
| 516 |
for (auto I = Range.first, E = Range.second; I != E; ++I) { |
| 517 |
Decors.push_back(I->second); |
| 518 |
} |
| 519 |
return Decors; |
| 520 |
} |
| 521 |
|
| 522 |
std::vector<SPIRVDecorate const *> SPIRVEntry::getDecorations() const { |
| 523 |
std::vector<SPIRVDecorate const *> Decors; |
| 524 |
Decors.reserve(Decorates.size()); |
| 525 |
for (auto &DecoPair : Decorates) |
| 526 |
Decors.push_back(DecoPair.second); |
| 527 |
return Decors; |
| 528 |
} |
| 529 |
|
| 530 |
std::set<SPIRVId> SPIRVEntry::getDecorateId(Decoration Kind, |
| 531 |
size_t Index) const { |
| 532 |
auto Range = DecorateIds.equal_range(Kind); |
| 533 |
std::set<SPIRVId> Value; |
| 534 |
for (auto I = Range.first, E = Range.second; I != E; ++I) { |
| 535 |
assert(Index < I->second->getLiteralCount() && "Invalid index"); |
| 536 |
Value.insert(I->second->getLiteral(Index)); |
| 537 |
} |
| 538 |
return Value; |
| 539 |
} |
| 540 |
|
| 541 |
std::vector<SPIRVDecorateId const *> |
| 542 |
SPIRVEntry::getDecorationIds(Decoration Kind) const { |
| 543 |
auto Range = DecorateIds.equal_range(Kind); |
| 544 |
std::vector<SPIRVDecorateId const *> Decors; |
| 545 |
Decors.reserve(DecorateIds.count(Kind)); |
| 546 |
for (auto I = Range.first, E = Range.second; I != E; ++I) { |
| 547 |
Decors.push_back(I->second); |
| 548 |
} |
| 549 |
return Decors; |
| 550 |
} |
| 551 |
|
| 552 |
bool SPIRVEntry::hasLinkageType() const { |
| 553 |
return OpCode == OpFunction || OpCode == OpVariable; |
| 554 |
} |
| 555 |
|
| 556 |
bool SPIRVEntry::isExtInst(const SPIRVExtInstSetKind InstSet) const { |
| 557 |
if (isExtInst()) { |
| 558 |
const SPIRVExtInst *EI = static_cast<const SPIRVExtInst *>(this); |
| 559 |
return EI->getExtSetKind() == InstSet; |
| 560 |
} |
| 561 |
return false; |
| 562 |
} |
| 563 |
|
| 564 |
bool SPIRVEntry::isExtInst(const SPIRVExtInstSetKind InstSet, |
| 565 |
const SPIRVWord ExtOp) const { |
| 566 |
if (isExtInst()) { |
| 567 |
const SPIRVExtInst *EI = static_cast<const SPIRVExtInst *>(this); |
| 568 |
if (EI->getExtSetKind() == InstSet) { |
| 569 |
return EI->getExtOp() == ExtOp; |
| 570 |
} |
| 571 |
} |
| 572 |
return false; |
| 573 |
} |
| 574 |
|
| 575 |
void SPIRVEntry::encodeDecorate(spv_ostream &O) const { |
| 576 |
for (auto &I : Decorates) |
| 577 |
O << *I.second; |
| 578 |
for (auto &I : DecorateIds) |
| 579 |
O << *I.second; |
| 580 |
} |
| 581 |
|
| 582 |
SPIRVLinkageTypeKind SPIRVEntry::getLinkageType() const { |
| 583 |
assert(hasLinkageType()); |
| 584 |
DecorateMapType::const_iterator Loc = |
| 585 |
Decorates.find(DecorationLinkageAttributes); |
| 586 |
if (Loc == Decorates.end()) |
| 587 |
return internal::LinkageTypeInternal; |
| 588 |
return static_cast<const SPIRVDecorateLinkageAttr *>(Loc->second) |
| 589 |
->getLinkageType(); |
| 590 |
} |
| 591 |
|
| 592 |
void SPIRVEntry::setLinkageType(SPIRVLinkageTypeKind LT) { |
| 593 |
assert(isValid(LT)); |
| 594 |
assert(hasLinkageType()); |
| 595 |
addDecorate(new SPIRVDecorateLinkageAttr(this, Name, LT)); |
| 596 |
} |
| 597 |
|
| 598 |
void SPIRVEntry::updateModuleVersion() const { |
| 599 |
if (!Module) |
| 600 |
return; |
| 601 |
|
| 602 |
Module->setMinSPIRVVersion( |
| 603 |
static_cast<VersionNumber>(getRequiredSPIRVVersion())); |
| 604 |
} |
| 605 |
|
| 606 |
spv_ostream &operator<<(spv_ostream &O, const SPIRVEntry &E) { |
| 607 |
E.validate(); |
| 608 |
E.encodeAll(O); |
| 609 |
O << SPIRVNL(); |
| 610 |
return O; |
| 611 |
} |
| 612 |
|
| 613 |
std::istream &operator>>(std::istream &I, SPIRVEntry &E) { |
| 614 |
E.decode(I); |
| 615 |
return I; |
| 616 |
} |
| 617 |
|
| 618 |
SPIRVEntryPoint::SPIRVEntryPoint(SPIRVModule *TheModule, |
| 619 |
SPIRVExecutionModelKind TheExecModel, |
| 620 |
SPIRVId TheId, const std::string &TheName, |
| 621 |
std::vector<SPIRVId> Variables) |
| 622 |
: SPIRVAnnotation(OpEntryPoint, TheModule->get<SPIRVFunction>(TheId), |
| 623 |
getSizeInWords(TheName) + Variables.size() + 3), |
| 624 |
ExecModel(TheExecModel), Name(TheName), Variables(Variables) {} |
| 625 |
|
| 626 |
void SPIRVEntryPoint::encode(spv_ostream &O) const { |
| 627 |
getEncoder(O) << ExecModel << Target << Name << Variables; |
| 628 |
} |
| 629 |
|
| 630 |
void SPIRVEntryPoint::decode(std::istream &I) { |
| 631 |
getDecoder(I) >> ExecModel >> Target >> Name; |
| 632 |
Variables.resize(WordCount - FixedWC - getSizeInWords(Name) + 1); |
| 633 |
getDecoder(I) >> Variables; |
| 634 |
Module->setName(getOrCreateTarget(), Name); |
| 635 |
Module->addEntryPoint(ExecModel, Target, Name, Variables); |
| 636 |
} |
| 637 |
|
| 638 |
void SPIRVExecutionMode::encode(spv_ostream &O) const { |
| 639 |
getEncoder(O) << Target << ExecMode << WordLiterals; |
| 640 |
} |
| 641 |
|
| 642 |
void SPIRVExecutionMode::decode(std::istream &I) { |
| 643 |
getDecoder(I) >> Target >> ExecMode; |
| 644 |
switch (static_cast<uint32_t>(ExecMode)) { |
| 645 |
case ExecutionModeLocalSize: |
| 646 |
case ExecutionModeLocalSizeId: |
| 647 |
case ExecutionModeLocalSizeHint: |
| 648 |
case ExecutionModeLocalSizeHintId: |
| 649 |
case ExecutionModeMaxWorkgroupSizeINTEL: |
| 650 |
WordLiterals.resize(3); |
| 651 |
break; |
| 652 |
case ExecutionModeInvocations: |
| 653 |
case ExecutionModeOutputVertices: |
| 654 |
case ExecutionModeVecTypeHint: |
| 655 |
case ExecutionModeDenormPreserve: |
| 656 |
case ExecutionModeDenormFlushToZero: |
| 657 |
case ExecutionModeSignedZeroInfNanPreserve: |
| 658 |
case ExecutionModeRoundingModeRTE: |
| 659 |
case ExecutionModeRoundingModeRTZ: |
| 660 |
case ExecutionModeRoundingModeRTPINTEL: |
| 661 |
case ExecutionModeRoundingModeRTNINTEL: |
| 662 |
case ExecutionModeFloatingPointModeALTINTEL: |
| 663 |
case ExecutionModeFloatingPointModeIEEEINTEL: |
| 664 |
case ExecutionModeSharedLocalMemorySizeINTEL: |
| 665 |
case ExecutionModeNamedBarrierCountINTEL: |
| 666 |
case ExecutionModeSubgroupSize: |
| 667 |
case ExecutionModeSubgroupsPerWorkgroup: |
| 668 |
case ExecutionModeSubgroupsPerWorkgroupId: |
| 669 |
case ExecutionModeMaxWorkDimINTEL: |
| 670 |
case ExecutionModeNumSIMDWorkitemsINTEL: |
| 671 |
case ExecutionModeSchedulerTargetFmaxMhzINTEL: |
| 672 |
case ExecutionModeStreamingInterfaceINTEL: |
| 673 |
case ExecutionModeMaximumRegistersINTEL: |
| 674 |
case ExecutionModeMaximumRegistersIdINTEL: |
| 675 |
case ExecutionModeNamedMaximumRegistersINTEL: |
| 676 |
WordLiterals.resize(1); |
| 677 |
break; |
| 678 |
default: |
| 679 |
// Do nothing. Keep this to avoid VS2013 warning. |
| 680 |
break; |
| 681 |
} |
| 682 |
getDecoder(I) >> WordLiterals; |
| 683 |
getOrCreateTarget()->addExecutionMode(Module->add(this)); |
| 684 |
} |
| 685 |
|
| 686 |
SPIRVForward *SPIRVAnnotationGeneric::getOrCreateTarget() const { |
| 687 |
SPIRVEntry *Entry = nullptr; |
| 688 |
bool Found = Module->exist(Target, &Entry); |
| 689 |
assert((!Found || Entry->getOpCode() == internal::OpForward) && |
| 690 |
"Annotations only allowed on forward"); |
| 691 |
if (!Found) |
| 692 |
Entry = Module->addForward(Target, nullptr); |
| 693 |
return static_cast<SPIRVForward *>(Entry); |
| 694 |
} |
| 695 |
|
| 696 |
SPIRVName::SPIRVName(const SPIRVEntry *TheTarget, const std::string &TheStr) |
| 697 |
: SPIRVAnnotation(OpName, TheTarget, getSizeInWords(TheStr) + 2), |
| 698 |
Str(TheStr) {} |
| 699 |
|
| 700 |
void SPIRVName::encode(spv_ostream &O) const { getEncoder(O) << Target << Str; } |
| 701 |
|
| 702 |
void SPIRVName::decode(std::istream &I) { |
| 703 |
getDecoder(I) >> Target >> Str; |
| 704 |
Module->setName(getOrCreateTarget(), Str); |
| 705 |
} |
| 706 |
|
| 707 |
void SPIRVName::validate() const { |
| 708 |
assert(WordCount == getSizeInWords(Str) + 2 && "Incorrect word count"); |
| 709 |
} |
| 710 |
|
| 711 |
_SPIRV_IMP_ENCDEC2(SPIRVString, Id, Str) |
| 712 |
_SPIRV_IMP_ENCDEC3(SPIRVMemberName, Target, MemberNumber, Str) |
| 713 |
|
| 714 |
void SPIRVLine::encode(spv_ostream &O) const { |
| 715 |
getEncoder(O) << FileName << Line << Column; |
| 716 |
} |
| 717 |
|
| 718 |
void SPIRVLine::decode(std::istream &I) { |
| 719 |
getDecoder(I) >> FileName >> Line >> Column; |
| 720 |
} |
| 721 |
|
| 722 |
void SPIRVLine::validate() const { |
| 723 |
assert(OpCode == OpLine); |
| 724 |
assert(WordCount == 4); |
| 725 |
assert(get<SPIRVEntry>(FileName)->getOpCode() == OpString); |
| 726 |
assert(Line != SPIRVWORD_MAX); |
| 727 |
assert(Column != SPIRVWORD_MAX); |
| 728 |
assert(!hasId()); |
| 729 |
} |
| 730 |
|
| 731 |
void SPIRVMemberName::validate() const { |
| 732 |
assert(OpCode == OpMemberName); |
| 733 |
assert(WordCount == getSizeInWords(Str) + FixedWC); |
| 734 |
assert(get<SPIRVEntry>(Target)->getOpCode() == OpTypeStruct); |
| 735 |
assert(MemberNumber < get<SPIRVTypeStruct>(Target)->getStructMemberCount()); |
| 736 |
} |
| 737 |
|
| 738 |
SPIRVExtInstImport::SPIRVExtInstImport(SPIRVModule *TheModule, SPIRVId TheId, |
| 739 |
const std::string &TheStr) |
| 740 |
: SPIRVEntry(TheModule, 2 + getSizeInWords(TheStr), OC, TheId), |
| 741 |
Str(TheStr) { |
| 742 |
validate(); |
| 743 |
} |
| 744 |
|
| 745 |
void SPIRVExtInstImport::encode(spv_ostream &O) const { |
| 746 |
getEncoder(O) << Id << Str; |
| 747 |
} |
| 748 |
|
| 749 |
void SPIRVExtInstImport::decode(std::istream &I) { |
| 750 |
getDecoder(I) >> Id >> Str; |
| 751 |
Module->importBuiltinSetWithId(Str, Id); |
| 752 |
} |
| 753 |
|
| 754 |
void SPIRVExtInstImport::validate() const { |
| 755 |
SPIRVEntry::validate(); |
| 756 |
assert(!Str.empty() && "Invalid builtin set"); |
| 757 |
} |
| 758 |
|
| 759 |
void SPIRVMemoryModel::encode(spv_ostream &O) const { |
| 760 |
getEncoder(O) << Module->getAddressingModel() << Module->getMemoryModel(); |
| 761 |
} |
| 762 |
|
| 763 |
void SPIRVMemoryModel::decode(std::istream &I) { |
| 764 |
SPIRVAddressingModelKind AddrModel; |
| 765 |
SPIRVMemoryModelKind MemModel; |
| 766 |
getDecoder(I) >> AddrModel >> MemModel; |
| 767 |
Module->setAddressingModel(AddrModel); |
| 768 |
Module->setMemoryModel(MemModel); |
| 769 |
} |
| 770 |
|
| 771 |
void SPIRVMemoryModel::validate() const { |
| 772 |
auto AM = Module->getAddressingModel(); |
| 773 |
auto MM = Module->getMemoryModel(); |
| 774 |
SPIRVCK(isValid(AM), InvalidAddressingModel, |
| 775 |
"Actual is " + std::to_string(AM)); |
| 776 |
SPIRVCK(isValid(MM), InvalidMemoryModel, "Actual is " + std::to_string(MM)); |
| 777 |
} |
| 778 |
|
| 779 |
void SPIRVSource::encode(spv_ostream &O) const { |
| 780 |
SPIRVWord Ver = SPIRVWORD_MAX; |
| 781 |
auto Language = Module->getSourceLanguage(&Ver); |
| 782 |
getEncoder(O) << Language << Ver; |
| 783 |
} |
| 784 |
|
| 785 |
void SPIRVSource::decode(std::istream &I) { |
| 786 |
SourceLanguage Lang = SourceLanguageUnknown; |
| 787 |
SPIRVWord Ver = SPIRVWORD_MAX; |
| 788 |
getDecoder(I) >> Lang >> Ver; |
| 789 |
Module->setSourceLanguage(Lang, Ver); |
| 790 |
} |
| 791 |
|
| 792 |
SPIRVSourceExtension::SPIRVSourceExtension(SPIRVModule *M, |
| 793 |
const std::string &SS) |
| 794 |
: SPIRVEntryNoId(M, 1 + getSizeInWords(SS)), S(SS) {} |
| 795 |
|
| 796 |
void SPIRVSourceExtension::encode(spv_ostream &O) const { getEncoder(O) << S; } |
| 797 |
|
| 798 |
void SPIRVSourceExtension::decode(std::istream &I) { |
| 799 |
getDecoder(I) >> S; |
| 800 |
Module->getSourceExtension().insert(S); |
| 801 |
} |
| 802 |
|
| 803 |
SPIRVExtension::SPIRVExtension(SPIRVModule *M, const std::string &SS) |
| 804 |
: SPIRVEntryNoId(M, 1 + getSizeInWords(SS)), S(SS) {} |
| 805 |
|
| 806 |
void SPIRVExtension::encode(spv_ostream &O) const { getEncoder(O) << S; } |
| 807 |
|
| 808 |
void SPIRVExtension::decode(std::istream &I) { |
| 809 |
getDecoder(I) >> S; |
| 810 |
Module->getExtension().insert(S); |
| 811 |
} |
| 812 |
|
| 813 |
SPIRVCapability::SPIRVCapability(SPIRVModule *M, SPIRVCapabilityKind K) |
| 814 |
: SPIRVEntryNoId(M, 2), Kind(K) { |
| 815 |
updateModuleVersion(); |
| 816 |
} |
| 817 |
|
| 818 |
void SPIRVCapability::encode(spv_ostream &O) const { getEncoder(O) << Kind; } |
| 819 |
|
| 820 |
void SPIRVCapability::decode(std::istream &I) { |
| 821 |
getDecoder(I) >> Kind; |
| 822 |
Module->addCapability(Kind); |
| 823 |
} |
| 824 |
|
| 825 |
template <spv::Op OC> void SPIRVContinuedInstINTELBase<OC>::validate() const { |
| 826 |
SPIRVEntry::validate(); |
| 827 |
} |
| 828 |
|
| 829 |
template <spv::Op OC> |
| 830 |
void SPIRVContinuedInstINTELBase<OC>::encode(spv_ostream &O) const { |
| 831 |
SPIRVEntry::getEncoder(O) << (Elements); |
| 832 |
} |
| 833 |
template <spv::Op OC> |
| 834 |
void SPIRVContinuedInstINTELBase<OC>::decode(std::istream &I) { |
| 835 |
SPIRVEntry::getDecoder(I) >> (Elements); |
| 836 |
} |
| 837 |
|
| 838 |
SPIRVType *SPIRVTypeStructContinuedINTEL::getMemberType(size_t I) const { |
| 839 |
return static_cast<SPIRVType *>(SPIRVEntry::getEntry(Elements[I])); |
| 840 |
} |
| 841 |
|
| 842 |
void SPIRVModuleProcessed::validate() const { |
| 843 |
assert(WordCount == FixedWC + getSizeInWords(ProcessStr) && |
| 844 |
"Incorrect word count in OpModuleProcessed"); |
| 845 |
} |
| 846 |
|
| 847 |
void SPIRVModuleProcessed::encode(spv_ostream &O) const { |
| 848 |
getEncoder(O) << ProcessStr; |
| 849 |
} |
| 850 |
|
| 851 |
void SPIRVModuleProcessed::decode(std::istream &I) { |
| 852 |
getDecoder(I) >> ProcessStr; |
| 853 |
Module->addModuleProcessed(ProcessStr); |
| 854 |
} |
| 855 |
|
| 856 |
std::string SPIRVModuleProcessed::getProcessStr() { return ProcessStr; } |
| 857 |
|
| 858 |
} // namespace SPIRV |