[naga wgsl-in] Reject loads of types that contain atomics (#10458)

Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
This commit is contained in:
Younes BerianeandErich Gubler authored and GitHub committed 2026-10-07 21:06:23 +00:00
1 parent d75cf081bd
commit 03017a1aba
3 files changed
+86 -11

No files matched your search

+1
View File
@@ -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
+18 -2
View File
@@ -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<ir::Type>,
types: &crate::UniqueArena<ir::Type>,
) -> 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 };
+67 -9
View File
@@ -2089,7 +2089,7 @@ fn invalid_functions() {
if function_name == "return_pointer"
}
check_validation! {
check_error_matches(
"
struct Atom {
a: atomic<u32>
@@ -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 <https://github.com/gfx-rs/wgpu/issues/10046>.
///
/// 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<u32> }
struct Outer { inner: Inner }
@group(0) @binding(0) var<storage, read_write> 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<u32> }
@group(0) @binding(0) var<storage, read_write> s: S;
fn f() {
_ = s;
}
",
"direct access to atomic variable is not allowed",
);
check_error_matches(
"
struct S { a: array<atomic<u32>, 2> }
@group(0) @binding(0) var<storage, read_write> 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<u32> }
struct Outer { inner: Inner, arr: array<atomic<u32>, 2> }
@group(0) @binding(0) var<storage, read_write> s: Outer;
fn f() -> u32 {
atomicStore(&s.inner.a, 1u);
atomicAdd(&s.arr[1], 1u);
let p = &s.inner;
return atomicLoad(&(*p).a);
}
",
);
}
#[test]