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" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 27 | |
| 28 | TINT_INSTANTIATE_TYPEINFO(tint::transform::Transform); |
| 29 | TINT_INSTANTIATE_TYPEINFO(tint::transform::Data); |
| 30 | |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 31 | namespace tint::transform { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 32 | |
| 33 | Data::Data() = default; |
| 34 | Data::Data(const Data&) = default; |
| 35 | Data::~Data() = default; |
| 36 | Data& Data::operator=(const Data&) = default; |
| 37 | |
| 38 | DataMap::DataMap() = default; |
| 39 | DataMap::DataMap(DataMap&&) = default; |
| 40 | DataMap::~DataMap() = default; |
| 41 | DataMap& DataMap::operator=(DataMap&&) = default; |
| 42 | |
| 43 | Output::Output() = default; |
| 44 | Output::Output(Program&& p) : program(std::move(p)) {} |
| 45 | Transform::Transform() = default; |
| 46 | Transform::~Transform() = default; |
| 47 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | Output Transform::Run(const Program* program, const DataMap& data /* = {} */) const { |
| 49 | ProgramBuilder builder; |
| 50 | CloneContext ctx(&builder, program); |
| 51 | Output output; |
| 52 | Run(ctx, data, output.data); |
| 53 | output.program = Program(std::move(builder)); |
| 54 | return output; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void Transform::Run(CloneContext& ctx, const DataMap&, DataMap&) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 58 | TINT_UNIMPLEMENTED(Transform, ctx.dst->Diagnostics()) |
| 59 | << "Transform::Run() unimplemented for " << TypeInfo().name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool Transform::ShouldRun(const Program*, const DataMap&) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 63 | return true; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void Transform::RemoveStatement(CloneContext& ctx, const ast::Statement* stmt) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 67 | auto* sem = ctx.src->Sem().Get(stmt); |
| 68 | if (auto* block = tint::As<sem::BlockStatement>(sem->Parent())) { |
| 69 | ctx.Remove(block->Declaration()->statements, stmt); |
| 70 | return; |
| 71 | } |
| 72 | if (tint::Is<sem::ForLoopStatement>(sem->Parent())) { |
| 73 | ctx.Replace(stmt, static_cast<ast::Expression*>(nullptr)); |
| 74 | return; |
| 75 | } |
| 76 | TINT_ICE(Transform, ctx.dst->Diagnostics()) |
| 77 | << "unable to remove statement from parent of type " << sem->TypeInfo().name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 78 | } |
| 79 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 80 | const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, const sem::Type* ty) { |
| 81 | if (ty->Is<sem::Void>()) { |
| 82 | return ctx.dst->create<ast::Void>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 84 | if (ty->Is<sem::I32>()) { |
| 85 | return ctx.dst->create<ast::I32>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 86 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 87 | if (ty->Is<sem::U32>()) { |
| 88 | return ctx.dst->create<ast::U32>(); |
| 89 | } |
| 90 | if (ty->Is<sem::F32>()) { |
| 91 | return ctx.dst->create<ast::F32>(); |
| 92 | } |
| 93 | if (ty->Is<sem::Bool>()) { |
| 94 | return ctx.dst->create<ast::Bool>(); |
| 95 | } |
| 96 | if (auto* m = ty->As<sem::Matrix>()) { |
| 97 | auto* el = CreateASTTypeFor(ctx, m->type()); |
| 98 | return ctx.dst->create<ast::Matrix>(el, m->rows(), m->columns()); |
| 99 | } |
| 100 | if (auto* v = ty->As<sem::Vector>()) { |
| 101 | auto* el = CreateASTTypeFor(ctx, v->type()); |
| 102 | return ctx.dst->create<ast::Vector>(el, v->Width()); |
| 103 | } |
| 104 | if (auto* a = ty->As<sem::Array>()) { |
| 105 | auto* el = CreateASTTypeFor(ctx, a->ElemType()); |
| 106 | ast::AttributeList attrs; |
| 107 | if (!a->IsStrideImplicit()) { |
| 108 | attrs.emplace_back(ctx.dst->create<ast::StrideAttribute>(a->Stride())); |
| 109 | } |
| 110 | if (a->IsRuntimeSized()) { |
| 111 | return ctx.dst->ty.array(el, nullptr, std::move(attrs)); |
| 112 | } else { |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame^] | 113 | return ctx.dst->ty.array(el, u32(a->Count()), std::move(attrs)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | if (auto* s = ty->As<sem::Struct>()) { |
| 117 | return ctx.dst->create<ast::TypeName>(ctx.Clone(s->Declaration()->name)); |
| 118 | } |
| 119 | if (auto* s = ty->As<sem::Reference>()) { |
| 120 | return CreateASTTypeFor(ctx, s->StoreType()); |
| 121 | } |
| 122 | if (auto* a = ty->As<sem::Atomic>()) { |
| 123 | return ctx.dst->create<ast::Atomic>(CreateASTTypeFor(ctx, a->Type())); |
| 124 | } |
| 125 | if (auto* t = ty->As<sem::DepthTexture>()) { |
| 126 | return ctx.dst->create<ast::DepthTexture>(t->dim()); |
| 127 | } |
| 128 | if (auto* t = ty->As<sem::DepthMultisampledTexture>()) { |
| 129 | return ctx.dst->create<ast::DepthMultisampledTexture>(t->dim()); |
| 130 | } |
| 131 | if (ty->Is<sem::ExternalTexture>()) { |
| 132 | return ctx.dst->create<ast::ExternalTexture>(); |
| 133 | } |
| 134 | if (auto* t = ty->As<sem::MultisampledTexture>()) { |
| 135 | return ctx.dst->create<ast::MultisampledTexture>(t->dim(), |
| 136 | CreateASTTypeFor(ctx, t->type())); |
| 137 | } |
| 138 | if (auto* t = ty->As<sem::SampledTexture>()) { |
| 139 | return ctx.dst->create<ast::SampledTexture>(t->dim(), CreateASTTypeFor(ctx, t->type())); |
| 140 | } |
| 141 | if (auto* t = ty->As<sem::StorageTexture>()) { |
| 142 | return ctx.dst->create<ast::StorageTexture>(t->dim(), t->texel_format(), |
| 143 | CreateASTTypeFor(ctx, t->type()), t->access()); |
| 144 | } |
| 145 | if (auto* s = ty->As<sem::Sampler>()) { |
| 146 | return ctx.dst->create<ast::Sampler>(s->kind()); |
| 147 | } |
| 148 | TINT_UNREACHABLE(Transform, ctx.dst->Diagnostics()) |
| 149 | << "Unhandled type: " << ty->TypeInfo().name; |
| 150 | return nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 151 | } |
| 152 | |
dan sinclair | b5599d3 | 2022-04-07 16:55:14 +0000 | [diff] [blame] | 153 | } // namespace tint::transform |