Commit Graph

1090 Commits

Author SHA1 Message Date
Steven Perron
9ba50e34f2 Avoid generating duplicate names when merging types
The merging types we do not remove other information related to the
types.  We simply leave it duplicated, and hope it is removed later.
This is what happens with decorations.  They are removed in the next
phase of remove duplicates.  However, for OpNames that is not the case.
We end up with two different names for the same id, which does not make
sense.

The solution is to remove the names and decorations for the type being
removed instead of rewriting them to refer to the other type.

Note that it is possible that if the first type does not have a name,
then the types will end up with no name.  That is fine because the names
should not have any semantic significance anyway.

The was identified in issue #1372, but this does not fix that issue.
2018-03-05 12:02:50 -05:00
Pierre Moreau
6cd6e5ebef Define Disassemble only when Effcee is used in fold_test 2018-03-02 16:40:52 -05:00
Alan Baker
52bceb3569 Handles more cases of redundant selects
* Handles OpConstantNull and vector types
 * vector selects (except against a null) are converted to vector
 shuffles
* Added tests
2018-03-02 14:28:08 -05:00
Alan Baker
824625760b Fixes #1361. Mark all non-constant global values as varying in CCP
* Also mark function parameters as varying
* Conservatively mark assignment instructions as varying if any input is
varying after attempting to fold
* Added a test to catch this case
2018-03-01 15:24:41 -05:00
Arseny Kapoulkine
8b27ba834d Vulkan BuiltIn variables can't have Location/Component decorations
As per Vulkan spec, BuiltIn variables can't have Location or Component
decorations. On some drivers, these can lead to driver crashing when
compiling the shader pipeline; for example, NVidia/AMD desktop drivers:
https://github.com/KhronosGroup/glslang/issues/1182.

This change adds validation and tests to catch this.
2018-03-01 15:00:08 -05:00
Alan Baker
ce5941a642 Fixes #1357. Support null constants better in folding
* getFloatConstantKind() now handles OpConstantNull
* PerformOperation() now handles OpConstantNull for vectors
* Fixed some instances where we would attempt to merge a division by 0
* added tests
2018-02-28 23:12:27 -05:00
GregF
bdaf8d56fb Opt: Add constant folding for FToI and IToF 2018-02-28 23:08:52 -05:00
Alan Baker
9457cabbce Fixes #1354. Do not merge integer division.
* Removes merging of div with a div or mul for integers
* Updated tests
2018-02-28 13:33:21 -05:00
Steven Perron
588f4fcc95 Add more folding rules for vector shuffle.
Adds rule to fold OpVectorShuffle with constant inputs.

Adds rules to fold OpCompositeExtrac being fed by an OpVectorShuffle.
2018-02-27 21:20:22 -05:00
Steven Perron
2cb589cc14 Remove uses DCEInst and call ADCE
The algorithm used in DCEInst to remove dead code is very slow.  It is
fine if you only want to remove a small number of instructions, but, if
you need to remove a large number of instructions, then the algorithm in
ADCE is much faster.

This PR removes the calls to DCEInst in the load-store removal passes
and adds a pass of ADCE afterwards.

A number of different iterations of the order of optimization, and I
believe this is the best I could find.

The results I have on 3 sets of shaders are:

Legalization:

Set 1: 5.39 -> 5.01
Set 2: 13.98 -> 8.38
Set 3: 98.00 -> 96.26

Performance passes:

Set 1: 6.90 -> 5.23
Set 2: 10.11 -> 6.62
Set 3: 253.69 -> 253.74

Size reduction passes:

Set 1: 7.16 -> 7.25
Set 2: 17.17 -> 16.81
Set 3: 112.06 -> 107.71

Note that the third set's compile time is large because of the large
number of basic blocks, not so much because of the number of
instructions.  That is why we don't see much gain there.
2018-02-27 21:06:08 -05:00
Alan Baker
802cf053c7 Merge arithmetic with non-trivial constant operands
Adding basis of arithmetic merging

* Refactored constant collection in ConstantManager
* New rules:
 * consecutive negates
 * negate of arithmetic op with a constant
 * consecutive muls
 * reciprocal of div

* Removed IRContext::CanFoldFloatingPoint
 * replaced by Instruction::IsFloatingPointFoldingAllowed
* Fixed some bad tests
* added some header comments

Added PerformIntegerOperation

* minor fixes to constants and tests
* fixed IntMultiplyBy1 to work with 64 bit ints
* added tests for integer mul merging

Adding test for vector integer multiply merging

Adding support for merging integer add and sub through negate

* Added tests

Adding rules to merge mult with preceding divide

* Has a couple tests, but needs more
* Added more comments

Fixed bug in integer division folding

* Will no longer merge through integer division if there would be a
remainder in the division
* Added a bunch more tests

Adding rules to merge divide and multiply through divide

* Improved comments
* Added tests

Adding rules to handle mul or div of a negation

* Added tests

Changes for review

* Early exit if no constants are involved in more functions
* fixed some comments
* removed unused declaration
* clarified some logic

Adding new rules for add and subtract

* Fold adds of adds, subtracts or negates
* Fold subtracts of adds, subtracts or negates
* Added tests
2018-02-27 13:02:13 -05:00
Stephen McGroarty
20b8cdb7c6 Make IR builder use the type manager for constants
This change makes the IR builder use the type manager to generate
OpTypeInts when creating OpConstants. This avoids dangling references
being stored by the created OpConstants.
2018-02-27 12:59:26 -05:00
Pierre Moreau
9394272c98 linker: merge debug annotations from category c)
Fixes: https://github.com/KhronosGroup/SPIRV-Tools/issues/1218
2018-02-27 12:31:50 -05:00
Pierre Moreau
bdd6617faa linker: Allow modules to be partially linked
Fixes: https://github.com/KhronosGroup/SPIRV-Tools/issues/1144
2018-02-27 12:21:13 -05:00
Victor Lomuller
3497a94460 Add loop unswitch pass.
It moves all conditional branching and switch whose conditions are loop
invariant and uniform. Before performing the loop unswitch we check that
the loop does not contain any instruction that would prevent it
(barriers, group instructions etc.).
2018-02-27 08:52:46 -05:00
Stephen McGroarty
e354984b09 Unroller support for multiple induction variables
Support for multiple induction variables within a loop and support for
loop condition operands <= and >=.
2018-02-27 11:50:08 +00:00
Steven Perron
3f19c2031a Preserve analysies in the simplification pass
Fixes a bug at the same time.  In `UpdateDefUse`, if the definition
already exists, we are not suppose to analyse it again.  When you do
the entries for the definition are deleted, and we don't want that.
The check for this was wrong.
2018-02-22 16:06:30 -05:00
GregF
46a9ec9d23 Opt: Check for side-effects in DCEInst()
This function now checks for side-effects before adding operand
instructions to the dead instruction work list.

Because this fix puts more pressure on IsCombinatorInstruction() to
be correct, this commit adds all OpConstant* and OpType* instructions
to combinator_ops_ set.

Fixes #1341.
2018-02-22 12:24:13 -05:00
Alan Baker
01760d2f0f Fixes #1338. Handle OpConstantNull in branch/switch conditions
* No longer assume the branch/switch condition must be bool or int
constants (respectively)
* Added a couple unit tests for each case
2018-02-21 10:22:39 -05:00
Arseny Kapoulkine
309be423cc Add folding for redundant add/sub/mul/div/mix operations
This change implements instruction folding for arithmetic operations
that are redundant, specifically:

  x + 0 = 0 + x = x
  x - 0 = x
  0 - x = -x
  x * 0 = 0 * x = 0
  x * 1 = 1 * x = x
  0 / x = 0
  x / 1 = x
  mix(a, b, 0) = a
  mix(a, b, 1) = b

Cache ExtInst import id in feature manager

This allows us to avoid string lookups during optimization; for now we
just cache GLSL std450 import id but I can imagine caching more sets as
they become utilized by the optimizer.

Add tests for add/sub/mul/div/mix folding

The tests cover scalar float/double cases, and some vector cases.

Since most of the code for floating point folding is shared, the tests
for vector folding are not as exhaustive as scalar.

To test sub->negate folding I had to implement a custom fixture.
2018-02-20 18:29:27 -05:00
Steven Perron
9d95a91a9f Fix folding insert feeding extract
I mixed up two cases when folding an OpCompositeExtract that is feed by
and OpCompositeInsert.  The specific cases are demonstracted in the new
test.  I mixed up the conditions for the cases, and treated one like the
other.

Fixes #1323.
2018-02-20 11:22:51 -05:00
Alan Baker
c3f34d8bf3 Fixes #1300. Adding checks for bad CCP transitions and unsettled values
* Now track propagation status and assert on bad statuses
 * Added helper methods to access instruction propagation status
* Modified the phi meet operator to properly reflect the paper it is
based on
* Modified SSA edge addition so that all edge are added, but only on
state changes
* Fixed a bug in instruction simulation where interesting conditional
branches would not mark the interesting edge as executed
 * Added a test to catch this bug
* Added an ostream operator for SSAPropagator::PropStatus
2018-02-18 19:41:34 -05:00
Arseny Kapoulkine
1054413600 Add constant folding rules for floating-point comparison
This change handles all 6 regular comparison types in two variations,
ordered (true if values are ordered *and* comparison is true) and
unordered (true if values are unordered *or* comparison is true).

Ordered comparison matches the default floating-point behavior on host
but we use std::isnan to check ordering explicitly anyway.

This change also slightly reworks the floating-point folding support
code to make it possible to define a folding operation that returns
boolean instead of floating point.

These tests exhaustively test ordered/unordered comparisons for
float/double.

Since for NaN inputs the comparison result doesn't depend on the
comparison function, we just test == and !=; NaN inputs result in true
unordered comparisons and false ordered comparisons.
2018-02-16 20:41:22 -05:00
Steven Perron
50f307f889 Simplify OpPhi instructions referencing unreachable continues
In dead branch elimination, we already recognize unreachable continue
blocks, and update OpPhi instruction accordingly.  This change adds an
extra check: if the head block has exactly 1 other incoming edge, then
replace the OpPhi with the value from that edge.

Fixes #1314.
2018-02-16 18:58:03 -05:00
Steven Perron
3756b387f3 Get CCP to use the constant floating point rules.
Fixes #1311
2018-02-16 13:49:47 -05:00
Lei Zhang
efe286cd32 SubgroupBallotKHR can enable SubgroupSize & SubgroupLocalInvocationId 2018-02-16 10:02:18 -05:00
Lei Zhang
f3a10470d3
Avoid using static unordered_map (#1304)
unordered_map is not POD. Using it as static may cause problems
when operator new() and operator delete() is customized.

Also changed some function signatures to use const char* instead
of std::string, which will give caller the flexibility to avoid
creating a std::string.
2018-02-15 10:19:15 -05:00
Arseny Kapoulkine
32a8e04c7d Add folding of redundant OpSelect insns
We can fold OpSelect into one of the operands in two cases:

- condition is constant
- both results are the same

Even if the original shader doesn't have either of these, if-conversion
pass sometimes ends up generating instructions like

   %7127 = OpSelect %int %3220 %7058 %7058

And this optimization cleans them up.
2018-02-15 10:03:22 -05:00
Steven Perron
6669d8163d Fold binary floating point operators.
Adds the floating rules for FAdd, FDiv, FMul, and FSub.

Contributes to #1164.
2018-02-14 15:48:15 -05:00
Stephen McGroarty
dd8400e150 Initial support for loop unrolling.
This patch adds initial support for loop unrolling in the form of a
series of utility classes which perform the unrolling. The pass can
be run with the command spirv-opt --loop-unroll. This will unroll
loops within the module which have the unroll hint set. The unroller
imposes a number of requirements on the loops it can unroll. These are
documented in the comments for the LoopUtils::CanPerformUnroll method in
loop_utils.h. Some of the restrictions will be lifted in future patches.
2018-02-14 15:44:38 -05:00
Alan Baker
229ebc0665 Fixes #1295. Mark undef values as varying in ccp.
* Undef now marked as varying in ccp
 * this prevents incorrect meet operations since phis were always not
 interesting
* added a test to catch the bug
2018-02-14 10:21:26 -05:00
Steven Perron
1d7b1423f9 Add folding of OpCompositeExtract and OpConstantComposite constant instructions.
Create files for constant folding rules.

Add the rules for OpConstantComposite and OpCompositeExtract.
2018-02-09 17:52:33 -05:00
Alexander Johnston
84ccd0b9ae Loop invariant code motion initial implementation 2018-02-08 22:55:47 -05:00
GregF
ca4457b4b6 SROA: Do replacement on structs with no partial references. 2018-02-08 15:20:02 -05:00
Steven Perron
06cdb96984 Make use of the instruction folder.
Implementation of the simplification pass.

- Create pass that calls the instruction folder on each instruction and
  propagate instructions that fold to a copy.  This will do copy
  propagation as well.

- Did not use the propagator engine because I want to modify the instruction
  as we go along.

- Change folding to not allocate new instructions, but make changes in
  place.  This change had a big impact on compile time.

- Add simplification pass to the legalization passes in place of
  insert-extract elimination.

- Added test cases for new folding rules.

- Added tests for the simplification pass

- Added a method to the CFG to apply a function to the basic blocks in
  reverse post order.

Contributes to #1164.
2018-02-07 23:01:47 -05:00
Andrey Tuganov
a61e4c1356 Disable check which fails Vulkan CTS 2018-02-07 13:31:35 -05:00
Andrey Tuganov
2f0c3aaa11 Add Vulkan-specific validation rules for atomics
Added atomic instructions validation rules from
https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#spirvenv-module-validation
2018-02-07 13:31:35 -05:00
Józef Kucia
3013897556 Build SPIRV-Tools as shared library
Add pkg-config file for shared libraries

Properly build SPIRV-Tools DLL

Test C interface with shared library

Set PATH to shared library file for c_interface_shared test

Otherwise, the test won't find SPIRV-Tools-shared.dll.

Do not use private functions when testing with shared library

Make all symbols hidden by default for shared library target
2018-02-07 10:43:32 -05:00
David Neto
e7fafdaa68 Fix test inclusion when Effcee is absent 2018-02-06 12:10:50 -05:00
Alan Baker
871022772e Registering a type now rebuilds it out of memory owned by the manager.
* Added TypeManager::RebuildType
 * rebuilds the type and its constituent types in terms of memory owned
 by the manager.
 * Used by TypeManager::RegisterType to properly allocate memory
* Adding an unit test to expose the issue
* Added some tests to provide coverage of RebuildType
* Added an accessor to the target pointer for a forward pointer
2018-02-06 10:17:56 -05:00
Andrey Tuganov
12e6860d07 Add barrier instructions validation pass 2018-02-05 13:14:55 -05:00
Steven Perron
bc1ec9418b Add general folding infrastructure.
Create the folding engine that will

1) attempt to fold an instruction.
2) iterates on the folding so small folding rules can be easily combined.
3) insert new instructions when needed.

