Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wgsl-in] Implement complete validation for size and align attributes #1979

Merged
merged 3 commits into from
Jun 16, 2022

Conversation

teoxoy
Copy link
Member

@teoxoy teoxoy commented Jun 9, 2022

From the WGSL spec:

align

Must be a power of 2, and must satisfy the required-alignment for the member type:

If align(n) is applied to a member of S with type T, and S is the store type or contained in the store type for a variable in address space C, then n must satisfy: n = k × RequiredAlignOf(T,C) for some positive integer k.

size

This number must be at least the byte-size of the type of the member:

If size(n) is applied to a member with type T, then SizeOf(T) ≤ n.

Note

  • tests have been modified accordingly
  • commits are self-contained

Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

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

This all looks fantastic. Much cleaner. Just a few points to address.

src/proc/layouter.rs Show resolved Hide resolved
src/proc/layouter.rs Outdated Show resolved Hide resolved
Comment on lines -140 to -141
alignment: Alignment::new(width as u32)
.ok_or(LayoutErrorInner::ZeroWidth.with(ty_handle))?,
Copy link
Member

Choose a reason for hiding this comment

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

It seems to me that we need to keep this an error. Naga should never panic on bad input, and this function is used by front ends, so we can't assume the types have been validated yet.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was planning to change the width in the IR from a u8 to either a PowerOfTwoU8 (concrete type or create some generic POT type used by widths and the Alignment type) or at least a NonZeroU8. Thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm... In src/lib.rs, at least, Bytes is used exclusively for 2, 4, and 8. Maybe an enum like VectorSize would suffice, with implementations of Mul with Alignment. Stuff like this would need to be changed to use a more relaxed type:

src/back/glsl/mod.rs

    /// A scalar with an unsupported width was requested.
    #[error("A scalar with an unsupported width was requested: {0:?} {1:?}")]
    UnsupportedScalar(crate::ScalarKind, crate::Bytes),

But that seems like an independent change. I think that would be best as a separate PR.

src/proc/layouter.rs Outdated Show resolved Hide resolved
src/valid/type.rs Show resolved Hide resolved
@jimblandy jimblandy merged commit ad536ce into gfx-rs:master Jun 16, 2022
@teoxoy
Copy link
Member Author

teoxoy commented Jun 16, 2022

@jimblandy I'm a bit confused, I thought we'll revert the removal of the error. The commits have been force-pushed but nothing seems to have changed; just a rebase?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants