Fix all tests that shared AST nodes

This is not valid. Will become an ICE.

Bug: tint:469
Change-Id: I02c0eea16daf7d83f4d6c251e06d9cac0dfd7ce4
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47777
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-04-17 00:42:41 +00:00 committed by Commit Bot service account
parent 0ecea57a8a
commit a0cf62f415
8 changed files with 61 additions and 81 deletions

View File

@ -111,17 +111,18 @@ ast::ConstructorExpression* ProgramBuilder::ConstructValueFilledWith(
type, static_cast<ProgramBuilder::f32>(elem_value)));
}
if (auto* v = unwrapped_type->As<type::Vector>()) {
auto* elem_default_value = ConstructValueFilledWith(v->type(), elem_value);
ast::ExpressionList el(v->size());
std::fill(el.begin(), el.end(), elem_default_value);
for (size_t i = 0; i < el.size(); i++) {
el[i] = ConstructValueFilledWith(v->type(), elem_value);
}
return create<ast::TypeConstructorExpression>(type, std::move(el));
}
if (auto* m = unwrapped_type->As<type::Matrix>()) {
auto* col_vec_type = create<type::Vector>(m->type(), m->rows());
auto* vec_default_value =
ConstructValueFilledWith(col_vec_type, elem_value);
ast::ExpressionList el(m->columns());
std::fill(el.begin(), el.end(), vec_default_value);
ast::ExpressionList el(col_vec_type->size());
for (size_t i = 0; i < el.size(); i++) {
el[i] = ConstructValueFilledWith(col_vec_type, elem_value);
}
return create<ast::TypeConstructorExpression>(type, std::move(el));
}
TINT_ASSERT(false);

View File

