opt.cpp 36 KB

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