Commit Graph

685 Commits

Author SHA1 Message Date
Steven Perron
c502a15f25
Handle 8-bit index in elim dead member (#4043)
The eliminate dead member pass is written assuming that the index to an
OpAccessChain will be a 32-bit integer or 64-bit integer.  That is
changed to work for any width 64-bits or less.

Fixes https://crbug.com/1151727
2020-12-01 14:48:25 -05:00
greg-lunarg
7046c05d2f
Add texel buffer out-of-bounds checking instrumentation (#4038)
This instruments ImageRead, ImageWrite and ImageFetch when applied to
texel buffers.

Also add new (but not yet generated) buffer OOB error codes differentiated
for VUID classification.
2020-12-01 11:28:16 -05:00
Alastair Donaldson
1299436c8f
Reject SPIR-V that applies void to OpUndef, OpCopyObject, OpPhi (#4036)
Fixes #4035.
2020-11-27 16:31:04 +00:00
David Neto
2c458414c0
BuildModule: optionally avoid adding new OpLine instructions (#4033)
* BuildModule: optionally avoid adding new OpLine instructions

Fixes #4029 for my use case

* Fix formatting

* Create last_line_inst_ only if doing extra line tracking
2020-11-25 14:54:32 -05:00
greg-lunarg
671914c28e
Fix buffer oob instrumentation for matrix refs (#4025)
Fix buffer oob instrumentation for matrix refs.

Matrix stride decoration is not on matrix type but is a member decoration
on the enclosing struct type. Also correctly apply matrix stride depending
on row or column major.
2020-11-18 12:39:15 -05:00
Jaebaek Seo
f686518cee
spirv-opt: properly preserve DebugValue indexes operand (#4022)
spirv-opt has a bug that `DebugInfoManager::AddDebugValueWithIndex()` does not
preserve `Indexes` operands of
[DebugValue](https://www.khronos.org/registry/spir-v/specs/unified1/OpenCL.DebugInfo.100.html#DebugValue).
It has to preserve all of those `Indexes` operands, but it preserves only the first index
operand.

This PR removes `DebugInfoManager::AddDebugValueWithIndex()` and lets the spirv-opt
use `DebugInfoManager::AddDebugValueForDecl()`.
`DebugInfoManager::AddDebugValueForDecl()` preserves the Indexes operand correctly.
2020-11-13 12:06:38 -05:00
Jaebaek Seo
c2b2b57885
Add DebugValue for invisible store in single_store_elim (#4002)
The front-end language compiler would simply emit DebugDeclare for
a variable when it is declared, which is effective through the variable's
scope. Since DebugDeclare only maps an OpVariable to a local variable,
the information can be removed when an optimization pass uses the
loaded value of the variable. DebugValue can be used to specify the
value of a variable. For each value update or phi instruction of a variable,
we can add DebugValue to help debugger inspect the variable at any
point of the program execution.
For example,

float a = 3;
... (complicated cfg) ...
foo(a); // <-- variable inspection: debugger can find DebugValue of `float a` in the nearest dominant

For the code with complicated CFG e.g., for-loop, if-statement, we
need help of ssa-rewrite to analyze the effective value of each variable
in each basic block.

If the value update of the variable happens only once and it dominates
all its uses, local-single-store-elim pass conducts the same value update
with ssa-rewrite and we have to let it add DebugValue for the value assignment.

One main issue is that we have to add DebugValue only when the value
update of a variable is visible to DebugDeclare. For example,

```
{  // scope1
   %stack = OpVariable %ptr_int %int_3
   {  // scope2
       DebugDeclare %foo %stack    <-- local variable "foo" in high-level language source code is declared as OpVariable "%stack"
       // add DebugValue "foo = 3"
       ...
       Store %stack %int_7   <-- foo = 7, add DebugValue "foo = 7"
       ...
       // debugger can inspect the value of "foo"
    }
    Store %stack %int_11   <-- out of "scope2" i.e., scope of "foo". DO NOT add DebugValue "foo = 11"
}
```

However, the initalization of a variable is an exception.
For example, an argument passing of an inlined function must be done out of
the function's scope, but we must add a DebugValue for it.

```
// in HLSL
bar(float arg) { ... }
...
float foo = 3;
bar(foo);

// in SPIR-V
%arg = OpVariable
OpStore %arg %foo   <-- Argument passing. Out of "float arg" scope, but we must add DebugValue for "float arg"
... body of function bar(float arg) ...
```

This PR handles the except case in local-single-store-elim pass. It adds
DebugValue for a store that is considered as an initialization.

The same exception handling code for ssa-rewrite is done by this commit: df4198e50e.
2020-11-04 13:43:59 -05:00
Diego Novillo
c74d5523bb
Fix SSA re-writing in the presence of variable pointers. (#4010)
This fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/3873.

In the presence of variable pointers, the reaching definition may be
another pointer.  For example, the following fragment:

 %2 = OpVariable %_ptr_Input_float Input
%11 = OpVariable %_ptr_Function__ptr_Input_float Function
      OpStore %11 %2
%12 = OpLoad %_ptr_Input_float %11
%13 = OpLoad %float %12

corresponds to the pseudo-code:

layout(location = 0) in flat float *%2
float %13;
float *%12;
float **%11;
*%11 = %2;
%12 = *%11;
%13 = *%12;

which ultimately, should correspond to:

%13 = *%2;

During rewriting, the pointer %12 is found to be replaceable by %2.
However, when processing the load %13 = *%12, the type of %12's reaching
definition is another float pointer (%2), instead of a float value.

When this happens, we need to continue looking up the reaching definition
chain until we get to a float value or a non-target var (i.e. a variable
that cannot be SSA replaced, like %2 in this case since it is a function
argument).
2020-11-04 10:23:53 -05:00
Junda Liu
82b378d671
spirv-opt: Add support to prevent functions from being inlined if they have DontInline flag (#3858)
This commit add support for optimizer to not inline functions with DontInline control flag, so that the [noinline] attribute in HLSL will be useful in DXC SPIR-V generation.

This is part of work of github.com/microsoft/DirectXShaderCompiler/issues/3158
2020-10-29 16:38:56 -04:00
Jaebaek Seo
56d0f50357
Propagate OpLine to all applied instructions in spirv-opt (#3951)
Based on the OpLine spec, an OpLine instruction must be applied to
the instructions physically following it up to the first occurrence
of the next end of block, the next OpLine instruction, or the next
OpNoLine instruction.

```
OpLine %file 0 0
OpNoLine
OpLine %file 1 1
OpStore %foo %int_1
%value = OpLoad %int %foo
OpLine %file 2 2
```

For the above code, the current spirv-opt keeps three line
instructions `OpLine %file 0 0`, `OpNoLine`, and `OpLine %file 1 1`
in `std::vector<Instruction> dbg_line_insts_` of Instruction class
for `OpStore %foo %int_1`. It does not put any line instruction to
`std::vector<Instruction> dbg_line_insts_` of
`%value = OpLoad %int %foo` even though `OpLine %file 1 1` must be
applied to `%value = OpLoad %int %foo` based on the spec.

This results in the missing line information for
`%value = OpLoad %int %foo` while each spirv-opt pass optimizes the
code. We have to put `OpLine %file 1 1` to
`std::vector<Instruction> dbg_line_insts_` of
both `%value = OpLoad %int %foo` and `OpStore %foo %int_1`.

This commit conducts the line instruction propagation and skips
emitting the eliminated line instructions at the end, which are the same
with PropagateLineInfoPass and RedundantLineInfoElimPass. This
commit removes PropagateLineInfoPass and RedundantLineInfoElimPass.

KhronosGroup/glslang#2440 is a related PR that stop using
PropagateLineInfoPass and RedundantLineInfoElimPass from glslang.
When the code in this PR applied, the glslang tests will pass.
2020-10-29 13:06:30 -04:00
Diego Novillo
cbd1fa6c42
Simplify logic to decide whether CCP modified the IR (#3997)
Simplify logic to decide whether CCP modified the IR.

The previous attempts at fixing this issue relied on marking the IR
changed only when CCP was able to fold an instruction during
propagation (https://github.com/KhronosGroup/SPIRV-Tools/pull/3799,
https://github.com/KhronosGroup/SPIRV-Tools/pull/3732).

Those fixes missed the case described in
https://github.com/KhronosGroup/SPIRV-Tools/issues/3991.  In this case,
the folder never actually succeeds in folding the instruction, but it
does create constants in the process.

Fixed with this change.
2020-10-28 10:18:34 -04:00
Jaebaek Seo
df4198e50e
Add DebugValue for DebugDecl invisible to value assignment (#3973)
For some cases, we have DebugDecl invisible to a value assignment, but
the value assignment information is important i.e., debugger cannot inspect
the variable without the information. For example, a parameter of an inlined
function must have its value assignment i.e., argument passing out of its
function scope. If we simply remove DebugDecl because it is invisible to the
argument passing, we cannot inspec the variable.

This PR
- Adds DebugValue for DebugDecl invisible to a value assignment. We use
the value of the variable in the basic block that contains DebugDecl, which is
found by ssa-rewrite. If the value instruction does not dominate DebugDecl,
we use the value of the variable in the immediate dominator of the basic block.
- Checks the visibility of DebugDecl for Phi value assignment based on the
all value operands of the Phi. Since Phi just references multiple values from
multiple basic blocks, scopes of value operands must be regarded as the scope
of the Phi.
2020-10-27 15:10:08 -04:00
greg-lunarg
34ae8a4757
Fix bounds check instrumentation to handle 16-bit values (#3983) 2020-10-26 09:31:35 -04:00
Jaebaek Seo
fd3948e161
Add DebugValue for function param regardless of scope (#3923)
`DebugInfoManager::AddDebugValueIfVarDeclIsVisible()` adds
OpenCL.DebugInfo.100 DebugValue from DebugDeclare only when the
DebugDeclare is visible to the give scope. It helps us correctly
handle a reference variable e.g.,

{ // scope #1.
  int foo = /* init */;
  { // scope #2.
    int& bar = foo;
    ...

in the above code, we must not propagate DebugValue of `int& bar` for
store instructions in the scope #1 because it is alive only in
the scope #2.

We have an exception: If the given DebugDeclare is used for a function
parameter, `DebugInfoManager::AddDebugValueIfVarDeclIsVisible()` has
to always add DebugValue instruction regardless
of the scope. It is because the initializer (store instruction) for
the function parameter can be out of the function parameter's scope
(the function) in particular when the function was inlined.

Without this change, the function parameter value information always
disappears whenever we run the function inlining pass and
`DebugInfoManager::AddDebugValueIfVarDeclIsVisible()`.
2020-10-16 10:18:41 -04:00
Jaebaek Seo
67f8e2eddc
Debug info preservation in convert-local-access-chains pass (#3835)
1. DebugValue/DebugDeclare references of load/store must not change
the behaviors of the convert-local-access-chains pass
2. We have to properly set the scope and line information of new
instructions made by the convert-local-access-chains pass
2020-10-02 10:45:24 -04:00
Jaebaek Seo
57abfd88c5
Debug info preservation in redundancy-elimination pass (#3839)
* Debug info preservation in redundancy-elimination pass
2020-10-01 09:22:16 -04:00
Jaebaek Seo
e246038364
Debug info preservation in if-conversion pass (#3861)
* Debug info preservation in if-conversion pass
2020-10-01 09:20:27 -04:00
Ryan Harrison
34ef0c3fdc
Fix missed modification flagging (#3814)
Fixes #3813
2020-09-16 19:57:04 -04:00
greg-lunarg
7e28d809c6
Add buffer oob check to bindless instrumentation (#3800) 2020-09-16 09:23:46 -04:00
Diego Novillo
286b3095dd
Properly mark IR changed if instruction folder creates more than one constant. (#3799)
In #3636, I missed that the instruction folder may create more than a
single constant per call.  Since CCP was only checking whether one
constant had been created after folding, it was wrongly thinking that
the IR had not changed.

Fixes #3738.
2020-09-14 09:00:38 -04:00
Alastair Donaldson
ed9863e46e
Favour 'integrity' over 'coherence' as a replacement for 'sanity'. (#3619) 2020-09-10 09:52:21 -04:00
Stefano Milizia
1ab52e54ab
spirv-opt: Add function to compute nesting depth of a block (#3771)
This PR adds the NestingDepth function to StructuredCFGAnalysis.
This function, given a block id, returns the number of merge
constructs containing it.

This is needed by spirv-fuzz, but it makes sense to add it to
StructuredCFGAnalaysis, which contains related functionalities.
2020-09-08 12:01:56 +01:00
Jaebaek Seo
8a0ebd40f8
Correctly replace debug lexical scope of instruction (#3718)
When we update OpenCL.DebugInfo.100 lexical scopes e.g., DebugFunction,
we have to replace DebugScope of each instruction that uses the lexical
scope correctly.
2020-08-31 10:05:38 -04:00
Stefano Milizia
7e4948b2a5
Add LoopNestingDepth function to StructuredCFGAnalysis (#3754) 2020-08-27 08:34:46 -04:00
greg-lunarg
bceab9fab4
Do not register DebugFunction for functions optimized away. (#3749) 2020-08-26 23:25:43 -04:00
Jaebaek Seo
e02f178a71
Handle DebugScope in compact-ids pass (#3724)
We have to update OpenCL.DebugInfo.100 DebugScope for instructions if we
change its lexical scope or DebugInlinedAt.
2020-08-26 10:15:36 -04:00
greg-lunarg
12df3cafee
Fix SSA-rewrite to remove DebugDeclare for variables without loads (#3719) 2020-08-24 15:33:01 -04:00
Steven Perron
3f8501de9e
Add undef for inlined void function (#3720)
It is possible that the result of a void function call is used.  In case
it is used, we need something that still defines its id after inlining.
We use an undef for that purpose.

Fixes https://github.com/KhronosGroup/SPIRV-Tools/issues/3704
2020-08-24 15:08:55 -04:00
Diego Novillo
b79773a35d
CCP should mark IR changed if it created new constants. (#3732)
CCP should mark IR changed if it created new constants.

This fixes #3636.

When CCP is simulating statements, it will sometimes successfully fold
an instruction, which laters switches to varying.  The initial fold of
the instruction may generate a new constant K.

The problem we were running into is when K never gets propagated to the
IR.  Its definition will still exist, so CCP should mark the IR modified
in this case.

In fixing this bug, I noticed that an existing test was suffering from
the same bug.  The change also makes PassTest::SinglePassRunAndMatch()
return the result from the pass, so that we can check that the pass
marks the IR modified in this case.
2020-08-20 16:48:11 -04:00
Jaebaek Seo
3434cb0b00
Let ADCE pass check DebugScope (#3703)
In the existing code, ADCE pass does not check DebugScope of an
instruction when it checks the users of each instruction, which results
in removing OpenCL.Debug.100 instructions that are only used by
DebugScope. This commit lets ADCE pass add DebugScope of an instruction
to the live instruction set when the instruction is added to the live
instruction set.
2020-08-18 09:33:20 -04:00
André Perez
ee7f0c882f
spirv-opt: Implement opt::Function::HasEarlyReturn function (#3711) 2020-08-18 09:31:24 -04:00
alan-baker
b4c4da3e76
Improve non-semantic instruction handling in the optimizer (#3693)
* No longer blindly add global non-semantic info instructions to global
  types and values
  * functions now have a list of non-semantic instructions that succeed
    them in the global scope
  * global non-semantic instructions go in global types and values if
    they appear before any function, otherwise they are attached to the
    immediate function predecessor in the module
* changed ADCE to use the function removal utility
* Modified EliminateFunction to have special handling for non-semantic
  instructions in the global scope
  * non-semantic instructions are moved to an earlier function (or full
    global set) if the function they are attached to is eliminated
  * Added IRContext::KillNonSemanticInfo to remove the tree of
    non-semantic instructions that use an instruction
  * this is used in function elimination
* There is still significant work in the optimizer to handle
  non-semantic instructions fully in the optimizer
2020-08-13 14:54:14 -04:00
Vasyl Teliman
948577c5df
Fix the bug (#3680) 2020-08-13 09:09:57 -04:00
Vasyl Teliman
1435e427da
Fix the bug (#3683) 2020-08-12 13:00:41 -04:00
Jaebaek Seo
f0ca96d12c
Preserve debug info in dead-insert-elim pass (#3652)
When we determine the liveness of an insert, we have to ignore the debug
instruction (it must not have any impact on the decision).
2020-08-12 10:19:37 -04:00
Steven Perron
8e1380996d
Handle no index access chain in local access chain convert (#3678)
* Handle no index access chain in local access chain convert

Fixes #3643
2020-08-10 22:03:02 -04:00
Jaebaek Seo
b78f4b1518
Remove DebugDeclare only for target variables in ssa-rewrite (#3511)
For each local variable, ssa-rewrite should remove its DebugDeclare
if and only if it is replaced by any number of DebugValues for store
and phi instructions.

For example, when we have two variables `a` whose DebugDeclare
will be replaced to DebugValues by ssa-rewrite pass and `b` whose
DebugDeclare will not be replaced, we have to remove only DebugDeclare
for `a`, not `b`.
2020-07-31 10:00:30 -04:00
Jaebaek Seo
ebaefda666
Debug info preservation in loop-unroll pass (#3548)
When we copy the loop body to unroll it, we have to copy its
instructions but DebugDeclare or DebugValue used for the declaration
i.e., DebugValue with Deref must not be copied and only the first block
can contain those instructions.
2020-07-30 12:18:06 -04:00
dan sinclair
a1ea15c902
Update some language usage. (#3611)
This CL updates various bits of language in line with the guidelines
provided by Android
(https://source.android.com/setup/contribute/respectful-code)
2020-07-29 13:50:58 -04:00
Alastair Donaldson
f9b088fe0d
Avoid use of 'sanity' and 'sanity check' in the code base (#3585)
In line with:

  https://source.android.com/setup/contribute/respectful-code

this change uses the terms 'coherence' and 'coherence check' where
'sanity' and 'sanity check' were previously used.
2020-07-28 23:55:02 -04:00
Jaebaek Seo
6a3eb679bd
Preserve debug info in scalar replacement pass (#3461)
1. Set the debug scope and line information for the new replacement
   instructions.
2. Replace DebugDeclare and DebugValue if their OpVariable or value
   operands are replaced by scalars. It uses 'Indexes' operand of
   DebugValue. For example,

   struct S { int a; int b;}
   S foo; // before scalar replacement

   int foo_a; // after scalar replacement
   int foo_b;

   DebugDeclare %dbg_foo %foo %null_expr // before

   DebugValue %dbg_foo %foo_a %Deref_expr 0 // after
   DebugValue %dbg_foo %foo_b %Deref_expr 1 // means Value(foo.members[1]) == Deref(%foo_b)
2020-07-27 13:02:25 -04:00
Jaebaek Seo
7c901a49c9
Preserve OpenCL.DebugInfo.100 through private-to-local pass (#3571)
A debug instruction must not have any impact on the private-to-local
optimization.
2020-07-27 09:27:47 -04:00
alan-baker
f3cec93665
Support SPV_KHR_terminate_invocation (#3568)
Covers:
- assembler
- disassembler
- validator
- optimizer

Co-authored-by: David Neto <dneto@google.com>
2020-07-22 11:45:02 -04:00
Steven Perron
dca2c86bc8
Sink pointer instructions in merge return (#3569)
We cannot create an OpPhi for pointers, so we have to regenerate these
instructions instead.

Fixes #3030
Fixes #3266
2020-07-22 11:10:58 -04:00
greg-lunarg
cf7e922e70
Preserve OpenCL.DebugInfo.100 through elim-dead-code-aggressive (#3542)
Essentially, it marks all DebugInfo instructions in functions (and their operands) as live. It treats DebugDeclare and DebugValue with Deref as loads and so marks Stores of their variables as live.

It marks each DebugGlobalVariables as live except for its variable. After closure, it rechecks if the variable is live. If not, the DebugGlobalVariable instruction's variable operand is set to DebugInfoNone, per the DebugInfo spec.
2020-07-21 16:10:09 -04:00
Vasyl Teliman
8e0215afe0
spirv-opt: Add support for OpLabel to dominator analysis (#3516)
Fixes #3515.
2020-07-15 12:59:35 +01:00
Jaebaek Seo
4c33fb0d3d
Rewrite KillDebugDeclares() (#3513)
DebugInfoManager::KillDebugDeclares() must erase the variable id
from |var_id_to_dbg_decl_| after killing its DebugDeclare
instructions.
2020-07-14 14:47:16 -04:00
greg-lunarg
cf8c86a2d9
Preserve OpenCL.DebugInfo.100 through elim-local-single-store (#3498)
This pass basically follows the same process as ssa-rewrite: it adds a DebugValue after each Store and removes the DebugDeclare or DebugValue Deref. It only does this if all instructions that are dependent on the Store are Loads and are replaced.
2020-07-10 15:17:14 -04:00
Jaebaek Seo
a687057a83
Preserve debug info in vector DCE pass (#3497)
This commit lets the vector DCE pass preserve the OpenCL.DebugInfo.100
information properly. When the vector DCE pass determines the liveness
of instructions, the debug instructions must not affect the decision. In
addition, when it kills some instructions, it has to kill DebugValue
instructions that use the killed instructions. When it updates some
composite values to meaningful values (not undef), it has to remove
DebugValue because the value information becomes incorrect.
2020-07-10 10:19:34 -04:00
greg-lunarg
44428352ba
Upgrade elim-local-single-block for OpenCL.DebugInfo.100 (#3451)
Creates a DebugValue when removing a store to a local variable.
2020-07-09 17:21:39 -04:00
Jaebaek Seo
f8eddbbe59
Preserve OpenCL.100.DebugInfo in reduce-load-size pass (#3492)
The decision to reduce the load must be not affected by debug
instructions. For example, even when a DebugValue references a
result id of a loaded composite value, this change lets the
reduce-load-size pass reduce the load if the full composite value is not
used anywhere other than the DebugValue.
2020-07-08 16:34:00 -04:00
Jaebaek Seo
6a4da9da42
Debug info preservation in copy-prop-array pass (#3444)
When the pass replaces the local variable `OpVariable` ids to their
corresponding pointers, we have to update operands of DebugValue or
DebugDeclare instructions.
2020-07-06 13:48:12 -04:00
Jaebaek Seo
fc0dc3a9c7
Fix ADCE pass bug for mulitple entries (#3470)
When there are multiple entries and the shader has a variable with
WorkGroup storage class, those multiple entry functions store values to
the variable. Since ADCE pass uses def-use chains to propagate the work
list, some of instructions in the work list are not actually a part of
the currently processed function. As a result, it adds instructions in
other functions and put them in |live_insts_|. However, it does not
have the control flow information for those instructions in other
functions i.e., |block2headerBranch_| and |header2nextHeaderBranch_|.
When it processes those instructions (they are added when it processes a
different function), it skips handling them because they are already in
|live_insts_| and does not check |block2headerBranch_| and
|header2nextHeaderBranch_|, which results in skipping some branches.
Even though those branches are live branches, it considers they are dead
branches.
2020-06-29 13:08:48 -04:00
Jaebaek Seo
efaae24d00
Clear debug information for kill and replacement (#3459)
For many spirv-opt passes such as simplify-instructions pass, we have to
correctly clear the OpenCL.DebugInfo.100 debug information for
KillInst() and ReplaceAllUses(). If we keep some debug information that
disappeared because of KillInst() and ReplaceAllUses(), adding new
DebugValue instructions based on the existing DebugDeclare information
will generate incorrect information. This CL update DebugInfoManager
and IRContext to correctly clear debug information.
2020-06-25 15:48:26 -04:00
Ehsan
7a1af58785
Support OpCompositeExtract pattern in desc_sroa (#3456)
* Support load and extract pattern in desc_sroa.

* Fix typo in comments.

* Load replacement var before use; and added test.

* fix formatting

* Address code review comments.
2020-06-23 12:24:53 -05:00
Jaebaek Seo
d4b9f576eb
[spirv-opt] debug info preservation in ssa-rewrite (#3356)
Add OpenCL.DebugInfo.100 `DebugValue` instructions for store
and phi instructions of local variables to provide the debugger with
the updated values of local variables correctly.
2020-06-19 14:57:43 -04:00
Ehsan
2a1b8c0622
Updated desc_sroa to support flattening structures (#3448)
Not all structures should be flattened.  Code patterns used by DXC are used to create checks for which structures should be flattened.
2020-06-19 14:35:18 -04:00
Steven Perron
545d158a2f
Use structured order to unroll loops. (#3443)
Fixes #3441
2020-06-18 16:00:34 -04:00
Jaebaek Seo
5342930f91
Debug info preservation in dead branch elimination (#3425)
* Debug info preservation in dead branch elimination

The DeadBranchElimPass class already handles the OpenCL.DebugInfo.100
instructions correctly. This commit just adds a unit test to make sure
it preserves the information properly.

* Add unit test of ReplaceAllWith for debug instruction
2020-06-18 14:55:45 -04:00
Ehsan
a7112d544b
Eliminate branches with condition of OpConstantNull (#3438) 2020-06-16 13:31:03 -04:00
dan sinclair
52a5f074e9
Update access control lists. (#3433)
This CL updates the access control lists used in SPIRV-Tools to the more
descriptive allow/deny naming.
2020-06-15 13:20:40 -04:00
Jaebaek Seo
5543d5faa9
Debug info preservation in ccp pass (#3420)
* Debug info preservation in ccp pass

For constant propagation, the ccp pass already replaces the result id of
a value with a result id of the corresponding constant value. As a part
of the replacement, it correctly updates the operands of
DebugValue/DebugDeclare as well. Since we do not have to any addition
work other than the ccp pass itself, this commit just adds unit tests to
check the debug information preservation.
2020-06-12 10:54:06 -04:00
Jaebaek Seo
636f449e15
Add tests for merge-return debug info preservation (#3389)
* Add tests for merge-return debug info preservation

* No instruct btw merge and terminator
2020-06-02 10:32:03 -04:00
Jaebaek Seo
50b1557886
Preserve debug info in inline pass (#3349)
Handles the OpenCL100Debug extension in inlining.  It preserves the information that is available while also adding the debug inlined at for all of the inlining that it does.
2020-05-21 13:09:43 -04:00
Diego Novillo
4dbe18b0c8
Reject folding comparisons with unfoldable types. (#3370)
Reject folding comparisons with unfoldable types.

Fixes #3343 

When CCP is evaluating an instruction, it was trying to fold a
comparison with 64 bit integers.  This was causing a fold failure later
since the folder still cannot deal with 64 bit integers.
2020-05-21 12:58:08 -04:00
Jaebaek Seo
2b987c49a4
Handle OpConstantNull in ssa-rewrite (#3362)
ssa-rewrite fails in `MemPass::GetPtr()` when the SPIR-V code contains
`OpLoad` for the result id of `OpConstantNull` because of the out of
index access for an operand to get the base address. This commit fixes
it.

Fixes #3344
2020-05-20 12:00:51 -04:00
Steven Perron
85c7e7956b
Don't register edges twice in merge return (#3350)
Fixes #3267
2020-05-19 10:28:04 -04:00
Steven Perron
bd0a2da946
Revert "Revert "[spirv-opt] refactor inlining pass (#3328)" (#3342)" (#3345)
This reverts commit d4fac3451b.
2020-05-14 10:55:47 -04:00
Steven Perron
d4fac3451b
Revert "[spirv-opt] refactor inlining pass (#3328)" (#3342)
This reverts commit 233246bc9c.
2020-05-13 23:44:19 -04:00
Jaebaek Seo
233246bc9c
[spirv-opt] refactor inlining pass (#3328)
- No longer inline functions with early exits. Merge return can modify them so they can be inlined.
- Otherwise no functional change, should be just refactoring.
2020-05-13 23:17:19 -04:00
Steven Perron
63fa9114a9
Do merge return if the return is not at the end of the function. (#3337)
* Do merge return if the return is not at the end of the function.

We will remove the code in inlining to handle a return in the middle of
a function.  To inline those functions, we need to run merge return to
move the return to the end of the function.
2020-05-12 11:56:16 -04:00
Jaebaek Seo
c8590c18bd
Preserve debug info for wrap-opkill (#3331)
Preserve debug info for wrap-opkill
2020-05-06 12:57:57 -04:00
Alastair Donaldson
49842b88ee
Generalize IsReadOnlyVariable() to apply to pointers (#3325)
Generalizes the IsReadOnlyVariable() method, and related methods, so
that they can be used to ask whether pointer result ids are read-only.

Fixes #3324.
2020-04-30 22:47:20 +01:00
Jaebaek Seo
d0a87194f7
Set DebugScope for termination instructions (#3323)
Many high-level languages like HLSL and GLSL generate termination
instructions such as return and branch from the actual part of the
high-level language code like return and if statements. This commit lets
IrLoader set `DebugScope` for termination instructions.
2020-04-28 09:30:44 -04:00
Jaebaek Seo
42268740c9
Add debug information analysis (#3305)
We need an analysis for OpenCL.DebugInfo.100 extension instructions such
as a map between function id and its DebugFunction. This commit add an
analysis for it.
2020-04-27 15:18:55 -04:00
David Neto
eed48ae479
Add spvtools::opt::Operand::AsLiteralUint64 (#3320) 2020-04-27 09:38:06 -04:00
Steven Perron
7d65bce0bb
Sampled images as read-only storage (#3295)
There are some cases where a variable that is declared as a sampled
image could be read only.  That is when the image type has sampled == 1.

Fixes #3288
2020-04-14 12:58:05 -04:00
Steven Perron
e70d25f6fa
Struct CFG analysus and single block loop (#3293)
Loop headers must be marked as in the continue if the loop header is
also the continue target.

Fixes #3264
2020-04-13 10:08:31 -04:00
Jaebaek Seo
000040e707
Preserve debug info in eliminate-dead-functions (#3251)
* Preserve debug info in eliminate-dead-functions

The elimination of dead functions makes OpFunction operand of
DebugFunction invalid. This commit replaces the operand with
DebugInfoNone.
2020-04-13 09:29:36 -04:00
Steven Perron
34be23373b
Handle more cases in dead member elim (#3289)
* Handle more cases in dead member elim

- Rewrite composite insert and extract operations on SpecConstnatOp.
- Leaves assert for Access chain instructions, which are only allowed
for kernels.
- Other operations do not require any extra code will no longer cause an
assert.

Fixes #3284.
Fixes #3282.
2020-04-09 15:44:20 -04:00
alan-baker
af01d57b5e
Update dominates to check for null nodes (#3271)
* Update dominates to check for null nodes

Fixes #3270
2020-04-02 08:19:54 -04:00
alan-baker
f20c0d7971
Set wrapped kill basic block's parent (#3269)
Fixes #3268

* Set the parent of the basic block for the wrapper function
* Add a test
2020-04-01 12:31:57 -04:00
alan-baker
022da4d0e0
Fix identification of Vulkan images and buffers (#3253)
Fixes #3252

* Image and buffer queries did not account for optional level of
arrayness on the variable
  * new tests
2020-03-25 17:38:24 -04:00
Jaebaek Seo
1c8bda3721
Add data structure for DebugScope, DebugDeclare in spirv-opt (#3183)
When DebugScope is given in SPIR-V, each instruction following the
DebugScope is from the lexical scope pointed by the DebugScope in
the high level language. We add DebugScope struction to keep the
scope information in Instruction class. When ir_loader loads
DebugScope/DebugNoScope, it keeps the scope information in
|last_dbg_scope_| and lets following instructions have that scope
information.

In terms of DebugDeclare/DebugValue, if it is in a function body
but outside of a basic block, we keep it in |debug_insts_in_header_|
of Function class. If it is in a basic block, we keep it as a normal
instruction i.e., in a instruction list of BasicBlock.
2020-03-23 11:01:18 -04:00
David Neto
60104cd974
Add opt::Operand::AsCString and AsString (#3240)
It only works when the operand is a literal string.
2020-03-19 12:44:28 -04:00
Daniel Koch
5a97e3a391
Add support for KHR_ray_{query,tracing} extensions (#3235)
Update validator for SPV_KHR_ray_tracing.

* Added handling for new enum types
* Add SpvScopeShaderCallKHR as a valid scope
* update spirv-headers

Co-authored-by: alelenv <alele@nvidia.com>
Co-authored-by: Torosdagli <ntorosda@amd.com>
Co-authored-by: Tobias Hector <tobias.hector@amd.com>
Co-authored-by: Steven Perron <stevenperron@google.com>
2020-03-17 15:30:19 -04:00
greg-lunarg
1fe9bcc108
Instrument: Debug Printf support (#3215)
Create a pass to instrument OpDebugPrintf instructions.  This pass replaces all OpDebugPrintf instructions with instructions to write a record containing the string id and the all specified values into a special printf output buffer (if space allows). This pass is designed to support the printf validation in the Vulkan validation layers.

Fixes #3210
2020-03-12 09:19:52 -04:00
Diego Novillo
a9624b4d5d
Handle TimeAMD in AmdExtensionToKhrPass. (#3168)
This adds support for replacing TimeAMD with OpReadClockKHR.  The scope
for OpReadClockKHR is fixed to be a subgroup because TimeAMD operates
only on subgroup.
2020-02-03 12:13:32 -05:00
Arseny Kapoulkine
0265a9d4de
Implement constant folding for many transcendentals (#3166)
* Implement constant folding for many transcendentals

This change adds support for folding of sin/cos/tan/asin/acos/atan,
exp/log/exp2/log2, sqrt, atan2 and pow.

The mechanism allows to use any C function to implement folding in the
future; for now I limited the actual additions to the most commonly used
intrinsics in the shaders.

Unary folder had to be tweaked to work with extended instructions - for
extended instructions, constants.size() == 2 and constants[0] ==
nullptr. This adjustment is similar to the one binary folder already
performs.

Fixes #1390.

* Fix Android build

On old versions of Android NDK, we don't get std::exp2/std::log2
because of partial C++11 support.

We do get ::exp2, but not ::log2 so we need to emulate that.
2020-02-03 09:20:47 -05:00
Steven Perron
97f1d485b7 Dead branch elim fix (#3160)
We must treat a branch to the merge node of a switch that is in the
header of a construct as a nested construced.  The original merge
instruction is still needed in that case.
2020-01-28 10:17:43 -05:00
greg-lunarg
e7afeb060e Use dummy switch instead of dummy loop in MergeReturn pass. (#3151)
Fixes #3127
2020-01-24 12:20:14 -05:00
Jaebaek Seo
dd37d73c5e Handle conflict between debug info and existing validation rule (#3104)
* Allow OpExtInst for DebugInfo between secion 9 and 10

Fixes #3086

* Handle spirv-opt errors on DebugInfo Ext

* Add IR Loader test

* Fix ir loader bug

* Handle DebugFunction/DebugTypeMember forward reference

* Add test cases (forward reference to function)

* Support old DebugInfo extension

* Validate local debug info out of function
2020-01-23 17:04:30 -05:00
Jaebaek Seo
f8d7df760c
Fix OpLine bug of merge-blocks pass (#3130)
As explained in #3118, spirv-opt merge-blocks pass causes a
spirv-val error when an OpBranch has an OpLine in front of it.

OpLoopMerge
OpBranch ; Will be killed by merge-blocks pass
OpLabel  ; Will be killed by merge-blocks pass
OpLine   ; will be placed between OpLoopMerge and OpBranch - error!
OpBranch

To fix this issue, this commit moves line info of OpBranch to
OpLoopMerge.

Fixes #3118
2020-01-14 14:35:21 -05:00
David Neto
8aa423930d
Avoid pessimizing std::move (#3124)
Should fix a warning
2019-12-27 12:05:58 -05:00
greg-lunarg
9215c1b7df Fix convert-relax-to-half invalid code (#3099) (#3106) 2019-12-20 21:08:12 -05:00
David Neto
e70b009b0f
Add support for SPV_KHR_non_semantic_info (#3110)
Add support for SPV_KHR_non_semantic_info

This entails a couple of changes:

- Allowing unknown OpExtInstImport that begin with the prefix `NonSemantic.`
- Allowing OpExtInst that reference any of those sets to contain unknown
  ext inst instruction numbers, and assume the format is always a series of IDs
  as guaranteed by the extension.
- Allowing those OpExtInst to appear in the types/variables/constants section.
- Not stripping OpString in the --strip-debug pass, since it may be referenced
  by these non-semantic OpExtInsts.
- Stripping them instead in the --strip-reflect pass.

* Add adjacency validation of non-semantic OpExtInst

- We validate and test that OpExtInst cannot appear before or between
  OpPhi instructions, or before/between OpFunctionParameter
  instructions.

* Change non-semantic extinst type to single value

* Add helper function spvExtInstIsNonSemantic() which will check if the extinst
  set is non-semantic or not, either the unknown generic value or any future
  recognised non-semantic set.

* Add test of a complex non-semantic extinst

* Use DefUseManager in StripDebugInfoPass to strip some OpStrings

* Any OpString used by a non-semantic instruction cannot be stripped, all others
  can so we search for uses to see if each string can be removed.
* We only do this if the non-semantic debug info extension is enabled, otherwise
  all strings can be trivially removed.

* Silence -Winconsistent-missing-override in protobufs
2019-12-18 18:10:29 -05:00
greg-lunarg
fccbc00aca Make Instrumentation format version 2 the default (Step 1) (#3096)
* Make Instrumentation format version 2 the default (Step 1)

Add new interfaces without version number argument. Remove version 1
logic and tests. Version interfaces will be removed in step 2 after
layers have transitioned to new interface.

* Add error messages to InstrumentPass().
2019-12-16 14:18:47 -05:00
Steven Perron
00ca4e5bdf
Don't crash when folding construct of empty struct (#3092)
* Don't crash when folding construct of empty struct

An OpCompositeConstruct of an empty struct will be folded to a constant
under normal circumstances.  However, if the id limit has been reached
and the constant cannot be generated, then other folding rules will be
tried.

These rules do not handle the case of an empty struct.  We add allow it
to be handled.

Fixes http://crbug/1030194

* Changes based on the review.
2019-12-10 14:58:30 -05:00
Sarah
0a5d99d02c Permit the debug instructions in WebGPU SPIR-V - remove from the optimizer (#3083)
continuing #3063
fixing #3052
2019-12-03 11:21:26 -05:00
David Neto
af7410597e graphics robust access: use signed clamp (#3073)
Access chain indices are always interpreted as signed integers.
So use signed clamp instead of unsigned clamp.  We must also
clamp to the max signed int for the index type.

Fixes #3072
2019-12-03 11:18:56 -05:00