[spirv-writer] Fixes to constant constructor determination.

The change from `cast` to type constructor casts causes our current
determination if a constructor is constant to no longer be correct.

This Cl updates the determination to match the current spec and adds a
bunch of unit tests to verify the behaviour..

Bug: tint:270
Change-Id: I8ce74eb7c3f849ce62815868313449d8ca2de6be
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/30020
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2020-10-14 15:14:11 +00:00 committed by Commit Bot service account
parent 4311dd05c8
commit 435916e544
5 changed files with 529 additions and 96 deletions

View File

@ -1128,6 +1128,64 @@ uint32_t Builder::GenerateConstructorExpression(
return 0; return 0;
} }
bool Builder::is_constructor_const(ast::Expression* expr, bool is_global_init) {
if (!expr->IsConstructor()) {
return false;
}
if (expr->AsConstructor()->IsScalarConstructor()) {
return true;
}
auto* tc = expr->AsConstructor()->AsTypeConstructor();
auto* result_type = tc->type()->UnwrapAliasPtrAlias();
for (size_t i = 0; i < tc->values().size(); ++i) {
auto* e = tc->values()[i].get();
if (!e->IsConstructor()) {
if (is_global_init) {
error_ = "constructor must be a constant expression";
return false;
}
return false;
}
if (!is_constructor_const(e, is_global_init)) {
return false;
}
if (has_error()) {
return false;
}
if (result_type->IsVector() && !e->AsConstructor()->IsScalarConstructor()) {
return false;
}
// This should all be handled by |is_constructor_const| call above
if (!e->AsConstructor()->IsScalarConstructor()) {
continue;
}
auto* sc = e->AsConstructor()->AsScalarConstructor();
ast::type::Type* subtype = result_type->UnwrapAliasPtrAlias();
if (subtype->IsVector()) {
subtype = subtype->AsVector()->type()->UnwrapAliasPtrAlias();
} else if (subtype->IsMatrix()) {
subtype = subtype->AsMatrix()->type()->UnwrapAliasPtrAlias();
} else if (subtype->IsArray()) {
subtype = subtype->AsArray()->type()->UnwrapAliasPtrAlias();
} else if (subtype->IsStruct()) {
subtype = subtype->AsStruct()
->impl()
->members()[i]
->type()
->UnwrapAliasPtrAlias();
}
if (subtype != sc->result_type()->UnwrapAliasPtrAlias()) {
return false;
}
}
return true;
}
uint32_t Builder::GenerateTypeConstructorExpression( uint32_t Builder::GenerateTypeConstructorExpression(
ast::TypeConstructorExpression* init, ast::TypeConstructorExpression* init,
bool is_global_init) { bool is_global_init) {
@ -1143,30 +1201,13 @@ uint32_t Builder::GenerateTypeConstructorExpression(
out << "__const"; out << "__const";
auto* result_type = init->type()->UnwrapAliasPtrAlias(); auto* result_type = init->type()->UnwrapAliasPtrAlias();
bool constructor_is_const = is_constructor_const(init, is_global_init);
OperandList ops; if (has_error()) {
bool constructor_is_const = true; return 0;
for (const auto& e : values) {
if (!e->IsConstructor()) {
if (is_global_init) {
error_ = "constructor must be a constant expression";
return 0;
}
constructor_is_const = false;
break;
} else if (result_type->IsVector()) {
// Even if we have constructor parameters if the types are different then
// the constructor is not const as we'll generate OpBitcast or
// OpCopyObject instructions.
auto* subtype = result_type->AsVector()->type()->UnwrapAliasPtrAlias();
if (subtype != e->result_type()->UnwrapAliasPtrAlias()) {
constructor_is_const = false;
break;
}
}
} }
bool can_cast_or_copy = result_type->is_scalar(); bool can_cast_or_copy = result_type->is_scalar();
if (result_type->IsVector() && result_type->AsVector()->type()->is_scalar()) { if (result_type->IsVector() && result_type->AsVector()->type()->is_scalar()) {
auto* value_type = values[0]->result_type()->UnwrapAliasPtrAlias(); auto* value_type = values[0]->result_type()->UnwrapAliasPtrAlias();
can_cast_or_copy = can_cast_or_copy =
@ -1190,6 +1231,7 @@ uint32_t Builder::GenerateTypeConstructorExpression(
result_type = result_type->AsVector()->type(); result_type = result_type->AsVector()->type();
} }
OperandList ops;
for (const auto& e : values) { for (const auto& e : values) {
uint32_t id = 0; uint32_t id = 0;
if (constructor_is_const) { if (constructor_is_const) {

View File

@ -437,6 +437,12 @@ class Builder {
SpvImageFormat convert_image_format_to_spv( SpvImageFormat convert_image_format_to_spv(
const ast::type::ImageFormat format); const ast::type::ImageFormat format);
/// Determines if the given type constructor is created from constant values
/// @param expr the expression to check
/// @param is_global_init if this is a global initializer
/// @returns true if the constructor is constant
bool is_constructor_const(ast::Expression* expr, bool is_global_init);
private: private:
/// @returns an Operand with a new result ID in it. Increments the next_id_ /// @returns an Operand with a new result ID in it. Increments the next_id_
/// automatically. /// automatically.

View File

@ -178,7 +178,6 @@ OpStore %1 %13
TEST_F(BuilderTest, Assign_Var_Complex_Constructor) { TEST_F(BuilderTest, Assign_Var_Complex_Constructor) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec3(&f32, 3); ast::type::VectorType vec3(&f32, 3);
ast::type::VectorType vec(&vec3, 3);
ast::ExpressionList vals; ast::ExpressionList vals;
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
@ -187,35 +186,11 @@ TEST_F(BuilderTest, Assign_Var_Complex_Constructor) {
std::make_unique<ast::FloatLiteral>(&f32, 2.0f))); std::make_unique<ast::FloatLiteral>(&f32, 2.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
auto first =
std::make_unique<ast::TypeConstructorExpression>(&vec3, std::move(vals));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
auto second =
std::make_unique<ast::TypeConstructorExpression>(&vec3, std::move(vals));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
auto third =
std::make_unique<ast::TypeConstructorExpression>(&vec3, std::move(vals));
vals.push_back(std::move(first));
vals.push_back(std::move(second));
vals.push_back(std::move(third));
auto init = auto init =
std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(vals)); std::make_unique<ast::TypeConstructorExpression>(&vec3, std::move(vals));
ast::Variable v("var", ast::StorageClass::kOutput, &vec); ast::Variable v("var", ast::StorageClass::kOutput, &vec3);
ast::AssignmentStatement assign( ast::AssignmentStatement assign(
std::make_unique<ast::IdentifierExpression>("var"), std::move(init)); std::make_unique<ast::IdentifierExpression>("var"), std::move(init));
@ -234,21 +209,17 @@ TEST_F(BuilderTest, Assign_Var_Complex_Constructor) {
EXPECT_TRUE(b.GenerateAssignStatement(&assign)) << b.error(); EXPECT_TRUE(b.GenerateAssignStatement(&assign)) << b.error();
EXPECT_FALSE(b.has_error()); EXPECT_FALSE(b.has_error());
EXPECT_EQ(DumpInstructions(b.types()), R"(%5 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32
%4 = OpTypeVector %5 3
%3 = OpTypeVector %4 3 %3 = OpTypeVector %4 3
%2 = OpTypePointer Output %3 %2 = OpTypePointer Output %3
%6 = OpConstantNull %3 %5 = OpConstantNull %3
%1 = OpVariable %2 Output %6 %1 = OpVariable %2 Output %5
%7 = OpConstant %5 1 %6 = OpConstant %4 1
%8 = OpConstant %5 2 %7 = OpConstant %4 2
%9 = OpConstant %5 3 %8 = OpConstant %4 3
%10 = OpConstantComposite %4 %7 %8 %9 %9 = OpConstantComposite %3 %6 %7 %8
%11 = OpConstantComposite %4 %9 %8 %7
%12 = OpConstantComposite %4 %8 %7 %9
%13 = OpConstantComposite %3 %10 %11 %12
)"); )");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"(OpStore %1 %13 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"(OpStore %1 %9
)"); )");
} }

View File

@ -95,6 +95,49 @@ TEST_F(BuilderTest, Constructor_Type) {
)"); )");
} }
TEST_F(BuilderTest, Constructor_Type_WithCasts) {
ast::type::F32Type f32;
ast::type::I32Type i32;
ast::type::VectorType vec(&f32, 2);
ast::ExpressionList type_vals;
type_vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
ast::ExpressionList vals;
vals.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(type_vals)));
type_vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
vals.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(type_vals)));
ast::TypeConstructorExpression t(&vec, std::move(vals));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
EXPECT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateExpression(&t), 7u);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%1 = OpTypeVector %2 2
%4 = OpTypeInt 32 1
%5 = OpConstant %4 1
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%3 = OpConvertSToF %2 %5
%6 = OpConvertSToF %2 %5
%7 = OpCompositeConstruct %1 %3 %6
)");
}
TEST_F(BuilderTest, Constructor_Type_WithAlias) { TEST_F(BuilderTest, Constructor_Type_WithAlias) {
ast::type::I32Type i32; ast::type::I32Type i32;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -2384,6 +2427,407 @@ TEST_F(BuilderTest, Constructor_Type_Convert_Vectors_U32_to_F32) {
)"); )");
} }
TEST_F(BuilderTest, IsConstructorConst_GlobalVectorWithAllConstConstructors) {
// vec3<f32>(1.0, 2.0, 3.0) -> true
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.f)));
ast::TypeConstructorExpression t(&vec, std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_TRUE(b.is_constructor_const(&t, true));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_GlobalVector_WithIdent) {
// vec3<f32>(a, b, c) -> false -- ERROR
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList params;
params.push_back(std::make_unique<ast::IdentifierExpression>("a"));
params.push_back(std::make_unique<ast::IdentifierExpression>("b"));
params.push_back(std::make_unique<ast::IdentifierExpression>("c"));
ast::TypeConstructorExpression t(&vec, std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_FALSE(b.is_constructor_const(&t, true));
EXPECT_TRUE(b.has_error());
EXPECT_EQ(b.error(), "constructor must be a constant expression");
}
TEST_F(BuilderTest, IsConstructorConst_GlobalArrayWithAllConstConstructors) {
// array<vec3<f32>, 2>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 2.0, 3.0))
// -> true
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::type::ArrayType ary(&vec, 2);
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.f)));
auto first =
std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(params));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.f)));
auto second =
std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(params));
ast::ExpressionList ary_params;
ary_params.push_back(std::move(first));
ary_params.push_back(std::move(second));
ast::TypeConstructorExpression t(&ary, std::move(ary_params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_TRUE(b.is_constructor_const(&t, true));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest,
IsConstructorConst_GlobalVectorWithMatchingTypeConstructors) {
// vec3<f32>(f32(1.0), f32(2.0)) -> false
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 2);
ast::ExpressionList vec_params;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0)));
vec_params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(params)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.0)));
vec_params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(params)));
ast::TypeConstructorExpression t(&vec, std::move(vec_params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_FALSE(b.is_constructor_const(&t, true));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_GlobalWithTypeCastConstructor) {
// vec3<f32>(f32(1), f32(2)) -> false
ast::type::F32Type f32;
ast::type::I32Type i32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList vec_params;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
vec_params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(params)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 2)));
vec_params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(params)));
ast::TypeConstructorExpression t(&vec, std::move(vec_params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_FALSE(b.is_constructor_const(&t, true));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_VectorWithAllConstConstructors) {
// vec3<f32>(1.0, 2.0, 3.0) -> true
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.f)));
ast::TypeConstructorExpression t(&vec, std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_TRUE(b.is_constructor_const(&t, false));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_Vector_WithIdent) {
// vec3<f32>(a, b, c) -> false
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList params;
params.push_back(std::make_unique<ast::IdentifierExpression>("a"));
params.push_back(std::make_unique<ast::IdentifierExpression>("b"));
params.push_back(std::make_unique<ast::IdentifierExpression>("c"));
ast::TypeConstructorExpression t(&vec, std::move(params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_FALSE(b.is_constructor_const(&t, false));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_ArrayWithAllConstConstructors) {
// array<vec3<f32>, 2>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 2.0, 3.0))
// -> true
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::type::ArrayType ary(&vec, 2);
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.f)));
auto first =
std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(params));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.f)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.f)));
auto second =
std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(params));
ast::ExpressionList ary_params;
ary_params.push_back(std::move(first));
ary_params.push_back(std::move(second));
ast::TypeConstructorExpression t(&ary, std::move(ary_params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_TRUE(b.is_constructor_const(&t, false));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_VectorWith_TypeCastConstConstructors) {
// vec2<f32>(f32(1.0), f32(2.0)) -> false
ast::type::F32Type f32;
ast::type::I32Type i32;
ast::type::VectorType vec(&f32, 2);
ast::ExpressionList vec_params;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
vec_params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(params)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 2)));
vec_params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(params)));
ast::TypeConstructorExpression t(&vec, std::move(vec_params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_FALSE(b.is_constructor_const(&t, false));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_WithTypeCastConstructor) {
// vec3<f32>(f32(1), f32(2)) -> false
ast::type::F32Type f32;
ast::type::I32Type i32;
ast::type::VectorType vec(&f32, 3);
ast::ExpressionList vec_params;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
vec_params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(params)));
params.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 2)));
vec_params.push_back(std::make_unique<ast::TypeConstructorExpression>(
&f32, std::move(params)));
ast::TypeConstructorExpression t(&vec, std::move(vec_params));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_FALSE(b.is_constructor_const(&t, false));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_BitCastScalars) {
ast::type::I32Type i32;
ast::type::U32Type u32;
ast::type::VectorType vec(&u32, 2);
ast::ExpressionList vals;
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1)));
ast::TypeConstructorExpression t(&vec, std::move(vals));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_FALSE(b.is_constructor_const(&t, false));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_Struct) {
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::StructMemberDecorationList decos;
ast::StructMemberList members;
members.push_back(
std::make_unique<ast::StructMember>("a", &f32, std::move(decos)));
members.push_back(
std::make_unique<ast::StructMember>("b", &vec, std::move(decos)));
auto s = std::make_unique<ast::Struct>(std::move(members));
ast::type::StructType s_type(std::move(s));
s_type.set_name("my_struct");
ast::ExpressionList vec_vals;
vec_vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2)));
vec_vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2)));
vec_vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2)));
ast::ExpressionList vals;
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2)));
vals.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec, std::move(vec_vals)));
ast::TypeConstructorExpression t(&s_type, std::move(vals));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_TRUE(b.is_constructor_const(&t, false));
EXPECT_FALSE(b.has_error());
}
TEST_F(BuilderTest, IsConstructorConst_Struct_WithIdentSubExpression) {
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
ast::StructMemberDecorationList decos;
ast::StructMemberList members;
members.push_back(
std::make_unique<ast::StructMember>("a", &f32, std::move(decos)));
members.push_back(
std::make_unique<ast::StructMember>("b", &vec, std::move(decos)));
auto s = std::make_unique<ast::Struct>(std::move(members));
ast::type::StructType s_type(std::move(s));
s_type.set_name("my_struct");
ast::ExpressionList vec_vals;
vec_vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2)));
vec_vals.push_back(std::make_unique<ast::IdentifierExpression>("a"));
vec_vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2)));
ast::ExpressionList vals;
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2)));
vals.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec, std::move(vec_vals)));
ast::TypeConstructorExpression t(&s_type, std::move(vals));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&t)) << td.error();
Builder b(&mod);
EXPECT_FALSE(b.is_constructor_const(&t, false));
EXPECT_FALSE(b.has_error());
}
} // namespace } // namespace
} // namespace spirv } // namespace spirv
} // namespace writer } // namespace writer