@ -468,7 +468,7 @@ TEST_F(ResolverIntrinsicTest, Select_Error_NoParams) {
}
TEST_F(ResolverIntrinsicTest, Select_Error_SelectorInt) {
auto* expr = Call("select", Expr(1), Expr(1), Expr(1));
auto* expr = Call("select", 1, 1, 1);
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
@ -483,8 +483,9 @@ TEST_F(ResolverIntrinsicTest, Select_Error_SelectorInt) {
}
TEST_F(ResolverIntrinsicTest, Select_Error_Matrix) {
auto* mat = mat2x2<float>(vec2<float>(1.0f, 1.0f), vec2<float>(1.0f, 1.0f));
auto* expr = Call("select", mat, mat, Expr(true));
auto* expr = Call(
"select", mat2x2<f32>(vec2<f32>(1.0f, 1.0f), vec2<f32>(1.0f, 1.0f)),
mat2x2<f32>(vec2<f32>(1.0f, 1.0f), vec2<f32>(1.0f, 1.0f)), Expr(true));
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
@ -499,7 +500,7 @@ TEST_F(ResolverIntrinsicTest, Select_Error_Matrix) {
}
TEST_F(ResolverIntrinsicTest, Select_Error_MismatchTypes) {
auto* expr = Call("select", 1.0f, vec2<float>(2.0f, 3.0f), Expr(true));
auto* expr = Call("select", 1.0f, vec2<f32>(2.0f, 3.0f), Expr(true));
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
@ -514,8 +515,8 @@ TEST_F(ResolverIntrinsicTest, Select_Error_MismatchTypes) {
}
TEST_F(ResolverIntrinsicTest, Select_Error_MismatchVectorSize) {
auto* expr = Call("select", vec2<float>(1.0f, 2.0f),
vec3<float>(3.0f, 4.0f, 5.0f), Expr(true));
auto* expr = Call("select", vec2<f32>(1.0f, 2.0f),
vec3<f32>(3.0f, 4.0f, 5.0f), Expr(true));
WrapInFunction(expr);
EXPECT_FALSE(r()->Resolve());
@ -1007,15 +1008,7 @@ TEST_P(ResolverIntrinsicTest_SingleParam_FloatOrInt, Sint_Scalar) {
TEST_P(ResolverIntrinsicTest_SingleParam_FloatOrInt, Sint_Vector) {
auto param = GetParam();
ast::ExpressionList vals;
vals.push_back(Expr(1));
vals.push_back(Expr(1));
vals.push_back(Expr(3));
ast::ExpressionList params;
params.push_back(vec3<i32>(vals));
auto* call = Call(param.name, params);
auto* call = Call(param.name, vec3<i32>(1, 1, 3));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
@ -1028,10 +1021,7 @@ TEST_P(ResolverIntrinsicTest_SingleParam_FloatOrInt, Sint_Vector) {
TEST_P(ResolverIntrinsicTest_SingleParam_FloatOrInt, Uint_Scalar) {
auto param = GetParam();
ast::ExpressionList params;
params.push_back(Expr(1u));
auto* call = Call(param.name, params);
auto* call = Call(param.name, 1u);
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();
@ -1087,10 +1077,7 @@ TEST_F(ResolverIntrinsicTest, Length_Scalar) {
}
TEST_F(ResolverIntrinsicTest, Length_FloatVector) {
ast::ExpressionList params;
params.push_back(vec3<f32>(1.0f, 1.0f, 3.0f));
auto* call = Call("length", params);
auto* call = Call("length", vec3<f32>(1.0f, 1.0f, 3.0f));
WrapInFunction(call);
EXPECT_TRUE(r()->Resolve()) << r()->error();

View File

@ -51,8 +51,7 @@ TEST_F(ResolverTypeConstructorValidationTest, InferTypeTest_Simple) {
auto* a_ident = Expr("a");
auto* b_ident = Expr("b");
WrapInFunction(Decl(a), Decl(b), Assign(a_ident, a_ident),
Assign(b_ident, b_ident));
WrapInFunction(Decl(a), Decl(b), Assign(a_ident, "a"), Assign(b_ident, "b"));
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_EQ(TypeOf(a_ident), ty.pointer(ty.i32(), sc));
@ -75,7 +74,7 @@ TEST_P(InferTypeTest_FromConstructorExpression, All) {
// Self-assign 'a' to force the expression to be resolved so we can test its
// type below
auto* a_ident = Expr("a");
WrapInFunction(Decl(a), Assign(a_ident, a_ident));
WrapInFunction(Decl(a), Assign(a_ident, "a"));
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_EQ(TypeOf(a_ident), ty.pointer(rhs_type, sc));
@ -126,7 +125,7 @@ TEST_P(InferTypeTest_FromArithmeticExpression, All) {
// Self-assign 'a' to force the expression to be resolved so we can test its
// type below
auto* a_ident = Expr("a");
WrapInFunction(Decl(a), Assign(a_ident, a_ident));
WrapInFunction(Decl(a), Assign(a_ident, "a"));
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_EQ(TypeOf(a_ident), ty.pointer(rhs_type, sc));
@ -171,7 +170,7 @@ TEST_P(InferTypeTest_FromCallExpression, All) {
// Self-assign 'a' to force the expression to be resolved so we can test its
// type below
auto* a_ident = Expr("a");
WrapInFunction(Decl(a), Assign(a_ident, a_ident));
WrapInFunction(Decl(a), Assign(a_ident, "a"));
ASSERT_TRUE(r()->Resolve()) << r()->error();
ASSERT_EQ(TypeOf(a_ident), ty.pointer(rhs_type, sc));

View File

@ -69,11 +69,7 @@ ast::ArrayAccessorExpression* BoundArrayAccessors::Transform(
if (size == 0) {
if (is_arr) {
// Call Cloneable::Clone() instead of CloneContext::Clone() to ensure the
// AST node is duplicated. CloneContext::Clone() will ensure that repeated
// calls with the same pointer return the *same* cloned node - in this
// case we actually want two copies.
auto* arr = static_cast<ast::Expression*>(expr->array()->Clone(ctx));
auto* arr = ctx->Clone(expr->array());
auto* arr_len = b.Call("arrayLength", arr);
auto* limit = b.Sub(arr_len, b.Expr(1u));
new_idx = b.Call("min", b.Construct<u32>(ctx->Clone(old_idx)), limit);

View File

@ -222,7 +222,10 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
for (auto* ret : sem_func->ReturnStatements()) {
auto* ret_sem = ctx.src->Sem().Get(ret);
// Reconstruct the return value using the newly created struct.
auto* new_ret_value = ctx.Clone(ret->value());
std::function<ast::Expression*()> new_ret_value = [&ctx, ret] {
return ctx.Clone(ret->value());
};
ast::ExpressionList ret_values;
if (ret_type->Is<type::Struct>()) {
if (!ret->value()->Is<ast::IdentifierExpression>()) {
@ -230,17 +233,17 @@ Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
// re-evaluating it multiple times.
auto temp = ctx.dst->Symbols().New();
auto* temp_var = ctx.dst->Decl(
ctx.dst->Const(temp, ctx.Clone(ret_type), new_ret_value));
ctx.dst->Const(temp, ctx.Clone(ret_type), new_ret_value()));
ctx.InsertBefore(ret_sem->Block()->statements(), ret, temp_var);
new_ret_value = ctx.dst->Expr(temp);
new_ret_value = [&ctx, temp] { return ctx.dst->Expr(temp); };
}
for (auto* member : new_struct_members) {
ret_values.push_back(
ctx.dst->MemberAccessor(new_ret_value, member->symbol()));
ctx.dst->MemberAccessor(new_ret_value(), member->symbol()));
}
} else {
ret_values.push_back(new_ret_value);
ret_values.push_back(new_ret_value());
}
auto* new_ret = ctx.dst->create<ast::ReturnStatement>(

View File

@ -14,6 +14,8 @@
#include "src/ast/stage_decoration.h"
#include "src/ast/variable_decl_statement.h"
#include "src/type/access_control_type.h"
#include "src/type/sampled_texture_type.h"
#include "src/writer/wgsl/test_helper.h"
namespace tint {
@ -111,6 +113,32 @@ TEST_F(WgslGeneratorImplTest, Emit_GlobalsInterleaved) {
)");
}
TEST_F(WgslGeneratorImplTest, Emit_Global_Sampler) {
Global("s", create<type::Sampler>(type::SamplerKind::kSampler),
ast::StorageClass::kUniformConstant);
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate(nullptr)) << gen.error();
EXPECT_EQ(gen.result(), " var s : sampler;\n");
}
TEST_F(WgslGeneratorImplTest, Emit_Global_Texture) {
auto* st =
create<type::SampledTexture>(type::TextureDimension::k1d, ty.f32());
Global("t", ty.access(ast::AccessControl::kReadOnly, st),
ast::StorageClass::kUniformConstant);
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate(nullptr)) << gen.error();
EXPECT_EQ(gen.result(), " var t : [[access(read)]] texture_1d<f32>;\n");
}
} // namespace
} // namespace wgsl
} // namespace writer

View File

@ -13,8 +13,6 @@
// limitations under the License.
#include "src/ast/variable_decl_statement.h"
#include "src/type/access_control_type.h"
#include "src/type/sampled_texture_type.h"
#include "src/writer/wgsl/test_helper.h"
namespace tint {
@ -25,7 +23,7 @@ namespace {
using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement) {
auto* var = Global("a", ty.f32(), ast::StorageClass::kInput);
auto* var = Var("a", ty.f32(), ast::StorageClass::kInput);
auto* stmt = create<ast::VariableDeclStatement>(var);
WrapInFunction(stmt);
@ -43,7 +41,7 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Function) {
// storage class. Rely on defaulting.
// https://github.com/gpuweb/gpuweb/issues/654
auto* var = Global("a", ty.f32(), ast::StorageClass::kFunction);
auto* var = Var("a", ty.f32(), ast::StorageClass::kFunction);
auto* stmt = create<ast::VariableDeclStatement>(var);
WrapInFunction(stmt);
@ -57,7 +55,7 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Function) {
}
TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
auto* var = Global("a", ty.f32(), ast::StorageClass::kPrivate);
auto* var = Var("a", ty.f32(), ast::StorageClass::kPrivate);
auto* stmt = create<ast::VariableDeclStatement>(var);
WrapInFunction(stmt);
@ -70,37 +68,6 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
EXPECT_EQ(gen.result(), " var<private> a : f32;\n");
}
TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Sampler) {
auto* var = Global("s", create<type::Sampler>(type::SamplerKind::kSampler),
ast::StorageClass::kUniformConstant);
auto* stmt = create<ast::VariableDeclStatement>(var);
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " var s : sampler;\n");
}
TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Texture) {
auto* st =
create<type::SampledTexture>(type::TextureDimension::k1d, ty.f32());
auto* var = Global(
"t", create<type::AccessControl>(ast::AccessControl::kReadOnly, st),
ast::StorageClass::kUniformConstant);
auto* stmt = create<ast::VariableDeclStatement>(var);
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " var t : [[access(read)]] texture_1d<f32>;\n");
}
} // namespace
} // namespace wgsl
} // namespace writer

View File

@ -76,7 +76,6 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Decorated_Multiple) {
TEST_F(WgslGeneratorImplTest, EmitVariable_Constructor) {
auto* v = Global("a", ty.f32(), ast::StorageClass::kInput, Expr(1.0f));
WrapInFunction(Decl(v));
GeneratorImpl& gen = Build();