spirv-remap.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #include <iostream>
  36. #include <fstream>
  37. #include <cstring>
  38. #include <stdexcept>
  39. #include "../SPIRV/SPVRemapper.h"
  40. namespace {
  41. typedef unsigned int SpvWord;
  42. // Poor man's basename: given a complete path, return file portion.
  43. // E.g:
  44. // Linux: /foo/bar/test -> test
  45. // Win: c:\foo\bar\test -> test
  46. // It's not very efficient, but that doesn't matter for our minimal-duty use.
  47. // Using boost::filesystem would be better in many ways, but want to avoid that dependency.
  48. // OS dependent path separator (avoiding boost::filesystem dependency)
  49. #if defined(_WIN32)
  50. char path_sep_char() { return '\\'; }
  51. #else
  52. char path_sep_char() { return '/'; }
  53. #endif
  54. std::string basename(const std::string filename)
  55. {
  56. const size_t sepLoc = filename.find_last_of(path_sep_char());
  57. return (sepLoc == filename.npos) ? filename : filename.substr(sepLoc+1);
  58. }
  59. void errHandler(const std::string& str) {
  60. std::cout << str << std::endl;
  61. exit(5);
  62. }
  63. void logHandler(const std::string& str) {
  64. std::cout << str << std::endl;
  65. }
  66. // Read word stream from disk
  67. void read(std::vector<SpvWord>& spv, const std::string& inFilename, int verbosity)
  68. {
  69. std::ifstream fp;
  70. if (verbosity > 0)
  71. logHandler(std::string(" reading: ") + inFilename);
  72. spv.clear();
  73. fp.open(inFilename, std::fstream::in | std::fstream::binary);
  74. if (fp.fail())
  75. errHandler("error opening file for read: ");
  76. // Reserve space (for efficiency, not for correctness)
  77. fp.seekg(0, fp.end);
  78. spv.reserve(size_t(fp.tellg()) / sizeof(SpvWord));
  79. fp.seekg(0, fp.beg);
  80. while (!fp.eof()) {
  81. SpvWord inWord;
  82. fp.read((char *)&inWord, sizeof(inWord));
  83. if (!fp.eof()) {
  84. spv.push_back(inWord);
  85. if (fp.fail())
  86. errHandler(std::string("error reading file: ") + inFilename);
  87. }
  88. }
  89. }
  90. void write(std::vector<SpvWord>& spv, const std::string& outFile, int verbosity)
  91. {
  92. if (outFile.empty())
  93. errHandler("missing output filename.");
  94. std::ofstream fp;
  95. if (verbosity > 0)
  96. logHandler(std::string(" writing: ") + outFile);
  97. fp.open(outFile, std::fstream::out | std::fstream::binary);
  98. if (fp.fail())
  99. errHandler(std::string("error opening file for write: ") + outFile);
  100. for (auto it = spv.cbegin(); it != spv.cend(); ++it) {
  101. SpvWord word = *it;
  102. fp.write((char *)&word, sizeof(word));
  103. if (fp.fail())
  104. errHandler(std::string("error writing file: ") + outFile);
  105. }
  106. // file is closed by destructor
  107. }
  108. // Print helpful usage message to stdout, and exit
  109. void usage(const char* const name, const char* const msg = 0)
  110. {
  111. if (msg)
  112. std::cout << msg << std::endl << std::endl;
  113. std::cout << "Usage: " << std::endl;
  114. std::cout << " " << basename(name)
  115. << " [-v[v[...]] | --verbose [int]]"
  116. << " [--map (all|types|names|funcs)]"
  117. << " [--dce (all|types|funcs)]"
  118. << " [--opt (all|loadstore)]"
  119. << " [--strip-all | --strip all | -s]"
  120. << " [--do-everything]"
  121. << " --input | -i file1 [file2...] --output|-o DESTDIR"
  122. << std::endl;
  123. std::cout << " " << basename(name) << " [--version | -V]" << std::endl;
  124. std::cout << " " << basename(name) << " [--help | -?]" << std::endl;
  125. exit(5);
  126. }
  127. // grind through each SPIR in turn
  128. void execute(const std::vector<std::string>& inputFile, const std::string& outputDir,
  129. int opts, int verbosity)
  130. {
  131. for (auto it = inputFile.cbegin(); it != inputFile.cend(); ++it) {
  132. const std::string &filename = *it;
  133. std::vector<SpvWord> spv;
  134. read(spv, filename, verbosity);
  135. spv::spirvbin_t(verbosity).remap(spv, opts);
  136. const std::string outfile = outputDir + path_sep_char() + basename(filename);
  137. write(spv, outfile, verbosity);
  138. }
  139. if (verbosity > 0)
  140. std::cout << "Done: " << inputFile.size() << " file(s) processed" << std::endl;
  141. }
  142. // Parse command line options
  143. void parseCmdLine(int argc, char** argv, std::vector<std::string>& inputFile,
  144. std::string& outputDir,
  145. int& options,
  146. int& verbosity)
  147. {
  148. if (argc < 2)
  149. usage(argv[0]);
  150. verbosity = 0;
  151. options = spv::spirvbin_t::NONE;
  152. // Parse command line.
  153. // boost::program_options would be quite a bit nicer, but we don't want to
  154. // introduce a dependency on boost.
  155. for (int a=1; a<argc; ) {
  156. const std::string arg = argv[a];
  157. if (arg == "--output" || arg == "-o") {
  158. // Output directory
  159. if (++a >= argc)
  160. usage(argv[0], "--output requires an argument");
  161. if (!outputDir.empty())
  162. usage(argv[0], "--output can be provided only once");
  163. outputDir = argv[a++];
  164. // Remove trailing directory separator characters
  165. while (!outputDir.empty() && outputDir.back() == path_sep_char())
  166. outputDir.pop_back();
  167. }
  168. else if (arg == "-vv") { verbosity = 2; ++a; } // verbosity shortcuts
  169. else if (arg == "-vvv") { verbosity = 3; ++a; } // ...
  170. else if (arg == "-vvvv") { verbosity = 4; ++a; } // ...
  171. else if (arg == "-vvvvv") { verbosity = 5; ++a; } // ...
  172. else if (arg == "--verbose" || arg == "-v") {
  173. ++a;
  174. verbosity = 1;
  175. if (a < argc) {
  176. char* end_ptr = 0;
  177. int verb = ::strtol(argv[a], &end_ptr, 10);
  178. // If we have not read to the end of the string or
  179. // the string contained no elements, then we do not want to
  180. // store the value.
  181. if (*end_ptr == '\0' && end_ptr != argv[a]) {
  182. verbosity = verb;
  183. ++a;
  184. }
  185. }
  186. }
  187. else if (arg == "--version" || arg == "-V") {
  188. std::cout << basename(argv[0]) << " version 0.97 " << __DATE__ << " " << __TIME__ << std::endl;
  189. exit(0);
  190. } else if (arg == "--input" || arg == "-i") {
  191. // Collect input files
  192. for (++a; a < argc && argv[a][0] != '-'; ++a)
  193. inputFile.push_back(argv[a]);
  194. } else if (arg == "--do-everything") {
  195. ++a;
  196. options = options | spv::spirvbin_t::DO_EVERYTHING;
  197. } else if (arg == "--strip-all" || arg == "-s") {
  198. ++a;
  199. options = options | spv::spirvbin_t::STRIP;
  200. } else if (arg == "--strip") {
  201. ++a;
  202. if (strncmp(argv[a], "all", 3) == 0) {
  203. options = options | spv::spirvbin_t::STRIP;
  204. ++a;
  205. }
  206. } else if (arg == "--dce") {
  207. // Parse comma (or colon, etc) separated list of things to dce
  208. ++a;
  209. for (const char* c = argv[a]; *c; ++c) {
  210. if (strncmp(c, "all", 3) == 0) {
  211. options = (options | spv::spirvbin_t::DCE_ALL);
  212. c += 3;
  213. } else if (strncmp(c, "*", 1) == 0) {
  214. options = (options | spv::spirvbin_t::DCE_ALL);
  215. c += 1;
  216. } else if (strncmp(c, "funcs", 5) == 0) {
  217. options = (options | spv::spirvbin_t::DCE_FUNCS);
  218. c += 5;
  219. } else if (strncmp(c, "types", 5) == 0) {
  220. options = (options | spv::spirvbin_t::DCE_TYPES);
  221. c += 5;
  222. }
  223. }
  224. ++a;
  225. } else if (arg == "--map") {
  226. // Parse comma (or colon, etc) separated list of things to map
  227. ++a;
  228. for (const char* c = argv[a]; *c; ++c) {
  229. if (strncmp(c, "all", 3) == 0) {
  230. options = (options | spv::spirvbin_t::MAP_ALL);
  231. c += 3;
  232. } else if (strncmp(c, "*", 1) == 0) {
  233. options = (options | spv::spirvbin_t::MAP_ALL);
  234. c += 1;
  235. } else if (strncmp(c, "types", 5) == 0) {
  236. options = (options | spv::spirvbin_t::MAP_TYPES);
  237. c += 5;
  238. } else if (strncmp(c, "names", 5) == 0) {
  239. options = (options | spv::spirvbin_t::MAP_NAMES);
  240. c += 5;
  241. } else if (strncmp(c, "funcs", 5) == 0) {
  242. options = (options | spv::spirvbin_t::MAP_FUNCS);
  243. c += 5;
  244. }
  245. }
  246. ++a;
  247. } else if (arg == "--opt") {
  248. ++a;
  249. for (const char* c = argv[a]; *c; ++c) {
  250. if (strncmp(c, "all", 3) == 0) {
  251. options = (options | spv::spirvbin_t::OPT_ALL);
  252. c += 3;
  253. } else if (strncmp(c, "*", 1) == 0) {
  254. options = (options | spv::spirvbin_t::OPT_ALL);
  255. c += 1;
  256. } else if (strncmp(c, "loadstore", 9) == 0) {
  257. options = (options | spv::spirvbin_t::OPT_LOADSTORE);
  258. c += 9;
  259. }
  260. }
  261. ++a;
  262. } else if (arg == "--help" || arg == "-?") {
  263. usage(argv[0]);
  264. } else {
  265. usage(argv[0], "Unknown command line option");
  266. }
  267. }
  268. }
  269. } // namespace
  270. int main(int argc, char** argv)
  271. {
  272. std::vector<std::string> inputFile;
  273. std::string outputDir;
  274. int opts;
  275. int verbosity;
  276. #ifdef use_cpp11
  277. // handle errors by exiting
  278. spv::spirvbin_t::registerErrorHandler(errHandler);
  279. // Log messages to std::cout
  280. spv::spirvbin_t::registerLogHandler(logHandler);
  281. #endif
  282. if (argc < 2)
  283. usage(argv[0]);
  284. parseCmdLine(argc, argv, inputFile, outputDir, opts, verbosity);
  285. if (outputDir.empty())
  286. usage(argv[0], "Output directory required");
  287. std::string errmsg;
  288. // Main operations: read, remap, and write.
  289. execute(inputFile, outputDir, opts, verbosity);
  290. // If we get here, everything went OK! Nothing more to be done.
  291. }