Skip to content

Commit

Permalink
address some of the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Sep 19, 2023
1 parent d44e58d commit 5a849f4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ impl<'a> Error<'a> {
notes: vec![],
},
Error::ExpectedConstExprConcreteIntegerScalar(span) => ParseError {
message: "must be a const-expression that resolves to a concrete integer scalar (u32, i32)".to_string(),
message: "must be a const-expression that resolves to a concrete integer scalar (u32 or i32)".to_string(),
labels: vec![(span, "must resolve to u32 or i32".into())],
notes: vec![],
},
Expand Down
6 changes: 1 addition & 5 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,7 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
}),
};

let res = eval
.try_eval_and_append(&expr, span)
.map_err(|e| Error::ConstantEvaluatorError(e, span));

match res {
match eval.try_eval_and_append(&expr, span) {
Ok(expr) => Ok(expr),
Err(_) => Ok(rctx.naga_expressions.append(expr, span)),
}
Expand Down
59 changes: 26 additions & 33 deletions src/proc/constant_evaluator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
arena::{Arena, Handle, UniqueArena},
ArraySize, BinaryOperator, Constant, Expression, FastHashMap, Literal, ScalarKind, Span, Type,
TypeInner, UnaryOperator,
ArraySize, BinaryOperator, Constant, Expression, Literal, ScalarKind, Span, Type, TypeInner,
UnaryOperator,
};

#[derive(Debug)]
Expand All @@ -23,13 +23,6 @@ pub struct ConstantEvaluatorEmitter<'a> {
pub block: &'a mut crate::Block,
}

#[derive(Debug)]
pub struct EvalData {
pub types: UniqueArena<Type>,
pub evald_expressions: Arena<Expression>,
pub mapping: FastHashMap<Handle<Expression>, Handle<Expression>>,
}

#[derive(Clone, Debug, PartialEq, thiserror::Error)]
pub enum ConstantEvaluatorError {
#[error("Constants cannot access function arguments")]
Expand Down Expand Up @@ -149,17 +142,17 @@ impl ConstantEvaluator<'_> {
log::trace!("try_eval_and_append: {:?}", expr);
match *expr {
Expression::Literal(_) | Expression::ZeroValue(_) | Expression::Constant(_) => {
Ok(self.register_constant(expr.clone(), span))
Ok(self.register_evaluated_expr(expr.clone(), span))
}
Expression::Compose { ref components, .. } => {
for component in components {
self.check_and_get(*component)?;
}
Ok(self.register_constant(expr.clone(), span))
Ok(self.register_evaluated_expr(expr.clone(), span))
}
Expression::Splat { value, .. } => {
self.check_and_get(value)?;
Ok(self.register_constant(expr.clone(), span))
Ok(self.register_evaluated_expr(expr.clone(), span))
}
Expression::AccessIndex { base, index } => {
let base = self.check_and_get(base)?;
Expand Down Expand Up @@ -266,7 +259,7 @@ impl ConstantEvaluator<'_> {
ty,
components: vec![value; size as usize],
};
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
Expression::ZeroValue(ty) => {
let inner = match self.types[ty].inner {
Expand All @@ -275,7 +268,7 @@ impl ConstantEvaluator<'_> {
};
let res_ty = self.types.insert(Type { name: None, inner }, span);
let expr = Expression::ZeroValue(res_ty);
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
_ => Err(ConstantEvaluatorError::SplatScalarOnly),
}
Expand Down Expand Up @@ -307,11 +300,11 @@ impl ConstantEvaluator<'_> {
Expression::ZeroValue(ty) => {
let dst_ty = get_dst_ty(ty)?;
let expr = Expression::ZeroValue(dst_ty);
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
Expression::Splat { value, .. } => {
let expr = Expression::Splat { size, value };
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
Expression::Compose {
ty,
Expand All @@ -328,7 +321,7 @@ impl ConstantEvaluator<'_> {
ty: dst_ty,
components,
};
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
_ => Err(ConstantEvaluatorError::SwizzleVectorOnly),
}
Expand Down Expand Up @@ -363,7 +356,7 @@ impl ConstantEvaluator<'_> {
};

let expr = Expression::Literal(literal);
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
crate::MathFunction::Clamp => {
let literal = match (const0, const1.unwrap(), const2.unwrap()) {
Expand Down Expand Up @@ -391,7 +384,7 @@ impl ConstantEvaluator<'_> {
};

let expr = Expression::Literal(literal);
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
_ => Err(ConstantEvaluatorError::NotImplemented(format!("{fun:?}"))),
}
Expand All @@ -408,7 +401,7 @@ impl ConstantEvaluator<'_> {
TypeInner::Array { size, .. } => match size {
crate::ArraySize::Constant(len) => {
let expr = Expression::Literal(Literal::U32(len.get()));
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
crate::ArraySize::Dynamic => {
Err(ConstantEvaluatorError::ArrayLengthDynamic)
Expand Down Expand Up @@ -446,7 +439,7 @@ impl ConstantEvaluator<'_> {
self.types.insert(Type { name: None, inner }, span)
}
};
Ok(self.register_constant(Expression::ZeroValue(ty), span))
Ok(self.register_evaluated_expr(Expression::ZeroValue(ty), span))
}
}
Expression::Splat { size, value } => {
Expand Down Expand Up @@ -502,7 +495,7 @@ impl ConstantEvaluator<'_> {
Literal::zero(kind, width)
.ok_or(ConstantEvaluatorError::TypeNotConstructible)?,
);
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
TypeInner::Vector { size, kind, width } => {
let scalar_ty = self.types.insert(
Expand All @@ -517,7 +510,7 @@ impl ConstantEvaluator<'_> {
ty,
components: vec![el; size as usize],
};
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
TypeInner::Matrix {
columns,
Expand All @@ -540,7 +533,7 @@ impl ConstantEvaluator<'_> {
ty,
components: vec![el; columns as usize],
};
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
TypeInner::Array {
base,
Expand All @@ -552,7 +545,7 @@ impl ConstantEvaluator<'_> {
ty,
components: vec![el; size.get() as usize],
};
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
TypeInner::Struct { ref members, .. } => {
let types: Vec<_> = members.iter().map(|m| m.ty).collect();
Expand All @@ -561,7 +554,7 @@ impl ConstantEvaluator<'_> {
components.push(self.eval_zero_value_impl(ty, span)?);
}
let expr = Expression::Compose { ty, components };
Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}
_ => Err(ConstantEvaluatorError::TypeNotConstructible),
}
Expand Down Expand Up @@ -647,7 +640,7 @@ impl ConstantEvaluator<'_> {
_ => return Err(ConstantEvaluatorError::InvalidCastArg),
};

Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}

