Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP add support for parsing Op::AtomicLoad in spv frontend #2304

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 20 additions & 0 deletions src/front/spv/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,23 @@ pub(super) fn map_storage_class(word: spirv::Word) -> Result<super::ExtendedClas
_ => return Err(Error::UnsupportedStorageClass(word)),
})
}

impl crate::AddressSpace {
pub(super) const fn try_from_spirv_semantics_and_scope(
semantics: spirv::MemorySemantics,
scope: spirv::Scope,
) -> Result<Self, Error> {
match (semantics, scope) {
(spirv::MemorySemantics::UNIFORM_MEMORY, spirv::Scope::Device) => {
Ok(crate::AddressSpace::Storage { access: crate::StorageAccess::all() })
}
(spirv::MemorySemantics::WORKGROUP_MEMORY, spirv::Scope::Workgroup) => {
Ok(crate::AddressSpace::WorkGroup)
}
(spirv::MemorySemantics::NONE, spirv::Scope::Invocation) => {
Ok(crate::AddressSpace::Private)
}
_ => Err(Error::UnsupportedMemoryScopeSemantics(scope, semantics)),
}
}
}
8 changes: 7 additions & 1 deletion src/front/spv/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::ModuleState;
use crate::arena::Handle;
use crate::{arena::Handle, ScalarKind};

#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand Down Expand Up @@ -50,6 +50,8 @@ pub enum Error {
rows: u8,
width: u8,
},
#[error("unsupported memory scope {0:?} and/or semantics {1:?}")]
UnsupportedMemoryScopeSemantics(spirv::Scope, spirv::MemorySemantics),
#[error("unknown binary operator {0:?}")]
UnknownBinaryOperator(spirv::Op),
#[error("unknown relational function {0:?}")]
Expand Down Expand Up @@ -92,6 +94,10 @@ pub enum Error {
InvalidAsType(Handle<crate::Type>),
#[error("invalid vector type {0:?}")]
InvalidVectorType(Handle<crate::Type>),
#[error("invalid atomic pointer of type {0:?}")]
InvalidAtomicScalarKind( ScalarKind ),
#[error("invalid atomic pointer of type {0:?}")]
InvalidAtomicPointer( Handle<crate::Type> ),
#[error("inconsistent comparison sampling {0:?}")]
InconsistentComparisonSampling(Handle<crate::GlobalVariable>),
#[error("wrong function result type %{0}")]
Expand Down
73 changes: 73 additions & 0 deletions src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,45 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
log::debug!("\t\t{:?} [{}]", inst.op, inst.wc);

match inst.op {
Op::AtomicLoad => {
// parse all the parameters
inst.expect(6)?;
let type_id = self.next()?;
let id = self.next()?;
let pointer_id = self.next()?;
let scope = self.next()?;
let semantics = self.next()?;
// convert raw ids into spirv
let scope = spirv::Scope::from_u32(scope)
.ok_or_else(|| Error::InvalidBarrierScope(scope))?;
let semantics = spirv::MemorySemantics::from_bits(semantics)
.ok_or_else(|| Error::InvalidBarrierMemorySemantics(semantics))?;
// type check the pointer we are loading
let pointer_lexp = self.lookup_expression.lookup(pointer_id)?.clone();
let pointer_handle = self.get_expr_handle(
pointer_id,
&pointer_lexp,
ctx,
&mut emitter,
&mut block,
body_idx,
);
let (kind, width) = self.ensure_atomic_pointer(&pointer_lexp, ctx, span)?;
// convert scope and semantics into an address space...
let space =
crate::AddressSpace::try_from_spirv_semantics_and_scope(semantics, scope)?;
let atomic_type = crate::TypeInner::Atomic { kind, width };
let expr = crate::Expression::Load {
pointer: pointer_handle,
};
let handle = ctx.expressions.append(expr, span);
let expr = LookupExpression {
handle,
type_id,
block_id,
};
self.lookup_expression.insert(id, expr);
}
Op::Line => {
inst.expect(4)?;
let _file_id = self.next()?;
Expand Down Expand Up @@ -3563,6 +3602,40 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
Ok(())
}

fn ensure_atomic_pointer(
&mut self,
pointer_lexp: &LookupExpression,
ctx: &mut BlockContext,
span: crate::Span,
) -> Result<(crate::ScalarKind, u8), Error> {
let pointer_lookup = self.lookup_type.lookup(pointer_lexp.type_id)?;
let pointer_type = ctx
.type_arena
.get_handle(pointer_lookup.handle)
.map_err(|h| Error::UnsupportedType(pointer_lookup.handle))?;
match pointer_type.inner {
crate::TypeInner::Pointer { base, .. } => match ctx.type_arena[base].inner {
crate::TypeInner::Scalar { kind, width } => match kind {
crate::ScalarKind::Uint
| crate::ScalarKind::Sint
| crate::ScalarKind::Float => Ok((kind, width)),
_ => {
log::error!("Pointer type to {:?} passed to atomic op", kind);
Err(Error::InvalidAtomicScalarKind(kind))
}
},
ref other => {
log::error!("Pointer type to {:?} passed to atomic op", other);
Err(Error::InvalidAtomicPointer(pointer_lookup.handle))
}
},
ref other => {
log::error!("Type {:?} passed to atomic op", other);
Err(Error::InvalidAtomicPointer(pointer_lookup.handle))
}
}
}

fn make_expression_storage(
&mut self,
globals: &Arena<crate::GlobalVariable>,
Expand Down
Binary file added tests/in/compute_atomics.spv
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/spirv-frontend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Tests for the spv frontend.

#[test]
fn parse_spv_with_atomics() {
env_logger::Builder::default()
.is_test(true)
.filter_level(log::LevelFilter::Trace)
.init();
let source = include_bytes!("in/compute_atomics.spv");
let opts = naga::front::spv::Options::default();
match naga::front::spv::parse_u8_slice(source, &opts) {
Ok(_) => {}
Err(e) => {
panic!("{e:#?}");
}
}
}