2016-06-29 20:16:03 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2016-06-29 20:16:03 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-29 20:16:03 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-06-29 20:16:03 +00:00
|
|
|
|
2017-04-02 06:36:35 +00:00
|
|
|
#include <algorithm>
|
2017-08-30 18:19:22 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
2016-06-29 20:16:03 +00:00
|
|
|
#include <iostream>
|
2016-09-22 15:50:12 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
2016-06-29 20:16:03 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/log.h"
|
2018-12-17 21:54:23 +00:00
|
|
|
#include "source/spirv_target_env.h"
|
2018-09-10 15:49:41 +00:00
|
|
|
#include "source/util/string_utils.h"
|
|
|
|
#include "spirv-tools/libspirv.hpp"
|
2017-04-02 06:36:35 +00:00
|
|
|
#include "spirv-tools/optimizer.hpp"
|
2016-06-30 18:24:04 +00:00
|
|
|
#include "tools/io.h"
|
2018-08-08 15:16:19 +00:00
|
|
|
#include "tools/util/cli_consumer.h"
|
2016-06-29 20:16:03 +00:00
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Status and actions to perform after parsing command-line arguments.
|
|
|
|
enum OptActions { OPT_CONTINUE, OPT_STOP };
|
|
|
|
|
|
|
|
struct OptStatus {
|
|
|
|
OptActions action;
|
|
|
|
int code;
|
|
|
|
};
|
|
|
|
|
2018-07-25 19:21:44 +00:00
|
|
|
// Message consumer for this tool. Used to emit diagnostics during
|
|
|
|
// initialization and setup. Note that |source| and |position| are irrelevant
|
|
|
|
// here because we are still not processing a SPIR-V input file.
|
|
|
|
void opt_diagnostic(spv_message_level_t level, const char* /*source*/,
|
|
|
|
const spv_position_t& /*positon*/, const char* message) {
|
|
|
|
if (level == SPV_MSG_ERROR) {
|
|
|
|
fprintf(stderr, "error: ");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s\n", message);
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
std::string GetListOfPassesAsString(const spvtools::Optimizer& optimizer) {
|
|
|
|
std::stringstream ss;
|
|
|
|
for (const auto& name : optimizer.GetPassNames()) {
|
|
|
|
ss << "\n\t\t" << name;
|
|
|
|
}
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2018-02-09 19:29:02 +00:00
|
|
|
const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_3;
|
|
|
|
|
2017-12-19 20:08:54 +00:00
|
|
|
std::string GetLegalizationPasses() {
|
2018-02-09 19:29:02 +00:00
|
|
|
spvtools::Optimizer optimizer(kDefaultEnvironment);
|
2017-12-19 20:08:54 +00:00
|
|
|
optimizer.RegisterLegalizationPasses();
|
|
|
|
return GetListOfPassesAsString(optimizer);
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
std::string GetOptimizationPasses() {
|
2018-02-09 19:29:02 +00:00
|
|
|
spvtools::Optimizer optimizer(kDefaultEnvironment);
|
2017-08-30 18:19:22 +00:00
|
|
|
optimizer.RegisterPerformancePasses();
|
|
|
|
return GetListOfPassesAsString(optimizer);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetSizePasses() {
|
2018-02-09 19:29:02 +00:00
|
|
|
spvtools::Optimizer optimizer(kDefaultEnvironment);
|
2017-08-30 18:19:22 +00:00
|
|
|
optimizer.RegisterSizePasses();
|
|
|
|
return GetListOfPassesAsString(optimizer);
|
|
|
|
}
|
|
|
|
|
2016-06-29 20:16:03 +00:00
|
|
|
void PrintUsage(const char* program) {
|
2017-12-05 16:28:12 +00:00
|
|
|
// NOTE: Please maintain flags in lexicographical order.
|
2016-06-29 20:16:03 +00:00
|
|
|
printf(
|
|
|
|
R"(%s - Optimize a SPIR-V binary file.
|
|
|
|
|
|
|
|
USAGE: %s [options] [<input>] -o <output>
|
|
|
|
|
|
|
|
The SPIR-V binary is read from <input>. If no file is specified,
|
|
|
|
or if <input> is "-", then the binary is read from standard input.
|
|
|
|
if <output> is "-", then the optimized output is written to
|
|
|
|
standard output.
|
|
|
|
|
|
|
|
NOTE: The optimizer is a work in progress.
|
|
|
|
|
2017-12-05 16:28:12 +00:00
|
|
|
Options (in lexicographical order):
|
2017-12-05 16:39:25 +00:00
|
|
|
--ccp
|
|
|
|
Apply the conditional constant propagation transform. This will
|
|
|
|
propagate constant values throughout the program, and simplify
|
|
|
|
expressions and conditional jumps with known predicate
|
|
|
|
values. Performed on entry point call tree functions and
|
|
|
|
exported functions.
|
2017-09-06 12:56:41 +00:00
|
|
|
--cfg-cleanup
|
|
|
|
Cleanup the control flow graph. This will remove any unnecessary
|
|
|
|
code from the CFG like unreachable code. Performed on entry
|
|
|
|
point call tree functions and exported functions.
|
2018-07-23 15:23:11 +00:00
|
|
|
--combine-access-chains
|
|
|
|
Combines chained access chains to produce a single instruction
|
|
|
|
where possible.
|
2017-12-05 16:28:12 +00:00
|
|
|
--compact-ids
|
|
|
|
Remap result ids to a compact range starting from %%1 and without
|
|
|
|
any gaps.
|
2017-08-01 18:55:29 +00:00
|
|
|
--convert-local-access-chains
|
|
|
|
Convert constant index access chain loads/stores into
|
|
|
|
equivalent load/stores with inserts and extracts. Performed
|
|
|
|
on function scope variables referenced only with load, store,
|
2017-08-15 23:58:28 +00:00
|
|
|
and constant index access chains in entry point call tree
|
|
|
|
functions.
|
2018-03-21 03:33:24 +00:00
|
|
|
--copy-propagate-arrays
|
|
|
|
Does propagation of memory references when an array is a copy of
|
|
|
|
another. It will only propagate an array if the source is never
|
|
|
|
written to, and the only store to the target is the copy.
|
2017-07-03 23:23:04 +00:00
|
|
|
--eliminate-common-uniform
|
|
|
|
Perform load/load elimination for duplicate uniform values.
|
|
|
|
Converts any constant index access chain uniform loads into
|
|
|
|
its equivalent load and extract. Some loads will be moved
|
|
|
|
to facilitate sharing. Performed only on entry point
|
2017-08-15 23:58:28 +00:00
|
|
|
call tree functions.
|
2017-12-05 16:28:12 +00:00
|
|
|
--eliminate-dead-branches
|
|
|
|
Convert conditional branches with constant condition to the
|
|
|
|
indicated unconditional brranch. Delete all resulting dead
|
|
|
|
code. Performed only on entry point call tree functions.
|
|
|
|
--eliminate-dead-code-aggressive
|
|
|
|
Delete instructions which do not contribute to a function's
|
|
|
|
output. Performed only on entry point call tree functions.
|
|
|
|
--eliminate-dead-const
|
|
|
|
Eliminate dead constants.
|
|
|
|
--eliminate-dead-functions
|
|
|
|
Deletes functions that cannot be reached from entry points or
|
|
|
|
exported functions.
|
2018-08-03 22:08:46 +00:00
|
|
|
--eliminate-dead-inserts
|
2018-01-27 00:05:33 +00:00
|
|
|
Deletes unreferenced inserts into composites, most notably
|
|
|
|
unused stores to vector components, that are not removed by
|
|
|
|
aggressive dead code elimination.
|
2017-12-05 16:28:12 +00:00
|
|
|
--eliminate-dead-variables
|
|
|
|
Deletes module scope variables that are not referenced.
|
|
|
|
--eliminate-insert-extract
|
2018-05-31 03:50:07 +00:00
|
|
|
DEPRECATED. This pass has been replaced by the simplification
|
|
|
|
pass, and that pass will be run instead.
|
|
|
|
See --simplify-instructions.
|
2017-12-05 16:28:12 +00:00
|
|
|
--eliminate-local-multi-store
|
|
|
|
Replace stores and loads of function scope variables that are
|
|
|
|
stored multiple times. Performed on variables referenceed only
|
|
|
|
with loads and stores. Performed only on entry point call tree
|
|
|
|
functions.
|
2017-08-01 18:55:29 +00:00
|
|
|
--eliminate-local-single-block
|
|
|
|
Perform single-block store/load and load/load elimination.
|
|
|
|
Performed only on function scope variables in entry point
|
2017-08-15 23:58:28 +00:00
|
|
|
call tree functions.
|
2017-08-01 18:55:29 +00:00
|
|
|
--eliminate-local-single-store
|
|
|
|
Replace stores and loads of function scope variables that are
|
|
|
|
only stored once. Performed on variables referenceed only with
|
2017-08-15 23:58:28 +00:00
|
|
|
loads and stores. Performed only on entry point call tree
|
|
|
|
functions.
|
2017-12-05 16:28:12 +00:00
|
|
|
--flatten-decorations
|
|
|
|
Replace decoration groups with repeated OpDecorate and
|
|
|
|
OpMemberDecorate instructions.
|
|
|
|
--fold-spec-const-op-composite
|
|
|
|
Fold the spec constants defined by OpSpecConstantOp or
|
|
|
|
OpSpecConstantComposite instructions to front-end constants
|
|
|
|
when possible.
|
|
|
|
--freeze-spec-const
|
|
|
|
Freeze the values of specialization constants to their default
|
|
|
|
values.
|
2018-01-16 16:15:06 +00:00
|
|
|
--if-conversion
|
|
|
|
Convert if-then-else like assignments into OpSelect.
|
2017-12-05 16:28:12 +00:00
|
|
|
--inline-entry-points-exhaustive
|
|
|
|
Exhaustively inline all function calls in entry point call tree
|
|
|
|
functions. Currently does not inline calls to functions with
|
|
|
|
early return in a loop.
|
2017-12-19 20:08:54 +00:00
|
|
|
--legalize-hlsl
|
|
|
|
Runs a series of optimizations that attempts to take SPIR-V
|
2018-08-08 15:16:19 +00:00
|
|
|
generated by an HLSL front-end and generates legal Vulkan SPIR-V.
|
2017-12-19 20:08:54 +00:00
|
|
|
The optimizations are:
|
|
|
|
%s
|
|
|
|
|
2018-08-08 15:16:19 +00:00
|
|
|
Note this does not guarantee legal code. This option passes the
|
|
|
|
option --relax-logical-pointer to the validator.
|
2017-12-07 15:55:57 +00:00
|
|
|
--local-redundancy-elimination
|
|
|
|
Looks for instructions in the same basic block that compute the
|
|
|
|
same value, and deletes the redundant ones.
|
2018-04-23 20:01:12 +00:00
|
|
|
--loop-fission
|
2018-12-07 19:11:15 +00:00
|
|
|
Splits any top level loops in which the register pressure has
|
|
|
|
exceeded a given threshold. The threshold must follow the use of
|
|
|
|
this flag and must be a positive integer value.
|
2018-04-20 14:14:45 +00:00
|
|
|
--loop-fusion
|
|
|
|
Identifies adjacent loops with the same lower and upper bound.
|
|
|
|
If this is legal, then merge the loops into a single loop.
|
|
|
|
Includes heuristics to ensure it does not increase number of
|
|
|
|
registers too much, while reducing the number of loads from
|
|
|
|
memory. Takes an additional positive integer argument to set
|
|
|
|
the maximum number of registers.
|
2018-12-07 19:11:15 +00:00
|
|
|
--loop-invariant-code-motion
|
|
|
|
Identifies code in loops that has the same value for every
|
|
|
|
iteration of the loop, and move it to the loop pre-header.
|
2018-02-27 11:50:08 +00:00
|
|
|
--loop-unroll
|
|
|
|
Fully unrolls loops marked with the Unroll flag
|
|
|
|
--loop-unroll-partial
|
|
|
|
Partially unrolls loops marked with the Unroll flag. Takes an
|
|
|
|
additional non-0 integer argument to set the unroll factor, or
|
|
|
|
how many times a loop body should be duplicated
|
2018-03-29 11:22:42 +00:00
|
|
|
--loop-peeling
|
|
|
|
Execute few first (respectively last) iterations before
|
|
|
|
(respectively after) the loop if it can elide some branches.
|
|
|
|
--loop-peeling-threshold
|
|
|
|
Takes a non-0 integer argument to set the loop peeling code size
|
|
|
|
growth threshold. The threshold prevents the loop peeling
|
|
|
|
from happening if the code size increase created by
|
|
|
|
the optimization is above the threshold.
|
2018-09-10 15:49:41 +00:00
|
|
|
--max-id-bound=<n>
|
|
|
|
Sets the maximum value for the id bound for the moudle. The
|
|
|
|
default is the minimum value for this limit, 0x3FFFFF. See
|
|
|
|
section 2.17 of the Spir-V specification.
|
2017-08-01 18:55:29 +00:00
|
|
|
--merge-blocks
|
|
|
|
Join two blocks into a single block if the second has the
|
|
|
|
first as its only predecessor. Performed only on entry point
|
2017-08-15 23:58:28 +00:00
|
|
|
call tree functions.
|
2017-11-08 21:22:10 +00:00
|
|
|
--merge-return
|
2018-03-06 16:20:28 +00:00
|
|
|
Changes functions that have multiple return statements so they
|
|
|
|
have a single return statement.
|
|
|
|
|
|
|
|
For structured control flow it is assumed that the only
|
|
|
|
unreachable blocks in the function are trivial merge and continue
|
|
|
|
blocks.
|
|
|
|
|
|
|
|
A trivial merge block contains the label and an OpUnreachable
|
|
|
|
instructions, nothing else. A trivial continue block contain a
|
|
|
|
label and an OpBranch to the header, nothing else.
|
|
|
|
|
|
|
|
These conditions are guaranteed to be met after running
|
|
|
|
dead-branch elimination.
|
2018-02-12 21:42:15 +00:00
|
|
|
--loop-unswitch
|
|
|
|
Hoists loop-invariant conditionals out of loops by duplicating
|
|
|
|
the loop on each branch of the conditional and adjusting each
|
|
|
|
copy of the loop.
|
2017-08-30 18:19:22 +00:00
|
|
|
-O
|
|
|
|
Optimize for performance. Apply a sequence of transformations
|
|
|
|
in an attempt to improve the performance of the generated
|
|
|
|
code. For this version of the optimizer, this flag is equivalent
|
|
|
|
to specifying the following optimization code names:
|
|
|
|
%s
|
|
|
|
-Os
|
|
|
|
Optimize for size. Apply a sequence of transformations in an
|
|
|
|
attempt to minimize the size of the generated code. For this
|
|
|
|
version of the optimizer, this flag is equivalent to specifying
|
|
|
|
the following optimization code names:
|
|
|
|
%s
|
|
|
|
|
|
|
|
NOTE: The specific transformations done by -O and -Os change
|
|
|
|
from release to release.
|
|
|
|
-Oconfig=<file>
|
|
|
|
Apply the sequence of transformations indicated in <file>.
|
|
|
|
This file contains a sequence of strings separated by whitespace
|
|
|
|
(tabs, newlines or blanks). Each string is one of the flags
|
|
|
|
accepted by spirv-opt. Optimizations will be applied in the
|
|
|
|
sequence they appear in the file. This is equivalent to
|
|
|
|
specifying all the flags on the command line. For example,
|
|
|
|
given the file opts.cfg with the content:
|
|
|
|
|
|
|
|
--inline-entry-points-exhaustive
|
|
|
|
--eliminate-dead-code-aggressive
|
|
|
|
|
|
|
|
The following two invocations to spirv-opt are equivalent:
|
|
|
|
|
|
|
|
$ spirv-opt -Oconfig=opts.cfg program.spv
|
|
|
|
|
|
|
|
$ spirv-opt --inline-entry-points-exhaustive \
|
|
|
|
--eliminate-dead-code-aggressive program.spv
|
|
|
|
|
|
|
|
Lines starting with the character '#' in the configuration
|
|
|
|
file indicate a comment and will be ignored.
|
|
|
|
|
|
|
|
The -O, -Os, and -Oconfig flags act as macros. Using one of them
|
|
|
|
is equivalent to explicitly inserting the underlying flags at
|
|
|
|
that position in the command line. For example, the invocation
|
|
|
|
'spirv-opt --merge-blocks -O ...' applies the transformation
|
|
|
|
--merge-blocks followed by all the transformations implied by
|
|
|
|
-O.
|
2018-01-04 17:59:50 +00:00
|
|
|
--print-all
|
|
|
|
Print SPIR-V assembly to standard error output before each pass
|
|
|
|
and after the last pass.
|
2017-12-11 18:10:24 +00:00
|
|
|
--private-to-local
|
|
|
|
Change the scope of private variables that are used in a single
|
|
|
|
function to that function.
|
2018-05-15 17:11:18 +00:00
|
|
|
--reduce-load-size
|
|
|
|
Replaces loads of composite objects where not every component is
|
|
|
|
used by loads of just the elements that are used.
|
2017-12-04 17:29:51 +00:00
|
|
|
--redundancy-elimination
|
|
|
|
Looks for instructions in the same function that compute the
|
|
|
|
same value, and deletes the redundant ones.
|
2017-11-30 22:03:06 +00:00
|
|
|
--relax-struct-store
|
2017-12-05 16:28:12 +00:00
|
|
|
Allow store from one struct type to a different type with
|
|
|
|
compatible layout and members. This option is forwarded to the
|
|
|
|
validator.
|
2018-05-15 17:11:18 +00:00
|
|
|
--remove-duplicates
|
|
|
|
Removes duplicate types, decorations, capabilities and extension
|
|
|
|
instructions.
|
2018-01-30 16:24:03 +00:00
|
|
|
--replace-invalid-opcode
|
|
|
|
Replaces instructions whose opcode is valid for shader modules,
|
|
|
|
but not for the current shader stage. To have an effect, all
|
|
|
|
entry points must have the same execution model.
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
--ssa-rewrite
|
|
|
|
Replace loads and stores to function local variables with
|
|
|
|
operations on SSA IDs.
|
2018-04-16 13:58:00 +00:00
|
|
|
--scalar-replacement[=<n>]
|
2017-11-30 22:03:06 +00:00
|
|
|
Replace aggregate function scope variables that are only accessed
|
|
|
|
via their elements with new function variables representing each
|
2018-04-16 13:58:00 +00:00
|
|
|
element. <n> is a limit on the size of the aggragates that will
|
|
|
|
be replaced. 0 means there is no limit. The default value is
|
|
|
|
100.
|
2017-12-05 16:28:12 +00:00
|
|
|
--set-spec-const-default-value "<spec id>:<default value> ..."
|
|
|
|
Set the default values of the specialization constants with
|
|
|
|
<spec id>:<default value> pairs specified in a double-quoted
|
|
|
|
string. <spec id>:<default value> pairs must be separated by
|
|
|
|
blank spaces, and in each pair, spec id and default value must
|
|
|
|
be separated with colon ':' without any blank spaces in between.
|
|
|
|
e.g.: --set-spec-const-default-value "1:100 2:400"
|
2018-02-02 16:55:05 +00:00
|
|
|
--simplify-instructions
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
Will simplify all instructions in the function as much as
|
2018-02-02 16:55:05 +00:00
|
|
|
possible.
|
2017-12-07 15:55:57 +00:00
|
|
|
--skip-validation
|
|
|
|
Will not validate the SPIR-V before optimizing. If the SPIR-V
|
|
|
|
is invalid, the optimizer may fail or generate incorrect code.
|
|
|
|
This options should be used rarely, and with caution.
|
|
|
|
--strength-reduction
|
|
|
|
Replaces instructions with equivalent and less expensive ones.
|
2017-12-05 16:28:12 +00:00
|
|
|
--strip-debug
|
|
|
|
Remove all debug instructions.
|
2018-03-09 21:08:57 +00:00
|
|
|
--strip-reflect
|
|
|
|
Remove all reflection information. For now, this covers
|
|
|
|
reflection information defined by SPV_GOOGLE_hlsl_functionality1.
|
2018-12-17 21:54:23 +00:00
|
|
|
--target-env=<env>
|
|
|
|
Set the target environment. Without this flag the target
|
|
|
|
enviroment defaults to spv1.3.
|
|
|
|
<env> must be one of vulkan1.0, vulkan1.1, opencl2.2, spv1.0,
|
|
|
|
spv1.1, spv1.2, spv1.3, or webgpu0.
|
2018-03-07 14:25:51 +00:00
|
|
|
--time-report
|
|
|
|
Print the resource utilization of each pass (e.g., CPU time,
|
|
|
|
RSS) to standard error output. Currently it supports only Unix
|
|
|
|
systems. This option is the same as -ftime-report in GCC. It
|
|
|
|
prints CPU/WALL/USR/SYS time (and RSS if possible), but note that
|
|
|
|
USR/SYS time are returned by getrusage() and can have a small
|
|
|
|
error.
|
2018-11-30 19:15:51 +00:00
|
|
|
--upgrade-memory-model
|
|
|
|
Upgrades the Logical GLSL450 memory model to Logical VulkanKHR.
|
|
|
|
Transforms memory, image, atomic and barrier operations to conform
|
|
|
|
to that model's requirements.
|
2018-04-23 15:13:07 +00:00
|
|
|
--vector-dce
|
|
|
|
This pass looks for components of vectors that are unused, and
|
|
|
|
removes them from the vector. Note this would still leave around
|
|
|
|
lots of dead code that a pass of ADCE will be able to remove.
|
2018-01-17 19:57:37 +00:00
|
|
|
--workaround-1209
|
|
|
|
Rewrites instructions for which there are known driver bugs to
|
|
|
|
avoid triggering those bugs.
|
|
|
|
Current workarounds: Avoid OpUnreachable in loops.
|
2017-12-05 16:28:12 +00:00
|
|
|
--unify-const
|
|
|
|
Remove the duplicated constants.
|
2017-08-30 18:19:22 +00:00
|
|
|
-h, --help
|
2017-08-01 18:55:29 +00:00
|
|
|
Print this help.
|
2017-08-30 18:19:22 +00:00
|
|
|
--version
|
2017-08-01 18:55:29 +00:00
|
|
|
Display optimizer version information.
|
2016-06-29 20:16:03 +00:00
|
|
|
)",
|
2017-12-19 20:08:54 +00:00
|
|
|
program, program, GetLegalizationPasses().c_str(),
|
|
|
|
GetOptimizationPasses().c_str(), GetSizePasses().c_str());
|
2016-06-29 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
// Reads command-line flags the file specified in |oconfig_flag|. This string
|
|
|
|
// is assumed to have the form "-Oconfig=FILENAME". This function parses the
|
|
|
|
// string and extracts the file name after the '=' sign.
|
|
|
|
//
|
|
|
|
// Flags found in |FILENAME| are pushed at the end of the vector |file_flags|.
|
|
|
|
//
|
|
|
|
// This function returns true on success, false on failure.
|
|
|
|
bool ReadFlagsFromFile(const char* oconfig_flag,
|
|
|
|
std::vector<std::string>* file_flags) {
|
|
|
|
const char* fname = strchr(oconfig_flag, '=');
|
|
|
|
if (fname == nullptr || fname[0] != '=') {
|
2018-08-03 12:05:52 +00:00
|
|
|
spvtools::Errorf(opt_diagnostic, nullptr, {}, "Invalid -Oconfig flag %s",
|
|
|
|
oconfig_flag);
|
2017-08-30 18:19:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fname++;
|
2016-06-29 20:16:03 +00:00
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
std::ifstream input_file;
|
|
|
|
input_file.open(fname);
|
|
|
|
if (input_file.fail()) {
|
2018-08-03 12:05:52 +00:00
|
|
|
spvtools::Errorf(opt_diagnostic, nullptr, {}, "Could not open file '%s'",
|
|
|
|
fname);
|
2017-08-30 18:19:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-29 20:16:03 +00:00
|
|
|
|
2018-08-03 22:08:46 +00:00
|
|
|
std::string line;
|
|
|
|
while (std::getline(input_file, line)) {
|
|
|
|
// Ignore empty lines and lines starting with the comment marker '#'.
|
|
|
|
if (line.length() == 0 || line[0] == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tokenize the line. Add all found tokens to the list of found flags. This
|
|
|
|
// mimics the way the shell will parse whitespace on the command line. NOTE:
|
|
|
|
// This does not support quoting and it is not intended to.
|
|
|
|
std::istringstream iss(line);
|
|
|
|
while (!iss.eof()) {
|
|
|
|
std::string flag;
|
|
|
|
iss >> flag;
|
2017-08-30 18:19:22 +00:00
|
|
|
file_flags->push_back(flag);
|
|
|
|
}
|
|
|
|
}
|
2016-06-29 20:16:03 +00:00
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-03 12:05:52 +00:00
|
|
|
OptStatus ParseFlags(int argc, const char** argv,
|
|
|
|
spvtools::Optimizer* optimizer, const char** in_file,
|
2018-09-10 15:49:41 +00:00
|
|
|
const char** out_file,
|
|
|
|
spvtools::ValidatorOptions* validator_options,
|
|
|
|
spvtools::OptimizerOptions* optimizer_options);
|
2017-08-30 18:19:22 +00:00
|
|
|
|
|
|
|
// Parses and handles the -Oconfig flag. |prog_name| contains the name of
|
|
|
|
// the spirv-opt binary (used to build a new argv vector for the recursive
|
|
|
|
// invocation to ParseFlags). |opt_flag| contains the -Oconfig=FILENAME flag.
|
|
|
|
// |optimizer|, |in_file| and |out_file| are as in ParseFlags.
|
|
|
|
//
|
|
|
|
// This returns the same OptStatus instance returned by ParseFlags.
|
|
|
|
OptStatus ParseOconfigFlag(const char* prog_name, const char* opt_flag,
|
2018-08-03 12:05:52 +00:00
|
|
|
spvtools::Optimizer* optimizer, const char** in_file,
|
2017-08-30 18:19:22 +00:00
|
|
|
const char** out_file) {
|
|
|
|
std::vector<std::string> flags;
|
|
|
|
flags.push_back(prog_name);
|
|
|
|
|
|
|
|
std::vector<std::string> file_flags;
|
|
|
|
if (!ReadFlagsFromFile(opt_flag, &file_flags)) {
|
2018-08-03 12:05:52 +00:00
|
|
|
spvtools::Error(opt_diagnostic, nullptr, {},
|
|
|
|
"Could not read optimizer flags from configuration file");
|
2017-08-30 18:19:22 +00:00
|
|
|
return {OPT_STOP, 1};
|
|
|
|
}
|
|
|
|
flags.insert(flags.end(), file_flags.begin(), file_flags.end());
|
|
|
|
|
|
|
|
const char** new_argv = new const char*[flags.size()];
|
|
|
|
for (size_t i = 0; i < flags.size(); i++) {
|
|
|
|
if (flags[i].find("-Oconfig=") != std::string::npos) {
|
2018-08-03 12:05:52 +00:00
|
|
|
spvtools::Error(
|
|
|
|
opt_diagnostic, nullptr, {},
|
|
|
|
"Flag -Oconfig= may not be used inside the configuration file");
|
2017-08-30 18:19:22 +00:00
|
|
|
return {OPT_STOP, 1};
|
|
|
|
}
|
|
|
|
new_argv[i] = flags[i].c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseFlags(static_cast<int>(flags.size()), new_argv, optimizer,
|
2018-09-10 15:49:41 +00:00
|
|
|
in_file, out_file, nullptr, nullptr);
|
2017-08-30 18:19:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 19:21:44 +00:00
|
|
|
// Canonicalize the flag in |argv[argi]| of the form '--pass arg' into
|
|
|
|
// '--pass=arg'. The optimizer only accepts arguments to pass names that use the
|
|
|
|
// form '--pass_name=arg'. Since spirv-opt also accepts the other form, this
|
|
|
|
// function makes the necessary conversion.
|
|
|
|
//
|
|
|
|
// Pass flags that require additional arguments should be handled here. Note
|
|
|
|
// that additional arguments should be given as a single string. If the flag
|
|
|
|
// requires more than one argument, the pass creator in
|
|
|
|
// Optimizer::GetPassFromFlag() should parse it accordingly (e.g., see the
|
|
|
|
// handler for --set-spec-const-default-value).
|
|
|
|
//
|
|
|
|
// If the argument requests one of the passes that need an additional argument,
|
|
|
|
// |argi| is modified to point past the current argument, and the string
|
|
|
|
// "argv[argi]=argv[argi + 1]" is returned. Otherwise, |argi| is unmodified and
|
|
|
|
// the string "|argv[argi]|" is returned.
|
|
|
|
std::string CanonicalizeFlag(const char** argv, int argc, int* argi) {
|
|
|
|
const char* cur_arg = argv[*argi];
|
2018-07-31 15:45:39 +00:00
|
|
|
const char* next_arg = (*argi + 1 < argc) ? argv[*argi + 1] : nullptr;
|
2018-07-25 19:21:44 +00:00
|
|
|
std::ostringstream canonical_arg;
|
|
|
|
canonical_arg << cur_arg;
|
|
|
|
|
|
|
|
// NOTE: DO NOT ADD NEW FLAGS HERE.
|
|
|
|
//
|
|
|
|
// These flags are supported for backwards compatibility. When adding new
|
|
|
|
// passes that need extra arguments in its command-line flag, please make them
|
|
|
|
// use the syntax "--pass_name[=pass_arg].
|
|
|
|
if (0 == strcmp(cur_arg, "--set-spec-const-default-value") ||
|
|
|
|
0 == strcmp(cur_arg, "--loop-fission") ||
|
|
|
|
0 == strcmp(cur_arg, "--loop-fusion") ||
|
|
|
|
0 == strcmp(cur_arg, "--loop-unroll-partial") ||
|
|
|
|
0 == strcmp(cur_arg, "--loop-peeling-threshold")) {
|
|
|
|
if (next_arg) {
|
|
|
|
canonical_arg << "=" << next_arg;
|
|
|
|
++(*argi);
|
2018-04-20 14:14:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-23 20:01:12 +00:00
|
|
|
|
2018-07-25 19:21:44 +00:00
|
|
|
return canonical_arg.str();
|
2018-02-27 11:50:08 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 02:08:59 +00:00
|
|
|
// Parses command-line flags. |argc| contains the number of command-line flags.
|
|
|
|
// |argv| points to an array of strings holding the flags. |optimizer| is the
|
|
|
|
// Optimizer instance used to optimize the program.
|
2017-08-30 18:19:22 +00:00
|
|
|
//
|
|
|
|
// On return, this function stores the name of the input program in |in_file|.
|
|
|
|
// The name of the output file in |out_file|. The return value indicates whether
|
|
|
|
// optimization should continue and a status code indicating an error or
|
|
|
|
// success.
|
2018-08-03 12:05:52 +00:00
|
|
|
OptStatus ParseFlags(int argc, const char** argv,
|
|
|
|
spvtools::Optimizer* optimizer, const char** in_file,
|
2018-09-10 15:49:41 +00:00
|
|
|
const char** out_file,
|
|
|
|
spvtools::ValidatorOptions* validator_options,
|
|
|
|
spvtools::OptimizerOptions* optimizer_options) {
|
2018-07-25 19:21:44 +00:00
|
|
|
std::vector<std::string> pass_flags;
|
2016-06-29 20:16:03 +00:00
|
|
|
for (int argi = 1; argi < argc; ++argi) {
|
|
|
|
const char* cur_arg = argv[argi];
|
|
|
|
if ('-' == cur_arg[0]) {
|
|
|
|
if (0 == strcmp(cur_arg, "--version")) {
|
2018-08-03 12:05:52 +00:00
|
|
|
spvtools::Logf(opt_diagnostic, SPV_MSG_INFO, nullptr, {}, "%s\n",
|
|
|
|
spvSoftwareVersionDetailsString());
|
2017-08-30 18:19:22 +00:00
|
|
|
return {OPT_STOP, 0};
|
2016-06-29 20:16:03 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) {
|
|
|
|
PrintUsage(argv[0]);
|
2017-08-30 18:19:22 +00:00
|
|
|
return {OPT_STOP, 0};
|
2016-06-29 20:16:03 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "-o")) {
|
2017-08-30 18:19:22 +00:00
|
|
|
if (!*out_file && argi + 1 < argc) {
|
|
|
|
*out_file = argv[++argi];
|
2016-06-29 20:16:03 +00:00
|
|
|
} else {
|
|
|
|
PrintUsage(argv[0]);
|
2017-08-30 18:19:22 +00:00
|
|
|
return {OPT_STOP, 1};
|
2016-06-29 20:16:03 +00:00
|
|
|
}
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if ('\0' == cur_arg[1]) {
|
|
|
|
// Setting a filename of "-" to indicate stdin.
|
|
|
|
if (!*in_file) {
|
|
|
|
*in_file = cur_arg;
|
2016-09-22 15:50:12 +00:00
|
|
|
} else {
|
2018-08-03 12:05:52 +00:00
|
|
|
spvtools::Error(opt_diagnostic, nullptr, {},
|
|
|
|
"More than one input file specified");
|
2017-08-30 18:19:22 +00:00
|
|
|
return {OPT_STOP, 1};
|
2016-09-22 15:50:12 +00:00
|
|
|
}
|
2017-08-30 18:19:22 +00:00
|
|
|
} else if (0 == strncmp(cur_arg, "-Oconfig=", sizeof("-Oconfig=") - 1)) {
|
|
|
|
OptStatus status =
|
|
|
|
ParseOconfigFlag(argv[0], cur_arg, optimizer, in_file, out_file);
|
|
|
|
if (status.action != OPT_CONTINUE) {
|
|
|
|
return status;
|
|
|
|
}
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--skip-validation")) {
|
2018-09-10 15:49:41 +00:00
|
|
|
optimizer_options->set_run_validator(false);
|
2018-01-04 17:59:50 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--print-all")) {
|
|
|
|
optimizer->SetPrintAll(&std::cerr);
|
2018-03-07 14:25:51 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--time-report")) {
|
|
|
|
optimizer->SetTimeReport(&std::cerr);
|
2018-07-25 19:21:44 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--relax-struct-store")) {
|
2018-09-10 15:49:41 +00:00
|
|
|
validator_options->SetRelaxStructStore(true);
|
|
|
|
} else if (0 == strncmp(cur_arg, "--max-id-bound=",
|
|
|
|
sizeof("--max-id-bound=") - 1)) {
|
|
|
|
auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg);
|
|
|
|
// Will not allow values in the range [2^31,2^32).
|
|
|
|
uint32_t max_id_bound =
|
|
|
|
static_cast<uint32_t>(atoi(split_flag.second.c_str()));
|
|
|
|
|
|
|
|
// That SPIR-V mandates the minimum value for max id bound but
|
|
|
|
// implementations may allow higher minimum bounds.
|
|
|
|
if (max_id_bound < kDefaultMaxIdBound) {
|
|
|
|
spvtools::Error(opt_diagnostic, nullptr, {},
|
|
|
|
"The max id bound must be at least 0x3FFFFF");
|
|
|
|
return {OPT_STOP, 1};
|
|
|
|
}
|
|
|
|
optimizer_options->set_max_id_bound(max_id_bound);
|
2018-11-06 16:30:19 +00:00
|
|
|
validator_options->SetUniversalLimit(spv_validator_limit_max_id_bound,
|
|
|
|
max_id_bound);
|
2018-12-17 21:54:23 +00:00
|
|
|
} else if (0 == strncmp(cur_arg,
|
|
|
|
"--target-env=", sizeof("--target-env=") - 1)) {
|
|
|
|
const auto split_flag = spvtools::utils::SplitFlagArgs(cur_arg);
|
|
|
|
const auto target_env_str = split_flag.second.c_str();
|
|
|
|
spv_target_env target_env;
|
|
|
|
if (!spvParseTargetEnv(target_env_str, &target_env)) {
|
|
|
|
spvtools::Error(opt_diagnostic, nullptr, {},
|
|
|
|
"Invalid value passed to --target-env");
|
|
|
|
return {OPT_STOP, 1};
|
|
|
|
}
|
|
|
|
optimizer->SetTargetEnv(target_env);
|
2016-06-29 20:16:03 +00:00
|
|
|
} else {
|
2018-07-25 19:21:44 +00:00
|
|
|
// Some passes used to accept the form '--pass arg', canonicalize them
|
|
|
|
// to '--pass=arg'.
|
|
|
|
pass_flags.push_back(CanonicalizeFlag(argv, argc, &argi));
|
|
|
|
|
|
|
|
// If we were requested to legalize SPIR-V generated from the HLSL
|
|
|
|
// front-end, skip validation.
|
2018-08-08 15:16:19 +00:00
|
|
|
if (0 == strcmp(cur_arg, "--legalize-hlsl")) {
|
2018-09-10 15:49:41 +00:00
|
|
|
validator_options->SetRelaxLogicalPointer(true);
|
2018-08-08 15:16:19 +00:00
|
|
|
}
|
2016-06-29 20:16:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-30 18:19:22 +00:00
|
|
|
if (!*in_file) {
|
|
|
|
*in_file = cur_arg;
|
2016-06-29 20:16:03 +00:00
|
|
|
} else {
|
2018-08-03 12:05:52 +00:00
|
|
|
spvtools::Error(opt_diagnostic, nullptr, {},
|
|
|
|
"More than one input file specified");
|
2017-08-30 18:19:22 +00:00
|
|
|
return {OPT_STOP, 1};
|
2016-06-29 20:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 19:21:44 +00:00
|
|
|
if (!optimizer->RegisterPassesFromFlags(pass_flags)) {
|
|
|
|
return {OPT_STOP, 1};
|
|
|
|
}
|
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
return {OPT_CONTINUE, 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main(int argc, const char** argv) {
|
|
|
|
const char* in_file = nullptr;
|
|
|
|
const char* out_file = nullptr;
|
|
|
|
|
2018-02-09 19:29:02 +00:00
|
|
|
spv_target_env target_env = kDefaultEnvironment;
|
2017-08-30 18:19:22 +00:00
|
|
|
|
2018-09-12 17:41:50 +00:00
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
spvtools::Optimizer optimizer(target_env);
|
2018-08-08 15:16:19 +00:00
|
|
|
optimizer.SetMessageConsumer(spvtools::utils::CLIMessageConsumer);
|
2017-08-30 18:19:22 +00:00
|
|
|
|
2018-11-06 16:30:19 +00:00
|
|
|
spvtools::ValidatorOptions validator_options;
|
|
|
|
spvtools::OptimizerOptions optimizer_options;
|
2017-12-07 15:55:57 +00:00
|
|
|
OptStatus status = ParseFlags(argc, argv, &optimizer, &in_file, &out_file,
|
2018-09-10 15:49:41 +00:00
|
|
|
&validator_options, &optimizer_options);
|
2018-11-06 16:30:19 +00:00
|
|
|
optimizer_options.set_validator_options(validator_options);
|
2017-10-24 19:13:13 +00:00
|
|
|
|
2017-08-30 18:19:22 +00:00
|
|
|
if (status.action == OPT_STOP) {
|
|
|
|
return status.code;
|
|
|
|
}
|
|
|
|
|
2016-06-29 20:16:03 +00:00
|
|
|
if (out_file == nullptr) {
|
2018-08-03 12:05:52 +00:00
|
|
|
spvtools::Error(opt_diagnostic, nullptr, {}, "-o required");
|
2016-06-29 20:16:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-02 06:36:35 +00:00
|
|
|
std::vector<uint32_t> binary;
|
2017-08-30 18:19:22 +00:00
|
|
|
if (!ReadFile<uint32_t>(in_file, "rb", &binary)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2016-06-29 20:16:03 +00:00
|
|
|
|
2017-04-02 06:36:35 +00:00
|
|
|
// By using the same vector as input and output, we save time in the case
|
|
|
|
// that there was no change.
|
2018-09-12 17:41:50 +00:00
|
|
|
bool ok =
|
|
|
|
optimizer.Run(binary.data(), binary.size(), &binary, optimizer_options);
|
2016-06-29 20:16:03 +00:00
|
|
|
|
2017-04-02 06:36:35 +00:00
|
|
|
if (!WriteFile<uint32_t>(out_file, "wb", binary.data(), binary.size())) {
|
2016-06-29 20:16:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-02 06:36:35 +00:00
|
|
|
return ok ? 0 : 1;
|
2016-06-29 20:16:03 +00:00
|
|
|
}
|