ast: Remove helpers from ast::Type

These are legacy methods that were written before the semantic type nodes.

These methods do not consider aliases, and any use of these is likely to be broken for aliases.

Fix up uses of these methods to use the semantic types instead.

Change-Id: Ia66749b279eddff655d3d755fef54a6263643e69
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/66601
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-10-15 14:17:31 +00:00 committed by Tint LUCI CQ
parent 4dfa394a3c
commit 5029e70b6e
9 changed files with 29 additions and 136 deletions

View File

@ -38,77 +38,5 @@ Type::Type(Type&&) = default;
Type::~Type() = default; Type::~Type() = default;
Type* Type::UnwrapAll() {
auto* type = this;
while (true) {
if (auto* ptr = type->As<Pointer>()) {
type = ptr->type();
} else {
break;
}
}
return type;
}
bool Type::is_scalar() const {
return IsAnyOf<F32, U32, I32, Bool>();
}
bool Type::is_float_scalar() const {
return Is<F32>();
}
bool Type::is_float_matrix() const {
return Is([](const Matrix* m) { return m->type()->is_float_scalar(); });
}
bool Type::is_float_vector() const {
return Is([](const Vector* v) { return v->type()->is_float_scalar(); });
}
bool Type::is_float_scalar_or_vector() const {
return is_float_scalar() || is_float_vector();
}
bool Type::is_float_scalar_or_vector_or_matrix() const {
return is_float_scalar() || is_float_vector() || is_float_matrix();
}
bool Type::is_integer_scalar() const {
return IsAnyOf<U32, I32>();
}
bool Type::is_unsigned_integer_vector() const {
return Is([](const Vector* v) { return v->type()->Is<U32>(); });
}
bool Type::is_signed_integer_vector() const {
return Is([](const Vector* v) { return v->type()->Is<I32>(); });
}
bool Type::is_unsigned_scalar_or_vector() const {
return Is<U32>() || is_unsigned_integer_vector();
}
bool Type::is_signed_scalar_or_vector() const {
return Is<I32>() || is_signed_integer_vector();
}
bool Type::is_integer_scalar_or_vector() const {
return is_unsigned_scalar_or_vector() || is_signed_scalar_or_vector();
}
bool Type::is_bool_vector() const {
return Is([](const Vector* v) { return v->type()->Is<Bool>(); });
}
bool Type::is_bool_scalar_or_vector() const {
return Is<Bool>() || is_bool_vector();
}
bool Type::is_handle() const {
return IsAnyOf<Sampler, Texture>();
}
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -40,43 +40,6 @@ class Type : public Castable<Type, Node> {
/// declared in WGSL. /// declared in WGSL.
virtual std::string FriendlyName(const SymbolTable& symbols) const = 0; virtual std::string FriendlyName(const SymbolTable& symbols) const = 0;
/// @returns the type with all aliasing, access control and pointers removed
Type* UnwrapAll();
/// @returns the type with all aliasing, access control and pointers removed
const Type* UnwrapAll() const { return const_cast<Type*>(this)->UnwrapAll(); }
/// @returns true if this type is a scalar
bool is_scalar() const;
/// @returns true if this type is a float scalar
bool is_float_scalar() const;
/// @returns true if this type is a float matrix
bool is_float_matrix() const;
/// @returns true if this type is a float vector
bool is_float_vector() const;
/// @returns true if this type is a float scalar or vector
bool is_float_scalar_or_vector() const;
/// @returns true if this type is a float scalar or vector or matrix
bool is_float_scalar_or_vector_or_matrix() const;
/// @returns true if this type is an integer scalar
bool is_integer_scalar() const;
/// @returns true if this type is a signed integer vector
bool is_signed_integer_vector() const;
/// @returns true if this type is an unsigned vector
bool is_unsigned_integer_vector() const;
/// @returns true if this type is an unsigned scalar or vector
bool is_unsigned_scalar_or_vector() const;
/// @returns true if this type is a signed scalar or vector
bool is_signed_scalar_or_vector() const;
/// @returns true if this type is an integer scalar or vector
bool is_integer_scalar_or_vector() const;
/// @returns true if this type is a boolean vector
bool is_bool_vector() const;
/// @returns true if this type is boolean scalar or vector
bool is_bool_scalar_or_vector() const;
/// @returns true if this type is a handle type
bool is_handle() const;
protected: protected:
/// Constructor /// Constructor
/// @param program_id the identifier of the program that owns this node /// @param program_id the identifier of the program that owns this node

View File

@ -702,7 +702,9 @@ TEST_F(ParserImplTest, TypeDecl_Array_Runtime_Vec) {
auto* a = t.value->As<ast::Array>(); auto* a = t.value->As<ast::Array>();
ASSERT_TRUE(a->IsRuntimeArray()); ASSERT_TRUE(a->IsRuntimeArray());
ASSERT_TRUE(a->type()->is_unsigned_integer_vector()); ASSERT_TRUE(a->type()->Is<ast::Vector>());
EXPECT_EQ(a->type()->As<ast::Vector>()->size(), 4u);
EXPECT_TRUE(a->type()->As<ast::Vector>()->type()->Is<ast::U32>());
EXPECT_EQ(t.value->source().range, (Source::Range{{1u, 1u}, {1u, 17u}})); EXPECT_EQ(t.value->source().range, (Source::Range{{1u, 1u}, {1u, 17u}}));
} }

