Skip to content

Commit

Permalink
Regenerate some decoration instructions using the grammar.
Browse files Browse the repository at this point in the history
  • Loading branch information
antiagainst committed Feb 9, 2017
1 parent 49c4004 commit 8411ea6
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 74 deletions.
7 changes: 7 additions & 0 deletions rspirv/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ fn main() {
let c = memrepr::gen_mr_builder_terminator(&grammar.instructions);
write!(c, path);
}
{
// Path to the generated builder for memory representation.
path.pop();
path.push("build_annotation.rs");
let c = memrepr::gen_mr_builder_annotation(&grammar.instructions);
write!(c, path);
}
{
// Path to the generated builder for memory representation.
path.pop();
Expand Down
36 changes: 36 additions & 0 deletions rspirv/build/memrepr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ fn get_push_extras(params: &[structs::Operand], container: &str)
s = "",
name = name,
container = container))
} else if param.kind == "PairIdRefLiteralInteger" {
Some(format!(
"{s:8}for v in {name} {{\n\
{s:12}{container}.push(mr::Operand::IdRef(v.0));\n\
{s:12}{container}.push(mr::Operand::LiteralInt32(v.1));\n\
{s:8}}}",
s = "",
name = name,
container = container))
} else if param.kind == "PairIdRefIdRef" {
Some(format!(
"{s:8}for v in {name} {{\n\
Expand Down Expand Up @@ -420,3 +429,30 @@ pub fn gen_mr_builder_debug(grammar: &Vec<structs::Instruction>) -> String {
}).collect();
format!("impl Builder {{\n{}\n}}", elements.join("\n\n"))
}

pub fn gen_mr_builder_annotation(grammar: &Vec<structs::Instruction>) -> String {
// Generate build methods for all constants.
let elements: Vec<String> = grammar.iter().filter(|inst| {
inst.class == "Annotation" && inst.opname != "OpDecorationGroup"
}).map(|inst| {
let params = get_param_list(&inst.operands).join(", ");
let extras = get_push_extras(&inst.operands, "inst.operands").join(";\n");
format!("{s:4}/// Appends an Op{opcode} instruction.\n\
{s:4}pub fn {name}(&mut self{x}{params}) {{\n\
{s:8}let {m}inst = mr::Instruction::new(\
spirv::Op::{opcode}, None, None, vec![{init}]);\n\
{extras}{y}\
{s:8}self.module.annotations.push(inst);\n\
{s:4}}}",
s = "",
name = get_function_name(&inst.opname),
extras = extras,
params = params,
x = if params.len() == 0 { "" } else { ", " },
m = if extras.len() == 0 { "" } else { "mut " },
y = if extras.len() != 0 { ";\n" } else { "" },
init = get_init_list(&inst.operands).join(", "),
opcode = &inst.opname[2..])
}).collect();
format!("impl Builder {{\n{}\n}}", elements.join("\n\n"))
}
50 changes: 50 additions & 0 deletions rspirv/mr/build_annotation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// AUTOMATICALLY GENERATED from the SPIR-V JSON grammar:
// external/spirv.core.grammar.json.
// DO NOT MODIFY!