View File

@ -176,7 +176,6 @@ TEST_F(BuilderTest, GlobalVar_Const) {
TEST_F(BuilderTest, GlobalVar_Complex_Constructor) { TEST_F(BuilderTest, GlobalVar_Complex_Constructor) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec3(&f32, 3); ast::type::VectorType vec3(&f32, 3);
ast::type::VectorType vec(&vec3, 3);
ast::ExpressionList vals; ast::ExpressionList vals;
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
@ -185,33 +184,8 @@ TEST_F(BuilderTest, GlobalVar_Complex_Constructor) {
std::make_unique<ast::FloatLiteral>(&f32, 2.0f))); std::make_unique<ast::FloatLiteral>(&f32, 2.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>( vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f))); std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
auto first =
std::make_unique<ast::TypeConstructorExpression>(&vec3, std::move(vals));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
auto second =
std::make_unique<ast::TypeConstructorExpression>(&vec3, std::move(vals));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
vals.push_back(std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
auto third =
std::make_unique<ast::TypeConstructorExpression>(&vec3, std::move(vals));
vals.push_back(std::move(first));
vals.push_back(std::move(second));
vals.push_back(std::move(third));
auto init = auto init =
std::make_unique<ast::TypeConstructorExpression>(&vec, std::move(vals)); std::make_unique<ast::TypeConstructorExpression>(&vec3, std::move(vals));
Context ctx; Context ctx;
ast::Module mod; ast::Module mod;
@ -227,16 +201,12 @@ TEST_F(BuilderTest, GlobalVar_Complex_Constructor) {
EXPECT_TRUE(b.GenerateGlobalVariable(&v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(&v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error(); ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%2 = OpTypeVector %3 3
%1 = OpTypeVector %2 3 %1 = OpTypeVector %2 3
%4 = OpConstant %3 1 %3 = OpConstant %2 1
%5 = OpConstant %3 2 %4 = OpConstant %2 2
%6 = OpConstant %3 3 %5 = OpConstant %2 3
%7 = OpConstantComposite %2 %4 %5 %6 %6 = OpConstantComposite %1 %3 %4 %5
%8 = OpConstantComposite %2 %6 %5 %4
%9 = OpConstantComposite %2 %5 %4 %6
%10 = OpConstantComposite %1 %7 %8 %9
)"); )");
} }