fn unary_op(
Expand Down Expand Up @@ -691,7 +684,7 @@ impl ConstantEvaluator<'_> {
_ => return Err(ConstantEvaluatorError::InvalidUnaryOpArg),
};

Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}

fn binary_op(
Expand Down Expand Up @@ -791,7 +784,7 @@ impl ConstantEvaluator<'_> {
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs),
};

Ok(self.register_constant(expr, span))
Ok(self.register_evaluated_expr(expr, span))
}

/// Deep copy `expr` from `expressions` into `self.expressions`.
Expand All @@ -810,17 +803,17 @@ impl ConstantEvaluator<'_> {
match expressions[expr] {
ref expr @ (Expression::Literal(_)
| Expression::Constant(_)
| Expression::ZeroValue(_)) => Ok(self.register_constant(expr.clone(), span)),
| Expression::ZeroValue(_)) => Ok(self.register_evaluated_expr(expr.clone(), span)),
Expression::Compose { ty, ref components } => {
let mut components = components.clone();
for component in &mut components {
*component = self.copy_from(*component, expressions)?;
}
Ok(self.register_constant(Expression::Compose { ty, components }, span))
Ok(self.register_evaluated_expr(Expression::Compose { ty, components }, span))
}
Expression::Splat { size, value } => {
let value = self.copy_from(value, expressions)?;
Ok(self.register_constant(Expression::Splat { size, value }, span))
Ok(self.register_evaluated_expr(Expression::Splat { size, value }, span))
}
_ => {
log::debug!("copy_from: SubexpressionsAreNotConstant");
Expand All @@ -829,7 +822,7 @@ impl ConstantEvaluator<'_> {
}
}

fn register_constant(&mut self, expr: Expression, span: Span) -> Handle<Expression> {
fn register_evaluated_expr(&mut self, expr: Expression, span: Span) -> Handle<Expression> {
if let Some(ref mut emitter) = self.emitter {
let is_running = emitter.emitter.is_running();
let needs_pre_emit = expr.needs_pre_emit();
Expand Down

0 comments on commit 5a849f4

Please sign in to comment.