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" |
Ben Clayton | 01004b7 | 2022-04-28 18:49:04 +0000 | [diff] [blame] | 21 | #include "src/tint/sem/atomic.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 22 | #include "src/tint/sem/block_statement.h" |
Ben Clayton | 01004b7 | 2022-04-28 18:49:04 +0000 | [diff] [blame] | 23 | #include "src/tint/sem/depth_multisampled_texture.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 24 | #include "src/tint/sem/for_loop_statement.h" |
Ben Clayton | 01004b7 | 2022-04-28 18:49:04 +0000 | [diff] [blame] | 25 | #include "src/tint/sem/reference.h" |
| 26 | #include "src/tint/sem/sampler.h" |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 27 | #include "src/tint/sem/variable.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 28 | |
| 29 | TINT_INSTANTIATE_TYPEINFO(tint::transform::Transform); |
| 30 | TINT_INSTANTIATE_TYPEINFO(tint::transform::Data); |
| 31 | |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 32 | namespace tint::transform { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 49 | Output Transform::Run(const Program* program, const DataMap& data /* = {} */) const { |
| 50 | ProgramBuilder builder; |
| 51 | CloneContext ctx(&builder, program); |
| 52 | Output output; |
| 53 | Run(ctx, data, output.data); |
| 54 | output.program = Program(std::move(builder)); |
| 55 | return output; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void Transform::Run(CloneContext& ctx, const DataMap&, DataMap&) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | TINT_UNIMPLEMENTED(Transform, ctx.dst->Diagnostics()) |
| 60 | << "Transform::Run() unimplemented for " << TypeInfo().name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | bool Transform::ShouldRun(const Program*, const DataMap&) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 64 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void Transform::RemoveStatement(CloneContext& ctx, const ast::Statement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 68 | auto* sem = ctx.src->Sem().Get(stmt); |
| 69 | if (auto* block = tint::As<sem::BlockStatement>(sem->Parent())) { |
| 70 | ctx.Remove(block->Declaration()->statements, stmt); |
| 71 | return; |
| 72 | } |
| 73 | if (tint::Is<sem::ForLoopStatement>(sem->Parent())) { |
| 74 | ctx.Replace(stmt, static_cast<ast::Expression*>(nullptr)); |
| 75 | return; |
| 76 | } |
| 77 | TINT_ICE(Transform, ctx.dst->Diagnostics()) |
| 78 | << "unable to remove statement from parent of type " << sem->TypeInfo().name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 79 | } |
| 80 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 81 | const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, const sem::Type* ty) { |
| 82 | if (ty->Is<sem::Void>()) { |
| 83 | return ctx.dst->create<ast::Void>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 84 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 85 | if (ty->Is<sem::I32>()) { |
| 86 | return ctx.dst->create<ast::I32>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 87 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 88 | if (ty->Is<sem::U32>()) { |
| 89 | return ctx.dst->create<ast::U32>(); |
| 90 | } |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 91 | if (ty->Is<sem::F16>()) { |
| 92 | return ctx.dst->create<ast::F16>(); |
| 93 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 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()); |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 110 | utils::Vector<const ast::Attribute*, 1> attrs; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 111 | if (!a->IsStrideImplicit()) { |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 112 | attrs.Push(ctx.dst->create<ast::StrideAttribute>(a->Stride())); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 113 | } |
| 114 | if (a->IsRuntimeSized()) { |
| 115 | return ctx.dst->ty.array(el, nullptr, std::move(attrs)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 116 | } |
Ben Clayton | 22c4850 | 2022-10-31 17:26:10 +0000 | [diff] [blame] | 117 | if (auto* override = std::get_if<sem::NamedOverrideArrayCount>(&a->Count())) { |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 118 | auto* count = ctx.Clone(override->variable->Declaration()); |
| 119 | return ctx.dst->ty.array(el, count, std::move(attrs)); |
| 120 | } |
Ben Clayton | 22c4850 | 2022-10-31 17:26:10 +0000 | [diff] [blame] | 121 | if (auto* override = std::get_if<sem::UnnamedOverrideArrayCount>(&a->Count())) { |
| 122 | auto* count = ctx.Clone(override->expr->Declaration()); |
| 123 | return ctx.dst->ty.array(el, count, std::move(attrs)); |
| 124 | } |
dan sinclair | 78f8067 | 2022-09-22 22:28:21 +0000 | [diff] [blame] | 125 | if (auto count = a->ConstantCount()) { |
| 126 | return ctx.dst->ty.array(el, u32(count.value()), std::move(attrs)); |
| 127 | } |
| 128 | TINT_ICE(Transform, ctx.dst->Diagnostics()) << sem::Array::kErrExpectedConstantCount; |
| 129 | return ctx.dst->ty.array(el, u32(1), std::move(attrs)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 130 | } |
| 131 | if (auto* s = ty->As<sem::Struct>()) { |
| 132 | return ctx.dst->create<ast::TypeName>(ctx.Clone(s->Declaration()->name)); |
| 133 | } |
| 134 | if (auto* s = ty->As<sem::Reference>()) { |
| 135 | return CreateASTTypeFor(ctx, s->StoreType()); |
| 136 | } |
| 137 | if (auto* a = ty->As<sem::Atomic>()) { |
| 138 | return ctx.dst->create<ast::Atomic>(CreateASTTypeFor(ctx, a->Type())); |
| 139 | } |
| 140 | if (auto* t = ty->As<sem::DepthTexture>()) { |
| 141 | return ctx.dst->create<ast::DepthTexture>(t->dim()); |
| 142 | } |
| 143 | if (auto* t = ty->As<sem::DepthMultisampledTexture>()) { |
| 144 | return ctx.dst->create<ast::DepthMultisampledTexture>(t->dim()); |
| 145 | } |
| 146 | if (ty->Is<sem::ExternalTexture>()) { |
| 147 | return ctx.dst->create<ast::ExternalTexture>(); |
| 148 | } |
| 149 | if (auto* t = ty->As<sem::MultisampledTexture>()) { |
| 150 | return ctx.dst->create<ast::MultisampledTexture>(t->dim(), |
| 151 | CreateASTTypeFor(ctx, t->type())); |
| 152 | } |
| 153 | if (auto* t = ty->As<sem::SampledTexture>()) { |
| 154 | return ctx.dst->create<ast::SampledTexture>(t->dim(), CreateASTTypeFor(ctx, t->type())); |
| 155 | } |
| 156 | if (auto* t = ty->As<sem::StorageTexture>()) { |
| 157 | return ctx.dst->create<ast::StorageTexture>(t->dim(), t->texel_format(), |
| 158 | CreateASTTypeFor(ctx, t->type()), t->access()); |
| 159 | } |
| 160 | if (auto* s = ty->As<sem::Sampler>()) { |
| 161 | return ctx.dst->create<ast::Sampler>(s->kind()); |
| 162 | } |
| 163 | TINT_UNREACHABLE(Transform, ctx.dst->Diagnostics()) |
| 164 | << "Unhandled type: " << ty->TypeInfo().name; |
| 165 | return nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 166 | } |
| 167 | |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 168 | } // namespace tint::transform |