From b90b6bff1d46056c7471fa39907d10b9fa6636d2 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 23 Aug 2022 16:23:05 +0000 Subject: [PATCH] tint: Minor no-op cleanup changes Change-Id: I91ad6044e7809ed2deab0fbd4417da90c7870d81 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/99705 Commit-Queue: Ben Clayton Kokoro: Kokoro Reviewed-by: Antonio Maiorano Commit-Queue: Ben Clayton --- src/tint/resolver/resolver.cc | 203 ++++++++++++----------- src/tint/transform/first_index_offset.cc | 2 +- src/tint/writer/glsl/generator_impl.cc | 34 +--- src/tint/writer/hlsl/generator_impl.cc | 36 ++-- src/tint/writer/msl/generator_impl.cc | 36 ++-- 5 files changed, 133 insertions(+), 178 deletions(-) diff --git a/src/tint/resolver/resolver.cc b/src/tint/resolver/resolver.cc index b32ca832c6..40156c941c 100644 --- a/src/tint/resolver/resolver.cc +++ b/src/tint/resolver/resolver.cc @@ -2183,121 +2183,124 @@ sem::Expression* Resolver::MemberAccessor(const ast::MemberAccessorExpression* e auto* object = sem_.Get(expr->structure); auto* source_var = object->SourceVariable(); - const sem::Type* ret = nullptr; - utils::Vector swizzle; + const sem::Type* ty = nullptr; // Object may be a side-effecting expression (e.g. function call). bool has_side_effects = object && object->HasSideEffects(); - if (auto* str = storage_ty->As()) { - Mark(expr->member); - auto symbol = expr->member->symbol; + return Switch( + storage_ty, // + [&](const sem::Struct* str) -> sem::Expression* { + Mark(expr->member); + auto symbol = expr->member->symbol; - const sem::StructMember* member = nullptr; - for (auto* m : str->Members()) { - if (m->Name() == symbol) { - ret = m->Type(); - member = m; - break; - } - } - - if (ret == nullptr) { - AddError("struct member " + builder_->Symbols().NameFor(symbol) + " not found", - expr->source); - return nullptr; - } - - // If we're extracting from a reference, we return a reference. - if (auto* ref = structure->As()) { - ret = builder_->create(ret, ref->StorageClass(), ref->Access()); - } - - const sem::Constant* val = nullptr; - if (auto r = const_eval_.MemberAccess(object, member)) { - val = r.Get(); - } else { - return nullptr; - } - return builder_->create(expr, ret, current_statement_, val, object, - member, has_side_effects, source_var); - } - - if (auto* vec = storage_ty->As()) { - Mark(expr->member); - std::string s = builder_->Symbols().NameFor(expr->member->symbol); - auto size = s.size(); - swizzle.Reserve(s.size()); - - for (auto c : s) { - switch (c) { - case 'x': - case 'r': - swizzle.Push(0u); + const sem::StructMember* member = nullptr; + for (auto* m : str->Members()) { + if (m->Name() == symbol) { + ty = m->Type(); + member = m; break; - case 'y': - case 'g': - swizzle.Push(1u); - break; - case 'z': - case 'b': - swizzle.Push(2u); - break; - case 'w': - case 'a': - swizzle.Push(3u); - break; - default: - AddError("invalid vector swizzle character", - expr->member->source.Begin() + swizzle.Length()); - return nullptr; + } } - if (swizzle.Back() >= vec->Width()) { - AddError("invalid vector swizzle member", expr->member->source); + if (ty == nullptr) { + AddError("struct member " + builder_->Symbols().NameFor(symbol) + " not found", + expr->source); return nullptr; } - } - if (size < 1 || size > 4) { - AddError("invalid vector swizzle size", expr->member->source); - return nullptr; - } - - // All characters are valid, check if they're being mixed - auto is_rgba = [](char c) { return c == 'r' || c == 'g' || c == 'b' || c == 'a'; }; - auto is_xyzw = [](char c) { return c == 'x' || c == 'y' || c == 'z' || c == 'w'; }; - if (!std::all_of(s.begin(), s.end(), is_rgba) && - !std::all_of(s.begin(), s.end(), is_xyzw)) { - AddError("invalid mixing of vector swizzle characters rgba with xyzw", - expr->member->source); - return nullptr; - } - - if (size == 1) { - // A single element swizzle is just the type of the vector. - ret = vec->type(); // If we're extracting from a reference, we return a reference. if (auto* ref = structure->As()) { - ret = builder_->create(ret, ref->StorageClass(), ref->Access()); + ty = builder_->create(ty, ref->StorageClass(), ref->Access()); } - } else { - // The vector will have a number of components equal to the length of - // the swizzle. - ret = builder_->create(vec->type(), static_cast(size)); - } - if (auto r = const_eval_.Swizzle(ret, object, swizzle)) { - auto* val = r.Get(); - return builder_->create(expr, ret, current_statement_, val, object, - std::move(swizzle), has_side_effects, source_var); - } - return nullptr; - } - AddError("invalid member accessor expression. Expected vector or struct, got '" + - sem_.TypeNameOf(storage_ty) + "'", - expr->structure->source); - return nullptr; + auto val = const_eval_.MemberAccess(object, member); + if (!val) { + return nullptr; + } + return builder_->create(expr, ty, current_statement_, + val.Get(), object, member, + has_side_effects, source_var); + }, + + [&](const sem::Vector* vec) -> sem::Expression* { + Mark(expr->member); + std::string s = builder_->Symbols().NameFor(expr->member->symbol); + auto size = s.size(); + utils::Vector swizzle; + swizzle.Reserve(s.size()); + + for (auto c : s) { + switch (c) { + case 'x': + case 'r': + swizzle.Push(0u); + break; + case 'y': + case 'g': + swizzle.Push(1u); + break; + case 'z': + case 'b': + swizzle.Push(2u); + break; + case 'w': + case 'a': + swizzle.Push(3u); + break; + default: + AddError("invalid vector swizzle character", + expr->member->source.Begin() + swizzle.Length()); + return nullptr; + } + + if (swizzle.Back() >= vec->Width()) { + AddError("invalid vector swizzle member", expr->member->source); + return nullptr; + } + } + + if (size < 1 || size > 4) { + AddError("invalid vector swizzle size", expr->member->source); + return nullptr; + } + + // All characters are valid, check if they're being mixed + auto is_rgba = [](char c) { return c == 'r' || c == 'g' || c == 'b' || c == 'a'; }; + auto is_xyzw = [](char c) { return c == 'x' || c == 'y' || c == 'z' || c == 'w'; }; + if (!std::all_of(s.begin(), s.end(), is_rgba) && + !std::all_of(s.begin(), s.end(), is_xyzw)) { + AddError("invalid mixing of vector swizzle characters rgba with xyzw", + expr->member->source); + return nullptr; + } + + if (size == 1) { + // A single element swizzle is just the type of the vector. + ty = vec->type(); + // If we're extracting from a reference, we return a reference. + if (auto* ref = structure->As()) { + ty = builder_->create(ty, ref->StorageClass(), ref->Access()); + } + } else { + // The vector will have a number of components equal to the length of + // the swizzle. + ty = builder_->create(vec->type(), static_cast(size)); + } + auto val = const_eval_.Swizzle(ty, object, swizzle); + if (!val) { + return nullptr; + } + return builder_->create(expr, ty, current_statement_, val.Get(), object, + std::move(swizzle), has_side_effects, source_var); + }, + + [&](Default) { + AddError("invalid member accessor expression. Expected vector or struct, got '" + + sem_.TypeNameOf(storage_ty) + "'", + expr->structure->source); + return nullptr; + }); } sem::Expression* Resolver::Binary(const ast::BinaryExpression* expr) { diff --git a/src/tint/transform/first_index_offset.cc b/src/tint/transform/first_index_offset.cc index ee80e951aa..34b481e3b9 100644 --- a/src/tint/transform/first_index_offset.cc +++ b/src/tint/transform/first_index_offset.cc @@ -145,7 +145,7 @@ void FirstIndexOffset::Run(CloneContext& ctx, const DataMap& inputs, DataMap& ou } } } - // Not interested in this experssion. Just clone. + // Not interested in this expression. Just clone. return nullptr; }); } diff --git a/src/tint/writer/glsl/generator_impl.cc b/src/tint/writer/glsl/generator_impl.cc index c66e1744b9..c2a667fbbb 100644 --- a/src/tint/writer/glsl/generator_impl.cc +++ b/src/tint/writer/glsl/generator_impl.cc @@ -1743,31 +1743,15 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, const ast::Expression* exp } } return Switch( - expr, - [&](const ast::IndexAccessorExpression* a) { // - return EmitIndexAccessor(out, a); - }, - [&](const ast::BinaryExpression* b) { // - return EmitBinary(out, b); - }, - [&](const ast::BitcastExpression* b) { // - return EmitBitcast(out, b); - }, - [&](const ast::CallExpression* c) { // - return EmitCall(out, c); - }, - [&](const ast::IdentifierExpression* i) { // - return EmitIdentifier(out, i); - }, - [&](const ast::LiteralExpression* l) { // - return EmitLiteral(out, l); - }, - [&](const ast::MemberAccessorExpression* m) { // - return EmitMemberAccessor(out, m); - }, - [&](const ast::UnaryOpExpression* u) { // - return EmitUnaryOp(out, u); - }, + expr, // + [&](const ast::IndexAccessorExpression* a) { return EmitIndexAccessor(out, a); }, + [&](const ast::BinaryExpression* b) { return EmitBinary(out, b); }, + [&](const ast::BitcastExpression* b) { return EmitBitcast(out, b); }, + [&](const ast::CallExpression* c) { return EmitCall(out, c); }, + [&](const ast::IdentifierExpression* i) { return EmitIdentifier(out, i); }, + [&](const ast::LiteralExpression* l) { return EmitLiteral(out, l); }, + [&](const ast::MemberAccessorExpression* m) { return EmitMemberAccessor(out, m); }, + [&](const ast::UnaryOpExpression* u) { return EmitUnaryOp(out, u); }, [&](Default) { // diagnostics_.add_error(diag::System::Writer, "unknown expression type: " + std::string(expr->TypeInfo().name)); diff --git a/src/tint/writer/hlsl/generator_impl.cc b/src/tint/writer/hlsl/generator_impl.cc index 444746cc0d..a7367dcbda 100644 --- a/src/tint/writer/hlsl/generator_impl.cc +++ b/src/tint/writer/hlsl/generator_impl.cc @@ -2631,32 +2631,16 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, const ast::Expression* exp } } return Switch( - expr, - [&](const ast::IndexAccessorExpression* a) { // - return EmitIndexAccessor(out, a); - }, - [&](const ast::BinaryExpression* b) { // - return EmitBinary(out, b); - }, - [&](const ast::BitcastExpression* b) { // - return EmitBitcast(out, b); - }, - [&](const ast::CallExpression* c) { // - return EmitCall(out, c); - }, - [&](const ast::IdentifierExpression* i) { // - return EmitIdentifier(out, i); - }, - [&](const ast::LiteralExpression* l) { // - return EmitLiteral(out, l); - }, - [&](const ast::MemberAccessorExpression* m) { // - return EmitMemberAccessor(out, m); - }, - [&](const ast::UnaryOpExpression* u) { // - return EmitUnaryOp(out, u); - }, - [&](Default) { // + expr, // + [&](const ast::IndexAccessorExpression* a) { return EmitIndexAccessor(out, a); }, + [&](const ast::BinaryExpression* b) { return EmitBinary(out, b); }, + [&](const ast::BitcastExpression* b) { return EmitBitcast(out, b); }, + [&](const ast::CallExpression* c) { return EmitCall(out, c); }, + [&](const ast::IdentifierExpression* i) { return EmitIdentifier(out, i); }, + [&](const ast::LiteralExpression* l) { return EmitLiteral(out, l); }, + [&](const ast::MemberAccessorExpression* m) { return EmitMemberAccessor(out, m); }, + [&](const ast::UnaryOpExpression* u) { return EmitUnaryOp(out, u); }, + [&](Default) { diagnostics_.add_error(diag::System::Writer, "unknown expression type: " + std::string(expr->TypeInfo().name)); return false; diff --git a/src/tint/writer/msl/generator_impl.cc b/src/tint/writer/msl/generator_impl.cc index cccf622402..837f9ac3c6 100644 --- a/src/tint/writer/msl/generator_impl.cc +++ b/src/tint/writer/msl/generator_impl.cc @@ -1764,36 +1764,20 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression* bool GeneratorImpl::EmitExpression(std::ostream& out, const ast::Expression* expr) { if (auto* sem = builder_.Sem().Get(expr)) { - if (auto constant = sem->ConstantValue()) { + if (auto* constant = sem->ConstantValue()) { return EmitConstant(out, constant); } } return Switch( - expr, - [&](const ast::IndexAccessorExpression* a) { // - return EmitIndexAccessor(out, a); - }, - [&](const ast::BinaryExpression* b) { // - return EmitBinary(out, b); - }, - [&](const ast::BitcastExpression* b) { // - return EmitBitcast(out, b); - }, - [&](const ast::CallExpression* c) { // - return EmitCall(out, c); - }, - [&](const ast::IdentifierExpression* i) { // - return EmitIdentifier(out, i); - }, - [&](const ast::LiteralExpression* l) { // - return EmitLiteral(out, l); - }, - [&](const ast::MemberAccessorExpression* m) { // - return EmitMemberAccessor(out, m); - }, - [&](const ast::UnaryOpExpression* u) { // - return EmitUnaryOp(out, u); - }, + expr, // + [&](const ast::IndexAccessorExpression* a) { return EmitIndexAccessor(out, a); }, + [&](const ast::BinaryExpression* b) { return EmitBinary(out, b); }, + [&](const ast::BitcastExpression* b) { return EmitBitcast(out, b); }, + [&](const ast::CallExpression* c) { return EmitCall(out, c); }, + [&](const ast::IdentifierExpression* i) { return EmitIdentifier(out, i); }, + [&](const ast::LiteralExpression* l) { return EmitLiteral(out, l); }, + [&](const ast::MemberAccessorExpression* m) { return EmitMemberAccessor(out, m); }, + [&](const ast::UnaryOpExpression* u) { return EmitUnaryOp(out, u); }, [&](Default) { // diagnostics_.add_error(diag::System::Writer, "unknown expression type: " + std::string(expr->TypeInfo().name));