tint: Add sem::Materialize

A new semantic expression node that wraps another semantic node. Used to
indicate the point at which compile-time, abstract numeric typed
expressions are implicitly converted to a concrete type.

Bug: tint:1504
Change-Id: I52e256bbbdeaa9d9eff4cb93b6f937dd00bdc5cb
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/90531
Reviewed-by: David Neto <dneto@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
Ben Clayton 2022-05-19 19:32:29 +00:00 committed by Dawn LUCI CQ
parent 6ac00ed0c0
commit 2081ee43bf
8 changed files with 141 additions and 0 deletions

View File

@ -616,6 +616,8 @@ libtint_source_set("libtint_sem_src") {
"sem/info.h",
"sem/loop_statement.cc",
"sem/loop_statement.h",
"sem/materialize.cc",
"sem/materialize.h",
"sem/matrix.cc",
"sem/matrix.h",
"sem/member_accessor_expression.cc",

View File

@ -421,6 +421,8 @@ set(TINT_LIB_SRCS
sem/if_statement.h
sem/loop_statement.cc
sem/loop_statement.h
sem/materialize.cc
sem/materialize.h
sem/matrix.cc
sem/matrix.h
sem/multisampled_texture.cc
@ -799,6 +801,7 @@ if(TINT_BUILD_TESTS)
sem/builtin_test.cc
sem/depth_multisampled_texture_test.cc
sem/depth_texture_test.cc
sem/expression_test.cc
sem/external_texture_test.cc
sem/f16_test.cc
sem/f32_test.cc

View File

@ -16,6 +16,8 @@
#include <utility>
#include "src/tint/sem/materialize.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Expression);
namespace tint::sem {
@ -37,4 +39,11 @@ Expression::Expression(const ast::Expression* declaration,
Expression::~Expression() = default;
const Expression* Expression::UnwrapMaterialize() const {
if (auto* m = As<Materialize>()) {
return m->Expr();
}
return this;
}
} // namespace tint::sem

View File

@ -76,6 +76,9 @@ class Expression : public Castable<Expression, Node> {
/// @return true of this expression may have side effects
bool HasSideEffects() const { return has_side_effects_; }
/// @return the inner expression node if this is a Materialize, otherwise this.
const Expression* UnwrapMaterialize() const;
protected:
/// The AST expression node for this semantic expression
const ast::Expression* const declaration_;

View File

@ -0,0 +1,39 @@
// Copyright 2022 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/tint/sem/expression.h"
#include "src/tint/sem/test_helper.h"
#include "src/tint/sem/materialize.h"
using namespace tint::number_suffixes; // NOLINT
namespace tint::sem {
namespace {
using ExpressionTest = TestHelper;
TEST_F(ExpressionTest, UnwrapMaterialize) {
auto* a = create<Expression>(/* declaration */ nullptr, create<I32>(), /* statement */ nullptr,
Constant{},
/* has_side_effects */ false, /* source_var */ nullptr);
auto* b = create<Materialize>(a, /* statement */ nullptr, Constant{create<I32>(), {1_a}});
EXPECT_EQ(a, a->UnwrapMaterialize());
EXPECT_EQ(a, b->UnwrapMaterialize());
}
} // namespace
} // namespace tint::sem

View File

@ -0,0 +1,36 @@
// Copyright 2022 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/tint/sem/materialize.h"
TINT_INSTANTIATE_TYPEINFO(tint::sem::Materialize);
namespace tint::sem {
Materialize::Materialize(const Expression* expr, const Statement* statement, Constant constant)
: Base(/* declaration */ expr->Declaration(),
/* type */ constant.Type(),
/* statement */ statement,
/* constant */ constant,
/* has_side_effects */ false,
/* source_var */ expr->SourceVariable()),
expr_(expr) {
// Materialize nodes only wrap compile-time expressions, and so the Materialize expression must
// have a constant value.
TINT_ASSERT(Semantic, constant.IsValid());
}
Materialize::~Materialize() = default;
} // namespace tint::sem

View File

@ -0,0 +1,48 @@
// Copyright 2022 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_TINT_SEM_MATERIALIZE_H_
#define SRC_TINT_SEM_MATERIALIZE_H_
#include "src/tint/sem/expression.h"
namespace tint::sem {
/// Materialize is a semantic expression which represents the materialization of a value of an
/// abstract numeric type to a value of a concrete type.
/// Abstract numeric materialization is implicit in WGSL, so the Materialize semantic node shares
/// the same AST node as the inner semantic node.
/// Abstract numerics types may only be used by compile-time expressions, so a Materialize semantic
/// node must have a valid Constant value.
class Materialize final : public Castable<Materialize, Expression> {
public:
/// Constructor
/// @param expr the inner expression, being materialized
/// @param statement the statement that owns this expression
/// @param constant the constant value of this expression
Materialize(const Expression* expr, const Statement* statement, Constant constant);
/// Destructor
~Materialize() override;
/// @return the target of the call
const Expression* Expr() const { return expr_; }
private:
Expression const* const expr_;
};
} // namespace tint::sem
#endif // SRC_TINT_SEM_MATERIALIZE_H_

View File

@ -290,6 +290,7 @@ tint_unittests_source_set("tint_unittests_sem_src") {
"../../src/tint/sem/builtin_test.cc",
"../../src/tint/sem/depth_multisampled_texture_test.cc",
"../../src/tint/sem/depth_texture_test.cc",
"../../src/tint/sem/expression_test.cc",
"../../src/tint/sem/external_texture_test.cc",
"../../src/tint/sem/f16_test.cc",
"../../src/tint/sem/f32_test.cc",