Commit Graph

2393 Commits

Author SHA1 Message Date
Sven van Haastregt
35e5f1160e
spirv-val: Accept valid OpSizeOf instructions (#5879)
Ensure valid `OpSizeOf` instructions are not rejected by spirv-val.
Until now, validation of a valid `OpSizeOf` instruction would fail
with the message that the operand "cannot be a type".

Signed-off-by: Sven van Haastregt <sven.vanhaastregt@arm.com>
2024-11-13 10:54:31 -05:00
Nathan Gauër
692529b941
[NFC] Apply small clang-tidy fixes (#5709)
* [NFC] Apply small clang-tidy fixes

- const for passed-by-value has no effect on prototypes.
- transitive includes.
- dead stores
- uses after move

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

* pr-feedback

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

---------

Signed-off-by: Nathan Gauër <brioche@google.com>
2024-11-12 14:45:05 -05:00
Shahbaz Youssefi
02433568af
tools: Accept hex representation as binary input (#5870)
Sometimes when debugging or logging, SPIR-V may be dumped as a stream of
hex values.  There are tools to convert such a stream to binary
(such as [1]) but they create an inconvenient extra step when for
example the disassembly of that hex stream is needed.

[1]: https://www.khronos.org/spir/visualizer/hexdump.html

In this change, the binary reader used by the tools is enhanced to
detect when the binary is actually a hex stream, and parse that instead.
The following formats are accepted, detected based on how the SPIR-V
magic number is output:

=== Words

If the first token of the hex stream is one of 0x07230203, 0x7230203,
x07230203, or x7230203, the hex stream is expected to consist of 32-bit
hex words prefixed with 0x or x.  For example:

    0x7230203, 0x10400, 0x180001, 0x79, 0x0

is parsed as:

    0x07230203 0x00010400 0x00180001 0x00000079 0x00000000

Note that `,` is optional in the stream, but the hex values are expected
to be delimited by either `,` or whitespace.

=== Bytes With Prefix

If the first token of the hex stream is one of 0x07, 0x7, x07, x7, 0x03,
0x3, x03, or x3, the hex stream is expected to consist of 8-bit hex
bytes prefixed with 0x or x.  If the first token has a value of 7, the
stream is big-endian.  Otherwise it's little-endian.  For example:

    0x3, 0x2, 0x23, 0x7, 0x0, 0x4, 0x1, 0x0, 0x1, 0x0, 0x18, 0x0, 0x79, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0

is parsed as:

    0x07230203 0x00010400 0x00180001 0x00000079 0x00000000

Similar to "Words", `,` is optional in the stream, but the hex values
are expected to be delimited by either `,` or whitespace.

=== Bytes Without Prefix

If the first two characters of the hex stream is 07, or 03, the hex
stream is expected to consist of 8-bit hex bytes of 2 characters each.
If the first token is 07, the stream is big-endian.  Otherwise it's
little-endian.  Unlike the other modes, delimiter is optional (which
automatically handles 32-bit word streams), but no 0-padding is done.
For example, all of the following:

    03, 02, 23, 07, 00, 04, 01, 00, 01, 00, 18, 00, 79, 00, 00, 00, 00, 00, 00, 00
    03 02 23 07 00 04 01 00 01 00 18 00 79 00 00 00 00 00 00 00
    03022307 00040100 01001800 79000000 00000000
    07,23,02,03,00,01,04,00,00,18,00,01,00,00,00,79,00,00,00,00
    07230203, 00010400, 00180001, 00000079, 00000000

are parsed as:

    0x07230203 0x00010400 0x00180001 0x00000079 0x00000000
2024-11-04 09:57:37 -05:00
dan sinclair
e5c1498afa
Update proto JSON options call. (#5867)
This CL updates the usage of JsonOptions which is deprecated to the newer
JsonPrintOptions.
2024-10-31 21:08:25 -04:00
Shahbaz Youssefi
5f1e86cb55
tools: Move io utils to cpp (#5869)
In preparation for more code in that file, which would get unwieldy in a
header.
2024-10-28 13:31:54 -04:00
Jeff Bolz
ce92630396
Add validation for SPV_NV_tensor_addressing and SPV_NV_cooperative_matrix2 (#5865) 2024-10-24 13:55:04 -04:00
Nathan Gauër
298055b25c
opt: add StorageImageWriteWithoutFormat to trimm pass (#5860)
* opt: add StorageImageWriteWithoutFormat to trimm pass

---------

Signed-off-by: Nathan Gauër <brioche@google.com>
2024-10-24 06:21:17 -04:00
Jeremy Gebben
ba37b3b513
opt: Remove InstDebugPrintfPass (#5856)
This code was only used by Vulkan-Validation layers and it has
now been replaced by code in that repository.
2024-10-17 15:05:56 -04:00
alan-baker
a832c13331
Fix switch case construct validation (#5852)
* Fix switch case construct validation

Fixes https://crbug.com/tint/372311599

* Stop using block depth in switch validation and instead use the more
  robust structured exit logic from the switch construct
  * This is valid because the function has already handled the
    additional valid cases for case constructs

* formatting
2024-10-16 11:09:21 -04:00
Cassandra Beckley
2ea729062b
opt: Fix array copy propagation of access chain pointers (#5853)
#5827 is broken for situations where a loaded value is stored to a pointer resulting from an OpAccessChain. Traverse access chains to find the original OpVariable to add to the worklist.
2024-10-16 10:51:05 -04:00
alan-baker
42b315c15b
Fix result type check in ValidateImageTexelPointer (#5848)
Fixes https://crbug.com/oss-fuzz/42537207

* Fix check and messages in ValidateImageTexelPointer
2024-10-10 14:10:07 -04:00
Cassandra Beckley
522dfead39
opt: Add support for OpExtInst to capability trim pass (#5836)
The grammar does track required capabilities for extended instruction
set operations, so we just need to look them up.
2024-10-04 00:42:48 -07:00
Cassandra Beckley
48f9f4a99e
opt: Update copy prop arrays to handle InterpolateAt* instructions (#5827)
Adds support for `InterpolateAtCentroid`, `InterpolateAtSample`, and `InterpolateAtOffset` to the copy propagate arrays pass, as well as propagating pointers with the `Input` storage class. Also handles situations where variables should be propagated in an order different than the order that they are declared in the source.
2024-10-02 09:38:32 -04:00
Cassandra Beckley
b1ad37b175
opt: Mark InterpolateAt* argument as live for DCE (#5824)
The GLSL 450 InterpolateAt* instructions should be treated as a load by dead code elimination. This is part of https://github.com/microsoft/DirectXShaderCompiler/issues/3649.
2024-09-26 16:49:36 -04:00
Steven Perron
5b38abc877
Do not distrubute OpSNegate into OpUDiv (#5823)
We cannot apply the negate to an operand of an OpUDiv instead of it
result. This is because the operands of the OpUDiv are interpreted as
unsigned. We stop the optimizer from doing that.

There were no tests for distributing a negate into OpIMul, OpSDiv, and
OpUDiv. Tests are added for all of these.

Fixes #5822
2024-09-26 15:00:56 -04:00
JN Mo
44936c4a9d
Add support for SPV_KHR_compute_shader_derivative (#5817)
* Add support for SPV_KHR_compute_shader_derivative

* Update tests for SPV_KHR_compute_shader_derivatives

---------

Co-authored-by: MagicPoncho <magicponcho@gmail.com>
2024-09-25 09:59:33 -04:00
alan-baker
362ce7c60d
Allow more levels of arrays with component decoration (#5820)
Fixes #5819

* Allow multiple levels of arrays in a variable with component
  decoration
2024-09-24 11:14:28 -04:00
Sven van Haastregt
7ba72f14f2
spirv-val: Disallow stores according to VUID 06924 (#5368)
Ensure that the validator rejects stores to objects of types
`OpTypeImage`, `OpTypeSampler`, `OpTypeSampledImage`,
`OpTypeAccelerationStructureKHR`, and arrays of these types, according
to `VUID-StandaloneSpirv-OpTypeImage-06924`.

Guard the check behind the before_hlsl_legalization option, as
sometimes we may have temporaries or local variables that are expected
to get optimized away.

Fixes #4796


Change-Id: Ie035c01c5f94e7bdfc16b5c6c85705f302b7bda3

Signed-off-by: Sven van Haastregt <sven.vanhaastregt@arm.com>
2024-09-23 08:34:30 -04:00
Ben Ashbaugh
20788a4c5f
add support for SPV_INTEL_global_variable_host_access (#5786) 2024-09-17 12:31:29 -04:00
Steven Perron
d85446fd7e
[OPT] Fix generating debugLocalVariable from debugGlobalVariable (#5803)
The code converting the global to local was generating an extra operand
that was incorrect. Fixed the code, and added a unit test.

Fixes #5776
2024-09-16 10:11:22 -04:00
David Neto
4451f6ab13
Implement to_string(uint32_t) without using the locale (#5805)
Using the locale takes a mutex deep in the C++ library.
Avoid this on hot compilation paths, e.g. in the validator.

Fixed: #5802
2024-09-12 14:30:18 -07:00
cheneym2
37d2fcb485
spirv-opt: fix crash in function declarations (#5796)
* spirv-opt: fix crash in function declarations

Function declarations contain no blocks, so bail before segfaulting
in function optimization passes that operate on blocks.

Fixes #5795

* spirv-opt: add test for optimizing declarations
2024-09-12 07:21:12 -04:00
alan-baker
380275eacd
Do not check structurally unreachable continue target predecessors (#5800)
Fixes #5784

* Rules only apply to structurally reachable blocks
2024-09-11 14:50:30 -04:00
alan-baker
6209efd77c
Update SPIRV-Headers (#5793)
* Fix capability trimming tests to use KHR names for compute derivatives
2024-09-09 18:47:40 -04:00
alan-baker
e9915cea8d
Update sampled image validation (#5789)
Fixes #5781

* Requires all image operands to match except for depth between operand
  and result
2024-09-09 13:09:19 -04:00
Laura Hermanns
b31baff4ee
[opt] Add struct-packing pass and unit test. (#5778)
This pass allows to re-assign offset layout decorations
to tightly pack a struct according to its packing rules.
2024-09-05 15:24:29 -04:00
alan-baker
61d6952e2e
Revert "OpSampledImage extra validation (#5695)" (#5785)
This reverts commit 70ad4dae7d.

Contributes to #5781

SPIR WG 2024-09-04: decision to relax validation requirement in the spec.
2024-09-04 14:59:51 -04:00
Kévin Petit
b21dda0ee7
Validate presence of Stride operand to OpCooperativeMatrix{Load,Store}KHR (#5777)
* Validate Stride operand to OpCooperativeMatrix{Load,Store}KHR

The specification requires the Stride operand for the RowMajorKHR and
ColumnMajorKHR layouts.

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

* Update source/val/validate_memory.cpp

Co-authored-by: alan-baker <alanbaker@google.com>

* add test to exercise memory layout from spec constant and fix validation

Change-Id: I06d7308c4a2b62d26d69e88e03bfa009a7f8fff3

* format fixes

Change-Id: I9cbabec0ed2172dcd228cc385551cb7a5b79df1a

---------

Signed-off-by: Kevin Petit <kevin.petit@arm.com>
Co-authored-by: alan-baker <alanbaker@google.com>
2024-08-29 08:15:31 -04:00
Steven Perron
246daf246b
[OPT] Avoid assert in generatecopy (#5756)
We want to be able to recover when fix storage class is not able to fix
everything, and just leave the spir-v in an invalid state. The pass
should not fail because of that.
2024-07-31 14:11:45 -07:00
alan-baker
8731673a5d
Allow ArrayStride on untyped pointers (#5746) 2024-07-24 13:36:11 -04:00
Steven Perron
81a116002b
[opt] Fix uses of type manager in fix storage class (#5740)
This removes some uses of the type manager. One use could not be
removed. Instead I had to update GenCopy to not use the type manager,
and be able to copy pointers.

Part of #5691
2024-07-24 14:42:00 +02:00
Karol Herbst
e99a5c033e
spirv-link: allow linking functions with different pointer arguments (#5534)
* linker: run dedup earlier

Otherwise `linkings_to_do` might end up with stale IDs.

* linker: allow linking functions with different pointer arguments

Since llvm-17 there are no typed pointers and hte SPIRV-LLVM-Translator
doesn't know the function signature of imported functions.

I'm investigating different ways of solving this problem and adding an
option to work around it inside `spirv-link` is one of those.

The code is almost complete, just I'm having troubles constructing the
bitcast to cast the pointer parameters to the final type.

Closes: https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2153

* test/linker: add tests to test the AllowPtrTypeMismatch feature
2024-07-24 08:38:19 -04:00
Steven Perron
ca373497f1
[opt] Fix pointer stores in DCE (#5739)
When a trying to mark store that use the same address as a load live, we
consider any use of the pointer in the store instruction enough to make
the store live. This is not correct. We should only mark the store as
live if it store to the pointer, and not storing the pointer to another
memory location.

This causes DCE to miss some dead code.
2024-07-24 14:36:26 +02:00
Ben Ashbaugh
a0817526b8
properly handle the load and store cache control operand types (#5664)
* properly handle the load and store cache control operand types

Without handling these operand types, disassembling a SPIR-V module that uses the cache control extension produces an invalid operand type error.

* add a round trip test for SPV_INTEL_cache_controls
2024-07-22 11:18:16 -04:00
Nathan Gauër
2ea4003633
opt: split composite from array flattening (#5733)
* opt: split composite from array flattening

DXC has an option to flatten resource arrays. But when this option
is not used, the resource arrays should be kept as-is.
On the other hand, when a struct contains resources, we MUST flatten is
to be compliant with the Vulkan spec.

Because this pass flattens both types of resources, using a struct of
resources automatically implied flattening arrays.
By adding those 2 new settings, we decide if the pass flattens only one type
of resources, or both.
Note: the flatten_arrays flag only impacts resource arrays.
Arrays of composites containing resources are still flattened.

Since the API is considered stable, I added 2 new functions to create
passes with one flag or the other, and kept the original behavior as-is.

Related to https://github.com/microsoft/DirectXShaderCompiler/issues/6745

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

* add commandline options

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

* clang-format

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

---------

Signed-off-by: Nathan Gauër <brioche@google.com>
2024-07-19 11:48:21 -04:00
alan-baker
3ab0d22608
Support SPV_KHR_untyped_pointers (#5736)
* Support SPV_KHR_untyped_pointers

Covers:
- assembler
- disassembler
- validator

fix copyright

Validate OpTypeUntypedPointerKHR

* Disallow an untyped pointer in a typed pointer
* Validate capability requirements for untyped pointer
* Allow duplicate untyped pointer declarations

Add round trip tests

Validate OpUntypedVariableKHR

Validate untyped access chains

* Add a test for opcodes that generate untyped pointers
* simplify some checks for operands needing types
* validate OpUnypedAccessChainKHR, OpUntypedInBoundsAccessChainKHR,
  OpUntypedPtrAccessChainKHR, OpUntypedInBoundsPtrAccessChainKHR

Unify variable validation

Validate OpCopyMemorySized

* Fix some opcode tests to accound for untyped pointers
* Add validation for OpCopyMemorySized for shaders and untyped pointers
* fix up tests

Validate pointer comparisons and bitcast

* Update more helpers
* Fix entry validation to allow OpUntypedVariableKHR
* Validate OpPtrEqual, OpPtrNotEqual and OpPtrDiff
* Validate OpBitcast

Validate atomics and untyped pointers

Make interface variable validation aware of untyped pointers

* Check OpUntypedVariableKHR in interface validation

More untyped pointer validation

* Validate interfaces more thoroughly
* Validate layouts for untyped pointer uses
* Improve capability checks for vulkan with OpTypeUntypedPointerKHR
* workgroup member explicit layout validation updates

More validation

* validate function arguments and parameters
* handle untyped pointer and variable in more places

Add a friendly assembly name for untyped pointers

Update OpCopyMemory validation and tests

Fix test for token update

Fixes for validation

* Allow typed pointers to contain untyped pointers
* Fix decoration validation
* add untyped pointer as a case for size and alignments

Fix interface validation

* Grabbed the wrong storage class operand for untyped variables
* Add ability to specify assembler options in validation tests

Add passthrough validation for OpUntypedArrayLengthKHR

More validation of untyped pointers

* Validate OpUntypedArrayLengthKHR
* Validate layout for OpLoad, OpStore, and OpUntypedArrayLengthKHR

Validation support for cooperative matrix and untyped pointers

* Allow untyped pointers for cooperative matrix KHR load and store

Updates to match spec

* Remove extra capability references
* Swap untyped variable data type and storage class operands
* update validation of variables

* update deps

---------

Co-authored-by: David Neto <dneto@google.com>
2024-07-17 14:51:37 -04:00
Steven Perron
6248fda376
Handle coop matrix in fix storage class (#5729) 2024-07-17 15:22:32 +02:00
LU-JOHN
7c778973e5
Allow fmt arg to printf to be an array of i8 in non-constant space (#5677)
* In spirv-val allow format arg to printf to be an array of i8 in Generic space

Signed-off-by: Lu, John <john.lu@intel.com>

* Allow more addr spaces for printf format string

Signed-off-by: Lu, John <john.lu@intel.com>

* Update printf format arg testcase

Signed-off-by: Lu, John <john.lu@intel.com>

* Apply clang-format

Signed-off-by: Lu, John <john.lu@intel.com>

* Reorder code for clarity

Signed-off-by: Lu, John <john.lu@intel.com>

* Only allow other addr spaces if extension is seen

Signed-off-by: Lu, John <john.lu@intel.com>

* Add test to check printf format with extension

Signed-off-by: Lu, John <john.lu@intel.com>

* Add extension correctly

Signed-off-by: Lu, John <john.lu@intel.com>

---------

Signed-off-by: Lu, John <john.lu@intel.com>
2024-07-16 09:20:04 -04:00
Sven van Haastregt
6a5fa5f70a
Fix SPRIV -> SPIRV typos (#5735)
Fix all occurrences of "spriv" typos (irrespective of case).

Signed-off-by: Sven van Haastregt <sven.vanhaastregt@arm.com>
2024-07-15 20:10:06 -04:00
Victor Lomuller
3bc9744d0a
Add FPEncoding operand type. (#5726)
This patch adds the optional FPEncoding operand that can be added to OpTypeFloat.
At the moment there is no usable operand, so support is limited to adding the entry.

Co-authored-by: Kévin Petit <kevin.petit@arm.com>
Co-authored-by: David Neto <dneto@google.com>
2024-07-03 13:18:40 -04:00
Steven Perron
ca004da9f9
Add knowledge of cooperative matrices (#5720)
* Add knowledge of cooperative matrices

Some optimizations are not aware of cooperative matrices, and either do
nothing or assert. This commits fixes that up.

* Add int tests, and a handle a couple more cases.

* Add float tests, and a handle a couple more cases.

* Add NV coop matrix as well.
2024-06-26 08:00:29 -04:00
Steven Perron
581279dedd
[OPT] Zero-extend unsigned 16-bit integers when bitcasting (#5714)
The folding rule `BitCastScalarOrVector` was incorrectly handling
bitcasting to unsigned integers smaller than 32-bits. It was simply
copying the entire 32-bit word containing the integer. This conflicts with the
requirement in section 2.2.1 of the SPIR-V spec which states that
unsigned numeric types with a bit width less than 32-bits must have the
high-order bits set to 0.

This change include a refactor of the bit extension code to be able to
test it better, and to use it in multiple files.

Fixes https://github.com/microsoft/DirectXShaderCompiler/issues/6319.
2024-06-19 19:17:05 +02:00
Shahbaz Youssefi
7564e142d6
spirv-dis: Add --nested-indent and --reorder-blocks (#5671)
With --nested-indent, the SPIR-V blocks are nested according to the
structured control flow.  Each OpLabel is nested that much with the
contents of the block nested a little more.  The blocks are separated by
a blank line for better visualization.

With --reorder-blocks, the SPIR-V blocks are reordered according to the
structured control flow.  This is particularly useful with
--nested-indent.

Note that with --nested-indent, the disassembly does not exactly show
the binary as-is, and the instructions may be reordered.
2024-06-17 09:54:18 -04:00
Nathan Gauër
bc28ac7c19
opt: add OpExtInst forward ref fixup pass (#5708)
This pass fixups the opcode used for OpExtInst instructions
to use OpExtInstWithForwardRefsKHR when it contains a forward
reference.
This pass is agnostic to the extension used, hence the validity
of the code depends of the validity of the usage:

If a forward reference is used on a non-semantic extended instruction,
the generated code will remain invalid, but the opcode will change.
What this pass guarantees is valid code won't become invalid.

---------

Signed-off-by: Nathan Gauër <brioche@google.com>
Co-authored-by: Steven Perron <stevenperron@google.com>
2024-06-13 02:09:58 -07:00
Nathan Gauër
65d30c3150
opt: fix Subgroup* trimming (#5706)
PR #5648 added support for the GroupNonUniformPartitionedNV. But there
was an issue: the opcodes are enabled by multiple capabilities, and the
actual operand is what matters.

Added testing coverage and the implementation to correctly trim a few
NonUniform capabilities.

Signed-off-by: Nathan Gauër <brioche@google.com>
2024-06-11 17:13:46 +02:00
Shahbaz Youssefi
c3178da8ea
spirv-dis: add decorations to comments (#5675) 2024-06-10 15:22:25 -04:00
Nathan Gauër
ce46482db7
Add KHR suffix to OpExtInstWithForwardRef opcode. (#5704)
The KHR suffix was missing from the published SPIR-V extension.
This is now fixed, but requires some patches in SPIRV-Tools.

Signed-off-by: Nathan Gauër <brioche@google.com>
2024-06-06 03:17:51 -07:00
alan-baker
7b5691084a
update tests for disassembly changes (#5694)
* update tests for disassembly changes
* use DEPS in wasm build
2024-06-05 13:23:01 -07:00
Nathan Gauër
6a2bdeee75
spirv-val, core: add support for OpExtInstWithForwardRefs (#5698)
* val, core: add support for OpExtInstWithForwardRefs

This commit adds validation and support for
OpExtInstWithForwardRefs. This new instruction will be used
for non-semantic debug info, when forward references are
required.

For now, this commit only fixes the code to handle this new instruction,
and adds validation rules. But it does not add the pass to generate/fix
the OpExtInst instruction when forward references are in use.
Such pass would be useful for DXC or other tools, but I wanted to land
validation rules first.

This commit also bumps SPIRV-Headers to get this new opcode.

---------

Signed-off-by: Nathan Gauër <brioche@google.com>
2024-06-04 16:18:06 +02:00
alan-baker
70ad4dae7d
OpSampledImage extra validation (#5695)
* Validate that the type of Image operand matches the result type's
  Image operand
2024-06-03 10:43:26 -04:00