Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame^] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "src/tint/transform/transform.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <string> |
| 19 | |
| 20 | #include "src/tint/program_builder.h" |
| 21 | #include "src/tint/sem/atomic_type.h" |
| 22 | #include "src/tint/sem/block_statement.h" |
| 23 | #include "src/tint/sem/depth_multisampled_texture_type.h" |
| 24 | #include "src/tint/sem/for_loop_statement.h" |
| 25 | #include "src/tint/sem/reference_type.h" |
| 26 | #include "src/tint/sem/sampler_type.h" |
| 27 | |
| 28 | TINT_INSTANTIATE_TYPEINFO(tint::transform::Transform); |
| 29 | TINT_INSTANTIATE_TYPEINFO(tint::transform::Data); |
| 30 | |
| 31 | namespace tint { |
| 32 | namespace transform { |
| 33 | |
| 34 | Data::Data() = default; |
| 35 | Data::Data(const Data&) = default; |
| 36 | Data::~Data() = default; |
| 37 | Data& Data::operator=(const Data&) = default; |
| 38 | |
| 39 | DataMap::DataMap() = default; |
| 40 | DataMap::DataMap(DataMap&&) = default; |
| 41 | DataMap::~DataMap() = default; |
| 42 | DataMap& DataMap::operator=(DataMap&&) = default; |
| 43 | |
| 44 | Output::Output() = default; |
| 45 | Output::Output(Program&& p) : program(std::move(p)) {} |
| 46 | Transform::Transform() = default; |
| 47 | Transform::~Transform() = default; |
| 48 | |
| 49 | Output Transform::Run(const Program* program, |
| 50 | const DataMap& data /* = {} */) const { |
| 51 | ProgramBuilder builder; |
| 52 | CloneContext ctx(&builder, program); |
| 53 | Output output; |
| 54 | Run(ctx, data, output.data); |
| 55 | output.program = Program(std::move(builder)); |
| 56 | return output; |
| 57 | } |
| 58 | |
| 59 | void Transform::Run(CloneContext& ctx, const DataMap&, DataMap&) const { |
| 60 | TINT_UNIMPLEMENTED(Transform, ctx.dst->Diagnostics()) |
| 61 | << "Transform::Run() unimplemented for " << TypeInfo().name; |
| 62 | } |
| 63 | |
| 64 | bool Transform::ShouldRun(const Program*, const DataMap&) const { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | void Transform::RemoveStatement(CloneContext& ctx, const ast::Statement* stmt) { |
| 69 | auto* sem = ctx.src->Sem().Get(stmt); |
| 70 | if (auto* block = tint::As<sem::BlockStatement>(sem->Parent())) { |
| 71 | ctx.Remove(block->Declaration()->statements, stmt); |
| 72 | return; |
| 73 | } |
| 74 | if (tint::Is<sem::ForLoopStatement>(sem->Parent())) { |
| 75 | ctx.Replace(stmt, static_cast<ast::Expression*>(nullptr)); |
| 76 | return; |
| 77 | } |
| 78 | TINT_ICE(Transform, ctx.dst->Diagnostics()) |
| 79 | << "unable to remove statement from parent of type " |
| 80 | << sem->TypeInfo().name; |
| 81 | } |
| 82 | |
| 83 | const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, |
| 84 | const sem::Type* ty) { |
| 85 | if (ty->Is<sem::Void>()) { |
| 86 | return ctx.dst->create<ast::Void>(); |
| 87 | } |
| 88 | if (ty->Is<sem::I32>()) { |
| 89 | return ctx.dst->create<ast::I32>(); |
| 90 | } |
| 91 | if (ty->Is<sem::U32>()) { |
| 92 | return ctx.dst->create<ast::U32>(); |
| 93 | } |
| 94 | if (ty->Is<sem::F32>()) { |
| 95 | return ctx.dst->create<ast::F32>(); |
| 96 | } |
| 97 | if (ty->Is<sem::Bool>()) { |
| 98 | return ctx.dst->create<ast::Bool>(); |
| 99 | } |
| 100 | if (auto* m = ty->As<sem::Matrix>()) { |
| 101 | auto* el = CreateASTTypeFor(ctx, m->type()); |
| 102 | return ctx.dst->create<ast::Matrix>(el, m->rows(), m->columns()); |
| 103 | } |
| 104 | if (auto* v = ty->As<sem::Vector>()) { |
| 105 | auto* el = CreateASTTypeFor(ctx, v->type()); |
| 106 | return ctx.dst->create<ast::Vector>(el, v->Width()); |
| 107 | } |
| 108 | if (auto* a = ty->As<sem::Array>()) { |
| 109 | auto* el = CreateASTTypeFor(ctx, a->ElemType()); |
| 110 | ast::AttributeList attrs; |
| 111 | if (!a->IsStrideImplicit()) { |
| 112 | attrs.emplace_back(ctx.dst->create<ast::StrideAttribute>(a->Stride())); |
| 113 | } |
| 114 | if (a->IsRuntimeSized()) { |
| 115 | return ctx.dst->ty.array(el, nullptr, std::move(attrs)); |
| 116 | } else { |
| 117 | return ctx.dst->ty.array(el, a->Count(), std::move(attrs)); |
| 118 | } |
| 119 | } |
| 120 | if (auto* s = ty->As<sem::Struct>()) { |
| 121 | return ctx.dst->create<ast::TypeName>(ctx.Clone(s->Declaration()->name)); |
| 122 | } |
| 123 | if (auto* s = ty->As<sem::Reference>()) { |
| 124 | return CreateASTTypeFor(ctx, s->StoreType()); |
| 125 | } |
| 126 | if (auto* a = ty->As<sem::Atomic>()) { |
| 127 | return ctx.dst->create<ast::Atomic>(CreateASTTypeFor(ctx, a->Type())); |
| 128 | } |
| 129 | if (auto* t = ty->As<sem::DepthTexture>()) { |
| 130 | return ctx.dst->create<ast::DepthTexture>(t->dim()); |
| 131 | } |
| 132 | if (auto* t = ty->As<sem::DepthMultisampledTexture>()) { |
| 133 | return ctx.dst->create<ast::DepthMultisampledTexture>(t->dim()); |
| 134 | } |
| 135 | if (ty->Is<sem::ExternalTexture>()) { |
| 136 | return ctx.dst->create<ast::ExternalTexture>(); |
| 137 | } |
| 138 | if (auto* t = ty->As<sem::MultisampledTexture>()) { |
| 139 | return ctx.dst->create<ast::MultisampledTexture>( |
| 140 | t->dim(), CreateASTTypeFor(ctx, t->type())); |
| 141 | } |
| 142 | if (auto* t = ty->As<sem::SampledTexture>()) { |
| 143 | return ctx.dst->create<ast::SampledTexture>( |
| 144 | t->dim(), CreateASTTypeFor(ctx, t->type())); |
| 145 | } |
| 146 | if (auto* t = ty->As<sem::StorageTexture>()) { |
| 147 | return ctx.dst->create<ast::StorageTexture>( |
| 148 | t->dim(), t->texel_format(), CreateASTTypeFor(ctx, t->type()), |
| 149 | t->access()); |
| 150 | } |
| 151 | if (auto* s = ty->As<sem::Sampler>()) { |
| 152 | return ctx.dst->create<ast::Sampler>(s->kind()); |
| 153 | } |
| 154 | TINT_UNREACHABLE(Transform, ctx.dst->Diagnostics()) |
| 155 | << "Unhandled type: " << ty->TypeInfo().name; |
| 156 | return nullptr; |
| 157 | } |
| 158 | |
| 159 | } // namespace transform |
| 160 | } // namespace tint |