Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad4bf42

Browse files
committedSep 23, 2021
Simplify extensions. Add Debug PrintF
1 parent ccbc13c commit ad4bf42

File tree

7 files changed

+110
-125
lines changed

7 files changed

+110
-125
lines changed
 

‎autogen/src/header.rs

+7-55
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ use proc_macro2::TokenStream;
66
use quote::quote;
77
use std::collections::BTreeMap;
88

9-
static GLSL_STD_450_SPEC_LINK: &str = "\
10-
https://www.khronos.org/registry/spir-v/specs/unified1/GLSL.std.450.html";
11-
12-
static OPENCL_STD_SPEC_LINK: &str = "\
13-
https://www.khronos.org/registry/spir-v/specs/unified1/OpenCL.ExtendedInstructionSet.100.html";
14-
159
/// Returns the markdown string containing a link to the spec for the given
1610
/// operand `kind`.
1711
fn get_spec_link(kind: &str) -> String {
@@ -304,48 +298,10 @@ pub fn gen_spirv_header(grammar: &structs::Grammar) -> TokenStream {
304298
}
305299
}
306300

307-
/// Returns the GLSL.std.450 extended instruction opcodes.
308-
pub fn gen_glsl_std_450_opcodes(grammar: &structs::ExtInstSetGrammar) -> TokenStream {
309-
// Get the instruction table.
310-
let opcodes = grammar.instructions.iter().map(|inst| {
311-
// Omit the "Op" prefix.
312-
let opname = as_ident(&inst.opname);
313-
let opcode = inst.opcode;
314-
quote! { #opname = #opcode }
315-
});
316-
317-
let from_prim_list = grammar
318-
.instructions
319-
.iter()
320-
.map(|inst| {
321-
let opname = as_ident(&inst.opname);
322-
let opcode = inst.opcode;
323-
quote! { #opcode => GLOp::#opname }
324-
})
325-
.collect::<Vec<_>>();
326-
327-
let comment = format!(
328-
"[GLSL.std.450]({}) extended instruction opcode",
329-
GLSL_STD_450_SPEC_LINK
330-
);
331-
let attribute = value_enum_attribute();
332-
let from_prim_impl = from_primitive_impl(&from_prim_list, &as_ident("GLOp"));
333-
334-
quote! {
335-
#[doc = #comment]
336-
#attribute
337-
#[allow(clippy::upper_case_acronyms)]
338-
pub enum GLOp {
339-
#(#opcodes),*
340-
}
341-
342-
#from_prim_impl
343-
}
344-
}
345-
346-
/// Returns the OpenCL.std extended instruction opcodes.
347-
pub fn gen_opencl_std_opcodes(grammar: &structs::ExtInstSetGrammar) -> TokenStream {
348-
// Get the instruction table.
301+
/// Returns extended instruction opcodes
302+
pub fn gen_opcodes(op: &str, grammar: &structs::ExtInstSetGrammar, comment: &str) -> TokenStream {
303+
let op = as_ident(op);
304+
// Get the instruction table
349305
let opcodes = grammar.instructions.iter().map(|inst| {
350306
// Omit the "Op" prefix.
351307
let opname = as_ident(&inst.opname);
@@ -359,22 +315,18 @@ pub fn gen_opencl_std_opcodes(grammar: &structs::ExtInstSetGrammar) -> TokenStre
359315
.map(|inst| {
360316
let opname = as_ident(&inst.opname);
361317
let opcode = inst.opcode;
362-
quote! { #opcode => CLOp::#opname }
318+
quote! { #opcode => #op::#opname }
363319
})
364320
.collect::<Vec<_>>();
365321

366-
let comment = format!(
367-
"[OpenCL.std]({}) extended instruction opcode",
368-
OPENCL_STD_SPEC_LINK
369-
);
370322
let attribute = value_enum_attribute();
371-
let from_prim_impl = from_primitive_impl(&from_prim_list, &as_ident("CLOp"));
323+
let from_prim_impl = from_primitive_impl(&from_prim_list, &op);
372324

373325
quote! {
374326
#[doc = #comment]
375327
#attribute
376328
#[allow(clippy::upper_case_acronyms)]
377-
pub enum CLOp {
329+
pub enum #op {
378330
#(#opcodes),*
379331
}
380332

‎autogen/src/main.rs

+65-47
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ mod table;
99
mod utils;
1010

1111
use std::{
12-
env, fs,
13-
io::{Read, Write},
12+
env,
13+
fs,
14+
io::Write,
1415
path::{Path, PathBuf},
1516
process,
1617
};
@@ -61,65 +62,82 @@ fn main() {
6162
panic!("SPIRV-Headers missing - please checkout using git submodule");
6263
}
6364

64-
let mut contents = String::new();
65-
66-
{
67-
let path = autogen_src_dir
68-
.join("external/SPIRV-Headers/include/spirv/unified1/spirv.core.grammar.json");
69-
let mut file = fs::File::open(path).unwrap();
70-
file.read_to_string(&mut contents).unwrap();
71-
}
72-
7365
let grammar: structs::Grammar = {
74-
let mut original = serde_json::from_str(&contents).unwrap();
66+
let mut original =
67+
serde_json::from_str(
68+
&std::str::from_utf8(
69+
&fs::read(autogen_src_dir.join(
70+
"external/SPIRV-Headers/include/spirv/unified1/spirv.core.grammar.json",
71+
))
72+
.unwrap(),
73+
)
74+
.unwrap(),
75+
)
76+
.unwrap();
7577
map_reserved_instructions(&mut original);
7678
original
7779
};
7880

79-
// For GLSLstd450 extended instruction set.
80-
{
81-
let path = autogen_src_dir.join(
82-
"external/SPIRV-Headers/include/spirv/unified1/extinst.glsl.std.450.grammar.json",
83-
);
84-
let mut file = fs::File::open(path).unwrap();
85-
contents.clear();
86-
file.read_to_string(&mut contents).unwrap();
87-
}
88-
let gl_grammar: structs::ExtInstSetGrammar = serde_json::from_str(&contents).unwrap();
89-
90-
// For OpenCL extended instruction set.
91-
{
92-
let path = autogen_src_dir.join(
93-
"external/SPIRV-Headers/include/spirv/unified1/extinst.opencl.std.100.grammar.json",
94-
);
95-
let mut file = fs::File::open(path).unwrap();
96-
contents.clear();
97-
file.read_to_string(&mut contents).unwrap();
98-
}
99-
let cl_grammar: structs::ExtInstSetGrammar = serde_json::from_str(&contents).unwrap();
81+
let extended_instruction_sets = [
82+
("GLSL.std.450", "GLOp", "https://www.khronos.org/registry/spir-v/specs/unified1/GLSL.std.450.html"),
83+
("OpenCL.std.100", "CLOp", "https://www.khronos.org/registry/spir-v/specs/unified1/OpenCL.ExtendedInstructionSet.100.html"),
84+
("NonSemantic.DebugPrintF", "DebugPrintFOp", "https://github.com/KhronosGroup/Vulkan-ValidationLayers/blob/master/docs/debug_printf.md"),
85+
];
86+
let extended_instruction_sets = extended_instruction_sets.map(|(ext, op, url)| {
87+
let grammar: structs::ExtInstSetGrammar = serde_json::from_str(
88+
&std::str::from_utf8(
89+
&fs::read(autogen_src_dir.join(format!(
90+
"external/SPIRV-Headers/include/spirv/unified1/extinst.{}.grammar.json",
91+
ext.to_lowercase()
92+
)))
93+
.unwrap(),
94+
)
95+
.unwrap(),
96+
)
97+
.unwrap();
98+
(ext, op, url, grammar)
99+
});
100100

101-
// Path to the generated SPIR-V header file.
101+
// SPIR-V header
102102
write_formatted(&autogen_src_dir.join("../spirv/autogen_spirv.rs"), {
103103
let core = header::gen_spirv_header(&grammar);
104-
let gl = header::gen_glsl_std_450_opcodes(&gl_grammar);
105-
let cl = header::gen_opencl_std_opcodes(&cl_grammar);
106-
format!("{}\n{}\n{}", core, gl, cl)
104+
let extended_instruction_sets =
105+
extended_instruction_sets
106+
.iter()
107+
.map(|(ext, op, url, grammar)| {
108+
header::gen_opcodes(
109+
op,
110+
&grammar,
111+
&format!("[{}]({}) extended instruction opcode", ext, url),
112+
)
113+
.to_string()
114+
});
115+
format!(
116+
"{}\n{}",
117+
core,
118+
extended_instruction_sets.collect::<Box<_>>().join("\n")
119+
)
107120
});
108121

109-
// Path to the generated instruction table.
122+
// Instruction table
110123
write_formatted(
111124
&autogen_src_dir.join("../rspirv/grammar/autogen_table.rs"),
112125
table::gen_grammar_inst_table_operand_kinds(&grammar),
113126
);
114-
// Path to the generated GLSLstd450 extended instruction set header.
115-
write_formatted(
116-
&autogen_src_dir.join("../rspirv/grammar/autogen_glsl_std_450.rs"),
117-
table::gen_glsl_std_450_inst_table(&gl_grammar),
118-
);
119-
write_formatted(
120-
&autogen_src_dir.join("../rspirv/grammar/autogen_opencl_std_100.rs"),
121-
table::gen_opencl_std_100_inst_table(&cl_grammar),
122-
);
127+
// Extended instruction sets
128+
for (ext, _, _, grammar) in extended_instruction_sets {
129+
write_formatted(
130+
&autogen_src_dir.join(format!(
131+
"../rspirv/grammar/autogen_{}.rs",
132+
ext.replace(".", "_").to_lowercase()
133+
)),
134+
table::gen_instruction_table(
135+
&grammar.instructions,
136+
&format!("{}_INSTRUCTION_TABLE", ext.replace(".", "_").to_uppercase()),
137+
true,
138+
),
139+
);
140+
}
123141

124142
// Path to the generated operands kind in data representation.
125143
write_formatted(

‎autogen/src/structs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ pub struct Grammar {
6565

6666
#[derive(Debug, Deserialize)]
6767
pub struct ExtInstSetGrammar {
68+
#[serde(default)]
6869
pub copyright: Vec<String>,
70+
#[serde(default)]
6971
pub version: u32,
7072
pub revision: u32,
7173
pub instructions: Vec<Instruction>,

‎autogen/src/table.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn convert_quantifier(quantifier: structs::Quantifier) -> Ident {
1919
/// `grammar` is expected to be an array of SPIR-V instructions.
2020
/// `name` is the name of the generated table.
2121
/// `is_ext` indicates whether the grammar is for an extended instruction set.
22-
fn gen_instruction_table(
22+
pub(crate) fn gen_instruction_table(
2323
grammar: &[structs::Instruction],
2424
name: &str,
2525
is_ext: bool,
@@ -82,23 +82,3 @@ pub fn gen_grammar_inst_table_operand_kinds(grammar: &structs::Grammar) -> Token
8282
#table
8383
}
8484
}
85-
86-
/// Writes the generated instruction table for GLSLstd450 extended instruction
87-
/// set from `grammar` to the file with the given `filename`.
88-
pub fn gen_glsl_std_450_inst_table(grammar: &structs::ExtInstSetGrammar) -> TokenStream {
89-
gen_instruction_table(
90-
&grammar.instructions,
91-
"GLSL_STD_450_INSTRUCTION_TABLE",
92-
true,
93-
)
94-
}
95-
96-
/// Writes the generated instruction table for OpenCLstd100 extended instruction
97-
/// set from `grammar` to the file with the given `filename`.
98-
pub fn gen_opencl_std_100_inst_table(grammar: &structs::ExtInstSetGrammar) -> TokenStream {
99-
gen_instruction_table(
100-
&grammar.instructions,
101-
"OPENCL_STD_100_INSTRUCTION_TABLE",
102-
true,
103-
)
104-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// AUTOMATICALLY GENERATED from the SPIR-V JSON grammar:
2+
// external/spirv.core.grammar.json.
3+
// DO NOT MODIFY!
4+
5+
static NONSEMANTIC_DEBUGPRINTF_INSTRUCTION_TABLE: &[ExtendedInstruction<'static>] = &[ext_inst!(
6+
DebugPrintf,
7+
1u32,
8+
[],
9+
[],
10+
[(IdRef, One), (IdRef, ZeroOrMore)]
11+
)];

‎rspirv/tests/spirv_blobs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rspirv::{
22
binary::{Assemble as _, Disassemble as _},
3-
dr, lift,
3+
dr,
4+
lift,
45
};
56

67
use std::path::PathBuf;

‎spirv/autogen_spirv.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -3989,7 +3989,7 @@ impl num_traits::FromPrimitive for GLOp {
39893989
Self::from_i64(n as i64)
39903990
}
39913991
}
3992-
#[doc = "[OpenCL.std](https://www.khronos.org/registry/spir-v/specs/unified1/OpenCL.ExtendedInstructionSet.100.html) extended instruction opcode"]
3992+
#[doc = "[OpenCL.std.100](https://www.khronos.org/registry/spir-v/specs/unified1/OpenCL.ExtendedInstructionSet.100.html) extended instruction opcode"]
39933993
#[repr(u32)]
39943994
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
39953995
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
@@ -4332,3 +4332,24 @@ impl num_traits::FromPrimitive for CLOp {
43324332
Self::from_i64(n as i64)
43334333
}
43344334
}
4335+
#[doc = "[NonSemantic.DebugPrintF](https://github.com/KhronosGroup/Vulkan-ValidationLayers/blob/master/docs/debug_printf.md) extended instruction opcode"]
4336+
#[repr(u32)]
4337+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
4338+
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
4339+
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
4340+
#[allow(clippy::upper_case_acronyms)]
4341+
pub enum DebugPrintFOp {
4342+
DebugPrintf = 1u32,
4343+
}
4344+
impl num_traits::FromPrimitive for DebugPrintFOp {
4345+
#[allow(trivial_numeric_casts)]
4346+
fn from_i64(n: i64) -> Option<Self> {
4347+
Some(match n as u32 {
4348+
1u32 => DebugPrintFOp::DebugPrintf,
4349+
_ => return None,
4350+
})
4351+
}
4352+
fn from_u64(n: u64) -> Option<Self> {
4353+
Self::from_i64(n as i64)
4354+
}
4355+
}

0 commit comments

Comments
 (0)
Please sign in to comment.