spirv-remap.cpp 15 KB

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