Commit Graph

1010 Commits

Author SHA1 Message Date
Steven Perron
9fbcce4ca1
Add unrolling to the legalization passes (#1903)
Adds unrolling to the legalization passes.

After enabling unrolling I found a bug when there is a self-referencing
phi node.  That has been fixed.

The test that checks for that the order of optimizations is correct also
needed to be updated.
2018-09-19 16:40:09 -04:00
Jaebaek Seo
0cd3e599ae Validator: correct out of bound check for OpMemberDecorate (#1881)
The number that indicates a member in OpMemberDecorate must be less
than the number of total members of struct.
2018-09-18 10:16:46 -04:00
Steven Perron
7075c49923
Add dummy loop in merge-return. (#1896)
The current implementation of merge return can create bad, but correct,
code.  When it is not in a loop construct, it will insert a lot of
extra branch around code.  The potentially large number of branches are
bad.  At the same time, it can separate code store to variables from
its uses hiding the fact that the store dominates the load.

This hurts the later analysis because the compiler thinks that multiple
values can reach a load, when there is really only 1.  This poorer
analysis leads to missed optimizations.

The solution is to create a dummy loop around the entire body of the
function, then we can break from that loop with a single branch.  Also
only new merge nodes would be those at the end of loops meaning that
most analysies will not be hurt.

Remove dead code for cases that are no longer possible.

It seems like some drivers expect there the be an OpSelectionMerge
before conditional branches, even if they are not strictly needed.
So we add them.
2018-09-18 08:52:47 -04:00
Steven Perron
5f599e700e
Fix infinite loop in dead-branch-elimination (#1891)
* Create structed cfg analysis.

There are lots of optimization that have to traverse the CFG in a
structured order just because it wants to know which constructs a
basic block in contained in.  This adds extra complexity to these
optimizations, for causes too much refactoring of older optimizations.

To help with this problem, I have written an analysis that can give this
information.

* Identify branches breaking from loops.

Dead branch elimination does a search for a conditional branch to the
end of the current selection construct.  This search assumes that the
only way to leave the construct is through the merge node.  But that is
not true.  The code can jump to the merge node of a loop that contains
the construct.

The search needs to take this into consideration.
2018-09-17 13:00:24 -04:00
Lei Zhang
63265097e5
Add support for VK_KHR_shader_atomic_int64 in validator 2018-09-14 14:07:25 -04:00
Steven Perron
6d5f1bc2e8
Allow merge blocks to merge two header blocks in some cases. (#1890)
In merge blocks, we do not allow the merging of two blocks with merge
instructions.  This is because if the two block are merged only 1 of
those instructions can exists.  However, if the successor block is the
merge block of the predecessor, then we can delete the merge instruction
in the predecessor.  In this case, we are able to merge the blocks.
2018-09-14 13:37:18 -04:00
Jaebaek Seo
2c2fee7979 Validator: check OpTypeBool inside Blocks (#1405)
OpTypeBool can only be used with non-externally visible shader
Storage Classes: Workgroup, CrossWorkgroup, Private, and Function.
2018-09-10 13:33:13 -04:00
Steven Perron
75c1bf2843
Add option for the max id bound. (#1870)
* Create a new entry point for the optimizer

Creates a new struct to hold the options for the optimizer, and creates
an entry point that take the optimizer options as a parameter.

The old entry point that takes validator options are now deprecated.
The validator options will be one of the optimizer options.

Part of the optimizer options will also be the upper bound on the id bound.

* Add a command line option to set the max value for the id bound.  The default is 0x3FFFFF.

* Modify `TakeNextIdBound` to return 0 when the limit is reached.
2018-09-10 11:49:41 -04:00
Steven Perron
f62d7978fc
Add validation check for arrays of void type. (#1880)
In the definition of an array
(https://www.khronos.org/registry/spir-v/specs/1.2/SPIRV.html#Array),
it specfically mentions that array elements have non-void type.  I've
added a check for that in this PR.

http://crbug.com/879016
2018-09-10 09:21:32 -04:00
David Neto
7600fc0e19 Update Dim capabilities
Corresponds to grammar changes for SPIR-V 1.3 Rev4.
2018-09-06 13:41:31 -04:00
David Neto
571251c8f8 Support SPV_KHR_vulkan_memory_model rev2
Support collapsed into one commit:
- Asm/Dis support for SPV_KHR_vulkan_memory_model
- Add Vulkan mem model image operands to switch
- Add TODO for source/validate_image.cpp
- val: Image operands NonPrivateTexelKHR, VolatileTexelKHR have no operands
  This is required for memory model tests to pass SPIR-V validation.
- Round trip tests: Test new flags on OpCopyMemory*
2018-09-06 13:30:32 -04:00
Corentin Wallez
21bcb9d8b9 BUILD.gn: Make a better interface with dependents. (#1877)
This splits the spvtools_config into a public and private part to avoid
leaking internal bits to dependents. A new target is added for the
public headers so that "gn check" works for dependents.

Also formats test/fuzzers/BUILD.gn
2018-09-06 10:50:27 -04:00
Tobin Ehlis
58e53ea82f Fix sign mismatch warning
Cast negative ints to uint32_t to avoid int/uint comparison warning.
2018-08-31 17:51:43 -04:00
Alan Baker
cb0f1f565b Remove struct member offset monotonicity check
Fixes #1822

* Remove check that struct member offsets must be monotonic
 * All environments match Vulkan behaviour now
 * updated offending tests
2018-08-31 09:45:45 -04:00
Steven Perron
482b1744ca
Validate all type ids. (#1868)
* Validate all type ids.

The validator does not check if the type of an instruction is actually
a type unless the OpCode has a specific requirement.  For example,
OpFAdd is checked, but OpUndef is not.

The commit add a generic check that if there is a type id then the id
defines a type.

http://crbug.com/876694

* Merge other checks for type into new one.

There are a couple check that the type id is a type for specific
opcodes.  Those have been mereged into 1.

Small changes to other test cases to make them valid enough for the
purpose of the test.
2018-08-27 23:45:32 -04:00
Steven Perron
06b42949b6
Validate uses of OpTypeFunction. (#1867)
In the specification of `OpTypeFunction`, it says

> OpFunction is the only valid use of OpTypeFunction.

This commit add a check in the validator for this rule.

A test started to fail because the new check happens before the check
the test case is testing.  Updated the test case to still fail the
check it was suppose to fail originally.

http://crbug.com/874571
2018-08-27 11:41:25 -04:00
Steven Perron
d746681fe9
Copy decorations when creating new ids. (#1843)
* Copy decorations when creating new ids.

When creating a new value based on an old value, we need to copy the
decorations to the new id.  This change does this in 3 places:

1) The variable holding the return value of the function generated by
merge return should get decorations from the function.

2) The results of the OpPhi instructions should get decorations from the
variable they are replacing in the ssa writer.

3) In local access chain convert the intermediate struct (result of
OpCompositeInsert) generated for the store replacement should get its
decorations from the variable being stored to.

Fixes #1787.
2018-08-24 11:55:39 -04:00
Steven Perron
b4d3618f77
Don't "break" from selection constructs. (#1862)
If seems like at least 1 driver does not like a condition jump to the end
of a selection construct.  We are generating these in the merge return
pass.  This change stops merge return from generating this sequence.

Part of #1861.
2018-08-23 14:38:25 -04:00
Steven Perron
6c73b1fb70
Update the order when predicating blocks. (#1859)
When doing predicate blocks, we need to traverse every block in
structured order in order to keep track of which construct a block is
contained in.  The standard way of traversing code in structured order
is to create a list with all of the nodes in order.  However, when
predicating blocks, new blocks are created, and those blocks are missed.
This causes branches that go too far.

The solution is to update the order as new blocks are created.  Since
we are using an std::list, we do not have to worry about invalidation of
iterators when changing the list.
2018-08-23 12:59:31 -04:00
Alan Baker
c5b38062ec Moving constant opcode validation into a new file
* Split constant opcode validation out of idUsage and into
validate_constants.cpp
 * minor style fixes
 * reduced duplication
 * fixed an issue with array sizing
2018-08-21 17:30:26 -04:00
Steven Perron
19264ef42c
Have PredicateBlocks jump the existing merge blocks. (#1849)
* Refactor PredicateBlocks

Refactor PredicateBlocks so that we know which constructs a return
is contained in.  Will be used later.

* Have PredicateBlocks jump the existing merge blocks.

In PredicateBlocks, we currently skip instructions with side effects,
but it still follows the same control flow (sort-of).  This causes a
problem, when we are trying to predicate code in a loop.  We skip all
of the code with side effects (IV increment), but still follow the
same control flow (jump back the start of the loop).  This creates an
infinite loop because the code will keep jumping back to the start of
the loop without changing the values that effect the exit condition.

This is a large change to merge-return.  When predicating a block that
is in a loop or merge construct, it will jump to the merge block of the
construct.  Once out of all constructs we will generate code as we did
before.
2018-08-21 12:04:08 -04:00
Alan Baker
197b4597a0 Fix EvalInt32IfConst to fail on type instructions.
Fixes https://crbug.com/875842

* EvalInt32IfConst dereferenced a null pointer if a type instruction was
sent as the id
2018-08-21 11:59:00 -04:00
Steven Perron
d693a83e36
Handle breaks from structured-ifs in DCE. (#1848)
* Handle breaks from structured-ifs in DCE.

dead code elimination assumes that are conditional branches except for
breaks and continues in loops will have an OpSelectionMerge before them.
That is not true when breaking out of a selection construct.

The fix is to look for breaks in selection constructs in the same place
we look for breaks and continues for loops.
2018-08-21 11:54:44 -04:00
Steven Perron
45c235d41f
Have dead-branch-elim handle conditional exits from selections. (#1850)
When dead-branch-elim folds a conditional branch, it also deletes the
OpSelectionMerge instruction.  If that construct contains a
conditional branch to the merge node, it will not have its own
OpSelectionMerge.  When the headers merge instruction is deleted, the
the inner conditional branch will no longer be legal.  It will be a
selection to a node that is not a merge node.

We fix this up by moving the OpSelectionMerge to a new location if it is
still needed.
2018-08-21 11:49:56 -04:00
Diego Novillo
03000a3a38 Add testing framework for tools.
This forks the testing harness from https://github.com/google/shaderc
to allow testing CLI tools.

New features needed for SPIRV-Tools include:

1- A new PlaceHolder subclass for spirv shaders.  This place holder
   calls spirv-as to convert assembly input into SPIRV bytecode. This is
   required for most tools in SPIRV-Tools.

2- A minimal testing file for testing basic functionality of spirv-opt.

Add tests for all flags in spirv-opt.

1. Adds tests to check that known flags match the names that each pass
   advertises.
2. Adds tests to check that -O, -Os and --legalize-hlsl schedule the
   expected passes.
3. Adds more functionality to Expect classes to support regular
   expression matching on stderr.
4. Add checks for integer arguments to optimization flags.
5. Fixes #1817 by modifying the parsing of integer arguments in
   flags that take them.
6. Fixes -Oconfig file parsing (#1778). It reads every line of the file
   into a string and then parses that string by tokenizing every group of
   characters between whitespaces (using the standard cin reading
   operator).  This mimics shell command-line parsing, but it does not
   support quoting (and I'm not planning to).
2018-08-17 15:03:14 -04:00
Steven Perron
36d675a404
Change when instruction is registered in validator. (#1840)
When doing the validator checks, an instruction is currently registered
at the end of IdPass.  This creates an inconsistency.  In IdPass, an
instruction that uses its own result will treat that use as a forward
reference.  Then in the following passes it will not because the
definition can be found.

It seems best to update the state after all of the check have been done
for the current instruction.  This makes it consistent for all of the
passes.

This makes a different when trying to verify OpTypeStruct.

Fixes https://crbug.com/874372.
2018-08-15 13:18:47 -04:00
Steven Perron
e065cc208f
Keep decorations when replacing loads in access-chain-convert. (#1829)
In local-access-chain-convert, we replace loads by load the entire
variable, then doing the extract.  The extract will have the same value
as the load.  However, if the load has a decoration on it, the
decoration is lost because we do not copy any them to the new id.

This is fixed by rewritting the load into the extract and keeping the
same result id.

This change has the effect that we do not call DCEInst on the loads
because the load is not being deleted, but replaced.  This could leave
OpAccessChain instructions around that are not used.  This is not a
problem for -O and -Os.  They run local_single_*_elim passes and then
dead code elimination.  The dce will remove the unused access chains,
and the load elimination passes work even if there are unused access
chains.  I have added test to them to ensure they will not loss
opportunities.

Fixes #1787.
2018-08-15 09:14:21 -04:00
dan sinclair
ef678672fb
Remove source/message.h (#1838)
The code in source/message was only used in a single set of tests to
format the output results. This CL changes the test to verify the
message instead of all the error values and removes the source/message
code.
2018-08-14 15:41:21 -04:00
dan sinclair
1963a2dbda
Use MakeUnique. (#1837)
This CL replaces instances of reset(new ..) with MakeUnique.
2018-08-14 15:01:50 -04:00
dan sinclair
1553025f4c
Move make_unique to source/util. (#1836)
This MakeUnique code is used in places other then source/opt so move it
to source/utils.
2018-08-14 12:44:54 -04:00
Alan Baker
8cb949ad34 Validate correct opcode uses of OpFunction
Fixes https://crbug.com/873457

* Filed Khronos SPIR-V issue 352
* Updated bad tests
* Added new test
2018-08-14 10:13:06 -04:00
dan sinclair
5fc011b453
Move bit_stream, move_to_front and huffman_codec. (#1833)
bit_stream, move_to_front and huffman_codec are only used by
source/tools. Move into that directory to make the usage clearer.
2018-08-14 09:52:05 -04:00
alan-baker
ce4547bdc7
Disallow void types in structs (#1832)
Fixes #1831

* Adds validation check that void is not a member of a struct
 * added a test
2018-08-14 08:55:49 -04:00
Alan Baker
e7fdcdba75 Split function opcode validation into new files.
* Moved function opcode validation out of idUsage and into new files
 * minor style changes
 * General opcode checking is in validate_function.cpp
 * Execution limitation checking is in
 validate_execution_limitations.cpp
* Execution limitations was split into a new pass as it requires other
validation to register those limitations first.
2018-08-13 17:04:57 -04:00
dan sinclair
967bfa2d17
Remove unused stats maps and parameters (#1828) 2018-08-13 16:45:39 -04:00
Alan Baker
397e02442e Fixing heap overflow in validation.
* Changed entry point validation to check storage class of variable
instead of pointer
 * added a test
* Moved several checks after opcode validation
 * These checks should be able to guarantee individual instructions are
 ok
* Updated tests due to reordered checks
2018-08-13 15:23:30 -04:00
dan sinclair
da0f1dcccc
Move spirv_stats into tools/stats. (#1826)
The spirv_stats code is only used by the tools/stats module. This CL
moves the code to that module.
2018-08-13 11:48:25 -04:00
Alan Baker
7d4b0464a3 Split annotation opcode validation into new file.
* Moves annotation opcode checks from idUsage into a new pass
 * minor style updates
2018-08-08 15:43:11 -04:00
Alan Baker
f2a990022a Move type instruction validation into separate file
* Moved type instruction validation out of validation idUsage into a new
file
* Consolidate type unique pass into new file
* Removed one bad test
* Reworked validation ordering
2018-08-08 12:55:39 -04:00
Steven Perron
5c8b4f5a1c
Validate the input to Optimizer::Run (#1799)
* Run the validator in the optimization fuzzers.

The optimizers assumes that the input to the optimizer is valid.  Since
the fuzzers do not check that the input is valid before passing the
spir-v to the optimizer, we are getting a few errors.

The solution is to run the validator in the optimizer to validate the
input.

For the legalization passes, we need to add an extra option to the
validator to accept certain types of variable pointers, even if the
capability is not given.  At the same time, we changed the option
"--legalize-hlsl" to relax the validator in the same way instead of
turning it off.
2018-08-08 11:16:19 -04:00
Alan Baker
3a20879f4d Unify validation of OpCopyMemory*
Fixes #1800

* Refactored duplication of code between OpCopyMemory and
OpCopyMemorySized validation
* Fixed some bugs in OpCopyMemorySized validation
* Replaced asserts with checks
* Added new tests
2018-08-07 19:01:58 -04:00
Alan Baker
2896b8f0e5 Refactor where opcodes are validated
* Replaced uses in opcode validation of current_function()
* Added non-const accessor to function lookup in ValidationState_t
* Updated a couple bad tests due to check reordering
2018-08-07 10:29:30 -04:00
dan sinclair
508df9a387
Remove unused bit stream methods. (#1807)
This CL deletes methods from bit stream which are never used and moves
several to the anonymous namespace in the bit_stream test file.
2018-08-07 09:10:54 -04:00
dan sinclair
e3ea909ebe
Simplify MoveToFront (#1806)
This CL removes the templating from the MoveToFront code as all non-test
code uses uint32_t as the variable.
2018-08-07 09:10:25 -04:00
dan sinclair
05057c9846
Fixup readabilty/inheritance warnings (#1805)
This CL removes the un-needed virtual qualifiers from methods already
marked as override.
2018-08-07 09:10:03 -04:00
dan sinclair
eda2cfbe12
Cleanup includes. (#1795)
This Cl cleans up the include paths to be relative to the top level
directory. Various include-what-you-use fixes have been added.
2018-08-03 15:06:09 -04:00
Corentin Wallez
2d9a325264 Refactor BUILD.gn so can easily be embedded in other projects
1.
BUILD.gn: Don't use the extra Chromium clang warnings

Also removes the unused .gn secondary_sources.

2.

Move fuzzers in test/ instead of testing/

This frees up testing/ to be the git subtree of Chromium's src/testing/
that contains test.gni, gtest, gmock and libfuzzer

3.

DEPS: get the whole testing/ subtree of Chromium

4.

BUILD.gn: Simplify the standalone gtest targets

These targets definitions are inspired from ANGLE's and add a variable
that is the path of the googletest directory so that it can be made
overridable in future commits.

6.

BUILD.gn: Add overridable variables for deps dirs

This avoids hardcoded paths to dependencies that make it hard to
integrate SPIRV-Tools in other GN projects.
2018-08-03 10:06:11 -04:00
dan sinclair
7861df9bb3
Remove using namespace commands. (#1794)
This CL removes the last two 'using namespace' commands.
2018-08-03 08:05:52 -04:00
dan sinclair
58a6876cee
Rewrite include guards (#1793)
This CL rewrites the include guards to make PRESUBMIT.py include guard
check happy.
2018-08-03 08:05:33 -04:00
dan sinclair
d38a0a3b44
Validation within function body when doing a FunctionCall. (#1790)
When validating a FunctionCall we can trigger an assert if we are not
currently within a function body. This CL adds verification that we are
within a function before attempting to add a function call.

Issue 1789.
2018-08-02 16:58:45 -04:00