View File

@ -244,7 +244,7 @@ class ResolverIntrinsicTest_TextureOperation
void add_call_param(std::string name, void add_call_param(std::string name,
const ast::Type* type, const ast::Type* type,
ast::ExpressionList* call_params) { ast::ExpressionList* call_params) {
if (type->UnwrapAll()->is_handle()) { if (type->IsAnyOf<ast::Texture, ast::Sampler>()) {
Global(name, type, Global(name, type,
ast::DecorationList{ ast::DecorationList{
create<ast::BindingDecoration>(0), create<ast::BindingDecoration>(0),

View File

@ -155,8 +155,9 @@ struct CanonicalizeEntryPointIO::State {
/// @param attributes the attributes to apply to the shader input /// @param attributes the attributes to apply to the shader input
/// @returns an expression which evaluates to the value of the shader input /// @returns an expression which evaluates to the value of the shader input
ast::Expression* AddInput(std::string name, ast::Expression* AddInput(std::string name,
ast::Type* type, sem::Type* type,
ast::DecorationList attributes) { ast::DecorationList attributes) {
auto* ast_type = CreateASTTypeFor(ctx, type);
if (cfg.shader_style == ShaderStyle::kSpirv) { if (cfg.shader_style == ShaderStyle::kSpirv) {
// Vulkan requires that integer user-defined fragment inputs are // Vulkan requires that integer user-defined fragment inputs are
// always decorated with `Flat`. // always decorated with `Flat`.
@ -178,10 +179,10 @@ struct CanonicalizeEntryPointIO::State {
if (HasSampleMask(attributes)) { if (HasSampleMask(attributes)) {
// Vulkan requires the type of a SampleMask builtin to be an array. // Vulkan requires the type of a SampleMask builtin to be an array.
// Declare it as array<u32, 1> and then load the first element. // Declare it as array<u32, 1> and then load the first element.
type = ctx.dst->ty.array(type, 1); ast_type = ctx.dst->ty.array(ast_type, 1);
value = ctx.dst->IndexAccessor(value, 0); value = ctx.dst->IndexAccessor(value, 0);
} }
ctx.dst->Global(symbol, type, ast::StorageClass::kInput, ctx.dst->Global(symbol, ast_type, ast::StorageClass::kInput,
std::move(attributes)); std::move(attributes));
return value; return value;
} else if (cfg.shader_style == ShaderStyle::kMsl && } else if (cfg.shader_style == ShaderStyle::kMsl &&
@ -192,7 +193,7 @@ struct CanonicalizeEntryPointIO::State {
? ctx.dst->Symbols().Register(name) ? ctx.dst->Symbols().Register(name)
: ctx.dst->Symbols().New(name); : ctx.dst->Symbols().New(name);
wrapper_ep_parameters.push_back( wrapper_ep_parameters.push_back(
ctx.dst->Param(symbol, type, std::move(attributes))); ctx.dst->Param(symbol, ast_type, std::move(attributes)));
return ctx.dst->Expr(symbol); return ctx.dst->Expr(symbol);
} else { } else {
// Otherwise, move it to the new structure member list. // Otherwise, move it to the new structure member list.
@ -200,7 +201,7 @@ struct CanonicalizeEntryPointIO::State {
? ctx.dst->Symbols().Register(name) ? ctx.dst->Symbols().Register(name)
: ctx.dst->Symbols().New(name); : ctx.dst->Symbols().New(name);
wrapper_struct_param_members.push_back( wrapper_struct_param_members.push_back(
ctx.dst->Member(symbol, type, std::move(attributes))); ctx.dst->Member(symbol, ast_type, std::move(attributes)));
return ctx.dst->MemberAccessor(InputStructSymbol(), symbol); return ctx.dst->MemberAccessor(InputStructSymbol(), symbol);
} }
} }
@ -211,7 +212,7 @@ struct CanonicalizeEntryPointIO::State {
/// @param attributes the attributes to apply to the shader output /// @param attributes the attributes to apply to the shader output
/// @param value the value of the shader output /// @param value the value of the shader output
void AddOutput(std::string name, void AddOutput(std::string name,
ast::Type* type, sem::Type* type,
ast::DecorationList attributes, ast::DecorationList attributes,
ast::Expression* value) { ast::Expression* value) {
// Vulkan requires that integer user-defined vertex outputs are // Vulkan requires that integer user-defined vertex outputs are
@ -226,7 +227,7 @@ struct CanonicalizeEntryPointIO::State {
OutputValue output; OutputValue output;
output.name = name; output.name = name;
output.type = type; output.type = CreateASTTypeFor(ctx, type);
output.attributes = std::move(attributes); output.attributes = std::move(attributes);
output.value = value; output.value = value;
wrapper_output_values.push_back(output); wrapper_output_values.push_back(output);
@ -249,8 +250,7 @@ struct CanonicalizeEntryPointIO::State {
} }
auto name = ctx.src->Symbols().NameFor(param->Declaration()->symbol()); auto name = ctx.src->Symbols().NameFor(param->Declaration()->symbol());
auto* type = ctx.Clone(param->Declaration()->type()); auto* input_expr = AddInput(name, param->Type(), std::move(attributes));
auto* input_expr = AddInput(name, type, std::move(attributes));
inner_call_parameters.push_back(input_expr); inner_call_parameters.push_back(input_expr);
} }
@ -273,9 +273,8 @@ struct CanonicalizeEntryPointIO::State {
auto* member_ast = member->Declaration(); auto* member_ast = member->Declaration();
auto name = ctx.src->Symbols().NameFor(member_ast->symbol()); auto name = ctx.src->Symbols().NameFor(member_ast->symbol());
auto* type = ctx.Clone(member_ast->type());
auto attributes = CloneShaderIOAttributes(member_ast->decorations()); auto attributes = CloneShaderIOAttributes(member_ast->decorations());
auto* input_expr = AddInput(name, type, std::move(attributes)); auto* input_expr = AddInput(name, member->Type(), std::move(attributes));
inner_struct_values.push_back(input_expr); inner_struct_values.push_back(input_expr);
} }
@ -300,20 +299,18 @@ struct CanonicalizeEntryPointIO::State {
auto* member_ast = member->Declaration(); auto* member_ast = member->Declaration();
auto name = ctx.src->Symbols().NameFor(member_ast->symbol()); auto name = ctx.src->Symbols().NameFor(member_ast->symbol());
auto* type = ctx.Clone(member_ast->type());
auto attributes = CloneShaderIOAttributes(member_ast->decorations()); auto attributes = CloneShaderIOAttributes(member_ast->decorations());
// Extract the original structure member. // Extract the original structure member.
AddOutput(name, type, std::move(attributes), AddOutput(name, member->Type(), std::move(attributes),
ctx.dst->MemberAccessor(original_result, name)); ctx.dst->MemberAccessor(original_result, name));
} }
} else if (!inner_ret_type->Is<sem::Void>()) { } else if (!inner_ret_type->Is<sem::Void>()) {
auto* type = ctx.Clone(func_ast->return_type());
auto attributes = auto attributes =
CloneShaderIOAttributes(func_ast->return_type_decorations()); CloneShaderIOAttributes(func_ast->return_type_decorations());
// Propagate the non-struct return value as is. // Propagate the non-struct return value as is.
AddOutput("value", type, std::move(attributes), AddOutput("value", func_sem->ReturnType(), std::move(attributes),
ctx.dst->Expr(original_result)); ctx.dst->Expr(original_result));
} }
} }
@ -333,7 +330,7 @@ struct CanonicalizeEntryPointIO::State {
// No existing sample mask builtin was found, so create a new output value // No existing sample mask builtin was found, so create a new output value
// using the fixed sample mask. // using the fixed sample mask.
AddOutput("fixed_sample_mask", ctx.dst->ty.u32(), AddOutput("fixed_sample_mask", ctx.dst->create<sem::U32>(),
{ctx.dst->Builtin(ast::Builtin::kSampleMask)}, {ctx.dst->Builtin(ast::Builtin::kSampleMask)},
ctx.dst->Expr(cfg.fixed_sample_mask)); ctx.dst->Expr(cfg.fixed_sample_mask));
} }
@ -341,7 +338,7 @@ struct CanonicalizeEntryPointIO::State {
/// Add a point size builtin to the wrapper function output. /// Add a point size builtin to the wrapper function output.
void AddVertexPointSize() { void AddVertexPointSize() {
// Create a new output value and assign it a literal 1.0 value. // Create a new output value and assign it a literal 1.0 value.
AddOutput("vertex_point_size", ctx.dst->ty.f32(), AddOutput("vertex_point_size", ctx.dst->create<sem::F32>(),
{ctx.dst->Builtin(ast::Builtin::kPointSize)}, ctx.dst->Expr(1.f)); {ctx.dst->Builtin(ast::Builtin::kPointSize)}, ctx.dst->Expr(1.f));
} }

View File

@ -181,7 +181,7 @@ type myf32 = f32;
struct tint_symbol_1 { struct tint_symbol_1 {
[[location(1)]] [[location(1)]]
loc1 : myf32; loc1 : f32;
}; };
fn frag_main_inner(loc1 : myf32) { fn frag_main_inner(loc1 : myf32) {
@ -982,16 +982,16 @@ fn foo(x : MyFragmentInput) -> myf32 {
struct tint_symbol_1 { struct tint_symbol_1 {
[[location(0)]] [[location(0)]]
col1 : myf32; col1 : f32;
[[location(1)]] [[location(1)]]
col2 : myf32; col2 : f32;
}; };
struct tint_symbol_2 { struct tint_symbol_2 {
[[location(0)]] [[location(0)]]
col1 : myf32; col1 : f32;
[[location(1)]] [[location(1)]]
col2 : myf32; col2 : f32;
}; };
fn frag_main_inner(inputs : MyFragmentInput) -> MyFragmentOutput { fn frag_main_inner(inputs : MyFragmentInput) -> MyFragmentOutput {

View File

@ -228,7 +228,7 @@ struct ModuleScopeVarToEntryPointParam::State {
// Use a pointer for non-handle types. // Use a pointer for non-handle types.
auto* param_type = store_type(); auto* param_type = store_type();
ast::DecorationList attributes; ast::DecorationList attributes;
if (!param_type->is_handle()) { if (!var->Type()->UnwrapRef()->is_handle()) {
param_type = ctx.dst->ty.pointer(param_type, var->StorageClass()); param_type = ctx.dst->ty.pointer(param_type, var->StorageClass());
is_pointer = true; is_pointer = true;

View File

@ -84,7 +84,10 @@ TEST_F(AppendVectorTest, Vec2i32FromVec2u32_u32) {
auto* v2u32_to_v2i32 = auto* v2u32_to_v2i32 =
vec_123->values()[0]->As<ast::TypeConstructorExpression>(); vec_123->values()[0]->As<ast::TypeConstructorExpression>();
ASSERT_NE(v2u32_to_v2i32, nullptr); ASSERT_NE(v2u32_to_v2i32, nullptr);
EXPECT_TRUE(v2u32_to_v2i32->type()->is_signed_integer_vector()); ASSERT_TRUE(v2u32_to_v2i32->type()->Is<ast::Vector>());
EXPECT_EQ(v2u32_to_v2i32->type()->As<ast::Vector>()->size(), 2u);
EXPECT_TRUE(
v2u32_to_v2i32->type()->As<ast::Vector>()->type()->Is<ast::I32>());
EXPECT_EQ(v2u32_to_v2i32->values().size(), 1u); EXPECT_EQ(v2u32_to_v2i32->values().size(), 1u);
EXPECT_EQ(v2u32_to_v2i32->values()[0], uvec_12); EXPECT_EQ(v2u32_to_v2i32->values()[0], uvec_12);

View File

@ -1622,7 +1622,7 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
if (type->Is<sem::Struct>()) { if (type->Is<sem::Struct>()) {
out << " [[stage_in]]"; out << " [[stage_in]]";
} else if (var->type()->is_handle()) { } else if (type->is_handle()) {
auto bp = var->binding_point(); auto bp = var->binding_point();
if (bp.group == nullptr || bp.binding == nullptr) { if (bp.group == nullptr || bp.binding == nullptr) {
TINT_ICE(Writer, diagnostics_) TINT_ICE(Writer, diagnostics_)