Skip to content

Commit db334f7

Browse files
feat(wgsl-in): create skeleton for parsing directives
1 parent 33c3828 commit db334f7

File tree

6 files changed

+254
-0
lines changed

6 files changed

+254
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

naga/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,4 @@ ron = "0.8.0"
101101
rspirv = { version = "0.11", git = "https://github.com/gfx-rs/rspirv", rev = "b969f175d5663258b4891e44b76c1544da9661ab" }
102102
serde = { workspace = true, features = ["derive"] }
103103
spirv = { version = "0.3", features = ["deserialize"] }
104+
strum.workspace = true

naga/src/front/wgsl/error.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ pub(crate) enum Error<'a> {
265265
PipelineConstantIDValue(Span),
266266
NotBool(Span),
267267
ConstAssertFailed(Span),
268+
DirectiveNotYetImplemented {
269+
kind: &'static str,
270+
span: Span,
271+
tracking_issue_num: u16,
272+
},
273+
DirectiveAfterFirstGlobalDecl {
274+
directive_span: Span,
275+
},
268276
}
269277

270278
#[derive(Clone, Debug)]
@@ -861,6 +869,37 @@ impl<'a> Error<'a> {
861869
labels: vec![(span, "evaluates to false".into())],
862870
notes: vec![],
863871
},
872+
Error::DirectiveNotYetImplemented {
873+
kind,
874+
span,
875+
tracking_issue_num,
876+
} => ParseError {
877+
message: format!("`{kind}` is not yet implemented"),
878+
labels: vec![(
879+
span,
880+
"this global directive is standard, but not yet implemented".into(),
881+
)],
882+
notes: vec![format!(
883+
concat!(
884+
"Let Naga maintainers know that you ran into this at ",
885+
"<https://github.com/gfx-rs/wgpu/issues/{}>, ",
886+
"so they can prioritize it!"
887+
),
888+
tracking_issue_num
889+
)],
890+
},
891+
Error::DirectiveAfterFirstGlobalDecl { directive_span } => ParseError {
892+
message: "expected global declaration, but found a global directive".into(),
893+
labels: vec![(
894+
directive_span,
895+
"written after first global declaration".into(),
896+
)],
897+
notes: vec![concat!(
898+
"global directives are only allowed before global declarations; ",
899+
"maybe hoist this closer to the top of the shader module?"
900+
)
901+
.into()],
902+
},
864903
}
865904
}
866905
}

