blob: 97e3f31fa652cea1cac516b20c4fb140c971eb9c [file] [log] [blame]
John Kessenich140f3df2015-06-26 16:58:36 -06001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2015 LunarG, Inc.
John Kessenich140f3df2015-06-26 16:58:36 -06003//
John Kessenich927608b2017-01-06 12:34:14 -07004// All rights reserved.
John Kessenich140f3df2015-06-26 16:58:36 -06005//
John Kessenich927608b2017-01-06 12:34:14 -07006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions
8// are met:
John Kessenich140f3df2015-06-26 16:58:36 -06009//
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//
John Kessenich927608b2017-01-06 12:34:14 -070022// 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.
John Kessenich140f3df2015-06-26 16:58:36 -060034//
35
36#ifndef SPIRVREMAPPER_H
37#define SPIRVREMAPPER_H
38
39#include <string>
40#include <vector>
John Kessenich66ec80e2016-08-05 14:04:23 -060041#include <cstdlib>
LoopDawg8004d362017-09-17 10:38:52 -060042#include <exception>
John Kessenich140f3df2015-06-26 16:58:36 -060043
44namespace spv {
45
46// MSVC defines __cplusplus as an older value, even when it supports almost all of 11.
47// We handle that here by making our own symbol.
48#if __cplusplus >= 201103L || _MSC_VER >= 1700
49# define use_cpp11 1
50#endif
51
52class spirvbin_base_t
53{
54public:
55 enum Options {
56 NONE = 0,
57 STRIP = (1<<0),
58 MAP_TYPES = (1<<1),
59 MAP_NAMES = (1<<2),
60 MAP_FUNCS = (1<<3),
61 DCE_FUNCS = (1<<4),
62 DCE_VARS = (1<<5),
63 DCE_TYPES = (1<<6),
64 OPT_LOADSTORE = (1<<7),
65 OPT_FWD_LS = (1<<8), // EXPERIMENTAL: PRODUCES INVALID SCHEMA-0 SPIRV
66 MAP_ALL = (MAP_TYPES | MAP_NAMES | MAP_FUNCS),
67 DCE_ALL = (DCE_FUNCS | DCE_VARS | DCE_TYPES),
68 OPT_ALL = (OPT_LOADSTORE),
69
70 ALL_BUT_STRIP = (MAP_ALL | DCE_ALL | OPT_ALL),
71 DO_EVERYTHING = (STRIP | ALL_BUT_STRIP)
72 };
73};
74
75} // namespace SPV
76
77#if !defined (use_cpp11)
John Kessenich66ec80e2016-08-05 14:04:23 -060078#include <cstdio>
baldurkff160f12016-10-13 19:24:42 +020079#include <cstdint>
John Kessenich140f3df2015-06-26 16:58:36 -060080
81namespace spv {
82class spirvbin_t : public spirvbin_base_t
83{
84public:
85 spirvbin_t(int /*verbose = 0*/) { }
86
steve-lunarga8456412016-08-17 16:18:06 -060087 void remap(std::vector<std::uint32_t>& /*spv*/, unsigned int /*opts = 0*/)
John Kessenich140f3df2015-06-26 16:58:36 -060088 {
89 printf("Tool not compiled for C++11, which is required for SPIR-V remapping.\n");
90 exit(5);
91 }
92};
93
94} // namespace SPV
95
96#else // defined (use_cpp11)
97
98#include <functional>
99#include <cstdint>
100#include <unordered_map>
101#include <unordered_set>
102#include <map>
103#include <set>
104#include <cassert>
105
John Kessenich5e4b1242015-08-06 22:53:06 -0600106#include "spirv.hpp"
Felix Kaaman61deffd2015-07-02 15:25:23 +0200107#include "spvIR.h"
John Kessenich140f3df2015-06-26 16:58:36 -0600108
109namespace spv {
110
111// class to hold SPIR-V binary data for remapping, DCE, and debug stripping
112class spirvbin_t : public spirvbin_base_t
113{
114public:
LoopDawg8004d362017-09-17 10:38:52 -0600115 spirvbin_t(int verbose = 0) : entryPoint(spv::NoResult), largestNewId(0), verbose(verbose), errorLatch(false)
116 { }
117
Alex Szpakowski844dd452017-01-08 17:57:21 -0400118 virtual ~spirvbin_t() { }
John Kessenichecba76f2017-01-06 00:34:48 -0700119
John Kessenich140f3df2015-06-26 16:58:36 -0600120 // remap on an existing binary in memory
121 void remap(std::vector<std::uint32_t>& spv, std::uint32_t opts = DO_EVERYTHING);
122
123 // Type for error/log handler functions
124 typedef std::function<void(const std::string&)> errorfn_t;
125 typedef std::function<void(const std::string&)> logfn_t;
126
127 // Register error/log handling functions (can be lambda fn / functor / etc)
128 static void registerErrorHandler(errorfn_t handler) { errorHandler = handler; }
129 static void registerLogHandler(logfn_t handler) { logHandler = handler; }
130
131protected:
132 // This can be overridden to provide other message behavior if needed
133 virtual void msg(int minVerbosity, int indent, const std::string& txt) const;
134
135private:
136 // Local to global, or global to local ID map
137 typedef std::unordered_map<spv::Id, spv::Id> idmap_t;
138 typedef std::unordered_set<spv::Id> idset_t;
GregF8548bab2016-02-01 16:44:57 -0700139 typedef std::unordered_map<spv::Id, int> blockmap_t;
John Kessenich140f3df2015-06-26 16:58:36 -0600140
141 void remap(std::uint32_t opts = DO_EVERYTHING);
142
143 // Map of names to IDs
144 typedef std::unordered_map<std::string, spv::Id> namemap_t;
145
146 typedef std::uint32_t spirword_t;
147
148 typedef std::pair<unsigned, unsigned> range_t;
149 typedef std::function<void(spv::Id&)> idfn_t;
150 typedef std::function<bool(spv::Op, unsigned start)> instfn_t;
151
152 // Special Values for ID map:
153 static const spv::Id unmapped; // unchanged from default value
154 static const spv::Id unused; // unused ID
155 static const int header_size; // SPIR header = 5 words
156
157 class id_iterator_t;
158
159 // For mapping type entries between different shaders
160 typedef std::vector<spirword_t> typeentry_t;
161 typedef std::map<spv::Id, typeentry_t> globaltypes_t;
162
163 // A set that preserves position order, and a reverse map
164 typedef std::set<int> posmap_t;
165 typedef std::unordered_map<spv::Id, int> posmap_rev_t;
166
steve-lunarg811d9f42016-08-17 08:33:49 -0600167 // Maps and ID to the size of its base type, if known.
168 typedef std::unordered_map<spv::Id, unsigned> typesize_map_t;
169
John Kessenich140f3df2015-06-26 16:58:36 -0600170 // handle error
LoopDawg8004d362017-09-17 10:38:52 -0600171 void error(const std::string& txt) const { errorLatch = true; errorHandler(txt); }
John Kessenich140f3df2015-06-26 16:58:36 -0600172
steve-lunarg811d9f42016-08-17 08:33:49 -0600173 bool isConstOp(spv::Op opCode) const;
174 bool isTypeOp(spv::Op opCode) const;
175 bool isStripOp(spv::Op opCode) const;
176 bool isFlowCtrl(spv::Op opCode) const;
177 range_t literalRange(spv::Op opCode) const;
178 range_t typeRange(spv::Op opCode) const;
179 range_t constRange(spv::Op opCode) const;
180 unsigned typeSizeInWords(spv::Id id) const;
181 unsigned idTypeSizeInWords(spv::Id id) const;
John Kessenichecba76f2017-01-06 00:34:48 -0700182
John Kessenich140f3df2015-06-26 16:58:36 -0600183 spv::Id& asId(unsigned word) { return spv[word]; }
184 const spv::Id& asId(unsigned word) const { return spv[word]; }
185 spv::Op asOpCode(unsigned word) const { return opOpCode(spv[word]); }
186 std::uint32_t asOpCodeHash(unsigned word);
187 spv::Decoration asDecoration(unsigned word) const { return spv::Decoration(spv[word]); }
188 unsigned asWordCount(unsigned word) const { return opWordCount(spv[word]); }
189 spv::Id asTypeConstId(unsigned word) const { return asId(word + (isTypeOp(asOpCode(word)) ? 1 : 2)); }
steve-lunarg811d9f42016-08-17 08:33:49 -0600190 unsigned idPos(spv::Id id) const;
John Kessenich140f3df2015-06-26 16:58:36 -0600191
steve-lunarg811d9f42016-08-17 08:33:49 -0600192 static unsigned opWordCount(spirword_t data) { return data >> spv::WordCountShift; }
193 static spv::Op opOpCode(spirword_t data) { return spv::Op(data & spv::OpCodeMask); }
John Kessenich140f3df2015-06-26 16:58:36 -0600194
195 // Header access & set methods
196 spirword_t magic() const { return spv[0]; } // return magic number
197 spirword_t bound() const { return spv[3]; } // return Id bound from header
198 spirword_t bound(spirword_t b) { return spv[3] = b; };
199 spirword_t genmagic() const { return spv[2]; } // generator magic
200 spirword_t genmagic(spirword_t m) { return spv[2] = m; }
201 spirword_t schemaNum() const { return spv[4]; } // schema number from header
202
203 // Mapping fns: get
204 spv::Id localId(spv::Id id) const { return idMapL[id]; }
205
206 // Mapping fns: set
207 inline spv::Id localId(spv::Id id, spv::Id newId);
208 void countIds(spv::Id id);
209
210 // Return next unused new local ID.
211 // NOTE: boost::dynamic_bitset would be more efficient due to find_next(),
212 // which std::vector<bool> doens't have.
213 inline spv::Id nextUnusedId(spv::Id id);
214
215 void buildLocalMaps();
216 std::string literalString(unsigned word) const; // Return literal as a std::string
217 int literalStringWords(const std::string& str) const { return (int(str.size())+4)/4; }
218
219 bool isNewIdMapped(spv::Id newId) const { return isMapped(newId); }
220 bool isOldIdUnmapped(spv::Id oldId) const { return localId(oldId) == unmapped; }
221 bool isOldIdUnused(spv::Id oldId) const { return localId(oldId) == unused; }
222 bool isOldIdMapped(spv::Id oldId) const { return !isOldIdUnused(oldId) && !isOldIdUnmapped(oldId); }
223 bool isFunction(spv::Id oldId) const { return fnPos.find(oldId) != fnPos.end(); }
224
225 // bool matchType(const globaltypes_t& globalTypes, spv::Id lt, spv::Id gt) const;
226 // spv::Id findType(const globaltypes_t& globalTypes, spv::Id lt) const;
227 std::uint32_t hashType(unsigned typeStart) const;
228
229 spirvbin_t& process(instfn_t, idfn_t, unsigned begin = 0, unsigned end = 0);
230 int processInstruction(unsigned word, instfn_t, idfn_t);
231
232 void validate() const;
233 void mapTypeConst();
234 void mapFnBodies();
235 void optLoadStore();
236 void dceFuncs();
237 void dceVars();
238 void dceTypes();
239 void mapNames();
240 void foldIds(); // fold IDs to smallest space
241 void forwardLoadStores(); // load store forwarding (EXPERIMENTAL)
242 void offsetIds(); // create relative offset IDs
243
244 void applyMap(); // remap per local name map
245 void mapRemainder(); // map any IDs we haven't touched yet
steve-lunarg297754c2016-12-09 11:13:23 -0700246 void stripDebug(); // strip all debug info
247 void stripDeadRefs(); // strips debug info for now-dead references after DCE
John Kessenich140f3df2015-06-26 16:58:36 -0600248 void strip(); // remove debug symbols
John Kessenichecba76f2017-01-06 00:34:48 -0700249
John Kessenich140f3df2015-06-26 16:58:36 -0600250 std::vector<spirword_t> spv; // SPIR words
251
252 namemap_t nameMap; // ID names from OpName
253
254 // Since we want to also do binary ops, we can't use std::vector<bool>. we could use
255 // boost::dynamic_bitset, but we're trying to avoid a boost dependency.
256 typedef std::uint64_t bits_t;
257 std::vector<bits_t> mapped; // which new IDs have been mapped
258 static const int mBits = sizeof(bits_t) * 4;
259
260 bool isMapped(spv::Id id) const { return id < maxMappedId() && ((mapped[id/mBits] & (1LL<<(id%mBits))) != 0); }
261 void setMapped(spv::Id id) { resizeMapped(id); mapped[id/mBits] |= (1LL<<(id%mBits)); }
262 void resizeMapped(spv::Id id) { if (id >= maxMappedId()) mapped.resize(id/mBits+1, 0); }
263 size_t maxMappedId() const { return mapped.size() * mBits; }
264
265 // Add a strip range for a given instruction starting at 'start'
266 // Note: avoiding brace initializers to please older versions os MSVC.
267 void stripInst(unsigned start) { stripRange.push_back(range_t(start, start + asWordCount(start))); }
268
269 // Function start and end. use unordered_map because we'll have
270 // many fewer functions than IDs.
271 std::unordered_map<spv::Id, range_t> fnPos;
John Kessenich140f3df2015-06-26 16:58:36 -0600272
273 // Which functions are called, anywhere in the module, with a call count
274 std::unordered_map<spv::Id, int> fnCalls;
John Kessenichecba76f2017-01-06 00:34:48 -0700275
steve-lunarg811d9f42016-08-17 08:33:49 -0600276 posmap_t typeConstPos; // word positions that define types & consts (ordered)
277 posmap_rev_t idPosR; // reverse map from IDs to positions
278 typesize_map_t idTypeSizeMap; // maps each ID to its type size, if known.
John Kessenichecba76f2017-01-06 00:34:48 -0700279
John Kessenich140f3df2015-06-26 16:58:36 -0600280 std::vector<spv::Id> idMapL; // ID {M}ap from {L}ocal to {G}lobal IDs
281
282 spv::Id entryPoint; // module entry point
283 spv::Id largestNewId; // biggest new ID we have mapped anything to
284
285 // Sections of the binary to strip, given as [begin,end)
286 std::vector<range_t> stripRange;
287
288 // processing options:
289 std::uint32_t options;
290 int verbose; // verbosity level
291
LoopDawg8004d362017-09-17 10:38:52 -0600292 // Error latch: this is set if the error handler is ever executed. It would be better to
293 // use a try/catch block and throw, but that's not desired for certain environments, so
294 // this is the alternative.
295 mutable bool errorLatch;
296
John Kessenich140f3df2015-06-26 16:58:36 -0600297 static errorfn_t errorHandler;
298 static logfn_t logHandler;
299};
300
301} // namespace SPV
302
303#endif // defined (use_cpp11)
304#endif // SPIRVREMAPPER_H