Skip to content

Commit

Permalink
Rename some ConstantEvaluator-associated types and values. (#2516)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy authored Sep 27, 2023
1 parent 0d536b9 commit b7b69ee
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/back/spv/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl<'w> BlockContext<'w> {
crate::Expression::ZeroValue(_) => self.writer.get_constant_null(result_type_id),
crate::Expression::Compose { ty, ref components } => {
self.temp_list.clear();
if self.expression_constness.contains(expr_handle) {
if self.expression_constness.is_const(expr_handle) {
self.temp_list.extend(
crate::proc::flatten_compose(
ty,
Expand Down Expand Up @@ -274,7 +274,7 @@ impl<'w> BlockContext<'w> {
let value_id = self.cached[value];
let components = &[value_id; 4][..size as usize];

if self.expression_constness.contains(expr_handle) {
if self.expression_constness.is_const(expr_handle) {
let ty = self
.writer
.get_expression_lookup_type(&self.fun_info[expr_handle].ty);
Expand Down Expand Up @@ -1777,7 +1777,7 @@ impl<'w> BlockContext<'w> {
crate::Statement::Emit(ref range) => {
for handle in range.clone() {
// omit const expressions as we've already cached those
if !self.expression_constness.contains(handle) {
if !self.expression_constness.is_const(handle) {
self.cache_expression_value(handle, &mut block)?;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/back/spv/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ impl Writer {
context.cached.reset(ir_function.expressions.len());
for (handle, expr) in ir_function.expressions.iter() {
if (expr.needs_pre_emit() && !matches!(*expr, crate::Expression::LocalVariable(_)))
|| context.expression_constness.contains(handle)
|| context.expression_constness.is_const(handle)
{
context.cache_expression_value(handle, &mut prelude)?;
}
Expand Down
6 changes: 3 additions & 3 deletions src/front/glsl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ impl<'a> Context<'a> {
}

pub fn add_expression(&mut self, expr: Expression, meta: Span) -> Result<Handle<Expression>> {
let (expressions, extra_data) = if self.is_const {
let (expressions, function_info) = if self.is_const {
(&mut self.module.const_expressions, None)
} else {
(
&mut self.expressions,
Some(crate::proc::ConstantEvaluatorExtraData {
Some(crate::proc::FunctionLocalData {
const_expressions: &self.module.const_expressions,
expression_constness: &mut self.expression_constness,
emitter: &mut self.emitter,
Expand All @@ -266,7 +266,7 @@ impl<'a> Context<'a> {
types: &mut self.module.types,
constants: &self.module.constants,
expressions,
extra_data,
function_local_data: function_info,
};

let res = eval.try_eval_and_append(&expr, meta).map_err(|e| Error {
Expand Down
2 changes: 1 addition & 1 deletion src/front/glsl/parser/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl<'source> ParsingContext<'source> {
} else if ctx.external {
init.and_then(|expr| ctx.ctx.lift_up_const_expression(expr).ok())
} else {
init.filter(|expr| ctx.ctx.expression_constness.contains(*expr))
init.filter(|expr| ctx.ctx.expression_constness.is_const(*expr))
};

let pointer = ctx.add_var(frontend, ty, name, maybe_const_expr, meta)?;
Expand Down
10 changes: 5 additions & 5 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::front::wgsl::parse::number::Number;
use crate::front::wgsl::parse::{ast, conv};
use crate::front::Typifier;
use crate::proc::{
ensure_block_returns, Alignment, ConstantEvaluator, ConstantEvaluatorExtraData, Emitter,
Layouter, ResolveContext, TypeResolution,
ensure_block_returns, Alignment, ConstantEvaluator, Emitter, FunctionLocalData, Layouter,
ResolveContext, TypeResolution,
};
use crate::{Arena, FastHashMap, FastIndexMap, Handle, Span};

Expand Down Expand Up @@ -344,7 +344,7 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
types: &mut self.module.types,
constants: &self.module.constants,
expressions: rctx.naga_expressions,
extra_data: Some(ConstantEvaluatorExtraData {
function_local_data: Some(FunctionLocalData {
const_expressions: &self.module.const_expressions,
expression_constness: rctx.expression_constness,
emitter: rctx.emitter,
Expand All @@ -362,7 +362,7 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
types: &mut self.module.types,
constants: &self.module.constants,
expressions: &mut self.module.const_expressions,
extra_data: None,
function_local_data: None,
};

eval.try_eval_and_append(&expr, span)
Expand Down Expand Up @@ -1184,7 +1184,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {

let (const_initializer, initializer) = {
match initializer {
Some(init) if ctx.expression_constness.contains(init) => {
Some(init) if ctx.expression_constness.is_const(init) => {
(Some(init), None)
}
Some(init) => (None, Some(init)),
Expand Down
26 changes: 13 additions & 13 deletions src/proc/constant_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pub struct ConstantEvaluator<'a> {

/// When `self.expressions` refers to a function's local expression
/// arena, this needs to be populated
pub extra_data: Option<ConstantEvaluatorExtraData<'a>>,
pub function_local_data: Option<FunctionLocalData<'a>>,
}

#[derive(Debug)]
pub struct ConstantEvaluatorExtraData<'a> {
pub struct FunctionLocalData<'a> {
/// Global constant expressions
pub const_expressions: &'a Arena<Expression>,
/// Tracks the constness of expressions residing in `ConstantEvaluator.expressions`
Expand All @@ -41,7 +41,7 @@ impl ExpressionConstnessTracker {
self.inner.insert(value.index());
}

pub fn contains(&self, value: Handle<Expression>) -> bool {
pub fn is_const(&self, value: Handle<Expression>) -> bool {
self.inner.contains(value.index())
}

Expand All @@ -53,9 +53,9 @@ impl ExpressionConstnessTracker {
| crate::Expression::ZeroValue(_)
| crate::Expression::Constant(_) => true,
crate::Expression::Compose { ref components, .. } => {
components.iter().all(|h| tracker.contains(*h))
components.iter().all(|h| tracker.is_const(*h))
}
crate::Expression::Splat { value, .. } => tracker.contains(value),
crate::Expression::Splat { value, .. } => tracker.is_const(value),
_ => false,
};
if insert {
Expand Down Expand Up @@ -139,8 +139,8 @@ pub enum ConstantEvaluatorError {

impl ConstantEvaluator<'_> {
fn check(&self, expr: Handle<Expression>) -> Result<(), ConstantEvaluatorError> {
if let Some(ref extra_data) = self.extra_data {
if !extra_data.expression_constness.contains(expr) {
if let Some(ref extra_data) = self.function_local_data {
if !extra_data.expression_constness.is_const(expr) {
log::debug!("check: SubexpressionsAreNotConstant");
return Err(ConstantEvaluatorError::SubexpressionsAreNotConstant);
}
Expand All @@ -156,7 +156,7 @@ impl ConstantEvaluator<'_> {
Expression::Constant(c) => {
// Are we working in a function's expression arena, or the
// module's constant expression arena?
if let Some(ref extra_data) = self.extra_data {
if let Some(ref extra_data) = self.function_local_data {
// Deep-copy the constant's value into our arena.
self.copy_from(self.constants[c].init, extra_data.const_expressions)
} else {
Expand Down Expand Up @@ -870,12 +870,12 @@ impl ConstantEvaluator<'_> {
}

fn register_evaluated_expr(&mut self, expr: Expression, span: Span) -> Handle<Expression> {
if let Some(ConstantEvaluatorExtraData {
if let Some(FunctionLocalData {
ref mut emitter,
ref mut block,
ref mut expression_constness,
..
}) = self.extra_data
}) = self.function_local_data
{
let is_running = emitter.is_running();
let needs_pre_emit = expr.needs_pre_emit();
Expand Down Expand Up @@ -1060,7 +1060,7 @@ mod tests {
types: &mut types,
constants: &constants,
expressions: &mut const_expressions,
extra_data: None,
function_local_data: None,
};

let res1 = solver
Expand Down Expand Up @@ -1146,7 +1146,7 @@ mod tests {
types: &mut types,
constants: &constants,
expressions: &mut const_expressions,
extra_data: None,
function_local_data: None,
};

let res = solver
Expand Down Expand Up @@ -1264,7 +1264,7 @@ mod tests {
types: &mut types,
constants: &constants,
expressions: &mut const_expressions,
extra_data: None,
function_local_data: None,
};

let root1 = Expression::AccessIndex { base, index: 1 };
Expand Down
3 changes: 1 addition & 2 deletions src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ mod terminator;
mod typifier;

pub use constant_evaluator::{
ConstantEvaluator, ConstantEvaluatorError, ConstantEvaluatorExtraData,
ExpressionConstnessTracker,
ConstantEvaluator, ConstantEvaluatorError, ExpressionConstnessTracker, FunctionLocalData,
};
pub use emitter::Emitter;
pub use index::{BoundsCheckPolicies, BoundsCheckPolicy, IndexableLength, IndexableLengthError};
Expand Down

0 comments on commit b7b69ee

Please sign in to comment.