| 1 |
//===- SPIRVEntry.h - 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 defines the base class for SPIRV entities. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#ifndef SPIRV_LIBSPIRV_SPIRVENTRY_H |
| 41 |
#define SPIRV_LIBSPIRV_SPIRVENTRY_H |
| 42 |
|
| 43 |
#include "LLVMSPIRVOpts.h" |
| 44 |
#include "SPIRVEnum.h" |
| 45 |
#include "SPIRVError.h" |
| 46 |
#include "SPIRVIsValidEnum.h" |
| 47 |
|
| 48 |
#include <cassert> |
| 49 |
#include <iostream> |
| 50 |
#include <map> |
| 51 |
#include <memory> |
| 52 |
#include <optional> |
| 53 |
#include <set> |
| 54 |
#include <string> |
| 55 |
#include <vector> |
| 56 |
|
| 57 |
namespace SPIRV { |
| 58 |
|
| 59 |
class SPIRVModule; |
| 60 |
class SPIRVEncoder; |
| 61 |
class SPIRVDecoder; |
| 62 |
class SPIRVType; |
| 63 |
class SPIRVValue; |
| 64 |
class SPIRVDecorate; |
| 65 |
class SPIRVDecorateId; |
| 66 |
class SPIRVForward; |
| 67 |
class SPIRVMemberDecorate; |
| 68 |
class SPIRVLine; |
| 69 |
class SPIRVString; |
| 70 |
class SPIRVExtInst; |
| 71 |
|
| 72 |
// Add declaration of encode/decode functions to a class. |
| 73 |
// Used inside class definition. |
| 74 |
#define _SPIRV_DCL_ENCDEC \ |
| 75 |
void encode(spv_ostream &O) const override; \ |
| 76 |
void decode(std::istream &I) override; |
| 77 |
|
| 78 |
#define _REQ_SPIRV_VER(Version) \ |
| 79 |
SPIRVWord getRequiredSPIRVVersion() const override { return Version; } |
| 80 |
|
| 81 |
// Add implementation of encode/decode functions to a class. |
| 82 |
// Used out side of class definition. |
| 83 |
#define _SPIRV_IMP_ENCDEC0(Ty) \ |
| 84 |
void Ty::encode(spv_ostream &O) const {} \ |
| 85 |
void Ty::decode(std::istream &I) {} |
| 86 |
#define _SPIRV_IMP_ENCDEC1(Ty, x) \ |
| 87 |
void Ty::encode(spv_ostream &O) const { getEncoder(O) << (x); } \ |
| 88 |
void Ty::decode(std::istream &I) { getDecoder(I) >> (x); } |
| 89 |
#define _SPIRV_IMP_ENCDEC2(Ty, x, y) \ |
| 90 |
void Ty::encode(spv_ostream &O) const { getEncoder(O) << (x) << (y); } \ |
| 91 |
void Ty::decode(std::istream &I) { getDecoder(I) >> (x) >> (y); } |
| 92 |
#define _SPIRV_IMP_ENCDEC3(Ty, x, y, z) \ |
| 93 |
void Ty::encode(spv_ostream &O) const { \ |
| 94 |
getEncoder(O) << (x) << (y) << (z); \ |
| 95 |
} \ |
| 96 |
void Ty::decode(std::istream &I) { getDecoder(I) >> (x) >> (y) >> (z); } |
| 97 |
#define _SPIRV_IMP_ENCDEC4(Ty, x, y, z, u) \ |
| 98 |
void Ty::encode(spv_ostream &O) const { \ |
| 99 |
getEncoder(O) << (x) << (y) << (z) << (u); \ |
| 100 |
} \ |
| 101 |
void Ty::decode(std::istream &I) { \ |
| 102 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u); \ |
| 103 |
} |
| 104 |
#define _SPIRV_IMP_ENCDEC5(Ty, x, y, z, u, v) \ |
| 105 |
void Ty::encode(spv_ostream &O) const { \ |
| 106 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v); \ |
| 107 |
} \ |
| 108 |
void Ty::decode(std::istream &I) { \ |
| 109 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v); \ |
| 110 |
} |
| 111 |
#define _SPIRV_IMP_ENCDEC6(Ty, x, y, z, u, v, w) \ |
| 112 |
void Ty::encode(spv_ostream &O) const { \ |
| 113 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v) << (w); \ |
| 114 |
} \ |
| 115 |
void Ty::decode(std::istream &I) { \ |
| 116 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v) >> (w); \ |
| 117 |
} |
| 118 |
#define _SPIRV_IMP_ENCDEC7(Ty, x, y, z, u, v, w, r) \ |
| 119 |
void Ty::encode(spv_ostream &O) const { \ |
| 120 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v) << (w) << (r); \ |
| 121 |
} \ |
| 122 |
void Ty::decode(std::istream &I) { \ |
| 123 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v) >> (w) >> (r); \ |
| 124 |
} |
| 125 |
#define _SPIRV_IMP_ENCDEC8(Ty, x, y, z, u, v, w, r, s) \ |
| 126 |
void Ty::encode(spv_ostream &O) const { \ |
| 127 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v) << (w) << (r) << (s); \ |
| 128 |
} \ |
| 129 |
void Ty::decode(std::istream &I) { \ |
| 130 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v) >> (w) >> (r) >> (s); \ |
| 131 |
} |
| 132 |
#define _SPIRV_IMP_ENCDEC9(Ty, x, y, z, u, v, w, r, s, t) \ |
| 133 |
void Ty::encode(spv_ostream &O) const { \ |
| 134 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v) << (w) << (r) << (s) \ |
| 135 |
<< (t); \ |
| 136 |
} \ |
| 137 |
void Ty::decode(std::istream &I) { \ |
| 138 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v) >> (w) >> (r) >> (s) >> \ |
| 139 |
(t); \ |
| 140 |
} |
| 141 |
|
| 142 |
// Add definition of encode/decode functions to a class. |
| 143 |
// Used inside class definition. |
| 144 |
#define _SPIRV_DEF_ENCDEC0 \ |
| 145 |
void encode(spv_ostream &O) const override {} \ |
| 146 |
void decode(std::istream &I) override {} |
| 147 |
#define _SPIRV_DEF_ENCDEC1(x) \ |
| 148 |
void encode(spv_ostream &O) const override { getEncoder(O) << (x); } \ |
| 149 |
void decode(std::istream &I) override { getDecoder(I) >> (x); } |
| 150 |
#define _SPIRV_DEF_ENCDEC2(x, y) \ |
| 151 |
void encode(spv_ostream &O) const override { getEncoder(O) << (x) << (y); } \ |
| 152 |
void decode(std::istream &I) override { getDecoder(I) >> (x) >> (y); } |
| 153 |
#define _SPIRV_DEF_ENCDEC3(x, y, z) \ |
| 154 |
void encode(spv_ostream &O) const override { \ |
| 155 |
getEncoder(O) << (x) << (y) << (z); \ |
| 156 |
} \ |
| 157 |
void decode(std::istream &I) override { getDecoder(I) >> (x) >> (y) >> (z); } |
| 158 |
#define _SPIRV_DEF_ENCDEC4(x, y, z, u) \ |
| 159 |
void encode(spv_ostream &O) const override { \ |
| 160 |
getEncoder(O) << (x) << (y) << (z) << (u); \ |
| 161 |
} \ |
| 162 |
void decode(std::istream &I) override { \ |
| 163 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u); \ |
| 164 |
} |
| 165 |
#define _SPIRV_DEF_ENCDEC5(x, y, z, u, v) \ |
| 166 |
void encode(spv_ostream &O) const override { \ |
| 167 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v); \ |
| 168 |
} \ |
| 169 |
void decode(std::istream &I) override { \ |
| 170 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v); \ |
| 171 |
} |
| 172 |
#define _SPIRV_DEF_ENCDEC6(x, y, z, u, v, w) \ |
| 173 |
void encode(spv_ostream &O) const override { \ |
| 174 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v) << (w); \ |
| 175 |
} \ |
| 176 |
void decode(std::istream &I) override { \ |
| 177 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v) >> (w); \ |
| 178 |
} |
| 179 |
#define _SPIRV_DEF_ENCDEC7(x, y, z, u, v, w, r) \ |
| 180 |
void encode(spv_ostream &O) const override { \ |
| 181 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v) << (w) << (r); \ |
| 182 |
} \ |
| 183 |
void decode(std::istream &I) override { \ |
| 184 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v) >> (w) >> (r); \ |
| 185 |
} |
| 186 |
#define _SPIRV_DEF_ENCDEC8(x, y, z, u, v, w, r, s) \ |
| 187 |
void encode(spv_ostream &O) const override { \ |
| 188 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v) << (w) << (r) << (s); \ |
| 189 |
} \ |
| 190 |
void decode(std::istream &I) override { \ |
| 191 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v) >> (w) >> (r) >> (s); \ |
| 192 |
} |
| 193 |
#define _SPIRV_DEF_ENCDEC9(x, y, z, u, v, w, r, s, t) \ |
| 194 |
void encode(spv_ostream &O) const override { \ |
| 195 |
getEncoder(O) << (x) << (y) << (z) << (u) << (v) << (w) << (r) << (s) \ |
| 196 |
<< (t); \ |
| 197 |
} \ |
| 198 |
void decode(std::istream &I) override { \ |
| 199 |
getDecoder(I) >> (x) >> (y) >> (z) >> (u) >> (v) >> (w) >> (r) >> (s) >> \ |
| 200 |
(t); \ |
| 201 |
} |
| 202 |
|
| 203 |
/// All SPIR-V in-memory-representation entities inherits from SPIRVEntry. |
| 204 |
/// Usually there are two flavors of constructors of SPIRV objects: |
| 205 |
/// |
| 206 |
/// 1. complete constructor: It requires all the parameters needed to create a |
| 207 |
/// SPIRV entity with complete information which can be validated. It is |
| 208 |
/// usually used by LLVM/SPIR-V translator to create SPIRV object |
| 209 |
/// corresponding to LLVM object. Such constructor calls validate() at |
| 210 |
/// the end of the construction. |
| 211 |
/// |
| 212 |
/// 2. incomplete constructor: For leaf classes, it has no parameters. |
| 213 |
/// It is usually called by SPIRVEntry::make(opcode) to create an incomplete |
| 214 |
/// object which should not be validated. Then setWordCount(count) is |
| 215 |
/// called to fix the size of the object if it is variable, and then the |
| 216 |
/// information is filled by the virtual function decode(istream). |
| 217 |
/// After that the object can be validated. |
| 218 |
/// |
| 219 |
/// To add a new SPIRV class: |
| 220 |
/// |
| 221 |
/// 1. It is recommended to name the class as SPIRVXXX if it has a fixed op code |
| 222 |
/// OpXXX. Although it is not mandatory, doing this facilitates adding it to |
| 223 |
/// the table of the factory function SPIRVEntry::create(). |
| 224 |
/// 2. Inherit from proper SPIRV class such as SPIRVType, SPIRVValue, |
| 225 |
/// SPIRVInstruction, etc. |
| 226 |
/// 3. Implement virtual function encode(), decode(), validate(). |
| 227 |
/// 4. If the object has variable size, implement virtual function |
| 228 |
/// setWordCount(). |
| 229 |
/// 5. If the class has special attributes, e.g. having no id, or having no |
| 230 |
/// type as a value, set them in the constructors. |
| 231 |
/// 6. If the class may represent SPIRV entity which has been added in version |
| 232 |
/// later than 1.0, implement virtual function getRequiredSPIRVVersion(). |
| 233 |
/// To automaticly update module's version you can also call protected |
| 234 |
/// function updateModuleVersion() in the constructor. |
| 235 |
/// 7. Add the class to the Table of SPIRVEntry::create(). |
| 236 |
/// 8. Add the class to SPIRVToLLVM and LLVMToSPIRV. |
| 237 |
|
| 238 |
class SPIRVEntry { |
| 239 |
public: |
| 240 |
enum SPIRVEntryAttrib { |
| 241 |
SPIRVEA_DEFAULT = 0, |
| 242 |
SPIRVEA_NOID = 1, // Entry has no valid id |
| 243 |
SPIRVEA_NOTYPE = 2, // Value has no type |
| 244 |
}; |
| 245 |
|
| 246 |
// Complete constructor for objects with id |
| 247 |
SPIRVEntry(SPIRVModule *M, unsigned TheWordCount, Op TheOpCode, SPIRVId TheId) |
| 248 |
: Module(M), OpCode(TheOpCode), Id(TheId), Attrib(SPIRVEA_DEFAULT), |
| 249 |
WordCount(TheWordCount), Line(nullptr) { |
| 250 |
SPIRVEntry::validate(); |
| 251 |
} |
| 252 |
|
| 253 |
// Complete constructor for objects without id |
| 254 |
SPIRVEntry(SPIRVModule *M, unsigned TheWordCount, Op TheOpCode) |
| 255 |
: Module(M), OpCode(TheOpCode), Id(SPIRVID_INVALID), Attrib(SPIRVEA_NOID), |
| 256 |
WordCount(TheWordCount), Line(nullptr) { |
| 257 |
SPIRVEntry::validate(); |
| 258 |
} |
| 259 |
|
| 260 |
// Incomplete constructor |
| 261 |
SPIRVEntry(Op TheOpCode) |
| 262 |
: Module(NULL), OpCode(TheOpCode), Id(SPIRVID_INVALID), |
| 263 |
Attrib(SPIRVEA_DEFAULT), WordCount(0), Line(nullptr) {} |
| 264 |
|
| 265 |
SPIRVEntry() |
| 266 |
: Module(NULL), OpCode(OpNop), Id(SPIRVID_INVALID), |
| 267 |
Attrib(SPIRVEA_DEFAULT), WordCount(0), Line(nullptr) {} |
| 268 |
|
| 269 |
virtual ~SPIRVEntry() {} |
| 270 |
|
| 271 |
bool exist(SPIRVId) const; |
| 272 |
template <class T> T *get(SPIRVId TheId) const { |
| 273 |
return static_cast<T *>(getEntry(TheId)); |
| 274 |
} |
| 275 |
SPIRVEntry *getEntry(SPIRVId) const; |
| 276 |
SPIRVEntry *getOrCreate(SPIRVId TheId) const; |
| 277 |
SPIRVValue *getValue(SPIRVId TheId) const; |
| 278 |
std::vector<SPIRVValue *> getValues(const std::vector<SPIRVId> &) const; |
| 279 |
std::vector<SPIRVId> getIds(const std::vector<SPIRVValue *>) const; |
| 280 |
SPIRVType *getValueType(SPIRVId TheId) const; |
| 281 |
std::vector<SPIRVType *> getValueTypes(const std::vector<SPIRVId> &) const; |
| 282 |
|
| 283 |
virtual SPIRVDecoder getDecoder(std::istream &); |
| 284 |
virtual SPIRVEncoder getEncoder(spv_ostream &) const; |
| 285 |
SPIRVErrorLog &getErrorLog() const; |
| 286 |
SPIRVId getId() const { |
| 287 |
assert(hasId()); |
| 288 |
return Id; |
| 289 |
} |
| 290 |
std::shared_ptr<const SPIRVLine> getLine() const { return Line; } |
| 291 |
std::shared_ptr<const SPIRVExtInst> getDebugLine() const { return DebugLine; } |
| 292 |
SPIRVLinkageTypeKind getLinkageType() const; |
| 293 |
Op getOpCode() const { return OpCode; } |
| 294 |
SPIRVModule *getModule() const { return Module; } |
| 295 |
virtual SPIRVCapVec getRequiredCapability() const { return SPIRVCapVec(); } |
| 296 |
virtual std::optional<ExtensionID> getRequiredExtension() const { return {}; } |
| 297 |
const std::string &getName() const { return Name; } |
| 298 |
size_t getNumDecorations() const { return Decorates.size(); } |
| 299 |
bool hasDecorate(Decoration Kind, size_t Index = 0, |
| 300 |
SPIRVWord *Result = 0) const; |
| 301 |
bool hasDecorateId(Decoration Kind, size_t Index = 0, |
| 302 |
SPIRVId *Result = 0) const; |
| 303 |
bool hasMemberDecorate(Decoration Kind, size_t Index = 0, |
| 304 |
SPIRVWord MemberNumber = 0, |
| 305 |
SPIRVWord *Result = 0) const; |
| 306 |
std::vector<SPIRVWord> getDecorationLiterals(Decoration Kind) const; |
| 307 |
std::vector<SPIRVId> getDecorationIdLiterals(Decoration Kind) const; |
| 308 |
std::vector<SPIRVWord> |
| 309 |
getMemberDecorationLiterals(Decoration Kind, SPIRVWord MemberNumber) const; |
| 310 |
std::vector<std::string> getDecorationStringLiteral(Decoration Kind) const; |
| 311 |
std::vector<std::vector<std::string>> |
| 312 |
getAllDecorationStringLiterals(Decoration Kind) const; |
| 313 |
std::vector<std::vector<std::string>> |
| 314 |
getAllMemberDecorationStringLiterals(Decoration Kind, |
| 315 |
SPIRVWord MemberNumber) const; |
| 316 |
std::vector<std::string> |
| 317 |
getMemberDecorationStringLiteral(Decoration Kind, |
| 318 |
SPIRVWord MemberNumber) const; |
| 319 |
std::set<SPIRVWord> getDecorate(Decoration Kind, size_t Index = 0) const; |
| 320 |
std::vector<SPIRVDecorate const *> getDecorations(Decoration Kind) const; |
| 321 |
std::vector<SPIRVDecorate const *> getDecorations() const; |
| 322 |
std::set<SPIRVId> getDecorateId(Decoration Kind, size_t Index = 0) const; |
| 323 |
std::vector<SPIRVDecorateId const *> getDecorationIds(Decoration Kind) const; |
| 324 |
bool hasId() const { return !(Attrib & SPIRVEA_NOID); } |
| 325 |
bool hasLine() const { return Line != nullptr; } |
| 326 |
bool hasLinkageType() const; |
| 327 |
bool isAtomic() const { return isAtomicOpCode(OpCode); } |
| 328 |
bool isBasicBlock() const { return isLabel(); } |
| 329 |
bool isExtInst() const { return OpCode == OpExtInst; } |
| 330 |
bool isExtInst(const SPIRVExtInstSetKind InstSet) const; |
| 331 |
bool isExtInst(const SPIRVExtInstSetKind InstSet, |
| 332 |
const SPIRVWord ExtOp) const; |
| 333 |
bool isDecorate() const { return OpCode == OpDecorate; } |
| 334 |
bool isDecorateId() const { return OpCode == OpDecorateId; } |
| 335 |
bool isMemberDecorate() const { return OpCode == OpMemberDecorate; } |
| 336 |
bool isForward() const { return OpCode == internal::OpForward; } |
| 337 |
bool isLabel() const { return OpCode == OpLabel; } |
| 338 |
bool isUndef() const { return OpCode == OpUndef; } |
| 339 |
bool isControlBarrier() const { return OpCode == OpControlBarrier; } |
| 340 |
bool isMemoryBarrier() const { return OpCode == OpMemoryBarrier; } |
| 341 |
bool isVariable() const { return OpCode == OpVariable; } |
| 342 |
bool isEndOfBlock() const; |
| 343 |
virtual bool isInst() const { return false; } |
| 344 |
virtual bool isOperandLiteral(unsigned Index) const { |
| 345 |
assert(0 && "not implemented"); |
| 346 |
return false; |
| 347 |
} |
| 348 |
virtual bool isImplemented() const { return true; } |
| 349 |
|
| 350 |
void addDecorate(SPIRVDecorate *); |
| 351 |
void addDecorate(SPIRVDecorateId *); |
| 352 |
void addDecorate(Decoration Kind); |
| 353 |
void addDecorate(Decoration Kind, SPIRVWord Literal); |
| 354 |
void eraseDecorate(Decoration); |
| 355 |
void eraseDecorateId(Decoration); |
| 356 |
void addMemberDecorate(SPIRVMemberDecorate *); |
| 357 |
void addMemberDecorate(SPIRVWord MemberNumber, Decoration Kind); |
| 358 |
void addMemberDecorate(SPIRVWord MemberNumber, Decoration Kind, |
| 359 |
SPIRVWord Literal); |
| 360 |
void eraseMemberDecorate(SPIRVWord MemberNumber, Decoration Kind); |
| 361 |
void setHasNoId() { Attrib |= SPIRVEA_NOID; } |
| 362 |
void setId(SPIRVId TheId) { Id = TheId; } |
| 363 |
void setLine(const std::shared_ptr<const SPIRVLine> &L); |
| 364 |
void setDebugLine(const std::shared_ptr<const SPIRVExtInst> &DL); |
| 365 |
void setLinkageType(SPIRVLinkageTypeKind); |
| 366 |
void setModule(SPIRVModule *TheModule); |
| 367 |
void setName(const std::string &TheName); |
| 368 |
virtual void setScope(SPIRVEntry *Scope){}; |
| 369 |
void takeAnnotations(SPIRVForward *); |
| 370 |
void takeDecorates(SPIRVEntry *); |
| 371 |
void takeDecorateIds(SPIRVEntry *); |
| 372 |
void takeMemberDecorates(SPIRVEntry *); |
| 373 |
void replaceTargetIdInDecorates(SPIRVId); |
| 374 |
|
| 375 |
/// After a SPIRV entry is created during reading SPIRV binary by default |
| 376 |
/// constructor, this function is called to allow the SPIRV entry to resize |
| 377 |
/// its variable sized member before decoding the remaining words. |
| 378 |
virtual void setWordCount(SPIRVWord TheWordCount); |
| 379 |
|
| 380 |
/// Create an empty SPIRV object by op code, e.g. OpTypeInt creates |
| 381 |
/// SPIRVTypeInt. |
| 382 |
static SPIRVEntry *create(Op); |
| 383 |
static std::unique_ptr<SPIRVEntry> createUnique(Op); |
| 384 |
|
| 385 |
/// Create an empty extended instruction. |
| 386 |
static std::unique_ptr<SPIRVExtInst> createUnique(SPIRVExtInstSetKind Set, |
| 387 |
unsigned ExtOp); |
| 388 |
|
| 389 |
friend spv_ostream &operator<<(spv_ostream &O, const SPIRVEntry &E); |
| 390 |
friend std::istream &operator>>(std::istream &I, SPIRVEntry &E); |
| 391 |
virtual void encodeLine(spv_ostream &O) const; |
| 392 |
virtual void encodeDebugLine(spv_ostream &O) const; |
| 393 |
virtual void encodeAll(spv_ostream &O) const; |
| 394 |
virtual void encodeName(spv_ostream &O) const; |
| 395 |
virtual void encodeChildren(spv_ostream &O) const; |
| 396 |
virtual void encodeDecorate(spv_ostream &O) const; |
| 397 |
virtual void encodeWordCountOpCode(spv_ostream &O) const; |
| 398 |
virtual void encode(spv_ostream &O) const; |
| 399 |
virtual void decode(std::istream &I); |
| 400 |
|
| 401 |
friend class SPIRVDecoder; |
| 402 |
|
| 403 |
/// Checks the integrity of the object. |
| 404 |
virtual void validate() const { |
| 405 |
assert(Module && "Invalid module"); |
| 406 |
assert(OpCode != OpNop && "Invalid op code"); |
| 407 |
assert((!hasId() || isValidId(Id)) && "Invalid Id"); |
| 408 |
if (WordCount > 65535) { |
| 409 |
std::stringstream SS; |
| 410 |
SS << "Id: " << Id << ", OpCode: " << OpCodeNameMap::map(OpCode) |
| 411 |
<< ", Name: \"" << Name << "\"\n"; |
| 412 |
getErrorLog().checkError(false, SPIRVEC_InvalidWordCount, SS.str()); |
| 413 |
} |
| 414 |
} |
| 415 |
void validateFunctionControlMask(SPIRVWord FCtlMask) const; |
| 416 |
void validateValues(const std::vector<SPIRVId> &) const; |
| 417 |
void validateBuiltin(SPIRVWord, SPIRVWord) const; |
| 418 |
|
| 419 |
// By default assume SPIRV 1.0 as required version |
| 420 |
virtual SPIRVWord getRequiredSPIRVVersion() const { |
| 421 |
return static_cast<SPIRVWord>(VersionNumber::SPIRV_1_0); |
| 422 |
} |
| 423 |
|
| 424 |
virtual std::vector<SPIRVEntry *> getNonLiteralOperands() const { |
| 425 |
return std::vector<SPIRVEntry *>(); |
| 426 |
} |
| 427 |
|
| 428 |
protected: |
| 429 |
/// An entry may have multiple FuncParamAttr decorations. |
| 430 |
typedef std::multimap<Decoration, const SPIRVDecorate *> DecorateMapType; |
| 431 |
typedef std::multimap<Decoration, const SPIRVDecorateId *> DecorateIdMapType; |
| 432 |
typedef std::multimap<std::pair<SPIRVWord, Decoration>, |
| 433 |
const SPIRVMemberDecorate *> |
| 434 |
MemberDecorateMapType; |
| 435 |
|
| 436 |
bool canHaveMemberDecorates() const { |
| 437 |
return OpCode == OpTypeStruct || OpCode == internal::OpForward; |
| 438 |
} |
| 439 |
MemberDecorateMapType &getMemberDecorates() { |
| 440 |
assert(canHaveMemberDecorates()); |
| 441 |
return MemberDecorates; |
| 442 |
} |
| 443 |
|
| 444 |
void updateModuleVersion() const; |
| 445 |
|
| 446 |
SPIRVModule *Module; |
| 447 |
Op OpCode; |
| 448 |
SPIRVId Id; |
| 449 |
std::string Name; |
| 450 |
unsigned Attrib; |
| 451 |
SPIRVWord WordCount; |
| 452 |
|
| 453 |
DecorateMapType Decorates; |
| 454 |
DecorateIdMapType DecorateIds; |
| 455 |
MemberDecorateMapType MemberDecorates; |
| 456 |
std::shared_ptr<const SPIRVLine> Line; |
| 457 |
std::shared_ptr<const SPIRVExtInst> DebugLine; |
| 458 |
}; |
| 459 |
|
| 460 |
class SPIRVEntryNoIdGeneric : public SPIRVEntry { |
| 461 |
public: |
| 462 |
SPIRVEntryNoIdGeneric(SPIRVModule *M, unsigned TheWordCount, Op OC) |
| 463 |
: SPIRVEntry(M, TheWordCount, OC) { |
| 464 |
setAttr(); |
| 465 |
} |
| 466 |
SPIRVEntryNoIdGeneric(Op OC) : SPIRVEntry(OC) { setAttr(); } |
| 467 |
|
| 468 |
protected: |
| 469 |
void setAttr() { setHasNoId(); } |
| 470 |
}; |
| 471 |
|
| 472 |
template <Op OC> class SPIRVEntryNoId : public SPIRVEntryNoIdGeneric { |
| 473 |
public: |
| 474 |
SPIRVEntryNoId(SPIRVModule *M, unsigned TheWordCount) |
| 475 |
: SPIRVEntryNoIdGeneric(M, TheWordCount, OC) {} |
| 476 |
SPIRVEntryNoId() : SPIRVEntryNoIdGeneric(OC) {} |
| 477 |
}; |
| 478 |
|
| 479 |
template <Op TheOpCode> |
| 480 |
class SPIRVEntryOpCodeOnly : public SPIRVEntryNoId<TheOpCode> { |
| 481 |
public: |
| 482 |
SPIRVEntryOpCodeOnly() { |
| 483 |
SPIRVEntry::WordCount = 1; |
| 484 |
validate(); |
| 485 |
} |
| 486 |
|
| 487 |
protected: |
| 488 |
_SPIRV_DEF_ENCDEC0 |
| 489 |
void validate() const override { assert(isValidId(SPIRVEntry::OpCode)); } |
| 490 |
}; |
| 491 |
|
| 492 |
template <Op TheOpCode> |
| 493 |
class SPIRVEntryUnimplemented : public SPIRVEntryNoId<TheOpCode> { |
| 494 |
public: |
| 495 |
SPIRVEntryUnimplemented() { |
| 496 |
SPIRVEntry::WordCount = 1; |
| 497 |
validate(); |
| 498 |
} |
| 499 |
bool isImplemented() const override { return false; } |
| 500 |
|
| 501 |
protected: |
| 502 |
_SPIRV_DEF_ENCDEC0 |
| 503 |
void validate() const override { assert(isValidId(SPIRVEntry::OpCode)); } |
| 504 |
}; |
| 505 |
|
| 506 |
class SPIRVAnnotationGeneric : public SPIRVEntryNoIdGeneric { |
| 507 |
public: |
| 508 |
// Complete constructor |
| 509 |
SPIRVAnnotationGeneric(SPIRVModule *TheModule, unsigned TheWordCount, Op OC, |
| 510 |
SPIRVId TheTarget = SPIRVID_INVALID) |
| 511 |
: SPIRVEntryNoIdGeneric(TheModule, TheWordCount, OC), Target(TheTarget) {} |
| 512 |
// Incomplete constructor |
| 513 |
SPIRVAnnotationGeneric(Op OC) |
| 514 |
: SPIRVEntryNoIdGeneric(OC), Target(SPIRVID_INVALID) {} |
| 515 |
|
| 516 |
SPIRVId getTargetId() const { return Target; } |
| 517 |
SPIRVForward *getOrCreateTarget() const; |
| 518 |
void setTargetId(SPIRVId T) { Target = T; } |
| 519 |
|
| 520 |
protected: |
| 521 |
SPIRVId Target; |
| 522 |
}; |
| 523 |
|
| 524 |
class SPIRVAnnotation : public SPIRVAnnotationGeneric { |
| 525 |
public: |
| 526 |
// Complete constructor |
| 527 |
SPIRVAnnotation(Op OC, const SPIRVEntry *TheTarget, unsigned TheWordCount) |
| 528 |
: SPIRVAnnotationGeneric(TheTarget->getModule(), TheWordCount, OC, |
| 529 |
TheTarget->getId()) {} |
| 530 |
// Incomplete constructors |
| 531 |
SPIRVAnnotation(Op OC) : SPIRVAnnotationGeneric(OC) {} |
| 532 |
SPIRVAnnotation() : SPIRVAnnotationGeneric(OpNop) {} |
| 533 |
}; |
| 534 |
|
| 535 |
class SPIRVEntryPoint : public SPIRVAnnotation { |
| 536 |
public: |
| 537 |
static const SPIRVWord FixedWC = 4; |
| 538 |
SPIRVEntryPoint(SPIRVModule *TheModule, SPIRVExecutionModelKind, |
| 539 |
SPIRVId TheId, const std::string &TheName, |
| 540 |
std::vector<SPIRVId> Variables); |
| 541 |
SPIRVEntryPoint() : SPIRVAnnotation(OpEntryPoint) {} |
| 542 |
|
| 543 |
_SPIRV_DCL_ENCDEC |
| 544 |
protected: |
| 545 |
SPIRVExecutionModelKind ExecModel = ExecutionModelMax; |
| 546 |
std::string Name; |
| 547 |
|
| 548 |
private: |
| 549 |
std::vector<SPIRVId> Variables; |
| 550 |
}; |
| 551 |
|
| 552 |
class SPIRVName : public SPIRVAnnotation { |
| 553 |
public: |
| 554 |
// Complete constructor |
| 555 |
SPIRVName(const SPIRVEntry *TheTarget, const std::string &TheStr); |
| 556 |
// Incomplete constructor |
| 557 |
SPIRVName() : SPIRVAnnotation(OpName) {} |
| 558 |
|
| 559 |
protected: |
| 560 |
_SPIRV_DCL_ENCDEC |
| 561 |
void validate() const override; |
| 562 |
|
| 563 |
std::string Str; |
| 564 |
}; |
| 565 |
|
| 566 |
class SPIRVMemberName : public SPIRVAnnotation { |
| 567 |
public: |
| 568 |
static const SPIRVWord FixedWC = 3; |
| 569 |
// Complete constructor |
| 570 |
SPIRVMemberName(const SPIRVEntry *TheTarget, SPIRVWord TheMemberNumber, |
| 571 |
const std::string &TheStr) |
| 572 |
: SPIRVAnnotation(OpName, TheTarget, FixedWC + getSizeInWords(TheStr)), |
| 573 |
MemberNumber(TheMemberNumber), Str(TheStr) { |
| 574 |
validate(); |
| 575 |
} |
| 576 |
// Incomplete constructor |
| 577 |
SPIRVMemberName() : SPIRVAnnotation(OpName), MemberNumber(SPIRVWORD_MAX) {} |
| 578 |
|
| 579 |
protected: |
| 580 |
_SPIRV_DCL_ENCDEC |
| 581 |
void validate() const override; |
| 582 |
SPIRVWord MemberNumber; |
| 583 |
std::string Str; |
| 584 |
}; |
| 585 |
|
| 586 |
class SPIRVString : public SPIRVEntry { |
| 587 |
static const Op OC = OpString; |
| 588 |
static const SPIRVWord FixedWC = 2; |
| 589 |
|
| 590 |
public: |
| 591 |
SPIRVString(SPIRVModule *M, SPIRVId TheId, const std::string &TheStr) |
| 592 |
: SPIRVEntry(M, FixedWC + getSizeInWords(TheStr), OC, TheId), |
| 593 |
Str(TheStr) {} |
| 594 |
SPIRVString() : SPIRVEntry(OC) {} |
| 595 |
_SPIRV_DCL_ENCDEC |
| 596 |
const std::string &getStr() const { return Str; } |
| 597 |
|
| 598 |
protected: |
| 599 |
std::string Str; |
| 600 |
}; |
| 601 |
|
| 602 |
class SPIRVLine : public SPIRVEntry { |
| 603 |
public: |
| 604 |
static const SPIRVWord WC = 4; |
| 605 |
// Complete constructor |
| 606 |
SPIRVLine(SPIRVModule *M, SPIRVId TheFileName, SPIRVWord TheLine, |
| 607 |
SPIRVWord TheColumn) |
| 608 |
: SPIRVEntry(M, WC, OpLine), FileName(TheFileName), Line(TheLine), |
| 609 |
Column(TheColumn) { |
| 610 |
Attrib = SPIRVEA_NOID | SPIRVEA_NOTYPE; |
| 611 |
validate(); |
| 612 |
} |
| 613 |
// Incomplete constructor |
| 614 |
SPIRVLine() |
| 615 |
: SPIRVEntry(OpLine), FileName(SPIRVID_INVALID), Line(SPIRVWORD_MAX), |
| 616 |
Column(SPIRVWORD_MAX) { |
| 617 |
Attrib = SPIRVEA_NOID | SPIRVEA_NOTYPE; |
| 618 |
} |
| 619 |
|
| 620 |
SPIRVWord getColumn() const { return Column; } |
| 621 |
|
| 622 |
void setColumn(const SPIRVWord Column) { this->Column = Column; } |
| 623 |
|
| 624 |
SPIRVId getFileName() const { return FileName; } |
| 625 |
|
| 626 |
const std::string &getFileNameStr() const { |
| 627 |
return get<SPIRVString>(FileName)->getStr(); |
| 628 |
} |
| 629 |
|
| 630 |
void setFileName(const SPIRVId FileName) { this->FileName = FileName; } |
| 631 |
|
| 632 |
SPIRVWord getLine() const { return Line; } |
| 633 |
|
| 634 |
void setLine(const SPIRVWord Line) { this->Line = Line; } |
| 635 |
|
| 636 |
bool operator!=(const SPIRVLine &O) const { |
| 637 |
return !equals(O.FileName, O.Line, O.Column); |
| 638 |
} |
| 639 |
|
| 640 |
bool equals(const SPIRVId TheFileName, const SPIRVWord TheLine, |
| 641 |
const SPIRVWord TheColumn) const { |
| 642 |
return FileName == TheFileName && Line == TheLine && Column == TheColumn; |
| 643 |
} |
| 644 |
|
| 645 |
protected: |
| 646 |
_SPIRV_DCL_ENCDEC |
| 647 |
void validate() const override; |
| 648 |
SPIRVId FileName; |
| 649 |
SPIRVWord Line; |
| 650 |
SPIRVWord Column; |
| 651 |
}; |
| 652 |
|
| 653 |
class SPIRVExecutionMode : public SPIRVAnnotation { |
| 654 |
public: |
| 655 |
// Complete constructor for LocalSize, LocalSizeHint |
| 656 |
SPIRVExecutionMode(Op OC, SPIRVEntry *TheTarget, |
| 657 |
SPIRVExecutionModeKind TheExecMode, SPIRVWord X, |
| 658 |
SPIRVWord Y, SPIRVWord Z) |
| 659 |
: SPIRVAnnotation(OC, TheTarget, 6), ExecMode(TheExecMode) { |
| 660 |
WordLiterals.push_back(X); |
| 661 |
WordLiterals.push_back(Y); |
| 662 |
WordLiterals.push_back(Z); |
| 663 |
updateModuleVersion(); |
| 664 |
} |
| 665 |
// Complete constructor for VecTypeHint, SubgroupSize, SubgroupsPerWorkgroup |
| 666 |
SPIRVExecutionMode(Op OC, SPIRVEntry *TheTarget, |
| 667 |
SPIRVExecutionModeKind TheExecMode, SPIRVWord Code) |
| 668 |
: SPIRVAnnotation(OC, TheTarget, 4), ExecMode(TheExecMode) { |
| 669 |
WordLiterals.push_back(Code); |
| 670 |
} |
| 671 |
// Complete constructor for ContractionOff |
| 672 |
SPIRVExecutionMode(Op OC, SPIRVEntry *TheTarget, |
| 673 |
SPIRVExecutionModeKind TheExecMode) |
| 674 |
: SPIRVAnnotation(OC, TheTarget, 3), ExecMode(TheExecMode) { |
| 675 |
updateModuleVersion(); |
| 676 |
} |
| 677 |
// Incomplete constructor |
| 678 |
SPIRVExecutionMode() |
| 679 |
: SPIRVAnnotation(OpExecutionMode), ExecMode(ExecutionModeInvocations) {} |
| 680 |
SPIRVExecutionModeKind getExecutionMode() const { return ExecMode; } |
| 681 |
const std::vector<SPIRVWord> &getLiterals() const { return WordLiterals; } |
| 682 |
SPIRVCapVec getRequiredCapability() const override { |
| 683 |
return getCapability(ExecMode); |
| 684 |
} |
| 685 |
|
| 686 |
SPIRVWord getRequiredSPIRVVersion() const override { |
| 687 |
switch (ExecMode) { |
| 688 |
case ExecutionModeFinalizer: |
| 689 |
case ExecutionModeInitializer: |
| 690 |
case ExecutionModeSubgroupSize: |
| 691 |
case ExecutionModeSubgroupsPerWorkgroup: |
| 692 |
return static_cast<SPIRVWord>(VersionNumber::SPIRV_1_1); |
| 693 |
|
| 694 |
default: |
| 695 |
return static_cast<SPIRVWord>(VersionNumber::SPIRV_1_0); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
std::optional<ExtensionID> getRequiredExtension() const override { |
| 700 |
switch (static_cast<unsigned>(ExecMode)) { |
| 701 |
case ExecutionModeMaximumRegistersINTEL: |
| 702 |
case ExecutionModeMaximumRegistersIdINTEL: |
| 703 |
case ExecutionModeNamedMaximumRegistersINTEL: |
| 704 |
return ExtensionID::SPV_INTEL_maximum_registers; |
| 705 |
default: |
| 706 |
return {}; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
protected: |
| 711 |
_SPIRV_DCL_ENCDEC |
| 712 |
SPIRVExecutionModeKind ExecMode; |
| 713 |
std::vector<SPIRVWord> WordLiterals; |
| 714 |
}; |
| 715 |
|
| 716 |
class SPIRVExecutionModeId : public SPIRVExecutionMode { |
| 717 |
public: |
| 718 |
// Complete constructor for LocalSizeId, LocalSizeHintId |
| 719 |
SPIRVExecutionModeId(SPIRVEntry *TheTarget, |
| 720 |
SPIRVExecutionModeKind TheExecMode, SPIRVWord X, |
| 721 |
SPIRVWord Y, SPIRVWord Z) |
| 722 |
: SPIRVExecutionMode(OpExecutionModeId, TheTarget, TheExecMode, X, Y, Z) { |
| 723 |
updateModuleVersion(); |
| 724 |
} |
| 725 |
// Complete constructor for SubgroupsPerWorkgroupId |
| 726 |
SPIRVExecutionModeId(SPIRVEntry *TheTarget, |
| 727 |
SPIRVExecutionModeKind TheExecMode, SPIRVWord Code) |
| 728 |
: SPIRVExecutionMode(OpExecutionModeId, TheTarget, TheExecMode, Code) { |
| 729 |
updateModuleVersion(); |
| 730 |
} |
| 731 |
// Incomplete constructor |
| 732 |
SPIRVExecutionModeId() : SPIRVExecutionMode() {} |
| 733 |
SPIRVWord getRequiredSPIRVVersion() const override { |
| 734 |
return static_cast<SPIRVWord>(VersionNumber::SPIRV_1_2); |
| 735 |
} |
| 736 |
}; |
| 737 |
|
| 738 |
class SPIRVComponentExecutionModes { |
| 739 |
typedef std::multimap<SPIRVExecutionModeKind, SPIRVExecutionMode *> |
| 740 |
SPIRVExecutionModeMap; |
| 741 |
typedef std::pair<SPIRVExecutionModeMap::const_iterator, |
| 742 |
SPIRVExecutionModeMap::const_iterator> |
| 743 |
SPIRVExecutionModeRange; |
| 744 |
|
| 745 |
public: |
| 746 |
void addExecutionMode(SPIRVExecutionMode *ExecMode) { |
| 747 |
// There should not be more than 1 execution mode kind except the ones |
| 748 |
// mentioned in SPV_KHR_float_controls and SPV_INTEL_float_controls2. |
| 749 |
#ifndef NDEBUG |
| 750 |
auto IsDenorm = [](auto EMK) { |
| 751 |
return EMK == ExecutionModeDenormPreserve || |
| 752 |
EMK == ExecutionModeDenormFlushToZero; |
| 753 |
}; |
| 754 |
auto IsRoundingMode = [](auto EMK) { |
| 755 |
return EMK == ExecutionModeRoundingModeRTE || |
| 756 |
EMK == ExecutionModeRoundingModeRTZ || |
| 757 |
EMK == ExecutionModeRoundingModeRTPINTEL || |
| 758 |
EMK == ExecutionModeRoundingModeRTNINTEL; |
| 759 |
}; |
| 760 |
auto IsFPMode = [](auto EMK) { |
| 761 |
return EMK == ExecutionModeFloatingPointModeALTINTEL || |
| 762 |
EMK == ExecutionModeFloatingPointModeIEEEINTEL; |
| 763 |
}; |
| 764 |
auto IsOtherFP = [](auto EMK) { |
| 765 |
return EMK == ExecutionModeSignedZeroInfNanPreserve; |
| 766 |
}; |
| 767 |
auto IsFloatControl = [&](auto EMK) { |
| 768 |
return IsDenorm(EMK) || IsRoundingMode(EMK) || IsFPMode(EMK) || |
| 769 |
IsOtherFP(EMK); |
| 770 |
}; |
| 771 |
auto IsMaxRegisters = [&](auto EMK) { |
| 772 |
return EMK == ExecutionModeMaximumRegistersINTEL || |
| 773 |
EMK == ExecutionModeMaximumRegistersIdINTEL || |
| 774 |
EMK == ExecutionModeNamedMaximumRegistersINTEL; |
| 775 |
}; |
| 776 |
auto IsCompatible = [&](SPIRVExecutionMode *EM0, SPIRVExecutionMode *EM1) { |
| 777 |
if (EM0->getTargetId() != EM1->getTargetId()) |
| 778 |
return true; |
| 779 |
auto EMK0 = EM0->getExecutionMode(); |
| 780 |
auto EMK1 = EM1->getExecutionMode(); |
| 781 |
if (!IsFloatControl(EMK0) || !IsFloatControl(EMK1)) |
| 782 |
return EMK0 != EMK1; |
| 783 |
auto TW0 = EM0->getLiterals().at(0); |
| 784 |
auto TW1 = EM1->getLiterals().at(0); |
| 785 |
if (TW0 != TW1) |
| 786 |
return true; |
| 787 |
return !(IsDenorm(EMK0) && IsDenorm(EMK1)) && |
| 788 |
!(IsRoundingMode(EMK0) && IsRoundingMode(EMK1)) && |
| 789 |
!(IsFPMode(EMK0) && IsFPMode(EMK1)) && |
| 790 |
!(IsMaxRegisters(EMK0) && IsMaxRegisters(EMK1)); |
| 791 |
}; |
| 792 |
for (auto I = ExecModes.begin(); I != ExecModes.end(); ++I) { |
| 793 |
assert(IsCompatible(ExecMode, (*I).second) && |
| 794 |
"Found incompatible execution modes"); |
| 795 |
} |
| 796 |
#endif // !NDEBUG |
| 797 |
SPIRVExecutionModeKind EMK = ExecMode->getExecutionMode(); |
| 798 |
ExecModes.emplace(EMK, ExecMode); |
| 799 |
} |
| 800 |
SPIRVExecutionMode *getExecutionMode(SPIRVExecutionModeKind EMK) const { |
| 801 |
auto Loc = ExecModes.find(EMK); |
| 802 |
if (Loc == ExecModes.end()) |
| 803 |
return nullptr; |
| 804 |
return Loc->second; |
| 805 |
} |
| 806 |
SPIRVExecutionModeId *getExecutionModeId(SPIRVExecutionModeKind EMK) const { |
| 807 |
auto Loc = ExecModes.find(EMK); |
| 808 |
if (Loc == ExecModes.end()) |
| 809 |
return nullptr; |
| 810 |
return static_cast<SPIRVExecutionModeId *>(Loc->second); |
| 811 |
} |
| 812 |
SPIRVExecutionModeRange |
| 813 |
getExecutionModeRange(SPIRVExecutionModeKind EMK) const { |
| 814 |
return ExecModes.equal_range(EMK); |
| 815 |
} |
| 816 |
|
| 817 |
protected: |
| 818 |
SPIRVExecutionModeMap ExecModes; |
| 819 |
}; |
| 820 |
|
| 821 |
class SPIRVExtInstImport : public SPIRVEntry { |
| 822 |
public: |
| 823 |
const static Op OC = OpExtInstImport; |
| 824 |
// Complete constructor |
| 825 |
SPIRVExtInstImport(SPIRVModule *TheModule, SPIRVId TheId, |
| 826 |
const std::string &TheStr); |
| 827 |
// Incomplete constructor |
| 828 |
SPIRVExtInstImport() : SPIRVEntry(OC) {} |
| 829 |
|
| 830 |
protected: |
| 831 |
_SPIRV_DCL_ENCDEC |
| 832 |
void validate() const override; |
| 833 |
|
| 834 |
std::string Str; |
| 835 |
}; |
| 836 |
|
| 837 |
class SPIRVMemoryModel : public SPIRVEntryNoId<OpMemoryModel> { |
| 838 |
public: |
| 839 |
SPIRVMemoryModel(SPIRVModule *M) : SPIRVEntryNoId(M, 3) {} |
| 840 |
SPIRVMemoryModel() {} |
| 841 |
_SPIRV_DCL_ENCDEC |
| 842 |
void validate() const override; |
| 843 |
}; |
| 844 |
|
| 845 |
class SPIRVSource : public SPIRVEntryNoId<OpSource> { |
| 846 |
public: |
| 847 |
SPIRVSource(SPIRVModule *M) : SPIRVEntryNoId(M, 3) {} |
| 848 |
SPIRVSource() {} |
| 849 |
_SPIRV_DCL_ENCDEC |
| 850 |
}; |
| 851 |
|
| 852 |
class SPIRVSourceExtension : public SPIRVEntryNoId<OpSourceExtension> { |
| 853 |
public: |
| 854 |
SPIRVSourceExtension(SPIRVModule *M, const std::string &SS); |
| 855 |
SPIRVSourceExtension() {} |
| 856 |
_SPIRV_DCL_ENCDEC |
| 857 |
private: |
| 858 |
std::string S; |
| 859 |
}; |
| 860 |
|
| 861 |
class SPIRVExtension : public SPIRVEntryNoId<OpExtension> { |
| 862 |
public: |
| 863 |
SPIRVExtension(SPIRVModule *M, const std::string &SS); |
| 864 |
SPIRVExtension() {} |
| 865 |
|
| 866 |
std::string getExtensionName() const { return S; } |
| 867 |
|
| 868 |
_SPIRV_DCL_ENCDEC |
| 869 |
private: |
| 870 |
std::string S; |
| 871 |
}; |
| 872 |
|
| 873 |
class SPIRVCapability : public SPIRVEntryNoId<OpCapability> { |
| 874 |
public: |
| 875 |
SPIRVCapability(SPIRVModule *M, SPIRVCapabilityKind K); |
| 876 |
SPIRVCapability() : Kind(CapabilityMatrix) {} |
| 877 |
_SPIRV_DCL_ENCDEC |
| 878 |
|
| 879 |
SPIRVWord getRequiredSPIRVVersion() const override { |
| 880 |
switch (Kind) { |
| 881 |
case CapabilityGroupNonUniform: |
| 882 |
case CapabilityGroupNonUniformVote: |
| 883 |
case CapabilityGroupNonUniformArithmetic: |
| 884 |
case CapabilityGroupNonUniformBallot: |
| 885 |
case CapabilityGroupNonUniformShuffle: |
| 886 |
case CapabilityGroupNonUniformShuffleRelative: |
| 887 |
case CapabilityGroupNonUniformClustered: |
| 888 |
return static_cast<SPIRVWord>(VersionNumber::SPIRV_1_3); |
| 889 |
|
| 890 |
case CapabilityNamedBarrier: |
| 891 |
case CapabilitySubgroupDispatch: |
| 892 |
case CapabilityPipeStorage: |
| 893 |
return static_cast<SPIRVWord>(VersionNumber::SPIRV_1_1); |
| 894 |
|
| 895 |
default: |
| 896 |
return static_cast<SPIRVWord>(VersionNumber::SPIRV_1_0); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
std::optional<ExtensionID> getRequiredExtension() const override { |
| 901 |
switch (static_cast<unsigned>(Kind)) { |
| 902 |
case CapabilityRoundToInfinityINTEL: |
| 903 |
case CapabilityFloatingPointModeINTEL: |
| 904 |
case CapabilityFunctionFloatControlINTEL: |
| 905 |
return ExtensionID::SPV_INTEL_float_controls2; |
| 906 |
case CapabilityVectorComputeINTEL: |
| 907 |
case CapabilityVectorAnyINTEL: |
| 908 |
return ExtensionID::SPV_INTEL_vector_compute; |
| 909 |
case CapabilityFunctionVariantsINTEL: |
| 910 |
case CapabilitySpecConditionalINTEL: |
| 911 |
return ExtensionID::SPV_INTEL_function_variants; |
| 912 |
case internal::CapabilityBFloat16ArithmeticINTEL: |
| 913 |
return ExtensionID::SPV_INTEL_bfloat16_arithmetic; |
| 914 |
case internal::CapabilityDeviceBarrierINTEL: |
| 915 |
return ExtensionID::SPV_INTEL_device_barrier; |
| 916 |
default: |
| 917 |
return {}; |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
private: |
| 922 |
SPIRVCapabilityKind Kind; |
| 923 |
}; |
| 924 |
|
| 925 |
template <class T> T *bcast(SPIRVEntry *E) { return static_cast<T *>(E); } |
| 926 |
|
| 927 |
template <spv::Op OC> bool isa(SPIRVEntry *E) { |
| 928 |
return E ? E->getOpCode() == OC : false; |
| 929 |
} |
| 930 |
|
| 931 |
template <spv::Op OC> |
| 932 |
class SPIRVContinuedInstINTELBase : public SPIRVEntryNoId<OC> { |
| 933 |
public: |
| 934 |
template <spv::Op _OC, class T = void> |
| 935 |
using EnableIfCompositeConst = |
| 936 |
typename std::enable_if_t<_OC == OpConstantCompositeContinuedINTEL || |
| 937 |
_OC == |
| 938 |
OpSpecConstantCompositeContinuedINTEL, |
| 939 |
T>; |
| 940 |
// Complete constructor |
| 941 |
SPIRVContinuedInstINTELBase(SPIRVModule *M, |
| 942 |
const std::vector<SPIRVValue *> &TheElements) |
| 943 |
: SPIRVEntryNoId<OC>(M, TheElements.size() + 1) { |
| 944 |
|
| 945 |
Elements = SPIRVEntry::getIds(TheElements); |
| 946 |
validate(); |
| 947 |
} |
| 948 |
|
| 949 |
SPIRVContinuedInstINTELBase(SPIRVModule *M, unsigned NumOfElements) |
| 950 |
: SPIRVEntryNoId<OC>(M, NumOfElements + 1) { |
| 951 |
Elements.resize(NumOfElements, SPIRVID_INVALID); |
| 952 |
validate(); |
| 953 |
} |
| 954 |
|
| 955 |
// Incomplete constructor |
| 956 |
SPIRVContinuedInstINTELBase() : SPIRVEntryNoId<OC>() {} |
| 957 |
|
| 958 |
template <spv::Op OPC = OC> |
| 959 |
EnableIfCompositeConst<OPC, std::vector<SPIRVValue *>> getElements() const { |
| 960 |
return SPIRVEntry::getValues(Elements); |
| 961 |
} |
| 962 |
|
| 963 |
SPIRVCapVec getRequiredCapability() const override { |
| 964 |
return getVec(CapabilityLongCompositesINTEL); |
| 965 |
} |
| 966 |
|
| 967 |
std::optional<ExtensionID> getRequiredExtension() const override { |
| 968 |
return ExtensionID::SPV_INTEL_long_constant_composite; |
| 969 |
} |
| 970 |
|
| 971 |
SPIRVWord getNumElements() const { return Elements.size(); } |
| 972 |
|
| 973 |
protected: |
| 974 |
void validate() const override; |
| 975 |
void setWordCount(SPIRVWord WordCount) override { |
| 976 |
SPIRVEntry::setWordCount(WordCount); |
| 977 |
Elements.resize(WordCount - 1); |
| 978 |
} |
| 979 |
_SPIRV_DCL_ENCDEC |
| 980 |
|
| 981 |
std::vector<SPIRVId> Elements; |
| 982 |
}; |
| 983 |
|
| 984 |
class SPIRVTypeStructContinuedINTEL |
| 985 |
: public SPIRVContinuedInstINTELBase<OpTypeStructContinuedINTEL> { |
| 986 |
public: |
| 987 |
constexpr static Op OC = OpTypeStructContinuedINTEL; |
| 988 |
// Complete constructor |
| 989 |
SPIRVTypeStructContinuedINTEL(SPIRVModule *M, unsigned NumOfElements) |
| 990 |
: SPIRVContinuedInstINTELBase<OC>(M, NumOfElements) {} |
| 991 |
|
| 992 |
// Incomplete constructor |
| 993 |
SPIRVTypeStructContinuedINTEL() : SPIRVContinuedInstINTELBase<OC>() {} |
| 994 |
|
| 995 |
void setElementId(size_t I, SPIRVId Id) { Elements[I] = Id; } |
| 996 |
|
| 997 |
SPIRVType *getMemberType(size_t I) const; |
| 998 |
}; |
| 999 |
|
| 1000 |
using SPIRVConstantCompositeContinuedINTEL = |
| 1001 |
SPIRVContinuedInstINTELBase<OpConstantCompositeContinuedINTEL>; |
| 1002 |
using SPIRVSpecConstantCompositeContinuedINTEL = |
| 1003 |
SPIRVContinuedInstINTELBase<OpSpecConstantCompositeContinuedINTEL>; |
| 1004 |
|
| 1005 |
template <spv::Op OpCode> struct InstToContinued; |
| 1006 |
|
| 1007 |
template <> struct InstToContinued<OpTypeStruct> { |
| 1008 |
using Type = SPIRVTypeStructContinuedINTEL *; |
| 1009 |
constexpr static spv::Op OpCode = OpTypeStructContinuedINTEL; |
| 1010 |
}; |
| 1011 |
|
| 1012 |
template <> struct InstToContinued<OpConstantComposite> { |
| 1013 |
using Type = SPIRVConstantCompositeContinuedINTEL *; |
| 1014 |
constexpr static spv::Op OpCode = OpConstantCompositeContinuedINTEL; |
| 1015 |
}; |
| 1016 |
|
| 1017 |
template <> struct InstToContinued<OpSpecConstantComposite> { |
| 1018 |
using Type = SPIRVSpecConstantCompositeContinuedINTEL *; |
| 1019 |
constexpr static spv::Op OpCode = OpSpecConstantCompositeContinuedINTEL; |
| 1020 |
}; |
| 1021 |
|
| 1022 |
class SPIRVModuleProcessed : public SPIRVEntryNoId<OpModuleProcessed> { |
| 1023 |
public: |
| 1024 |
SPIRVModuleProcessed(SPIRVModule *M, const std::string &Process) |
| 1025 |
: SPIRVEntryNoId(M, FixedWC + getSizeInWords(Process)), |
| 1026 |
ProcessStr(Process) { |
| 1027 |
updateModuleVersion(); |
| 1028 |
} |
| 1029 |
SPIRVModuleProcessed() { updateModuleVersion(); } |
| 1030 |
_SPIRV_DCL_ENCDEC |
| 1031 |
void validate() const override; |
| 1032 |
SPIRVWord getRequiredSPIRVVersion() const override { |
| 1033 |
return static_cast<SPIRVWord>(VersionNumber::SPIRV_1_1); |
| 1034 |
} |
| 1035 |
|
| 1036 |
std::string getProcessStr(); |
| 1037 |
|
| 1038 |
private: |
| 1039 |
std::string ProcessStr; |
| 1040 |
static const SPIRVWord FixedWC = 1; |
| 1041 |
}; |
| 1042 |
|
| 1043 |
// ToDo: The following typedef's are place holders for SPIRV entity classes |
| 1044 |
// to be implemented. |
| 1045 |
// Each time a new class is implemented, remove the corresponding typedef. |
| 1046 |
// This is also an indication of how much work is left. |
| 1047 |
#define _SPIRV_OP(x) typedef SPIRVEntryUnimplemented<Op##x> SPIRV##x; |
| 1048 |
_SPIRV_OP(Nop) |
| 1049 |
_SPIRV_OP(SourceContinued) |
| 1050 |
_SPIRV_OP(TypeRuntimeArray) |
| 1051 |
_SPIRV_OP(Image) |
| 1052 |
_SPIRV_OP(ImageTexelPointer) |
| 1053 |
_SPIRV_OP(ImageSampleDrefImplicitLod) |
| 1054 |
_SPIRV_OP(ImageSampleDrefExplicitLod) |
| 1055 |
_SPIRV_OP(ImageSampleProjImplicitLod) |
| 1056 |
_SPIRV_OP(ImageSampleProjExplicitLod) |
| 1057 |
_SPIRV_OP(ImageSampleProjDrefImplicitLod) |
| 1058 |
_SPIRV_OP(ImageSampleProjDrefExplicitLod) |
| 1059 |
_SPIRV_OP(ImageFetch) |
| 1060 |
_SPIRV_OP(ImageGather) |
| 1061 |
_SPIRV_OP(ImageDrefGather) |
| 1062 |
_SPIRV_OP(QuantizeToF16) |
| 1063 |
_SPIRV_OP(ArrayLength) |
| 1064 |
_SPIRV_OP(OuterProduct) |
| 1065 |
_SPIRV_OP(DPdx) |
| 1066 |
_SPIRV_OP(DPdy) |
| 1067 |
_SPIRV_OP(Fwidth) |
| 1068 |
_SPIRV_OP(DPdxFine) |
| 1069 |
_SPIRV_OP(DPdyFine) |
| 1070 |
_SPIRV_OP(FwidthFine) |
| 1071 |
_SPIRV_OP(DPdxCoarse) |
| 1072 |
_SPIRV_OP(DPdyCoarse) |
| 1073 |
_SPIRV_OP(FwidthCoarse) |
| 1074 |
_SPIRV_OP(EmitVertex) |
| 1075 |
_SPIRV_OP(EndPrimitive) |
| 1076 |
_SPIRV_OP(EmitStreamVertex) |
| 1077 |
_SPIRV_OP(EndStreamPrimitive) |
| 1078 |
_SPIRV_OP(Kill) |
| 1079 |
_SPIRV_OP(ImageSparseSampleImplicitLod) |
| 1080 |
_SPIRV_OP(ImageSparseSampleExplicitLod) |
| 1081 |
_SPIRV_OP(ImageSparseSampleDrefImplicitLod) |
| 1082 |
_SPIRV_OP(ImageSparseSampleDrefExplicitLod) |
| 1083 |
_SPIRV_OP(ImageSparseSampleProjImplicitLod) |
| 1084 |
_SPIRV_OP(ImageSparseSampleProjExplicitLod) |
| 1085 |
_SPIRV_OP(ImageSparseSampleProjDrefImplicitLod) |
| 1086 |
_SPIRV_OP(ImageSparseSampleProjDrefExplicitLod) |
| 1087 |
_SPIRV_OP(ImageSparseFetch) |
| 1088 |
_SPIRV_OP(ImageSparseGather) |
| 1089 |
_SPIRV_OP(ImageSparseDrefGather) |
| 1090 |
_SPIRV_OP(ImageSparseTexelsResident) |
| 1091 |
_SPIRV_OP(NoLine) |
| 1092 |
_SPIRV_OP(TypeNamedBarrier) |
| 1093 |
_SPIRV_OP(NamedBarrierInitialize) |
| 1094 |
_SPIRV_OP(MemoryNamedBarrier) |
| 1095 |
_SPIRV_OP(GetKernelMaxNumSubgroups) |
| 1096 |
_SPIRV_OP(GetKernelLocalSizeForSubgroupCount) |
| 1097 |
#undef _SPIRV_OP |
| 1098 |
|
| 1099 |
} // namespace SPIRV |
| 1100 |
#endif // SPIRV_LIBSPIRV_SPIRVENTRY_H |