impl Builder {
/// Appends an OpDecorate instruction.
pub fn decorate(&mut self, target: spirv::Word, decoration: spirv::Decoration) {
let inst = mr::Instruction::new(spirv::Op::Decorate, None, None, vec![mr::Operand::IdRef(target), mr::Operand::Decoration(decoration)]);
self.module.annotations.push(inst);
}

/// Appends an OpMemberDecorate instruction.
pub fn member_decorate(&mut self, structure_type: spirv::Word, member: u32, decoration: spirv::Decoration) {
let inst = mr::Instruction::new(spirv::Op::MemberDecorate, None, None, vec![mr::Operand::IdRef(structure_type), mr::Operand::LiteralInt32(member), mr::Operand::Decoration(decoration)]);
self.module.annotations.push(inst);
}

/// Appends an OpGroupDecorate instruction.
pub fn group_decorate(&mut self, decoration_group: spirv::Word, targets: Vec<spirv::Word>) {
let mut inst = mr::Instruction::new(spirv::Op::GroupDecorate, None, None, vec![mr::Operand::IdRef(decoration_group)]);
for v in targets {
inst.operands.push(mr::Operand::IdRef(v))
};
self.module.annotations.push(inst);
}

/// Appends an OpGroupMemberDecorate instruction.
pub fn group_member_decorate(&mut self, decoration_group: spirv::Word, targets: Vec<(spirv::Word, u32)>) {
let mut inst = mr::Instruction::new(spirv::Op::GroupMemberDecorate, None, None, vec![mr::Operand::IdRef(decoration_group)]);
for v in targets {
inst.operands.push(mr::Operand::IdRef(v.0));
inst.operands.push(mr::Operand::LiteralInt32(v.1));
};
self.module.annotations.push(inst);
}
}
84 changes: 10 additions & 74 deletions rspirv/mr/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,20 @@ impl Builder {

include!("build_type.rs");
include!("build_constant.rs");
include!("build_annotation.rs");
include!("build_terminator.rs");
include!("build_debug.rs");

impl Builder {
/// Appends an OpDecorationGroup instruction and returns the result id.
pub fn decoration_group(&mut self) -> spirv::Word {
let id = self.id();
self.module
.annotations
.push(mr::Instruction::new(spirv::Op::DecorationGroup, None, Some(id), vec![]));
id
}

pub fn string(&mut self, s: String) -> spirv::Word {
let id = self.id();
self.module.debugs.push(mr::Instruction::new(spirv::Op::String,
Expand All @@ -230,80 +240,6 @@ impl Builder {
}
}

impl Builder {
/// Appends an OpDecorate instruction and returns the result id.
pub fn decorate(&mut self,
target: spirv::Word,
decoration: spirv::Decoration,
mut params: Vec<mr::Operand>)
-> spirv::Word {
let id = self.id();
let mut operands = vec![mr::Operand::IdRef(target), mr::Operand::Decoration(decoration)];
operands.append(&mut params);
self.module
.annotations
.push(mr::Instruction::new(spirv::Op::Decorate, None, Some(id), operands));
id
}

/// Appends an OpMemberDecorate instruction and returns the result id.
pub fn member_decorate(&mut self,
structure: spirv::Word,
member: spirv::Word,
decoration: spirv::Decoration,
mut params: Vec<mr::Operand>)
-> spirv::Word {
let id = self.id();
let mut operands = vec![mr::Operand::IdRef(structure),
mr::Operand::IdRef(member),
mr::Operand::Decoration(decoration)];
operands.append(&mut params);
self.module
.annotations
.push(mr::Instruction::new(spirv::Op::MemberDecorate, None, Some(id), operands));
id
}

/// Appends an OpDecorationGroup instruction and returns the result id.
pub fn decoration_group(&mut self) -> spirv::Word {
let id = self.id();
self.module
.annotations
.push(mr::Instruction::new(spirv::Op::DecorationGroup, None, Some(id), vec![]));
id
}

/// Appends an OpGroupDecorate instruction and returns the result id.
pub fn group_decorate(&mut self, group: spirv::Word, targets: Vec<spirv::Word>) -> spirv::Word {
let id = self.id();
let mut operands = vec![mr::Operand::IdRef(group)];
for v in targets {
operands.push(mr::Operand::IdRef(v));
}
self.module
.annotations
.push(mr::Instruction::new(spirv::Op::GroupDecorate, None, Some(id), operands));
id
}

/// Appends an OpGroupMemberDecorate instruction and returns the result id.
pub fn group_member_decorate(&mut self,
group: spirv::Word,
targets: Vec<(spirv::Word, u32)>)
-> spirv::Word {
let id = self.id();
let mut operands = vec![mr::Operand::IdRef(group)];
for (target, member) in targets {
operands.push(mr::Operand::IdRef(target));
operands.push(mr::Operand::LiteralInt32(member));
}
self.module
.annotations
.push(mr::Instruction::new(spirv::Op::GroupMemberDecorate, None, Some(id), operands));
id
}
}

impl Builder {
/// Appends an OpConstant instruction with the given 32-bit float `value`.
/// or the module if no basic block is under construction.
Expand Down

0 comments on commit 8411ea6

Please sign in to comment.