| 1 |
//===- SPIRVUtil.h - SPIR-V Utility Functions -------------------*- 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 SPIR-V utility functions. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#ifndef SPIRV_LIBSPIRV_SPIRVUTIL_H |
| 41 |
#define SPIRV_LIBSPIRV_SPIRVUTIL_H |
| 42 |
|
| 43 |
#include <ostream> |
| 44 |
#define spv_ostream std::ostream |
| 45 |
|
| 46 |
#include "llvm/Support/raw_ostream.h" |
| 47 |
|
| 48 |
#include <algorithm> |
| 49 |
#include <cassert> |
| 50 |
#include <cstdint> |
| 51 |
#include <functional> |
| 52 |
#include <limits> |
| 53 |
#include <map> |
| 54 |
#include <set> |
| 55 |
#include <sstream> |
| 56 |
#include <string> |
| 57 |
#include <unordered_set> |
| 58 |
#include <vector> |
| 59 |
|
| 60 |
// MSVC supports "magic statics" since MSVS 2015. |
| 61 |
// For the previous version of MSVS we should guard |
| 62 |
// initialization of local static variables. |
| 63 |
#if defined(_MSC_VER) && (_MSC_VER < 1900) |
| 64 |
#include "llvm/Support/Mutex.h" |
| 65 |
#include "llvm/Support/MutexGuard.h" |
| 66 |
#endif // LLVM_MSC_PREREQ(1900) |
| 67 |
|
| 68 |
namespace SPIRV { |
| 69 |
#if defined(_MSC_VER) && (_MSC_VER < 1900) |
| 70 |
static llvm::sys::Mutex MapLock; |
| 71 |
#endif // LLVM_MSC_PREREQ(1900) |
| 72 |
|
| 73 |
#define SPIRV_DEF_NAMEMAP(Type, MapType) \ |
| 74 |
typedef SPIRVMap<Type, std::string>(MapType); \ |
| 75 |
inline MapType getNameMap(Type) { \ |
| 76 |
MapType MT; \ |
| 77 |
return MT; \ |
| 78 |
} |
| 79 |
|
| 80 |
constexpr unsigned MaxWordCount = UINT16_MAX; |
| 81 |
|
| 82 |
// A bi-way map |
| 83 |
template <class Ty1, class Ty2, class Identifier = void> struct SPIRVMap { |
| 84 |
public: |
| 85 |
typedef Ty1 KeyTy; |
| 86 |
typedef Ty2 ValueTy; |
| 87 |
// Initialize map entries |
| 88 |
void init(); |
| 89 |
|
| 90 |
static Ty2 map(Ty1 Key) { |
| 91 |
Ty2 Val = {}; |
| 92 |
bool Found = find(Key, &Val); |
| 93 |
(void)Found; |
| 94 |
assert(Found && "Invalid key"); |
| 95 |
return Val; |
| 96 |
} |
| 97 |
|
| 98 |
static Ty1 rmap(Ty2 Key) { |
| 99 |
Ty1 Val = {}; |
| 100 |
bool Found = rfind(Key, &Val); |
| 101 |
(void)Found; |
| 102 |
assert(Found && "Invalid key"); |
| 103 |
return Val; |
| 104 |
} |
| 105 |
|
| 106 |
static const SPIRVMap &getMap() { |
| 107 |
#if defined(_MSC_VER) && (_MSC_VER < 1900) |
| 108 |
llvm::sys::ScopedLock mapGuard(MapLock); |
| 109 |
#endif // LLVM_MSC_PREREQ(1900) |
| 110 |
static const SPIRVMap Map(false); |
| 111 |
return Map; |
| 112 |
} |
| 113 |
|
| 114 |
static const SPIRVMap &getRMap() { |
| 115 |
#if defined(_MSC_VER) && (_MSC_VER < 1900) |
| 116 |
llvm::sys::ScopedLock mapGuard(MapLock); |
| 117 |
#endif // LLVM_MSC_PREREQ(1900) |
| 118 |
static const SPIRVMap Map(true); |
| 119 |
return Map; |
| 120 |
} |
| 121 |
|
| 122 |
static void foreach (std::function<void(Ty1, Ty2)> F) { |
| 123 |
for (auto &I : getMap().Map) |
| 124 |
F(I.first, I.second); |
| 125 |
} |
| 126 |
|
| 127 |
// For each key/value in the map executes function \p F. |
| 128 |
// If \p F returns false break the iteration. |
| 129 |
static void foreachConditional(std::function<bool(const Ty1 &, Ty2)> F) { |
| 130 |
for (auto &I : getMap().Map) { |
| 131 |
if (!F(I.first, I.second)) |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
static bool find(Ty1 Key, Ty2 *Val = nullptr) { |
| 137 |
const SPIRVMap &Map = getMap(); |
| 138 |
typename MapTy::const_iterator Loc = Map.Map.find(Key); |
| 139 |
if (Loc == Map.Map.end()) |
| 140 |
return false; |
| 141 |
if (Val) |
| 142 |
*Val = Loc->second; |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
static bool rfind(Ty2 Key, Ty1 *Val = nullptr) { |
| 147 |
const SPIRVMap &Map = getRMap(); |
| 148 |
typename RevMapTy::const_iterator Loc = Map.RevMap.find(Key); |
| 149 |
if (Loc == Map.RevMap.end()) |
| 150 |
return false; |
| 151 |
if (Val) |
| 152 |
*Val = Loc->second; |
| 153 |
return true; |
| 154 |
} |
| 155 |
SPIRVMap() : IsReverse(false) {} |
| 156 |
|
| 157 |
protected: |
| 158 |
SPIRVMap(bool Reverse) : IsReverse(Reverse) { init(); } |
| 159 |
typedef std::map<Ty1, Ty2> MapTy; |
| 160 |
typedef std::map<Ty2, Ty1> RevMapTy; |
| 161 |
|
| 162 |
void add(Ty1 V1, Ty2 V2) { |
| 163 |
if (IsReverse) { |
| 164 |
RevMap[V2] = V1; |
| 165 |
return; |
| 166 |
} |
| 167 |
Map[V1] = V2; |
| 168 |
} |
| 169 |
MapTy Map; |
| 170 |
RevMapTy RevMap; |
| 171 |
bool IsReverse; |
| 172 |
}; |
| 173 |
|
| 174 |
inline std::vector<std::string> getVec(const std::string &S, char Delim) { |
| 175 |
std::vector<std::string> Strs; |
| 176 |
std::stringstream SS(S); |
| 177 |
std::string Item; |
| 178 |
while (std::getline(SS, Item, Delim)) |
| 179 |
Strs.push_back(Item); |
| 180 |
return Strs; |
| 181 |
} |
| 182 |
|
| 183 |
inline std::unordered_set<std::string> getUnordSet(const std::string &S, |
| 184 |
char Delim = ' ') { |
| 185 |
std::unordered_set<std::string> Strs; |
| 186 |
std::stringstream SS(S); |
| 187 |
std::string Item; |
| 188 |
while (std::getline(SS, Item, Delim)) |
| 189 |
Strs.insert(Item); |
| 190 |
return Strs; |
| 191 |
} |
| 192 |
|
| 193 |
inline std::set<std::string> getSet(const std::string &S, char Delim = ' ') { |
| 194 |
std::set<std::string> Strs; |
| 195 |
std::stringstream SS(S); |
| 196 |
std::string Item; |
| 197 |
while (std::getline(SS, Item, Delim)) |
| 198 |
Strs.insert(Item); |
| 199 |
return Strs; |
| 200 |
} |
| 201 |
|
| 202 |
template <typename VT, typename KT> VT map(KT Key) { |
| 203 |
return SPIRVMap<KT, VT>::map(Key); |
| 204 |
} |
| 205 |
|
| 206 |
template <typename KT, typename VT> KT rmap(VT V) { |
| 207 |
return SPIRVMap<KT, VT>::rmap(V); |
| 208 |
} |
| 209 |
|
| 210 |
template <typename VT, typename KT> |
| 211 |
std::unordered_set<VT> map(const std::unordered_set<KT> &KSet) { |
| 212 |
VT V; |
| 213 |
std::unordered_set<VT> VSet; |
| 214 |
for (auto &I : KSet) |
| 215 |
if (SPIRVMap<KT, VT>::find(I, &V)) |
| 216 |
VSet.insert(V); |
| 217 |
return VSet; |
| 218 |
} |
| 219 |
|
| 220 |
template <typename VT, typename KT> std::set<VT> map(const std::set<KT> &KSet) { |
| 221 |
VT V; |
| 222 |
std::set<VT> VSet; |
| 223 |
for (auto &I : KSet) |
| 224 |
if (SPIRVMap<KT, VT>::find(I, &V)) |
| 225 |
VSet.insert(V); |
| 226 |
return VSet; |
| 227 |
} |
| 228 |
|
| 229 |
template <typename KT, typename VT> |
| 230 |
std::unordered_set<KT> rmap(const std::unordered_set<VT> &KSet) { |
| 231 |
KT V; |
| 232 |
std::unordered_set<KT> VSet; |
| 233 |
for (auto &I : KSet) |
| 234 |
if (SPIRVMap<KT, VT>::rfind(I, &V)) |
| 235 |
VSet.insert(V); |
| 236 |
return VSet; |
| 237 |
} |
| 238 |
|
| 239 |
template <typename KT, typename VT> |
| 240 |
std::set<KT> rmap(const std::set<VT> &KSet) { |
| 241 |
KT V; |
| 242 |
std::set<KT> VSet; |
| 243 |
for (auto &I : KSet) |
| 244 |
if (SPIRVMap<KT, VT>::rfind(I, &V)) |
| 245 |
VSet.insert(V); |
| 246 |
return VSet; |
| 247 |
} |
| 248 |
|
| 249 |
template <typename KT, typename VT, typename Any> |
| 250 |
std::set<KT> rmap(const std::map<VT, Any> &KMap) { |
| 251 |
KT V; |
| 252 |
std::set<KT> VSet; |
| 253 |
for (auto &I : KMap) |
| 254 |
if (SPIRVMap<KT, VT>::rfind(I.first, &V)) |
| 255 |
VSet.insert(V); |
| 256 |
|
| 257 |
return VSet; |
| 258 |
} |
| 259 |
|
| 260 |
template <typename K> std::string getName(K Key) { |
| 261 |
std::string Name; |
| 262 |
if (SPIRVMap<K, std::string>::find(Key, &Name)) |
| 263 |
return Name; |
| 264 |
return ""; |
| 265 |
} |
| 266 |
|
| 267 |
template <typename K> bool getByName(const std::string &Name, K &Key) { |
| 268 |
return SPIRVMap<K, std::string>::rfind(Name, &Key); |
| 269 |
} |
| 270 |
|
| 271 |
// Add a number as a string to a string |
| 272 |
template <class T> std::string concat(const std::string &S, const T &N) { |
| 273 |
std::stringstream Ss; |
| 274 |
Ss << S << N; |
| 275 |
return Ss.str(); |
| 276 |
} |
| 277 |
|
| 278 |
inline std::string concat(const std::string &S1, const std::string &S2, |
| 279 |
char Delim = ' ') { |
| 280 |
std::string S; |
| 281 |
if (S1.empty()) |
| 282 |
S = S2; |
| 283 |
else if (!S2.empty()) |
| 284 |
S = S1 + Delim + S2; |
| 285 |
return S; |
| 286 |
} |
| 287 |
|
| 288 |
inline std::string operator+(const std::string &S, int N) { |
| 289 |
return concat(S, N); |
| 290 |
} |
| 291 |
|
| 292 |
inline std::string operator+(const std::string &S, unsigned N) { |
| 293 |
return concat(S, N); |
| 294 |
} |
| 295 |
|
| 296 |
template <typename T> std::string getStr(const T &C, char Delim = ' ') { |
| 297 |
std::stringstream SS; |
| 298 |
bool First = true; |
| 299 |
for (auto &I : C) { |
| 300 |
if (!First) |
| 301 |
SS << Delim; |
| 302 |
else |
| 303 |
First = false; |
| 304 |
SS << I; |
| 305 |
} |
| 306 |
return SS.str(); |
| 307 |
} |
| 308 |
|
| 309 |
template <class MapTy> unsigned mapBitMask(unsigned BM) { |
| 310 |
unsigned Res = 0; |
| 311 |
MapTy::foreach ([&](typename MapTy::KeyTy K, typename MapTy::ValueTy V) { |
| 312 |
Res |= BM & (unsigned)K ? (unsigned)V : 0; |
| 313 |
}); |
| 314 |
return Res; |
| 315 |
} |
| 316 |
|
| 317 |
template <class MapTy> unsigned rmapBitMask(unsigned BM) { |
| 318 |
unsigned Res = 0; |
| 319 |
MapTy::foreach ([&](typename MapTy::KeyTy K, typename MapTy::ValueTy V) { |
| 320 |
Res |= BM & (unsigned)V ? (unsigned)K : 0; |
| 321 |
}); |
| 322 |
return Res; |
| 323 |
} |
| 324 |
|
| 325 |
// Get the number of words used for encoding a string literal in SPIRV |
| 326 |
inline unsigned getSizeInWords(const std::string &Str) { |
| 327 |
assert(Str.length() / 4 + 1 <= std::numeric_limits<unsigned>::max()); |
| 328 |
return static_cast<unsigned>(Str.length() / 4 + 1); |
| 329 |
} |
| 330 |
|
| 331 |
inline std::string getString(std::vector<uint32_t>::const_iterator Begin, |
| 332 |
std::vector<uint32_t>::const_iterator End) { |
| 333 |
std::string Str = std::string(); |
| 334 |
for (auto I = Begin; I != End; ++I) { |
| 335 |
uint32_t Word = *I; |
| 336 |
for (unsigned J = 0u; J < 32u; J += 8u) { |
| 337 |
char Char = (char)((Word >> J) & 0xff); |
| 338 |
if (Char == '\0') |
| 339 |
return Str; |
| 340 |
Str += Char; |
| 341 |
} |
| 342 |
} |
| 343 |
return Str; |
| 344 |
} |
| 345 |
|
| 346 |
inline std::string getString(const std::vector<uint32_t> &V) { |
| 347 |
return getString(V.cbegin(), V.cend()); |
| 348 |
} |
| 349 |
|
| 350 |
// if vector of Literals is expected to contain more than one Literal String |
| 351 |
inline std::vector<std::string> getVecString(const std::vector<uint32_t> &V) { |
| 352 |
std::vector<std::string> Result; |
| 353 |
std::string Str; |
| 354 |
for (auto It = V.cbegin(); It < V.cend(); It += getSizeInWords(Str)) { |
| 355 |
Str.clear(); |
| 356 |
Str = getString(It, V.cend()); |
| 357 |
Result.push_back(Str); |
| 358 |
} |
| 359 |
return Result; |
| 360 |
} |
| 361 |
|
| 362 |
inline std::vector<uint32_t> getVec(const std::string &Str) { |
| 363 |
std::vector<uint32_t> V; |
| 364 |
auto StrSize = Str.size(); |
| 365 |
uint32_t CurrentWord = 0u; |
| 366 |
for (unsigned I = 0u; I < StrSize; ++I) { |
| 367 |
if (I % 4u == 0u && I != 0u) { |
| 368 |
V.push_back(CurrentWord); |
| 369 |
CurrentWord = 0u; |
| 370 |
} |
| 371 |
assert(Str[I] && "0 is not allowed in string"); |
| 372 |
CurrentWord += ((uint32_t)Str[I]) << ((I % 4u) * 8u); |
| 373 |
} |
| 374 |
if (CurrentWord != 0u) |
| 375 |
V.push_back(CurrentWord); |
| 376 |
if (StrSize % 4 == 0) |
| 377 |
V.push_back(0); |
| 378 |
return V; |
| 379 |
} |
| 380 |
|
| 381 |
template <typename T> inline std::set<T> getSet(T Op1) { |
| 382 |
std::set<T> S; |
| 383 |
S.insert(Op1); |
| 384 |
return S; |
| 385 |
} |
| 386 |
|
| 387 |
template <typename T> inline std::vector<T> getVec(T Op1) { |
| 388 |
std::vector<T> V; |
| 389 |
V.push_back(Op1); |
| 390 |
return V; |
| 391 |
} |
| 392 |
|
| 393 |
template <typename T> inline std::vector<T> getVec(T Op1, T Op2) { |
| 394 |
std::vector<T> V; |
| 395 |
V.push_back(Op1); |
| 396 |
V.push_back(Op2); |
| 397 |
return V; |
| 398 |
} |
| 399 |
|
| 400 |
template <typename T> inline std::vector<T> getVec(T Op1, T Op2, T Op3) { |
| 401 |
std::vector<T> V; |
| 402 |
V.push_back(Op1); |
| 403 |
V.push_back(Op2); |
| 404 |
V.push_back(Op3); |
| 405 |
return V; |
| 406 |
} |
| 407 |
|
| 408 |
template <typename T> |
| 409 |
inline std::vector<T> getVec(T Op1, const std::vector<T> &Ops2) { |
| 410 |
std::vector<T> V; |
| 411 |
V.push_back(Op1); |
| 412 |
V.insert(V.end(), Ops2.begin(), Ops2.end()); |
| 413 |
return V; |
| 414 |
} |
| 415 |
|
| 416 |
template <typename MapTy, typename FuncTy> |
| 417 |
typename MapTy::mapped_type |
| 418 |
getOrInsert(MapTy &Map, typename MapTy::key_type Key, FuncTy Func) { |
| 419 |
typename MapTy::iterator Loc = Map.find(Key); |
| 420 |
if (Loc != Map.end()) |
| 421 |
return Loc->second; |
| 422 |
typename MapTy::mapped_type NF = Func(); |
| 423 |
Map[Key] = NF; |
| 424 |
return NF; |
| 425 |
} |
| 426 |
|
| 427 |
template <typename T> std::string toString(const T *Object) { |
| 428 |
if (Object == nullptr) |
| 429 |
return ""; |
| 430 |
std::string S; |
| 431 |
llvm::raw_string_ostream RSOS(S); |
| 432 |
Object->print(RSOS); |
| 433 |
RSOS.flush(); |
| 434 |
return S; |
| 435 |
} |
| 436 |
|
| 437 |
} // namespace SPIRV |
| 438 |
|
| 439 |
#endif // SPIRV_LIBSPIRV_SPIRVUTIL_H |