opt.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. // Copyright (c) 2016 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <algorithm>
  15. #include <cassert>
  16. #include <cstring>
  17. #include <fstream>
  18. #include <iostream>
  19. #include <memory>
  20. #include <sstream>
  21. #include <string>
  22. #include <vector>
  23. #include "source/opt/log.h"
  24. #include "source/spirv_target_env.h"
  25. #include "source/util/string_utils.h"
  26. #include "spirv-tools/libspirv.hpp"
  27. #include "spirv-tools/optimizer.hpp"
  28. #include "tools/io.h"
  29. #include "tools/util/cli_consumer.h"
  30. namespace {
  31. // Status and actions to perform after parsing command-line arguments.
  32. enum OptActions { OPT_CONTINUE, OPT_STOP };
  33. struct OptStatus {
  34. OptActions action;
  35. int code;
  36. };
  37. // Message consumer for this tool. Used to emit diagnostics during
  38. // initialization and setup. Note that |source| and |position| are irrelevant
  39. // here because we are still not processing a SPIR-V input file.
  40. void opt_diagnostic(spv_message_level_t level, const char* /*source*/,
  41. const spv_position_t& /*positon*/, const char* message) {
  42. if (level == SPV_MSG_ERROR) {
  43. fprintf(stderr, "error: ");
  44. }
  45. fprintf(stderr, "%s\n", message);
  46. }
  47. std::string GetListOfPassesAsString(const spvtools::Optimizer& optimizer) {
  48. std::stringstream ss;
  49. for (const auto& name : optimizer.GetPassNames()) {
  50. ss << "\n\t\t" << name;
  51. }
  52. return ss.str();
  53. }
  54. const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_5;
  55. std::string GetLegalizationPasses() {
  56. spvtools::Optimizer optimizer(kDefaultEnvironment);
  57. optimizer.RegisterLegalizationPasses();
  58. return GetListOfPassesAsString(optimizer);
  59. }
  60. std::string GetOptimizationPasses() {
  61. spvtools::Optimizer optimizer(kDefaultEnvironment);
  62. optimizer.RegisterPerformancePasses();
  63. return GetListOfPassesAsString(optimizer);
  64. }
  65. std::string GetSizePasses() {
  66. spvtools::Optimizer optimizer(kDefaultEnvironment);
  67. optimizer.RegisterSizePasses();
  68. return GetListOfPassesAsString(optimizer);
  69. }
  70. std::string GetVulkanToWebGPUPasses() {
  71. spvtools::Optimizer optimizer(SPV_ENV_VULKAN_1_1);
  72. optimizer.RegisterVulkanToWebGPUPasses();
  73. return GetListOfPassesAsString(optimizer);
  74. }
  75. std::string GetWebGPUToVulkanPasses() {
  76. spvtools::Optimizer optimizer(SPV_ENV_WEBGPU_0);
  77. optimizer.RegisterWebGPUToVulkanPasses();
  78. return GetListOfPassesAsString(optimizer);
  79. }
  80. void PrintUsage(const char* program) {
  81. std::string target_env_list = spvTargetEnvList(16, 80);
  82. // NOTE: Please maintain flags in lexicographical order.
  83. printf(
  84. R"(%s - Optimize a SPIR-V binary file.
  85. USAGE: %s [options] [<input>] -o <output>
  86. The SPIR-V binary is read from <input>. If no file is specified,
  87. or if <input> is "-", then the binary is read from standard input.
  88. if <output> is "-", then the optimized output is written to
  89. standard output.
  90. NOTE: The optimizer is a work in progress.
  91. Options (in lexicographical order):)",
  92. program, program);
  93. printf(R"(
  94. --amd-ext-to-khr
  95. Replaces the extensions VK_AMD_shader_ballot, VK_AMD_gcn_shader,
  96. and VK_AMD_shader_trinary_minmax with equivalant code using core
  97. instructions and capabilities.)");
  98. printf(R"(
  99. --ccp
  100. Apply the conditional constant propagation transform. This will
  101. propagate constant values throughout the program, and simplify
  102. expressions and conditional jumps with known predicate
  103. values. Performed on entry point call tree functions and
  104. exported functions.)");
  105. printf(R"(
  106. --cfg-cleanup
  107. Cleanup the control flow graph. This will remove any unnecessary
  108. code from the CFG like unreachable code. Performed on entry
  109. point call tree functions and exported functions.)");
  110. printf(R"(
  111. --combine-access-chains
  112. Combines chained access chains to produce a single instruction
  113. where possible.)");
  114. printf(R"(
  115. --compact-ids
  116. Remap result ids to a compact range starting from %%1 and without
  117. any gaps.)");
  118. printf(R"(
  119. --convert-local-access-chains
  120. Convert constant index access chain loads/stores into
  121. equivalent load/stores with inserts and extracts. Performed
  122. on function scope variables referenced only with load, store,
  123. and constant index access chains in entry point call tree
  124. functions.)");
  125. printf(R"(
  126. --convert-relaxed-to-half
  127. Convert all RelaxedPrecision arithmetic operations to half
  128. precision, inserting conversion operations where needed.
  129. Run after function scope variable load and store elimination
  130. for better results. Simplify-instructions, redundancy-elimination
  131. and DCE should be run after this pass to eliminate excess
  132. conversions. This conversion is useful when the target platform
  133. does not support RelaxedPrecision or ignores it. This pass also
  134. removes all RelaxedPrecision decorations.)");
  135. printf(R"(
  136. --copy-propagate-arrays
  137. Does propagation of memory references when an array is a copy of
  138. another. It will only propagate an array if the source is never
  139. written to, and the only store to the target is the copy.)");
  140. printf(R"(
  141. --decompose-initialized-variables
  142. Decomposes initialized variable declarations into a declaration
  143. followed by a store of the initial value. This is done to work
  144. around known issues with some Vulkan drivers for initialize
  145. variables.)");
  146. printf(R"(
  147. --descriptor-scalar-replacement
  148. Replaces every array variable |desc| that has a DescriptorSet
  149. and Binding decorations with a new variable for each element of
  150. the array. Suppose |desc| was bound at binding |b|. Then the
  151. variable corresponding to |desc[i]| will have binding |b+i|.
  152. The descriptor set will be the same. All accesses to |desc|
  153. must be in OpAccessChain instructions with a literal index for
  154. the first index.)");
  155. printf(R"(
  156. --eliminate-dead-branches
  157. Convert conditional branches with constant condition to the
  158. indicated unconditional brranch. Delete all resulting dead
  159. code. Performed only on entry point call tree functions.)");
  160. printf(R"(
  161. --eliminate-dead-code-aggressive
  162. Delete instructions which do not contribute to a function's
  163. output. Performed only on entry point call tree functions.)");
  164. printf(R"(
  165. --eliminate-dead-const
  166. Eliminate dead constants.)");
  167. printf(R"(
  168. --eliminate-dead-functions
  169. Deletes functions that cannot be reached from entry points or
  170. exported functions.)");
  171. printf(R"(
  172. --eliminate-dead-inserts
  173. Deletes unreferenced inserts into composites, most notably
  174. unused stores to vector components, that are not removed by
  175. aggressive dead code elimination.)");
  176. printf(R"(
  177. --eliminate-dead-variables
  178. Deletes module scope variables that are not referenced.)");
  179. printf(R"(
  180. --eliminate-insert-extract
  181. DEPRECATED. This pass has been replaced by the simplification
  182. pass, and that pass will be run instead.
  183. See --simplify-instructions.)");
  184. printf(R"(
  185. --eliminate-local-multi-store
  186. Replace stores and loads of function scope variables that are
  187. stored multiple times. Performed on variables referenceed only
  188. with loads and stores. Performed only on entry point call tree
  189. functions.)");
  190. printf(R"(
  191. --eliminate-local-single-block
  192. Perform single-block store/load and load/load elimination.
  193. Performed only on function scope variables in entry point
  194. call tree functions.)");
  195. printf(R"(
  196. --eliminate-local-single-store
  197. Replace stores and loads of function scope variables that are
  198. only stored once. Performed on variables referenceed only with
  199. loads and stores. Performed only on entry point call tree
  200. functions.)");
  201. printf(R"(
  202. --flatten-decorations
  203. Replace decoration groups with repeated OpDecorate and
  204. OpMemberDecorate instructions.)");
  205. printf(R"(
  206. --fold-spec-const-op-composite
  207. Fold the spec constants defined by OpSpecConstantOp or
  208. OpSpecConstantComposite instructions to front-end constants
  209. when possible.)");
  210. printf(R"(
  211. --freeze-spec-const
  212. Freeze the values of specialization constants to their default
  213. values.)");
  214. printf(R"(
  215. --graphics-robust-access
  216. Clamp indices used to access buffers and internal composite
  217. values, providing guarantees that satisfy Vulkan's
  218. robustBufferAccess rules.)");
  219. printf(R"(
  220. --generate-webgpu-initializers
  221. Adds initial values to OpVariable instructions that are missing
  222. them, due to their storage type requiring them for WebGPU.)");
  223. printf(R"(
  224. --if-conversion
  225. Convert if-then-else like assignments into OpSelect.)");
  226. printf(R"(
  227. --inline-entry-points-exhaustive
  228. Exhaustively inline all function calls in entry point call tree
  229. functions. Currently does not inline calls to functions with
  230. early return in a loop.)");
  231. printf(R"(
  232. --legalize-hlsl
  233. Runs a series of optimizations that attempts to take SPIR-V
  234. generated by an HLSL front-end and generates legal Vulkan SPIR-V.
  235. The optimizations are:
  236. %s
  237. Note this does not guarantee legal code. This option passes the
  238. option --relax-logical-pointer to the validator.)",
  239. GetLegalizationPasses().c_str());
  240. printf(R"(
  241. --legalize-vector-shuffle
  242. Converts any usages of 0xFFFFFFFF for the literals in
  243. OpVectorShuffle to a literal 0. This is done since 0xFFFFFFFF is
  244. forbidden in WebGPU.)");
  245. printf(R"(
  246. --local-redundancy-elimination
  247. Looks for instructions in the same basic block that compute the
  248. same value, and deletes the redundant ones.)");
  249. printf(R"(
  250. --loop-fission
  251. Splits any top level loops in which the register pressure has
  252. exceeded a given threshold. The threshold must follow the use of
  253. this flag and must be a positive integer value.)");
  254. printf(R"(
  255. --loop-fusion
  256. Identifies adjacent loops with the same lower and upper bound.
  257. If this is legal, then merge the loops into a single loop.
  258. Includes heuristics to ensure it does not increase number of
  259. registers too much, while reducing the number of loads from
  260. memory. Takes an additional positive integer argument to set
  261. the maximum number of registers.)");
  262. printf(R"(
  263. --loop-invariant-code-motion
  264. Identifies code in loops that has the same value for every
  265. iteration of the loop, and move it to the loop pre-header.)");
  266. printf(R"(
  267. --loop-unroll
  268. Fully unrolls loops marked with the Unroll flag)");
  269. printf(R"(
  270. --loop-unroll-partial
  271. Partially unrolls loops marked with the Unroll flag. Takes an
  272. additional non-0 integer argument to set the unroll factor, or
  273. how many times a loop body should be duplicated)");
  274. printf(R"(
  275. --loop-peeling
  276. Execute few first (respectively last) iterations before
  277. (respectively after) the loop if it can elide some branches.)");
  278. printf(R"(
  279. --loop-peeling-threshold
  280. Takes a non-0 integer argument to set the loop peeling code size
  281. growth threshold. The threshold prevents the loop peeling
  282. from happening if the code size increase created by
  283. the optimization is above the threshold.)");
  284. printf(R"(
  285. --max-id-bound=<n>
  286. Sets the maximum value for the id bound for the moudle. The
  287. default is the minimum value for this limit, 0x3FFFFF. See
  288. section 2.17 of the Spir-V specification.)");
  289. printf(R"(
  290. --merge-blocks
  291. Join two blocks into a single block if the second has the
  292. first as its only predecessor. Performed only on entry point
  293. call tree functions.)");
  294. printf(R"(
  295. --merge-return
  296. Changes functions that have multiple return statements so they
  297. have a single return statement.
  298. For structured control flow it is assumed that the only
  299. unreachable blocks in the function are trivial merge and continue
  300. blocks.
  301. A trivial merge block contains the label and an OpUnreachable
  302. instructions, nothing else. A trivial continue block contain a
  303. label and an OpBranch to the header, nothing else.
  304. These conditions are guaranteed to be met after running
  305. dead-branch elimination.)");
  306. printf(R"(
  307. --loop-unswitch
  308. Hoists loop-invariant conditionals out of loops by duplicating
  309. the loop on each branch of the conditional and adjusting each
  310. copy of the loop.)");
  311. printf(R"(
  312. -O
  313. Optimize for performance. Apply a sequence of transformations
  314. in an attempt to improve the performance of the generated
  315. code. For this version of the optimizer, this flag is equivalent
  316. to specifying the following optimization code names:
  317. %s)",
  318. GetOptimizationPasses().c_str());
  319. printf(R"(
  320. -Os
  321. Optimize for size. Apply a sequence of transformations in an
  322. attempt to minimize the size of the generated code. For this
  323. version of the optimizer, this flag is equivalent to specifying
  324. the following optimization code names:
  325. %s
  326. NOTE: The specific transformations done by -O and -Os change
  327. from release to release.)",
  328. GetSizePasses().c_str());
  329. printf(R"(
  330. -Oconfig=<file>
  331. Apply the sequence of transformations indicated in <file>.
  332. This file contains a sequence of strings separated by whitespace
  333. (tabs, newlines or blanks). Each string is one of the flags
  334. accepted by spirv-opt. Optimizations will be applied in the
  335. sequence they appear in the file. This is equivalent to
  336. specifying all the flags on the command line. For example,
  337. given the file opts.cfg with the content:
  338. --inline-entry-points-exhaustive
  339. --eliminate-dead-code-aggressive
  340. The following two invocations to spirv-opt are equivalent:
  341. $ spirv-opt -Oconfig=opts.cfg program.spv
  342. $ spirv-opt --inline-entry-points-exhaustive \
  343. --eliminate-dead-code-aggressive program.spv
  344. Lines starting with the character '#' in the configuration
  345. file indicate a comment and will be ignored.
  346. The -O, -Os, and -Oconfig flags act as macros. Using one of them
  347. is equivalent to explicitly inserting the underlying flags at
  348. that position in the command line. For example, the invocation
  349. 'spirv-opt --merge-blocks -O ...' applies the transformation
  350. --merge-blocks followed by all the transformations implied by
  351. -O.)");
  352. printf(R"(
  353. --preserve-bindings
  354. Ensure that the optimizer preserves all bindings declared within
  355. the module, even when those bindings are unused.)");
  356. printf(R"(
  357. --preserve-spec-constants
  358. Ensure that the optimizer preserves all specialization constants declared
  359. within the module, even when those constants are unused.)");
  360. printf(R"(
  361. --print-all
  362. Print SPIR-V assembly to standard error output before each pass
  363. and after the last pass.)");
  364. printf(R"(
  365. --private-to-local
  366. Change the scope of private variables that are used in a single
  367. function to that function.)");
  368. printf(R"(
  369. --reduce-load-size
  370. Replaces loads of composite objects where not every component is
  371. used by loads of just the elements that are used.)");
  372. printf(R"(
  373. --redundancy-elimination
  374. Looks for instructions in the same function that compute the
  375. same value, and deletes the redundant ones.)");
  376. printf(R"(
  377. --relax-float-ops
  378. Decorate all float operations with RelaxedPrecision if not already
  379. so decorated. This does not decorate types or variables.)");
  380. printf(R"(
  381. --relax-struct-store
  382. Allow store from one struct type to a different type with
  383. compatible layout and members. This option is forwarded to the
  384. validator.)");
  385. printf(R"(
  386. --remove-duplicates
  387. Removes duplicate types, decorations, capabilities and extension
  388. instructions.)");
  389. printf(R"(
  390. --replace-invalid-opcode
  391. Replaces instructions whose opcode is valid for shader modules,
  392. but not for the current shader stage. To have an effect, all
  393. entry points must have the same execution model.)");
  394. printf(R"(
  395. --ssa-rewrite
  396. Replace loads and stores to function local variables with
  397. operations on SSA IDs.)");
  398. printf(R"(
  399. --scalar-replacement[=<n>]
  400. Replace aggregate function scope variables that are only accessed
  401. via their elements with new function variables representing each
  402. element. <n> is a limit on the size of the aggragates that will
  403. be replaced. 0 means there is no limit. The default value is
  404. 100.)");
  405. printf(R"(
  406. --set-spec-const-default-value "<spec id>:<default value> ..."
  407. Set the default values of the specialization constants with
  408. <spec id>:<default value> pairs specified in a double-quoted
  409. string. <spec id>:<default value> pairs must be separated by
  410. blank spaces, and in each pair, spec id and default value must
  411. be separated with colon ':' without any blank spaces in between.
  412. e.g.: --set-spec-const-default-value "1:100 2:400")");
  413. printf(R"(
  414. --simplify-instructions
  415. Will simplify all instructions in the function as much as
  416. possible.)");
  417. printf(R"(
  418. --split-invalid-unreachable
  419. Attempts to legalize for WebGPU cases where an unreachable
  420. merge-block is also a continue-target by splitting it into two
  421. seperate blocks. There exist legal, for Vulkan, instances of this
  422. pattern that cannot be converted into legal WebGPU, so this
  423. conversion may not succeed.)");
  424. printf(R"(
  425. --skip-validation
  426. Will not validate the SPIR-V before optimizing. If the SPIR-V
  427. is invalid, the optimizer may fail or generate incorrect code.
  428. This options should be used rarely, and with caution.)");
  429. printf(R"(
  430. --strength-reduction
  431. Replaces instructions with equivalent and less expensive ones.)");
  432. printf(R"(
  433. --strip-atomic-counter-memory
  434. Removes AtomicCountMemory bit from memory semantics values.)");
  435. printf(R"(
  436. --strip-debug
  437. Remove all debug instructions.)");
  438. printf(R"(
  439. --strip-reflect
  440. Remove all reflection information. For now, this covers
  441. reflection information defined by SPV_GOOGLE_hlsl_functionality1.)");
  442. printf(R"(
  443. --target-env=<env>
  444. Set the target environment. Without this flag the target
  445. enviroment defaults to spv1.3. <env> must be one of
  446. {%s})",
  447. target_env_list.c_str());
  448. printf(R"(
  449. --time-report
  450. Print the resource utilization of each pass (e.g., CPU time,
  451. RSS) to standard error output. Currently it supports only Unix
  452. systems. This option is the same as -ftime-report in GCC. It
  453. prints CPU/WALL/USR/SYS time (and RSS if possible), but note that
  454. USR/SYS time are returned by getrusage() and can have a small
  455. error.)");
  456. printf(R"(
  457. --upgrade-memory-model
  458. Upgrades the Logical GLSL450 memory model to Logical VulkanKHR.
  459. Transforms memory, image, atomic and barrier operations to conform
  460. to that model's requirements.)");
  461. printf(R"(
  462. --vector-dce
  463. This pass looks for components of vectors that are unused, and
  464. removes them from the vector. Note this would still leave around
  465. lots of dead code that a pass of ADCE will be able to remove.)");
  466. printf(R"(
  467. --vulkan-to-webgpu
  468. Turns on the prescribed passes for converting from Vulkan to
  469. WebGPU and sets the target environment to webgpu0. Other passes
  470. may be turned on via additional flags, but such combinations are
  471. not tested.
  472. Using --target-env with this flag is not allowed.
  473. This flag is the equivalent of passing in --target-env=webgpu0
  474. and specifying the following optimization code names:
  475. %s
  476. NOTE: This flag is a WIP and its behaviour is subject to change.)",
  477. GetVulkanToWebGPUPasses().c_str());
  478. printf(R"(
  479. --webgpu-to-vulkan
  480. Turns on the prescribed passes for converting from WebGPU to
  481. Vulkan and sets the target environment to vulkan1.1. Other passes
  482. may be turned on via additional flags, but such combinations are
  483. not tested.
  484. Using --target-env with this flag is not allowed.
  485. This flag is the equivalent of passing in --target-env=vulkan1.1
  486. and specifying the following optimization code names:
  487. %s
  488. NOTE: This flag is a WIP and its behaviour is subject to change.)",
  489. GetWebGPUToVulkanPasses().c_str());
  490. printf(R"(
  491. --workaround-1209
  492. Rewrites instructions for which there are known driver bugs to
  493. avoid triggering those bugs.
  494. Current workarounds: Avoid OpUnreachable in loops.)");
  495. printf(R"(
  496. --unify-const
  497. Remove the duplicated constants.)");
  498. printf(R"(
  499. --validate-after-all
  500. Validate the module after each pass is performed.)");
  501. printf(R"(
  502. -h, --help
  503. Print this help.)");
  504. printf(R"(
  505. --version
  506. Display optimizer version information.
  507. )");
  508. }
  509. // Reads command-line flags the file specified in |oconfig_flag|. This string
  510. // is assumed to have the form "-Oconfig=FILENAME". This function parses the
  511. // string and extracts the file name after the '=' sign.
  512. //
  513. // Flags found in |FILENAME| are pushed at the end of the vector |file_flags|.
  514. //
  515. // This function returns true on success, false on failure.
  516. bool ReadFlagsFromFile(const char* oconfig_flag,
  517. std::vector<std::string>* file_flags) {
  518. const char* fname = strchr(oconfig_flag, '=');
  519. if (fname == nullptr || fname[0] != '=') {
  520. spvtools::Errorf(opt_diagnostic, nullptr, {}, "Invalid -Oconfig flag %s",
  521. oconfig_flag);
  522. return false;
  523. }
  524. fname++;
  525. std::ifstream input_file;
  526. input_file.open(fname);
  527. if (input_file.fail()) {
  528. spvtools::Errorf(opt_diagnostic, nullptr, {}, "Could not open file '%s'",
  529. fname);
  530. return false;
  531. }
  532. std::string line;
  533. while (std::getline(input_file, line)) {
  534. // Ignore empty lines and lines starting with the comment marker '#'.
  535. if (line.length() == 0 || line[0] == '#') {
  536. continue;
  537. }
  538. // Tokenize the line. Add all found tokens to the list of found flags. This
  539. // mimics the way the shell will parse whitespace on the command line. NOTE:
  540. // This does not support quoting and it is not intended to.
  541. std::istringstream iss(line);
  542. while (!iss.eof()) {
  543. std::string flag;
  544. iss >> flag;
  545. file_flags->push_back(flag);
  546. }
  547. }
  548. return true;
  549. }
  550. OptStatus ParseFlags(int argc, const char** argv,
  551. spvtools::Optimizer* optimizer, const char** in_file,
  552. const char** out_file,
  553. spvtools::ValidatorOptions* validator_options,
  554. spvtools::OptimizerOptions* optimizer_options);
  555. // Parses and handles the -Oconfig flag. |prog_name| contains the name of
  556. // the spirv-opt binary (used to build a new argv vector for the recursive
  557. // invocation to ParseFlags). |opt_flag| contains the -Oconfig=FILENAME flag.
  558. // |optimizer|, |in_file|, |out_file|, |validator_options|, and
  559. // |optimizer_options| are as in ParseFlags.
  560. //
  561. // This returns the same OptStatus instance returned by ParseFlags.
  562. OptStatus ParseOconfigFlag(const char* prog_name, const char* opt_flag,
  563. spvtools::Optimizer* optimizer, const char** in_file,
  564. const char** out_file,
  565. spvtools::ValidatorOptions* validator_options,
  566. spvtools::OptimizerOptions* optimizer_options) {
  567. std::vector<std::string> flags;
  568. flags.push_back(prog_name);
  569. std::vector<std::string> file_flags;
  570. if (!ReadFlagsFromFile(opt_flag, &file_flags)) {
  571. spvtools::Error(opt_diagnostic, nullptr, {},
  572. "Could not read optimizer flags from configuration file");
  573. return {OPT_STOP, 1};
  574. }
  575. flags.insert(flags.end(), file_flags.begin(), file_flags.end());
  576. const char** new_argv = new const char*[flags.size()];
  577. for (size_t i = 0; i < flags.size(); i++) {
  578. if (flags[i].find("-Oconfig=") != std::string::npos) {
  579. spvtools::Error(
  580. opt_diagnostic, nullptr, {},
  581. "Flag -Oconfig= may not be used inside the configuration file");
  582. return {OPT_STOP, 1};
  583. }
  584. new_argv[i] = flags[i].c_str();
  585. }
  586. auto ret_val =
  587. ParseFlags(static_cast<int>(flags.size()), new_argv, optimizer, in_file,
  588. out_file, validator_options, optimizer_options);
  589. delete[] new_argv;
  590. return ret_val;
  591. }
  592. // Canonicalize the flag in |argv[argi]| of the form '--pass arg' into
  593. // '--pass=arg'. The optimizer only accepts arguments to pass names that use the
  594. // form '--pass_name=arg'. Since spirv-opt also accepts the other form, this
  595. // function makes the necessary conversion.
  596. //
  597. // Pass flags that require additional arguments should be handled here. Note
  598. // that additional arguments should be given as a single string. If the flag
  599. // requires more than one argument, the pass creator in
  600. // Optimizer::GetPassFromFlag() should parse it accordingly (e.g., see the
  601. // handler for --set-spec-const-default-value).
  602. //
  603. // If the argument requests one of the passes that need an additional argument,
  604. // |argi| is modified to point past the current argument, and the string
  605. // "argv[argi]=argv[argi + 1]" is returned. Otherwise, |argi| is unmodified and
  606. // the string "|argv[argi]|" is returned.
  607. std::string CanonicalizeFlag(const char** argv, int argc, int* argi) {
  608. const char* cur_arg = argv[*argi];
  609. const char* next_arg = (*argi + 1 < argc) ? argv[*argi + 1] : nullptr;
  610. std::ostringstream canonical_arg;
  611. canonical_arg << cur_arg;
  612. // NOTE: DO NOT ADD NEW FLAGS HERE.
  613. //
  614. // These flags are supported for backwards compatibility. When adding new
  615. // passes that need extra arguments in its command-line flag, please make them
  616. // use the syntax "--pass_name[=pass_arg].
  617. if (0 == strcmp(cur_arg, "--set-spec-const-default-value") ||
  618. 0 == strcmp(cur_arg, "--loop-fission") ||
  619. 0 == strcmp(cur_arg, "--loop-fusion") ||
  620. 0 == strcmp(cur_arg, "--loop-unroll-partial") ||
  621. 0 == strcmp(cur_arg, "--loop-peeling-threshold")) {
  622. if (next_arg) {
  623. canonical_arg << "=" << next_arg;
  624. ++(*argi);
  625. }
  626. }
  627. return canonical_arg.str();
  628. }
  629. // Parses command-line flags. |argc| contains the number of command-line flags.
  630. // |argv| points to an array of strings holding the flags. |optimizer| is the
  631. // Optimizer instance used to optimize the program.
  632. //
  633. // On return, this function stores the name of the input program in |in_file|.
  634. // The name of the output file in |out_file|. The return value indicates whether
  635. // optimization should continue and a status code indicating an error or
  636. // success.
  637. OptStatus ParseFlags(int argc, const char** argv,
  638. spvtools::Optimizer* optimizer, const char** in_file,
  639. const char** out_file,
  640. spvtools::ValidatorOptions* validator_options,
  641. spvtools::OptimizerOptions* optimizer_options) {
  642. std::vector<std::string> pass_flags;
  643. bool target_env_set = false;
  644. bool vulkan_to_webgpu_set = false;
  645. bool webgpu_to_vulkan_set = false;
  646. for (int argi = 1; argi < argc; ++argi) {
  647. const char* cur_arg = argv[argi];
  648. if ('-' == cur_arg[0]) {
  649. if (0 == strcmp(cur_arg, "--version")) {
  650. spvtools::Logf(opt_diagnostic, SPV_MSG_INFO, nullptr, {}, "%s\n",
  651. spvSoftwareVersionDetailsString());
  652. return {OPT_STOP, 0};
  653. } else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) {
  654. PrintUsage(argv[0]);
  655. return {OPT_STOP, 0};
  656. } else if (0 == strcmp(cur_arg, "-o")) {
  657. if (!*out_file && argi + 1 < argc) {
  658. *out_file = argv[++argi];
  659. } else {
  660. PrintUsage(argv[0]);
  661. return {OPT_STOP, 1};
  662. }
  663. } else if ('\0' == cur_arg[1]) {
  664. // Setting a filename of "-" to indicate stdin.
  665. if (!*in_file) {
  666. *in_file = cur_arg;
  667. } else {
  668. spvtools::Error(opt_diagnostic, nullptr, {},
  669. "More than one input file specified");
  670. return {OPT_STOP, 1};
  671. }
  672. } else if (0 == strncmp(cur_arg, "-Oconfig=", sizeof("-Oconfig=") - 1)) {
  673. OptStatus status =
  674. ParseOconfigFlag(argv[0], cur_arg, optimizer, in_file, out_file,
  675. validator_options, optimizer_options);
  676. if (status.action != OPT_CONTINUE) {
  677. return status;
  678. }
  679. } else if (0 == strcmp(cur_arg, "--skip-validation")) {
  680. optimizer_options->set_run_validator(false);
  681. } else if (0 == strcmp(cur_arg, "--print-all")) {
  682. optimizer->SetPrintAll(&std::cerr);
  683. } else if (0 == strcmp(cur_arg, "--preserve-bindings")) {
  684. optimizer_options->set_preserve_bindings(true);
  685. } else if (0 == strcmp(cur_arg, "--preserve-spec-constants")) {
  686. optimizer_options->set_preserve_spec_constants(true);
  687. } else if (0 == strcmp(cur_arg, "--time-report")) {
  688. optimizer->SetTimeReport(&std::cerr);
  689. } else if (0 == strcmp(cur_arg, "--relax-struct-store")) {
  690. validator_options->SetRelaxStructStore(true);
  691. } else if (0 == strncmp(cur_arg, "--max-id-bound=",
  692. sizeof("--max-id-bound=") - 1)) {
  693. auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg);
  694. // Will not allow values in the range [2^31,2^32).
  695. uint32_t max_id_bound =
  696. static_cast<uint32_t>(atoi(split_flag.second.c_str()));
  697. // That SPIR-V mandates the minimum value for max id bound but
  698. // implementations may allow higher minimum bounds.
  699. if (max_id_bound < kDefaultMaxIdBound) {
  700. spvtools::Error(opt_diagnostic, nullptr, {},
  701. "The max id bound must be at least 0x3FFFFF");
  702. return {OPT_STOP, 1};
  703. }
  704. optimizer_options->set_max_id_bound(max_id_bound);
  705. validator_options->SetUniversalLimit(spv_validator_limit_max_id_bound,
  706. max_id_bound);
  707. } else if (0 == strncmp(cur_arg,
  708. "--target-env=", sizeof("--target-env=") - 1)) {
  709. target_env_set = true;
  710. if (vulkan_to_webgpu_set) {
  711. spvtools::Error(opt_diagnostic, nullptr, {},
  712. "--vulkan-to-webgpu defines the target environment, "
  713. "so --target-env cannot be set at the same time");
  714. return {OPT_STOP, 1};
  715. }
  716. if (webgpu_to_vulkan_set) {
  717. spvtools::Error(opt_diagnostic, nullptr, {},
  718. "--webgpu-to-vulkan defines the target environment, "
  719. "so --target-env cannot be set at the same time");
  720. return {OPT_STOP, 1};
  721. }
  722. const auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg);
  723. const auto target_env_str = split_flag.second.c_str();
  724. spv_target_env target_env;
  725. if (!spvParseTargetEnv(target_env_str, &target_env)) {
  726. spvtools::Error(opt_diagnostic, nullptr, {},
  727. "Invalid value passed to --target-env");
  728. return {OPT_STOP, 1};
  729. }
  730. optimizer->SetTargetEnv(target_env);
  731. } else if (0 == strcmp(cur_arg, "--vulkan-to-webgpu")) {
  732. vulkan_to_webgpu_set = true;
  733. if (target_env_set) {
  734. spvtools::Error(opt_diagnostic, nullptr, {},
  735. "--vulkan-to-webgpu defines the target environment, "
  736. "so --target-env cannot be set at the same time");
  737. return {OPT_STOP, 1};
  738. }
  739. if (webgpu_to_vulkan_set) {
  740. spvtools::Error(opt_diagnostic, nullptr, {},
  741. "Cannot use both --webgpu-to-vulkan and "
  742. "--vulkan-to-webgpu at the same time, invoke twice "
  743. "if you are wanting to go to and from");
  744. return {OPT_STOP, 1};
  745. }
  746. optimizer->SetTargetEnv(SPV_ENV_VULKAN_1_1);
  747. optimizer->RegisterVulkanToWebGPUPasses();
  748. } else if (0 == strcmp(cur_arg, "--webgpu-to-vulkan")) {
  749. webgpu_to_vulkan_set = true;
  750. if (target_env_set) {
  751. spvtools::Error(opt_diagnostic, nullptr, {},
  752. "--webgpu-to-vulkan defines the target environment, "
  753. "so --target-env cannot be set at the same time");
  754. return {OPT_STOP, 1};
  755. }
  756. if (vulkan_to_webgpu_set) {
  757. spvtools::Error(opt_diagnostic, nullptr, {},
  758. "Cannot use both --webgpu-to-vulkan and "
  759. "--vulkan-to-webgpu at the same time, invoke twice "
  760. "if you are wanting to go to and from");
  761. return {OPT_STOP, 1};
  762. }
  763. optimizer->SetTargetEnv(SPV_ENV_WEBGPU_0);
  764. optimizer->RegisterWebGPUToVulkanPasses();
  765. } else if (0 == strcmp(cur_arg, "--validate-after-all")) {
  766. optimizer->SetValidateAfterAll(true);
  767. } else {
  768. // Some passes used to accept the form '--pass arg', canonicalize them
  769. // to '--pass=arg'.
  770. pass_flags.push_back(CanonicalizeFlag(argv, argc, &argi));
  771. // If we were requested to legalize SPIR-V generated from the HLSL
  772. // front-end, skip validation.
  773. if (0 == strcmp(cur_arg, "--legalize-hlsl")) {
  774. validator_options->SetBeforeHlslLegalization(true);
  775. }
  776. }
  777. } else {
  778. if (!*in_file) {
  779. *in_file = cur_arg;
  780. } else {
  781. spvtools::Error(opt_diagnostic, nullptr, {},
  782. "More than one input file specified");
  783. return {OPT_STOP, 1};
  784. }
  785. }
  786. }
  787. if (!optimizer->RegisterPassesFromFlags(pass_flags)) {
  788. return {OPT_STOP, 1};
  789. }
  790. return {OPT_CONTINUE, 0};
  791. }
  792. } // namespace
  793. int main(int argc, const char** argv) {
  794. const char* in_file = nullptr;
  795. const char* out_file = nullptr;
  796. spv_target_env target_env = kDefaultEnvironment;
  797. spvtools::Optimizer optimizer(target_env);
  798. optimizer.SetMessageConsumer(spvtools::utils::CLIMessageConsumer);
  799. spvtools::ValidatorOptions validator_options;
  800. spvtools::OptimizerOptions optimizer_options;
  801. OptStatus status = ParseFlags(argc, argv, &optimizer, &in_file, &out_file,
  802. &validator_options, &optimizer_options);
  803. optimizer_options.set_validator_options(validator_options);
  804. if (status.action == OPT_STOP) {
  805. return status.code;
  806. }
  807. if (out_file == nullptr) {
  808. spvtools::Error(opt_diagnostic, nullptr, {}, "-o required");
  809. return 1;
  810. }
  811. std::vector<uint32_t> binary;
  812. if (!ReadFile<uint32_t>(in_file, "rb", &binary)) {
  813. return 1;
  814. }
  815. // By using the same vector as input and output, we save time in the case
  816. // that there was no change.
  817. bool ok =
  818. optimizer.Run(binary.data(), binary.size(), &binary, optimizer_options);
  819. if (!WriteFile<uint32_t>(out_file, "wb", binary.data(), binary.size())) {
  820. return 1;
  821. }
  822. return ok ? 0 : 1;
  823. }