First bits of IR validation

This commit is contained in:
Dzmitry Malyshau
2020-06-24 22:55:46 -04:00
committed by Dzmitry Malyshau
parent 84615eead3
commit 6eecea5dad
4 changed files with 72 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
use std::{fmt, hash, marker::PhantomData, num::NonZeroU32};
use std::{cmp::Ordering, fmt, hash, marker::PhantomData, num::NonZeroU32};
/// An unique index in the arena array that a handle points to.
///
@@ -29,6 +29,16 @@ impl<T> PartialEq for Handle<T> {
}
}
impl<T> Eq for Handle<T> {}
impl<T> PartialOrd for Handle<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.index.partial_cmp(&other.index)
}
}
impl<T> Ord for Handle<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.index.cmp(&other.index)
}
}
impl<T> fmt::Debug for Handle<T> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "Handle({})", self.index)

View File

@@ -1,4 +1,6 @@
mod interface;
mod typifier;
mod validator;
pub use typifier::{check_constant_types, ResolveError, Typifier, UnexpectedConstantTypeError};
pub use validator::{ValidationError, Validator};

56
src/proc/validator.rs Normal file
View File

@@ -0,0 +1,56 @@
use crate::arena::Handle;
pub struct Validator {}
#[derive(Clone, Debug)]
pub enum ValidationError {
/// The type width is not supported.
InvalidTypeWidth(crate::ScalarKind, crate::Bytes),
/// A type handle can not be resolved.
UnresolvedType(Handle<crate::Type>),
/// There are instructions after `return`/`break`/`continue`.
InvalidControlFlowExitTail,
}
impl Validator {
pub fn new() -> Self {
Validator {}
}
pub fn validate(&mut self, module: &crate::Module) -> Result<(), ValidationError> {
for (handle, ty) in module.types.iter() {
use crate::TypeInner as Ti;
match ty.inner {
Ti::Scalar { kind, width }
| Ti::Vector { kind, width, .. }
| Ti::Matrix { kind, width, .. } => {
if width != 32 {
return Err(ValidationError::InvalidTypeWidth(kind, width));
}
}
Ti::Pointer { base, class: _ } => {
if base >= handle {
return Err(ValidationError::UnresolvedType(base));
}
}
Ti::Array { base, .. } => {
if base >= handle {
return Err(ValidationError::UnresolvedType(base));
}
}
Ti::Struct { ref members } => {
//TODO: check that offsets are not intersecting?
for member in members {
if member.ty >= handle {
return Err(ValidationError::UnresolvedType(member.ty));
}
}
}
Ti::Image { .. } => {}
Ti::Sampler { comparison: _ } => {}
}
}
Ok(())
}
}

View File

@@ -20,6 +20,7 @@ fn load_glsl(name: &str, entry: &str, exec: ExecutionModel) -> naga::Module {
#[test]
fn convert_quad() {
let module = load_wgsl("quad.wgsl");
naga::proc::Validator::new().validate(&module).unwrap();
{
use naga::back::msl;
let mut binding_map = msl::BindingMap::default();
@@ -51,6 +52,7 @@ fn convert_quad() {
#[test]
fn convert_boids() {
let module = load_wgsl("boids.wgsl");
naga::proc::Validator::new().validate(&module).unwrap();
{
use naga::back::msl;
let mut binding_map = msl::BindingMap::default();
@@ -93,6 +95,7 @@ fn convert_boids() {
#[ignore]
fn convert_phong_lighting() {
let module = load_glsl("glsl_phong_lighting.frag", "main", ExecutionModel::Fragment);
naga::proc::Validator::new().validate(&module).unwrap();
let header = naga::Header {
version: (1, 0, 0),