From fb13f025a34bb3926903779f5ae224b9f4385a38 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 7 May 2021 15:05:44 +0000 Subject: [PATCH] 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 Kokoro: Kokoro Reviewed-by: Antonio Maiorano --- src/writer/wgsl/generator_impl.cc | 456 ++++++++++-------------------- src/writer/wgsl/generator_impl.h | 4 +- 2 files changed, 146 insertions(+), 314 deletions(-) diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 46a16e7c4b..2030f7bfa7 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -49,7 +49,7 @@ #include "src/ast/workgroup_decoration.h" #include "src/sem/access_control_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/depth_texture_type.h" #include "src/sem/f32_type.h" @@ -119,47 +119,24 @@ bool GeneratorImpl::Generate(const ast::Function* entry) { return true; } -bool GeneratorImpl::EmitConstructedType(typ::Type type) { +bool GeneratorImpl::EmitConstructedType(const ast::Type* ty) { make_indent(); - if (auto* ty = type.ast) { - if (auto* alias = ty->As()) { - out_ << "type " << program_->Symbols().NameFor(alias->symbol()) << " = "; - if (!EmitType(alias->type())) { - return false; - } - out_ << ";" << std::endl; - } else if (auto* str = ty->As()) { - if (!EmitStructType(str)) { - return false; - } - } else { - diagnostics_.add_error("unknown constructed type: " + ty->type_name()); + if (auto* alias = ty->As()) { + out_ << "type " << program_->Symbols().NameFor(alias->symbol()) << " = "; + if (!EmitType(alias->type())) { return false; } - return true; - } - - if (auto* ty = type.sem) { - if (auto* alias = ty->As()) { - out_ << "type " << program_->Symbols().NameFor(alias->symbol()) << " = "; - if (!EmitType(alias->type())) { - return false; - } - out_ << ";" << std::endl; - } else if (auto* str = ty->As()) { - if (!EmitStructType(str->Declaration())) { - return false; - } - } else { - diagnostics_.add_error("unknown constructed type: " + ty->type_name()); + out_ << ";" << std::endl; + } else if (auto* str = ty->As()) { + if (!EmitStructType(str)) { return false; } - return true; + } else { + diagnostics_.add_error("unknown constructed type: " + ty->type_name()); + return false; } - - TINT_ICE(diagnostics_) << "EmitConstructedType() called with nullptr"; - return false; + return true; } bool GeneratorImpl::EmitExpression(ast::Expression* expr) { @@ -396,291 +373,146 @@ bool GeneratorImpl::EmitImageFormat(const ast::ImageFormat fmt) { return true; } -bool GeneratorImpl::EmitType(typ::Type type) { - if (auto* ty = type.ast) { - if (auto* ac = ty->As()) { - 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()) { - out_ << program_->Symbols().NameFor(alias->symbol()); - } else if (auto* ary = ty->As()) { - for (auto* deco : ary->decorations()) { - if (auto* stride = deco->As()) { - out_ << "[[stride(" << stride->stride() << ")]] "; - } - } - - out_ << "array<"; - if (!EmitType(ary->type())) { - return false; - } - - if (!ary->IsRuntimeArray()) - out_ << ", " << ary->size(); - - out_ << ">"; - } else if (ty->Is()) { - out_ << "bool"; - } else if (ty->Is()) { - out_ << "f32"; - } else if (ty->Is()) { - out_ << "i32"; - } else if (auto* mat = ty->As()) { - out_ << "mat" << mat->columns() << "x" << mat->rows() << "<"; - if (!EmitType(mat->type())) { - return false; - } - out_ << ">"; - } else if (auto* ptr = ty->As()) { - out_ << "ptr<" << ptr->storage_class() << ", "; - if (!EmitType(ptr->type())) { - return false; - } - out_ << ">"; - } else if (auto* sampler = ty->As()) { - out_ << "sampler"; - - if (sampler->IsComparison()) { - out_ << "_comparison"; - } - } else if (auto* str = ty->As()) { - // 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->name()); - } else if (auto* texture = ty->As()) { - out_ << "texture_"; - if (texture->Is()) { - out_ << "depth_"; - } else if (texture->Is()) { - /* nothing to emit */ - } else if (texture->Is()) { - out_ << "multisampled_"; - } else if (texture->Is()) { - 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()) { - out_ << "<"; - if (!EmitType(sampled->type())) { - return false; - } - out_ << ">"; - } else if (auto* ms = texture->As()) { - out_ << "<"; - if (!EmitType(ms->type())) { - return false; - } - out_ << ">"; - } else if (auto* storage = texture->As()) { - out_ << "<"; - if (!EmitImageFormat(storage->image_format())) { - return false; - } - out_ << ">"; - } - - } else if (ty->Is()) { - out_ << "u32"; - } else if (auto* vec = ty->As()) { - out_ << "vec" << vec->size() << "<"; - if (!EmitType(vec->type())) { - return false; - } - out_ << ">"; - } else if (ty->Is()) { - out_ << "void"; - } else if (auto* tn = ty->As()) { - out_ << program_->Symbols().NameFor(tn->name()); +bool GeneratorImpl::EmitType(const ast::Type* ty) { + if (auto* ac = ty->As()) { + out_ << "[[access("; + if (ac->IsReadOnly()) { + out_ << "read"; + } else if (ac->IsWriteOnly()) { + out_ << "write"; + } else if (ac->IsReadWrite()) { + out_ << "read_write"; } else { - diagnostics_.add_error("unknown type in EmitType: " + ty->type_name()); + diagnostics_.add_error("invalid access control"); + return false; + } + out_ << ")]] "; + if (!EmitType(ac->type())) { return false; } return true; - } + } else if (auto* alias = ty->As()) { + out_ << program_->Symbols().NameFor(alias->symbol()); + } else if (auto* ary = ty->As()) { + for (auto* deco : ary->decorations()) { + if (auto* stride = deco->As()) { + out_ << "[[stride(" << stride->stride() << ")]] "; + } + } - if (auto* ty = type.sem) { - if (auto* ac = ty->As()) { - 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()) { - out_ << program_->Symbols().NameFor(alias->symbol()); - } else if (auto* ary = ty->As()) { - for (auto* deco : ary->decorations()) { - if (auto* stride = deco->As()) { - out_ << "[[stride(" << stride->stride() << ")]] "; - } - } - - out_ << "array<"; - if (!EmitType(ary->type())) { - return false; - } - - if (!ary->IsRuntimeArray()) - out_ << ", " << ary->size(); - - out_ << ">"; - } else if (ty->Is()) { - out_ << "bool"; - } else if (ty->Is()) { - out_ << "f32"; - } else if (ty->Is()) { - out_ << "i32"; - } else if (auto* mat = ty->As()) { - out_ << "mat" << mat->columns() << "x" << mat->rows() << "<"; - if (!EmitType(mat->type())) { - return false; - } - out_ << ">"; - } else if (auto* ptr = ty->As()) { - out_ << "ptr<" << ptr->storage_class() << ", "; - if (!EmitType(ptr->type())) { - return false; - } - out_ << ">"; - } else if (auto* sampler = ty->As()) { - out_ << "sampler"; - - if (sampler->IsComparison()) { - out_ << "_comparison"; - } - } else if (auto* str = ty->As()) { - // 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()) { - out_ << "texture_"; - if (texture->Is()) { - out_ << "depth_"; - } else if (texture->Is()) { - /* nothing to emit */ - } else if (texture->Is()) { - out_ << "multisampled_"; - } else if (texture->Is()) { - 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()) { - out_ << "<"; - if (!EmitType(sampled->type())) { - return false; - } - out_ << ">"; - } else if (auto* ms = texture->As()) { - out_ << "<"; - if (!EmitType(ms->type())) { - return false; - } - out_ << ">"; - } else if (auto* storage = texture->As()) { - out_ << "<"; - if (!EmitImageFormat(storage->image_format())) { - return false; - } - out_ << ">"; - } - - } else if (ty->Is()) { - out_ << "u32"; - } else if (auto* vec = ty->As()) { - out_ << "vec" << vec->size() << "<"; - if (!EmitType(vec->type())) { - return false; - } - out_ << ">"; - } else if (ty->Is()) { - out_ << "void"; - } else { - diagnostics_.add_error("unknown type in EmitType: " + ty->type_name()); + out_ << "array<"; + if (!EmitType(ary->type())) { return false; } - return true; - } - TINT_ICE(diagnostics_) << "EmitType() called with nullptr"; - return false; + if (!ary->IsRuntimeArray()) + out_ << ", " << ary->size(); + + out_ << ">"; + } else if (ty->Is()) { + out_ << "bool"; + } else if (ty->Is()) { + out_ << "f32"; + } else if (ty->Is()) { + out_ << "i32"; + } else if (auto* mat = ty->As()) { + out_ << "mat" << mat->columns() << "x" << mat->rows() << "<"; + if (!EmitType(mat->type())) { + return false; + } + out_ << ">"; + } else if (auto* ptr = ty->As()) { + out_ << "ptr<" << ptr->storage_class() << ", "; + if (!EmitType(ptr->type())) { + return false; + } + out_ << ">"; + } else if (auto* sampler = ty->As()) { + out_ << "sampler"; + + if (sampler->IsComparison()) { + out_ << "_comparison"; + } + } else if (auto* str = ty->As()) { + // 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->name()); + } else if (auto* texture = ty->As()) { + out_ << "texture_"; + if (texture->Is()) { + out_ << "depth_"; + } else if (texture->Is()) { + /* nothing to emit */ + } else if (texture->Is()) { + out_ << "multisampled_"; + } else if (texture->Is()) { + 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()) { + out_ << "<"; + if (!EmitType(sampled->type())) { + return false; + } + out_ << ">"; + } else if (auto* ms = texture->As()) { + out_ << "<"; + if (!EmitType(ms->type())) { + return false; + } + out_ << ">"; + } else if (auto* storage = texture->As()) { + out_ << "<"; + if (!EmitImageFormat(storage->image_format())) { + return false; + } + out_ << ">"; + } + + } else if (ty->Is()) { + out_ << "u32"; + } else if (auto* vec = ty->As()) { + out_ << "vec" << vec->size() << "<"; + if (!EmitType(vec->type())) { + return false; + } + out_ << ">"; + } else if (ty->Is()) { + out_ << "void"; + } else if (auto* tn = ty->As()) { + out_ << program_->Symbols().NameFor(tn->name()); + } else { + diagnostics_.add_error("unknown type in EmitType: " + ty->type_name()); + return false; + } + return true; } bool GeneratorImpl::EmitStructType(const ast::Struct* str) { diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index 96636122cf..e7ed3debea 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -58,7 +58,7 @@ class GeneratorImpl : public TextGenerator { /// Handles generating a constructed type /// @param ty the constructed to generate /// @returns true if the constructed was emitted - bool EmitConstructedType(typ::Type ty); + bool EmitConstructedType(const ast::Type* ty); /// Handles an array accessor expression /// @param expr the expression to emit /// @returns true if the array accessor was emitted @@ -166,7 +166,7 @@ class GeneratorImpl : public TextGenerator { /// Handles generating type /// @param type the type to generate /// @returns true if the type is emitted - bool EmitType(typ::Type type); + bool EmitType(const ast::Type* type); /// Handles generating a struct declaration /// @param str the struct /// @returns true if the struct is emitted