writer/wgsl: Only generate from the AST types

Remove the legacy generation path that uses the semantic types.

Bug: tint:724
Change-Id: I38128ab2049cb1dcea333c24453d6636b0ebb8c6
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50300
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Ben Clayton 2021-05-07 15:05:44 +00:00 committed by Commit Bot service account
parent ba6ab5e6bd
commit fb13f025a3
2 changed files with 146 additions and 314 deletions

View File

@ -49,7 +49,7 @@
#include "src/ast/workgroup_decoration.h" #include "src/ast/workgroup_decoration.h"
#include "src/sem/access_control_type.h" #include "src/sem/access_control_type.h"
#include "src/sem/alias_type.h" #include "src/sem/alias_type.h"
#include "src/sem/array_type.h" #include "src/sem/array.h"
#include "src/sem/bool_type.h" #include "src/sem/bool_type.h"
#include "src/sem/depth_texture_type.h" #include "src/sem/depth_texture_type.h"
#include "src/sem/f32_type.h" #include "src/sem/f32_type.h"
@ -119,10 +119,9 @@ bool GeneratorImpl::Generate(const ast::Function* entry) {
return true; return true;
} }
bool GeneratorImpl::EmitConstructedType(typ::Type type) { bool GeneratorImpl::EmitConstructedType(const ast::Type* ty) {
make_indent(); make_indent();
if (auto* ty = type.ast) {
if (auto* alias = ty->As<ast::Alias>()) { if (auto* alias = ty->As<ast::Alias>()) {
out_ << "type " << program_->Symbols().NameFor(alias->symbol()) << " = "; out_ << "type " << program_->Symbols().NameFor(alias->symbol()) << " = ";
if (!EmitType(alias->type())) { if (!EmitType(alias->type())) {
@ -140,28 +139,6 @@ bool GeneratorImpl::EmitConstructedType(typ::Type type) {
return true; return true;
} }
if (auto* ty = type.sem) {
if (auto* alias = ty->As<sem::Alias>()) {
out_ << "type " << program_->Symbols().NameFor(alias->symbol()) << " = ";
if (!EmitType(alias->type())) {
return false;
}
out_ << ";" << std::endl;
} else if (auto* str = ty->As<sem::Struct>()) {
if (!EmitStructType(str->Declaration())) {
return false;
}
} else {
diagnostics_.add_error("unknown constructed type: " + ty->type_name());
return false;
}
return true;
}
TINT_ICE(diagnostics_) << "EmitConstructedType() called with nullptr";
return false;
}
bool GeneratorImpl::EmitExpression(ast::Expression* expr) { bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (auto* a = expr->As<ast::ArrayAccessorExpression>()) { if (auto* a = expr->As<ast::ArrayAccessorExpression>()) {
return EmitArrayAccessor(a); return EmitArrayAccessor(a);
@ -396,8 +373,7 @@ bool GeneratorImpl::EmitImageFormat(const ast::ImageFormat fmt) {
return true; return true;
} }
bool GeneratorImpl::EmitType(typ::Type type) { bool GeneratorImpl::EmitType(const ast::Type* ty) {
if (auto* ty = type.ast) {
if (auto* ac = ty->As<ast::AccessControl>()) { if (auto* ac = ty->As<ast::AccessControl>()) {
out_ << "[[access("; out_ << "[[access(";
if (ac->IsReadOnly()) { if (ac->IsReadOnly()) {
@ -539,150 +515,6 @@ bool GeneratorImpl::EmitType(typ::Type type) {
return true; return true;
} }
if (auto* ty = type.sem) {
if (auto* ac = ty->As<sem::AccessControl>()) {
out_ << "[[access(";
if (ac->IsReadOnly()) {
out_ << "read";
} else if (ac->IsWriteOnly()) {
out_ << "write";
} else if (ac->IsReadWrite()) {
out_ << "read_write";
} else {
diagnostics_.add_error("invalid access control");
return false;
}
out_ << ")]] ";
if (!EmitType(ac->type())) {
return false;
}
return true;
} else if (auto* alias = ty->As<sem::Alias>()) {
out_ << program_->Symbols().NameFor(alias->symbol());
} else if (auto* ary = ty->As<sem::ArrayType>()) {
for (auto* deco : ary->decorations()) {
if (auto* stride = deco->As<ast::StrideDecoration>()) {
out_ << "[[stride(" << stride->stride() << ")]] ";
}
}
out_ << "array<";
if (!EmitType(ary->type())) {
return false;
}
if (!ary->IsRuntimeArray())
out_ << ", " << ary->size();
out_ << ">";
} else if (ty->Is<sem::Bool>()) {
out_ << "bool";
} else if (ty->Is<sem::F32>()) {
out_ << "f32";
} else if (ty->Is<sem::I32>()) {
out_ << "i32";
} else if (auto* mat = ty->As<sem::Matrix>()) {
out_ << "mat" << mat->columns() << "x" << mat->rows() << "<";
if (!EmitType(mat->type())) {
return false;
}
out_ << ">";
} else if (auto* ptr = ty->As<sem::Pointer>()) {
out_ << "ptr<" << ptr->storage_class() << ", ";
if (!EmitType(ptr->type())) {
return false;
}
out_ << ">";
} else if (auto* sampler = ty->As<sem::Sampler>()) {
out_ << "sampler";
if (sampler->IsComparison()) {
out_ << "_comparison";
}
} else if (auto* str = ty->As<sem::Struct>()) {
// The struct, as a type, is just the name. We should have already emitted
// the declaration through a call to |EmitStructType| earlier.
out_ << program_->Symbols().NameFor(str->Declaration()->name());
} else if (auto* texture = ty->As<sem::Texture>()) {
out_ << "texture_";
if (texture->Is<sem::DepthTexture>()) {
out_ << "depth_";
} else if (texture->Is<sem::SampledTexture>()) {
/* nothing to emit */
} else if (texture->Is<sem::MultisampledTexture>()) {
out_ << "multisampled_";
} else if (texture->Is<sem::StorageTexture>()) {
out_ << "storage_";
} else {
diagnostics_.add_error("unknown texture type");
return false;
}
switch (texture->dim()) {
case ast::TextureDimension::k1d:
out_ << "1d";
break;
case ast::TextureDimension::k2d:
out_ << "2d";
break;
case ast::TextureDimension::k2dArray:
out_ << "2d_array";
break;
case ast::TextureDimension::k3d:
out_ << "3d";
break;
case ast::TextureDimension::kCube:
out_ << "cube";
break;
case ast::TextureDimension::kCubeArray:
out_ << "cube_array";
break;
default:
diagnostics_.add_error("unknown texture dimension");
return false;
}
if (auto* sampled = texture->As<sem::SampledTexture>()) {
out_ << "<";
if (!EmitType(sampled->type())) {
return false;
}
out_ << ">";
} else if (auto* ms = texture->As<sem::MultisampledTexture>()) {
out_ << "<";
if (!EmitType(ms->type())) {
return false;
}
out_ << ">";
} else if (auto* storage = texture->As<sem::StorageTexture>()) {
out_ << "<";
if (!EmitImageFormat(storage->image_format())) {
return false;
}
out_ << ">";
}
} else if (ty->Is<sem::U32>()) {
out_ << "u32";
} else if (auto* vec = ty->As<sem::Vector>()) {
out_ << "vec" << vec->size() << "<";
if (!EmitType(vec->type())) {
return false;
}
out_ << ">";
} else if (ty->Is<sem::Void>()) {
out_ << "void";
} else {
diagnostics_.add_error("unknown type in EmitType: " + ty->type_name());
return false;
}
return true;
}
TINT_ICE(diagnostics_) << "EmitType() called with nullptr";
return false;
}
bool GeneratorImpl::EmitStructType(const ast::Struct* str) { bool GeneratorImpl::EmitStructType(const ast::Struct* str) {
for (auto* deco : str->decorations()) { for (auto* deco : str->decorations()) {
out_ << "[["; out_ << "[[";

View File

@ -58,7 +58,7 @@ class GeneratorImpl : public TextGenerator {
/// Handles generating a constructed type /// Handles generating a constructed type
/// @param ty the constructed to generate /// @param ty the constructed to generate
/// @returns true if the constructed was emitted /// @returns true if the constructed was emitted
bool EmitConstructedType(typ::Type ty); bool EmitConstructedType(const ast::Type* ty);
/// Handles an array accessor expression /// Handles an array accessor expression
/// @param expr the expression to emit /// @param expr the expression to emit
/// @returns true if the array accessor was emitted /// @returns true if the array accessor was emitted
@ -166,7 +166,7 @@ class GeneratorImpl : public TextGenerator {
/// Handles generating type /// Handles generating type
/// @param type the type to generate /// @param type the type to generate
/// @returns true if the type is emitted /// @returns true if the type is emitted
bool EmitType(typ::Type type); bool EmitType(const ast::Type* type);
/// Handles generating a struct declaration /// Handles generating a struct declaration
/// @param str the struct /// @param str the struct
/// @returns true if the struct is emitted /// @returns true if the struct is emitted