Skip to content

Commit d5b7f23

Browse files
committed
Multiple OperandKinds are incompatible
1 parent 3e55be3 commit d5b7f23

File tree

6 files changed

+61
-22
lines changed

6 files changed

+61
-22
lines changed

autogen/src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,20 @@ fn main() {
174174
tables.push_str("include!(\"autogen_table.rs\");\n");
175175
write_formatted(
176176
&autogen_src_dir.join("../rspirv/grammar/autogen_table.rs"),
177-
table::gen_grammar_inst_table_operand_kinds(&grammar),
177+
table::gen_grammar_inst_table_operand_kinds(
178+
&grammar.operand_kinds,
179+
&grammar.instructions,
180+
None,
181+
"INSTRUCTION",
182+
),
178183
);
179184
// Extended instruction sets
180185
for (ext, spirv_op, _, grammar) in extended_instruction_sets {
181186
let autogen_file = format!("autogen_{}.rs", ext.replace(".", "_").to_lowercase());
182187
write_formatted(
183188
&autogen_src_dir.join(format!("../rspirv/grammar/{autogen_file}")),
184-
table::gen_instruction_table(
189+
table::gen_grammar_inst_table_operand_kinds(
190+
&grammar.operand_kinds,
185191
&grammar.instructions,
186192
Some(spirv_op),
187193
&format!("{}_INSTRUCTION", ext.replace(".", "_").to_uppercase()),

autogen/src/structs.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ fn num_or_hex<'de, D: de::Deserializer<'de>>(d: D) -> result::Result<u32, D::Err
9494
}
9595

9696
fn visit_str<E: de::Error>(self, value: &str) -> result::Result<Self::Value, E> {
97-
let hex = value.strip_prefix("0x").unwrap();
97+
// let hex = value.strip_prefix("0x").unwrap();
98+
let Some(hex) = value.strip_prefix("0x") else {
99+
// TODO: Remove this edge-case again when https://github.com/KhronosGroup/SPIRV-Headers/pull/550 is merged
100+
return Ok(value.parse().unwrap());
101+
};
98102
Ok(u32::from_str_radix(hex, 16).unwrap())
99103
}
100104

autogen/src/table.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ fn convert_quantifier(quantifier: structs::Quantifier) -> Ident {
1313
})
1414
}
1515

16-
/// Returns the code for the whole instruction table by walking the given
17-
/// `grammar`.
16+
/// Returns the code for the whole instruction table.
1817
///
19-
/// `grammar` is expected to be an array of SPIR-V instructions.
18+
/// `instructions` is expected to be an array of SPIR-V instructions.
2019
/// `name` is the name of the generated table.
2120
/// `is_ext` indicates whether the grammar is for an extended instruction set.
2221
pub(crate) fn gen_instruction_table(
23-
grammar: &[structs::Instruction],
22+
instructions: &[structs::Instruction],
2423
ext_op_name: Option<&str>,
2524
name: &str,
2625
) -> TokenStream {
2726
// Vector for strings for all instructions.
28-
let instructions = grammar.iter().map(|inst| {
27+
let instructions = instructions.iter().map(|inst| {
2928
// Vector of strings for all operands.
3029
let operands = inst.operands.iter().map(|e| {
3130
let kind = as_ident(&e.kind);
@@ -65,24 +64,33 @@ pub(crate) fn gen_instruction_table(
6564

6665
/// Returns the generated grammar::INSTRUCTION_TABLE and grammar::OperandKind
6766
/// by walking the given SPIR-V `grammar`.
68-
pub fn gen_grammar_inst_table_operand_kinds(grammar: &structs::Grammar) -> TokenStream {
67+
pub fn gen_grammar_inst_table_operand_kinds(
68+
operand_kinds: &[structs::OperandKind],
69+
instructions: &[structs::Instruction],
70+
ext_op_name: Option<&str>,
71+
name: &str,
72+
) -> TokenStream {
6973
// Enum for all operand kinds.
70-
let elements = grammar
71-
.operand_kinds
72-
.iter()
73-
.map(|kind| as_ident(&kind.kind));
74+
let elements = operand_kinds.iter().map(|kind| as_ident(&kind.kind));
7475

7576
// Instruction table.
76-
let table = gen_instruction_table(&grammar.instructions, None, "INSTRUCTION");
77+
let table = gen_instruction_table(instructions, ext_op_name, name);
7778

78-
quote! {
79-
#[doc = "All operand kinds in the SPIR-V grammar."]
80-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
81-
#[allow(clippy::upper_case_acronyms)]
82-
pub enum OperandKind {
83-
#(#elements),*
84-
}
79+
let operand_kinds = if operand_kinds.is_empty() {
80+
None
81+
} else {
82+
Some(quote! {
83+
#[doc = "All operand kinds in the SPIR-V grammar."]
84+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
85+
#[allow(clippy::upper_case_acronyms)]
86+
pub enum OperandKind {
87+
#(#elements),*
88+
}
89+
})
90+
};
8591

92+
quote! {
93+
#operand_kinds
8694
#table
8795
}
8896
}

rspirv/grammar/autogen_debuginfo.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
// external/spirv.core.grammar.json.
33
// DO NOT MODIFY!
44

5+
#[doc = "All operand kinds in the SPIR-V grammar."]
6+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7+
#[allow(clippy::upper_case_acronyms)]
8+
pub enum OperandKind {
9+
DebugInfoFlags,
10+
DebugBaseTypeAttributeEncoding,
11+
DebugCompositeType,
12+
DebugTypeQualifier,
13+
DebugOperation,
14+
}
515
static DEBUGINFO_INSTRUCTIONS: &[ExtendedInstruction<'static>] = &[
616
ext_inst!(DebugInfoOp, DebugInfoNone, [], [], []),
717
ext_inst!(

rspirv/grammar/autogen_opencl_debuginfo_100.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
// external/spirv.core.grammar.json.
33
// DO NOT MODIFY!
44

5+
#[doc = "All operand kinds in the SPIR-V grammar."]
6+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7+
#[allow(clippy::upper_case_acronyms)]
8+
pub enum OperandKind {
9+
DebugInfoFlags,
10+
DebugBaseTypeAttributeEncoding,
11+
DebugCompositeType,
12+
DebugTypeQualifier,
13+
DebugOperation,
14+
DebugImportedEntity,
15+
}
516
static OPENCL_DEBUGINFO_100_INSTRUCTIONS: &[ExtendedInstruction<'static>] = &[
617
ext_inst!(CLDebugInfoOp, DebugInfoNone, [], [], []),
718
ext_inst!(

0 commit comments

Comments
 (0)