Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ By @cwfitzgerald in [#8388](https://github.com/gfx-rs/wgpu/pull/8388).
- In the MSL backend, `subgroupBallot` now keeps lanes 32-63, which were lost on GPUs with 64-wide SIMD groups (AMD GPUs on macOS). By @ErkoRisthein in [#10797](https://github.com/gfx-rs/wgpu/pull/10797).
- Report a WGSL error instead of panicking when the condition of a `const_assert` is a composite value, such as `const_assert(vec2i());`. By @MilosLord in [#10897](https://github.com/gfx-rs/wgpu/pull/10897).
- Fix WGSL `continuing` blocks falsely rejecting declarations that shadow variables from the loop body (e.g., `loop { let x = a; continuing { let x = b; } }`) as redefinitions. By @ekarad1um in [#10980](https://github.com/gfx-rs/wgpu/pull/10980).
- Stop emitting `ArrayStride`, `Offset` and `MatrixStride` decorations in SPIR-V for arrays and structs that contain a `bool`. Such types are never host-shareable, and the decorations described a one-byte `bool`. Arm's proprietary Mali driver (seen with `r20p0` on a Mali-G52) honored them, so an `array<bool, N>` element read as `a[i] || a[i + 1]`. By @matthargett in [#XXXXX](https://github.com/gfx-rs/wgpu/pull/XXXXX).

#### Validation

Expand Down
67 changes: 57 additions & 10 deletions naga/src/back/spv/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2323,7 +2323,10 @@ impl Writer {
let id = self.id_gen.next();
let instruction = match ty.inner {
crate::TypeInner::Array { base, size, stride } => {
self.decorate(id, Decoration::ArrayStride, &[stride]);
// See `contains_bool` for why these arrays get no stride.
if !contains_bool(&module.types, base) {
self.decorate(id, Decoration::ArrayStride, &[stride]);
}

let type_id = self.get_handle_type_id(base);
match size.resolve(module.to_ctx())? {
Expand Down Expand Up @@ -2354,6 +2357,7 @@ impl Writer {
} => {
let mut has_runtime_array = false;
let mut member_ids = Vec::with_capacity(members.len());
let explicit_layout = !contains_bool(&module.types, handle);
for (index, member) in members.iter().enumerate() {
let member_ty = &module.types[member.ty];
match member_ty.inner {
Expand All @@ -2366,7 +2370,13 @@ impl Writer {
}
_ => (),
}
self.decorate_struct_member(id, index, member, &module.types)?;
self.decorate_struct_member(
id,
index,
member,
&module.types,
explicit_layout,
)?;
let member_id = self.get_handle_type_id(member.ty);
member_ids.push(member_id);
}
Expand Down Expand Up @@ -2613,6 +2623,7 @@ impl Writer {
next_index as usize,
member,
&module.types,
true,
)?;
self.get_handle_type_id(member.ty)
}
Expand Down Expand Up @@ -3663,7 +3674,13 @@ impl Writer {
binding: None,
offset: 0,
};
self.decorate_struct_member(wrapper_type_id, 0, &member, &ir_module.types)?;
self.decorate_struct_member(
wrapper_type_id,
0,
&member,
&ir_module.types,
true,
)?;

Instruction::type_struct(wrapper_type_id, &[inner_type_id])
.to_words(&mut self.logical_layout.declarations);
Expand Down Expand Up @@ -3739,23 +3756,29 @@ impl Writer {
index: usize,
member: &crate::StructMember,
arena: &UniqueArena<crate::Type>,
explicit_layout: bool,
) -> Result<(), Error> {
use spirv::Decoration;

self.annotations.push(Instruction::member_decorate(
struct_id,
index as u32,
Decoration::Offset,
&[member.offset],
));

if self.flags.contains(WriterFlags::DEBUG) {
if let Some(ref name) = member.name {
self.debugs
.push(Instruction::member_name(struct_id, index as u32, name));
}
}

// See `contains_bool` for which structs get no layout decorations.
if !explicit_layout {
return Ok(());
}

self.annotations.push(Instruction::member_decorate(
struct_id,
index as u32,
Decoration::Offset,
&[member.offset],
));

// Matrices and (potentially nested) arrays of matrices both require decorations,
// so "see through" any arrays to determine if they're needed.
let mut member_array_subty_inner = &arena[member.ty].inner;
Expand Down Expand Up @@ -4135,6 +4158,30 @@ impl Writer {
}
}

/// Returns `true` if `ty` is, or contains, a `bool`.
///
/// Such a type is not host-shareable, so it can never be the type of a buffer
/// and never needs the explicit layout decorations (`ArrayStride`, `Offset`,
/// `MatrixStride`) that SPIR-V requires there. It must not have them either:
/// they come from Naga's own layout, in which a `bool` takes one byte, while
/// SPIR-V gives `OpTypeBool` no size. Arm's proprietary Mali driver (seen with
/// `r20p0` on a Mali-G52) lays variables out by these decorations but reads
/// and writes a `bool` as a wider value, so the elements of an
/// `array<bool, N>` with `ArrayStride 1` overlap and `a[i]` reads as
/// `a[i] || a[i + 1]`.
fn contains_bool(types: &UniqueArena<crate::Type>, ty: Handle<crate::Type>) -> bool {
match types[ty].inner {
crate::TypeInner::Scalar(scalar) | crate::TypeInner::Vector { scalar, .. } => {
scalar.kind == crate::ScalarKind::Bool
}
crate::TypeInner::Array { base, .. } => contains_bool(types, base),
crate::TypeInner::Struct { ref members, .. } => {
members.iter().any(|member| contains_bool(types, member.ty))
Comment on lines +4178 to +4179

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Cache boolean containment per type instead of expanding shared subtypes

For shaders with nested structs that reuse the same member type, this recursion performs exponential work even though the type arena is small. For example, starting with S0 containing a u32 and defining each Sn with two S(n-1) members makes checking S27 visit hundreds of millions of nodes; its 512 MiB declared size is still below MAX_TYPE_SIZE. write_logical_layout repeats this traversal for every declared type, so a small, valid shader can stall SPIR-V compilation and monopolize the process handling untrusted shaders. Compute containment once per type in arena order, or memoize results by type handle.

AGENTS.md reference: AGENTS.md:L17-L17

Useful? React with 👍 / 👎.

}
_ => false,
}
}

#[test]
fn test_write_physical_layout() {
let mut writer = Writer::new(&Options::default()).unwrap();
Expand Down
1 change: 0 additions & 1 deletion naga/tests/out/spv/wgsl-access.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ OpDecorate %33 ArrayStride 4
OpDecorate %35 ArrayStride 16
OpMemberDecorate %37 0 Offset 0
OpDecorate %39 ArrayStride 4
OpDecorate %43 ArrayStride 1
OpMemberDecorate %45 0 Offset 0
OpMemberDecorate %46 0 Offset 0
OpMemberDecorate %47 0 Offset 0
Expand Down
15 changes: 0 additions & 15 deletions naga/tests/out/spv/wgsl-aliased-ray-query.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,6 @@ OpMemberDecorate %8 2 Offset 8
OpMemberDecorate %8 3 Offset 12
OpMemberDecorate %8 4 Offset 16
OpMemberDecorate %8 5 Offset 32
OpMemberDecorate %12 0 Offset 0
OpMemberDecorate %12 1 Offset 4
OpMemberDecorate %12 2 Offset 8
OpMemberDecorate %12 3 Offset 12
OpMemberDecorate %12 4 Offset 16
OpMemberDecorate %12 5 Offset 20
OpMemberDecorate %12 6 Offset 24
OpMemberDecorate %12 7 Offset 28
OpMemberDecorate %12 8 Offset 36
OpMemberDecorate %12 9 Offset 48
OpMemberDecorate %12 9 ColMajor
OpMemberDecorate %12 9 MatrixStride 16
OpMemberDecorate %12 10 Offset 112
OpMemberDecorate %12 10 ColMajor
OpMemberDecorate %12 10 MatrixStride 16
OpDecorate %13 DescriptorSet 0
OpDecorate %13 Binding 0
%2 = OpTypeVoid
Expand Down
4 changes: 0 additions & 4 deletions naga/tests/out/spv/wgsl-atomicCompareExchange-int64.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ OpExecutionMode %19 LocalSize 1 1 1
OpExecutionMode %103 LocalSize 1 1 1
OpDecorate %5 ArrayStride 8
OpDecorate %8 ArrayStride 8
OpMemberDecorate %10 0 Offset 0
OpMemberDecorate %10 1 Offset 8
OpMemberDecorate %11 0 Offset 0
OpMemberDecorate %11 1 Offset 8
OpDecorate %12 DescriptorSet 0
OpDecorate %12 Binding 0
OpDecorate %13 Block
Expand Down
4 changes: 0 additions & 4 deletions naga/tests/out/spv/wgsl-atomicCompareExchange.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ OpExecutionMode %19 LocalSize 1 1 1
OpExecutionMode %104 LocalSize 1 1 1
OpDecorate %5 ArrayStride 4
OpDecorate %7 ArrayStride 4
OpMemberDecorate %10 0 Offset 0
OpMemberDecorate %10 1 Offset 4
OpMemberDecorate %11 0 Offset 0
OpMemberDecorate %11 1 Offset 4
OpDecorate %12 DescriptorSet 0
OpDecorate %12 Binding 0
OpDecorate %13 Block
Expand Down
4 changes: 0 additions & 4 deletions naga/tests/out/spv/wgsl-atomicOps-int64.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ OpExecutionMode %32 LocalSize 2 1 1
OpDecorate %5 ArrayStride 8
OpMemberDecorate %8 0 Offset 0
OpMemberDecorate %8 1 Offset 8
OpMemberDecorate %11 0 Offset 0
OpMemberDecorate %11 1 Offset 8
OpMemberDecorate %12 0 Offset 0
OpMemberDecorate %12 1 Offset 8
OpDecorate %13 DescriptorSet 0
OpDecorate %13 Binding 0
OpDecorate %14 Block
Expand Down
4 changes: 0 additions & 4 deletions naga/tests/out/spv/wgsl-atomicOps.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ OpExecutionMode %31 LocalSize 2 1 1
OpDecorate %5 ArrayStride 4
OpMemberDecorate %7 0 Offset 0
OpMemberDecorate %7 1 Offset 4
OpMemberDecorate %10 0 Offset 0
OpMemberDecorate %10 1 Offset 4
OpMemberDecorate %11 0 Offset 0
OpMemberDecorate %11 1 Offset 4
OpDecorate %12 DescriptorSet 0
OpDecorate %12 Binding 0
OpDecorate %13 Block
Expand Down
10 changes: 0 additions & 10 deletions naga/tests/out/spv/wgsl-mesh-shader.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,10 @@ OpDecorate %362 Location 0
OpDecorate %365 BuiltIn PrimitiveTriangleIndicesEXT
OpDecorate %368 Location 1
OpDecorate %368 PerPrimitiveEXT
OpMemberDecorate %6 0 Offset 0
OpMemberDecorate %6 1 Offset 16
OpMemberDecorate %7 0 Offset 0
OpMemberDecorate %7 1 Offset 16
OpMemberDecorate %10 0 Offset 0
OpMemberDecorate %10 1 Offset 12
OpMemberDecorate %10 2 Offset 16
OpMemberDecorate %11 0 Offset 0
OpDecorate %12 ArrayStride 32
OpDecorate %14 ArrayStride 32
OpMemberDecorate %16 0 Offset 0
OpMemberDecorate %16 1 Offset 96
OpMemberDecorate %16 2 Offset 128
OpMemberDecorate %16 3 Offset 132
OpDecorate %47 BuiltIn LocalInvocationIndex
OpDecorate %95 BuiltIn LocalInvocationId
OpDecorate %101 BuiltIn LocalInvocationIndex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %11 "f" %17
OpExecutionMode %11 LocalSize 1 1 1
OpMemberDecorate %6 0 Offset 0
OpMemberDecorate %6 1 Offset 4
OpDecorate %17 BuiltIn LocalInvocationIndex
%2 = OpTypeVoid
%3 = OpTypeInt 32 1
Expand Down
15 changes: 0 additions & 15 deletions naga/tests/out/spv/wgsl-ray-query-no-init-tracking.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ OpEntryPoint GLCompute %246 "main" %15 %17
OpEntryPoint GLCompute %266 "main_candidate" %15
OpExecutionMode %246 LocalSize 1 1 1
OpExecutionMode %266 LocalSize 1 1 1
OpMemberDecorate %10 0 Offset 0
OpMemberDecorate %10 1 Offset 4
OpMemberDecorate %10 2 Offset 8
OpMemberDecorate %10 3 Offset 12
OpMemberDecorate %10 4 Offset 16
OpMemberDecorate %10 5 Offset 20
OpMemberDecorate %10 6 Offset 24
OpMemberDecorate %10 7 Offset 28
OpMemberDecorate %10 8 Offset 36
OpMemberDecorate %10 9 Offset 48
OpMemberDecorate %10 9 ColMajor
OpMemberDecorate %10 9 MatrixStride 16
OpMemberDecorate %10 10 Offset 112
OpMemberDecorate %10 10 ColMajor
OpMemberDecorate %10 10 MatrixStride 16
OpMemberDecorate %12 0 Offset 0
OpMemberDecorate %12 1 Offset 4
OpMemberDecorate %12 2 Offset 8
Expand Down
15 changes: 0 additions & 15 deletions naga/tests/out/spv/wgsl-ray-query.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ OpEntryPoint GLCompute %246 "main" %15 %17
OpEntryPoint GLCompute %266 "main_candidate" %15
OpExecutionMode %246 LocalSize 1 1 1
OpExecutionMode %266 LocalSize 1 1 1
OpMemberDecorate %10 0 Offset 0
OpMemberDecorate %10 1 Offset 4
OpMemberDecorate %10 2 Offset 8
OpMemberDecorate %10 3 Offset 12
OpMemberDecorate %10 4 Offset 16
OpMemberDecorate %10 5 Offset 20
OpMemberDecorate %10 6 Offset 24
OpMemberDecorate %10 7 Offset 28
OpMemberDecorate %10 8 Offset 36
OpMemberDecorate %10 9 Offset 48
OpMemberDecorate %10 9 ColMajor
OpMemberDecorate %10 9 MatrixStride 16
OpMemberDecorate %10 10 Offset 112
OpMemberDecorate %10 10 ColMajor
OpMemberDecorate %10 10 MatrixStride 16
OpMemberDecorate %12 0 Offset 0
OpMemberDecorate %12 1 Offset 4
OpMemberDecorate %12 2 Offset 8
Expand Down
15 changes: 0 additions & 15 deletions naga/tests/out/spv/wgsl-ray-tracing-invocation-reorder.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ OpMemberDecorate %14 2 Offset 8
OpMemberDecorate %14 3 Offset 12
OpMemberDecorate %14 4 Offset 16
OpMemberDecorate %14 5 Offset 32
OpMemberDecorate %17 0 Offset 0
OpMemberDecorate %17 1 Offset 4
OpMemberDecorate %17 2 Offset 8
OpMemberDecorate %17 3 Offset 12
OpMemberDecorate %17 4 Offset 16
OpMemberDecorate %17 5 Offset 20
OpMemberDecorate %17 6 Offset 24
OpMemberDecorate %17 7 Offset 28
OpMemberDecorate %17 8 Offset 36
OpMemberDecorate %17 9 Offset 48
OpMemberDecorate %17 9 ColMajor
OpMemberDecorate %17 9 MatrixStride 16
OpMemberDecorate %17 10 Offset 112
OpMemberDecorate %17 10 ColMajor
OpMemberDecorate %17 10 MatrixStride 16
OpDecorate %22 DescriptorSet 0
OpDecorate %22 Binding 0
OpDecorate %24 DescriptorSet 0
Expand Down
Loading