naga/src/front/wgsl/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,21 @@ impl Frontend {
5858
pub fn parse_str(source: &str) -> Result<crate::Module, ParseError> {
5959
Frontend::new().parse(source)
6060
}
61+
62+
#[cfg(test)]
63+
#[track_caller]
64+
pub fn assert_parse_err(input: &str, snapshot: &str) {
65+
let output = parse_str(input)
66+
.expect_err("expected parser error")
67+
.emit_to_string(input);
68+
if output != snapshot {
69+
for diff in diff::lines(snapshot, &output) {
70+
match diff {
71+
diff::Result::Left(l) => println!("-{l}"),
72+
diff::Result::Both(l, _) => println!(" {l}"),
73+
diff::Result::Right(r) => println!("+{r}"),
74+
}
75+
}
76+
panic!("Error snapshot failed");
77+
}
78+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
2+
pub enum DirectiveKind {
3+
Unimplemented(UnimplementedDirectiveKind),
4+
}
5+
6+
impl DirectiveKind {
7+
pub fn from_ident(s: &str) -> Option<(Self, &'static str)> {
8+
Some(match s {
9+
"diagnostic" => (
10+
Self::Unimplemented(UnimplementedDirectiveKind::Diagnostic),
11+
"diagnostic",
12+
),
13+
"enable" => (
14+
Self::Unimplemented(UnimplementedDirectiveKind::Enable),
15+
"enable",
16+
),
17+
"requires" => (
18+
Self::Unimplemented(UnimplementedDirectiveKind::Requires),
19+
"requires",
20+
),
21+
_ => return None,
22+
})
23+
}
24+
25+
#[cfg(test)]
26+
fn iter() -> impl Iterator<Item = Self> {
27+
use strum::IntoEnumIterator;
28+
29+
UnimplementedDirectiveKind::iter().map(Self::Unimplemented)
30+
}
31+
}
32+
33+
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
34+
#[cfg_attr(test, derive(strum::EnumIter))]
35+
pub enum UnimplementedDirectiveKind {
36+
Diagnostic,
37+
Enable,
38+
Requires,
39+
}
40+
41+
impl UnimplementedDirectiveKind {
42+
pub const fn tracking_issue_num(self) -> u16 {
43+
match self {
44+
Self::Diagnostic => 5320,
45+
Self::Requires => 6350,
46+
Self::Enable => 5476,
47+
}
48+
}
49+
}
50+
51+
#[cfg(test)]
52+
mod test {
53+
use strum::IntoEnumIterator;
54+
55+
use crate::front::wgsl::assert_parse_err;
56+
57+
use super::{DirectiveKind, UnimplementedDirectiveKind};
58+
59+
#[test]
60+
fn unimplemented_directives() {
61+
for unsupported_shader in UnimplementedDirectiveKind::iter() {
62+
let shader;
63+
let expected_msg;
64+
match unsupported_shader {
65+
UnimplementedDirectiveKind::Diagnostic => {
66+
shader = "diagnostic(off,derivative_uniformity);";
67+
expected_msg = "\
68+
error: `diagnostic` is not yet implemented
69+
┌─ wgsl:1:1
70+
71+
1 │ diagnostic(off,derivative_uniformity);
72+
│ ^^^^^^^^^^ this global directive is standard, but not yet implemented
73+
74+
= note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/5320>, so they can prioritize it!
75+
76+
";
77+
}
78+
UnimplementedDirectiveKind::Enable => {
79+
shader = "enable f16;";
80+
expected_msg = "\
81+
error: `enable` is not yet implemented
82+
┌─ wgsl:1:1
83+
84+
1 │ enable f16;
85+
│ ^^^^^^ this global directive is standard, but not yet implemented
86+
87+
= note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/5476>, so they can prioritize it!
88+
89+
";
90+
}
91+
UnimplementedDirectiveKind::Requires => {
92+
shader = "requires readonly_and_readwrite_storage_textures";
93+
expected_msg = "\
94+
error: `requires` is not yet implemented
95+
┌─ wgsl:1:1
96+
97+
1 │ requires readonly_and_readwrite_storage_textures
98+
│ ^^^^^^^^ this global directive is standard, but not yet implemented
99+
100+
= note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/6350>, so they can prioritize it!
101+
102+
";
103+
}
104+
};
105+
106+
assert_parse_err(shader, expected_msg);
107+
}
108+
}
109+
110+
#[test]
111+
fn directive_after_global_decl() {
112+
for unsupported_shader in DirectiveKind::iter() {
113+
let directive;
114+
let expected_msg;
115+
match unsupported_shader {
116+
DirectiveKind::Unimplemented(UnimplementedDirectiveKind::Diagnostic) => {
117+
directive = "diagnostic(off,derivative_uniformity)";
118+
expected_msg = "\
119+
error: expected global declaration, but found a global directive
120+
┌─ wgsl:2:1
121+
122+
2 │ diagnostic(off,derivative_uniformity);
123+
│ ^^^^^^^^^^ written after first global declaration
124+
125+
= note: global directives are only allowed before global declarations; maybe hoist this closer to the top of the shader module?
126+
127+
";
128+
}
129+
DirectiveKind::Unimplemented(UnimplementedDirectiveKind::Enable) => {
130+
directive = "enable f16";
131+
expected_msg = "\
132+
error: expected global declaration, but found a global directive
133+
┌─ wgsl:2:1
134+
135+
2 │ enable f16;
136+
│ ^^^^^^ written after first global declaration
137+
138+
= note: global directives are only allowed before global declarations; maybe hoist this closer to the top of the shader module?
139+
140+
";
141+
}
142+
DirectiveKind::Unimplemented(UnimplementedDirectiveKind::Requires) => {
143+
directive = "requires readonly_and_readwrite_storage_textures";
144+
expected_msg = "\
145+
error: expected global declaration, but found a global directive
146+
┌─ wgsl:2:1
147+
148+
2 │ requires readonly_and_readwrite_storage_textures;
149+
│ ^^^^^^^^ written after first global declaration
150+
151+
= note: global directives are only allowed before global declarations; maybe hoist this closer to the top of the shader module?
152+
153+
";
154+
}
155+
}
156+
157+
let shader = format!(
158+
"\
159+
@group(0) @binding(0) var<storage> thing: i32;
160+
{directive};
161+
"
162+
);
163+
assert_parse_err(&shader, expected_msg);
164+
}
165+
}
166+
}

naga/src/front/wgsl/parse/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::front::wgsl::error::{Error, ExpectedToken};
2+
use crate::front::wgsl::parse::directive::DirectiveKind;
23
use crate::front::wgsl::parse::lexer::{Lexer, Token};
34
use crate::front::wgsl::parse::number::Number;
45
use crate::front::wgsl::Scalar;
@@ -7,6 +8,7 @@ use crate::{Arena, FastIndexSet, Handle, ShaderStage, Span};
78

89
pub mod ast;
910
pub mod conv;
11+
pub mod directive;
1012
pub mod lexer;
1113
pub mod number;
1214

@@ -136,6 +138,7 @@ enum Rule {
136138
SingularExpr,
137139
UnaryExpr,
138140
GeneralExpr,
141+
Directive,
139142
}
140143

141144
struct ParsedAttribute<T> {
@@ -2357,6 +2360,9 @@ impl Parser {
23572360
let start = lexer.start_byte_offset();
23582361
let kind = match lexer.next() {
23592362
(Token::Separator(';'), _) => None,
2363+
(Token::Word(word), directive_span) if DirectiveKind::from_ident(word).is_some() => {
2364+
return Err(Error::DirectiveAfterFirstGlobalDecl { directive_span });
2365+
}
23602366
(Token::Word("struct"), _) => {
23612367
let name = lexer.next_ident()?;
23622368

@@ -2474,6 +2480,29 @@ impl Parser {
24742480

24752481
let mut lexer = Lexer::new(source);
24762482
let mut tu = ast::TranslationUnit::default();
2483+
2484+
// Parse directives.
2485+
#[allow(clippy::never_loop, unreachable_code)]
2486+
while let Ok((ident, span)) = lexer.peek_ident_with_span() {
2487+
if let Some((kind, name)) = DirectiveKind::from_ident(ident) {
2488+
self.push_rule_span(Rule::Directive, &mut lexer);
2489+
let _ = lexer.next_ident_with_span().unwrap();
2490+
match kind {
2491+
DirectiveKind::Unimplemented(unimplemented) => {
2492+
return Err(Error::DirectiveNotYetImplemented {
2493+
kind: name,
2494+
span,
2495+
tracking_issue_num: unimplemented.tracking_issue_num(),
2496+
})
2497+
}
2498+
}
2499+
self.pop_rule_span(&lexer);
2500+
} else {
2501+
break;
2502+
}
2503+
// TODO: add directives to set of expected things in errors later
2504+
}
2505+
24772506
loop {
24782507
match self.global_decl(&mut lexer, &mut tu) {
24792508
Err(error) => return Err(error),

0 commit comments

Comments
 (0)