Skip to content

Commit

Permalink
Spirv v0.3 compatibility fix (#131)
Browse files Browse the repository at this point in the history
* Fix compatibility with spirv v0.3

* Remove part of num_traits usage

* fmt
  • Loading branch information
PENGUINLIONG authored Mar 13, 2024
1 parent c598eab commit 276940d
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 329 deletions.
488 changes: 173 additions & 315 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion spirq-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ maintenance = { status = "actively-developed" }

[dependencies]
fnv = "1.0.7"
spirv = ">=0.2"
spirv = ">=0.3"
anyhow = "1.0"
ordered-float = "3.4"
num-traits = "0.2"
Expand Down
5 changes: 0 additions & 5 deletions spirq-core/src/parse/instr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! SPIR-V instruction parser.
use anyhow::bail;
use num_traits::FromPrimitive;
use spirv::Op;
use std::{borrow::Borrow, fmt, ops::Deref};

Expand Down Expand Up @@ -224,10 +223,6 @@ impl<'a> Operands<'a> {
let cstr = CStr::from_bytes_until_nul(bytes)?;
Ok(cstr.to_str()?)
}
pub fn read_enum<E: FromPrimitive>(&mut self) -> Result<E> {
self.read_u32()
.and_then(|x| FromPrimitive::from_u32(x).ok_or(anyhow!("invalid enum value")))
}
pub fn read_list(&mut self) -> Result<&'a [u32]> {
let rv = self.0;
self.0 = &[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

out += [
"use anyhow::{bail, Result};",
"use num_traits::FromPrimitive;",
"use spirq_core::spirv::Op;",
"",
"fn unknown_decorate_parameter_index(decoration: u32, i: usize) -> Result<&'static str> {",
Expand Down
1 change: 0 additions & 1 deletion spirq-spvasm/src/generated/decorate_parameter_enum_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::{bail, Result};
use num_traits::FromPrimitive;
use spirq_core::spirv::Op;

fn unknown_decorate_parameter_index(decoration: u32, i: usize) -> Result<&'static str> {
Expand Down
9 changes: 7 additions & 2 deletions spirq-spvasm/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use pretty_assertions::assert_eq;
use spirq::spirv;
use spirq_core::parse::bin::SpirvHeader;

use crate::asm::Assembler;
Expand All @@ -8,7 +9,7 @@ use crate::dis::Disassembler;
fn test_asm_dis_roundtrip() {
let code = r#"
; SPIR-V
; Version: 1.5
; Version: SPIRV_VERSION
; Generator: 0; 0
; Bound: 13
; Schema: 0
Expand All @@ -26,8 +27,12 @@ fn test_asm_dis_roundtrip() {
%void_10 = OpTypeVoid
"#
.trim_start();
let code = code.replace(
"SPIRV_VERSION",
&format!("{}.{}", spirv::MAJOR_VERSION, spirv::MINOR_VERSION),
);
let header = SpirvHeader::default();
let spv = Assembler::new().assemble(code, header).unwrap();
let spv = Assembler::new().assemble(&code, header).unwrap();
let spvasm = Disassembler::new()
.name_type_ids(true)
.disassemble(&spv.into())
Expand Down
16 changes: 14 additions & 2 deletions spirq/src/instr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use spirq_core::error::anyhow;
use std::convert::TryFrom;

use crate::{parse::Instr, spirv::*};
Expand All @@ -13,7 +14,18 @@ type MemberIdx = u32;

#[macro_export]
macro_rules! define_ops {
($($opcode:ident { $($field:ident: $type:ty = $read_fn:ident(),)+ })+) => {
(read_enum: $type:ty: $operands:expr) => {
{
<$type>::from_u32($operands.read_u32()?)
.ok_or_else(|| anyhow!("invalid enum value"))?
}
};
($read_fn:ident: $type:ty: $operands:expr) => {
{
$operands.$read_fn()?
}
};
($($opcode:ident { $($field:ident: $type:ty = $read_fn:tt(),)+ })+) => {
$(
pub struct $opcode<'a> {
$( pub $field: $type, )*
Expand All @@ -24,7 +36,7 @@ macro_rules! define_ops {
fn try_from(instr: &'a Instr) -> ::spirq_core::error::Result<Self> {
let mut operands = instr.operands();
let op = $opcode {
$( $field: operands.$read_fn()?, )+
$( $field: define_ops!($read_fn: $type: operands), )+
_ph: ::std::marker::PhantomData,
};
Ok(op)
Expand Down
4 changes: 2 additions & 2 deletions spirq/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::BTreeMap;
use std::convert::TryFrom;

use fnv::{FnvHashMap as HashMap, FnvHashSet as HashSet};
use num_traits::FromPrimitive;
use spirq_core::parse::Instrs;

use crate::{
Expand Down Expand Up @@ -929,7 +928,8 @@ impl<'a> ReflectIntermediate<'a> {
};

let func_id = operands.read_u32()?;
let exec_mode = operands.read_enum::<spirv::ExecutionMode>()?;
let exec_mode = spirv::ExecutionMode::from_u32(operands.read_u32()?)
.ok_or_else(|| anyhow!("invalid execution mode"))?;
let operands = operands
.read_list()?
.into_iter()
Expand Down

0 comments on commit 276940d

Please sign in to comment.