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 @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- [PR#62](https://github.com/Rust-GPU/rspirv2/pull/62) `Word` is now an alias for `u32`, makes many `Word(id)` and `word.0` unnecessary
- [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`
Expand Down
14 changes: 7 additions & 7 deletions crates/grammar-parser/src/codegen/operands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ fn emit_rust_like_enum(
if !param_symbols.is_empty() {
quote! {
Self::#symbol (#(#param_symbols),*) => {
writer.write(Word(#value));
writer.write(#value);
#(OperandEncoding::encode(#param_symbols, &mut *writer));*
}
}
} else {
quote!(Self::#symbol => writer.write(Word(#value)))
quote!(Self::#symbol => writer.write(#value))
}
});

Expand Down Expand Up @@ -143,7 +143,7 @@ fn emit_rust_like_enum(

fn decode(reader: &mut OperandReader<'_>) -> Result<Self, DecodeError> {
profiling::function_scope!();
let variant = reader.pull()?.0;
let variant = reader.pull()?;
Ok(match variant {
#(#decode,)*
_ => return Err(DecodeErrorKind::UnknownEnumVariant {
Expand Down Expand Up @@ -235,12 +235,12 @@ fn emit_c_like_enum(operand_kind: &OperandKind<'_>, enumerants: &[Enumerant<'_>]

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

fn decode(reader: &mut OperandReader<'_>) -> Result<Self, DecodeError> {
profiling::function_scope!();
let variant = reader.pull()?.0;
let variant = reader.pull()?;
Ok(match variant {
#(#decode,)*
_ => return Err(DecodeErrorKind::UnknownEnumVariant {
Expand Down Expand Up @@ -358,12 +358,12 @@ fn emit_bitflags_enum_common(

fn encode(&self, writer: &mut impl WordWriter) {
profiling::function_scope!();
writer.write(Word(self.bits()));
writer.write(self.bits());
}

fn decode(reader: &mut OperandReader<'_>) -> Result<Self, DecodeError> {
profiling::function_scope!();
let bits = reader.pull()?.0;
let bits = reader.pull()?;
Self::from_bits(bits).ok_or(DecodeError::invalid_bitflags::<#name>(stringify!(#name), bits))
}

Expand Down
3 changes: 1 addition & 2 deletions crates/rspirv2-types/src/binary/id_alloc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::Word;
use crate::operand::IdResult;
use std::sync::Arc;
use std::sync::atomic::AtomicU32;
Expand Down Expand Up @@ -27,7 +26,7 @@ impl IdResultAlloc {
pub fn alloc_id(&mut self) -> IdResult {
let id = self.0.fetch_add(1, Relaxed);
if id <= ID_RESULT_MAX {
IdResult(Word(id))
IdResult(id)
} else {
panic!("Out of IdResults")
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspirv2-types/src/binary/reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Word;
use crate::binary::{DecodeError, DecodeErrorKind};
use crate::meta::InstMeta;
use crate::{Word, WordExt};
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::ops::{Deref, DerefMut};
Expand Down
2 changes: 1 addition & 1 deletion crates/rspirv2-types/src/binary/writer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Word;
use crate::{Word, WordExt};
use smallvec::SmallVec;
use std::ops::{Deref, DerefMut};

Expand Down
2 changes: 1 addition & 1 deletion crates/rspirv2-types/src/dis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl Display for ResolvedIdName<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ResolvedIdName::Named(name) => write!(f, "{name}"),
ResolvedIdName::Raw(id) => write!(f, "{}", id.0.0),
ResolvedIdName::Raw(id) => write!(f, "{}", id.0),
}
}
}
Expand Down
45 changes: 19 additions & 26 deletions crates/rspirv2-types/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::ops::Deref;

pub const SPIRV_MAGIC: Word = Word(0x07230203);
pub const SPIRV_MAGIC: Word = 0x07230203;

/// The version of the SPIR-V module
#[repr(C, align(4))]
Expand Down Expand Up @@ -35,21 +35,19 @@ impl SpirvVersion {
#[inline]
pub fn from_word(value: Word) -> Self {
Self {
reserved0: (value.0 >> 24) as u8,
major: (value.0 >> 16) as u8,
minor: (value.0 >> 8) as u8,
reserved1: value.0 as u8,
reserved0: (value >> 24) as u8,
major: (value >> 16) as u8,
minor: (value >> 8) as u8,
reserved1: value as u8,
}
}

#[inline]
pub fn to_word(self) -> Word {
Word(
(self.reserved0 as u32) << 24
| (self.major as u32) << 16
| (self.minor as u32) << 8
| (self.reserved1 as u32),
)
(self.reserved0 as u32) << 24
| (self.major as u32) << 16
| (self.minor as u32) << 8
| (self.reserved1 as u32)
}

pub fn validate(self) -> Result<(), ParseError> {
Expand Down Expand Up @@ -89,7 +87,7 @@ impl SpirvHeader {
version,
generator_magic,
bound,
reserved: Word(0),
reserved: 0,
}
}

Expand Down Expand Up @@ -122,7 +120,7 @@ impl SpirvHeader {
return Err(ParseError::MismatchedMagic(self.magic));
}
self.version.validate()?;
if self.reserved != Word(0) {
if self.reserved != 0 {
return Err(ParseError::HeaderReservedNotZero(self));
}
Ok(())
Expand Down Expand Up @@ -172,7 +170,7 @@ impl<ISA: InstEncoding> Module<ISA> {
}

let magic = Word::from_le_bytes(chunks[0]);
let requires_swap = magic == Word(SPIRV_MAGIC.0.swap_bytes());
let requires_swap = magic == SPIRV_MAGIC.swap_bytes();
if (magic != SPIRV_MAGIC) && !requires_swap {
return Err(ParseError::MismatchedMagic(magic));
}
Expand Down Expand Up @@ -322,12 +320,7 @@ mod tests {
#[test]
fn test_spirv_header_roundtrip() {
let test = |array: [u32; 5]| {
assert_eq!(
SpirvHeader::from_array(array.map(Word))
.to_array()
.map(|v| v.0),
array
);
assert_eq!(SpirvHeader::from_array(array).to_array(), array);
};
test([0, 1, 2, 3, 5]);
test([0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555]);
Expand All @@ -337,20 +330,20 @@ mod tests {

#[test]
fn test_spirv_header_validate() {
let valid = SpirvHeader::new(SpirvVersion::new(1, 6), Word(0), Word(0));
let valid = SpirvHeader::new(SpirvVersion::new(1, 6), 0, 0);
assert_eq!(valid.validate(), Ok(()));

let mut wrong_magic = valid;
wrong_magic.magic = Word(12345678);
wrong_magic.magic = 12345678;
assert_eq!(
wrong_magic.validate(),
Err(ParseError::MismatchedMagic(Word(12345678)))
Err(ParseError::MismatchedMagic(12345678))
);
}

#[test]
fn test_from_bytes() {
let valid = SpirvHeader::new(SpirvVersion::new(1, 6), Word(0), Word(0));
let valid = SpirvHeader::new(SpirvVersion::new(1, 6), 0, 0);
assert_eq!(valid.validate(), Ok(()));

let test = |bytes: &[u8]| {
Expand All @@ -362,13 +355,13 @@ mod tests {
let ne_bytes = valid
.to_array()
.iter()
.flat_map(|v| v.0.to_ne_bytes())
.flat_map(|v| v.to_ne_bytes())
.collect::<Vec<_>>();
test(&ne_bytes);
let re_bytes = valid
.to_array()
.iter()
.flat_map(|v| v.0.swap_bytes().to_ne_bytes())
.flat_map(|v| v.swap_bytes().to_ne_bytes())
.collect::<Vec<_>>();
test(&re_bytes);
}
Expand Down
40 changes: 39 additions & 1 deletion crates/rspirv2-types/src/operand/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ unsafe impl bytemuck::Zeroable for IdResult {}
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Pod for IdResult {}

impl From<u32> for IdResult {
#[inline]
fn from(value: u32) -> Self {
Self(value)
}
}

impl IdResult {
#[inline]
pub fn as_u32(&self) -> u32 {
self.0
}

#[inline]
pub fn as_usize(&self) -> usize {
self.0 as usize
}
}

unsafe impl Operand for IdResult {
const KIND: &OperandKind = &OPERAND_KIND_ID_RESULT;
}
Expand Down Expand Up @@ -96,7 +115,7 @@ impl Display for IdResultWriter<'_> {
let name = ctx.id_to_name(id_result);
let name_len = match name {
ResolvedIdName::Named(name) => name.len(),
ResolvedIdName::Raw(id) => id.0.0.checked_ilog10().unwrap_or(1) as usize + 1,
ResolvedIdName::Raw(id) => id.0.checked_ilog10().unwrap_or(1) as usize + 1,
};
let pad_len = ctx.padding.len().saturating_sub(name_len + 4);
let style = ctx.color(ID_RESULT_COLOR);
Expand Down Expand Up @@ -134,6 +153,25 @@ macro_rules! id_ref {
}
}

impl From<u32> for $name {
#[inline]
fn from(value: u32) -> Self {
Self(IdResult(value))
}
}

impl $name {
#[inline]
pub fn as_u32(&self) -> u32 {
self.0.0
}

#[inline]
pub fn as_usize(&self) -> usize {
self.0.0 as usize
}
}

unsafe impl Operand for $name {
const KIND: &OperandKind = &$kind;
}
Expand Down
11 changes: 4 additions & 7 deletions crates/rspirv2-types/src/operand/literal_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,14 @@ impl LiteralConst {
impl From<u64> for LiteralConst {
#[inline]
fn from(value: u64) -> Self {
LiteralConst(SmallVec::from_slice(&[
Word(value as u32),
Word((value >> 32) as u32),
]))
LiteralConst(SmallVec::from_slice(&[value as u32, (value >> 32) as u32]))
}
}

impl From<u32> for LiteralConst {
#[inline]
fn from(value: u32) -> Self {
LiteralConst(SmallVec::from_slice(&[Word(value)]))
LiteralConst(SmallVec::from_slice(&[value]))
}
}

Expand Down Expand Up @@ -105,7 +102,7 @@ impl_float!(f64);
impl LiteralConst {
pub fn as_u64(&self) -> Result<u64, DecodeError> {
if self.0.len() == 2 {
Ok(self.0[0].0 as u64 | (self.0[1].0 as u64) << 32)
Ok(self.0[0] as u64 | (self.0[1] as u64) << 32)
} else {
Err(DecodeErrorKind::LiteralConstOfWrongWordSize {
expected_size: 2,
Expand All @@ -117,7 +114,7 @@ impl LiteralConst {

pub fn as_u32(&self) -> Result<u32, DecodeError> {
if self.0.len() == 1 {
Ok(self.0[0].0)
Ok(self.0[0])
} else {
Err(DecodeErrorKind::LiteralConstOfWrongWordSize {
expected_size: 1,
Expand Down
4 changes: 2 additions & 2 deletions crates/rspirv2-types/src/operand/literal_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ unsafe impl bytemuck::Pod for LiteralFloat {}
impl LiteralFloat {
#[inline]
pub fn new(value: f32) -> Self {
Self(Word(value.to_bits()))
Self(value.to_bits())
}

#[inline]
Expand All @@ -45,7 +45,7 @@ impl LiteralFloat {

#[inline]
pub fn to_f32(&self) -> f32 {
f32::from_bits(self.0.0)
f32::from_bits(self.0)
}
}

Expand Down
20 changes: 5 additions & 15 deletions crates/rspirv2-types/src/operand/literal_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,24 @@ unsafe impl bytemuck::Zeroable for LiteralInteger {}
unsafe impl bytemuck::Pod for LiteralInteger {}

impl LiteralInteger {
#[inline]
pub fn from_word(value: Word) -> Self {
Self(value)
}

#[inline]
pub fn new(value: u32) -> Self {
Self(Word(value))
Self(value)
}

#[inline]
pub fn from_bool(value: bool) -> Self {
Self(Word(value as u32))
}

#[inline]
pub fn to_word(&self) -> Word {
self.0
Self(value as u32)
}

#[inline]
pub fn to_u32(&self) -> u32 {
self.0.0
self.0
}

#[inline]
pub fn to_bool(&self) -> bool {
self.0.0 != 0
self.0 != 0
}
}

Expand All @@ -101,6 +91,6 @@ unsafe impl OperandEncoding for LiteralInteger {
#[inline]
fn dis_fmt(&self, f: &mut Formatter<'_>, ctx: OperandDisContext<'_>) -> std::fmt::Result {
let color = ctx.color(AnsiColor::Red.on_default());
write!(f, " {color}{}{color:#}", self.0.0)
write!(f, " {color}{}{color:#}", self.0)
}
}
2 changes: 1 addition & 1 deletion crates/rspirv2-types/src/operand/parameterized_bitmask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<T: ParameterizedBitmaskBits> ParameterizedBitmask<T> {
if !self.bits.contains(mask) {
// insert
self.bits.insert(mask);
self.extra.insert_many(offset, (0..len).map(|_| Word(0)));
self.extra.insert_many(offset, (0..len).map(|_| 0));
}
let mut writer = WordSliceWriter::new(&mut self.extra[offset..offset + len]);
param.encode(&mut writer);
Expand Down
Loading
Loading