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

WIP: MVP for diagnostic rules #6148

Draft
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

ErichDonGubler
Copy link
Member

@ErichDonGubler ErichDonGubler commented Aug 23, 2024

Connections

Partialy resolves #5320. Offers a workaround for #3135.

Description

This change implements a meaningful first iteration of support for diagnostic(…) directives and @diagnostic(…) attributes; to wit:

  1. Set up a skeleton for parsing directives, which WGSL does not yet have (feature: Parse enable directives & SHADER-F16 support #5701 also does this, but hasn't been merged yet).
  2. Work out the essential state that Naga needs to track for compilation messages controlled by diagnostic(…) rules.
  3. Control Naga's current uniformity analysis errors via the derivative_uniformity rule. The current analysis is incorrect, and blocks users in issues like Feature request: disable uniformity analysis on shader creation #3135. This can give them a way forward.

Several things are out-of-scope:

  • There are more grammar rules where, in order to conform fully with the WGSL spec., we should optionally parse @diagnostic(…) attributes. This PR does not attempt to handle all of these cases, to limit the complexity of this PR. These cases should not be necessary up-front to deliver value to WGSL authors. Therefore, parsing will be expanded to allow for these cases in one or more follow-up PRs.
  • Naga's current diagnostic model for shader compilation offers either success, or a single error message. This conflicts with the notion of warnings, where (1) we may have multiple warning items in otherwise accepted WGSL, or (2) we may have warnings in addition to a fatal error. diagnostic(warn, …), therefore, will not report a compilation item, as a typical WebGPU user might expect, for warnings in this PR. Instead, we will opt for log::warn!(…) entries. This will be fixed in a separate PR.
  • Because of the above, Naga also cannot yet emit warning-level compilation items for unrecognized diagnostic rule names. These will remain an error, for now.

Testing

Current test plan:

Checklist

  • Run cargo fmt.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
    • --target wasm32-unknown-emscripten
  • Run cargo xtask test to run tests.
  • Add change to CHANGELOG.md. See simple instructions inside file.

@ErichDonGubler ErichDonGubler added area: validation Issues related to validation, diagnostics, and error handling area: api Issues related to API surface area: cts Issues stemming from the WebGPU Conformance Test Suite naga Shader Translator area: naga front-end lang: WGSL WebGPU Shading Language labels Aug 23, 2024
@ErichDonGubler ErichDonGubler self-assigned this Aug 23, 2024
@ErichDonGubler ErichDonGubler changed the title WIP: MVP for diagnostic directives and attributes WIP: MVP for diagnostic rules Aug 23, 2024
Comment on lines +2477 to +2480
while lexer.skip(Token::Separator('.')) {
let (segment, _span) = lexer.next_ident_with_span()?;
segments.push(segment);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

There are, at most, two segments here (called "diagnostic name tokens" upstream, maybe rename?), per spec.:

The name of a triggering rule is either:

Copy link
Member

Choose a reason for hiding this comment

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

In WGSL, the idea is:

  • If you see a single-component diagnostic rule name, it's an error if the name isn't recognized.
  • If you see a double-component diagnostic rule name, it's ignored if the name isn't recognized. This lets you write clippy.my_picky_lint and have browsers ignore it.

So ultimately joining them into a String might not be what we want. But there's no need to fix that at this stage.

@@ -2433,4 +2454,43 @@ impl Parser {
}
Ok(brace_nesting_level + 1)
}

fn diagnostic_control<'a>(&self, lexer: &mut Lexer<'a>) -> Result<(), Error<'a>> {
// TODO(maybe): self.push_rule_span(Rule::???, lexer)
Copy link
Member Author

Choose a reason for hiding this comment

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

@gfx-rs/naga: It's unclear to me when to use Rule spans. Should they be used for this new functionality? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

I have always been very unsure what sorts of bugs the WGSL front end's Rule stack makes it easy to detect or fix. In a recursive descent parser, the parser's own Rust call stack indicates what we were in the midst of parsing, and the Rule stack is either identical to that, or misleading.

It does seem like the code uses push_rule_span and pop_rule_span to build spans that cover non-trivial subtrees. But you can do that just as well by calling Lexer::span_from yourself; that's something you see in the code as well.

My personal preference would be to just use Lexer::span_from directly. Logically, it should be easier to debug span problems when there are fewer "players" influencing how they're produced.

@jimblandy
Copy link
Member

Several things are out-of-scope:

  • There are more grammar rules where, in order to conform fully with the WGSL spec., we should optionally parse @diagnostic(…) attributes. This PR does not attempt to handle all of these cases, to limit the complexity of this PR. These cases should not be necessary up-front to deliver value to WGSL authors.

This is fine, but one of the reasons this issue is prioritized is that we want to be able to parse the shaders people are trying to run on the web. We can decide whether it's more convenient for us to address that in this PR or in follow-up PRs, but in order to deliver value to WGSL authors, the follow-up PRs may end up being just as urgent as this one.

@jimblandy
Copy link
Member

Overall, the scope and test plan in the OP looks fine to me.

lexer.expect(Token::Separator(','))?;
let (diagnostic_rule_name, diagnostic_rule_name_span) = lexer.capture_span(|lexer| {
// TODO: ensure that errors are good
let mut segments = Vec::new();
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use Vec::with_capacity(2)?

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 actually thinking of replacing this with an ArrayVec, pending tests with stack usage. But yes, failing that, setting capacity would be good.

@ErichDonGubler
Copy link
Member Author

ErichDonGubler commented Sep 3, 2024

@jimblandy:

[O]ne of the reasons this issue is prioritized is that we want to be able to parse the shaders people are trying to run on the web.

Speaking as the person who's linked most (all?) concrete failures against bug 1882800 in Bugzilla, the cases I've actually observed are (1) directive usage, by a wide margin, and then (2) as an attribute of a function. I don't think I've observed other application uses except for parse testing, like with the CTS. Thus, I believe making this PR focus on (1) and then following up quickly with (2) should ease most of the pressure we have on this feature. After that point, I believe we can prioritize based on blockers reported by users.

@jimblandy
Copy link
Member

the cases I've actually observed are (1) directive usage, by a wide margin, and then (2) as an attribute of a function

Okay, sounds like you're way ahead of me. +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: api Issues related to API surface area: cts Issues stemming from the WebGPU Conformance Test Suite area: naga front-end area: validation Issues related to validation, diagnostics, and error handling lang: WGSL WebGPU Shading Language naga Shader Translator
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Support diagnostic filters in WGSL (i.e., the @diagnostic(…) attribute)
2 participants