Skip to content

Commit

Permalink
feat(wgsl-in): create skeleton for parsing directives
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Oct 13, 2024
1 parent 33c3828 commit db334f7
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions naga/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,4 @@ ron = "0.8.0"
rspirv = { version = "0.11", git = "https://github.com/gfx-rs/rspirv", rev = "b969f175d5663258b4891e44b76c1544da9661ab" }
serde = { workspace = true, features = ["derive"] }
spirv = { version = "0.3", features = ["deserialize"] }
strum.workspace = true
39 changes: 39 additions & 0 deletions naga/src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ pub(crate) enum Error<'a> {
PipelineConstantIDValue(Span),
NotBool(Span),
ConstAssertFailed(Span),
DirectiveNotYetImplemented {
kind: &'static str,
span: Span,
tracking_issue_num: u16,
},
DirectiveAfterFirstGlobalDecl {
directive_span: Span,
},
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -861,6 +869,37 @@ impl<'a> Error<'a> {
labels: vec![(span, "evaluates to false".into())],
notes: vec![],
},
Error::DirectiveNotYetImplemented {
kind,
span,
tracking_issue_num,
} => ParseError {
message: format!("`{kind}` is not yet implemented"),
labels: vec![(
span,
"this global directive is standard, but not yet implemented".into(),
)],
notes: vec![format!(
concat!(
"Let Naga maintainers know that you ran into this at ",
"<https://github.com/gfx-rs/wgpu/issues/{}>, ",
"so they can prioritize it!"
),
tracking_issue_num
)],
},
Error::DirectiveAfterFirstGlobalDecl { directive_span } => ParseError {
message: "expected global declaration, but found a global directive".into(),
labels: vec![(
directive_span,
"written after first global declaration".into(),
)],
notes: vec![concat!(
"global directives are only allowed before global declarations; ",
"maybe hoist this closer to the top of the shader module?"
)
.into()],
},
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions naga/src/front/wgsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ impl Frontend {
pub fn parse_str(source: &str) -> Result<crate::Module, ParseError> {
Frontend::new().parse(source)
}

#[cfg(test)]
#[track_caller]
pub fn assert_parse_err(input: &str, snapshot: &str) {
let output = parse_str(input)
.expect_err("expected parser error")
.emit_to_string(input);
if output != snapshot {
for diff in diff::lines(snapshot, &output) {
match diff {
diff::Result::Left(l) => println!("-{l}"),
diff::Result::Both(l, _) => println!(" {l}"),
diff::Result::Right(r) => println!("+{r}"),
}
}
panic!("Error snapshot failed");
}
}
166 changes: 166 additions & 0 deletions naga/src/front/wgsl/parse/directive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum DirectiveKind {
Unimplemented(UnimplementedDirectiveKind),
}

impl DirectiveKind {
pub fn from_ident(s: &str) -> Option<(Self, &'static str)> {
Some(match s {
"diagnostic" => (
Self::Unimplemented(UnimplementedDirectiveKind::Diagnostic),
"diagnostic",
),
"enable" => (
Self::Unimplemented(UnimplementedDirectiveKind::Enable),
"enable",
),
"requires" => (
Self::Unimplemented(UnimplementedDirectiveKind::Requires),
"requires",
),
_ => return None,
})
}

#[cfg(test)]
fn iter() -> impl Iterator<Item = Self> {
use strum::IntoEnumIterator;

UnimplementedDirectiveKind::iter().map(Self::Unimplemented)
}
}

#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(test, derive(strum::EnumIter))]
pub enum UnimplementedDirectiveKind {
Diagnostic,
Enable,
Requires,
}

impl UnimplementedDirectiveKind {
pub const fn tracking_issue_num(self) -> u16 {
match self {
Self::Diagnostic => 5320,
Self::Requires => 6350,
Self::Enable => 5476,
}
}
}

#[cfg(test)]
mod test {
use strum::IntoEnumIterator;

use crate::front::wgsl::assert_parse_err;

use super::{DirectiveKind, UnimplementedDirectiveKind};

#[test]
fn unimplemented_directives() {
for unsupported_shader in UnimplementedDirectiveKind::iter() {
let shader;
let expected_msg;
match unsupported_shader {
UnimplementedDirectiveKind::Diagnostic => {
shader = "diagnostic(off,derivative_uniformity);";
expected_msg = "\
error: `diagnostic` is not yet implemented
┌─ wgsl:1:1
1 │ diagnostic(off,derivative_uniformity);
│ ^^^^^^^^^^ this global directive is standard, but not yet implemented
= note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/5320>, so they can prioritize it!
";
}
UnimplementedDirectiveKind::Enable => {
shader = "enable f16;";
expected_msg = "\
error: `enable` is not yet implemented
┌─ wgsl:1:1
1 │ enable f16;
│ ^^^^^^ this global directive is standard, but not yet implemented
= note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/5476>, so they can prioritize it!
";
}
UnimplementedDirectiveKind::Requires => {
shader = "requires readonly_and_readwrite_storage_textures";
expected_msg = "\
error: `requires` is not yet implemented
┌─ wgsl:1:1
1 │ requires readonly_and_readwrite_storage_textures
│ ^^^^^^^^ this global directive is standard, but not yet implemented
= note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/6350>, so they can prioritize it!
";
}
};

assert_parse_err(shader, expected_msg);
}
}

#[test]
fn directive_after_global_decl() {
for unsupported_shader in DirectiveKind::iter() {
let directive;
let expected_msg;
match unsupported_shader {
DirectiveKind::Unimplemented(UnimplementedDirectiveKind::Diagnostic) => {
directive = "diagnostic(off,derivative_uniformity)";
expected_msg = "\
error: expected global declaration, but found a global directive
┌─ wgsl:2:1
2 │ diagnostic(off,derivative_uniformity);
│ ^^^^^^^^^^ written after first global declaration
= note: global directives are only allowed before global declarations; maybe hoist this closer to the top of the shader module?
";
}
DirectiveKind::Unimplemented(UnimplementedDirectiveKind::Enable) => {
directive = "enable f16";
expected_msg = "\
error: expected global declaration, but found a global directive
┌─ wgsl:2:1
2 │ enable f16;
│ ^^^^^^ written after first global declaration
= note: global directives are only allowed before global declarations; maybe hoist this closer to the top of the shader module?
";
}
DirectiveKind::Unimplemented(UnimplementedDirectiveKind::Requires) => {
directive = "requires readonly_and_readwrite_storage_textures";
expected_msg = "\
error: expected global declaration, but found a global directive
┌─ wgsl:2:1
2 │ requires readonly_and_readwrite_storage_textures;
│ ^^^^^^^^ written after first global declaration
= note: global directives are only allowed before global declarations; maybe hoist this closer to the top of the shader module?
";
}
}

let shader = format!(
"\
@group(0) @binding(0) var<storage> thing: i32;
{directive};
"
);
assert_parse_err(&shader, expected_msg);
}
}
}
29 changes: 29 additions & 0 deletions naga/src/front/wgsl/parse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::front::wgsl::error::{Error, ExpectedToken};
use crate::front::wgsl::parse::directive::DirectiveKind;
use crate::front::wgsl::parse::lexer::{Lexer, Token};
use crate::front::wgsl::parse::number::Number;
use crate::front::wgsl::Scalar;
Expand All @@ -7,6 +8,7 @@ use crate::{Arena, FastIndexSet, Handle, ShaderStage, Span};

pub mod ast;
pub mod conv;
pub mod directive;
pub mod lexer;
pub mod number;

Expand Down Expand Up @@ -136,6 +138,7 @@ enum Rule {
SingularExpr,
UnaryExpr,
GeneralExpr,
Directive,
}

struct ParsedAttribute<T> {
Expand Down Expand Up @@ -2357,6 +2360,9 @@ impl Parser {
let start = lexer.start_byte_offset();
let kind = match lexer.next() {
(Token::Separator(';'), _) => None,
(Token::Word(word), directive_span) if DirectiveKind::from_ident(word).is_some() => {
return Err(Error::DirectiveAfterFirstGlobalDecl { directive_span });
}
(Token::Word("struct"), _) => {
let name = lexer.next_ident()?;

Expand Down Expand Up @@ -2474,6 +2480,29 @@ impl Parser {

let mut lexer = Lexer::new(source);
let mut tu = ast::TranslationUnit::default();

// Parse directives.
#[allow(clippy::never_loop, unreachable_code)]
while let Ok((ident, span)) = lexer.peek_ident_with_span() {
if let Some((kind, name)) = DirectiveKind::from_ident(ident) {
self.push_rule_span(Rule::Directive, &mut lexer);
let _ = lexer.next_ident_with_span().unwrap();
match kind {
DirectiveKind::Unimplemented(unimplemented) => {
return Err(Error::DirectiveNotYetImplemented {
kind: name,
span,
tracking_issue_num: unimplemented.tracking_issue_num(),
})
}
}
self.pop_rule_span(&lexer);
} else {
break;
}
// TODO: add directives to set of expected things in errors later
}

loop {
match self.global_decl(&mut lexer, &mut tu) {
Err(error) => return Err(error),
Expand Down

0 comments on commit db334f7

Please sign in to comment.