I've added the minimum number of rules needed to test the features above.
2018-02-02 12:24:11 -05:00
Victor Lomuller
50e85c865c Add LoopUtils class to gather some loop transformation support.
This patch adds LoopUtils class to handle some loop related transformations. For now it has 2 transformations that simplifies other transformations such as loop unroll or unswitch:
 - Dedicate exit blocks: this ensure that all exit basic block
   (out-of-loop basic blocks that have a predecessor in the loop)
   have all their predecessors in the loop;
 - Loop Closed SSA (LCSSA): this ensure that all definitions in a loop are used inside the loop
   or in a phi instruction in an exit basic block.

It also adds the following capabilities:
 - Loop::IsLCSSA to test if the loop is in a LCSSA form
 - Loop::GetOrCreatePreHeaderBlock that can build a loop preheader if required;
 - New methods to allow on the fly updates of the loop descriptors.
 - New methods to allow on the fly updates of the CFG analysis.
 - Instruction::SetOperand to allow expression of the index relative to Instruction::NumOperands (to be compatible with the index returned by DefUseManager::ForEachUse)
2018-02-01 15:35:09 -05:00
Steven Perron
61d8c0384b Add pass to reaplce invalid opcodes
Creates a pass that will remove instructions that are invalid for the
current shader stage.  For the instruction to be considered for replacement

1) The opcode must be valid for a shader modules.
2) The opcode must be invalid for the current shader stage.
3) All entry points to the module must be for the same shader stage.
4) The function containing the instruction must be reachable from an entry point.

Fixes #1247.
2018-02-01 15:25:09 -05:00
Andrey Tuganov
d37869c842 Added OpenCL ExtInst validation rules 2018-02-01 14:14:13 -05:00
Jeremy Hayes
cd68f2b176 Add adjacency validation pass
Validate OpPhi predecessors.
Validate OpLoopMerge successors.
Validate OpSelectionMerge successors.
Fix collateral damage to existing tests.
Remove ValidateIdWithMessage.OpSampledImageUsedInOpPhiBad.
2018-02-01 14:10:55 -05:00
Alan Baker
2735e0851e Remove constexpr from Analysis operators
* Had to remove templating from InstructionBuilder as a result
 * now preserved analyses are specified as a constructor argument
* updated tests and uses
* changed static_assert to a runtime assert
 * this should probably get further changes in the future
2018-01-31 14:44:43 -05:00
Andrey Tuganov
44d88c8d9c Add memory semantics checks to validate atomics 2018-01-30 18:00:01 -05:00
Alan Baker
16949236fe Prevent unnecessary changes to the IR in dead branch elim
* When handling unreachable merges and continues, do not optimize to the
same IR
 * pass did not check whether the unreachable blocks were in the
 optimized form before transforming them
* added a test to catch this issue
2018-01-30 16:51:58 -05:00
Andrey Tuganov
c86cb76a22 Improved error message in val capabilities 2018-01-30 16:22:10 -05:00
Alan Baker
e661da7941 Enhancements to block merging
* Should handle all possibilities
 * Stricter checks for what is disallowed:
  * header and header
  * merge and merge
 * Allow header and merge blocks to be merged
  * Erases the structured control declaration if merging header and
    merge blocks together.
2018-01-30 16:05:51 -05:00
Alan Baker
6704233d39 Fix dereference of possibly nullptr
* If the dead branch elim is performed on a module without structured
control flow, the OpSelectionMerge may not be present
 * Add a check for pointer validity before dereferencing
* Added a test to catch the bug
2018-01-30 10:15:43 -05:00
GregF
f28b106173 InsertExtractElim: Split out DeadInsertElim as separate pass 2018-01-30 08:52:14 -05:00
Alan Baker
1b46f7ecad Fixes in CCP for #1228
* Forces traversal of phis if the def has changed to varying
* Mark a phi as varying if all incoming values are varying
* added a test to catch the bug
2018-01-29 15:12:05 -05:00
Victor Lomuller
6018de81de Add LoopDescriptor as an IRContext analysis.
Move some function definitions from header to source to avoid circular definition.
2018-01-25 16:12:32 -05:00
Greg Fischer
684997eb72 DeadInsertElim: Detect and DCE dead Inserts
This adds Dead Insert Elimination to the end of the
--eliminate-insert-extract pass. See the new tests for examples of code
that will benefit.

Essentially, this removes OpCompositeInsert instructions which are not
used, either because there is no instruction which uses the value at the
index it is inserted, or because a subsequent insert intercepts any such
use.

This code has been seen to remove significant amounts of dead code from
real-life HLSL shaders being ported to Vulkan. In fact, it is needed to
remove dead texture samples which cause Vulkan validation layer errors
(unbound textures and samplers) if not removed . Such DCE is thus
required for fxc equivalence and legalization.

