From 03017a1aba68edd36ceae25b64a32f79b1fc356d Mon Sep 17 00:00:00 2001 From: Younes Beriane Date: Wed, 7 Oct 2026 23:06:23 +0200 Subject: [PATCH] [naga wgsl-in] Reject loads of types that contain atomics (#10458) Co-authored-by: Erich Gubler --- CHANGELOG.md | 1 + naga/src/front/wgsl/lower/mod.rs | 20 ++++++++- naga/tests/naga/wgsl_errors.rs | 76 ++++++++++++++++++++++++++++---- 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a8dbd6d8..71163de55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -209,6 +209,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - Lower `@builtin(instance_index)` in `@any_hit` and `@closest_hit` entry points to SPIR-V's `InstanceId` rather than `InstanceIndex`, which Vulkan only permits in the vertex stage. By @JMS55 in [10154](https://github.com/gfx-rs/wgpu/pull/10154). - Report WGSL type mismatches in `return` statements, function call arguments and composite constructors as WGSL errors naming both types, instead of IR validation errors that could only name the operands by handle index (such as "The \`return\` expression Some([1]) does not match the declared return type Some([1])"). By @emilk in [#9973](https://github.com/gfx-rs/wgpu/pull/9973). - Implement constant evaluation of the `extractBits`, `insertBits`, `faceForward`, `reflect`, and `refract` built-in functions. Evaluates expression at compile time to report issues early like `offset` and `count` selecting bits beyond the width of the data. By @MinerSheep in [#10258](https://github.com/gfx-rs/wgpu/pull/10258). +- Reject WGSL loads of structs and arrays that contain atomics, which previously caused a panic in the HLSL backend. By @drakeo338 in [#10458](https://github.com/gfx-rs/wgpu/issues/10458). #### Validation diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index 04b7e644c..1dbd442f0 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -905,8 +905,24 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> { // Reject direct access to atomic variables that does not go // through a built-in function. - if resolve_inner!(self, pointer).is_atomic_pointer(&self.module.types) { - return Err(Box::new(Error::InvalidAtomicAccess(span))); + if let ir::TypeInner::Pointer { base, .. } = *resolve_inner!(self, pointer) { + fn type_contains_atomic( + ty: Handle, + types: &crate::UniqueArena, + ) -> bool { + match types[ty].inner { + ir::TypeInner::Atomic(_) => true, + ir::TypeInner::Array { base, .. } => type_contains_atomic(base, types), + ir::TypeInner::Struct { ref members, .. } => members + .iter() + .any(|member| type_contains_atomic(member.ty, types)), + _ => false, + } + } + + if type_contains_atomic(base, &self.module.types) { + return Err(Box::new(Error::InvalidAtomicAccess(span))); + } } let load = ir::Expression::Load { pointer }; diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 4ebe5dd5b..8aecf6b48 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -2089,7 +2089,7 @@ fn invalid_functions() { if function_name == "return_pointer" } - check_validation! { + check_error_matches( " struct Atom { a: atomic @@ -2100,14 +2100,72 @@ fn invalid_functions() { fn return_atomic() -> Atom { return atom; } - ": - Err(naga::valid::ValidationError::Function { - name: function_name, - source: naga::valid::FunctionError::NonConstructibleReturnType, - .. - }) - if function_name == "return_atomic" - } + ", + "direct access to atomic variable is not allowed", + ); +} + +/// Regression test for . +/// +/// Loading a value whose type contains an atomic, even nested inside a +/// struct or array, must be rejected by the front end. It used to reach the +/// backends, where the HLSL writer panicked. +#[test] +fn load_type_containing_atomic() { + check( + " + struct Inner { a: atomic } + struct Outer { inner: Inner } + @group(0) @binding(0) var s: Outer; + fn f() { + let x = s; + } + ", + r#"error: direct access to atomic variable is not allowed + ┌─ wgsl:6:21 + │ +6 │ let x = s; + │ ^ atomic variables cannot be accessed directly; use atomic built-in functions + +"#, + ); + + check_error_matches( + " + struct S { a: atomic } + @group(0) @binding(0) var s: S; + fn f() { + _ = s; + } + ", + "direct access to atomic variable is not allowed", + ); + + check_error_matches( + " + struct S { a: array, 2> } + @group(0) @binding(0) var s: S; + fn f() { + let x = s.a; + } + ", + "direct access to atomic variable is not allowed", + ); + + // Accessing the atomic through the built-in functions is still fine. + check_success( + " + struct Inner { a: atomic } + struct Outer { inner: Inner, arr: array, 2> } + @group(0) @binding(0) var s: Outer; + fn f() -> u32 { + atomicStore(&s.inner.a, 1u); + atomicAdd(&s.arr[1], 1u); + let p = &s.inner; + return atomicLoad(&(*p).a); + } + ", + ); } #[test]