Commit Graph

2393 Commits

Author SHA1 Message Date
Steven Perron
148c97f687
Avoid use of type manager in extact->construct folding (#5684)
* Avoid use of type manager in extact->construct folding

When dealing with structs the type manager merge two different structs
into a single entry if they have all of the same decorations and
element types. This is because they hash to the same value in the hash
table. This can cause problems if you need to get the id of a type from
the type manager because you could get either one. In this case, it
returns the wrong one.

The fix avoids using the type manager in one place. I have not
looked closely at other places the type manager is used to make
sure it is used safely everywhere.

Fixes #5624

* Remove use of TypeManager::GetId

This removes a use of TypeManager::GetId by keeping the id around. This
avoid a potential problem if the type manager gets confused. These types
of bugs are hard to generate test cases for, so I do not have a test.
However, existing tests make sure that do not regress.
2024-05-31 14:13:20 +02:00
Kévin Petit
7e1a8cdc53
Basic support for SPV_EXT_replicated_composites (#5690)
* Basic support for SPV_EXT_replicated_composites

Validation will follow as a separate PR (still need to write a test suite)

Change-Id: Ic95fa6ce39d32f5ac2787bc38dba2748c9cc58f7
Signed-off-by: Kevin Petit <kevin.petit@arm.com>

* Update SPIRV-Headers

Change-Id: I6c0df248d99c13b49d78528d035a4222027c0232

---------

Signed-off-by: Kevin Petit <kevin.petit@arm.com>
2024-05-30 10:58:44 -04:00
Spencer Fricke
3d24089292
spirv-val: Add Duplicate EntryPoint Builtin check (#5678)
* spirv-val: Add Decoration::builtin()
* spirv-val: Add Duplicate EntryPoint Builtin check
* spirv-val: Handle Built-ins in/out of block
* spirv-val: Remove extra CheckBuiltInVariable
2024-05-29 14:38:37 -04:00
Steven Perron
336b5710a5
Do not fold mul and adds to generate fmas (#5682)
This removes the folding rules added in #4783 and #4808. They lead to
poor code generation on Adreno devices when 16-bit floating point values
were used. Since this change is transformation is suppose to be neutral,
there is no general reason to continue doing it.

I have talked to the owners of SwiftShader, and they do not mind if the
transform is removed. They were the ones the requested the change in the
first place.

Fixes #5658
2024-05-22 13:01:26 -04:00
Sven van Haastregt
e2646f5e95
spirv-val: Consider target env for OpReadClockKHR scope (#5681)
The Scope operand of `OpReadClockKHR` was always validated using the
Vulkan environment rules, which only allow `Subgroup` or `Device`.
For the OpenCL environment, `Workgroup` is also a valid Scope, so
`Workgroup` should not be rejected in the universal environment.

Guard the existing Scope check behind `spvIsVulkanEnv` and add a new
Scope check for the OpenCL environment.

Signed-off-by: Sven van Haastregt <sven.vanhaastregt@arm.com>
2024-05-21 13:02:17 -04:00
alan-baker
ccf3e3c103
Improve matrix layout validation (#5662)
* Check for matrix decorations on arrays of matrices
  * MatrixStide, RowMajor and ColMajor can be applied to matrix or
    arrays of matrix members
  * Check that matrix stride satisfies alignment in arrays
2024-05-14 15:13:54 -04:00
Sven van Haastregt
199038f10c
spirv-val: Validate MemoryAccessMask of OpCooperativeMatrixStoreKHR (#5668)
Reject `OpCooperativeMatrixStoreKHR` with a `MakePointerVisibleKHR`
MemoryAccess operand, as `MakePointerVisibleKHR` is not supposed to be
used with store operations.

The `CoopMatKHRStoreMemoryAccessFail` test failed to catch this
because it used the helper function `GenCoopMatLoadStoreShader` which
generates `...NV` instead of `...KHR` instructions.  Add a new helper
function to generate similar shaders for the KHR extension, as the NV
and KHR extensions have various subtle differences that makes
parameterizing the original helper function non-trivial.

Signed-off-by: Sven van Haastregt <sven.vanhaastregt@arm.com>
2024-05-10 15:49:10 -04:00
Jeremy Gebben
9241a58a80
opt: Remove bindless and buff addr instrumentation passes (#5657)
These were only used by Vulkan-Validation layers, but they
have been replaced by other code for several months.
2024-05-02 18:52:17 -04:00
Spencer Fricke
57a42e6c1d
spirv-val: Separate Location check for tess patch (#5654) 2024-04-30 12:29:22 -04:00
Wooyoung Kim
53c0736064
A fix to support of SPV_QCOM_image_processing2 (#5646)
Fixing validation of decorations attached to
texture/sampler operands of OpImageBlockMatchWindowSSDQCOM
and OpImageBlockMatchWindowSADQCOM
2024-04-18 17:30:20 -04:00
Spencer Fricke
2904985aee
spirv-val: Add Vulkan check for Rect Dim in OpTypeImage (#5644) 2024-04-15 10:56:12 -04:00
alan-baker
02470f606f
Validate duplicate decorations and execution modes (#5641)
* Disallow duplicate decorations generally

* Only FuncParamAttr and UserSemantic can be applied to the same target
  multiple times
* Unchecked: completely duplicate UserSemantic and FuncParamAttr

* Disallow duplicate execution modes generally
  * Exceptions for float controls, float controls2 and some intel
    execution modes

* Fix invalid fuzzer transforms
2024-04-12 08:51:41 -04:00
Rodrigo Locatti
6761288d39
Validator: Support SPV_NV_raw_access_chains (#5568) 2024-04-10 10:40:10 -04:00
Diego Novillo
3983d15a1d
Fix rebuilding types with circular references (#5623). (#5637)
This fixes the problem reported in #5623 using the observation that if
we are re-building a type that already exists in the type pool, we
should just return that type.

This makes type rebuilding more efficient, and it also prevents the
type builder from getting itself into infinite recursion (as reported in
this issue).

In fixing this, I found a couple of other bugs in the type builder:

- When rebuilding an Array type, we were not re-building the element
  type. This caused stale type references in the rebuilt type.

- This bug had not been caught by the test, because the test itself had
  a bug in it: the test was rebuilding types on top of the same ID (the
  ID counter was never incremented).

Initially, the bug in the test caused a failure with the new logic in
the builder because we now return types from the pool directly, which
causes a failure when two incompatible types are registered under the
same ID.

Fixing that issue in the test exposed another bug in the rebuilder: we
were not re-building the element type for Array types. This was causing
a stale type reference inside Array types which was later caught by the
type removal logic in the test.
2024-04-09 10:36:21 -04:00
Jeremy Hayes
ade1f7cfd7
Add AliasedPointer decoration (#5635)
Fix #5607

When inlining, decorate return variable with AliasedPointer if the
storage class of the pointee type is PhysicalStorageBuffer.
2024-04-05 11:45:55 -06:00
Wooyoung Kim
9bd44d028e
Suppot for SPV_QCOM_image_processing2 (#5582) 2024-02-28 16:26:28 -05:00
alan-baker
fbc7a14b3e
Fix access chain struct checks (#5592)
* Fix access chain struct checks

Fixes https://crbug.com/oss-fuzz/66948

* Negative indices are invalid for struct access
* Fix typos

* formatting
2024-02-27 15:54:08 -05:00
Spencer Fricke
1b643eac5d
spirv-val: Make Constant evaluation consistent (#5587)
Bring 64-bit evaluation in line with 32-bit evaluation.
2024-02-21 17:52:13 -05:00
Jeff Bolz
b0a5c4ac12
SPV_NV_shader_atomic_fp16_vector (#5581) 2024-02-14 15:58:12 -05:00
Spencer Fricke
f9184c6501
spirv-val: Revert Validate PhysicalStorageBuffer Stage Interface (#5575) 2024-02-13 21:24:20 -05:00
Spencer Fricke
20ad38c18d
spirv-val: Multiple interface var with same SC (#5528) 2024-02-13 15:55:43 -05:00
Steven Perron
e08c012b19
[OPT] Identify arrays with unknown length in copy prop arrays (#5570)
* [OPT] Identify arrays with unknown length in copy prop arrays

The code in copy propagate arrays assumes that the length of an
OpTypeArray is known at compile time, but that is not true when the size
is an OpSpecConstant. We try to fix that assumption.

Fixes https://crbug.com/oss-fuzz/66634
2024-02-13 14:41:38 -05:00
Ben Ashbaugh
0c986f596d
update image enum tests to remove Kernel capability (#5562)
We are removing Kernel from the image channel order and image
channel data type enums because Kernel is already required
transitively, so we need to update the tests to match.
2024-02-13 11:07:39 -05:00
Steven Perron
b7413609cf
[OPT] Use new instruction folder for for all opcodes in spec consti folding (#5569)
* [OPT] Use new instruction folder for for all opcodes in spec consti folding

When folding and OpSpecConstantOp, we use the new instruction folder for
a small number of opcodes. This enable the new instruction folder for
all opcodes and uses the old one as a fall back. This allows us to
remove some code from the older folder that is now covered by the new
one.

Fixes #5499
2024-02-12 19:52:55 +00:00
Spencer Fricke
784b064f90
spirv-val: Validate PhysicalStorageBuffer Stage Interface (#5539)
Disallow PhysicalStorageBuffer pointers in Input and Output storage classes.
2024-02-12 09:51:38 -05:00
Steven Perron
a8959dc653
Fold 64-bit int operations (#5561)
Adds folding rules that will fold basic artimetic for signed and
unsigned integers of all sizes, including 64-bit.

Also folds OpSConvert and OpUConvert.
2024-02-09 14:02:48 -05:00
Steven Perron
032c15aaf5
[NFC] Refactor code to fold instruction in fold tests. (#5558)
We repeat basically the same code multiple times in the different types of
folding tests. This commit adds a function that builds the module, finds the
instruction to fold, and folds it. Doing the routine checks at the same
time. We also have a couple generic functions for checking that an
instruction is a constant with the expected value.
2024-02-06 13:05:05 -05:00
Nathan Gauër
ab59dc6087
opt: prevent meld to merge block with MaximalReconvergence (#5557)
The extension SPV_KHR_maximal_reconvergence adds more constraints
around the merge blocks, and how the control flow can be altered.

The one we address here is explained in the following part of the spec:

  Note: This means that the instructions in a break block will execute as if
  they were still diverged according to the loop iteration. This restricts
  potential transformations an implementation may perform on the IR to match
  shader author expectations. Similarly, instructions in the loop construct
  cannot be moved into the continue construct unless it can be proven that
  invocations are always converged.

Until the optimizer is clever enough to determine if the invocation
have already converged, we shall not meld a block which branches to a
merge block into it, as it might move some instructions outside of the
convergence region.

This behavior being only required with the extension, this commit
behavior change is gated by the extension.
This means using wave operations without the maximal reconvergence
extension might lead to undefined behaviors.

Co-authored-by: Natalie Chouinard <chouinard.nm@gmail.com>
2024-02-06 06:12:00 -05:00
Ben Doherty
8d3ee2e8f0
spirv-opt: Fix OpCompositeExtract relaxation with struct operands (#5536) 2024-02-01 15:19:02 -07:00
Spencer Fricke
61c51d4baf
spirv-val: Add Mesh Primitive Built-In validaiton (#5529) 2024-02-01 14:20:42 -05:00
Natalie Chouinard
5d3c8b73f7
opt: Add OpEntryPoint to DescriptorScalarReplacement pass (#5553)
Add OpEntryPoint to the list of instructions processed by the
DescriptorScalarReplacement pass. This is necessary for SPIR-V 1.4 and
above where global variables must be included in the interface.

Fixes microsoft/DirectXShaderCompiler#5962
2024-02-01 09:50:36 -05:00
Scott Todd
80bc99c3d4
Skip entire test/ folder if SPIRV_SKIP_TESTS is set. (#5548)
Without this (or similar filtering), the `spirv-tools_expect_unittests` and `spirv-tools_spirv_test_framework_unittests` Python tests at `test/tools/` get defined even when `SPIRV_SKIP_TESTS` is set.
2024-01-26 16:47:13 -05:00
ruiminzhao
b951948eaa
SPV_KHR_quad_control (#5547)
* SPV_KHR_quad_control

1. Add two new execute modes: RequireFullQuadsKHR and QuadDerivativesKHR
2. Add two opCodes: OpGroupNonUniformQuadAllKHR and
   OpGroupNonUniformQuadAnyKHR
3. Add one Capability: QuadControlKHR

* update DEPS

* Fixes

* Build fixes
* Formatting fixes
* Test fixes

* formatting

---------

Co-authored-by: Alan Baker <alanbaker@google.com>
2024-01-26 15:49:56 -05:00
Natalie Chouinard
0045b01ff9
opt: Add VulkanMemoryModelDeviceScope to trim (#5544)
Add the VulkanMemoryModelDeviceScope capability to the capability
trimming pass. According the the spec, "If the Vulkan memory model is
declared and any instruction uses Device scope, the
VulkanMemoryModelDeviceScope capability must be declared." Since this
case, based on the type of an operand, is not covered by the JSON
grammar, it is added explicitly.
2024-01-25 14:05:04 -05:00
alan-baker
ef2f432364
Add support for SPV_KHR_float_controls2 (#5543)
* Test asm/dis for SPV_KHR_float_controls2
* SPV_KHR_float_controls2 validation

---------

Co-authored-by: David Neto <dneto@google.com>
2024-01-25 10:22:09 -05:00
alan-baker
de3d5acc04
Add tooling support for SPV_KHR_maximal_reconvergence (#5542)
* Validation for SPV_KHR_maximal_reconvergence
* Add pass to add/remove maximal reconvergence execution mode
---------

Co-authored-by: David Neto <dneto@google.com>
2024-01-25 09:39:49 -05:00
David Neto
14000ad47a
Use python3 explicitly. (#5540)
Some Linux images don't ship with a plain 'python'
2024-01-23 15:42:34 -05:00
Spencer Fricke
c96fe8b943
spirv-val: Re-enable OpControlBarrier VU (#5527) 2024-01-17 11:18:23 -05:00
Steven Perron
36be541ee3
Remove unnecessary debug code (#5523) 2024-01-11 07:55:04 -08:00
Nathan Gauër
c7affa1707
opt: add Int16 and Float16 to capability trim pass (#5519)
Add support for Int16 and Float16 trim.

Signed-off-by: Nathan Gauër <brioche@google.com>
2024-01-04 20:01:03 +01:00
alan-baker
e03c8f5c8e
Fix broken build (#5505)
Fixes #5503

* SPIRV-Headers name change broke the build
  * Update SPIRV-Headers deps and fix
2023-12-11 11:45:10 -05:00
Jeremy Gebben
6b4f0c9d0b
instrument: Fix handling of gl_InvocationID (#5493)
This is an int and needs to be cast to a unit for inclusion in the
stage specific data passed to the instrumentation check function.
2023-12-05 09:59:51 -07:00
Jeremy Gebben
b5d60826e9
printf: Remove stage specific info (#5495)
Remove stage specific debug info that is only needed by GPU-AV.
This allows debug printfs to be used in multi-stage shader modules.

Fixes #4892
2023-12-04 15:43:36 -07:00
ncesario-lunarg
2da75e152e
Do not crash when tryingto fold unsupported spec constant (#5496)
Remove assertion in FoldWithInstructionFolder; there are cases where
folding spec constants is unsupported.

Closes #5492.
2023-12-04 08:48:16 -05:00
Sajjad Mirza
246e6d4c68
spirv-val: Loosen restriction on base type of DebugTypePointer and DebugTypeQualifier (#5479)
* Allow base type for DebugTypePointer and DebugTypeQualifier to be any DebugType
2023-11-17 10:22:46 -05:00
ChristianReinbold
0df791f97a
Fix nullptr argument in MarkInsertChain (#5465)
Fixes an access violation issue that sporadically occured for me when DXC uses spirv-opt to legalize generated spirv code.
2023-11-16 19:36:32 +00:00
Nathan Gauër
f43c464d53
opt: add PhysicalStorageBufferAddresses to trim (#5476)
The PhysicalStorageBufferAddresses capability can now be
trimmed. From the spec, it seems any instruction enabled by this
required some operand to have the PhysicalStorageBuffer storage class.
This means checking the storage class is enough.

Now, because the pass uses the grammar, we don't need to add any
new logic.

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-11-14 12:49:04 -05:00
Nathan Gauër
c91e9d09b5
opt: add StorageImageReadWithoutFormat to cap trim (#5475)
The StorageImageReadWithoutFormat capability is only required when
an image type with the format set to Unknown is used with some specific
OpImageRead or OpImageSparseRead instructions.

This patch adds the required code to the capability trimming pass to
remove the StorageImageReadWithoutFormat capability when not required.

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-11-14 09:29:31 -05:00
Steven Perron
9e7a1f2ddd
Fix array size calculation (#5463)
The function that get the number of elements in a composite variable
returns an incorrect values for the arrays. This is fixed, so that it
returns the correct number of elements for arrays where the number of
elements is represented as a 32-bit integer and is known at compile
time.

Fixes #4953
2023-11-02 13:29:57 -04:00
Steven Perron
a08f648c86
Remove references to __FILE__ (#5462)
* Remove references to __FILE__

Uses of `__FILE__` leak the directory structure of the machine used to
build because it adds a string to the string table with the full path
name. I've removed the uses that show up in the release builds.

Fixes #5416
2023-11-01 15:19:48 -07:00
Spencer Fricke
c87755bb9f
spirv-val: Add WorkgroupMemoryExplicitLayoutKHR check for Block (#5461) 2023-11-01 10:48:40 -04:00
Cassandra Beckley
73876defc8
opt: support 64-bit OpAccessChain index in FixStorageClass (#5446)
The SPIR-V specification allows any scalar integer type as an index. DXC
usually emits indexes as 32-bit integer types, however, in some cases it
is possible to make it emit 64-bit indexes instead (as in
https://github.com/microsoft/DirectXShaderCompiler/issues/5638).
2023-10-19 20:02:46 +00:00
Steven Perron
5bb595091b
Add ComputeDerivativeGroup*NV capabilities to trim capabilities pass. (#5430)
* Add ComputeDerivativeGroup*NV capabilities to trim capabilities pass.

* Add SPV_NV_compute_shader_derivatives to allow lists

No tests needed for this. The code path is well tested. Just adding new
data.
2023-10-16 19:03:33 +00:00
Cassandra Beckley
023a8c79e9
opt: add Float64 capability to trim pass (#5428) 2023-10-05 11:12:09 +02:00
Cassandra Beckley
1bc0e6f59a
Add a new legalization pass to dedupe invocation interlock instructions (#5409)
Add a new legalization pass to dedupe invocation interlock instructions

DXC will be adding support for HLSL's rasterizer ordered views by using
the SPV_EXT_fragment_shader_interlock_extension. That extension
stipulates that if an entry point has an interlock ordering execution
mode, it must dynamically execute OpBeginInvocationInterlockEXT and
OpEndInvocationInterlockEXT, in that order, exactly once. This would be
difficult to determine in DXC's SPIR-V backend, so instead we will emit
these instructions potentially multiple times, and use this legalization
pass to ensure that the final SPIR-V follows the specification.

This PR uses data-flow analysis to determine where to place begin and
end instructions; in essence, determining whether a block contains or is
preceded by a begin instruction is similar to a specialized case of a
reaching definitions analysis, where we have only a single definition,
such as `bool has_begun = false`. For this simpler case, we can compute
the set of blocks using BFS to determine the reachability of the begin
instruction.

We need to do this for both begin and end instructions, so I have
generalized portions of the code to run both forward and backward over
the CFG for each respective case.
2023-09-27 19:54:10 -04:00
Jeremy Gebben
ee7598d497
instrument: Use Import linkage for instrumentation functions (#5355)
These functions are getting far too complicated to code in SPIRV-Tools
C++. Replace them with import stubs so that the real implementations
can live in Vulkan-ValidationLayers where they belong.

VVL will need to define these functions in spirv and link them to the
instrumented version of the user's shader.

From here on out, VVL can redefine the functions and any data they use
without updating SPIRV-Tools. Changing the function declarations will
still require both VVL and SPIRV-Tools to be updated in lock step.
2023-09-20 10:50:30 -06:00
David Neto
a996591b1c
Update SPIRV-Headers, add cache control operand kinds (#5406)
* Update SPIRV-Headers, add cache control operand kinds

Adds SPV_OPERAND_TYPE_LOAD_CACHE_CONTROL
and  SPV_OPERAND_TYPE_STORE_CACHE_CONTROL,
from SPV_INTEL_cache_controls

Fixes: #5404

* Update tests: remove Kernel from constant sampler enum dependencies

This corresponds to header change
https://github.com/KhronosGroup/SPIRV-Headers/pull/378
2023-09-13 17:43:12 -04:00
Cassandra Beckley
361638cfd0
Make sure that fragment shader interlock instructions are not removed by DCE (#5400) 2023-09-11 15:26:10 -04:00
Nathan Gauër
47b63a4d7d
val: re-add ImageMSArray validation (#5394)
This has been removed in #4752, but not added since.

* fixup! val: re-add ImageMSArray validation

clang-format
2023-09-07 09:39:28 -04:00
Nathan Gauër
4e0b94ed7a
opt: add ImageMSArray capability to trim pass. (#5395)
From the Capability's text in the SPIRV spec:

```
An MS operand in OpTypeImage indicates multisampled, used with an
OpTypeImage having Sampled == 2 and Arrayed == 1.
```

Adding this logic to the capability trimming pass.
2023-09-05 18:36:03 +00:00
Nathan Gauër
1f07f483ef
opt: add raytracing/rayquery to trim pass (#5397)
Adds the RayTracingKHR and RayQueryKHR capabilities to
the supported capabilities list (this includes the linked extension).
(NV and KHR capabilities/extensions shared the same IDs, so it also
works for NV flavors of those).
2023-09-05 14:36:14 +00:00
Nathan Gauër
1121c23198
opt: add Int64 capability to trim pass (#5398)
Adds support for Int64 capability trimming.
2023-09-05 09:47:46 -04:00
Nathan Gauër
3cc7e1c4c3
NFC: rename tests using capability as prefix (#5396) 2023-09-04 14:32:28 -07:00
Cassandra Beckley
4c16c35b16
opt: add FragmentShader*InterlockEXT to capability trim pass (#5390)
* opt: add FragmentShader*InterlockEXT to capability trim pass

* move to addInstructionRequirementsForOpcode
2023-09-04 11:27:56 +02:00
Jeremy Gebben
714966003d
opt: Add SwitchDescriptorSetPass (#5375)
This is a simple pass to change DescriptorSet decoration values.
2023-08-22 00:16:35 +00:00
Jeremy Gebben
6520d83eff
linker: Add --use-highest-version option (#5376)
Currently spirv-link fails if all input files don't use the same
SPIR-V version. Add an option to instead use the highest input
version as the output version. Note that if one of the 'old'
input files uses an opcode that is deprecated in the 'new'
version, the output spirv will be invalid.
2023-08-21 17:05:33 -06:00
Wooyoung Kim
89ca3aa571
SPV_QCOM_image_processing support (#5223) 2023-08-15 15:15:21 -04:00
Nathan Gauër
0f17d05c48
opt: add bitmask support for capability trimming (#5372)
Some operands are not simple values, but bitmasks.
The lookup in the table for required decomposing the mask into
single values.
This commit adds support for such operands, like MinLod|Offset.
2023-08-15 09:50:57 -04:00
Nathan Gauër
8714d7fad2
enable StorageUniform16 (#5371)
Adds support for the StorageUniform16 capability.
2023-08-10 13:54:31 -04:00
David Neto
8e3da01b45
Move token version/cap/ext checks from parsing to validation (#5370)
A token is allowed to parse even when it's from the wrong
version, or is not enabled by a capability or extension.
This allows more modules to parse.

Version/capability/extension checking is fully moved to
validation instead.

Fixes: #5364
2023-08-10 12:19:12 -04:00
Nathan Gauër
4788ff1578
opt: add StorageUniformBufferBlock16 to trim pass (#5367)
Add StorageUniformBufferBlock16 to the list of enabled capabilities.
2023-08-10 14:21:35 +00:00
Nathan Gauër
ebda56e352
opt: add StoragePushConstant16 to trim pass (#5366)
* opt: add StoragePushConstant16 to trim pass

* fix comment
2023-08-10 12:34:46 +00:00
Nathan Gauër
60e684fe71
opt: fix StorageInputOutput16 trimming. (#5359)
* opt: fix StorageInputOutput16 trimming.

While integrating this pass into DXC, I found a lot of missing
cases. This PR fixes a few issues centered around this capability
while laying out fondations for more fixes.

1. The grammar can define extensions in operand & opcode tables.
   - opcode can rely on common capabilities, but require a new
     extension.
   - opcode can also rely on a capability which requires an extension.
   Sometimes, the extension is listed twice, in the opcode, and
   capability. But this redundancy is not guaranteed.

2. minVersion check. The condition was flipped: we added the extension
   when the minVersion was less than current.
   Didn't noticed the issue as I only tests on the default env.

3. Capability/Extension instructions were not ignored.
   - `OpCapability Foo` will require the `Foo` capability.
   - it doesn't mean the module requires the `Foo` capability.
   Same for extensions.

This commit adds disabled tests, for fixes which are too large to
be brought into this already large PR.
2023-08-09 06:30:23 -04:00
David Neto
09b76c23ea
Update SPIRV-Headers; test some coop matrix enums (#5361)
Test:
  MatrixASignedComponentsKHR
  MatrixBSignedComponentsKHR
  MatrixCSignedComponentsKHR
  ResultSignedComponentsKHR
2023-08-04 14:50:54 -04:00
Jeremy Gebben
47fff21d52
instrument: Reduce number of inst_bindless_stream_write_6 calls (#5327)
Multiple calls to this function were causing vkCreateGraphicsPipelines
to be 3x slower on some driver. I suspect this was because each
call had to be inlined separately which bloated the code and caused
more work in the driver's SPIRV -> native instruction compilation.
2023-08-01 13:49:12 -06:00
ncesario-lunarg
a0f1c87272
opt: Fix incorrect half float conversion (#5349)
Fixes image operands not decorated as relaxed from
getting marked relaxed and converted to half precision.

Fixes #5044.
2023-07-26 10:03:24 -04:00
Nathan Gauër
35d8b05de4
opt: add capability trimming pass (not default). (#5278)
This commit adds a new optimization which tries to remove unnecessary
capabilities from a SPIR-V module.

When compiling a SPIR-V module, you may have some dead-code using
features gated by a capability.
DCE will remove this code, but the capability will remain. This means
your module would still require some capability, even if it doesn't
require it. Calling this pass on your module would remove obsolete
capabilities.

This pass wouldn't be enabled by default, and would only be usable
from the API (at least for now).

NOTE: this commit only adds the basic skeleton/structure, and
doesn't mark as supported many capabilities it could support.
I'll add them as supported as I write tests.

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-07-25 16:52:41 +02:00
Steven Perron
d52c39c37d
Do not crash when folding 16-bit OpFDiv (#5338)
The code currently tries to get the value of the floating point constant
to see if it is -0.0. However, we are not able to get the value for
16-bit floating point value, and we hit an assert.

To avoid this, we add an early check for the width to make sure it is
either 32 or 64.

Fixes https://github.com/microsoft/DirectXShaderCompiler/issues/5413.
2023-07-21 10:17:12 -04:00
Nathan Gauër
17d9669d51
enumset: add iterator based constructor/insert (#5344)
Expanding a bit the EnumSet API to have iterator-based
insert and constructors (like the STL).
This is also a pre-requisite from the capability-trimming pass as
it allows to build a const set from a constexpr std::array easily.

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-07-20 17:54:50 +00:00
Nathan Gauër
bf03d40922
opt: change Get* functions to return const& (#5331)
GetCapabilities returned a const*, and GetExtensions did not exist.
This commit adds GetExtensions, and changes the return value to
be a const&.

This commit also removes the overload to GetCapabilities which returns
a mutable set, as it is unused.

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-07-20 10:18:19 -04:00
David Neto
876ccc6cd5
Add /bigobj to test_opt for VS 2017 (#5336)
This apparently is required for debug builds.

Fixes: #5335
2023-07-20 10:14:35 -04:00
ncesario-lunarg
7dd5f95d25
[spirv-opt] Handle OpFunction in GetPtr (#5316)
When using PhysicalStorageBuffer it is possible for a function to
return a pointer type. This was not being handled correctly in
`GetLoadedVariablesFromFunctionCall` in the DCE pass because
`IsPtr` returns the wrong result.

Fixes #5270.
2023-07-17 19:16:25 +00:00
Nathan Gauër
85a4482131
NFC: makes the FeatureManager immutable for users (#5329)
* NFC: makes the FeatureManager immutable for users

The FeatureManager contains some internal state, like
a set of capabilities and extensions. Those are derived
from the module.

Before this commit, the FeatureManager exposed Remove* functions
which could unsync the reported extensions/capabilities from
the truth: the module.

The only valid usecase to remove items directly from the FeatureManager
is by the context itself, when an instruction is killed:
instead of running the whole an analysis, we remove the single outdated
item.

The was 2 users who mutated its state:
 - one to invalidate the manager. Moved to call a reset function.
 - one who removed an extension from the feature manager after removing
   it from the module. This logic has been moved to the context, who
   now handles the extension removal itself.

Signed-off-by: Nathan Gauër <brioche@google.com>

* clang-format

* add RemoveCapability since the fuzztests are using it

* add tests

---------

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-07-17 11:15:08 -04:00
Nathan Gauër
29431859f5
NFC: replace EnumSet::ForEach with range-based-for (#5322)
EnumSet now supports iterators, meaning we can remove the custom
ForEach.

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-07-13 14:40:47 -04:00
Nathan Gauër
5b4fb072eb
enumset: fix bug in the new iterator class (#5321)
The iterator class was initialized by setting the offset
and bucket to 0. Big oversight: what if the first enum is
not valid? Then `*iterator->begin()` would return the wrong
value.

Because the first capacity is Matrix, this bug was not visible by
any SPIRV test.
And this specific case wasn't tested correctly in the new enumset tests.

Signed-off-by: Nathan Gauër <brioche@google.com>

---------

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-07-13 09:55:24 -04:00
Jeremy Gebben
9266197c37
instrument: Cast gl_VertexIndex and InstanceIndex to uint (#5319)
This avoids errors like this from instrumenting vertex shaders:

error: 165: Expected Constituents to be scalars or vectors of the
  same type as Result Type components
  %195 = OpCompositeConstruct %v4uint %uint_0 %191 %194 %uint_0
2023-07-12 15:12:26 -06:00
Nathan Gauër
3424b16c10
enumset: STL-ize container (#5311)
This commit adds forward iterator, and renames functions to
it matches the std::unordered_set/std::set better.
This goes against the SPIR-V coding style, but might be better in
the long run, especially when this set is used along real STL
sets.
(Right now, they are not compatible, and requires 2 syntaxes).

This container could in theory handle bidirectional
iterator, but for now, only forward seemed required for
our use-cases.

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-07-12 11:34:44 -04:00
Spencer Fricke
7ff331af66
source: Give better message if using new Source Language (#5314) 2023-07-11 11:50:41 -04:00
alan-baker
0530a532fc
Validate GroupNonUniform instructions (#5296)
Fixes #5283

* Validate group non-uniform instructions
2023-07-11 08:40:40 -04:00
Nathan Gauër
0f3bea06ef
NFC: rewrite EnumSet to handle larger enums. (#5289)
The current EnumSet implementation is only efficient for enums with
values < than 64. The reason is the first 63 values are stored as a
bitmask in a 64 bit unsigned integer, and the other values are stored
in a std::set.
For small enums, this is fine (most SPIR-V enums have IDs < than 64),
but performance starts to drop with larger enums (Capabilities,
opcodes).

Design considerations:
----------------------

This PR changes the internal behavior of the EnumSet to handle enums
with arbitrary values while staying performant.
The idea is to extend the 64-bits buckets sparsely:
 - each bucket can store 64 value, starting from a multiplier of 64.
This could be considered as a hashset with linear probing.

- For small enums, there is a slight memory overhead due to the bucket
storage, but lookup is still constant.
- For linearly distributed values, lookup is constant.
- Worse case for storage are for enums with values which are multiples of 64.
But lookup is constant.
- Worse case for lookup are enums with a lot of small ranges scattered in
the space (requires linear probing).

For enums like capabilities/opcodes, this bucketing is useful as values
are usually scatters in distinct, but almost contiguous blocks.
(vendors usually have allocated ranges, like [5000;5500], while [1000;5000]
is mostly unused).

Benchmarking:
-------------

Benchmarking was done in 2 ways:
 - a benchmark built for the occasion, which only measure the EnumSet
   performance.
 - SPIRV-Tools tests, to measure a more realist scenario.

Running SPIR-V tests with both implementations shows the same
performance (delta < noise). So seems like we have no regressions.
This method is noisy by nature (I/O, etc), but the most representative
of a real-life scenario.

Protocol:
 - run spirv-tests with no stdout using perf, multiple times.
Result:
 - measure noise is larger than the observed difference.

The custom benchmark was testing EnumSet interfaces using SPIRV enums.
Doing thousand of insertion/deletion/lookup, with 2 kind of scenarios:
 - add once, lookup many times.
 - add/delete/loopkup many time.

For small enums, results are similar (delta < noise). Seems relevant
with the previously observed results as most SPIRV enums are small, and
SPIRV-Tools is not doing that many intensive operations on EnumSets.

Performance on large enums (opcode/capabilities) shows an improvement:

+-----------------------------+---------+---------+---------+
| Metric                      |  Old    |   New   | Delta % |
+-----------------------------+---------+---------+---------+
| Execution time              |   27s   |   7s    |  -72%   |
| Instruction count           |  174b   |  129b   |  -25%   |
| Branch count                |   28b   |   33b   |  +17%   |
| Branch miss                 |  490m   |   26m   |  -94%   |
| Cache-misses                |  149k   |   26k   |  -82%   |
+-----------------------------+---------+---------+---------+

Future work
-----------

This was by-design an NFC change to compare apples-to-apples.
The next PR aims to add STL-like iterators to the EnumSet to allow
using it with STL algorithms, and range-based for loops.

Signed-off-by: Nathan Gauër <brioche@google.com>
2023-07-07 10:41:52 -04:00
alan-baker
310a67020a
Validate layouts for PhysicalStorageBuffer pointers (#5291)
* Validate layouts for PhysicalStorageBuffer pointers

Fixes #5282

* These pointers may not orginate from a variable so standard layout
  validation misses them
* Now checks every instructions that results in a physical storage
  buffer pointer
  * May not start from a Block-decorated struct so that part is fudged
    with a valid layout

* formatting
2023-06-23 19:17:55 +00:00
archimedus
04cdb2d344
SPV_KHR_cooperative_matrix (#5286)
* SPV_KHR_cooperative_matrix

* Update DEPS with headers

* Update according to review recommendations

* Bugfix and formatting

* Formatting missed or damaged by VS2022
2023-06-22 18:33:36 -04:00
Jeremy Gebben
daee1e7d34
instrument: Combine descriptor length and init state checking (#5274)
Simplify what we add to user code by moving most of it into a function
that checks both that the descriptor index is in bounds and the
initialization state. Move error logging into this function as
well.

Remove many options to turn off parts of the instrumentation,
because there were far too many permutations to keep working and
test properly.

Combine Buffer and TexBuffer error checking. This requires that VVL
set the length of TexBuffers in the descriptor input state, rather
than relying on the instrumentation code to call OpImageQuerySize.
Since the error log includes the descriptor set and binding numbers
we can use a single OOB error code rather than having 4 per-type
error codes.

Since the error codes are getting renumbered, make them start at 1
rather than 0 so it is easier to determine if the error code was
actually set by the instrumentation.
2023-06-22 09:39:49 -06:00
Juan Ramos
a63ac9f73d
cmake: Use modern Python3 CMake support (#5277)
From the 3.27 release notes:
The FindPythonInterp and FindPythonLibs modules, which have been
deprecated since CMake 3.12, have been removed by policy CMP0148.
Port projects to FindPython3, FindPython2, or FindPython.

closes #4145
2023-06-19 15:02:41 -04:00
Laura Hermanns
951980e5ac
Enable vector constant folding (#4913) (#5272)
- Add test case 6 to UIntVectorInstructionFoldingTest
- Add test case 3 to IntVectorInstructionFoldingTest
2023-06-19 15:01:51 -04:00
Steven Perron
6b9fc79330
Fold negation of integer vectors (#5269) 2023-06-16 10:37:21 -04:00
Jeremy Gebben
d33bea5847
instrument: Fix buffer address length calculations (#5257)
The length of a uvec3 was assumed to be 16 bytes, but it is 12.
Sometimes the stride might be 16 bytes though, which is probably the
source of the confusion.

Redo structure length to be the offset + length of the last member.

Add tests to cover arrays of uvec3s and uvec3 struct members.

Fixes https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/5691
2023-06-14 16:14:46 -06:00
Shahbaz Youssefi
9c66587d14
spirv-diff: Update test expectations (#5264)
Seems to have been left out due to submission race condition
2023-06-09 16:28:30 -04:00
Jim Blandy
ae1843b67c
spirv-diff: Leave undefined ids unpaired. (#5262)
If an id in one module is not defined by any instruction, don't bother
matching it with an id in the other module, as this disturbs the
reported id bound, resulting in spurious differences.

Fixes #5260.
2023-06-09 15:00:46 -04:00
Jim Blandy
93c13345e1
spirv-diff: Properly match SPV_KHR_ray_query types. (#5259)
Fixes #5258.
2023-06-08 10:42:45 -04:00