Repository navigation
[naga spv-out] Don't give types that contain a bool a layout - #8
matthargett wants to merge 1 commit into
Conversation
The SPIR-V backend decorated every array with `ArrayStride` and every struct member with `Offset` (and `MatrixStride` for matrices), using Naga's own layout, in which a `bool` is one byte. SPIR-V gives `OpTypeBool` no size, and a type that contains a bool is never host-shareable, so these decorations describe nothing a driver needs. Arm's proprietary Mali driver (seen with r20p0 on a Mali-G52 MC2) lays function, private and workgroup variables out by the decorations anyway, and reads and writes a bool as a wider value. With `ArrayStride 1` the elements of an `array<bool, N>` overlap: `a[i]` reads as `a[i] || a[i + 1]` for N >= 3. Arrays of bool vectors, arrays of structs with bool members, and structs that hold both a bool vector and other bools are read wrongly in the same way, and a constant `array<vec3<bool>, N>` crashes the driver's shader compiler. Leave the layout decorations off arrays and structs that contain a bool. This does not address explicit layouts on other types used outside of buffers (gfx-rs#7696).
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 89500208d6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| crate::TypeInner::Struct { ref members, .. } => { | ||
| members.iter().any(|member| contains_bool(types, member.ty)) |
There was a problem hiding this comment.
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 👍 / 👎.
Connections
Found running the WebGPU CTS with
cts_runneron a Mali-G52 MC2 (Arm proprietary Vulkan 1.1 driverr20p0, Android 10). Other fixes for the same device: #7, #9, #10.Related upstream:
gfx-rs/wgpuissue 7696 and PR 9295 are about explicit layout decorations on types in theWorkgroupstorage class. This is narrower (only types that contain abool) but covers every storage class. It doesn't fix 7696.Description
The SPIR-V backend decorates every array with
ArrayStrideand every struct member withOffset, from Naga's own layout, in which aboolis one byte.array<bool, 8>getsArrayStride 1. SPIR-V givesOpTypeBoolno size and such types are never host-shareable, so the decorations describe nothing.This driver lays function, private and workgroup variables out by them anyway and reads a bool as a wider value, so neighbours overlap: element
iof anarray<bool, N>reads asa[i] || a[i + 1]. Arrays of bool vectors and of structs with bool members go wrong the same way, and a constantarray<vec3<bool>, N>crashes the driver's shader compiler.This leaves
ArrayStride,OffsetandMatrixStrideoff arrays and structs that contain a bool. It isn't keyed on the driver.Testing
cargo fmt --checkand clippy for naga are clean; all 423 naga tests pass. Eleven SPIR-V snapshots lose decorations and nothing else;spirv-valgives each the same verdict before and after.cts_runnerbuilt from trunk60cef2be8and from this branch. Each line fills the aggregate from a buffer at run time and copies it back out:bool_conversionandworkgroupUniformLoad(execution), 116 cases: on trunk 81 pass, 12 fail and 23 have no result (the driver's compiler segfaults); all 116 pass with this branch.cts_runner/test.lston the device (54,915 of 54,931 cases; 16 left out to protect the phone's memory): 45,478 pass, 86 fail, 9,351 skip. 85 of the failures are in selectors markedfails-if(vulkan).Squash or Rebase?
Single commit.
LLM Use?
Claude Code (Claude Mythos 5.2) found the wrong results on the device, narrowed them down with the probe in the image, wrote the change, ran the tests and drafted this description.