Skip to content

Commit

Permalink
Add opcode for GLSL and OpenCL in the header
Browse files Browse the repository at this point in the history
Also provides get() methods to query extended instructions using
a symbolic opcode.
  • Loading branch information
antiagainst committed Oct 11, 2017
1 parent b590840 commit 1e180a9
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 33 deletions.
56 changes: 31 additions & 25 deletions codegen/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn main() {
}

let mut contents = String::new();

{
let path = codegen_src_dir.join("external/spirv.core.grammar.json");
let filename = path.to_str().unwrap();
Expand All @@ -93,11 +94,37 @@ fn main() {
}
let grammar: structs::Grammar = serde_json::from_str(&contents).unwrap();

// For GLSLstd450 extended instruction set.
{
let path = codegen_src_dir.join(
"external/SPIRV-Headers/include/spirv/1.1/extinst.glsl.std.450.grammar.json");
let filename = path.to_str().unwrap();
let mut file = fs::File::open(filename).unwrap();
contents.clear();
file.read_to_string(&mut contents).unwrap();
}
let gl_grammar: structs::ExtInstSetGrammar = serde_json::from_str(&contents).unwrap();

// For OpenCL extended instruction set.
{
let path = codegen_src_dir.join(
"external/SPIRV-Headers/include/spirv/1.1/extinst.opencl.std.100.grammar.json");
let filename = path.to_str().unwrap();
let mut file = fs::File::open(filename).unwrap();
contents.clear();
file.read_to_string(&mut contents).unwrap();
}
let cl_grammar: structs::ExtInstSetGrammar = serde_json::from_str(&contents).unwrap();

{
// Path to the generated SPIR-V header file.
let path = codegen_src_dir.join("../spirv/spirv.rs");
let c = header::gen_spirv_header(&grammar);
write!(c, path);
let core = header::gen_spirv_header(&grammar);
let gl = header::gen_glsl_std_450_opcodes(&gl_grammar);
let cl = header::gen_opencl_std_opcodes(&cl_grammar);

write!(core + "\n" + &gl + "\n" + &cl, path);

}

{
Expand Down Expand Up @@ -193,38 +220,17 @@ fn main() {
fmt_write!(c, path);
}

// For GLSLstd450 extended instruction set.
{
let path = codegen_src_dir.join(
"external/SPIRV-Headers/include/spirv/1.1/extinst.glsl.std.450.grammar.json");
let filename = path.to_str().unwrap();
let mut file = fs::File::open(filename).unwrap();
contents.clear();
file.read_to_string(&mut contents).unwrap();
}
let grammar: structs::ExtInstSetGrammar = serde_json::from_str(&contents).unwrap();

{
// Path to the generated GLSLstd450 extended instruction set header.
let path = codegen_src_dir.join("../rspirv/grammar/glsl_std_450.rs");
let c = table::gen_glsl_std_450_inst_table(&grammar);
let c = table::gen_glsl_std_450_inst_table(&gl_grammar);
write!(c, path);
}

// For OpenCL extended instruction set.
{
let path = codegen_src_dir.join(
"external/SPIRV-Headers/include/spirv/1.1/extinst.opencl.std.100.grammar.json");
let filename = path.to_str().unwrap();
let mut file = fs::File::open(filename).unwrap();
contents.clear();
file.read_to_string(&mut contents).unwrap();
}
let grammar: structs::ExtInstSetGrammar = serde_json::from_str(&contents).unwrap();

{
let path = codegen_src_dir.join("../rspirv/grammar/opencl_std_100.rs");
let c = table::gen_opencl_std_100_inst_table(&grammar);
let c = table::gen_opencl_std_100_inst_table(&cl_grammar);
write!(c, path);
}
}
48 changes: 48 additions & 0 deletions codegen/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ use utils::*;
static VAULE_ENUM_ATTRIBUTE: &'static str = "\
#[repr(u32)]\n#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, FromPrimitive, Hash)]";

static GLSL_STD_450_SPEC_LINK: &'static str = "\
https://www.khronos.org/registry/spir-v/specs/1.0/GLSL.std.450.html";

static OPENCL_STD_SPEC_LINK: &'static str = "\
https://www.khronos.org/registry/spir-v/specs/1.2/OpenCL.ExtendedInstructionSet.100.html";

/// Returns the markdown string containing a link to the spec for the given
/// operand `kind`.
fn get_spec_link(kind: &str) -> String {
Expand Down Expand Up @@ -126,3 +132,45 @@ pub fn gen_spirv_header(grammar: &structs::Grammar) -> String {

ret
}

/// Returns the GLSL.std.450 extended instruction opcodes.
pub fn gen_glsl_std_450_opcodes(grammar: &structs::ExtInstSetGrammar) -> String {
let mut ret = String::new();

{ // Opcodes.
// Get the instruction table.
let opcodes: Vec<String> = grammar.instructions.iter().map(|inst| {
// Omit the "Op" prefix.
format!(" {} = {},", inst.opname, inst.opcode)
}).collect();
ret.push_str(&format!("/// [GLSL.std.450]({link}) extended instruction opcode\n\
{attribute}\n\
pub enum GLOp {{\n{opcodes}\n}}\n",
link = GLSL_STD_450_SPEC_LINK,
attribute = VAULE_ENUM_ATTRIBUTE,
opcodes = opcodes.join("\n")));
}

ret
}

/// Returns the OpenCL.std extended instruction opcodes.
pub fn gen_opencl_std_opcodes(grammar: &structs::ExtInstSetGrammar) -> String {
let mut ret = String::new();

{ // Opcodes.
// Get the instruction table.
let opcodes: Vec<String> = grammar.instructions.iter().map(|inst| {
// Omit the "Op" prefix.
format!(" {} = {},", inst.opname, inst.opcode)
}).collect();
ret.push_str(&format!("/// [OpenCL.std]({link}) extended instruction opcode\n\
{attribute}\n\
pub enum CLOp {{\n{opcodes}\n}}\n",
link = OPENCL_STD_SPEC_LINK,
attribute = VAULE_ENUM_ATTRIBUTE,
opcodes = opcodes.join("\n")));
}

ret
}
39 changes: 31 additions & 8 deletions rspirv/grammar/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,18 @@ impl CoreInstructionTable {
/// Looks up the given `opcode` in the instruction table and returns
/// a reference to the instruction grammar entry if found.
pub fn lookup_opcode(opcode: u16) -> Option<&'static Instruction<'static>> {
INSTRUCTION_TABLE.iter().find(|inst| (inst.opcode as u16) == opcode)
INSTRUCTION_TABLE.iter().find(|inst| {
(inst.opcode as u16) == opcode
})
}

/// Returns a reference to the instruction grammar entry with the given
/// `opcode`.
pub fn get(opcode: spirv::Op) -> &'static Instruction<'static> {
INSTRUCTION_TABLE.iter()
.find(|inst| (inst.opcode == opcode))
.expect("internal error")
INSTRUCTION_TABLE
.iter()
.find(|inst| (inst.opcode == opcode))
.expect("internal error")
}
}

Expand All @@ -132,8 +135,18 @@ impl GlslStd450InstructionTable {
/// Looks up the given `opcode` in the instruction table and returns
/// a reference to the instruction grammar entry if found.
pub fn lookup_opcode(opcode: u32) -> Option<&'static ExtendedInstruction<'static>> {
GLSL_STD_450_INSTRUCTION_TABLE.iter()
.find(|inst| inst.opcode == opcode)
GLSL_STD_450_INSTRUCTION_TABLE.iter().find(|inst| {
inst.opcode == opcode
})
}

/// Returns a reference to the instruction grammar entry with the given
/// `opcode`.
pub fn get(opcode: spirv::GLOp) -> &'static ExtendedInstruction<'static> {
GLSL_STD_450_INSTRUCTION_TABLE
.iter()
.find(|inst| (inst.opcode == opcode as spirv::Word))
.expect("internal error")
}
}

Expand All @@ -148,8 +161,18 @@ impl OpenCLStd100InstructionTable {
/// Looks up the given `opcode` in the instruction table and returns
/// a reference to the instruction grammar entry if found.
pub fn lookup_opcode(opcode: u32) -> Option<&'static ExtendedInstruction<'static>> {
OPENCL_STD_100_INSTRUCTION_TABLE.iter()
.find(|inst| inst.opcode == opcode)
OPENCL_STD_100_INSTRUCTION_TABLE.iter().find(|inst| {
inst.opcode == opcode
})
}

/// Returns a reference to the instruction grammar entry with the given
/// `opcode`.
pub fn get(opcode: spirv::CLOp) -> &'static ExtendedInstruction<'static> {
OPENCL_STD_100_INSTRUCTION_TABLE
.iter()
.find(|inst| (inst.opcode == opcode as spirv::Word))
.expect("internal error")
}
}

Expand Down
Loading

0 comments on commit 1e180a9

Please sign in to comment.