| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#ifndef SPIRVREMAPPER_H |
| 37 |
#define SPIRVREMAPPER_H |
| 38 |
|
| 39 |
#include <string> |
| 40 |
#include <vector> |
| 41 |
#include <cstdlib> |
| 42 |
#include <exception> |
| 43 |
|
| 44 |
namespace spv { |
| 45 |
|
| 46 |
class spirvbin_base_t |
| 47 |
{ |
| 48 |
public: |
| 49 |
enum Options { |
| 50 |
NONE = 0, |
| 51 |
STRIP = (1<<0), |
| 52 |
MAP_TYPES = (1<<1), |
| 53 |
MAP_NAMES = (1<<2), |
| 54 |
MAP_FUNCS = (1<<3), |
| 55 |
DCE_FUNCS = (1<<4), |
| 56 |
DCE_VARS = (1<<5), |
| 57 |
DCE_TYPES = (1<<6), |
| 58 |
OPT_LOADSTORE = (1<<7), |
| 59 |
OPT_FWD_LS = (1<<8), |
| 60 |
MAP_ALL = (MAP_TYPES | MAP_NAMES | MAP_FUNCS), |
| 61 |
DCE_ALL = (DCE_FUNCS | DCE_VARS | DCE_TYPES), |
| 62 |
OPT_ALL = (OPT_LOADSTORE), |
| 63 |
|
| 64 |
ALL_BUT_STRIP = (MAP_ALL | DCE_ALL | OPT_ALL), |
| 65 |
DO_EVERYTHING = (STRIP | ALL_BUT_STRIP) |
| 66 |
}; |
| 67 |
}; |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
#include <functional> |
| 72 |
#include <cstdint> |
| 73 |
#include <unordered_map> |
| 74 |
#include <unordered_set> |
| 75 |
#include <map> |
| 76 |
#include <set> |
| 77 |
#include <cassert> |
| 78 |
|
| 79 |
#include "spirv.hpp" |
| 80 |
|
| 81 |
namespace spv { |
| 82 |
const Id NoResult = 0; |
| 83 |
|
| 84 |
|
| 85 |
class spirvbin_t : public spirvbin_base_t |
| 86 |
{ |
| 87 |
public: |
| 88 |
spirvbin_t(int verbose = 0) : entryPoint(spv::NoResult), largestNewId(0), verbose(verbose), errorLatch(false) |
| 89 |
{ } |
| 90 |
|
| 91 |
virtual ~spirvbin_t() { } |
| 92 |
|
| 93 |
|
| 94 |
void remap(std::vector<std::uint32_t>& spv, const std::vector<std::string>& whiteListStrings, |
| 95 |
std::uint32_t opts = DO_EVERYTHING); |
| 96 |
|
| 97 |
|
| 98 |
void remap(std::vector<std::uint32_t>& spv, std::uint32_t opts = DO_EVERYTHING); |
| 99 |
|
| 100 |
|
| 101 |
typedef std::function<void(const std::string&)> errorfn_t; |
| 102 |
typedef std::function<void(const std::string&)> logfn_t; |
| 103 |
|
| 104 |
|
| 105 |
static void registerErrorHandler(errorfn_t handler) { errorHandler = handler; } |
| 106 |
static void registerLogHandler(logfn_t handler) { logHandler = handler; } |
| 107 |
|
| 108 |
protected: |
| 109 |
|
| 110 |
virtual void msg(int minVerbosity, int indent, const std::string& txt) const; |
| 111 |
|
| 112 |
private: |
| 113 |
|
| 114 |
typedef std::unordered_map<spv::Id, spv::Id> idmap_t; |
| 115 |
typedef std::unordered_set<spv::Id> idset_t; |
| 116 |
typedef std::unordered_map<spv::Id, int> blockmap_t; |
| 117 |
|
| 118 |
void remap(std::uint32_t opts = DO_EVERYTHING); |
| 119 |
|
| 120 |
|
| 121 |
typedef std::unordered_map<std::string, spv::Id> namemap_t; |
| 122 |
|
| 123 |
typedef std::uint32_t spirword_t; |
| 124 |
|
| 125 |
typedef std::pair<unsigned, unsigned> range_t; |
| 126 |
typedef std::function<void(spv::Id&)> idfn_t; |
| 127 |
typedef std::function<bool(spv::Op, unsigned start)> instfn_t; |
| 128 |
|
| 129 |
|
| 130 |
static const spv::Id unmapped; |
| 131 |
static const spv::Id unused; |
| 132 |
static const int header_size; |
| 133 |
|
| 134 |
class id_iterator_t; |
| 135 |
|
| 136 |
|
| 137 |
typedef std::vector<spirword_t> typeentry_t; |
| 138 |
typedef std::map<spv::Id, typeentry_t> globaltypes_t; |
| 139 |
|
| 140 |
|
| 141 |
typedef std::set<int> posmap_t; |
| 142 |
typedef std::unordered_map<spv::Id, int> posmap_rev_t; |
| 143 |
|
| 144 |
|
| 145 |
typedef std::unordered_map<spv::Id, unsigned> typesize_map_t; |
| 146 |
|
| 147 |
|
| 148 |
void error(const std::string& txt) const { errorLatch = true; errorHandler(txt); } |
| 149 |
|
| 150 |
bool isConstOp(spv::Op opCode) const; |
| 151 |
bool isTypeOp(spv::Op opCode) const; |
| 152 |
bool isStripOp(spv::Op opCode) const; |
| 153 |
bool isFlowCtrl(spv::Op opCode) const; |
| 154 |
range_t literalRange(spv::Op opCode) const; |
| 155 |
range_t typeRange(spv::Op opCode) const; |
| 156 |
range_t constRange(spv::Op opCode) const; |
| 157 |
unsigned typeSizeInWords(spv::Id id) const; |
| 158 |
unsigned idTypeSizeInWords(spv::Id id) const; |
| 159 |
|
| 160 |
bool isStripOp(spv::Op opCode, unsigned start) const; |
| 161 |
|
| 162 |
spv::Id& asId(unsigned word) { return spv[word]; } |
| 163 |
const spv::Id& asId(unsigned word) const { return spv[word]; } |
| 164 |
spv::Op asOpCode(unsigned word) const { return opOpCode(spv[word]); } |
| 165 |
std::uint32_t asOpCodeHash(unsigned word); |
| 166 |
spv::Decoration asDecoration(unsigned word) const { return spv::Decoration(spv[word]); } |
| 167 |
unsigned asWordCount(unsigned word) const { return opWordCount(spv[word]); } |
| 168 |
spv::Id asTypeConstId(unsigned word) const { return asId(word + (isTypeOp(asOpCode(word)) ? 1 : 2)); } |
| 169 |
unsigned idPos(spv::Id id) const; |
| 170 |
|
| 171 |
static unsigned opWordCount(spirword_t data) { return data >> spv::WordCountShift; } |
| 172 |
static spv::Op opOpCode(spirword_t data) { return spv::Op(data & spv::OpCodeMask); } |
| 173 |
|
| 174 |
|
| 175 |
spirword_t magic() const { return spv[0]; } |
| 176 |
spirword_t bound() const { return spv[3]; } |
| 177 |
spirword_t bound(spirword_t b) { return spv[3] = b; } |
| 178 |
spirword_t genmagic() const { return spv[2]; } |
| 179 |
spirword_t genmagic(spirword_t m) { return spv[2] = m; } |
| 180 |
spirword_t schemaNum() const { return spv[4]; } |
| 181 |
|
| 182 |
|
| 183 |
spv::Id localId(spv::Id id) const { return idMapL[id]; } |
| 184 |
|
| 185 |
|
| 186 |
inline spv::Id localId(spv::Id id, spv::Id newId); |
| 187 |
void countIds(spv::Id id); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
inline spv::Id nextUnusedId(spv::Id id); |
| 193 |
|
| 194 |
void buildLocalMaps(); |
| 195 |
std::string literalString(unsigned word) const; |
| 196 |
int literalStringWords(const std::string& str) const { return (int(str.size())+4)/4; } |
| 197 |
|
| 198 |
bool isNewIdMapped(spv::Id newId) const { return isMapped(newId); } |
| 199 |
bool isOldIdUnmapped(spv::Id oldId) const { return localId(oldId) == unmapped; } |
| 200 |
bool isOldIdUnused(spv::Id oldId) const { return localId(oldId) == unused; } |
| 201 |
bool isOldIdMapped(spv::Id oldId) const { return !isOldIdUnused(oldId) && !isOldIdUnmapped(oldId); } |
| 202 |
bool isFunction(spv::Id oldId) const { return fnPos.find(oldId) != fnPos.end(); } |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
std::uint32_t hashType(unsigned typeStart) const; |
| 207 |
|
| 208 |
spirvbin_t& process(instfn_t, idfn_t, unsigned begin = 0, unsigned end = 0); |
| 209 |
int processInstruction(unsigned word, instfn_t, idfn_t); |
| 210 |
|
| 211 |
void validate() const; |
| 212 |
void mapTypeConst(); |
| 213 |
void mapFnBodies(); |
| 214 |
void optLoadStore(); |
| 215 |
void dceFuncs(); |
| 216 |
void dceVars(); |
| 217 |
void dceTypes(); |
| 218 |
void mapNames(); |
| 219 |
void foldIds(); |
| 220 |
void forwardLoadStores(); |
| 221 |
void offsetIds(); |
| 222 |
|
| 223 |
void applyMap(); |
| 224 |
void mapRemainder(); |
| 225 |
void stripDebug(); |
| 226 |
void stripDeadRefs(); |
| 227 |
void strip(); |
| 228 |
|
| 229 |
std::vector<spirword_t> spv; |
| 230 |
|
| 231 |
std::vector<std::string> stripWhiteList; |
| 232 |
|
| 233 |
namemap_t nameMap; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
typedef std::uint64_t bits_t; |
| 238 |
std::vector<bits_t> mapped; |
| 239 |
static const int mBits = sizeof(bits_t) * 4; |
| 240 |
|
| 241 |
bool isMapped(spv::Id id) const { return id < maxMappedId() && ((mapped[id/mBits] & (1LL<<(id%mBits))) != 0); } |
| 242 |
void setMapped(spv::Id id) { resizeMapped(id); mapped[id/mBits] |= (1LL<<(id%mBits)); } |
| 243 |
void resizeMapped(spv::Id id) { if (id >= maxMappedId()) mapped.resize(id/mBits+1, 0); } |
| 244 |
size_t maxMappedId() const { return mapped.size() * mBits; } |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
void stripInst(unsigned start) { stripRange.push_back(range_t(start, start + asWordCount(start))); } |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
std::unordered_map<spv::Id, range_t> fnPos; |
| 253 |
|
| 254 |
|
| 255 |
std::unordered_map<spv::Id, int> fnCalls; |
| 256 |
|
| 257 |
posmap_t typeConstPos; |
| 258 |
posmap_rev_t idPosR; |
| 259 |
typesize_map_t idTypeSizeMap; |
| 260 |
|
| 261 |
std::vector<spv::Id> idMapL; |
| 262 |
|
| 263 |
spv::Id entryPoint; |
| 264 |
spv::Id largestNewId; |
| 265 |
|
| 266 |
|
| 267 |
std::vector<range_t> stripRange; |
| 268 |
|
| 269 |
|
| 270 |
std::uint32_t options; |
| 271 |
int verbose; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
mutable bool errorLatch; |
| 277 |
|
| 278 |
static errorfn_t errorHandler; |
| 279 |
static logfn_t logHandler; |
| 280 |
}; |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
#endif |