Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [PR#53](https://github.com/Rust-GPU/rspirv2/pull/53) rename `rspirv2::custom_inst` to `op_switch`
- [PR#52](https://github.com/Rust-GPU/rspirv2/pull/52) change `InstEncoding::id_result_type()` return type from `IdResult` to `IdResultType`
- [PR#60](https://github.com/Rust-GPU/rspirv2/pull/60) `LiteralExtInstInteger` and `LiteralSpecConstantOpInteger` are now just aliases of `LiteralInteger`
- [PR#61](https://github.com/Rust-GPU/rspirv2/pull/61) `InstEncoding::encode` cannot error anymore

### Fixed

Expand Down
18 changes: 8 additions & 10 deletions crates/grammar-parser/src/codegen/inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,12 @@ pub fn write_inst(writer: &mut GrammarWriter, grammar: &Grammar<'_>) -> anyhow::

quote! {
impl OpSpecOpInst for #struct_ident {
fn op_spec_op_encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn op_spec_op_encode(&self, writer: &mut impl WordWriter) {
profiling::function_scope!();
OperandEncoding::encode(&self.#member_id_result_type, &mut *writer)?;
OperandEncoding::encode(&self.#member_id_result, &mut *writer)?;
writer.write_op(Self::META.opcode, 0)?;
#(OperandEncoding::encode(&self.#members, &mut *writer)?;)*
Ok(())
OperandEncoding::encode(&self.#member_id_result_type, &mut *writer);
OperandEncoding::encode(&self.#member_id_result, &mut *writer);
writer.write_op(Self::META.opcode, 0);
#(OperandEncoding::encode(&self.#members, &mut *writer);)*
}

fn op_spec_op_decode(reader: &[Word]) -> Result<Self, DecodeError> {
Expand Down Expand Up @@ -229,12 +228,11 @@ pub fn write_inst(writer: &mut GrammarWriter, grammar: &Grammar<'_>) -> anyhow::
#id_result_type_get
}

fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
profiling::function_scope!();
let len = 1 #(+OperandEncoding::word_len(&self.#members))*;
writer.write_op(Self::META.opcode, len)?;
#(OperandEncoding::encode(&self.#members, &mut *writer)?;)*
Ok(())
writer.write_op(Self::META.opcode, len);
#(OperandEncoding::encode(&self.#members, &mut *writer);)*
}

fn decode(reader: InstReader<'_>) -> Result<Self, DecodeError> {
Expand Down
4 changes: 2 additions & 2 deletions crates/grammar-parser/src/codegen/inst_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn write_op_spec_op_inst_enum(
);
quote! {
impl OpSpecOpInst for #name {
fn op_spec_op_encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn op_spec_op_encode(&self, writer: &mut impl WordWriter) {
profiling::function_scope!();
match self {
#(#encode_match)*
Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn quote_inst_set<'a, 'b: 'a>(
stringify!(#name)
}

fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
profiling::function_scope!();
match self {
#(#encode_match)*
Expand Down
21 changes: 7 additions & 14 deletions crates/grammar-parser/src/codegen/operands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ pub fn write_operands(writer: &mut GrammarWriter, grammar: &Grammar<'_>) -> anyh
}

fn param_to_member_decl(p: &OperandSpecMeta<'_>) -> TokenStream {
let docs = p
.name
.as_ref()
.map(|name| make_doc(name))
.unwrap_or_default();
let docs = p.name.as_ref().map_or_default(|name| make_doc(name));
let ty = OperandKind::type_ident(&p.kind);
match p.quantifier {
Quantifier::One => quote!(#docs #ty),
Expand Down Expand Up @@ -91,7 +87,7 @@ fn emit_rust_like_enum(
quote! {
Self::#symbol (#(#param_symbols),*) => {
writer.write(Word(#value));
#(OperandEncoding::encode(#param_symbols, &mut *writer)?);*
#(OperandEncoding::encode(#param_symbols, &mut *writer));*
}
}
} else {
Expand Down Expand Up @@ -138,12 +134,11 @@ fn emit_rust_like_enum(
unsafe impl OperandEncoding for #name {
const FIXED_LEN: Option<usize> = None;

fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
profiling::function_scope!();
match self {
#(#encode),*
}
Ok(())
}

fn decode(reader: &mut OperandReader<'_>) -> Result<Self, DecodeError> {
Expand Down Expand Up @@ -238,10 +233,9 @@ fn emit_c_like_enum(operand_kind: &OperandKind<'_>, enumerants: &[Enumerant<'_>]
unsafe impl OperandEncoding for #name {
const FIXED_LEN: Option<usize> = Some(1);

fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError>{
fn encode(&self, writer: &mut impl WordWriter) {
profiling::function_scope!();
writer.write(Word(*self as u32));
Ok(())
}

fn decode(reader: &mut OperandReader<'_>) -> Result<Self, DecodeError> {
Expand Down Expand Up @@ -362,10 +356,9 @@ fn emit_bitflags_enum_common(
unsafe impl OperandEncoding for #name {
const FIXED_LEN: Option<usize> = Some(1);

fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
profiling::function_scope!();
writer.write(Word(self.bits()));
Ok(())
}

fn decode(reader: &mut OperandReader<'_>) -> Result<Self, DecodeError> {
Expand Down Expand Up @@ -533,8 +526,8 @@ fn emit_parameterised_bitmask(
unsafe impl OperandEncoding for #name {
const FIXED_LEN: Option<usize> = None;

fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
self.0.encode(writer)
fn encode(&self, writer: &mut impl WordWriter) {
self.0.encode(writer);
}

fn decode(reader: &mut OperandReader<'_>) -> Result<Self, DecodeError> {
Expand Down
28 changes: 0 additions & 28 deletions crates/rspirv2-types/src/binary/encode_error.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/rspirv2-types/src/binary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod decode_error;
mod encode_error;
mod id_alloc;
mod reader;
mod writer;

pub use decode_error::*;
pub use encode_error::*;
pub use id_alloc::*;
pub use reader::*;
pub use writer::*;
6 changes: 2 additions & 4 deletions crates/rspirv2-types/src/binary/writer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::Word;
use crate::binary::EncodeError;
use smallvec::SmallVec;
use std::ops::{Deref, DerefMut};

Expand All @@ -17,10 +16,9 @@ pub trait WordWriter: Sized {
///
/// Should not be overwritten.
#[inline]
fn write_op(&mut self, op: u16, len: usize) -> Result<(), EncodeError> {
fn write_op(&mut self, op: u16, len: usize) {
self.inst_reserve(len);
self.write(Word::new_op(op, len)?);
Ok(())
self.write(Word::new_op(op, len));
}

/// Expect this many words to be written by the next instruction.
Expand Down
8 changes: 3 additions & 5 deletions crates/rspirv2-types/src/inst.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Word;
use crate::binary::{DecodeError, DecodeErrorKind, EncodeError, InstReader, WordWriter};
use crate::binary::{DecodeError, DecodeErrorKind, InstReader, WordWriter};
use crate::dis::DisContext;
use crate::meta::InstMeta;
use crate::operand::{IdResult, IdResultType};
Expand Down Expand Up @@ -30,7 +30,7 @@ pub trait InstEncoding: Sized + Debug + Eq {
}

/// Encode this instruction to a [`WordWriter`]
fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError>;
fn encode(&self, writer: &mut impl WordWriter);

/// Decode this instruction from an [`InstReader`], error when opcode is unknown.
///
Expand Down Expand Up @@ -75,9 +75,7 @@ impl InstEncoding for () {
"()"
}

fn encode(&self, _: &mut impl WordWriter) -> Result<(), EncodeError> {
Ok(())
}
fn encode(&self, _: &mut impl WordWriter) {}

fn decode(reader: InstReader<'_>) -> Result<Self, DecodeError> {
Err(DecodeErrorKind::UnknownOpCode {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspirv2-types/src/op_spec_op.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Word;
use crate::binary::{DecodeError, EncodeError, WordWriter};
use crate::binary::{DecodeError, WordWriter};
use crate::inst::InstEncoding;
use crate::operand::OperandDisContext;
use std::fmt::{Display, Formatter};
Expand All @@ -21,7 +21,7 @@ use std::fmt::{Display, Formatter};
/// * <https://github.com/KhronosGroup/SPIRV-Headers/issues/124>
pub trait OpSpecOpInst: InstEncoding {
/// Encode this instruction to a [`WordWriter`]
fn op_spec_op_encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError>;
fn op_spec_op_encode(&self, writer: &mut impl WordWriter);

/// Decode this instruction from a slice of words.
///
Expand Down
8 changes: 3 additions & 5 deletions crates/rspirv2-types/src/operand/id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! All Operands with `Category::Id`

use crate::Word;
use crate::binary::{DecodeError, EncodeError, OperandReader, WordWriter};
use crate::binary::{DecodeError, OperandReader, WordWriter};
use crate::dis::ResolvedIdName;
use crate::meta::{Category, OperandKind};
use crate::operand::{Operand, OperandDisContext, OperandEncoding};
Expand Down Expand Up @@ -55,9 +55,8 @@ unsafe impl OperandEncoding for IdResult {
const FIXED_LEN: Option<usize> = Some(1);

#[inline]
fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
writer.write(self.0);
Ok(())
}

#[inline]
Expand Down Expand Up @@ -143,9 +142,8 @@ macro_rules! id_ref {
const FIXED_LEN: Option<usize> = Some(1);

#[inline]
fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
writer.write(self.0.0);
Ok(())
}

#[inline]
Expand Down
5 changes: 2 additions & 3 deletions crates/rspirv2-types/src/operand/literal_const.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Word;
use crate::binary::{DecodeError, DecodeErrorKind, EncodeError, OperandReader, WordWriter};
use crate::binary::{DecodeError, DecodeErrorKind, OperandReader, WordWriter};
use crate::meta::{Category, OperandKind};
use crate::operand::{Operand, OperandDisContext, OperandEncoding};
use anstyle::AnsiColor;
Expand Down Expand Up @@ -178,9 +178,8 @@ unsafe impl OperandEncoding for LiteralConst {
}

#[inline]
fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
writer.write_iter(self.0.iter().copied());
Ok(())
}

#[inline]
Expand Down
5 changes: 2 additions & 3 deletions crates/rspirv2-types/src/operand/literal_float.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Word;
use crate::binary::{DecodeError, EncodeError, OperandReader, WordWriter};
use crate::binary::{DecodeError, OperandReader, WordWriter};
use crate::meta::{Category, OperandKind};
use crate::operand::{Operand, OperandDisContext, OperandEncoding};
use anstyle::AnsiColor;
Expand Down Expand Up @@ -57,9 +57,8 @@ unsafe impl OperandEncoding for LiteralFloat {
const FIXED_LEN: Option<usize> = Some(1);

#[inline]
fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
writer.write(self.0);
Ok(())
}

#[inline]
Expand Down
5 changes: 2 additions & 3 deletions crates/rspirv2-types/src/operand/literal_integer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Word;
use crate::binary::{DecodeError, EncodeError, OperandReader, WordWriter};
use crate::binary::{DecodeError, OperandReader, WordWriter};
use crate::meta::{Category, OperandKind};
use crate::operand::{Operand, OperandDisContext, OperandEncoding};
use anstyle::AnsiColor;
Expand Down Expand Up @@ -89,9 +89,8 @@ unsafe impl OperandEncoding for LiteralInteger {
const FIXED_LEN: Option<usize> = Some(1);

#[inline]
fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
writer.write(self.0);
Ok(())
}

#[inline]
Expand Down
7 changes: 3 additions & 4 deletions crates/rspirv2-types/src/operand/literal_string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Word;
use crate::binary::{DecodeError, DecodeErrorKind, EncodeError, OperandReader, WordWriter};
use crate::binary::{DecodeError, DecodeErrorKind, OperandReader, WordWriter};
use crate::dis::escape_cow_str;
use crate::meta::{Category, OperandKind};
use crate::operand::{Operand, OperandDisContext, OperandEncoding};
Expand Down Expand Up @@ -46,14 +46,13 @@ unsafe impl OperandEncoding for LiteralString {
(self.0.len() + 1).div_ceil(4)
}

fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> {
fn encode(&self, writer: &mut impl WordWriter) {
profiling::function_scope!();
let (chunks, remainder) = self.0.as_bytes().as_chunks::<4>();
let mut last = [0; 4];
last[..remainder.len()].copy_from_slice(remainder);
writer.write_iter(chunks.iter().copied().map(Word::from_le_bytes));
writer.write(Word::from_le_bytes(last));
Ok(())
}

fn decode(reader: &mut OperandReader<'_>) -> Result<Self, DecodeError> {
Expand Down Expand Up @@ -122,7 +121,7 @@ mod tests {

fn roundtrip(str: &str, expected_spirv: &[[u8; 4]]) -> anyhow::Result<()> {
let mut spirv = Vec::<Word>::default();
LiteralString(str.to_string()).encode(&mut spirv)?;
LiteralString(str.to_string()).encode(&mut spirv);
let read = LiteralString::decode(&mut OperandReader::new(spirv.as_slice()))?;
assert_eq!(str, read.as_str());
assert_eq!(
Expand Down
Loading
Loading