This analysis operates across "chains" of Inserts which can also contain
Phi instructions.
2018-01-25 16:07:21 -05:00
Alan Baker
2e93e806e4 Initial implementation of if conversion
* Handles simple cases only
* Identifies phis in blocks with two predecessors and attempts to
convert the phi to an select
 * does not perform code motion currently so the converted values must
 dominate the join point (e.g. can't be defined in the branches)
 * limited for now to two predecessors, but can be extended to handle
 more cases
* Adding if conversion to -O and -Os
2018-01-25 09:42:00 -08:00
Andrey Tuganov
b2eb840468 Validator: restricted some atomic ops for shaders
Ban floating point case for OpAtomicLoad, OpAtomicExchange,
OpAtomicCompareExchange. In graphics (Shader) environments, these
instructions only operate on scalar integers. Ban the floating point
case. OpenCL supports atomic_float.
2018-01-24 14:06:06 -08:00
Andrey Tuganov
bdc78377bc Added Vulkan-specifc checks to image validation
Implemented Vulkan-specific rules:
- OpTypeImage must declare a scalar 32-bit float or 32-bit integer type
for the “Sampled Type”.
- OpSampledImage must only consume an “Image” operand whose type has its
“Sampled” operand set to 1.
2018-01-24 14:05:42 -08:00
Steven Perron
c4835e1bd8 Use id_map in Fold*ToConstant
The folding routines are suppose to use the id_map provided to map the
ids in the instruction.  The ones I just added are missing it.
2018-01-22 16:27:31 -05:00
Steven Perron
6c409e30a2 Add generic folding function and use in CCP
The current folding routines have a very cumbersome interface, make them
harder to use, and not a obvious how to extend.

This change is to create a new interface for the folding routines, and
show how it can be used by calling it from CCP.

This does not make a significant change to the behaviour of CCP.  In
general it should produce the same code as before; however it is
possible that an instruction that takes 32-bit integers as inputs and
the result is not a 32-bit integer or bool will not be folded as before.

It seems like andriod has a problem with INT32_MAX and the like.  I'll
explicitly define those if the are not already defined.
2018-01-22 14:26:49 -05:00
Alan Baker
3b780db7f8 Fixes infinite loop in ADCE
* Addresses how breaks are indentified to prevent infinite loops when
back to back loop share a merge and header
* Added test to catch the bug
2018-01-19 11:08:46 -05:00
Victor Lomuller
cf3b2a58c4 Introduce an instruction builder helper class.
The class factorize the instruction building process.
Def-use manager analysis can be updated on the fly to maintain coherency.
To be updated to take into account more analysis.
2018-01-19 10:17:45 -05:00
Alan Baker
73940aba1b Simplifying code for adding instructions to worklist
* AddToWorklist can now be called unconditionally
 * It will only add instructions that have not already been marked as
 live
 * Fixes a case where a merge was not added to the worklist because the
 branch was already marked as live
* Added two similar tests that fail without the fix
2018-01-18 20:36:46 -05:00
Steven Perron
34d4294c2c Create a pass to work around a driver bug related to OpUnreachable.
We have come across a driver bug where and OpUnreachable inside a loop
is causing the shader to go into an infinite loop.  This commit will try
to avoid this bug by turning OpUnreachable instructions that are
contained in a loop into branches to the loop merge block.

This is not added to "-O" and "-Os" because it should only be used if
the driver being targeted has this problem.

Fixes #1209.
2018-01-18 20:31:46 -05:00
Alan Baker
18618061cb Adding testcase for #1210 2018-01-18 12:58:07 -05:00
Alan Baker
80b743a570 Adding support for switch removal in ADCE
* Updated code to handle switches
* Enabled disabled test and added a couple new ones
2018-01-17 11:05:42 -05:00
Alan Baker
5ffe862f28 Fixes missing increment in common uniform elim
* Addresses #1203
* Increments inIdx in IsConstantIndexAccessChain
 * added test to catch the bug
2018-01-16 14:47:35 -05:00
Steven Perron
6cc772c3ce Skip SpecConstants in CCP.
At the moment specialization constants look like constants to ccp.  This
causes a problem because they are handled differently by the constant
manager.

I choose to simply skip over them, and not try to add them to the value
table.  We can do specialization before ccp if we want to be able to
propagate these values.

Fixes #1199.
2018-01-15 09:53:23 -05:00
Steven Perron
24f9947050 Move initialization of the const mgr to the constructor.
The current code expects the users of the constant manager to initialize
it with all of the constants in the module.  The problem is that you do
not want to redo the work multiple times.  So I decided to move that
code to the constructor of the constant manager.  This way it will
always be initialized on first use.

I also removed an assert that expects all constant instructions to be
successfully mapped.  This is because not all OpConstant* instruction
can map to a constant, and neither do the OpSpecConstant* instructions.

The real problem is that an OpConstantComposite can contain a member
that is OpUndef.  I tried to treat OpUndef like OpConstantNull, but this
failed because an OpSpecConstantComposite with an OpUndef cannot be
changed to an OpConstantComposite.  Since I feel this case will not be
common, I decided to not complicate the code.

Fixes #1193.
2018-01-12 13:53:21 -05:00
Alan Baker
672494da13 Adding ostream operators for IR structures
* Added for Instruction, BasicBlock, Function and Module
* Uses new disassembly functionality that can disassemble individual
instructions
 * For debug use only (no caching is done)
 * Each output converts module to binary, parses and outputs an
 individual instruction
* Added a test for whole module output
* Disabling Microsoft checked iterator warnings
* Updated check_copyright.py to accept 2018
2018-01-12 11:19:58 -05:00
Alan Baker
eb0c73dad6 Maintain instruction to block mapping in phi insertion
* Changed MemPass::InsertPhiInstructions to set basic blocks for new
phis
* Local SSA elim now maintains instr to block mapping
 * Added a test and confirmed it fails without the updated phis
* IRContext::set_instr_block no longer builds the map if the analysis is
invalid
* Added instruction to block mapping verification to
IRContext::IsConsistent()
2018-01-12 10:16:53 -05:00
Greg Fischer
5eafc00ad5 InsertExtractElim: Optimize through VectorShuffle, Mix
This improves Extract replacement to continue through VectorShuffle.
It will also handle Mix with 0.0 or 1.0 in the a-value of the desired
component.

To facilitate optimization of VectorShuffle, the algorithm was refactored
to pass around the indices of the extract in a vector rather than pass the
extract instruction itself. This allows the indices to be modified as the
algorithm progresses.
2018-01-12 09:41:45 -05:00
Alan Baker
3a054e1ddc Adding additional functionality to ADCE.
Modified ADCE to remove dead globals.
* Entry point and execution mode instructions are marked as alive
* Reachable functions and their parameters are marked as alive
* Instruction deletion now deferred until the end of the pass
* Eliminated dead insts set, added IsDead to calculate that value
instead
* Ported applicable dead variable elimination tests
* Ported dead constant elim tests

Added dead function elimination to ADCE
* ported dead function elim tests

Added handling of decoration groups in ADCE
* Uses a custom sorter to traverse decorations in a specific order
* Simplifies necessary checks

Updated -O and -Os pass lists.
2018-01-10 08:35:48 -05:00
Andrey Tuganov
d54a286c75 Fix validation rules for GLSL pack/unpack 2x32 2018-01-09 13:10:29 -05:00
Alan Baker
1b6cfd3409 Rewriting dead branch elimination.
Pass now paints live blocks and fixes constant branches and switches as
it goes. No longer requires structured control flow. It also removes
unreachable blocks as a side effect. It fixes the IR (phis) before doing
any code removal (other than terminator changes).

Added several unit tests for updated/new functionality.

Does not remove dead edge from a phi node:
* Checks that incoming edges are live in order to retain them
* Added BasicBlock::IsSuccessor
* added test

Fixing phi updates in the presence of extra backedge blocks

* Added tests to catch bug

Reworked how phis are updated

* Instead of creating a new Phi and RAUW'ing the old phi with it, I now
replace the phi operands, but maintain the def/use manager correctly.

For unreachable merge:

* When considering unreachable continue blocks the code now properly
checks whether the incoming edge will continue to be live.

Major refactoring for review

* Broke into 4 major functions
 * marking live blocks
 * marking structured targets
 * fixing phis
 * deleting blocks
2018-01-09 12:21:39 -05:00
Diego Novillo
e5560d64de Fix constant propagation of induction variables.
This fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/1143.
When an instruction transitions from constant to bottom (varying) in the
lattice, we were telling the propagator that the instruction was
varying, but never updating the actual value in the values table.

This led to incorrect value substitutions at the end of propagation.

The patch also re-enables CCP in -O and -Os.
2018-01-08 15:34:35 -05:00
Lei Zhang
44f27f9289 Allow relaxing validation of pointers in logical addressing mode
In HLSL structured buffer legalization, pointer to pointer types
are emitted to indicate a structured buffer variable should be
treated as an alias of some other variable. We need an option to
relax the check of pointer types in logical addressing mode to
catch other validation errors.
2018-01-08 10:36:23 -05:00
Victor Lomuller
e8ad02f3dd Add loop descriptors and some required dominator tree extensions.
Add post-order tree iterator.

Add DominatorTreeNode extensions:
 - Add begin/end methods to do pre-order and post-order tree traversal from a given DominatorTreeNode

Add DominatorTree extensions:
  - Add begin/end methods to do pre-order and post-order tree traversal
  - Tree traversal ignore by default the pseudo entry block
  - Retrieve a DominatorTreeNode from a basic block

Add loop descriptor:
  - Add a LoopDescriptor class to register all loops in a given function.
  - Add a Loop class to describe a loop:
    - Loop parent
    - Nested loops
    - Loop depth
    - Loop header, merge, continue and preheader
    - Basic blocks that belong to the loop

Correct a bug that forced dominator tree to be constantly rebuilt.
2018-01-08 09:31:13 -05:00
Pierre Moreau
7183ad526e Linker code cleanups
Turn `Linker::Link()` into free functions

  As very little information was kept in the Linker class, we can get rid
  of the whole class and have the `Link()` as free functions instead; the
  environment target as well as the consumer are passed along through an
  `spv_context` object.
  The resulting linked_binary is passed as a pointer rather than a
  reference to follow the Google C++ Style guidelines.

  Addresses remaining comments from
  https://github.com/KhronosGroup/SPIRV-Tools/pull/693 about the SPIR-V
  linker.

Fix variable naming in the linker

  Some of the variables were using mixed case, which did not follow the
  Google C++ Style guidelines.

Linker: Use EXPECT_EQ when possible and update some test

* Replace occurrences of ASSERT_EQ by EXPECT_EQ when possible;
* Reformulated some of the error messages;
* Added the symbol name in the error message when there is a type or
  decoration mismatch between the imported and exported declarations.

Opt: List all duplicates removed by RemoveDuplicatePass in the header

Opt: Make the const version of GetLabelInst() return a pointer

  For consistency with the non-const version, as well as other similar
  functions.

Opt: Rename function_end to EndInst()

  As pointed out by dneto0 the previous name was quite confusing and could
  be mistaken with a function returning an end iterator.
  Also change the return type of the const version to a pointer rather
  than a reference, for consistency.

Opt: Add performance comment to RemoveDuplicateTypes and decorations

  This comment was requested during the review of
  https://github.com/KhronosGroup/SPIRV-Tools/pull/693.

Opt: Add comments and fix variable naming in RemoveDuplicatePass

* Add missing comments to private functions;
* Rename variables that were using mixed case;
* Add TODO for moving AreTypesEqual out.

Linker: Remove commented out code and add TODOs

Linker: Merged together strings that were too much splitted

Implement a C++ RAII wrapper around spv_context
2018-01-05 13:28:44 -05:00
Steven Perron
ccb921dd2b Allow getting the base pointer of an image load/store.
In value numbering, we treat loads and stores of images, ie OpImageLoad,
as a memory operation where it is interested in the "base address" of
the instruction.  In those cases, it is an image instruction.

The problem is that `Instruction::GetBaseAddress()` does not account for
the image instructions, so the assert at the end to make sure it found
a valid base address for its addressing mode fails.

The solution is to look at the load/store instruction to determine how
the assertion should be done.

Fixes #1160.
2018-01-05 13:26:10 -05:00
Diego Novillo
716718a5e9 Fix infinite simulation cycles in SSA propagator.
This fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/1159.  I
had missed a nuance in the original algorithm.  When simulating Phi
instructions, the SSA edges out of a Phi instruction should never be
added to the list of edges to simulate.

Phi instructions can be in SSA def-use cycles with other Phi
instructions.  This was causing the propagator to fall into an infinite
loop when the same def-use edge kept being added to the queue.

The original algorithm in the paper specifically separates the visit of
a Phi instruction vs the visit of a regular instruction.  This fix makes
the implementation match the original algorithm.
2018-01-05 10:29:39 -05:00
Pierre Moreau
702852bd22 Opt: Make DecorationManager::HaveTheSameDecorations symmetric
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/1112

Also: Add SpvOpDecorateId to IsAnnotationInst()
2018-01-04 14:07:25 -05:00
Andrey Tuganov
a376b197ae Validator checks out of bounds composite access
1. Added OpCompositeExtract/Insert out of bounds checks where possible
(everything except RuntimeArray)
2. Moved validation of OpCompositeExtract/Insert from validate_id.cpp to
validate_composites.cpp.
2018-01-04 14:02:38 -05:00
Diego Novillo
5f100789fb Handle execution termination instructions when building edges.
This fixes issue https://github.com/KhronosGroup/SPIRV-Tools/issues/1153.

When building CFG edges, edges out of a OpKill and OpUnreachable
instruction should be directed to the CFG's pseudo exit block.
2018-01-03 15:25:03 -05:00
Diego Novillo
135150a1a8 Do not insert Phi nodes in CCP propagator.
In CCP we should not need to insert Phi nodes because CCP never looks at
loads/stores.  This required adjusting two tests that relied on Phi
instructions being inserted.  I changed the tests to have the Phi
instructions pre-inserted.

I also added a new test to make sure that CCP does not try to look
through stores and loads.

Finally, given that CCP does not handle loads/stores, it's better to run
mem2reg before it.  I've changed the -O/-Os schedules to run local
multi-store elimination before CCP.

Although this is just an efficiency fix for CCP, it is
also working around a bug in Phi insertion.  When Phi instructions are
inserted, they are never associated a basic block.  This causes a
segfault when the propagator tries to lookup CFG edges when analyzing
Phi instructions.
2018-01-03 15:12:25 -05:00
Andrey Tuganov
25d396b4a2 Add ExtInst validation pass (GLSL only for now)
Validates all GLSL.std.450 extended instructions.
2018-01-02 16:53:25 -05:00
GregF
226f263cbc Test: Fix linux/gcc defined-but-not-used warnings/errors 2017-12-29 09:22:36 -05:00
Andrey Tuganov
a91aa53893 Disallow Dim=SubpassData for OpImageSparseRead 2017-12-22 09:45:15 -05:00
David Neto
59de6100b5 Add asm, dis support for DebugInfo extended instruction set
Add grammar file for DebugInfo extended instruction set
- Each new operand enum kind in extinst.debuginfo.grammar.json maps
  to a new value in spv_operand_type_t.
- Add new concrete enum operand types for DebugInfo

Generate a C header for the DebugInfo extended instruction set

Add table lookup of DebugInfo extended instrutions

Handle the debug info operand types in binary parser,
disassembler, and assembler.

Add DebugInfo round trip tests for assembler, disassembler

Android.mk: Support DebugInfo extended instruction set

The extinst.debuginfo.grammar.json file is currently part of
SPIRV-Tools source.

It contributes operand type enums, so it has to be processed
along with the core grammar files.

We also generate a C header DebugInfo.h.

Add necessary grammar file processing to Android.mk.
2017-12-22 09:39:36 -05:00
Diego Novillo
4ba9dcc8a0 Implement SSA CCP (SSA Conditional Constant Propagation).
This implements the conditional constant propagation pass proposed in

Constant propagation with conditional branches,
Wegman and Zadeck, ACM TOPLAS 13(2):181-210.

The main logic resides in CCPPass::VisitInstruction.  Instruction that
may produce a constant value are evaluated with the constant folder. If
they produce a new constant, the instruction is considered interesting.
Otherwise, it's considered varying (for unfoldable instructions) or
just not interesting (when not enough operands have a constant value).

The other main piece of logic is in CCPPass::VisitBranch.  This
evaluates the selector of the branch.  When it's found to be a known
value, it computes the destination basic block and sets it.  This tells
the propagator which branches to follow.

The patch required extensions to the constant manager as well. Instead
of hashing the Constant pointers, this patch changes the constant pool
to hash the contents of the Constant.  This allows the lookups to be
done using the actual values of the Constant, preventing duplicate
definitions.
2017-12-21 14:29:45 -05:00
Steven Perron
756b277fb8 Store all enabled capabilities in the feature manger.
In order to keep track of all of the implicit capabilities as well as
the explicit ones, we will add them all to the feature manager.  That is
the object that needs to be queried when checking if a capability is
enabled.

The name of the "HasCapability" function in the module was changed to
make it more obvious that it does not check for implied capabilities.

Keep an spv_context and AssemblyGrammar in IRContext
2017-12-21 11:14:53 -05:00
Alan Baker
1ab8ad654a Fixing bugs in type manager memory management
* changed the way duplicate types are removed to stop copying
instructions
* Reworked RemoveDuplicatesPass::AreTypesSame to use type manager and
type equality
* Reworked TypeManager memory management to store a pool of unique
pointers of types
 * removed unique pointers from id map
 * fixed instances where free'd memory could be accessed
2017-12-21 08:59:06 -05:00
David Neto
c9a881ecc6 Make a string parameter const ref 2017-12-19 18:42:35 -05:00
Pierre Moreau
424f744db1 Opt: Fix implementation and comment of AreDecorationsTheSame
Target should not be ignored when comparing decorations in RemoveDuplicates
Opt: Remove unused code in RemoveDuplicateDecorations
2017-12-19 15:36:47 -05:00
Steven Perron
79a00649b4 Allow pointers to pointers in logical addressing mode.
A few optimizations are updates to handle code that is suppose to be
using the logical addressing mode, but still has variables that contain
pointers as long as the pointer are to opaque objects.  This is called
"relaxed logical addressing".

|Instruction::GetBaseAddress| will check that pointers that are use meet
the relaxed logical addressing rules.  Optimization that now handle
relaxed logical addressing instead of logical addressing are:

 - aggressive dead-code elimination
 - local access chain convert
 - local store elimination passes.
2017-12-19 14:29:14 -05:00
Steven Perron
b86eb6842b Convert private variables to function scope.
When a private variable is used in a single function, it can be
converted to a function scope variable in that function.  This adds a
pass that does that.  The pass can be enabled using the option
`--private-to-local`.

This transformation allows other transformations to act on these
variables.

Also moved `FindPointerToType` from the inline class to the type manager.
2017-12-19 14:21:04 -05:00
David Neto
8135dd6375 More validation on primitive instructions
- Test validation success for OpEmitVertex OpEndPrimitive
- Test missing capabilities for primitive instructions
- Primitive instructions require Geometry execution model
2017-12-19 13:26:07 -05:00
Jesus Carabano
4dbcef62ee validate & test of literal's upper bits
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/660
2017-12-19 13:19:56 -05:00
Andrey Tuganov
dbc3a662c6 Image Operand Sample allows sparse image opcodes
@ehsannas had filed an issue against SPIR-V spec, concerning
Image Operands section (3.14):
Sample
A following operand is the sample number of the sample to use. Only
valid with OpImageFetch, OpImageRead, and OpImageWrite.

Relaxing the check to allow OpImageSparseRead and
OpImageSparseFetch to fix failing tests.
2017-12-18 11:21:38 -05:00
Alan Baker
616908503d Improving the usability of the type manager. The type manager hashes
types. This allows the lookup of type declaration ids from arbitrarily
constructed types. Users should be cautious when dealing with non-unique
types (structs and potentially pointers) to get the exact id if
necessary.

* Changed the spec composite constant folder to handle ambiguous composites
* Added functionality to create necessary instructions for a type
* Added ability to remove ids from the type manager
2017-12-18 08:20:56 -05:00
GregF
0f80406315 ADCE: Only mark true breaks and continues of live loops
This fixes issue #1075

- Mark continue when conditional branch with merge block.
  Only mark if merge block is not continue block.

- Handle conditional branch break with preceding merge
2017-12-15 11:53:57 -05:00
Jeremy Hayes
cdfbf26c13 Add primitive instruction validation pass 2017-12-15 09:53:29 -05:00
Andrey Tuganov
af7d5799a5 Refactor include of latest spir-v header versions 2017-12-14 11:18:20 -05:00
Andrey Tuganov
532b327d4d Add validation rules for atomic instructions
Validates all OpAtomicXXX instructions.
2017-12-13 18:29:38 -05:00
Greg Fischer
22faa2b083 ADCE: Empty Loop Elimination
This entirely eliminates loops which do not contain live code.
2017-12-12 13:53:15 -05:00
Steven Perron
07ce16d1e7 Set the parent for basic blocks during inlining.
Inlining is not setting the parent (function) for each basic block.
This can cause problems for later optimizations.  The solution is to set
the parent for each new block just before it is linked into the
function.
2017-12-12 13:39:08 -05:00
Andrey Tuganov
c520d43649 Add validator checks for sparse image opcodes 2017-12-12 12:04:23 -05:00
Pierre Moreau
12447d8465 Support OpenCL 1.2 and 2.0 target environments
include: Add target environment enums for OpenCL 1.2 and 2.0

Validator: Validate OpenCL capabilities

Update validate capabilities to handle embedded profiles

Add test for OpenCL capabilities validation

Update messages to mention the OpenCL profile used

Re-format val_capability_test.cpp
2017-12-12 11:35:39 -05:00
David Neto
7ba59ac8dc Force gtest to expose ::testing::Combine
Work around faulty logic in googletest, where ::testing::Combine
is accidentally disabled for VS 2017.
See https://github.com/google/googletest/issues/1352
2017-12-12 08:58:51 -05:00
Andrey Tuganov
dbd8d0e7b8 Reenable OpCopyObject validation rules
Vulkan CTS fix has been submitted.
2017-12-11 12:33:11 -05:00
Alan Baker
867451f49e Add scalar replacement
Adds a scalar replacement pass. The pass considers all function scope
variables of composite type. If there are accesses to individual
elements (and it is legal) the pass replaces the variable with a
variable for each composite element and updates all the uses.

Added the pass to -O
Added NumUses and NumUsers to DefUseManager
Added some helper methods for the inst to block mapping in context
Added some helper methods for specific constant types

No longer generate duplicate pointer types.

* Now searches for an existing pointer of the appropriate type instead
of failing validation
* Fixed spec constant extracts
* Addressed changes for review
* Changed RunSinglePassAndMatch to be able to run validation
 * current users do not enable it

Added handling of acceptable decorations.

* Decorations are also transfered where appropriate

Refactored extension checking into FeatureManager

* Context now owns a feature manager
 * consciously NOT an analysis
 * added some test
* fixed some minor issues related to decorates
* added some decorate related tests for scalar replacement
2017-12-11 10:51:13 -05:00
GregF
78c025abe9 MultiStore: Support OpVariable Initialization
Treat an OpVariable with initialization as if it was an OpStore.
With PR #1073, this completes work for issue #1017.
2017-12-11 10:37:14 -05:00
GregF
c6fdf68c2f SingleStore: Support OpVariable Initialization
Treat an OpVariable with initialization as if it was an OpStore.
This fixes issue #1017.
2017-12-08 16:02:14 -05:00
Steven Perron
5d602abd66 Add global redundancy elimination
Adds a pass that looks for redundant instruction in a function, and
removes them.  The algorithm is a hash table based value numbering
algorithm that traverses the dominator tree.

This pass removes completely redundant instructions, not partially
redundant ones.
2017-12-07 18:35:38 -05:00
Steven Perron
851e1ad985 Kill names and decoration in inlining.
Currently when inlining a call, the name and decorations for the result of the
call is not deleted.  This should be changed.  Added a test for this as well.

This fixes issue #622.
2017-12-07 12:20:45 -05:00
Victor Lomuller
731d1899b1 Add depth first iterator for trees
- Add generic depth first iterator
 - Update the dominator tree to use this iterator instead of "randomly"
   iterate over the nodes
2017-12-07 10:07:56 -05:00
Stephen McGroarty
8ba68fa9b9 Dominator Tree Analysis (#3)
Support for dominator and post dominator analysis on ir::Functions. This patch contains a DominatorTree class for building the tree and DominatorAnalysis and DominatorAnalysisPass classes for interfacing and caching the built trees.
2017-12-05 22:59:43 -05:00
Lei Zhang
b93c066b0b CMake: allow both SPIRV-Headers and spirv-headers
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/1057
2017-12-05 14:41:31 -05:00
Andrey Tuganov
94e3e7b8ef Add composite instruction validation pass
Validates instructions in the opcode range from OpVectorExtractDynamic
to OpTranspose.
2017-12-05 10:15:51 -05:00
Andrey Tuganov
bf184310b2 Fix some of the known issues in image validation
Applied some of the spec clarifications made in conversation with
@johnkslang.
2017-12-04 18:57:34 -05:00
Diego Novillo
e9ecc0cbfd Remove cfg_ field from SSAPropagator class - NFC.
When I moved the CFG into IRContext
(https://github.com/KhronosGroup/SPIRV-Tools/pull/1019), I forgot to
update SSAPropagator to stop requiring one.

Fixed with this patch.
2017-12-04 15:28:21 -05:00
Steven Perron
65046eca7c Change IRContext::KillInst to delete instructions.
The current method of removing an instruction is to call ToNop.  The
problem with this is that it leaves around an instruction that later
passes will look at.  We should just delete the instruction.

In MemPass there is a utility routine called DCEInst.  It can delete
essentially any instruction, which can invalidate pointers now that they
are actually deleted.  The interface was changed to add a call back that
can be used to update any local data structures that contain
ir::Intruction*.
2017-12-04 11:07:45 -05:00
Steven Perron
b35b52f97b Compute value number when the value table is constructed.
Computing the value numbers on demand, as we do now, can lead to
different results depending on the order in which the users asks for
the value numbers.  To make things more stable, we compute them ahead
of time.
2017-12-04 11:02:04 -05:00
Lei Zhang
0dd4ee27b1 Fix Dref type check in validator
Dref should be of 32-bit scalar floating type.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/1012
2017-12-01 10:17:45 -05:00
David Neto
3c2e4c7d99 Fix validation of group ops in SPV_AMD_shader_ballot
This needs custom code since the rules from the extension
are not encoded in the grammar.

Changes are:
- The new group instructions don't require Group capability
  when the extension is declared.
- The Reduce, InclusiveScan, ExclusiveScan normally require the Kernel
  capability, but don't when the extension is declared.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/991
2017-11-30 10:26:04 -05:00
Andrey Tuganov
e1ceff9f54 Validate OpTypeImage and OpTypeSampleImage
Added new validation rules to the validate image pass.
2017-11-29 13:21:04 -05:00
GregF
8dd3d93cf6 AggressiveDCE: Add merge and continue branches for live loop.
This ensures that an if-break is not eliminated from a loop.

This fixes issue #989
2017-11-29 09:56:21 -05:00
Diego Novillo
8ffed974ac Fix windows build. unsigned vs signed comparison in EXPECT_EQ. 2017-11-28 09:13:42 -05:00
Diego Novillo
74327845aa Generic value propagation engine.
This class implements a generic value propagation algorithm based on the
conditional constant propagation algorithm proposed in

     Constant propagation with conditional branches,
     Wegman and Zadeck, ACM TOPLAS 13(2):181-210.

The implementation is based on

     A Propagation Engine for GCC
     Diego Novillo, GCC Summit 2005
     http://ols.fedoraproject.org/GCC/Reprints-2005/novillo-Reprint.pdf

The purpose of this implementation is to act as a common framework for any
transformation that needs to propagate values from statements producing new
values to statements using those values.
2017-11-27 23:32:06 -05:00
Diego Novillo
83228137e1 Re-format source tree - NFC.
Re-formatted the source tree with the command:

$ /usr/bin/clang-format -style=file -i \
    $(find include source tools test utils -name '*.cpp' -or -name '*.h')

This required a fix to source/val/decoration.h.  It was not including
spirv.h, which broke builds when the #include headers were re-ordered by
clang-format.
2017-11-27 14:31:49 -05:00
Andrey Tuganov
d8b2013ecf Derivative opcodes require Fragment exec model
Added validator check that all derivative opcodes require Fragment
execution model.
2017-11-27 12:05:25 -05:00
Andrey Tuganov
c170afd93b Relaxed OpImageWrite texel type check 2017-11-24 14:31:08 -05:00
Andrey Tuganov
f84f266977 Relaxed OpImageRead validation rules
Removed the check that result type of OpImageRead should be a vector4.
Will reenable/adapt once the spec is clarified on what the right
dimension should be.
2017-11-24 10:12:24 -05:00
Andrey Tuganov
3e08a3f718 Add validation checks for Execution Model
Currently checks that these instructions are called from entry points
with Fragment execution model.
OpImageImplicit*
OpImageQueryLod
OpKill
2017-11-23 23:38:03 -05:00
Alan Baker
746bfd210a Adding new def -> use mapping container
Replaced representation of uses

* Changed uses from unordered_map<uint32_t, UseList> to
set<pairInstruction*, Instruction*>>
* Replaced GetUses with ForEachUser and ForEachUse functions
* updated passes to use new functions
* partially updated tests
* lots of cleanup still todo

Adding an unique id to Instruction generated by IRContext

Each instruction is given an unique id that can be used for ordering
purposes. The ids are generated via the IRContext.

Major changes:
* Instructions now contain a uint32_t for unique id and a cached context
pointer
 * Most constructors have been modified to take a context as input
 * unfortunately I cannot remove the default and copy constructors, but
 developers should avoid these
* Added accessors to parents of basic block and function
* Removed the copy constructors for BasicBlock and Function and replaced
them with Clone functions
* Reworked BuildModule to return an IRContext owning the built module
 * Since all instructions require a context, the context now becomes the
basic unit for IR
* Added a constructor to context to create an owned module internally
* Replaced uses of Instruction's copy constructor with Clone whereever I
found them
* Reworked the linker functionality to perform clones into a different
context instead of moves
* Updated many tests to be consistent with the above changes
 * Still need to add new tests to cover added functionality
* Added comparison operators to Instruction

Adding tests for Instruction, IRContext and IR loading

Fixed some header comments for BuildModule

Fixes to get tests passing again

* Reordered two linker steps to avoid use/def problems
* Fixed def/use manager uses in merge return pass
* Added early return for GetAnnotations
* Changed uses of Instruction::ToNop in passes to IRContext::KillInst

Simplifying the uses for some contexts in passes
2017-11-23 16:40:02 -05:00
Lei Zhang
b02c9a5802 Allow derived access chain without uses in access chain conversion 2017-11-23 16:00:28 -05:00
Andrey Tuganov
ab892f7bd6 Add derivatives validation pass
Checks operands of instructions in opcode range from OpDPdx to
OpFwidthCoarse.
2017-11-23 14:17:10 -05:00
David Neto
c2999273d9 Move SetContextMessageConsumer into libspirv namespace
Avoid polluting the global namespace.
2017-11-23 13:56:12 -05:00
Steven Perron
28c415500d Create a local value numbering pass
Creates a pass that removes redundant instructions within the same basic
block.  This will be implemented using a hash based value numbering
algorithm.

Added a number of functions that check for the Vulkan descriptor types.
These are used to determine if we are variables are read-only or not.

Implemented a function to check if loads and variables are read-only.
Implemented kernel specific and shader specific versions.

A big change is that the Combinator analysis in ADCE is factored out
into the IRContext as an analysis. This was done because it is being
reused in the value number table.
2017-11-23 11:45:09 -05:00
Andrey Tuganov
f407ae2b50 Validator pass for image instructions
Includes validation rules for OpImageXXX and ImageOperand.

Doesn't include OpTypeImage and OpImageSparseXXX.

Disabled an invalid test.
2017-11-22 14:34:15 -05:00
GregF
e28edd458b Optimize loads/stores on nested structs
Also fix LocalAccessChainConvert test: nested structs now convert

Add InsertExtractElim test for nested struct
2017-11-21 17:56:03 -05:00
Andrey Tuganov
250a235a8d Add new compression algorithm and models
Add new "short descriptor" algorithm to MARK-V codec.

Add three shader compression models:
lite - fast, poor compression
mid - balanced
max - best compression
2017-11-21 17:32:58 -05:00
Alan Baker
a771713e42 Adding an unique id to Instruction generated by IRContext
Each instruction is given an unique id that can be used for ordering
purposes. The ids are generated via the IRContext.

Major changes:
* Instructions now contain a uint32_t for unique id and a cached context
pointer
 * Most constructors have been modified to take a context as input
 * unfortunately I cannot remove the default and copy constructors, but
 developers should avoid these
* Added accessors to parents of basic block and function
* Removed the copy constructors for BasicBlock and Function and replaced
them with Clone functions
* Reworked BuildModule to return an IRContext owning the built module
 * Since all instructions require a context, the context now becomes the
basic unit for IR
* Added a constructor to context to create an owned module internally
* Replaced uses of Instruction's copy constructor with Clone whereever I
found them
* Reworked the linker functionality to perform clones into a different
context instead of moves
* Updated many tests to be consistent with the above changes
 * Still need to add new tests to cover added functionality
* Added comparison operators to Instruction
* Added an internal option to LinkerOptions to verify merged ids are
unique
* Added a test for the linker to verify merged ids are unique

* Updated MergeReturnPass to supply a context
* Updated DecorationManager to supply a context for cloned decorations

* Reworked several portions of the def use tests in anticipation of next
set of changes
2017-11-20 17:49:10 -05:00
Lei Zhang
4019bcfd57 Fix hard-coded header path
If SPIRV-Tools is used as an external project and have
googletest being kept in the same directory as it, we
won't have gmock-matchers.h in external/. This will
result in a compilation error.

Use gmock.h instead.
2017-11-17 19:54:55 -05:00
Steven Perron
eb4653a67f Add the decoration manager to the IRContext.
To make the decoration manger available everywhere, and to reduce the
number of times it needs to be build, I add one the IRContext.

As the same time, I move code that modifies decoration instruction into
the IRContext from mempass and the decoration manager.  This will make
it easier to keep everything up to date.

This should take care of issue #928.
2017-11-15 12:48:03 -05:00
Alan Baker
a92d69b43d Initial implementation of merge return pass.
Works with current DefUseManager infrastructure.

Added merge return to the standard opts.

Added validation to passes.

Disabled pass for shader capabilty.
2017-11-15 10:27:04 -05:00
Diego Novillo
98281ed411 Add analysis to compute mappings between instructions and basic blocks.
This analysis builds a map from instructions to the basic block that
contains them.  It is accessed via get_instr_block().  Once built, it is kept
up-to-date by the IRContext, as long as instructions are removed via
KillInst.

I have not yet marked passes that preserve this analysis. I will do it
in a separate change.

Other changes:

- Add documentation about analysis values requirement to be powers of 2.
- Force a re-build of the def-use manager in tests.
- Fix AllPreserveFirstOnlyAfterPassWithChange to use the
  DummyPassPreservesFirst pass.
- Fix sentinel value for IRContext::Analysis enum.

- Fix logic for checking if the instr<->block mapping is valid in KillInst.
2017-11-13 13:21:48 -05:00
Daniel Schürmann
a76d0977ac Fix decorations of inlined functions.
Fixes issue #728.  Currently the inliner is not generating decorations for
inlined code which corresponds to function code which has decorations. An
example of decorations that are relevant: RelaxedPrecision, NoContraction.

The solution is to replicate the decoration during inlining.
2017-11-13 12:49:25 -05:00
David Neto
76555bd4ba Tests: Add optional dependency on Effcee stateful matcher
Add Effcee as an optional dependency for use in tests.  In future it will
be a required dependency.

Effcee is a stateful pattern matcher that has much of the functionality
of LLVM's FileCheck, except in library form.  Effcee makes it much easier
to write tests for optimization passes.

Demonstrate its use in a test for the strength-reduction pass.

Update README.md with example commands of how to get sources.

Update Appveyor and Travis-CI build rules.

Also: Include test libraries if not SPIRV_SKIP_TESTS
- SPIRV_SKIP_TESTS is implied by SPIRV_SKIP_EXECUTABLES
2017-11-13 11:34:21 -05:00
Steven Perron
f32d11f74b Add the IRContext (part 2): Add def-use manager
This change will move the instances of the def-use manager to the
IRContext.  This allows it to persists across optimization, and does
not have to be rebuilt multiple times.

Added test to ensure that the IRContext is validating and invalidating
the analyses correctly.
2017-11-08 13:35:34 -05:00
GregF
ac04b2faea Opt: Fix HasLoads to not report decoration as load. 2017-11-07 17:39:58 -05:00
Nuno Subtil
2dddb8193b Validate storage class of target pointer for OpStore 2017-11-02 13:44:11 -04:00
Steven Perron
476cae6f7d Add the IRContext (part 1)
This is the first part of adding the IRContext.  This class is meant to
hold the extra data that is build on top of the module that it
owns.

The first part will simply create the IRContext class and get it passed
to the passes in place of the module.  For now it does not have any
functionality of its own, but it acts more as a wrapper for the module.

The functions that I added to the IRContext are those that either
traverse the headers or add to them.  I did this because we may decide
to have other ways of dealing with these sections (for example adding a
type pool, or use the decoration manager).

I also added the function that add to the header because the IRContext
needs to know when an instruction is added to update other data
structures appropriately.

Note that there is still lots of work that needs to be done.  There are
still many places that change the module, and do not inform the context.
That will be the next step.
2017-10-31 13:46:05 -04:00
Nuno Subtil
d861ceffd4 Add validation for OpBranchConditional 2017-10-31 12:05:20 -04:00
GregF
94bec26afe ADCE: Dead if elimination
Mark structured conditional branches live only if one or more instructions
in their associated construct is marked live. After closure, replace dead
structured conditional branches with a branch to its merge and remove
dead blocks.

ADCE: Dead If Elim: Remove duplicate StructuredOrder code

Also generalize ComputeStructuredOrder so that the caller can specify the
root block for the order. Phi insertion uses pseudo_entry_block and adce and
dead branch elim use the first block of the function.

ADCE: Dead If Elim: Pull redundant code out of InsertPhiInstructions

ADCE: Dead If Elim: Encapsulate CFG Cleanup Initialization

ADCE: Dead If Elim: Remove redundant code from ADCE initialization

ADCE: Dead If: Use CFGCleanup to eliminate newly dead blocks

Moved bulk of CFG Cleanup code into MemPass.
2017-10-31 11:51:30 -04:00
Steven Perron
716138ee14 Add option to relax validation of store types.
There are a number of users of spriv-opt that are hitting errors
because of stores with different types.  In general, this is wrong, but,
in these cases, the types are the exact same except for decorations.

The options is "--relax-store-struct", and it can be used with the
validator or the optimizer.

We assume that if layout information is missing it is consistent.  For
example if one struct has a offset of one of its members, and the other
one does not, we will still consider them as being layout compatible.
The problem will be if both struct has and offset decoration for
corresponding members, and the offset are different.
2017-10-28 18:48:21 -04:00
Steven Perron
94dc66b74d Change the sections in the module to use the InstructionList class.
This change will replace a number of the
std::vector<std::unique_ptr<Instruction>> member of the module to
InstructionList.  This is for consistency and to make it easier to
delete instructions that are no longer needed.
2017-10-25 15:52:06 -04:00
Lei Zhang
063dbea0f1 Turn all function static non-POD variables into global POD variables
Function static non-POD data causes problems with DLL lifetime.
This pull request turns all static info tables into strict POD
tables. Specifically, the capabilities/extensions field of
opcode/operand/extended-instruction table are turned into two
fields, one for the count and the other a pointer to an array of
capabilities/extensions. CapabilitySet/EnumSet are not used in
the static table anymore, but they are still used for checking
inclusion by constructing on the fly, which should be cheap for
the majority cases.

Also moves all these tables into the global namespace to avoid
C++11 function static thread-safe initialization overhead.
2017-10-25 15:44:19 -04:00
Józef Kucia
90862fe4b1 Validate SpvOpVectorShuffle 2017-10-24 11:45:03 -04:00
Jesus Carabano
13e6598947 restrict opcodes targeting OpDecorationGroup 2017-10-24 11:39:08 -04:00
Daniel Schürmann
97990dc907 Fixed --eliminate-common-uniform so that it does not eliminate loads of volatile variables. 2017-10-24 11:17:33 -04:00
Andrey Tuganov
cfd95f3d5a Refactored compression debugger
Markv codec now receives two optional callbacks:
LogConsumer for internal codec logging
DebugConsumer for testing if encoding->decoding produces the original
results.
2017-10-23 22:12:40 -04:00
Steven Perron
5834719fc1 Add pass to remove dead variables at the module level.
There does not seem to be any pass that remove global variables.  I
think we could use one.  This pass will look specifically for global
variables that are not referenced and are not exported.  Any decoration
associated with the variable will also be removed.  However, this could
cause types or constants to become unreferenced.  They will not be
removed.  Another pass will have to be called to remove those.
2017-10-23 13:57:05 -04:00
David Neto
2436794736 Optimizer: OpModuleProcessed is in its own layout section
This is a recent decision from the SPIR WG.  The spec update
has not yet been published.
Khronos SPIR-V internal issue 199
2017-10-23 10:46:37 -04:00
Andrey Tuganov
39e25fd8ab Add validation pass for conversion instructions
The pass checks correctness of operands of instruction in opcode range
OpConvertFToU - OpBitset.

Disabled invalid tests

Disabled UConvert validation until Vulkan CTS can catch up.

Add validate_conversion to Android.mk

Also remove duplicate entry in CMakeLists.txt.
2017-10-20 13:51:24 -04:00
Steven Perron
bb7802b18c Change BasicBlock to use InstructionList to hold instructions.
This is the first step in replacing the std::vector of Instruction
pointers to using and intrusive linked list.

To this end, we created the InstructionList class.  It inherites from
the IntrusiveList class, but add the extra concept of ownership.  An
InstructionList owns the instruction that are in it.  This is to be
consistent with the current ownership rules where the vector owns the
instruction that are in it.

The other larger change is that the inst_ member of the BasicBlock class
was changed to using the InstructionList class.

Added test for the InsertBefore functions, and making sure that the
InstructionList destructor will delete the elements that it contains.

I've also add extra comments to explain ownership a little better.
2017-10-20 12:37:44 -04:00
David Neto
8ec62deb23 The reviewed cfg_cleanup optimize pass 2017-10-19 15:28:09 -04:00
Diego Novillo
c75704ec08 CFG cleanup pass - Remove unreachable blocks.
- Adds a new pass CFGCleanupPass.  This serves as an umbrella pass to
  remove unnecessary cruft from a CFG.
- Currently, the only cleanup operation done is the removal of
  unreachable basic blocks.
- Adds unit tests.
- Adds a flag to spirvopt to execute the pass (--cfg-cleanup).
2017-10-19 15:16:29 -04:00
GregF
1a9061a2be ADCE: Treat privates like locals in entry point with no calls
This is needed for ongoing legalization of HLSL. It allows removal
of accesses to textures/buffers that are not used.
2017-10-13 15:39:14 -04:00
David Neto
941a234132 Validator: Test OpReturnValue type check
The check already existed.  I added a test for it.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/876
2017-10-13 15:19:13 -04:00
Andrey Tuganov
8de8dd8c8c Reenable validate type unique pass
Vulkan CTS patch fixing the instances of non-unique type declaration in
autogenerated code has recently been submitted.
2017-10-12 15:46:06 -04:00
Andrey Tuganov
2401fc0a72 Refactored MARK-V API
- switched from C to C++
- moved MARK-V model creation from backend to frontend
- The same MARK-V model object can be used to encode/decode multiple
files
- Added MARK-V model factory (currently only one option)
- Added --validate option to spirv-markv (run validation while
encoding/decoding)
2017-10-12 15:40:40 -04:00
Andrey Tuganov
b54997e6eb Validator checks OpReturn called from void func
Added check into validate_cfg which checks that OpReturn is not called
from functions which are supposed to return a value.
2017-10-12 15:32:32 -04:00
Steven Perron
720beb161a Generic intrusive linked list class.
This commit is the initial implementation of the intrusive linked list
class.  It includes the implementation in the header files, and unit
test.

The iterators are circular: incrementing end() gives begin() and
decrementing begin() gives end().  Also made it valid to
decrement end().

Expliticly defines move constructor and move assignment
- Visual Studio 2013 does not implicitly generate the move constructor or
  move assignments.  So they need to be explicit, otherwise it will try to
  use the copy constructor, which we explicitly deleted.
- Can't use "= default" either.
  Seems like VS2013 does not support explicitly using the default move
  constructors and move assignments, so I wrote them out.
2017-10-12 12:40:18 -04:00
GregF
63064bd9eb DeadBranchElim: Add dead case elimination
Expands dead branch elimination to eliminate dead switch cases. It also
changes dbe to eliminate orphaned merge blocks and recursively eliminate
any blocks thereby orphaned.
2017-10-12 11:44:05 -04:00
Pierre Moreau
86627f7b3f Implement Linker (module combiner)
Add extra iterators for ir::Module's sections
Add extra getters to ir::Function
Add a const version of BasicBlock::GetLabelInst()

Use the max of all inputs' version as version

Split debug in debug1 and debug2
- Debug1 instructions have to be placed before debug2 instructions.

Error out if different addressing or memory models are found

Exit early if no binaries were given

Error out if entry points are redeclared

Implement copy ctors for Function and BasicBlock
- Visual Studio ends up generating copy constructors that call deleted
  functions while compiling the linker code, while GCC and clang do not.
  So explicitly write those functions to avoid Visual Studio messing up.

Move removing duplicate capabilities to its own pass

Add functions running on all IDs present in an instruction

Remove duplicate SpvOpExtInstImport

Give default options value for link functions

Remove linkage capability if not making a library

Check types before allowing to link

Detect if two types/variables/functions have different decorations

Remove decorations of imported variables/functions and their types

Add a DecorationManager

Add a method for removing all decorations of id

Add methods for removing operands from instructions

Error out if one of the modules has a non-zero schema

Update README.md to talk about the linker

Do not freak out if an imported built-in variable has no export
2017-10-06 18:33:53 -04:00
GregF
da04f5640e AggressiveDCE: Fix to not treat parameter memory refs as local
This fixes a bug that incorrectly deletes stores to parameters, which
can be used to return values from functions.
2017-10-05 10:59:45 -04:00
Pierre Moreau
c87e9671ab Compact-ids pass should update the header ID bound 2017-10-03 11:24:28 -04:00
David Neto
169266e9b8 DiagnosticStream move ctor moves output duties to new object
- Take over contents of the expiring message stream
- Prevent the expiring object from emitting anything during destruction
2017-10-03 11:23:54 -04:00
David Neto
77feb8dd03 Compact-ids pass should update instruction's result_id member
Also update the result type field.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/827
2017-09-27 08:31:05 -04:00
Andrey Tuganov
64d5e5214f Add bitwise operations validator pass
The pass checks correctness of operand types of all bitwise instructions
(opcode range from SpvOpShiftRightLogical to SpvOpBitCount).
2017-09-26 14:22:37 -04:00
Andrey Tuganov
dcf42433a6 Add remaining opcodes to arithmetics validation
Add validation rules for:
- OpIAddCarry
- OpISubBorrow
- OpUMulExtended
- OpSMulExtended

Includes some refactoring of old code.
2017-09-26 11:47:34 -04:00
Steven Perron
e43c91046b Create the dead function elimination pass
Creates a pass called eliminate dead functions that looks for functions
that could never be called, and deletes them from the module.

To support this change a new function was added to the Pass class to
traverse the call trees from diffent starting points.

Includes a test to ensure that annotations are removed when deleting a
dead function.  They were not, so fixed that up as well.

Did some cleanup of the assembly for the test in pass_test.cpp.  Trying
to make them smaller and easier to read.
2017-09-26 11:18:06 -04:00
Andrey Tuganov
c25b5bea35 Add SPIRV_SPIRV_COMPRESSION option to cmake
The option is off by default.
cmake -DSPIRV_BUILD_COMPRESSION=ON ..
enables the compression lib, executable, and test build.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/834
2017-09-25 14:37:08 -04:00
Andrey Tuganov
3f5e1a91ae Validator: fix logicals pass for OpSelect pointers
OpSelect works with pointers also when capability
VariablePointersStorageBuffer is declared (before worked only with
capability VariablePointers).
2017-09-21 16:12:14 -04:00
David Neto
33b879c105 elim-multi-store: only patch loop header phis that we created
There can already be OpPhi instructions in a loop header that
are unrelated to the optimization.  We should not be patching those.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/826
2017-09-21 10:01:30 -04:00
Andrey Tuganov
cf85ad1429 Add validate logicals pass to the validator
New pass checks operands of all instructions listed under
3.32.15. Relational and Logical Instructions
2017-09-20 10:37:12 -04:00
Andrey Tuganov
4e3cc2f57f Refactored validate_aritmetics.cpp
Improved error messages and readability.
2017-09-20 10:30:54 -04:00
Steven Perron
e4c7d8e748 Add strength reduction; for now replace multiply by power of 2
Create a new optimization pass, strength reduction, which will replace
integer multiplication by a constant power of 2 with an equivalent bit
shift.  More changes could be added later.

- Does not duplicate constants

- Adds vector |Concat| utility function to a common test header.
2017-09-18 17:01:36 -04:00
David Neto
a91cecfefc Recognize SPV_AMD_shader_fragment_mask 2017-09-14 10:37:18 -04:00
Andrey Tuganov
c6dfc11880 Add new checks to validate arithmetics pass
New operations:
- OpDot
- OpVectorTimesScalar
- OpMatrixTimesScalar
- OpVectorTimesMatrix
- OpMatrixTimesVector
- OpMatrixTimesMatrix
- OpOuterProduct
2017-09-08 11:08:41 -04:00
David Neto
c843ef8ab5 validator: OpModuleProcessed allowed in layout section 7c
Recent spec fix from SPIR Working group:
  Allow OpModuleProcessed after debug names, but before any
  annotation instructions.
2017-09-07 17:45:51 -04:00
Andrey Tuganov
b36acbec0e Update MARK-V to version 1.01
Includes:
- Multi-sequence move-to-front
- Coding by id descriptor
- Statistical coding of non-id words
- Joint coding of opcode and num_operands

Removed explicit form Huffman codec constructor
- The standard use case for it is to be constructed from initializer list.

Using serialization for Huffman codecs
2017-09-06 16:03:16 -04:00
David Neto
25ddfec08e Inliner: Fix LoopMerge when inline into loop header of multi block loop
This adapts the fix for the single-block loop.  Split the loop like
before.  But when we move the OpLoopMerge back to the loop header,
redirect the continue target only when the original loop was a single
block loop.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/800
2017-09-05 19:46:24 -04:00
Andrey Tuganov
82df4bbd68 Add validation pass for arithmetic operations
The pass checks if arithmetic operations (such as OpFMul) receive
correct operands.
2017-09-05 12:21:53 -04:00
David Neto
0d3b8329a4 Make enums for all currently published extensions
Use the list from the SPIR-V registry page.  Also, capture it as
a string so it's much easier to update via copy-paste.

The validator will accept modules that declare these known
extensions.  However, we might not know about new tokens or
instructions declared in them.  For that we need grammar updates
applied to SPIRV-Headers.
2017-09-02 15:10:52 -04:00
David Neto
860c4197b0 Inliner: Remap callee entry block id to single-trip loop header
Otherwise cloned phis can be invalid.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/790
2017-09-01 15:56:14 -04:00
David Neto
efff5fabfa Inline: Fix single-block loop caller cases
If the caller block is a single-block loop and inlining will
replace the caller block by several blocks, then:
- The original OpLoopMerge instruction will end up in the *last*
  such block.  That's the wrong place to put it.
- Move it back to the end of the first block.
- Update its Continue Target ID to point to the last block

We also have to take care of cases where the inlined code
begins with a structured header block.  In this case
we need to ensure the restored OpLoopMerge does not appear
in the same block as the merge instruction from the callee's
first block.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/787
2017-09-01 15:47:17 -04:00
David Neto
e6279cde7a Update tests for new preferred name as ShaderViewportIndexLayerEXT
This reacts to a recent update to SPIRV-Headers
2017-09-01 10:29:57 -04:00
Andrey Tuganov
725284c2ef Extension allows multiple same OpTypePointer types
SPV_KHR_variable_pointers allows OpTypePointer to declare multiple
pointer identical types.

https://github.com/KhronosGroup/SPIRV-Tools/issues/781
2017-09-01 10:14:15 -04:00
GregF
7c3de19ce7 DeadBranchElim: Fix dead block detection to ignore backedges
- DeadBranchElim: Make sure to mark orphan'd merge blocks and continue
targets as live.
- Add test with loop in dead branch
- Add test that orphan'd merge block is handled.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/776
2017-08-30 13:37:46 -04:00
GregF
a699d1ade7 Inline: Fix remapping of non-label forward references in callee phi 2017-08-29 18:35:05 -06:00
Andrey Tuganov
d41a52415a Fix encode zero bits on word boundary bug
Bit stream writer was manifesting incorrect behaviour when the following
two conditions were met:
- writer was on 64-bit word boundary
- WriteBits was invoked with num_bits=0 (can happen when a Huffman codec has only one
value)

The bug was causing very rare sporadic corruption which was detected by
tests after a random experimental change in MARK-V model.
2017-08-28 13:36:39 -04:00
David Neto
851ff8395a Updated capabilites for SampleMask
SPIRV-Headers recently fixed the capability dependency
for SampleMask.  It depends on Shader, not SampleRateShading
2017-08-24 10:00:39 -04:00
GregF
429ca05b3f Opt: Create InlineOpaquePass
Only inline calls to functions with opaque params or return

TODO: Handle parameter type or return type where the opqaue
type is buried within an array.
2017-08-18 18:04:30 -04:00
GregF
c8c86a0d36 Opt: Have "size" passes process full entry point call tree.
Includes code to deal correctly with OpFunctionParameter. This
is needed by opaque propagation which may not exhaustively inline
entry point functions.

Adds ProcessEntryPointCallTree: a method to do work on the
functions in the entry point call trees in a deterministic order.
2017-08-18 10:16:01 -04:00
Andrey Tuganov
17d941af4f Huffman codec can serialize to text
Refactored the Huffman codec implementation and added ability to
serialize to C++-like text format. This would reduce the time-complexity
if loading hard-coded codecs.
2017-08-15 23:57:21 -04:00
GregF
1d477b9898 Opt: Add opaque tests 2017-08-15 15:54:41 -06:00
Andrey Tuganov
78cf86150e Add id descriptor feature to SPIR-V
Id descriptors are computed as a recursive hash of all instructions used
to define an id. Descriptors are invarint of actual id values and
the similar code in different files would produce the same descriptors.

Multiple ids can have the same descriptor. For example
%1 = OpConstant %u32 1
%2 = OpConstant %u32 1
would produce two ids with the same descriptor. But
%3 = OpConstant %s32 1
%4 = OpConstant %u32 2
would have descriptors different from %1 and %2.

Descriptors will be used as handles of move-to-front sequences in SPIR-V
compression.
2017-08-10 18:44:52 -04:00
GregF
b0310a4156 ADCE: Add support for function calls
ADCE will now generate correct code in the presence of function calls.
This is needed for opaque type optimization needed by glslang. Currently
all function calls are marked as live. TODO: mark calls live only if they
write a non-local.
2017-08-10 17:30:05 -04:00
David Neto
2a1014be9c Inliner: callee can have early return that isn't multi-return
Avoid generating an invalid OpLabel.
Create the continue target for the single-trip loop only if
you actually created the header for the single-trip loop.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/755
2017-08-10 11:43:44 -04:00
GregF
e28bd39997 Inline: Split out InlineExhaustivePass from InlinePass 2017-08-04 17:56:46 -04:00
GregF
f4b29f3bf7 Add CommonUniformElim pass
- UniformElim: Only process reachable blocks

- UniformElim: Don't reuse loads of samplers and images across blocks.
  Added a second phase which only reuses loads within a block for samplers
  and images.

- UniformElim: Upgrade CopyObject skipping in GetPtr

- UniformElim: Add extensions whitelist
  Currently disallowing SPV_KHR_variable_pointers because it doesn't
  handle extended pointer forms.

- UniformElim: Do not process shaders with GroupDecorate

- UniformElim: Bail on shaders with non-32-bit ints.

- UniformElim: Document support for only single index and add TODO.
2017-08-03 11:34:58 -04:00
GregF
c1b46eedbd Add MemPass, move all shared functions to it. 2017-08-02 14:24:02 -04:00
Andrey Tuganov
30bee67439 Add multi-sequence move-to-front implementation
Add MultiMoveToFront class which supports multiple move-to-front
sequences and allows to promote value in all sequences at once.

Added caching for last accessed sequence handle and last accessed value
in each sequence.
2017-08-02 14:07:24 -04:00
GregF
7954740d54 Opt: Delete names and decorations of dead instructions 2017-07-26 18:36:41 -04:00
Lei Zhang
9f6efc76c8 Opt: HasOnlySupportedRefs should consider OpCopyObject
This fixes test failure after merging the previous pull request.
2017-07-25 23:22:09 -04:00
GregF
1182415581 Add extension whitelists to size-reduction passes.
Currently only SPV_KHR_variable_pointers is disallowed in passes which
do pointer analysis. Positive and negative tests of the general extensions
mechanism were added to aggressive_dce but cover all passes.
2017-07-25 19:14:02 -04:00
GregF
adb237f3bd Fix handling of CopyObject in GetPtr and its call sites 2017-07-21 18:08:01 -04:00
GregF
9de4e69856 Add AggressiveDCEPass
Create aggressive dead code elimination pass
This pass eliminates unused code from functions. In addition,
it detects and eliminates code which may have spurious uses but which do
not contribute to the output of the function. The most common cause of
such code sequences is summations in loops whose result is no longer used
due to dead code elimination. This optimization has additional compile
time cost over standard dead code elimination.

This pass only processes entry point functions. It also only processes
shaders with logical addressing. It currently will not process functions
with function calls. It currently only supports the GLSL.std.450 extended
instruction set. It currently does not support any extensions.

This pass will be made more effective by first running passes that remove
dead control flow and inlines function calls.

This pass can be especially useful after running Local Access Chain
Conversion, which tends to cause cycles of dead code to be left after
Store/Load elimination passes are completed. These cycles cannot be
eliminated with standard dead code elimination.

Additionally: This transform uses a whitelist of instructions that it
knows do have side effects, (a.k.a. combinators).  It assumes other
instructions have side effects: it will not remove them, and assumes
they have side effects via their ID operands.
2017-07-10 11:30:25 -04:00
GregF
cc8bad3a5b Add LocalMultiStoreElim pass
A SSA local variable load/store elimination pass.
For every entry point function, eliminate all loads and stores of function
scope variables only referenced with non-access-chain loads and stores.
Eliminate the variables as well.

The presence of access chain references and function calls can inhibit
the above optimization.

Only shader modules with logical addressing are currently processed.
Currently modules with any extensions enabled are not processed. This
is left for future work.

This pass is most effective if preceeded by Inlining and
LocalAccessChainConvert. LocalSingleStoreElim and LocalSingleBlockElim
will reduce the work that this pass has to do.
2017-07-07 17:54:21 -04:00
GregF
52e247f221 DeadBranchElim: Add DeadBranchElimPass 2017-07-07 15:16:25 -04:00
David Neto
35a0695844 Include memory and semantics IDs when iterating over inbound IDs
Fixes Instruction::ForEachInId so it covers
SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID and SPV_OPERAND_TYPE_SCOPE_ID.
Future proof a bit by using the common spvIsIdType routine.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/697
2017-07-05 10:36:57 -04:00
Andrey Tuganov
abc6f5a672 MARK-V decoder supports extended instructions 2017-07-04 16:31:19 -04:00
Chris Forbes
78338d5ba9 Convert pattern stack from deque to vector, and share it
Also move various vector::reserve calls to State ctor
Negligible perf benefit, but more tidy.
2017-07-04 12:02:26 -04:00
Andrey Tuganov
e842c17eb5 Added fixed width encoding to bit_stream
Fixed width encoding is intended to be used for small unsigned integers
when the upper bound is known both to the encoder and the decoder
(for example move-to-front rank).
2017-07-04 11:57:13 -04:00
Andrey Tuganov
73e8dac5b9 Added compression tool tools/spirv-markv. Work in progress.
Command line application is located at tools/spirv-markv
API at include/spirv-tools/markv.h

At the moment only very basic compression is implemented, mostly varint.
Scope of supported SPIR-V opcodes is also limited.

Using a simple move-to-front implementation instead of encoding mapped
ids.

Work in progress:
- Does not cover all of SPIR-V
- Does not promise compatibility of compression/decompression across
  different versions of the code.
2017-06-30 12:22:48 -04:00
Andrey Tuganov
8d3882a408 Added log(n) move-to-front implementation
The implementation is based on AVL and order statistic tree.

It accepts all kinds of values and the implementation
doesn't expect the behaviour to be consistent with id coding.

Intended by SPIR-V compression algorithms.
2017-06-29 16:16:18 -04:00
Andrey Tuganov
40a2829611 Added Huffman codec to utils
Attached ids to Huffman nodes for deterministic internal node
comparison.
2017-06-29 14:51:01 -04:00
GregF
ad1d0351a0 BlockMerge: Add BlockMergePass
Also, add BasicBlock::tail()
2017-06-27 11:31:33 -04:00
Rex Xu
5fbbadca4e Add support for SPV AMD extensions 2017-06-21 15:08:07 -04:00
GregF
6136bf9e0b mem2reg: Add InsertExtractElimPass 2017-06-21 08:13:15 -04:00
GregF
0c5722fc01 mem2reg: Add LocalSingleStoreElimPass
Eliminate function scope variables with one store, if possible.
2017-06-19 10:43:02 -04:00
GregF
7c8da66bc2 mem2reg: Add pass to eliminate local loads and stores in single block. 2017-06-12 17:03:47 -04:00
GregF
aa7e687ef0 Mem2Reg: Add Local Access Chain Convert pass
- Supports OpAccessChain and OpInBoundsAccessChain
- Does not process modules with non-32-bit integer types.
2017-06-04 12:49:27 -04:00
Andrey Tuganov
20fe946666 Added extension SPV_VALIDATOR_ignore_type_decl_unique
If extension is present in the module, validator will ignore the "type
declaration must be unique" rule.
2017-05-25 11:36:48 -04:00
David Neto
dbc2049aa3 Add SPIR-V 1.2 support, for OpenCL 2.2 2017-05-15 17:10:07 -04:00
David Neto
eb720b2ad1 Fix size_t conversion error on MinGW 2017-05-15 11:55:22 -04:00
Greg Fischer
bba812f4e4 Inline: Inline early return function if no returns in loop. 2017-05-12 17:18:00 -04:00
Andrey Tuganov
3eb716cae4 Added bit stream utils
Also added generalized zigzag coding.
- Due to signed integers being mostly non-negative, improved zigzag coding
  to favor positive values.
2017-05-12 17:08:33 -04:00
Andrey Tuganov
f5facf842f Stats analyzer aggregates OpConstant usage 2017-05-09 11:50:15 -04:00
Andrey Tuganov
b4cf371936 Stats analyzer uses validator
Stats analyzer calls validator to check the instruction and update
validator state.

Fixed unit tests (validator was failing).
2017-05-09 11:17:23 -04:00
Mike Lothain
3f90058d2f Update set_spec_const_default_value_test.cpp
Work around warning of dangling else
2017-05-08 10:41:48 -04:00
Andrey Tuganov
87a3f651e2 Added Markov chain analysis to stats
Added data structure to SpirvStats which is used to collect statistics
on opcodes following other opcodes.

Added a simple analysis print-out to spirv-stats.
2017-04-26 02:31:13 -04:00
GregF
a107d349e3 Inline: Do not inline functions with multiple returns (for now) 2017-04-25 16:50:57 -06:00
qining
144f59e451 Add bit pattern interface for setting default value for spec constants 2017-04-21 11:22:47 -04:00
Andrey Tuganov
1e309af80a Added --compact-ids to /tools/opt
The pass remaps ids to a compact set which starts with %1 and
has no gaps.
2017-04-20 10:54:39 -04:00
Andrey Tuganov
b173d1c3cd Added option --preserve-numeric-ids to tools/spirv-as
Added options to tools::Assemble
2017-04-13 14:13:33 -04:00
Andrey Tuganov
4f216402ba Added statistical analysis tool (tool/stats)
Currently analyzes percentages of the following:
- version (how many 1.0 or 1.1)
- generator
- extension
- capability
- opcode prevalence
2017-04-10 17:15:04 -04:00
David Neto
72debb8fd4 Test source language HLSL 2017-04-07 09:54:38 -04:00
David Neto
bf68c81426 Support SPV_KHR_storage_buffer_storage_class
Includes interaction with SPV_KHR_variable_pointers

TODO: Update capabilitly dependency tests for new capabilities.
2017-04-07 09:54:38 -04:00
Ehsan Nasiri
23af06c3a3 Validator support for Variable Pointer extension.
If the variable_pointer extension is used:

* OpLoad's pointer argument may be the result of any of the following:
 * OpSelect
 * OpPhi
 * OpFunctionCall
 * OpPtrAccessChain
 * OpCopyObject
 * OpLoad
 * OpConstantNull

* Return value of a function may be a pointer.

* It is valid to use a pointer as the return value of a function.

* OpStore should allow a variable pointer argument.
2017-04-07 09:49:48 -04:00
David Neto
4895ace29b Update cap tests for SPV_KHR_16bit_storage 2017-04-07 09:49:48 -04:00
David Neto
4087e89bc6 Test asm,dis support for SPV_KHR_variable_pointers 2017-04-07 09:49:48 -04:00
David Neto
11a867f412 Add FlattenDecoration transform
Add --flatten-decorations to spirv-opt

Flattens decoration groups.  That is, replace OpDecorationGroup
and its uses in OpGroupDecorate and OpGroupMemberDecorate with
ordinary OpDecorate and OpMemberDecorate instructions.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/602
2017-04-06 11:19:56 -04:00
Lei Zhang
5c3c054c1f Group targets into folders
This plays nicer with IDEs.
2017-04-06 09:44:33 -04:00
David Neto
afc60bbebf Fix optimizer on when to write the binary
The spvtools::Optimizer::Run method should also write the output binary
if optimization succeeds without changes but the output binary vector
does not have exactly the same contents as the input binary.
We have to check both the base pointer of the storage and the size of
the vector

Added a test for this too.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/611
2017-04-03 15:48:50 -04:00
David Neto
ceb1d4f2fd Avoid inlining calls to external functions
External functions don't have bodies to inline anyway.
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/605
2017-03-31 10:36:58 -04:00
David Neto
2410e00851 Add low priority test suggestions for inliner 2017-03-29 18:02:40 -04:00
Greg Fischer
04fcc66743 Add exhaustive function call inlining to spirv-opt
Inlining is done for all functions designated as entry points.

Add optional validation to test fixture method SinglePassRunAndCheck.
2017-03-29 18:02:40 -04:00
David Neto
7fe8a57a5b Support SPV_AMD_gcn_shader
Supported in assembler, disassembler, and binary parser.

The validator does not check SPV_AMD_gcn_shader validation rules
beyond parsing the extension.

Adds generic support for generating instruction tables for vendor
extensions.

Adds generic support for extensions the validator should recognize
(but not check) but which aren't derived from the SPIR-V core
grammar file.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/594
2017-03-23 16:32:35 -04:00
David Neto
fa834dea40 Fix validator message for bad logical pointer
Affects OpLoad and OpStore validation.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/588
2017-03-23 15:50:45 -04:00
David Neto
ddd40f855b Update capability tests for SPV_KHR_16bit_storage rev 7
The SPIR-V core grammar file in a recent SPIRV-Headers
applied the fix from Rev 7 of SPV_KHR_16bit_storage:
FPRoundingMode enums are now enabled by the capabilities
introduced by that extension.

Update the SPIRV-Tools tests accordingly.
2017-03-23 15:46:08 -04:00
David Neto
0066a363d6 Add SpirvTools::Validate that takes an options object
Add spvtools::ValidatorOptions RAII wrapper around
a spv_validator_options value.
2017-03-17 12:48:51 -04:00
Andrey Tuganov
971ede33b2 Validator dissallows unsupported Vulkan capability
If environment is SPV_ENV_VULKAN_1_0, disallow OpCapability operands
which are not supported by Vulkan 1.0.

See https://github.com/KhronosGroup/SPIRV-Tools/issues/572
2017-03-17 10:28:35 -04:00
Andrey Tuganov
c804c125c5 Autogenererating enum-string mappings from grammar
Autogenerating the following code:
- extension enum
- extension-to-string
- string-to-extension
- capability-to-string

Capability mapping table will not compile if incomplete.
TODO: Use "spirv-latest-version.h" instead of 1.1.

Added function to generate capability tables for tests.
2017-03-16 15:29:17 -04:00
Andrey Tuganov
b011633171 Validator checks if operands require extension
If required extension is not found, returning new error
SPV_ERROR_MISSING_EXTENSION.
2017-03-14 13:39:43 -04:00
Andrey Tuganov
1fb8c37b57 Refactored enum_set
- removed forgotten file enum_set.cpp
- added IsEmpty and HasAnyOf
- hidden unsafe functions Add(uint32_t), Contains(uint32_t)
- added new tests
2017-03-10 13:38:32 -05:00
Andrey Tuganov
1c43cb8550 Validator parses and registers OpExtension
Known extensions are saved in validation state. Unknown extension
produce a dignostic message, but do not fail the validation.

Moved extension definitions to their own file.
2017-03-09 15:28:33 -05:00
David Neto
42037205a8 Test SPV_KHR_multiview in assembler, disassembler 2017-03-08 17:11:22 -05:00
David Neto
7327f63c34 Test SPV_KHR_device_group in assembler, disassembler 2017-03-08 17:11:22 -05:00
David Neto
83b334e549 Test SPV_KHR_16bit_storage in assembler, disassembler 2017-03-08 17:11:22 -05:00
David Neto
0004f721e3 Test SPV_KHR_subgroup_vote support in assembler, disassembler 2017-03-08 17:11:22 -05:00
Andrey Tuganov
9696393c5b Fix nullptr dereferencing in val_fixtures.cpp 2017-03-08 15:41:46 -05:00
David Neto
892b009959 Change Ids in tests to avoid confusion
The assembler assigns ID numbers sequentially, so it's confusing
to have a %1 in the source assembly when it isn't the first mentioned
ID.  Rewrite the ID names to avoid this situation in a few cases.
2017-03-07 09:51:32 -05:00
Aliya Pazylbekova
edb52647bd Validate that SpecId decoration target is a OpSpecConstant instruction
on a scalar

Fixes: https://github.com/KhronosGroup/SPIRV-Tools/issues/275
2017-03-07 09:51:16 -05:00
Andrey Tuganov
11fbe5dc1f Validator: unique type check allows runtime arrays
OpTypeRuntimeArray is an aggregate and therefore can have duplicate
declarations.
2017-03-02 17:24:29 -05:00
Ehsan Nasiri
2046e24498 Fixes issue #489.
From the SPIR-V Spec 2.16.1:

A function declaration (an OpFunction with no basic blocks), must have
a Linkage Attributes Decoration with the Import Linkage Type.

A function definition (an OpFunction with basic blocks) cannot be
decorated with the Import Linkage Type.
2017-03-02 10:06:29 -05:00
Lei Zhang
40be4bdd34 Allow using FPRoundingMode when see VK_KHR_16bit_storage
According to the extension, FPRoundingMode should be allowed to
use without requiring Kernel capability when VK_KHR_16bit_storage
is enabled.
2017-03-01 18:25:28 -05:00
Andrey Tuganov
94d94e1f4a Validator: downgraded dupl type decl to warning
Validator check for uniqueness of type declarations
(commit 0e9c24fdd1)
was causing failures in vulkancts tests.

Downgrading from error to warning.
2017-03-01 17:50:26 -05:00
Andrey Tuganov
0e9c24fdd1 Issue 559: check type declaration uniqueness
Adds PassTypeUnique to the validator.
Disallows repeated declarations of all types except for aggregates.
2017-02-28 22:27:08 -05:00
Ehsan Nasiri
da4ae05638 Add command line options struct for the validator
The limit for the number of struct members is parameterized using
command line options.

Add --max-struct-depth command line option.
Add --max-switch-branches command line option.
Add --max-function-args command line option.
Add --max-control-flow-nesting-depth option.
Add --max-access-chain-indexes option.
2017-02-28 12:00:06 -05:00
David Neto
af7125dfb0 Validation of type decls for SPV_KHR_16bit_storage
Allow declaration of 16bit int or 16bit float in
the presence of capabilities from SPV_KHR_16bit_storage
2017-02-27 13:12:59 -05:00
Adam Van Ymeren
c0949703b1 Fixes issue #548
Add validation checks for the reserved OpImageSparseSampleProj* opcodes.
2017-02-16 16:32:33 -05:00
David Neto
dadd5161bb Check strict domination of merge block
If a merge block is reachable, then it must be *strictly* dominated
by its header.  Until now we've allowed the header and the merge
block to be the same.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/551

Also: Use dominates and postdominates methods on BasicBlock to
improve readability.
2017-02-10 12:26:10 -05:00
Ehsan Nasiri
f2867d7485 The Signedness in OpTypeInt must always be 0.
When Kernel capability is used, The Signedness in OpTypeInt must always
be 0. Fixes issue #492.
2017-02-10 12:21:16 -05:00
Ehsan Nasiri
affbc31849 Making the unit tests more robust.
It is best to check the error messages of unit tests that fail
validation. This will ensure that a validation failure is due to what we
expect and not due to some secondary reason.

Updating SPIR-V Validator unit tests with error message checks.
2017-01-29 22:50:37 -05:00
Ehsan Nasiri
cb009b3862 Fixes issue #497.
The Linkage Attributes Decoration cannot be applied to functions
targeted by an OpEntryPoint instruction.
2017-01-19 16:34:47 -05:00
Ehsan Nasiri
9c71c572e5 Check BuiltIn Decoration rules.
When applied to a structure-type member, all members of that structure
type must also be decorated with BuiltIn. (No allowed mixing of built-in
variables and non-built-in variables within a single structure.)

When applied to a structure-type member, that structure type cannot be
contained as a member of another structure type.

There is at most one object per Storage Class that can contain a
structure type containing members decorated with BuiltIn, consumed per
entry-point.
2017-01-19 16:01:06 -05:00
Ehsan Nasiri
3a7df2cf1b Fixes issue #532.
It is acceptable for OpAccessChain, OpInBoundsAccessChain,
OpPtrAccessChain, OpInBoundsPtrAccessChain, OpCompositeInsert, and
OpCompositeExtract to not take any indexes as arguments. In such cases,
no indexing will be done on the Base pointer/composite.
2017-01-19 11:03:04 -05:00
Ehsan Nasiri
eb7d2534df Fixes issue #493.
Check function call operands and return type agree with the called
function type.

The validation code exists. I have fixed the unit tests.
2017-01-18 17:04:00 -05:00
Ehsan Nasiri
d75bf5ef03 Validation for decoration rules. Fixes issue #499.
Added a new file where all the decoration validation can be performed.

In this change the SPIRV Spec Section 2.16.1 is implemented:
"It is illegal to initialize an imported variable. This means
that a module-scope OpVariable with initialization value cannot be
marked with the Import Linkage Type."

Also added unit tests.
2017-01-18 16:29:14 -05:00
Ehsan Nasiri
fcf7df069d Adding decoration class and tests.
* Added the decoration class as well as the code that registers the
decorations for each <id> and also decorations for struct members.

* Added unit tests for decorations in ValidationState as well as
decoration id tests.
2017-01-16 16:52:08 -05:00
Ehsan Nasiri
1c11c8690f Validation of OpEntryPoint usage.
According to the SPIRV Spec (2.16.1):
* There is at least one OpEntryPoint instruction, unless the Linkage
capability is being used.

* No function can be targeted by both an OpEntryPoint instruction and an
OpFunctionCall instruction.

Also updated unit tests to includ OpEntryPoint.
2017-01-13 16:15:11 -05:00
Ehsan Nasiri
68e36ec7e9 Introducing a new flow for running the Validator.
We are adding a new API which can be called to run the SPIR-V validator,
and retrieve the ValidationState_t object. This is very useful for
unit testing.

I have also added basic unit tests that demonstrate usage of this flow
and ease of use to verify correctness.
2017-01-13 16:07:03 -05:00
David Neto
545a6c1dc3 Provide friendly names for true and false 2017-01-11 16:33:45 -05:00
Ehsan Nasiri
d1868b9361 Validation for Composite Extract and Insert.
The validity of each command is checked based on the descripton in
SPIR-V Spec Section 3.32.12 (Composite Instructions).

Also checked that the number of indexes passed to these commands does
not exceed the limit described in 2.17 (Universal Limits).

Also added unit tests for each one.
2017-01-06 17:14:38 -05:00
Ehsan Nasiri
109ce225c9 Fixes bug #517.
entry_block_to_construct_ maps an entry block to its construct. The key
in this map (the entry block) is not unique, and therefore the entry for
the continue construct gets overwritten when the selection construct is
discovered.

Since a given block may be the entry block of different types of
constructs, the (basic_block, construct_type) pair should be able to
uniquely identify the construct.

Adds test:
- In this test, a basic block is the entry block of a continue construct
  as well as the entry block of a selection construct.
  It can be shown that this unit test would crash without the fix in this
  PR and passes with the fix in this PR.
2017-01-06 14:23:17 -05:00
Ehsan Nasiri
ff272a9820 Adding validation code for PtrAccessChain.
Validation for OpPtrAccessChain is similar to OpAccessChain with the
following difference: OpPtrAccessChain takes an extra argument (word 4)
which is the Element <id> argument.

Validation for OpInBoundsPtrAccessChain is also similar to OpPtrAccessChain.

Also added tests for all access chain instructions:
Modified the existing parameterized tests to accommodate OpPtrAccessChain and
OpInBoundsPtrAccessChain.

Also fixed a typo in previous commits.
2017-01-03 16:36:35 -05:00
Ehsan Nasiri
c656c23ad3 Refactoring unit tests using TEST_P.
Using parameterized unit tests to avoid duplicate code that runs the
tests of OpAccessChain and OpInBoundsAccessChain.

This is also a steppingstone to adding tests for OpPtrAccessChain and
OpInBoundsPtrAccessChain.
2017-01-03 14:58:48 -05:00
Ehsan Nasiri
6993fc413d Validation code for control flow nesting depth.
According to Section 2.17 (Universal Limits) of the SPIR-V Spec, the
control flow nesting depth may not be larger than 1023.

This is checked only when we are required to have structured
control flow.  Otherwise it's not clear how to compute control
flow nesting depth.
2016-12-23 14:14:50 -05:00
David Neto
37422e9dba Support building under CYGWIN
- Parse CHANGES file with Universal Python line endings in case
  the source tree was checked out with Windows line endings.
- Use our own clone of strnlen_s which might not be available
  everywhere.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/508
2016-12-21 10:34:23 -05:00
David Neto
5a0b5ca4a7 Get vendor tool info from the SPIR-V registry file
Update old tests to use officially registered vendor names;
this affected "Codeplay".
2016-12-09 14:01:43 -05:00
Ehsan Nasiri
d004cd46db Adding validation for OpInBoundsAccessChain.
The validation for this instruction is similar to OpAccessChain.

Include a test for testing a struct with (valid) depth of 255.
2016-12-08 15:11:30 -05:00
Ehsan Nasiri
4fb79b54f2 Validation for struct nesting depth.
According to sectin 2.17 in SPIR-V Spec, the structure nesting depth may
not be larger than 255. This is interpreted as structures nested in
structures. The code does not look into arrays or follow pointers to see
if it reaches a structure downstream.

Use memoization to avoid exponential runtime.
2016-12-07 16:15:56 -05:00
David Neto
d5b0cd34c9 An OpVariable initializer can be a module-scope variable
Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/482
2016-12-07 16:04:51 -05:00
Ehsan Nasiri
aadf696fce Fixing bug in OpAccessChain validation code.
The validation code for OpAccessChain was missing OpTypeRuntimeArray as
a possible type that can be indexed into.

This was caught by running the validator on VKCTS.

Also adding unit tests for it.
2016-12-06 09:55:39 -05:00
Ehsan Nasiri
1f6123fa5f Add a test that exercises wrong usage of result id
Result <id> resulting from an instruction within a function may not be
used outside that function.
2016-12-06 09:50:08 -05:00
Ehsan Nasiri
bab1aad856 Adding validation code for OpAccessChain.
* Result Type must be an OpTypePointer. Its Type operand must be the
type reached by walking the Base’s type hierarchy down to the last
provided index in Indexes, and its Storage Class operand must be the
same as the Storage Class of Base.

* Base must be a pointer, pointing to the base of a composite object.

* Indexes walk the type hierarchy to the desired depth, potentially down
to scalar granularity. The first index in Indexes will select the
top-level member/element/component/element of the base composite. All
composite constituents use zero-based numbering, as described by their
OpType... instruction. The second index will apply similarly to that
result, and so on. Once any non-composite type is reached, there must
be no remaining (unused) indexes. Each of the Indexes must:
- be a scalar integer type,
- be an OpConstant when indexing into a structure.

* Check for the case where no indexes are passed to OpAccessChain.

Minor improvements based on code review.
2016-12-02 13:50:41 -05:00
Ehsan Nasiri
4b26d514ef Validate the number of global and local variables.
According to the Universal Limits section of the SPIR-V Spec (2.17), the
number of global variables may not exceed 65,535 and the number of local
variables may not exceed 524,287.

Also added unit tests for each one.
2016-12-01 16:04:13 -05:00
Ehsan Nasiri
490617133e Validate the number of arguments in OpTypeFunction
According to the SPIR-V spec (section 2.17: Universal Limits), the
OpTypeFunction instruction may not take more than 255 arguments for the
function. Also added unit tests for it.
2016-12-01 15:49:31 -05:00
Ehsan Nasiri
3c8bc80e3a Adding validation code for OpSwitch limits
The number of (literal, label) pairs passed to OpSwitch may not exceed
16,383. Added code to validate this and added unit tests for it.

Also fixed a typo in another validor error message.
2016-11-30 15:36:05 -05:00
Ehsan Nasiri
bef80716d7 Validation for number of Struct members.
The number of members in a struct may not exceed 16,383.
Also Adding unit tests for Struct size limit check.
2016-11-30 15:29:37 -05:00
Ehsan Nasiri
bd5b0bfca1 Checks that result IDs are within the ID bound specified in the SPIR-V header
This is described in Section 2.17 of the SPIR-V Spec.

* Updated existing unit test 'SemanticsIdIsAnIdNotALiteral' to pass by
manipulating the ID bound in its binary header.

* Fixed boundary check in the code.

* Added unit test to check the case that the largest ID is equal to the
ID bound.
2016-11-28 11:44:10 -05:00
Ehsan Nasiri
f72189c249 Validation for OpSampledImage instruction.
This change implements the validation for usages of OpSampledImage
instruction as described in the Data Rules section of the Universal
Validation Rules of the SPIR-V Spec.
2016-11-24 09:29:10 -05:00
Ehsan Nasiri
6fa6a3f647 Adding validation for OpSpecConstantComposite.
SpecConstantComposite may specialize to a vector, matrix, array, or
struct. In each case, the number of components and type of components
that are being specialized to must match the expected result type.

Removed use of macros in these tests.
Now using the spvValidateBase class. Using CompileSuccessfully(), and
ValidateInstructions() to compile to binary and run the validator. Also
using getDiagnosticString() to check the proper error message string.
All the heavy lifting is done in ValidateBase class.
2016-11-19 15:47:28 -05:00
Ehsan Nasiri
8c414eb579 Adding validation code for OpTypeStruct.
According to the Data Rules section of 2.16.1. Universal Validation
Rules of the SPIR-V Spec:

Forward reference operands in an OpTypeStruct
* must be later declared with OpTypePointer
* the type pointed to must be an OpTypeStruct
* had an earlier OpTypeForwardPointer forward reference to the same <id>
2016-11-16 16:41:56 -05:00
David Neto
c935253c91 Make friendly number-based names for OpConstant
For example:
  %int_42 = OpConstant %int 42
  %int_n42 = OpConstant %int -42
  %float_3_14 = OpConstant %float 3.14
2016-11-15 14:05:03 -05:00
David Neto
4f750c0dcc Extract EmitNumericLiteral from disassembler
Test with disassembly of numeric literals.
2016-11-15 14:05:03 -05:00
Ehsan Nasiri
d0620061c7 Adding validation code for more data rules.
These rules are under "Data Rules" in 2.16.1 (Universal Validation
Rules) part of the SPIR-V 1.1 Specification document:

* Scalar floating-point types can be parameterized only as 32 bit, plus
any additional sizes enabled by capabilities.

* Scalar integer types can be parameterized only as 32 bit, plus any
additional sizes enabled by capabilities.

* Vector types can only be parameterized with numerical types or the
OpTypeBool type.

* Matrix types can only be parameterized with floating-point types.

* Matrix types can only be parameterized as having only 2, 3, or 4
columns.

* Specialization constants (see Specialization) are limited to integers,
Booleans, floating-point numbers, and vectors of these.
2016-11-15 13:38:39 -05:00
Lei Zhang
38036a7203 Rename validation source files to comply with Google style guide. 2016-11-08 17:19:56 -08:00
Lei Zhang
2881fe958f Rename validation tests to comply with Google style guide. 2016-11-08 17:19:11 -08:00
Ehsan Nasiri
6c899a52f9 Adding validation for vector data rule.
Number of components in a vector can be 2 or 3 or 4. If Vector16
capability is used, 8 and 16 components are also allowed.
Also added unit tests for vector data rule.
2016-11-08 17:48:30 -05:00
Lei Zhang
4f57e140bf Renaming test files to comply with Google style guide.
Also posfixing test files with `_test' to make it more clear.
2016-11-07 14:41:51 -05:00
David Neto
ee6c877b53 Test SPV_KHR_shader_draw_parameters support
In assembler, disassembler, parser.
2016-10-12 10:47:28 -04:00
Lei Zhang
c1d5e660ee Add begin() and end() for TypeManager. 2016-09-30 16:56:42 -04:00
David Neto
f0a96c91f8 Remove unnecessary #include
This will make it easier to port these tests to different contexts.
2016-09-29 16:44:18 -04:00
Lei Zhang
f9cb7c282c Avoid appending EOL in StringifyMessage().
EOL should be appended when outputing to streams by the tool.
2016-09-23 15:13:24 -04:00
Lei Zhang
069b0a7339 Fix operand class test coverage table.
* Fix a missing column.
* Convert line endings.

This will make the table beautiful and searchable on Github.
2016-09-23 11:58:25 -04:00
Umar Arshad
a6ef1ea29e Fix forward reference issues with forword pointer
* Allows OpTypeForwardPointer to reference IDs not yet declared in
  the module
* Allows OpTypeStruct to reference IDs not yet declared in
  the module

Possible Issue: OpTypeStruct should only allow forward references
if the ID is a pointer that is referenced by a forward pointer. Need
Type support in Validator which is currently a work in progress.
2016-09-22 16:54:02 -04:00
Lei Zhang
1ca817a38e Use nullptr as the default message consumer to ignore all messages.
There is no difference between the previous IgnoreMessage() function
and a null std::function, from functionality's perspective.
The user can set nullptr as the MessageConsumer, so need to guard
against nullptr before calling the consumer anyway. It's better
we use it internally so that it may expose problems by us instead
of the user.
2016-09-21 17:23:03 -04:00
Lei Zhang
6effeaa7f1 Change some asserts to normal errors in IrLoader. 2016-09-21 17:22:00 -04:00
Lei Zhang
5edf054926 Add more Assemble(), Disassemble() and Validate() overloads. 2016-09-21 14:42:04 -04:00
Lei Zhang
620f05e679 Publish the C++ interface. 2016-09-21 14:41:47 -04:00
Lei Zhang
92411caa3b Rename SpvTools to SpirvTools. 2016-09-21 14:28:33 -04:00
Lei Zhang
6849a3cefd Use C style names for MessageLevel. 2016-09-21 12:44:37 -04:00
Lei Zhang
80eb0793c9 Change to use enum instead of enum class for MessageLevel.
This allows us to create C interface with MessageLevel later.
2016-09-21 11:30:41 -04:00
Lei Zhang
f18e1f293b Add a C++ interface to the optimization functionalities. 2016-09-20 16:52:05 -04:00
Lei Zhang
1bc0b275dd Allow changing MessageConsumer in Pass & PassManager.
Default-constructed Pass/PassManager will have a MessageConsumer
which ignores all messages. SetMessageConsumer() should be called
to supply a meaningful MessageConsumer.
2016-09-20 16:48:00 -04:00
Lei Zhang
2cbb2cce3e Change interface of Pass::Process() to return possible failures. 2016-09-20 16:40:17 -04:00
David Neto
12b5777912 Provide friendly names for shader ballot BuiltIns 2016-09-19 17:31:25 -04:00
Lei Zhang
3590279a73 Set the default source name as "input". 2016-09-19 10:20:22 -04:00
David Neto
9382035a22 Add basic support for SPV_KHR_shader_ballot
Requires use of SPIRV-Headers that has support
for SPV_KHR_shader_ballot.

Adds assembler, disassembler, binary parser support.

Adds general support for allowing an operand to be
only enabled by a set of extensions.

TODO: Validator support for extension checking.
2016-09-16 16:38:36 -04:00
Lei Zhang
8654caa565 Prepare the C++ interface for publication.
* Use PIMPL idiom in the C++ interface.
* Clean up interface for assembling and disassembling.
* Add validation into C++ interface.
* Add more tests for the C++ interface.
2016-09-16 16:37:27 -04:00
Lei Zhang
b54686d017 Move functions for building modules outside of the C++ interface. 2016-09-16 14:02:47 -04:00
Lei Zhang
869440ebd4 Let SPIRV_ASSERT() exit the program if the assertion fails. 2016-09-16 13:14:57 -04:00
Lei Zhang
114af21994 Add MessageConsumer to PassManager, Pass, and analysis interfaces.
Also convert some uses of assert() in optimization code to use
SPIRV_ASSERT(), SPIRV_UNIMPLEMENTED(), or SPIRV_UNREACHABLE()
accordingly.
2016-09-16 13:14:57 -04:00
Lei Zhang
bfd7cd6d6e Add utility macros for logging messages.
Add the following macros for logging purpose:
* SPIRV_ASSERT
* SPIRV_DEBUG
* SPIRV_UNIMPLEMENTED
* SPIRV_UNREACHABLE

The last two is always turned on, while the first two can only
be turned on in debug build.
2016-09-16 09:56:34 -04:00
Lei Zhang
755f97f534 Add a callback mechanism for communicating messages to callers.
Every time an event happens in the library that the user should be
aware of, the callback will be invoked.

The existing diagnostic mechanism is hijacked internally by a
callback that creates an diagnostic object each time an event
happens.
2016-09-15 12:35:48 -04:00