Commit Graph

671 Commits

Author SHA1 Message Date
Erich Gubler
4200136e2c refactor(naga): invert coordinate operand typeck to is_none_or 2025-02-26 22:58:50 -05:00
Erich Gubler
c626d4f819 build: update CORE_MSRV 1.80.1 → 1.82.0 2025-02-26 22:58:50 -05:00
Erich Gubler
8774ab53d5 chore: remove std::mem::* imports now unnecessary with CORE_MSRV
`std::mem::{size,align}_of{,_val}` was added to `std::prelude` in Rust
1.80; see
[`rust`#123168](https://github.com/rust-lang/rust/pull/123168/).
2025-02-26 22:58:50 -05:00
Erich Gubler
18951ea3d2 build: update CORE_MSRV 1.76.0 → 1.80.1 2025-02-26 22:58:50 -05:00
Junjun Dong
93f64dc847 perf: wgsl::…::Lowerer::lower: don't clone diagnostic_filters arena (#7176)
Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
2025-02-25 20:52:03 +00:00
Jamie Nicol
b7d1f4c6cf [naga spv-out] Ensure loops generated by SPIRV backend are bounded (#7080)
If it is undefined behaviour for loops to be infinite, then, when
encountering an infinite loop, downstream compilers are able to make
certain optimizations that may be unsafe. For example, omitting bounds
checks. To prevent this, we must ensure that any loops emitted by our
backends are provably bounded. We already do this for both the MSL and
HLSL backends. This patch makes us do so for SPIRV as well.

The construct used is the same as for HLSL and MSL backends: use a
vec2<u32> to emulate a 64-bit counter, which is incremented every
iteration and breaks after 2^64 iterations.

While the implementation is fairly verbose for the SPIRV backend, the
logic is simple enough. The one point of note is that SPIRV requires
`OpVariable` instructions with a `Function` storage class to be
located at the start of the first block of the function. We therefore
remember the IDs generated for each loop counter variable in a
function whilst generating the function body's code. The instructions
to declare these variables are then emitted in `Function::to_words()`
prior to emitting the function's body.

As this may negatively impact shader performance, this workaround can
be disabled using the same mechanism as for other backends: eg calling
Device::create_shader_module_trusted() and setting the
ShaderRuntimeChecks::force_loop_bounding flag to false.
2025-02-25 15:23:44 +01:00
Jim Blandy
e0f01854a8 [naga wgsl-in] Introduce AbstractRule enum.
Introduce the `AbstractRule` enum to control the behavior of
`Lowerer::type_and_init`. This is slightly clearer than
`allow_abstract: bool`, but more wordy.
2025-02-25 09:25:25 +00:00
Jim Blandy
ab70237126 [naga] Introduce TypeInner::is_abstract utility function.
Define `TypeInner::is_abstract`, and let it take a type arena so that
it can properly handle abstract arrays. Use this in compaction and in
the WGSL front end's conversion code to recognize abstract types.
2025-02-25 09:25:25 +00:00
Jamie Nicol
9f949459f7 [naga wgsl-in] Add additional test cases regarding abstract-typed global consts 2025-02-25 09:25:25 +00:00
Jamie Nicol
dd2d53814d [naga wgsl-in] Do not eagerly concretize global const declarations of abstract types
Instead allow the const to be converted and each time it is const
evaluated as part of another expression. This allows an abstract const
to be used as a different type depending on the context.

A consequence of this is that abstract types may now find their way to
the validation stage, which we don't want. We therefore additionally
now ensure that the compact pass removes global constants of abstract
types. This will have no *functional* effect on shaders generated by
the backends, as the expressions belonging to the abstract consts in
the IR will not actually be used, as any usage in the input shader
will have been const-evaluated away. Certain unused const declarations
will now be removed, however, as can be seen by the effect on the
snapshot outputs.
2025-02-25 09:25:25 +00:00
Jamie Nicol
da90d7aaa6 [naga wgsl-in] Ensure ConstantEvaluator::cast_array() handles Constant expressions
It must call ConstantEvaluator::check_and_get() to possibly retrieve
the constant expression from a separate arena, like is currently done
when evaluating `As` expressions for non-array casts.
2025-02-25 09:25:25 +00:00
Connor Fitzgerald
dc97a61ee7 Configure spv-in settings in toml file (#7215) 2025-02-25 05:23:03 +00:00
Connor Fitzgerald
8fb09a5ad6 Move REPO_MSRV to 1.85 (#7218) 2025-02-24 23:39:34 -05:00
Kent Slaney
6be5558865 [naga] allow trailing commas in template lists (#7142)
Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
2025-02-25 03:02:55 +00:00
Connor Fitzgerald
2c42a1844f Move wgsl snapshots from tests/naga/in to tests/naga/in/wgsl (#7214) 2025-02-24 21:33:06 -05:00
Alexandre Mommers
92a1702c3c [naga wgsl-in] Add support for unsigned types when calling textureLoad with the level parameter. (#7058) 2025-02-24 21:13:06 +00:00
Samson
2f255edc60 [naga] Use const ctx instead of global ctx for type resolution (#6935)
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-02-24 16:24:37 +01:00
dependabot[bot]
853d90e00a chore(deps): bump the patch-updates group with 14 updates (#7209)
Bumps the patch-updates group with 14 updates:

| Package | From | To |
| --- | --- | --- |
| [anyhow](https://github.com/dtolnay/anyhow) | `1.0.95` | `1.0.96` |
| [libc](https://github.com/rust-lang/libc) | `0.2.169` | `0.2.170` |
| [log](https://github.com/rust-lang/log) | `0.4.25` | `0.4.26` |
| [serde_json](https://github.com/serde-rs/json) | `1.0.138` | `1.0.139` |
| [serde](https://github.com/serde-rs/serde) | `1.0.217` | `1.0.218` |
| [unicode-ident](https://github.com/dtolnay/unicode-ident) | `1.0.16` | `1.0.17` |
| [cc](https://github.com/rust-lang/cc-rs) | `1.2.14` | `1.2.15` |
| [clap](https://github.com/clap-rs/clap) | `4.5.29` | `4.5.30` |
| [clap_builder](https://github.com/clap-rs/clap) | `4.5.29` | `4.5.30` |
| [either](https://github.com/rayon-rs/either) | `1.13.0` | `1.14.0` |
| [serde_derive](https://github.com/serde-rs/serde) | `1.0.217` | `1.0.218` |
| [target-triple](https://github.com/dtolnay/target-triple) | `0.1.3` | `0.1.4` |
| [uuid](https://github.com/uuid-rs/uuid) | `1.13.1` | `1.14.0` |
| [winnow](https://github.com/winnow-rs/winnow) | `0.7.2` | `0.7.3` |


Updates `anyhow` from 1.0.95 to 1.0.96
- [Release notes](https://github.com/dtolnay/anyhow/releases)
- [Commits](https://github.com/dtolnay/anyhow/compare/1.0.95...1.0.96)

Updates `libc` from 0.2.169 to 0.2.170
- [Release notes](https://github.com/rust-lang/libc/releases)
- [Changelog](https://github.com/rust-lang/libc/blob/0.2.170/CHANGELOG.md)
- [Commits](https://github.com/rust-lang/libc/compare/0.2.169...0.2.170)

Updates `log` from 0.4.25 to 0.4.26
- [Release notes](https://github.com/rust-lang/log/releases)
- [Changelog](https://github.com/rust-lang/log/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rust-lang/log/compare/0.4.25...0.4.26)

Updates `serde_json` from 1.0.138 to 1.0.139
- [Release notes](https://github.com/serde-rs/json/releases)
- [Commits](https://github.com/serde-rs/json/compare/v1.0.138...v1.0.139)

Updates `serde` from 1.0.217 to 1.0.218
- [Release notes](https://github.com/serde-rs/serde/releases)
- [Commits](https://github.com/serde-rs/serde/compare/v1.0.217...v1.0.218)

Updates `unicode-ident` from 1.0.16 to 1.0.17
- [Release notes](https://github.com/dtolnay/unicode-ident/releases)
- [Commits](https://github.com/dtolnay/unicode-ident/compare/1.0.16...1.0.17)

Updates `cc` from 1.2.14 to 1.2.15
- [Release notes](https://github.com/rust-lang/cc-rs/releases)
- [Changelog](https://github.com/rust-lang/cc-rs/blob/main/CHANGELOG.md)
- [Commits](https://github.com/rust-lang/cc-rs/compare/cc-v1.2.14...cc-v1.2.15)

Updates `clap` from 4.5.29 to 4.5.30
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/clap_complete-v4.5.29...clap_complete-v4.5.30)

Updates `clap_builder` from 4.5.29 to 4.5.30
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/v4.5.29...v4.5.30)

Updates `either` from 1.13.0 to 1.14.0
- [Commits](https://github.com/rayon-rs/either/compare/1.13.0...1.14.0)

Updates `serde_derive` from 1.0.217 to 1.0.218
- [Release notes](https://github.com/serde-rs/serde/releases)
- [Commits](https://github.com/serde-rs/serde/compare/v1.0.217...v1.0.218)

Updates `target-triple` from 0.1.3 to 0.1.4
- [Release notes](https://github.com/dtolnay/target-triple/releases)
- [Commits](https://github.com/dtolnay/target-triple/compare/0.1.3...0.1.4)

Updates `uuid` from 1.13.1 to 1.14.0
- [Release notes](https://github.com/uuid-rs/uuid/releases)
- [Commits](https://github.com/uuid-rs/uuid/compare/1.13.1...v1.14.0)

Updates `winnow` from 0.7.2 to 0.7.3
- [Changelog](https://github.com/winnow-rs/winnow/blob/main/CHANGELOG.md)
- [Commits](https://github.com/winnow-rs/winnow/compare/v0.7.2...v0.7.3)

---
updated-dependencies:
- dependency-name: anyhow
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: libc
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: log
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: serde_json
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: serde
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: unicode-ident
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: cc
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: clap
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: clap_builder
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: either
  dependency-type: indirect
  update-type: version-update:semver-minor
  dependency-group: patch-updates
- dependency-name: serde_derive
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: target-triple
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: uuid
  dependency-type: indirect
  update-type: version-update:semver-minor
  dependency-group: patch-updates
- dependency-name: winnow
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-02-23 22:06:20 -05:00
Connor Fitzgerald
a8af685fd5 Convert Snapshot Configuration to TOML (#7160)
* Prepare for Toml

* Convert Configurations Entirely To TOML
2025-02-21 19:55:01 -05:00
Connor Fitzgerald
473dd2fdd9 Refactor Snapshots to Be Fully Configuration Based (#7153)
Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
2025-02-21 18:22:54 -05:00
Connor Fitzgerald
4d24df2b3b Fix enumeration of wgpu example shaders in example_wgsl test (#7191)
Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
2025-02-21 19:31:30 +00:00
Connor Fitzgerald
1ec34839ee Improve Validation Errors with Naga Validator (#7185) 2025-02-21 10:57:59 -05:00
Jim Blandy
5dad2c4cd4 [naga] Apply module compaction to GLSL tests. (#7186) 2025-02-19 20:59:06 -05:00
Connor Fitzgerald
e590555a8c test: use spirv-as instead of pre-assembled SPIR-V (#7157) 2025-02-17 14:48:09 -05:00
Samson
7240c18554 fix(naga): Forbid negative indexing in const expressions (#7155)
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-02-17 14:38:00 -05:00
Jamie Nicol
d625d083c3 [naga wgsl-in] Implement any() and all() during const evaluation (#7166) 2025-02-17 14:13:49 -05:00
Junjun Dong
98649a79a0 feat: update naga::back::spv::Function::to_words (#7156) 2025-02-16 21:43:24 -05:00
Connor Fitzgerald
d8833d0798 Use Update After Bind Descriptors for Bind Groups With Binding Arrays (#6815)
* Use Update After Bind Descriptors for Bind Groups With Binding Arrays

Update After Bind

x

* Comments

* Fix URL
2025-02-15 12:02:27 -05:00
CrazyboyQCD
a45e2db43a [Naga] replace unicode-xid with unicode-ident (#7135) 2025-02-14 20:58:17 -05:00
Jim Blandy
f90f19c7e8 [naga spv-out] Factor out wrapped divide/module generation.
Move the code to generate the definition of an overflow-safe
divide/modulo SPIR-V function into its own Rust function, to reduce
indentation and clarify influences. This commit isn't intended to
cause any change in behavior.
2025-02-14 15:17:40 -08:00
Jamie Nicol
cb9666c6a7 [naga hlsl-out] Document the need for wrapper functions for integer division, modulo, abs(), and unary negation
Explain we need the wrapper functions not just to avoid undefined
behaviour, but also to ensure we adhere to the WGSL spec. Additionally
link to issue #7109 in cases where our workaround needs follow-up work
for non-32-bit integer types.
2025-02-14 15:17:40 -08:00
Jamie Nicol
b3b40c03d4 [naga msl-out] Document the need for wrapper functions for integer division, modulo, abs(), and unary negation
Explain we need the wrapper functions not just to avoid undefined
behaviour (or unspecified in the case of division), but also to ensure
we adhere to the WGSL spec.
2025-02-14 15:17:40 -08:00
Jim Blandy
96de35aac1 [hlsl-out] Clarify comment on HLSL floating-point divide by zero.
Replace the link to the resolved WGSL spec issue about floating-point
division by zero (gpuweb/gpuweb#2798) with links to the Direct3D 11
functional specification (which Direct3D 12 inherits) and the DXIL
specification, explaining that HLSL does what WGSL wants here.
2025-02-14 15:17:40 -08:00
Jim Blandy
bafeee6680 [hlsl-out] Clean up repetition in write_mapped_math_functions.
Since every `match` arm ends up looking up the type of the operation's
first argument, just do that once. This avoids a repetitive lookup for
`Abs`.
2025-02-14 15:17:40 -08:00
Jim Blandy
a25098f5ec [naga hlsl-out] Use Scalar associated constants in patterns. 2025-02-14 15:17:40 -08:00
Jamie Nicol
6cf4b710b8 [naga spv-out] Avoid undefined behaviour for integer division and modulo
Integer division or modulo is undefined behaviour in SPIR-V when the
divisor is zero, or when the dividend is the most negative number
representable by the result type and the divisor is negative one.

This patch makes us avoid this undefined behaviour and instead ensures
we adhere to the WGSL spec in these cases: for divisions the
expression evaluates to the value of the dividend, and for modulos the
expression evaluates to zero.

Similarily to how we handle these cases for the MSL and HLSL backends,
prior to emitting each function we emit code for any helper functions
required by that function's expressions. In this case that is helper
functions for integer division and modulo. Then, when emitting the
actual function's body, if we encounter an expression which needs
wrapped we instead emit a function call to the helper.
2025-02-14 15:17:40 -08:00
Jamie Nicol
b6186ba332 [naga hlsl-out] Avoid undefined behaviour for integer division, modulo, negation, and abs
Emit helper functions for MathFunction::Abs and UnaryOperator::Negate
with a signed integer scalar or vector operand. And for
BinaryOperator::Divide and BinaryOperator::Modulo with signed or
unsigned integer scalar or vector operands.

Abs and Negate are written to avoid signed integer overflow when the
operand equals INT_MIN. This is achieved by bitcasting the value to
unsigned, using the negation operator, then bitcasting the result back
to signed. As HLSL's bitcast functions asint() and asuint() only work
for 32-bit types, we only use this workaround in such cases.

Division and Modulo avoid undefined behaviour for INT_MIN / -1 and
divide-by-zero by using 1 for the divisor instead. Additionally we
avoid undefined behaviour when using the modulo operator on operands
of mixed signedness by using the equation from the WGSL spec, using
division, subtraction and multiplication, rather than HLSL's modulus
operator.
2025-02-14 15:17:40 -08:00
Jamie Nicol
88f6e9b8f8 [naga hlsl-out] Avoid undefined behaviour for signed integer addition, subtraction, and multiplication
Though not explicitly specified one way or the other, we have been
informed by DirectX engineers that signed integer overflow may be
undefined behaviour in some cases. To avoid this, we therefore bitcast
signed operands to unsigned prior to performing addition, subtraction,
or multiplication, then bitcast the result back to signed. As signed
types are represented as two's complement, this gives the correct
result whilst avoid any potential undefined behaviour.

Unfortunately HLSL's bitcast functions asint() and asuint() only work
for the 32-bit int and uint types. We therefore only apply this
workaround for 32-bit signed arithmetic. Support for other bit widths
could be added in the future, but extra care must be taken when
converting from unsigned to signed to avoid undefined or
implemented-defined behaviour.
2025-02-14 15:17:40 -08:00
Jamie Nicol
30363ef493 [naga hlsl-out] Wrap int literals in constructor-style cast
There is no literal suffix in HLSL for the integer type. We can,
however, wrap integer literals in a constructor style cast. This
avoids ambiguity when passing literals to overloaded functions, which
we'll make use of in the subsequent patch in this PR.
2025-02-14 15:17:40 -08:00
Jamie Nicol
8807c83363 [naga msl-out] Avoid undefined behaviour for integer division, modulo, negation, and abs
Adds infrastructure to the MSL backend for emitting helper functions,
based upon the existing HLSL backend equivalent. Emit helper functions
for MathFunction::Abs and UnaryOperator::Negate with a signed integer
scalar or vector operand. And for BinaryOperator::Divide and
BinaryOperator::Modulo with signed or unsigned integer scalar or
vector operands.

Abs and Negate are written to avoid signed integer overflow when the
operand equals INT_MIN. This is achieved by bitcasting the value to
unsigned and using the negation operator, then bitcasting the result
back to signed.

Division and Modulo avoid undefined bevaviour for INT_MIN / -1 and
divide-by-zero by using 1 for the divisor instead. Additionally we
avoid undefined behaviour when using the modulo operator when either
or both operands are negative by using the equation from the WGSL
spec, using division, subtraction and multiplication, rather than
MSL's modulus operator.

Lastly, as the usage of the wrapper function for unary integer
negation makes the negation_avoids_prefix_decrement() testcase less
interesting, we extend it to additionally test negating floats.
2025-02-14 15:17:40 -08:00
Jamie Nicol
4bb09e1d3c [naga wgsl-in] Ensure constant evaluation correctly handles Composes of vector ZeroValues (#7138) 2025-02-14 13:18:12 -05:00
Bruce Mitchener
bae0e70e8d Fix clippy::unneeded_struct_pattern lints (#7136) 2025-02-14 01:32:21 -05:00
Vecvec
5af9e30009 Fix HLSL single scalar loads (#7104) 2025-02-12 18:05:32 +00:00
Jim Blandy
0dd6a1cfe2 [naga] Retain unnamed overrides' types during compaction.
Ensure that unnamed overrides' types are retained, even when the
overrides are used only by global expressions. Add a test case.

Fixes #7072.
2025-02-12 09:19:56 -08:00
Jim Blandy
c934184c9e [naga] Retain unnamed constants' types during compaction.
Ensure that unnamed constants' types are retained, even when the
constants are used only by global expressions. Add a test case.

The old code marked all named constants as used; then processed
function bodies; and then marked the types of all used constants as
used. This ensured that, as long as a constant was used by a function,
its type would be retained, but didn't address the case where a
constant is used only by a global expression.
2025-02-12 09:19:56 -08:00
Dzmitry Malyshau
189c97c88a Implement ray query candidate intersection generation and confirmation (#7047) 2025-02-12 16:23:29 +01:00
Jamie Nicol
b477c66a9d [naga wgsl-in] Do not attempt automatic type conversion for non-abstract types
Attempting to convert a non-abstract type will always fail, which can
result in unrelated errors being misreported as type conversion
errors. For example, in #7035 we made it so that abstract types can be
used as return values. This changed the final testcase in the
invalid_functions() test to fail due to failing to convert a u32 to an
atomic<u32>, rather than with a NonConstructibleReturnType error.

This patch makes it so that we do not attempt to convert non-abstract
types in the first place. This avoids the AutoConversion error for
that testcase, meaning the NonConstructibleReturnType is once again
reported.

Various callsites of try_automatic_conversions() have been updated to
ensure they still return an InitializationTypeMismatch error if the
types are mismatched, even if try_automatic_conversions() succeeds (eg
when conversion was not attempted due to the type being concrete). To
reduce code duplication these callsites were all adapted to use the
existing type_and_init() helper function.

Lastly, a couple of tests that expected to result in a AutoConversion
error have been adjusted to ensure this still occurs, by ensuring the
type used is abstract.
2025-02-12 15:10:57 +00:00
Jamie Nicol
a546e60d6d [naga wgsl-in] Ensure correct array stride when converting types during const evaluation (#7112)
When converting the underlying scalar type of an array during const
evaluation, we currently use the resulting base type's size as the array
stride for the resulting type. For certain types, this may not match the
required alignment and will therefore result in a validation error.

For example, `array<vec3<f32>, N>` should have a stride of 16. But if
declared with an abstract initializer, eg `array(vec3(0.0))` we will
incorrectly determine the stride to be 12.

To solve this, we use the proc::Layouter struct to determine the
required array stride during const evaluation. To avoid repeating
layouting work, we reuse the Lowerer's layouter, passing it through the
various *Contexts through to the ConstantEvaluator.
2025-02-12 15:46:13 +01:00
Shaye Garg
d7a42933a5 [naga] Error on duplicate fields in structs (#7088)
* error in wgsl-in

* changelog

* add test
2025-02-12 03:27:55 +00:00
Connor Fitzgerald
aec14e2f63 Fix naga-fuzz Build on Windows (#7107) 2025-02-11 09:19:43 +01:00