[resolver]: Begin constant value evaluation

Move the bulk of the constant evaulation logic out of transform::FoldConstants and into Resolver and sem::Expression.

transform::FoldConstants now replace TypeConstructor nodes that have a constant value on the expression.

This is ground work to:
* Cleaning up the HLSL uniform buffer indexing, which is `/` and `%` arithmatic heavy
* Prepares us to handle `constexpr` when it lands in the spec
* Provide a centralized place to do constant evaluation, instead of the
  having similar logic scattered around the codebase.

Change-Id: I3e2f542be692046a8d243b62a82556db519953e7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57426
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton
2021-07-13 12:18:13 +00:00
parent c6bcab02fd
commit 71f619b6f1
22 changed files with 992 additions and 398 deletions

View File

@@ -22,7 +22,8 @@ namespace sem {
Call::Call(const ast::Expression* declaration,
const CallTarget* target,
Statement* statement)
: Base(declaration, target->ReturnType(), statement), target_(target) {}
: Base(declaration, target->ReturnType(), statement, Constant{}),
target_(target) {}
Call::~Call() = default;

63
src/sem/constant.cc Normal file
View File

@@ -0,0 +1,63 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0(the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/sem/constant.h"
#include <utility>
#include "src/debug.h"
#include "src/program_builder.h"
#include "src/sem/type.h"
namespace tint {
namespace sem {
namespace {
const Type* ElemType(const Type* ty, size_t num_elements) {
diag::List diag;
if (ty->is_scalar()) {
if (num_elements != 1) {
TINT_ICE(Semantic, diag)
<< "sem::Constant() type <-> num_element mismatch. type: '"
<< ty->type_name() << "' num_elements: " << num_elements;
}
return ty;
}
if (auto* vec = ty->As<Vector>()) {
if (num_elements != vec->size()) {
TINT_ICE(Semantic, diag)
<< "sem::Constant() type <-> num_element mismatch. type: '"
<< ty->type_name() << "' num_elements: " << num_elements;
}
TINT_ASSERT(Semantic, vec->type()->is_scalar());
return vec->type();
}
TINT_UNREACHABLE(Semantic, diag) << "Unsupported sem::Constant type";
return nullptr;
}
} // namespace
Constant::Constant() {}
Constant::Constant(const sem::Type* ty, Scalars els)
: type_(ty), elem_type_(ElemType(ty, els.size())), elems_(std::move(els)) {}
Constant::Constant(const Constant&) = default;
Constant::~Constant() = default;
} // namespace sem
} // namespace tint

130
src/sem/constant.h Normal file
View File

@@ -0,0 +1,130 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0(the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_SEM_CONSTANT_H_
#define SRC_SEM_CONSTANT_H_
#include <vector>
#include "src/program_builder.h"
#include "src/sem/type.h"
namespace tint {
namespace sem {
/// A Constant is compile-time known expression value, expressed as a flattened
/// list of scalar values. Value may be of a scalar or vector type.
class Constant {
using i32 = ProgramBuilder::i32;
using u32 = ProgramBuilder::u32;
using f32 = ProgramBuilder::f32;
public:
/// Scalar holds a single constant scalar value, as a union of an i32, u32,
/// f32 or boolean.
union Scalar {
/// The scalar value as a i32
int32_t i32;
/// The scalar value as a u32
uint32_t u32;
/// The scalar value as a f32
float f32;
/// The scalar value as a bool
bool bool_;
/// Constructs the scalar with the i32 value `v`
/// @param v the value of the Scalar
Scalar(ProgramBuilder::i32 v) : i32(v) {} // NOLINT
/// Constructs the scalar with the u32 value `v`
/// @param v the value of the Scalar
Scalar(ProgramBuilder::u32 v) : u32(v) {} // NOLINT
/// Constructs the scalar with the f32 value `v`
/// @param v the value of the Scalar
Scalar(ProgramBuilder::f32 v) : f32(v) {} // NOLINT
/// Constructs the scalar with the bool value `v`
/// @param v the value of the Scalar
Scalar(bool v) : bool_(v) {} // NOLINT
};
/// Scalars is a list of scalar values
using Scalars = std::vector<Scalar>;
/// Constructs an invalid Constant
Constant();
/// Constructs a Constant of the given type and element values
/// @param ty the Constant type
/// @param els the Constant element values
Constant(const Type* ty, Scalars els);
/// Copy constructor
Constant(const Constant&);
/// Destructor
~Constant();
/// @returns true if the Constant has been initialized
bool IsValid() const { return type_ != nullptr; }
/// @return true if the Constant has been initialized
operator bool() const { return IsValid(); }
/// @returns the type of the Constant
const sem::Type* Type() const { return type_; }
/// @returns the element type of the Constant
const sem::Type* ElementType() const { return elem_type_; }
/// @returns the constant's scalar elements
const Scalars& Elements() const { return elems_; }
/// Calls `func(s)` with s being the current scalar value at `index`.
/// `func` is typically a lambda of the form '[](auto&& s)'.
/// @param index the index of the scalar value
/// @param func a function with signature `T(S)`
/// @return the value returned by func.
template <typename Func>
auto WithScalarAt(size_t index, Func&& func) const {
auto* elem_type = ElementType();
if (elem_type->Is<I32>()) {
return func(elems_[index].i32);
}
if (elem_type->Is<U32>()) {
return func(elems_[index].u32);
}
if (elem_type->Is<F32>()) {
return func(elems_[index].f32);
}
if (elem_type->Is<Bool>()) {
return func(elems_[index].bool_);
}
diag::List diags;
TINT_UNREACHABLE(Semantic, diags)
<< "invalid scalar type " << type_->type_name();
return func(~0);
}
private:
const sem::Type* type_ = nullptr;
const sem::Type* elem_type_ = nullptr;
Scalars elems_;
};
} // namespace sem
} // namespace tint
#endif // SRC_SEM_CONSTANT_H_

View File

@@ -14,6 +14,8 @@
#include "src/sem/expression.h"
#include <utility>
TINT_INSTANTIATE_TYPEINFO(tint::sem::Expression);
namespace tint {
@@ -21,10 +23,16 @@ namespace sem {
Expression::Expression(const ast::Expression* declaration,
const sem::Type* type,
Statement* statement)
: declaration_(declaration), type_(type), statement_(statement) {
Statement* statement,
Constant constant)
: declaration_(declaration),
type_(type),
statement_(statement),
constant_(std::move(constant)) {
TINT_ASSERT(Semantic, type_);
}
Expression::~Expression() = default;
} // namespace sem
} // namespace tint

View File

@@ -16,6 +16,7 @@
#define SRC_SEM_EXPRESSION_H_
#include "src/ast/expression.h"
#include "src/sem/constant.h"
#include "src/sem/node.h"
namespace tint {
@@ -31,9 +32,14 @@ class Expression : public Castable<Expression, Node> {
/// @param declaration the AST node
/// @param type the resolved type of the expression
/// @param statement the statement that owns this expression
/// @param constant the constant value of the expression. May be invalid
Expression(const ast::Expression* declaration,
const sem::Type* type,
Statement* statement);
Statement* statement,
Constant constant);
/// Destructor
~Expression() override;
/// @return the resolved type of the expression
sem::Type* Type() const { return const_cast<sem::Type*>(type_); }
@@ -41,6 +47,9 @@ class Expression : public Castable<Expression, Node> {
/// @return the statement that owns this expression
Statement* Stmt() const { return statement_; }
/// @return the constant value of this expression
const Constant& ConstantValue() const { return constant_; }
/// @returns the AST node
ast::Expression* Declaration() const {
return const_cast<ast::Expression*>(declaration_);
@@ -50,6 +59,7 @@ class Expression : public Castable<Expression, Node> {
const ast::Expression* declaration_;
const sem::Type* const type_;
Statement* const statement_;
Constant const constant_;
};
} // namespace sem

View File

@@ -28,7 +28,7 @@ MemberAccessorExpression::MemberAccessorExpression(
ast::MemberAccessorExpression* declaration,
const sem::Type* type,
Statement* statement)
: Base(declaration, type, statement) {}
: Base(declaration, type, statement, Constant{}) {}
MemberAccessorExpression::~MemberAccessorExpression() = default;

View File

@@ -52,7 +52,6 @@ class Type : public Castable<Type, Node> {
/// @returns the inner type if this is a reference, `this` otherwise
const Type* UnwrapRef() const;
/// @returns true if this type is a scalar
bool is_scalar() const;
/// @returns true if this type is a numeric scalar

View File

@@ -14,6 +14,8 @@
#include "src/sem/variable.h"
#include <utility>
#include "src/ast/identifier_expression.h"
#include "src/ast/variable.h"
@@ -50,8 +52,10 @@ Variable::~Variable() = default;
VariableUser::VariableUser(ast::IdentifierExpression* declaration,
const sem::Type* type,
Statement* statement,
sem::Variable* variable)
: Base(declaration, type, statement), variable_(variable) {}
sem::Variable* variable,
Constant constant_value)
: Base(declaration, type, statement, std::move(constant_value)),
variable_(variable) {}
} // namespace sem
} // namespace tint

View File

@@ -109,10 +109,12 @@ class VariableUser : public Castable<VariableUser, Expression> {
/// @param type the resolved type of the expression
/// @param statement the statement that owns this expression
/// @param variable the semantic variable
/// @param constant_value the constant value for the variable. May be invalid
VariableUser(ast::IdentifierExpression* declaration,
const sem::Type* type,
Statement* statement,
sem::Variable* variable);
sem::Variable* variable,
Constant constant_value);
/// @returns the variable that this expression refers to
const sem::Variable* Variable() const { return variable_; }