| 1 |
//===- SPIRVStream.h - Class to represent a SPIR-V Stream -------*- 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 Word class for SPIR-V. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#ifndef SPIRV_LIBSPIRV_SPIRVSTREAM_H |
| 41 |
#define SPIRV_LIBSPIRV_SPIRVSTREAM_H |
| 42 |
|
| 43 |
#include "SPIRVDebug.h" |
| 44 |
#include "SPIRVExtInst.h" |
| 45 |
#include "SPIRVModule.h" |
| 46 |
#include <cctype> |
| 47 |
#include <cstdint> |
| 48 |
#include <iostream> |
| 49 |
#include <string> |
| 50 |
#include <vector> |
| 51 |
|
| 52 |
namespace SPIRV { |
| 53 |
|
| 54 |
#ifndef _SPIRV_SUPPORT_TEXT_FMT |
| 55 |
#define _SPIRV_SUPPORT_TEXT_FMT |
| 56 |
#endif |
| 57 |
|
| 58 |
#ifdef _SPIRV_SUPPORT_TEXT_FMT |
| 59 |
// Use textual format for SPIRV. |
| 60 |
extern bool SPIRVUseTextFormat; |
| 61 |
#endif |
| 62 |
|
| 63 |
class SPIRVFunction; |
| 64 |
class SPIRVBasicBlock; |
| 65 |
|
| 66 |
class SPIRVDecoder { |
| 67 |
public: |
| 68 |
SPIRVDecoder(std::istream &InputStream, SPIRVModule &Module) |
| 69 |
: IS(InputStream), M(Module), WordCount(0), OpCode(OpNop), Scope(NULL) {} |
| 70 |
SPIRVDecoder(std::istream &InputStream, SPIRVFunction &F); |
| 71 |
SPIRVDecoder(std::istream &InputStream, SPIRVBasicBlock &BB); |
| 72 |
|
| 73 |
void setScope(SPIRVEntry *); |
| 74 |
bool getWordCountAndOpCode(); |
| 75 |
SPIRVEntry *getEntry(); |
| 76 |
void validate() const; |
| 77 |
void ignore(size_t N); |
| 78 |
void ignoreInstruction(); |
| 79 |
std::vector<SPIRVEntry *> |
| 80 |
getContinuedInstructions(const spv::Op ContinuedOpCode); |
| 81 |
std::vector<SPIRVEntry *> getSourceContinuedInstructions(); |
| 82 |
|
| 83 |
std::istream &IS; |
| 84 |
SPIRVModule &M; |
| 85 |
SPIRVWord WordCount; |
| 86 |
Op OpCode; |
| 87 |
SPIRVEntry *Scope; // A function or basic block |
| 88 |
}; |
| 89 |
|
| 90 |
class SPIRVEncoder { |
| 91 |
public: |
| 92 |
explicit SPIRVEncoder(spv_ostream &OutputStream) : OS(OutputStream) {} |
| 93 |
spv_ostream &OS; |
| 94 |
}; |
| 95 |
|
| 96 |
/// Output a new line in text mode. Do nothing in binary mode. |
| 97 |
class SPIRVNL { |
| 98 |
friend spv_ostream &operator<<(spv_ostream &O, const SPIRVNL &E); |
| 99 |
}; |
| 100 |
|
| 101 |
template <typename T> |
| 102 |
const SPIRVDecoder &decodeBinary(const SPIRVDecoder &I, T &V) { |
| 103 |
uint32_t W; |
| 104 |
I.IS.read(reinterpret_cast<char *>(&W), sizeof(W)); |
| 105 |
V = static_cast<T>(W); |
| 106 |
SPIRVDBG(spvdbgs() << "Read word: W = " << W << " V = " << V << '\n'); |
| 107 |
return I; |
| 108 |
} |
| 109 |
|
| 110 |
#ifdef _SPIRV_SUPPORT_TEXT_FMT |
| 111 |
/// Skip comment and whitespace. Comment starts with ';', ends with '\n'. |
| 112 |
inline std::istream &skipcomment(std::istream &IS) { |
| 113 |
if (IS.eof() || IS.bad()) |
| 114 |
return IS; |
| 115 |
|
| 116 |
char C = IS.peek(); |
| 117 |
|
| 118 |
while (std::char_traits<char>::not_eof(C) && std::isspace(C)) { |
| 119 |
IS.get(); |
| 120 |
C = IS.peek(); |
| 121 |
} |
| 122 |
|
| 123 |
while (std::char_traits<char>::not_eof(C) && C == ';') { |
| 124 |
IS.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); |
| 125 |
C = IS.peek(); |
| 126 |
while (std::char_traits<char>::not_eof(C) && std::isspace(C)) { |
| 127 |
IS.get(); |
| 128 |
C = IS.peek(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return IS; |
| 133 |
} |
| 134 |
#endif |
| 135 |
|
| 136 |
template <typename T> |
| 137 |
const SPIRVDecoder &operator>>(const SPIRVDecoder &I, T &V) { |
| 138 |
#ifdef _SPIRV_SUPPORT_TEXT_FMT |
| 139 |
if (SPIRVUseTextFormat) { |
| 140 |
uint32_t W; |
| 141 |
I.IS >> skipcomment >> W; |
| 142 |
V = static_cast<T>(W); |
| 143 |
SPIRVDBG(spvdbgs() << "Read word: W = " << W << " V = " << V << '\n'); |
| 144 |
return I; |
| 145 |
} |
| 146 |
#endif |
| 147 |
return decodeBinary(I, V); |
| 148 |
} |
| 149 |
|
| 150 |
template <typename T> |
| 151 |
const SPIRVDecoder &operator>>(const SPIRVDecoder &I, T *&P) { |
| 152 |
SPIRVId Id; |
| 153 |
I >> Id; |
| 154 |
P = static_cast<T *>(I.M.getEntry(Id)); |
| 155 |
return I; |
| 156 |
} |
| 157 |
|
| 158 |
template <typename IterTy> |
| 159 |
const SPIRVDecoder &operator>>(const SPIRVDecoder &Decoder, |
| 160 |
const std::pair<IterTy, IterTy> &Range) { |
| 161 |
for (IterTy I = Range.first, E = Range.second; I != E; ++I) |
| 162 |
Decoder >> *I; |
| 163 |
return Decoder; |
| 164 |
} |
| 165 |
|
| 166 |
template <typename T> |
| 167 |
const SPIRVDecoder &operator>>(const SPIRVDecoder &I, std::vector<T> &V) { |
| 168 |
for (size_t J = 0, E = V.size(); J != E; ++J) |
| 169 |
I >> V[J]; |
| 170 |
return I; |
| 171 |
} |
| 172 |
|
| 173 |
template <typename T> |
| 174 |
const SPIRVDecoder &operator>>(const SPIRVDecoder &I, std::optional<T> &V) { |
| 175 |
if (V) |
| 176 |
I >> V.value(); |
| 177 |
return I; |
| 178 |
} |
| 179 |
|
| 180 |
template <typename T> |
| 181 |
const SPIRVEncoder &operator<<(const SPIRVEncoder &O, T V) { |
| 182 |
#ifdef _SPIRV_SUPPORT_TEXT_FMT |
| 183 |
if (SPIRVUseTextFormat) { |
| 184 |
O.OS << V << " "; |
| 185 |
return O; |
| 186 |
} |
| 187 |
#endif |
| 188 |
uint32_t W = static_cast<uint32_t>(V); |
| 189 |
O.OS.write(reinterpret_cast<char *>(&W), sizeof(W)); |
| 190 |
return O; |
| 191 |
} |
| 192 |
|
| 193 |
template <typename T> |
| 194 |
const SPIRVEncoder &operator<<(const SPIRVEncoder &O, T *P) { |
| 195 |
return O << P->getId(); |
| 196 |
} |
| 197 |
template <> const SPIRVEncoder &operator<<(const SPIRVEncoder &O, SPIRVType *P); |
| 198 |
|
| 199 |
template <typename T> |
| 200 |
const SPIRVEncoder &operator<<(const SPIRVEncoder &O, const std::vector<T> &V) { |
| 201 |
for (size_t I = 0, E = V.size(); I != E; ++I) |
| 202 |
O << V[I]; |
| 203 |
return O; |
| 204 |
} |
| 205 |
|
| 206 |
template <typename T> |
| 207 |
const SPIRVEncoder &operator<<(const SPIRVEncoder &O, |
| 208 |
const std::optional<T> &V) { |
| 209 |
if (V) |
| 210 |
O << V.value(); |
| 211 |
return O; |
| 212 |
} |
| 213 |
|
| 214 |
template <typename IterTy> |
| 215 |
const SPIRVEncoder &operator<<(const SPIRVEncoder &Encoder, |
| 216 |
const std::pair<IterTy, IterTy> &Range) { |
| 217 |
for (IterTy I = Range.first, E = Range.second; I != E; ++I) |
| 218 |
Encoder << *I; |
| 219 |
return Encoder; |
| 220 |
} |
| 221 |
|
| 222 |
#define SPIRV_DEC_ENCDEC(Type) \ |
| 223 |
const SPIRVEncoder &operator<<(const SPIRVEncoder &O, Type V); \ |
| 224 |
const SPIRVDecoder &operator>>(const SPIRVDecoder &I, Type &V); |
| 225 |
|
| 226 |
SPIRV_DEC_ENCDEC(Op) |
| 227 |
SPIRV_DEC_ENCDEC(Capability) |
| 228 |
SPIRV_DEC_ENCDEC(Decoration) |
| 229 |
SPIRV_DEC_ENCDEC(OCLExtOpKind) |
| 230 |
SPIRV_DEC_ENCDEC(SPIRVDebugExtOpKind) |
| 231 |
SPIRV_DEC_ENCDEC(NonSemanticAuxDataOpKind) |
| 232 |
SPIRV_DEC_ENCDEC(NamedMaximumNumberOfRegisters) |
| 233 |
SPIRV_DEC_ENCDEC(InitializationModeQualifier) |
| 234 |
SPIRV_DEC_ENCDEC(HostAccessQualifier) |
| 235 |
SPIRV_DEC_ENCDEC(LinkageType) |
| 236 |
|
| 237 |
const SPIRVEncoder &operator<<(const SPIRVEncoder &O, const std::string &Str); |
| 238 |
const SPIRVDecoder &operator>>(const SPIRVDecoder &I, std::string &Str); |
| 239 |
|
| 240 |
} // namespace SPIRV |
| 241 |
#endif // SPIRV_LIBSPIRV_SPIRVSTREAM_H |