SPVRemapper.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // Copyright (C) 2015 LunarG, Inc.
  3. //
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. //
  10. // Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. //
  13. // Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. //
  18. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  19. // contributors may be used to endorse or promote products derived
  20. // from this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. // POSSIBILITY OF SUCH DAMAGE.
  34. //
  35. #ifndef SPIRVREMAPPER_H
  36. #define SPIRVREMAPPER_H
  37. #include <string>
  38. #include <vector>
  39. #include <cstdlib>
  40. #include <exception>
  41. namespace spv {
  42. class spirvbin_base_t
  43. {
  44. public:
  45. enum Options {
  46. NONE = 0,
  47. STRIP = (1<<0),
  48. MAP_TYPES = (1<<1),
  49. MAP_NAMES = (1<<2),
  50. MAP_FUNCS = (1<<3),
  51. DCE_FUNCS = (1<<4),
  52. DCE_VARS = (1<<5),
  53. DCE_TYPES = (1<<6),
  54. OPT_LOADSTORE = (1<<7),
  55. OPT_FWD_LS = (1<<8), // EXPERIMENTAL: PRODUCES INVALID SCHEMA-0 SPIRV
  56. MAP_ALL = (MAP_TYPES | MAP_NAMES | MAP_FUNCS),
  57. DCE_ALL = (DCE_FUNCS | DCE_VARS | DCE_TYPES),
  58. OPT_ALL = (OPT_LOADSTORE),
  59. ALL_BUT_STRIP = (MAP_ALL | DCE_ALL | OPT_ALL),
  60. DO_EVERYTHING = (STRIP | ALL_BUT_STRIP)
  61. };
  62. };
  63. } // namespace SPV
  64. #include <functional>
  65. #include <cstdint>
  66. #include <unordered_map>
  67. #include <unordered_set>
  68. #include <map>
  69. #include <set>
  70. #include <cassert>
  71. #include "spirv.hpp"
  72. namespace spv {
  73. const Id NoResult = 0;
  74. // class to hold SPIR-V binary data for remapping, DCE, and debug stripping
  75. class spirvbin_t : public spirvbin_base_t
  76. {
  77. public:
  78. spirvbin_t(int verbose = 0) : entryPoint(spv::NoResult), largestNewId(0), verbose(verbose), errorLatch(false)
  79. { }
  80. virtual ~spirvbin_t() { }
  81. // remap on an existing binary in memory
  82. void remap(std::vector<std::uint32_t>& spv, const std::vector<std::string>& whiteListStrings,
  83. std::uint32_t opts = DO_EVERYTHING);
  84. // remap on an existing binary in memory - legacy interface without white list
  85. void remap(std::vector<std::uint32_t>& spv, std::uint32_t opts = DO_EVERYTHING);
  86. // Type for error/log handler functions
  87. typedef std::function<void(const std::string&)> errorfn_t;
  88. typedef std::function<void(const std::string&)> logfn_t;
  89. // Register error/log handling functions (can be lambda fn / functor / etc)
  90. static void registerErrorHandler(errorfn_t handler) { errorHandler = handler; }
  91. static void registerLogHandler(logfn_t handler) { logHandler = handler; }
  92. protected:
  93. // This can be overridden to provide other message behavior if needed
  94. virtual void msg(int minVerbosity, int indent, const std::string& txt) const;
  95. private:
  96. // Local to global, or global to local ID map
  97. typedef std::unordered_map<spv::Id, spv::Id> idmap_t;
  98. typedef std::unordered_set<spv::Id> idset_t;
  99. typedef std::unordered_map<spv::Id, int> blockmap_t;
  100. void remap(std::uint32_t opts = DO_EVERYTHING);
  101. // Map of names to IDs
  102. typedef std::unordered_map<std::string, spv::Id> namemap_t;
  103. typedef std::uint32_t spirword_t;
  104. typedef std::pair<unsigned, unsigned> range_t;
  105. typedef std::function<void(spv::Id&)> idfn_t;
  106. typedef std::function<bool(spv::Op, unsigned start)> instfn_t;
  107. // Special Values for ID map:
  108. static const spv::Id unmapped; // unchanged from default value
  109. static const spv::Id unused; // unused ID
  110. static const int header_size; // SPIR header = 5 words
  111. class id_iterator_t;
  112. // For mapping type entries between different shaders
  113. typedef std::vector<spirword_t> typeentry_t;
  114. typedef std::map<spv::Id, typeentry_t> globaltypes_t;
  115. // A set that preserves position order, and a reverse map
  116. typedef std::set<int> posmap_t;
  117. typedef std::unordered_map<spv::Id, int> posmap_rev_t;
  118. // Maps and ID to the size of its base type, if known.
  119. typedef std::unordered_map<spv::Id, unsigned> typesize_map_t;
  120. // handle error
  121. void error(const std::string& txt) const { errorLatch = true; errorHandler(txt); }
  122. bool isConstOp(spv::Op opCode) const;
  123. bool isTypeOp(spv::Op opCode) const;
  124. bool isStripOp(spv::Op opCode) const;
  125. bool isFlowCtrl(spv::Op opCode) const;
  126. range_t literalRange(spv::Op opCode) const;
  127. range_t typeRange(spv::Op opCode) const;
  128. range_t constRange(spv::Op opCode) const;
  129. unsigned typeSizeInWords(spv::Id id) const;
  130. unsigned idTypeSizeInWords(spv::Id id) const;
  131. bool isStripOp(spv::Op opCode, unsigned start) const;
  132. spv::Id& asId(unsigned word) { return spv[word]; }
  133. const spv::Id& asId(unsigned word) const { return spv[word]; }
  134. spv::Op asOpCode(unsigned word) const { return opOpCode(spv[word]); }
  135. std::uint32_t asOpCodeHash(unsigned word);
  136. spv::Decoration asDecoration(unsigned word) const { return spv::Decoration(spv[word]); }
  137. unsigned asWordCount(unsigned word) const { return opWordCount(spv[word]); }
  138. spv::Id asTypeConstId(unsigned word) const { return asId(word + (isTypeOp(asOpCode(word)) ? 1 : 2)); }
  139. unsigned idPos(spv::Id id) const;
  140. static unsigned opWordCount(spirword_t data) { return data >> spv::WordCountShift; }
  141. static spv::Op opOpCode(spirword_t data) { return spv::Op(data & spv::OpCodeMask); }
  142. // Header access & set methods
  143. spirword_t magic() const { return spv[0]; } // return magic number
  144. spirword_t bound() const { return spv[3]; } // return Id bound from header
  145. spirword_t bound(spirword_t b) { return spv[3] = b; }
  146. spirword_t genmagic() const { return spv[2]; } // generator magic
  147. spirword_t genmagic(spirword_t m) { return spv[2] = m; }
  148. spirword_t schemaNum() const { return spv[4]; } // schema number from header
  149. // Mapping fns: get
  150. spv::Id localId(spv::Id id) const { return idMapL[id]; }
  151. // Mapping fns: set
  152. inline spv::Id localId(spv::Id id, spv::Id newId);
  153. void countIds(spv::Id id);
  154. // Return next unused new local ID.
  155. // NOTE: boost::dynamic_bitset would be more efficient due to find_next(),
  156. // which std::vector<bool> doens't have.
  157. inline spv::Id nextUnusedId(spv::Id id);
  158. void buildLocalMaps();
  159. std::string literalString(unsigned word) const; // Return literal as a std::string
  160. int literalStringWords(const std::string& str) const { return (int(str.size())+4)/4; }
  161. bool isNewIdMapped(spv::Id newId) const { return isMapped(newId); }
  162. bool isOldIdUnmapped(spv::Id oldId) const { return localId(oldId) == unmapped; }
  163. bool isOldIdUnused(spv::Id oldId) const { return localId(oldId) == unused; }
  164. bool isOldIdMapped(spv::Id oldId) const { return !isOldIdUnused(oldId) && !isOldIdUnmapped(oldId); }
  165. bool isFunction(spv::Id oldId) const { return fnPos.find(oldId) != fnPos.end(); }
  166. // bool matchType(const globaltypes_t& globalTypes, spv::Id lt, spv::Id gt) const;
  167. // spv::Id findType(const globaltypes_t& globalTypes, spv::Id lt) const;
  168. std::uint32_t hashType(unsigned typeStart) const;
  169. spirvbin_t& process(instfn_t, idfn_t, unsigned begin = 0, unsigned end = 0);
  170. int processInstruction(unsigned word, instfn_t, idfn_t);
  171. void validate() const;
  172. void mapTypeConst();
  173. void mapFnBodies();
  174. void optLoadStore();
  175. void dceFuncs();
  176. void dceVars();
  177. void dceTypes();
  178. void mapNames();
  179. void foldIds(); // fold IDs to smallest space
  180. void forwardLoadStores(); // load store forwarding (EXPERIMENTAL)
  181. void offsetIds(); // create relative offset IDs
  182. void applyMap(); // remap per local name map
  183. void mapRemainder(); // map any IDs we haven't touched yet
  184. void stripDebug(); // strip all debug info
  185. void stripDeadRefs(); // strips debug info for now-dead references after DCE
  186. void strip(); // remove debug symbols
  187. std::vector<spirword_t> spv; // SPIR words
  188. std::vector<std::string> stripWhiteList;
  189. namemap_t nameMap; // ID names from OpName
  190. // Since we want to also do binary ops, we can't use std::vector<bool>. we could use
  191. // boost::dynamic_bitset, but we're trying to avoid a boost dependency.
  192. typedef std::uint64_t bits_t;
  193. std::vector<bits_t> mapped; // which new IDs have been mapped
  194. static const int mBits = sizeof(bits_t) * 4;
  195. bool isMapped(spv::Id id) const { return id < maxMappedId() && ((mapped[id/mBits] & (1LL<<(id%mBits))) != 0); }
  196. void setMapped(spv::Id id) { resizeMapped(id); mapped[id/mBits] |= (1LL<<(id%mBits)); }
  197. void resizeMapped(spv::Id id) { if (id >= maxMappedId()) mapped.resize(id/mBits+1, 0); }
  198. size_t maxMappedId() const { return mapped.size() * mBits; }
  199. // Add a strip range for a given instruction starting at 'start'
  200. // Note: avoiding brace initializers to please older versions os MSVC.
  201. void stripInst(unsigned start) { stripRange.push_back(range_t(start, start + asWordCount(start))); }
  202. // Function start and end. use unordered_map because we'll have
  203. // many fewer functions than IDs.
  204. std::unordered_map<spv::Id, range_t> fnPos;
  205. // Which functions are called, anywhere in the module, with a call count
  206. std::unordered_map<spv::Id, int> fnCalls;
  207. posmap_t typeConstPos; // word positions that define types & consts (ordered)
  208. posmap_rev_t idPosR; // reverse map from IDs to positions
  209. typesize_map_t idTypeSizeMap; // maps each ID to its type size, if known.
  210. std::vector<spv::Id> idMapL; // ID {M}ap from {L}ocal to {G}lobal IDs
  211. spv::Id entryPoint; // module entry point
  212. spv::Id largestNewId; // biggest new ID we have mapped anything to
  213. // Sections of the binary to strip, given as [begin,end)
  214. std::vector<range_t> stripRange;
  215. // processing options:
  216. std::uint32_t options;
  217. int verbose; // verbosity level
  218. // Error latch: this is set if the error handler is ever executed. It would be better to
  219. // use a try/catch block and throw, but that's not desired for certain environments, so
  220. // this is the alternative.
  221. mutable bool errorLatch;
  222. static errorfn_t errorHandler;
  223. static logfn_t logHandler;
  224. };
  225. } // namespace SPV
  226. #endif // SPIRVREMAPPER_H